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

large array on stack leads to segmentation fault #109533

Closed
umutacar opened this issue Mar 23, 2023 · 4 comments
Closed

large array on stack leads to segmentation fault #109533

umutacar opened this issue Mar 23, 2023 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@umutacar
Copy link

umutacar commented Mar 23, 2023

The following program compiles but seg faults

fn main() {
    let a:[i32;10_000_000] = [10;10_000_000];
}

It appears to be running out of stack space.

@umutacar umutacar added the C-bug Category: This is a bug. label Mar 23, 2023
@umutacar umutacar changed the title large array on stack leads to segmentation fauld large array on stack leads to segmentation fault Mar 23, 2023
@ChrisDenton
Copy link
Member

What would you like to happen?

@umutacar
Copy link
Author

Run and terminate.

@SkiFire13
Copy link
Contributor

It appears to be running out of stack space.

The stack normally has a size of ~1-2MB and you're trying to allocate an array of 40MB, of course it will overflow. Note that if you do stack overflow, you're guaranteed to segfault. The compiler inserts checks to ensure you don't skip guard pages, so the stack overflow has no risk to corrupt the memory of the rest of the program.

If you really need values this big on the stack you can use a crate like stacker to grow it to match your needs.

@scottmcm
Copy link
Member

This is rust intentionally checking for stack overflow, as it guarantees safety for that. The segfault is just the way the current implementation happens to be enforcing that safety, since terminating the process is sound.

If you want that much contiguous memory, you want a Vec:

fn main() {
    let a: Vec<i32> = vec![10; 10_000_000];
}

@scottmcm scottmcm closed this as not planned Won't fix, can't repro, duplicate, stale Mar 23, 2023
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.
Projects
None yet
Development

No branches or pull requests

4 participants