Skip to content

Commit

Permalink
when writing uninit to an allocation, also clear relocations like oth…
Browse files Browse the repository at this point in the history
…er writes do
  • Loading branch information
RalfJung committed Apr 17, 2022
1 parent ad4e98e commit 85ee04c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
13 changes: 9 additions & 4 deletions compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,11 @@ impl<'tcx, 'a, Tag: Provenance, Extra> AllocRefMut<'a, 'tcx, Tag, Extra> {
}

/// Mark the entire referenced range as uninitalized
pub fn write_uninit(&mut self) {
self.alloc.mark_init(self.range, false);
pub fn write_uninit(&mut self) -> InterpResult<'tcx> {
Ok(self
.alloc
.write_uninit(&self.tcx, self.range)
.map_err(|e| e.to_interp_error(self.alloc_id))?)
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ where
// Zero-sized access
return Ok(());
};
alloc.write_uninit();
alloc.write_uninit()?;
Ok(())
}

Expand Down
12 changes: 9 additions & 3 deletions compiler/rustc_middle/src/mir/interpret/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,7 @@ impl<Tag: Provenance, Extra> Allocation<Tag, Extra> {
let val = match val {
ScalarMaybeUninit::Scalar(scalar) => scalar,
ScalarMaybeUninit::Uninit => {
self.mark_init(range, false);
return Ok(());
return self.write_uninit(cx, range);
}
};

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

Ok(())
}

/// Write "uninit" to the given memory range.
pub fn write_uninit(&mut self, cx: &impl HasDataLayout, range: AllocRange) -> AllocResult {
self.mark_init(range, false);
self.clear_relocations(cx, range)?;
return Ok(());
}
}

/// Relocations.
Expand Down Expand Up @@ -1056,7 +1062,7 @@ impl<Tag: Copy, Extra> Allocation<Tag, Extra> {
})
}

pub fn mark_init(&mut self, range: AllocRange, is_init: bool) {
fn mark_init(&mut self, range: AllocRange, is_init: bool) {
if range.size.bytes() == 0 {
return;
}
Expand Down

0 comments on commit 85ee04c

Please sign in to comment.