Title: [290671] trunk/Source/WebCore
Revision
290671
Author
[email protected]
Date
2022-03-01 13:19:10 -0800 (Tue, 01 Mar 2022)

Log Message

Web app fails only when dev tools is open
https://bugs.webkit.org/show_bug.cgi?id=235017

Reviewed by Devin Rousso.

Using the `ScriptExecutionContext` from `event.target()->scriptExecutionContext()` can result the either having a
different script context from the one used when calling `willHandleEvent`, or the event target's context could be
`nullptr`. This can occur when handling the event in `EventTarget::innerInvokeEventListeners` results in a
context change for the event's target, like a MessagePort that has been `disentangle`d, which sets the script
execution context to `nullptr`. Because we only need the script execution context to get the correct injected
script, and the correct injected script for the action below will always be the same injected script used in
`willHandleEvent`, we ignore the current script execution context of the event's target and use the context the
event's target had when it began invoking event listeners.

This change protects us both from the reported crash, as well as leaving an injected script in a bad state
because we did not call `setEventValue` and `clearEventValue` on matching injected scripts for a single event.

* inspector/InspectorInstrumentation.cpp:
(WebCore::InspectorInstrumentation::willHandleEventImpl):
(WebCore::InspectorInstrumentation::didHandleEventImpl):
* inspector/InspectorInstrumentation.h:
(WebCore::InspectorInstrumentation::willHandleEvent):
(WebCore::InspectorInstrumentation::didHandleEvent):
* inspector/agents/InspectorDOMDebuggerAgent.cpp:
(WebCore::InspectorDOMDebuggerAgent::willHandleEvent):
(WebCore::InspectorDOMDebuggerAgent::didHandleEvent):
* inspector/agents/InspectorDOMDebuggerAgent.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (290670 => 290671)


--- trunk/Source/WebCore/ChangeLog	2022-03-01 21:02:20 UTC (rev 290670)
+++ trunk/Source/WebCore/ChangeLog	2022-03-01 21:19:10 UTC (rev 290671)
@@ -1,3 +1,33 @@
+2022-03-01  Patrick Angle  <[email protected]>
+
+        Web app fails only when dev tools is open
+        https://bugs.webkit.org/show_bug.cgi?id=235017
+
+        Reviewed by Devin Rousso.
+
+        Using the `ScriptExecutionContext` from `event.target()->scriptExecutionContext()` can result the either having a
+        different script context from the one used when calling `willHandleEvent`, or the event target's context could be
+        `nullptr`. This can occur when handling the event in `EventTarget::innerInvokeEventListeners` results in a
+        context change for the event's target, like a MessagePort that has been `disentangle`d, which sets the script
+        execution context to `nullptr`. Because we only need the script execution context to get the correct injected
+        script, and the correct injected script for the action below will always be the same injected script used in
+        `willHandleEvent`, we ignore the current script execution context of the event's target and use the context the
+        event's target had when it began invoking event listeners.
+
+        This change protects us both from the reported crash, as well as leaving an injected script in a bad state
+        because we did not call `setEventValue` and `clearEventValue` on matching injected scripts for a single event.
+
+        * inspector/InspectorInstrumentation.cpp:
+        (WebCore::InspectorInstrumentation::willHandleEventImpl):
+        (WebCore::InspectorInstrumentation::didHandleEventImpl):
+        * inspector/InspectorInstrumentation.h:
+        (WebCore::InspectorInstrumentation::willHandleEvent):
+        (WebCore::InspectorInstrumentation::didHandleEvent):
+        * inspector/agents/InspectorDOMDebuggerAgent.cpp:
+        (WebCore::InspectorDOMDebuggerAgent::willHandleEvent):
+        (WebCore::InspectorDOMDebuggerAgent::didHandleEvent):
+        * inspector/agents/InspectorDOMDebuggerAgent.h:
+
 2022-03-01  Martin Robinson  <[email protected]>
 
         When interpolating between transform lists partial prefix matches should not use matrix interpolation

Modified: trunk/Source/WebCore/inspector/InspectorInstrumentation.cpp (290670 => 290671)


--- trunk/Source/WebCore/inspector/InspectorInstrumentation.cpp	2022-03-01 21:02:20 UTC (rev 290670)
+++ trunk/Source/WebCore/inspector/InspectorInstrumentation.cpp	2022-03-01 21:19:10 UTC (rev 290671)
@@ -419,22 +419,22 @@
         timelineAgent->willDispatchEvent(event, document.frame());
 }
 
-void InspectorInstrumentation::willHandleEventImpl(InstrumentingAgents& instrumentingAgents, Event& event, const RegisteredEventListener& listener)
+void InspectorInstrumentation::willHandleEventImpl(InstrumentingAgents& instrumentingAgents, ScriptExecutionContext& context, Event& event, const RegisteredEventListener& listener)
 {
     if (auto* webDebuggerAgent = instrumentingAgents.enabledWebDebuggerAgent())
         webDebuggerAgent->willHandleEvent(listener);
 
     if (auto* domDebuggerAgent = instrumentingAgents.enabledDOMDebuggerAgent())
-        domDebuggerAgent->willHandleEvent(event, listener);
+        domDebuggerAgent->willHandleEvent(context, event, listener);
 }
 
-void InspectorInstrumentation::didHandleEventImpl(InstrumentingAgents& instrumentingAgents, Event& event, const RegisteredEventListener& listener)
+void InspectorInstrumentation::didHandleEventImpl(InstrumentingAgents& instrumentingAgents, ScriptExecutionContext& context, Event& event, const RegisteredEventListener& listener)
 {
     if (auto* webDebuggerAgent = instrumentingAgents.enabledWebDebuggerAgent())
         webDebuggerAgent->didDispatchAsyncCall();
 
     if (auto* domDebuggerAgent = instrumentingAgents.enabledDOMDebuggerAgent())
-        domDebuggerAgent->didHandleEvent(event, listener);
+        domDebuggerAgent->didHandleEvent(context, event, listener);
 }
 
 void InspectorInstrumentation::didDispatchEventImpl(InstrumentingAgents& instrumentingAgents, const Event& event)

Modified: trunk/Source/WebCore/inspector/InspectorInstrumentation.h (290670 => 290671)


--- trunk/Source/WebCore/inspector/InspectorInstrumentation.h	2022-03-01 21:02:20 UTC (rev 290670)
+++ trunk/Source/WebCore/inspector/InspectorInstrumentation.h	2022-03-01 21:19:10 UTC (rev 290671)
@@ -389,8 +389,8 @@
     static void willRemoveEventListenerImpl(InstrumentingAgents&, EventTarget&, const AtomString& eventType, EventListener&, bool capture);
     static bool isEventListenerDisabledImpl(InstrumentingAgents&, EventTarget&, const AtomString& eventType, EventListener&, bool capture);
     static void willDispatchEventImpl(InstrumentingAgents&, Document&, const Event&);
-    static void willHandleEventImpl(InstrumentingAgents&, Event&, const RegisteredEventListener&);
-    static void didHandleEventImpl(InstrumentingAgents&, Event&, const RegisteredEventListener&);
+    static void willHandleEventImpl(InstrumentingAgents&, ScriptExecutionContext&, Event&, const RegisteredEventListener&);
+    static void didHandleEventImpl(InstrumentingAgents&, ScriptExecutionContext&, Event&, const RegisteredEventListener&);
     static void didDispatchEventImpl(InstrumentingAgents&, const Event&);
     static void willDispatchEventOnWindowImpl(InstrumentingAgents&, const Event&, DOMWindow&);
     static void didDispatchEventOnWindowImpl(InstrumentingAgents&, const Event&);
@@ -892,7 +892,7 @@
 {
     FAST_RETURN_IF_NO_FRONTENDS(void());
     if (auto* agents = instrumentingAgents(context))
-        return willHandleEventImpl(*agents, event, listener);
+        return willHandleEventImpl(*agents, context, event, listener);
 }
 
 inline void InspectorInstrumentation::didHandleEvent(ScriptExecutionContext& context, Event& event, const RegisteredEventListener& listener)
@@ -899,7 +899,7 @@
 {
     FAST_RETURN_IF_NO_FRONTENDS(void());
     if (auto* agents = instrumentingAgents(context))
-        return didHandleEventImpl(*agents, event, listener);
+        return didHandleEventImpl(*agents, context, event, listener);
 }
 
 inline void InspectorInstrumentation::willDispatchEventOnWindow(Frame* frame, const Event& event, DOMWindow& window)

Modified: trunk/Source/WebCore/inspector/agents/InspectorDOMDebuggerAgent.cpp (290670 => 290671)


--- trunk/Source/WebCore/inspector/agents/InspectorDOMDebuggerAgent.cpp	2022-03-01 21:02:20 UTC (rev 290670)
+++ trunk/Source/WebCore/inspector/agents/InspectorDOMDebuggerAgent.cpp	2022-03-01 21:19:10 UTC (rev 290671)
@@ -218,9 +218,12 @@
     return makeUnexpected("Not supported");
 }
 
-void InspectorDOMDebuggerAgent::willHandleEvent(Event& event, const RegisteredEventListener& registeredEventListener)
+void InspectorDOMDebuggerAgent::willHandleEvent(ScriptExecutionContext& scriptExecutionContext, Event& event, const RegisteredEventListener& registeredEventListener)
 {
-    auto state = event.target()->scriptExecutionContext()->globalObject();
+    // `event.target()->scriptExecutionContext()` can change between `willHandleEvent` and `didHandleEvent`. The passed
+    // `scriptExecutionContext` parameter will always match in companion calls to `willHandleEvent` and
+    // `didHandleEvent`, and will not be null.
+    auto state = scriptExecutionContext.globalObject();
     auto injectedScript = m_injectedScriptManager.injectedScriptFor(state);
     if (injectedScript.hasNoValue())
         return;
@@ -259,9 +262,12 @@
     m_debuggerAgent->schedulePauseForSpecialBreakpoint(*breakpoint, Inspector::DebuggerFrontendDispatcher::Reason::Listener, WTFMove(eventData));
 }
 
-void InspectorDOMDebuggerAgent::didHandleEvent(Event& event, const RegisteredEventListener& registeredEventListener)
+void InspectorDOMDebuggerAgent::didHandleEvent(ScriptExecutionContext& scriptExecutionContext, Event& event, const RegisteredEventListener& registeredEventListener)
 {
-    auto state = event.target()->scriptExecutionContext()->globalObject();
+    // `event.target()->scriptExecutionContext()` can change between `willHandleEvent` and `didHandleEvent`. Here it
+    // could also be nullptr. The passed `scriptExecutionContext` parameter here will always match in companion calls to
+    // `willHandleEvent` and `didHandleEvent`, and will not be null.
+    auto state = scriptExecutionContext.globalObject();
     auto injectedScript = m_injectedScriptManager.injectedScriptFor(state);
     if (injectedScript.hasNoValue())
         return;

Modified: trunk/Source/WebCore/inspector/agents/InspectorDOMDebuggerAgent.h (290670 => 290671)


--- trunk/Source/WebCore/inspector/agents/InspectorDOMDebuggerAgent.h	2022-03-01 21:02:20 UTC (rev 290670)
+++ trunk/Source/WebCore/inspector/agents/InspectorDOMDebuggerAgent.h	2022-03-01 21:19:10 UTC (rev 290671)
@@ -76,8 +76,8 @@
     virtual void mainFrameNavigated();
     void willSendXMLHttpRequest(const String& url);
     void willFetch(const String& url);
-    void willHandleEvent(Event&, const RegisteredEventListener&);
-    void didHandleEvent(Event&, const RegisteredEventListener&);
+    void willHandleEvent(ScriptExecutionContext&, Event&, const RegisteredEventListener&);
+    void didHandleEvent(ScriptExecutionContext&, Event&, const RegisteredEventListener&);
     void willFireTimer(bool oneShot);
     void didFireTimer(bool oneShot);
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to