Auto merge of #2241 - RalfJung:extern_static_in_const, r=RalfJung

fix ICE when const refers to extern static

Fixes https://github.com/rust-lang/miri/issues/2234
Needs https://github.com/rust-lang/rust/pull/98099
This commit is contained in:
bors 2022-06-22 04:04:37 +00:00
commit db0d4b68ba
5 changed files with 44 additions and 6 deletions

View File

@ -1 +1 @@
cdcc53b7dc002ea4a7a28105010c5a1126ee31b7
a09c668c965f735f4cd59e7158662b9daa0b71ba

View File

@ -618,7 +618,7 @@ fn init_allocation_extra<'b>(
id: AllocId,
alloc: Cow<'b, Allocation>,
kind: Option<MemoryKind<Self::MemoryKind>>,
) -> Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>> {
) -> InterpResult<'tcx, Cow<'b, Allocation<Self::PointerTag, Self::AllocExtra>>> {
if ecx.machine.tracked_alloc_ids.contains(&id) {
register_diagnostic(NonHaltingDiagnostic::CreatedAlloc(id));
}
@ -653,15 +653,28 @@ fn init_allocation_extra<'b>(
data_race: race_alloc,
weak_memory: buffer_alloc,
},
|ptr| Evaluator::tag_alloc_base_pointer(ecx, ptr),
);
Cow::Owned(alloc)
|ptr| ecx.global_base_pointer(ptr),
)?;
Ok(Cow::Owned(alloc))
}
fn tag_alloc_base_pointer(
ecx: &MiriEvalContext<'mir, 'tcx>,
ptr: Pointer<AllocId>,
) -> Pointer<Tag> {
if cfg!(debug_assertions) {
// The machine promises to never call us on thread-local or extern statics.
let alloc_id = ptr.provenance;
match ecx.tcx.get_global_alloc(alloc_id) {
Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_thread_local_static(def_id) => {
panic!("tag_alloc_base_pointer called on thread-local static")
}
Some(GlobalAlloc::Static(def_id)) if ecx.tcx.is_foreign_item(def_id) => {
panic!("tag_alloc_base_pointer called on extern static")
}
_ => {}
}
}
let absolute_addr = intptrcast::GlobalStateInner::rel_ptr_to_addr(ecx, ptr);
let sb_tag = if let Some(stacked_borrows) = &ecx.machine.stacked_borrows {
stacked_borrows.borrow_mut().base_tag(ptr.provenance)

View File

@ -587,7 +587,7 @@ fn get_or_create_thread_local_alloc(
// This allocation will be deallocated when the thread dies, so it is not in read-only memory.
allocation.mutability = Mutability::Mut;
// Create a fresh allocation with this content.
let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into());
let new_alloc = this.allocate_raw_ptr(allocation, MiriMemoryKind::Tls.into())?;
this.machine.threads.set_thread_local_alloc(def_id, new_alloc);
Ok(new_alloc)
}

View File

@ -0,0 +1,11 @@
//! Even referencing an unknown `extern static` already triggers an error.
extern "C" {
static E: [u8; 0];
}
static X: &'static [u8; 0] = unsafe { &E };
fn main() {
let _val = X; //~ ERROR is not supported by Miri
}

View File

@ -0,0 +1,14 @@
error: unsupported operation: `extern` static `E` from crate `extern_static_in_const` is not supported by Miri
--> $DIR/extern_static_in_const.rs:LL:CC
|
LL | let _val = X;
| ^ `extern` static `E` from crate `extern_static_in_const` is not supported by Miri
|
= help: this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support
= note: inside `main` at $DIR/extern_static_in_const.rs:LL:CC
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
error: aborting due to previous error