#include "config.h"
#include "StackBounds.h"
#if OS(DARWIN)
#include <mach/task.h>
#include <mach/thread_act.h>
#include <pthread.h>
#elif OS(WINDOWS)
#include <windows.h>
#elif OS(SOLARIS)
#include <thread.h>
#elif OS(UNIX)
#include <pthread.h>
#if HAVE(PTHREAD_NP_H)
#include <pthread_np.h>
#endif
#endif
namespace WTF {
#if OS(DARWIN)
void StackBounds::initialize()
{
pthread_t thread = pthread_self();
m_origin = pthread_get_stackaddr_np(thread);
rlim_t size = 0;
if (pthread_main_np()) {
rlimit limit;
getrlimit(RLIMIT_STACK, &limit);
size = limit.rlim_cur;
} else
size = pthread_get_stacksize_np(thread);
m_bound = static_cast<char*>(m_origin) - size;
}
#elif OS(SOLARIS)
void StackBounds::initialize()
{
stack_t s;
thr_stksegment(&s);
m_origin = s.ss_sp;
m_bound = static_cast<char*>(m_origin) - s.ss_size;
}
#elif OS(OPENBSD)
void StackBounds::initialize()
{
pthread_t thread = pthread_self();
stack_t stack;
pthread_stackseg_np(thread, &stack);
m_origin = stack.ss_sp;
#if CPU(HPPA)
m_bound = static_cast<char*>(m_origin) + stack.ss_size;
#else
m_bound = static_cast<char*>(m_origin) - stack.ss_size;
#endif
}
#elif OS(UNIX)
void StackBounds::initialize()
{
void* stackBase = 0;
size_t stackSize = 0;
pthread_t thread = pthread_self();
pthread_attr_t sattr;
pthread_attr_init(&sattr);
#if HAVE(PTHREAD_NP_H) || OS(NETBSD)
pthread_attr_get_np(thread, &sattr);
#else
pthread_getattr_np(thread, &sattr);
#endif
int rc = pthread_attr_getstack(&sattr, &stackBase, &stackSize);
(void)rc; ASSERT(stackBase);
pthread_attr_destroy(&sattr);
m_bound = stackBase;
m_origin = static_cast<char*>(stackBase) + stackSize;
}
#elif OS(WINDOWS)
void StackBounds::initialize()
{
MEMORY_BASIC_INFORMATION stackOrigin = { 0 };
VirtualQuery(&stackOrigin, &stackOrigin, sizeof(stackOrigin));
m_origin = static_cast<char*>(stackOrigin.BaseAddress) + stackOrigin.RegionSize;
#if OS(WINCE)
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
DWORD pageSize = systemInfo.dwPageSize;
MEMORY_BASIC_INFORMATION stackMemory;
VirtualQuery(m_origin, &stackMemory, sizeof(stackMemory));
m_bound = static_cast<char*>(m_origin) - stackMemory.RegionSize + pageSize;
#else
MEMORY_BASIC_INFORMATION uncommittedMemory;
VirtualQuery(stackOrigin.AllocationBase, &uncommittedMemory, sizeof(uncommittedMemory));
ASSERT(uncommittedMemory.State == MEM_RESERVE);
MEMORY_BASIC_INFORMATION guardPage;
VirtualQuery(static_cast<char*>(uncommittedMemory.BaseAddress) + uncommittedMemory.RegionSize, &guardPage, sizeof(guardPage));
ASSERT(guardPage.Protect & PAGE_GUARD);
void* endOfStack = stackOrigin.AllocationBase;
#ifndef NDEBUG
MEMORY_BASIC_INFORMATION committedMemory;
VirtualQuery(static_cast<char*>(guardPage.BaseAddress) + guardPage.RegionSize, &committedMemory, sizeof(committedMemory));
ASSERT(committedMemory.State == MEM_COMMIT);
void* computedEnd = static_cast<char*>(m_origin) - (uncommittedMemory.RegionSize + guardPage.RegionSize + committedMemory.RegionSize);
ASSERT(stackOrigin.AllocationBase == uncommittedMemory.AllocationBase);
ASSERT(stackOrigin.AllocationBase == guardPage.AllocationBase);
ASSERT(stackOrigin.AllocationBase == committedMemory.AllocationBase);
ASSERT(stackOrigin.AllocationBase == uncommittedMemory.BaseAddress);
ASSERT(endOfStack == computedEnd);
#endif // NDEBUG
m_bound = static_cast<char*>(endOfStack) + guardPage.RegionSize;
#endif // OS(WINCE)
}
#else
#error Need a way to get the stack bounds on this platform
#endif
}