adjust to canonical_alloc_id removal
This commit is contained in:
parent
345b033c3f
commit
0a4e8caa8c
4 changed files with 14 additions and 72 deletions
|
|
@ -94,8 +94,8 @@ pub fn report_error<'tcx, 'mir>(
|
|||
let helps = match e.kind {
|
||||
Unsupported(UnsupportedOpInfo::NoMirFor(..)) =>
|
||||
vec![format!("make sure to use a Miri sysroot, which you can prepare with `cargo miri setup`")],
|
||||
Unsupported(UnsupportedOpInfo::ReadBytesAsPointer) =>
|
||||
panic!("`ReadBytesAsPointer` cannot be raised by Miri"),
|
||||
Unsupported(UnsupportedOpInfo::ReadBytesAsPointer | UnsupportedOpInfo::ThreadLocalStatic(_) | UnsupportedOpInfo::ReadExternStatic(_)) =>
|
||||
panic!("Error should never be raised by Miri: {:?}", e.kind),
|
||||
Unsupported(_) =>
|
||||
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")],
|
||||
UndefinedBehavior(UndefinedBehaviorInfo::AlignmentCheckFailed { .. }) =>
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ use log::trace;
|
|||
use rand::Rng;
|
||||
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_mir::interpret::{AllocCheck, AllocId, InterpResult, Memory, Machine, Pointer, PointerArithmetic};
|
||||
use rustc_target::abi::{Size, HasDataLayout};
|
||||
|
||||
use crate::*;
|
||||
|
|
@ -79,7 +78,7 @@ impl<'mir, 'tcx> GlobalState {
|
|||
) -> InterpResult<'tcx, u64> {
|
||||
let mut global_state = memory.extra.intptrcast.borrow_mut();
|
||||
let global_state = &mut *global_state;
|
||||
let id = Evaluator::canonical_alloc_id(memory, ptr.alloc_id);
|
||||
let id = ptr.alloc_id;
|
||||
|
||||
// There is nothing wrong with a raw pointer being cast to an integer only after
|
||||
// it became dangling. Hence `MaybeDead`.
|
||||
|
|
|
|||
|
|
@ -426,44 +426,26 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn thread_local_alloc_id(
|
||||
fn thread_local_static_alloc_id(
|
||||
ecx: &mut InterpCx<'mir, 'tcx, Self>,
|
||||
def_id: DefId,
|
||||
) -> InterpResult<'tcx, AllocId> {
|
||||
ecx.get_or_create_thread_local_alloc_id(def_id)
|
||||
}
|
||||
|
||||
fn adjust_global_const(
|
||||
ecx: &InterpCx<'mir, 'tcx, Self>,
|
||||
mut val: mir::interpret::ConstValue<'tcx>,
|
||||
) -> InterpResult<'tcx, mir::interpret::ConstValue<'tcx>> {
|
||||
// FIXME: Remove this, do The Right Thing in `thread_local_alloc_id` instead.
|
||||
ecx.remap_thread_local_alloc_ids(&mut val)?;
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
fn canonical_alloc_id(mem: &Memory<'mir, 'tcx, Self>, id: AllocId) -> AllocId {
|
||||
let tcx = mem.tcx;
|
||||
// Figure out if this is an extern static, and if yes, which one.
|
||||
let def_id = match tcx.get_global_alloc(id) {
|
||||
Some(GlobalAlloc::Static(def_id)) if tcx.is_foreign_item(def_id) => def_id,
|
||||
_ => {
|
||||
// No need to canonicalize anything.
|
||||
return id;
|
||||
}
|
||||
};
|
||||
let attrs = tcx.get_attrs(def_id);
|
||||
fn extern_static_alloc_id(
|
||||
memory: &Memory<'mir, 'tcx, Self>,
|
||||
def_id: DefId,
|
||||
) -> InterpResult<'tcx, AllocId> {
|
||||
let attrs = memory.tcx.get_attrs(def_id);
|
||||
let link_name = match attr::first_attr_value_str_by_name(&attrs, sym::link_name) {
|
||||
Some(name) => name,
|
||||
None => tcx.item_name(def_id),
|
||||
None => memory.tcx.item_name(def_id),
|
||||
};
|
||||
// Check if we know this one.
|
||||
if let Some(canonical_id) = mem.extra.extern_statics.get(&link_name) {
|
||||
trace!("canonical_alloc_id: {:?} ({}) -> {:?}", id, link_name, canonical_id);
|
||||
*canonical_id
|
||||
if let Some(&id) = memory.extra.extern_statics.get(&link_name) {
|
||||
Ok(id)
|
||||
} else {
|
||||
// Return original id; `Memory::get_static_alloc` will throw an error.
|
||||
id
|
||||
throw_unsup_format!("`extern` static {:?} is not supported by Miri", def_id)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,11 +11,7 @@ use log::trace;
|
|||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_hir::def_id::DefId;
|
||||
use rustc_index::vec::{Idx, IndexVec};
|
||||
use rustc_middle::{
|
||||
middle::codegen_fn_attrs::CodegenFnAttrFlags,
|
||||
mir,
|
||||
ty::{self, Instance},
|
||||
};
|
||||
use rustc_middle::ty::{self, Instance};
|
||||
|
||||
use crate::sync::SynchronizationState;
|
||||
use crate::*;
|
||||
|
|
@ -499,41 +495,6 @@ impl<'mir, 'tcx: 'mir> ThreadManager<'mir, 'tcx> {
|
|||
// Public interface to thread management.
|
||||
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
|
||||
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
|
||||
/// A workaround for thread-local statics until
|
||||
/// https://github.com/rust-lang/rust/issues/70685 is fixed: change the
|
||||
/// thread-local allocation id with a freshly generated allocation id for
|
||||
/// the currently active thread.
|
||||
fn remap_thread_local_alloc_ids(
|
||||
&self,
|
||||
val: &mut mir::interpret::ConstValue<'tcx>,
|
||||
) -> InterpResult<'tcx> {
|
||||
let this = self.eval_context_ref();
|
||||
match *val {
|
||||
mir::interpret::ConstValue::Scalar(Scalar::Ptr(ref mut ptr)) => {
|
||||
let alloc_id = ptr.alloc_id;
|
||||
let alloc = this.tcx.get_global_alloc(alloc_id);
|
||||
let tcx = this.tcx;
|
||||
let is_thread_local = |def_id| {
|
||||
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
|
||||
};
|
||||
match alloc {
|
||||
Some(GlobalAlloc::Static(def_id)) if is_thread_local(def_id) => {
|
||||
ptr.alloc_id = this.get_or_create_thread_local_alloc_id(def_id)?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// FIXME: Handling only `Scalar` seems to work for now, but at
|
||||
// least in principle thread-locals could be in any constant, so
|
||||
// we should also consider other cases. However, once
|
||||
// https://github.com/rust-lang/rust/issues/70685 gets fixed,
|
||||
// this code will have to be rewritten anyway.
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get a thread-specific allocation id for the given thread-local static.
|
||||
/// If needed, allocate a new one.
|
||||
///
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue