2 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
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.
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.
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
22 #include "StackBounds.h"
26 #include <mach/task.h>
27 #include <mach/thread_act.h>
45 #include <sys/procfs.h>
50 #if HAVE(PTHREAD_NP_H)
51 #include <pthread_np.h>
58 // Bug 26276 - Need a mechanism to determine stack extent
60 // These platforms should now be working correctly:
62 // These platforms are not:
63 // WINDOWS, SOLARIS, OPENBSD, WINCE
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)
72 return static_cast<char*>(origin) - estimatedStackSize;
78 void StackBounds::initialize()
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);
87 void StackBounds::initialize()
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);
97 LOG_ERROR("Unable to open /proc/self (errno: %d)", errno);
100 devctl(fd, DCMD_PROC_TIDSTATUS, &threadInfo, sizeof(threadInfo), 0);
102 stackBase = reinterpret_cast<void*>(threadInfo.stkbase);
103 stackSize = threadInfo.stksize;
106 m_bound = static_cast<char*>(stackBase) + 0x1000; // 4kb guard page
107 m_origin = static_cast<char*>(stackBase) + stackSize;
112 void StackBounds::initialize()
117 m_bound = estimateStackBound(m_origin);
122 void StackBounds::initialize()
124 pthread_t thread = pthread_self();
126 pthread_stackseg_np(thread, &stack);
127 m_origin = stack.ss_sp;
128 m_bound = estimateStackBound(m_origin);
133 void StackBounds::initialize()
136 size_t stackSize = 0;
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);
145 // FIXME: this function is non-portable; other POSIX systems may have different np alternatives
146 pthread_getattr_np(thread, &sattr);
148 int rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
149 (void)rc; // FIXME: Deal with error code somehow? Seems fatal.
151 pthread_attr_destroy(&sattr);
153 m_origin = static_cast<char*>(stackBase) + stackSize;
158 static bool detectGrowingDownward(void* previousFrame)
160 // Find the address of this stack frame by taking the address of a local variable.
162 return previousFrame > &thisFrame;
165 static inline bool isPageWritable(void* page)
167 MEMORY_BASIC_INFORMATION memoryInformation;
168 DWORD result = VirtualQuery(page, &memoryInformation, sizeof(memoryInformation));
170 // return false on error, including ptr outside memory
171 if (result != sizeof(memoryInformation))
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;
181 static inline void* getLowerStackBound(char* currentPage, DWORD pageSize)
183 while (currentPage > 0) {
184 // check for underflow
185 if (currentPage >= reinterpret_cast<char*>(pageSize))
186 currentPage -= pageSize;
190 if (!isPageWritable(currentPage))
191 return currentPage + pageSize;
197 static inline void* getUpperStackBound(char* currentPage, DWORD pageSize)
200 // guaranteed to complete because isPageWritable returns false at end of memory
201 currentPage += pageSize;
202 } while (isPageWritable(currentPage));
204 return currentPage - pageSize;
207 void StackBounds::initialize()
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);
213 SYSTEM_INFO systemInfo;
214 GetSystemInfo(&systemInfo);
215 DWORD pageSize = systemInfo.dwPageSize;
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);
222 m_origin = isGrowingDownward ? upperStackBound : lowerStackBound;
223 m_bound = isGrowingDownward ? lowerStackBound : upperStackBound;
228 void StackBounds::initialize()
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
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
243 asm ( "movl %%fs:0x18, %0\n"
246 m_origin = static_cast<void*>(pTib->StackBase);
248 PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
249 m_origin = reinterpret_cast<void*>(pTib->StackBase);
251 #error Need a way to get the stack bounds on this platform (Windows)
253 // Looks like we should be able to get pTib->StackLimit
254 m_bound = estimateStackBound(m_origin);
258 #error Need a way to get the stack bounds on this platform