Repository navigation
wasm: free WebAssembly.validate's module copy on success - #1212
Merged
Merged
Conversation
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.
Owner
Author
|
reviewer verdict: approved at 61ad447.
|
This was referenced Nov 2, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WebAssembly.validate()leaked its private copy of the module bytes on every successful validation.What / why
tjs_wasm_validatecopies the input withwasm_runtime_mallocand loads the copy withwasm_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:LoadArgsis zero-initialised, sowasm_binary_freeableis false, and the module may point into the buffer while it is loaded. The caller has to keep the buffer alive untilwasm_runtime_unloadand 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_resolvecall from #1195 is unchanged.Introduced in 5ccd1f2.
Test
New
tests/test-wasm-validate-leak.jsbuilds an empty module with a 4 MiB custom section, asserts thatvalidate()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/statuson Linux (busyboxpson alpine has no-o rss=) andps -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 underTJS_GC_STRESS. It matches thetest-wasm-*.jsglob intests/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.N=0max RSSN=300max RSS (ok=300)N=300 INVALID=1(failure path)tjs run tests/test-wasm-validate-leak.jsRSS grew 256.6 MiB over 64 validations./build/tjs test tests/: 490/490 OK, includingtest-wasm-externref.js.pnpm run lintis clean, andclang-format(18.1.8) leaves the tree unchanged.Devteam card: t_898d5274