Skip to content

Commit bc8d4df

Browse files
committed
refactor dlsym: dispatch symbols via the normal shim mechanism
1 parent f9003c0 commit bc8d4df

24 files changed

+168
-461
lines changed

src/tools/miri/src/helpers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
960960
self.check_abi(abi, exp_abi)?;
961961
if let Some((body, instance)) = self.eval_context_mut().lookup_exported_symbol(link_name)? {
962962
// If compiler-builtins is providing the symbol, then don't treat it as a clash.
963-
// We'll use our built-in implementation in `emulate_foreign_item_by_name` for increased
963+
// We'll use our built-in implementation in `emulate_foreign_item_inner` for increased
964964
// performance. Note that this means we won't catch any undefined behavior in
965965
// compiler-builtins when running other crates, but Miri can still be run on
966966
// compiler-builtins itself (or any crate that uses it as a normal dependency)

src/tools/miri/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(yeet_expr)]
99
#![feature(nonzero_ops)]
1010
#![feature(round_ties_even)]
11+
#![feature(let_chains)]
1112
#![feature(lint_reasons)]
1213
#![feature(trait_upcasting)]
1314
// Configure clippy and other lints
@@ -86,9 +87,8 @@ pub use rustc_const_eval::interpret::*;
8687
// Resolve ambiguity.
8788
pub use rustc_const_eval::interpret::{self, AllocMap, PlaceTy, Provenance as _};
8889

89-
pub use crate::shims::dlsym::{Dlsym, EvalContextExt as _};
9090
pub use crate::shims::env::{EnvVars, EvalContextExt as _};
91-
pub use crate::shims::foreign_items::EvalContextExt as _;
91+
pub use crate::shims::foreign_items::{DynSym, EvalContextExt as _};
9292
pub use crate::shims::intrinsics::EvalContextExt as _;
9393
pub use crate::shims::os_str::EvalContextExt as _;
9494
pub use crate::shims::panic::{CatchUnwindData, EvalContextExt as _};

src/tools/miri/src/machine.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,7 @@ impl<'mir, 'tcx> MiriMachine<'mir, 'tcx> {
709709
"android" => {
710710
// "signal"
711711
let layout = this.machine.layouts.const_raw_ptr;
712-
let dlsym = Dlsym::from_str("signal".as_bytes(), &this.tcx.sess.target.os)?
713-
.expect("`signal` must be an actual dlsym on android");
714-
let ptr = this.fn_ptr(FnVal::Other(dlsym));
712+
let ptr = this.fn_ptr(FnVal::Other(DynSym::from_str("signal")));
715713
let val = ImmTy::from_scalar(Scalar::from_pointer(ptr, this), layout);
716714
Self::alloc_extern_static(this, "signal", val)?;
717715
// A couple zero-initialized pointer-sized extern statics.
@@ -867,7 +865,7 @@ impl<'mir, 'tcx> MiriInterpCxExt<'mir, 'tcx> for MiriInterpCx<'mir, 'tcx> {
867865
/// Machine hook implementations.
868866
impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
869867
type MemoryKind = MiriMemoryKind;
870-
type ExtraFnVal = Dlsym;
868+
type ExtraFnVal = DynSym;
871869

872870
type FrameExtra = FrameExtra<'tcx>;
873871
type AllocExtra = AllocExtra<'tcx>;
@@ -939,15 +937,15 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for MiriMachine<'mir, 'tcx> {
939937
#[inline(always)]
940938
fn call_extra_fn(
941939
ecx: &mut MiriInterpCx<'mir, 'tcx>,
942-
fn_val: Dlsym,
940+
fn_val: DynSym,
943941
abi: Abi,
944942
args: &[FnArg<'tcx, Provenance>],
945943
dest: &PlaceTy<'tcx, Provenance>,
946944
ret: Option<mir::BasicBlock>,
947-
_unwind: mir::UnwindAction,
945+
unwind: mir::UnwindAction,
948946
) -> InterpResult<'tcx> {
949947
let args = ecx.copy_fn_args(args)?; // FIXME: Should `InPlace` arguments be reset to uninit?
950-
ecx.call_dlsym(fn_val, abi, &args, dest, ret)
948+
ecx.emulate_dyn_sym(fn_val, abi, &args, dest, ret, unwind)
951949
}
952950

953951
#[inline(always)]

src/tools/miri/src/shims/dlsym.rs

Lines changed: 0 additions & 48 deletions
This file was deleted.

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

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_apfloat::Float;
66
use rustc_ast::expand::allocator::AllocatorKind;
77
use rustc_hir::{
88
def::DefKind,
9-
def_id::{CrateNum, DefId, LOCAL_CRATE},
9+
def_id::{CrateNum, LOCAL_CRATE},
1010
};
1111
use rustc_middle::middle::{
1212
codegen_fn_attrs::CodegenFnAttrFlags, dependency_format::Linkage,
@@ -25,7 +25,18 @@ use super::backtrace::EvalContextExt as _;
2525
use crate::helpers::target_os_is_unix;
2626
use crate::*;
2727

28-
/// Returned by `emulate_foreign_item_by_name`.
28+
/// Type of dynamic symbols (for `dlsym` et al)
29+
#[derive(Debug, Copy, Clone)]
30+
pub struct DynSym(Symbol);
31+
32+
#[allow(clippy::should_implement_trait)]
33+
impl DynSym {
34+
pub fn from_str(name: &str) -> Self {
35+
DynSym(Symbol::intern(name))
36+
}
37+
}
38+
39+
/// Returned by `emulate_foreign_item_inner`.
2940
pub enum EmulateByNameResult<'mir, 'tcx> {
3041
/// The caller is expected to jump to the return block.
3142
NeedsJumping,
@@ -254,15 +265,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
254265
/// is delegated to another function.
255266
fn emulate_foreign_item(
256267
&mut self,
257-
def_id: DefId,
268+
link_name: Symbol,
258269
abi: Abi,
259270
args: &[OpTy<'tcx, Provenance>],
260271
dest: &PlaceTy<'tcx, Provenance>,
261272
ret: Option<mir::BasicBlock>,
262273
unwind: mir::UnwindAction,
263274
) -> InterpResult<'tcx, Option<(&'mir mir::Body<'tcx>, ty::Instance<'tcx>)>> {
264275
let this = self.eval_context_mut();
265-
let link_name = this.item_link_name(def_id);
266276
let tcx = this.tcx.tcx;
267277

268278
// First: functions that diverge.
@@ -322,7 +332,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
322332
};
323333

324334
// Second: functions that return immediately.
325-
match this.emulate_foreign_item_by_name(link_name, abi, args, dest)? {
335+
match this.emulate_foreign_item_inner(link_name, abi, args, dest)? {
326336
EmulateByNameResult::NeedsJumping => {
327337
trace!("{:?}", this.dump_place(dest));
328338
this.go_to_block(ret);
@@ -345,6 +355,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
345355
Ok(None)
346356
}
347357

358+
/// Emulates a call to a `DynSym`.
359+
fn emulate_dyn_sym(
360+
&mut self,
361+
sym: DynSym,
362+
abi: Abi,
363+
args: &[OpTy<'tcx, Provenance>],
364+
dest: &PlaceTy<'tcx, Provenance>,
365+
ret: Option<mir::BasicBlock>,
366+
unwind: mir::UnwindAction,
367+
) -> InterpResult<'tcx> {
368+
let res = self.emulate_foreign_item(sym.0, abi, args, dest, ret, unwind)?;
369+
assert!(res.is_none(), "DynSyms that delegate are not supported");
370+
Ok(())
371+
}
372+
348373
/// Emulates calling the internal __rust_* allocator functions
349374
fn emulate_allocator(
350375
&mut self,
@@ -373,8 +398,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
373398
}
374399
}
375400

376-
/// Emulates calling a foreign item using its name.
377-
fn emulate_foreign_item_by_name(
401+
fn emulate_foreign_item_inner(
378402
&mut self,
379403
link_name: Symbol,
380404
abi: Abi,
@@ -1045,11 +1069,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
10451069
_ =>
10461070
return match this.tcx.sess.target.os.as_ref() {
10471071
target_os if target_os_is_unix(target_os) =>
1048-
shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_by_name(
1072+
shims::unix::foreign_items::EvalContextExt::emulate_foreign_item_inner(
10491073
this, link_name, abi, args, dest,
10501074
),
10511075
"windows" =>
1052-
shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_by_name(
1076+
shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_inner(
10531077
this, link_name, abi, args, dest,
10541078
),
10551079
_ => Ok(EmulateByNameResult::NotSupported),

src/tools/miri/src/shims/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub mod unix;
99
pub mod windows;
1010
mod x86;
1111

12-
pub mod dlsym;
1312
pub mod env;
1413
pub mod os_str;
1514
pub mod panic;
@@ -58,7 +57,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
5857
// foreign function
5958
// Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
6059
let args = this.copy_fn_args(args)?; // FIXME: Should `InPlace` arguments be reset to uninit?
61-
return this.emulate_foreign_item(instance.def_id(), abi, &args, dest, ret, unwind);
60+
let link_name = this.item_link_name(instance.def_id());
61+
return this.emulate_foreign_item(link_name, abi, &args, dest, ret, unwind);
6262
}
6363

6464
// Otherwise, load the MIR.

src/tools/miri/src/shims/unix/android/dlsym.rs

Lines changed: 0 additions & 54 deletions
This file was deleted.

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,26 @@ use shims::foreign_items::EmulateByNameResult;
66

77
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
88

9+
pub fn is_dyn_sym(name: &str) -> bool {
10+
matches!(name, "signal")
11+
}
12+
913
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
10-
fn emulate_foreign_item_by_name(
14+
fn emulate_foreign_item_inner(
1115
&mut self,
1216
link_name: Symbol,
13-
_abi: Abi,
14-
_args: &[OpTy<'tcx, Provenance>],
15-
_dest: &PlaceTy<'tcx, Provenance>,
17+
abi: Abi,
18+
args: &[OpTy<'tcx, Provenance>],
19+
dest: &PlaceTy<'tcx, Provenance>,
1620
) -> InterpResult<'tcx, EmulateByNameResult<'mir, 'tcx>> {
17-
let _this = self.eval_context_mut();
18-
#[allow(clippy::match_single_binding)]
21+
let this = self.eval_context_mut();
22+
1923
match link_name.as_str() {
24+
"signal" if this.frame_in_std() => {
25+
let [_sig, _func] =
26+
this.check_shim(abi, Abi::C { unwind: false }, link_name, args)?;
27+
this.write_null(dest)?;
28+
}
2029
_ => return Ok(EmulateByNameResult::NotSupported),
2130
}
2231

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
pub mod dlsym;
21
pub mod foreign_items;

src/tools/miri/src/shims/unix/dlsym.rs

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)