| 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 #include "wtf/StackUtil.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 blink { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 31 char dummy[kSafeStackFrameSize]; | 32 char dummy[kSafeStackFrameSize]; |
| 32 | 33 |
| 33 // Check that the stack frame can be used. | 34 // Check that the stack frame can be used. |
| 34 dummy[sizeof(dummy) - 1] = 0; | 35 dummy[sizeof(dummy) - 1] = 0; |
| 35 return currentStackFrameBaseOnCallee(dummy); | 36 return currentStackFrameBaseOnCallee(dummy); |
| 36 } | 37 } |
| 37 | 38 |
| 38 void StackFrameDepth::enableStackLimit() { | 39 void StackFrameDepth::enableStackLimit() { |
| 39 // All supported platforms will currently return a non-zero estimate, | 40 // All supported platforms will currently return a non-zero estimate, |
| 40 // except if ASan is enabled. | 41 // except if ASan is enabled. |
| 41 size_t stackSize = getUnderestimatedStackSize(); | 42 size_t stackSize = WTF::getUnderestimatedStackSize(); |
| 42 if (!stackSize) { | 43 if (!stackSize) { |
| 43 m_stackFrameLimit = getFallbackStackLimit(); | 44 m_stackFrameLimit = getFallbackStackLimit(); |
| 44 return; | 45 return; |
| 45 } | 46 } |
| 46 | 47 |
| 47 static const int kStackRoomSize = 1024; | 48 static const int kStackRoomSize = 1024; |
| 48 | 49 |
| 49 Address stackBase = reinterpret_cast<Address>(getStackStart()); | 50 Address stackBase = reinterpret_cast<Address>(WTF::getStackStart()); |
| 50 RELEASE_ASSERT(stackSize > static_cast<const size_t>(kStackRoomSize)); | 51 RELEASE_ASSERT(stackSize > static_cast<const size_t>(kStackRoomSize)); |
| 51 size_t stackRoom = stackSize - kStackRoomSize; | 52 size_t stackRoom = stackSize - kStackRoomSize; |
| 52 RELEASE_ASSERT(stackBase > reinterpret_cast<Address>(stackRoom)); | 53 RELEASE_ASSERT(stackBase > reinterpret_cast<Address>(stackRoom)); |
| 53 m_stackFrameLimit = reinterpret_cast<uintptr_t>(stackBase - stackRoom); | 54 m_stackFrameLimit = reinterpret_cast<uintptr_t>(stackBase - stackRoom); |
| 54 | 55 |
| 55 // If current stack use is already exceeding estimated limit, mark as | 56 // If current stack use is already exceeding estimated limit, mark as |
| 56 // disabled. | 57 // disabled. |
| 57 if (!isSafeToRecurse()) | 58 if (!isSafeToRecurse()) |
| 58 disableStackLimit(); | 59 disableStackLimit(); |
| 59 } | 60 } |
| 60 | 61 |
| 61 size_t StackFrameDepth::getUnderestimatedStackSize() { | |
| 62 // 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. | |
| 64 #if defined(ADDRESS_SANITIZER) | |
| 65 return 0; | |
| 66 #endif | |
| 67 | |
| 68 // FIXME: On Mac OSX and Linux, this method cannot estimate stack size | |
| 69 // correctly for the main thread. | |
| 70 | |
| 71 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | |
| 72 // pthread_getattr_np() can fail if the thread is not invoked by | |
| 73 // pthread_create() (e.g., the main thread of webkit_unit_tests). | |
| 74 // If so, a conservative size estimate is returned. | |
| 75 | |
| 76 pthread_attr_t attr; | |
| 77 int error; | |
| 78 #if OS(FREEBSD) | |
| 79 pthread_attr_init(&attr); | |
| 80 error = pthread_attr_get_np(pthread_self(), &attr); | |
| 81 #else | |
| 82 error = pthread_getattr_np(pthread_self(), &attr); | |
| 83 #endif | |
| 84 if (!error) { | |
| 85 void* base; | |
| 86 size_t size; | |
| 87 error = pthread_attr_getstack(&attr, &base, &size); | |
| 88 RELEASE_ASSERT(!error); | |
| 89 pthread_attr_destroy(&attr); | |
| 90 return size; | |
| 91 } | |
| 92 #if OS(FREEBSD) | |
| 93 pthread_attr_destroy(&attr); | |
| 94 #endif | |
| 95 | |
| 96 // Return a 512k stack size, (conservatively) assuming the following: | |
| 97 // - that size is much lower than the pthreads default (x86 pthreads has a 2M | |
| 98 // default.) | |
| 99 // - no one is running Blink with an RLIMIT_STACK override, let alone as | |
| 100 // low as 512k. | |
| 101 // | |
| 102 return 512 * 1024; | |
| 103 #elif OS(MACOSX) | |
| 104 // pthread_get_stacksize_np() returns too low a value for the main thread on | |
| 105 // OSX 10.9, | |
| 106 // http://mail.openjdk.java.net/pipermail/hotspot-dev/2013-October/011369.html | |
| 107 // | |
| 108 // Multiple workarounds possible, adopt the one made by | |
| 109 // https://github.com/robovm/robovm/issues/274 | |
| 110 // (cf. | |
| 111 // https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Mult
ithreading/CreatingThreads/CreatingThreads.html | |
| 112 // on why hardcoding sizes is reasonable.) | |
| 113 if (pthread_main_np()) { | |
| 114 #if defined(IOS) | |
| 115 pthread_attr_t attr; | |
| 116 pthread_attr_init(&attr); | |
| 117 size_t guardSize = 0; | |
| 118 pthread_attr_getguardsize(&attr, &guardSize); | |
| 119 // Stack size for the main thread is 1MB on iOS including the guard page | |
| 120 // size. | |
| 121 return (1 * 1024 * 1024 - guardSize); | |
| 122 #else | |
| 123 // Stack size for the main thread is 8MB on OSX excluding the guard page | |
| 124 // size. | |
| 125 return (8 * 1024 * 1024); | |
| 126 #endif | |
| 127 } | |
| 128 return pthread_get_stacksize_np(pthread_self()); | |
| 129 #elif OS(WIN) && COMPILER(MSVC) | |
| 130 return ThreadState::current()->threadStackSize(); | |
| 131 #else | |
| 132 #error "Stack frame size estimation not supported on this platform." | |
| 133 return 0; | |
| 134 #endif | |
| 135 } | |
| 136 | |
| 137 void* StackFrameDepth::getStackStart() { | |
| 138 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | |
| 139 pthread_attr_t attr; | |
| 140 int error; | |
| 141 #if OS(FREEBSD) | |
| 142 pthread_attr_init(&attr); | |
| 143 error = pthread_attr_get_np(pthread_self(), &attr); | |
| 144 #else | |
| 145 error = pthread_getattr_np(pthread_self(), &attr); | |
| 146 #endif | |
| 147 if (!error) { | |
| 148 void* base; | |
| 149 size_t size; | |
| 150 error = pthread_attr_getstack(&attr, &base, &size); | |
| 151 RELEASE_ASSERT(!error); | |
| 152 pthread_attr_destroy(&attr); | |
| 153 return reinterpret_cast<uint8_t*>(base) + size; | |
| 154 } | |
| 155 #if OS(FREEBSD) | |
| 156 pthread_attr_destroy(&attr); | |
| 157 #endif | |
| 158 #if defined(__GLIBC__) | |
| 159 // 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 | |
| 161 // the start of the stack. | |
| 162 // See https://code.google.com/p/nativeclient/issues/detail?id=3431. | |
| 163 return __libc_stack_end; | |
| 164 #else | |
| 165 ASSERT_NOT_REACHED(); | |
| 166 return nullptr; | |
| 167 #endif | |
| 168 #elif OS(MACOSX) | |
| 169 return pthread_get_stackaddr_np(pthread_self()); | |
| 170 #elif OS(WIN) && COMPILER(MSVC) | |
| 171 // On Windows stack limits for the current thread are available in | |
| 172 // the thread information block (TIB). Its fields can be accessed through | |
| 173 // FS segment register on x86 and GS segment register on x86_64. | |
| 174 #ifdef _WIN64 | |
| 175 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase))); | |
| 176 #else | |
| 177 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); | |
| 178 #endif | |
| 179 #else | |
| 180 #error Unsupported getStackStart on this platform. | |
| 181 #endif | |
| 182 } | |
| 183 | |
| 184 } // namespace blink | 62 } // namespace blink |
| OLD | NEW |