Skip to content

Commit 0a4e8ca

Browse files
committed
adjust to canonical_alloc_id removal
1 parent 345b033 commit 0a4e8ca

File tree

4 files changed

+14
-72
lines changed

4 files changed

+14
-72
lines changed

src/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ pub fn report_error<'tcx, 'mir>(
9494
let helps = match e.kind {
9595
Unsupported(UnsupportedOpInfo::NoMirFor(..)) =>
9696
vec![format!("make sure to use a Miri sysroot, which you can prepare with `cargo miri setup`")],
97-
Unsupported(UnsupportedOpInfo::ReadBytesAsPointer) =>
98-
panic!("`ReadBytesAsPointer` cannot be raised by Miri"),
97+
Unsupported(UnsupportedOpInfo::ReadBytesAsPointer | UnsupportedOpInfo::ThreadLocalStatic(_) | UnsupportedOpInfo::ReadExternStatic(_)) =>
98+
panic!("Error should never be raised by Miri: {:?}", e.kind),
9999
Unsupported(_) =>
100100
vec![format!("this is likely not a bug in the program; it indicates that the program performed an operation that the interpreter does not support")],
101101
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. }) =>

src/intptrcast.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use log::trace;
66
use rand::Rng;
77

88
use rustc_data_structures::fx::FxHashMap;
9-
use rustc_mir::interpret::{AllocCheck, AllocId, InterpResult, Memory, Machine, Pointer, PointerArithmetic};
109
use rustc_target::abi::{Size, HasDataLayout};
1110

1211
use crate::*;
@@ -79,7 +78,7 @@ impl<'mir, 'tcx> GlobalState {
7978
) -> InterpResult<'tcx, u64> {
8079
let mut global_state = memory.extra.intptrcast.borrow_mut();
8180
let global_state = &mut *global_state;
82-
let id = Evaluator::canonical_alloc_id(memory, ptr.alloc_id);
81+
let id = ptr.alloc_id;
8382

8483
// There is nothing wrong with a raw pointer being cast to an integer only after
8584
// it became dangling. Hence `MaybeDead`.

src/machine.rs

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -426,44 +426,26 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
426426
Ok(())
427427
}
428428

429-
fn thread_local_alloc_id(
429+
fn thread_local_static_alloc_id(
430430
ecx: &mut InterpCx<'mir, 'tcx, Self>,
431431
def_id: DefId,
432432
) -> InterpResult<'tcx, AllocId> {
433433
ecx.get_or_create_thread_local_alloc_id(def_id)
434434
}
435435

436-
fn adjust_global_const(
437-
ecx: &InterpCx<'mir, 'tcx, Self>,
438-
mut val: mir::interpret::ConstValue<'tcx>,
439-
) -> InterpResult<'tcx, mir::interpret::ConstValue<'tcx>> {
440-
// FIXME: Remove this, do The Right Thing in `thread_local_alloc_id` instead.
441-
ecx.remap_thread_local_alloc_ids(&mut val)?;
442-
Ok(val)
443-
}
444-
445-
fn canonical_alloc_id(mem: &Memory<'mir, 'tcx, Self>, id: AllocId) -> AllocId {
446-
let tcx = mem.tcx;
447-
// Figure out if this is an extern static, and if yes, which one.
448-
let def_id = match tcx.get_global_alloc(id) {
449-
Some(GlobalAlloc::Static(def_id)) if tcx.is_foreign_item(def_id) => def_id,
450-
_ => {
451-
// No need to canonicalize anything.
452-
return id;
453-
}
454-
};
455-
let attrs = tcx.get_attrs(def_id);
436+
fn extern_static_alloc_id(
437+
memory: &Memory<'mir, 'tcx, Self>,
438+
def_id: DefId,
439+
) -> InterpResult<'tcx, AllocId> {
440+
let attrs = memory.tcx.get_attrs(def_id);
456441
let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
457442
Some(name) => name,
458-
None => tcx.item_name(def_id),
443+
None => memory.tcx.item_name(def_id),
459444
};
460-
// Check if we know this one.
461-
if let Some(canonical_id) = mem.extra.extern_statics.get(&link_name) {
462-
trace!("canonical_alloc_id: {:?} ({}) -> {:?}", id, link_name, canonical_id);
463-
*canonical_id
445+
if let Some(&id) = memory.extra.extern_statics.get(&link_name) {
446+
Ok(id)
464447
} else {
465-
// Return original id; `Memory::get_static_alloc` will throw an error.
466-
id
448+
throw_unsup_format!("`extern` static {:?} is not supported by Miri", def_id)
467449
}
468450
}
469451

src/thread.rs

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ use log::trace;
1111
use rustc_data_structures::fx::FxHashMap;
1212
use rustc_hir::def_id::DefId;
1313
use rustc_index::vec::{Idx, IndexVec};
14-
use rustc_middle::{
15-
middle::codegen_fn_attrs::CodegenFnAttrFlags,
16-
mir,
17-
ty::{self, Instance},
18-
};
14+
use rustc_middle::ty::{self, Instance};
1915

2016
use crate::sync::SynchronizationState;
2117
use crate::*;
@@ -499,41 +495,6 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
499495
// Public interface to thread management.
500496
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
501497
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
502-
/// A workaround for thread-local statics until
503-
/// https://github.com/rust-lang/rust/issues/70685 is fixed: change the
504-
/// thread-local allocation id with a freshly generated allocation id for
505-
/// the currently active thread.
506-
fn remap_thread_local_alloc_ids(
507-
&self,
508-
val: &mut mir::interpret::ConstValue<'tcx>,
509-
) -> InterpResult<'tcx> {
510-
let this = self.eval_context_ref();
511-
match *val {
512-
mir::interpret::ConstValue::Scalar(Scalar::Ptr(ref mut ptr)) => {
513-
let alloc_id = ptr.alloc_id;
514-
let alloc = this.tcx.get_global_alloc(alloc_id);
515-
let tcx = this.tcx;
516-
let is_thread_local = |def_id| {
517-
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
518-
};
519-
match alloc {
520-
Some(GlobalAlloc::Static(def_id)) if is_thread_local(def_id) => {
521-
ptr.alloc_id = this.get_or_create_thread_local_alloc_id(def_id)?;
522-
}
523-
_ => {}
524-
}
525-
}
526-
_ => {
527-
// FIXME: Handling only `Scalar` seems to work for now, but at
528-
// least in principle thread-locals could be in any constant, so
529-
// we should also consider other cases. However, once
530-
// https://github.com/rust-lang/rust/issues/70685 gets fixed,
531-
// this code will have to be rewritten anyway.
532-
}
533-
}
534-
Ok(())
535-
}
536-
537498
/// Get a thread-specific allocation id for the given thread-local static.
538499
/// If needed, allocate a new one.
539500
///

0 commit comments

Comments
 (0)