Skip to content

Commit 591b642

Browse files
committed
std::thread update freensd stack guard handling.
up to now, it had been assumed the stack guard setting default is not touched in the field but some user might just want to disable it or increase it. checking it once at runtime should be enough.
1 parent 8c0b4f6 commit 591b642

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

Diff for: library/std/src/sys/pal/unix/thread.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -847,10 +847,37 @@ pub mod guard {
847847
let stackptr = get_stack_start_aligned()?;
848848
let guardaddr = stackptr.addr();
849849
// Technically the number of guard pages is tunable and controlled
850-
// by the security.bsd.stack_guard_page sysctl, but there are
851-
// few reasons to change it from the default. The default value has
852-
// been 1 ever since FreeBSD 11.1 and 10.4.
853-
const GUARD_PAGES: usize = 1;
850+
// by the security.bsd.stack_guard_page sysctl.
851+
// By default it is 1, checking once is enough since it is
852+
// a boot time config value.
853+
static ONCE: crate::sync::Once = crate::sync::Once::new();
854+
static mut GUARD_PAGES: usize = 1;
855+
856+
ONCE.call_once(|| {
857+
extern "C" {
858+
pub fn sysctlbyname(
859+
oid: *const libc::c_char,
860+
ov: *mut libc::c_void,
861+
osize: *mut libc::size_t,
862+
nv: *const libc::c_void,
863+
nsize: libc::size_t,
864+
) -> libc::c_int;
865+
}
866+
let mut guard: usize = 0;
867+
let mut size = crate::mem::size_of_val(&guard);
868+
let oid = crate::ffi::CStr::from_bytes_with_nul(b"security.bsd.stack_guard_page\0")
869+
.unwrap();
870+
let res = sysctlbyname(
871+
oid.as_ptr(),
872+
&mut guard as *mut _ as *mut _,
873+
&mut size as *mut _ as *mut _,
874+
crate::ptr::null_mut(),
875+
0,
876+
);
877+
if res == 0 {
878+
GUARD_PAGES = guard;
879+
}
880+
});
854881
let guard = guardaddr..guardaddr + GUARD_PAGES * page_size;
855882
Some(guard)
856883
} else if cfg!(target_os = "openbsd") {

0 commit comments

Comments
 (0)