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

Skip the main thread's manual stack guard on Linux #43072

Merged
merged 1 commit into from
Jul 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions src/libstd/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,37 @@ pub mod guard {
as *mut libc::c_void;
}

// Rellocate the last page of the stack.
// This ensures SIGBUS will be raised on
// stack overflow.
let result = mmap(stackaddr, psize, PROT_NONE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);

if result != stackaddr || result == MAP_FAILED {
panic!("failed to allocate a guard page");
}

let offset = if cfg!(any(target_os = "linux", target_os = "freebsd")) {
2
if cfg!(target_os = "linux") {
// Linux doesn't allocate the whole stack right away, and
// the kernel has its own stack-guard mechanism to fault
// when growing too close to an existing mapping. If we map
// our own guard, then the kernel starts enforcing a rather
// large gap above that, rendering much of the possible
// stack space useless. See #43052.
//
// Instead, we'll just note where we expect rlimit to start
// faulting, so our handler can report "stack overflow", and
// trust that the kernel's own stack guard will work.
Some(stackaddr as usize)
} else {
1
};
// Reallocate the last page of the stack.
// This ensures SIGBUS will be raised on
// stack overflow.
let result = mmap(stackaddr, psize, PROT_NONE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);

if result != stackaddr || result == MAP_FAILED {
panic!("failed to allocate a guard page");
}

Some(stackaddr as usize + offset * psize)
let offset = if cfg!(target_os = "freebsd") {
2
} else {
1
};

Some(stackaddr as usize + offset * psize)
}
}

#[cfg(target_os = "solaris")]
Expand Down