Title: [242004] releases/WebKitGTK/webkit-2.22
Revision
242004
Author
ape...@igalia.com
Date
2019-02-23 17:06:49 -0800 (Sat, 23 Feb 2019)

Log Message

Merged r241499 - Crash in DOMTimer::fired
https://bugs.webkit.org/show_bug.cgi?id=194638

Reviewed by Brent Fulgham.

Source/WebCore:

This patch continues the saga of hunting down timer related crashes after r239814, r225985, r227934.

The crash was caused by the bug that we don't remove a DOMTimer from NestedTimersMap if a DOMTimer
is created & installed inside another DOMTimer's callback (via execute call in DOMTimer::fired).

Fixed the crash by using a Ref in NestedTimersMap. This will keep the timer alive until we exit
from DOMTimer::fired. Because DOMTimer::fired always calls stopTracking() which clears the map
we would not leak these DOM timers.

We could, alternatively, use WeakPtr in NestedTimersMap but that would unnecessarily increase the
size of DOMTimer for a very marginal benefit of DOMTimer objcets being deleted slightly earlier.
Deleting itself in DOMTimer's destructor involves more logic & house keeping in the timer code,
and is no longer the preferred approach when dealing with these classes of bugs in WebKit.

Test: fast/dom/timer-destruction-during-firing.html

* page/DOMTimer.cpp:
(WebCore::NestedTimersMap::add):
(WebCore::DOMTimer::install):
(WebCore::DOMTimer::fired):

LayoutTests:

Added a regression test. It needs debug assertions without the fix.

* fast/dom/timer-destruction-during-firing-expected.txt: Added.
* fast/dom/timer-destruction-during-firing.html: Added.

Modified Paths

Added Paths

Diff

Modified: releases/WebKitGTK/webkit-2.22/LayoutTests/ChangeLog (242003 => 242004)


--- releases/WebKitGTK/webkit-2.22/LayoutTests/ChangeLog	2019-02-24 01:06:41 UTC (rev 242003)
+++ releases/WebKitGTK/webkit-2.22/LayoutTests/ChangeLog	2019-02-24 01:06:49 UTC (rev 242004)
@@ -1,3 +1,15 @@
+2019-02-13  Ryosuke Niwa  <rn...@webkit.org>
+
+        Crash in DOMTimer::fired
+        https://bugs.webkit.org/show_bug.cgi?id=194638
+
+        Reviewed by Brent Fulgham.
+
+        Added a regression test. It needs debug assertions without the fix.
+
+        * fast/dom/timer-destruction-during-firing-expected.txt: Added.
+        * fast/dom/timer-destruction-during-firing.html: Added.
+
 2019-02-12  Jiewen Tan  <jiewen_...@apple.com>
 
         Further restricting webarchive loads

Added: releases/WebKitGTK/webkit-2.22/LayoutTests/fast/dom/timer-destruction-during-firing-expected.txt (0 => 242004)


--- releases/WebKitGTK/webkit-2.22/LayoutTests/fast/dom/timer-destruction-during-firing-expected.txt	                        (rev 0)
+++ releases/WebKitGTK/webkit-2.22/LayoutTests/fast/dom/timer-destruction-during-firing-expected.txt	2019-02-24 01:06:49 UTC (rev 242004)
@@ -0,0 +1,3 @@
+This tests deleting DOMTimer inside another DOMTimer. WebKit should not hit any debug assertions.
+
+PASS

Added: releases/WebKitGTK/webkit-2.22/LayoutTests/fast/dom/timer-destruction-during-firing.html (0 => 242004)


--- releases/WebKitGTK/webkit-2.22/LayoutTests/fast/dom/timer-destruction-during-firing.html	                        (rev 0)
+++ releases/WebKitGTK/webkit-2.22/LayoutTests/fast/dom/timer-destruction-during-firing.html	2019-02-24 01:06:49 UTC (rev 242004)
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html>
+<body>
+<p>This tests deleting DOMTimer inside another DOMTimer. WebKit should not hit any debug assertions.</p>
+<p id="result"></p>
+<script src=""
+<script>
+
+if (!window.testRunner)
+    document.getElementById('result').textContent = 'This test requires testRunner';
+else {
+    testRunner.dumpAsText();
+    testRunner.waitUntilDone();
+
+    setTimeout(() => {
+        for (let k = 0; k < 50; k++) {
+            const frames = [];
+            for (let i = 0; i < 1; i++)
+                frames[i] = createTimerInNewFrame();
+            for (const frame of frames)
+                frame.remove();
+            frames.length = 0;
+            gc();
+        }
+        self.postMessage('end', '*');
+    }, 0);
+
+    window._onmessage_ = () => {
+        document.getElementById('result').textContent = 'PASS';
+        testRunner.notifyDone();
+    }
+}
+
+function createTimerInNewFrame()
+{
+    const frame = document.createElement('iframe');
+    document.body.appendChild(frame);
+    frame.contentWindow.setTimeout(() => {}, 0);
+    return frame;
+}
+
+</script>
+</body>
+</html>

Modified: releases/WebKitGTK/webkit-2.22/Source/WebCore/ChangeLog (242003 => 242004)


--- releases/WebKitGTK/webkit-2.22/Source/WebCore/ChangeLog	2019-02-24 01:06:41 UTC (rev 242003)
+++ releases/WebKitGTK/webkit-2.22/Source/WebCore/ChangeLog	2019-02-24 01:06:49 UTC (rev 242004)
@@ -1,3 +1,31 @@
+2019-02-13  Ryosuke Niwa  <rn...@webkit.org>
+
+        Crash in DOMTimer::fired
+        https://bugs.webkit.org/show_bug.cgi?id=194638
+
+        Reviewed by Brent Fulgham.
+
+        This patch continues the saga of hunting down timer related crashes after r239814, r225985, r227934.
+
+        The crash was caused by the bug that we don't remove a DOMTimer from NestedTimersMap if a DOMTimer
+        is created & installed inside another DOMTimer's callback (via execute call in DOMTimer::fired).
+
+        Fixed the crash by using a Ref in NestedTimersMap. This will keep the timer alive until we exit
+        from DOMTimer::fired. Because DOMTimer::fired always calls stopTracking() which clears the map
+        we would not leak these DOM timers.
+
+        We could, alternatively, use WeakPtr in NestedTimersMap but that would unnecessarily increase the
+        size of DOMTimer for a very marginal benefit of DOMTimer objcets being deleted slightly earlier.
+        Deleting itself in DOMTimer's destructor involves more logic & house keeping in the timer code,
+        and is no longer the preferred approach when dealing with these classes of bugs in WebKit.
+
+        Test: fast/dom/timer-destruction-during-firing.html
+
+        * page/DOMTimer.cpp:
+        (WebCore::NestedTimersMap::add):
+        (WebCore::DOMTimer::install):
+        (WebCore::DOMTimer::fired):
+
 2018-01-31  Ryosuke Niwa  <rn...@webkit.org>
 
         Add a release assertion to ensure timers are deleted in the right thread

Modified: releases/WebKitGTK/webkit-2.22/Source/WebCore/page/DOMTimer.cpp (242003 => 242004)


--- releases/WebKitGTK/webkit-2.22/Source/WebCore/page/DOMTimer.cpp	2019-02-24 01:06:41 UTC (rev 242003)
+++ releases/WebKitGTK/webkit-2.22/Source/WebCore/page/DOMTimer.cpp	2019-02-24 01:06:49 UTC (rev 242004)
@@ -113,7 +113,7 @@
 DOMTimerFireState* DOMTimerFireState::current = nullptr;
 
 struct NestedTimersMap {
-    typedef HashMap<int, DOMTimer*>::const_iterator const_iterator;
+    typedef HashMap<int, Ref<DOMTimer>>::const_iterator const_iterator;
 
     static NestedTimersMap* instanceForContext(ScriptExecutionContext& context)
     {
@@ -139,10 +139,10 @@
         nestedTimers.clear();
     }
 
-    void add(int timeoutId, DOMTimer* timer)
+    void add(int timeoutId, Ref<DOMTimer>&& timer)
     {
         if (isTrackingNestedTimers)
-            nestedTimers.add(timeoutId, timer);
+            nestedTimers.add(timeoutId, WTFMove(timer));
     }
 
     void remove(int timeoutId)
@@ -162,7 +162,7 @@
     }
 
     static bool isTrackingNestedTimers;
-    HashMap<int /* timeoutId */, DOMTimer*> nestedTimers;
+    HashMap<int /* timeoutId */, Ref<DOMTimer>> nestedTimers;
 };
 
 bool NestedTimersMap::isTrackingNestedTimers = false;
@@ -234,7 +234,7 @@
 
     // Keep track of nested timer installs.
     if (NestedTimersMap* nestedTimers = NestedTimersMap::instanceForContext(context))
-        nestedTimers->add(timer->m_timeoutId, timer);
+        nestedTimers->add(timer->m_timeoutId, *timer);
 
     return timer->m_timeoutId;
 }
@@ -380,8 +380,8 @@
 
     // Check if we should throttle nested single-shot timers.
     if (nestedTimers) {
-        for (auto& keyValue : *nestedTimers) {
-            auto* timer = keyValue.value;
+        for (auto& idAndTimer : *nestedTimers) {
+            auto& timer = idAndTimer.value;
             if (timer->isActive() && !timer->repeatInterval())
                 timer->updateThrottlingStateIfNecessary(fireState);
         }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to