Skip to content

Commit 486720f

Browse files
committed
fix determinig the size of foreign static allocations
1 parent 5612feb commit 486720f

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/librustc_mir/interpret/memory.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::collections::VecDeque;
1010
use std::ptr;
1111
use std::borrow::Cow;
1212

13-
use rustc::ty::{self, Instance, query::TyCtxtAt};
13+
use rustc::ty::{self, Instance, ParamEnv, query::TyCtxtAt};
1414
use rustc::ty::layout::{Align, TargetDataLayout, Size, HasDataLayout};
1515
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1616

@@ -536,19 +536,33 @@ impl<'mir, 'tcx, M: Machine<'mir, 'tcx>> Memory<'mir, 'tcx, M> {
536536
) -> InterpResult<'static, (Size, Align)> {
537537
// Regular allocations.
538538
if let Ok(alloc) = self.get(id) {
539-
Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align))
539+
return Ok((Size::from_bytes(alloc.bytes.len() as u64), alloc.align));
540540
}
541541
// Function pointers.
542-
else if let Ok(_) = self.get_fn_alloc(id) {
543-
if let AllocCheck::Dereferencable = liveness {
542+
if let Ok(_) = self.get_fn_alloc(id) {
543+
return if let AllocCheck::Dereferencable = liveness {
544544
// The caller requested no function pointers.
545545
err!(DerefFunctionPointer)
546546
} else {
547547
Ok((Size::ZERO, Align::from_bytes(1).unwrap()))
548+
};
549+
}
550+
// Foreign statics.
551+
// Can't do this in the match argument, we may get cycle errors since the lock would
552+
// be held throughout the match.
553+
let alloc = self.tcx.alloc_map.lock().get(id);
554+
match alloc {
555+
Some(GlobalAlloc::Static(did)) => {
556+
assert!(self.tcx.is_foreign_item(did));
557+
// Use size and align of the type
558+
let ty = self.tcx.type_of(did);
559+
let layout = self.tcx.layout_of(ParamEnv::empty().and(ty)).unwrap();
560+
return Ok((layout.size, layout.align.abi));
548561
}
562+
_ => {}
549563
}
550564
// The rest must be dead.
551-
else if let AllocCheck::MaybeDead = liveness {
565+
if let AllocCheck::MaybeDead = liveness {
552566
// Deallocated pointers are allowed, we should be able to find
553567
// them in the map.
554568
Ok(*self.dead_alloc_map.get(&id)

0 commit comments

Comments
 (0)