Skip to content

Commit b49fb76

Browse files
committed
miri realloc: do not require giving old size+align
1 parent 6ea4036 commit b49fb76

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/librustc_mir/interpret/memory.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
158158
pub fn reallocate(
159159
&mut self,
160160
ptr: Pointer<M::PointerTag>,
161-
old_size: Size,
162-
old_align: Align,
161+
old_size_and_align: Option<(Size, Align)>,
163162
new_size: Size,
164163
new_align: Align,
165164
kind: MemoryKind<M::MemoryKinds>,
@@ -171,15 +170,19 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
171170
// For simplicities' sake, we implement reallocate as "alloc, copy, dealloc".
172171
// This happens so rarely, the perf advantage is outweighed by the maintenance cost.
173172
let new_ptr = self.allocate(new_size, new_align, kind);
173+
let old_size = match old_size_and_align {
174+
Some((size, _align)) => size,
175+
None => Size::from_bytes(self.get(ptr.alloc_id)?.bytes.len() as u64),
176+
};
174177
self.copy(
175178
ptr.into(),
176-
old_align,
179+
Align::from_bytes(1).unwrap(), // old_align anyway gets checked below by `deallocate`
177180
new_ptr.into(),
178181
new_align,
179182
old_size.min(new_size),
180183
/*nonoverlapping*/ true,
181184
)?;
182-
self.deallocate(ptr, Some((old_size, old_align)), kind)?;
185+
self.deallocate(ptr, old_size_and_align, kind)?;
183186

184187
Ok(new_ptr)
185188
}
@@ -198,7 +201,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
198201
pub fn deallocate(
199202
&mut self,
200203
ptr: Pointer<M::PointerTag>,
201-
size_and_align: Option<(Size, Align)>,
204+
old_size_and_align: Option<(Size, Align)>,
202205
kind: MemoryKind<M::MemoryKinds>,
203206
) -> InterpResult<'tcx> {
204207
trace!("deallocating: {}", ptr.alloc_id);
@@ -232,7 +235,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
232235
format!("{:?}", kind),
233236
));
234237
}
235-
if let Some((size, align)) = size_and_align {
238+
if let Some((size, align)) = old_size_and_align {
236239
if size.bytes() != alloc.bytes.len() as u64 || align != alloc.align {
237240
let bytes = Size::from_bytes(alloc.bytes.len() as u64);
238241
return err!(IncorrectAllocationInformation(size,

0 commit comments

Comments
 (0)