Skip to content

Commit

Permalink
Rollup merge of rust-lang#62295 - RalfJung:miri-realloc, r=cramertj
Browse files Browse the repository at this point in the history
miri realloc: do not require giving old size+align
  • Loading branch information
Centril authored Jul 3, 2019
2 parents 3bd6234 + b49fb76 commit a368388
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/librustc_mir/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
pub fn reallocate(
&mut self,
ptr: Pointer<M::PointerTag>,
old_size: Size,
old_align: Align,
old_size_and_align: Option<(Size, Align)>,
new_size: Size,
new_align: Align,
kind: MemoryKind<M::MemoryKinds>,
Expand All @@ -171,15 +170,19 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
// This happens so rarely, the perf advantage is outweighed by the maintenance cost.
let new_ptr = self.allocate(new_size, new_align, kind);
let old_size = match old_size_and_align {
Some((size, _align)) => size,
None => Size::from_bytes(self.get(ptr.alloc_id)?.bytes.len() as u64),
};
self.copy(
ptr.into(),
old_align,
Align::from_bytes(1).unwrap(), // old_align anyway gets checked below by `deallocate`
new_ptr.into(),
new_align,
old_size.min(new_size),
/*nonoverlapping*/ true,
)?;
self.deallocate(ptr, Some((old_size, old_align)), kind)?;
self.deallocate(ptr, old_size_and_align, kind)?;

Ok(new_ptr)
}
Expand All @@ -198,7 +201,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
pub fn deallocate(
&mut self,
ptr: Pointer<M::PointerTag>,
size_and_align: Option<(Size, Align)>,
old_size_and_align: Option<(Size, Align)>,
kind: MemoryKind<M::MemoryKinds>,
) -> InterpResult<'tcx> {
trace!("deallocating: {}", ptr.alloc_id);
Expand Down Expand Up @@ -232,7 +235,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
format!("{:?}", kind),
));
}
if let Some((size, align)) = size_and_align {
if let Some((size, align)) = old_size_and_align {
if size.bytes() != alloc.bytes.len() as u64 || align != alloc.align {
let bytes = Size::from_bytes(alloc.bytes.len() as u64);
return err!(IncorrectAllocationInformation(size,
Expand Down

0 comments on commit a368388

Please sign in to comment.