From e0511fe822dcd087c8f6e61ca358a4e390b09038 Mon Sep 17 00:00:00 2001 From: Huang Qi Date: Mon, 17 Jan 2022 20:45:55 +0800 Subject: [PATCH] Correct stack base calculation on Mac and NuttX (#963) The return address of pthread_get_stackaddr_np() in MacOS and NuttX may be the base address or the end (boundary) address of the native stack, if it is the end address, we get the base address according to it and the stack size, so as to get the actual stack boundary address correctly. Signed-off-by: Huang Qi --- core/shared/platform/common/posix/posix_thread.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/core/shared/platform/common/posix/posix_thread.c b/core/shared/platform/common/posix/posix_thread.c index 1e7e4f375..4a20c10a0 100644 --- a/core/shared/platform/common/posix/posix_thread.c +++ b/core/shared/platform/common/posix/posix_thread.c @@ -326,10 +326,18 @@ os_thread_get_stack_boundary() #elif defined(__APPLE__) || defined(__NuttX__) if ((addr = (uint8 *)pthread_get_stackaddr_np(self))) { stack_size = pthread_get_stacksize_np(self); + + /** + * Check whether stack_addr is the base or end of the stack, + * change it to the base if it is the end of stack. + */ + if (addr <= (uint8 *)&stack_size) + addr = addr + stack_size; + if (stack_size > max_stack_size) - addr -= max_stack_size; - else - addr -= stack_size; + stack_size = max_stack_size; + + addr -= stack_size; /* Reserved 1 guard page at least for safety */ addr += page_size; }