Skip to content

Commit 57c7fac

Browse files
committed
Fix miri
1 parent 61a8112 commit 57c7fac

File tree

12 files changed

+66
-20
lines changed

12 files changed

+66
-20
lines changed

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 0 deletions
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/extern_static.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Provides the `extern static` that this platform expects.
22
3+
use rustc_symbol_mangling::mangle_internal_symbol;
4+
35
use crate::*;
46

57
impl<'tcx> MiriMachine<'tcx> {
@@ -50,7 +52,11 @@ impl<'tcx> MiriMachine<'tcx> {
5052
// "__rust_alloc_error_handler_should_panic"
5153
let val = ecx.tcx.sess.opts.unstable_opts.oom.should_panic();
5254
let val = ImmTy::from_int(val, ecx.machine.layouts.u8);
53-
Self::alloc_extern_static(ecx, "__rust_alloc_error_handler_should_panic", val)?;
55+
Self::alloc_extern_static(
56+
ecx,
57+
&mangle_internal_symbol(*this.tcx, "__rust_alloc_error_handler_should_panic"),
58+
val,
59+
)?;
5460

5561
if ecx.target_os_is_unix() {
5662
// "environ" is mandated by POSIX.

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_hir::def_id::CrateNum;
1111
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1212
use rustc_middle::{mir, ty};
1313
use rustc_span::Symbol;
14+
use rustc_symbol_mangling::mangle_internal_symbol;
1415

1516
use self::helpers::{ToHost, ToSoft};
1617
use 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.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
383383
this.write_int(1, dest)?;
384384
}
385385
"TlsFree" => {
386-
let [key] = this.check_shim(abi, ExternAbi::System { unwind: false }, link_name, args)?;
386+
let [key] =
387+
this.check_shim(abi, ExternAbi::System { unwind: false }, link_name, args)?;
387388
let key = u128::from(this.read_scalar(key)?.to_u32()?);
388389
this.machine.tls.delete_tls_key(key)?;
389390

src/tools/miri/tests/fail/alloc/too_large.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#![feature(rustc_attrs)]
2+
13
extern "Rust" {
4+
#[rustc_std_internal_symbol]
25
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
36
}
47

src/tools/miri/tests/fail/alloc/unsupported_big_alignment.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// because rustc does not support alignments that large.
33
// https://github.com/rust-lang/miri/issues/3687
44

5+
#![feature(rustc_attrs)]
6+
57
extern "Rust" {
8+
#[rustc_std_internal_symbol]
69
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
710
}
811

src/tools/miri/tests/fail/alloc/unsupported_non_power_two_alignment.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Test non-power-of-two alignment.
2+
3+
#![feature(rustc_attrs)]
4+
25
extern "Rust" {
6+
#[rustc_std_internal_symbol]
37
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
48
}
59

src/tools/miri/tests/fail/data_race/dealloc_read_race1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Avoid accidental synchronization via address reuse inside `thread::spawn`.
44
//@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0
55

6+
#![feature(rustc_attrs)]
7+
68
use std::thread::spawn;
79

810
#[derive(Copy, Clone)]
@@ -12,6 +14,7 @@ unsafe impl<T> Send for EvilSend<T> {}
1214
unsafe impl<T> Sync for EvilSend<T> {}
1315

1416
extern "Rust" {
17+
#[rustc_std_internal_symbol]
1518
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
1619
}
1720

src/tools/miri/tests/fail/data_race/dealloc_read_race2.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Avoid accidental synchronization via address reuse inside `thread::spawn`.
44
//@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0
55

6+
#![feature(rustc_attrs)]
7+
68
use std::thread::spawn;
79

810
#[derive(Copy, Clone)]
@@ -12,6 +14,7 @@ unsafe impl<T> Send for EvilSend<T> {}
1214
unsafe impl<T> Sync for EvilSend<T> {}
1315

1416
extern "Rust" {
17+
#[rustc_std_internal_symbol]
1518
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
1619
}
1720

src/tools/miri/tests/fail/data_race/dealloc_write_race1.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Avoid accidental synchronization via address reuse inside `thread::spawn`.
44
//@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0
55

6+
#![feature(rustc_attrs)]
7+
68
use std::thread::spawn;
79

810
#[derive(Copy, Clone)]
@@ -12,6 +14,7 @@ unsafe impl<T> Send for EvilSend<T> {}
1214
unsafe impl<T> Sync for EvilSend<T> {}
1315

1416
extern "Rust" {
17+
#[rustc_std_internal_symbol]
1518
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
1619
}
1720
pub fn main() {

0 commit comments

Comments
 (0)