1
Fork 0
This commit is contained in:
bjorn3 2024-06-30 20:27:00 +00:00
parent b754ef727c
commit 0b0ccd403b
11 changed files with 64 additions and 19 deletions

View file

@ -71,6 +71,7 @@ extern crate rustc_index;
extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
extern crate rustc_symbol_mangling;
extern crate rustc_target;
// Linking `rustc_driver` pulls in the required object code as the rest of the rustc crates are
// shipped only as rmeta files.

View file

@ -1,5 +1,7 @@
//! Provides the `extern static` that this platform expects.
use rustc_symbol_mangling::mangle_internal_symbol;
use crate::*;
impl<'tcx> MiriMachine<'tcx> {
@ -50,7 +52,11 @@ impl<'tcx> MiriMachine<'tcx> {
// "__rust_alloc_error_handler_should_panic"
let val = ecx.tcx.sess.opts.unstable_opts.oom.should_panic();
let val = ImmTy::from_int(val, ecx.machine.layouts.u8);
Self::alloc_extern_static(ecx, "__rust_alloc_error_handler_should_panic", val)?;
Self::alloc_extern_static(
ecx,
&mangle_internal_symbol(*ecx.tcx, "__rust_alloc_error_handler_should_panic"),
val,
)?;
if ecx.target_os_is_unix() {
// "environ" is mandated by POSIX.

View file

@ -12,6 +12,7 @@ use rustc_middle::mir::interpret::AllocInit;
use rustc_middle::ty::Ty;
use rustc_middle::{mir, ty};
use rustc_span::Symbol;
use rustc_symbol_mangling::mangle_internal_symbol;
use rustc_target::callconv::{Conv, FnAbi};
use self::helpers::{ToHost, ToSoft};
@ -51,7 +52,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Some shims forward to other MIR bodies.
match link_name.as_str() {
"__rust_alloc_error_handler" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_error_handler") => {
// Forward to the right symbol that implements this function.
let Some(handler_kind) = this.tcx.alloc_error_handler_kind(()) else {
// in real code, this symbol does not exist without an allocator
@ -59,9 +60,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
"`__rust_alloc_error_handler` cannot be called when no alloc error handler is set"
);
};
let name = alloc_error_handler_name(handler_kind);
let name =
mangle_internal_symbol(*this.tcx, alloc_error_handler_name(handler_kind));
let handler = this
.lookup_exported_symbol(Symbol::intern(name))?
.lookup_exported_symbol(Symbol::intern(&name))?
.expect("missing alloc error handler symbol");
return interp_ok(Some(handler));
}
@ -136,15 +138,29 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
// Find it if it was not cached.
let mut instance_and_crate: Option<(ty::Instance<'_>, CrateNum)> = None;
helpers::iter_exported_symbols(tcx, |cnum, def_id| {
if tcx.is_foreign_item(def_id) {
// Skip over imports of items
return interp_ok(());
}
let attrs = tcx.codegen_fn_attrs(def_id);
// FIXME use tcx.symbol_name(instance) instead
let symbol_name = if let Some(export_name) = attrs.export_name {
export_name
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
} else if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
|| attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
{
tcx.item_name(def_id)
} else {
// Skip over items without an explicitly defined symbol name.
return interp_ok(());
};
let symbol_name =
if attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL) {
Symbol::intern(&mangle_internal_symbol(tcx, symbol_name.as_str()))
} else {
symbol_name
};
if symbol_name == link_name {
if let Some((original_instance, original_cnum)) = instance_and_crate {
// Make sure we are consistent wrt what is 'first' and 'second'.
@ -489,7 +505,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
}
// Rust allocation
"__rust_alloc" | "miri_alloc" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc")
|| name == "miri_alloc" =>
{
let default = |ecx: &mut MiriInterpCx<'tcx>| {
// Only call `check_shim` when `#[global_allocator]` isn't used. When that
// 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> {
ecx.check_rustc_alloc_request(size, align)?;
let memory_kind = match link_name.as_str() {
"__rust_alloc" => MiriMemoryKind::Rust,
"miri_alloc" => MiriMemoryKind::Miri,
_ => unreachable!(),
_ => MiriMemoryKind::Rust,
};
let ptr = ecx.allocate_ptr(
@ -516,15 +533,14 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
};
match link_name.as_str() {
"__rust_alloc" => return this.emulate_allocator(default),
"miri_alloc" => {
default(this)?;
return interp_ok(EmulateItemResult::NeedsReturn);
}
_ => unreachable!(),
_ => return this.emulate_allocator(default),
}
}
"__rust_alloc_zeroed" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_alloc_zeroed") => {
return this.emulate_allocator(|this| {
// See the comment for `__rust_alloc` why `check_shim` is only called in the
// default case.
@ -543,7 +559,9 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.write_pointer(ptr, dest)
});
}
"__rust_dealloc" | "miri_dealloc" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_dealloc")
|| name == "miri_dealloc" =>
{
let default = |ecx: &mut MiriInterpCx<'tcx>| {
// See the comment for `__rust_alloc` why `check_shim` is only called in the
// default case.
@ -554,9 +572,8 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
let align = ecx.read_target_usize(align)?;
let memory_kind = match link_name.as_str() {
"__rust_dealloc" => MiriMemoryKind::Rust,
"miri_dealloc" => MiriMemoryKind::Miri,
_ => unreachable!(),
_ => MiriMemoryKind::Rust,
};
// 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> {
};
match link_name.as_str() {
"__rust_dealloc" => {
return this.emulate_allocator(default);
}
"miri_dealloc" => {
default(this)?;
return interp_ok(EmulateItemResult::NeedsReturn);
}
_ => unreachable!(),
_ => return this.emulate_allocator(default),
}
}
"__rust_realloc" => {
name if name == mangle_internal_symbol(*this.tcx, "__rust_realloc") => {
return this.emulate_allocator(|this| {
// See the comment for `__rust_alloc` why `check_shim` is only called in the
// default case.

View file

@ -1,4 +1,7 @@
#![feature(rustc_attrs)]
extern "Rust" {
#[rustc_std_internal_symbol]
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
}

View file

@ -2,7 +2,10 @@
// because rustc does not support alignments that large.
// https://github.com/rust-lang/miri/issues/3687
#![feature(rustc_attrs)]
extern "Rust" {
#[rustc_std_internal_symbol]
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
}

View file

@ -1,5 +1,9 @@
// Test non-power-of-two alignment.
#![feature(rustc_attrs)]
extern "Rust" {
#[rustc_std_internal_symbol]
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
}

View file

@ -3,6 +3,8 @@
// Avoid accidental synchronization via address reuse inside `thread::spawn`.
//@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0
#![feature(rustc_attrs)]
use std::thread::spawn;
#[derive(Copy, Clone)]
@ -12,6 +14,7 @@ unsafe impl<T> Send for EvilSend<T> {}
unsafe impl<T> Sync for EvilSend<T> {}
extern "Rust" {
#[rustc_std_internal_symbol]
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
}

View file

@ -3,6 +3,8 @@
// Avoid accidental synchronization via address reuse inside `thread::spawn`.
//@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0
#![feature(rustc_attrs)]
use std::thread::spawn;
#[derive(Copy, Clone)]
@ -12,6 +14,7 @@ unsafe impl<T> Send for EvilSend<T> {}
unsafe impl<T> Sync for EvilSend<T> {}
extern "Rust" {
#[rustc_std_internal_symbol]
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
}

View file

@ -3,6 +3,8 @@
// Avoid accidental synchronization via address reuse inside `thread::spawn`.
//@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0
#![feature(rustc_attrs)]
use std::thread::spawn;
#[derive(Copy, Clone)]
@ -12,6 +14,7 @@ unsafe impl<T> Send for EvilSend<T> {}
unsafe impl<T> Sync for EvilSend<T> {}
extern "Rust" {
#[rustc_std_internal_symbol]
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
}
pub fn main() {

View file

@ -3,6 +3,8 @@
// Avoid accidental synchronization via address reuse inside `thread::spawn`.
//@compile-flags: -Zmiri-address-reuse-cross-thread-rate=0
#![feature(rustc_attrs)]
use std::thread::spawn;
#[derive(Copy, Clone)]
@ -12,6 +14,7 @@ unsafe impl<T> Send for EvilSend<T> {}
unsafe impl<T> Sync for EvilSend<T> {}
extern "Rust" {
#[rustc_std_internal_symbol]
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
}
pub fn main() {

View file

@ -40,6 +40,7 @@ fn main() {
extern "Rust" {
fn bar() -> i32;
#[rustc_std_internal_symbol]
fn baz() -> i32;
fn qux() -> i32;
}
@ -63,6 +64,7 @@ fn main() {
extern "C" {
fn bar() -> i32;
#[rustc_std_internal_symbol]
fn baz() -> i32;
fn qux() -> i32;
}