| 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 "config.h" | 5 #include "config.h" |
| 6 #include "platform/heap/StackFrameDepth.h" | 6 #include "platform/heap/StackFrameDepth.h" |
| 7 | 7 |
| 8 #include "public/platform/Platform.h" | 8 #include "public/platform/Platform.h" |
| 9 | 9 |
| 10 #if OS(WIN) | 10 #if OS(WIN) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // NEVER_INLINE ensures that |dummy| array on configureLimit() is not optimized
away, | 22 // NEVER_INLINE ensures that |dummy| array on configureLimit() is not optimized
away, |
| 23 // and the stack frame base register is adjusted |kSafeStackFrameSize|. | 23 // and the stack frame base register is adjusted |kSafeStackFrameSize|. |
| 24 NEVER_INLINE static uintptr_t currentStackFrameBaseOnCallee(const char* dummy) | 24 NEVER_INLINE static uintptr_t currentStackFrameBaseOnCallee(const char* dummy) |
| 25 { | 25 { |
| 26 s_avoidOptimization = dummy; | 26 s_avoidOptimization = dummy; |
| 27 return StackFrameDepth::currentStackFrame(); | 27 return StackFrameDepth::currentStackFrame(); |
| 28 } | 28 } |
| 29 | 29 |
| 30 void StackFrameDepth::configureLimit() | 30 void StackFrameDepth::configureLimit() |
| 31 { | 31 { |
| 32 static const int kStackRoomSize = 1024; | 32 // Allocate a large object in stack and query stack frame pointer after it. |
| 33 | |
| 34 size_t stackSize = getUnderestimatedStackSize(); | |
| 35 if (stackSize) { | |
| 36 size_t stackBase = reinterpret_cast<size_t>(getStackStart()); | |
| 37 m_stackFrameLimit = stackBase - stackSize + kStackRoomSize; | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 // Fallback version | |
| 42 // Allocate a 32KB object on stack and query stack frame base after it. | |
| 43 char dummy[kSafeStackFrameSize]; | 33 char dummy[kSafeStackFrameSize]; |
| 44 m_stackFrameLimit = currentStackFrameBaseOnCallee(dummy); | 34 m_stackFrameLimit = currentStackFrameBaseOnCallee(dummy); |
| 45 | 35 |
| 46 // Assert that the stack frame can be used. | 36 // Assert that the stack frame can be used. |
| 47 dummy[sizeof(dummy) - 1] = 0; | 37 dummy[sizeof(dummy) - 1] = 0; |
| 48 } | 38 } |
| 49 | 39 |
| 50 size_t StackFrameDepth::getUnderestimatedStackSize() | 40 size_t StackFrameDepth::getUnderestimatedStackSize() |
| 51 { | 41 { |
| 52 // FIXME: On ChromeOS and Mac OSX, this method cannot estimate stack size | |
| 53 // correctly for the main thread. | |
| 54 | |
| 55 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 42 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
| 56 // pthread_getattr_np() can fail if the thread is not invoked by | 43 // We cannot get the stack size in these platforms because |
| 57 // pthread_create() (e.g., the main thread of webkit_unit_tests). | 44 // pthread_getattr_np() can fail for the main thread. |
| 58 // In this case, this method returns 0 and the caller must handle it. | 45 // This is OK because ThreadState::current() doesn't use the stack size |
| 59 | 46 // in these platforms. |
| 60 pthread_attr_t attr; | |
| 61 int error; | |
| 62 #if OS(FREEBSD) | |
| 63 pthread_attr_init(&attr); | |
| 64 error = pthread_attr_get_np(pthread_self(), &attr); | |
| 65 #else | |
| 66 error = pthread_getattr_np(pthread_self(), &attr); | |
| 67 #endif | |
| 68 if (!error) { | |
| 69 void* base; | |
| 70 size_t size; | |
| 71 error = pthread_attr_getstack(&attr, &base, &size); | |
| 72 RELEASE_ASSERT(!error); | |
| 73 pthread_attr_destroy(&attr); | |
| 74 return size; | |
| 75 } | |
| 76 #if OS(FREEBSD) | |
| 77 pthread_attr_destroy(&attr); | |
| 78 #endif | |
| 79 | |
| 80 return 0; | 47 return 0; |
| 81 #elif OS(MACOSX) | 48 #elif OS(MACOSX) |
| 82 // FIXME: pthread_get_stacksize_np() seems to return shorter size than actua
l | 49 return pthread_get_stacksize_np(pthread_self()); |
| 83 // stack size. | |
| 84 return 0; | |
| 85 #elif OS(WIN) && COMPILER(MSVC) | 50 #elif OS(WIN) && COMPILER(MSVC) |
| 86 // On Windows stack limits for the current thread are available in | 51 // On Windows stack limits for the current thread are available in |
| 87 // the thread information block (TIB). Its fields can be accessed through | 52 // the thread information block (TIB). Its fields can be accessed through |
| 88 // FS segment register on x86 and GS segment register on x86_64. | 53 // FS segment register on x86 and GS segment register on x86_64. |
| 89 #ifdef _WIN64 | 54 #ifdef _WIN64 |
| 90 return __readgsqword(offsetof(NT_TIB64, StackBase)) - __readgsqword(offsetof
(NT_TIB64, StackLimit)); | 55 return __readgsqword(offsetof(NT_TIB64, StackBase)) - __readgsqword(offsetof
(NT_TIB64, StackLimit)); |
| 91 #else | 56 #else |
| 92 return __readfsdword(offsetof(NT_TIB, StackBase)) - __readfsdword(offsetof(N
T_TIB, StackLimit)); | 57 return __readfsdword(offsetof(NT_TIB, StackBase)) - __readfsdword(offsetof(N
T_TIB, StackLimit)); |
| 93 #endif | 58 #endif |
| 94 #else | 59 #else |
| 95 #error "Stack frame size estimation not supported on this platform." | |
| 96 return 0; | 60 return 0; |
| 97 #endif | 61 #endif |
| 98 } | 62 } |
| 99 | 63 |
| 100 void* StackFrameDepth::getStackStart() | 64 void* StackFrameDepth::getStackStart() |
| 101 { | 65 { |
| 102 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 66 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
| 103 pthread_attr_t attr; | 67 pthread_attr_t attr; |
| 104 int error; | 68 int error; |
| 105 #if OS(FREEBSD) | 69 #if OS(FREEBSD) |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase)))
; | 103 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase)))
; |
| 140 #else | 104 #else |
| 141 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); | 105 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); |
| 142 #endif | 106 #endif |
| 143 #else | 107 #else |
| 144 #error Unsupported getStackStart on this platform. | 108 #error Unsupported getStackStart on this platform. |
| 145 #endif | 109 #endif |
| 146 } | 110 } |
| 147 | 111 |
| 148 } // namespace blink | 112 } // namespace blink |
| OLD | NEW |