Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rollup of 5 pull requests #67455

Merged
merged 43 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d419a5f
Fix pointing at arg when cause is outside of call
VirrageS Dec 1, 2019
e305bf8
Rename tests and add short test description
VirrageS Dec 3, 2019
c4bbe9c
Alias `TraitItem` & `ImplItem`.
Centril Nov 30, 2019
73557fa
Use `Option` in `ImplItemKind::Const`.
Centril Dec 1, 2019
f6403c6
Use `Option` in `ImplItemKind::Method`.
Centril Dec 1, 2019
c02fd31
`TraitItemKind::Type` -> `TraitItemKind::TyAlias`.
Centril Dec 1, 2019
3907376
Unify `{Trait,Impl}ItemKind::TyAlias` structures.
Centril Dec 1, 2019
92a372b
Unify `{Impl,Trait}Item` as `AssocItem`.
Centril Dec 1, 2019
2d92aa5
Fuse associated constant parsing.
Centril Dec 1, 2019
10270bc
Fuse associated type parsing.
Centril Dec 1, 2019
7672bff
Unify associated function parsing.
Centril Dec 1, 2019
63a9030
Unify associated item parsing.
Centril Dec 1, 2019
fa828d7
Relocate `is_const_item`.
Centril Dec 1, 2019
404013e
Leave a FIXME re. `allow_plus`.
Centril Dec 1, 2019
34d9170
parse: refactor fun ret ty & param ty
Centril Dec 1, 2019
9193d7a
Unify associated item pretty printing.
Centril Dec 1, 2019
76576d4
Unify associated item mut visitors.
Centril Dec 1, 2019
c6c17e3
Simplify `nt_to_tokenstream`.
Centril Dec 1, 2019
0d8a9d7
Unify associated item visitor.
Centril Dec 1, 2019
51ccdeb
Unify associated item parsing more.
Centril Dec 1, 2019
b499a88
Unify assoc item visitors more.
Centril Dec 2, 2019
0d41d0f
Move `allow_c_varadic` logic to `ast_validation`.
Centril Dec 2, 2019
3a57a2c
`ast_validation`: move trait item logic to proper place.
Centril Dec 2, 2019
35e9e09
More c-variadic errors as semantic restrictions.
Centril Dec 2, 2019
abf2e7a
Remove `ast::{Impl,Trait}{Item,ItemKind}`.
Centril Dec 7, 2019
e52f902
`AssocImplKind::{Method -> Fn}`.
Centril Dec 7, 2019
74d4fbc
De-fatalize `...` parsing.
Centril Dec 8, 2019
054458b
make visitor uses more robust
Centril Dec 11, 2019
2d8d813
Update tokio crates to latest versions
mati865 Dec 4, 2019
aa0ef5a
Fix handling of wasm import modules and names
alexcrichton Dec 16, 2019
3a19fbf
Add Rvalue::AddressOf to MIR
matthewjasper Dec 23, 2018
35919ac
Start generating AddressOf rvalues in MIR
matthewjasper Apr 20, 2019
5fb797c
Make slice drop shims use AddressOf
matthewjasper Apr 20, 2019
7081c79
Add mir opt test for AddressOf
matthewjasper Apr 20, 2019
7b0cc6a
Check const-propagation of borrows of unsized places
matthewjasper Sep 17, 2019
1593194
Update test now that reference to pointer casts have more checks
matthewjasper Sep 17, 2019
6dcc789
Add more tests for raw_ref_op
matthewjasper Sep 18, 2019
a749116
Fix comment ordering
matthewjasper Dec 2, 2019
ef01330
Rollup merge of #64588 - matthewjasper:mir-address-of, r=oli-obk
Centril Dec 20, 2019
ba1a488
Rollup merge of #67031 - mati865:tokio-update, r=nikomatsakis
Centril Dec 20, 2019
ec82174
Rollup merge of #67131 - Centril:item-merge, r=petrochenkov
Centril Dec 20, 2019
5a8083c
Rollup merge of #67354 - VirrageS:blame-wrong-line, r=estebank
Centril Dec 20, 2019
43d1532
Rollup merge of #67363 - alexcrichton:wasm-import-modules, r=eddyb
Centril Dec 20, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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 #50021
Closes #56309
Closes #63562

[1]: https://bugs.llvm.org/show_bug.cgi?id=44316
  • Loading branch information
alexcrichton committed Dec 16, 2019
commit aa0ef5a01ff329daa822e7443ca7d6ae2bfc8476
11 changes: 11 additions & 0 deletions src/librustc_codegen_llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,17 @@ pub fn from_fn_attrs(
const_cstr!("wasm-import-module"),
&module,
);

let name = codegen_fn_attrs.link_name.unwrap_or_else(|| {
cx.tcx.item_name(instance.def_id())
});
let name = CString::new(&name.as_str()[..]).unwrap();
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
const_cstr!("wasm-import-name"),
&name,
);
}
}
}
Expand Down
28 changes: 24 additions & 4 deletions src/librustc_codegen_utils/symbol_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,32 @@ fn symbol_name(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> Symbol {
};

let attrs = tcx.codegen_fn_attrs(def_id);

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

if let Some(name) = attrs.export_name {
Expand Down
28 changes: 28 additions & 0 deletions src/test/run-make/wasm-symbols-different-module/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-include ../../run-make-fulldeps/tools.mk

# only-wasm32-bare

all:
$(RUSTC) foo.rs --target wasm32-unknown-unknown
$(NODE) verify-imports.js $(TMPDIR)/foo.wasm a/foo b/foo
$(RUSTC) foo.rs --target wasm32-unknown-unknown -C lto
$(NODE) verify-imports.js $(TMPDIR)/foo.wasm a/foo b/foo
$(RUSTC) foo.rs --target wasm32-unknown-unknown -O
$(NODE) verify-imports.js $(TMPDIR)/foo.wasm a/foo b/foo
$(RUSTC) foo.rs --target wasm32-unknown-unknown -O -C lto
$(NODE) verify-imports.js $(TMPDIR)/foo.wasm a/foo b/foo

$(RUSTC) bar.rs --target wasm32-unknown-unknown
$(NODE) verify-imports.js $(TMPDIR)/bar.wasm m1/f m1/g m2/f
$(RUSTC) bar.rs --target wasm32-unknown-unknown -C lto
$(NODE) verify-imports.js $(TMPDIR)/bar.wasm m1/f m1/g m2/f
$(RUSTC) bar.rs --target wasm32-unknown-unknown -O
$(NODE) verify-imports.js $(TMPDIR)/bar.wasm m1/f m1/g m2/f
$(RUSTC) bar.rs --target wasm32-unknown-unknown -O -C lto
$(NODE) verify-imports.js $(TMPDIR)/bar.wasm m1/f m1/g m2/f

$(RUSTC) baz.rs --target wasm32-unknown-unknown
$(NODE) verify-imports.js $(TMPDIR)/baz.wasm sqlite/allocate sqlite/deallocate

$(RUSTC) log.rs --target wasm32-unknown-unknown
$(NODE) verify-imports.js $(TMPDIR)/log.wasm test/log
33 changes: 33 additions & 0 deletions src/test/run-make/wasm-symbols-different-module/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//! Issue #50021

#![crate_type = "cdylib"]

mod m1 {
#[link(wasm_import_module = "m1")]
extern "C" {
pub fn f();
}
#[link(wasm_import_module = "m1")]
extern "C" {
pub fn g();
}
}

mod m2 {
#[link(wasm_import_module = "m2")]
extern "C" {
pub fn f(_: i32);
}
}

#[no_mangle]
pub unsafe fn run() {
m1::f();
m1::g();

// In generated code, expected:
// (import "m2" "f" (func $f (param i32)))
// but got:
// (import "m1" "f" (func $f (param i32)))
m2::f(0);
}
22 changes: 22 additions & 0 deletions src/test/run-make/wasm-symbols-different-module/baz.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! Issue #63562

#![crate_type = "cdylib"]

mod foo {
#[link(wasm_import_module = "sqlite")]
extern "C" {
pub fn allocate(size: usize) -> i32;
pub fn deallocate(ptr: i32, size: usize);
}
}

#[no_mangle]
pub extern "C" fn allocate() {
unsafe {
foo::allocate(1);
foo::deallocate(1, 2);
}
}

#[no_mangle]
pub extern "C" fn deallocate() {}
23 changes: 23 additions & 0 deletions src/test/run-make/wasm-symbols-different-module/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#![crate_type = "cdylib"]

mod a {
#[link(wasm_import_module = "a")]
extern "C" {
pub fn foo();
}
}

mod b {
#[link(wasm_import_module = "b")]
extern "C" {
pub fn foo();
}
}

#[no_mangle]
pub fn start() {
unsafe {
a::foo();
b::foo();
}
}
16 changes: 16 additions & 0 deletions src/test/run-make/wasm-symbols-different-module/log.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//! Issue #56309

#![crate_type = "cdylib"]

#[link(wasm_import_module = "test")]
extern "C" {
fn log(message_data: u32, message_size: u32);
}

#[no_mangle]
pub fn main() {
let message = "Hello, world!";
unsafe {
log(message.as_ptr() as u32, message.len() as u32);
}
}
32 changes: 32 additions & 0 deletions src/test/run-make/wasm-symbols-different-module/verify-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const fs = require('fs');
const process = require('process');
const assert = require('assert');
const buffer = fs.readFileSync(process.argv[2]);

let m = new WebAssembly.Module(buffer);
let list = WebAssembly.Module.imports(m);
console.log('imports', list);
if (list.length !== process.argv.length - 3)
throw new Error("wrong number of imports")

const imports = new Map();
for (let i = 3; i < process.argv.length; i++) {
const [module, name] = process.argv[i].split('/');
if (!imports.has(module))
imports.set(module, new Map());
imports.get(module).set(name, true);
}

for (let i of list) {
if (imports.get(i.module) === undefined || imports.get(i.module).get(i.name) === undefined)
throw new Error(`didn't find import of ${i.module}::${i.name}`);
imports.get(i.module).delete(i.name);

if (imports.get(i.module).size === 0)
imports.delete(i.module);
}

console.log(imports);
if (imports.size !== 0) {
throw new Error('extra imports');
}