Skip to content

Commit 33a6382

Browse files
committed
review
1 parent 8afb088 commit 33a6382

File tree

3 files changed

+20
-32
lines changed

3 files changed

+20
-32
lines changed

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -791,30 +791,16 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes>
791791

792792
/// Write "uninit" to the given memory range.
793793
pub fn write_uninit(&mut self, cx: &impl HasDataLayout, range: AllocRange) -> AllocResult {
794+
self.bytes[range.start.bytes_usize()..range.end().bytes_usize()].fill(0);
794795
self.mark_init(range, false);
795796
self.provenance.clear(range, cx)?;
796797
Ok(())
797798
}
798799

799-
/// Initialize all previously uninitialized bytes in the entire allocation, but
800-
/// do not actually mark them as init. Before calling this, make sure all
801-
/// provenance in this allocation is exposed!
802-
pub fn prepare_for_native_access(&mut self) {
803-
let full_range = AllocRange { start: Size::ZERO, size: Size::from_bytes(self.len()) };
804-
// Overwrite uninitialized bytes with 0, to ensure we don't leak whatever their value happens to be.
805-
for chunk in self.init_mask.range_as_init_chunks(full_range) {
806-
if !chunk.is_init() {
807-
let uninit_bytes = &mut self.bytes
808-
[chunk.range().start.bytes_usize()..chunk.range().end.bytes_usize()];
809-
uninit_bytes.fill(0);
810-
}
811-
}
812-
}
813-
814800
/// Mark all bytes in the given range as initialised and reset the provenance
815801
/// to wildcards. This entirely breaks the normal mechanisms for tracking
816802
/// initialisation and is only provided for Miri operating in native-lib
817-
/// mode. UB will be missed if the underlying bytes were not actually initialized.
803+
/// mode. UB will be missed if the underlying bytes were not actually written to.
818804
///
819805
/// If `range` is `None`, defaults to performing this on the whole allocation.
820806
pub fn process_native_write(&mut self, cx: &impl HasDataLayout, range: Option<AllocRange>) {

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
213213
}
214214

215215
/// Overwrites all provenance in the given range with wildcard provenance.
216-
/// Pointers partially overwritten will have their provenances
216+
/// Pointers partially overwritten will have their provenances preserved
217+
/// bytewise on their remaining bytes.
217218
///
218219
/// Provided for usage in Miri and panics otherwise.
219220
pub fn write_wildcards(&mut self, cx: &impl HasDataLayout, range: AllocRange) {
@@ -223,25 +224,27 @@ impl<Prov: Provenance> ProvenanceMap<Prov> {
223224
);
224225
let wildcard = Prov::WILDCARD.unwrap();
225226

226-
// Get pointer provenances that overlap with the range, then remove them.
227+
let bytes = self.bytes.get_or_insert_with(Box::default);
228+
229+
// Remove pointer provenances that overlap with the range, then readd the edge ones bytewise.
227230
let ptr_range = Self::adjusted_range_ptrs(range, cx);
228-
let removed = ptr_range.filter_map(|i| self.ptrs.remove(&i).map(|p| (i, p)));
231+
let ptrs = self.ptrs.range(ptr_range.clone());
232+
if let Some((offset, prov)) = ptrs.first() {
233+
for byte_ofs in *offset..range.start {
234+
bytes.insert(byte_ofs, *prov);
235+
}
236+
}
237+
if let Some((offset, prov)) = ptrs.last() {
238+
for byte_ofs in range.end()..*offset + cx.data_layout().pointer_size() {
239+
bytes.insert(byte_ofs, *prov);
240+
}
241+
}
242+
self.ptrs.remove_range(ptr_range);
243+
229244
// Overwrite bytewise provenance.
230-
let bytes = self.bytes.get_or_insert_with(Box::default);
231245
for offset in range.start..range.end() {
232246
bytes.insert(offset, wildcard);
233247
}
234-
// Now readd pointer provenances that were only partly overwritten. Always
235-
// check both the start and end to allow for a write to happen in the middle
236-
// of a pointer.
237-
for (rm_ofs, prov) in removed {
238-
for head_ofs in rm_ofs..range.start {
239-
bytes.insert(head_ofs, prov);
240-
}
241-
for tail_ofs in range.end()..rm_ofs + cx.data_layout().pointer_size() {
242-
bytes.insert(tail_ofs, prov);
243-
}
244-
}
245248
}
246249
}
247250

src/tools/miri/src/shims/native_lib/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
252252
// Prepare for possible write from native code if mutable.
253253
if info.mutbl.is_mut() {
254254
let (alloc, cx) = this.get_alloc_raw_mut(alloc_id)?;
255-
alloc.prepare_for_native_access();
256255
alloc.process_native_write(&cx.tcx, None);
257256
// Also expose *mutable* provenance for the interpreter-level allocation.
258257
std::hint::black_box(alloc.get_bytes_unchecked_raw_mut().expose_provenance());

0 commit comments

Comments
 (0)