1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
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 #include "mozilla/Assertions.h"  // MOZ_ASSERT, MOZ_RELEASE_ASSERT, MOZ_CRASH
10 
11 #ifdef XP_WIN
12 #  include "util/Windows.h"
13 #elif defined(__wasi__)
14 // Nothing
15 #elif defined(XP_DARWIN) || defined(DARWIN) || defined(XP_UNIX)
16 #  include <pthread.h>
17 #  if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
18 #    include <pthread_np.h>
19 #  endif
20 #  if defined(SOLARIS) || defined(AIX)
21 #    include <ucontext.h>
22 #  endif
23 #  if defined(ANDROID) && !defined(__aarch64__)
24 #    include <sys/types.h>
25 #    include <unistd.h>
26 #  endif
27 #  if defined(XP_LINUX) && !defined(ANDROID) && defined(__GLIBC__)
28 #    include <dlfcn.h>
29 #    include <sys/syscall.h>
30 #    include <sys/types.h>
31 #    include <unistd.h>
32 #    define gettid() static_cast<pid_t>(syscall(__NR_gettid))
33 #  endif
34 #else
35 #  error "Unsupported platform"
36 #endif
37 
38 #include "js/friend/StackLimits.h"  // JS_STACK_GROWTH_DIRECTION
39 
40 #if defined(XP_WIN)
41 
GetNativeStackBaseImpl()42 void* js::GetNativeStackBaseImpl() {
43   PNT_TIB pTib = reinterpret_cast<PNT_TIB>(NtCurrentTeb());
44   return static_cast<void*>(pTib->StackBase);
45 }
46 
47 #elif defined(SOLARIS)
48 
49 static_assert(JS_STACK_GROWTH_DIRECTION < 0);
50 
GetNativeStackBaseImpl()51 void* js::GetNativeStackBaseImpl() {
52   stack_t st;
53   stack_getbounds(&st);
54   return static_cast<char*>(st.ss_sp) + st.ss_size;
55 }
56 
57 #elif defined(AIX)
58 
59 static_assert(JS_STACK_GROWTH_DIRECTION < 0);
60 
GetNativeStackBaseImpl()61 void* js::GetNativeStackBaseImpl() {
62   ucontext_t context;
63   getcontext(&context);
64   return static_cast<char*>(context.uc_stack.ss_sp) + context.uc_stack.ss_size;
65 }
66 
67 #elif defined(XP_LINUX) && !defined(ANDROID) && defined(__GLIBC__)
GetNativeStackBaseImpl()68 void* js::GetNativeStackBaseImpl() {
69   // On the main thread, get stack base from glibc's __libc_stack_end rather
70   // than pthread APIs to avoid filesystem calls /proc/self/maps.  Non-main
71   // threads spawned with pthreads can read this information directly from their
72   // pthread struct, but the main thread must go parse /proc/self/maps to figure
73   // the mapped stack address space ranges.  We want to avoid reading from
74   // /proc/ so that firefox can run in sandboxed environments where /proc may
75   // not be mounted.
76   if (gettid() == getpid()) {
77     void** pLibcStackEnd = (void**)dlsym(RTLD_DEFAULT, "__libc_stack_end");
78 
79     // If __libc_stack_end is not found, architecture specific frame pointer
80     // hopping will need to be implemented.
81     MOZ_RELEASE_ASSERT(
82         pLibcStackEnd,
83         "__libc_stack_end unavailable, unable to setup stack range for JS");
84     void* stackBase = *pLibcStackEnd;
85     MOZ_RELEASE_ASSERT(
86         stackBase, "invalid stack base, unable to setup stack range for JS");
87 
88     // We don't need to fix stackBase, as it already roughly points to beginning
89     // of the stack
90     return stackBase;
91   }
92 
93   // Non-main threads have the required info stored in memory, so no filesystem
94   // calls are made.
95   pthread_t thread = pthread_self();
96   pthread_attr_t sattr;
97   pthread_attr_init(&sattr);
98   pthread_getattr_np(thread, &sattr);
99 
100   // stackBase will be the *lowest* address on all architectures.
101   void* stackBase = nullptr;
102   size_t stackSize = 0;
103   int rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
104   if (rc) {
105     MOZ_CRASH(
106         "call to pthread_attr_getstack failed, unable to setup stack range for "
107         "JS");
108   }
109   MOZ_RELEASE_ASSERT(stackBase,
110                      "invalid stack base, unable to setup stack range for JS");
111   pthread_attr_destroy(&sattr);
112 
113 #  if JS_STACK_GROWTH_DIRECTION > 0
114   return stackBase;
115 #  else
116   return static_cast<char*>(stackBase) + stackSize;
117 #  endif
118 }
119 
120 #elif defined(__wasi__)
121 
122 // Since we rearrange the layout for wasi via --stack-first flag for the linker
123 // the final layout is: 0x00 | <- stack | data | heap -> |.
124 static void* const NativeStackBase = __builtin_frame_address(0);
125 
GetNativeStackBaseImpl()126 void* js::GetNativeStackBaseImpl() {
127   MOZ_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
128   return NativeStackBase;
129 }
130 
131 #else  // __wasi__
132 
GetNativeStackBaseImpl()133 void* js::GetNativeStackBaseImpl() {
134   pthread_t thread = pthread_self();
135 #  if defined(XP_DARWIN) || defined(DARWIN)
136   return pthread_get_stackaddr_np(thread);
137 
138 #  else
139   pthread_attr_t sattr;
140   pthread_attr_init(&sattr);
141 #    if defined(__OpenBSD__)
142   stack_t ss;
143 #    elif defined(PTHREAD_NP_H) || defined(_PTHREAD_NP_H_) || defined(NETBSD)
144   /* e.g. on FreeBSD 4.8 or newer, neundorf@kde.org */
145   pthread_attr_get_np(thread, &sattr);
146 #    else
147   /*
148    * FIXME: this function is non-portable;
149    * other POSIX systems may have different np alternatives
150    */
151   pthread_getattr_np(thread, &sattr);
152 #    endif
153 
154   void* stackBase = 0;
155   size_t stackSize = 0;
156   int rc;
157 #    if defined(__OpenBSD__)
158   rc = pthread_stackseg_np(pthread_self(), &ss);
159   stackBase = (void*)((size_t)ss.ss_sp - ss.ss_size);
160   stackSize = ss.ss_size;
161 #    elif defined(ANDROID) && !defined(__aarch64__)
162   if (gettid() == getpid()) {
163     // bionic's pthread_attr_getstack prior to API 21 doesn't tell the truth
164     // for the main thread (see bug 846670). So we scan /proc/self/maps to
165     // find the segment which contains the stack.
166     rc = -1;
167 
168     // Put the string on the stack, otherwise there is the danger that it
169     // has not been decompressed by the the on-demand linker. Bug 1165460.
170     //
171     // The volatile keyword should stop the compiler from trying to omit
172     // the stack copy in the future (hopefully).
173     volatile char path[] = "/proc/self/maps";
174     FILE* fs = fopen((const char*)path, "r");
175 
176     if (fs) {
177       char line[100];
178       unsigned long stackAddr = (unsigned long)&sattr;
179       while (fgets(line, sizeof(line), fs) != nullptr) {
180         unsigned long stackStart;
181         unsigned long stackEnd;
182         if (sscanf(line, "%lx-%lx ", &stackStart, &stackEnd) == 2 &&
183             stackAddr >= stackStart && stackAddr < stackEnd) {
184           stackBase = (void*)stackStart;
185           stackSize = stackEnd - stackStart;
186           rc = 0;
187           break;
188         }
189       }
190       fclose(fs);
191     }
192   } else {
193     // For non main-threads pthread allocates the stack itself so it tells
194     // the truth.
195     rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
196   }
197 #    else
198   rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
199 #    endif
200   if (rc) {
201     MOZ_CRASH();
202   }
203   MOZ_ASSERT(stackBase);
204   pthread_attr_destroy(&sattr);
205 
206 #    if JS_STACK_GROWTH_DIRECTION > 0
207   return stackBase;
208 #    else
209   return static_cast<char*>(stackBase) + stackSize;
210 #    endif
211 #  endif
212 }
213 
214 #endif /* !XP_WIN */
215