Title: [225015] trunk/Source/WTF
Revision
225015
Author
[email protected]
Date
2017-11-18 06:09:50 -0800 (Sat, 18 Nov 2017)

Log Message

[WTF] Remove CPU(HPPA) in StackBounds by using runtime stack direction test
https://bugs.webkit.org/show_bug.cgi?id=179859

Reviewed by JF Bastien.

Currently, we know that CPU(HPPA)'s stack direction is upward! But listing
CPU architectures here is not a scalable way.

Instead, we use runtime stack direction test. By doing so, we can handle
such a strange architecture without listing the CPU to Platform.h. This paves
the way to dropping many CPUs in Platform.h by replacing them with CPU(UNKNOWN)[1].

We also fix StackBounds::isGrowingDownward().

[1]: https://bugs.webkit.org/show_bug.cgi?id=179243

* wtf/StackBounds.cpp:
(WTF::StackBounds::stackDirection):
(WTF::testStackDirection2):
(WTF::testStackDirection):
(WTF::StackBounds::newThreadStackBounds):
(WTF::StackBounds::currentThreadStackBoundsInternal):
* wtf/StackBounds.h:
(WTF::StackBounds::isGrowingDownward const):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (225014 => 225015)


--- trunk/Source/WTF/ChangeLog	2017-11-18 11:46:06 UTC (rev 225014)
+++ trunk/Source/WTF/ChangeLog	2017-11-18 14:09:50 UTC (rev 225015)
@@ -1,3 +1,30 @@
+2017-11-18  Yusuke Suzuki  <[email protected]>
+
+        [WTF] Remove CPU(HPPA) in StackBounds by using runtime stack direction test
+        https://bugs.webkit.org/show_bug.cgi?id=179859
+
+        Reviewed by JF Bastien.
+
+        Currently, we know that CPU(HPPA)'s stack direction is upward! But listing
+        CPU architectures here is not a scalable way.
+
+        Instead, we use runtime stack direction test. By doing so, we can handle
+        such a strange architecture without listing the CPU to Platform.h. This paves
+        the way to dropping many CPUs in Platform.h by replacing them with CPU(UNKNOWN)[1].
+
+        We also fix StackBounds::isGrowingDownward().
+
+        [1]: https://bugs.webkit.org/show_bug.cgi?id=179243
+
+        * wtf/StackBounds.cpp:
+        (WTF::StackBounds::stackDirection):
+        (WTF::testStackDirection2):
+        (WTF::testStackDirection):
+        (WTF::StackBounds::newThreadStackBounds):
+        (WTF::StackBounds::currentThreadStackBoundsInternal):
+        * wtf/StackBounds.h:
+        (WTF::StackBounds::isGrowingDownward const):
+
 2017-11-17  Chris Dumez  <[email protected]>
 
         Use a strongly typed identifier for SWServer::Connection

Modified: trunk/Source/WTF/wtf/StackBounds.cpp (225014 => 225015)


--- trunk/Source/WTF/wtf/StackBounds.cpp	2017-11-18 11:46:06 UTC (rev 225014)
+++ trunk/Source/WTF/wtf/StackBounds.cpp	2017-11-18 14:09:50 UTC (rev 225015)
@@ -20,6 +20,8 @@
 
 #include "config.h"
 #include "StackBounds.h"
+#include <mutex>
+#include <wtf/NoTailCalls.h>
 
 #if OS(DARWIN)
 
@@ -42,10 +44,42 @@
 
 namespace WTF {
 
+#if CPU(X86) || CPU(X86_64) || CPU(ARM) || CPU(ARM64) || CPU(MIPS)
+ALWAYS_INLINE StackBounds::StackDirection StackBounds::stackDirection()
+{
+    return StackDirection::Downward;
+}
+#else
+static NEVER_INLINE NOT_TAIL_CALLED StackBounds::StackDirection testStackDirection2(volatile const int* pointer)
+{
+    volatile int stackValue = 42;
+    return (pointer < &stackValue) ? StackBounds::StackDirection::Upward : StackBounds::StackDirection::Downward;
+}
+
+static NEVER_INLINE NOT_TAIL_CALLED StackBounds::StackDirection testStackDirection()
+{
+    NO_TAIL_CALLS();
+    volatile int stackValue = 42;
+    return testStackDirection2(&stackValue);
+}
+
+NEVER_INLINE StackBounds::StackDirection StackBounds::stackDirection()
+{
+    static StackBounds::StackDirection result = StackBounds::StackDirection::Downward;
+    static std::once_flag onceKey;
+    std::call_once(onceKey, [] {
+        NO_TAIL_CALLS();
+        result = testStackDirection();
+    });
+    return result;
+}
+#endif
+
 #if OS(DARWIN)
 
 StackBounds StackBounds::newThreadStackBounds(PlatformThreadHandle thread)
 {
+    ASSERT(stackDirection() == StackDirection::Downward);
     void* origin = pthread_get_stackaddr_np(thread);
     rlim_t size = pthread_get_stacksize_np(thread);
     void* bound = static_cast<char*>(origin) - size;
@@ -54,6 +88,7 @@
 
 StackBounds StackBounds::currentThreadStackBoundsInternal()
 {
+    ASSERT(stackDirection() == StackDirection::Downward);
     if (pthread_main_np()) {
         // FIXME: <rdar://problem/13741204>
         // pthread_get_size lies to us when we're the main thread, use get_rlimit instead
@@ -76,11 +111,11 @@
     stack_t stack;
     pthread_stackseg_np(thread, &stack);
     void* origin = stack.ss_sp;
-#if CPU(HPPA)
-    void* bound = static_cast<char*>(origin) + stack.ss_size;
-#else
-    void* bound = static_cast<char*>(origin) - stack.ss_size;
-#endif
+    void* bound = nullptr;
+    if (stackDirection() == StackDirection::Upward)
+        bound = static_cast<char*>(origin) + stack.ss_size;
+    else
+        bound = static_cast<char*>(origin) - stack.ss_size;
     return StackBounds { origin, bound };
 }
 
@@ -105,6 +140,10 @@
     ASSERT(bound);
     pthread_attr_destroy(&sattr);
     void* origin = static_cast<char*>(bound) + stackSize;
+    // pthread_attr_getstack's bound is the lowest accessible pointer of the stack.
+    // If stack grows up, origin and bound in this code should be swapped.
+    if (stackDirection() == StackDirection::Upward)
+        std::swap(origin, bound);
     return StackBounds { origin, bound };
 }
 
@@ -119,6 +158,7 @@
 
 StackBounds StackBounds::currentThreadStackBoundsInternal()
 {
+    ASSERT(stackDirection() == StackDirection::Downward);
     MEMORY_BASIC_INFORMATION stackOrigin = { 0 };
     VirtualQuery(&stackOrigin, &stackOrigin, sizeof(stackOrigin));
     // stackOrigin.AllocationBase points to the reserved stack memory base address.

Modified: trunk/Source/WTF/wtf/StackBounds.h (225014 => 225015)


--- trunk/Source/WTF/wtf/StackBounds.h	2017-11-18 11:46:06 UTC (rev 225014)
+++ trunk/Source/WTF/wtf/StackBounds.h	2017-11-18 14:09:50 UTC (rev 225015)
@@ -41,6 +41,8 @@
     const static size_t s_defaultAvailabilityDelta = 64 * 1024;
 
 public:
+    enum class StackDirection { Upward, Downward };
+
     static constexpr StackBounds emptyBounds() { return StackBounds(); }
 
 #if HAVE(STACK_BOUNDS_FOR_NEW_THREAD)
@@ -121,7 +123,7 @@
     bool isGrowingDownward() const
     {
         ASSERT(m_origin && m_bound);
-        return true;
+        return m_bound <= m_origin;
     }
 
 private:
@@ -137,6 +139,8 @@
     {
     }
 
+    static StackDirection stackDirection();
+
     WTF_EXPORT_PRIVATE static StackBounds currentThreadStackBoundsInternal();
 
     void checkConsistency() const
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to