Title: [291255] trunk/Source/WTF
Revision
291255
Author
[email protected]
Date
2022-03-14 15:39:57 -0700 (Mon, 14 Mar 2022)

Log Message

Enhance StackCheck debugging support and bump up the ASAN reserved zone size.
https://bugs.webkit.org/show_bug.cgi?id=237805

Reviewed by Robin Morisset.

1. Apply the ASAN multiplier to StackBound::DefaultReservedZone instead of just
   in the StackCheck class.  This ensures that all stack checks using this
   default value gets the necessary bump for ASAN as well.

   Also bump the ASAN multipler from 2 to 3.  We have evidence that an ASAN Debug
   build inflates stack usage more than 2x over a non-ASAN Debug build.

2. Enhance the StackCheck class to dump some debugging information when
   VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE is enabled.

* WTF.xcodeproj/project.pbxproj:
* wtf/StackBounds.h:
* wtf/StackCheck.cpp: Added.
(WTF::StackCheck::Scope::reportVerificationFailureAndCrash):
* wtf/StackCheck.h:
(WTF::StackCheck::Scope::Scope):
(WTF::StackCheck::Scope::~Scope):
(WTF::StackCheck::StackCheck):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (291254 => 291255)


--- trunk/Source/WTF/ChangeLog	2022-03-14 22:13:08 UTC (rev 291254)
+++ trunk/Source/WTF/ChangeLog	2022-03-14 22:39:57 UTC (rev 291255)
@@ -1,3 +1,29 @@
+2022-03-14  Mark Lam  <[email protected]>
+
+        Enhance StackCheck debugging support and bump up the ASAN reserved zone size.
+        https://bugs.webkit.org/show_bug.cgi?id=237805
+
+        Reviewed by Robin Morisset.
+
+        1. Apply the ASAN multiplier to StackBound::DefaultReservedZone instead of just
+           in the StackCheck class.  This ensures that all stack checks using this
+           default value gets the necessary bump for ASAN as well.
+
+           Also bump the ASAN multipler from 2 to 3.  We have evidence that an ASAN Debug
+           build inflates stack usage more than 2x over a non-ASAN Debug build.
+
+        2. Enhance the StackCheck class to dump some debugging information when
+           VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE is enabled.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/StackBounds.h:
+        * wtf/StackCheck.cpp: Added.
+        (WTF::StackCheck::Scope::reportVerificationFailureAndCrash):
+        * wtf/StackCheck.h:
+        (WTF::StackCheck::Scope::Scope):
+        (WTF::StackCheck::Scope::~Scope):
+        (WTF::StackCheck::StackCheck):
+
 2022-03-12  Tim Horton  <[email protected]>
 
         Fail the build if Internal or Experimental web preferences are missing human readable names

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (291254 => 291255)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2022-03-14 22:13:08 UTC (rev 291254)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2022-03-14 22:39:57 UTC (rev 291255)
@@ -3,7 +3,7 @@
 	archiveVersion = 1;
 	classes = {
 	};
-	objectVersion = 46;
+	objectVersion = 52;
 	objects = {
 
 /* Begin PBXBuildFile section */
@@ -805,6 +805,7 @@
 		FE05FAFF1FE5007500093230 /* WTFAssertions.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE05FAFE1FE5007500093230 /* WTFAssertions.cpp */; };
 		FE1E2C3B2240C06600F6B729 /* PtrTag.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1E2C392240C05400F6B729 /* PtrTag.cpp */; };
 		FE1E2C42224187C600F6B729 /* PlatformRegisters.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1E2C41224187C600F6B729 /* PlatformRegisters.cpp */; };
+		FE35D09227DC5ECC009DFA5B /* StackCheck.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE35D09127DC5ECC009DFA5B /* StackCheck.cpp */; };
 		FEDACD3D1630F83F00C69634 /* StackStats.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEDACD3B1630F83F00C69634 /* StackStats.cpp */; };
 		FEEA4DF9216D7BE400AC0602 /* StackPointer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEEA4DF8216D7BE400AC0602 /* StackPointer.cpp */; };
 /* End PBXBuildFile section */
@@ -1691,6 +1692,7 @@
 		FE1D6D87237401CD007A5C26 /* StackCheck.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StackCheck.h; sourceTree = "<group>"; };
 		FE1E2C392240C05400F6B729 /* PtrTag.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PtrTag.cpp; sourceTree = "<group>"; };
 		FE1E2C41224187C600F6B729 /* PlatformRegisters.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PlatformRegisters.cpp; sourceTree = "<group>"; };
+		FE35D09127DC5ECC009DFA5B /* StackCheck.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StackCheck.cpp; sourceTree = "<group>"; };
 		FE3842342325CC80009DD445 /* ResourceUsage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ResourceUsage.h; sourceTree = "<group>"; };
 		FE7497E4208FFCAA0003565B /* PtrTag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PtrTag.h; sourceTree = "<group>"; };
 		FE7497ED209163060003565B /* MetaAllocatorPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MetaAllocatorPtr.h; sourceTree = "<group>"; };
@@ -2211,6 +2213,7 @@
 				A8A4730D151A825B004123FF /* Spectrum.h */,
 				A8A4730E151A825B004123FF /* StackBounds.cpp */,
 				A8A4730F151A825B004123FF /* StackBounds.h */,
+				FE35D09127DC5ECC009DFA5B /* StackCheck.cpp */,
 				FE1D6D87237401CD007A5C26 /* StackCheck.h */,
 				FEEA4DF8216D7BE400AC0602 /* StackPointer.cpp */,
 				FEEA4DF7216D608C00AC0602 /* StackPointer.h */,
@@ -3549,6 +3552,7 @@
 				DCEE22011CEA7551000C2396 /* BlockObjCExceptions.mm in Sources */,
 				A8A473AC151A825B004123FF /* cached-powers.cc in Sources */,
 				5C1F05932164356B0039302C /* CFURLExtras.cpp in Sources */,
+				FE35D09227DC5ECC009DFA5B /* StackCheck.cpp in Sources */,
 				0F66B28A1DC97BAB004A1D3F /* ClockType.cpp in Sources */,
 				A8A47460151A825B004123FF /* CollatorDefault.cpp in Sources */,
 				A8A47463151A825B004123FF /* CollatorICU.cpp in Sources */,

Modified: trunk/Source/WTF/wtf/StackBounds.h (291254 => 291255)


--- trunk/Source/WTF/wtf/StackBounds.h	2022-03-14 22:13:08 UTC (rev 291254)
+++ trunk/Source/WTF/wtf/StackBounds.h	2022-03-14 22:39:57 UTC (rev 291255)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2019 Apple Inc. All Rights Reserved.
+ * Copyright (C) 2010-2022 Apple Inc. All Rights Reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -41,7 +41,15 @@
     // functions was seen to be as high as 27k. Hence, 64k is chosen as a
     // conservative availability value that is not too large but comfortably
     // exceeds 27k with some buffer for error.
+#if !ASAN_ENABLED
     static constexpr size_t DefaultReservedZone = 64 * 1024;
+#else
+    // ASAN inflates stack frames a lot. A factor of 3 was empirically found to
+    // be the ratio of inflation of stack usage between 2 consecutive stack
+    // recursion checkpoints. So, we'll also multiply the reserved zone size
+    // accordingly to accommodate this.
+    static constexpr size_t DefaultReservedZone = 64 * 1024 * 3;
+#endif
 
     static constexpr StackBounds emptyBounds() { return StackBounds(); }
 

Added: trunk/Source/WTF/wtf/StackCheck.cpp (0 => 291255)


--- trunk/Source/WTF/wtf/StackCheck.cpp	                        (rev 0)
+++ trunk/Source/WTF/wtf/StackCheck.cpp	2022-03-14 22:39:57 UTC (rev 291255)
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2022 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include <wtf/StackCheck.h>
+
+#if VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
+#include <wtf/DataLog.h>
+#endif
+
+namespace WTF {
+
+#if VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
+
+NO_RETURN_DUE_TO_CRASH void StackCheck::Scope::reportVerificationFailureAndCrash()
+{
+    uint8_t* currentStackCheckpoint = m_checker.m_lastStackCheckpoint;
+    uint8_t* previousStackCheckpoint = m_savedLastStackCheckpoint;
+    ptrdiff_t stackBetweenCheckpoints = previousStackCheckpoint - currentStackCheckpoint;
+
+    dataLogLn("Stack check failure:");
+    dataLogLn("    Previous checkpoint stack position: ", RawPointer(previousStackCheckpoint));
+    dataLogLn("    Current checkpoint stack position: ", RawPointer(currentStackCheckpoint));
+    dataLogLn("    Stack between checkpoints: ", stackBetweenCheckpoints);
+    dataLogLn("    ReservedZone space: ", m_checker.m_reservedZone);
+    dataLogLn();
+    if constexpr (verboseStackCheckVerification) {
+        dataLogLn("    Stack at previous checkpoint:");
+        m_savedLastCheckpointStackTrace->dump(WTF::dataFile(), "      ");
+        dataLogLn();
+        dataLogLn("    Stack at current checkpoint:");
+        m_checker.m_lastCheckpointStackTrace->dump(WTF::dataFile(), "      ");
+        dataLogLn();
+    } else {
+        dataLogLn("    To see the stack traces at the 2 checkpoints, set verboseStackCheckVerification to true in StackCheck.h, rebuild, and re-run your test.");
+        dataLogLn();
+    }
+
+    RELEASE_ASSERT(stackBetweenCheckpoints > 0);
+    RELEASE_ASSERT(previousStackCheckpoint - currentStackCheckpoint < static_cast<ptrdiff_t>(m_checker.m_reservedZone));
+    RELEASE_ASSERT_NOT_REACHED();
+}
+
+#endif // VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
+
+} // namespace WTF
+

Modified: trunk/Source/WTF/wtf/StackCheck.h (291254 => 291255)


--- trunk/Source/WTF/wtf/StackCheck.h	2022-03-14 22:13:08 UTC (rev 291254)
+++ trunk/Source/WTF/wtf/StackCheck.h	2022-03-14 22:39:57 UTC (rev 291255)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2019-2020 Apple Inc. All rights reserved.
+ * Copyright (C) 2019-2022 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -28,8 +28,6 @@
 #include <wtf/StackBounds.h>
 #include <wtf/Threading.h>
 
-namespace WTF {
-
 // We only enable the reserved zone size check by default on ASSERT_ENABLED
 // builds (which usually mean Debug builds). However, it is more valuable to
 // run this test on Release builds. That said, we don't want to do pay this
@@ -40,6 +38,14 @@
 
 #define VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE ASSERT_ENABLED
 
+#if VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
+#include <wtf/StackTrace.h>
+
+constexpr bool verboseStackCheckVerification = false;
+#endif
+
+namespace WTF {
+
 class StackCheck {
     WTF_MAKE_FAST_ALLOCATED;
 public:
@@ -48,7 +54,6 @@
         Scope(StackCheck& checker)
             : m_checker(checker)
         {
-            m_checker.isSafeToRecurse();
 #if VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
             RELEASE_ASSERT(checker.m_ownerThread == &Thread::current());
 
@@ -59,11 +64,17 @@
             // 2. use a larger reservedZone size.
             uint8_t* currentStackCheckpoint = static_cast<uint8_t*>(currentStackPointer());
             uint8_t* previousStackCheckpoint = m_checker.m_lastStackCheckpoint;
-            RELEASE_ASSERT(previousStackCheckpoint - currentStackCheckpoint > 0);
-            RELEASE_ASSERT(previousStackCheckpoint - currentStackCheckpoint < static_cast<ptrdiff_t>(m_checker.m_reservedZone));
+            ptrdiff_t stackBetweenCheckpoints = previousStackCheckpoint - currentStackCheckpoint;
 
             m_savedLastStackCheckpoint = m_checker.m_lastStackCheckpoint;
             m_checker.m_lastStackCheckpoint = currentStackCheckpoint;
+            if constexpr (verboseStackCheckVerification) {
+                m_savedLastCheckpointStackTrace = WTFMove(m_checker.m_lastCheckpointStackTrace);
+                m_checker.m_lastCheckpointStackTrace = StackTrace::captureStackTrace(INT_MAX);
+            }
+
+            if (UNLIKELY(stackBetweenCheckpoints <= 0 || stackBetweenCheckpoints >= static_cast<ptrdiff_t>(m_checker.m_reservedZone)))
+                reportVerificationFailureAndCrash();
 #endif
         }
 
@@ -71,6 +82,8 @@
         ~Scope()
         {
             m_checker.m_lastStackCheckpoint = m_savedLastStackCheckpoint;
+            if constexpr (verboseStackCheckVerification)
+                m_checker.m_lastCheckpointStackTrace = WTFMove(m_savedLastCheckpointStackTrace);
         }
 #endif
 
@@ -77,13 +90,18 @@
         bool isSafeToRecurse() { return m_checker.isSafeToRecurse(); }
 
     private:
+#if VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
+        WTF_EXPORT_PRIVATE NO_RETURN_DUE_TO_CRASH void reportVerificationFailureAndCrash();
+#endif
+
         StackCheck& m_checker;
 #if VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
         uint8_t* m_savedLastStackCheckpoint;
+        std::unique_ptr<StackTrace> m_savedLastCheckpointStackTrace;
 #endif
     };
 
-    StackCheck(const StackBounds& bounds = Thread::current().stack(), size_t minReservedZone = defaultReservedZoneSize)
+    StackCheck(const StackBounds& bounds = Thread::current().stack(), size_t minReservedZone = StackBounds::DefaultReservedZone)
         : m_stackLimit(bounds.recursionLimit(minReservedZone))
 #if VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
         , m_ownerThread(&Thread::current())
@@ -95,17 +113,12 @@
     inline bool isSafeToRecurse() { return currentStackPointer() >= m_stackLimit; }
 
 private:
-#if ASAN_ENABLED
-    static constexpr size_t defaultReservedZoneSize = StackBounds::DefaultReservedZone * 2;
-#else
-    static constexpr size_t defaultReservedZoneSize = StackBounds::DefaultReservedZone;
-#endif
-
     void* m_stackLimit;
 #if VERIFY_STACK_CHECK_RESERVED_ZONE_SIZE
     Thread* m_ownerThread;
     uint8_t* m_lastStackCheckpoint;
     size_t m_reservedZone;
+    std::unique_ptr<StackTrace> m_lastCheckpointStackTrace;
 #endif
 
     friend class Scope;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to