Modified: trunk/Source/WTF/ChangeLog (150620 => 150621)
--- trunk/Source/WTF/ChangeLog 2013-05-24 00:43:10 UTC (rev 150620)
+++ trunk/Source/WTF/ChangeLog 2013-05-24 01:33:04 UTC (rev 150621)
@@ -1,3 +1,9 @@
+2013-05-23 Brent Fulgham <[email protected]>
+
+ [Windows] Rolling back r150600 as it breaks the VS2010 builds.
+
+ * wtf/StackBounds.cpp: Rollback.
+
2013-05-23 Patrick Gansterer <[email protected]>
Use correct stack size on Solaris and OpenBSD
Modified: trunk/Source/WTF/wtf/StackBounds.cpp (150620 => 150621)
--- trunk/Source/WTF/wtf/StackBounds.cpp 2013-05-24 00:43:10 UTC (rev 150620)
+++ trunk/Source/WTF/wtf/StackBounds.cpp 2013-05-24 01:33:04 UTC (rev 150621)
@@ -55,6 +55,24 @@
namespace WTF {
+// Bug 26276 - Need a mechanism to determine stack extent
+//
+// These platforms should now be working correctly:
+// DARWIN, QNX, UNIX
+// These platforms are not:
+// WINDOWS, SOLARIS, OPENBSD, WINCE
+//
+// FIXME: remove this! - this code unsafely guesses at stack sizes!
+#if OS(WINDOWS) || OS(SOLARIS) || OS(OPENBSD)
+// Based on the current limit used by the JSC parser, guess the stack size.
+static const ptrdiff_t estimatedStackSize = 128 * sizeof(void*) * 1024;
+// This method assumes the stack is growing downwards.
+static void* estimateStackBound(void* origin)
+{
+ return static_cast<char*>(origin) - estimatedStackSize;
+}
+#endif
+
#if OS(DARWIN)
void StackBounds::initialize()
@@ -106,7 +124,7 @@
stack_t s;
thr_stksegment(&s);
m_origin = s.ss_sp;
- m_bound = static_cast<char*>(m_origin) - s.ss_size;
+ m_bound = estimateStackBound(m_origin);
}
#elif OS(OPENBSD)
@@ -117,11 +135,7 @@
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
+ m_bound = estimateStackBound(m_origin);
}
#elif OS(UNIX)
@@ -149,46 +163,105 @@
m_origin = static_cast<char*>(stackBase) + stackSize;
}
-#elif OS(WINDOWS)
+#elif OS(WINCE)
+static bool detectGrowingDownward(void* previousFrame)
+{
+ // Find the address of this stack frame by taking the address of a local variable.
+ int thisFrame;
+ return previousFrame > &thisFrame;
+}
+
+static inline bool isPageWritable(void* page)
+{
+ MEMORY_BASIC_INFORMATION memoryInformation;
+ DWORD result = VirtualQuery(page, &memoryInformation, sizeof(memoryInformation));
+
+ // return false on error, including ptr outside memory
+ if (result != sizeof(memoryInformation))
+ return false;
+
+ DWORD protect = memoryInformation.Protect & ~(PAGE_GUARD | PAGE_NOCACHE);
+ return protect == PAGE_READWRITE
+ || protect == PAGE_WRITECOPY
+ || protect == PAGE_EXECUTE_READWRITE
+ || protect == PAGE_EXECUTE_WRITECOPY;
+}
+
+static inline void* getLowerStackBound(char* currentPage, DWORD pageSize)
+{
+ while (currentPage > 0) {
+ // check for underflow
+ if (currentPage >= reinterpret_cast<char*>(pageSize))
+ currentPage -= pageSize;
+ else
+ currentPage = 0;
+
+ if (!isPageWritable(currentPage))
+ return currentPage + pageSize;
+ }
+
+ return 0;
+}
+
+static inline void* getUpperStackBound(char* currentPage, DWORD pageSize)
+{
+ do {
+ // guaranteed to complete because isPageWritable returns false at end of memory
+ currentPage += pageSize;
+ } while (isPageWritable(currentPage));
+
+ return currentPage - pageSize;
+}
+
void StackBounds::initialize()
{
+ // find the address of this stack frame by taking the address of a local variable
+ void* thisFrame = &thisFrame;
+ bool isGrowingDownward = detectGrowingDownward(thisFrame);
+
SYSTEM_INFO systemInfo;
GetSystemInfo(&systemInfo);
DWORD pageSize = systemInfo.dwPageSize;
- MEMORY_BASIC_INFORMATION stackOrigin;
- VirtualQuery(&stackOrigin, &stackOrigin, sizeof(stackOrigin));
- // stackOrigin.AllocationBase points to the reserved stack memory base address.
+ // scan all of memory starting from this frame, and return the last writeable page found
+ char* currentPage = reinterpret_cast<char*>(reinterpret_cast<DWORD>(thisFrame) & ~(pageSize - 1));
+ void* lowerStackBound = getLowerStackBound(currentPage, pageSize);
+ void* upperStackBound = getUpperStackBound(currentPage, pageSize);
- m_origin = static_cast<char*>(stackOrigin.BaseAddress) + stackOrigin.RegionSize;
-#if OS(WINCE)
- MEMORY_BASIC_INFORMATION stackMemory;
- VirtualQuery(m_origin, &stackMemory, sizeof(stackMemory));
+ m_origin = isGrowingDownward ? upperStackBound : lowerStackBound;
+ m_bound = isGrowingDownward ? lowerStackBound : upperStackBound;
+}
- m_bound = static_cast<char*>(m_origin) - stackMemory.RegionSize + pageSize;
+#elif OS(WINDOWS)
+
+void StackBounds::initialize()
+{
+#if CPU(X86) && COMPILER(MSVC)
+ // offset 0x18 from the FS segment register gives a pointer to
+ // the thread information block for the current thread
+ NT_TIB* pTib;
+ __asm {
+ MOV EAX, FS:[18h]
+ MOV pTib, EAX
+ }
+ m_origin = static_cast<void*>(pTib->StackBase);
+#elif CPU(X86) && COMPILER(GCC)
+ // offset 0x18 from the FS segment register gives a pointer to
+ // the thread information block for the current thread
+ NT_TIB* pTib;
+ asm ( "movl %%fs:0x18, %0\n"
+ : "=r" (pTib)
+ );
+ m_origin = static_cast<void*>(pTib->StackBase);
+#elif CPU(X86_64)
+ PNT_TIB64 pTib = reinterpret_cast<PNT_TIB64>(NtCurrentTeb());
+ m_origin = reinterpret_cast<void*>(pTib->StackBase);
#else
- // The stack on Windows consists out of three parts (reserved memory, a guard page and initially committed memory),
- // which need to me queried seperately to get the full size of the stack.
- // See http://msdn.microsoft.com/en-us/library/ms686774%28VS.85%29.aspx for more information.
-
- MEMORY_BASIC_INFORMATION reservedMemory;
- VirtualQuery(stackOrigin.AllocationBase, &reservedMemory, sizeof(reservedMemory));
- ASSERT(reservedMemory.State == MEM_RESERVE);
- // reservedMemory.BaseAddress and reservedMemory.RegionSize describe reserved (uncommitted) portion of the stack.
-
- MEMORY_BASIC_INFORMATION guardPage;
- VirtualQuery(static_cast<char*>(reservedMemory.BaseAddress) + reservedMemory.RegionSize, &guardPage, sizeof(guardPage));
- ASSERT(guardPage.Protect & PAGE_GUARD);
- // guardPage.BaseAddress and guardPage.RegionSize describe the guard page.
-
- MEMORY_BASIC_INFORMATION committedMemory;
- VirtualQuery(static_cast<char*>(guardPage.BaseAddress) + guardPage.RegionSize, &committedMemory, sizeof(committedMemory));
- ASSERT(committedMemory.State == MEM_COMMIT);
- // committedMemory.BaseAddress, committedMemory.RegionSize describe the committed (i.e. accessed) portion of the stack.
-
- m_bound = static_cast<char*>(m_origin) - (reservedMemory.RegionSize - guardPage.RegionSize + committedMemory.RegionSize) + pageSize;
+#error Need a way to get the stack bounds on this platform (Windows)
#endif
+ // Looks like we should be able to get pTib->StackLimit
+ m_bound = estimateStackBound(m_origin);
}
#else