Skip to content

Commit cfd52bb

Browse files
committed
rustc: Stop passing --allow-undefined on wasm targets
This commit updates how the linker is invoked on WebAssembly targets (all of them) to avoid passing the `--allow-undefined` flag to the linker. Historically, if I remember this correctly, when `wasm-ld` was first integrated this was practically required because at the time it was otherwise impossible to import a function from the host into a wasm binary. Or, at least, I'm pretty sure that was why this was added. At the time, as the documentation around this option indicates, it was known that this was going to be a hazard. This doesn't match behavior on native, for example, and can easily paper over what should be a linker error with some sort of other obscure runtime error. An example is that this program currently compiles and links, it just prints null: unsafe extern "C" { static nonexistent: u8; } fn main() { println!("{:?}", &raw const nonexistent); } This can easily lead to mistakes like rust-lang/libc/4880 and defer what should be a compile-time link error to weird or unusual behavior at link time. Additionally, in the intervening time since `wasm-ld` was first introduced here, lots has changed and notably this program works as expected: #[link(wasm_import_module = "host")] unsafe extern "C" { fn foo(); } fn main() { unsafe { foo(); } } Notably this continues to compile without error and the final wasm binary indeed has an imported function from the host. What this change means, however, is that this program: unsafe extern "C" { fn foo(); } fn main() { unsafe { foo(); } } this currently compiles successfully and emits an import from the `env` module. After this change, however, this will fail to compile with a link error stating that the `foo` symbol is not defined.
1 parent 198328a commit cfd52bb

File tree

4 files changed

+8
-10
lines changed

4 files changed

+8
-10
lines changed

compiler/rustc_target/src/spec/base/wasm.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ pub(crate) fn options() -> TargetOptions {
2828
// stack overflow will be guaranteed to trap as it underflows instead of
2929
// corrupting static data.
3030
concat!($prefix, "--stack-first"),
31-
// FIXME we probably shouldn't pass this but instead pass an explicit list
32-
// of symbols we'll allow to be undefined. We don't currently have a
33-
// mechanism of knowing, however, which symbols are intended to be imported
34-
// from the environment and which are intended to be imported from other
35-
// objects linked elsewhere. This is a coarse approximation but is sure to
36-
// hide some bugs and frustrate someone at some point, so we should ideally
37-
// work towards a world where we can explicitly list symbols that are
38-
// supposed to be imported and have all other symbols generate errors if
39-
// they remain undefined.
40-
concat!($prefix, "--allow-undefined"),
4131
// LLD only implements C++-like demangling, which doesn't match our own
4232
// mangling scheme. Tell LLD to not demangle anything and leave it up to
4333
// us to demangle these symbols later. Currently rustc does not perform

tests/run-make/wasm-stringify-ints-small/foo.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
#![crate_type = "cdylib"]
22

3+
#[link(wasm_import_module = "the-world")]
34
extern "C" {
45
fn observe(ptr: *const u8, len: usize);
56
}
67

78
macro_rules! s {
89
( $( $f:ident -> $t:ty );* $(;)* ) => {
910
$(
11+
#[link(wasm_import_module = "the-world")]
1012
extern "C" {
1113
fn $f() -> $t;
1214
}

tests/run-make/wasm-unexpected-features/foo.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ unsafe extern "Rust" {
1414
fn __rust_alloc_error_handler(size: usize, align: usize) -> !;
1515
}
1616

17+
#[rustc_std_internal_symbol]
18+
pub unsafe fn __rdl_alloc_error_handler(_size: usize, _align: usize) -> ! {
19+
loop {}
20+
}
21+
1722
#[used]
1823
static mut BUF: [u8; 1024] = [0; 1024];
1924

tests/ui/linking/executable-no-mangle-strip.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//@ run-pass
22
//@ ignore-windows-gnu: only statics marked with used can be GC-ed on windows-gnu
3+
//@ ignore-wasm: wasm, for better or worse, exports all #[no_mangle]
34

45
// Regression test for <https://github.com/rust-lang/rust/issues/139744>.
56
// Functions in the binary marked with no_mangle should be GC-ed if they

0 commit comments

Comments
 (0)