-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Stack space used by assertions ever-growing with opt-level=0, causing stack overflow #97790
Comments
A workaround on Windows is to increase the maximum stack size. For example, in a [target.x86_64-pc-windows-msvc]
rustflags = ["-C", "link-arg=/STACK:0x800000"] You could also set the |
I'm not sure this is a good workaround, as it will simply delay the issue and make it harder to notice. It's also harder to do portably and reliably. This stack overflow is not typically triggered in manually written code, but rather generated one, which can -- depending on the application -- be very long. A better workaround, that bindgen now uses, is to declare local functions and immediately call them. Stack space is released at the end of a function call. Local blocks |
Triage: Thank you for reporting. Adding optimizations to opt-level 0 would defeat the purpose of opt-level 0; fast and "straightforward" compilation. To me it does not seem unreasonable to require manually increasing the stack size with opt-level 0 for the giant example code you posted. I'll close this as won't fix, but of course feel free to reopen if you strongly disagree. |
Thank you for your answer! As stated 2 years ago, I'm not sure if increasing the stack size is a suitable workaround -- the problem just manifests itself more rarely and can cause even more debugging if tested in a slightly different configuration. Furthermore, is preventing a stack overflow during compilation really an optimization? The fact that higher opt levels avoid the overflow seems to be more a side effect than by-design. But I see of course the goal to keep generated code simple. If there is wide agreement among Rust maintainers that this isn't worth fixing, do you think it deserves a dedicated error message (not sure if imminent stack overflow is detectable at all, or if there's some context available after-the-fact). Or alternatively, some documentation about compiler limits and such behavior? (Note that I cannot reopen the issue myself, but it's probably OK given how rarely this occurs in practice. Would still be nice to hear more input on the error message/docs. Thanks!) |
In this case a good workaround seems to have been found already in rust-lang/rust-bindgen#2218 ?
If it was not an optimization we would not have the problem in opt-level 0 I think?
(safe) Rust guarantees no undefined behavior, which includes detecting stack overflow. Often it can be reported with a nice error, but not always. See https://users.rust-lang.org/t/is-rust-guaranteed-to-detect-stack-overflows/52593 for some more discussion regarding this.
Oh, thanks for letting me know. That's weird. Since you can file new issues it seems logical to allow you to re-open your own issues. Anyway, then let's keep discussing until we agree on what to do with this issue. |
When using bindgen, I ran into the issue that the generated tests -- which can contain a lot of
assert_eq!
statements in one function -- seem to linearly allocate stack space, not reusing space from a previous independent assertion. With a sufficient number of statements, this causes a stack overflow.The problem does not occur with
RUSTFLAGS=-C opt-level=2
.I made a reproducible example in this Gist. Couldn't paste it here in a
<details>
block due to character limit.Downstream issue: rust-lang/rust-bindgen#2218.
Found a similar issue #34283, but it's probably not directly related.
I'm not sure if the current behavior is by design (possibly excessive stack usage as a result of no optimizations), but it is a problem for many code-generation scenarios.
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: