#include "config.h"
#include <wtf/StackBounds.h>
#if OS(DARWIN)
#include <pthread.h>
#elif OS(WINDOWS)
#include <windows.h>
#elif OS(UNIX)
#include <pthread.h>
#if HAVE(PTHREAD_NP_H)
#include <pthread_np.h>
#endif
#endif
namespace WTF {
#if OS(DARWIN)
StackBounds StackBounds::newThreadStackBounds(PlatformThreadHandle thread)
{
void* origin = pthread_get_stackaddr_np(thread);
rlim_t size = pthread_get_stacksize_np(thread);
void* bound = static_cast<char*>(origin) - size;
return StackBounds { origin, bound };
}
StackBounds StackBounds::currentThreadStackBoundsInternal()
{
if (pthread_main_np()) {
void* origin = pthread_get_stackaddr_np(pthread_self());
rlimit limit;
getrlimit(RLIMIT_STACK, &limit);
rlim_t size = limit.rlim_cur;
void* bound = static_cast<char*>(origin) - size;
return StackBounds { origin, bound };
}
return newThreadStackBounds(pthread_self());
}
#elif OS(UNIX)
#if OS(OPENBSD)
StackBounds StackBounds::newThreadStackBounds(PlatformThreadHandle thread)
{
stack_t stack;
pthread_stackseg_np(thread, &stack);
void* origin = stack.ss_sp;
void* bound = static_cast<char*>(origin) - stack.ss_size;
return StackBounds { origin, bound };
}
#else // !OS(OPENBSD)
StackBounds StackBounds::newThreadStackBounds(PlatformThreadHandle thread)
{
void* bound = nullptr;
size_t stackSize = 0;
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, &bound, &stackSize);
UNUSED_PARAM(rc);
ASSERT(bound);
pthread_attr_destroy(&sattr);
void* origin = static_cast<char*>(bound) + stackSize;
return StackBounds { origin, bound };
}
#endif // OS(OPENBSD)
StackBounds StackBounds::currentThreadStackBoundsInternal()
{
return newThreadStackBounds(pthread_self());
}
#elif OS(WINDOWS)
StackBounds StackBounds::currentThreadStackBoundsInternal()
{
MEMORY_BASIC_INFORMATION stackOrigin { };
VirtualQuery(&stackOrigin, &stackOrigin, sizeof(stackOrigin));
void* origin = static_cast<char*>(stackOrigin.BaseAddress) + stackOrigin.RegionSize;
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*>(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
void* bound = static_cast<char*>(endOfStack) + guardPage.RegionSize;
return StackBounds { origin, bound };
}
#else
#error Need a way to get the stack bounds on this platform
#endif
}