#![allow(improper_ctypes)]
#[macro_use]
extern crate cfg_if;
extern crate libc;
#[cfg(windows)]
extern crate winapi;
#[macro_use]
extern crate psm;
use std::cell::Cell;
#[inline(always)]
pub fn maybe_grow<R, F: FnOnce() -> R>(red_zone: usize, stack_size: usize, callback: F) -> R {
let enough_space = remaining_stack().map_or(false, |remaining| remaining >= red_zone);
if enough_space {
callback()
} else {
grow(stack_size, callback)
}
}
pub fn grow<R, F: FnOnce() -> R>(stack_size: usize, callback: F) -> R {
let mut ret = None;
let ret_ref = &mut ret;
_grow(stack_size, move || {
*ret_ref = Some(callback());
});
ret.unwrap()
}
pub fn remaining_stack() -> Option<usize> {
let current_ptr = current_stack_ptr();
get_stack_limit().map(|limit| current_ptr - limit)
}
psm_stack_information! (
yes {
fn current_stack_ptr() -> usize {
psm::stack_pointer() as usize
}
}
no {
#[inline(always)]
fn current_stack_ptr() -> usize {
unsafe {
let mut x = std::mem::MaybeUninit::<u8>::uninit();
x.as_mut_ptr().write_volatile(42);
x.as_ptr() as usize
}
}
}
);
thread_local! {
static STACK_LIMIT: Cell<Option<usize>> = Cell::new(unsafe {
guess_os_stack_limit()
})
}
#[inline(always)]
fn get_stack_limit() -> Option<usize> {
STACK_LIMIT.with(|s| s.get())
}
#[inline(always)]
#[allow(unused)]
fn set_stack_limit(l: Option<usize>) {
STACK_LIMIT.with(|s| s.set(l))
}
psm_stack_manipulation! {
yes {
struct StackRestoreGuard {
new_stack: *mut libc::c_void,
stack_bytes: usize,
old_stack_limit: Option<usize>,
}
impl StackRestoreGuard {
#[cfg(target_arch = "wasm32")]
unsafe fn new(stack_bytes: usize, _page_size: usize) -> StackRestoreGuard {
let layout = std::alloc::Layout::from_size_align(stack_bytes, 16).unwrap();
let ptr = std::alloc::alloc(layout);
assert!(!ptr.is_null(), "unable to allocate stack");
StackRestoreGuard {
new_stack: ptr as *mut _,
stack_bytes,
old_stack_limit: get_stack_limit(),
}
}
#[cfg(not(target_arch = "wasm32"))]
unsafe fn new(stack_bytes: usize, page_size: usize) -> StackRestoreGuard {
let new_stack = libc::mmap(
std::ptr::null_mut(),
stack_bytes,
libc::PROT_NONE,
libc::MAP_PRIVATE |
libc::MAP_ANON,
-1, 0
);
if new_stack == libc::MAP_FAILED {
panic!("unable to allocate stack")
}
let guard = StackRestoreGuard {
new_stack,
stack_bytes,
old_stack_limit: get_stack_limit(),
};
let above_guard_page = new_stack.add(page_size);
#[cfg(not(target_os = "openbsd"))]
let result = libc::mprotect(
above_guard_page,
stack_bytes - page_size,
libc::PROT_READ | libc::PROT_WRITE
);
#[cfg(target_os = "openbsd")]
let result = if libc::mmap(
above_guard_page,
stack_bytes - page_size,
libc::PROT_READ | libc::PROT_WRITE,
libc::MAP_FIXED | libc::MAP_PRIVATE | libc::MAP_ANON | libc::MAP_STACK,
-1,
0) == above_guard_page {
0
} else {
-1
};
if result == -1 {
drop(guard);
panic!("unable to set stack permissions")
}
guard
}
}
impl Drop for StackRestoreGuard {
fn drop(&mut self) {
#[cfg(target_arch = "wasm32")]
unsafe {
std::alloc::dealloc(
self.new_stack as *mut u8,
std::alloc::Layout::from_size_align_unchecked(self.stack_bytes, 16),
);
}
#[cfg(not(target_arch = "wasm32"))]
unsafe {
libc::munmap(self.new_stack, self.stack_bytes);
}
set_stack_limit(self.old_stack_limit);
}
}
fn _grow<F: FnOnce()>(stack_size: usize, callback: F) {
let page_size = page_size();
let requested_pages = stack_size
.checked_add(page_size - 1)
.expect("unreasonably large stack requested") / page_size;
let stack_pages = std::cmp::max(1, requested_pages) + 2;
let stack_bytes = stack_pages.checked_mul(page_size)
.expect("unreasonably large stack requesteed");
unsafe {
let guard = StackRestoreGuard::new(stack_bytes, page_size);
let above_guard_page = guard.new_stack.add(page_size);
set_stack_limit(Some(above_guard_page as usize));
let panic = psm::on_stack(above_guard_page as *mut _, stack_size, move || {
std::panic::catch_unwind(std::panic::AssertUnwindSafe(callback)).err()
});
drop(guard);
if let Some(p) = panic {
std::panic::resume_unwind(p);
}
}
}
fn page_size() -> usize {
#[cfg(not(target_arch = "wasm32"))]
unsafe { libc::sysconf(libc::_SC_PAGE_SIZE) as usize }
#[cfg(target_arch = "wasm32")]
{ 65536 }
}
}
no {
#[cfg(not(windows))]
fn _grow<F: FnOnce()>(stack_size: usize, callback: F) {
drop(stack_size);
callback();
}
}
}
cfg_if! {
if #[cfg(windows)] {
use std::ptr;
use std::io;
use winapi::shared::basetsd::*;
use winapi::shared::minwindef::{LPVOID, BOOL};
use winapi::shared::ntdef::*;
use winapi::um::fibersapi::*;
use winapi::um::memoryapi::*;
use winapi::um::processthreadsapi::*;
use winapi::um::winbase::*;
#[link(name="stacker")]
extern {
fn __stacker_get_current_fiber() -> PVOID;
}
struct FiberInfo<F> {
callback: std::mem::MaybeUninit<F>,
panic: Option<Box<dyn std::any::Any + Send + 'static>>,
parent_fiber: LPVOID,
}
unsafe extern "system" fn fiber_proc<F: FnOnce()>(data: LPVOID) {
let data = &mut *(data as *mut FiberInfo<F>);
let old_stack_limit = get_stack_limit();
set_stack_limit(guess_os_stack_limit());
let callback = data.callback.as_ptr();
data.panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(callback.read())).err();
set_stack_limit(old_stack_limit);
SwitchToFiber(data.parent_fiber);
return;
}
fn _grow<F: FnOnce()>(stack_size: usize, callback: F) {
unsafe {
let was_fiber = IsThreadAFiber() == TRUE as BOOL;
let mut data = FiberInfo {
callback: std::mem::MaybeUninit::new(callback),
panic: None,
parent_fiber: {
if was_fiber {
__stacker_get_current_fiber()
} else {
ConvertThreadToFiber(ptr::null_mut())
}
},
};
if data.parent_fiber.is_null() {
panic!("unable to convert thread to fiber: {}", io::Error::last_os_error());
}
let fiber = CreateFiber(
stack_size as SIZE_T,
Some(fiber_proc::<F>),
&mut data as *mut FiberInfo<F> as *mut _,
);
if fiber.is_null() {
panic!("unable to allocate fiber: {}", io::Error::last_os_error());
}
SwitchToFiber(fiber);
DeleteFiber(fiber);
if !was_fiber {
if ConvertFiberToThread() == 0 {
panic!("unable to convert back to thread: {}", io::Error::last_os_error());
}
}
if let Some(p) = data.panic {
std::panic::resume_unwind(p);
}
}
}
#[inline(always)]
fn get_thread_stack_guarantee() -> usize {
let min_guarantee = if cfg!(target_pointer_width = "32") {
0x1000
} else {
0x2000
};
let mut stack_guarantee = 0;
unsafe {
SetThreadStackGuarantee(&mut stack_guarantee)
};
std::cmp::max(stack_guarantee, min_guarantee) as usize + 0x1000
}
#[inline(always)]
unsafe fn guess_os_stack_limit() -> Option<usize> {
type QueryT = winapi::um::winnt::MEMORY_BASIC_INFORMATION;
let mut mi = std::mem::MaybeUninit::<QueryT>::uninit();
VirtualQuery(
psm::stack_pointer() as *const _,
mi.as_mut_ptr(),
std::mem::size_of::<QueryT>() as SIZE_T,
);
Some(mi.assume_init().AllocationBase as usize + get_thread_stack_guarantee() + 0x1000)
}
} else if #[cfg(any(target_os = "linux", target_os="solaris", target_os = "netbsd"))] {
unsafe fn guess_os_stack_limit() -> Option<usize> {
let mut attr = std::mem::MaybeUninit::<libc::pthread_attr_t>::uninit();
assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
assert_eq!(libc::pthread_getattr_np(libc::pthread_self(),
attr.as_mut_ptr()), 0);
let mut stackaddr = std::ptr::null_mut();
let mut stacksize = 0;
assert_eq!(libc::pthread_attr_getstack(
attr.as_ptr(), &mut stackaddr, &mut stacksize
), 0);
assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);
Some(stackaddr as usize)
}
} else if #[cfg(any(target_os = "freebsd", target_os = "dragonfly"))] {
unsafe fn guess_os_stack_limit() -> Option<usize> {
let mut attr = std::mem::MaybeUninit::<libc::pthread_attr_t>::uninit();
assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
assert_eq!(libc::pthread_attr_get_np(libc::pthread_self(), attr.as_mut_ptr()), 0);
let mut stackaddr = std::ptr::null_mut();
let mut stacksize = 0;
assert_eq!(libc::pthread_attr_getstack(
attr.as_ptr(), &mut stackaddr, &mut stacksize
), 0);
assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);
Some(stackaddr as usize)
}
} else if #[cfg(target_os = "openbsd")] {
unsafe fn guess_os_stack_limit() -> Option<usize> {
let mut stackinfo = std::mem::MaybeUninit::<libc::stack_t>::uninit();
assert_eq!(libc::pthread_stackseg_np(libc::pthread_self(), stackinfo.as_mut_ptr()), 0);
Some(stackinfo.assume_init().ss_sp as usize - stackinfo.assume_init().ss_size)
}
} else if #[cfg(target_os = "macos")] {
unsafe fn guess_os_stack_limit() -> Option<usize> {
Some(libc::pthread_get_stackaddr_np(libc::pthread_self()) as usize -
libc::pthread_get_stacksize_np(libc::pthread_self()) as usize)
}
} else {
#[inline(always)]
unsafe fn guess_os_stack_limit() -> Option<usize> {
None
}
}
}