@@ -12,6 +12,7 @@ use rustc_middle::mir::interpret::AllocInit;
1212use rustc_middle:: ty:: Ty ;
1313use rustc_middle:: { mir, ty} ;
1414use rustc_span:: Symbol ;
15+ use rustc_symbol_mangling:: mangle_internal_symbol;
1516use rustc_target:: callconv:: { Conv , FnAbi } ;
1617
1718use self :: helpers:: { ToHost , ToSoft } ;
@@ -51,17 +52,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5152
5253 // Some shims forward to other MIR bodies.
5354 match link_name. as_str ( ) {
54- "__rust_alloc_error_handler" => {
55+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_alloc_error_handler" ) => {
5556 // Forward to the right symbol that implements this function.
5657 let Some ( handler_kind) = this. tcx . alloc_error_handler_kind ( ( ) ) else {
5758 // in real code, this symbol does not exist without an allocator
5859 throw_unsup_format ! (
5960 "`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
6061 ) ;
6162 } ;
62- let name = alloc_error_handler_name ( handler_kind) ;
63+ let name =
64+ mangle_internal_symbol ( * this. tcx , alloc_error_handler_name ( handler_kind) ) ;
6365 let handler = this
64- . lookup_exported_symbol ( Symbol :: intern ( name) ) ?
66+ . lookup_exported_symbol ( Symbol :: intern ( & name) ) ?
6567 . expect ( "missing alloc error handler symbol" ) ;
6668 return interp_ok ( Some ( handler) ) ;
6769 }
@@ -136,15 +138,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
136138 // Find it if it was not cached.
137139 let mut instance_and_crate: Option < ( ty:: Instance < ' _ > , CrateNum ) > = None ;
138140 helpers:: iter_exported_symbols ( tcx, |cnum, def_id| {
141+ if tcx. is_foreign_item ( def_id) {
142+ // Skip over imports of items
143+ return interp_ok ( ( ) ) ;
144+ }
145+
139146 let attrs = tcx. codegen_fn_attrs ( def_id) ;
147+ // FIXME use tcx.symbol_name(instance) instead
140148 let symbol_name = if let Some ( export_name) = attrs. export_name {
141149 export_name
142- } else if attrs. flags . contains ( CodegenFnAttrFlags :: NO_MANGLE ) {
150+ } else if attrs. flags . contains ( CodegenFnAttrFlags :: NO_MANGLE )
151+ || attrs. flags . contains ( CodegenFnAttrFlags :: RUSTC_STD_INTERNAL_SYMBOL )
152+ {
143153 tcx. item_name ( def_id)
144154 } else {
145155 // Skip over items without an explicitly defined symbol name.
146156 return interp_ok ( ( ) ) ;
147157 } ;
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+ } ;
148164 if symbol_name == link_name {
149165 if let Some ( ( original_instance, original_cnum) ) = instance_and_crate {
150166 // Make sure we are consistent wrt what is 'first' and 'second'.
@@ -489,7 +505,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
489505 }
490506
491507 // Rust allocation
492- "__rust_alloc" | "miri_alloc" => {
508+ name if name == mangle_internal_symbol ( * this. tcx , "__rust_alloc" )
509+ || name == "miri_alloc" =>
510+ {
493511 let default = |ecx : & mut MiriInterpCx < ' tcx > | {
494512 // Only call `check_shim` when `#[global_allocator]` isn't used. When that
495513 // macro is used, we act like no shim exists, so that the exported function can run.
@@ -500,9 +518,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
500518 ecx. check_rustc_alloc_request ( size, align) ?;
501519
502520 let memory_kind = match link_name. as_str ( ) {
503- "__rust_alloc" => MiriMemoryKind :: Rust ,
504521 "miri_alloc" => MiriMemoryKind :: Miri ,
505- _ => unreachable ! ( ) ,
522+ _ => MiriMemoryKind :: Rust ,
506523 } ;
507524
508525 let ptr = ecx. allocate_ptr (
@@ -516,15 +533,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
516533 } ;
517534
518535 match link_name. as_str ( ) {
519- "__rust_alloc" => return this. emulate_allocator ( default) ,
520536 "miri_alloc" => {
521537 default ( this) ?;
522538 return interp_ok ( EmulateItemResult :: NeedsReturn ) ;
523539 }
524- _ => unreachable ! ( ) ,
540+ _ => return this . emulate_allocator ( default ) ,
525541 }
526542 }
527- "__rust_alloc_zeroed" => {
543+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_alloc_zeroed" ) => {
528544 return this. emulate_allocator ( |this| {
529545 // See the comment for `__rust_alloc` why `check_shim` is only called in the
530546 // default case.
@@ -543,7 +559,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
543559 this. write_pointer ( ptr, dest)
544560 } ) ;
545561 }
546- "__rust_dealloc" | "miri_dealloc" => {
562+ name if name == mangle_internal_symbol ( * this. tcx , "__rust_dealloc" )
563+ || name == "miri_dealloc" =>
564+ {
547565 let default = |ecx : & mut MiriInterpCx < ' tcx > | {
548566 // See the comment for `__rust_alloc` why `check_shim` is only called in the
549567 // default case.
@@ -554,9 +572,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
554572 let align = ecx. read_target_usize ( align) ?;
555573
556574 let memory_kind = match link_name. as_str ( ) {
557- "__rust_dealloc" => MiriMemoryKind :: Rust ,
558575 "miri_dealloc" => MiriMemoryKind :: Miri ,
559- _ => unreachable ! ( ) ,
576+ _ => MiriMemoryKind :: Rust ,
560577 } ;
561578
562579 // No need to check old_size/align; we anyway check that they match the allocation.
@@ -568,17 +585,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
568585 } ;
569586
570587 match link_name. as_str ( ) {
571- "__rust_dealloc" => {
572- return this. emulate_allocator ( default) ;
573- }
574588 "miri_dealloc" => {
575589 default ( this) ?;
576590 return interp_ok ( EmulateItemResult :: NeedsReturn ) ;
577591 }
578- _ => unreachable ! ( ) ,
592+ _ => return this . emulate_allocator ( default ) ,
579593 }
580594 }
581- "__rust_realloc" => {
595+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_realloc" ) => {
582596 return this. emulate_allocator ( |this| {
583597 // See the comment for `__rust_alloc` why `check_shim` is only called in the
584598 // default case.
0 commit comments