Skip to content

Commit 804a6e3

Browse files
committed
Move wasm_import_module_map handling into shims/wasi
1 parent 092ffea commit 804a6e3

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

src/machine.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,24 +1243,16 @@ impl<'tcx> Machine<'tcx> for MiriMachine<'tcx> {
12431243
// foreign function
12441244
// Any needed call to `goto_block` will be performed by `emulate_foreign_item`.
12451245
let args = ecx.copy_fn_args(args); // FIXME: Should `InPlace` arguments be reset to uninit?
1246-
1247-
let link_name = if ecx.tcx.sess.target.is_like_wasm
1248-
&& let Some(module) =
1249-
ecx.tcx.wasm_import_module_map(instance.def_id().krate).get(&instance.def_id())
1250-
{
1251-
// Adapted from https://github.com/rust-lang/rust/blob/90b65889799733f21ebdf59d96411aa531c5900a/compiler/rustc_codegen_llvm/src/attributes.rs#L549-L562
1252-
let codegen_fn_attrs = ecx.tcx.codegen_instance_attrs(instance.def);
1253-
let name = codegen_fn_attrs
1254-
.symbol_name
1255-
.unwrap_or_else(|| ecx.tcx.item_name(instance.def_id()));
1256-
// $$$ is unlikely to occur in either the import module name or item name, so use it
1257-
// as a separator here. It will be split again in emulate_foreign_item_inner for wasi.
1258-
Symbol::intern(&format!("{}$$${}", module, name))
1259-
} else {
1260-
Symbol::intern(ecx.tcx.symbol_name(instance).name)
1261-
};
1262-
1263-
return ecx.emulate_foreign_item(link_name, abi, &args, dest, ret, unwind);
1246+
let link_name = Symbol::intern(ecx.tcx.symbol_name(instance).name);
1247+
return ecx.emulate_foreign_item(
1248+
Some(instance),
1249+
link_name,
1250+
abi,
1251+
&args,
1252+
dest,
1253+
ret,
1254+
unwind,
1255+
);
12641256
}
12651257

12661258
if ecx.machine.data_race.as_genmc_ref().is_some()

src/shims/foreign_items.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
4343
/// is delegated to another function.
4444
fn emulate_foreign_item(
4545
&mut self,
46+
instance: Option<Instance<'tcx>>,
4647
link_name: Symbol,
4748
abi: &FnAbi<'tcx, Ty<'tcx>>,
4849
args: &[OpTy<'tcx>],
@@ -73,7 +74,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
7374
let dest = this.force_allocation(dest)?;
7475

7576
// The rest either implements the logic, or falls back to `lookup_exported_symbol`.
76-
match this.emulate_foreign_item_inner(link_name, abi, args, &dest)? {
77+
match this.emulate_foreign_item_inner(instance, link_name, abi, args, &dest)? {
7778
EmulateItemResult::NeedsReturn => {
7879
trace!("{:?}", this.dump_place(&dest.clone().into()));
7980
this.return_to_block(ret)?;
@@ -118,7 +119,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
118119
ret: Option<mir::BasicBlock>,
119120
unwind: mir::UnwindAction,
120121
) -> InterpResult<'tcx> {
121-
let res = self.emulate_foreign_item(sym.0, abi, args, dest, ret, unwind)?;
122+
let res = self.emulate_foreign_item(None, sym.0, abi, args, dest, ret, unwind)?;
122123
assert!(res.is_none(), "DynSyms that delegate are not supported");
123124
interp_ok(())
124125
}
@@ -248,6 +249,7 @@ impl<'tcx> EvalContextExtPriv<'tcx> for crate::MiriInterpCx<'tcx> {}
248249
trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
249250
fn emulate_foreign_item_inner(
250251
&mut self,
252+
instance: Option<Instance<'tcx>>,
251253
link_name: Symbol,
252254
abi: &FnAbi<'tcx, Ty<'tcx>>,
253255
args: &[OpTy<'tcx>],
@@ -848,7 +850,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
848850
),
849851
"wasi" =>
850852
shims::wasi::foreign_items::EvalContextExt::emulate_foreign_item_inner(
851-
this, link_name, abi, args, dest,
853+
this, instance, link_name, abi, args, dest,
852854
),
853855
"windows" =>
854856
shims::windows::foreign_items::EvalContextExt::emulate_foreign_item_inner(

src/shims/wasi/foreign_items.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_abi::CanonAbi;
2-
use rustc_middle::ty::Ty;
2+
use rustc_middle::ty::{Instance, Ty};
33
use rustc_span::Symbol;
44
use rustc_target::callconv::FnAbi;
55

@@ -14,27 +14,38 @@ impl<'tcx> EvalContextExt<'tcx> for crate::MiriInterpCx<'tcx> {}
1414
pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
1515
fn emulate_foreign_item_inner(
1616
&mut self,
17+
instance: Option<Instance<'tcx>>,
1718
link_name: Symbol,
1819
abi: &FnAbi<'tcx, Ty<'tcx>>,
1920
args: &[OpTy<'tcx>],
2021
dest: &MPlaceTy<'tcx>,
2122
) -> InterpResult<'tcx, EmulateItemResult> {
2223
let this = self.eval_context_mut();
2324

24-
let (interface, name) = if let Some((module, name)) = link_name.as_str().split_once("$$$") {
25+
let (interface, name) = if let Some(instance) = instance
26+
&& let Some(module) =
27+
this.tcx.wasm_import_module_map(instance.def_id().krate).get(&instance.def_id())
28+
{
29+
// Adapted from https://github.com/rust-lang/rust/blob/90b65889799733f21ebdf59d96411aa531c5900a/compiler/rustc_codegen_llvm/src/attributes.rs#L549-L562
30+
let codegen_fn_attrs = this.tcx.codegen_instance_attrs(instance.def);
31+
let name = codegen_fn_attrs
32+
.symbol_name
33+
.unwrap_or_else(|| this.tcx.item_name(instance.def_id()));
34+
2535
// According to the component model, the version should be matched as semver, but for
2636
// simplicity we strip the version entirely for now. Once we support wasm-wasip3 it may
2737
// become actually important to match on the version, but for now it shouldn't matter.
28-
let (module, _version) = module
38+
let (interface, _version) = module
2939
.split_once('@')
3040
.ok_or_else(|| err_unsup_format!("module name {module} must contain a version"))?;
31-
(Some(module), name)
41+
42+
(Some(interface), name)
3243
} else {
3344
// This item is provided by wasi-libc, not imported from the wasi runtime
34-
(None, link_name.as_str())
45+
(None, link_name)
3546
};
3647

37-
match (interface, name) {
48+
match (interface, name.as_str()) {
3849
// Allocation
3950
(None, "posix_memalign") => {
4051
let [memptr, align, size] =

0 commit comments

Comments
 (0)