Title: [250762] trunk/Source/WTF
Revision
250762
Author
[email protected]
Date
2019-10-04 18:59:35 -0700 (Fri, 04 Oct 2019)

Log Message

Make a ThreadSafeRefCounted object safe to ref & deref inside its destructor
https://bugs.webkit.org/show_bug.cgi?id=201576

Reviewed by Geoffrey Garen.

This patch leaves m_refCount 1 inside the last deref call to ThreadSafeRefCounted
such that ref'ing and deref'ing it again inside its destructor would never try
to double delete the object.

Also added m_deletionHasBegun like RefCounted.

* wtf/ThreadSafeRefCounted.h:
(WTF::ThreadSafeRefCountedBase::ref const):
(WTF::ThreadSafeRefCountedBase::hasOneRef const):
(WTF::ThreadSafeRefCountedBase::derefBase const):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (250761 => 250762)


--- trunk/Source/WTF/ChangeLog	2019-10-05 01:38:21 UTC (rev 250761)
+++ trunk/Source/WTF/ChangeLog	2019-10-05 01:59:35 UTC (rev 250762)
@@ -1,3 +1,21 @@
+2019-10-04  Ryosuke Niwa  <[email protected]>
+
+        Make a ThreadSafeRefCounted object safe to ref & deref inside its destructor
+        https://bugs.webkit.org/show_bug.cgi?id=201576
+
+        Reviewed by Geoffrey Garen.
+
+        This patch leaves m_refCount 1 inside the last deref call to ThreadSafeRefCounted
+        such that ref'ing and deref'ing it again inside its destructor would never try
+        to double delete the object.
+
+        Also added m_deletionHasBegun like RefCounted.
+
+        * wtf/ThreadSafeRefCounted.h:
+        (WTF::ThreadSafeRefCountedBase::ref const):
+        (WTF::ThreadSafeRefCountedBase::hasOneRef const):
+        (WTF::ThreadSafeRefCountedBase::derefBase const):
+
 2019-10-04  Heiko Becker  <[email protected]>
 
         Fix build with icu 65.1

Modified: trunk/Source/WTF/wtf/ThreadSafeRefCounted.h (250761 => 250762)


--- trunk/Source/WTF/wtf/ThreadSafeRefCounted.h	2019-10-05 01:38:21 UTC (rev 250761)
+++ trunk/Source/WTF/wtf/ThreadSafeRefCounted.h	2019-10-05 01:59:35 UTC (rev 250762)
@@ -32,6 +32,12 @@
 
 namespace WTF {
 
+#if defined(NDEBUG) && !ENABLE(SECURITY_ASSERTIONS)
+#define CHECK_THREAD_SAFE_REF_COUNTED_LIFECYCLE 0
+#else
+#define CHECK_THREAD_SAFE_REF_COUNTED_LIFECYCLE 1
+#endif
+
 class ThreadSafeRefCountedBase {
     WTF_MAKE_NONCOPYABLE(ThreadSafeRefCountedBase);
     WTF_MAKE_FAST_ALLOCATED;
@@ -38,13 +44,27 @@
 public:
     ThreadSafeRefCountedBase() = default;
 
+#if CHECK_THREAD_SAFE_REF_COUNTED_LIFECYCLE
+    ~ThreadSafeRefCountedBase()
+    {
+        // When this ThreadSafeRefCounted object is a part of another object, derefBase() is never called on this object.
+        m_deletionHasBegun = true;
+    }
+#endif
+
     void ref() const
     {
+#if CHECK_THREAD_SAFE_REF_COUNTED_LIFECYCLE
+        ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun);
+#endif
         ++m_refCount;
     }
 
     bool hasOneRef() const
     {
+#if CHECK_THREAD_SAFE_REF_COUNTED_LIFECYCLE
+        ASSERT(!m_deletionHasBegun);
+#endif
         return refCount() == 1;
     }
 
@@ -57,11 +77,31 @@
     // Returns whether the pointer should be freed or not.
     bool derefBase() const
     {
-        return !--m_refCount;
+        ASSERT(m_refCount);
+
+#if CHECK_THREAD_SAFE_REF_COUNTED_LIFECYCLE
+        ASSERT_WITH_SECURITY_IMPLICATION(!m_deletionHasBegun);
+#endif
+
+        if (UNLIKELY(!--m_refCount)) {
+            // Setting m_refCount to 1 here prevents double delete within the destructor but not from another thread
+            // since such a thread could have ref'ed this object long after it had been deleted. See webkit.org/b/201576.
+            m_refCount = 1;
+#if CHECK_THREAD_SAFE_REF_COUNTED_LIFECYCLE
+            m_deletionHasBegun = true;
+#endif
+            return true;
+        }
+
+        return false;
     }
 
 private:
     mutable std::atomic<unsigned> m_refCount { 1 };
+
+#if CHECK_THREAD_SAFE_REF_COUNTED_LIFECYCLE
+    mutable std::atomic<bool> m_deletionHasBegun { false };
+#endif
 };
 
 enum class DestructionThread { Any, Main, MainRunLoop };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to