Skip to content
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

Huge stack arrays cause stack overflow with segmentation fault #40862

Closed
dns2utf8 opened this issue Mar 27, 2017 · 5 comments
Closed

Huge stack arrays cause stack overflow with segmentation fault #40862

dns2utf8 opened this issue Mar 27, 2017 · 5 comments
Labels
C-bug Category: This is a bug. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@dns2utf8
Copy link
Contributor

I noticed a 4MB stack allocated buffer within a thread causes the thread to panic.
In the main it takes 8MB to crash on amd64: Run on play

The strange part is, even when I box it, it still segfaults: Run on Play

Am I using the Box wrong? Why does it SEGV at all? In my understanding allocating 8MB at once is not such a big deal anymore.

@bstrie
Copy link
Contributor

bstrie commented Mar 27, 2017

The ability to guarantee in-place heap construction is tracked by #27779 , and the ability to guarantee that real stack overflows properly abort (rather than segfault) is blocked by #16012 .

@dns2utf8
Copy link
Contributor Author

Well this is very unfortunate. But how does this affect boxed values like Box::new( [0u8, 1 << 24] )?

@shepmaster
Copy link
Member

But how does this affect boxed values like

In debug mode, the array is still built on the stack, then moved to the Box. That's what @bstrie means by "guarantee in-place heap construction" — if that was present, there should be a way to guarantee that the array is never placed on the stack.

In release mode, I think this will sometimes correctly avoid the stack allocation.

In the mean time, you are encouraged to use into_boxed_slice (playground):

fn main() {
    vec![0u8; 1 << 24].into_boxed_slice();
}

@shepmaster
Copy link
Member

I noticed a 4MB stack allocated buffer within a thread causes the thread to panic. In the main it takes 8MB to crash

From a relevant Stack Overflow question:

The default stack size of the main thread is typically 8 MiB, so that array is going to occupy half of the available stack. That's a lot, but there's still room available, so the benchmark runs normally.

The stack size of a new thread, however, is typically much smaller. On Linux it is 2 MiB, and other platforms could be even smaller. So, your 4 MiB array easily overflows the thread's stack and causes a stack overflow / segfault.

@Mark-Simulacrum Mark-Simulacrum added I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 20, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 27, 2017
@alexcrichton
Copy link
Member

I believe this has since been fixed now that stack probes are implemented, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants