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/. */
7 #include "util/NativeStack.h"
9 #include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_RELEASE_ASSERT, MOZ_CRASH
12 # include "util/WindowsWrapper.h"
13 #elif defined(__wasi__)
15 #elif defined(XP_DARWIN) || defined(DARWIN) || defined(XP_UNIX)
17 # if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
18 # include <pthread_np.h>
20 # if defined(SOLARIS) || defined(AIX)
21 # include <ucontext.h>
23 # if defined(ANDROID) && !defined(__aarch64__)
24 # include <sys/types.h>
27 # if defined(XP_LINUX) && !defined(ANDROID) && defined(__GLIBC__)
29 # include <sys/syscall.h>
30 # include <sys/types.h>
32 # define gettid() static_cast<pid_t>(syscall(__NR_gettid))
35 # error "Unsupported platform"
38 #include "js/friend/StackLimits.h" // JS_STACK_GROWTH_DIRECTION
42 void* js::GetNativeStackBaseImpl() {
43 PNT_TIB pTib
= reinterpret_cast<PNT_TIB
>(NtCurrentTeb());
44 return static_cast<void*>(pTib
->StackBase
);
47 #elif defined(SOLARIS)
49 static_assert(JS_STACK_GROWTH_DIRECTION
< 0);
51 void* js::GetNativeStackBaseImpl() {
54 return static_cast<char*>(st
.ss_sp
) + st
.ss_size
;
59 static_assert(JS_STACK_GROWTH_DIRECTION
< 0);
61 void* js::GetNativeStackBaseImpl() {
64 return static_cast<char*>(context
.uc_stack
.ss_sp
) + context
.uc_stack
.ss_size
;
67 #elif defined(XP_LINUX) && !defined(ANDROID) && defined(__GLIBC__)
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
76 if (gettid() == getpid()) {
77 void** pLibcStackEnd
= (void**)dlsym(RTLD_DEFAULT
, "__libc_stack_end");
79 // If __libc_stack_end is not found, architecture specific frame pointer
80 // hopping will need to be implemented.
83 "__libc_stack_end unavailable, unable to setup stack range for JS");
84 void* stackBase
= *pLibcStackEnd
;
86 stackBase
, "invalid stack base, unable to setup stack range for JS");
88 // We don't need to fix stackBase, as it already roughly points to beginning
93 // Non-main threads have the required info stored in memory, so no filesystem
95 pthread_t thread
= pthread_self();
97 pthread_attr_init(&sattr
);
98 pthread_getattr_np(thread
, &sattr
);
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
);
106 "call to pthread_attr_getstack failed, unable to setup stack range for "
109 MOZ_RELEASE_ASSERT(stackBase
,
110 "invalid stack base, unable to setup stack range for JS");
111 pthread_attr_destroy(&sattr
);
113 # if JS_STACK_GROWTH_DIRECTION > 0
116 return static_cast<char*>(stackBase
) + stackSize
;
120 #elif defined(__wasi__)
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);
126 void* js::GetNativeStackBaseImpl() {
127 MOZ_ASSERT(JS_STACK_GROWTH_DIRECTION
< 0);
128 return NativeStackBase
;
133 void* js::GetNativeStackBaseImpl() {
134 pthread_t thread
= pthread_self();
135 # if defined(XP_DARWIN) || defined(DARWIN)
136 return pthread_get_stackaddr_np(thread
);
139 pthread_attr_t sattr
;
140 pthread_attr_init(&sattr
);
141 # if defined(__OpenBSD__)
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
);
148 * FIXME: this function is non-portable;
149 * other POSIX systems may have different np alternatives
151 pthread_getattr_np(thread
, &sattr
);
155 size_t stackSize
= 0;
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.
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.
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");
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
;
193 // For non main-threads pthread allocates the stack itself so it tells
195 rc
= pthread_attr_getstack(&sattr
, &stackBase
, &stackSize
);
198 rc
= pthread_attr_getstack(&sattr
, &stackBase
, &stackSize
);
203 MOZ_ASSERT(stackBase
);
204 pthread_attr_destroy(&sattr
);
206 # if JS_STACK_GROWTH_DIRECTION > 0
209 return static_cast<char*>(stackBase
) + stackSize
;