-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[wgsl-in] Enforce WGSL statement nesting depth limit #4352
Comments
Related to gpuweb/gpuweb#1959 (comment) |
Hmm... that lists a minimum limit of 1023 for control flow. If 230 empty blocks already cause an overflow, some optimizations may be needed (since I assume translation of SPIR-V to WGSL should support these kind of cases below the minimum limits). |
Did you try running in release? This makes a big difference to our Metal backend. |
Hmm when running (edit: I kinda should have thought of trying release mode, thanks for reminding me btw.) |
We can proceed in one of the 2 ways:
|
With regards to the first point, since WGSL is meant to translate well into SPIR-V, it'd probably be best to just adapt the limits of that as good as possible. With regards to the second point, I think that that might be quite a bit of an endeavor, if it is possible at all, since certain parts of the grammar inherently require the use of a stack, like blocks and expressions (be that through recursion or more directly). Do you already have any particular ideas regarding how such a rewrite could be accomplished / in what way the code would be structured differently from the current code? I'd be really quite interested in that, as in my mind currently parsing is rather strongly associated with recursive parsing functions mimicking the grammar of the language, making use of the call stack for context and partial results. Perhaps a more direct use of a dedicated stack on the heap? |
It's just that there is no direct mapping between a WGSL block and any limits in SPIR-V, if I understand correctly.
Yes, that would be a dedicated stack on the heap. |
It'd be nice to know whether this even happens any more in the new front end. |
I reproduced this on Linux with:
|
I hit this with a shader generated with |
How did you hit this with |
Heya @teoxoy - It actually happened in the WGSL frontend. I have a test that takes all of my shaders generated by |
WGSL says:
If we enforced this limit, I don't think we would blow the stack. |
WGSL doesn't seem to specify a limit on expression nesting depth, but given that lowering does recurse, Naga may need to impose its own limit. I think this is permitted by the spec as an uncategorized error. |
Bumping priority as this is a blocker for fuzzing the WGSL front end. |
Running the wgsl_fuzzer triggers a related crash:
|
Updated source location for recursion between statements and blocks: wgpu/naga/src/front/wgsl/parse/mod.rs Lines 1630 to 1638 in 9f91c95
|
Posting some answers to questions asked in Mozilla-private chat:
|
A program like the following
will cause naga to overflow the stack as it recurses in
https://github.com/gfx-rs/naga/blob/db845347f23f3a00add894d38cf62d28f47669ed/src/front/wgsl/mod.rs#L2987-L3012
There should probably be some arbitrary but fixed limit to nesting which is checked with a counter. Programs exceeding that should cause naga to fail safely with an appropriate error message.
Edit: Since this fails at a depth of ~230 for me, I'd suggest going for something like 64 (which is already pretty ridiculous anyways, but I think nobody should be complaining with 64, at least not about it being too little).
The text was updated successfully, but these errors were encountered: