Skip to content

Commit

Permalink
[naga wgsl-in] Return error if wgsl parser recurses too deeply.
Browse files Browse the repository at this point in the history
It's currently trivial to write a shader that causes the wgsl parser
to recurse too deeply and overflow the stack. This patch makes the
parser return an error when recursing too deeply, before the stack
overflows.

It makes use of a new function Parser::track_recursion(). This
increments a counter returning an error if the value is too high,
before calling the user-provided function and returning its return
value after decrementing the counter again.

Any recursively-called functions can simply be modified to call
track_recursion(), providing their previous contents in a closure as
the argument. All instances of recursion during parsing call through
either Parser::statement(), Parser::unary_expression(), or
Parser::type_decl(), so only these functions have been updated as
described in order to keep the patch as unobtrusive as possible.

A value of 256 has been chosen as the recursion limit, but can be
later tweaked if required. This avoids the stack overflow in the
testcase attached to issue gfx-rs#5757.
  • Loading branch information
jamienicol committed Jan 9, 2025
1 parent 6580528 commit da2ade9
Showing 1 changed file with 424 additions and 397 deletions.
Loading

0 comments on commit da2ade9

Please sign in to comment.