@@ -343,6 +343,20 @@ pub mod guard {
343
343
// it can eventually grow to. It cannot be used to determine
344
344
// the position of kernel's stack guard.
345
345
None
346
+ } else if cfg ! ( target_os = "freebsd" ) {
347
+ // FreeBSD's stack autogrows, and optionally includes a guard page
348
+ // at the bottom. If we try to remap the bottom of the stack
349
+ // ourselves, FreeBSD's guard page moves upwards. So we'll just use
350
+ // the builtin guard page.
351
+ let stackaddr = get_stack_start_aligned ( ) ?;
352
+ let guardaddr = stackaddr as usize ;
353
+ // Technically the number of guard pages is tunable and controlled
354
+ // by the security.bsd.stack_guard_page sysctl, but there are
355
+ // few reasons to change it from the default. The default value has
356
+ // been 1 ever since FreeBSD 11.1 and 10.4.
357
+ const GUARD_PAGES : usize = 1 ;
358
+ let guard = guardaddr..guardaddr + GUARD_PAGES * page_size;
359
+ Some ( guard)
346
360
} else {
347
361
// Reallocate the last page of the stack.
348
362
// This ensures SIGBUS will be raised on
@@ -371,9 +385,8 @@ pub mod guard {
371
385
}
372
386
373
387
let guardaddr = stackaddr as usize ;
374
- let offset = if cfg ! ( target_os = "freebsd" ) { 2 } else { 1 } ;
375
388
376
- Some ( guardaddr..guardaddr + offset * page_size)
389
+ Some ( guardaddr..guardaddr + page_size)
377
390
}
378
391
}
379
392
@@ -417,11 +430,7 @@ pub mod guard {
417
430
assert_eq ! ( libc:: pthread_attr_getstack( & attr, & mut stackaddr, & mut size) , 0 ) ;
418
431
419
432
let stackaddr = stackaddr as usize ;
420
- ret = if cfg ! ( target_os = "freebsd" ) {
421
- // FIXME does freebsd really fault *below* the guard addr?
422
- let guardaddr = stackaddr - guardsize;
423
- Some ( guardaddr - PAGE_SIZE . load ( Ordering :: Relaxed ) ..guardaddr)
424
- } else if cfg ! ( target_os = "netbsd" ) {
433
+ ret = if cfg ! ( any( target_os = "freebsd" , target_os = "netbsd" ) ) {
425
434
Some ( stackaddr - guardsize..stackaddr)
426
435
} else if cfg ! ( all( target_os = "linux" , target_env = "musl" ) ) {
427
436
Some ( stackaddr - guardsize..stackaddr)
0 commit comments