Skip to content

Commit 003f741

Browse files
committed
expose params on prepare_for_native_write
1 parent e8a1e53 commit 003f741

File tree

2 files changed

+18
-18
lines changed

2 files changed

+18
-18
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,12 +1020,10 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10201020
// Also expose the provenance of the interpreter-level allocation, so it can
10211021
// be read by FFI. The `black_box` is defensive programming as LLVM likes
10221022
// to (incorrectly) optimize away ptr2int casts whose result is unused.
1023-
if paranoid {
1024-
std::hint::black_box(alloc.get_bytes_unchecked_raw().expose_provenance());
1025-
// Prepare for possible write from native code if mutable.
1026-
if info.mutbl.is_mut() {
1027-
self.get_alloc_raw_mut(id)?.0.prepare_for_native_write();
1028-
}
1023+
std::hint::black_box(alloc.get_bytes_unchecked_raw().expose_provenance());
1024+
// Prepare for possible write from native code if mutable.
1025+
if info.mutbl.is_mut() {
1026+
self.get_alloc_raw_mut(id)?.0.prepare_for_native_write(paranoid);
10291027
}
10301028
}
10311029
interp_ok(())
@@ -1041,15 +1039,15 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10411039
pub fn apply_accesses(
10421040
&mut self,
10431041
mut ids: Vec<AllocId>,
1044-
reads: Vec<std::ops::Range<u64>>,
1045-
writes: Vec<std::ops::Range<u64>>,
1042+
reads: Vec<std::ops::Range<usize>>,
1043+
writes: Vec<std::ops::Range<usize>>,
10461044
) -> InterpResult<'tcx> {
10471045
/// Helper function to avoid some code duplication over range overlaps.
10481046
fn get_start_size(
1049-
rg: std::ops::Range<u64>,
1050-
alloc_base: u64,
1051-
alloc_size: u64,
1052-
) -> Option<(u64, u64)> {
1047+
rg: std::ops::Range<usize>,
1048+
alloc_base: usize,
1049+
alloc_size: usize,
1050+
) -> Option<(usize, usize)> {
10531051
// A bunch of range bounds nonsense that effectively simplifies to
10541052
// "get the starting point of the overlap and the length from there".
10551053
// Needs to also account for the allocation being in the middle of the
@@ -1061,7 +1059,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10611059
} else {
10621060
rg.end - rg.start
10631061
};
1064-
let start: u64 = signed_start.try_into().unwrap_or(0);
1062+
let start: usize = signed_start.try_into().unwrap_or(0);
10651063
let size = std::cmp::min(size_uncapped, alloc_size - start);
10661064
Some((start, size))
10671065
}
@@ -1078,13 +1076,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
10781076
continue;
10791077
}
10801078

1081-
let alloc_base: u64 = {
1079+
let alloc_base: usize = {
10821080
// Keep the alloc here so the borrow checker is happy
10831081
let alloc = self.get_alloc_raw(id)?;
10841082
// No need for black_box trickery since we actually use the address
1085-
alloc.get_bytes_unchecked_raw().expose_provenance().try_into().unwrap()
1083+
alloc.get_bytes_unchecked_raw().expose_provenance()
10861084
};
1087-
let alloc_size = info.size.bytes();
1085+
let alloc_size = info.size.bytes().try_into().unwrap();
10881086

10891087
// Find reads which overlap with the current allocation
10901088
for rg in &reads {

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
799799
/// Initialize all previously uninitialized bytes in the entire allocation, and set
800800
/// provenance of everything to `Wildcard`. Before calling this, make sure all
801801
/// provenance in this allocation is exposed!
802-
pub fn prepare_for_native_write(&mut self) {
802+
pub fn prepare_for_native_write(&mut self, paranoid: bool) {
803803
let full_range = AllocRange { start: Size::ZERO, size: Size::from_bytes(self.len()) };
804804
// Overwrite uninitialized bytes with 0, to ensure we don't leak whatever their value happens to be.
805805
for chunk in self.init_mask.range_as_init_chunks(full_range) {
@@ -809,7 +809,9 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
809809
uninit_bytes.fill(0);
810810
}
811811
}
812-
self.mark_foreign_write(full_range);
812+
if paranoid {
813+
self.mark_foreign_write(full_range);
814+
}
813815
}
814816

815817
/// Initialise previously uninitialised bytes in the given range, and set provenance of

0 commit comments

Comments
 (0)