Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Side by Side Diff: third_party/WebKit/Source/wtf/StackUtil.cpp

Issue 2623273007: Fast path for ThreadSpecific for main thread on TLS-slow platforms (Closed)
Patch Set: [WIP] Fast path for currentThread() for main thread on TLS-slow platforms Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/WTFThreadData.h"
8 9
9 #if OS(WIN) 10 #if OS(WIN)
10 #include <stddef.h> 11 #include <stddef.h>
11 #include <windows.h> 12 #include <windows.h>
12 #include <winnt.h> 13 #include <winnt.h>
13 #elif defined(__GLIBC__) 14 #elif defined(__GLIBC__)
14 extern "C" void* __libc_stack_end; // NOLINT 15 extern "C" void* __libc_stack_end; // NOLINT
15 #endif 16 #endif
16 17
17 namespace blink { 18 namespace WTF {
18 19
19 static const char* s_avoidOptimization = nullptr; 20 size_t getUnderestimatedStackSize() {
20
21 // NEVER_INLINE ensures that |dummy| array on configureLimit() is not optimized
22 // away, and the stack frame base register is adjusted |kSafeStackFrameSize|.
23 NEVER_INLINE static uintptr_t currentStackFrameBaseOnCallee(const char* dummy) {
24 s_avoidOptimization = dummy;
25 return StackFrameDepth::currentStackFrame();
26 }
27
28 uintptr_t StackFrameDepth::getFallbackStackLimit() {
29 // Allocate an |kSafeStackFrameSize|-sized object on stack and query
30 // stack frame base after it.
31 char dummy[kSafeStackFrameSize];
32
33 // Check that the stack frame can be used.
34 dummy[sizeof(dummy) - 1] = 0;
35 return currentStackFrameBaseOnCallee(dummy);
36 }
37
38 void StackFrameDepth::enableStackLimit() {
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
47 static const int kStackRoomSize = 1024;
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, 21 // 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. 22 // and its size is different from the value which APIs tells us.
64 #if defined(ADDRESS_SANITIZER) 23 #if defined(ADDRESS_SANITIZER)
65 return 0; 24 return 0;
66 #endif 25 #endif
67 26
68 // FIXME: On Mac OSX and Linux, this method cannot estimate stack size 27 // FIXME: On Mac OSX and Linux, this method cannot estimate stack size
69 // correctly for the main thread. 28 // correctly for the main thread.
70 29
71 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) 30 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD)
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 // size. 79 // size.
121 return (1 * 1024 * 1024 - guardSize); 80 return (1 * 1024 * 1024 - guardSize);
122 #else 81 #else
123 // Stack size for the main thread is 8MB on OSX excluding the guard page 82 // Stack size for the main thread is 8MB on OSX excluding the guard page
124 // size. 83 // size.
125 return (8 * 1024 * 1024); 84 return (8 * 1024 * 1024);
126 #endif 85 #endif
127 } 86 }
128 return pthread_get_stacksize_np(pthread_self()); 87 return pthread_get_stacksize_np(pthread_self());
129 #elif OS(WIN) && COMPILER(MSVC) 88 #elif OS(WIN) && COMPILER(MSVC)
130 return ThreadState::current()->threadStackSize(); 89 return WTFThreadData::current().threadStackSize();
131 #else 90 #else
132 #error "Stack frame size estimation not supported on this platform." 91 #error "Stack frame size estimation not supported on this platform."
133 return 0; 92 return 0;
134 #endif 93 #endif
135 } 94 }
136 95
137 void* StackFrameDepth::getStackStart() { 96 void* getStackStart() {
138 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) 97 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD)
139 pthread_attr_t attr; 98 pthread_attr_t attr;
140 int error; 99 int error;
141 #if OS(FREEBSD) 100 #if OS(FREEBSD)
142 pthread_attr_init(&attr); 101 pthread_attr_init(&attr);
143 error = pthread_attr_get_np(pthread_self(), &attr); 102 error = pthread_attr_get_np(pthread_self(), &attr);
144 #else 103 #else
145 error = pthread_getattr_np(pthread_self(), &attr); 104 error = pthread_getattr_np(pthread_self(), &attr);
146 #endif 105 #endif
147 if (!error) { 106 if (!error) {
148 void* base; 107 void* base;
149 size_t size; 108 size_t size;
150 error = pthread_attr_getstack(&attr, &base, &size); 109 error = pthread_attr_getstack(&attr, &base, &size);
151 RELEASE_ASSERT(!error); 110 RELEASE_ASSERT(!error);
152 pthread_attr_destroy(&attr); 111 pthread_attr_destroy(&attr);
153 return reinterpret_cast<uint8_t*>(base) + size; 112 return reinterpret_cast<uint8_t*>(base) + size;
154 } 113 }
155 #if OS(FREEBSD) 114 #if OS(FREEBSD)
156 pthread_attr_destroy(&attr); 115 pthread_attr_destroy(&attr);
157 #endif 116 #endif
158 #if defined(__GLIBC__) 117 #if defined(__GLIBC__)
159 // pthread_getattr_np can fail for the main thread. In this case 118 // 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 119 // just like NaCl we rely on the __libc_stack_end to give us
161 // the start of the stack. 120 // the start of the stack.
162 // See https://code.google.com/p/nativeclient/issues/detail?id=3431. 121 // See https://code.google.com/p/nativeclient/issues/detail?id=3431.
163 return __libc_stack_end; 122 return __libc_stack_end;
164 #else 123 #else
165 ASSERT_NOT_REACHED(); 124 NOTREACHED();
166 return nullptr; 125 return nullptr;
167 #endif 126 #endif
168 #elif OS(MACOSX) 127 #elif OS(MACOSX)
169 return pthread_get_stackaddr_np(pthread_self()); 128 return pthread_get_stackaddr_np(pthread_self());
170 #elif OS(WIN) && COMPILER(MSVC) 129 #elif OS(WIN) && COMPILER(MSVC)
171 // On Windows stack limits for the current thread are available in 130 // On Windows stack limits for the current thread are available in
172 // the thread information block (TIB). Its fields can be accessed through 131 // the thread information block (TIB). Its fields can be accessed through
173 // FS segment register on x86 and GS segment register on x86_64. 132 // FS segment register on x86 and GS segment register on x86_64.
174 #ifdef _WIN64 133 #ifdef _WIN64
175 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase))); 134 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase)));
176 #else 135 #else
177 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); 136 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase)));
178 #endif 137 #endif
179 #else 138 #else
180 #error Unsupported getStackStart on this platform. 139 #error Unsupported getStackStart on this platform.
181 #endif 140 #endif
182 } 141 }
183 142
184 } // namespace blink 143 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698