1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "util/NativeStack.h"
8 
9 #ifdef XP_WIN
10 #include "util/Windows.h"
11 #elif defined(XP_DARWIN) || defined(DARWIN) || defined(XP_UNIX)
12 #include <pthread.h>
13 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
14 #include <pthread_np.h>
15 #endif
16 #if defined(ANDROID) && !defined(__aarch64__)
17 #include <sys/types.h>
18 #include <unistd.h>
19 #endif
20 #else
21 #error "Unsupported platform"
22 #endif
23 
24 #include "jsfriendapi.h"
25 
26 #if defined(XP_WIN)
27 
GetNativeStackBaseImpl()28 void* js::GetNativeStackBaseImpl() {
29   PNT_TIB pTib = reinterpret_cast<PNT_TIB>(NtCurrentTeb());
30   return static_cast<void*>(pTib->StackBase);
31 }
32 
33 #elif defined(SOLARIS)
34 
35 #include <ucontext.h>
36 
37 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
38 
GetNativeStackBaseImpl()39 void* js::GetNativeStackBaseImpl() {
40   stack_t st;
41   stack_getbounds(&st);
42   return static_cast<char*>(st.ss_sp) + st.ss_size;
43 }
44 
45 #elif defined(AIX)
46 
47 #include <ucontext.h>
48 
49 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
50 
GetNativeStackBaseImpl()51 void* js::GetNativeStackBaseImpl() {
52   ucontext_t context;
53   getcontext(&context);
54   return static_cast<char*>(context.uc_stack.ss_sp) + context.uc_stack.ss_size;
55 }
56 
57 #else /* XP_UNIX */
58 
GetNativeStackBaseImpl()59 void* js::GetNativeStackBaseImpl() {
60   pthread_t thread = pthread_self();
61 #if defined(XP_DARWIN) || defined(DARWIN)
62   return pthread_get_stackaddr_np(thread);
63 
64 #else
65   pthread_attr_t sattr;
66   pthread_attr_init(&sattr);
67 #if defined(__OpenBSD__)
68   stack_t ss;
69 #elif defined(PTHREAD_NP_H) || defined(_PTHREAD_NP_H_) || defined(NETBSD)
70   /* e.g. on FreeBSD 4.8 or newer, neundorf@kde.org */
71   pthread_attr_get_np(thread, &sattr);
72 #else
73   /*
74    * FIXME: this function is non-portable;
75    * other POSIX systems may have different np alternatives
76    */
77   pthread_getattr_np(thread, &sattr);
78 #endif
79 
80   void* stackBase = 0;
81   size_t stackSize = 0;
82   int rc;
83 #if defined(__OpenBSD__)
84   rc = pthread_stackseg_np(pthread_self(), &ss);
85   stackBase = (void*)((size_t)ss.ss_sp - ss.ss_size);
86   stackSize = ss.ss_size;
87 #elif defined(ANDROID) && !defined(__aarch64__)
88   if (gettid() == getpid()) {
89     // bionic's pthread_attr_getstack prior to API 21 doesn't tell the truth
90     // for the main thread (see bug 846670). So we scan /proc/self/maps to
91     // find the segment which contains the stack.
92     rc = -1;
93 
94     // Put the string on the stack, otherwise there is the danger that it
95     // has not been decompressed by the the on-demand linker. Bug 1165460.
96     //
97     // The volatile keyword should stop the compiler from trying to omit
98     // the stack copy in the future (hopefully).
99     volatile char path[] = "/proc/self/maps";
100     FILE* fs = fopen((const char*)path, "r");
101 
102     if (fs) {
103       char line[100];
104       unsigned long stackAddr = (unsigned long)&sattr;
105       while (fgets(line, sizeof(line), fs) != nullptr) {
106         unsigned long stackStart;
107         unsigned long stackEnd;
108         if (sscanf(line, "%lx-%lx ", &stackStart, &stackEnd) == 2 &&
109             stackAddr >= stackStart && stackAddr < stackEnd) {
110           stackBase = (void*)stackStart;
111           stackSize = stackEnd - stackStart;
112           rc = 0;
113           break;
114         }
115       }
116       fclose(fs);
117     }
118   } else
119     // For non main-threads pthread allocates the stack itself so it tells
120     // the truth.
121     rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
122 #else
123   rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
124 #endif
125   if (rc) MOZ_CRASH();
126   MOZ_ASSERT(stackBase);
127   pthread_attr_destroy(&sattr);
128 
129 #if JS_STACK_GROWTH_DIRECTION > 0
130   return stackBase;
131 #else
132   return static_cast<char*>(stackBase) + stackSize;
133 #endif
134 #endif
135 }
136 
137 #endif /* !XP_WIN */
138