Skip to content

Prevent stack overflow in JsirToAst under ThinLTO and add depth limit. - #231

Open
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1035289758
Open

copybara-service[bot] wants to merge 1 commit into
mainfrom
test_1035289758

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Prevent stack overflow in JsirToAst under ThinLTO and add depth limit.

Under ThinLTO, Clang inlined leaf JsirToAst::Visit* methods into dispatchers like VisitExpression and inlined JsirToAst::Create (with JsTrivia and its nlohmann::json extra field) into recursive expression visitors. Combined per-level frames on nested JshirConditionalExpressionOp trees expanded to ~2.5 KiB, overflowing thread stacks on deeply nested IR trees, while JshirFileToAst had no recursion depth bound.

Annotate leaf JsirToAst::Visit* declarations and JsirToAst::Create with ABSL_ATTRIBUTE_NOINLINE so JsTrivia stays off recursive caller frames and dispatchers remain compact (24 B for VisitExpression, 360 B for VisitConditionalExpression, 392 B for VisitBinaryExpression under ThinLTO). Add an IrToAst base class in ir_to_ast_util.h that tracks active recursion depth during traversal via MakeOpConverter (using a compile-time member pointer so the [this] closure stays 8 B in std::function SBO and emits a direct call) and convert IrToAstSourcePrinter, JsirToAst, and the astgen conversion tests to instance-based Visit* methods. JshirFileToAst defaults to kDefaultMaxJshirToAstRecursionDepth = 4096 (configurable via std::optional<int>) and returns absl::ResourceExhaustedError on overflow without a second traversal or thread_local state.

Under ThinLTO, Clang inlined leaf `JsirToAst::Visit*` methods into dispatchers like `VisitExpression` and inlined `JsirToAst::Create` (with `JsTrivia` and its `nlohmann::json` `extra` field) into recursive expression visitors. Combined per-level frames on nested `JshirConditionalExpressionOp` trees expanded to ~2.5 KiB, overflowing thread stacks on deeply nested IR trees, while `JshirFileToAst` had no recursion depth bound.

Annotate leaf `JsirToAst::Visit*` declarations and `JsirToAst::Create` with `ABSL_ATTRIBUTE_NOINLINE` so `JsTrivia` stays off recursive caller frames and dispatchers remain compact (24 B for `VisitExpression`, 360 B for `VisitConditionalExpression`, 392 B for `VisitBinaryExpression` under ThinLTO). Add an `IrToAst` base class in `ir_to_ast_util.h` that tracks active recursion depth during traversal via `MakeOpConverter` (using a compile-time member pointer so the `[this]` closure stays 8 B in `std::function` SBO and emits a direct call) and convert `IrToAstSourcePrinter`, `JsirToAst`, and the `astgen` conversion tests to instance-based `Visit*` methods. `JshirFileToAst` defaults to `kDefaultMaxJshirToAstRecursionDepth = 4096` (configurable via `std::optional<int>`) and returns `absl::ResourceExhaustedError` on overflow without a second traversal or `thread_local` state.

PiperOrigin-RevId: 1035289758
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant