Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e1d353a

Browse files
committedJun 30, 2024
Partially fix miri
1 parent 10bad43 commit e1d353a

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed
 

‎src/tools/miri/src/helpers.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_middle::ty::{
2424
FloatTy, IntTy, Ty, TyCtxt, UintTy,
2525
};
2626
use rustc_session::config::CrateType;
27-
use rustc_span::{sym, Span, Symbol};
27+
use rustc_span::{Span, Symbol};
2828
use rustc_target::abi::{Align, FieldIdx, FieldsShape, Size, Variants};
2929
use rustc_target::spec::abi::Abi;
3030

@@ -1182,14 +1182,6 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
11821182
this.alloc_mark_immutable(provenance.get_alloc_id().unwrap()).unwrap();
11831183
}
11841184

1185-
fn item_link_name(&self, def_id: DefId) -> Symbol {
1186-
let tcx = self.eval_context_ref().tcx;
1187-
match tcx.get_attrs(def_id, sym::link_name).filter_map(|a| a.value_str()).next() {
1188-
Some(name) => name,
1189-
None => tcx.item_name(def_id),
1190-
}
1191-
}
1192-
11931185
/// Converts `src` from floating point to integer type `dest_ty`
11941186
/// after rounding with mode `round`.
11951187
/// Returns `None` if `f` is NaN or out of range.

‎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/machine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
954954
// foreign function
955955
// Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
956956
let args = ecx.copy_fn_args(args); // FIXME: Should `InPlace` arguments be reset to uninit?
957-
let link_name = ecx.item_link_name(instance.def_id());
957+
let link_name = Symbol::intern(ecx.tcx.symbol_name(instance).name);
958958
return ecx.emulate_foreign_item(link_name, abi, &args, dest, ret, unwind);
959959
}
960960

@@ -1050,7 +1050,7 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
10501050
ecx: &MiriInterpCx<'tcx>,
10511051
def_id: DefId,
10521052
) -> InterpResult<'tcx, StrictPointer> {
1053-
let link_name = ecx.item_link_name(def_id);
1053+
let link_name = Symbol::intern(ecx.tcx.symbol_name(Instance::mono(*ecx.tcx, def_id)).name);
10541054
if let Some(&ptr) = ecx.machine.extern_statics.get(&link_name) {
10551055
// Various parts of the engine rely on `get_alloc_info` for size and alignment
10561056
// information. That uses the type information of this static.

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

+14-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,
@@ -52,7 +53,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
5253
match link_name.as_str() {
5354
// This matches calls to the foreign item `panic_impl`.
5455
// The implementation is provided by the function with the `#[panic_handler]` attribute.
55-
"panic_impl" => {
56+
name if name == mangle_internal_symbol(*this.tcx, "rust_begin_unwind") => {
5657
// We don't use `check_shim` here because we are just forwarding to the lang
5758
// item. Argument count checking will be performed when the returned `Body` is
5859
// called.
@@ -470,7 +471,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
470471
}
471472

472473
// Rust allocation
473-
"__rust_alloc" | "miri_alloc" => {
474+
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc")
475+
|| name == "miri_alloc" =>
476+
{
474477
let default = |this: &mut MiriInterpCx<'tcx>| {
475478
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
476479
// macro is used, we act like no shim exists, so that the exported function can run.
@@ -481,9 +484,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
481484
this.check_rustc_alloc_request(size, align)?;
482485

483486
let memory_kind = match link_name.as_str() {
484-
"__rust_alloc" => MiriMemoryKind::Rust,
485487
"miri_alloc" => MiriMemoryKind::Miri,
486-
_ => unreachable!(),
488+
_ => MiriMemoryKind::Rust,
487489
};
488490

489491
let ptr = this.allocate_ptr(
@@ -496,15 +498,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
496498
};
497499

498500
match link_name.as_str() {
499-
"__rust_alloc" => return this.emulate_allocator(default),
500501
"miri_alloc" => {
501502
default(this)?;
502503
return Ok(EmulateItemResult::NeedsReturn);
503504
}
504-
_ => unreachable!(),
505+
_ => return this.emulate_allocator(default),
505506
}
506507
}
507-
"__rust_alloc_zeroed" => {
508+
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_zeroed") => {
508509
return this.emulate_allocator(|this| {
509510
// See the comment for `__rust_alloc` why `check_shim` is only called in the
510511
// default case.
@@ -529,7 +530,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
529530
this.write_pointer(ptr, dest)
530531
});
531532
}
532-
"__rust_dealloc" | "miri_dealloc" => {
533+
name if name == mangle_internal_symbol(*this.tcx, "__rust_dealloc")
534+
|| name == "miri_dealloc" =>
535+
{
533536
let default = |this: &mut MiriInterpCx<'tcx>| {
534537
// See the comment for `__rust_alloc` why `check_shim` is only called in the
535538
// default case.
@@ -540,9 +543,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
540543
let align = this.read_target_usize(align)?;
541544

542545
let memory_kind = match link_name.as_str() {
543-
"__rust_dealloc" => MiriMemoryKind::Rust,
544546
"miri_dealloc" => MiriMemoryKind::Miri,
545-
_ => unreachable!(),
547+
_ => MiriMemoryKind::Rust,
546548
};
547549

548550
// No need to check old_size/align; we anyway check that they match the allocation.
@@ -554,17 +556,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
554556
};
555557

556558
match link_name.as_str() {
557-
"__rust_dealloc" => {
558-
return this.emulate_allocator(default);
559-
}
560559
"miri_dealloc" => {
561560
default(this)?;
562561
return Ok(EmulateItemResult::NeedsReturn);
563562
}
564-
_ => unreachable!(),
563+
_ => return this.emulate_allocator(default),
565564
}
566565
}
567-
"__rust_realloc" => {
566+
name if name == mangle_internal_symbol(*this.tcx, "__rust_realloc") => {
568567
return this.emulate_allocator(|this| {
569568
// See the comment for `__rust_alloc` why `check_shim` is only called in the
570569
// default case.

0 commit comments

Comments
 (0)
Failed to load comments.