Skip to content

bound the AST height of flat script expressions - #1259

Open
aysha-afrah26 wants to merge 1 commit into
BehaviorTree:masterfrom
aysha-afrah26:script-expression-height
Open

aysha-afrah26 wants to merge 1 commit into
BehaviorTree:masterfrom
aysha-afrah26:script-expression-height

Conversation

@aysha-afrah26

Copy link
Copy Markdown
Contributor

The nesting cap from #1174 limits how deep ScriptParser::parseExpr recurses, but not how tall the resulting AST gets. A left-associative chain like 1+1+1+... is consumed by the while loop in parseExpr at depth 1, yet each operator wraps the previous result as its lhs, so the tree is as tall as the expression is long. ExprBinaryArithmetic::evaluate() then recurses once per level, and so does the shared_ptr destructor chain when the AST is freed.

So a long Script, _skipIf, _while or postcondition attribute passes createTreeFromText and then takes the process down on the first tick:

<Script code="x := 1+1+1+ ... +1"/>     50,000 terms, about 100 KB
-> SIGSEGV, stack overflow in ExprBinaryArithmetic::evaluate

How long the script has to be depends on the stack of the thread that ticks the tree. In a Release build on macOS I measured about 256 bytes per level:

  • 512 KB stack (the macOS default for std::thread): crashes at 2,000 terms, roughly 4 KB of script
  • 1 MB stack: crashes at 4,000 terms
  • 8 MB main thread: crashes at 32,000 terms
  • around 1M terms crash ValidateScript itself, while the AST is destroyed

The parser now tracks the height of the expression it builds and throws a regular parse error past 1024 levels. That limit is deliberately higher than kMaxNestingDepth, so the 1,000-term flat expression that DeeplyNestedScriptIsRejected expects to be valid still parses and evaluates (I also checked it under ASan+UBSan and on a 512 KB thread). Heights add up across parentheses, so ((1+1+...)+1+...) can't sidestep the limit by splitting the chain.

The change is private to ScriptParser in script_parser.cpp, so there is no API or ABI change. The only behavior change is that a single expression taller than 1024 levels is rejected when the tree is created, instead of crashing when it runs.

ParserTest.LongFlatScriptIsRejected crashes with SIGSEGV on master and passes with the fix. The full ctest suite passes in Release and with ASan+UBSan; the one Release failure, LoggerTest.SqliteLogger_ExecSqlStatement, fails the same way on untouched master. pre-commit (clang-format, codespell) passes. clang-tidy did not run locally because clangd-21 isn't installed here.

The nesting cap in ScriptParser::parseExpr counts parser recursion only.
A flat chain such as 1+1+1+... is parsed by the loop without recursing,
but it builds a left-deep AST that evaluate() and the node destructors
walk recursively, one frame per operator, so a long Script attribute
passes tree creation and overflows the stack on the first tick.

Track the height of the expression being built and reject it past 1024
levels, so the evaluation depth no longer grows with the script length.

This branch has not been deployed

No deployments
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