Title: [275543] trunk
Revision
275543
Author
[email protected]
Date
2021-04-06 12:51:49 -0700 (Tue, 06 Apr 2021)

Log Message

Assert failure in isCloneInShadowTreeOfSVGUseElement
https://bugs.webkit.org/show_bug.cgi?id=224174

Reviewed by Darin Adler and Antti Koivisto.

Source/WebCore:

The bug was caused by two related but distinct issues:
1. An element can have an instance that had been removed from a use element's shadow tree
   but not yet deleted. Because SVGElement clears its correspondingElement in its destructor,
   when addEventListener is called on such an element, it can try to add an event listener
   on this instance which is in the process of getting disposed.
2. DOM mutation events can be fired on the corresponding element of an instance inside
   a use element’s shadow tree with EventQueueScope in the stack when the event is schedueld
   via Node::dispatchScopedEvent, e.g. because use element's shadow tree was updated during
   a style update at the beginning of document.execComand. Because SVGUseElement::cloneTarget
   constructs the shadow tree by cloning the original tree while it's disconnected from the
   document, Node::dispatchSubtreeModifiedEvent sees isInShadowTree() to be false and happily
   tries to dispach DOMSubtreeModified event using Node::dispatchScopedEvent. This works fine
   when the event is dispatched synchronously since these elements had never been exposed to
   any scripts yet and they are still disconnected so no scripts have had an opportunity to
   attach an event listener. But when EventQueueScope in the stack, Node::dispatchScopedEvent
   will queue up the event and fire it later when those instance elements had been inserted
   into use element's shadow tree.

This patch addresses (1) by severing correspondingElement relationship as soon as an instance
is removed from its use element's shadow tree, and (2) by not dispatching a scheduled mutation
event if the target is inside a shadow tree. Note that this patch also addresses (2) for
a regular shadow tree attached by author scripts.

Tests: fast/shadow-dom/mutation-event-in-shadow-tree.html
       svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand.html
       svg/dom/update-svg-use-shadow-tree-with-execCommand.html

* dom/ScopedEventQueue.cpp:
(WebCore::ScopedEventQueue::dispatchEvent const):
* svg/SVGElement.cpp:
(WebCore::SVGElement::~SVGElement):
(WebCore::SVGElement::removedFromAncestor):
(WebCore::SVGElement::addEventListener):
(WebCore::SVGElement::removeEventListener):

LayoutTests:

Added tests for mutating nodes which is later inserted into a shadow tree during execCommand
as well as forcing a SVG use element to update its shadow tree by mutating the corresponding
element tree during execCommand.

* fast/shadow-dom/mutation-event-in-shadow-tree-expected.txt: Added.
* fast/shadow-dom/mutation-event-in-shadow-tree.html: Added.
* svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand-expected.txt: Added.
* svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand.html: Added.
* svg/dom/update-svg-use-shadow-tree-with-execCommand-expected.txt: Added.
* svg/dom/update-svg-use-shadow-tree-with-execCommand.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (275542 => 275543)


--- trunk/LayoutTests/ChangeLog	2021-04-06 19:47:47 UTC (rev 275542)
+++ trunk/LayoutTests/ChangeLog	2021-04-06 19:51:49 UTC (rev 275543)
@@ -1,3 +1,21 @@
+2021-04-06  Ryosuke Niwa  <[email protected]>
+
+        Assert failure in isCloneInShadowTreeOfSVGUseElement
+        https://bugs.webkit.org/show_bug.cgi?id=224174
+
+        Reviewed by Darin Adler and Antti Koivisto.
+
+        Added tests for mutating nodes which is later inserted into a shadow tree during execCommand
+        as well as forcing a SVG use element to update its shadow tree by mutating the corresponding
+        element tree during execCommand.
+
+        * fast/shadow-dom/mutation-event-in-shadow-tree-expected.txt: Added.
+        * fast/shadow-dom/mutation-event-in-shadow-tree.html: Added.
+        * svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand-expected.txt: Added.
+        * svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand.html: Added.
+        * svg/dom/update-svg-use-shadow-tree-with-execCommand-expected.txt: Added.
+        * svg/dom/update-svg-use-shadow-tree-with-execCommand.html: Added.
+
 2021-04-06  Jiewen Tan  <[email protected]>
 
         WebCrypto in Safari will not AES-GCM encrypt 0 bytes

Added: trunk/LayoutTests/fast/shadow-dom/mutation-event-in-shadow-tree-expected.txt (0 => 275543)


--- trunk/LayoutTests/fast/shadow-dom/mutation-event-in-shadow-tree-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/shadow-dom/mutation-event-in-shadow-tree-expected.txt	2021-04-06 19:51:49 UTC (rev 275543)
@@ -0,0 +1,13 @@
+This tests mutating shadow tree inside execCommand. WebKit should not dispach DOM mutation events.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS mutationEventsWith((element) => element.setAttribute('class', 'foo')) is "[]"
+PASS mutationEventsWith((element) => element.appendChild(document.createElement('span'))) is "[]"
+PASS mutationEventsWith((element) => { element.textContent = 'hello'; element.firstChild.appendData(' world') }) is "[]"
+PASS mutationEventsWith((element) => element.appendChild(element.appendChild(document.createElement('span')))) is "[]"
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/shadow-dom/mutation-event-in-shadow-tree.html (0 => 275543)


--- trunk/LayoutTests/fast/shadow-dom/mutation-event-in-shadow-tree.html	                        (rev 0)
+++ trunk/LayoutTests/fast/shadow-dom/mutation-event-in-shadow-tree.html	2021-04-06 19:51:49 UTC (rev 275543)
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html>
+<body>
+<script src=""
+<script>
+
+description('This tests mutating shadow tree inside execCommand. WebKit should not dispach DOM mutation events.');
+
+function mutationEventsWith(testFunction)
+{
+    const host = document.createElement('div');
+    document.body.appendChild(host);
+    const shadowRoot = host.attachShadow({mode: 'closed'});
+    shadowRoot.innerHTML = '<div id="editor" contenteditable>hello <span></span>world</div>';
+    const editor = shadowRoot.getElementById('editor');
+    const iframe = shadowRoot.querySelector('span').appendChild(document.createElement('iframe'));
+
+    editor.focus();
+    document.execCommand('selectAll');
+
+    const events = [];
+    const eventTypes = ['DOMSubtreeModified', 'DOMNodeInserted', 'DOMNodeInsertedIntoDocument', 'DOMNodeRemoved', 'DOMNodeRemovedFromDocument', 'DOMCharacterDataModified'];
+    iframe.contentWindow._onunload_ = () => {
+        const div = document.createElement('div');
+        for (const type of eventTypes)
+            div.addEventListener(type, (event) => { events.push(event.type); }, {once: true});
+        testFunction(div);
+        shadowRoot.appendChild(div);
+    };
+
+    document.execCommand('delete', false, null);
+    host.remove();
+
+    return JSON.stringify(events);
+}
+
+shouldBeEqualToString(`mutationEventsWith((element) => element.setAttribute('class', 'foo'))`, `[]`);
+shouldBeEqualToString(`mutationEventsWith((element) => element.appendChild(document.createElement('span')))`, `[]`);
+shouldBeEqualToString(`mutationEventsWith((element) => { element.textContent = 'hello'; element.firstChild.appendData(' world') })`, `[]`);
+shouldBeEqualToString(`mutationEventsWith((element) => element.appendChild(element.appendChild(document.createElement('span'))))`, `[]`);
+
+</script>
+</body>
+</html>

Added: trunk/LayoutTests/svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand-expected.txt (0 => 275543)


--- trunk/LayoutTests/svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand-expected.txt	2021-04-06 19:51:49 UTC (rev 275543)
@@ -0,0 +1,4 @@
+This tests mutating SVG symbol element's subtree referenced by a SVG use element inside a event queue scope.
+WebKit should not hit any assertions or crash.
+
+PASS.

Added: trunk/LayoutTests/svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand.html (0 => 275543)


--- trunk/LayoutTests/svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand.html	                        (rev 0)
+++ trunk/LayoutTests/svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand.html	2021-04-06 19:51:49 UTC (rev 275543)
@@ -0,0 +1,29 @@
+<script>
+
+if (window.testRunner)
+    testRunner.dumpAsText();
+
+function nestedSubtreeMutations(...callbacks) {
+    const nextCallback = callbacks.shift();
+    if (!nextCallback)
+        return;
+    g.addEventListener("DOMSubtreeModified", () => {
+        nestedSubtreeMutations(...callbacks);
+    }, {once: true});
+    nextCallback();
+}
+
+_onload_ = () => {
+    nestedSubtreeMutations(
+        () => g.setAttribute("stroke", "blue"),
+        () => document.execCommand("usecss", false),
+        () => tspan.appendChild(document.createElement('rect')),
+        () => document.execCommand("usecss", false),
+        () => g.setAttribute("onclick", "var00063"));
+    document.body.innerText = `This tests mutating SVG symbol element's subtree referenced by a SVG use element inside a event queue scope.
+WebKit should not hit any assertions or crash.
+
+PASS.`;
+}
+</script>
+<svg><symbol id="symbol1"><g id="g"><tspan id="tspan"/></symbol><use href="" />

Added: trunk/LayoutTests/svg/dom/update-svg-use-shadow-tree-with-execCommand-expected.txt (0 => 275543)


--- trunk/LayoutTests/svg/dom/update-svg-use-shadow-tree-with-execCommand-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/svg/dom/update-svg-use-shadow-tree-with-execCommand-expected.txt	2021-04-06 19:51:49 UTC (rev 275543)
@@ -0,0 +1,13 @@
+This tests updating the SVG use element's shadow tree inside a event queue scope.
+WebKit should not dispach any mutation events
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS subtreeModifiedFired is false
+document.execCommand("usecss", false)
+PASS subtreeModifiedFired is false
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/svg/dom/update-svg-use-shadow-tree-with-execCommand.html (0 => 275543)


--- trunk/LayoutTests/svg/dom/update-svg-use-shadow-tree-with-execCommand.html	                        (rev 0)
+++ trunk/LayoutTests/svg/dom/update-svg-use-shadow-tree-with-execCommand.html	2021-04-06 19:51:49 UTC (rev 275543)
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html>
+<body>
+<svg><symbol id="symbol1"><g id="g"><tspan id="tspan"/></symbol><use id="use" href="" /></svg>
+<script src=""
+<script>
+
+description(`This tests updating the SVG use element's shadow tree inside a event queue scope.<br>
+WebKit should not dispach any mutation events`);
+
+g.setAttribute("stroke", "blue");
+let subtreeModifiedFired = false;
+g.addEventListener("DOMSubtreeModified", () => subtreeModifiedFired = true, {once: true});
+
+shouldBeFalse('subtreeModifiedFired');
+evalAndLog('document.execCommand("usecss", false)');
+shouldBeFalse('subtreeModifiedFired');
+
+successfullyParsed = true;
+
+</script>
+</body>
+</html>

Modified: trunk/Source/WebCore/ChangeLog (275542 => 275543)


--- trunk/Source/WebCore/ChangeLog	2021-04-06 19:47:47 UTC (rev 275542)
+++ trunk/Source/WebCore/ChangeLog	2021-04-06 19:51:49 UTC (rev 275543)
@@ -1,3 +1,45 @@
+2021-04-06  Ryosuke Niwa  <[email protected]>
+
+        Assert failure in isCloneInShadowTreeOfSVGUseElement
+        https://bugs.webkit.org/show_bug.cgi?id=224174
+
+        Reviewed by Darin Adler and Antti Koivisto.
+
+        The bug was caused by two related but distinct issues:
+        1. An element can have an instance that had been removed from a use element's shadow tree
+           but not yet deleted. Because SVGElement clears its correspondingElement in its destructor,
+           when addEventListener is called on such an element, it can try to add an event listener
+           on this instance which is in the process of getting disposed.
+        2. DOM mutation events can be fired on the corresponding element of an instance inside
+           a use element’s shadow tree with EventQueueScope in the stack when the event is schedueld
+           via Node::dispatchScopedEvent, e.g. because use element's shadow tree was updated during
+           a style update at the beginning of document.execComand. Because SVGUseElement::cloneTarget
+           constructs the shadow tree by cloning the original tree while it's disconnected from the
+           document, Node::dispatchSubtreeModifiedEvent sees isInShadowTree() to be false and happily
+           tries to dispach DOMSubtreeModified event using Node::dispatchScopedEvent. This works fine
+           when the event is dispatched synchronously since these elements had never been exposed to
+           any scripts yet and they are still disconnected so no scripts have had an opportunity to
+           attach an event listener. But when EventQueueScope in the stack, Node::dispatchScopedEvent
+           will queue up the event and fire it later when those instance elements had been inserted
+           into use element's shadow tree.
+
+        This patch addresses (1) by severing correspondingElement relationship as soon as an instance
+        is removed from its use element's shadow tree, and (2) by not dispatching a scheduled mutation
+        event if the target is inside a shadow tree. Note that this patch also addresses (2) for
+        a regular shadow tree attached by author scripts.
+
+        Tests: fast/shadow-dom/mutation-event-in-shadow-tree.html
+               svg/dom/mutate-symbol-subtree-referenced-by-use-during-execCommand.html
+               svg/dom/update-svg-use-shadow-tree-with-execCommand.html
+
+        * dom/ScopedEventQueue.cpp:
+        (WebCore::ScopedEventQueue::dispatchEvent const):
+        * svg/SVGElement.cpp:
+        (WebCore::SVGElement::~SVGElement):
+        (WebCore::SVGElement::removedFromAncestor):
+        (WebCore::SVGElement::addEventListener):
+        (WebCore::SVGElement::removeEventListener):
+
 2021-04-06  Eric Carlson  <[email protected]>
 
         [Cocoa] audio played at 2x speed video is not pitch corrected

Modified: trunk/Source/WebCore/dom/Node.h (275542 => 275543)


--- trunk/Source/WebCore/dom/Node.h	2021-04-06 19:47:47 UTC (rev 275542)
+++ trunk/Source/WebCore/dom/Node.h	2021-04-06 19:51:49 UTC (rev 275543)
@@ -216,6 +216,7 @@
     bool isTreeScope() const { return hasNodeFlag(NodeFlag::IsDocumentNode) || hasNodeFlag(NodeFlag::IsShadowRoot); }
     bool isDocumentFragment() const { return hasNodeFlag(NodeFlag::IsDocumentFragment); }
     bool isShadowRoot() const { return hasNodeFlag(NodeFlag::IsShadowRoot); }
+    bool isUserAgentShadowRoot() const; // Defined in ShadowRoot.h
 
     bool hasCustomStyleResolveCallbacks() const { return hasNodeFlag(NodeFlag::HasCustomStyleResolveCallbacks); }
 
@@ -225,7 +226,7 @@
     // If this node is in a shadow tree, returns its shadow host. Otherwise, returns null.
     WEBCORE_EXPORT Element* shadowHost() const;
     ShadowRoot* containingShadowRoot() const;
-    ShadowRoot* shadowRoot() const;
+    ShadowRoot* shadowRoot() const; // Defined in ShadowRoot.h
     bool isClosedShadowHidden(const Node&) const;
 
     HTMLSlotElement* assignedSlot() const;
@@ -240,7 +241,7 @@
     Node* nonBoundaryShadowTreeRootNode();
 
     // Node's parent or shadow tree host.
-    ContainerNode* parentOrShadowHostNode() const;
+    ContainerNode* parentOrShadowHostNode() const; // Defined in ShadowRoot.h
     ContainerNode* parentInComposedTree() const;
     Element* parentElementInComposedTree() const;
     Element* parentOrShadowHostElement() const;

Modified: trunk/Source/WebCore/dom/ScopedEventQueue.cpp (275542 => 275543)


--- trunk/Source/WebCore/dom/ScopedEventQueue.cpp	2021-04-06 19:47:47 UTC (rev 275542)
+++ trunk/Source/WebCore/dom/ScopedEventQueue.cpp	2021-04-06 19:51:49 UTC (rev 275543)
@@ -56,6 +56,8 @@
 
 void ScopedEventQueue::dispatchEvent(const ScopedEvent& event) const
 {
+    if (event.event->eventInterface() == MutationEventInterfaceType && event.target->isInShadowTree())
+        return;
     event.target->dispatchEvent(event.event);
 }
 

Modified: trunk/Source/WebCore/dom/ShadowRoot.h (275542 => 275543)


--- trunk/Source/WebCore/dom/ShadowRoot.h	2021-04-06 19:47:47 UTC (rev 275542)
+++ trunk/Source/WebCore/dom/ShadowRoot.h	2021-04-06 19:51:49 UTC (rev 275543)
@@ -153,6 +153,11 @@
     return downcast<Element>(*this).shadowRoot();
 }
 
+inline bool Node::isUserAgentShadowRoot() const
+{
+    return isShadowRoot() && downcast<ShadowRoot>(*this).mode() == ShadowRootMode::UserAgent;
+}
+
 inline ContainerNode* Node::parentOrShadowHostNode() const
 {
     ASSERT(isMainThreadOrGCThread());

Modified: trunk/Source/WebCore/svg/SVGElement.cpp (275542 => 275543)


--- trunk/Source/WebCore/svg/SVGElement.cpp	2021-04-06 19:47:47 UTC (rev 275542)
+++ trunk/Source/WebCore/svg/SVGElement.cpp	2021-04-06 19:51:49 UTC (rev 275543)
@@ -171,9 +171,7 @@
     if (m_svgRareData) {
         for (SVGElement* instance : m_svgRareData->instances())
             instance->m_svgRareData->setCorrespondingElement(nullptr);
-        if (auto correspondingElement = makeRefPtr(m_svgRareData->correspondingElement()))
-            correspondingElement->m_svgRareData->instances().remove(this);
-
+        RELEASE_ASSERT(!m_svgRareData->correspondingElement());
         m_svgRareData = nullptr;
     }
     document().accessSVGExtensions().rebuildAllElementReferencesForTarget(*this);
@@ -253,6 +251,9 @@
         document().accessSVGExtensions().removeAllElementReferencesForTarget(*this);
     }
     invalidateInstances();
+
+    if (removalType.treeScopeChanged && oldParentOfRemovedTree.isUserAgentShadowRoot())
+        setCorrespondingElement(nullptr);
 }
 
 SVGSVGElement* SVGElement::ownerSVGElement() const
@@ -375,6 +376,7 @@
     ASSERT(!instanceUpdatesBlocked());
     for (auto* instance : instances()) {
         ASSERT(instance->correspondingElement() == this);
+        ASSERT(instance->isInUserAgentShadowTree());
         bool result = instance->Node::addEventListener(eventType, listener.copyRef(), options);
         ASSERT_UNUSED(result, result);
     }
@@ -402,6 +404,7 @@
     ASSERT(!instanceUpdatesBlocked());
     for (auto& instance : instances()) {
         ASSERT(instance->correspondingElement() == this);
+        ASSERT(instance->isInUserAgentShadowTree());
 
         if (instance->Node::removeEventListener(eventType, listener, options))
             continue;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to