Skip to content

Commit e0511fe

Browse files
authored
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 <[email protected]>
1 parent 092efbf commit e0511fe

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

core/shared/platform/common/posix/posix_thread.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,18 @@ os_thread_get_stack_boundary()
326326
#elif defined(__APPLE__) || defined(__NuttX__)
327327
if ((addr = (uint8 *)pthread_get_stackaddr_np(self))) {
328328
stack_size = pthread_get_stacksize_np(self);
329+
330+
/**
331+
* Check whether stack_addr is the base or end of the stack,
332+
* change it to the base if it is the end of stack.
333+
*/
334+
if (addr <= (uint8 *)&stack_size)
335+
addr = addr + stack_size;
336+
329337
if (stack_size > max_stack_size)
330-
addr -= max_stack_size;
331-
else
332-
addr -= stack_size;
338+
stack_size = max_stack_size;
339+
340+
addr -= stack_size;
333341
/* Reserved 1 guard page at least for safety */
334342
addr += page_size;
335343
}

0 commit comments

Comments
 (0)