1/*
2 * Copyright (C) 2017-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 "WasmCodeBlock.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "WasmBBQPlanInlines.h"
32#include "WasmCallee.h"
33#include "WasmFormat.h"
34#include "WasmWorklist.h"
35
36namespace JSC { namespace Wasm {
37
38Ref<CodeBlock> CodeBlock::create(Context* context, MemoryMode mode, ModuleInformation& moduleInformation, CreateEmbedderWrapper&& createEmbedderWrapper, ThrowWasmException throwWasmException)
39{
40 auto* result = new (NotNull, fastMalloc(sizeof(CodeBlock))) CodeBlock(context, mode, moduleInformation, WTFMove(createEmbedderWrapper), throwWasmException);
41 return adoptRef(*result);
42}
43
44CodeBlock::CodeBlock(Context* context, MemoryMode mode, ModuleInformation& moduleInformation, CreateEmbedderWrapper&& createEmbedderWrapper, ThrowWasmException throwWasmException)
45 : m_calleeCount(moduleInformation.internalFunctionCount())
46 , m_mode(mode)
47{
48 RefPtr<CodeBlock> protectedThis = this;
49
50 m_plan = adoptRef(*new BBQPlan(context, makeRef(moduleInformation), BBQPlan::FullCompile, createSharedTask<Plan::CallbackType>([this, protectedThis = WTFMove(protectedThis)] (Plan&) {
51 auto locker = holdLock(m_lock);
52 if (m_plan->failed()) {
53 m_errorMessage = m_plan->errorMessage();
54 setCompilationFinished();
55 return;
56 }
57
58 // FIXME: we should eventually collect the BBQ code.
59 m_callees.resize(m_calleeCount);
60 m_optimizedCallees.resize(m_calleeCount);
61 m_wasmIndirectCallEntryPoints.resize(m_calleeCount);
62
63 m_plan->initializeCallees([&] (unsigned calleeIndex, RefPtr<Wasm::Callee>&& embedderEntrypointCallee, Ref<Wasm::Callee>&& wasmEntrypointCallee) {
64 if (embedderEntrypointCallee) {
65 auto result = m_embedderCallees.set(calleeIndex, WTFMove(embedderEntrypointCallee));
66 ASSERT_UNUSED(result, result.isNewEntry);
67 }
68 m_callees[calleeIndex] = WTFMove(wasmEntrypointCallee);
69 m_wasmIndirectCallEntryPoints[calleeIndex] = m_callees[calleeIndex]->entrypoint();
70 });
71
72 m_wasmToWasmExitStubs = m_plan->takeWasmToWasmExitStubs();
73 m_wasmToWasmCallsites = m_plan->takeWasmToWasmCallsites();
74 m_tierUpCounts = m_plan->takeTierUpCounts();
75
76 setCompilationFinished();
77 }), WTFMove(createEmbedderWrapper), throwWasmException));
78 m_plan->setMode(mode);
79
80 auto& worklist = Wasm::ensureWorklist();
81 // Note, immediately after we enqueue the plan, there is a chance the above callback will be called.
82 worklist.enqueue(makeRef(*m_plan.get()));
83}
84
85CodeBlock::~CodeBlock() { }
86
87void CodeBlock::waitUntilFinished()
88{
89 RefPtr<Plan> plan;
90 {
91 auto locker = holdLock(m_lock);
92 plan = m_plan;
93 }
94
95 if (plan) {
96 auto& worklist = Wasm::ensureWorklist();
97 worklist.completePlanSynchronously(*plan.get());
98 }
99 // else, if we don't have a plan, we're already compiled.
100}
101
102void CodeBlock::compileAsync(Context* context, AsyncCompilationCallback&& task)
103{
104 RefPtr<Plan> plan;
105 {
106 auto locker = holdLock(m_lock);
107 plan = m_plan;
108 }
109
110 if (plan) {
111 // We don't need to keep a RefPtr on the Plan because the worklist will keep
112 // a RefPtr on the Plan until the plan finishes notifying all of its callbacks.
113 RefPtr<CodeBlock> protectedThis = this;
114 plan->addCompletionTask(context, createSharedTask<Plan::CallbackType>([this, task = WTFMove(task), protectedThis = WTFMove(protectedThis)] (Plan&) {
115 task->run(makeRef(*this));
116 }));
117 } else
118 task->run(makeRef(*this));
119}
120
121bool CodeBlock::isSafeToRun(MemoryMode memoryMode)
122{
123 if (!runnable())
124 return false;
125
126 switch (m_mode) {
127 case Wasm::MemoryMode::BoundsChecking:
128 return true;
129 case Wasm::MemoryMode::Signaling:
130 // Code being in Signaling mode means that it performs no bounds checks.
131 // Its memory, even if empty, absolutely must also be in Signaling mode
132 // because the page protection detects out-of-bounds accesses.
133 return memoryMode == Wasm::MemoryMode::Signaling;
134 }
135 RELEASE_ASSERT_NOT_REACHED();
136 return false;
137}
138
139
140void CodeBlock::setCompilationFinished()
141{
142 m_plan = nullptr;
143 m_compilationFinished.store(true);
144}
145
146} } // namespace JSC::Wasm
147
148#endif // ENABLE(WEBASSEMBLY)
149