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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/*
* Copyright (C) 2016-2017 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 "WasmBBQPlan.h"
#if ENABLE(WEBASSEMBLY)
#include "B3Compilation.h"
#include "JSCInlines.h"
#include "JSGlobalObject.h"
#include "WasmB3IRGenerator.h"
#include "WasmBinding.h"
#include "WasmCallee.h"
#include "WasmCallingConvention.h"
#include "WasmFaultSignalHandler.h"
#include "WasmMemory.h"
#include "WasmModuleParser.h"
#include "WasmTierUpCount.h"
#include "WasmValidate.h"
#include <wtf/DataLog.h>
#include <wtf/Locker.h>
#include <wtf/MonotonicTime.h>
#include <wtf/StdLibExtras.h>
#include <wtf/SystemTracing.h>
#include <wtf/text/StringBuilder.h>
namespace JSC { namespace Wasm {
static const bool verbose = false;
BBQPlan::BBQPlan(VM* vm, Ref<ModuleInformation> info, AsyncWork work, CompletionTask&& task)
: Base(vm, WTFMove(info), WTFMove(task))
, m_state(State::Validated)
, m_asyncWork(work)
{
}
BBQPlan::BBQPlan(VM* vm, Vector<uint8_t>&& source, AsyncWork work, CompletionTask&& task)
: BBQPlan(vm, adoptRef(*new ModuleInformation(WTFMove(source))), work, WTFMove(task))
{
m_state = State::Initial;
}
BBQPlan::BBQPlan(VM* vm, const uint8_t* source, size_t sourceLength, AsyncWork work, CompletionTask&& task)
: Base(vm, source, sourceLength, WTFMove(task))
, m_state(State::Initial)
, m_asyncWork(work)
{
}
const char* BBQPlan::stateString(State state)
{
switch (state) {
case State::Initial: return "Initial";
case State::Validated: return "Validated";
case State::Prepared: return "Prepared";
case State::Compiled: return "Compiled";
case State::Completed: return "Completed";
}
RELEASE_ASSERT_NOT_REACHED();
}
void BBQPlan::moveToState(State state)
{
ASSERT(state >= m_state);
dataLogLnIf(verbose && state != m_state, "moving to state: ", stateString(state), " from state: ", stateString(m_state));
m_state = state;
}
bool BBQPlan::parseAndValidateModule()
{
if (m_state != State::Initial)
return true;
dataLogLnIf(verbose, "starting validation");
MonotonicTime startTime;
if (verbose || Options::reportCompileTimes())
startTime = MonotonicTime::now();
{
ModuleParser moduleParser(m_source, m_sourceLength, m_moduleInformation);
auto parseResult = moduleParser.parse();
if (!parseResult) {
Base::fail(holdLock(m_lock), WTFMove(parseResult.error()));
return false;
}
}
const auto& functionLocations = m_moduleInformation->functionLocationInBinary;
for (unsigned functionIndex = 0; functionIndex < functionLocations.size(); ++functionIndex) {
dataLogLnIf(verbose, "Processing function starting at: ", functionLocations[functionIndex].start, " and ending at: ", functionLocations[functionIndex].end);
const uint8_t* functionStart = m_source + functionLocations[functionIndex].start;
size_t functionLength = functionLocations[functionIndex].end - functionLocations[functionIndex].start;
ASSERT(functionLength <= m_sourceLength);
SignatureIndex signatureIndex = m_moduleInformation->internalFunctionSignatureIndices[functionIndex];
const Signature& signature = SignatureInformation::get(signatureIndex);
auto validationResult = validateFunction(functionStart, functionLength, signature, m_moduleInformation.get());
if (!validationResult) {
if (verbose) {
for (unsigned i = 0; i < functionLength; ++i)
dataLog(RawPointer(reinterpret_cast<void*>(functionStart[i])), ", ");
dataLogLn();
}
Base::fail(holdLock(m_lock), makeString(validationResult.error(), ", in function at index ", String::number(functionIndex))); // FIXME make this an Expected.
return false;
}
}
if (verbose || Options::reportCompileTimes())
dataLogLn("Took ", (MonotonicTime::now() - startTime).microseconds(), " us to validate module");
moveToState(State::Validated);
if (m_asyncWork == Validation)
complete(holdLock(m_lock));
return true;
}
void BBQPlan::prepare()
{
ASSERT(m_state == State::Validated);
dataLogLnIf(verbose, "Starting preparation");
auto tryReserveCapacity = [this] (auto& vector, size_t size, const char* what) {
if (UNLIKELY(!vector.tryReserveCapacity(size))) {
StringBuilder builder;
builder.appendLiteral("Failed allocating enough space for ");
builder.appendNumber(size);
builder.append(what);
fail(holdLock(m_lock), builder.toString());
return false;
}
return true;
};
const auto& functionLocations = m_moduleInformation->functionLocationInBinary;
if (!tryReserveCapacity(m_wasmToWasmExitStubs, m_moduleInformation->importFunctionSignatureIndices.size(), " WebAssembly to JavaScript stubs")
|| !tryReserveCapacity(m_unlinkedWasmToWasmCalls, functionLocations.size(), " unlinked WebAssembly to WebAssembly calls")
|| !tryReserveCapacity(m_wasmInternalFunctions, functionLocations.size(), " WebAssembly functions")
|| !tryReserveCapacity(m_compilationContexts, functionLocations.size(), " compilation contexts")
|| !tryReserveCapacity(m_tierUpCounts, functionLocations.size(), " tier-up counts"))
return;
m_unlinkedWasmToWasmCalls.resize(functionLocations.size());
m_wasmInternalFunctions.resize(functionLocations.size());
m_compilationContexts.resize(functionLocations.size());
m_tierUpCounts.resize(functionLocations.size());
for (unsigned importIndex = 0; importIndex < m_moduleInformation->imports.size(); ++importIndex) {
Import* import = &m_moduleInformation->imports[importIndex];
if (import->kind != ExternalKind::Function)
continue;
unsigned importFunctionIndex = m_wasmToWasmExitStubs.size();
dataLogLnIf(verbose, "Processing import function number ", importFunctionIndex, ": ", makeString(import->module), ": ", makeString(import->field));
auto binding = wasmToWasm(importFunctionIndex);
if (UNLIKELY(!binding)) {
switch (binding.error()) {
case BindingFailure::OutOfMemory:
return fail(holdLock(m_lock), makeString("Out of executable memory at import ", String::number(importIndex)));
}
RELEASE_ASSERT_NOT_REACHED();
}
m_wasmToWasmExitStubs.uncheckedAppend(binding.value());
}
const uint32_t importFunctionCount = m_moduleInformation->importFunctionCount();
for (const auto& exp : m_moduleInformation->exports) {
if (exp.kindIndex >= importFunctionCount)
m_exportedFunctionIndices.add(exp.kindIndex - importFunctionCount);
}
for (const auto& element : m_moduleInformation->elements) {
for (const uint32_t elementIndex : element.functionIndices) {
if (elementIndex >= importFunctionCount)
m_exportedFunctionIndices.add(elementIndex - importFunctionCount);
}
}
if (m_moduleInformation->startFunctionIndexSpace && m_moduleInformation->startFunctionIndexSpace >= importFunctionCount)
m_exportedFunctionIndices.add(*m_moduleInformation->startFunctionIndexSpace - importFunctionCount);
moveToState(State::Prepared);
}
// We don't have a semaphore class... and this does kinda interesting things.
class BBQPlan::ThreadCountHolder {
public:
ThreadCountHolder(BBQPlan& plan)
: m_plan(plan)
{
LockHolder locker(m_plan.m_lock);
m_plan.m_numberOfActiveThreads++;
}
~ThreadCountHolder()
{
LockHolder locker(m_plan.m_lock);
m_plan.m_numberOfActiveThreads--;
if (!m_plan.m_numberOfActiveThreads && !m_plan.hasWork())
m_plan.complete(locker);
}
BBQPlan& m_plan;
};
void BBQPlan::compileFunctions(CompilationEffort effort)
{
ASSERT(m_state >= State::Prepared);
dataLogLnIf(verbose, "Starting compilation");
if (!hasWork())
return;
TraceScope traceScope(WebAssemblyCompileStart, WebAssemblyCompileEnd);
ThreadCountHolder holder(*this);
size_t bytesCompiled = 0;
const auto& functionLocations = m_moduleInformation->functionLocationInBinary;
while (true) {
if (effort == Partial && bytesCompiled >= Options::webAssemblyPartialCompileLimit())
return;
uint32_t functionIndex;
{
auto locker = holdLock(m_lock);
if (m_currentIndex >= functionLocations.size()) {
if (hasWork())
moveToState(State::Compiled);
return;
}
functionIndex = m_currentIndex;
++m_currentIndex;
}
const uint8_t* functionStart = m_source + functionLocations[functionIndex].start;
size_t functionLength = functionLocations[functionIndex].end - functionLocations[functionIndex].start;
ASSERT(functionLength <= m_sourceLength);
SignatureIndex signatureIndex = m_moduleInformation->internalFunctionSignatureIndices[functionIndex];
const Signature& signature = SignatureInformation::get(signatureIndex);
unsigned functionIndexSpace = m_wasmToWasmExitStubs.size() + functionIndex;
ASSERT_UNUSED(functionIndexSpace, m_moduleInformation->signatureIndexFromFunctionIndexSpace(functionIndexSpace) == signatureIndex);
ASSERT(validateFunction(functionStart, functionLength, signature, m_moduleInformation.get()));
m_unlinkedWasmToWasmCalls[functionIndex] = Vector<UnlinkedWasmToWasmCall>();
TierUpCount* tierUp = Options::useBBQTierUpChecks() ? &m_tierUpCounts[functionIndex] : nullptr;
auto parseAndCompileResult = parseAndCompile(m_compilationContexts[functionIndex], functionStart, functionLength, signature, m_unlinkedWasmToWasmCalls[functionIndex], m_moduleInformation.get(), m_mode, CompilationMode::BBQMode, functionIndex, tierUp);
if (UNLIKELY(!parseAndCompileResult)) {
auto locker = holdLock(m_lock);
if (!m_errorMessage) {
// Multiple compiles could fail simultaneously. We arbitrarily choose the first.
fail(locker, makeString(parseAndCompileResult.error(), ", in function at index ", String::number(functionIndex))); // FIXME make this an Expected.
}
m_currentIndex = functionLocations.size();
return;
}
m_wasmInternalFunctions[functionIndex] = WTFMove(*parseAndCompileResult);
if (m_exportedFunctionIndices.contains(functionIndex)) {
auto locker = holdLock(m_lock);
auto result = m_jsToWasmInternalFunctions.add(functionIndex, createJSToWasmWrapper(m_compilationContexts[functionIndex], signature, &m_unlinkedWasmToWasmCalls[functionIndex], m_moduleInformation.get(), m_mode, functionIndex));
ASSERT_UNUSED(result, result.isNewEntry);
}
bytesCompiled += functionLength;
}
}
void BBQPlan::complete(const AbstractLocker& locker)
{
ASSERT(m_state != State::Compiled || m_currentIndex >= m_moduleInformation->functionLocationInBinary.size());
dataLogLnIf(verbose, "Starting Completion");
if (!failed() && m_state == State::Compiled) {
for (uint32_t functionIndex = 0; functionIndex < m_moduleInformation->functionLocationInBinary.size(); functionIndex++) {
CompilationContext& context = m_compilationContexts[functionIndex];
SignatureIndex signatureIndex = m_moduleInformation->internalFunctionSignatureIndices[functionIndex];
{
LinkBuffer linkBuffer(*context.wasmEntrypointJIT, nullptr, JITCompilationCanFail);
if (UNLIKELY(linkBuffer.didFailToAllocate())) {
Base::fail(locker, makeString("Out of executable memory in function at index ", String::number(functionIndex)));
return;
}
m_wasmInternalFunctions[functionIndex]->entrypoint.compilation = std::make_unique<B3::Compilation>(
FINALIZE_CODE(linkBuffer, ("WebAssembly function[%i] %s", functionIndex, SignatureInformation::get(signatureIndex).toString().ascii().data())),
WTFMove(context.wasmEntrypointByproducts));
}
if (auto jsToWasmInternalFunction = m_jsToWasmInternalFunctions.get(functionIndex)) {
LinkBuffer linkBuffer(*context.jsEntrypointJIT, nullptr, JITCompilationCanFail);
if (UNLIKELY(linkBuffer.didFailToAllocate())) {
Base::fail(locker, makeString("Out of executable memory in function entrypoint at index ", String::number(functionIndex)));
return;
}
jsToWasmInternalFunction->entrypoint.compilation = std::make_unique<B3::Compilation>(
FINALIZE_CODE(linkBuffer, ("JavaScript->WebAssembly entrypoint[%i] %s", functionIndex, SignatureInformation::get(signatureIndex).toString().ascii().data())),
WTFMove(context.jsEntrypointByproducts));
}
}
for (auto& unlinked : m_unlinkedWasmToWasmCalls) {
for (auto& call : unlinked) {
void* executableAddress;
if (m_moduleInformation->isImportedFunctionFromFunctionIndexSpace(call.functionIndexSpace)) {
// FIXME imports could have been linked in B3, instead of generating a patchpoint. This condition should be replaced by a RELEASE_ASSERT. https://bugs.webkit.org/show_bug.cgi?id=166462
executableAddress = m_wasmToWasmExitStubs.at(call.functionIndexSpace).code().executableAddress();
} else
executableAddress = m_wasmInternalFunctions.at(call.functionIndexSpace - m_moduleInformation->importFunctionCount())->entrypoint.compilation->code().executableAddress();
MacroAssembler::repatchNearCall(call.callLocation, CodeLocationLabel(executableAddress));
}
}
}
if (!isComplete()) {
moveToState(State::Completed);
runCompletionTasks(locker);
}
}
void BBQPlan::work(CompilationEffort effort)
{
switch (m_state) {
case State::Initial:
parseAndValidateModule();
if (!hasWork()) {
ASSERT(isComplete());
return;
}
FALLTHROUGH;
case State::Validated:
prepare();
return;
case State::Prepared:
compileFunctions(effort);
return;
default:
break;
}
return;
}
} } // namespace JSC::Wasm
#endif // ENABLE(WEBASSEMBLY)
|