Title: [265229] trunk
Revision
265229
Author
[email protected]
Date
2020-08-03 15:53:06 -0700 (Mon, 03 Aug 2020)

Log Message

Occasional crashes when running nested runloops while using UIWebView
https://bugs.webkit.org/show_bug.cgi?id=213067
<rdar://problem/64239727>

Reviewed by Darin Adler.

Source/WebCore:

New test: WebKitLegacy.NestedRunLoopUnderRunLoopObserverDoubleUnlock

* platform/ios/wak/WebCoreThread.mm:
(MainRunLoopAutoUnlock):
(_WebThreadAutoLock):
Under certain circumstances, a CFRunLoopObserver that is already removed
can fire again (e.g. when removed in a nested runloop, it can be fired again
in the main runloop).

The WebThread code assumes that the auto-unlock observer is called exactly
once after being installed. If this is not the case, the main thread lock
count can underflow, wreaking havoc the next time we try to lock.
Instead of depending on CFRunLoop to do this for us, make it explicit
with a global boolean.

This leaves in place the somewhat-odd behavior that running a nested
runloop can cause the main runloop's WebThread lock to be dropped, because
a great deal of code (and tests) depend on this behavior (keeping the lock
held until the outer runloop spins would result in deadlock if the inner
runloop's loop condition depends on the WebThread being able to execute code).
However, this is not unique to this underflow case, and appears to be
extremely long-standing behavior, so maintaining it seems the best course of action.

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKitLegacy/ios/WebThreadLock.mm: Added.
Add a API test that fails before this change.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (265228 => 265229)


--- trunk/Source/WebCore/ChangeLog	2020-08-03 22:48:47 UTC (rev 265228)
+++ trunk/Source/WebCore/ChangeLog	2020-08-03 22:53:06 UTC (rev 265229)
@@ -1,3 +1,34 @@
+2020-08-03  Tim Horton  <[email protected]>
+
+        Occasional crashes when running nested runloops while using UIWebView
+        https://bugs.webkit.org/show_bug.cgi?id=213067
+        <rdar://problem/64239727>
+
+        Reviewed by Darin Adler.
+
+        New test: WebKitLegacy.NestedRunLoopUnderRunLoopObserverDoubleUnlock
+
+        * platform/ios/wak/WebCoreThread.mm:
+        (MainRunLoopAutoUnlock):
+        (_WebThreadAutoLock):
+        Under certain circumstances, a CFRunLoopObserver that is already removed
+        can fire again (e.g. when removed in a nested runloop, it can be fired again
+        in the main runloop).
+
+        The WebThread code assumes that the auto-unlock observer is called exactly
+        once after being installed. If this is not the case, the main thread lock
+        count can underflow, wreaking havoc the next time we try to lock.
+        Instead of depending on CFRunLoop to do this for us, make it explicit
+        with a global boolean.
+
+        This leaves in place the somewhat-odd behavior that running a nested
+        runloop can cause the main runloop's WebThread lock to be dropped, because
+        a great deal of code (and tests) depend on this behavior (keeping the lock
+        held until the outer runloop spins would result in deadlock if the inner
+        runloop's loop condition depends on the WebThread being able to execute code).
+        However, this is not unique to this underflow case, and appears to be
+        extremely long-standing behavior, so maintaining it seems the best course of action.
+
 2020-08-03  Wenson Hsieh  <[email protected]>
 
         Remove the ENABLE_DATA_INTERACTION feature flag

Modified: trunk/Source/WebCore/platform/ios/wak/WebCoreThread.mm (265228 => 265229)


--- trunk/Source/WebCore/platform/ios/wak/WebCoreThread.mm	2020-08-03 22:48:47 UTC (rev 265228)
+++ trunk/Source/WebCore/platform/ios/wak/WebCoreThread.mm	2020-08-03 22:53:06 UTC (rev 265229)
@@ -116,6 +116,7 @@
 #endif
 
 static CFRunLoopObserverRef mainRunLoopAutoUnlockObserver;
+static BOOL mainThreadHasPendingAutoUnlock;
 
 static Lock startupLock;
 static StaticCondition startupCondition;
@@ -447,7 +448,11 @@
 
     if (sMainThreadModalCount)
         return;
-    
+
+    if (!mainThreadHasPendingAutoUnlock)
+        return;
+
+    mainThreadHasPendingAutoUnlock = NO;
     CFRunLoopRemoveObserver(CFRunLoopGetCurrent(), mainRunLoopAutoUnlockObserver, kCFRunLoopCommonModes);
 
     _WebThreadUnlock();
@@ -458,6 +463,7 @@
     ASSERT(!WebThreadIsCurrent());
 
     if (!mainThreadLockCount) {
+        mainThreadHasPendingAutoUnlock = YES;
         CFRunLoopAddObserver(CFRunLoopGetCurrent(), mainRunLoopAutoUnlockObserver, kCFRunLoopCommonModes);    
         _WebThreadLock();
         CFRunLoopWakeUp(CFRunLoopGetMain());

Modified: trunk/Tools/ChangeLog (265228 => 265229)


--- trunk/Tools/ChangeLog	2020-08-03 22:48:47 UTC (rev 265228)
+++ trunk/Tools/ChangeLog	2020-08-03 22:53:06 UTC (rev 265229)
@@ -1,3 +1,15 @@
+2020-08-03  Tim Horton  <[email protected]>
+
+        Occasional crashes when running nested runloops while using UIWebView
+        https://bugs.webkit.org/show_bug.cgi?id=213067
+        <rdar://problem/64239727>
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKitLegacy/ios/WebThreadLock.mm: Added.
+        Add a API test that fails before this change.
+
 2020-08-03  Wenson Hsieh  <[email protected]>
 
         Remove the ENABLE_DATA_INTERACTION feature flag

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (265228 => 265229)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2020-08-03 22:48:47 UTC (rev 265228)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2020-08-03 22:53:06 UTC (rev 265229)
@@ -152,6 +152,7 @@
 		2DB0232F1E4E871800707123 /* InteractionDeadlockAfterCrash.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2DB0232E1E4E871800707123 /* InteractionDeadlockAfterCrash.mm */; };
 		2DB647881F4163D60051A89E /* WKWebViewDoesNotLogDuringInitialization.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2DB647871F4161F70051A89E /* WKWebViewDoesNotLogDuringInitialization.mm */; };
 		2DC4CF771D2D9DD800ECCC94 /* DataDetection.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2DC4CF761D2D9DD800ECCC94 /* DataDetection.mm */; };
+		2DC9451724D8AC0200430376 /* WebThreadLock.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2DC9451624D8AC0200430376 /* WebThreadLock.mm */; };
 		2DD7D3AF178227B30026E1E3 /* lots-of-text-vertical-lr.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 2DD7D3AE178227AC0026E1E3 /* lots-of-text-vertical-lr.html */; };
 		2DE71AFE1D49C0BD00904094 /* AnimatedResize.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2DE71AFD1D49C0BD00904094 /* AnimatedResize.mm */; };
 		2DE71B001D49C3ED00904094 /* blinking-div.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 2DE71AFF1D49C2F000904094 /* blinking-div.html */; };
@@ -277,9 +278,9 @@
 		510477771D298E72009747EB /* IDBDeleteRecovery.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 510477761D298E57009747EB /* IDBDeleteRecovery.html */; };
 		510477781D29923B009747EB /* IDBDeleteRecovery.mm in Sources */ = {isa = PBXBuildFile; fileRef = 510477751D298E03009747EB /* IDBDeleteRecovery.mm */; };
 		510A91F824D3622100BFD89C /* SonyDualShock3.mm in Sources */ = {isa = PBXBuildFile; fileRef = 510A91F724D3621900BFD89C /* SonyDualShock3.mm */; };
-		510A921424D615B400BFD89C /* GoogleStadia.mm in Sources */ = {isa = PBXBuildFile; fileRef = 510A921324D60A3E00BFD89C /* GoogleStadia.mm */; };
 		510A920A24D5048900BFD89C /* LogitechF310.mm in Sources */ = {isa = PBXBuildFile; fileRef = 510A920924D5048300BFD89C /* LogitechF310.mm */; };
 		510A920C24D5275500BFD89C /* LogitechF710.mm in Sources */ = {isa = PBXBuildFile; fileRef = 510A920B24D5275100BFD89C /* LogitechF710.mm */; };
+		510A921424D615B400BFD89C /* GoogleStadia.mm in Sources */ = {isa = PBXBuildFile; fileRef = 510A921324D60A3E00BFD89C /* GoogleStadia.mm */; };
 		5110FCF11E01CD64006F8D0B /* IDBIndexUpgradeToV2.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 5110FCF01E01CD53006F8D0B /* IDBIndexUpgradeToV2.html */; };
 		5110FCF61E01CD83006F8D0B /* IndexUpgrade.sqlite3 in Copy Resources */ = {isa = PBXBuildFile; fileRef = 5110FCF31E01CD77006F8D0B /* IndexUpgrade.sqlite3 */; };
 		5110FCF91E01CD8A006F8D0B /* IndexUpgrade.blob in Copy Resources */ = {isa = PBXBuildFile; fileRef = 5110FCF21E01CD77006F8D0B /* IndexUpgrade.blob */; };
@@ -1816,6 +1817,7 @@
 		2DB0232E1E4E871800707123 /* InteractionDeadlockAfterCrash.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = InteractionDeadlockAfterCrash.mm; sourceTree = "<group>"; };
 		2DB647871F4161F70051A89E /* WKWebViewDoesNotLogDuringInitialization.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WKWebViewDoesNotLogDuringInitialization.mm; sourceTree = "<group>"; };
 		2DC4CF761D2D9DD800ECCC94 /* DataDetection.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = DataDetection.mm; sourceTree = "<group>"; };
+		2DC9451624D8AC0200430376 /* WebThreadLock.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebThreadLock.mm; sourceTree = "<group>"; };
 		2DD355351BD08378005DF4A7 /* AutoLayoutIntegration.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = AutoLayoutIntegration.mm; sourceTree = "<group>"; };
 		2DD7D3A9178205D00026E1E3 /* ResizeReversePaginatedWebView.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ResizeReversePaginatedWebView.cpp; sourceTree = "<group>"; };
 		2DD7D3AE178227AC0026E1E3 /* lots-of-text-vertical-lr.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "lots-of-text-vertical-lr.html"; sourceTree = "<group>"; };
@@ -1976,9 +1978,9 @@
 		510477751D298E03009747EB /* IDBDeleteRecovery.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = IDBDeleteRecovery.mm; sourceTree = "<group>"; };
 		510477761D298E57009747EB /* IDBDeleteRecovery.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = IDBDeleteRecovery.html; sourceTree = "<group>"; };
 		510A91F724D3621900BFD89C /* SonyDualShock3.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = SonyDualShock3.mm; sourceTree = "<group>"; };
-		510A921324D60A3E00BFD89C /* GoogleStadia.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = GoogleStadia.mm; sourceTree = "<group>"; };
 		510A920924D5048300BFD89C /* LogitechF310.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = LogitechF310.mm; sourceTree = "<group>"; };
 		510A920B24D5275100BFD89C /* LogitechF710.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = LogitechF710.mm; sourceTree = "<group>"; };
+		510A921324D60A3E00BFD89C /* GoogleStadia.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = GoogleStadia.mm; sourceTree = "<group>"; };
 		5110FCEF1E01CBAA006F8D0B /* IDBIndexUpgradeToV2.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = IDBIndexUpgradeToV2.mm; sourceTree = "<group>"; };
 		5110FCF01E01CD53006F8D0B /* IDBIndexUpgradeToV2.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = IDBIndexUpgradeToV2.html; sourceTree = "<group>"; };
 		5110FCF21E01CD77006F8D0B /* IndexUpgrade.blob */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = IndexUpgrade.blob; sourceTree = "<group>"; };
@@ -4554,6 +4556,7 @@
 				F434CA1922E65BCA005DDB26 /* ScrollToRevealSelection.mm */,
 				0F4FFA9D1ED3AA8500F7111F /* SnapshotViaRenderInContext.mm */,
 				31E9BDA0247F4C62002E51A2 /* WebGLPrepareDisplayOnWebThread.mm */,
+				2DC9451624D8AC0200430376 /* WebThreadLock.mm */,
 			);
 			path = ios;
 			sourceTree = "<group>";
@@ -5424,6 +5427,7 @@
 				5120C83D1E6751290025B250 /* WebsiteDataStoreCustomPaths.mm in Sources */,
 				5C9E56851DF9145400C9EE33 /* WebsitePolicies.mm in Sources */,
 				931C281D22BC55F2001D98C4 /* WebSQLBasics.mm in Sources */,
+				2DC9451724D8AC0200430376 /* WebThreadLock.mm in Sources */,
 				7CCE7ED41A411A7E00447C4C /* WebViewCanPasteURL.mm in Sources */,
 				5C0BF8911DD599A900B00328 /* WebViewCanPasteZeroPng.mm in Sources */,
 				7C83E0421D0A63FD00FEBCF3 /* WebViewCloseInsideDidFinishLoadForFrame.mm in Sources */,

Added: trunk/Tools/TestWebKitAPI/Tests/WebKitLegacy/ios/WebThreadLock.mm (0 => 265229)


--- trunk/Tools/TestWebKitAPI/Tests/WebKitLegacy/ios/WebThreadLock.mm	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitLegacy/ios/WebThreadLock.mm	2020-08-03 22:53:06 UTC (rev 265229)
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2020 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. AND ITS CONTRIBUTORS ``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 ITS 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.
+ */
+
+#import "config.h"
+
+#if USE(WEB_THREAD)
+
+#import "PlatformUtilities.h"
+#import <Foundation/Foundation.h>
+#import <WebCore/WebCoreThread.h>
+#import <wtf/RetainPtr.h>
+
+namespace TestWebKitAPI {
+
+TEST(WebKitLegacy, NestedRunLoopUnderRunLoopObserverDoubleUnlock)
+{
+    WebThreadEnable();
+
+    WebThreadLock();
+    
+    __block BOOL spunInnerRunLoop = NO;
+    auto observer = adoptCF(CFRunLoopObserverCreateWithHandler(NULL, kCFRunLoopBeforeWaiting, NO, 2, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
+        Util::spinRunLoop(1);
+        spunInnerRunLoop = YES;
+    }));
+
+    CFRunLoopAddObserver(CFRunLoopGetMain(), observer.get(), kCFRunLoopCommonModes);
+
+    // We must use a time-based instead of count-or-condition-based spin,
+    // so that we hit the above "before waiting" observer.
+    CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true);
+    
+    EXPECT_TRUE(spunInnerRunLoop);
+    
+    // Spinning the runloop should have resulted in dropping the lock,
+    // and *not underflowing* the lock count (which is also reported as "locked").
+    EXPECT_FALSE(WebThreadIsLocked());
+}
+
+}
+
+#endif
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to