#include "src/wasm/wasm-engine.h"
#include <optional>
#include "src/base/hashing.h"
#include "src/base/platform/time.h"
#include "src/base/small-vector.h"
#include "src/common/assert-scope.h"
#include "src/common/globals.h"
#include "src/debug/debug.h"
#include "src/diagnostics/code-tracer.h"
#include "src/diagnostics/compilation-statistics.h"
#include "src/execution/frames.h"
#include "src/execution/v8threads.h"
#include "src/handles/global-handles-inl.h"
#include "src/logging/counters.h"
#include "src/logging/metrics.h"
#include "src/objects/heap-number.h"
#include "src/objects/managed-inl.h"
#include "src/objects/objects-inl.h"
#include "src/objects/objects.h"
#include "src/objects/primitive-heap-object.h"
#include "src/utils/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/names-provider.h"
#include "src/wasm/pgo.h"
#include "src/wasm/stacks.h"
#include "src/wasm/std-object-sizes.h"
#include "src/wasm/streaming-decoder.h"
#include "src/wasm/wasm-code-pointer-table.h"
#include "src/wasm/wasm-debug.h"
#include "src/wasm/wasm-limits.h"
#include "src/wasm/wasm-objects-inl.h"
#if V8_ENABLE_DRUMBRAKE
#include "src/wasm/interpreter/wasm-interpreter-inl.h"
#endif
#ifdef V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
#include "src/debug/wasm/gdb-server/gdb-server.h"
#endif
namespace v8::internal::wasm {
#define TRACE_CODE_GC(...) \
do { \
if (v8_flags.trace_wasm_code_gc) PrintF("[wasm-gc] " __VA_ARGS__); \
} while (false)
class WasmOrphanedGlobalHandle {
public:
WasmOrphanedGlobalHandle() = default;
void InitializeLocation(std::unique_ptr<Address*> location) {
location_ = std::move(location);
}
static void Destroy(WasmOrphanedGlobalHandle* that) {
Address** location = that->location_.get();
if (location) GlobalHandles::Destroy(*location);
that->location_.reset();
*that->prev_ptr_ = that->next_;
if (that->next_ != nullptr) that->next_->prev_ptr_ = that->prev_ptr_;
delete that;
}
private:
friend class WasmEngine;
WasmOrphanedGlobalHandle* next_ = nullptr;
WasmOrphanedGlobalHandle** prev_ptr_ = nullptr;
std::unique_ptr<Address*> location_;
};
std::atomic<bool> WasmEngine::had_nondeterminism_{false};
WasmOrphanedGlobalHandle* WasmEngine::NewOrphanedGlobalHandle(
WasmOrphanedGlobalHandle** pointer) {
WasmOrphanedGlobalHandle* orphan = new WasmOrphanedGlobalHandle();
orphan->next_ = *pointer;
orphan->prev_ptr_ = pointer;
if (orphan->next_ != nullptr) orphan->next_->prev_ptr_ = &orphan->next_;
*pointer = orphan;
return orphan;
}
void WasmEngine::FreeAllOrphanedGlobalHandles(WasmOrphanedGlobalHandle* start) {
while (start != nullptr) {
WasmOrphanedGlobalHandle* next = start->next_;
delete start;
start = next;
}
}
size_t WasmEngine::NativeModuleCount() const {
base::MutexGuard guard(&mutex_);
return native_modules_.size();
}
class WasmEngine::LogCodesTask : public CancelableTask {
friend class WasmEngine;
public:
explicit LogCodesTask(Isolate* isolate)
: CancelableTask(isolate), isolate_(isolate) {}
void RunInternal() override {
GetWasmEngine()->LogOutstandingCodesForIsolate(isolate_);
}
private:
Isolate* const isolate_;
};
namespace {
void CheckNoArchivedThreads(Isolate* isolate) {
class ArchivedThreadsVisitor : public ThreadVisitor {
void VisitThread(Isolate* isolate, ThreadLocalTop* top) override {
FATAL("archived threads in combination with wasm not supported");
}
} archived_threads_visitor;
isolate->thread_manager()->IterateArchivedThreads(&archived_threads_visitor);
}
class WasmGCForegroundTask : public CancelableTask {
public:
explicit WasmGCForegroundTask(Isolate* isolate)
: CancelableTask(isolate->cancelable_task_manager()), isolate_(isolate) {}
void RunInternal() final {
GetWasmEngine()->ReportLiveCodeFromStackForGC(isolate_);
}
private:
Isolate* isolate_;
};
class ClearWeakScriptHandleTask : public CancelableTask {
public:
explicit ClearWeakScriptHandleTask(Isolate* isolate,
std::unique_ptr<Address*> location)
: CancelableTask(isolate->cancelable_task_manager()) {
handle_ = isolate->NewWasmOrphanedGlobalHandle();
handle_->InitializeLocation(std::move(location));
}
void RunInternal() override {
WasmOrphanedGlobalHandle::Destroy(handle_);
handle_ = nullptr;
}
private:
WasmOrphanedGlobalHandle* handle_;
};
class WeakScriptHandle {
public:
WeakScriptHandle(DirectHandle<Script> script, Isolate* isolate)
: script_id_(script->id()), isolate_(isolate) {
DCHECK(IsString(script->name()) || IsUndefined(script->name()));
if (IsString(script->name())) {
source_url_ = Cast<String>(script->name())->ToCString();
}
auto global_handle = isolate->global_handles()->Create(*script);
location_ = std::make_unique<Address*>(global_handle.location());
GlobalHandles::MakeWeak(location_.get());
}
~WeakScriptHandle() {
if (location_ == nullptr || *location_ == nullptr) return;
GetWasmEngine()->ClearWeakScriptHandle(isolate_, std::move(location_));
}
WeakScriptHandle(WeakScriptHandle&&) V8_NOEXCEPT = default;
DirectHandle<Script> handle() const {
return DirectHandle<Script>::FromSlot(*location_);
}
void Clear() { location_.reset(); }
int script_id() const { return script_id_; }
const std::shared_ptr<const char[]>& source_url() const {
return source_url_;
}
private:
std::unique_ptr<Address*> location_;
int script_id_;
std::shared_ptr<const char[]> source_url_;
Isolate* isolate_;
};
std::vector<std::shared_ptr<NativeModule>>* native_modules_kept_alive_for_pgo;
}
std::shared_ptr<NativeModule> NativeModuleCache::MaybeGetNativeModule(
ModuleOrigin origin, base::Vector<const uint8_t> wire_bytes,
const CompileTimeImports& compile_imports) {
if (!v8_flags.wasm_native_module_cache) return nullptr;
if (origin != kWasmOrigin) return nullptr;
base::MutexGuard lock(&mutex_);
size_t prefix_hash = PrefixHash(wire_bytes);
NativeModuleCache::Key key{prefix_hash, compile_imports, wire_bytes};
while (true) {
auto it = map_.find(key);
if (it == map_.end()) {
[[maybe_unused]] auto [iterator, inserted] =
map_.emplace(key, std::nullopt);
DCHECK(inserted);
return nullptr;
}
if (it->second.has_value()) {
if (auto shared_native_module = it->second.value().lock()) {
DCHECK_EQ(
shared_native_module->compile_imports().compare(compile_imports),
0);
DCHECK_EQ(shared_native_module->wire_bytes(), wire_bytes);
return shared_native_module;
}
}
cache_cv_.Wait(&mutex_);
}
}
bool NativeModuleCache::GetStreamingCompilationOwnership(
size_t prefix_hash, const CompileTimeImports& compile_imports) {
if (!v8_flags.wasm_native_module_cache) return true;
base::MutexGuard lock(&mutex_);
auto it = map_.lower_bound(Key{prefix_hash, compile_imports, {}});
if (it != map_.end() && it->first.prefix_hash == prefix_hash) {
DCHECK_IMPLIES(!it->first.bytes.empty(),
PrefixHash(it->first.bytes) == prefix_hash);
return false;
}
Key key{prefix_hash, compile_imports, {}};
DCHECK(!map_.contains(key));
map_.emplace(key, std::nullopt);
return true;
}
void NativeModuleCache::StreamingCompilationFailed(
size_t prefix_hash, const CompileTimeImports& compile_imports) {
if (!v8_flags.wasm_native_module_cache) return;
base::MutexGuard lock(&mutex_);
Key key{prefix_hash, compile_imports, {}};
map_.erase(key);
cache_cv_.NotifyAll();
}
std::shared_ptr<NativeModule> NativeModuleCache::Update(
std::shared_ptr<NativeModule> native_module, bool error) {
DCHECK_NOT_NULL(native_module);
if (!v8_flags.wasm_native_module_cache) return native_module;
if (native_module->module()->origin != kWasmOrigin) return native_module;
base::Vector<const uint8_t> wire_bytes = native_module->wire_bytes();
DCHECK(!wire_bytes.empty());
size_t prefix_hash = PrefixHash(native_module->wire_bytes());
base::MutexGuard lock(&mutex_);
const CompileTimeImports& compile_imports = native_module->compile_imports();
map_.erase(Key{prefix_hash, compile_imports, {}});
const Key key{prefix_hash, compile_imports, wire_bytes};
auto it = map_.find(key);
if (it != map_.end()) {
if (it->second.has_value()) {
auto conflicting_module = it->second.value().lock();
if (conflicting_module != nullptr) {
DCHECK_EQ(conflicting_module->wire_bytes(), wire_bytes);
return conflicting_module;
}
}
map_.erase(it);
}
if (!error) {
[[maybe_unused]] auto [iterator, inserted] = map_.emplace(
key, std::optional<std::weak_ptr<NativeModule>>(native_module));
DCHECK(inserted);
}
cache_cv_.NotifyAll();
return native_module;
}
void NativeModuleCache::Erase(NativeModule* native_module) {
if (!v8_flags.wasm_native_module_cache) return;
if (native_module->module()->origin != kWasmOrigin) return;
if (native_module->wire_bytes().empty()) return;
base::MutexGuard lock(&mutex_);
size_t prefix_hash = PrefixHash(native_module->wire_bytes());
map_.erase(Key{prefix_hash, native_module->compile_imports(),
native_module->wire_bytes()});
cache_cv_.NotifyAll();
}
size_t NativeModuleCache::PrefixHash(base::Vector<const uint8_t> wire_bytes) {
Decoder decoder(wire_bytes.begin(), wire_bytes.end());
decoder.consume_bytes(8, "module header");
SectionCode section_id = SectionCode::kUnknownSectionCode;
base::Hasher hasher;
while (decoder.ok() && decoder.more()) {
section_id = static_cast<SectionCode>(decoder.consume_u8());
uint32_t section_size = decoder.consume_u32v("section size");
if (section_id == SectionCode::kCodeSectionCode) {
hasher.Add(section_size);
break;
}
const uint8_t* payload_start = decoder.pc();
decoder.consume_bytes(section_size, "section payload");
hasher.AddRange(base::VectorOf(payload_start, section_size));
}
return hasher.hash();
}
struct WasmEngine::CurrentGCInfo {
explicit CurrentGCInfo(int8_t gc_sequence_index)
: gc_sequence_index(gc_sequence_index) {
DCHECK_NE(0, gc_sequence_index);
}
std::unordered_map<Isolate*, WasmGCForegroundTask*> outstanding_isolates;
std::unordered_set<WasmCode*> dead_code;
const int8_t gc_sequence_index;
int8_t next_gc_sequence_index = 0;
base::TimeTicks start_time;
};
struct WasmEngine::IsolateInfo {
IsolateInfo(Isolate* isolate, bool log_code)
: log_codes(log_code), async_counters(isolate->async_counters()) {
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
v8::Platform* platform = V8::GetCurrentPlatform();
foreground_task_runner = platform->GetForegroundTaskRunner(v8_isolate);
}
~IsolateInfo() {
DCHECK_EQ(0, code_to_log.size());
for (auto& [native_module, script_handle] : scripts) {
script_handle.Clear();
}
}
std::unordered_set<NativeModule*> native_modules;
std::unordered_map<NativeModule*, WeakScriptHandle> scripts;
bool log_codes;
struct CodeToLogPerScript {
std::vector<WasmCode*> code;
std::shared_ptr<NativeModule> native_module;
std::shared_ptr<const char[]> source_url;
};
std::unordered_map<int, CodeToLogPerScript> code_to_log;
std::shared_ptr<v8::TaskRunner> foreground_task_runner;
const std::shared_ptr<Counters> async_counters;
bool keep_in_debug_state = false;
};
void WasmEngine::ClearWeakScriptHandle(Isolate* isolate,
std::unique_ptr<Address*> location) {
mutex_.AssertHeld();
IsolateInfo* isolate_info = isolates_[isolate].get();
std::shared_ptr<TaskRunner> runner = isolate_info->foreground_task_runner;
runner->PostTask(std::make_unique<ClearWeakScriptHandleTask>(
isolate, std::move(location)));
}
struct WasmEngine::NativeModuleInfo {
explicit NativeModuleInfo(std::weak_ptr<NativeModule> native_module)
: weak_ptr(std::move(native_module)) {}
std::weak_ptr<NativeModule> weak_ptr;
std::unordered_set<Isolate*> isolates;
};
WasmEngine::WasmEngine()
#ifdef V8_ENABLE_TURBOFAN
: call_descriptors_(&allocator_)
#endif
{
}
WasmEngine::~WasmEngine() {
#ifdef V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
gdb_server_.reset();
#endif
if (V8_UNLIKELY(v8_flags.print_wasm_offheap_memory_size)) {
PrintCurrentMemoryConsumptionEstimate();
}
if (V8_UNLIKELY(native_modules_kept_alive_for_pgo)) {
delete native_modules_kept_alive_for_pgo;
}
operations_barrier_->CancelAndWait();
for (WasmCode* code : potentially_dead_code_) {
code->DcheckRefCountIsOne();
delete code;
}
DCHECK(async_compile_jobs_.empty());
DCHECK(isolates_.empty());
DCHECK(native_modules_.empty());
DCHECK(native_module_cache_.empty());
}
bool WasmEngine::SyncValidate(Isolate* isolate, WasmEnabledFeatures enabled,
CompileTimeImports compile_imports,
base::Vector<const uint8_t> bytes) {
TRACE_EVENT0("v8.wasm", "wasm.SyncValidate");
if (bytes.empty()) return false;
WasmDetectedFeatures unused_detected_features;
auto result =
DecodeWasmModule(isolate, enabled, bytes, true, kWasmOrigin,
DecodingMethod::kSync, &unused_detected_features);
if (result.failed()) return false;
WasmError error = ValidateAndSetBuiltinImports(
result.value().get(), bytes, compile_imports, &unused_detected_features);
return !error.has_error();
}
MaybeHandle<AsmWasmData> WasmEngine::SyncCompileTranslatedAsmJs(
Isolate* isolate, ErrorThrower* thrower,
base::OwnedVector<const uint8_t> bytes, DirectHandle<Script> script,
base::Vector<const uint8_t> asm_js_offset_table_bytes,
DirectHandle<HeapNumber> uses_bitset, LanguageMode language_mode) {
int compilation_id = next_compilation_id_.fetch_add(1);
TRACE_EVENT1("v8.wasm", "wasm.SyncCompileTranslatedAsmJs", "id",
compilation_id);
ModuleOrigin origin = language_mode == LanguageMode::kSloppy
? kAsmJsSloppyOrigin
: kAsmJsStrictOrigin;
WasmDetectedFeatures detected_features;
ModuleResult result = DecodeWasmModule(
isolate, WasmEnabledFeatures::ForAsmjs(), bytes.as_vector(), false,
origin, DecodingMethod::kSync, &detected_features);
if (result.failed()) {
std::cout << result.error().message();
UNREACHABLE();
}
result.value()->asm_js_offset_information =
std::make_unique<AsmJsOffsetInformation>(asm_js_offset_table_bytes);
constexpr ProfileInformation* kNoProfileInformation = nullptr;
v8::metrics::Recorder::ContextId context_id =
isolate->GetOrRegisterRecorderContextId(isolate->native_context());
std::shared_ptr<NativeModule> native_module = CompileToNativeModule(
isolate, WasmEnabledFeatures::ForAsmjs(), detected_features,
CompileTimeImports{}, thrower, std::move(result).value(),
std::move(bytes), compilation_id, context_id, kNoProfileInformation);
if (!native_module) return {};
native_module->LogWasmCodes(isolate, *script);
{
base::MutexGuard guard(&mutex_);
DCHECK(isolates_.contains(isolate));
auto& scripts = isolates_[isolate]->scripts;
if (!scripts.contains(native_module.get())) {
scripts.emplace(native_module.get(), WeakScriptHandle(script, isolate));
}
}
return AsmWasmData::New(isolate, std::move(native_module), uses_bitset);
}
DirectHandle<WasmModuleObject> WasmEngine::FinalizeTranslatedAsmJs(
Isolate* isolate, DirectHandle<AsmWasmData> asm_wasm_data,
DirectHandle<Script> script) {
std::shared_ptr<NativeModule> native_module =
asm_wasm_data->managed_native_module()->get();
DirectHandle<WasmModuleObject> module_object =
WasmModuleObject::New(isolate, std::move(native_module), script);
return module_object;
}
MaybeDirectHandle<WasmModuleObject> WasmEngine::SyncCompile(
Isolate* isolate, WasmEnabledFeatures enabled_features,
CompileTimeImports compile_imports, ErrorThrower* thrower,
base::OwnedVector<const uint8_t> bytes) {
int compilation_id = next_compilation_id_.fetch_add(1);
TRACE_EVENT1("v8.wasm", "wasm.SyncCompile", "id", compilation_id);
v8::metrics::Recorder::ContextId context_id =
isolate->GetOrRegisterRecorderContextId(isolate->native_context());
std::shared_ptr<WasmModule> module;
WasmDetectedFeatures detected_features;
{
bool validate_module = v8_flags.wasm_jitless;
ModuleResult result = DecodeWasmModule(
isolate, enabled_features, bytes.as_vector(), validate_module,
kWasmOrigin, DecodingMethod::kSync, &detected_features);
if (result.failed()) {
thrower->CompileFailed(result.error());
return {};
}
module = std::move(result).value();
if (WasmError error =
ValidateAndSetBuiltinImports(module.get(), bytes.as_vector(),
compile_imports, &detected_features)) {
thrower->CompileError("%s @+%u", error.message().c_str(), error.offset());
return {};
}
}
std::unique_ptr<ProfileInformation> pgo_info;
if (V8_UNLIKELY(v8_flags.experimental_wasm_pgo_from_file)) {
pgo_info = LoadProfileFromFile(module.get(), bytes.as_vector());
}
std::shared_ptr<NativeModule> native_module = CompileToNativeModule(
isolate, enabled_features, detected_features, std::move(compile_imports),
thrower, std::move(module), std::move(bytes), compilation_id, context_id,
pgo_info.get());
if (!native_module) return {};
#ifdef DEBUG
{
base::MutexGuard lock(&mutex_);
DCHECK(isolates_.contains(isolate));
DCHECK(isolates_[isolate]->native_modules.contains(native_module.get()));
DCHECK(native_modules_.contains(native_module.get()));
DCHECK(native_modules_[native_module.get()]->isolates.contains(isolate));
}
#endif
constexpr base::Vector<const char> kNoSourceUrl;
DirectHandle<Script> script =
GetOrCreateScript(isolate, native_module, kNoSourceUrl);
native_module->LogWasmCodes(isolate, *script);
DirectHandle<WasmModuleObject> module_object =
WasmModuleObject::New(isolate, std::move(native_module), script);
isolate->debug()->OnAfterCompile(script);
return module_object;
}
MaybeDirectHandle<WasmInstanceObject> WasmEngine::SyncInstantiate(
Isolate* isolate, ErrorThrower* thrower,
DirectHandle<WasmModuleObject> module_object,
MaybeDirectHandle<JSReceiver> imports,
MaybeDirectHandle<JSArrayBuffer> memory) {
TRACE_EVENT0("v8.wasm", "wasm.SyncInstantiate");
return InstantiateToInstanceObject(isolate, thrower, module_object, imports,
memory);
}
void WasmEngine::AsyncInstantiate(
Isolate* isolate, std::unique_ptr<InstantiationResultResolver> resolver,
DirectHandle<WasmModuleObject> module_object,
MaybeDirectHandle<JSReceiver> imports) {
ErrorThrower thrower(isolate, "WebAssembly.instantiate()");
TRACE_EVENT0("v8.wasm", "wasm.AsyncInstantiate");
v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate));
catcher.SetVerbose(false);
catcher.SetCaptureMessage(false);
MaybeDirectHandle<WasmInstanceObject> instance_object =
SyncInstantiate(isolate, &thrower, module_object, imports,
DirectHandle<JSArrayBuffer>::null());
if (!instance_object.is_null()) {
resolver->OnInstantiationSucceeded(instance_object.ToHandleChecked());
return;
}
if (isolate->has_exception()) {
thrower.Reset();
if (isolate->is_execution_terminating()) return;
DirectHandle<JSAny> exception(Cast<JSAny>(isolate->exception()), isolate);
isolate->clear_exception();
resolver->OnInstantiationFailed(exception);
} else {
DCHECK(thrower.error());
resolver->OnInstantiationFailed(thrower.Reify());
}
}
void WasmEngine::AsyncCompile(
Isolate* isolate, WasmEnabledFeatures enabled,
CompileTimeImports compile_imports,
std::shared_ptr<CompilationResultResolver> resolver,
base::OwnedVector<const uint8_t> bytes,
const char* api_method_name_for_errors) {
int compilation_id = next_compilation_id_.fetch_add(1);
TRACE_EVENT1("v8.wasm", "wasm.AsyncCompile", "id", compilation_id);
if (!v8_flags.wasm_async_compilation || v8_flags.wasm_jitless) {
ErrorThrower thrower(isolate, api_method_name_for_errors);
MaybeDirectHandle<WasmModuleObject> module_object;
module_object = SyncCompile(isolate, enabled, std::move(compile_imports),
&thrower, std::move(bytes));
if (thrower.error()) {
resolver->OnCompilationFailed(thrower.Reify());
return;
}
DirectHandle<WasmModuleObject> module = module_object.ToHandleChecked();
resolver->OnCompilationSucceeded(module);
return;
}
if (v8_flags.wasm_test_streaming) {
std::shared_ptr<StreamingDecoder> streaming_decoder =
StartStreamingCompilation(enabled, std::move(compile_imports),
api_method_name_for_errors,
std::move(resolver));
streaming_decoder->InitializeIsolateSpecificInfo(isolate);
auto* rng = isolate->random_number_generator();
base::SmallVector<base::Vector<const uint8_t>, 16> ranges;
if (!bytes.empty()) ranges.push_back(bytes.as_vector());
for (int round = 0; round < 4; ++round) {
for (auto it = ranges.begin(); it != ranges.end(); ++it) {
auto range = *it;
if (range.size() < 2 || !rng->NextBool()) continue;
static_assert(kV8MaxWasmModuleSize <= kMaxInt);
size_t split_point =
1 + rng->NextInt(static_cast<int>(range.size() - 1));
it = ranges.insert(it, range.SubVector(0, split_point)) + 1;
*it = range.SubVectorFrom(split_point);
}
}
for (auto range : ranges) {
streaming_decoder->OnBytesReceived(range);
}
streaming_decoder->Finish({});
return;
}
AsyncCompileJob* job = CreateAsyncCompileJob(
enabled, std::move(compile_imports), std::move(bytes),
api_method_name_for_errors, std::move(resolver), compilation_id);
job->InitializeIsolateSpecificInfo(isolate);
job->StartAsyncDecoding();
}
std::shared_ptr<StreamingDecoder> WasmEngine::StartStreamingCompilation(
WasmEnabledFeatures enabled, CompileTimeImports compile_imports,
const char* api_method_name,
std::shared_ptr<CompilationResultResolver> resolver) {
int compilation_id = next_compilation_id_.fetch_add(1);
TRACE_EVENT1("v8.wasm", "wasm.StartStreamingCompilation", "id",
compilation_id);
if (v8_flags.wasm_async_compilation) {
AsyncCompileJob* job = CreateAsyncCompileJob(
enabled, std::move(compile_imports), {}, api_method_name,
std::move(resolver), compilation_id);
return job->CreateStreamingDecoder();
}
return StreamingDecoder::CreateSyncStreamingDecoder(
enabled, std::move(compile_imports), api_method_name,
std::move(resolver));
}
void WasmEngine::CompileFunction(NativeModule* native_module,
uint32_t function_index, ExecutionTier tier) {
DCHECK(!v8_flags.wasm_jitless);
WasmDetectedFeatures detected;
WasmCompilationUnit::CompileWasmFunction(
native_module, &detected,
&native_module->module()->functions[function_index], tier);
}
void WasmEngine::EnterDebuggingForIsolate(Isolate* isolate) {
if (v8_flags.wasm_jitless) return;
std::vector<std::shared_ptr<NativeModule>> native_modules;
{
base::MutexGuard lock(&mutex_);
IsolateInfo* isolate_info = isolates_[isolate].get();
if (isolate_info->keep_in_debug_state) return;
isolate_info->keep_in_debug_state = true;
for (auto* native_module : isolate_info->native_modules) {
DCHECK(native_modules_.contains(native_module));
if (auto shared_ptr = native_modules_[native_module]->weak_ptr.lock()) {
native_modules.emplace_back(std::move(shared_ptr));
}
native_module->SetDebugState(kDebugging);
}
}
WasmCodeRefScope ref_scope;
for (auto& native_module : native_modules) {
native_module->RemoveCompiledCode(
NativeModule::RemoveFilter::kRemoveNonDebugCode);
}
}
void WasmEngine::LeaveDebuggingForIsolate(Isolate* isolate) {
std::vector<std::pair<std::shared_ptr<NativeModule>, bool>> native_modules;
{
base::MutexGuard lock(&mutex_);
isolates_[isolate]->keep_in_debug_state = false;
auto can_remove_debug_code = [this](NativeModule* native_module) {
DCHECK(native_modules_.contains(native_module));
for (auto* isolate : native_modules_[native_module]->isolates) {
DCHECK(isolates_.contains(isolate));
if (isolates_[isolate]->keep_in_debug_state) return false;
}
return true;
};
for (auto* native_module : isolates_[isolate]->native_modules) {
DCHECK(native_modules_.contains(native_module));
auto shared_ptr = native_modules_[native_module]->weak_ptr.lock();
if (!shared_ptr) continue;
if (!native_module->IsInDebugState()) continue;
bool remove_debug_code = can_remove_debug_code(native_module);
if (remove_debug_code) native_module->SetDebugState(kNotDebugging);
native_modules.emplace_back(std::move(shared_ptr), remove_debug_code);
}
}
for (auto& entry : native_modules) {
auto& native_module = entry.first;
bool remove_debug_code = entry.second;
if (native_module->HasDebugInfo()) {
native_module->GetDebugInfo()->RemoveIsolate(isolate);
}
if (remove_debug_code) {
WasmCodeRefScope ref_scope;
native_module->RemoveCompiledCode(
NativeModule::RemoveFilter::kRemoveDebugCode);
}
}
}
namespace {
DirectHandle<Script> CreateWasmScript(
Isolate* isolate, std::shared_ptr<NativeModule> native_module,
base::Vector<const char> source_url) {
base::Vector<const uint8_t> wire_bytes = native_module->wire_bytes();
const WasmModule* module = native_module->module();
DirectHandle<String> url_str;
if (!source_url.empty()) {
url_str = isolate->factory()
->NewStringFromUtf8(source_url, AllocationType::kOld)
.ToHandleChecked();
} else {
uint32_t hash = static_cast<uint32_t>(GetWireBytesHash(wire_bytes));
base::EmbeddedVector<char, 32> buffer;
if (module->name.is_empty()) {
int url_len = SNPrintF(buffer, "wasm://wasm/%08x", hash);
DCHECK(url_len >= 0 && url_len < buffer.length());
url_str = isolate->factory()
->NewStringFromUtf8(buffer.SubVector(0, url_len),
AllocationType::kOld)
.ToHandleChecked();
} else {
int hash_len = SNPrintF(buffer, "-%08x", hash);
DCHECK(hash_len >= 0 && hash_len < buffer.length());
DirectHandle<String> prefix =
isolate->factory()->NewStringFromStaticChars("wasm://wasm/");
DirectHandle<String> module_name =
WasmModuleObject::ExtractUtf8StringFromModuleBytes(
isolate, wire_bytes, module->name, kNoInternalize);
DirectHandle<String> hash_str =
isolate->factory()
->NewStringFromUtf8(buffer.SubVector(0, hash_len))
.ToHandleChecked();
url_str = isolate->factory()
->NewConsString(prefix, module_name)
.ToHandleChecked();
url_str = isolate->factory()
->NewConsString(url_str, hash_str)
.ToHandleChecked();
}
}
DirectHandle<PrimitiveHeapObject> source_map_url =
isolate->factory()->undefined_value();
if (module->debug_symbols[WasmDebugSymbols::Type::SourceMap].type !=
WasmDebugSymbols::Type::None) {
auto source_map_symbols =
module->debug_symbols[WasmDebugSymbols::Type::SourceMap];
base::Vector<const char> external_url =
ModuleWireBytes(wire_bytes)
.GetNameOrNull(source_map_symbols.external_url);
MaybeDirectHandle<String> src_map_str =
isolate->factory()->NewStringFromUtf8(external_url,
AllocationType::kOld);
source_map_url = src_map_str.ToHandleChecked();
}
size_t code_size_estimate = native_module->committed_code_space();
size_t memory_estimate =
code_size_estimate +
wasm::WasmCodeManager::EstimateNativeModuleMetaDataSize(module);
DirectHandle<Managed<wasm::NativeModule>> managed_native_module =
Managed<wasm::NativeModule>::From(isolate, memory_estimate,
std::move(native_module));
DirectHandle<Script> script =
isolate->factory()->NewScript(isolate->factory()->undefined_value());
{
DisallowGarbageCollection no_gc;
Tagged<Script> raw_script = *script;
raw_script->set_compilation_state(Script::CompilationState::kCompiled);
raw_script->set_context_data(isolate->native_context()->debug_context_id());
raw_script->set_name(*url_str);
raw_script->set_type(Script::Type::kWasm);
raw_script->set_source_mapping_url(*source_map_url);
raw_script->set_line_ends(ReadOnlyRoots(isolate).empty_fixed_array(),
SKIP_WRITE_BARRIER);
raw_script->set_wasm_managed_native_module(*managed_native_module);
raw_script->set_wasm_breakpoint_infos(
ReadOnlyRoots(isolate).empty_fixed_array(), SKIP_WRITE_BARRIER);
raw_script->set_wasm_weak_instance_list(
ReadOnlyRoots(isolate).empty_weak_array_list(), SKIP_WRITE_BARRIER);
static constexpr bool kIsSharedCrossOrigin = true;
static constexpr bool kIsOpaque = false;
static constexpr bool kIsWasm = true;
static constexpr bool kIsModule = false;
raw_script->set_origin_options(ScriptOriginOptions(
kIsSharedCrossOrigin, kIsOpaque, kIsWasm, kIsModule));
}
return script;
}
}
DirectHandle<WasmModuleObject> WasmEngine::ImportNativeModule(
Isolate* isolate, std::shared_ptr<NativeModule> shared_native_module,
base::Vector<const char> source_url) {
NativeModule* native_module = shared_native_module.get();
ModuleWireBytes wire_bytes(native_module->wire_bytes());
DirectHandle<Script> script =
GetOrCreateScript(isolate, shared_native_module, source_url);
native_module->LogWasmCodes(isolate, *script);
DirectHandle<WasmModuleObject> module_object =
WasmModuleObject::New(isolate, std::move(shared_native_module), script);
{
base::MutexGuard lock(&mutex_);
DCHECK(isolates_.contains(isolate));
IsolateInfo* isolate_info = isolates_.find(isolate)->second.get();
isolate_info->native_modules.insert(native_module);
DCHECK(native_modules_.contains(native_module));
native_modules_[native_module]->isolates.insert(isolate);
if (isolate_info->log_codes && !native_module->log_code()) {
EnableCodeLogging(native_module);
}
}
isolate->debug()->OnAfterCompile(script);
return module_object;
}
void WasmEngine::FlushLiftoffCode() {
DCHECK(v8_flags.flush_liftoff_code);
std::vector<std::shared_ptr<NativeModule>> native_modules;
WasmCodeRefScope ref_scope;
base::MutexGuard guard(&mutex_);
for (auto& [native_module, info] : native_modules_) {
std::shared_ptr<NativeModule> shared = info->weak_ptr.lock();
if (!shared) continue;
native_module->RemoveCompiledCode(
NativeModule::RemoveFilter::kRemoveLiftoffCode);
native_modules.emplace_back(std::move(shared));
}
}
size_t WasmEngine::GetLiftoffCodeSizeForTesting() {
base::MutexGuard guard(&mutex_);
size_t codesize_liftoff = 0;
for (auto& [native_module, info] : native_modules_) {
codesize_liftoff += native_module->SumLiftoffCodeSizeForTesting();
}
return codesize_liftoff;
}
std::shared_ptr<CompilationStatistics>
WasmEngine::GetOrCreateTurboStatistics() {
base::MutexGuard guard(&mutex_);
if (compilation_stats_ == nullptr) {
compilation_stats_.reset(new CompilationStatistics());
}
return compilation_stats_;
}
void WasmEngine::DumpAndResetTurboStatistics() {
base::MutexGuard guard(&mutex_);
if (compilation_stats_ != nullptr) {
StdoutStream os;
os << AsPrintableStatistics{"Turbofan Wasm", *compilation_stats_, false}
<< std::endl;
}
compilation_stats_.reset();
}
void WasmEngine::DumpTurboStatistics() {
base::MutexGuard guard(&mutex_);
if (compilation_stats_ != nullptr) {
StdoutStream os;
os << AsPrintableStatistics{"Turbofan Wasm", *compilation_stats_, false}
<< std::endl;
}
}
CodeTracer* WasmEngine::GetCodeTracer() {
base::MutexGuard guard(&mutex_);
if (code_tracer_ == nullptr) code_tracer_.reset(new CodeTracer(-1));
return code_tracer_.get();
}
AsyncCompileJob* WasmEngine::CreateAsyncCompileJob(
WasmEnabledFeatures enabled, CompileTimeImports compile_imports,
base::OwnedVector<const uint8_t> bytes, const char* api_method_name,
std::shared_ptr<CompilationResultResolver> resolver, int compilation_id) {
AsyncCompileJob* job =
new AsyncCompileJob(enabled, std::move(compile_imports), std::move(bytes),
api_method_name, std::move(resolver), compilation_id);
base::MutexGuard guard(&mutex_);
async_compile_jobs_[job] = std::unique_ptr<AsyncCompileJob>(job);
return job;
}
std::unique_ptr<AsyncCompileJob> WasmEngine::RemoveCompileJob(
AsyncCompileJob* job) {
base::MutexGuard guard(&mutex_);
auto item = async_compile_jobs_.find(job);
DCHECK(item != async_compile_jobs_.end());
std::unique_ptr<AsyncCompileJob> result = std::move(item->second);
async_compile_jobs_.erase(item);
return result;
}
bool WasmEngine::HasRunningCompileJob(Isolate* isolate) {
base::MutexGuard guard(&mutex_);
DCHECK(isolates_.contains(isolate));
for (auto& entry : async_compile_jobs_) {
if (entry.first->isolate() == isolate) return true;
}
return false;
}
void WasmEngine::DeleteCompileJobsOnContext(DirectHandle<Context> context) {
std::vector<std::unique_ptr<AsyncCompileJob>> jobs_to_delete;
{
base::MutexGuard guard(&mutex_);
for (auto it = async_compile_jobs_.begin();
it != async_compile_jobs_.end();) {
DirectHandle<NativeContext> job_context;
if (it->first->context().ToHandle(&job_context) &&
job_context.is_identical_to(context)) {
jobs_to_delete.push_back(std::move(it->second));
it = async_compile_jobs_.erase(it);
} else {
++it;
}
}
}
}
void WasmEngine::DeleteCompileJobsOnIsolate(Isolate* isolate) {
std::vector<std::unique_ptr<AsyncCompileJob>> jobs_to_delete;
std::vector<std::weak_ptr<NativeModule>> modules_in_isolate;
{
base::MutexGuard guard(&mutex_);
for (auto it = async_compile_jobs_.begin();
it != async_compile_jobs_.end();) {
if (it->first->isolate() == isolate) {
jobs_to_delete.push_back(std::move(it->second));
it = async_compile_jobs_.erase(it);
} else {
++it;
}
}
DCHECK(isolates_.contains(isolate));
auto* isolate_info = isolates_[isolate].get();
for (auto* native_module : isolate_info->native_modules) {
DCHECK(native_modules_.contains(native_module));
modules_in_isolate.emplace_back(native_modules_[native_module]->weak_ptr);
}
}
for (auto& weak_module : modules_in_isolate) {
if (auto shared_module = weak_module.lock()) {
shared_module->compilation_state()->CancelInitialCompilation();
}
}
}
void WasmEngine::AddIsolate(Isolate* isolate) {
const bool log_code = WasmCode::ShouldBeLogged(isolate);
{
auto isolate_info = std::make_unique<IsolateInfo>(isolate, log_code);
base::MutexGuard guard(&mutex_);
DCHECK(!isolates_.contains(isolate));
isolates_.emplace(isolate, std::move(isolate_info));
}
bool has_mpk = WasmCodeManager::HasMemoryProtectionKeySupport();
isolate->counters()->wasm_memory_protection_keys_support()->AddSample(
has_mpk ? 1 : 0);
if (log_code) {
GetWasmImportWrapperCache()->LogForIsolate(isolate);
}
auto callback = [](v8::Isolate* v8_isolate, v8::GCType type,
v8::GCCallbackFlags flags, void* data) {
Isolate* isolate = reinterpret_cast<Isolate*>(v8_isolate);
Counters* counters = isolate->counters();
WasmEngine* engine = GetWasmEngine();
{
base::MutexGuard lock(&engine->mutex_);
DCHECK(engine->isolates_.contains(isolate));
for (auto* native_module : engine->isolates_[isolate]->native_modules) {
native_module->SampleCodeSize(counters);
}
}
Histogram* metadata_histogram = counters->wasm_engine_metadata_size_kb();
if (metadata_histogram->Enabled()) {
size_t engine_meta_data = engine->EstimateCurrentMemoryConsumption();
metadata_histogram->AddSample(static_cast<int>(engine_meta_data / KB));
}
};
isolate->heap()->AddGCEpilogueCallback(callback, v8::kGCTypeMarkSweepCompact,
nullptr);
#ifdef V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
if (gdb_server_) {
gdb_server_->AddIsolate(isolate);
}
#endif
}
void WasmEngine::RemoveIsolate(Isolate* isolate) {
#ifdef V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
if (gdb_server_) {
gdb_server_->RemoveIsolate(isolate);
}
#endif
std::set<std::shared_ptr<NativeModule>> native_modules_with_code_to_log;
WasmCodeRefScope code_ref_scope_for_dead_code;
base::MutexGuard guard(&mutex_);
auto isolates_it = isolates_.find(isolate);
DCHECK_NE(isolates_.end(), isolates_it);
IsolateInfo* isolate_info = isolates_it->second.get();
for (auto* native_module : isolate_info->native_modules) {
DCHECK(native_modules_.contains(native_module));
NativeModuleInfo* native_module_info =
native_modules_.find(native_module)->second.get();
auto has_isolate_with_code_logging = [this, native_module_info] {
return std::any_of(native_module_info->isolates.begin(),
native_module_info->isolates.end(),
[this](Isolate* isolate) {
return isolates_.find(isolate)->second->log_codes;
});
};
DCHECK_EQ(native_module->log_code(), has_isolate_with_code_logging());
DCHECK(native_module_info->isolates.contains(isolate));
native_module_info->isolates.erase(isolate);
if (native_module->log_code() && !has_isolate_with_code_logging()) {
DisableCodeLogging(native_module);
}
if (native_module->HasDebugInfo()) {
native_module->GetDebugInfo()->RemoveIsolate(isolate);
}
}
if (current_gc_info_) {
if (RemoveIsolateFromCurrentGC(isolate)) PotentiallyFinishCurrentGC();
}
for (auto& [script_id, code_to_log] : isolate_info->code_to_log) {
if (code_to_log.native_module == nullptr) {
DCHECK_EQ(script_id, -1);
} else {
native_modules_with_code_to_log.insert(code_to_log.native_module);
}
for (WasmCode* code : code_to_log.code) {
WasmCodeRefScope::AddRef(code);
code->DecRefOnLiveCode();
}
}
isolate_info->code_to_log.clear();
isolates_.erase(isolates_it);
}
void WasmEngine::LogCode(base::Vector<WasmCode*> code_vec) {
if (code_vec.empty()) return;
NativeModule* native_module = code_vec[0]->native_module();
if (!native_module->log_code()) return;
using TaskToSchedule =
std::pair<std::shared_ptr<v8::TaskRunner>, std::unique_ptr<LogCodesTask>>;
std::vector<TaskToSchedule> to_schedule;
{
base::MutexGuard guard(&mutex_);
DCHECK(native_modules_.contains(native_module));
NativeModuleInfo* native_module_info =
native_modules_.find(native_module)->second.get();
std::shared_ptr<NativeModule> shared_native_module =
native_module_info->weak_ptr.lock();
DCHECK_NOT_NULL(shared_native_module);
for (Isolate* isolate : native_module_info->isolates) {
DCHECK(isolates_.contains(isolate));
IsolateInfo* info = isolates_[isolate].get();
if (info->log_codes == false) continue;
auto script_it = info->scripts.find(native_module);
if (script_it == info->scripts.end()) continue;
if (info->code_to_log.empty()) {
isolate->stack_guard()->RequestLogWasmCode();
to_schedule.emplace_back(info->foreground_task_runner,
std::make_unique<LogCodesTask>(isolate));
}
WeakScriptHandle& weak_script_handle = script_it->second;
auto& log_entry = info->code_to_log[weak_script_handle.script_id()];
if (!log_entry.native_module) {
log_entry.native_module = shared_native_module;
}
if (!log_entry.source_url) {
log_entry.source_url = weak_script_handle.source_url();
}
log_entry.code.insert(log_entry.code.end(), code_vec.begin(),
code_vec.end());
for (WasmCode* code : code_vec) {
DCHECK_EQ(native_module, code->native_module());
code->IncRef();
}
}
}
for (auto& [runner, task] : to_schedule) {
runner->PostTask(std::move(task));
}
}
bool WasmEngine::LogWrapperCode(WasmCode* code) {
DCHECK_NULL(code->native_module());
if (!num_modules_with_code_logging_.load(std::memory_order_relaxed)) {
return false;
}
using TaskToSchedule =
std::pair<std::shared_ptr<v8::TaskRunner>, std::unique_ptr<LogCodesTask>>;
std::vector<TaskToSchedule> to_schedule;
bool did_trigger_code_logging = false;
{
base::MutexGuard guard(&mutex_);
for (const auto& entry : isolates_) {
Isolate* isolate = entry.first;
IsolateInfo* info = entry.second.get();
if (info->log_codes == false) continue;
did_trigger_code_logging = true;
if (info->code_to_log.empty()) {
isolate->stack_guard()->RequestLogWasmCode();
to_schedule.emplace_back(info->foreground_task_runner,
std::make_unique<LogCodesTask>(isolate));
}
constexpr int kNoScriptId = -1;
auto& log_entry = info->code_to_log[kNoScriptId];
log_entry.code.push_back(code);
code->IncRef();
}
DCHECK_EQ(did_trigger_code_logging, num_modules_with_code_logging_.load(
std::memory_order_relaxed) > 0);
}
for (auto& [runner, task] : to_schedule) {
runner->PostTask(std::move(task));
}
return did_trigger_code_logging;
}
void WasmEngine::EnableCodeLogging(Isolate* isolate) {
base::MutexGuard guard(&mutex_);
auto it = isolates_.find(isolate);
DCHECK_NE(isolates_.end(), it);
IsolateInfo* info = it->second.get();
if (info->log_codes) return;
info->log_codes = true;
for (NativeModule* native_module : info->native_modules) {
if (!native_module->log_code()) EnableCodeLogging(native_module);
}
}
void WasmEngine::EnableCodeLogging(NativeModule* native_module) {
mutex_.AssertHeld();
DCHECK(!native_module->log_code());
native_module->EnableCodeLogging();
num_modules_with_code_logging_.fetch_add(1, std::memory_order_relaxed);
DCHECK_EQ(
num_modules_with_code_logging_.load(std::memory_order_relaxed),
std::count_if(
native_modules_.begin(), native_modules_.end(),
[](std::pair<NativeModule* const, std::unique_ptr<NativeModuleInfo>>&
pair) { return pair.first->log_code(); }));
}
void WasmEngine::DisableCodeLogging(NativeModule* native_module) {
mutex_.AssertHeld();
DCHECK(native_module->log_code());
native_module->DisableCodeLogging();
num_modules_with_code_logging_.fetch_sub(1, std::memory_order_relaxed);
DCHECK_EQ(
num_modules_with_code_logging_.load(std::memory_order_relaxed),
std::count_if(
native_modules_.begin(), native_modules_.end(),
[](std::pair<NativeModule* const, std::unique_ptr<NativeModuleInfo>>&
pair) { return pair.first->log_code(); }));
}
void WasmEngine::LogOutstandingCodesForIsolate(Isolate* isolate) {
std::unordered_map<int, IsolateInfo::CodeToLogPerScript> code_to_log_map;
{
base::MutexGuard guard(&mutex_);
DCHECK(isolates_.contains(isolate));
code_to_log_map.swap(isolates_[isolate]->code_to_log);
}
bool should_log = WasmCode::ShouldBeLogged(isolate);
TRACE_EVENT0("v8.wasm", "wasm.LogCode");
for (auto& [script_id, code_to_log] : code_to_log_map) {
if (should_log) {
for (WasmCode* code : code_to_log.code) {
const char* source_url = code_to_log.source_url.get();
if (!source_url) source_url = "";
code->LogCode(isolate, source_url, script_id);
}
}
WasmCode::DecrementRefCount(base::VectorOf(code_to_log.code));
}
}
std::shared_ptr<NativeModule> WasmEngine::NewNativeModule(
Isolate* isolate, WasmEnabledFeatures enabled_features,
WasmDetectedFeatures detected_features, CompileTimeImports compile_imports,
std::shared_ptr<const WasmModule> module, size_t code_size_estimate) {
std::shared_ptr<NativeModule> native_module = NewUnownedNativeModule(
enabled_features, detected_features, compile_imports, std::move(module),
code_size_estimate);
UseNativeModuleInIsolate(native_module.get(), isolate);
return native_module;
}
std::shared_ptr<NativeModule> WasmEngine::NewUnownedNativeModule(
WasmEnabledFeatures enabled_features,
WasmDetectedFeatures detected_features, CompileTimeImports compile_imports,
std::shared_ptr<const WasmModule> module, size_t code_size_estimate) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.wasm.detailed"),
"wasm.NewNativeModule");
std::shared_ptr<NativeModule> native_module =
GetWasmCodeManager()->NewNativeModule(
enabled_features, detected_features, std::move(compile_imports),
code_size_estimate, std::move(module));
base::MutexGuard lock(&mutex_);
if (V8_UNLIKELY(v8_flags.experimental_wasm_pgo_to_file)) {
if (!native_modules_kept_alive_for_pgo) {
native_modules_kept_alive_for_pgo =
new std::vector<std::shared_ptr<NativeModule>>;
}
native_modules_kept_alive_for_pgo->emplace_back(native_module);
}
DCHECK(!native_modules_.contains(native_module.get()));
native_modules_.insert(std::make_pair(
native_module.get(), std::make_unique<NativeModuleInfo>(native_module)));
return native_module;
}
void WasmEngine::UseNativeModuleInIsolate(NativeModule* native_module,
Isolate* isolate) {
#ifdef V8_ENABLE_WASM_GDB_REMOTE_DEBUGGING
if (v8_flags.wasm_gdb_remote && !gdb_server_) {
gdb_server_ = gdb_server::GdbServer::Create();
gdb_server_->AddIsolate(isolate);
}
#endif
bool remove_all_code = false;
{
base::MutexGuard guard(&mutex_);
NativeModuleInfo* native_module_info = native_modules_[native_module].get();
IsolateInfo* isolate_info = isolates_[isolate].get();
DCHECK_NOT_NULL(native_module_info);
DCHECK_NOT_NULL(isolate_info);
bool is_first_use_in_isolate =
native_module_info->isolates.insert(isolate).second;
DCHECK_EQ(is_first_use_in_isolate,
!isolate_info->native_modules.contains(native_module));
if (is_first_use_in_isolate) {
isolate_info->native_modules.insert(native_module);
if (isolate_info->keep_in_debug_state &&
!native_module->IsInDebugState()) {
remove_all_code = true;
native_module->SetDebugState(kDebugging);
}
if (isolate_info->log_codes && !native_module->log_code()) {
EnableCodeLogging(native_module);
}
isolate->counters()->wasm_modules_per_isolate()->AddSample(
static_cast<int>(isolate_info->native_modules.size()));
if (native_module_info->isolates.size() == 1) {
isolate->counters()->wasm_modules_per_engine()->AddSample(
static_cast<int>(native_modules_.size()));
}
}
}
if (remove_all_code) {
WasmCodeRefScope ref_scope;
native_module->RemoveCompiledCode(
NativeModule::RemoveFilter::kRemoveNonDebugCode);
}
}
std::shared_ptr<NativeModule> WasmEngine::MaybeGetNativeModule(
ModuleOrigin origin, base::Vector<const uint8_t> wire_bytes,
const CompileTimeImports& compile_imports) {
TRACE_EVENT1("v8.wasm", "wasm.GetNativeModuleFromCache", "wire_bytes",
wire_bytes.size());
std::shared_ptr<NativeModule> native_module =
native_module_cache_.MaybeGetNativeModule(origin, wire_bytes,
compile_imports);
if (native_module) {
TRACE_EVENT0("v8.wasm", "CacheHit");
}
return native_module;
}
std::shared_ptr<NativeModule> WasmEngine::UpdateNativeModuleCache(
bool has_error, std::shared_ptr<NativeModule> native_module,
Isolate* isolate) {
void* prev = native_module.get();
native_module =
native_module_cache_.Update(std::move(native_module), has_error);
if (prev != native_module.get()) {
UseNativeModuleInIsolate(native_module.get(), isolate);
}
return native_module;
}
bool WasmEngine::GetStreamingCompilationOwnership(
size_t prefix_hash, const CompileTimeImports& compile_imports) {
TRACE_EVENT0("v8.wasm", "wasm.GetStreamingCompilationOwnership");
if (native_module_cache_.GetStreamingCompilationOwnership(prefix_hash,
compile_imports)) {
return true;
}
TRACE_EVENT0("v8.wasm", "CacheHit");
return false;
}
void WasmEngine::StreamingCompilationFailed(
size_t prefix_hash, const CompileTimeImports& compile_imports) {
native_module_cache_.StreamingCompilationFailed(prefix_hash, compile_imports);
}
void WasmEngine::FreeNativeModule(NativeModule* native_module) {
base::MutexGuard guard(&mutex_);
auto module = native_modules_.find(native_module);
DCHECK_NE(native_modules_.end(), module);
auto part_of_native_module = [native_module](WasmCode* code) {
return code->native_module() == native_module;
};
if (Isolate* isolate = Isolate::TryGetCurrent()) {
CHECK_EQ(isolate->thread_id(), ThreadId::Current());
native_module->counter_updates()->Publish(isolate);
}
for (Isolate* isolate : module->second->isolates) {
DCHECK(isolates_.contains(isolate));
IsolateInfo* info = isolates_[isolate].get();
DCHECK(info->native_modules.contains(native_module));
info->native_modules.erase(native_module);
info->scripts.erase(native_module);
GetWasmCodeManager()->FlushCodeLookupCache(isolate);
#ifdef DEBUG
for (auto& log_entry : info->code_to_log) {
for (WasmCode* code : log_entry.second.code) {
DCHECK_NE(native_module, code->native_module());
}
}
#endif
}
if (current_gc_info_) {
for (auto it = current_gc_info_->dead_code.begin(),
end = current_gc_info_->dead_code.end();
it != end;) {
if ((*it)->native_module() == native_module) {
it = current_gc_info_->dead_code.erase(it);
} else {
++it;
}
}
TRACE_CODE_GC("Native module %p died, reducing dead code objects to %zu.\n",
native_module, current_gc_info_->dead_code.size());
}
std::erase_if(potentially_dead_code_, part_of_native_module);
if (native_module->log_code()) DisableCodeLogging(native_module);
native_module_cache_.Erase(native_module);
native_modules_.erase(module);
}
void WasmEngine::ReportLiveCodeForGC(Isolate* isolate,
std::unordered_set<WasmCode*>& live_code) {
TRACE_EVENT0("v8.wasm", "wasm.ReportLiveCodeForGC");
TRACE_CODE_GC("Isolate %d reporting %zu live code objects.\n", isolate->id(),
live_code.size());
base::MutexGuard guard(&mutex_);
if (current_gc_info_ == nullptr) return;
if (!RemoveIsolateFromCurrentGC(isolate)) return;
isolate->counters()->wasm_module_num_triggered_code_gcs()->AddSample(
current_gc_info_->gc_sequence_index);
for (WasmCode* code : live_code) current_gc_info_->dead_code.erase(code);
PotentiallyFinishCurrentGC();
}
namespace {
void ReportLiveCodeFromFrameForGC(
Isolate* isolate, StackFrame* frame,
std::unordered_set<wasm::WasmCode*>& live_wasm_code) {
if (frame->type() == StackFrame::WASM) {
WasmFrame* wasm_frame = WasmFrame::cast(frame);
WasmCode* code = wasm_frame->wasm_code();
live_wasm_code.insert(code);
#if V8_TARGET_ARCH_X64
if (code->is_inspectable()) {
Address osr_target =
base::Memory<Address>(wasm_frame->fp() - kOSRTargetOffset);
if (osr_target) {
WasmCode* osr_code =
GetWasmCodeManager()->LookupCode(isolate, osr_target);
DCHECK_NOT_NULL(osr_code);
live_wasm_code.insert(osr_code);
}
}
#endif
} else if (frame->type() == StackFrame::WASM_TO_JS) {
live_wasm_code.insert(static_cast<WasmToJsFrame*>(frame)->wasm_code());
}
}
}
void WasmEngine::ReportLiveCodeFromStackForGC(Isolate* isolate) {
wasm::WasmCodeRefScope code_ref_scope;
std::unordered_set<wasm::WasmCode*> live_wasm_code;
for (const std::unique_ptr<StackMemory>& stack : isolate->wasm_stacks()) {
if (stack->IsActive()) {
continue;
}
for (StackFrameIterator it(isolate, stack.get()); !it.done();
it.Advance()) {
StackFrame* const frame = it.frame();
ReportLiveCodeFromFrameForGC(isolate, frame, live_wasm_code);
}
}
for (StackFrameIterator it(isolate, isolate->thread_local_top(),
StackFrameIterator::FirstStackOnly{});
!it.done(); it.Advance()) {
StackFrame* const frame = it.frame();
ReportLiveCodeFromFrameForGC(isolate, frame, live_wasm_code);
}
CheckNoArchivedThreads(isolate);
GetWasmCodeManager()->FlushCodeLookupCache(isolate);
ReportLiveCodeForGC(isolate, live_wasm_code);
}
void WasmEngine::AddPotentiallyDeadCode(WasmCode* code) {
base::MutexGuard guard(&mutex_);
DCHECK(code->is_dying());
auto added = potentially_dead_code_.insert(code);
DCHECK(added.second);
USE(added);
new_potentially_dead_code_size_ += code->instructions().size();
if (v8_flags.wasm_code_gc) {
size_t dead_code_limit =
v8_flags.stress_wasm_code_gc
? 0
: 64 * KB + GetWasmCodeManager()->committed_code_space() / 10;
if (new_potentially_dead_code_size_ > dead_code_limit) {
TriggerCodeGC_Locked(dead_code_limit);
}
}
}
void WasmEngine::TriggerCodeGC_Locked(size_t dead_code_limit) {
bool inc_gc_count =
num_code_gcs_triggered_ < std::numeric_limits<int8_t>::max();
if (current_gc_info_ == nullptr) {
if (inc_gc_count) ++num_code_gcs_triggered_;
TRACE_CODE_GC(
"Triggering GC (potentially dead: %zu bytes; limit: %zu bytes).\n",
new_potentially_dead_code_size_, dead_code_limit);
TriggerGC(num_code_gcs_triggered_);
} else if (current_gc_info_->next_gc_sequence_index == 0) {
if (inc_gc_count) ++num_code_gcs_triggered_;
TRACE_CODE_GC(
"Scheduling another GC after the current one (potentially dead: "
"%zu bytes; limit: %zu bytes).\n",
new_potentially_dead_code_size_, dead_code_limit);
current_gc_info_->next_gc_sequence_index = num_code_gcs_triggered_;
DCHECK_NE(0, current_gc_info_->next_gc_sequence_index);
}
}
void WasmEngine::TriggerCodeGCForTesting() {
if (!v8_flags.wasm_code_gc) return;
base::MutexGuard guard(&mutex_);
TRACE_CODE_GC("Wasm Code GC explicitly requested for testing:\n");
if (new_potentially_dead_code_size_ == 0) {
DCHECK(potentially_dead_code_.empty());
TRACE_CODE_GC("But there is nothing to do.\n");
return;
}
TriggerCodeGC_Locked(0);
}
void WasmEngine::FreeDeadCode(const DeadCodeMap& dead_code,
std::vector<WasmCode*>& dead_wrappers) {
base::MutexGuard guard(&mutex_);
FreeDeadCodeLocked(dead_code, dead_wrappers);
}
void WasmEngine::FreeDeadCodeLocked(const DeadCodeMap& dead_code,
std::vector<WasmCode*>& dead_wrappers) {
TRACE_EVENT0("v8.wasm", "wasm.FreeDeadCode");
mutex_.AssertHeld();
for (auto& dead_code_entry : dead_code) {
NativeModule* native_module = dead_code_entry.first;
const std::vector<WasmCode*>& code_vec = dead_code_entry.second;
TRACE_CODE_GC("Freeing %zu code object%s of module %p.\n", code_vec.size(),
code_vec.size() == 1 ? "" : "s", native_module);
native_module->FreeCode(base::VectorOf(code_vec));
}
if (dead_wrappers.size()) {
TRACE_CODE_GC("Freeing %zu wrapper%s.\n", dead_wrappers.size(),
dead_wrappers.size() == 1 ? "" : "s");
GetWasmImportWrapperCache()->Free(dead_wrappers);
}
}
DirectHandle<Script> WasmEngine::GetOrCreateScript(
Isolate* isolate, const std::shared_ptr<NativeModule>& native_module,
base::Vector<const char> source_url) {
{
base::MutexGuard guard(&mutex_);
DCHECK(isolates_.contains(isolate));
auto& scripts = isolates_[isolate]->scripts;
auto it = scripts.find(native_module.get());
if (it != scripts.end()) {
DirectHandle<Script> weak_global_handle = it->second.handle();
if (weak_global_handle.is_null()) {
scripts.erase(it);
} else {
return DirectHandle<Script>::New(*weak_global_handle, isolate);
}
}
}
auto script = CreateWasmScript(isolate, native_module, source_url);
{
base::MutexGuard guard(&mutex_);
DCHECK(isolates_.contains(isolate));
auto& scripts = isolates_[isolate]->scripts;
DCHECK(!scripts.contains(native_module.get()));
scripts.emplace(native_module.get(), WeakScriptHandle(script, isolate));
return script;
}
}
std::shared_ptr<OperationsBarrier>
WasmEngine::GetBarrierForBackgroundCompile() {
return operations_barrier_;
}
void WasmEngine::TriggerGC(int8_t gc_sequence_index) {
mutex_.AssertHeld();
DCHECK_NULL(current_gc_info_);
DCHECK(v8_flags.wasm_code_gc);
new_potentially_dead_code_size_ = 0;
current_gc_info_.reset(new CurrentGCInfo(gc_sequence_index));
for (WasmCode* code : potentially_dead_code_) {
current_gc_info_->dead_code.insert(code);
}
for (const auto& entry : isolates_) {
Isolate* isolate = entry.first;
auto& gc_task = current_gc_info_->outstanding_isolates[isolate];
if (!gc_task) {
auto new_task = std::make_unique<WasmGCForegroundTask>(isolate);
gc_task = new_task.get();
DCHECK(isolates_.contains(isolate));
isolates_[isolate]->foreground_task_runner->PostTask(std::move(new_task));
}
isolate->stack_guard()->RequestWasmCodeGC();
}
TRACE_CODE_GC(
"Starting GC (nr %d). Number of potentially dead code objects: %zu\n",
current_gc_info_->gc_sequence_index, current_gc_info_->dead_code.size());
PotentiallyFinishCurrentGC();
DCHECK(current_gc_info_ == nullptr ||
!current_gc_info_->outstanding_isolates.empty());
}
bool WasmEngine::RemoveIsolateFromCurrentGC(Isolate* isolate) {
mutex_.AssertHeld();
DCHECK_NOT_NULL(current_gc_info_);
return current_gc_info_->outstanding_isolates.erase(isolate) != 0;
}
void WasmEngine::PotentiallyFinishCurrentGC() {
mutex_.AssertHeld();
TRACE_CODE_GC(
"Remaining dead code objects: %zu; outstanding isolates: %zu.\n",
current_gc_info_->dead_code.size(),
current_gc_info_->outstanding_isolates.size());
if (!current_gc_info_->outstanding_isolates.empty()) return;
size_t num_freed = 0;
DeadCodeMap dead_code;
std::vector<WasmCode*> dead_wrappers;
for (WasmCode* code : current_gc_info_->dead_code) {
DCHECK(potentially_dead_code_.contains(code));
DCHECK(code->is_dying());
potentially_dead_code_.erase(code);
if (code->DecRefOnDeadCode()) {
NativeModule* native_module = code->native_module();
if (native_module) {
dead_code[native_module].push_back(code);
} else {
dead_wrappers.push_back(code);
}
++num_freed;
}
}
FreeDeadCodeLocked(dead_code, dead_wrappers);
TRACE_CODE_GC("Found %zu dead code objects, freed %zu.\n",
current_gc_info_->dead_code.size(), num_freed);
USE(num_freed);
int8_t next_gc_sequence_index = current_gc_info_->next_gc_sequence_index;
current_gc_info_.reset();
if (next_gc_sequence_index != 0) TriggerGC(next_gc_sequence_index);
}
void WasmEngine::DecodeAllNameSections(CanonicalTypeNamesProvider* target) {
base::MutexGuard lock(&mutex_);
for (const auto& [native_module, native_module_info] : native_modules_) {
target->DecodeNames(native_module);
}
}
size_t WasmEngine::EstimateCurrentMemoryConsumption() const {
#ifdef V8_ENABLE_TURBOFAN
UPDATE_WHEN_CLASS_CHANGES(WasmEngine, 8392);
#else
UPDATE_WHEN_CLASS_CHANGES(WasmEngine, 8368);
#endif
UPDATE_WHEN_CLASS_CHANGES(IsolateInfo, 168);
UPDATE_WHEN_CLASS_CHANGES(NativeModuleInfo, 56);
UPDATE_WHEN_CLASS_CHANGES(CurrentGCInfo, 96);
size_t result = sizeof(WasmEngine);
result += GetCanonicalTypeNamesProvider()->EstimateCurrentMemoryConsumption();
result += type_canonicalizer_.EstimateCurrentMemoryConsumption();
{
base::MutexGuard lock(&mutex_);
result += ContentSize(async_compile_jobs_);
result += async_compile_jobs_.size() * sizeof(AsyncCompileJob);
result += ContentSize(potentially_dead_code_);
result += ContentSize(isolates_);
result += isolates_.size() * sizeof(IsolateInfo);
for (const auto& [isolate, isolate_info] : isolates_) {
result += ContentSize(isolate_info->native_modules);
result += ContentSize(isolate_info->scripts);
result += ContentSize(isolate_info->code_to_log);
}
result += ContentSize(native_modules_);
result += native_modules_.size() * sizeof(NativeModuleInfo);
for (const auto& [native_module, native_module_info] : native_modules_) {
result += native_module->EstimateCurrentMemoryConsumption();
result += ContentSize(native_module_info->isolates);
}
if (current_gc_info_) {
result += sizeof(CurrentGCInfo);
result += ContentSize(current_gc_info_->outstanding_isolates);
result += ContentSize(current_gc_info_->dead_code);
}
}
if (v8_flags.trace_wasm_offheap_memory) {
PrintF("WasmEngine: %zu\n", result);
}
return result;
}
void WasmEngine::PrintCurrentMemoryConsumptionEstimate() const {
DCHECK(v8_flags.print_wasm_offheap_memory_size);
PrintF("Off-heap memory size of WasmEngine: %zu\n",
EstimateCurrentMemoryConsumption());
}
int WasmEngine::GetDeoptsExecutedCount() const {
return deopts_executed_.load(std::memory_order::relaxed);
}
int WasmEngine::IncrementDeoptsExecutedCount() {
int previous_value = deopts_executed_.fetch_add(1, std::memory_order_relaxed);
return previous_value + 1;
}
namespace {
struct GlobalWasmState {
WasmCodeManager code_manager;
WasmImportWrapperCache import_wrapper_cache;
WasmEngine engine;
CanonicalTypeNamesProvider type_names_provider;
};
GlobalWasmState* global_wasm_state = nullptr;
}
void WasmEngine::InitializeOncePerProcess() {
DCHECK_NULL(global_wasm_state);
global_wasm_state = new GlobalWasmState();
#ifdef V8_ENABLE_DRUMBRAKE
if (v8_flags.wasm_jitless) {
WasmInterpreter::InitializeOncePerProcess();
}
#endif
GetProcessWideWasmCodePointerTable()->Initialize();
}
void WasmEngine::GlobalTearDown() {
#ifdef V8_ENABLE_DRUMBRAKE
if (v8_flags.wasm_jitless) {
WasmInterpreter::GlobalTearDown();
}
#endif
delete global_wasm_state;
global_wasm_state = nullptr;
GetProcessWideWasmCodePointerTable()->TearDown();
}
WasmEngine* GetWasmEngine() {
DCHECK_NOT_NULL(global_wasm_state);
return &global_wasm_state->engine;
}
WasmCodeManager* GetWasmCodeManager() {
DCHECK_NOT_NULL(global_wasm_state);
return &global_wasm_state->code_manager;
}
WasmImportWrapperCache* GetWasmImportWrapperCache() {
DCHECK_NOT_NULL(global_wasm_state);
return &global_wasm_state->import_wrapper_cache;
}
CanonicalTypeNamesProvider* GetCanonicalTypeNamesProvider() {
DCHECK_NOT_NULL(global_wasm_state);
return &global_wasm_state->type_names_provider;
}
uint32_t max_mem32_pages() {
static_assert(
kV8MaxWasmMemory32Pages * kWasmPageSize <= JSArrayBuffer::kMaxByteLength,
"Wasm memories must not be bigger than JSArrayBuffers");
static_assert(kV8MaxWasmMemory32Pages <= kMaxUInt32);
return std::min(uint32_t{kV8MaxWasmMemory32Pages},
v8_flags.wasm_max_mem_pages.value());
}
uint32_t max_mem64_pages() {
static_assert(
kV8MaxWasmMemory64Pages * kWasmPageSize <= JSArrayBuffer::kMaxByteLength,
"Wasm memories must not be bigger than JSArrayBuffers");
static_assert(kV8MaxWasmMemory64Pages <= kMaxUInt32);
return std::min(uint32_t{kV8MaxWasmMemory64Pages},
v8_flags.wasm_max_mem_pages.value());
}
uint32_t max_table_size() {
return std::min(uint32_t{kV8MaxWasmTableSize},
v8_flags.wasm_max_table_size.value());
}
uint32_t max_table_init_entries() {
return std::min(uint32_t{kV8MaxWasmTableInitEntries},
v8_flags.wasm_max_table_size.value());
}
size_t max_module_size() {
constexpr size_t kMin = 16;
constexpr size_t kMax = kV8MaxWasmModuleSize;
static_assert(kMin <= kV8MaxWasmModuleSize);
return std::clamp(v8_flags.wasm_max_module_size.value(), kMin, kMax);
}
#undef TRACE_CODE_GC
}