@@ -7,6 +7,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
7
7
use rustc_middle:: mir;
8
8
use rustc_middle:: ty;
9
9
use rustc_span:: Symbol ;
10
+ use rustc_symbol_mangling:: mangle_internal_symbol;
10
11
use rustc_target:: {
11
12
abi:: { Align , Size } ,
12
13
spec:: abi:: Abi ,
@@ -135,15 +136,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
135
136
// Find it if it was not cached.
136
137
let mut instance_and_crate: Option < ( ty:: Instance < ' _ > , CrateNum ) > = None ;
137
138
helpers:: iter_exported_symbols ( tcx, |cnum, def_id| {
139
+ if tcx. is_foreign_item ( def_id) {
140
+ // Skip over imports of items
141
+ return Ok ( ( ) ) ;
142
+ }
143
+
138
144
let attrs = tcx. codegen_fn_attrs ( def_id) ;
145
+ // FIXME use tcx.symbol_name(instance) instead
139
146
let symbol_name = if let Some ( export_name) = attrs. export_name {
140
147
export_name
141
- } 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
+ {
142
151
tcx. item_name ( def_id)
143
152
} else {
144
153
// Skip over items without an explicitly defined symbol name.
145
154
return Ok ( ( ) ) ;
146
155
} ;
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
+ } ;
147
162
if symbol_name == link_name {
148
163
if let Some ( ( original_instance, original_cnum) ) = instance_and_crate {
149
164
// Make sure we are consistent wrt what is 'first' and 'second'.
@@ -455,7 +470,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
455
470
}
456
471
457
472
// Rust allocation
458
- "__rust_alloc" | "miri_alloc" => {
473
+ name if name == mangle_internal_symbol ( * this. tcx , "__rust_alloc" )
474
+ || name == "miri_alloc" =>
475
+ {
459
476
let default = |this : & mut MiriInterpCx < ' tcx > | {
460
477
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
461
478
// macro is used, we act like no shim exists, so that the exported function can run.
@@ -466,9 +483,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
466
483
this. check_rustc_alloc_request ( size, align) ?;
467
484
468
485
let memory_kind = match link_name. as_str ( ) {
469
- "__rust_alloc" => MiriMemoryKind :: Rust ,
470
486
"miri_alloc" => MiriMemoryKind :: Miri ,
471
- _ => unreachable ! ( ) ,
487
+ _ => MiriMemoryKind :: Rust ,
472
488
} ;
473
489
474
490
let ptr = this. allocate_ptr (
@@ -481,15 +497,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
481
497
} ;
482
498
483
499
match link_name. as_str ( ) {
484
- "__rust_alloc" => return this. emulate_allocator ( default) ,
485
500
"miri_alloc" => {
486
501
default ( this) ?;
487
502
return Ok ( EmulateItemResult :: NeedsReturn ) ;
488
503
}
489
- _ => unreachable ! ( ) ,
504
+ _ => return this . emulate_allocator ( default ) ,
490
505
}
491
506
}
492
- "__rust_alloc_zeroed" => {
507
+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_alloc_zeroed" ) => {
493
508
return this. emulate_allocator ( |this| {
494
509
// See the comment for `__rust_alloc` why `check_shim` is only called in the
495
510
// default case.
@@ -514,7 +529,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
514
529
this. write_pointer ( ptr, dest)
515
530
} ) ;
516
531
}
517
- "__rust_dealloc" | "miri_dealloc" => {
532
+ name if name == mangle_internal_symbol ( * this. tcx , "__rust_dealloc" )
533
+ || name == "miri_dealloc" =>
534
+ {
518
535
let default = |this : & mut MiriInterpCx < ' tcx > | {
519
536
// See the comment for `__rust_alloc` why `check_shim` is only called in the
520
537
// default case.
@@ -525,9 +542,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
525
542
let align = this. read_target_usize ( align) ?;
526
543
527
544
let memory_kind = match link_name. as_str ( ) {
528
- "__rust_dealloc" => MiriMemoryKind :: Rust ,
529
545
"miri_dealloc" => MiriMemoryKind :: Miri ,
530
- _ => unreachable ! ( ) ,
546
+ _ => MiriMemoryKind :: Rust ,
531
547
} ;
532
548
533
549
// No need to check old_size/align; we anyway check that they match the allocation.
@@ -539,17 +555,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
539
555
} ;
540
556
541
557
match link_name. as_str ( ) {
542
- "__rust_dealloc" => {
543
- return this. emulate_allocator ( default) ;
544
- }
545
558
"miri_dealloc" => {
546
559
default ( this) ?;
547
560
return Ok ( EmulateItemResult :: NeedsReturn ) ;
548
561
}
549
- _ => unreachable ! ( ) ,
562
+ _ => return this . emulate_allocator ( default ) ,
550
563
}
551
564
}
552
- "__rust_realloc" => {
565
+ name if name == mangle_internal_symbol ( * this . tcx , "__rust_realloc" ) => {
553
566
return this. emulate_allocator ( |this| {
554
567
// See the comment for `__rust_alloc` why `check_shim` is only called in the
555
568
// default case.
0 commit comments