Skip to content

Commit c119291

Browse files
committed
get_size_and_align: fix handling of function pointers
1 parent 828e7b6 commit c119291

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/librustc_mir/interpret/memory.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,26 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
535535
id: AllocId,
536536
liveness: AllocCheck,
537537
) -> InterpResult<'static, (Size, Align)> {
538+
// # Regular allocations
538539
// Don't use `self.get` here as that will
539540
// a) cause cycles in case `id` refers to a static
540541
// b) duplicate a static's allocation in miri
541542
if let Some((_, alloc)) = self.alloc_map.get(id) {
542543
return Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align));
543544
}
544-
// Not a local allocation, check the global `tcx.alloc_map`.
545545

546+
// # Function pointers
547+
// (both global from `alloc_map` and local from `extra_fn_ptr_map`)
548+
if let Ok(_) = self.get_fn_alloc(id) {
549+
return if let AllocCheck::Dereferencable = liveness {
550+
// The caller requested no function pointers.
551+
err!(DerefFunctionPointer)
552+
} else {
553+
Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
554+
};
555+
}
556+
557+
// # Statics
546558
// Can't do this in the match argument, we may get cycle errors since the lock would
547559
// be held throughout the match.
548560
let alloc = self.tcx.alloc_map.lock().get(id);
@@ -557,14 +569,8 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
557569
// Need to duplicate the logic here, because the global allocations have
558570
// different associated types than the interpreter-local ones.
559571
Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align)),
560-
Some(GlobalAlloc::Function(_)) => {
561-
if let AllocCheck::Dereferencable = liveness {
562-
// The caller requested no function pointers.
563-
err!(DerefFunctionPointer)
564-
} else {
565-
Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
566-
}
567-
},
572+
Some(GlobalAlloc::Function(_)) =>
573+
bug!("We already checked function pointers above"),
568574
// The rest must be dead.
569575
None => if let AllocCheck::MaybeDead = liveness {
570576
// Deallocated pointers are allowed, we should be able to find

0 commit comments

Comments
 (0)