Title: [247766] trunk/Source/WebKit
Revision
247766
Author
[email protected]
Date
2019-07-23 21:29:09 -0700 (Tue, 23 Jul 2019)

Log Message

Crash under WebKit:WTF::Detail::CallableWrapper<WebKit::ResourceLoadStatisticsMemoryStore::updateCookieBlocking(WTF::CompletionHandler<void ()>&&)::$_32::operator()()::'lambda'(), void>::call
https://bugs.webkit.org/show_bug.cgi?id=200071
<rdar://problem/53335583>

Reviewed by Brent Fulgham and Youenn Fablet.

The WebResourceLoadStatisticsStore is a main thread object. In its destructor, it was dispatching
to the background queue to destroy the m_statisticsStore / m_persistentStorage data members, which
live on the background queue. It would then synchronously wait for the background queue to finish
destroying them. The idea was to guarantee that the ResourceLoadStatisticsMemoryStore and the
ResourceLoadStatisticsPersistentStorage would never outlive the WebResourceLoadStatisticsStore,
given that they keep a raw pointer back to the WebResourceLoadStatisticsStore (via m_store data
member).

The issue is that *while* the WebResourceLoadStatisticsStore destructor is running on the main
thread, the background queue may be running code in ResourceLoadStatisticsMemoryStore or
ResourceLoadStatisticsPersistentStorage which refs the WebResourceLoadStatisticsStore, even
though its ref count has already reached 0. It is actually a common pattern in
ResourceLoadStatisticsMemoryStore to call RunLoop::main().dispatch() and ref their m_store in
the lambda, so that they can interact with the WebResourceLoadStatisticsStore.

To address the issue, we now destroy m_statisticsStore / m_persistentStorage *before* the
WebResourceLoadStatisticsStore destructor runs. The NetworkSession destructor now calls
WebResourceLoadStatisticsStore::didDestroyNetworkSession() which takes care of destroying
m_statisticsStore / m_persistentStorage on the background queue, synchronously. The
WebResourceLoadStatisticsStore destructor will only run later, once all remaining references
to it are gone.

* NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:
(WebKit::WebResourceLoadStatisticsStore::~WebResourceLoadStatisticsStore):
(WebKit::WebResourceLoadStatisticsStore::didDestroyNetworkSession):
* NetworkProcess/Classifier/WebResourceLoadStatisticsStore.h:
* NetworkProcess/NetworkSession.cpp:
(WebKit::NetworkSession::~NetworkSession):

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (247765 => 247766)


--- trunk/Source/WebKit/ChangeLog	2019-07-24 03:05:26 UTC (rev 247765)
+++ trunk/Source/WebKit/ChangeLog	2019-07-24 04:29:09 UTC (rev 247766)
@@ -1,3 +1,40 @@
+2019-07-23  Chris Dumez  <[email protected]>
+
+        Crash under WebKit:WTF::Detail::CallableWrapper<WebKit::ResourceLoadStatisticsMemoryStore::updateCookieBlocking(WTF::CompletionHandler<void ()>&&)::$_32::operator()()::'lambda'(), void>::call
+        https://bugs.webkit.org/show_bug.cgi?id=200071
+        <rdar://problem/53335583>
+
+        Reviewed by Brent Fulgham and Youenn Fablet.
+
+        The WebResourceLoadStatisticsStore is a main thread object. In its destructor, it was dispatching
+        to the background queue to destroy the m_statisticsStore / m_persistentStorage data members, which
+        live on the background queue. It would then synchronously wait for the background queue to finish
+        destroying them. The idea was to guarantee that the ResourceLoadStatisticsMemoryStore and the
+        ResourceLoadStatisticsPersistentStorage would never outlive the WebResourceLoadStatisticsStore,
+        given that they keep a raw pointer back to the WebResourceLoadStatisticsStore (via m_store data
+        member).
+
+        The issue is that *while* the WebResourceLoadStatisticsStore destructor is running on the main
+        thread, the background queue may be running code in ResourceLoadStatisticsMemoryStore or
+        ResourceLoadStatisticsPersistentStorage which refs the WebResourceLoadStatisticsStore, even
+        though its ref count has already reached 0. It is actually a common pattern in
+        ResourceLoadStatisticsMemoryStore to call RunLoop::main().dispatch() and ref their m_store in
+        the lambda, so that they can interact with the WebResourceLoadStatisticsStore.
+
+        To address the issue, we now destroy m_statisticsStore / m_persistentStorage *before* the
+        WebResourceLoadStatisticsStore destructor runs. The NetworkSession destructor now calls
+        WebResourceLoadStatisticsStore::didDestroyNetworkSession() which takes care of destroying
+        m_statisticsStore / m_persistentStorage on the background queue, synchronously. The
+        WebResourceLoadStatisticsStore destructor will only run later, once all remaining references
+        to it are gone.
+
+        * NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp:
+        (WebKit::WebResourceLoadStatisticsStore::~WebResourceLoadStatisticsStore):
+        (WebKit::WebResourceLoadStatisticsStore::didDestroyNetworkSession):
+        * NetworkProcess/Classifier/WebResourceLoadStatisticsStore.h:
+        * NetworkProcess/NetworkSession.cpp:
+        (WebKit::NetworkSession::~NetworkSession):
+
 2019-07-23  Alex Christensen  <[email protected]>
 
         Move _javascript_ConfigurationDirectory SPI from _WKProcessPoolConfiguration to WKProcessPoolPrivate to make it runtime switchable

Modified: trunk/Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp (247765 => 247766)


--- trunk/Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp	2019-07-24 03:05:26 UTC (rev 247765)
+++ trunk/Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.cpp	2019-07-24 04:29:09 UTC (rev 247766)
@@ -180,7 +180,15 @@
 WebResourceLoadStatisticsStore::~WebResourceLoadStatisticsStore()
 {
     ASSERT(RunLoop::isMain());
+    ASSERT(!m_statisticsStore);
+    ASSERT(!m_persistentStorage);
+}
 
+void WebResourceLoadStatisticsStore::didDestroyNetworkSession()
+{
+    ASSERT(RunLoop::isMain());
+
+    m_networkSession = nullptr;
     flushAndDestroyPersistentStore();
 }
 
@@ -335,6 +343,9 @@
             return;
         case StorageAccessStatus::RequiresUserPrompt:
             {
+            if (!m_networkSession)
+                return completionHandler(StorageAccessWasGranted::No, StorageAccessPromptWasShown::No);
+
             CompletionHandler<void(bool)> requestConfirmationCompletionHandler = [this, protectedThis = protectedThis.copyRef(), subFrameDomain, topFrameDomain, frameID, pageID, completionHandler = WTFMove(completionHandler)] (bool userDidGrantAccess) mutable {
                 if (userDidGrantAccess)
                     grantStorageAccess(subFrameDomain, topFrameDomain, frameID, pageID, StorageAccessPromptWasShown::Yes, WTFMove(completionHandler));
@@ -358,13 +369,11 @@
             return;
         }
 
-        if (m_statisticsStore) {
-            m_statisticsStore->requestStorageAccess(WTFMove(subFrameDomain), WTFMove(topFrameDomain), frameID, pageID, [statusHandler = WTFMove(statusHandler)](StorageAccessStatus status) mutable {
-                postTaskReply([statusHandler = WTFMove(statusHandler), status]() mutable {
-                    statusHandler(status);
-                });
+        m_statisticsStore->requestStorageAccess(WTFMove(subFrameDomain), WTFMove(topFrameDomain), frameID, pageID, [statusHandler = WTFMove(statusHandler)](StorageAccessStatus status) mutable {
+            postTaskReply([statusHandler = WTFMove(statusHandler), status]() mutable {
+                statusHandler(status);
             });
-        }
+        });
     });
 }
 

Modified: trunk/Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.h (247765 => 247766)


--- trunk/Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.h	2019-07-24 03:05:26 UTC (rev 247765)
+++ trunk/Source/WebKit/NetworkProcess/Classifier/WebResourceLoadStatisticsStore.h	2019-07-24 04:29:09 UTC (rev 247766)
@@ -94,6 +94,8 @@
 
     ~WebResourceLoadStatisticsStore();
 
+    void didDestroyNetworkSession();
+
     static const OptionSet<WebsiteDataType>& monitoredDataTypes();
 
     WorkQueue& statisticsQueue() { return m_statisticsQueue.get(); }

Modified: trunk/Source/WebKit/NetworkProcess/NetworkSession.cpp (247765 => 247766)


--- trunk/Source/WebKit/NetworkProcess/NetworkSession.cpp	2019-07-24 03:05:26 UTC (rev 247765)
+++ trunk/Source/WebKit/NetworkProcess/NetworkSession.cpp	2019-07-24 04:29:09 UTC (rev 247766)
@@ -107,6 +107,11 @@
 
 NetworkSession::~NetworkSession()
 {
+#if ENABLE(RESOURCE_LOAD_STATISTICS)
+    if (m_resourceLoadStatistics)
+        m_resourceLoadStatistics->didDestroyNetworkSession();
+#endif
+
     m_storageManager->resume();
     m_storageManager->waitUntilTasksFinished();
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to