@@ -12,6 +12,7 @@ use rustc_middle::mir::interpret::AllocInit;
12
12
use rustc_middle:: ty:: Ty ;
13
13
use rustc_middle:: { mir, ty} ;
14
14
use rustc_span:: Symbol ;
15
+ use rustc_symbol_mangling:: mangle_internal_symbol;
15
16
use rustc_target:: callconv:: { Conv , FnAbi } ;
16
17
17
18
use self :: helpers:: { ToHost , ToSoft } ;
@@ -51,17 +52,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
51
52
52
53
// Some shims forward to other MIR bodies.
53
54
match link_name. as_str ( ) {
54
- "__rust_alloc_error_handler" => {
55
+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_alloc_error_handler" ) => {
55
56
// Forward to the right symbol that implements this function.
56
57
let Some ( handler_kind) = this. tcx . alloc_error_handler_kind ( ( ) ) else {
57
58
// in real code, this symbol does not exist without an allocator
58
59
throw_unsup_format ! (
59
60
"`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
60
61
) ;
61
62
} ;
62
- let name = alloc_error_handler_name ( handler_kind) ;
63
+ let name =
64
+ mangle_internal_symbol ( * this. tcx , alloc_error_handler_name ( handler_kind) ) ;
63
65
let handler = this
64
- . lookup_exported_symbol ( Symbol :: intern ( name) ) ?
66
+ . lookup_exported_symbol ( Symbol :: intern ( & name) ) ?
65
67
. expect ( "missing alloc error handler symbol" ) ;
66
68
return interp_ok ( Some ( handler) ) ;
67
69
}
@@ -136,15 +138,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
136
138
// Find it if it was not cached.
137
139
let mut instance_and_crate: Option < ( ty:: Instance < ' _ > , CrateNum ) > = None ;
138
140
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
+
139
146
let attrs = tcx. codegen_fn_attrs ( def_id) ;
147
+ // FIXME use tcx.symbol_name(instance) instead
140
148
let symbol_name = if let Some ( export_name) = attrs. export_name {
141
149
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
+ {
143
153
tcx. item_name ( def_id)
144
154
} else {
145
155
// Skip over items without an explicitly defined symbol name.
146
156
return interp_ok ( ( ) ) ;
147
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
+ } ;
148
164
if symbol_name == link_name {
149
165
if let Some ( ( original_instance, original_cnum) ) = instance_and_crate {
150
166
// Make sure we are consistent wrt what is 'first' and 'second'.
@@ -489,7 +505,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
489
505
}
490
506
491
507
// Rust allocation
492
- "__rust_alloc" | "miri_alloc" => {
508
+ name if name == mangle_internal_symbol ( * this. tcx , "__rust_alloc" )
509
+ || name == "miri_alloc" =>
510
+ {
493
511
let default = |ecx : & mut MiriInterpCx < ' tcx > | {
494
512
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
495
513
// 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> {
500
518
ecx. check_rustc_alloc_request ( size, align) ?;
501
519
502
520
let memory_kind = match link_name. as_str ( ) {
503
- "__rust_alloc" => MiriMemoryKind :: Rust ,
504
521
"miri_alloc" => MiriMemoryKind :: Miri ,
505
- _ => unreachable ! ( ) ,
522
+ _ => MiriMemoryKind :: Rust ,
506
523
} ;
507
524
508
525
let ptr = ecx. allocate_ptr (
@@ -516,15 +533,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
516
533
} ;
517
534
518
535
match link_name. as_str ( ) {
519
- "__rust_alloc" => return this. emulate_allocator ( default) ,
520
536
"miri_alloc" => {
521
537
default ( this) ?;
522
538
return interp_ok ( EmulateItemResult :: NeedsReturn ) ;
523
539
}
524
- _ => unreachable ! ( ) ,
540
+ _ => return this . emulate_allocator ( default ) ,
525
541
}
526
542
}
527
- "__rust_alloc_zeroed" => {
543
+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_alloc_zeroed" ) => {
528
544
return this. emulate_allocator ( |this| {
529
545
// See the comment for `__rust_alloc` why `check_shim` is only called in the
530
546
// default case.
@@ -543,7 +559,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
543
559
this. write_pointer ( ptr, dest)
544
560
} ) ;
545
561
}
546
- "__rust_dealloc" | "miri_dealloc" => {
562
+ name if name == mangle_internal_symbol ( * this. tcx , "__rust_dealloc" )
563
+ || name == "miri_dealloc" =>
564
+ {
547
565
let default = |ecx : & mut MiriInterpCx < ' tcx > | {
548
566
// See the comment for `__rust_alloc` why `check_shim` is only called in the
549
567
// default case.
@@ -554,9 +572,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
554
572
let align = ecx. read_target_usize ( align) ?;
555
573
556
574
let memory_kind = match link_name. as_str ( ) {
557
- "__rust_dealloc" => MiriMemoryKind :: Rust ,
558
575
"miri_dealloc" => MiriMemoryKind :: Miri ,
559
- _ => unreachable ! ( ) ,
576
+ _ => MiriMemoryKind :: Rust ,
560
577
} ;
561
578
562
579
// 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> {
568
585
} ;
569
586
570
587
match link_name. as_str ( ) {
571
- "__rust_dealloc" => {
572
- return this. emulate_allocator ( default) ;
573
- }
574
588
"miri_dealloc" => {
575
589
default ( this) ?;
576
590
return interp_ok ( EmulateItemResult :: NeedsReturn ) ;
577
591
}
578
- _ => unreachable ! ( ) ,
592
+ _ => return this . emulate_allocator ( default ) ,
579
593
}
580
594
}
581
- "__rust_realloc" => {
595
+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_realloc" ) => {
582
596
return this. emulate_allocator ( |this| {
583
597
// See the comment for `__rust_alloc` why `check_shim` is only called in the
584
598
// default case.
0 commit comments