-
-
Notifications
You must be signed in to change notification settings - Fork 3k
sema: add basic compile-time detection for stack escape violations #25286
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
Conversation
Elements are computed at comptime, so don't declare them as "var".
Catch simple but common cases of returning pointers and slices to stack-allocated memory. While not a complete solution to stack escape detection, this is still useful and has identified bugs in real-world projects. Currently detects: - Direct pointers to local variables - Pointers to struct fields of stack-allocated structs - Array-to-slice conversions of stack arrays This prevents undefined behavior from dangling stack references that would otherwise only be caught at runtime (if at all).
Given that it is legal for a function to return an undefined pointer, this constitutes a breaking language change that needs to be specified in a language change proposal, which has been rejected in #5725. Feel free to provide an argument to revive the proposal. A compiler enhancement to address this problem that does not change language semantics would be to use your same detection logic here, but rather than emitting a compile error, return undefined rather than the pointer to local. This will cause the problem to be detected at runtime in safe build modes due to the pointer address 0xaaaaaaaaaaaaaaaa being unmappable. |
You don't like my counter proposal? |
How does your proposal not violate the zen?
|
I don't really get your point. The language intentionally allows it, so it cannot be a compile error. It sounds like you want to make an argument to change the language. If that is your intention, then please go do that. Quoting the Zen is not an argument. If the reasoning in the rejected proposal is not clear, then please construct a steel man argument to the best of your ability and then argue against it. This is the path towards getting what you want. |
My statement was not an argument but genuine question. I don't mean to discuss this on a closed issue, but I do want to clarify what I'm confused about. I have carefully (I hope) read through #5725. Unfortunately none of the feedback was ever addressed so it is difficult to actually construct an argument against anything, since nothing was said counter to the points already made. My thoughts are as follows,
|
I amended #25312 to include all the test cases that were in this patch. All of them can and should be caught by ast-check rather than Sema. |
Catch simple but common cases of returning pointers and slices to stack-allocated memory.
While not a complete solution to stack escape detection, this is still useful and has identified bugs in real-world projects.
Currently detects:
This prevents undefined behavior from dangling stack references that would otherwise only be caught at runtime (if at all).