1/*
2 * Copyright (C) 2017 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 "WasmModule.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "WasmBBQPlanInlines.h"
32#include "WasmModuleInformation.h"
33#include "WasmWorklist.h"
34
35namespace JSC { namespace Wasm {
36
37Module::Module(Ref<ModuleInformation>&& moduleInformation)
38 : m_moduleInformation(WTFMove(moduleInformation))
39{
40}
41
42Module::~Module() { }
43
44Wasm::SignatureIndex Module::signatureIndexFromFunctionIndexSpace(unsigned functionIndexSpace) const
45{
46 return m_moduleInformation->signatureIndexFromFunctionIndexSpace(functionIndexSpace);
47}
48
49static Module::ValidationResult makeValidationResult(BBQPlan& plan)
50{
51 ASSERT(!plan.hasWork());
52 if (plan.failed())
53 return Unexpected<String>(plan.errorMessage());
54 return Module::ValidationResult(Module::create(plan.takeModuleInformation()));
55}
56
57static Plan::CompletionTask makeValidationCallback(Module::AsyncValidationCallback&& callback)
58{
59 return createSharedTask<Plan::CallbackType>([callback = WTFMove(callback)] (Plan& plan) {
60 ASSERT(!plan.hasWork());
61 callback->run(makeValidationResult(static_cast<BBQPlan&>(plan)));
62 });
63}
64
65Module::ValidationResult Module::validateSync(Context* context, Vector<uint8_t>&& source)
66{
67 Ref<BBQPlan> plan = adoptRef(*new BBQPlan(context, WTFMove(source), BBQPlan::Validation, Plan::dontFinalize(), nullptr, nullptr));
68 plan->parseAndValidateModule();
69 return makeValidationResult(plan.get());
70}
71
72void Module::validateAsync(Context* context, Vector<uint8_t>&& source, Module::AsyncValidationCallback&& callback)
73{
74 Ref<Plan> plan = adoptRef(*new BBQPlan(context, WTFMove(source), BBQPlan::Validation, makeValidationCallback(WTFMove(callback)), nullptr, nullptr));
75 Wasm::ensureWorklist().enqueue(WTFMove(plan));
76}
77
78Ref<CodeBlock> Module::getOrCreateCodeBlock(Context* context, MemoryMode mode, CreateEmbedderWrapper&& createEmbedderWrapper, ThrowWasmException throwWasmException)
79{
80 RefPtr<CodeBlock> codeBlock;
81 auto locker = holdLock(m_lock);
82 codeBlock = m_codeBlocks[static_cast<uint8_t>(mode)];
83 // If a previous attempt at a compile errored out, let's try again.
84 // Compilations from valid modules can fail because OOM and cancellation.
85 // It's worth retrying.
86 // FIXME: We might want to back off retrying at some point:
87 // https://bugs.webkit.org/show_bug.cgi?id=170607
88 if (!codeBlock || (codeBlock->compilationFinished() && !codeBlock->runnable())) {
89 codeBlock = CodeBlock::create(context, mode, const_cast<ModuleInformation&>(moduleInformation()), WTFMove(createEmbedderWrapper), throwWasmException);
90 m_codeBlocks[static_cast<uint8_t>(mode)] = codeBlock;
91 }
92 return codeBlock.releaseNonNull();
93}
94
95Ref<CodeBlock> Module::compileSync(Context* context, MemoryMode mode, CreateEmbedderWrapper&& createEmbedderWrapper, ThrowWasmException throwWasmException)
96{
97 Ref<CodeBlock> codeBlock = getOrCreateCodeBlock(context, mode, WTFMove(createEmbedderWrapper), throwWasmException);
98 codeBlock->waitUntilFinished();
99 return codeBlock;
100}
101
102void Module::compileAsync(Context* context, MemoryMode mode, CodeBlock::AsyncCompilationCallback&& task, CreateEmbedderWrapper&& createEmbedderWrapper, ThrowWasmException throwWasmException)
103{
104 Ref<CodeBlock> codeBlock = getOrCreateCodeBlock(context, mode, WTFMove(createEmbedderWrapper), throwWasmException);
105 codeBlock->compileAsync(context, WTFMove(task));
106}
107
108} } // namespace JSC::Wasm
109
110#endif // ENABLE(WEBASSEMBLY)
111