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 // Allocate a large object in stack and query stack frame pointer after it. | 32 static const int kStackRoomSize= 1024; |
kouhei (in TOK)
2015/02/12 03:46:05
space
| |
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. | |
33 char dummy[kSafeStackFrameSize]; | 43 char dummy[kSafeStackFrameSize]; |
34 m_stackFrameLimit = currentStackFrameBaseOnCallee(dummy); | 44 m_stackFrameLimit = currentStackFrameBaseOnCallee(dummy); |
35 | 45 |
36 // Assert that the stack frame can be used. | 46 // Assert that the stack frame can be used. |
37 dummy[sizeof(dummy) - 1] = 0; | 47 dummy[sizeof(dummy) - 1] = 0; |
38 } | 48 } |
39 | 49 |
40 size_t StackFrameDepth::getUnderestimatedStackSize() | 50 size_t StackFrameDepth::getUnderestimatedStackSize() |
41 { | 51 { |
42 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 52 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
43 // We cannot get the stack size in these platforms because | 53 // pthread_getattr_np() can fail if the thread is not invoked by |
44 // pthread_getattr_np() can fail for the main thread. | 54 // pthread_create() (e.g., the main thread of webkit_unit_tests). |
45 // This is OK because ThreadState::current() doesn't use the stack size | 55 // In this case, this method returns 0 and the caller must handle it. |
46 // in these platforms. | 56 |
57 pthread_attr_t attr; | |
58 int error; | |
59 #if OS(FREEBSD) | |
60 pthread_attr_init(&attr); | |
61 error = pthread_attr_get_np(pthread_self(), &attr); | |
62 #else | |
63 error = pthread_getattr_np(pthread_self(), &attr); | |
64 #endif | |
65 if (!error) { | |
66 void* base; | |
67 size_t size; | |
68 error = pthread_attr_getstack(&attr, &base, &size); | |
69 RELEASE_ASSERT(!error); | |
70 pthread_attr_destroy(&attr); | |
71 return size; | |
72 } | |
73 #if OS(FREEBSD) | |
74 pthread_attr_destroy(&attr); | |
75 #endif | |
76 | |
47 return 0; | 77 return 0; |
48 #elif OS(MACOSX) | 78 #elif OS(MACOSX) |
49 return pthread_get_stacksize_np(pthread_self()); | 79 return pthread_get_stacksize_np(pthread_self()); |
50 #elif OS(WIN) && COMPILER(MSVC) | 80 #elif OS(WIN) && COMPILER(MSVC) |
51 // On Windows stack limits for the current thread are available in | 81 // On Windows stack limits for the current thread are available in |
52 // the thread information block (TIB). Its fields can be accessed through | 82 // the thread information block (TIB). Its fields can be accessed through |
53 // FS segment register on x86 and GS segment register on x86_64. | 83 // FS segment register on x86 and GS segment register on x86_64. |
54 #ifdef _WIN64 | 84 #ifdef _WIN64 |
55 return __readgsqword(offsetof(NT_TIB64, StackBase)) - __readgsqword(offsetof (NT_TIB64, StackLimit)); | 85 return __readgsqword(offsetof(NT_TIB64, StackBase)) - __readgsqword(offsetof (NT_TIB64, StackLimit)); |
56 #else | 86 #else |
57 return __readfsdword(offsetof(NT_TIB, StackBase)) - __readfsdword(offsetof(N T_TIB, StackLimit)); | 87 return __readfsdword(offsetof(NT_TIB, StackBase)) - __readfsdword(offsetof(N T_TIB, StackLimit)); |
58 #endif | 88 #endif |
59 #else | 89 #else |
90 #error "Stack frame size estimation not supported on this platform." | |
60 return 0; | 91 return 0; |
61 #endif | 92 #endif |
62 } | 93 } |
63 | 94 |
64 void* StackFrameDepth::getStackStart() | 95 void* StackFrameDepth::getStackStart() |
65 { | 96 { |
66 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 97 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
67 pthread_attr_t attr; | 98 pthread_attr_t attr; |
68 int error; | 99 int error; |
69 #if OS(FREEBSD) | 100 #if OS(FREEBSD) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase))) ; | 134 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase))) ; |
104 #else | 135 #else |
105 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); | 136 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); |
106 #endif | 137 #endif |
107 #else | 138 #else |
108 #error Unsupported getStackStart on this platform. | 139 #error Unsupported getStackStart on this platform. |
109 #endif | 140 #endif |
110 } | 141 } |
111 | 142 |
112 } // namespace blink | 143 } // namespace blink |
OLD | NEW |