1/*
2 * Copyright (C) 2016-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 "WasmPlan.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "B3Compilation.h"
32#include "WasmB3IRGenerator.h"
33#include "WasmBinding.h"
34#include "WasmCallee.h"
35#include "WasmCallingConvention.h"
36#include "WasmFaultSignalHandler.h"
37#include "WasmMemory.h"
38#include "WasmModuleParser.h"
39#include "WasmValidate.h"
40#include <wtf/DataLog.h>
41#include <wtf/Locker.h>
42#include <wtf/MonotonicTime.h>
43#include <wtf/StdLibExtras.h>
44#include <wtf/SystemTracing.h>
45
46namespace JSC { namespace Wasm {
47
48namespace WasmPlanInternal {
49static const bool verbose = false;
50}
51
52Plan::Plan(Context* context, Ref<ModuleInformation> info, CompletionTask&& task, CreateEmbedderWrapper&& createEmbedderWrapper, ThrowWasmException throwWasmException)
53 : m_moduleInformation(WTFMove(info))
54 , m_createEmbedderWrapper(WTFMove(createEmbedderWrapper))
55 , m_throwWasmException(throwWasmException)
56{
57 m_completionTasks.append(std::make_pair(context, WTFMove(task)));
58}
59
60Plan::Plan(Context* context, Ref<ModuleInformation> info, CompletionTask&& task)
61 : Plan(context, WTFMove(info), WTFMove(task), nullptr, nullptr)
62{
63}
64
65Plan::Plan(Context* context, CompletionTask&& task)
66 : m_moduleInformation(ModuleInformation::create())
67{
68 m_completionTasks.append(std::make_pair(context, WTFMove(task)));
69}
70
71void Plan::runCompletionTasks(const AbstractLocker&)
72{
73 ASSERT(isComplete() && !hasWork());
74
75 for (auto& task : m_completionTasks)
76 task.second->run(*this);
77 m_completionTasks.clear();
78 m_completed.notifyAll();
79}
80
81void Plan::addCompletionTask(Context* context, CompletionTask&& task)
82{
83 LockHolder locker(m_lock);
84 if (!isComplete())
85 m_completionTasks.append(std::make_pair(context, WTFMove(task)));
86 else
87 task->run(*this);
88}
89
90void Plan::waitForCompletion()
91{
92 LockHolder locker(m_lock);
93 if (!isComplete()) {
94 m_completed.wait(m_lock);
95 }
96}
97
98bool Plan::tryRemoveContextAndCancelIfLast(Context& context)
99{
100 LockHolder locker(m_lock);
101
102 if (!ASSERT_DISABLED) {
103 // We allow the first completion task to not have a Context.
104 for (unsigned i = 1; i < m_completionTasks.size(); ++i)
105 ASSERT(m_completionTasks[i].first);
106 }
107
108 bool removedAnyTasks = false;
109 m_completionTasks.removeAllMatching([&] (const std::pair<Context*, CompletionTask>& pair) {
110 bool shouldRemove = pair.first == &context;
111 removedAnyTasks |= shouldRemove;
112 return shouldRemove;
113 });
114
115 if (!removedAnyTasks)
116 return false;
117
118 if (isComplete()) {
119 // We trivially cancel anything that's completed.
120 return true;
121 }
122
123 // FIXME: Make 0 index not so magical: https://bugs.webkit.org/show_bug.cgi?id=171395
124 if (m_completionTasks.isEmpty() || (m_completionTasks.size() == 1 && !m_completionTasks[0].first)) {
125 fail(locker, "WebAssembly Plan was cancelled. If you see this error message please file a bug at bugs.webkit.org!"_s);
126 return true;
127 }
128
129 return false;
130}
131
132void Plan::fail(const AbstractLocker& locker, String&& errorMessage)
133{
134 dataLogLnIf(WasmPlanInternal::verbose, "failing with message: ", errorMessage);
135 m_errorMessage = WTFMove(errorMessage);
136 complete(locker);
137}
138
139Plan::~Plan() { }
140
141} } // namespace JSC::Wasm
142
143#endif // ENABLE(WEBASSEMBLY)
144