@@ -11,6 +11,7 @@ use rustc_hir::def_id::CrateNum;
1111use rustc_middle:: middle:: codegen_fn_attrs:: CodegenFnAttrFlags ;
1212use rustc_middle:: { mir, ty} ;
1313use rustc_span:: Symbol ;
14+ use rustc_symbol_mangling:: mangle_internal_symbol;
1415
1516use self :: helpers:: { ToHost , ToSoft } ;
1617use super :: alloc:: EvalContextExt as _;
@@ -49,17 +50,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
4950
5051 // Some shims forward to other MIR bodies.
5152 match link_name. as_str ( ) {
52- "__rust_alloc_error_handler" => {
53+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_alloc_error_handler" ) => {
5354 // Forward to the right symbol that implements this function.
5455 let Some ( handler_kind) = this. tcx . alloc_error_handler_kind ( ( ) ) else {
5556 // in real code, this symbol does not exist without an allocator
5657 throw_unsup_format ! (
5758 "`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
5859 ) ;
5960 } ;
60- let name = alloc_error_handler_name ( handler_kind) ;
61+ let name =
62+ mangle_internal_symbol ( * this. tcx , alloc_error_handler_name ( handler_kind) ) ;
6163 let handler = this
62- . lookup_exported_symbol ( Symbol :: intern ( name) ) ?
64+ . lookup_exported_symbol ( Symbol :: intern ( & name) ) ?
6365 . expect ( "missing alloc error handler symbol" ) ;
6466 return interp_ok ( Some ( handler) ) ;
6567 }
@@ -134,15 +136,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
134136 // Find it if it was not cached.
135137 let mut instance_and_crate: Option < ( ty:: Instance < ' _ > , CrateNum ) > = None ;
136138 helpers:: iter_exported_symbols ( tcx, |cnum, def_id| {
139+ if tcx. is_foreign_item ( def_id) {
140+ // Skip over imports of items
141+ return interp_ok ( ( ) ) ;
142+ }
143+
137144 let attrs = tcx. codegen_fn_attrs ( def_id) ;
145+ // FIXME use tcx.symbol_name(instance) instead
138146 let symbol_name = if let Some ( export_name) = attrs. export_name {
139147 export_name
140- } else if attrs. flags . contains ( CodegenFnAttrFlags :: NO_MANGLE ) {
148+ } else if attrs. flags . contains ( CodegenFnAttrFlags :: NO_MANGLE )
149+ || attrs. flags . contains ( CodegenFnAttrFlags :: RUSTC_STD_INTERNAL_SYMBOL )
150+ {
141151 tcx. item_name ( def_id)
142152 } else {
143153 // Skip over items without an explicitly defined symbol name.
144154 return interp_ok ( ( ) ) ;
145155 } ;
156+ let symbol_name =
157+ if attrs. flags . contains ( CodegenFnAttrFlags :: RUSTC_STD_INTERNAL_SYMBOL ) {
158+ Symbol :: intern ( & mangle_internal_symbol ( tcx, symbol_name. as_str ( ) ) )
159+ } else {
160+ symbol_name
161+ } ;
146162 if symbol_name == link_name {
147163 if let Some ( ( original_instance, original_cnum) ) = instance_and_crate {
148164 // Make sure we are consistent wrt what is 'first' and 'second'.
@@ -495,7 +511,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
495511 }
496512
497513 // Rust allocation
498- "__rust_alloc" | "miri_alloc" => {
514+ name if name == mangle_internal_symbol ( * this. tcx , "__rust_alloc" )
515+ || name == "miri_alloc" =>
516+ {
499517 let default = |ecx : & mut MiriInterpCx < ' tcx > | {
500518 // Only call `check_shim` when `#[global_allocator]` isn't used. When that
501519 // macro is used, we act like no shim exists, so that the exported function can run.
@@ -506,9 +524,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
506524 ecx. check_rustc_alloc_request ( size, align) ?;
507525
508526 let memory_kind = match link_name. as_str ( ) {
509- "__rust_alloc" => MiriMemoryKind :: Rust ,
510527 "miri_alloc" => MiriMemoryKind :: Miri ,
511- _ => unreachable ! ( ) ,
528+ _ => MiriMemoryKind :: Rust ,
512529 } ;
513530
514531 let ptr = ecx. allocate_ptr (
@@ -521,15 +538,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
521538 } ;
522539
523540 match link_name. as_str ( ) {
524- "__rust_alloc" => return this. emulate_allocator ( default) ,
525541 "miri_alloc" => {
526542 default ( this) ?;
527543 return interp_ok ( EmulateItemResult :: NeedsReturn ) ;
528544 }
529- _ => unreachable ! ( ) ,
545+ _ => return this . emulate_allocator ( default ) ,
530546 }
531547 }
532- "__rust_alloc_zeroed" => {
548+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_alloc_zeroed" ) => {
533549 return this. emulate_allocator ( |this| {
534550 // See the comment for `__rust_alloc` why `check_shim` is only called in the
535551 // default case.
@@ -554,7 +570,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
554570 this. write_pointer ( ptr, dest)
555571 } ) ;
556572 }
557- "__rust_dealloc" | "miri_dealloc" => {
573+ name if name == mangle_internal_symbol ( * this. tcx , "__rust_dealloc" )
574+ || name == "miri_dealloc" =>
575+ {
558576 let default = |ecx : & mut MiriInterpCx < ' tcx > | {
559577 // See the comment for `__rust_alloc` why `check_shim` is only called in the
560578 // default case.
@@ -565,9 +583,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
565583 let align = ecx. read_target_usize ( align) ?;
566584
567585 let memory_kind = match link_name. as_str ( ) {
568- "__rust_dealloc" => MiriMemoryKind :: Rust ,
569586 "miri_dealloc" => MiriMemoryKind :: Miri ,
570- _ => unreachable ! ( ) ,
587+ _ => MiriMemoryKind :: Rust ,
571588 } ;
572589
573590 // No need to check old_size/align; we anyway check that they match the allocation.
@@ -579,17 +596,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
579596 } ;
580597
581598 match link_name. as_str ( ) {
582- "__rust_dealloc" => {
583- return this. emulate_allocator ( default) ;
584- }
585599 "miri_dealloc" => {
586600 default ( this) ?;
587601 return interp_ok ( EmulateItemResult :: NeedsReturn ) ;
588602 }
589- _ => unreachable ! ( ) ,
603+ _ => return this . emulate_allocator ( default ) ,
590604 }
591605 }
592- "__rust_realloc" => {
606+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_realloc" ) => {
593607 return this. emulate_allocator ( |this| {
594608 // See the comment for `__rust_alloc` why `check_shim` is only called in the
595609 // default case.
0 commit comments