@@ -69,7 +69,6 @@ mod imp {
6969 use super :: Handler ;
7070 use super :: thread_info:: { delete_current_info, set_current_info, with_current_info} ;
7171 use crate :: ops:: Range ;
72- use crate :: sync:: OnceLock ;
7372 use crate :: sync:: atomic:: { Atomic , AtomicBool , AtomicPtr , AtomicUsize , Ordering } ;
7473 use crate :: sys:: pal:: unix:: os;
7574 use crate :: { io, mem, panic, ptr} ;
@@ -396,6 +395,10 @@ mod imp {
396395 } else if cfg ! ( all( target_os = "linux" , target_env = "musl" ) ) {
397396 install_main_guard_linux_musl ( page_size)
398397 } else if cfg ! ( target_os = "freebsd" ) {
398+ #[ cfg( not( target_os = "freebsd" ) ) ]
399+ return None ;
400+ // The FreeBSD code cannot be checked on non-BSDs.
401+ #[ cfg( target_os = "freebsd" ) ]
399402 install_main_guard_freebsd ( page_size)
400403 } else if cfg ! ( any( target_os = "netbsd" , target_os = "openbsd" ) ) {
401404 install_main_guard_bsds ( page_size)
@@ -432,6 +435,7 @@ mod imp {
432435 }
433436
434437 #[ forbid( unsafe_op_in_unsafe_fn) ]
438+ #[ cfg( target_os = "freebsd" ) ]
435439 unsafe fn install_main_guard_freebsd ( page_size : usize ) -> Option < Range < usize > > {
436440 // FreeBSD's stack autogrows, and optionally includes a guard page
437441 // at the bottom. If we try to remap the bottom of the stack
@@ -443,38 +447,23 @@ mod imp {
443447 // by the security.bsd.stack_guard_page sysctl.
444448 // By default it is 1, checking once is enough since it is
445449 // a boot time config value.
446- static PAGES : OnceLock < usize > = OnceLock :: new ( ) ;
450+ static PAGES : crate :: sync :: OnceLock < usize > = crate :: sync :: OnceLock :: new ( ) ;
447451
448452 let pages = PAGES . get_or_init ( || {
449- use crate :: sys:: weak:: dlsym;
450- dlsym ! (
451- fn sysctlbyname(
452- name: * const libc:: c_char,
453- oldp: * mut libc:: c_void,
454- oldlenp: * mut libc:: size_t,
455- newp: * const libc:: c_void,
456- newlen: libc:: size_t,
457- ) -> libc:: c_int;
458- ) ;
459453 let mut guard: usize = 0 ;
460454 let mut size = size_of_val ( & guard) ;
461455 let oid = c"security.bsd.stack_guard_page" ;
462- match sysctlbyname. get ( ) {
463- Some ( fcn)
464- if unsafe {
465- fcn (
466- oid. as_ptr ( ) ,
467- ( & raw mut guard) . cast ( ) ,
468- & raw mut size,
469- ptr:: null_mut ( ) ,
470- 0 ,
471- ) == 0
472- } =>
473- {
474- guard
475- }
476- _ => 1 ,
477- }
456+
457+ let r = unsafe {
458+ libc:: sysctlbyname (
459+ oid. as_ptr ( ) ,
460+ ( & raw mut guard) . cast ( ) ,
461+ & raw mut size,
462+ ptr:: null_mut ( ) ,
463+ 0 ,
464+ )
465+ } ;
466+ if r == 0 { guard } else { 1 }
478467 } ) ;
479468 Some ( guardaddr..guardaddr + pages * page_size)
480469 }
0 commit comments