@@ -505,24 +505,24 @@ pub mod guard {
505505 #[ cfg( target_os = "macos" ) ]
506506 unsafe fn get_stack_start ( ) -> Option < * mut libc:: c_void > {
507507 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) ) )
511510 }
512511
513512 #[ cfg( target_os = "openbsd" ) ]
514513 unsafe fn get_stack_start ( ) -> Option < * mut libc:: c_void > {
515514 let mut current_stack: libc:: stack_t = crate :: mem:: zeroed ( ) ;
516515 assert_eq ! ( libc:: pthread_stackseg_np( libc:: pthread_self( ) , & mut current_stack) , 0 ) ;
517516
517+ let stack_ptr = current_stack. ss_sp ;
518518 let stackaddr = if libc:: pthread_main_np ( ) == 1 {
519519 // 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 )
521521 } else {
522522 // new thread
523- current_stack . ss_sp as usize - current_stack. ss_size
523+ stack_ptr . addr ( ) - current_stack. ss_size
524524 } ;
525- Some ( stackaddr as * mut libc :: c_void )
525+ Some ( stack_ptr . with_addr ( stack_addr ) )
526526 }
527527
528528 #[ cfg( any(
@@ -557,19 +557,20 @@ pub mod guard {
557557 unsafe fn get_stack_start_aligned ( ) -> Option < * mut libc:: c_void > {
558558 let page_size = PAGE_SIZE . load ( Ordering :: Relaxed ) ;
559559 assert ! ( page_size != 0 ) ;
560- let stackaddr = get_stack_start ( ) ?;
560+ let stackptr = get_stack_start ( ) ?;
561+ let stackaddr = stackptr. addr ( ) ;
561562
562563 // Ensure stackaddr is page aligned! A parent process might
563564 // have reset RLIMIT_STACK to be non-page aligned. The
564565 // pthread_attr_getstack() reports the usable stack area
565566 // stackaddr < stackaddr + stacksize, so if stackaddr is not
566567 // page-aligned, calculate the fix such that stackaddr <
567568 // new_page_aligned_stackaddr < stackaddr + stacksize
568- let remainder = ( stackaddr as usize ) % page_size;
569+ let remainder = ( stackaddr) % page_size;
569570 Some ( if remainder == 0 {
570- stackaddr
571+ stackptr
571572 } else {
572- ( ( stackaddr as usize ) + page_size - remainder) as * mut libc :: c_void
573+ stackptr . with_addr ( stackaddr + page_size - remainder)
573574 } )
574575 }
575576
@@ -588,8 +589,8 @@ pub mod guard {
588589 // Instead, we'll just note where we expect rlimit to start
589590 // faulting, so our handler can report "stack overflow", and
590591 // 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 ( ) ;
593594 Some ( stackaddr - page_size..stackaddr)
594595 } else if cfg ! ( all( target_os = "linux" , target_env = "musl" ) ) {
595596 // For the main thread, the musl's pthread_attr_getstack
@@ -602,8 +603,8 @@ pub mod guard {
602603 // at the bottom. If we try to remap the bottom of the stack
603604 // ourselves, FreeBSD's guard page moves upwards. So we'll just use
604605 // 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 ( ) ;
607608 // Technically the number of guard pages is tunable and controlled
608609 // by the security.bsd.stack_guard_page sysctl, but there are
609610 // few reasons to change it from the default. The default value has
@@ -620,33 +621,34 @@ pub mod guard {
620621 // than the initial mmap() used, so we mmap() here with
621622 // read/write permissions and only then mprotect() it to
622623 // no permissions at all. See issue #50313.
623- let stackaddr = get_stack_start_aligned ( ) ?;
624+ let stackptr = get_stack_start_aligned ( ) ?;
624625 let result = mmap (
625- stackaddr ,
626+ stackptr ,
626627 page_size,
627628 PROT_READ | PROT_WRITE ,
628629 MAP_PRIVATE | MAP_ANON | MAP_FIXED ,
629630 -1 ,
630631 0 ,
631632 ) ;
632- if result != stackaddr || result == MAP_FAILED {
633+ if result != stackptr || result == MAP_FAILED {
633634 panic ! ( "failed to allocate a guard page: {}" , io:: Error :: last_os_error( ) ) ;
634635 }
635636
636- let result = mprotect ( stackaddr , page_size, PROT_NONE ) ;
637+ let result = mprotect ( stackptr , page_size, PROT_NONE ) ;
637638 if result != 0 {
638639 panic ! ( "failed to protect the guard page: {}" , io:: Error :: last_os_error( ) ) ;
639640 }
640641
641- let guardaddr = stackaddr as usize ;
642+ let guardaddr = stackptr . addr ( ) ;
642643
643644 Some ( guardaddr..guardaddr + page_size)
644645 }
645646 }
646647
647648 #[ cfg( any( target_os = "macos" , target_os = "openbsd" , target_os = "solaris" ) ) ]
648649 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 ( ) ;
650652 Some ( stackaddr - PAGE_SIZE . load ( Ordering :: Relaxed ) ..stackaddr)
651653 }
652654
@@ -679,11 +681,11 @@ pub mod guard {
679681 panic ! ( "there is no guard page" ) ;
680682 }
681683 }
682- let mut stackaddr = crate :: ptr:: null_mut ( ) ;
684+ let mut stackptr = crate :: ptr:: null_mut :: < libc :: c_void > ( ) ;
683685 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 ) ;
685687
686- let stackaddr = stackaddr as usize ;
688+ let stackaddr = stackptr . addr ( ) ;
687689 ret = if cfg ! ( any( target_os = "freebsd" , target_os = "netbsd" ) ) {
688690 Some ( stackaddr - guardsize..stackaddr)
689691 } else if cfg ! ( all( target_os = "linux" , target_env = "musl" ) ) {
0 commit comments