Address review comments

This commit is contained in:
Tatsuyuki Ishi 2018-03-18 23:02:06 +09:00
parent 1bb89f1b3c
commit a185b56b7c
4 changed files with 39 additions and 12 deletions

View File

@ -1476,13 +1476,15 @@ pub fn in_rustc_thread<F, R>(f: F) -> Result<R, Box<Any + Send>>
} else if rlim.rlim_max < STACK_SIZE as libc::rlim_t {
true
} else {
std::rt::deinit_stack_guard();
rlim.rlim_cur = STACK_SIZE as libc::rlim_t;
if libc::setrlimit(libc::RLIMIT_STACK, &mut rlim) != 0 {
let err = io::Error::last_os_error();
error!("in_rustc_thread: error calling setrlimit: {}", err);
true
// We have already deinited the stack. Further corruption is
// not allowed.
panic!("in_rustc_thread: error calling setrlimit: {}", err);
} else {
std::thread::update_stack_guard();
std::rt::update_stack_guard();
false
}
}

View File

@ -73,3 +73,18 @@ fn lang_start<T: ::process::Termination + 'static>
{
lang_start_internal(&move || main().report(), argc, argv)
}
/// Function used for reverting changes to the main stack before setrlimit().
/// This is POSIX (non-Linux) specific and unlikely to be directly stabilized.
#[unstable(feature = "rustc_stack_internals", issue = "0")]
pub unsafe fn deinit_stack_guard() {
::sys::thread::guard::deinit();
}
/// Function used for resetting the main stack guard address after setrlimit().
/// This is POSIX specific and unlikely to be directly stabilized.
#[unstable(feature = "rustc_stack_internals", issue = "0")]
pub unsafe fn update_stack_guard() {
let main_guard = ::sys::thread::guard::init();
::sys_common::thread_info::reset_guard(main_guard);
}

View File

@ -222,7 +222,7 @@ pub mod guard {
#[cfg_attr(test, allow(dead_code))]
pub mod guard {
use libc;
use libc::mmap;
use libc::{mmap, munmap};
use libc::{PROT_NONE, MAP_PRIVATE, MAP_ANON, MAP_FAILED, MAP_FIXED};
use ops::Range;
use sys::os;
@ -336,6 +336,24 @@ pub mod guard {
}
}
pub unsafe fn deinit() {
if !cfg!(target_os = "linux") {
if let Some(mut stackaddr) = get_stack_start() {
// Ensure address is aligned. Same as above.
let remainder = (stackaddr as usize) % PAGE_SIZE;
if remainder != 0 {
stackaddr = ((stackaddr as usize) + PAGE_SIZE - remainder)
as *mut libc::c_void;
}
// Undo the guard page mapping.
if munmap(stackaddr, PAGE_SIZE) != 0 {
panic!("unable to deallocate the guard page");
}
}
}
}
#[cfg(any(target_os = "macos",
target_os = "bitrig",
target_os = "openbsd",

View File

@ -208,14 +208,6 @@ pub use self::local::{LocalKey, AccessError};
#[unstable(feature = "libstd_thread_internals", issue = "0")]
#[doc(hidden)] pub use self::local::os::Key as __OsLocalKeyInner;
/// Function used for resetting the main stack guard address after setrlimit().
/// This is POSIX specific and unlikely to be directly stabilized.
#[unstable(feature = "rustc_stack_internals", issue = "0")]
pub unsafe fn update_stack_guard() {
let main_guard = imp::guard::init();
thread_info::reset_guard(main_guard);
}
////////////////////////////////////////////////////////////////////////////////
// Builder
////////////////////////////////////////////////////////////////////////////////