#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "third_party/blink/renderer/bindings/core/v8/v8_initializer.h"
#include <algorithm>
#include <limits>
#include <memory>
#include <utility>
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/system/sys_info.h"
#include "build/build_config.h"
#include "components/crash/core/common/crash_key.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "third_party/blink/public/common/switches.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/bindings/core/v8/binding_security.h"
#include "third_party/blink/renderer/bindings/core/v8/capture_source_location.h"
#include "third_party/blink/renderer/bindings/core/v8/isolated_world_csp.h"
#include "third_party/blink/renderer/bindings/core/v8/native_value_traits_impl.h"
#include "third_party/blink/renderer/bindings/core/v8/referrer_script_info.h"
#include "third_party/blink/renderer/bindings/core/v8/rejected_promises.h"
#include "third_party/blink/renderer/bindings/core/v8/sanitize_script_errors.h"
#include "third_party/blink/renderer/bindings/core/v8/script_controller.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/bindings/core/v8/shadow_realm_context.h"
#include "third_party/blink/renderer/bindings/core/v8/use_counter_callback.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_context_snapshot.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_dom_exception.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_embedder_graph_builder.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_error_event.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_gc_controller.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_idle_task_runner.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_metrics.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_throw_dom_exception.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_trusted_script.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_string_trustedscript.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_wasm_response_extensions.h"
#include "third_party/blink/renderer/bindings/core/v8/worker_or_worklet_script_controller.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/dom/events/event_dispatch_forbidden_scope.h"
#include "third_party/blink/renderer/core/events/error_event.h"
#include "third_party/blink/renderer/core/execution_context/agent.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/frame/csp/content_security_policy.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/core/inspector/main_thread_debugger.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/script/modulator.h"
#include "third_party/blink/renderer/core/shadow_realm/shadow_realm_global_scope.h"
#include "third_party/blink/renderer/core/trustedtypes/trusted_types_util.h"
#include "third_party/blink/renderer/core/workers/worker_global_scope.h"
#include "third_party/blink/renderer/core/workers/worklet_global_scope.h"
#include "third_party/blink/renderer/platform/bindings/active_script_wrappable_manager.h"
#include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
#include "third_party/blink/renderer/platform/bindings/source_location.h"
#include "third_party/blink/renderer/platform/bindings/v8_dom_wrapper.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_context_data.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/thread_state.h"
#include "third_party/blink/renderer/platform/heap/thread_state_storage.h"
#include "third_party/blink/renderer/platform/instrumentation/histogram.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/blink/renderer/platform/scheduler/common/features.h"
#include "third_party/blink/renderer/platform/scheduler/public/cooperative_scheduling_manager.h"
#include "third_party/blink/renderer/platform/scheduler/public/main_thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/reporting_disposition.h"
#include "third_party/blink/renderer/platform/wtf/sanitizers.h"
#include "third_party/blink/renderer/platform/wtf/stack_util.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "tools/v8_context_snapshot/buildflags.h"
#include "v8/include/v8-profiler.h"
#include "v8/include/v8.h"
#if defined(V8_USE_EXTERNAL_STARTUP_DATA)
#include "gin/public/v8_snapshot_file_type.h"
#endif
namespace blink {
#if BUILDFLAG(IS_WIN)
bool FilterETWSessionByURLCallback(v8::Local<v8::Context> context,
const std::string& json_payload);
#endif
namespace {
String ExtractMessageForConsole(v8::Isolate* isolate,
v8::Local<v8::Value> data) { … }
mojom::ConsoleMessageLevel MessageLevelFromNonFatalErrorLevel(int error_level) { … }
const size_t kWasmWireBytesLimit = …;
}
void V8Initializer::MessageHandlerInMainThread(v8::Local<v8::Message> message,
v8::Local<v8::Value> data) { … }
void V8Initializer::MessageHandlerInWorker(v8::Local<v8::Message> message,
v8::Local<v8::Value> data) { … }
static void PromiseRejectHandler(v8::PromiseRejectMessage data,
RejectedPromises& rejected_promises,
ScriptState* script_state) { … }
void V8Initializer::PromiseRejectHandlerInMainThread(
v8::PromiseRejectMessage data) { … }
static void PromiseRejectHandlerInWorker(v8::PromiseRejectMessage data) { … }
void V8Initializer::FailedAccessCheckCallbackInMainThread(
v8::Local<v8::Object> holder,
v8::AccessType type,
v8::Local<v8::Value> data) { … }
static bool ContentSecurityPolicyCodeGenerationCheck(
v8::Local<v8::Context> context,
v8::Local<v8::String> source) { … }
static std::pair<bool, v8::MaybeLocal<v8::String>>
TrustedTypesCodeGenerationCheck(v8::Local<v8::Context> context,
v8::Local<v8::Value> source,
bool is_code_like) { … }
v8::ModifyCodeGenerationFromStringsResult
V8Initializer::CodeGenerationCheckCallbackInMainThread(
v8::Local<v8::Context> context,
v8::Local<v8::Value> source,
bool is_code_like) { … }
bool V8Initializer::WasmCodeGenerationCheckCallbackInMainThread(
v8::Local<v8::Context> context,
v8::Local<v8::String> source) { … }
void V8Initializer::WasmAsyncResolvePromiseCallback(
v8::Isolate* isolate,
v8::Local<v8::Context> context,
v8::Local<v8::Promise::Resolver> resolver,
v8::Local<v8::Value> compilation_result,
v8::WasmAsyncSuccess success) { … }
namespace {
bool SharedArrayBufferConstructorEnabledCallback(
v8::Local<v8::Context> context) { … }
v8::Local<v8::Value> NewRangeException(v8::Isolate* isolate,
const char* message) { … }
void ThrowRangeException(v8::Isolate* isolate, const char* message) { … }
BASE_FEATURE(…);
bool WasmModuleOverride(const v8::FunctionCallbackInfo<v8::Value>& args) { … }
bool WasmInstanceOverride(const v8::FunctionCallbackInfo<v8::Value>& args) { … }
bool WasmJSStringBuiltinsEnabledCallback(v8::Local<v8::Context> context) { … }
bool WasmJSPromiseIntegrationEnabledCallback(v8::Local<v8::Context> context) { … }
v8::MaybeLocal<v8::Promise> HostImportModuleDynamically(
v8::Local<v8::Context> context,
v8::Local<v8::Data> v8_host_defined_options,
v8::Local<v8::Value> v8_referrer_resource_url,
v8::Local<v8::String> v8_specifier,
v8::Local<v8::FixedArray> v8_import_attributes) { … }
void HostGetImportMetaProperties(v8::Local<v8::Context> context,
v8::Local<v8::Module> module,
v8::Local<v8::Object> meta) { … }
struct PrintV8OOM { … };
std::ostream& operator<<(std::ostream& os, const PrintV8OOM& oom_details) { … }
}
void V8Initializer::InitializeV8Common(v8::Isolate* isolate) { … }
void ReportV8FatalError(const char* location, const char* message) { … }
void ReportV8OOMError(const char* location, const v8::OOMDetails& details) { … }
namespace {
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { … };
V8PerIsolateData::V8ContextSnapshotMode GetV8ContextSnapshotMode() { … }
}
void V8Initializer::InitializeIsolateHolder(
const intptr_t* reference_table,
const std::string js_command_line_flags) { … }
v8::Isolate* V8Initializer::InitializeMainThread() { … }
#if defined(ARCH_CPU_32_BITS) && BUILDFLAG(IS_WIN)
static const int kWorkerMaxStackSize = 492 * 1024;
#else
static const int kWorkerMaxStackSize = …;
#endif
void V8Initializer::InitializeWorker(v8::Isolate* isolate) { … }
}