blob: abe60b83b0f34339c6a1dba71e37d047ccb9ba29 [file]
// Copyright 2016 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/snapshot/code-serializer.h"
#if V8_USE_OPENSSL
// OpenSSL 3.0+ deprecates the low-level SHA256_Init/Update/Final API, which
// remains the standard streaming SHA-256 interface in BoringSSL. Suppress the
// deprecation warning when building against OpenSSL (e.g. in Node.js).
#define OPENSSL_SUPPRESS_DEPRECATED
#include <openssl/sha.h>
#endif
#include <algorithm>
#include <memory>
#include "src/base/container-utils.h"
#include "src/base/logging.h"
#include "src/base/numerics/safe_conversions.h"
#include "src/base/platform/elapsed-timer.h"
#include "src/base/platform/platform.h"
#include "src/baseline/baseline-batch-compiler.h"
#include "src/codegen/background-merge-task.h"
#include "src/common/globals.h"
#include "src/handles/maybe-handles.h"
#include "src/handles/persistent-handles.h"
#include "src/heap/heap-inl.h"
#include "src/heap/parked-scope-inl.h"
#include "src/logging/counters-scopes.h"
#include "src/logging/log.h"
#include "src/logging/runtime-call-stats-scope.h"
#include "src/objects/objects-inl.h"
#include "src/objects/shared-function-info.h"
#include "src/objects/slots.h"
#include "src/objects/string-inl.h"
#include "src/objects/visitors.h"
#include "src/sandbox/check.h"
#include "src/sandbox/testing.h"
#include "src/snapshot/object-deserializer.h"
#include "src/snapshot/snapshot-utils.h"
#include "src/snapshot/snapshot.h"
#include "src/tracing/trace-event.h"
#include "src/utils/memcopy.h"
#include "src/utils/version.h"
namespace v8 {
namespace internal {
AlignedCachedData::AlignedCachedData(const uint8_t* data, int length)
: owns_data_(false), rejected_(false), data_(data), length_(length) {
if (!IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment)) {
uint8_t* copy = NewArray<uint8_t>(length);
DCHECK(IsAligned(reinterpret_cast<intptr_t>(copy), kPointerAlignment));
CopyBytes(copy, data, length);
data_ = copy;
AcquireDataOwnership();
}
}
GuardedCodeCacheForTesting::GuardedCodeCacheForTesting(const uint8_t* data,
int length) {
v8::PageAllocator* page_allocator = GetPlatformPageAllocator();
size_t page_size = page_allocator->AllocatePageSize();
size_t data_size = RoundUp(base::checked_cast<size_t>(length), page_size);
memory_ = VirtualMemory(page_allocator, data_size + page_size, {}, page_size);
CHECK(memory_.IsReserved());
CHECK(memory_.SetPermissions(memory_.address(), data_size,
PageAllocator::kReadWrite));
guard_page_ = memory_.address() + data_size;
// Code cache sizes are pointer-aligned, so AlignedCachedData uses the copy as
// is.
CHECK(IsAligned(length, kPointerAlignment));
uint8_t* copy = reinterpret_cast<uint8_t*>(guard_page_ - length);
std::copy_n(data, length, copy);
data_ = base::VectorOf(copy, length);
#ifdef V8_ENABLE_MEMORY_CORRUPTION_API
SandboxTesting::RegisterSafeMemoryRegion(
guard_page_, page_size, SandboxTesting::kOnlyReadAccessIsSafe);
#endif // V8_ENABLE_MEMORY_CORRUPTION_API
}
GuardedCodeCacheForTesting::~GuardedCodeCacheForTesting() {
#ifdef V8_ENABLE_MEMORY_CORRUPTION_API
SandboxTesting::UnregisterSafeMemoryRegion(guard_page_);
#endif // V8_ENABLE_MEMORY_CORRUPTION_API
}
CodeSerializer::CodeSerializer(Isolate* isolate, SourceHash source_hash)
: Serializer(isolate, Snapshot::kDefaultSerializerFlags),
source_hash_(source_hash),
trusted_serializer_(isolate, this) {}
// static
ScriptCompiler::CachedData* CodeSerializer::Serialize(
Isolate* isolate, Handle<SharedFunctionInfo> info) {
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.SerializeCode");
NestedTimedHistogramScope histogram_timer(
isolate->counters()->compile_serialize());
RCS_SCOPE(isolate, RuntimeCallCounterId::kCompileSerialize);
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileSerialize");
base::ElapsedTimer timer;
if (v8_flags.profile_deserialization) timer.Start();
DirectHandle<Script> script(Cast<Script>(info->script()), isolate);
if (v8_flags.trace_serializer) {
PrintF("[Serializing from");
ShortPrint(script->name());
PrintF("]\n");
}
// Serialize code object.
DirectHandle<String> source(Cast<String>(script->source()), isolate);
DirectHandle<FixedArray> wrapped_arguments;
uint32_t context_extension_count = 0;
if (script->is_wrapped()) {
wrapped_arguments =
DirectHandle<FixedArray>(script->wrapped_arguments(), isolate);
context_extension_count = info->context_extension_count();
}
HandleScope scope(isolate);
CodeSerializer cs(
isolate, SerializedCodeData::SourceHash(source, wrapped_arguments,
script->origin_options(), isolate,
context_extension_count));
DisallowGarbageCollection no_gc;
#ifndef DEBUG
cs.reference_map()->AddAttachedReference(*source);
#endif
AlignedCachedData* cached_data = cs.SerializeSharedFunctionInfo(info);
if (v8_flags.profile_deserialization) {
double ms = timer.Elapsed().InMillisecondsF();
int length = cached_data->length();
PrintF("[Serializing to %d bytes took %0.3f ms]\n", length, ms);
}
ScriptCompiler::CachedData* result =
new ScriptCompiler::CachedData(cached_data->data(), cached_data->length(),
ScriptCompiler::CachedData::BufferOwned);
cached_data->ReleaseDataOwnership();
delete cached_data;
return result;
}
AlignedCachedData* CodeSerializer::SerializeSharedFunctionInfo(
Handle<SharedFunctionInfo> info) {
DisallowGarbageCollection no_gc;
if (!isolate()->requires_bytecode_for_code_coverage()) {
VisitRootPointer(Root::kHandleScope, nullptr,
FullObjectSlot(info.location()));
SerializeDeferredObjects();
trusted_serializer_.SerializeExports();
trusted_serializer_.FinishSection();
FinishUntrustedSection();
}
SerializedCodeData data(Payload(),
trusted_serializer_.ExternalReferencesPayload(),
trusted_serializer_.Payload(), this);
return data.GetScriptData();
}
bool CodeSerializer::SerializeTrustedExternalReference(
ExternalReferenceEncoder::Value reference, ExternalPointerTag tag) {
if (!V8_ENABLE_SANDBOX_BOOL) return false;
// The tag is read from the external pointer table entry that an in-sandbox
// object refers to, so it can be the tag of any entry.
SBXCHECK(base::contains(kSerializableExternalPointerTags, tag));
return Serializer::SerializeTrustedExternalReference(reference, tag);
}
void CodeSerializer::SerializeObjectImpl(Handle<HeapObject> obj,
SlotType slot_type) {
// NOTE(mmarchini): If we try to serialize an InterpreterData our process
// will crash since it stores a code object. Instead, we serialize the
// bytecode array stored within the InterpreterData, which is the important
// information. On deserialization we'll create our code objects again, if
// --interpreted-frames-native-stack is on. See v8:9122 for more context
if (V8_UNLIKELY(isolate()->interpreted_frames_native_stack()) &&
IsInterpreterData(*obj)) {
obj = handle(OutsideSandboxCast<InterpreterData>(obj)->bytecode_array(),
isolate());
}
InstanceType instance_type;
{
DisallowGarbageCollection no_gc;
Tagged<HeapObject> raw = *obj;
if (SerializeTrustedExport(raw)) return;
if (SerializeRoot(raw)) return;
if (SerializeReadOnlyObjectReference(raw, &sink_)) return;
if (SerializeBackReference(raw)) return;
DCHECK(!trusted_serializer_.ReferenceMapContains(obj));
instance_type = raw->map()->instance_type();
CHECK(!InstanceTypeChecker::IsInstructionStream(instance_type));
}
// Past this point we should not see any (context-specific) maps anymore.
CHECK(!InstanceTypeChecker::IsMap(instance_type));
// There should be no references to the global object embedded.
CHECK(!InstanceTypeChecker::IsJSGlobalProxy(instance_type) &&
!InstanceTypeChecker::IsJSGlobalObject(instance_type));
// Embedded FixedArrays that need rehashing must support rehashing.
CHECK_IMPLIES(obj->NeedsRehashing(), obj->CanBeRehashed());
// We expect no instantiated function objects or contexts.
CHECK(!InstanceTypeChecker::IsJSFunction(instance_type) &&
!InstanceTypeChecker::IsContext(instance_type));
SerializeGeneric(obj, slot_type);
}
void CodeSerializer::SerializeDeferredObject(Handle<HeapObject> obj) {
SerializeGeneric(obj, SlotType::kAnySlot);
}
void CodeSerializer::SerializeGeneric(Handle<HeapObject> heap_object,
SlotType slot_type) {
ReadOnlyRoots roots(isolate());
InstanceType instance_type = heap_object->map()->instance_type();
if (InstanceTypeChecker::IsScript(instance_type)) {
DirectHandle<FixedArray> host_options;
{
DisallowGarbageCollection no_gc;
Tagged<Script> script_obj = Cast<Script>(*heap_object);
DCHECK(!script_obj->has_eval_origin());
// We don't want to serialize host options to avoid serializing
// unnecessary object graph.
host_options =
direct_handle(script_obj->host_defined_options(), isolate());
script_obj->set_host_defined_options(roots.empty_fixed_array());
}
{
ObjectSerializer serializer(this, heap_object, &sink_);
serializer.Serialize(slot_type);
}
{
DisallowGarbageCollection no_gc;
Tagged<Script> script_obj = Cast<Script>(*heap_object);
script_obj->set_host_defined_options(*host_options);
}
return;
} else if (InstanceTypeChecker::IsSharedFunctionInfo(instance_type)) {
DirectHandle<DebugInfo> debug_info;
CachedTieringDecision cached_tiering_decision;
bool restore_bytecode = false;
{
DisallowGarbageCollection no_gc;
Tagged<SharedFunctionInfo> sfi = Cast<SharedFunctionInfo>(*heap_object);
DCHECK(!sfi->IsApiFunction());
if (auto maybe_debug_info = sfi->TryGetDebugInfo(isolate())) {
debug_info = direct_handle(maybe_debug_info.value(), isolate());
// Clear debug info.
if (debug_info->HasInstrumentedBytecodeArray()) {
restore_bytecode = true;
sfi->SetActiveBytecodeArray(
debug_info->OriginalBytecodeArray(isolate()), isolate());
}
}
if (v8_flags.profile_guided_optimization) {
cached_tiering_decision = sfi->cached_tiering_decision();
if (cached_tiering_decision > CachedTieringDecision::kEarlySparkplug) {
sfi->set_cached_tiering_decision(
CachedTieringDecision::kEarlySparkplug);
}
}
}
{
ObjectSerializer serializer(this, heap_object, &sink_);
serializer.Serialize(slot_type);
}
DisallowGarbageCollection no_gc;
Tagged<SharedFunctionInfo> sfi = Cast<SharedFunctionInfo>(*heap_object);
if (restore_bytecode) {
sfi->SetActiveBytecodeArray(debug_info->debug_bytecode_array(),
isolate());
}
if (v8_flags.profile_guided_optimization &&
cached_tiering_decision > CachedTieringDecision::kEarlySparkplug) {
sfi->set_cached_tiering_decision(cached_tiering_decision);
}
return;
} else if (InstanceTypeChecker::IsScopeInfo(instance_type)) {
// TODO(ishell): define a dedicated instance type for DependentCode and
// serialize DependentCode objects as an empty_dependent_code instead
// of customizing ScopeInfo serialization.
static_assert(DEPENDENT_CODE_TYPE == WEAK_ARRAY_LIST_TYPE);
Handle<ScopeInfo> scope_info = Cast<ScopeInfo>(heap_object);
DirectHandle<DependentCode> dependent_code;
bool restore_dependent_code = false;
if (scope_info->SloppyEvalCanExtendVars()) {
// If |scope_info| has a dependent code field, serialize it as an empty
// dependent code in order to avoid accidental serialization of optimized
// code.
Tagged<DependentCode> empty_dependent_code =
DependentCode::empty_dependent_code(ReadOnlyRoots(isolate()));
if (scope_info->dependent_code() != empty_dependent_code) {
dependent_code = direct_handle(scope_info->dependent_code(), isolate());
restore_dependent_code = true;
scope_info->set_dependent_code(empty_dependent_code);
}
}
{
ObjectSerializer serializer(this, scope_info, &sink_);
serializer.Serialize(slot_type);
}
if (restore_dependent_code) {
scope_info->set_dependent_code(*dependent_code);
}
return;
}
// Object has not yet been serialized. Serialize it here.
ObjectSerializer serializer(this, heap_object, &sink_);
serializer.Serialize(slot_type);
}
namespace {
// NOTE(mmarchini): when v8_flags.interpreted_frames_native_stack is on, we want
// to create duplicates of InterpreterEntryTrampoline for the deserialized
// functions, otherwise we'll call the builtin IET for those functions (which
// is not what a user of this flag wants).
void CreateInterpreterDataForDeserializedCode(
Isolate* isolate, DirectHandle<SharedFunctionInfo> result_sfi,
bool log_code_creation) {
DCHECK_IMPLIES(log_code_creation, isolate->NeedsSourcePositions());
DirectHandle<Script> script(Cast<Script>(result_sfi->script()), isolate);
if (log_code_creation) Script::InitLineEnds(isolate, script);
Tagged<String> name = ReadOnlyRoots(isolate).empty_string();
if (IsString(script->name())) name = Cast<String>(script->name());
DirectHandle<String> name_handle(name, isolate);
SharedFunctionInfo::ScriptIterator iter(isolate, *script);
for (Tagged<SharedFunctionInfo> shared_info = iter.Next();
!shared_info.is_null(); shared_info = iter.Next()) {
IsCompiledScope is_compiled(shared_info, isolate);
if (!is_compiled.is_compiled()) continue;
DCHECK(shared_info->HasBytecodeArray());
DirectHandle<SharedFunctionInfo> sfi(shared_info, isolate);
DirectHandle<BytecodeArray> bytecode(sfi->GetBytecodeArray(isolate),
isolate);
DirectHandle<Code> code =
Builtins::CreateInterpreterEntryTrampolineForProfiling(isolate);
DirectHandle<InterpreterData> interpreter_data =
isolate->factory()->NewInterpreterData(bytecode, code);
if (sfi->HasBaselineCode()) {
sfi->baseline_code(kAcquireLoad)
->set_bytecode_or_interpreter_data(*interpreter_data);
} else {
sfi->set_interpreter_data(isolate, *interpreter_data);
}
if (!log_code_creation) continue;
DirectHandle<AbstractCode> abstract_code = Cast<AbstractCode>(code);
Script::PositionInfo info;
Script::GetPositionInfo(script, sfi->StartPosition(), &info);
int line_num = info.line_start + 1;
int column_num = info.line_end + 1;
PROFILE(isolate,
CodeCreateEvent(LogEventListener::CodeTag::kFunction, abstract_code,
sfi, name_handle, line_num, column_num));
}
}
class StressOffThreadDeserializeThread final : public ParkingThread {
public:
explicit StressOffThreadDeserializeThread(Isolate* isolate,
AlignedCachedData* cached_data)
: ParkingThread(
base::Thread::Options("StressOffThreadDeserializeThread", 2 * MB)),
isolate_(isolate),
cached_data_(cached_data) {}
void Run() final {
LocalIsolate local_isolate(isolate_, ThreadKind::kBackground);
UnparkedScope unparked_scope(&local_isolate);
LocalHandleScope handle_scope(&local_isolate);
CodeSerializer::StartDeserializeOffThread(&local_isolate, cached_data_,
&off_thread_data_);
}
MaybeDirectHandle<SharedFunctionInfo> Finalize(
Isolate* isolate, DirectHandle<String> source,
const ScriptDetails& script_details) {
return CodeSerializer::FinishOffThreadDeserialize(
isolate, std::move(off_thread_data_), cached_data_, source,
script_details);
}
private:
Isolate* isolate_;
AlignedCachedData* cached_data_;
OffThreadDeserializeData off_thread_data_;
};
void FinalizeDeserialization(Isolate* isolate,
DirectHandle<SharedFunctionInfo> result,
const base::ElapsedTimer& timer,
const ScriptDetails& script_details) {
// Devtools can report time in this function as profiler overhead, since none
// of the following tasks would need to happen normally.
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
"V8.FinalizeDeserialization");
const bool log_code_creation = isolate->IsLoggingCodeCreation();
if (V8_UNLIKELY(isolate->interpreted_frames_native_stack())) {
CreateInterpreterDataForDeserializedCode(isolate, result,
log_code_creation);
}
DirectHandle<Script> script(Cast<Script>(result->script()), isolate);
// Reset the script details, including host-defined options.
{
DisallowGarbageCollection no_gc;
SetScriptFieldsFromDetails(isolate, *script, script_details, &no_gc);
}
bool needs_source_positions = isolate->NeedsSourcePositions();
if (!log_code_creation && !needs_source_positions) return;
if (needs_source_positions) {
Script::InitLineEnds(isolate, script);
}
DirectHandle<String> name(IsString(script->name())
? Cast<String>(script->name())
: ReadOnlyRoots(isolate).empty_string(),
isolate);
if (V8_UNLIKELY(v8_flags.log_function_events)) {
LOG(isolate,
FunctionEvent("deserialize", script->id(),
timer.Elapsed().InMillisecondsF(),
result->StartPosition(), result->EndPosition(), *name));
}
SharedFunctionInfo::ScriptIterator iter(isolate, *script);
for (Tagged<SharedFunctionInfo> info = iter.Next(); !info.is_null();
info = iter.Next()) {
if (!info->is_compiled()) continue;
DirectHandle<SharedFunctionInfo> shared_info(info, isolate);
if (needs_source_positions) {
SharedFunctionInfo::EnsureSourcePositionsAvailable(isolate, shared_info);
}
Script::PositionInfo pos_info;
Script::GetPositionInfo(script, shared_info->StartPosition(), &pos_info);
int line_num = pos_info.line + 1;
int column_num = pos_info.column + 1;
PROFILE(
isolate,
CodeCreateEvent(
shared_info->is_toplevel() ? LogEventListener::CodeTag::kScript
: LogEventListener::CodeTag::kFunction,
direct_handle(shared_info->abstract_code(isolate), isolate),
shared_info, name, line_num, column_num));
}
}
#ifdef V8_ENABLE_SPARKPLUG
void BaselineBatchCompileIfSparkplugCompiled(Isolate* isolate,
Tagged<Script> script) {
// Here is main thread, we trigger early baseline compilation only in
// concurrent sparkplug and baseline batch compilation mode which consumes
// little main thread execution time.
if (v8_flags.concurrent_sparkplug && v8_flags.baseline_batch_compilation) {
SharedFunctionInfo::ScriptIterator iter(isolate, script);
for (Tagged<SharedFunctionInfo> info = iter.Next(); !info.is_null();
info = iter.Next()) {
if (info->cached_tiering_decision() != CachedTieringDecision::kPending &&
CanCompileWithBaseline(isolate, info)) {
isolate->baseline_batch_compiler()->EnqueueSFI(info);
}
}
}
}
#else
void BaselineBatchCompileIfSparkplugCompiled(Isolate*, Tagged<Script>) {}
#endif // V8_ENABLE_SPARKPLUG
const char* ToString(SerializedCodeSanityCheckResult result) {
switch (result) {
case SerializedCodeSanityCheckResult::kSuccess:
return "success";
case SerializedCodeSanityCheckResult::kMagicNumberMismatch:
return "magic number mismatch";
case SerializedCodeSanityCheckResult::kVersionMismatch:
return "version mismatch";
case SerializedCodeSanityCheckResult::kSourceMismatch:
return "source mismatch";
case SerializedCodeSanityCheckResult::kFlagsMismatch:
return "flags mismatch";
case SerializedCodeSanityCheckResult::kChecksumMismatch:
return "checksum mismatch";
case SerializedCodeSanityCheckResult::kInvalidHeader:
return "invalid header";
case SerializedCodeSanityCheckResult::kLengthMismatch:
return "length mismatch";
case SerializedCodeSanityCheckResult::kReadOnlySnapshotChecksumMismatch:
return "read-only snapshot checksum mismatch";
}
UNREACHABLE();
}
} // namespace
MaybeDirectHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
Isolate* isolate, AlignedCachedData* cached_data,
DirectHandle<String> source, const ScriptDetails& script_details,
MaybeDirectHandle<Script> maybe_cached_script) {
if (v8_flags.stress_background_compile) {
StressOffThreadDeserializeThread thread(isolate, cached_data);
CHECK(thread.Start());
thread.ParkedJoin(isolate->main_thread_local_isolate());
return thread.Finalize(isolate, source, script_details);
// TODO(leszeks): Compare off-thread deserialized data to on-thread.
}
base::ElapsedTimer timer;
if (v8_flags.profile_deserialization || v8_flags.log_function_events) {
timer.Start();
}
HandleScope scope(isolate);
DirectHandle<FixedArray> wrapped_arguments;
if (!script_details.wrapped_arguments.is_null()) {
wrapped_arguments = script_details.wrapped_arguments.ToHandleChecked();
}
SerializedCodeSanityCheckResult sanity_check_result =
SerializedCodeSanityCheckResult::kSuccess;
const SerializedCodeData scd = SerializedCodeData::FromCachedData(
isolate, cached_data,
SerializedCodeData::SourceHash(source, wrapped_arguments,
script_details.origin_options, isolate,
script_details.context_extension_count),
&sanity_check_result);
if (sanity_check_result != SerializedCodeSanityCheckResult::kSuccess) {
if (v8_flags.profile_deserialization) {
PrintF("[Cached code failed check: %s]\n", ToString(sanity_check_result));
}
DCHECK(cached_data->rejected());
isolate->counters()->code_cache_reject_reason()->AddSample(
static_cast<int>(sanity_check_result));
return MaybeDirectHandle<SharedFunctionInfo>();
}
if (scd.IsEmpty()) return MaybeDirectHandle<SharedFunctionInfo>();
// Deserialize.
MaybeDirectHandle<SharedFunctionInfo> maybe_result =
ObjectDeserializer::DeserializeSharedFunctionInfo(isolate, &scd, source);
DirectHandle<SharedFunctionInfo> result;
if (!maybe_result.ToHandle(&result)) {
// Deserializing may fail if the reservations cannot be fulfilled.
if (v8_flags.profile_deserialization) PrintF("[Deserializing failed]\n");
return MaybeDirectHandle<SharedFunctionInfo>();
}
// Check whether the newly deserialized data should be merged into an
// existing Script from the Isolate compilation cache. If so, perform
// the merge in a single-threaded manner since this deserialization was
// single-threaded.
if (DirectHandle<Script> cached_script;
maybe_cached_script.ToHandle(&cached_script)) {
BackgroundMergeTask merge;
merge.SetUpOnMainThread(isolate, cached_script);
CHECK(merge.HasPendingBackgroundWork());
DirectHandle<Script> new_script(Cast<Script>(result->script()), isolate);
merge.BeginMergeInBackground(isolate->AsLocalIsolate(), new_script);
CHECK(merge.HasPendingForegroundWork());
result = merge.CompleteMergeInForeground(isolate, new_script);
}
Tagged<Script> script = Cast<Script>(result->script());
script->set_deserialized(true);
BaselineBatchCompileIfSparkplugCompiled(isolate, script);
if (v8_flags.profile_deserialization) {
double ms = timer.Elapsed().InMillisecondsF();
int length = cached_data->length();
PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms);
}
FinalizeDeserialization(isolate, result, timer, script_details);
return scope.CloseAndEscape(result);
}
DirectHandle<Script> OffThreadDeserializeData::GetOnlyScript(LocalHeap* heap) {
std::unique_ptr<PersistentHandles> previous_persistent_handles =
heap->DetachPersistentHandles();
heap->AttachPersistentHandles(std::move(persistent_handles));
DCHECK_EQ(scripts.size(), 1);
// Make a non-persistent handle to return.
DirectHandle<Script> script = direct_handle(*scripts[0], heap);
DCHECK_EQ(*script, maybe_result.ToHandleChecked()->script());
persistent_handles = heap->DetachPersistentHandles();
if (previous_persistent_handles) {
heap->AttachPersistentHandles(std::move(previous_persistent_handles));
}
return script;
}
void CodeSerializer::StartDeserializeOffThread(LocalIsolate* local_isolate,
AlignedCachedData* cached_data,
OffThreadDeserializeData* data) {
DCHECK(!local_isolate->heap()->HasPersistentHandles());
const SerializedCodeData scd =
SerializedCodeData::FromCachedDataWithoutSource(
local_isolate, cached_data, &data->sanity_check_result);
if (data->sanity_check_result != SerializedCodeSanityCheckResult::kSuccess) {
// Exit early but don't report yet, we'll re-check this when finishing on
// the main thread
DCHECK(cached_data->rejected());
return;
}
if (scd.IsEmpty()) return;
MaybeDirectHandle<SharedFunctionInfo> local_maybe_result =
OffThreadObjectDeserializer::DeserializeSharedFunctionInfo(
local_isolate, &scd, &data->scripts);
data->maybe_result =
local_isolate->heap()->NewPersistentMaybeHandle(local_maybe_result);
data->persistent_handles = local_isolate->heap()->DetachPersistentHandles();
}
bool CodeSerializer::NotifySourceTextAvailable(
Isolate* isolate, OffThreadDeserializeData* data,
const AlignedCachedData* cached_data, DirectHandle<String> source,
const ScriptDetails& script_details) {
HandleScope scope(isolate);
// Only check the source hash against the immutable cached_data header and
// write the main-thread-only data->source_sanity_check_result field here. Do
// not read/write data->sanity_check_result or mutate cached_data->Reject(),
// as StartDeserializeOffThread may still be running concurrently on a
// background thread. Full rejection and histogram reporting happen in
// FinishOffThreadDeserialize.
SerializedCodeData scd(cached_data);
SerializedCodeSanityCheckResult sanity_check_result =
scd.SanityCheckJustSource(isolate, source, script_details);
data->source_sanity_check_result = sanity_check_result;
return sanity_check_result == SerializedCodeSanityCheckResult::kSuccess;
}
MaybeDirectHandle<SharedFunctionInfo>
CodeSerializer::FinishOffThreadDeserialize(
Isolate* isolate, OffThreadDeserializeData&& data,
AlignedCachedData* cached_data, DirectHandle<String> source,
const ScriptDetails& script_details,
BackgroundMergeTask* background_merge_task) {
base::ElapsedTimer timer;
if (v8_flags.profile_deserialization || v8_flags.log_function_events) {
timer.Start();
}
HandleScope scope(isolate);
// Do a source sanity check now that we have the source. It's important for
// FromPartiallySanityCheckedCachedData call that the sanity_check_result
// holds the result of the off-thread sanity check.
SerializedCodeSanityCheckResult sanity_check_result =
data.sanity_check_result;
const SerializedCodeData scd =
SerializedCodeData::FromPartiallySanityCheckedCachedData(
isolate, cached_data, data, source, script_details,
&sanity_check_result);
USE(scd);
if (sanity_check_result != SerializedCodeSanityCheckResult::kSuccess) {
// The only case where the deserialization result could exist despite a
// check failure is on a source mismatch, since we can't test for this
// off-thread.
DCHECK_IMPLIES(!data.maybe_result.is_null(),
sanity_check_result ==
SerializedCodeSanityCheckResult::kSourceMismatch);
// The only kind of sanity check we can't test for off-thread is a source
// mismatch.
DCHECK_IMPLIES(sanity_check_result != data.sanity_check_result,
sanity_check_result ==
SerializedCodeSanityCheckResult::kSourceMismatch);
if (v8_flags.profile_deserialization) {
PrintF("[Cached code failed check: %s]\n", ToString(sanity_check_result));
}
DCHECK(cached_data->rejected());
isolate->counters()->code_cache_reject_reason()->AddSample(
static_cast<int>(sanity_check_result));
return MaybeDirectHandle<SharedFunctionInfo>();
}
Handle<SharedFunctionInfo> result;
if (!data.maybe_result.ToHandle(&result)) {
// Deserializing may fail if the reservations cannot be fulfilled.
if (v8_flags.profile_deserialization) {
PrintF("[Off-thread deserializing failed]\n");
}
return MaybeDirectHandle<SharedFunctionInfo>();
}
// Change the result persistent handle into a regular handle.
DCHECK(data.persistent_handles->Contains(result.location()));
result = handle(*result, isolate);
if (background_merge_task &&
background_merge_task->HasPendingForegroundWork()) {
DCHECK_EQ(data.scripts.size(), 1);
DirectHandle<Script> new_script = data.scripts[0];
result =
background_merge_task->CompleteMergeInForeground(isolate, new_script);
DCHECK(Object::StrictEquals(Cast<Script>(result->script())->source(),
*source));
DCHECK(isolate->factory()->script_list()->Contains(
MakeWeak(result->script())));
} else {
DirectHandle<Script> result_script(Cast<Script>(result->script()), isolate);
// Fix up the source on the script. This should be the only deserialized
// script, and the off-thread deserializer should have set its source to the
// empty string. In debug mode the code cache does contain the original
// source.
DCHECK_EQ(data.scripts.size(), 1);
DCHECK_EQ(*result_script, *data.scripts[0]);
#ifdef DEBUG
if (!Cast<String>(result_script->source())->Equals(*source)) {
isolate->PushStackTraceAndDie(
"deserialized script source mismatch",
reinterpret_cast<void*>(result_script->source().ptr()),
reinterpret_cast<void*>(source->ptr()));
}
#else
// Check whether the source is reclaimed (replaced by empty string) or
// unreclaimable (already a read-only string).
CHECK(HeapLayout::InReadOnlySpace(result_script->source()));
#endif
Script::SetSource(isolate, result_script, source);
// Fix up the script list to include the newly deserialized script.
Handle<WeakArrayList> list = isolate->factory()->script_list();
for (Handle<Script> script : data.scripts) {
script->set_deserialized(true);
BaselineBatchCompileIfSparkplugCompiled(isolate, *script);
DCHECK(data.persistent_handles->Contains(script.location()));
list = WeakArrayList::AddToEnd(isolate, list,
MaybeObjectDirectHandle::Weak(script));
}
isolate->heap()->SetRootScriptList(*list);
}
if (v8_flags.profile_deserialization) {
double ms = timer.Elapsed().InMillisecondsF();
int length = cached_data->length();
PrintF("[Finishing off-thread deserialize from %d bytes took %0.3f ms]\n",
length, ms);
}
FinalizeDeserialization(isolate, result, timer, script_details);
DCHECK(!background_merge_task ||
!background_merge_task->HasPendingForegroundWork());
return scope.CloseAndEscape(result);
}
SerializedCodeData::SerializedCodeData(
const std::vector<uint8_t>* untrusted_payload,
const std::vector<uint8_t>* external_references_payload,
const std::vector<uint8_t>* trusted_payload, const CodeSerializer* cs) {
DisallowGarbageCollection no_gc;
// Allocate backing store and create result data.
AllocateWithPayload(kHeaderSize, untrusted_payload,
external_references_payload, trusted_payload);
CHECK_LE(size_, kMaxInt);
DCHECK(IsAligned(size_, kPointerAlignment));
DCHECK(IsAligned(GetHeaderValue(kTrustedPayloadLengthOffset),
kPointerAlignment));
// Set header values.
SetHeaderValue(kVersionHashOffset, Version::Hash());
SetHeaderSourceHash(cs->source_hash());
SetHeaderValue(kFlagHashOffset, FlagList::Hash());
SetHeaderValue(kReadOnlySnapshotChecksumOffset,
Snapshot::ExtractReadOnlySnapshotChecksum(
cs->isolate()->snapshot_blob()));
// Zero out any padding in the header.
static_assert(kUnalignedHeaderSize <= kHeaderSize);
std::fill_n(data_ + kUnalignedHeaderSize, kHeaderSize - kUnalignedHeaderSize,
0);
uint32_t checksum =
v8_flags.verify_snapshot_checksum ? Checksum(ChecksummedContent()) : 0;
SetHeaderValue(kChecksumOffset, checksum);
}
SerializedCodeSanityCheckResult SerializedCodeData::SanityCheck(
uint32_t expected_ro_snapshot_checksum,
SourceHash expected_source_hash) const {
SerializedCodeSanityCheckResult result =
SanityCheckWithoutSource(expected_ro_snapshot_checksum);
if (result != SerializedCodeSanityCheckResult::kSuccess) return result;
return SanityCheckJustSource(expected_source_hash);
}
SerializedCodeSanityCheckResult SerializedCodeData::SanityCheckJustSource(
Isolate* isolate, DirectHandle<String> source,
const ScriptDetails& script_details) const {
DirectHandle<FixedArray> wrapped_arguments;
if (!script_details.wrapped_arguments.is_null()) {
wrapped_arguments = script_details.wrapped_arguments.ToHandleChecked();
}
return SanityCheckJustSource(
SourceHash(source, wrapped_arguments, script_details.origin_options,
isolate, script_details.context_extension_count));
}
SerializedCodeSanityCheckResult SerializedCodeData::SanityCheckJustSource(
SourceHash expected_source_hash) const {
if (size_ < kHeaderSize) {
return SerializedCodeSanityCheckResult::kInvalidHeader;
}
SourceHash source_hash = GetHeaderSourceHash();
if (source_hash != expected_source_hash) {
return SerializedCodeSanityCheckResult::kSourceMismatch;
}
return SerializedCodeSanityCheckResult::kSuccess;
}
SerializedCodeSanityCheckResult SerializedCodeData::SanityCheckWithoutSource(
uint32_t expected_ro_snapshot_checksum) const {
if (size_ < kHeaderSize) {
return SerializedCodeSanityCheckResult::kInvalidHeader;
}
uint32_t magic_number = GetMagicNumber();
if (magic_number != kMagicNumber) {
return SerializedCodeSanityCheckResult::kMagicNumberMismatch;
}
uint32_t version_hash = GetHeaderValue(kVersionHashOffset);
if (version_hash != Version::Hash()) {
return SerializedCodeSanityCheckResult::kVersionMismatch;
}
uint32_t flags_hash = GetHeaderValue(kFlagHashOffset);
if (flags_hash != FlagList::Hash()) {
return SerializedCodeSanityCheckResult::kFlagsMismatch;
}
uint32_t ro_snapshot_checksum =
GetHeaderValue(kReadOnlySnapshotChecksumOffset);
if (ro_snapshot_checksum != expected_ro_snapshot_checksum) {
return SerializedCodeSanityCheckResult::kReadOnlySnapshotChecksumMismatch;
}
uint32_t payload_length = GetHeaderValue(kPayloadLengthOffset);
uint32_t max_payload_length = size_ - kHeaderSize;
if (payload_length > max_payload_length) {
return SerializedCodeSanityCheckResult::kLengthMismatch;
}
if (GetHeaderValue(kTrustedPayloadLengthOffset) > payload_length) {
return SerializedCodeSanityCheckResult::kLengthMismatch;
}
if (v8_flags.verify_snapshot_checksum) {
uint32_t checksum = GetHeaderValue(kChecksumOffset);
if (Checksum(ChecksummedContent()) != checksum) {
return SerializedCodeSanityCheckResult::kChecksumMismatch;
}
}
return SerializedCodeSanityCheckResult::kSuccess;
}
void SerializedCodeData::SetHeaderSourceHash(const SourceHash& hash) {
memcpy(data_ + kSourceHashOffset, hash.data(), SourceHash::kSize);
}
SerializedCodeData::SourceHash SerializedCodeData::GetHeaderSourceHash() const {
return SourceHash(data_ + kSourceHashOffset);
}
namespace {
void HashString(SHA256_CTX* ctx, Isolate* isolate,
DirectHandle<String> string) {
string = String::Flatten(isolate, string);
DisallowGarbageCollection no_gc;
SharedStringAccessGuardIfNeeded access_guard(isolate);
String::FlatContent flat = string->GetFlatContent(no_gc, access_guard);
using LengthField = base::BitField<uint32_t, 0, 31>;
static_assert(String::kMaxLength <= LengthField::kMax);
using IsOneByteField = LengthField::Next<bool, 1>;
uint32_t header = LengthField::encode(string->length()) |
IsOneByteField::encode(flat.IsOneByte());
SHA256_Update(ctx, &header, sizeof(header));
if (flat.IsOneByte()) {
base::Vector<const uint8_t> chars = flat.ToOneByteVector();
SHA256_Update(ctx, chars.begin(), chars.length());
} else {
DCHECK(flat.IsTwoByte());
base::Vector<const base::uc16> chars = flat.ToUC16Vector();
SHA256_Update(ctx, chars.begin(), chars.length() * sizeof(base::uc16));
}
}
} // namespace
SerializedCodeData::SourceHash::SourceHash(
DirectHandle<String> source, DirectHandle<FixedArray> wrapped_arguments,
ScriptOriginOptions origin_options, Isolate* isolate,
uint32_t context_extension_count) {
DCHECK_IMPLIES(wrapped_arguments.is_null(), context_extension_count == 0);
if (v8_flags.code_cache_source_hash_sha256) {
SHA256_CTX ctx;
SHA256_Init(&ctx);
// Hash compilation-affecting metadata bits into the SHA-256 context.
// Only IsModule() in ScriptOriginOptions affects how JS source text is
// parsed and compiled into bytecode.
uint8_t flags = 0;
if (!wrapped_arguments.is_null()) flags |= 1;
if (origin_options.IsModule()) flags |= 2;
SHA256_Update(&ctx, &flags, sizeof(flags));
// For CompileFunction, wrapped_arguments holds the parameter names (while
// source only holds the function body) and context_extension_count holds
// the number of outer WITH_SCOPE extensions. Hash the context extension
// count, argument count, and each length-prefixed parameter string so
// parameter or extension count changes invalidate the cache.
if (!wrapped_arguments.is_null()) {
SHA256_Update(&ctx, &context_extension_count,
sizeof(context_extension_count));
uint32_t argc = wrapped_arguments->ulength().value();
SHA256_Update(&ctx, &argc, sizeof(argc));
for (uint32_t i = 0; i < argc; ++i) {
DirectHandle<String> arg(Cast<String>(wrapped_arguments->get(i)),
isolate);
HashString(&ctx, isolate, arg);
}
}
HashString(&ctx, isolate, source);
SHA256_Final(data_.data(), &ctx);
} else {
data_.fill(0);
using LengthField = base::BitField<uint32_t, 0, 29>;
static_assert(String::kMaxLength <= LengthField::kMax,
"String length must fit into a LengthField");
using HasWrappedArgumentsField = LengthField::Next<bool, 1>;
using IsModuleField = HasWrappedArgumentsField::Next<bool, 1>;
uint32_t hash = 0;
hash = LengthField::update(hash, source->length());
hash = HasWrappedArgumentsField::update(hash, !wrapped_arguments.is_null());
hash = IsModuleField::update(hash, origin_options.IsModule());
base::WriteLittleEndianValue<uint32_t>(
reinterpret_cast<Address>(data_.data()), hash);
base::WriteLittleEndianValue<uint32_t>(
reinterpret_cast<Address>(data_.data() + kUInt32Size),
context_extension_count);
}
}
// Return ScriptData object and relinquish ownership over it to the caller.
AlignedCachedData* SerializedCodeData::GetScriptData() {
DCHECK(owns_data_);
AlignedCachedData* result = new AlignedCachedData(data_, size_);
result->AcquireDataOwnership();
owns_data_ = false;
data_ = nullptr;
return result;
}
base::Vector<const uint8_t> SerializedCodeData::UntrustedPayload() const {
uint32_t trusted_length = GetHeaderValue(kTrustedPayloadLengthOffset);
const uint8_t* payload = data_ + kHeaderSize + trusted_length;
DCHECK(IsAligned(reinterpret_cast<intptr_t>(payload), kPointerAlignment));
uint32_t length = GetHeaderValue(kPayloadLengthOffset) - trusted_length;
DCHECK_EQ(data_ + size_, payload + length);
return base::Vector<const uint8_t>(payload, length);
}
base::Vector<const uint8_t> SerializedCodeData::TrustedPayload() const {
const uint8_t* payload = data_ + kHeaderSize;
DCHECK(IsAligned(reinterpret_cast<intptr_t>(payload), kPointerAlignment));
uint32_t length = GetHeaderValue(kTrustedPayloadLengthOffset);
return base::Vector<const uint8_t>(payload, length);
}
// static
base::Vector<const uint8_t> CodeSerializer::UntrustedPayloadForTesting(
base::Vector<const uint8_t> cache) {
return SerializedCodeData(cache.begin(),
base::checked_cast<int>(cache.size()))
.UntrustedPayload();
}
// static
std::vector<uint8_t> CodeSerializer::WithUntrustedPayloadForTesting(
base::Vector<const uint8_t> header_and_trusted_section,
base::Vector<const uint8_t> untrusted) {
std::vector<uint8_t> result(header_and_trusted_section.begin(),
header_and_trusted_section.end());
result.insert(result.end(), untrusted.begin(), untrusted.end());
// Pad like Serializer::Pad: GetUint30 always reads 4 bytes, so a stream
// needs 3 bytes after its last operand even if it is already aligned.
result.insert(result.end(), sizeof(int32_t) - 1, kNop);
while (!IsAligned(result.size(), kPointerAlignment)) result.push_back(kNop);
SerializedCodeData scd(result.data(), base::checked_cast<int>(result.size()));
scd.SetHeaderValue(SerializedCodeData::kPayloadLengthOffset,
base::checked_cast<uint32_t>(
result.size() - SerializedCodeData::kHeaderSize));
return result;
}
SerializedCodeData::SerializedCodeData(const AlignedCachedData* data)
: SerializedData(const_cast<uint8_t*>(data->data()), data->length()) {}
SerializedCodeData SerializedCodeData::FromCachedData(
Isolate* isolate, AlignedCachedData* cached_data,
SourceHash expected_source_hash,
SerializedCodeSanityCheckResult* rejection_result) {
DisallowGarbageCollection no_gc;
SerializedCodeData scd(cached_data);
*rejection_result = scd.SanityCheck(
Snapshot::ExtractReadOnlySnapshotChecksum(isolate->snapshot_blob()),
expected_source_hash);
if (*rejection_result != SerializedCodeSanityCheckResult::kSuccess) {
cached_data->Reject();
return SerializedCodeData(nullptr, 0);
}
return scd;
}
SerializedCodeData SerializedCodeData::FromCachedDataWithoutSource(
LocalIsolate* local_isolate, AlignedCachedData* cached_data,
SerializedCodeSanityCheckResult* rejection_result) {
DisallowGarbageCollection no_gc;
SerializedCodeData scd(cached_data);
*rejection_result =
scd.SanityCheckWithoutSource(Snapshot::ExtractReadOnlySnapshotChecksum(
local_isolate->snapshot_blob()));
if (*rejection_result != SerializedCodeSanityCheckResult::kSuccess) {
cached_data->Reject();
return SerializedCodeData(nullptr, 0);
}
return scd;
}
SerializedCodeData SerializedCodeData::FromPartiallySanityCheckedCachedData(
Isolate* isolate, AlignedCachedData* cached_data,
const OffThreadDeserializeData& data, DirectHandle<String> source,
const ScriptDetails& script_details,
SerializedCodeSanityCheckResult* rejection_result) {
// The previous call to FromCachedDataWithoutSource may have already rejected
// the cached data, so reuse the previous rejection result if it's not a
// success.
*rejection_result = data.sanity_check_result;
if (*rejection_result != SerializedCodeSanityCheckResult::kSuccess) {
// FromCachedDataWithoutSource doesn't check the source, so there can't be
// a source mismatch.
DCHECK_NE(*rejection_result,
SerializedCodeSanityCheckResult::kSourceMismatch);
cached_data->Reject();
return SerializedCodeData(nullptr, 0);
}
SerializedCodeData scd(cached_data);
if (data.source_sanity_check_result.has_value()) {
DCHECK_EQ(*data.source_sanity_check_result,
scd.SanityCheckJustSource(isolate, source, script_details));
*rejection_result = *data.source_sanity_check_result;
} else {
*rejection_result =
scd.SanityCheckJustSource(isolate, source, script_details);
}
if (*rejection_result != SerializedCodeSanityCheckResult::kSuccess) {
// This check only checks the source, so the only possible failure is a
// source mismatch.
DCHECK_EQ(*rejection_result,
SerializedCodeSanityCheckResult::kSourceMismatch);
cached_data->Reject();
return SerializedCodeData(nullptr, 0);
}
return scd;
}
} // namespace internal
} // namespace v8