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