1 | /* |
2 | * Copyright (C) 2018 Apple Inc. All Rights Reserved. |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions |
6 | * are met: |
7 | * 1. Redistributions of source code must retain the above copyright |
8 | * notice, this list of conditions and the following disclaimer. |
9 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * notice, this list of conditions and the following disclaimer in the |
11 | * documentation and/or other materials provided with the distribution. |
12 | * |
13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "WorkletScriptController.h" |
28 | |
29 | #if ENABLE(CSS_PAINTING_API) |
30 | |
31 | #include "JSDOMBinding.h" |
32 | #include "JSEventTarget.h" |
33 | #include "JSExecState.h" |
34 | #include "JSPaintWorkletGlobalScope.h" |
35 | #include "ScriptSourceCode.h" |
36 | #include "WebCoreJSClientData.h" |
37 | #include "WorkletConsoleClient.h" |
38 | |
39 | #include <JavaScriptCore/Completion.h> |
40 | #include <JavaScriptCore/Exception.h> |
41 | #include <JavaScriptCore/ExceptionHelpers.h> |
42 | #include <JavaScriptCore/GCActivityCallback.h> |
43 | #include <JavaScriptCore/JSLock.h> |
44 | #include <JavaScriptCore/PromiseDeferredTimer.h> |
45 | #include <JavaScriptCore/StrongInlines.h> |
46 | |
47 | namespace WebCore { |
48 | using namespace JSC; |
49 | |
50 | WorkletScriptController::WorkletScriptController(WorkletGlobalScope* workletGlobalScope) |
51 | : m_vm(VM::create()) |
52 | , m_workletGlobalScope(workletGlobalScope) |
53 | , m_workletGlobalScopeWrapper(*m_vm) |
54 | { |
55 | m_vm->heap.acquireAccess(); // It's not clear that we have good discipline for heap access, so turn it on permanently. |
56 | JSVMClientData::initNormalWorld(m_vm.get()); |
57 | } |
58 | |
59 | WorkletScriptController::~WorkletScriptController() |
60 | { |
61 | JSLockHolder lock(vm()); |
62 | forbidExecution(); |
63 | |
64 | if (m_workletGlobalScopeWrapper) { |
65 | m_workletGlobalScopeWrapper->clearDOMGuardedObjects(); |
66 | m_workletGlobalScopeWrapper->setConsoleClient(nullptr); |
67 | m_consoleClient = nullptr; |
68 | } |
69 | m_workletGlobalScopeWrapper.clear(); |
70 | m_vm = nullptr; |
71 | } |
72 | |
73 | void WorkletScriptController::forbidExecution() |
74 | { |
75 | ASSERT(m_workletGlobalScope->isContextThread()); |
76 | m_executionForbidden = true; |
77 | } |
78 | |
79 | bool WorkletScriptController::isExecutionForbidden() const |
80 | { |
81 | ASSERT(m_workletGlobalScope->isContextThread()); |
82 | return m_executionForbidden; |
83 | } |
84 | |
85 | void WorkletScriptController::disableEval(const String& errorMessage) |
86 | { |
87 | if (isExecutionForbidden()) |
88 | return; |
89 | |
90 | initScriptIfNeeded(); |
91 | JSLockHolder lock { vm() }; |
92 | |
93 | m_workletGlobalScopeWrapper->setEvalEnabled(false, errorMessage); |
94 | } |
95 | |
96 | void WorkletScriptController::disableWebAssembly(const String& errorMessage) |
97 | { |
98 | if (isExecutionForbidden()) |
99 | return; |
100 | |
101 | initScriptIfNeeded(); |
102 | JSLockHolder lock { vm() }; |
103 | |
104 | m_workletGlobalScopeWrapper->setWebAssemblyEnabled(false, errorMessage); |
105 | } |
106 | |
107 | |
108 | template<typename JSGlobalScopePrototype, typename JSGlobalScope, typename GlobalScope> |
109 | void WorkletScriptController::initScriptWithSubclass() |
110 | { |
111 | ASSERT(!m_workletGlobalScopeWrapper); |
112 | |
113 | JSLockHolder lock { vm() }; |
114 | |
115 | // Explicitly protect the global object's prototype so it isn't collected |
116 | // when we allocate the global object. (Once the global object is fully |
117 | // constructed, it can mark its own prototype.) |
118 | Structure* contextPrototypeStructure = JSGlobalScopePrototype::createStructure(*m_vm, nullptr, jsNull()); |
119 | auto* contextPrototype = JSGlobalScopePrototype::create(*m_vm, nullptr, contextPrototypeStructure); |
120 | Structure* structure = JSGlobalScope::createStructure(*m_vm, nullptr, contextPrototype); |
121 | auto* proxyStructure = JSProxy::createStructure(*m_vm, nullptr, jsNull(), PureForwardingProxyType); |
122 | auto* proxy = JSProxy::create(*m_vm, proxyStructure); |
123 | |
124 | m_workletGlobalScopeWrapper.set(*m_vm, JSGlobalScope::create(*m_vm, structure, static_cast<GlobalScope&>(*m_workletGlobalScope), proxy)); |
125 | contextPrototypeStructure->setGlobalObject(*m_vm, m_workletGlobalScopeWrapper.get()); |
126 | ASSERT(structure->globalObject() == m_workletGlobalScopeWrapper); |
127 | ASSERT(m_workletGlobalScopeWrapper->structure(*m_vm)->globalObject() == m_workletGlobalScopeWrapper); |
128 | contextPrototype->structure(*m_vm)->setGlobalObject(*m_vm, m_workletGlobalScopeWrapper.get()); |
129 | contextPrototype->structure(*m_vm)->setPrototypeWithoutTransition(*m_vm, JSGlobalScope::prototype(*m_vm, *m_workletGlobalScopeWrapper.get())); |
130 | |
131 | proxy->setTarget(*m_vm, m_workletGlobalScopeWrapper.get()); |
132 | proxy->structure(*m_vm)->setGlobalObject(*m_vm, m_workletGlobalScopeWrapper.get()); |
133 | |
134 | ASSERT(m_workletGlobalScopeWrapper->globalObject() == m_workletGlobalScopeWrapper); |
135 | ASSERT(asObject(m_workletGlobalScopeWrapper->getPrototypeDirect(*m_vm))->globalObject() == m_workletGlobalScopeWrapper); |
136 | |
137 | m_consoleClient = std::make_unique<WorkletConsoleClient>(*m_workletGlobalScope); |
138 | m_workletGlobalScopeWrapper->setConsoleClient(m_consoleClient.get()); |
139 | } |
140 | |
141 | void WorkletScriptController::initScript() |
142 | { |
143 | if (isExecutionForbidden()) |
144 | return; |
145 | |
146 | if (is<PaintWorkletGlobalScope>(m_workletGlobalScope)) { |
147 | initScriptWithSubclass<JSPaintWorkletGlobalScopePrototype, JSPaintWorkletGlobalScope, PaintWorkletGlobalScope>(); |
148 | return; |
149 | } |
150 | |
151 | ASSERT_NOT_REACHED(); |
152 | } |
153 | |
154 | void WorkletScriptController::evaluate(const ScriptSourceCode& sourceCode, String* returnedExceptionMessage) |
155 | { |
156 | if (isExecutionForbidden()) |
157 | return; |
158 | |
159 | NakedPtr<JSC::Exception> exception; |
160 | evaluate(sourceCode, exception, returnedExceptionMessage); |
161 | if (exception) { |
162 | JSLockHolder lock(vm()); |
163 | reportException(m_workletGlobalScopeWrapper->globalExec(), exception); |
164 | } |
165 | } |
166 | |
167 | // Important: The caller of this function must verify that the returned exception message does not violate CORS if it is going to be passed back to JS. |
168 | void WorkletScriptController::evaluate(const ScriptSourceCode& sourceCode, NakedPtr<JSC::Exception>& returnedException, String* returnedExceptionMessage) |
169 | { |
170 | if (isExecutionForbidden()) |
171 | return; |
172 | |
173 | initScriptIfNeeded(); |
174 | |
175 | auto& state = *m_workletGlobalScopeWrapper->globalExec(); |
176 | VM& vm = state.vm(); |
177 | JSLockHolder lock { vm }; |
178 | |
179 | JSExecState::profiledEvaluate(&state, JSC::ProfilingReason::Other, sourceCode.jsSourceCode(), m_workletGlobalScopeWrapper->globalThis(), returnedException); |
180 | |
181 | if (returnedException && returnedExceptionMessage) { |
182 | // This FIXME is from WorkerScriptController. |
183 | // FIXME: It's not great that this can run arbitrary code to string-ify the value of the exception. |
184 | // Do we need to do anything to handle that properly, if it, say, raises another exception? |
185 | *returnedExceptionMessage = returnedException->value().toWTFString(&state); |
186 | } |
187 | } |
188 | |
189 | void WorkletScriptController::setException(JSC::Exception* exception) |
190 | { |
191 | auto* exec = m_workletGlobalScopeWrapper->globalExec(); |
192 | VM& vm = exec->vm(); |
193 | auto scope = DECLARE_THROW_SCOPE(vm); |
194 | throwException(exec, scope, exception); |
195 | } |
196 | |
197 | } // namespace WebCore |
198 | #endif |
199 | |