Skip to content

Commit

Permalink
Use a more conservative way to deinit stack guard
Browse files Browse the repository at this point in the history
  • Loading branch information
ishitatsuyuki committed Mar 25, 2018
1 parent 9127990 commit d39b02c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/libstd/sys/unix/thread.rs
Expand Up @@ -223,8 +223,8 @@ pub mod guard {
#[cfg_attr(test, allow(dead_code))]
pub mod guard {
use libc;
use libc::{mmap, munmap};
use libc::{PROT_NONE, MAP_PRIVATE, MAP_ANON, MAP_FAILED, MAP_FIXED};
use libc::mmap;
use libc::{PROT_NONE, PROT_READ, PROT_WRITE, MAP_PRIVATE, MAP_ANON, MAP_FAILED, MAP_FIXED};
use ops::Range;
use sys::os;

Expand Down Expand Up @@ -347,9 +347,19 @@ pub mod guard {
pub unsafe fn deinit() {
if !cfg!(target_os = "linux") {
if let Some(stackaddr) = get_stack_start_aligned() {
// Undo the guard page mapping.
if munmap(stackaddr, PAGE_SIZE) != 0 {
panic!("unable to deallocate the guard page");
// Remove the protection on the guard page.
// FIXME: we cannot unmap the page, because when we mmap()
// above it may be already mapped by the OS, which we can't
// detect from mmap()'s return value. If we unmap this page,
// it will lead to failure growing stack size on platforms like
// macOS. Instead, just restore the page to a writable state.
// This ain't Linux, so we probably don't need to care about
// execstack.
let result = mmap(stackaddr, PAGE_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON | MAP_FIXED, -1, 0);

if result != stackaddr || result == MAP_FAILED {
panic!("unable to reset the guard page");
}
}
}
Expand Down

0 comments on commit d39b02c

Please sign in to comment.