Skip to content

Commit 7ff3bad

Browse files
committed
std::threads: revisit stack address calculation on netbsd.
like older linux glibc versions, we need to get the guard size and increasing the stack's bottom address accordingly.
1 parent 7606c13 commit 7ff3bad

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

library/std/src/sys/pal/unix/thread.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -803,12 +803,30 @@ pub mod guard {
803803
Some(stack_ptr.with_addr(stackaddr))
804804
}
805805

806+
#[cfg(target_os = "netbsd")]
807+
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
808+
let mut ret = None;
809+
let mut attr: libc::pthread_attr_t = crate::mem::zeroed();
810+
let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr);
811+
if e == 0 {
812+
let mut stackaddr = crate::ptr::null_mut();
813+
let mut stacksize = 0;
814+
let mut guardsize = 0;
815+
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut stacksize), 0);
816+
// on netbsd, we need to take in account the guard size to push up
817+
// the stack's address from the bottom.
818+
assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0);
819+
stackaddr = stackaddr.add(guardsize);
820+
ret = Some(stackaddr);
821+
}
822+
ret
823+
}
824+
806825
#[cfg(any(
807826
target_os = "android",
808827
target_os = "freebsd",
809828
target_os = "hurd",
810829
target_os = "linux",
811-
target_os = "netbsd",
812830
target_os = "l4re"
813831
))]
814832
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
@@ -911,9 +929,10 @@ pub mod guard {
911929
}
912930
}) * page_size;
913931
Some(guard)
914-
} else if cfg!(target_os = "openbsd") {
932+
} else if cfg!(any(target_os = "openbsd", target_os = "netbsd")) {
915933
// OpenBSD stack already includes a guard page, and stack is
916934
// immutable.
935+
// NetBSD stack includes the guard page.
917936
//
918937
// We'll just note where we expect rlimit to start
919938
// faulting, so our handler can report "stack overflow", and

0 commit comments

Comments
 (0)