Jelajahi Sumber

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>
Huang Qi 4 tahun lalu
induk
melakukan
e0511fe822
1 mengubah file dengan 11 tambahan dan 3 penghapusan
  1. 11 3
      core/shared/platform/common/posix/posix_thread.c

+ 11 - 3
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;
     }