Title: [234109] trunk
Revision
234109
Author
[email protected]
Date
2018-07-23 13:23:18 -0700 (Mon, 23 Jul 2018)

Log Message

[Web Animations] Querying the current time of a finished CSSAnimation after removing its target leads to a crash
https://bugs.webkit.org/show_bug.cgi?id=187906

Reviewed by Dean Jackson.

Source/WebCore:

Test: webanimations/accessing-current-time-after-finished-css-animation-target-removal.html

Because we carelessly look at a CSSAnimation's effect's timing in DeclarativeAnimation::bindingsCurrentTime
without checking that the effect is non-null, we can crash in the case where the animation is finished and
its target element has been removed, which caused the effect to be set to null.

We do not actually fix the lack of a null check, which will be the scope of a different patch, but instead
ensure that we do _not_ set the animation's effect to null when its target is removed, which used to be
performed via a call to WebAnimation::remove(). Instead, we introduce AnimationTimeline::elementWasRemoved()
which notifies the timeline of an element being removed such that we may stop referencing any animation
targeting this element from the various data structures holding strong references to the animation in question,
and we then cancel the animation silently, which is a new option that ensures promises aren't resolved or
rejected as a result.

Finally, the WebAnimation and AnimationEffectReadOnly classes established a ref-cycle as WebAnimation has
`RefPtr<AnimationEffectReadOnly> m_effect` and AnimationEffectReadOnly has `RefPtr<WebAnimation> m_animation`.
While it is correct that WebAnimation owns its effect, which is established by the DOM API, the
reverse is not correct since we only hold the reverse internally for the benefit of our implementation.
As such, we change AnimationEffectReadOnly's m_animation to be a WeakPtr<WebAnimation>. This means not
calling WebAnimation::remove() and simply removing the animation from the animation maps on the timeline
is sufficient to guarantee that the document timeline will not leak (and with it the document).

* animation/AnimationEffectReadOnly.h:
(WebCore::AnimationEffectReadOnly::setAnimation):
* animation/AnimationTimeline.cpp:
(WebCore::AnimationTimeline::elementWasRemoved):
* animation/AnimationTimeline.h:
* animation/WebAnimation.cpp:
(WebCore::WebAnimation::cancel):
(WebCore::WebAnimation::resetPendingTasks):
* animation/WebAnimation.h:
* dom/Element.cpp:
(WebCore::Element::removedFromAncestor):
* rendering/updating/RenderTreeUpdater.cpp:
(WebCore::RenderTreeUpdater::tearDownRenderers):

LayoutTests:

Add a new test that checks the behavior of a CSSAnimation instance after its completion and removal of its target.

* webanimations/accessing-current-time-after-finished-css-animation-target-removal-expected.txt: Added.
* webanimations/accessing-current-time-after-finished-css-animation-target-removal.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (234108 => 234109)


--- trunk/LayoutTests/ChangeLog	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/LayoutTests/ChangeLog	2018-07-23 20:23:18 UTC (rev 234109)
@@ -1,3 +1,15 @@
+2018-07-23  Antoine Quint  <[email protected]>
+
+        [Web Animations] Querying the current time of a finished CSSAnimation after removing its target leads to a crash
+        https://bugs.webkit.org/show_bug.cgi?id=187906
+
+        Reviewed by Dean Jackson.
+
+        Add a new test that checks the behavior of a CSSAnimation instance after its completion and removal of its target.
+
+        * webanimations/accessing-current-time-after-finished-css-animation-target-removal-expected.txt: Added.
+        * webanimations/accessing-current-time-after-finished-css-animation-target-removal.html: Added.
+
 2018-07-23  Chris Dumez  <[email protected]>
 
         REGRESSION(PSON?): [ WK2 ] http/tests/workers/service/client-*-page-cache.html LayoutTests are flaky

Added: trunk/LayoutTests/webanimations/accessing-current-time-after-finished-css-animation-target-removal-expected.txt (0 => 234109)


--- trunk/LayoutTests/webanimations/accessing-current-time-after-finished-css-animation-target-removal-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/webanimations/accessing-current-time-after-finished-css-animation-target-removal-expected.txt	2018-07-23 20:23:18 UTC (rev 234109)
@@ -0,0 +1,3 @@
+
+PASS Current time is null when removing an animation's target after completion. 
+

Added: trunk/LayoutTests/webanimations/accessing-current-time-after-finished-css-animation-target-removal.html (0 => 234109)


--- trunk/LayoutTests/webanimations/accessing-current-time-after-finished-css-animation-target-removal.html	                        (rev 0)
+++ trunk/LayoutTests/webanimations/accessing-current-time-after-finished-css-animation-target-removal.html	2018-07-23 20:23:18 UTC (rev 234109)
@@ -0,0 +1,37 @@
+<!DOCTYPE html><!-- webkit-test-runner [ enableWebAnimationsCSSIntegration=true ] -->
+<meta charset=utf-8>
+<title>Crash accessing a CSSAnimation's current time after removal of its target</title>
+<style>
+    @keyframes animation {
+        from {
+            margin-left: 0px;
+        }
+        to {
+            margin-left: 100px;
+        }
+    }
+</style>
+<body>
+<script src=""
+<script src=""
+<script>
+
+'use strict';
+
+test(t => {
+    const target = document.body.appendChild(document.createElement("div"));
+    target.style.animation = "animation 100ms";
+
+    const animations = target.getAnimations();
+    assert_equals(animations.length, 1, "The target element has one animation.");
+
+    const animation = animations[0];
+    animation.finish();
+
+    assert_equals(animation.currentTime, 100, "The animation's current time is 100 after animation completes.");
+    target.remove();
+    assert_equals(animation.currentTime, null, "The animation's current time is null after removing its target element.");
+}, "Current time is null when removing an animation's target after completion.");
+
+</script>
+</body>
\ No newline at end of file

Modified: trunk/Source/WebCore/ChangeLog (234108 => 234109)


--- trunk/Source/WebCore/ChangeLog	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/Source/WebCore/ChangeLog	2018-07-23 20:23:18 UTC (rev 234109)
@@ -1,3 +1,46 @@
+2018-07-23  Antoine Quint  <[email protected]>
+
+        [Web Animations] Querying the current time of a finished CSSAnimation after removing its target leads to a crash
+        https://bugs.webkit.org/show_bug.cgi?id=187906
+
+        Reviewed by Dean Jackson.
+
+        Test: webanimations/accessing-current-time-after-finished-css-animation-target-removal.html
+
+        Because we carelessly look at a CSSAnimation's effect's timing in DeclarativeAnimation::bindingsCurrentTime
+        without checking that the effect is non-null, we can crash in the case where the animation is finished and
+        its target element has been removed, which caused the effect to be set to null.
+
+        We do not actually fix the lack of a null check, which will be the scope of a different patch, but instead
+        ensure that we do _not_ set the animation's effect to null when its target is removed, which used to be
+        performed via a call to WebAnimation::remove(). Instead, we introduce AnimationTimeline::elementWasRemoved()
+        which notifies the timeline of an element being removed such that we may stop referencing any animation
+        targeting this element from the various data structures holding strong references to the animation in question,
+        and we then cancel the animation silently, which is a new option that ensures promises aren't resolved or
+        rejected as a result.
+
+        Finally, the WebAnimation and AnimationEffectReadOnly classes established a ref-cycle as WebAnimation has
+        `RefPtr<AnimationEffectReadOnly> m_effect` and AnimationEffectReadOnly has `RefPtr<WebAnimation> m_animation`.
+        While it is correct that WebAnimation owns its effect, which is established by the DOM API, the
+        reverse is not correct since we only hold the reverse internally for the benefit of our implementation.
+        As such, we change AnimationEffectReadOnly's m_animation to be a WeakPtr<WebAnimation>. This means not
+        calling WebAnimation::remove() and simply removing the animation from the animation maps on the timeline
+        is sufficient to guarantee that the document timeline will not leak (and with it the document).
+
+        * animation/AnimationEffectReadOnly.h:
+        (WebCore::AnimationEffectReadOnly::setAnimation):
+        * animation/AnimationTimeline.cpp:
+        (WebCore::AnimationTimeline::elementWasRemoved):
+        * animation/AnimationTimeline.h:
+        * animation/WebAnimation.cpp:
+        (WebCore::WebAnimation::cancel):
+        (WebCore::WebAnimation::resetPendingTasks):
+        * animation/WebAnimation.h:
+        * dom/Element.cpp:
+        (WebCore::Element::removedFromAncestor):
+        * rendering/updating/RenderTreeUpdater.cpp:
+        (WebCore::RenderTreeUpdater::tearDownRenderers):
+
 2018-07-23  Aditya Keerthi  <[email protected]>
 
         [iOS] Add support for input[type=color]

Modified: trunk/Source/WebCore/animation/AnimationEffectReadOnly.h (234108 => 234109)


--- trunk/Source/WebCore/animation/AnimationEffectReadOnly.h	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/Source/WebCore/animation/AnimationEffectReadOnly.h	2018-07-23 20:23:18 UTC (rev 234109)
@@ -31,6 +31,7 @@
 #include <wtf/Ref.h>
 #include <wtf/RefCounted.h>
 #include <wtf/RefPtr.h>
+#include <wtf/WeakPtr.h>
 
 namespace WebCore {
 
@@ -50,7 +51,7 @@
     virtual void animationSuspensionStateDidChange(bool) = 0;
 
     WebAnimation* animation() const { return m_animation.get(); }
-    void setAnimation(RefPtr<WebAnimation>&& animation) { m_animation = animation; }
+    void setAnimation(WebAnimation* animation) { m_animation = makeWeakPtr(animation); }
 
     std::optional<Seconds> localTime() const;
     std::optional<Seconds> activeTime() const;
@@ -83,7 +84,7 @@
     std::optional<double> directedProgress() const;
     std::optional<double> transformedProgress() const;
 
-    RefPtr<WebAnimation> m_animation;
+    WeakPtr<WebAnimation> m_animation;
     RefPtr<AnimationEffectTimingReadOnly> m_timing;
 };
 

Modified: trunk/Source/WebCore/animation/AnimationTimeline.cpp (234108 => 234109)


--- trunk/Source/WebCore/animation/AnimationTimeline.cpp	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/Source/WebCore/animation/AnimationTimeline.cpp	2018-07-23 20:23:18 UTC (rev 234109)
@@ -182,6 +182,15 @@
     return animations;
 }
 
+void AnimationTimeline::elementWasRemoved(Element& element)
+{
+    for (auto& animation : animationsForElement(element)) {
+        animationWasRemovedFromElement(*animation, element);
+        removeAnimation(*animation);
+        animation->cancel(WebAnimation::Silently::Yes);
+    }
+}
+
 void AnimationTimeline::removeAnimationsForElement(Element& element)
 {
     for (auto& animation : animationsForElement(element))

Modified: trunk/Source/WebCore/animation/AnimationTimeline.h (234108 => 234109)


--- trunk/Source/WebCore/animation/AnimationTimeline.h	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/Source/WebCore/animation/AnimationTimeline.h	2018-07-23 20:23:18 UTC (rev 234109)
@@ -60,6 +60,7 @@
     enum class Ordering { Sorted, Unsorted };
     const ListHashSet<RefPtr<WebAnimation>>& animations() const { return m_animations; }
     Vector<RefPtr<WebAnimation>> animationsForElement(Element&, Ordering ordering = Ordering::Unsorted) const;
+    void elementWasRemoved(Element&);
     void removeAnimationsForElement(Element&);
     void cancelDeclarativeAnimationsForElement(Element&);
     void animationWasAddedToElement(WebAnimation&, Element&);

Modified: trunk/Source/WebCore/animation/WebAnimation.cpp (234108 => 234109)


--- trunk/Source/WebCore/animation/WebAnimation.cpp	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/Source/WebCore/animation/WebAnimation.cpp	2018-07-23 20:23:18 UTC (rev 234109)
@@ -509,6 +509,11 @@
 
 void WebAnimation::cancel()
 {
+    cancel(Silently::No);
+}
+
+void WebAnimation::cancel(Silently silently)
+{
     // 3.4.16. Canceling an animation
     // https://drafts.csswg.org/web-animations-1/#canceling-an-animation-section
     //
@@ -519,10 +524,10 @@
     // 1. If animation's play state is not idle, perform the following steps:
     if (playState() != PlayState::Idle) {
         // 1. Run the procedure to reset an animation's pending tasks on animation.
-        resetPendingTasks();
+        resetPendingTasks(silently);
 
         // 2. Reject the current finished promise with a DOMException named "AbortError".
-        if (!m_finishedPromise->isFulfilled())
+        if (silently == Silently::No && !m_finishedPromise->isFulfilled())
             m_finishedPromise->reject(Exception { AbortError });
 
         // 3. Let current finished promise be a new (pending) Promise object.
@@ -539,7 +544,8 @@
         //    to origin-relative time, let the scheduled event time be the result of applying that procedure to timeline time. Otherwise, the
         //    scheduled event time is an unresolved time value.
         // Otherwise, queue a task to dispatch cancelEvent at animation. The task source for this task is the DOM manipulation task source.
-        enqueueAnimationPlaybackEvent(eventNames().cancelEvent, std::nullopt, m_timeline ? m_timeline->currentTime() : std::nullopt);
+        if (silently == Silently::No)
+            enqueueAnimationPlaybackEvent(eventNames().cancelEvent, std::nullopt, m_timeline ? m_timeline->currentTime() : std::nullopt);
     }
 
     // 2. Make animation's hold time unresolved.
@@ -569,7 +575,7 @@
     }
 }
 
-void WebAnimation::resetPendingTasks()
+void WebAnimation::resetPendingTasks(Silently silently)
 {
     // The procedure to reset an animation's pending tasks for animation is as follows:
     // https://drafts.csswg.org/web-animations-1/#reset-an-animations-pending-tasks
@@ -587,7 +593,8 @@
         setTimeToRunPendingPauseTask(TimeToRunPendingTask::NotScheduled);
 
     // 4. Reject animation's current ready promise with a DOMException named "AbortError".
-    m_readyPromise->reject(Exception { AbortError });
+    if (silently == Silently::No)
+        m_readyPromise->reject(Exception { AbortError });
 
     // 5. Let animation's current ready promise be the result of creating a new resolved Promise object.
     m_readyPromise = makeUniqueRef<ReadyPromise>(*this, &WebAnimation::readyPromiseResolve);

Modified: trunk/Source/WebCore/animation/WebAnimation.h (234108 => 234109)


--- trunk/Source/WebCore/animation/WebAnimation.h	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/Source/WebCore/animation/WebAnimation.h	2018-07-23 20:23:18 UTC (rev 234109)
@@ -36,6 +36,7 @@
 #include <wtf/RefPtr.h>
 #include <wtf/Seconds.h>
 #include <wtf/UniqueRef.h>
+#include <wtf/WeakPtr.h>
 
 namespace WebCore {
 
@@ -46,7 +47,7 @@
 class Element;
 class RenderStyle;
 
-class WebAnimation : public RefCounted<WebAnimation>, public EventTargetWithInlineData, public ActiveDOMObject {
+class WebAnimation : public RefCounted<WebAnimation>, public CanMakeWeakPtr<WebAnimation>, public EventTargetWithInlineData, public ActiveDOMObject {
 public:
     static Ref<WebAnimation> create(Document&, AnimationEffectReadOnly*);
     static Ref<WebAnimation> create(Document&, AnimationEffectReadOnly*, AnimationTimeline*);
@@ -88,6 +89,7 @@
     FinishedPromise& finished() { return m_finishedPromise.get(); }
 
     virtual void cancel();
+    void cancel(Silently);
     ExceptionOr<void> finish();
     ExceptionOr<void> play();
     ExceptionOr<void> pause();
@@ -157,7 +159,7 @@
     ExceptionOr<void> play(AutoRewind);
     void runPendingPauseTask();
     void runPendingPlayTask();
-    void resetPendingTasks();
+    void resetPendingTasks(Silently = Silently::No);
     void setEffectInternal(RefPtr<AnimationEffectReadOnly>&&, bool = false);
     void setTimelineInternal(RefPtr<AnimationTimeline>&&);
 

Modified: trunk/Source/WebCore/dom/Element.cpp (234108 => 234109)


--- trunk/Source/WebCore/dom/Element.cpp	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/Source/WebCore/dom/Element.cpp	2018-07-23 20:23:18 UTC (rev 234109)
@@ -1827,7 +1827,7 @@
     RefPtr<Frame> frame = document().frame();
     if (RuntimeEnabledFeatures::sharedFeatures().webAnimationsCSSIntegrationEnabled()) {
         if (auto* timeline = document().existingTimeline())
-            timeline->removeAnimationsForElement(*this);
+            timeline->elementWasRemoved(*this);
     } else if (frame)
         frame->animation().cancelAnimations(*this);
 

Modified: trunk/Source/WebCore/rendering/updating/RenderTreeUpdater.cpp (234108 => 234109)


--- trunk/Source/WebCore/rendering/updating/RenderTreeUpdater.cpp	2018-07-23 20:17:42 UTC (rev 234108)
+++ trunk/Source/WebCore/rendering/updating/RenderTreeUpdater.cpp	2018-07-23 20:23:18 UTC (rev 234109)
@@ -556,7 +556,7 @@
                 if (RuntimeEnabledFeatures::sharedFeatures().webAnimationsCSSIntegrationEnabled()) {
                     if (timeline) {
                         if (document.renderTreeBeingDestroyed())
-                            timeline->removeAnimationsForElement(element);
+                            timeline->elementWasRemoved(element);
                         else if (teardownType == TeardownType::RendererUpdateCancelingAnimations)
                             timeline->cancelDeclarativeAnimationsForElement(element);
                     }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to