Skip to content

Commit db0d4b6

Browse files
committedJun 22, 2022
Auto merge of #2241 - RalfJung:extern_static_in_const, r=RalfJung
fix ICE when const refers to extern static Fixes #2234 Needs rust-lang/rust#98099
2 parents cbb649a + b29a706 commit db0d4b6

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed
 

‎rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cdcc53b7dc002ea4a7a28105010c5a1126ee31b7
1+
a09c668c965f735f4cd59e7158662b9daa0b71ba

‎src/machine.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
618618
id: AllocId,
619619
alloc: Cow<'b, Allocation>,
620620
kind: Option<MemoryKind<Self::MemoryKind>>,
621-
) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>> {
621+
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>> {
622622
if ecx.machine.tracked_alloc_ids.contains(&id) {
623623
register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
624624
}
@@ -653,15 +653,28 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
653653
data_race: race_alloc,
654654
weak_memory: buffer_alloc,
655655
},
656-
|ptr| Evaluator::tag_alloc_base_pointer(ecx, ptr),
657-
);
658-
Cow::Owned(alloc)
656+
|ptr| ecx.global_base_pointer(ptr),
657+
)?;
658+
Ok(Cow::Owned(alloc))
659659
}
660660

661661
fn tag_alloc_base_pointer(
662662
ecx: &MiriEvalContext<'mir, 'tcx>,
663663
ptr: Pointer<AllocId>,
664664
) -> Pointer<Tag> {
665+
if cfg!(debug_assertions) {
666+
// The machine promises to never call us on thread-local or extern statics.
667+
let alloc_id = ptr.provenance;
668+
match ecx.tcx.get_global_alloc(alloc_id) {
669+
Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_thread_local_static(def_id) => {
670+
panic!("tag_alloc_base_pointer called on thread-local static")
671+
}
672+
Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_foreign_item(def_id) => {
673+
panic!("tag_alloc_base_pointer called on extern static")
674+
}
675+
_ => {}
676+
}
677+
}
665678
let absolute_addr = intptrcast::GlobalStateInner::rel_ptr_to_addr(ecx, ptr);
666679
let sb_tag = if let Some(stacked_borrows) = &ecx.machine.stacked_borrows {
667680
stacked_borrows.borrow_mut().base_tag(ptr.provenance)

‎src/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
587587
// This allocation will be deallocated when the thread dies, so it is not in read-only memory.
588588
allocation.mutability = Mutability::Mut;
589589
// Create a fresh allocation with this content.
590-
let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into());
590+
let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into())?;
591591
this.machine.threads.set_thread_local_alloc(def_id, new_alloc);
592592
Ok(new_alloc)
593593
}

‎tests/fail/extern_static_in_const.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Even referencing an unknown `extern static` already triggers an error.
2+
3+
extern "C" {
4+
static E: [u8; 0];
5+
}
6+
7+
static X: &'static [u8; 0] = unsafe { &E };
8+
9+
fn main() {
10+
let _val = X; //~ ERROR is not supported by Miri
11+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: unsupported operation: `extern` static `E` from crate `extern_static_in_const` is not supported by Miri
2+
--> $DIR/extern_static_in_const.rs:LL:CC
3+
|
4+
LL | let _val = X;
5+
| ^ `extern` static `E` from crate `extern_static_in_const` is not supported by Miri
6+
|
7+
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
8+
9+
= note: inside `main` at $DIR/extern_static_in_const.rs:LL:CC
10+
11+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
12+
13+
error: aborting due to previous error
14+

0 commit comments

Comments
 (0)
Please sign in to comment.