]> git.lizzy.rs Git - rust.git/commit
Fix handling of wasm import modules and names
authorAlex Crichton <alex@alexcrichton.com>
Mon, 16 Dec 2019 22:15:57 +0000 (14:15 -0800)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 16 Dec 2019 22:43:46 +0000 (14:43 -0800)
commitaa0ef5a01ff329daa822e7443ca7d6ae2bfc8476
tree8936865fbcc15c6f946c06ba60dcc7869bf0d8a4
parente9469a6aec2f49fa1e2ae670649f293866932253
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
src/librustc_codegen_llvm/attributes.rs
src/librustc_codegen_utils/symbol_names.rs
src/test/run-make/wasm-symbols-different-module/Makefile [new file with mode: 0644]
src/test/run-make/wasm-symbols-different-module/bar.rs [new file with mode: 0644]
src/test/run-make/wasm-symbols-different-module/baz.rs [new file with mode: 0644]
src/test/run-make/wasm-symbols-different-module/foo.rs [new file with mode: 0644]
src/test/run-make/wasm-symbols-different-module/log.rs [new file with mode: 0644]
src/test/run-make/wasm-symbols-different-module/verify-imports.js [new file with mode: 0644]