Title: [170754] trunk/Source
Revision
170754
Author
beid...@apple.com
Date
2014-07-03 09:19:07 -0700 (Thu, 03 Jul 2014)

Log Message

Possible crash in IconDatabase in WebCore::IconDatabase::dispatchDidRemoveAllIconsOnMainThread
<rdar://problem/17437687> and https://bugs.webkit.org/show_bug.cgi?id=134517

Reviewed by Eric Carlson.

Source/WebCore:
Since WebCore::IconDatabase is not RefCounted there’s no obvious way to keep it alive until it is
truly no longer needed.

This isn’t generally a problem because they are usually a singleton that lasts the lifetime of the process.

In the WebKit2 case, WebCore::IconDatabases can come and go as their owning WebIconDatabases come and go.

So we can rely on WebIconDatabase to handle the lifetime appropriately.

* loader/icon/IconDatabase.cpp:
(WebCore::IconDatabase::close): If the database is actually closed, notify the client.
(WebCore::IconDatabase::IconDatabase):
(WebCore::IconDatabase::isOpen): Take into account whether there’s any main thread callbacks, plus what
  is covered by isOpenBesidesMainThreadCallbacks.
(WebCore::IconDatabase::isOpenBesidesMainThreadCallbacks): Take into account whether the sync thread
  is still running and whether or not the database file is still open.
(WebCore::IconDatabase::checkClosedAfterMainThreadCallback): Checks to see if the database has just
  become 100% closed and - if so - notify the client.
(WebCore::IconDatabase::dispatchDidImportIconURLForPageURLOnMainThread): Updated to increment the main thread
  callback count and to call checkClosedAfterMainThreadCallback when done.
(WebCore::IconDatabase::dispatchDidImportIconDataForPageURLOnMainThread): Ditto.
(WebCore::IconDatabase::dispatchDidRemoveAllIconsOnMainThread): Ditto.
(WebCore::IconDatabase::dispatchDidFinishURLImportOnMainThread): Ditto.
* loader/icon/IconDatabase.h:

* loader/icon/IconDatabaseClient.h:
(WebCore::IconDatabaseClient::didClose): Added. For the IconDatabase to tell its client it is 100% closed.

Source/WebKit2:
* UIProcess/WebContext.cpp:
(WebKit::WebContext::~WebContext): Instead of directly deref’ing the WebIconDatabase, ask it to
  deref itself when appropriate.

* UIProcess/WebIconDatabase.cpp:
(WebKit::WebIconDatabase::WebIconDatabase):
(WebKit::WebIconDatabase::didClose): If this WebIconDatabase is supposed to deref itself when
  appropriate, do so now.
(WebKit::WebIconDatabase::derefWhenAppropriate): If the WebCore::IconDatabase is still open then
  defer this deref.
* UIProcess/WebIconDatabase.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (170753 => 170754)


--- trunk/Source/WebCore/ChangeLog	2014-07-03 15:46:36 UTC (rev 170753)
+++ trunk/Source/WebCore/ChangeLog	2014-07-03 16:19:07 UTC (rev 170754)
@@ -1,3 +1,38 @@
+2014-07-03  Brady Eidson  <beid...@apple.com>
+
+        Possible crash in IconDatabase in WebCore::IconDatabase::dispatchDidRemoveAllIconsOnMainThread
+        <rdar://problem/17437687> and https://bugs.webkit.org/show_bug.cgi?id=134517
+
+        Reviewed by Eric Carlson.
+
+        Since WebCore::IconDatabase is not RefCounted there’s no obvious way to keep it alive until it is
+        truly no longer needed.
+
+        This isn’t generally a problem because they are usually a singleton that lasts the lifetime of the process.
+
+        In the WebKit2 case, WebCore::IconDatabases can come and go as their owning WebIconDatabases come and go.
+
+        So we can rely on WebIconDatabase to handle the lifetime appropriately. 
+
+        * loader/icon/IconDatabase.cpp:
+        (WebCore::IconDatabase::close): If the database is actually closed, notify the client.
+        (WebCore::IconDatabase::IconDatabase):
+        (WebCore::IconDatabase::isOpen): Take into account whether there’s any main thread callbacks, plus what
+          is covered by isOpenBesidesMainThreadCallbacks.
+        (WebCore::IconDatabase::isOpenBesidesMainThreadCallbacks): Take into account whether the sync thread
+          is still running and whether or not the database file is still open.
+        (WebCore::IconDatabase::checkClosedAfterMainThreadCallback): Checks to see if the database has just
+          become 100% closed and - if so - notify the client.
+        (WebCore::IconDatabase::dispatchDidImportIconURLForPageURLOnMainThread): Updated to increment the main thread
+          callback count and to call checkClosedAfterMainThreadCallback when done.
+        (WebCore::IconDatabase::dispatchDidImportIconDataForPageURLOnMainThread): Ditto.
+        (WebCore::IconDatabase::dispatchDidRemoveAllIconsOnMainThread): Ditto.
+        (WebCore::IconDatabase::dispatchDidFinishURLImportOnMainThread): Ditto.
+        * loader/icon/IconDatabase.h:
+
+        * loader/icon/IconDatabaseClient.h:
+        (WebCore::IconDatabaseClient::didClose): Added. For the IconDatabase to tell its client it is 100% closed.
+
 2014-07-03  Chris Fleizach  <cfleiz...@apple.com>
 
         AX: VoiceOver does not read aria-expanded attribute on controls in Safari

Modified: trunk/Source/WebCore/loader/icon/IconDatabase.cpp (170753 => 170754)


--- trunk/Source/WebCore/loader/icon/IconDatabase.cpp	2014-07-03 15:46:36 UTC (rev 170753)
+++ trunk/Source/WebCore/loader/icon/IconDatabase.cpp	2014-07-03 16:19:07 UTC (rev 170754)
@@ -168,7 +168,11 @@
     m_removeIconsRequested = false;
 
     m_syncDB.close();
-    ASSERT(!isOpen());
+
+    // If there are still main thread callbacks in flight then the database might not actually be closed yet.
+    // But if it is closed, notify the client now.
+    if (!isOpen() && m_client)
+        m_client->didClose();
 }
 
 void IconDatabase::removeAllIcons()
@@ -793,6 +797,7 @@
     , m_syncThreadHasWorkToDo(false)
     , m_retainOrReleaseIconRequested(false)
     , m_initialPruningComplete(false)
+    , m_mainThreadCallbackCount(0)
     , m_client(defaultClient())
 {
     LOG(IconDatabase, "Creating IconDatabase %p", this);
@@ -864,8 +869,13 @@
 
 bool IconDatabase::isOpen() const
 {
+    return isOpenBesidesMainThreadCallbacks() || m_mainThreadCallbackCount;
+}
+
+bool IconDatabase::isOpenBesidesMainThreadCallbacks() const
+{
     MutexLocker locker(m_syncLock);
-    return m_syncDB.isOpen();
+    return m_syncThreadRunning || m_syncDB.isOpen();
 }
 
 String IconDatabase::databasePath() const
@@ -2066,45 +2076,73 @@
     SQLiteStatement(m_syncDB, "INSERT INTO IconDatabaseInfo (key, value) VALUES ('ExcludedFromBackup', 1)").executeCommand();
 }
 
+void IconDatabase::checkClosedAfterMainThreadCallback()
+{
+    ASSERT_NOT_SYNC_THREAD();
+
+    // If there are still callbacks in flight from the sync thread we cannot possibly be closed.
+    if (--m_mainThreadCallbackCount)
+        return;
+
+    // Even if there's no more pending callbacks the database might otherwise still be open.
+    if (isOpenBesidesMainThreadCallbacks())
+        return;
+
+    // This database is now actually closed! But first notify the client.
+    if (m_client)
+        m_client->didClose();
+}
+
 void IconDatabase::dispatchDidImportIconURLForPageURLOnMainThread(const String& pageURL)
 {
     ASSERT_ICON_SYNC_THREAD();
+    ++m_mainThreadCallbackCount;
 
     String pageURLCopy = pageURL.isolatedCopy();
     callOnMainThread([this, pageURLCopy] {
-        m_client->didImportIconURLForPageURL(pageURLCopy);
+        if (m_client)
+            m_client->didImportIconURLForPageURL(pageURLCopy);
+        checkClosedAfterMainThreadCallback();
     });
 }
 
 void IconDatabase::dispatchDidImportIconDataForPageURLOnMainThread(const String& pageURL)
 {
     ASSERT_ICON_SYNC_THREAD();
+    ++m_mainThreadCallbackCount;
 
     String pageURLCopy = pageURL.isolatedCopy();
     callOnMainThread([this, pageURLCopy] {
-        m_client->didImportIconDataForPageURL(pageURLCopy);
+        if (m_client)
+            m_client->didImportIconDataForPageURL(pageURLCopy);
+        checkClosedAfterMainThreadCallback();
     });
 }
 
 void IconDatabase::dispatchDidRemoveAllIconsOnMainThread()
 {
     ASSERT_ICON_SYNC_THREAD();
+    ++m_mainThreadCallbackCount;
 
     callOnMainThread([this] {
-        m_client->didRemoveAllIcons();
+        if (m_client)
+            m_client->didRemoveAllIcons();
+        checkClosedAfterMainThreadCallback();
     });
 }
 
 void IconDatabase::dispatchDidFinishURLImportOnMainThread()
 {
     ASSERT_ICON_SYNC_THREAD();
+    ++m_mainThreadCallbackCount;
 
     callOnMainThread([this] {
-        m_client->didFinishURLImport();
+        if (m_client)
+            m_client->didFinishURLImport();
+        checkClosedAfterMainThreadCallback();
     });
 }
 
-
 } // namespace WebCore
 
 #endif // ENABLE(ICONDATABASE)

Modified: trunk/Source/WebCore/loader/icon/IconDatabase.h (170753 => 170754)


--- trunk/Source/WebCore/loader/icon/IconDatabase.h	2014-07-03 15:46:36 UTC (rev 170753)
+++ trunk/Source/WebCore/loader/icon/IconDatabase.h	2014-07-03 16:19:07 UTC (rev 170754)
@@ -203,6 +203,9 @@
     bool wasExcludedFromBackup();
     void setWasExcludedFromBackup();
 
+    bool isOpenBesidesMainThreadCallbacks() const;
+    void checkClosedAfterMainThreadCallback();
+
     bool m_initialPruningComplete;
         
     void setIconURLForPageURLInSQLDatabase(const String&, const String&);
@@ -221,6 +224,7 @@
     void dispatchDidImportIconDataForPageURLOnMainThread(const String&);
     void dispatchDidRemoveAllIconsOnMainThread();
     void dispatchDidFinishURLImportOnMainThread();
+    std::atomic<uint32_t> m_mainThreadCallbackCount;
     
     // The client is set by the main thread before the thread starts, and from then on is only used by the sync thread
     IconDatabaseClient* m_client;

Modified: trunk/Source/WebCore/loader/icon/IconDatabaseClient.h (170753 => 170754)


--- trunk/Source/WebCore/loader/icon/IconDatabaseClient.h	2014-07-03 15:46:36 UTC (rev 170753)
+++ trunk/Source/WebCore/loader/icon/IconDatabaseClient.h	2014-07-03 16:19:07 UTC (rev 170754)
@@ -42,6 +42,7 @@
     virtual void didChangeIconForPageURL(const String&) = 0;
     virtual void didRemoveAllIcons() = 0;
     virtual void didFinishURLImport() = 0;
+    virtual void didClose() { }
 };
  
 } // namespace WebCore 

Modified: trunk/Source/WebKit2/ChangeLog (170753 => 170754)


--- trunk/Source/WebKit2/ChangeLog	2014-07-03 15:46:36 UTC (rev 170753)
+++ trunk/Source/WebKit2/ChangeLog	2014-07-03 16:19:07 UTC (rev 170754)
@@ -1,3 +1,22 @@
+2014-07-03  Brady Eidson  <beid...@apple.com>
+
+        Possible crash in IconDatabase in WebCore::IconDatabase::dispatchDidRemoveAllIconsOnMainThread
+        <rdar://problem/17437687> and https://bugs.webkit.org/show_bug.cgi?id=134517
+
+        Reviewed by Eric Carlson.
+
+        * UIProcess/WebContext.cpp:
+        (WebKit::WebContext::~WebContext): Instead of directly deref’ing the WebIconDatabase, ask it to
+          deref itself when appropriate.
+
+        * UIProcess/WebIconDatabase.cpp:
+        (WebKit::WebIconDatabase::WebIconDatabase):
+        (WebKit::WebIconDatabase::didClose): If this WebIconDatabase is supposed to deref itself when
+          appropriate, do so now.
+        (WebKit::WebIconDatabase::derefWhenAppropriate): If the WebCore::IconDatabase is still open then
+          defer this deref.
+        * UIProcess/WebIconDatabase.h:
+
 2014-07-03  Carlos Garcia Campos  <cgar...@igalia.com>
 
         REGRESSION(r170676): [GTK] UI process crashes when the Web Process crashes

Modified: trunk/Source/WebKit2/UIProcess/WebContext.cpp (170753 => 170754)


--- trunk/Source/WebKit2/UIProcess/WebContext.cpp	2014-07-03 15:46:36 UTC (rev 170753)
+++ trunk/Source/WebKit2/UIProcess/WebContext.cpp	2014-07-03 16:19:07 UTC (rev 170754)
@@ -257,7 +257,9 @@
 
     m_iconDatabase->invalidate();
     m_iconDatabase->clearContext();
-    
+    WebIconDatabase* rawIconDatabase = m_iconDatabase.release().leakRef();
+    rawIconDatabase->derefWhenAppropriate();
+
 #if ENABLE(NETSCAPE_PLUGIN_API)
     m_pluginSiteDataManager->invalidate();
     m_pluginSiteDataManager->clearContext();

Modified: trunk/Source/WebKit2/UIProcess/WebIconDatabase.cpp (170753 => 170754)


--- trunk/Source/WebKit2/UIProcess/WebIconDatabase.cpp	2014-07-03 15:46:36 UTC (rev 170753)
+++ trunk/Source/WebKit2/UIProcess/WebIconDatabase.cpp	2014-07-03 16:19:07 UTC (rev 170754)
@@ -52,6 +52,7 @@
     : m_webContext(&context)
     , m_urlImportCompleted(false)
     , m_databaseCleanupDisabled(false)
+    , m_shouldDerefWhenAppropriate(false)
 {
     m_webContext->addMessageReceiver(Messages::WebIconDatabase::messageReceiverName(), *this);
 }
@@ -284,6 +285,24 @@
     m_urlImportCompleted = true;
 }
 
+void WebIconDatabase::didClose()
+{
+    if (!m_shouldDerefWhenAppropriate)
+        return;
+
+    deref();
+}
+
+void WebIconDatabase::derefWhenAppropriate()
+{
+    if (m_iconDatabaseImpl && m_iconDatabaseImpl->isOpen()) {
+        m_shouldDerefWhenAppropriate = true;
+        return;
+    }
+
+    deref();
+}
+
 void WebIconDatabase::notifyIconDataReadyForPageURL(const String& pageURL)
 {
     m_iconDatabaseClient.iconDataReadyForPageURL(this, API::URL::create(pageURL).get());

Modified: trunk/Source/WebKit2/UIProcess/WebIconDatabase.h (170753 => 170754)


--- trunk/Source/WebKit2/UIProcess/WebIconDatabase.h	2014-07-03 15:46:36 UTC (rev 170753)
+++ trunk/Source/WebKit2/UIProcess/WebIconDatabase.h	2014-07-03 16:19:07 UTC (rev 170754)
@@ -84,6 +84,11 @@
 
     void setPrivateBrowsingEnabled(bool);
 
+    // Called when the WebContext is through with this WebIconDatabase but the
+    // WebCore::IconDatabase possibly isn't done shutting down.
+    // In that case this WebIconDatabase will deref() itself when the time is right.
+    void derefWhenAppropriate();
+
 private:
     explicit WebIconDatabase(WebContext&);
 
@@ -93,6 +98,7 @@
     virtual void didChangeIconForPageURL(const String&) override;
     virtual void didRemoveAllIcons() override;
     virtual void didFinishURLImport() override;
+    virtual void didClose() override;
 
     // IPC::MessageReceiver
     virtual void didReceiveMessage(IPC::Connection*, IPC::MessageDecoder&) override;
@@ -105,6 +111,7 @@
     std::unique_ptr<WebCore::IconDatabase> m_iconDatabaseImpl;
     bool m_urlImportCompleted;
     bool m_databaseCleanupDisabled;
+    bool m_shouldDerefWhenAppropriate;
     HashMap<uint64_t, String> m_pendingLoadDecisionURLMap;
 
     WebIconDatabaseClient m_iconDatabaseClient;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to