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