Skip to content

Commit

Permalink
POSIX Port: Round up stack sizes correctly on macOS
Browse files Browse the repository at this point in the history
On macOS, pthread_attr_setstacksize requires the stack size to be a multiple of
the system page size.

The previous changes here assumed the use of pthread_attr_setstack, however
the transition to pthread_attr_setstacksize means we can avoid rounding the
pxEndOfStack pointer.

This is additionally positive because pxEndOfStack was actually being rounded
in the wrong direction -- it ought to have been rounded down (_trunc) instead of
up due to the stack growth direction. In my case this actually caused pxEndOfStack
to end up after pxTopOfStack, which underflowed and created a huge stack size
request + EXC_BAD_ACCESS in pthread_create.

Signed-off-by: Paul Hollinsky <paulhollinsky@gmail.com>
  • Loading branch information
hollinsky committed Oct 20, 2024
1 parent a49c35b commit f9f0208
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions portable/ThirdParty/GCC/Posix/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
thread = ( Thread_t * ) ( pxTopOfStack + 1 ) - 1;
pxTopOfStack = ( StackType_t * ) thread - 1;

#ifdef __APPLE__
pxEndOfStack = ( StackType_t * ) mach_vm_round_page( pxEndOfStack );
#endif

ulStackSize = ( size_t ) ( pxTopOfStack + 1 - pxEndOfStack ) * sizeof( *pxTopOfStack );

#ifdef __APPLE__
ulStackSize = mach_vm_trunc_page( ulStackSize );
/*
* On macOS, pthread_attr_setstacksize requires the stack to be a multiple of the system page size.
* Round up to the next page boundary.
*/
ulStackSize = mach_vm_round_page( ulStackSize );
#endif

thread->pxCode = pxCode;
Expand Down

0 comments on commit f9f0208

Please sign in to comment.