// Copyright 2018 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "src/wasm/wasm-engine.h" #include "src/code-tracer.h" #include "src/compilation-statistics.h" #include "src/counters.h" #include "src/objects-inl.h" #include "src/objects/heap-number.h" #include "src/objects/js-promise.h" #include "src/ostreams.h" #include "src/wasm/function-compiler.h" #include "src/wasm/module-compiler.h" #include "src/wasm/module-decoder.h" #include "src/wasm/module-instantiate.h" #include "src/wasm/streaming-decoder.h" #include "src/wasm/wasm-objects-inl.h" namespace v8 { namespace internal { namespace wasm { namespace { class LogCodesTask : public Task { public: LogCodesTask(base::Mutex* mutex, LogCodesTask** task_slot, Isolate* isolate) : mutex_(mutex), task_slot_(task_slot), isolate_(isolate) { DCHECK_NOT_NULL(task_slot); DCHECK_NOT_NULL(isolate); } ~LogCodesTask() { // If the platform deletes this task before executing it, we also deregister // it to avoid use-after-free from still-running background threads. if (!cancelled()) DeregisterTask(); } // Hold the {mutex_} when calling this method. void AddCode(WasmCode* code) { code_to_log_.push_back(code); } void Run() override { if (cancelled()) return; DeregisterTask(); // If by now we should not log code any more, do not log it. if (!WasmCode::ShouldBeLogged(isolate_)) return; for (WasmCode* code : code_to_log_) { code->LogCode(isolate_); } } void Cancel() { // Cancel will only be called on Isolate shutdown, which happens on the // Isolate's foreground thread. Thus no synchronization needed. isolate_ = nullptr; } bool cancelled() const { return isolate_ == nullptr; } void DeregisterTask() { // The task will only be deregistered from the foreground thread (executing // this task or calling its destructor), thus we do not need synchronization // on this field access. if (task_slot_ == nullptr) return; // already deregistered. // Remove this task from the {IsolateInfo} in the engine. The next // logging request will allocate and schedule a new task. base::MutexGuard guard(mutex_); DCHECK_EQ(this, *task_slot_); *task_slot_ = nullptr; task_slot_ = nullptr; } private: // The mutex of the WasmEngine. base::Mutex* const mutex_; // The slot in the WasmEngine where this LogCodesTask is stored. This is // cleared by this task before execution or on task destruction. LogCodesTask** task_slot_; Isolate* isolate_; std::vector code_to_log_; }; } // namespace struct WasmEngine::IsolateInfo { explicit IsolateInfo(Isolate* isolate) : log_codes(WasmCode::ShouldBeLogged(isolate)) { v8::Isolate* v8_isolate = reinterpret_cast(isolate); v8::Platform* platform = V8::GetCurrentPlatform(); foreground_task_runner = platform->GetForegroundTaskRunner(v8_isolate); } // All native modules that are being used by this Isolate (currently only // grows, never shrinks). std::set native_modules; // Caches whether code needs to be logged on this isolate. bool log_codes; // The currently scheduled LogCodesTask. LogCodesTask* log_codes_task = nullptr; // The foreground task runner of the isolate (can be called from background). std::shared_ptr foreground_task_runner; }; WasmEngine::WasmEngine() : code_manager_(&memory_tracker_, FLAG_wasm_max_code_space * MB) {} WasmEngine::~WasmEngine() { // Synchronize on all background compile tasks. background_compile_task_manager_.CancelAndWait(); // All AsyncCompileJobs have been canceled. DCHECK(async_compile_jobs_.empty()); // All Isolates have been deregistered. DCHECK(isolates_.empty()); // All NativeModules did die. DCHECK(isolates_per_native_module_.empty()); } bool WasmEngine::SyncValidate(Isolate* isolate, const WasmFeatures& enabled, const ModuleWireBytes& bytes) { // TODO(titzer): remove dependency on the isolate. if (bytes.start() == nullptr || bytes.length() == 0) return false; ModuleResult result = DecodeWasmModule(enabled, bytes.start(), bytes.end(), true, kWasmOrigin, isolate->counters(), allocator()); return result.ok(); } MaybeHandle WasmEngine::SyncCompileTranslatedAsmJs( Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes, Vector asm_js_offset_table_bytes, Handle uses_bitset) { ModuleResult result = DecodeWasmModule(kAsmjsWasmFeatures, bytes.start(), bytes.end(), false, kAsmJsOrigin, isolate->counters(), allocator()); CHECK(!result.failed()); // Transfer ownership of the WasmModule to the {Managed} generated // in {CompileToNativeModule}. Handle export_wrappers; std::unique_ptr native_module = CompileToNativeModule(isolate, kAsmjsWasmFeatures, thrower, std::move(result).value(), bytes, &export_wrappers); if (!native_module) return {}; // Create heap objects for asm.js offset table to be stored in the module // object. Handle asm_js_offset_table = isolate->factory()->NewByteArray(asm_js_offset_table_bytes.length()); asm_js_offset_table->copy_in(0, asm_js_offset_table_bytes.start(), asm_js_offset_table_bytes.length()); return AsmWasmData::New(isolate, std::move(native_module), export_wrappers, asm_js_offset_table, uses_bitset); } Handle WasmEngine::FinalizeTranslatedAsmJs( Isolate* isolate, Handle asm_wasm_data, Handle