@@ -163,10 +163,7 @@ psm_stack_manipulation! {
163
163
-1 , // Some implementations assert fd = -1 if MAP_ANON is specified
164
164
0
165
165
) ;
166
- if new_stack == libc:: MAP_FAILED {
167
- let error = std:: io:: Error :: last_os_error( ) ;
168
- panic!( "allocating stack failed with: {}" , error)
169
- }
166
+ assert_ne!( new_stack, libc:: MAP_FAILED , "mmap failed to allocate stack: {}" , std:: io:: Error :: last_os_error( ) ) ;
170
167
let guard = StackRestoreGuard {
171
168
new_stack,
172
169
stack_bytes,
@@ -191,11 +188,7 @@ psm_stack_manipulation! {
191
188
} else {
192
189
-1
193
190
} ;
194
- if result == -1 {
195
- let error = std:: io:: Error :: last_os_error( ) ;
196
- drop( guard) ;
197
- panic!( "setting stack permissions failed with: {}" , error)
198
- }
191
+ assert_ne!( result, -1 , "mprotect/mmap failed: {}" , std:: io:: Error :: last_os_error( ) ) ;
199
192
guard
200
193
}
201
194
}
@@ -222,7 +215,7 @@ psm_stack_manipulation! {
222
215
fn _grow( stack_size: usize , callback: & mut dyn FnMut ( ) ) {
223
216
// Calculate a number of pages we want to allocate for the new stack.
224
217
// For maximum portability we want to produce a stack that is aligned to a page and has
225
- // a size that’ s a multiple of page size. Furthermore we want to allocate two extras pages
218
+ // a size that' s a multiple of page size. Furthermore we want to allocate two extras pages
226
219
// for the stack guard. To achieve that we do our calculations in number of pages and
227
220
// convert to bytes last.
228
221
let page_size = page_size( ) ;
@@ -412,30 +405,51 @@ cfg_if! {
412
405
Some ( mi. assume_init( ) . AllocationBase as usize + get_thread_stack_guarantee( ) + 0x1000 )
413
406
}
414
407
} else if #[ cfg( any( target_os = "linux" , target_os="solaris" , target_os = "netbsd" ) ) ] {
408
+ unsafe fn destroy_pthread_attr( attr: * mut libc:: pthread_attr_t) {
409
+ let ret = libc:: pthread_attr_destroy( attr) ;
410
+ assert!( ret == 0 , "pthread_attr_destroy failed with error code: {}" , ret) ;
411
+ }
412
+
415
413
unsafe fn guess_os_stack_limit( ) -> Option <usize > {
416
414
let mut attr = std:: mem:: MaybeUninit :: <libc:: pthread_attr_t>:: uninit( ) ;
417
- assert_eq!( libc:: pthread_attr_init( attr. as_mut_ptr( ) ) , 0 ) ;
418
- assert_eq!( libc:: pthread_getattr_np( libc:: pthread_self( ) ,
419
- attr. as_mut_ptr( ) ) , 0 ) ;
415
+ if libc:: pthread_attr_init( attr. as_mut_ptr( ) ) != 0 {
416
+ return None ;
417
+ }
418
+ if libc:: pthread_getattr_np( libc:: pthread_self( ) , attr. as_mut_ptr( ) ) != 0 {
419
+ destroy_pthread_attr( attr. as_mut_ptr( ) ) ;
420
+ return None ;
421
+ }
420
422
let mut stackaddr = std:: ptr:: null_mut( ) ;
421
423
let mut stacksize = 0 ;
422
- assert_eq!( libc:: pthread_attr_getstack(
423
- attr. as_ptr( ) , & mut stackaddr, & mut stacksize
424
- ) , 0 ) ;
425
- assert_eq!( libc:: pthread_attr_destroy( attr. as_mut_ptr( ) ) , 0 ) ;
424
+ if libc:: pthread_attr_getstack( attr. as_ptr( ) , & mut stackaddr, & mut stacksize) != 0 {
425
+ destroy_pthread_attr( attr. as_mut_ptr( ) ) ;
426
+ return None ;
427
+ }
428
+ destroy_pthread_attr( attr. as_mut_ptr( ) ) ;
426
429
Some ( stackaddr as usize )
427
430
}
428
431
} else if #[ cfg( any( target_os = "freebsd" , target_os = "dragonfly" , target_os = "illumos" ) ) ] {
432
+ unsafe fn destroy_pthread_attr( attr: * mut libc:: pthread_attr_t) {
433
+ let ret = libc:: pthread_attr_destroy( attr) ;
434
+ assert!( ret == 0 , "pthread_attr_destroy failed with error code: {}" , ret) ;
435
+ }
436
+
429
437
unsafe fn guess_os_stack_limit( ) -> Option <usize > {
430
438
let mut attr = std:: mem:: MaybeUninit :: <libc:: pthread_attr_t>:: uninit( ) ;
431
- assert_eq!( libc:: pthread_attr_init( attr. as_mut_ptr( ) ) , 0 ) ;
432
- assert_eq!( libc:: pthread_attr_get_np( libc:: pthread_self( ) , attr. as_mut_ptr( ) ) , 0 ) ;
439
+ if libc:: pthread_attr_init( attr. as_mut_ptr( ) ) != 0 {
440
+ return None ;
441
+ }
442
+ if libc:: pthread_attr_get_np( libc:: pthread_self( ) , attr. as_mut_ptr( ) ) != 0 {
443
+ destroy_pthread_attr( attr. as_mut_ptr( ) ) ;
444
+ return None ;
445
+ }
433
446
let mut stackaddr = std:: ptr:: null_mut( ) ;
434
447
let mut stacksize = 0 ;
435
- assert_eq!( libc:: pthread_attr_getstack(
436
- attr. as_ptr( ) , & mut stackaddr, & mut stacksize
437
- ) , 0 ) ;
438
- assert_eq!( libc:: pthread_attr_destroy( attr. as_mut_ptr( ) ) , 0 ) ;
448
+ if libc:: pthread_attr_getstack( attr. as_ptr( ) , & mut stackaddr, & mut stacksize) != 0 {
449
+ destroy_pthread_attr( attr. as_mut_ptr( ) ) ;
450
+ return None ;
451
+ }
452
+ destroy_pthread_attr( attr. as_mut_ptr( ) ) ;
439
453
Some ( stackaddr as usize )
440
454
}
441
455
} else if #[ cfg( target_os = "openbsd" ) ] {
0 commit comments