LegalizeJSInterface: Pick unique names for generated stubs #9276
All Vouch review confidence checks passed
Residual risk 0.15 = Change Risk (0.37) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 236 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.327
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #9276"
general
weight: 0.35
tlively
Fixes #9243
LegalizeJSInterface could read freed memory when the module already contained a function with the name it wanted to give a generated stub.
Cause
For each illegal import, makeLegalStubForCalledImport builds two functions, a stub named legalfunc$ plus the import name and an import named legalimport$ plus the import name, and returns a raw pointer to the stub. The caller stores that pointer in illegalImportsToLegal, and Fixer::visitCall later uses it to rewrite calls.
Both functions were only added to the module when no function with that name already existed. When one did exist (in the issue, a user function called $legalfunc$imp), the stub was not added, so its unique_ptr freed it when the function returned. The pointer saved in illegalImportsToLegal then pointed at freed memory, and Fixer::visitCall read it. That's the heap-use-after-free in the report, and with a Release build the repro segfaults.
Fix
The stub and the legalimport function now get their names from Names::getValidFunctionName, which returns the requested name if it's free and otherwise adds a numeric suffix. Since the names are now always unique, both functions are always added and the "only if absent" checks are gone, so the stored pointer always refers to a function owned by the module. makeLegalStubForCalledImport is called once per illegal import, so this doesn't create duplicate stubs. Modules without a name collision get the same names as before.
Test
I added test/lit/passes/legalize-js-interface-name-collision.wast, based on the repro from the issue plus a colliding $legalimport$imp, with CHECK lines generated by scripts/update_lit_checks.py. With the fix the output uses $legalfunc$imp_4 and $legalimport$imp_4 and the pass completes, while on main the same input crashes wasm-opt. The existing legalize-js-interface lit tests still pass, and git clang-format is clean.