diff --git a/src/tools/miri/src/lib.rs b/src/tools/miri/src/lib.rs index 8da00861f905b..55da2fd6a628a 100644 --- a/src/tools/miri/src/lib.rs +++ b/src/tools/miri/src/lib.rs @@ -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. diff --git a/src/tools/miri/src/shims/foreign_items.rs b/src/tools/miri/src/shims/foreign_items.rs index 9004f7efc8b5e..0a8ef2322db9b 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, @@ -455,7 +456,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 +469,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 +483,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 +515,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 +528,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 +541,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.