← Back to Pull requests

LegalizeJSInterface: Pick unique names for generated stubs #9276

Closed
unknown closed this pull request without merging on Oct 20, 2026 06:28 UTC
View on GitHub
unknown unknown commented on Oct 19, 2026 09:22 UTC
Author

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.

Diff: 236 lines (+177 / -59) across 2 files Reviewer: tlively Review duration: 1266m
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
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 37.2% (MEDIUM) · Higher = larger blast radius
37.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 14.7% · Safe to merge
14.7%