From d0aa2924776c1eab96a55870c317a2f51b578b0b Mon Sep 17 00:00:00 2001 From: Michael Fairman Date: Sun, 1 Oct 2023 12:34:43 -0700 Subject: [PATCH] bugfix: correct computation of stack size on Mac Posix port Computes the stack size after the end is aligned to a page boundary. The original code in (#674) did not account for this and some combinations of stack addresses and sizes would cause a segfault. Tested on ARM64 MacOS and Linux, but not Intel (yet). --- portable/ThirdParty/GCC/Posix/port.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/portable/ThirdParty/GCC/Posix/port.c b/portable/ThirdParty/GCC/Posix/port.c index 8ec8193971b..24dd89f4539 100644 --- a/portable/ThirdParty/GCC/Posix/port.c +++ b/portable/ThirdParty/GCC/Posix/port.c @@ -148,10 +148,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__ - pxEndOfStack = mach_vm_round_page( pxEndOfStack ); ulStackSize = mach_vm_trunc_page( ulStackSize ); #endif