Skip to content

Commit 43d1532

Browse files
authored
Rollup merge of rust-lang#67363 - alexcrichton:wasm-import-modules, r=eddyb
Fix handling of wasm import modules and names The WebAssembly targets of rustc have weird issues around name mangling and import the same name from different modules. This all largely stems from the fact that we're using literal symbol names in LLVM IR to represent what a function is called when it's imported, and we're not using the wasm-specific `wasm-import-name` attribute. This in turn leads to two issues: * If, in the same codegen unit, the same FFI symbol is referenced twice then rustc, when translating to LLVM IR, will only reference one symbol from the first wasm module referenced. * There's also a bug in LLD [1] where even if two codegen units reference different modules, having the same symbol names means that LLD coalesces the symbols and only refers to one wasm module. Put another way, all our imported wasm symbols from the environment are keyed off their LLVM IR symbol name, which has lots of collisions today. This commit fixes the issue by implementing two changes: 1. All wasm symbols with `#[link(wasm_import_module = "...")]` are mangled by default in LLVM IR. This means they're all given unique names. 2. Symbols then use the `wasm-import-name` attribute to ensure that the WebAssembly file uses the correct import name. When put together this should ensure we don't trip over the LLD bug [1] and we also codegen IR correctly always referencing the right symbols with the right import module/name pairs. Closes rust-lang#50021 Closes rust-lang#56309 Closes rust-lang#63562 [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
2 parents 5a8083c + aa0ef5a commit 43d1532

File tree

8 files changed

+189
-4
lines changed

8 files changed

+189
-4
lines changed

src/librustc_codegen_llvm/attributes.rs

+11
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@ pub fn from_fn_attrs(
344344
const_cstr!("wasm-import-module"),
345345
&module,
346346
);
347+
348+
let name = codegen_fn_attrs.link_name.unwrap_or_else(|| {
349+
cx.tcx.item_name(instance.def_id())
350+
});
351+
let name = CString::new(&name.as_str()[..]).unwrap();
352+
llvm::AddFunctionAttrStringValue(
353+
llfn,
354+
llvm::AttributePlace::Function,
355+
const_cstr!("wasm-import-name"),
356+
&name,
357+
);
347358
}
348359
}
349360
}

src/librustc_codegen_utils/symbol_names.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,32 @@ fn symbol_name(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> Symbol {
142142
};
143143

144144
let attrs = tcx.codegen_fn_attrs(def_id);
145+
146+
// Foreign items by default use no mangling for their symbol name. There's a
147+
// few exceptions to this rule though:
148+
//
149+
// * This can be overridden with the `#[link_name]` attribute
150+
//
151+
// * On the wasm32 targets there is a bug (or feature) in LLD [1] where the
152+
// same-named symbol when imported from different wasm modules will get
153+
// hooked up incorectly. As a result foreign symbols, on the wasm target,
154+
// with a wasm import module, get mangled. Additionally our codegen will
155+
// deduplicate symbols based purely on the symbol name, but for wasm this
156+
// isn't quite right because the same-named symbol on wasm can come from
157+
// different modules. For these reasons if `#[link(wasm_import_module)]`
158+
// is present we mangle everything on wasm because the demangled form will
159+
// show up in the `wasm-import-name` custom attribute in LLVM IR.
160+
//
161+
// [1]: https://bugs.llvm.org/show_bug.cgi?id=44316
145162
if is_foreign {
146-
if let Some(name) = attrs.link_name {
147-
return name;
163+
if tcx.sess.target.target.arch != "wasm32" ||
164+
!tcx.wasm_import_module_map(def_id.krate).contains_key(&def_id)
165+
{
166+
if let Some(name) = attrs.link_name {
167+
return name;
168+
}
169+
return tcx.item_name(def_id);
148170
}
149-
// Don't mangle foreign items.
150-
return tcx.item_name(def_id);
151171
}
152172

153173
if let Some(name) = attrs.export_name {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
# only-wasm32-bare
4+
5+
all:
6+
$(RUSTC) foo.rs --target wasm32-unknown-unknown
7+
$(NODE) verify-imports.js $(TMPDIR)/foo.wasm a/foo b/foo
8+
$(RUSTC) foo.rs --target wasm32-unknown-unknown -C lto
9+
$(NODE) verify-imports.js $(TMPDIR)/foo.wasm a/foo b/foo
10+
$(RUSTC) foo.rs --target wasm32-unknown-unknown -O
11+
$(NODE) verify-imports.js $(TMPDIR)/foo.wasm a/foo b/foo
12+
$(RUSTC) foo.rs --target wasm32-unknown-unknown -O -C lto
13+
$(NODE) verify-imports.js $(TMPDIR)/foo.wasm a/foo b/foo
14+
15+
$(RUSTC) bar.rs --target wasm32-unknown-unknown
16+
$(NODE) verify-imports.js $(TMPDIR)/bar.wasm m1/f m1/g m2/f
17+
$(RUSTC) bar.rs --target wasm32-unknown-unknown -C lto
18+
$(NODE) verify-imports.js $(TMPDIR)/bar.wasm m1/f m1/g m2/f
19+
$(RUSTC) bar.rs --target wasm32-unknown-unknown -O
20+
$(NODE) verify-imports.js $(TMPDIR)/bar.wasm m1/f m1/g m2/f
21+
$(RUSTC) bar.rs --target wasm32-unknown-unknown -O -C lto
22+
$(NODE) verify-imports.js $(TMPDIR)/bar.wasm m1/f m1/g m2/f
23+
24+
$(RUSTC) baz.rs --target wasm32-unknown-unknown
25+
$(NODE) verify-imports.js $(TMPDIR)/baz.wasm sqlite/allocate sqlite/deallocate
26+
27+
$(RUSTC) log.rs --target wasm32-unknown-unknown
28+
$(NODE) verify-imports.js $(TMPDIR)/log.wasm test/log
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Issue #50021
2+
3+
#![crate_type = "cdylib"]
4+
5+
mod m1 {
6+
#[link(wasm_import_module = "m1")]
7+
extern "C" {
8+
pub fn f();
9+
}
10+
#[link(wasm_import_module = "m1")]
11+
extern "C" {
12+
pub fn g();
13+
}
14+
}
15+
16+
mod m2 {
17+
#[link(wasm_import_module = "m2")]
18+
extern "C" {
19+
pub fn f(_: i32);
20+
}
21+
}
22+
23+
#[no_mangle]
24+
pub unsafe fn run() {
25+
m1::f();
26+
m1::g();
27+
28+
// In generated code, expected:
29+
// (import "m2" "f" (func $f (param i32)))
30+
// but got:
31+
// (import "m1" "f" (func $f (param i32)))
32+
m2::f(0);
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Issue #63562
2+
3+
#![crate_type = "cdylib"]
4+
5+
mod foo {
6+
#[link(wasm_import_module = "sqlite")]
7+
extern "C" {
8+
pub fn allocate(size: usize) -> i32;
9+
pub fn deallocate(ptr: i32, size: usize);
10+
}
11+
}
12+
13+
#[no_mangle]
14+
pub extern "C" fn allocate() {
15+
unsafe {
16+
foo::allocate(1);
17+
foo::deallocate(1, 2);
18+
}
19+
}
20+
21+
#[no_mangle]
22+
pub extern "C" fn deallocate() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![crate_type = "cdylib"]
2+
3+
mod a {
4+
#[link(wasm_import_module = "a")]
5+
extern "C" {
6+
pub fn foo();
7+
}
8+
}
9+
10+
mod b {
11+
#[link(wasm_import_module = "b")]
12+
extern "C" {
13+
pub fn foo();
14+
}
15+
}
16+
17+
#[no_mangle]
18+
pub fn start() {
19+
unsafe {
20+
a::foo();
21+
b::foo();
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Issue #56309
2+
3+
#![crate_type = "cdylib"]
4+
5+
#[link(wasm_import_module = "test")]
6+
extern "C" {
7+
fn log(message_data: u32, message_size: u32);
8+
}
9+
10+
#[no_mangle]
11+
pub fn main() {
12+
let message = "Hello, world!";
13+
unsafe {
14+
log(message.as_ptr() as u32, message.len() as u32);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fs = require('fs');
2+
const process = require('process');
3+
const assert = require('assert');
4+
const buffer = fs.readFileSync(process.argv[2]);
5+
6+
let m = new WebAssembly.Module(buffer);
7+
let list = WebAssembly.Module.imports(m);
8+
console.log('imports', list);
9+
if (list.length !== process.argv.length - 3)
10+
throw new Error("wrong number of imports")
11+
12+
const imports = new Map();
13+
for (let i = 3; i < process.argv.length; i++) {
14+
const [module, name] = process.argv[i].split('/');
15+
if (!imports.has(module))
16+
imports.set(module, new Map());
17+
imports.get(module).set(name, true);
18+
}
19+
20+
for (let i of list) {
21+
if (imports.get(i.module) === undefined || imports.get(i.module).get(i.name) === undefined)
22+
throw new Error(`didn't find import of ${i.module}::${i.name}`);
23+
imports.get(i.module).delete(i.name);
24+
25+
if (imports.get(i.module).size === 0)
26+
imports.delete(i.module);
27+
}
28+
29+
console.log(imports);
30+
if (imports.size !== 0) {
31+
throw new Error('extra imports');
32+
}

0 commit comments

Comments
 (0)