Skip to content

Size the results vector in HostFunc::DoCall - #2917

Open
aizu-m wants to merge 1 commit into
WebAssembly:mainfrom
aizu-m:host-func-call-results
Open

aizu-m wants to merge 1 commit into
WebAssembly:mainfrom
aizu-m:host-func-call-results

Conversation

@aizu-m

@aizu-m aizu-m commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

ASan, wasm C API, calling a re-exported host import with wasm_func_call:

==214512==ERROR: AddressSanitizer: access-violation on unknown address 0x000000000000
==214512==The signal is caused by a WRITE memory access.
    #0 host_f repro.c:8
    #1 wasm_func_new::<lambda_0>::operator() interp-wasm-c-api.cc:812
    #5 wabt::interp::HostFunc::DoCall interp.cc:575
    #6 wabt::interp::Func::Call interp.cc:512
    #7 wasm_func_call interp-wasm-c-api.cc:882

How I found it: I was fuzzing the interpreter by instantiating modules and calling every export. Only exports that were re-exported imports behaved oddly, so I followed that path by hand.

What I saw:

  • A call from wasm reaches a host function through Thread::DoCall. That function builds Values results(func_type.results.size()) before it calls the host.
  • Func::Call is the public entry point. It hands the caller's results to HostFunc::DoCall unchanged, and that vector is normally empty.
  • The callback writes results[i] using the declared result arity, so it writes past an empty vector.
  • In the C API, wasm_func_new and wasm_func_new_with_env size their wasm_val_vec_t from that empty vector. The embedder's callback gets results->size == 0 with data == NULL. FromWabtValues then reads the empty vector using the declared result count.

The trigger needs nothing unusual. A module only has to re-export an import, (import "" "f" (func (result i32))) (export "f" (func 0)). An embedder that calls that export through wasm_func_call hits the bug.

Conclusion: the size belongs to the callee, so it is set in HostFunc::DoCall. On the Thread::DoCall path the vector is already the right size, so the resize does nothing there. On the direct path, the callback now gets the vector it expects. With the patch the reproducer returns 42, and the new InterpTest.HostFunc_CallReexport passes. That test fails on main with results.size() equal to 0, followed by a vector subscript assertion. All 143 unit tests and the interp file tests pass.

Thread::DoCall sizes the results vector to the callee's result count
before calling a host function, but Func::Call passes the caller's
vector through unchanged, which is empty. A host callback then writes
results[i] past the end of an empty vector. Through the wasm C API,
wasm_func_new sizes its wasm_val_vec_t from that vector, so the
embedder's callback gets results->size == 0 and data == NULL.

Resize results to the function type's result count in HostFunc::DoCall
so both call paths give the callback a correctly sized vector.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant