Skip to content

Commit a8616f2

Browse files
authored
Merge pull request #4241 from RalfJung/mangle_internal_symbol_cache
cache mangle_internal_symbol results
2 parents ab8836f + 024c8c3 commit a8616f2

File tree

3 files changed

+40
-33
lines changed

3 files changed

+40
-33
lines changed

src/tools/miri/src/helpers.rs

+13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_middle::ty::layout::{FnAbiOf, LayoutOf, MaybeResult, TyAndLayout};
1717
use rustc_middle::ty::{self, Binder, FloatTy, FnSig, IntTy, Ty, TyCtxt, UintTy};
1818
use rustc_session::config::CrateType;
1919
use rustc_span::{Span, Symbol};
20+
use rustc_symbol_mangling::mangle_internal_symbol;
2021
use rustc_target::callconv::{Conv, FnAbi};
2122

2223
use crate::*;
@@ -1258,6 +1259,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
12581259

12591260
interp_ok(array)
12601261
}
1262+
1263+
fn mangle_internal_symbol<'a>(&'a mut self, name: &'static str) -> &'a str
1264+
where
1265+
'tcx: 'a,
1266+
{
1267+
let this = self.eval_context_mut();
1268+
let tcx = *this.tcx;
1269+
this.machine
1270+
.mangle_internal_symbol_cache
1271+
.entry(name)
1272+
.or_insert_with(|| mangle_internal_symbol(tcx, name))
1273+
}
12611274
}
12621275

12631276
impl<'tcx> MiriMachine<'tcx> {

src/tools/miri/src/machine.rs

+5
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,9 @@ pub struct MiriMachine<'tcx> {
611611
pub(crate) reject_in_isolation_warned: RefCell<FxHashSet<String>>,
612612
/// Remembers which int2ptr casts we have already warned about.
613613
pub(crate) int2ptr_warned: RefCell<FxHashSet<Span>>,
614+
615+
/// Cache for `mangle_internal_symbol`.
616+
pub(crate) mangle_internal_symbol_cache: FxHashMap<&'static str, String>,
614617
}
615618

616619
impl<'tcx> MiriMachine<'tcx> {
@@ -757,6 +760,7 @@ impl<'tcx> MiriMachine<'tcx> {
757760
native_call_mem_warned: Cell::new(false),
758761
reject_in_isolation_warned: Default::default(),
759762
int2ptr_warned: Default::default(),
763+
mangle_internal_symbol_cache: Default::default(),
760764
}
761765
}
762766

@@ -930,6 +934,7 @@ impl VisitProvenance for MiriMachine<'_> {
930934
native_call_mem_warned: _,
931935
reject_in_isolation_warned: _,
932936
int2ptr_warned: _,
937+
mangle_internal_symbol_cache: _,
933938
} = self;
934939

935940
threads.visit_provenance(visit);

src/tools/miri/src/shims/foreign_items.rs

+22-33
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ use rustc_hir::def::DefKind;
99
use rustc_hir::def_id::CrateNum;
1010
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1111
use rustc_middle::mir::interpret::AllocInit;
12-
use rustc_middle::ty::Ty;
12+
use rustc_middle::ty::{Instance, Ty};
1313
use rustc_middle::{mir, ty};
1414
use rustc_span::Symbol;
15-
use rustc_symbol_mangling::mangle_internal_symbol;
1615
use rustc_target::callconv::{Conv, FnAbi};
1716

1817
use self::helpers::{ToHost, ToSoft};
@@ -52,19 +51,19 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5251

5352
// Some shims forward to other MIR bodies.
5453
match link_name.as_str() {
55-
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_error_handler") => {
54+
name if name == this.mangle_internal_symbol("__rust_alloc_error_handler") => {
5655
// Forward to the right symbol that implements this function.
5756
let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {
5857
// in real code, this symbol does not exist without an allocator
5958
throw_unsup_format!(
6059
"`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
6160
);
6261
};
63-
let name =
64-
mangle_internal_symbol(*this.tcx, alloc_error_handler_name(handler_kind));
65-
let handler = this
66-
.lookup_exported_symbol(Symbol::intern(&name))?
67-
.expect("missing alloc error handler symbol");
62+
let name = Symbol::intern(
63+
this.mangle_internal_symbol(alloc_error_handler_name(handler_kind)),
64+
);
65+
let handler =
66+
this.lookup_exported_symbol(name)?.expect("missing alloc error handler symbol");
6867
return interp_ok(Some(handler));
6968
}
7069
_ => {}
@@ -138,30 +137,22 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
138137
// Find it if it was not cached.
139138
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
140139
helpers::iter_exported_symbols(tcx, |cnum, def_id| {
140+
let attrs = tcx.codegen_fn_attrs(def_id);
141+
// Skip over imports of items.
141142
if tcx.is_foreign_item(def_id) {
142-
// Skip over imports of items
143143
return interp_ok(());
144144
}
145-
146-
let attrs = tcx.codegen_fn_attrs(def_id);
147-
// FIXME use tcx.symbol_name(instance) instead
148-
let symbol_name = if let Some(export_name) = attrs.export_name {
149-
export_name
150-
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
151-
|| attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
145+
// Skip over items without an explicitly defined symbol name.
146+
if !(attrs.export_name.is_some()
147+
|| attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
148+
|| attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL))
152149
{
153-
tcx.item_name(def_id)
154-
} else {
155-
// Skip over items without an explicitly defined symbol name.
156150
return interp_ok(());
157-
};
158-
let symbol_name =
159-
if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
160-
Symbol::intern(&mangle_internal_symbol(tcx, symbol_name.as_str()))
161-
} else {
162-
symbol_name
163-
};
164-
if symbol_name == link_name {
151+
}
152+
153+
let instance = Instance::mono(tcx, def_id);
154+
let symbol_name = tcx.symbol_name(instance).name;
155+
if symbol_name == link_name.as_str() {
165156
if let Some((original_instance, original_cnum)) = instance_and_crate {
166157
// Make sure we are consistent wrt what is 'first' and 'second'.
167158
let original_span = tcx.def_span(original_instance.def_id()).data();
@@ -505,9 +496,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
505496
}
506497

507498
// Rust allocation
508-
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc")
509-
|| name == "miri_alloc" =>
510-
{
499+
name if name == this.mangle_internal_symbol("__rust_alloc") || name == "miri_alloc" => {
511500
let default = |ecx: &mut MiriInterpCx<'tcx>| {
512501
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
513502
// macro is used, we act like no shim exists, so that the exported function can run.
@@ -540,7 +529,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
540529
_ => return this.emulate_allocator(default),
541530
}
542531
}
543-
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_zeroed") => {
532+
name if name == this.mangle_internal_symbol("__rust_alloc_zeroed") => {
544533
return this.emulate_allocator(|this| {
545534
// See the comment for `__rust_alloc` why `check_shim` is only called in the
546535
// default case.
@@ -559,7 +548,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
559548
this.write_pointer(ptr, dest)
560549
});
561550
}
562-
name if name == mangle_internal_symbol(*this.tcx, "__rust_dealloc")
551+
name if name == this.mangle_internal_symbol("__rust_dealloc")
563552
|| name == "miri_dealloc" =>
564553
{
565554
let default = |ecx: &mut MiriInterpCx<'tcx>| {
@@ -592,7 +581,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
592581
_ => return this.emulate_allocator(default),
593582
}
594583
}
595-
name if name == mangle_internal_symbol(*this.tcx, "__rust_realloc") => {
584+
name if name == this.mangle_internal_symbol("__rust_realloc") => {
596585
return this.emulate_allocator(|this| {
597586
// See the comment for `__rust_alloc` why `check_shim` is only called in the
598587
// default case.

0 commit comments

Comments
 (0)