From 7ff3bade73b3507b49ad2771b0dd2650e7c08d57 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Mon, 4 Mar 2024 22:31:28 +0000 Subject: [PATCH 1/2] 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. --- library/std/src/sys/pal/unix/thread.rs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/library/std/src/sys/pal/unix/thread.rs b/library/std/src/sys/pal/unix/thread.rs index 2af6382f3daee..8c9167ee9eb28 100644 --- a/library/std/src/sys/pal/unix/thread.rs +++ b/library/std/src/sys/pal/unix/thread.rs @@ -803,12 +803,30 @@ pub mod guard { Some(stack_ptr.with_addr(stackaddr)) } + #[cfg(target_os = "netbsd")] + unsafe fn get_stack_start() -> Option<*mut libc::c_void> { + let mut ret = None; + let mut attr: libc::pthread_attr_t = crate::mem::zeroed(); + let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr); + if e == 0 { + let mut stackaddr = crate::ptr::null_mut(); + let mut stacksize = 0; + let mut guardsize = 0; + assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut stacksize), 0); + // on netbsd, we need to take in account the guard size to push up + // the stack's address from the bottom. + assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0); + stackaddr = stackaddr.add(guardsize); + ret = Some(stackaddr); + } + ret + } + #[cfg(any( target_os = "android", target_os = "freebsd", target_os = "hurd", target_os = "linux", - target_os = "netbsd", target_os = "l4re" ))] unsafe fn get_stack_start() -> Option<*mut libc::c_void> { @@ -911,9 +929,10 @@ pub mod guard { } }) * page_size; Some(guard) - } else if cfg!(target_os = "openbsd") { + } else if cfg!(any(target_os = "openbsd", target_os = "netbsd")) { // OpenBSD stack already includes a guard page, and stack is // immutable. + // NetBSD stack includes the guard page. // // We'll just note where we expect rlimit to start // faulting, so our handler can report "stack overflow", and From ffdd97f79178f16c5306a84af933aa31f223ffb4 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Fri, 8 Mar 2024 21:54:03 +0000 Subject: [PATCH 2/2] further changes from feedback --- library/std/src/sys/pal/unix/thread.rs | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/library/std/src/sys/pal/unix/thread.rs b/library/std/src/sys/pal/unix/thread.rs index 8c9167ee9eb28..6520ca9fc48ef 100644 --- a/library/std/src/sys/pal/unix/thread.rs +++ b/library/std/src/sys/pal/unix/thread.rs @@ -803,28 +803,10 @@ pub mod guard { Some(stack_ptr.with_addr(stackaddr)) } - #[cfg(target_os = "netbsd")] - unsafe fn get_stack_start() -> Option<*mut libc::c_void> { - let mut ret = None; - let mut attr: libc::pthread_attr_t = crate::mem::zeroed(); - let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr); - if e == 0 { - let mut stackaddr = crate::ptr::null_mut(); - let mut stacksize = 0; - let mut guardsize = 0; - assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut stacksize), 0); - // on netbsd, we need to take in account the guard size to push up - // the stack's address from the bottom. - assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0); - stackaddr = stackaddr.add(guardsize); - ret = Some(stackaddr); - } - ret - } - #[cfg(any( target_os = "android", target_os = "freebsd", + target_os = "netbsd", target_os = "hurd", target_os = "linux", target_os = "l4re"