Skip to content

Commit 85ee04c

Browse files
committed
when writing uninit to an allocation, also clear relocations like other writes do
1 parent ad4e98e commit 85ee04c

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

compiler/rustc_const_eval/src/interpret/memory.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,11 @@ impl<'tcx, 'a, Tag: Provenance, Extra> AllocRefMut<'a, 'tcx, Tag, Extra> {
892892
}
893893

894894
/// Mark the entire referenced range as uninitalized
895-
pub fn write_uninit(&mut self) {
896-
self.alloc.mark_init(self.range, false);
895+
pub fn write_uninit(&mut self) -> InterpResult<'tcx> {
896+
Ok(self
897+
.alloc
898+
.write_uninit(&self.tcx, self.range)
899+
.map_err(|e| e.to_interp_error(self.alloc_id))?)
897900
}
898901
}
899902

@@ -1053,8 +1056,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10531056
// This also avoids writing to the target bytes so that the backing allocation is never
10541057
// touched if the bytes stay uninitialized for the whole interpreter execution. On contemporary
10551058
// operating system this can avoid physically allocating the page.
1056-
dest_alloc.mark_init(dest_range, false); // `Size` multiplication
1057-
dest_alloc.mark_relocation_range(relocations);
1059+
dest_alloc
1060+
.write_uninit(&tcx, dest_range)
1061+
.map_err(|e| e.to_interp_error(dest_alloc_id))?; // `Size` multiplication
1062+
// We can forget about the relocations, this is all not initialized anyway.
10581063
return Ok(());
10591064
}
10601065

compiler/rustc_const_eval/src/interpret/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ where
823823
// Zero-sized access
824824
return Ok(());
825825
};
826-
alloc.write_uninit();
826+
alloc.write_uninit()?;
827827
Ok(())
828828
}
829829

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,7 @@ impl<Tag: Provenance, Extra> Allocation<Tag, Extra> {
429429
let val = match val {
430430
ScalarMaybeUninit::Scalar(scalar) => scalar,
431431
ScalarMaybeUninit::Uninit => {
432-
self.mark_init(range, false);
433-
return Ok(());
432+
return self.write_uninit(cx, range);
434433
}
435434
};
436435

@@ -455,6 +454,13 @@ impl<Tag: Provenance, Extra> Allocation<Tag, Extra> {
455454

456455
Ok(())
457456
}
457+
458+
/// Write "uninit" to the given memory range.
459+
pub fn write_uninit(&mut self, cx: &impl HasDataLayout, range: AllocRange) -> AllocResult {
460+
self.mark_init(range, false);
461+
self.clear_relocations(cx, range)?;
462+
return Ok(());
463+
}
458464
}
459465

460466
/// Relocations.
@@ -1056,7 +1062,7 @@ impl<Tag: Copy, Extra> Allocation<Tag, Extra> {
10561062
})
10571063
}
10581064

1059-
pub fn mark_init(&mut self, range: AllocRange, is_init: bool) {
1065+
fn mark_init(&mut self, range: AllocRange, is_init: bool) {
10601066
if range.size.bytes() == 0 {
10611067
return;
10621068
}

0 commit comments

Comments
 (0)