Skip to content

Commit 9c91546

Browse files
committed
Fix miri
1 parent a077f35 commit 9c91546

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

src/tools/miri/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ extern crate rustc_index;
6767
extern crate rustc_middle;
6868
extern crate rustc_session;
6969
extern crate rustc_span;
70+
extern crate rustc_symbol_mangling;
7071
extern crate rustc_target;
7172
// Linking `rustc_driver` pulls in the required object code as the rest of the rustc crates are
7273
// shipped only as rmeta files.

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

+28-15
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
77
use rustc_middle::mir;
88
use rustc_middle::ty;
99
use rustc_span::Symbol;
10+
use rustc_symbol_mangling::mangle_internal_symbol;
1011
use rustc_target::{
1112
abi::{Align, Size},
1213
spec::abi::Abi,
@@ -135,15 +136,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
135136
// Find it if it was not cached.
136137
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
137138
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+
138144
let attrs = tcx.codegen_fn_attrs(def_id);
145+
// FIXME use tcx.symbol_name(instance) instead
139146
let symbol_name = if let Some(export_name) = attrs.export_name {
140147
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+
{
142151
tcx.item_name(def_id)
143152
} else {
144153
// Skip over items without an explicitly defined symbol name.
145154
return Ok(());
146155
};
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+
};
147162
if symbol_name == link_name {
148163
if let Some((original_instance, original_cnum)) = instance_and_crate {
149164
// Make sure we are consistent wrt what is 'first' and 'second'.
@@ -455,7 +470,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
455470
}
456471

457472
// Rust allocation
458-
"__rust_alloc" | "miri_alloc" => {
473+
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc")
474+
|| name == "miri_alloc" =>
475+
{
459476
let default = |this: &mut MiriInterpCx<'tcx>| {
460477
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
461478
// 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> {
466483
this.check_rustc_alloc_request(size, align)?;
467484

468485
let memory_kind = match link_name.as_str() {
469-
"__rust_alloc" => MiriMemoryKind::Rust,
470486
"miri_alloc" => MiriMemoryKind::Miri,
471-
_ => unreachable!(),
487+
_ => MiriMemoryKind::Rust,
472488
};
473489

474490
let ptr = this.allocate_ptr(
@@ -481,15 +497,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
481497
};
482498

483499
match link_name.as_str() {
484-
"__rust_alloc" => return this.emulate_allocator(default),
485500
"miri_alloc" => {
486501
default(this)?;
487502
return Ok(EmulateItemResult::NeedsReturn);
488503
}
489-
_ => unreachable!(),
504+
_ => return this.emulate_allocator(default),
490505
}
491506
}
492-
"__rust_alloc_zeroed" => {
507+
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_zeroed") => {
493508
return this.emulate_allocator(|this| {
494509
// See the comment for `__rust_alloc` why `check_shim` is only called in the
495510
// default case.
@@ -514,7 +529,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
514529
this.write_pointer(ptr, dest)
515530
});
516531
}
517-
"__rust_dealloc" | "miri_dealloc" => {
532+
name if name == mangle_internal_symbol(*this.tcx, "__rust_dealloc")
533+
|| name == "miri_dealloc" =>
534+
{
518535
let default = |this: &mut MiriInterpCx<'tcx>| {
519536
// See the comment for `__rust_alloc` why `check_shim` is only called in the
520537
// default case.
@@ -525,9 +542,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
525542
let align = this.read_target_usize(align)?;
526543

527544
let memory_kind = match link_name.as_str() {
528-
"__rust_dealloc" => MiriMemoryKind::Rust,
529545
"miri_dealloc" => MiriMemoryKind::Miri,
530-
_ => unreachable!(),
546+
_ => MiriMemoryKind::Rust,
531547
};
532548

533549
// 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> {
539555
};
540556

541557
match link_name.as_str() {
542-
"__rust_dealloc" => {
543-
return this.emulate_allocator(default);
544-
}
545558
"miri_dealloc" => {
546559
default(this)?;
547560
return Ok(EmulateItemResult::NeedsReturn);
548561
}
549-
_ => unreachable!(),
562+
_ => return this.emulate_allocator(default),
550563
}
551564
}
552-
"__rust_realloc" => {
565+
name if name == mangle_internal_symbol(*this.tcx, "__rust_realloc") => {
553566
return this.emulate_allocator(|this| {
554567
// See the comment for `__rust_alloc` why `check_shim` is only called in the
555568
// default case.

src/tools/miri/tests/pass/function_calls/exported_symbol.rs

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn main() {
4040

4141
extern "Rust" {
4242
fn bar() -> i32;
43+
#[rustc_std_internal_symbol]
4344
fn baz() -> i32;
4445
fn qux() -> i32;
4546
}
@@ -63,6 +64,7 @@ fn main() {
6364

6465
extern "C" {
6566
fn bar() -> i32;
67+
#[rustc_std_internal_symbol]
6668
fn baz() -> i32;
6769
fn qux() -> i32;
6870
}

0 commit comments

Comments
 (0)