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) |
11 #include <stddef.h> | 11 #include <stddef.h> |
12 #include <windows.h> | 12 #include <windows.h> |
13 #include <winnt.h> | 13 #include <winnt.h> |
14 #elif defined(__GLIBC__) | 14 #elif defined(__GLIBC__) |
15 extern "C" void* __libc_stack_end; // NOLINT | 15 extern "C" void* __libc_stack_end; // NOLINT |
16 #endif | 16 #endif |
17 | 17 |
18 namespace blink { | 18 namespace blink { |
19 | 19 |
20 const int StackFrameDepth::kStackRoomSize = 1024; | |
haraken
2015/02/10 12:15:09
Shall we make this a static variable of StackFrame
kouhei (in TOK)
2015/02/10 12:35:38
static const int +1
peria
2015/02/12 02:31:58
Done.
| |
20 static const char* s_avoidOptimization = nullptr; | 21 static const char* s_avoidOptimization = nullptr; |
21 | 22 |
22 // NEVER_INLINE ensures that |dummy| array on configureLimit() is not optimized away, | 23 // NEVER_INLINE ensures that |dummy| array on configureLimit() is not optimized away, |
23 // and the stack frame base register is adjusted |kSafeStackFrameSize|. | 24 // and the stack frame base register is adjusted |kSafeStackFrameSize|. |
24 NEVER_INLINE static uintptr_t currentStackFrameBaseOnCallee(const char* dummy) | 25 NEVER_INLINE static uintptr_t currentStackFrameBaseOnCallee(const char* dummy) |
25 { | 26 { |
26 s_avoidOptimization = dummy; | 27 s_avoidOptimization = dummy; |
27 return StackFrameDepth::currentStackFrame(); | 28 return StackFrameDepth::currentStackFrame(); |
28 } | 29 } |
29 | 30 |
30 void StackFrameDepth::configureLimit() | 31 void StackFrameDepth::configureLimit() |
31 { | 32 { |
32 // Allocate a large object in stack and query stack frame pointer after it. | 33 size_t stackSize = getUnderestimatedStackSize(); |
34 if (stackSize) { | |
35 size_t stackBase = reinterpret_cast<size_t>(getStackStart()); | |
36 m_stackFrameLimit = stackBase - stackSize + kStackRoomSize; | |
37 return; | |
38 } | |
39 | |
40 // Fallback version | |
41 // Allocate a 32KB object on stack and query stack frame base after it. | |
33 char dummy[kSafeStackFrameSize]; | 42 char dummy[kSafeStackFrameSize]; |
34 m_stackFrameLimit = currentStackFrameBaseOnCallee(dummy); | 43 m_stackFrameLimit = currentStackFrameBaseOnCallee(dummy); |
35 | 44 |
36 // Assert that the stack frame can be used. | 45 // Assert that the stack frame can be used. |
37 dummy[sizeof(dummy) - 1] = 0; | 46 dummy[0] = 0; |
haraken
2015/02/10 12:15:09
What is this change for?
kouhei (in TOK)
2015/02/10 12:35:38
To check if the top of the stack is actually writa
peria
2015/02/12 02:31:58
will do in another CL.
| |
38 } | 47 } |
39 | 48 |
40 size_t StackFrameDepth::getUnderestimatedStackSize() | 49 size_t StackFrameDepth::getUnderestimatedStackSize() |
41 { | 50 { |
42 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 51 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
43 // We cannot get the stack size in these platforms because | 52 // We cannot get the stack size on the main thread in these platforms, becau se |
haraken
2015/02/10 12:15:09
Update the comment like:
// pthread_getattr_np()
peria
2015/02/12 02:31:58
Done.
| |
44 // pthread_getattr_np() can fail for the main thread. | 53 // pthread_getattr_np() can fail. |
45 // This is OK because ThreadState::current() doesn't use the stack size | 54 // In this case, this method returns 0 and the caller must handle it. |
46 // in these platforms. | 55 |
56 pthread_attr_t attr; | |
57 int error; | |
58 #if OS(FREEBSD) | |
59 pthread_attr_init(&attr); | |
60 error = pthread_attr_get_np(pthread_self(), &attr); | |
61 #else | |
62 error = pthread_getattr_np(pthread_self(), &attr); | |
63 #endif | |
64 if (!error) { | |
65 void* base; | |
66 size_t size; | |
67 error = pthread_attr_getstack(&attr, &base, &size); | |
68 RELEASE_ASSERT(!error); | |
69 pthread_attr_destroy(&attr); | |
70 return size; | |
71 } | |
72 #if OS(FREEBSD) | |
73 pthread_attr_destroy(&attr); | |
74 #endif | |
75 | |
47 return 0; | 76 return 0; |
48 #elif OS(MACOSX) | 77 #elif OS(MACOSX) |
49 return pthread_get_stacksize_np(pthread_self()); | 78 return pthread_get_stacksize_np(pthread_self()); |
50 #elif OS(WIN) && COMPILER(MSVC) | 79 #elif OS(WIN) && COMPILER(MSVC) |
51 // On Windows stack limits for the current thread are available in | 80 // On Windows stack limits for the current thread are available in |
52 // the thread information block (TIB). Its fields can be accessed through | 81 // the thread information block (TIB). Its fields can be accessed through |
53 // FS segment register on x86 and GS segment register on x86_64. | 82 // FS segment register on x86 and GS segment register on x86_64. |
54 #ifdef _WIN64 | 83 #ifdef _WIN64 |
55 return __readgsqword(offsetof(NT_TIB64, StackBase)) - __readgsqword(offsetof (NT_TIB64, StackLimit)); | 84 return __readgsqword(offsetof(NT_TIB64, StackBase)) - __readgsqword(offsetof (NT_TIB64, StackLimit)); |
56 #else | 85 #else |
57 return __readfsdword(offsetof(NT_TIB, StackBase)) - __readfsdword(offsetof(N T_TIB, StackLimit)); | 86 return __readfsdword(offsetof(NT_TIB, StackBase)) - __readfsdword(offsetof(N T_TIB, StackLimit)); |
58 #endif | 87 #endif |
59 #else | 88 #else |
89 #error "Stack frame size estimation not supported on this platform." | |
60 return 0; | 90 return 0; |
61 #endif | 91 #endif |
62 } | 92 } |
63 | 93 |
64 void* StackFrameDepth::getStackStart() | 94 void* StackFrameDepth::getStackStart() |
65 { | 95 { |
66 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) | 96 #if defined(__GLIBC__) || OS(ANDROID) || OS(FREEBSD) |
67 pthread_attr_t attr; | 97 pthread_attr_t attr; |
68 int error; | 98 int error; |
69 #if OS(FREEBSD) | 99 #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))) ; | 133 return reinterpret_cast<void*>(__readgsqword(offsetof(NT_TIB64, StackBase))) ; |
104 #else | 134 #else |
105 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); | 135 return reinterpret_cast<void*>(__readfsdword(offsetof(NT_TIB, StackBase))); |
106 #endif | 136 #endif |
107 #else | 137 #else |
108 #error Unsupported getStackStart on this platform. | 138 #error Unsupported getStackStart on this platform. |
109 #endif | 139 #endif |
110 } | 140 } |
111 | 141 |
112 } // namespace blink | 142 } // namespace blink |
OLD | NEW |