| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "platform/heap/StackFrameDepth.h" |
| 6 | 6 |
| 7 #include "public/platform/Platform.h" | 7 #include "public/platform/Platform.h" |
| 8 | 8 |
| 9 #if OS(WIN) | 9 #if OS(WIN) |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 #include <windows.h> | 11 #include <windows.h> |
| 12 #include <winnt.h> | 12 #include <winnt.h> |
| 13 #elif defined(__GLIBC__) | 13 #elif defined(__GLIBC__) |
| 14 extern "C" void* __libc_stack_end; // NOLINT | 14 extern "C" void* __libc_stack_end; // NOLINT |
| 15 #endif | 15 #endif |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 | 18 |
| 19 static const char* s_avoidOptimization = nullptr; | 19 static const char* s_avoidOptimization = nullptr; |
| 20 | 20 |
| 21 uintptr_t StackFrameDepth::s_stackFrameLimit = kMinimumStackLimit; | |
| 22 | |
| 23 // NEVER_INLINE ensures that |dummy| array on configureLimit() is not optimized
away, | 21 // NEVER_INLINE ensures that |dummy| array on configureLimit() is not optimized
away, |
| 24 // and the stack frame base register is adjusted |kSafeStackFrameSize|. | 22 // and the stack frame base register is adjusted |kSafeStackFrameSize|. |
| 25 NEVER_INLINE static uintptr_t currentStackFrameBaseOnCallee(const char* dummy) | 23 NEVER_INLINE static uintptr_t currentStackFrameBaseOnCallee(const char* dummy) |
| 26 { | 24 { |
| 27 s_avoidOptimization = dummy; | 25 s_avoidOptimization = dummy; |
| 28 return StackFrameDepth::currentStackFrame(); | 26 return StackFrameDepth::currentStackFrame(); |
| 29 } | 27 } |
| 30 | 28 |
| 31 uintptr_t StackFrameDepth::getFallbackStackLimit() | 29 uintptr_t StackFrameDepth::getFallbackStackLimit() |
| 32 { | 30 { |
| 33 // Allocate an |kSafeStackFrameSize|-sized object on stack and query | 31 // Allocate an |kSafeStackFrameSize|-sized object on stack and query |
| 34 // stack frame base after it. | 32 // stack frame base after it. |
| 35 char dummy[kSafeStackFrameSize]; | 33 char dummy[kSafeStackFrameSize]; |
| 36 | 34 |
| 37 // Check that the stack frame can be used. | 35 // Check that the stack frame can be used. |
| 38 dummy[sizeof(dummy) - 1] = 0; | 36 dummy[sizeof(dummy) - 1] = 0; |
| 39 return currentStackFrameBaseOnCallee(dummy); | 37 return currentStackFrameBaseOnCallee(dummy); |
| 40 } | 38 } |
| 41 | 39 |
| 42 void StackFrameDepth::enableStackLimit() | 40 void StackFrameDepth::enableStackLimit() |
| 43 { | 41 { |
| 44 // All supported platforms will currently return a non-zero estimate, | 42 // All supported platforms will currently return a non-zero estimate, |
| 45 // except if ASan is enabled. | 43 // except if ASan is enabled. |
| 46 size_t stackSize = getUnderestimatedStackSize(); | 44 size_t stackSize = getUnderestimatedStackSize(); |
| 47 if (!stackSize) { | 45 if (!stackSize) { |
| 48 s_stackFrameLimit = getFallbackStackLimit(); | 46 m_stackFrameLimit = getFallbackStackLimit(); |
| 49 return; | 47 return; |
| 50 } | 48 } |
| 51 | 49 |
| 52 static const int kStackRoomSize = 1024; | 50 static const int kStackRoomSize = 1024; |
| 53 | 51 |
| 54 Address stackBase = reinterpret_cast<Address>(getStackStart()); | 52 Address stackBase = reinterpret_cast<Address>(getStackStart()); |
| 55 RELEASE_ASSERT(stackSize > static_cast<const size_t>(kStackRoomSize)); | 53 CHECK_GT(stackSize, static_cast<const size_t>(kStackRoomSize)); |
| 56 size_t stackRoom = stackSize - kStackRoomSize; | 54 size_t stackRoom = stackSize - kStackRoomSize; |
| 57 RELEASE_ASSERT(stackBase > reinterpret_cast<Address>(stackRoom)); | 55 CHECK_GT(stackBase, reinterpret_cast<Address>(stackRoom)); |
| 58 s_stackFrameLimit = reinterpret_cast<uintptr_t>(stackBase - stackRoom); | 56 m_stackFrameLimit = reinterpret_cast<uintptr_t>(stackBase - stackRoom); |
| 59 | 57 |
| 60 // If current stack use is already exceeding estimated limit, mark as disabl
ed. | 58 // If current stack use is already exceeding estimated limit, mark as disabl
ed. |
| 61 if (!isSafeToRecurse()) | 59 if (!isSafeToRecurse()) |
| 62 disableStackLimit(); | 60 disableStackLimit(); |
| 63 } | 61 } |
| 64 | 62 |
| 65 size_t StackFrameDepth::getUnderestimatedStackSize() | 63 size_t StackFrameDepth::getUnderestimatedStackSize() |
| 66 { | 64 { |
| 67 // FIXME: ASAN bot uses a fake stack as a thread stack frame, | 65 // FIXME: ASAN bot uses a fake stack as a thread stack frame, |
| 68 // and its size is different from the value which APIs tells us. | 66 // and its size is different from the value which APIs tells us. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 83 #if OS(FREEBSD) | 81 #if OS(FREEBSD) |
| 84 pthread_attr_init(&attr); | 82 pthread_attr_init(&attr); |
| 85 error = pthread_attr_get_np(pthread_self(), &attr); | 83 error = pthread_attr_get_np(pthread_self(), &attr); |
| 86 #else | 84 #else |
| 87 error = pthread_getattr_np(pthread_self(), &attr); | 85 error = pthread_getattr_np(pthread_self(), &attr); |
| 88 #endif | 86 #endif |
| 89 if (!error) { | 87 if (!error) { |
| 90 void* base; | 88 void* base; |
| 91 size_t size; | 89 size_t size; |
| 92 error = pthread_attr_getstack(&attr, &base, &size); | 90 error = pthread_attr_getstack(&attr, &base, &size); |
| 93 RELEASE_ASSERT(!error); | 91 CHECK(!error); |
| 94 pthread_attr_destroy(&attr); | 92 pthread_attr_destroy(&attr); |
| 95 return size; | 93 return size; |
| 96 } | 94 } |
| 97 #if OS(FREEBSD) | 95 #if OS(FREEBSD) |
| 98 pthread_attr_destroy(&attr); | 96 pthread_attr_destroy(&attr); |
| 99 #endif | 97 #endif |
| 100 | 98 |
| 101 // Return a 512k stack size, (conservatively) assuming the following: | 99 // Return a 512k stack size, (conservatively) assuming the following: |
| 102 // - that size is much lower than the pthreads default (x86 pthreads has a
2M default.) | 100 // - that size is much lower than the pthreads default (x86 pthreads has a
2M default.) |
| 103 // - no one is running Blink with an RLIMIT_STACK override, let alone as | 101 // - no one is running Blink with an RLIMIT_STACK override, let alone as |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 #if OS(FREEBSD) | 139 #if OS(FREEBSD) |
| 142 pthread_attr_init(&attr); | 140 pthread_attr_init(&attr); |
| 143 error = pthread_attr_get_np(pthread_self(), &attr); | 141 error = pthread_attr_get_np(pthread_self(), &attr); |
| 144 #else | 142 #else |
| 145 error = pthread_getattr_np(pthread_self(), &attr); | 143 error = pthread_getattr_np(pthread_self(), &attr); |
| 146 #endif | 144 #endif |
| 147 if (!error) { | 145 if (!error) { |
| 148 void* base; | 146 void* base; |
| 149 size_t size; | 147 size_t size; |
| 150 error = pthread_attr_getstack(&attr, &base, &size); | 148 error = pthread_attr_getstack(&attr, &base, &size); |
| 151 RELEASE_ASSERT(!error); | 149 CHECK(!error); |
| 152 pthread_attr_destroy(&attr); | 150 pthread_attr_destroy(&attr); |
| 153 return reinterpret_cast<uint8_t*>(base) + size; | 151 return reinterpret_cast<uint8_t*>(base) + size; |
| 154 } | 152 } |
| 155 #if OS(FREEBSD) | 153 #if OS(FREEBSD) |
| 156 pthread_attr_destroy(&attr); | 154 pthread_attr_destroy(&attr); |
| 157 #endif | 155 #endif |
| 158 #if defined(__GLIBC__) | 156 #if defined(__GLIBC__) |
| 159 // pthread_getattr_np can fail for the main thread. In this case | 157 // 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 | 158 // just like NaCl we rely on the __libc_stack_end to give us |
| 161 // the start of the stack. | 159 // the start of the stack. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 175 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase)))
; | 173 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase)))
; |
| 176 #else | 174 #else |
| 177 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); | 175 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); |
| 178 #endif | 176 #endif |
| 179 #else | 177 #else |
| 180 #error Unsupported getStackStart on this platform. | 178 #error Unsupported getStackStart on this platform. |
| 181 #endif | 179 #endif |
| 182 } | 180 } |
| 183 | 181 |
| 184 } // namespace blink | 182 } // namespace blink |
| OLD | NEW |