OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/heap/StackFrameDepth.h" | 5 #include "wtf/StackUtil.h" |
6 | 6 |
7 #include "public/platform/Platform.h" | 7 #include "wtf/Assertions.h" |
8 #include "wtf/Threading.h" | |
9 #include "wtf/WTFThreadData.h" | |
8 | 10 |
9 #if OS(WIN) | 11 #if OS(WIN) |
10 #include <stddef.h> | 12 #include <stddef.h> |
11 #include <windows.h> | 13 #include <windows.h> |
12 #include <winnt.h> | 14 #include <winnt.h> |
13 #elif defined(__GLIBC__) | 15 #elif defined(__GLIBC__) |
14 extern "C" void* __libc_stack_end; // NOLINT | 16 extern "C" void* __libc_stack_end; // NOLINT |
15 #endif | 17 #endif |
16 | 18 |
17 namespace blink { | 19 namespace WTF { |
18 | 20 |
19 static const char* s_avoidOptimization = nullptr; | 21 namespace { |
20 | 22 |
21 // NEVER_INLINE ensures that |dummy| array on configureLimit() is not optimized | 23 uintptr_t mainThreadStackStart() { |
22 // away, and the stack frame base register is adjusted |kSafeStackFrameSize|. | 24 static uintptr_t s_mainThreadStackStart = |
23 NEVER_INLINE static uintptr_t currentStackFrameBaseOnCallee(const char* dummy) { | 25 reinterpret_cast<uintptr_t>(WTF::getStackStart()) - sizeof(void*); |
haraken
2017/01/18 05:29:42
Add a comment why we're subtracting sizeof(void*).
Charlie Harrison
2017/01/18 16:29:55
I had to look at historical blame to figure this o
| |
24 s_avoidOptimization = dummy; | 26 return s_mainThreadStackStart; |
25 return StackFrameDepth::currentStackFrame(); | |
26 } | 27 } |
27 | 28 |
28 uintptr_t StackFrameDepth::getFallbackStackLimit() { | 29 uintptr_t mainThreadUnderestimatedStackSize() { |
29 // Allocate an |kSafeStackFrameSize|-sized object on stack and query | 30 static uintptr_t s_underestimatedStackSize; |
30 // stack frame base after it. | 31 if (s_underestimatedStackSize) |
31 char dummy[kSafeStackFrameSize]; | 32 return s_underestimatedStackSize; |
32 | 33 size_t underestimatedStackSize = getUnderestimatedStackSize(); |
33 // Check that the stack frame can be used. | 34 if (underestimatedStackSize > sizeof(void*)) { |
34 dummy[sizeof(dummy) - 1] = 0; | 35 s_underestimatedStackSize = underestimatedStackSize - sizeof(void*); |
haraken
2017/01/18 05:29:42
Ditto.
Charlie Harrison
2017/01/18 16:29:55
Done.
| |
35 return currentStackFrameBaseOnCallee(dummy); | 36 } |
37 return s_underestimatedStackSize; | |
36 } | 38 } |
37 | 39 |
38 void StackFrameDepth::enableStackLimit() { | 40 } // namespace |
39 // All supported platforms will currently return a non-zero estimate, | |
40 // except if ASan is enabled. | |
41 size_t stackSize = getUnderestimatedStackSize(); | |
42 if (!stackSize) { | |
43 m_stackFrameLimit = getFallbackStackLimit(); | |
44 return; | |
45 } | |
46 | 41 |
47 static const int kStackRoomSize = 1024; | 42 size_t getUnderestimatedStackSize() { |
48 | |
49 Address stackBase = reinterpret_cast<Address>(getStackStart()); | |
50 RELEASE_ASSERT(stackSize > static_cast<const size_t>(kStackRoomSize)); | |
51 size_t stackRoom = stackSize - kStackRoomSize; | |
52 RELEASE_ASSERT(stackBase > reinterpret_cast<Address>(stackRoom)); | |
53 m_stackFrameLimit = reinterpret_cast<uintptr_t>(stackBase - stackRoom); | |
54 | |
55 // If current stack use is already exceeding estimated limit, mark as | |
56 // disabled. | |
57 if (!isSafeToRecurse()) | |
58 disableStackLimit(); | |
59 } | |
60 | |
61 size_t StackFrameDepth::getUnderestimatedStackSize() { | |
62 // FIXME: ASAN bot uses a fake stack as a thread stack frame, | 43 // FIXME: ASAN bot uses a fake stack as a thread stack frame, |
63 // and its size is different from the value which APIs tells us. | 44 // and its size is different from the value which APIs tells us. |
64 #if defined(ADDRESS_SANITIZER) | 45 #if defined(ADDRESS_SANITIZER) |
65 return 0; | 46 return 0; |
66 #endif | 47 #endif |
67 | 48 |
68 // FIXME: On Mac OSX and Linux, this method cannot estimate stack size | 49 // FIXME: On Mac OSX and Linux, this method cannot estimate stack size |
69 // correctly for the main thread. | 50 // correctly for the main thread. |
70 | 51 |
71 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 52 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
120 // size. | 101 // size. |
121 return (1 * 1024 * 1024 - guardSize); | 102 return (1 * 1024 * 1024 - guardSize); |
122 #else | 103 #else |
123 // Stack size for the main thread is 8MB on OSX excluding the guard page | 104 // Stack size for the main thread is 8MB on OSX excluding the guard page |
124 // size. | 105 // size. |
125 return (8 * 1024 * 1024); | 106 return (8 * 1024 * 1024); |
126 #endif | 107 #endif |
127 } | 108 } |
128 return pthread_get_stacksize_np(pthread_self()); | 109 return pthread_get_stacksize_np(pthread_self()); |
129 #elif OS(WIN) && COMPILER(MSVC) | 110 #elif OS(WIN) && COMPILER(MSVC) |
130 return ThreadState::current()->threadStackSize(); | 111 return WTFThreadData::threadStackSize(); |
131 #else | 112 #else |
132 #error "Stack frame size estimation not supported on this platform." | 113 #error "Stack frame size estimation not supported on this platform." |
133 return 0; | 114 return 0; |
134 #endif | 115 #endif |
135 } | 116 } |
136 | 117 |
137 void* StackFrameDepth::getStackStart() { | 118 void* getStackStart() { |
138 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 119 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
139 pthread_attr_t attr; | 120 pthread_attr_t attr; |
140 int error; | 121 int error; |
141 #if OS(FREEBSD) | 122 #if OS(FREEBSD) |
142 pthread_attr_init(&attr); | 123 pthread_attr_init(&attr); |
143 error = pthread_attr_get_np(pthread_self(), &attr); | 124 error = pthread_attr_get_np(pthread_self(), &attr); |
144 #else | 125 #else |
145 error = pthread_getattr_np(pthread_self(), &attr); | 126 error = pthread_getattr_np(pthread_self(), &attr); |
146 #endif | 127 #endif |
147 if (!error) { | 128 if (!error) { |
148 void* base; | 129 void* base; |
149 size_t size; | 130 size_t size; |
150 error = pthread_attr_getstack(&attr, &base, &size); | 131 error = pthread_attr_getstack(&attr, &base, &size); |
151 RELEASE_ASSERT(!error); | 132 RELEASE_ASSERT(!error); |
152 pthread_attr_destroy(&attr); | 133 pthread_attr_destroy(&attr); |
153 return reinterpret_cast<uint8_t*>(base) + size; | 134 return reinterpret_cast<uint8_t*>(base) + size; |
154 } | 135 } |
155 #if OS(FREEBSD) | 136 #if OS(FREEBSD) |
156 pthread_attr_destroy(&attr); | 137 pthread_attr_destroy(&attr); |
157 #endif | 138 #endif |
158 #if defined(__GLIBC__) | 139 #if defined(__GLIBC__) |
159 // pthread_getattr_np can fail for the main thread. In this case | 140 // pthread_getattr_np can fail for the main thread. In this case |
160 // just like NaCl we rely on the __libc_stack_end to give us | 141 // just like NaCl we rely on the __libc_stack_end to give us |
161 // the start of the stack. | 142 // the start of the stack. |
162 // See https://code.google.com/p/nativeclient/issues/detail?id=3431. | 143 // See https://code.google.com/p/nativeclient/issues/detail?id=3431. |
163 return __libc_stack_end; | 144 return __libc_stack_end; |
164 #else | 145 #else |
165 ASSERT_NOT_REACHED(); | 146 NOTREACHED(); |
166 return nullptr; | 147 return nullptr; |
167 #endif | 148 #endif |
168 #elif OS(MACOSX) | 149 #elif OS(MACOSX) |
169 return pthread_get_stackaddr_np(pthread_self()); | 150 return pthread_get_stackaddr_np(pthread_self()); |
170 #elif OS(WIN) && COMPILER(MSVC) | 151 #elif OS(WIN) && COMPILER(MSVC) |
171 // On Windows stack limits for the current thread are available in | 152 // On Windows stack limits for the current thread are available in |
172 // the thread information block (TIB). Its fields can be accessed through | 153 // the thread information block (TIB). Its fields can be accessed through |
173 // FS segment register on x86 and GS segment register on x86_64. | 154 // FS segment register on x86 and GS segment register on x86_64. |
174 #ifdef _WIN64 | 155 #ifdef _WIN64 |
175 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase))); | 156 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase))); |
176 #else | 157 #else |
177 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); | 158 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); |
178 #endif | 159 #endif |
179 #else | 160 #else |
180 #error Unsupported getStackStart on this platform. | 161 #error Unsupported getStackStart on this platform. |
181 #endif | 162 #endif |
182 } | 163 } |
183 | 164 |
184 } // namespace blink | 165 namespace internal { |
166 | |
167 bool mayNotBeMainThread() { | |
haraken
2017/01/18 05:29:42
Can we move this function (and all functions calle
Charlie Harrison
2017/01/18 16:29:55
Done, though I haven't inlined the initializers to
| |
168 uintptr_t dummy; | |
169 uintptr_t addressDiff = | |
170 mainThreadStackStart() - reinterpret_cast<uintptr_t>(&dummy); | |
171 // This is a fast way to judge if we are in the main thread. | |
172 // If |&dummy| is within |s_mainThreadUnderestimatedStackSize| byte from | |
173 // the stack start of the main thread, we judge that we are in | |
174 // the main thread. | |
175 return addressDiff >= mainThreadUnderestimatedStackSize(); | |
176 } | |
177 | |
178 #if OS(WIN) && COMPILER(MSVC) | |
179 size_t threadStackSize() { | |
180 // Notice that we cannot use the TIB's StackLimit for the stack end, as i | |
181 // tracks the end of the committed range. We're after the end of the reserved | |
182 // stack area (most of which will be uncommitted, most times.) | |
183 MEMORY_BASIC_INFORMATION stackInfo; | |
184 memset(&stackInfo, 0, sizeof(MEMORY_BASIC_INFORMATION)); | |
185 size_t resultSize = | |
186 VirtualQuery(&stackInfo, &stackInfo, sizeof(MEMORY_BASIC_INFORMATION)); | |
187 DCHECK_GE(resultSize, sizeof(MEMORY_BASIC_INFORMATION)); | |
188 uint8_t* stackEnd = reinterpret_cast<uint8_t*>(stackInfo.AllocationBase); | |
189 | |
190 uint8_t* stackStart = reinterpret_cast<uint8_t*>(WTF::getStackStart()); | |
191 RELEASE_ASSERT(stackStart && stackStart > stackEnd); | |
192 size_t s_threadStackSize = static_cast<size_t>(stackStart - stackEnd); | |
193 // When the third last page of the reserved stack is accessed as a | |
194 // guard page, the second last page will be committed (along with removing | |
195 // the guard bit on the third last) _and_ a stack overflow exception | |
196 // is raised. | |
197 // | |
198 // We have zero interest in running into stack overflow exceptions while | |
199 // marking objects, so simply consider the last three pages + one above | |
200 // as off-limits and adjust the reported stack size accordingly. | |
201 // | |
202 // http://blogs.msdn.com/b/satyem/archive/2012/08/13/thread-s-stack-memory-man agement.aspx | |
203 // explains the details. | |
204 RELEASE_ASSERT(s_threadStackSize > 4 * 0x1000); | |
205 s_threadStackSize -= 4 * 0x1000; | |
206 return s_threadStackSize; | |
207 } | |
208 #endif | |
209 | |
210 } // namespace internal | |
211 | |
212 } // namespace WTF | |
OLD | NEW |