Skip to content

Commit bd874a9

Browse files
committed
make check_ptr_access_align work on function pointer allocations
1 parent adbe755 commit bd874a9

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

compiler/rustc_mir/src/interpret/memory.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
232232
M::GLOBAL_KIND.map(MemoryKind::Machine),
233233
"dynamically allocating global memory"
234234
);
235-
let alloc =
236-
M::init_allocation_extra(self, id, Cow::Owned(alloc), Some(kind));
235+
let alloc = M::init_allocation_extra(self, id, Cow::Owned(alloc), Some(kind));
237236
self.alloc_map.insert(id, (kind, alloc.into_owned()));
238237
M::tag_alloc_base_pointer(self, Pointer::from(id))
239238
}
@@ -372,7 +371,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
372371
)
373372
}
374373

375-
/// Check if the given pointer is allowed to do a memory access of given `size` and `align`
374+
/// Check if the given pointerpoints to live memory of given `size` and `align`
376375
/// (ignoring `M::enforce_alignment`). The caller can control the error message for the
377376
/// out-of-bounds case.
378377
#[inline(always)]
@@ -384,7 +383,15 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
384383
msg: CheckInAllocMsg,
385384
) -> InterpResult<'tcx> {
386385
self.check_and_deref_ptr(ptr, size, Some(align), msg, |alloc_id, _, _| {
387-
let (size, align) = self.get_size_and_align(alloc_id, AllocCheck::Dereferenceable)?;
386+
let check = match msg {
387+
CheckInAllocMsg::DerefTest | CheckInAllocMsg::MemoryAccessTest => {
388+
AllocCheck::Dereferenceable
389+
}
390+
CheckInAllocMsg::PointerArithmeticTest | CheckInAllocMsg::InboundsTest => {
391+
AllocCheck::Live
392+
}
393+
};
394+
let (size, align) = self.get_size_and_align(alloc_id, check)?;
388395
Ok((size, align, ()))
389396
})?;
390397
Ok(())
@@ -551,8 +558,7 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
551558
// `get_global_alloc` that we can actually use directly without inserting anything anywhere.
552559
// So the error type is `InterpResult<'tcx, &Allocation<M::PointerTag>>`.
553560
let a = self.alloc_map.get_or(id, || {
554-
let alloc = self.get_global_alloc(id, /*is_write*/ false)
555-
.map_err(Err)?;
561+
let alloc = self.get_global_alloc(id, /*is_write*/ false).map_err(Err)?;
556562
match alloc {
557563
Cow::Borrowed(alloc) => {
558564
// We got a ref, cheaply return that as an "error" so that the

0 commit comments

Comments
 (0)