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 <huangqi3@xiaomi.com>
This commit is contained in:
Huang Qi 2022-01-17 20:45:55 +08:00 committed by GitHub
parent 092efbfe21
commit e0511fe822
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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;
}