Skip to content

wasm: free WebAssembly.validate's module copy on success - #1212

Merged
saghul merged 1 commit into
masterfrom
devteam/t_898d5274-wasm-validate-leak
Nov 2, 2026
Merged

saghul merged 1 commit into
masterfrom
devteam/t_898d5274-wasm-validate-leak

Conversation

@saghul

@saghul saghul commented Nov 2, 2026

Copy link
Copy Markdown
Owner

WebAssembly.validate() leaked its private copy of the module bytes on every successful validation.

What / why

tjs_wasm_validate copies the input with wasm_runtime_malloc and loads the copy with wasm_runtime_load_ex. On success it unloaded the module and returned without freeing the copy. Only the failure path freed it. The comment said WAMR "takes ownership on success", but it doesn't: LoadArgs is zero-initialised, so wasm_binary_freeable is false, and the module may point into the buffer while it is loaded. The caller has to keep the buffer alive until wasm_runtime_unload and then free it.

This PR frees the copy after the unload on both paths and fixes the comment. The copy stays because the loader may modify the buffer in place. The wasm_runtime_load_ex + no_resolve call from #1195 is unchanged.

Introduced in 5ccd1f2.

Test

New tests/test-wasm-validate-leak.js builds an empty module with a 4 MiB custom section, asserts that validate() returns true, does 4 warm-up calls and then 64 more, and checks that RSS grew by less than 64 MiB. It reads /proc/self/status on Linux (busybox ps on alpine has no -o rss=) and ps -o rss= -p <pid> elsewhere. It also checks that the input bytes are unchanged. It is skipped on Windows (no portable RSS source), under ASan (its free quarantine keeps freed chunks resident) and under TJS_GC_STRESS. It matches the test-wasm-*.js glob in tests/feature-skip.json, so WASM-off builds skip it too.

How verified (macOS x86_64, Release build)

Repro: validate a valid empty module with a 1 MiB custom section N times, using N=300 /usr/bin/time -l ./build/tjs run leak.js.

before after
N=0 max RSS 5.8 MB 5.8 MB
N=300 max RSS (ok=300) 324.1 MB 8.1 MB
N=300 INVALID=1 (failure path) 7.9 MB 7.9 MB
tjs run tests/test-wasm-validate-leak.js fails: RSS grew 256.6 MiB over 64 validations passes
  • ./build/tjs test tests/: 490/490 OK, including test-wasm-externref.js.
  • pnpm run lint is clean, and clang-format (18.1.8) leaves the tree unchanged.

Devteam card: t_898d5274

WAMR never takes ownership of the buffer passed to wasm_runtime_load_ex,
so the copy made by tjs_wasm_validate leaked on every successful
validation. Free it after unloading the module, on both paths.

Introduced in 5ccd1f2.
@saghul

saghul commented Nov 2, 2026

Copy link
Copy Markdown
Owner Author

reviewer verdict: approved at 61ad447.

  • tjs_wasm_validate now unloads first and then frees the copy on both paths. There is no free before the unload and no double free. I checked the ownership rule against the pinned WAMR: wasm_runtime_load_ex stores is_binary_freeable = args->wasm_binary_freeable, which is false here, and AOT is off (WAMR_BUILD_AOT 0). So WAMR never frees the buffer, and nothing keeps a pointer into it after the unload. The new comment says this correctly, and the no_resolve call from wasm: serialize WAMR native symbol registration across Workers #1195 is unchanged.
  • I reproduced it on a local Release build (macOS x86_64, mimalloc). With N=300 /usr/bin/time -l ./build/tjs run leak.js, max RSS was 324.1 MB before the fix (master's wasm.c swapped in) and 8.1 MB after, with ok=300 both times. N=0 was 5.8 MB and INVALID=1 was 7.9 MB, the same on both builds.
  • tests/test-wasm-validate-leak.js fails before the fix ("RSS grew … MiB", assertion at :70) and passes after. ./build/tjs test tests/ passed 490/490, including test-wasm-externref.js. pnpm run lint is clean, and the changed region of src/wasm.c is clang-format clean.
  • CI so far: Alpine Debug (mimalloc ON and OFF, the /proc path) and macOS macos-15 (Release mimalloc=OFF, Debug mimalloc=ON, the ps path) pass. ASAN (Linux) passes. ASAN (Windows) failed in test-serve-h3-listener-failure.js with "failed to create HTTP server vhost", a TCP port collision in an HTTP/3 server test. That is unrelated to this diff, and the new test exits early on Windows. It needs a re-run before landing. Other jobs were still pending when I checked.
  • Deep review: no trigger applies. The diff changes the lifetime of a buffer, not the parsing logic (2a), and it is about 75 lines in 2 files.

@saghul
saghul merged commit 0dff16b into master Nov 2, 2026
75 of 76 checks passed
@saghul
saghul deleted the devteam/t_898d5274-wasm-validate-leak branch November 2, 2026 23:23
saghul added a commit that referenced this pull request Nov 3, 2026
…rt ranges (#1216)

## What

Test-only change to `tests/test-serve-h3-listener-failure.js`: the UDP squatter now binds an explicit random port from the dynamic range (49152–65535) instead of port 0. It retries for up to 20 attempts when the UDP squat fails or `serve()` fails with `failed to create HTTP server vhost`. No runtime, C or core JS changes.

## Why

The test has flaked twice on Windows CI with `AssertionError: unexpected error: failed to create HTTP server vhost`, on PRs that don't touch it:

- #1212, ASAN (Windows): job 125589513140 (run 41536488136, attempt 1)
- #1208, Windows (Debug, mimalloc=ON): job 125580421040 (run 41533795132, attempt 1)

#1153 already added a retry loop, but each attempt took a fresh port from `tjs.listen('udp', '127.0.0.1', 0)`. On Windows, port 0 hands out sequential, nearby ephemeral ports. GitHub's Windows runners also have TCP excluded port ranges (Hyper-V/WinNAT) inside the dynamic range. When the first UDP port lands in one of those ranges, the next 9 land there too, so in both failed jobs all 10 attempts hit the TCP vhost bind error. `createHandle()` in `src/js/core/httpserver.js` already handles this case for `serve({ port: 0, http3: true })` by retrying on random dynamic-range ports. This PR does the same in the test, copying the constants rather than exporting them from core.

Other changes:
- The squatter is closed on every path, including when the loop runs out, and is never leaked from one attempt to the next.
- The `HTTP/3 (QUIC) listener` assertion message now lists every port tried and its error (`unexpected error after N attempts (ports …): …`), so the next CI failure can be diagnosed from the log.
- The three final assertions check the same things as before.

## How verified (macOS, Debug build)

- `./build/tjs run tests/test-serve-h3-listener-failure.js` passed 20 out of 20 runs in a loop.
- Retry paths, exercised with temporary copies of the test that are not committed:
  - Also holding `tjs.listen('tcp', '127.0.0.1', port)` on the first attempt: `60712 (serve: failed to create HTTP server vhost) | 63968 (serve: failed to create HTTP/3 (QUIC) listener on port 63968)`, then pass.
  - Pre-occupying the UDP port of the first pick: `62337 (udp: EADDRINUSE: address already in use) | 63296 (serve: failed to create HTTP/3 (QUIC) listener on port 63296)`, then pass.
- `make lint`: clean.
- `./build/tjs test tests/`: 493/493 OK.

The Windows behaviour can't be reproduced on macOS. This PR's CI Windows jobs are the real check.

Devteam card: t_43694f67
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