Issue 242316
Summary [lld][WebAssembly] `--wrap` skips signature resolution: crash with LTO, invalid module on a type mismatch
Labels lld
Assignees
Reporter aleksisch
    `wrapSymbols` moves the callers of `foo` to `__wrap_foo` (and the callers of `__real_foo` to
`foo`) by swapping symbol pointers. That skips `SymbolTable::addUndefinedFunction`, which every
other direct call goes through, so the redirected calls never have their signature filled or
checked. Two symptoms follow.

## 1. Crash: undefined wrapper of a function seen only in bitcode

`addWrappedSymbols` creates `__wrap_foo` before LTO with `foo`'s signature. When `foo` is known
only from bitcode that signature is still null, and nothing fills it after LTO. An undefined
`__wrap_foo` then crashes the link when wasm-ld builds its stub or its import.

`wrap.ll`:
```llvm
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-unknown"

declare void @f()

define void @_start() {
  call void @f()
  ret void
}
```

```sh
llvm-as wrap.ll -o wrap.o
wasm-ld wrap.o --wrap=f -o out.wasm
wasm-ld wrap.o --wrap=f --import-undefined -o out.wasm
```

```
wasm-ld: error: out.wasm.lto.o: undefined symbol: __wrap_f
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ ...
 #4 lld::make<lld::wasm::SyntheticFunction, llvm::wasm::WasmSignature const&, ...>
 #5 lld::wasm::SymbolTable::replaceWithUnreachable(...)
 #6 lld::wasm::SymbolTable::createUndefinedStub(llvm::wasm::WasmSignature const&)
 #7 lld::wasm::scanRelocations(lld::wasm::InputChunk*)
```

With `--import-undefined` (what Emscripten's `-sERROR_ON_UNDEFINED_SYMBOLS=0` passes) the link
segfaults with no message at all, in `lld::wasm::ImportSection::addImport`. `--warn-unresolved-symbols`
crashes as well. It does not depend on `f` being undefined: with `f` defined in the same
bitcode the result is the same. Without LTO (`clang --target=wasm32 -c wrap.ll`) the link reports
the undefined symbol, or imports it, as expected.

## 2. Invalid output: wrapper of a different type

The same skipped step leaves a wrapper whose type differs from the calls it takes over unchecked.
No LTO is involved.

`caller.ll`:
```llvm
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-unknown"

define void @f() {
  ret void
}

define void @_start() {
  call void @f()
  ret void
}
```

`wrapper.ll`:
```llvm
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-i128:128-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-unknown"

define void @__wrap_f(i32 %x) {
  ret void
}
```

```sh
clang --target=wasm32 -c caller.ll -o caller.o
clang --target=wasm32 -c wrapper.ll -o wrapper.o
wasm-ld caller.o wrapper.o --wrap=f -o out.wasm
```

The link succeeds with no diagnostic, and the module does not validate:

```
WebAssembly.Module(): Compiling function #0:"_start" failed:
not enough arguments on the stack for call (need 1, got 0)
```

The same mismatch without `--wrap` (a call to `g()` with `g(i32)` defined elsewhere) gives
`warning: function signature mismatch: g` and a valid module that calls a `signature_mismatch:g`
stub. A `__real_f` call of the wrong type behaves like the `--wrap` case.

## Suggested fix

Suggested fix is to call it like every other call: resolve each redirected caller through
`SymbolTable::addUndefinedFunction` with the signature its calls were compiled for, as
`ObjFile::createUndefined` does for a call by name. An empty wrapper signature is then filled
from `foo` (case 1), and a wrapper of a different type gets a `signature_mismatch` variant with
the usual warning and stub (case 2). `wrapSymbols` has to run before `handleSymbolVariants` so
that a variant it creates is resolved.

With the change, case 1 reports `undefined symbol: __wrap_f` and exits 1 (or imports / stubs it
with `--import-undefined` / `--warn-unresolved-symbols`), case 2 warns and writes a valid module,
and `lld/test/wasm` passes.

```diff
diff --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index 1c8f988e4280..d2fce810bef2 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -1232,9 +1232,19 @@ static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
 // so that wrapped symbols are swapped as instructed by the command line.
 static void wrapSymbols(ArrayRef<WrappedSymbol> wrapped) {
 DenseMap<Symbol *, Symbol *> map;
+  auto redirect = [](Symbol *from, Symbol *to) -> Symbol * {
+    const WasmSignature *sig = from->getSignature();
+ if (!sig)
+      return to;
+    return symtab->addUndefinedFunction(
+ to->getName(), std::nullopt, std::nullopt,
+ WASM_SYMBOL_UNDEFINED | (to->flags & WASM_SYMBOL_BINDING_MASK),
+ dyn_cast_or_null<ObjFile>(from->getFile()), sig,
+ /*isCalledDirectly=*/true);
+  };
   for (const WrappedSymbol &w : wrapped) {
-    map[w.sym] = w.wrap;
-    map[w.real] = w.sym;
+    map[w.sym] = redirect(w.sym, w.wrap);
+    map[w.real] = redirect(w.real, w.sym);
   }
 
   // Update pointers in input files.
@@ -1466,6 +1476,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
 
 createOptionalSymbols();
 
+  // Apply symbol renames for -wrap.
+  if (!wrapped.empty())
+    wrapSymbols(wrapped);
+
   // Resolve any variant symbols that were created due to signature
   // mismatches.
 symtab->handleSymbolVariants();
@@ -1473,10 +1487,6 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   if (errorCount())
     return;
 
-  // Apply symbol renames for -wrap.
-  if (!wrapped.empty())
-    wrapSymbols(wrapped);
-
   for (auto &iter : ctx.arg.exportedSymbols) {
     Symbol *sym = symtab->find(iter.first());
 if (sym && sym->isDefined())
```

Seen with main at 71feac71c701.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to