Skip to content

Commit 3760c05

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 3760c05

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

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

+22-4
Original file line numberDiff line numberDiff line change
@@ -847,10 +847,28 @@ 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+
let mut guard: usize = 0;
858+
let mut size = crate::mem::size_of_val(&guard);
859+
let oid = crate::ffi::CStr::from_bytes_with_nul(b"security.bsd.stack_guard_page\0")
860+
.unwrap();
861+
let res = libc::sysctlbyname(
862+
oid.as_ptr(),
863+
&mut guard as *mut _ as *mut _,
864+
&mut size as *mut _ as *mut _,
865+
crate::ptr::null_mut(),
866+
0,
867+
);
868+
if res == 0 {
869+
GUARD_PAGES = guard;
870+
}
871+
});
854872
let guard = guardaddr..guardaddr + GUARD_PAGES * page_size;
855873
Some(guard)
856874
} else if cfg!(target_os = "openbsd") {

0 commit comments

Comments
 (0)