@@ -505,24 +505,24 @@ pub mod guard {
505
505
#[ cfg( target_os = "macos" ) ]
506
506
unsafe fn get_stack_start ( ) -> Option < * mut libc:: c_void > {
507
507
let th = libc:: pthread_self ( ) ;
508
- let stackaddr =
509
- libc:: pthread_get_stackaddr_np ( th) as usize - libc:: pthread_get_stacksize_np ( th) ;
510
- Some ( stackaddr as * mut libc:: c_void )
508
+ let stackptr = libc:: pthread_get_stackaddr_np ( th) ;
509
+ Some ( stackptr. with_addr ( stackptr. with_addr ( ) - libc:: pthread_get_stacksize_np ( th) ) )
511
510
}
512
511
513
512
#[ cfg( target_os = "openbsd" ) ]
514
513
unsafe fn get_stack_start ( ) -> Option < * mut libc:: c_void > {
515
514
let mut current_stack: libc:: stack_t = crate :: mem:: zeroed ( ) ;
516
515
assert_eq ! ( libc:: pthread_stackseg_np( libc:: pthread_self( ) , & mut current_stack) , 0 ) ;
517
516
517
+ let stack_ptr = current_stack. ss_sp ;
518
518
let stackaddr = if libc:: pthread_main_np ( ) == 1 {
519
519
// main thread
520
- current_stack . ss_sp as usize - current_stack. ss_size + PAGE_SIZE . load ( Ordering :: Relaxed )
520
+ stack_ptr . addr ( ) - current_stack. ss_size + PAGE_SIZE . load ( Ordering :: Relaxed )
521
521
} else {
522
522
// new thread
523
- current_stack . ss_sp as usize - current_stack. ss_size
523
+ stack_ptr . addr ( ) - current_stack. ss_size
524
524
} ;
525
- Some ( stackaddr as * mut libc :: c_void )
525
+ Some ( stack_ptr . with_addr ( stack_addr ) )
526
526
}
527
527
528
528
#[ cfg( any(
@@ -557,19 +557,20 @@ pub mod guard {
557
557
unsafe fn get_stack_start_aligned ( ) -> Option < * mut libc:: c_void > {
558
558
let page_size = PAGE_SIZE . load ( Ordering :: Relaxed ) ;
559
559
assert ! ( page_size != 0 ) ;
560
- let stackaddr = get_stack_start ( ) ?;
560
+ let stackptr = get_stack_start ( ) ?;
561
+ let stackaddr = stackptr. addr ( ) ;
561
562
562
563
// Ensure stackaddr is page aligned! A parent process might
563
564
// have reset RLIMIT_STACK to be non-page aligned. The
564
565
// pthread_attr_getstack() reports the usable stack area
565
566
// stackaddr < stackaddr + stacksize, so if stackaddr is not
566
567
// page-aligned, calculate the fix such that stackaddr <
567
568
// new_page_aligned_stackaddr < stackaddr + stacksize
568
- let remainder = ( stackaddr as usize ) % page_size;
569
+ let remainder = ( stackaddr) % page_size;
569
570
Some ( if remainder == 0 {
570
- stackaddr
571
+ stackptr
571
572
} else {
572
- ( ( stackaddr as usize ) + page_size - remainder) as * mut libc :: c_void
573
+ stackptr . with_addr ( stackaddr + page_size - remainder)
573
574
} )
574
575
}
575
576
@@ -588,8 +589,8 @@ pub mod guard {
588
589
// Instead, we'll just note where we expect rlimit to start
589
590
// faulting, so our handler can report "stack overflow", and
590
591
// trust that the kernel's own stack guard will work.
591
- let stackaddr = get_stack_start_aligned ( ) ?;
592
- let stackaddr = stackaddr as usize ;
592
+ let stackptr = get_stack_start_aligned ( ) ?;
593
+ let stackaddr = stackptr . addr ( ) ;
593
594
Some ( stackaddr - page_size..stackaddr)
594
595
} else if cfg ! ( all( target_os = "linux" , target_env = "musl" ) ) {
595
596
// For the main thread, the musl's pthread_attr_getstack
@@ -602,8 +603,8 @@ pub mod guard {
602
603
// at the bottom. If we try to remap the bottom of the stack
603
604
// ourselves, FreeBSD's guard page moves upwards. So we'll just use
604
605
// the builtin guard page.
605
- let stackaddr = get_stack_start_aligned ( ) ?;
606
- let guardaddr = stackaddr as usize ;
606
+ let stackptr = get_stack_start_aligned ( ) ?;
607
+ let guardaddr = stackptr . addr ( ) ;
607
608
// Technically the number of guard pages is tunable and controlled
608
609
// by the security.bsd.stack_guard_page sysctl, but there are
609
610
// few reasons to change it from the default. The default value has
@@ -620,33 +621,34 @@ pub mod guard {
620
621
// than the initial mmap() used, so we mmap() here with
621
622
// read/write permissions and only then mprotect() it to
622
623
// no permissions at all. See issue #50313.
623
- let stackaddr = get_stack_start_aligned ( ) ?;
624
+ let stackptr = get_stack_start_aligned ( ) ?;
624
625
let result = mmap (
625
- stackaddr ,
626
+ stackptr ,
626
627
page_size,
627
628
PROT_READ | PROT_WRITE ,
628
629
MAP_PRIVATE | MAP_ANON | MAP_FIXED ,
629
630
-1 ,
630
631
0 ,
631
632
) ;
632
- if result != stackaddr || result == MAP_FAILED {
633
+ if result != stackptr || result == MAP_FAILED {
633
634
panic ! ( "failed to allocate a guard page: {}" , io:: Error :: last_os_error( ) ) ;
634
635
}
635
636
636
- let result = mprotect ( stackaddr , page_size, PROT_NONE ) ;
637
+ let result = mprotect ( stackptr , page_size, PROT_NONE ) ;
637
638
if result != 0 {
638
639
panic ! ( "failed to protect the guard page: {}" , io:: Error :: last_os_error( ) ) ;
639
640
}
640
641
641
- let guardaddr = stackaddr as usize ;
642
+ let guardaddr = stackptr . addr ( ) ;
642
643
643
644
Some ( guardaddr..guardaddr + page_size)
644
645
}
645
646
}
646
647
647
648
#[ cfg( any( target_os = "macos" , target_os = "openbsd" , target_os = "solaris" ) ) ]
648
649
pub unsafe fn current ( ) -> Option < Guard > {
649
- let stackaddr = get_stack_start ( ) ? as usize ;
650
+ let stackptr = get_stack_start ( ) ?;
651
+ let stackaddr = stackptr. addr ( ) ;
650
652
Some ( stackaddr - PAGE_SIZE . load ( Ordering :: Relaxed ) ..stackaddr)
651
653
}
652
654
@@ -679,11 +681,11 @@ pub mod guard {
679
681
panic ! ( "there is no guard page" ) ;
680
682
}
681
683
}
682
- let mut stackaddr = crate :: ptr:: null_mut ( ) ;
684
+ let mut stackptr = crate :: ptr:: null_mut :: < libc :: c_void > ( ) ;
683
685
let mut size = 0 ;
684
- assert_eq ! ( libc:: pthread_attr_getstack( & attr, & mut stackaddr , & mut size) , 0 ) ;
686
+ assert_eq ! ( libc:: pthread_attr_getstack( & attr, & mut stackptr , & mut size) , 0 ) ;
685
687
686
- let stackaddr = stackaddr as usize ;
688
+ let stackaddr = stackptr . addr ( ) ;
687
689
ret = if cfg ! ( any( target_os = "freebsd" , target_os = "netbsd" ) ) {
688
690
Some ( stackaddr - guardsize..stackaddr)
689
691
} else if cfg ! ( all( target_os = "linux" , target_env = "musl" ) ) {
0 commit comments