Skip to content

Commit 9fa15ff

Browse files
authored
Rollup merge of #85472 - RalfJung:allocation, r=oli-obk
CTFE Machine: do not expose Allocation `Memory` is careful now to not expose direct access to `Allocation`, but this one slipped through. r? ``@oli-obk``
2 parents b2becf0 + 50a9f00 commit 9fa15ff

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

compiler/rustc_mir/src/interpret/machine.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
313313
#[inline(always)]
314314
fn memory_read(
315315
_memory_extra: &Self::MemoryExtra,
316-
_alloc: &Allocation<Self::PointerTag, Self::AllocExtra>,
316+
_alloc_extra: &Self::AllocExtra,
317317
_ptr: Pointer<Self::PointerTag>,
318318
_size: Size,
319319
) -> InterpResult<'tcx> {
@@ -324,7 +324,7 @@ pub trait Machine<'mir, 'tcx>: Sized {
324324
#[inline(always)]
325325
fn memory_written(
326326
_memory_extra: &mut Self::MemoryExtra,
327-
_alloc: &mut Allocation<Self::PointerTag, Self::AllocExtra>,
327+
_alloc_extra: &mut Self::AllocExtra,
328328
_ptr: Pointer<Self::PointerTag>,
329329
_size: Size,
330330
) -> InterpResult<'tcx> {
@@ -335,8 +335,9 @@ pub trait Machine<'mir, 'tcx>: Sized {
335335
#[inline(always)]
336336
fn memory_deallocated(
337337
_memory_extra: &mut Self::MemoryExtra,
338-
_alloc: &mut Allocation<Self::PointerTag, Self::AllocExtra>,
338+
_alloc_extra: &mut Self::AllocExtra,
339339
_ptr: Pointer<Self::PointerTag>,
340+
_size: Size,
340341
) -> InterpResult<'tcx> {
341342
Ok(())
342343
}

compiler/rustc_mir/src/interpret/memory.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,11 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
343343
}
344344

345345
// Let the machine take some extra action
346-
M::memory_deallocated(&mut self.extra, &mut alloc, ptr)?;
346+
let size = alloc.size();
347+
M::memory_deallocated(&mut self.extra, &mut alloc.extra, ptr, size)?;
347348

348349
// Don't forget to remember size and align of this now-dead allocation
349-
let old = self.dead_alloc_map.insert(ptr.alloc_id, (alloc.size(), alloc.align));
350+
let old = self.dead_alloc_map.insert(ptr.alloc_id, (size, alloc.align));
350351
if old.is_some() {
351352
bug!("Nothing can be deallocated twice");
352353
}
@@ -591,7 +592,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
591592
},
592593
)?;
593594
if let Some((ptr, alloc)) = ptr_and_alloc {
594-
M::memory_read(&self.extra, alloc, ptr, size)?;
595+
M::memory_read(&self.extra, &alloc.extra, ptr, size)?;
595596
let range = alloc_range(ptr.offset, size);
596597
Ok(Some(AllocRef { alloc, range, tcx: self.tcx, alloc_id: ptr.alloc_id }))
597598
} else {
@@ -660,7 +661,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
660661
// FIXME: can we somehow avoid looking up the allocation twice here?
661662
// We cannot call `get_raw_mut` inside `check_and_deref_ptr` as that would duplicate `&mut self`.
662663
let (alloc, extra) = self.get_raw_mut(ptr.alloc_id)?;
663-
M::memory_written(extra, alloc, ptr, size)?;
664+
M::memory_written(extra, &mut alloc.extra, ptr, size)?;
664665
let range = alloc_range(ptr.offset, size);
665666
Ok(Some(AllocRefMut { alloc, range, tcx, alloc_id: ptr.alloc_id }))
666667
} else {
@@ -1029,7 +1030,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
10291030
Some(src_ptr) => src_ptr,
10301031
};
10311032
let src_alloc = self.get_raw(src.alloc_id)?;
1032-
M::memory_read(&self.extra, src_alloc, src, size)?;
1033+
M::memory_read(&self.extra, &src_alloc.extra, src, size)?;
10331034
// We need the `dest` ptr for the next operation, so we get it now.
10341035
// We already did the source checks and called the hooks so we are good to return early.
10351036
let dest = match dest {
@@ -1058,7 +1059,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
10581059

10591060
// Destination alloc preparations and access hooks.
10601061
let (dest_alloc, extra) = self.get_raw_mut(dest.alloc_id)?;
1061-
M::memory_written(extra, dest_alloc, dest, size * num_copies)?;
1062+
M::memory_written(extra, &mut dest_alloc.extra, dest, size * num_copies)?;
10621063
let dest_bytes = dest_alloc
10631064
.get_bytes_mut_ptr(&tcx, alloc_range(dest.offset, size * num_copies))
10641065
.as_mut_ptr();

0 commit comments

Comments
 (0)