Title: [250843] trunk/Source/WebCore
Revision
250843
Author
[email protected]
Date
2019-10-08 11:39:04 -0700 (Tue, 08 Oct 2019)

Log Message

Make sure ActiveDOMObject properly deals with detached documents
https://bugs.webkit.org/show_bug.cgi?id=202596

Reviewed by Geoffrey Garen.

For detached document, the script execution context is their context document.
The ActiveDOMObject constructor taking a Document& would make sure to get the
document's contextDocument. However, if the ActiveDOMObject constructor taking
a ScriptExecutionContext* is called, it would assume this is the right script
execution context, which is unsafe. In this patch, all ActiveDOMObject
constructors now check for detached documents and make sure to use their
context document when necessary.

* dom/ActiveDOMObject.cpp:
(WebCore::suitableScriptExecutionContext):
(WebCore::ActiveDOMObject::ActiveDOMObject):
* dom/ActiveDOMObject.h:
* dom/Document.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (250842 => 250843)


--- trunk/Source/WebCore/ChangeLog	2019-10-08 18:30:40 UTC (rev 250842)
+++ trunk/Source/WebCore/ChangeLog	2019-10-08 18:39:04 UTC (rev 250843)
@@ -1,3 +1,24 @@
+2019-10-08  Chris Dumez  <[email protected]>
+
+        Make sure ActiveDOMObject properly deals with detached documents
+        https://bugs.webkit.org/show_bug.cgi?id=202596
+
+        Reviewed by Geoffrey Garen.
+
+        For detached document, the script execution context is their context document.
+        The ActiveDOMObject constructor taking a Document& would make sure to get the
+        document's contextDocument. However, if the ActiveDOMObject constructor taking
+        a ScriptExecutionContext* is called, it would assume this is the right script
+        execution context, which is unsafe. In this patch, all ActiveDOMObject
+        constructors now check for detached documents and make sure to use their
+        context document when necessary.
+
+        * dom/ActiveDOMObject.cpp:
+        (WebCore::suitableScriptExecutionContext):
+        (WebCore::ActiveDOMObject::ActiveDOMObject):
+        * dom/ActiveDOMObject.h:
+        * dom/Document.h:
+
 2019-10-08  Ross Kirsling  <[email protected]>
 
         Unreviewed. Restabilize non-unified build.

Modified: trunk/Source/WebCore/dom/ActiveDOMObject.cpp (250842 => 250843)


--- trunk/Source/WebCore/dom/ActiveDOMObject.cpp	2019-10-08 18:30:40 UTC (rev 250842)
+++ trunk/Source/WebCore/dom/ActiveDOMObject.cpp	2019-10-08 18:39:04 UTC (rev 250843)
@@ -32,21 +32,38 @@
 
 namespace WebCore {
 
-ActiveDOMObject::ActiveDOMObject(ScriptExecutionContext* scriptExecutionContext)
-    : ContextDestructionObserver(scriptExecutionContext)
-    , m_pendingActivityCount(0)
-#if !ASSERT_DISABLED
-    , m_suspendIfNeededWasCalled(false)
-#endif
+static inline ScriptExecutionContext* suitableScriptExecutionContext(ScriptExecutionContext* scriptExecutionContext)
 {
-    ASSERT(!is<Document>(m_scriptExecutionContext) || &downcast<Document>(m_scriptExecutionContext)->contextDocument() == downcast<Document>(m_scriptExecutionContext));
-    if (!m_scriptExecutionContext)
+    // For detached documents, make sure we observe their context document instead.
+    return is<Document>(scriptExecutionContext) ? &downcast<Document>(*scriptExecutionContext).contextDocument() : scriptExecutionContext;
+}
+
+inline ActiveDOMObject::ActiveDOMObject(ScriptExecutionContext* context, CheckedScriptExecutionContextType)
+    : ContextDestructionObserver(context)
+{
+    ASSERT(!is<Document>(context) || &downcast<Document>(context)->contextDocument() == downcast<Document>(context));
+    if (!context)
         return;
 
-    ASSERT(m_scriptExecutionContext->isContextThread());
-    m_scriptExecutionContext->didCreateActiveDOMObject(*this);
+    ASSERT(context->isContextThread());
+    context->didCreateActiveDOMObject(*this);
 }
 
+ActiveDOMObject::ActiveDOMObject(ScriptExecutionContext* scriptExecutionContext)
+    : ActiveDOMObject(suitableScriptExecutionContext(scriptExecutionContext), CheckedScriptExecutionContext)
+{
+}
+
+ActiveDOMObject::ActiveDOMObject(Document* document)
+    : ActiveDOMObject(document ? &document->contextDocument() : nullptr, CheckedScriptExecutionContext)
+{
+}
+
+ActiveDOMObject::ActiveDOMObject(Document& document)
+    : ActiveDOMObject(&document.contextDocument(), CheckedScriptExecutionContext)
+{
+}
+
 ActiveDOMObject::~ActiveDOMObject()
 {
     ASSERT(canAccessThreadLocalDataForThread(m_creationThread));
@@ -91,11 +108,6 @@
     return m_pendingActivityCount;
 }
 
-bool ActiveDOMObject::canSuspendForDocumentSuspension() const
-{
-    return false;
-}
-
 void ActiveDOMObject::suspend(ReasonForSuspension)
 {
 }

Modified: trunk/Source/WebCore/dom/ActiveDOMObject.h (250842 => 250843)


--- trunk/Source/WebCore/dom/ActiveDOMObject.h	2019-10-08 18:30:40 UTC (rev 250842)
+++ trunk/Source/WebCore/dom/ActiveDOMObject.h	2019-10-08 18:39:04 UTC (rev 250843)
@@ -116,14 +116,17 @@
 
 protected:
     explicit ActiveDOMObject(ScriptExecutionContext*);
-    explicit ActiveDOMObject(Document*) = delete;
-    explicit ActiveDOMObject(Document&); // Implemented in Document.h
+    explicit ActiveDOMObject(Document*);
+    explicit ActiveDOMObject(Document&);
     virtual ~ActiveDOMObject();
 
 private:
-    unsigned m_pendingActivityCount;
+    enum CheckedScriptExecutionContextType { CheckedScriptExecutionContext };
+    ActiveDOMObject(ScriptExecutionContext*, CheckedScriptExecutionContextType);
+
+    unsigned m_pendingActivityCount { 0 };
 #if !ASSERT_DISABLED
-    bool m_suspendIfNeededWasCalled;
+    bool m_suspendIfNeededWasCalled { false };
     Ref<Thread> m_creationThread { Thread::current() };
 #endif
 };

Modified: trunk/Source/WebCore/dom/Document.h (250842 => 250843)


--- trunk/Source/WebCore/dom/Document.h	2019-10-08 18:30:40 UTC (rev 250842)
+++ trunk/Source/WebCore/dom/Document.h	2019-10-08 18:39:04 UTC (rev 250843)
@@ -2104,11 +2104,6 @@
     return &document().contextDocument();
 }
 
-inline ActiveDOMObject::ActiveDOMObject(Document& document)
-    : ActiveDOMObject(static_cast<ScriptExecutionContext*>(&document.contextDocument()))
-{
-}
-
 } // namespace WebCore
 
 SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::Document)
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to