Skip to content

Commit

Permalink
Fix miri
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 committed Jul 5, 2024
1 parent a077f35 commit 9c91546
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/tools/miri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extern crate rustc_index;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_symbol_mangling;
extern crate rustc_target;
// Linking `rustc_driver` pulls in the required object code as the rest of the rustc crates are
// shipped only as rmeta files.
Expand Down
43 changes: 28 additions & 15 deletions src/tools/miri/src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::mir;
use rustc_middle::ty;
use rustc_span::Symbol;
use rustc_symbol_mangling::mangle_internal_symbol;
use rustc_target::{
abi::{Align, Size},
spec::abi::Abi,
Expand Down Expand Up @@ -135,15 +136,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Find it if it was not cached.
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
helpers::iter_exported_symbols(tcx, |cnum, def_id| {
if tcx.is_foreign_item(def_id) {
// Skip over imports of items
return Ok(());
}

let attrs = tcx.codegen_fn_attrs(def_id);
// FIXME use tcx.symbol_name(instance) instead
let symbol_name = if let Some(export_name) = attrs.export_name {
export_name
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
|| attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
{
tcx.item_name(def_id)
} else {
// Skip over items without an explicitly defined symbol name.
return Ok(());
};
let symbol_name =
if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
Symbol::intern(&mangle_internal_symbol(tcx, symbol_name.as_str()))
} else {
symbol_name
};
if symbol_name == link_name {
if let Some((original_instance, original_cnum)) = instance_and_crate {
// Make sure we are consistent wrt what is 'first' and 'second'.
Expand Down Expand Up @@ -455,7 +470,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
}

// Rust allocation
"__rust_alloc" | "miri_alloc" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc")
|| name == "miri_alloc" =>
{
let default = |this: &mut MiriInterpCx<'tcx>| {
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
// macro is used, we act like no shim exists, so that the exported function can run.
Expand All @@ -466,9 +483,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.check_rustc_alloc_request(size, align)?;

let memory_kind = match link_name.as_str() {
"__rust_alloc" => MiriMemoryKind::Rust,
"miri_alloc" => MiriMemoryKind::Miri,
_ => unreachable!(),
_ => MiriMemoryKind::Rust,
};

let ptr = this.allocate_ptr(
Expand All @@ -481,15 +497,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
};

match link_name.as_str() {
"__rust_alloc" => return this.emulate_allocator(default),
"miri_alloc" => {
default(this)?;
return Ok(EmulateItemResult::NeedsReturn);
}
_ => unreachable!(),
_ => return this.emulate_allocator(default),
}
}
"__rust_alloc_zeroed" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_zeroed") => {
return this.emulate_allocator(|this| {
// See the comment for `__rust_alloc` why `check_shim` is only called in the
// default case.
Expand All @@ -514,7 +529,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.write_pointer(ptr, dest)
});
}
"__rust_dealloc" | "miri_dealloc" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_dealloc")
|| name == "miri_dealloc" =>
{
let default = |this: &mut MiriInterpCx<'tcx>| {
// See the comment for `__rust_alloc` why `check_shim` is only called in the
// default case.
Expand All @@ -525,9 +542,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
let align = this.read_target_usize(align)?;

let memory_kind = match link_name.as_str() {
"__rust_dealloc" => MiriMemoryKind::Rust,
"miri_dealloc" => MiriMemoryKind::Miri,
_ => unreachable!(),
_ => MiriMemoryKind::Rust,
};

// No need to check old_size/align; we anyway check that they match the allocation.
Expand All @@ -539,17 +555,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
};

match link_name.as_str() {
"__rust_dealloc" => {
return this.emulate_allocator(default);
}
"miri_dealloc" => {
default(this)?;
return Ok(EmulateItemResult::NeedsReturn);
}
_ => unreachable!(),
_ => return this.emulate_allocator(default),
}
}
"__rust_realloc" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_realloc") => {
return this.emulate_allocator(|this| {
// See the comment for `__rust_alloc` why `check_shim` is only called in the
// default case.
Expand Down
2 changes: 2 additions & 0 deletions src/tools/miri/tests/pass/function_calls/exported_symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn main() {

extern "Rust" {
fn bar() -> i32;
#[rustc_std_internal_symbol]
fn baz() -> i32;
fn qux() -> i32;
}
Expand All @@ -63,6 +64,7 @@ fn main() {

extern "C" {
fn bar() -> i32;
#[rustc_std_internal_symbol]
fn baz() -> i32;
fn qux() -> i32;
}
Expand Down

0 comments on commit 9c91546

Please sign in to comment.