Title: [248918] trunk/Source
Revision
248918
Author
cdu...@apple.com
Date
2019-08-20 15:18:23 -0700 (Tue, 20 Aug 2019)

Log Message

WebSQLiteDatabaseTracker does not ensure it is still alive after dispatching to the main thread
https://bugs.webkit.org/show_bug.cgi?id=200925

Reviewed by Geoffrey Garen.

WebSQLiteDatabaseTracker does not ensure it is still alive after dispatching to the main thread,
which is not safe. Use WeakPtr to address the issue.

* Shared/WebSQLiteDatabaseTracker.cpp:
(WebKit::WebSQLiteDatabaseTracker::WebSQLiteDatabaseTracker):
(WebKit::WebSQLiteDatabaseTracker::willBeginFirstTransaction):
(WebKit::WebSQLiteDatabaseTracker::didFinishLastTransaction):
* Shared/WebSQLiteDatabaseTracker.h:

Modified Paths

Diff

Modified: trunk/Source/WTF/wtf/WeakPtr.h (248917 => 248918)


--- trunk/Source/WTF/wtf/WeakPtr.h	2019-08-20 21:33:06 UTC (rev 248917)
+++ trunk/Source/WTF/wtf/WeakPtr.h	2019-08-20 22:18:23 UTC (rev 248918)
@@ -154,12 +154,19 @@
         m_impl->clear();
     }
 
-    WeakPtr<T> createWeakPtr(T& object) const
+    void initializeIfNeeded(const T& object) const
     {
+        if (m_impl)
+            return;
+
         ASSERT(m_wasConstructedOnMainThread == isMainThread());
-        if (!m_impl)
-            m_impl = WeakPtrImpl::create(&object);
+        m_impl = WeakPtrImpl::create(const_cast<T*>(&object));
+    }
 
+    WeakPtr<T> createWeakPtr(T& object) const
+    {
+        initializeIfNeeded(object);
+
         ASSERT(&object == m_impl->get<T>());
         return WeakPtr<T>(makeRef(*m_impl));
     }
@@ -166,9 +173,7 @@
 
     WeakPtr<const T> createWeakPtr(const T& object) const
     {
-        ASSERT(m_wasConstructedOnMainThread == isMainThread());
-        if (!m_impl)
-            m_impl = WeakPtrImpl::create(const_cast<T*>(&object));
+        initializeIfNeeded(object);
 
         ASSERT(&object == m_impl->get<T>());
         return WeakPtr<T>(makeRef(*m_impl));
@@ -192,13 +197,24 @@
 #endif
 };
 
-template<typename T> class CanMakeWeakPtr {
+// We use lazy initialization of the WeakPtrFactory by default to avoid unnecessary initialization. Eager
+// initialization is however useful if you plan to call makeWeakPtr() from other threads.
+enum class WeakPtrFactoryInitialization { Lazy, Eager };
+
+template<typename T, WeakPtrFactoryInitialization initializationMode = WeakPtrFactoryInitialization::Lazy> class CanMakeWeakPtr {
 public:
-    typedef T WeakValueType;
+    using WeakValueType = T;
 
     const WeakPtrFactory<T>& weakPtrFactory() const { return m_weakPtrFactory; }
     WeakPtrFactory<T>& weakPtrFactory() { return m_weakPtrFactory; }
 
+protected:
+    CanMakeWeakPtr()
+    {
+        if (initializationMode == WeakPtrFactoryInitialization::Eager)
+            m_weakPtrFactory.initializeIfNeeded(static_cast<T&>(*this));
+    }
+
 private:
     WeakPtrFactory<T> m_weakPtrFactory;
 };
@@ -278,4 +294,5 @@
 using WTF::CanMakeWeakPtr;
 using WTF::WeakPtr;
 using WTF::WeakPtrFactory;
+using WTF::WeakPtrFactoryInitialization;
 using WTF::makeWeakPtr;

Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.h (248917 => 248918)


--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.h	2019-08-20 21:33:06 UTC (rev 248917)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.h	2019-08-20 22:18:23 UTC (rev 248918)
@@ -40,7 +40,7 @@
 
 class CDMPrivateMediaSourceAVFObjC;
 
-class CDMSessionAVStreamSession : public CDMSessionMediaSourceAVFObjC, public CanMakeWeakPtr<CDMSessionAVStreamSession> {
+class CDMSessionAVStreamSession : public CDMSessionMediaSourceAVFObjC {
     WTF_MAKE_FAST_ALLOCATED;
 public:
     CDMSessionAVStreamSession(Vector<int>&& protocolVersions, CDMPrivateMediaSourceAVFObjC&, LegacyCDMSessionClient*);

Modified: trunk/Source/WebKit/ChangeLog (248917 => 248918)


--- trunk/Source/WebKit/ChangeLog	2019-08-20 21:33:06 UTC (rev 248917)
+++ trunk/Source/WebKit/ChangeLog	2019-08-20 22:18:23 UTC (rev 248918)
@@ -1,3 +1,19 @@
+2019-08-20  Chris Dumez  <cdu...@apple.com>
+
+        WebSQLiteDatabaseTracker does not ensure it is still alive after dispatching to the main thread
+        https://bugs.webkit.org/show_bug.cgi?id=200925
+
+        Reviewed by Geoffrey Garen.
+
+        WebSQLiteDatabaseTracker does not ensure it is still alive after dispatching to the main thread,
+        which is not safe. Use WeakPtr to address the issue.
+
+        * Shared/WebSQLiteDatabaseTracker.cpp:
+        (WebKit::WebSQLiteDatabaseTracker::WebSQLiteDatabaseTracker):
+        (WebKit::WebSQLiteDatabaseTracker::willBeginFirstTransaction):
+        (WebKit::WebSQLiteDatabaseTracker::didFinishLastTransaction):
+        * Shared/WebSQLiteDatabaseTracker.h:
+
 2019-08-20  Brent Fulgham  <bfulg...@apple.com>
 
         [FTW] Fix scrolling in modern WebKit views

Modified: trunk/Source/WebKit/Shared/WebSQLiteDatabaseTracker.cpp (248917 => 248918)


--- trunk/Source/WebKit/Shared/WebSQLiteDatabaseTracker.cpp	2019-08-20 21:33:06 UTC (rev 248917)
+++ trunk/Source/WebKit/Shared/WebSQLiteDatabaseTracker.cpp	2019-08-20 22:18:23 UTC (rev 248918)
@@ -60,15 +60,17 @@
 
 void WebSQLiteDatabaseTracker::willBeginFirstTransaction()
 {
-    RunLoop::main().dispatch([this] {
-        m_hysteresis.start();
+    RunLoop::main().dispatch([weakThis = makeWeakPtr(*this)] {
+        if (weakThis)
+            weakThis->m_hysteresis.start();
     });
 }
 
 void WebSQLiteDatabaseTracker::didFinishLastTransaction()
 {
-    RunLoop::main().dispatch([this] {
-        m_hysteresis.stop();
+    RunLoop::main().dispatch([weakThis = makeWeakPtr(*this)] {
+        if (weakThis)
+            weakThis->m_hysteresis.stop();
     });
 }
 

Modified: trunk/Source/WebKit/Shared/WebSQLiteDatabaseTracker.h (248917 => 248918)


--- trunk/Source/WebKit/Shared/WebSQLiteDatabaseTracker.h	2019-08-20 21:33:06 UTC (rev 248917)
+++ trunk/Source/WebKit/Shared/WebSQLiteDatabaseTracker.h	2019-08-20 22:18:23 UTC (rev 248918)
@@ -28,10 +28,12 @@
 #include <WebCore/SQLiteDatabaseTrackerClient.h>
 #include <pal/HysteresisActivity.h>
 #include <wtf/Noncopyable.h>
+#include <wtf/WeakPtr.h>
 
 namespace WebKit {
 
-class WebSQLiteDatabaseTracker final : public WebCore::SQLiteDatabaseTrackerClient {
+// Use eager initialization for the WeakPtrFactory since we call makeWeakPtr() from a non-main thread.
+class WebSQLiteDatabaseTracker final : public WebCore::SQLiteDatabaseTrackerClient, public CanMakeWeakPtr<WebSQLiteDatabaseTracker, WeakPtrFactoryInitialization::Eager> {
     WTF_MAKE_NONCOPYABLE(WebSQLiteDatabaseTracker)
 public:
     using IsHoldingLockedFilesHandler = Function<void(bool)>;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to