1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Copyright (C) 2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WasmStreamingCompiler.h"
#include "DeferredWorkTimerInlines.h"
#include "JSBigInt.h"
#include "JSWebAssembly.h"
#include "JSWebAssemblyCompileError.h"
#include "JSWebAssemblyHelpers.h"
#include "JSWebAssemblyInstance.h"
#include "JSWebAssemblyModule.h"
#include "StrongInlines.h"
#include "WasmIPIntPlan.h"
#include "WasmLLIntPlan.h"
#include "WasmStreamingPlan.h"
#include "WasmWorklist.h"
#if ENABLE(WEBASSEMBLY)
namespace JSC { namespace Wasm {
StreamingCompiler::StreamingCompiler(VM& vm, CompilerMode compilerMode, JSGlobalObject* globalObject, JSPromise* promise, JSObject* importObject)
: m_vm(vm)
, m_compilerMode(compilerMode)
, m_info(Wasm::ModuleInformation::create())
, m_parser(m_info.get(), *this)
{
Vector<JSCell*> dependencies;
dependencies.append(globalObject);
if (importObject)
dependencies.append(importObject);
m_ticket = vm.deferredWorkTimer->addPendingWork(DeferredWorkTimer::WorkType::AtSomePoint, vm, promise, WTFMove(dependencies));
ASSERT(vm.deferredWorkTimer->hasPendingWork(m_ticket));
ASSERT(vm.deferredWorkTimer->hasDependencyInPendingWork(m_ticket, globalObject));
ASSERT(!importObject || vm.deferredWorkTimer->hasDependencyInPendingWork(m_ticket, importObject));
}
StreamingCompiler::~StreamingCompiler()
{
if (m_ticket) {
auto ticket = std::exchange(m_ticket, nullptr);
m_vm.deferredWorkTimer->scheduleWorkSoon(ticket, [](DeferredWorkTimer::Ticket) { });
}
}
Ref<StreamingCompiler> StreamingCompiler::create(VM& vm, CompilerMode compilerMode, JSGlobalObject* globalObject, JSPromise* promise, JSObject* importObject)
{
return adoptRef(*new StreamingCompiler(vm, compilerMode, globalObject, promise, importObject));
}
bool StreamingCompiler::didReceiveFunctionData(FunctionCodeIndex functionIndex, const Wasm::FunctionData&)
{
if (!m_plan) {
if (Options::useWasmIPInt())
m_plan = adoptRef(*new IPIntPlan(m_vm, m_info.copyRef(), m_compilerMode, Plan::dontFinalize()));
else
m_plan = adoptRef(*new LLIntPlan(m_vm, m_info.copyRef(), m_compilerMode, Plan::dontFinalize()));
// Plan already failed in preparation. We do not start threaded compilation.
// Keep Plan failed, and "finalize" will reject promise with that failure.
if (!m_plan->failed()) {
m_remainingCompilationRequests = m_info->functions.size();
m_threadedCompilationStarted = true;
}
}
if (m_threadedCompilationStarted) {
Ref<Plan> plan = adoptRef(*new StreamingPlan(m_vm, m_info.copyRef(), *m_plan, functionIndex, createSharedTask<Plan::CallbackType>([compiler = Ref { *this }](Plan& plan) {
compiler->didCompileFunction(static_cast<StreamingPlan&>(plan));
})));
ensureWorklist().enqueue(WTFMove(plan));
}
return true;
}
void StreamingCompiler::didCompileFunction(StreamingPlan& plan)
{
Locker locker { m_lock };
ASSERT(m_threadedCompilationStarted);
if (plan.failed())
m_plan->didFailInStreaming(plan.errorMessage());
m_remainingCompilationRequests--;
if (!m_remainingCompilationRequests)
m_plan->didCompileFunctionInStreaming();
completeIfNecessary();
}
void StreamingCompiler::didFinishParsing()
{
if (!m_plan) {
// Reaching here means that this WebAssembly module has no functions.
ASSERT(!m_info->functions.size());
ASSERT(!m_remainingCompilationRequests);
if (Options::useWasmIPInt())
m_plan = adoptRef(*new IPIntPlan(m_vm, m_info.copyRef(), m_compilerMode, Plan::dontFinalize()));
else
m_plan = adoptRef(*new LLIntPlan(m_vm, m_info.copyRef(), m_compilerMode, Plan::dontFinalize()));
// If plan is already failed in preparation, we will reject promise with plan's failure soon in finalize.
}
}
void StreamingCompiler::completeIfNecessary()
{
if (m_eagerFailed)
return;
if (!m_remainingCompilationRequests && m_finalized) {
m_plan->completeInStreaming();
didComplete();
}
}
void StreamingCompiler::didComplete()
{
auto makeValidationResult = [](EntryPlan& plan) -> Module::ValidationResult {
ASSERT(!plan.hasWork());
if (plan.failed())
return Unexpected<String>(plan.errorMessage());
if (Options::useWasmIPInt())
return JSC::Wasm::Module::ValidationResult(Module::create(static_cast<IPIntPlan&>(plan)));
return JSC::Wasm::Module::ValidationResult(Module::create(static_cast<LLIntPlan&>(plan)));
};
auto result = makeValidationResult(*m_plan);
auto ticket = std::exchange(m_ticket, nullptr);
switch (m_compilerMode) {
case CompilerMode::Validation: {
m_vm.deferredWorkTimer->scheduleWorkSoon(ticket, [result = WTFMove(result)](DeferredWorkTimer::Ticket ticket) mutable {
JSPromise* promise = jsCast<JSPromise*>(ticket->target());
JSGlobalObject* globalObject = jsCast<JSGlobalObject*>(ticket->dependencies()[0]);
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (UNLIKELY(!result.has_value())) {
throwException(globalObject, scope, createJSWebAssemblyCompileError(globalObject, vm, result.error()));
promise->rejectWithCaughtException(globalObject, scope);
return;
}
JSWebAssemblyModule* module = JSWebAssemblyModule::create(vm, globalObject->webAssemblyModuleStructure(), WTFMove(result.value()));
scope.release();
promise->resolve(globalObject, module);
});
return;
}
case CompilerMode::FullCompile: {
m_vm.deferredWorkTimer->scheduleWorkSoon(ticket, [result = WTFMove(result)](DeferredWorkTimer::Ticket ticket) mutable {
JSPromise* promise = jsCast<JSPromise*>(ticket->target());
JSGlobalObject* globalObject = jsCast<JSGlobalObject*>(ticket->dependencies()[0]);
JSObject* importObject = jsCast<JSObject*>(ticket->dependencies()[1]);
VM& vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
if (UNLIKELY(!result.has_value())) {
throwException(globalObject, scope, createJSWebAssemblyCompileError(globalObject, vm, result.error()));
promise->rejectWithCaughtException(globalObject, scope);
return;
}
JSWebAssemblyModule* module = JSWebAssemblyModule::create(vm, globalObject->webAssemblyModuleStructure(), WTFMove(result.value()));
JSWebAssembly::instantiateForStreaming(vm, globalObject, promise, module, importObject);
if (UNLIKELY(scope.exception())) {
promise->rejectWithCaughtException(globalObject, scope);
return;
}
});
return;
}
}
}
void StreamingCompiler::finalize(JSGlobalObject* globalObject)
{
auto state = m_parser.finalize();
if (state != StreamingParser::State::Finished) {
fail(globalObject, createJSWebAssemblyCompileError(globalObject, globalObject->vm(), m_parser.errorMessage()));
return;
}
{
Locker locker { m_lock };
m_finalized = true;
completeIfNecessary();
}
}
void StreamingCompiler::fail(JSGlobalObject* globalObject, JSValue error)
{
{
Locker locker { m_lock };
ASSERT(!m_finalized);
if (m_eagerFailed)
return;
m_eagerFailed = true;
}
auto ticket = std::exchange(m_ticket, nullptr);
JSPromise* promise = jsCast<JSPromise*>(ticket->target());
// The pending work TicketData was keeping the promise alive. We need to
// make sure it is reachable from the stack before we remove it from the
// pending work list. Note: m_ticket stores it as a PackedPtr, which is not
// scannable by the GC.
WTF::compilerFence();
m_vm.deferredWorkTimer->cancelPendingWork(ticket);
promise->reject(globalObject, error);
}
void StreamingCompiler::cancel()
{
{
Locker locker { m_lock };
ASSERT(!m_finalized);
if (m_eagerFailed)
return;
m_eagerFailed = true;
}
auto ticket = std::exchange(m_ticket, nullptr);
m_vm.deferredWorkTimer->cancelPendingWork(ticket);
}
} } // namespace JSC::Wasm
#endif // ENABLE(WEBASSEMBLY)
|