-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Excessively chaining operators causes a stack overflow in rustc #109648
Comments
Technically, it did (in the Windows case). The stack overflow was caught by Rust's handler and a message emitted to explain that it had reached stack overflow, instead of simply exiting without remark. This fulfills the letter of your expectation for it to "exit gracefully", but perhaps not your intent. You may wish to clarify how graceful an exit you were hoping for. It may also be interesting to know if you somehow found a way to use this to cause a stack overflow that did not emit such a message. I am not sure how to interpret the Playground case, honestly, perhaps that qualifies... Possibly we can manage to avoid overflowing, in any case. I only make such a pedantic note because it can be hard to do better in the face of resource exhaustion. |
That's a very fair point. I had actually done the playground one first after someone on Reddit mentioned this, and only tried on windows while writing this. What's weird is that while windows was able to at least give me a general idea of what happened, the playground straight died with a SIGSEGV. |
The way the Playground one goes, I really wondered if there is something that happens due to libc??? But no, the backtrace is really thousands of lines like this:
|
Disregard that, I was talking nonsense and looking at the wrong values. Stack offset is only a bit over ~1KB but consistently is about ~1KB each time. I think the problem is more just "no attempt to guard the final page" or something like that. |
Well, I see this note in https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/stack/fn.ensure_sufficient_stack.html
So that might be a possible fix here (if it's not already called in there). |
Diagnosing stack overflows is imperfect on Linux. It's always a |
Example now compiles with... more operators, since #112673 added
Note that the signal handler dumps that huge error message on Linux because we install a custom signal handler, which feels like it was written in the Medieval Rust Era or something. rust/compiler/rustc_driver_impl/src/lib.rs Lines 1430 to 1494 in fcaf04e
On Windows, we simply wind up using std's signal handler, which prints that simple message. So I guess to resolve this bug we should improve the error message or simply delete that code. |
I have opened #113565 to make the error message less cryptic, when that merges I think we should declare victory on this. |
Victory declared. |
Excessive (extremely excessive) chaining of operators causes a stack overflow in rustc. In my experimenting I found that this happens with precisely 6115 chained operators in the playground, but it appears to depend on your compilation environment as it took approx. 8000 on my machine. This is easily avoided by splitting the chain (or having an iota of sanity), but I would expect
rustc
to at least exit gracefully in this case. The code below uses only the Not (!
) operator, but at least the Neg (-
) operator also works, as does mixing the two (ie-!-!-!
).Playground
Code
Local (Windows) Error Message
The playground error message was too long to include here, but a copy can be found in this gist: https://gist.github.com/grind086/703b519f8ff02f32b164d9388abed59b
Meta
The above code was tested in the playground (Debug, Stable, v1.68.1), and a version with 8000 chained operators crashed
rustc
on my local machine with the followingrustc --version --verbose
:The text was updated successfully, but these errors were encountered: