Merge "Better solution for Docomo 1339 bug." into tizen_2.1
[framework/web/webkit-efl.git] / Source / WTF / wtf / StackBounds.cpp
1 /*
2  *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3  *  Copyright (C) 2007 Eric Seidel <eric@webkit.org>
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Lesser General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public
16  *  License along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20
21 #include "config.h"
22 #include "StackBounds.h"
23
24 #if OS(DARWIN)
25
26 #include <mach/task.h>
27 #include <mach/thread_act.h>
28 #include <pthread.h>
29
30 #elif OS(WINDOWS)
31
32 #include <windows.h>
33
34 #elif OS(SOLARIS)
35
36 #include <thread.h>
37
38 #elif OS(QNX)
39
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <pthread.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <sys/procfs.h>
46
47 #elif OS(UNIX)
48
49 #include <pthread.h>
50 #if HAVE(PTHREAD_NP_H)
51 #include <pthread_np.h>
52 #endif
53
54 #endif
55
56 namespace WTF {
57
58 // Bug 26276 - Need a mechanism to determine stack extent
59 //
60 // These platforms should now be working correctly:
61 //     DARWIN, QNX, UNIX
62 // These platforms are not:
63 //     WINDOWS, SOLARIS, OPENBSD, WINCE
64 //
65 // FIXME: remove this! - this code unsafely guesses at stack sizes!
66 #if OS(WINDOWS) || OS(SOLARIS) || OS(OPENBSD)
67 // Based on the current limit used by the JSC parser, guess the stack size.
68 static const ptrdiff_t estimatedStackSize = 128 * sizeof(void*) * 1024;
69 // This method assumes the stack is growing downwards.
70 static void* estimateStackBound(void* origin)
71 {
72     return static_cast<char*>(origin) - estimatedStackSize;
73 }
74 #endif
75
76 #if OS(DARWIN)
77
78 void StackBounds::initialize()
79 {
80     pthread_t thread = pthread_self();
81     m_origin = pthread_get_stackaddr_np(thread);
82     m_bound = static_cast<char*>(m_origin) - pthread_get_stacksize_np(thread);
83 }
84
85 #elif OS(QNX)
86
87 void StackBounds::initialize()
88 {
89     void* stackBase = 0;
90     size_t stackSize = 0;
91
92     struct _debug_thread_info threadInfo;
93     memset(&threadInfo, 0, sizeof(threadInfo));
94     threadInfo.tid = pthread_self();
95     int fd = open("/proc/self", O_RDONLY);
96     if (fd == -1) {
97         LOG_ERROR("Unable to open /proc/self (errno: %d)", errno);
98         CRASH();
99     }
100     devctl(fd, DCMD_PROC_TIDSTATUS, &threadInfo, sizeof(threadInfo), 0);
101     close(fd);
102     stackBase = reinterpret_cast<void*>(threadInfo.stkbase);
103     stackSize = threadInfo.stksize;
104     ASSERT(stackBase);
105
106     m_bound = static_cast<char*>(stackBase) + 0x1000; // 4kb guard page
107     m_origin = static_cast<char*>(stackBase) + stackSize;
108 }
109
110 #elif OS(SOLARIS)
111
112 void StackBounds::initialize()
113 {
114     stack_t s;
115     thr_stksegment(&s);
116     m_origin = s.ss_sp;
117     m_bound = estimateStackBound(m_origin);
118 }
119
120 #elif OS(OPENBSD)
121
122 void StackBounds::initialize()
123 {
124     pthread_t thread = pthread_self();
125     stack_t stack;
126     pthread_stackseg_np(thread, &stack);
127     m_origin = stack.ss_sp;
128     m_bound = estimateStackBound(m_origin);
129 }
130
131 #elif OS(UNIX)
132
133 void StackBounds::initialize()
134 {
135     void* stackBase = 0;
136     size_t stackSize = 0;
137
138     pthread_t thread = pthread_self();
139     pthread_attr_t sattr;
140     pthread_attr_init(&sattr);
141 #if HAVE(PTHREAD_NP_H) || OS(NETBSD)
142     // e.g. on FreeBSD 5.4, neundorf@kde.org
143     pthread_attr_get_np(thread, &sattr);
144 #else
145     // FIXME: this function is non-portable; other POSIX systems may have different np alternatives
146     pthread_getattr_np(thread, &sattr);
147 #endif
148     int rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
149     (void)rc; // FIXME: Deal with error code somehow? Seems fatal.
150     ASSERT(stackBase);
151     pthread_attr_destroy(&sattr);
152     m_bound = stackBase;
153     m_origin = static_cast<char*>(stackBase) + stackSize;
154 }
155
156 #elif OS(WINCE)
157
158 static bool detectGrowingDownward(void* previousFrame)
159 {
160     // Find the address of this stack frame by taking the address of a local variable.
161     int thisFrame;
162     return previousFrame > &thisFrame;
163 }
164
165 static inline bool isPageWritable(void* page)
166 {
167     MEMORY_BASIC_INFORMATION memoryInformation;
168     DWORD result = VirtualQuery(page, &memoryInformation, sizeof(memoryInformation));
169
170     // return false on error, including ptr outside memory
171     if (result != sizeof(memoryInformation))
172         return false;
173
174     DWORD protect = memoryInformation.Protect & ~(PAGE_GUARD | PAGE_NOCACHE);
175     return protect == PAGE_READWRITE
176         || protect == PAGE_WRITECOPY
177         || protect == PAGE_EXECUTE_READWRITE
178         || protect == PAGE_EXECUTE_WRITECOPY;
179 }
180
181 static inline void* getLowerStackBound(char* currentPage, DWORD pageSize)
182 {
183     while (currentPage > 0) {
184         // check for underflow
185         if (currentPage >= reinterpret_cast<char*>(pageSize))
186             currentPage -= pageSize;
187         else
188             currentPage = 0;
189
190         if (!isPageWritable(currentPage))
191             return currentPage + pageSize;
192     }
193
194     return 0;
195 }
196
197 static inline void* getUpperStackBound(char* currentPage, DWORD pageSize)
198 {
199     do {
200         // guaranteed to complete because isPageWritable returns false at end of memory
201         currentPage += pageSize;
202     } while (isPageWritable(currentPage));
203
204     return currentPage - pageSize;
205 }
206
207 void StackBounds::initialize()
208 {
209     // find the address of this stack frame by taking the address of a local variable
210     void* thisFrame = &thisFrame;
211     bool isGrowingDownward = detectGrowingDownward(thisFrame);
212
213     SYSTEM_INFO systemInfo;
214     GetSystemInfo(&systemInfo);
215     DWORD pageSize = systemInfo.dwPageSize;
216
217     // scan all of memory starting from this frame, and return the last writeable page found
218     char* currentPage = reinterpret_cast<char*>(reinterpret_cast<DWORD>(thisFrame) & ~(pageSize - 1));
219     void* lowerStackBound = getLowerStackBound(currentPage, pageSize);
220     void* upperStackBound = getUpperStackBound(currentPage, pageSize);
221
222     m_origin = isGrowingDownward ? upperStackBound : lowerStackBound;
223     m_bound = isGrowingDownward ? lowerStackBound : upperStackBound;
224 }
225
226 #elif OS(WINDOWS)
227
228 void StackBounds::initialize()
229 {
230 #if CPU(X86) && COMPILER(MSVC)
231     // offset 0x18 from the FS segment register gives a pointer to
232     // the thread information block for the current thread
233     NT_TIB* pTib;
234     __asm {
235         MOV EAX, FS:[18h]
236         MOV pTib, EAX
237     }
238     m_origin = static_cast<void*>(pTib->StackBase);
239 #elif CPU(X86) && COMPILER(GCC)
240     // offset 0x18 from the FS segment register gives a pointer to
241     // the thread information block for the current thread
242     NT_TIB* pTib;
243     asm ( "movl %%fs:0x18, %0\n"
244           : "=r" (pTib)
245         );
246     m_origin = static_cast<void*>(pTib->StackBase);
247 #elif CPU(X86_64)
248     PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
249     m_origin = reinterpret_cast<void*>(pTib->StackBase);
250 #else
251 #error Need a way to get the stack bounds on this platform (Windows)
252 #endif
253     // Looks like we should be able to get pTib->StackLimit
254     m_bound = estimateStackBound(m_origin);
255 }
256
257 #else
258 #error Need a way to get the stack bounds on this platform
259 #endif
260
261 } // namespace WTF