Closed Bug 2042305 Opened 6 months ago Closed 5 months ago

Missing overrecursion checks in Modules.cpp

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

RESOLVED FIXED
153 Branch
Tracking Status
firefox153 --- fixed

People

(Reporter: jandem, Assigned: allstars.chh)

References

(Blocks 1 open bug, Regressed 1 open bug)

Details

(Keywords: ai-involved)

Attachments

(4 external files)

Summary

Four graph-traversal functions in js/src/vm/Modules.cpp recurse on the C++ stack with no AutoCheckRecursionLimit guard, even though three of their sibling traversals in the same file do carry one. On a sufficiently deep module graph each of the unguarded functions overflows the native stack (SIGSEGV / UBSan stack-overflow) instead of throwing a catchable InternalError: too much recursion.

Function Recursion check? Triggering operation
InnerModuleLoading ✅ (loading)
InnerModuleLinking ✅ (linking)
AsyncModuleExecutionRejected ✅ (async error)
ModuleGetExportedNames ❌ namespace creation
CyclicModuleResolveExport ❌ namespace creation / indirect export resolution
InnerModuleEvaluation ❌ moduleEvaluate
GatherAvailableModuleAncestors ❌ async-leaf fulfillment

The reason these aren't usually observed is incidental: a typical embedder loads-and-links an entire graph in one shot, and the loading/linking guards cap the achievable depth (~2500 frames on this build) well below what evaluation, export resolution, or async-completion can survive. That cap is bypassed by linking the chain bottom-up, incrementally: InnerModuleLoading only recurses on New modules and InnerModuleLinking returns immediately for already-Linked deps, so per-module moduleLink() calls each recurse only one level while building an arbitrarily deep, fully-linked chain.

SpiderMonkey's public API exposes JS::ModuleLink / JS::ModuleEvaluate / JS::GetModuleNamespace as independent steps and permits incremental linking, so the defect is reachable by an embedder that links incrementally. Regardless of any host's strategy, the missing guard is a code defect — these functions should carry an AutoCheckRecursionLimit like their siblings.

Test cases

All three repros confirmed crashing (SIGSEGV) on current trunk. Each prints progress before the crash, so the failing call is unambiguous.

1. ModuleGetExportedNames — star-export chain → namespace creation

// js --fuzzing-safe repro1.js   =>   SIGSEGV in ModuleGetExportedNames
var N = 200000;
var leaf = parseModule("export var x = 1;");
registerModule("m" + (N - 1), leaf);
moduleLink(leaf);
for (var i = N - 2; i >= 0; i--) {
  var m = parseModule("export * from 'm" + (i + 1) + "';");
  registerModule("m" + i, m);
  moduleLink(m);    // shallow: child already Linked
}
var head = parseModule("import * as ns from 'm0'; ns;");
registerModule("head", head);
moduleLink(head);   // namespace creation -> ModuleGetExportedNames recurses N deep

2. InnerModuleEvaluation — linked import chain → moduleEvaluate

// js --fuzzing-safe repro2.js   =>   SIGSEGV in InnerModuleEvaluation
var N = 200000;
var mods = new Array(N + 1);
for (var i = 0; i <= N; i++) {
  var src = (i < N) ? ('import "m' + (i + 1) + '";') : 'export let leaf = 1;';
  mods[i] = registerModule('m' + i, parseModule(src, 'm' + i + '.js'));
}
for (var i = N; i >= 0; i--) moduleLink(mods[i]);    // shallow
moduleEvaluate(mods[0]);   // InnerModuleEvaluation recurses N deep

3. GatherAvailableModuleAncestors — async chain → drainJobQueue

// js --fuzzing-safe repro3.js   =>   SIGSEGV in GatherAvailableModuleAncestors
var N = 500000;
var mods = new Array(N);
mods[N - 1] = registerModule('g' + (N - 1),
    parseModule('await 1; export var leaf = 1;', 'g' + (N - 1) + '.js'));
for (var i = N - 2; i >= 0; i--) {
  mods[i] = registerModule('g' + i,
      parseModule('import "g' + (i + 1) + '";', 'g' + i + '.js'));
}
for (var i = N - 1; i >= 0; i--) moduleLink(mods[i]);
for (var i = N - 1; i >= 0; i--) moduleEvaluate(mods[i]);  // shallow: dep is EvaluatingAsync
drainJobQueue();   // leaf fulfills -> GatherAvailableModuleAncestors recurses N deep

4. CyclicModuleResolveExport — no separate small test

The same function is called transitively by the export-resolution path that ModuleGetExportedNames initiates, and recurses on indirect-export entries (line 1011) and star-export entries (line 1052). Repro 1 in practice crashes in ModuleGetExportedNames first because that's the earlier call in GetOrCreateModuleNamespace; once Repro 1 is fixed, the same chain will crash here next.

The natural standalone trigger — an indirect-export chain (export {x} from "next") followed by a single resolution — is O(N²) at link time because each moduleLink() resolves the module's indirect exports by walking the rest of the chain, so it isn't practical as a small test. The defect and fix are identical to (1).

Suggested fix

Add the standard guard near the top of each unguarded function, mirroring InnerModuleLinking:

AutoCheckRecursionLimit recursion(cx);
if (!recursion.check(cx)) {
  return false;
}

Yoshi, can you take a look at this one?

Flags: needinfo?(allstars.chh)
Assignee: nobody → allstars.chh
Blocks: sm-modules
Severity: -- → S4
Flags: needinfo?(allstars.chh)
Priority: -- → P3
Duplicate of this bug: 2042963
Keywords: ai-involved
Duplicate of this bug: 2042966
Duplicate of this bug: 2043301
Duplicate of this bug: 2042967
Duplicate of this bug: 2040099
Regressions: 2045102
QA Whiteboard: [qa-triage-done-c154/b153]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: