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>
41 #if HAVE(PTHREAD_NP_H)
42 #include <pthread_np.h>
51 void StackBounds::initialize()
53 pthread_t thread = pthread_self();
54 m_origin = pthread_get_stackaddr_np(thread);
56 if (pthread_main_np()) {
57 // FIXME: <rdar://problem/13741204>
58 // pthread_get_size lies to us when we're the main thread, use get_rlimit instead
60 getrlimit(RLIMIT_STACK, &limit);
61 size = limit.rlim_cur;
63 size = pthread_get_stacksize_np(thread);
65 m_bound = static_cast<char*>(m_origin) - size;
70 void StackBounds::initialize()
75 m_bound = static_cast<char*>(m_origin) - s.ss_size;
80 void StackBounds::initialize()
82 pthread_t thread = pthread_self();
84 pthread_stackseg_np(thread, &stack);
85 m_origin = stack.ss_sp;
87 m_bound = static_cast<char*>(m_origin) + stack.ss_size;
89 m_bound = static_cast<char*>(m_origin) - stack.ss_size;
95 void StackBounds::initialize()
100 pthread_t thread = pthread_self();
101 pthread_attr_t sattr;
102 pthread_attr_init(&sattr);
103 #if HAVE(PTHREAD_NP_H) || OS(NETBSD)
104 // e.g. on FreeBSD 5.4, neundorf@kde.org
105 pthread_attr_get_np(thread, &sattr);
107 // FIXME: this function is non-portable; other POSIX systems may have different np alternatives
108 pthread_getattr_np(thread, &sattr);
110 int rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
111 (void)rc; // FIXME: Deal with error code somehow? Seems fatal.
113 pthread_attr_destroy(&sattr);
115 m_origin = static_cast<char*>(stackBase) + stackSize;
120 void StackBounds::initialize()
122 MEMORY_BASIC_INFORMATION stackOrigin = { 0 };
123 VirtualQuery(&stackOrigin, &stackOrigin, sizeof(stackOrigin));
124 // stackOrigin.AllocationBase points to the reserved stack memory base address.
126 m_origin = static_cast<char*>(stackOrigin.BaseAddress) + stackOrigin.RegionSize;
128 SYSTEM_INFO systemInfo;
129 GetSystemInfo(&systemInfo);
130 DWORD pageSize = systemInfo.dwPageSize;
132 MEMORY_BASIC_INFORMATION stackMemory;
133 VirtualQuery(m_origin, &stackMemory, sizeof(stackMemory));
135 m_bound = static_cast<char*>(m_origin) - stackMemory.RegionSize + pageSize;
137 // The stack on Windows consists out of three parts (uncommitted memory, a guard page and present
138 // committed memory). The 3 regions have different BaseAddresses but all have the same AllocationBase
139 // since they are all from the same VirtualAlloc. The 3 regions are laid out in memory (from high to
142 // High |-------------------| -----
143 // | committedMemory | ^
144 // |-------------------| |
145 // | guardPage | reserved memory for the stack
146 // |-------------------| |
147 // | uncommittedMemory | v
148 // Low |-------------------| ----- <--- stackOrigin.AllocationBase
150 // See http://msdn.microsoft.com/en-us/library/ms686774%28VS.85%29.aspx for more information.
152 MEMORY_BASIC_INFORMATION uncommittedMemory;
153 VirtualQuery(stackOrigin.AllocationBase, &uncommittedMemory, sizeof(uncommittedMemory));
154 ASSERT(uncommittedMemory.State == MEM_RESERVE);
156 MEMORY_BASIC_INFORMATION guardPage;
157 VirtualQuery(static_cast<char*>(uncommittedMemory.BaseAddress) + uncommittedMemory.RegionSize, &guardPage, sizeof(guardPage));
158 ASSERT(guardPage.Protect & PAGE_GUARD);
160 void* endOfStack = stackOrigin.AllocationBase;
163 MEMORY_BASIC_INFORMATION committedMemory;
164 VirtualQuery(static_cast<char*>(guardPage.BaseAddress) + guardPage.RegionSize, &committedMemory, sizeof(committedMemory));
165 ASSERT(committedMemory.State == MEM_COMMIT);
167 void* computedEnd = static_cast<char*>(m_origin) - (uncommittedMemory.RegionSize + guardPage.RegionSize + committedMemory.RegionSize);
169 ASSERT(stackOrigin.AllocationBase == uncommittedMemory.AllocationBase);
170 ASSERT(stackOrigin.AllocationBase == guardPage.AllocationBase);
171 ASSERT(stackOrigin.AllocationBase == committedMemory.AllocationBase);
172 ASSERT(stackOrigin.AllocationBase == uncommittedMemory.BaseAddress);
173 ASSERT(endOfStack == computedEnd);
175 m_bound = static_cast<char*>(endOfStack) + guardPage.RegionSize;
180 #error Need a way to get the stack bounds on this platform