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 "jsnativestack.h"
8
9 #ifdef XP_WIN
10 # include "jswin.h"
11
12 #elif defined(XP_DARWIN) || defined(DARWIN) || defined(XP_UNIX)
13 # include <pthread.h>
14
15 # if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
16 # include <pthread_np.h>
17 # endif
18
19 # if defined(ANDROID)
20 # include <sys/types.h>
21 # include <unistd.h>
22 # endif
23
24 #else
25 # error "Unsupported platform"
26
27 #endif
28
29 #if defined(XP_WIN)
30
31 void*
GetNativeStackBaseImpl()32 js::GetNativeStackBaseImpl()
33 {
34 # if defined(_M_IX86) && defined(_MSC_VER)
35 /*
36 * offset 0x18 from the FS segment register gives a pointer to
37 * the thread information block for the current thread
38 */
39 NT_TIB* pTib;
40 __asm {
41 MOV EAX, FS:[18h]
42 MOV pTib, EAX
43 }
44 return static_cast<void*>(pTib->StackBase);
45
46 # elif defined(_M_X64)
47 PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
48 return reinterpret_cast<void*>(pTib->StackBase);
49
50 # elif defined(_M_ARM)
51 PNT_TIB pTib = reinterpret_cast<PNT_TIB>(NtCurrentTeb());
52 return static_cast<void*>(pTib->StackBase);
53
54 # elif defined(_WIN32) && defined(__GNUC__)
55 NT_TIB* pTib;
56 asm ("movl %%fs:0x18, %0\n" : "=r" (pTib));
57 return static_cast<void*>(pTib->StackBase);
58
59 # endif
60 }
61
62 #elif defined(SOLARIS)
63
64 #include <ucontext.h>
65
66 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
67
68 void*
GetNativeStackBaseImpl()69 js::GetNativeStackBaseImpl()
70 {
71 stack_t st;
72 stack_getbounds(&st);
73 return static_cast<char*>(st.ss_sp) + st.ss_size;
74 }
75
76 #elif defined(AIX)
77
78 #include <ucontext.h>
79
80 JS_STATIC_ASSERT(JS_STACK_GROWTH_DIRECTION < 0);
81
82 void*
GetNativeStackBaseImpl()83 js::GetNativeStackBaseImpl()
84 {
85 ucontext_t context;
86 getcontext(&context);
87 return static_cast<char*>(context.uc_stack.ss_sp) +
88 context.uc_stack.ss_size;
89 }
90
91 #else /* XP_UNIX */
92
93 void*
GetNativeStackBaseImpl()94 js::GetNativeStackBaseImpl()
95 {
96 pthread_t thread = pthread_self();
97 # if defined(XP_DARWIN) || defined(DARWIN)
98 return pthread_get_stackaddr_np(thread);
99
100 # else
101 pthread_attr_t sattr;
102 pthread_attr_init(&sattr);
103 # if defined(__OpenBSD__)
104 stack_t ss;
105 # elif defined(PTHREAD_NP_H) || defined(_PTHREAD_NP_H_) || defined(NETBSD)
106 /* e.g. on FreeBSD 4.8 or newer, neundorf@kde.org */
107 pthread_attr_get_np(thread, &sattr);
108 # else
109 /*
110 * FIXME: this function is non-portable;
111 * other POSIX systems may have different np alternatives
112 */
113 pthread_getattr_np(thread, &sattr);
114 # endif
115
116 void* stackBase = 0;
117 size_t stackSize = 0;
118 int rc;
119 # if defined(__OpenBSD__)
120 rc = pthread_stackseg_np(pthread_self(), &ss);
121 stackBase = (void*)((size_t) ss.ss_sp - ss.ss_size);
122 stackSize = ss.ss_size;
123 # elif defined(ANDROID)
124 if (gettid() == getpid()) {
125 // bionic's pthread_attr_getstack doesn't tell the truth for the main
126 // thread (see bug 846670). So we scan /proc/self/maps to find the
127 // segment which contains the stack.
128 rc = -1;
129
130 // Put the string on the stack, otherwise there is the danger that it
131 // has not been decompressed by the the on-demand linker. Bug 1165460.
132 //
133 // The volatile keyword should stop the compiler from trying to omit
134 // the stack copy in the future (hopefully).
135 volatile char path[] = "/proc/self/maps";
136 FILE* fs = fopen((const char*)path, "r");
137
138 if (fs) {
139 char line[100];
140 unsigned long stackAddr = (unsigned long)&sattr;
141 while (fgets(line, sizeof(line), fs) != nullptr) {
142 unsigned long stackStart;
143 unsigned long stackEnd;
144 if (sscanf(line, "%lx-%lx ", &stackStart, &stackEnd) == 2 &&
145 stackAddr >= stackStart && stackAddr < stackEnd) {
146 stackBase = (void*)stackStart;
147 stackSize = stackEnd - stackStart;
148 rc = 0;
149 break;
150 }
151 }
152 fclose(fs);
153 }
154 } else
155 // For non main-threads pthread allocates the stack itself so it tells
156 // the truth.
157 rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
158 # else
159 rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
160 # endif
161 if (rc)
162 MOZ_CRASH();
163 MOZ_ASSERT(stackBase);
164 pthread_attr_destroy(&sattr);
165
166 # if JS_STACK_GROWTH_DIRECTION > 0
167 return stackBase;
168 # else
169 return static_cast<char*>(stackBase) + stackSize;
170 # endif
171 # endif
172 }
173
174 #endif /* !XP_WIN */
175