fix(react_compiler): avoid recursive CFG traversal stack overflows

Closed
AL
1mo ago
+162-37

Large generated React components can overflow the WASM call stack while traversing the lowered control-flow graph, even when their JSX is shallow. On current main (7d2e9ebb), the browser reproduction first traps in get_reverse_postordered_blocks::visit; making that iterative exposes another overflow in mark_predecessors::visit.

Replace both recursive traversals with explicit stacks. Reverse postorder retains fallthrough-first traversal, reverse successor visitation, and the unused-to-used revisit behavior. Predecessor marking retains forward DFS insertion order and records edges to already-visited blocks, preserving phi operand order.

Validation

  • cargo test -p oxc_react_compiler --all-features: 21 unit tests, 25 integration tests, and all 1,833 fixture snapshots pass without snapshot changes.
  • CARGO_BUILD_WARNINGS=deny cargo lint -p oxc_react_compiler and cargo fmt --package oxc_react_compiler --check pass.
  • Built napi/transform-react with pnpm build-test; all 33 package tests pass. The same 33 tests also pass when importing the built transform-react.wasip1.cjs binding directly.
  • Built release WASM before and after on the same main revision. In Chrome 155 on macOS arm64, four previously failing inputs each fail 3/3 times before and compile with memoization and no diagnostics 3/3 times after. Two successful controls produce byte-identical code before/after. This calls the upstream binding directly in fresh workers, without a bundler or fallback.
  • The 20,000-block regression aborts with stack overflow before the first change and again with only the first traversal fixed. Both traversal fixes pass. Fault-injecting reversed predecessor order / skipping unused-to-used revisits makes the corresponding semantic tests fail.

This removes these two recursion limits; it does not bound the cost of the remaining compiler passes. The 600-group stress input below completes after the fix but takes roughly 2.3 seconds and reaches 642 MiB of WASM linear-memory capacity in the browser. That is capacity, not measured resident memory. This PR does not claim to resolve the different member-expression-chain report in #27995.

Standalone reproduction

Install @oxc-transform-react/binding-wasm32-wasip1@0.155.0, save this as repro.mjs, and run node repro.mjs /path/to/transform-react.wasm32-wasip1.wasm. Tested against locally built main and patched release WASM; main throws RangeError: Maximum call stack size exceeded, patched reports fatal: false, no errors, and memoized: true. The same source reproduces in a browser worker.

import fs from 'node:fs';
import { createInstance } from '@oxc-transform-react/binding-wasm32-wasip1/workerd';

const children = Array.from({ length: 600 }, (_, i) => `
  <section data-id={getId('s${i}', React.use(Scope))}>
    <div data-id={getId('d${i}', React.use(Scope))}
      style={getStyle(getId('d${i}', React.use(Scope)),
        {opacity: frame / 30, transform: 'translateX(' + frame + 'px)'},
        React.use(Overrides))}/>
  </section>`).join('');
const source = `import React from 'react';
  const Scope = React.createContext(null), Overrides = React.createContext(null);
  const getId = (id, scope) => String(scope ?? 'root') + ':' + id;
  const getStyle = (id, style, overrides) => ({...style, ...overrides?.[id]});
  export default function App({frame}) { return <>${children}</>; }`;

const instance = await createInstance(
  await WebAssembly.compile(fs.readFileSync(process.argv[2])),
);
try {
  const result = instance.exports.transformSync('App.tsx', source, {
    jsx: 'preserve', reactCompiler: {target: '19', reportDiagnostics: true},
  });
  console.log({fatal: result.fatal, errors: result.errors,
    memoized: result.code.includes('react/compiler-runtime')});
} finally {
  await instance.dispose();
}

Why each file changes

FileReason
crates/oxc_react_compiler/src/react_compiler_lowering/hir_builder.rsReplace the two recursive graph walks and add focused long-path, ordering/cycle, and reachability regressions.

AI disclosure: Codex was used to investigate, implement, review, and test this change.