#[inline(always)]
pub fn sp() -> usize {
sp_impl()
}
#[cfg(target_arch = "x86_64")]
#[inline(always)]
fn sp_impl() -> usize {
let sp: usize;
unsafe {
core::arch::asm!("mov {}, rsp", out(reg) sp, options(nomem, nostack, preserves_flags));
}
sp
}
#[cfg(target_arch = "x86")]
#[inline(always)]
fn sp_impl() -> usize {
let sp: usize;
unsafe {
core::arch::asm!("mov {}, esp", out(reg) sp, options(nomem, nostack, preserves_flags));
}
sp
}
#[cfg(any(target_arch = "aarch64", target_arch = "arm"))]
#[inline(always)]
fn sp_impl() -> usize {
let sp: usize;
unsafe {
core::arch::asm!("mov {}, sp", out(reg) sp, options(nomem, nostack, preserves_flags));
}
sp
}
#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
#[inline(always)]
fn sp_impl() -> usize {
let sp: usize;
unsafe {
core::arch::asm!("mv {}, sp", out(reg) sp, options(nomem, nostack, preserves_flags));
}
sp
}
#[cfg(not(any(
target_arch = "x86",
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "arm",
target_arch = "riscv32",
target_arch = "riscv64",
)))]
#[inline(never)]
fn sp_impl() -> usize {
let probe = 0usize;
core::hint::black_box(&probe) as *const usize as usize
}
#[cfg(feature = "std")]
pub fn current_bounds() -> Option<(usize, usize)> {
bounds_impl()
}
#[cfg(all(
feature = "std",
any(target_os = "linux", target_os = "android", target_os = "fuchsia")
))]
fn bounds_impl() -> Option<(usize, usize)> {
let mut attr = core::mem::MaybeUninit::<libc::pthread_attr_t>::uninit();
unsafe {
if libc::pthread_getattr_np(libc::pthread_self(), attr.as_mut_ptr()) != 0 {
return None;
}
let mut addr: *mut libc::c_void = core::ptr::null_mut();
let mut size: libc::size_t = 0;
let rc = libc::pthread_attr_getstack(attr.as_ptr(), &mut addr, &mut size);
libc::pthread_attr_destroy(attr.as_mut_ptr());
if rc != 0 || addr.is_null() || size == 0 {
return None;
}
let limit = addr as usize;
let base = limit.checked_add(size)?;
Some((base, trustworthy_limit(base, limit)))
}
}
#[cfg(all(feature = "std", any(target_os = "linux", target_os = "android")))]
const TRUSTED_STACK_MAX: usize = 1024 * 1024 * 1024;
#[cfg(all(feature = "std", any(target_os = "linux", target_os = "android")))]
const ASSUMED_MAIN_STACK: usize = 8 * 1024 * 1024;
#[cfg(all(feature = "std", any(target_os = "linux", target_os = "android")))]
fn trustworthy_limit(base: usize, limit: usize) -> usize {
if base - limit <= TRUSTED_STACK_MAX || !is_initial_thread() {
return limit;
}
cap_limit(base, limit, stack_rlimit().unwrap_or(ASSUMED_MAIN_STACK))
}
#[cfg(all(feature = "std", target_os = "fuchsia"))]
fn trustworthy_limit(_base: usize, limit: usize) -> usize {
limit
}
#[cfg(all(feature = "std", any(target_os = "linux", target_os = "android")))]
const fn cap_limit(base: usize, limit: usize, cap: usize) -> usize {
if base - limit > cap {
base - cap
} else {
limit
}
}
#[cfg(all(feature = "std", any(target_os = "linux", target_os = "android")))]
fn is_initial_thread() -> bool {
unsafe { libc::syscall(libc::SYS_gettid) as libc::pid_t == libc::getpid() }
}
#[cfg(all(feature = "std", any(target_os = "linux", target_os = "android")))]
fn stack_rlimit() -> Option<usize> {
let mut limit = core::mem::MaybeUninit::<libc::rlimit>::uninit();
let limit = unsafe {
if libc::getrlimit(libc::RLIMIT_STACK, limit.as_mut_ptr()) != 0 {
return None;
}
limit.assume_init()
};
if limit.rlim_cur == libc::RLIM_INFINITY {
return None;
}
usize::try_from(limit.rlim_cur).ok()
}
#[cfg(all(feature = "std", target_vendor = "apple"))]
fn bounds_impl() -> Option<(usize, usize)> {
let (base, size) = unsafe {
let this = libc::pthread_self();
(
libc::pthread_get_stackaddr_np(this) as usize,
libc::pthread_get_stacksize_np(this),
)
};
if base == 0 || size == 0 {
return None;
}
Some((base, base.checked_sub(size)?))
}
#[cfg(all(feature = "std", windows))]
fn bounds_impl() -> Option<(usize, usize)> {
#[link(name = "kernel32")]
unsafe extern "system" {
fn GetCurrentThreadStackLimits(low: *mut usize, high: *mut usize);
}
let mut low = 0usize;
let mut high = 0usize;
unsafe { GetCurrentThreadStackLimits(&mut low, &mut high) };
if low == 0 || high <= low {
return None;
}
Some((high, low))
}
#[cfg(all(
feature = "std",
not(any(
target_os = "linux",
target_os = "android",
target_os = "fuchsia",
target_vendor = "apple",
windows,
))
))]
fn bounds_impl() -> Option<(usize, usize)> {
None
}
#[cfg(feature = "std")]
pub fn thread_id() -> u64 {
thread_id_impl()
}
#[cfg(all(feature = "std", any(target_os = "linux", target_os = "android")))]
fn thread_id_impl() -> u64 {
let tid = unsafe { libc::syscall(libc::SYS_gettid) };
if tid < 0 { 0 } else { tid as u64 }
}
#[cfg(all(
feature = "std",
unix,
not(any(target_os = "linux", target_os = "android"))
))]
fn thread_id_impl() -> u64 {
(unsafe { libc::pthread_self() }) as usize as u64
}
#[cfg(all(feature = "std", windows))]
fn thread_id_impl() -> u64 {
#[link(name = "kernel32")]
unsafe extern "system" {
fn GetCurrentThreadId() -> u32;
}
u64::from(unsafe { GetCurrentThreadId() })
}
#[cfg(all(feature = "std", not(any(unix, windows))))]
fn thread_id_impl() -> u64 {
0
}
#[cfg(all(test, feature = "std", any(target_os = "linux", target_os = "android")))]
mod tests {
use super::*;
#[test]
fn capping_a_limit() {
let base = 0x7fff_0000_0000;
assert_eq!(cap_limit(base, base - 4096, 8192), base - 4096);
assert_eq!(cap_limit(base, base - 8192, 8192), base - 8192);
assert_eq!(cap_limit(base, base - 16384, 8192), base - 8192);
assert_eq!(
cap_limit(base, 0x5555_0000_0000, ASSUMED_MAIN_STACK),
base - ASSUMED_MAIN_STACK
);
assert_eq!(cap_limit(4096, 0, usize::MAX), 0);
}
#[test]
fn the_test_harness_does_not_run_on_the_initial_thread() {
assert!(!is_initial_thread());
}
#[test]
fn a_stack_rlimit_is_readable_or_unlimited() {
if let Some(limit) = stack_rlimit() {
assert!(limit > 0);
}
}
}