diff --git a/src/tools/miri/src/lib.rs b/src/tools/miri/src/lib.rs index 2b3ae6df5de8a..39a456626d1df 100644 --- a/src/tools/miri/src/lib.rs +++ b/src/tools/miri/src/lib.rs @@ -66,6 +66,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. diff --git a/src/tools/miri/src/shims/extern_static.rs b/src/tools/miri/src/shims/extern_static.rs index 17ac2638a69f4..06ea22bff4b81 100644 --- a/src/tools/miri/src/shims/extern_static.rs +++ b/src/tools/miri/src/shims/extern_static.rs @@ -1,5 +1,7 @@ //! Provides the `extern static` that this platform expects. +use rustc_symbol_mangling::mangle_internal_symbol; + use crate::*; impl<'tcx> MiriMachine<'tcx> { @@ -53,7 +55,11 @@ impl<'tcx> MiriMachine<'tcx> { // "__rust_alloc_error_handler_should_panic" let val = this.tcx.sess.opts.unstable_opts.oom.should_panic(); let val = ImmTy::from_int(val, this.machine.layouts.u8); - Self::alloc_extern_static(this, "__rust_alloc_error_handler_should_panic", val)?; + Self::alloc_extern_static( + this, + &mangle_internal_symbol(*this.tcx, "__rust_alloc_error_handler_should_panic"), + val, + )?; if this.target_os_is_unix() { // "environ" is mandated by POSIX. diff --git a/src/tools/miri/src/shims/foreign_items.rs b/src/tools/miri/src/shims/foreign_items.rs index 7f6f63ff5e79b..aa823077678e9 100644 --- a/src/tools/miri/src/shims/foreign_items.rs +++ b/src/tools/miri/src/shims/foreign_items.rs @@ -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, @@ -49,7 +50,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // Some shims forward to other MIR bodies. match link_name.as_str() { - "__rust_alloc_error_handler" => { + name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_error_handler") => { // Forward to the right symbol that implements this function. let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else { // in real code, this symbol does not exist without an allocator @@ -57,9 +58,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { "`__rust_alloc_error_handler` cannot be called when no alloc error handler is set" ); }; - let name = alloc_error_handler_name(handler_kind); + let name = + mangle_internal_symbol(*this.tcx, alloc_error_handler_name(handler_kind)); let handler = this - .lookup_exported_symbol(Symbol::intern(name))? + .lookup_exported_symbol(Symbol::intern(&name))? .expect("missing alloc error handler symbol"); return Ok(Some(handler)); } @@ -135,15 +137,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'. @@ -455,7 +471,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. @@ -466,9 +484,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( @@ -481,15 +498,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. @@ -514,7 +530,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. @@ -525,9 +543,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. @@ -539,17 +556,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. diff --git a/src/tools/miri/tests/fail/alloc/too_large.rs b/src/tools/miri/tests/fail/alloc/too_large.rs index 4e28d2401d79f..c53318855aba0 100644 --- a/src/tools/miri/tests/fail/alloc/too_large.rs +++ b/src/tools/miri/tests/fail/alloc/too_large.rs @@ -1,4 +1,7 @@ +#![feature(rustc_attrs)] + extern "Rust" { + #[rustc_std_internal_symbol] fn __rust_alloc(size: usize, align: usize) -> *mut u8; } diff --git a/src/tools/miri/tests/fail/data_race/dealloc_read_race1.rs b/src/tools/miri/tests/fail/data_race/dealloc_read_race1.rs index f174909e9d56b..d0a2848205410 100644 --- a/src/tools/miri/tests/fail/data_race/dealloc_read_race1.rs +++ b/src/tools/miri/tests/fail/data_race/dealloc_read_race1.rs @@ -3,6 +3,8 @@ // Avoid accidental synchronization via address reuse inside `thread::spawn`. //@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0 +#![feature(rustc_attrs)] + use std::thread::spawn; #[derive(Copy, Clone)] @@ -12,6 +14,7 @@ unsafe impl Send for EvilSend {} unsafe impl Sync for EvilSend {} extern "Rust" { + #[rustc_std_internal_symbol] fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize); } diff --git a/src/tools/miri/tests/fail/data_race/dealloc_read_race2.rs b/src/tools/miri/tests/fail/data_race/dealloc_read_race2.rs index 1edfbf5e61c0d..f56c44cabc234 100644 --- a/src/tools/miri/tests/fail/data_race/dealloc_read_race2.rs +++ b/src/tools/miri/tests/fail/data_race/dealloc_read_race2.rs @@ -3,6 +3,8 @@ // Avoid accidental synchronization via address reuse inside `thread::spawn`. //@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0 +#![feature(rustc_attrs)] + use std::thread::spawn; #[derive(Copy, Clone)] @@ -12,6 +14,7 @@ unsafe impl Send for EvilSend {} unsafe impl Sync for EvilSend {} extern "Rust" { + #[rustc_std_internal_symbol] fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize); } diff --git a/src/tools/miri/tests/fail/data_race/dealloc_write_race1.rs b/src/tools/miri/tests/fail/data_race/dealloc_write_race1.rs index 7605f1911db77..a16ea25e11ce8 100644 --- a/src/tools/miri/tests/fail/data_race/dealloc_write_race1.rs +++ b/src/tools/miri/tests/fail/data_race/dealloc_write_race1.rs @@ -3,6 +3,8 @@ // Avoid accidental synchronization via address reuse inside `thread::spawn`. //@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0 +#![feature(rustc_attrs)] + use std::thread::spawn; #[derive(Copy, Clone)] @@ -12,6 +14,7 @@ unsafe impl Send for EvilSend {} unsafe impl Sync for EvilSend {} extern "Rust" { + #[rustc_std_internal_symbol] fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize); } pub fn main() { diff --git a/src/tools/miri/tests/fail/data_race/dealloc_write_race2.rs b/src/tools/miri/tests/fail/data_race/dealloc_write_race2.rs index 4f3819bd636b2..f3855e33c98d9 100644 --- a/src/tools/miri/tests/fail/data_race/dealloc_write_race2.rs +++ b/src/tools/miri/tests/fail/data_race/dealloc_write_race2.rs @@ -3,6 +3,8 @@ // Avoid accidental synchronization via address reuse inside `thread::spawn`. //@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0 +#![feature(rustc_attrs)] + use std::thread::spawn; #[derive(Copy, Clone)] @@ -12,6 +14,7 @@ unsafe impl Send for EvilSend {} unsafe impl Sync for EvilSend {} extern "Rust" { + #[rustc_std_internal_symbol] fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize); } pub fn main() { diff --git a/src/tools/miri/tests/pass/function_calls/exported_symbol.rs b/src/tools/miri/tests/pass/function_calls/exported_symbol.rs index 27aee9c883588..d804f3642b1d2 100644 --- a/src/tools/miri/tests/pass/function_calls/exported_symbol.rs +++ b/src/tools/miri/tests/pass/function_calls/exported_symbol.rs @@ -40,6 +40,7 @@ fn main() { extern "Rust" { fn bar() -> i32; + #[rustc_std_internal_symbol] fn baz() -> i32; fn qux() -> i32; } @@ -63,6 +64,7 @@ fn main() { extern "C" { fn bar() -> i32; + #[rustc_std_internal_symbol] fn baz() -> i32; fn qux() -> i32; }