// Copyright 2007-2010 the V8 project authors. All rights reserved. // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following // disclaimer in the documentation and/or other materials provided // with the distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include #include "include/cppgc/allocation.h" #include "include/v8-context.h" #include "include/v8-cppgc.h" #include "include/v8-extension.h" #include "include/v8-function.h" #include "include/v8-locker.h" #include "include/v8-platform.h" #include "include/v8-sandbox.h" #include "include/v8-snapshot.h" #include "src/api/api-inl.h" #include "src/codegen/compilation-cache.h" #include "src/codegen/compiler.h" #include "src/codegen/script-details.h" #include "src/common/assert-scope.h" #include "src/debug/debug-coverage.h" #include "src/flags/flags.h" #include "src/heap/heap-inl.h" #include "src/heap/heap-layout-inl.h" #include "src/heap/parked-scope-inl.h" #include "src/heap/read-only-heap.h" #include "src/heap/read-only-promotion.h" #include "src/heap/safepoint.h" #include "src/heap/spaces.h" #include "src/numbers/hash-seed-inl.h" #include "src/objects/js-array-buffer-inl.h" #include "src/objects/js-regexp-inl.h" #include "src/objects/objects-inl.h" #include "src/runtime/runtime.h" #include "src/snapshot/code-serializer.h" #include "src/snapshot/context-deserializer.h" #include "src/snapshot/context-serializer.h" #include "src/snapshot/read-only-deserializer.h" #include "src/snapshot/read-only-serializer.h" #include "src/snapshot/shared-heap-deserializer.h" #include "src/snapshot/shared-heap-serializer.h" #include "src/snapshot/snapshot-compression.h" #include "src/snapshot/snapshot.h" #include "src/snapshot/startup-deserializer.h" #include "src/snapshot/startup-serializer.h" #include "test/cctest/cctest.h" #include "test/cctest/heap/heap-utils.h" #include "test/cctest/setup-isolate-for-tests.h" namespace v8 { namespace internal { namespace { // A convenience struct to simplify management of the blobs required to // deserialize an isolate. struct StartupBlobs { base::Vector startup; base::Vector read_only; base::Vector shared_space; void Dispose() { startup.Dispose(); read_only.Dispose(); shared_space.Dispose(); } }; } // namespace // TestSerializer is used for testing isolate serialization. class TestSerializer { public: static v8::Isolate* NewIsolateInitialized() { const bool kEnableSerializer = true; DisableEmbeddedBlobRefcounting(); v8::Isolate* v8_isolate = NewIsolate(kEnableSerializer); v8::Isolate::Scope isolate_scope(v8_isolate); i::Isolate* isolate = reinterpret_cast(v8_isolate); isolate->InitWithoutSnapshot(); return v8_isolate; } // Wraps v8::Isolate::New, but with a test isolate under the hood. // Allows flexibility to bootstrap with or without snapshot even when // the production Isolate class has one or the other behavior baked in. static v8::Isolate* NewIsolate(const v8::Isolate::CreateParams& params) { const bool kEnableSerializer = false; v8::Isolate* v8_isolate = NewIsolate(kEnableSerializer); v8::Isolate::Initialize(v8_isolate, params); return v8_isolate; } static v8::Isolate* NewIsolateFromBlob(const StartupBlobs& blobs) { SnapshotData startup_snapshot(blobs.startup); SnapshotData read_only_snapshot(blobs.read_only); SnapshotData shared_space_snapshot(blobs.shared_space); const bool kEnableSerializer = false; v8::Isolate* v8_isolate = NewIsolate(kEnableSerializer); v8::Isolate::Scope isolate_scope(v8_isolate); i::Isolate* isolate = reinterpret_cast(v8_isolate); isolate->InitWithSnapshot(&startup_snapshot, &read_only_snapshot, &shared_space_snapshot, false); return v8_isolate; } private: // Creates an Isolate instance configured for testing. static v8::Isolate* NewIsolate(bool with_serializer) { i::Isolate* isolate = i::Isolate::New(); v8::Isolate* v8_isolate = reinterpret_cast(isolate); if (with_serializer) isolate->enable_serializer(); isolate->set_array_buffer_allocator(CcTest::array_buffer_allocator()); isolate->setup_delegate_ = new SetupIsolateDelegateForTests; return v8_isolate; } }; namespace { enum CodeCacheType { kLazy, kEager, kAfterExecute }; void DisableAlwaysOpt() { // Isolates prepared for serialization do not optimize. The only exception is // with the flag --always-turbofan. v8_flags.always_turbofan = false; } base::Vector WritePayload( const base::Vector& payload) { int length = payload.length(); uint8_t* blob = NewArray(length); memcpy(blob, payload.begin(), length); return base::VectorOf(blob, length); } // Convenience wrapper around the convenience wrapper. v8::StartupData CreateSnapshotDataBlob(const char* embedded_source) { v8::StartupData data = CreateSnapshotDataBlobInternal( v8::SnapshotCreator::FunctionCodeHandling::kClear, embedded_source); return data; } StartupBlobs Serialize(v8::Isolate* isolate) { // We have to create one context. One reason for this is so that the builtins // can be loaded from self hosted JS builtins and their addresses can be // processed. This will clear the pending fixups array, which would otherwise // contain GC roots that would confuse the serialization/deserialization // process. v8::Isolate::Scope isolate_scope(isolate); { v8::HandleScope scope(isolate); v8::Context::New(isolate); } Isolate* i_isolate = reinterpret_cast(isolate); { // Note that we need to run a garbage collection without stack at this // point, so that all dead objects are reclaimed. This is required to avoid // conservative stack scanning and guarantee deterministic behaviour. DisableConservativeStackScanningScopeForTesting no_stack_scanning( i_isolate->heap()); heap::InvokeMemoryReducingMajorGCs(i_isolate->heap()); } // Note this effectively reimplements Snapshot::Create, keep in sync. SafepointScope safepoint(i_isolate, SafepointKind::kIsolate); DisallowGarbageCollection no_gc; HandleScope scope(i_isolate); if (i_isolate->heap()->read_only_space()->writable()) { // Promote objects from mutable heap spaces to read-only space prior to // serialization. Objects can be promoted if a) they are themselves // immutable-after-deserialization and b) all objects in the transitive // object graph also satisfy condition a). ReadOnlyPromotion::Promote(i_isolate, safepoint, no_gc); // When creating the snapshot from scratch, we are responsible for sealing // the RO heap here. Note we cannot delegate the responsibility e.g. to // Isolate::Init since it should still be possible to allocate into RO // space after the Isolate has been initialized, for example as part of // Context creation. i_isolate->read_only_heap()->OnCreateHeapObjectsComplete(i_isolate); } ReadOnlySerializer read_only_serializer(i_isolate, Snapshot::kDefaultSerializerFlags); read_only_serializer.Serialize(); SharedHeapSerializer shared_space_serializer( i_isolate, Snapshot::kDefaultSerializerFlags); StartupSerializer ser(i_isolate, Snapshot::kDefaultSerializerFlags, &shared_space_serializer); ser.SerializeStrongReferences(no_gc); ser.SerializeWeakReferencesAndDeferred(); shared_space_serializer.FinalizeSerialization(); SnapshotData startup_snapshot(&ser); SnapshotData read_only_snapshot(&read_only_serializer); SnapshotData shared_space_snapshot(&shared_space_serializer); return {WritePayload(startup_snapshot.RawData()), WritePayload(read_only_snapshot.RawData()), WritePayload(shared_space_snapshot.RawData())}; } base::Vector ConstructSource(base::Vector head, base::Vector body, base::Vector tail, int repeats) { size_t source_length = head.size() + body.size() * repeats + tail.size(); char* source = NewArray(source_length); CopyChars(source, head.begin(), head.length()); for (int i = 0; i < repeats; i++) { CopyChars(source + head.length() + i * body.length(), body.begin(), body.length()); } CopyChars(source + head.length() + repeats * body.length(), tail.begin(), tail.length()); return base::VectorOf(source, source_length); } v8::Isolate* Deserialize(const StartupBlobs& blobs) { v8::Isolate* isolate = TestSerializer::NewIsolateFromBlob(blobs); CHECK(isolate); return isolate; } void SanityCheck(v8::Isolate* v8_isolate) { Isolate* isolate = reinterpret_cast(v8_isolate); v8::HandleScope scope(v8_isolate); #ifdef VERIFY_HEAP HeapVerifier::VerifyHeap(isolate->heap()); #endif CHECK(IsJSObject(*isolate->global_object())); CHECK(IsContext(*isolate->native_context())); isolate->factory()->InternalizeString(base::StaticCharVector("Empty")); } void TestStartupSerializerOnceImpl() { v8::Isolate* isolate = TestSerializer::NewIsolateInitialized(); StartupBlobs blobs = Serialize(isolate); isolate->Dispose(); isolate = Deserialize(blobs); { v8::HandleScope handle_scope(isolate); v8::Isolate::Scope isolate_scope(isolate); v8::Local env = v8::Context::New(isolate); env->Enter(); SanityCheck(isolate); } isolate->Dispose(); blobs.Dispose(); FreeCurrentEmbeddedBlob(); } } // namespace UNINITIALIZED_TEST(StartupSerializerOnce) { DisableAlwaysOpt(); TestStartupSerializerOnceImpl(); } UNINITIALIZED_TEST(StartupSerializerTwice) { DisableAlwaysOpt(); v8::Isolate* isolate = TestSerializer::NewIsolateInitialized(); StartupBlobs blobs1 = Serialize(isolate); isolate->Dispose(); isolate = Deserialize(blobs1); StartupBlobs blobs2 = Serialize(isolate); isolate->Dispose(); blobs1.Dispose(); isolate = Deserialize(blobs2); { v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); v8::Local env = v8::Context::New(isolate); env->Enter(); SanityCheck(isolate); } isolate->Dispose(); blobs2.Dispose(); FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(StartupSerializerOnceRunScript) { DisableAlwaysOpt(); v8::Isolate* isolate = TestSerializer::NewIsolateInitialized(); StartupBlobs blobs = Serialize(isolate); isolate->Dispose(); isolate = Deserialize(blobs); { v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); v8::Local env = v8::Context::New(isolate); env->Enter(); const char* c_source = "\"1234\".length"; v8::Local script = v8_compile(c_source); v8::Maybe result = script->Run(isolate->GetCurrentContext()) .ToLocalChecked() ->Int32Value(isolate->GetCurrentContext()); CHECK_EQ(4, result.FromJust()); } isolate->Dispose(); blobs.Dispose(); FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(StartupSerializerTwiceRunScript) { DisableAlwaysOpt(); v8::Isolate* isolate = TestSerializer::NewIsolateInitialized(); StartupBlobs blobs1 = Serialize(isolate); isolate->Dispose(); isolate = Deserialize(blobs1); StartupBlobs blobs2 = Serialize(isolate); isolate->Dispose(); blobs1.Dispose(); isolate = Deserialize(blobs2); { v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); v8::Local env = v8::Context::New(isolate); env->Enter(); const char* c_source = "\"1234\".length"; v8::Local script = v8_compile(c_source); v8::Maybe result = script->Run(isolate->GetCurrentContext()) .ToLocalChecked() ->Int32Value(isolate->GetCurrentContext()); CHECK_EQ(4, result.FromJust()); } isolate->Dispose(); blobs2.Dispose(); FreeCurrentEmbeddedBlob(); } static void SerializeContext(base::Vector* startup_blob_out, base::Vector* read_only_blob_out, base::Vector* shared_space_blob_out, base::Vector* context_blob_out) { v8::Isolate* v8_isolate = TestSerializer::NewIsolateInitialized(); Isolate* isolate = reinterpret_cast(v8_isolate); Heap* heap = isolate->heap(); { v8::Isolate::Scope isolate_scope(v8_isolate); v8::Persistent env; { HandleScope scope(isolate); env.Reset(v8_isolate, v8::Context::New(v8_isolate)); } CHECK(!env.IsEmpty()); { v8::HandleScope handle_scope(v8_isolate); v8::Local::New(v8_isolate, env)->Enter(); } // If we don't do this then we end up with a stray root pointing at the // context even after we have disposed of env. { // Note that we need to run a garbage collection without stack at this // point, so that all dead objects are reclaimed. This is required to // avoid conservative stack scanning and guarantee deterministic // behaviour. DisableConservativeStackScanningScopeForTesting no_stack_scanning(heap); heap::InvokeMemoryReducingMajorGCs(heap); } { v8::HandleScope handle_scope(v8_isolate); v8::Local::New(v8_isolate, env)->Exit(); } HandleScope scope(isolate); i::Tagged raw_context = i::Cast(*v8::Utils::OpenPersistent(env)); env.Reset(); IsolateSafepointScope safepoint(heap); if (!isolate->initialized_from_snapshot()) { // When creating the snapshot from scratch, we are responsible for sealing // the RO heap here. Note we cannot delegate the responsibility e.g. to // Isolate::Init since it should still be possible to allocate into RO // space after the Isolate has been initialized, for example as part of // Context creation. isolate->read_only_heap()->OnCreateHeapObjectsComplete(isolate); } DisallowGarbageCollection no_gc; SnapshotByteSink read_only_sink; ReadOnlySerializer read_only_serializer(isolate, Snapshot::kDefaultSerializerFlags); read_only_serializer.Serialize(); SharedHeapSerializer shared_space_serializer( isolate, Snapshot::kDefaultSerializerFlags); SnapshotByteSink startup_sink; StartupSerializer startup_serializer( isolate, Snapshot::kDefaultSerializerFlags, &shared_space_serializer); startup_serializer.SerializeStrongReferences(no_gc); SnapshotByteSink context_sink; ContextSerializer context_serializer( isolate, Snapshot::kDefaultSerializerFlags, &startup_serializer, SerializeEmbedderFieldsCallback(v8::SerializeInternalFieldsCallback())); context_serializer.Serialize(&raw_context, no_gc); startup_serializer.SerializeWeakReferencesAndDeferred(); shared_space_serializer.FinalizeSerialization(); SnapshotData read_only_snapshot(&read_only_serializer); SnapshotData shared_space_snapshot(&shared_space_serializer); SnapshotData startup_snapshot(&startup_serializer); SnapshotData context_snapshot(&context_serializer); *context_blob_out = WritePayload(context_snapshot.RawData()); *startup_blob_out = WritePayload(startup_snapshot.RawData()); *read_only_blob_out = WritePayload(read_only_snapshot.RawData()); *shared_space_blob_out = WritePayload(shared_space_snapshot.RawData()); } v8_isolate->Dispose(); } #ifdef SNAPSHOT_COMPRESSION UNINITIALIZED_TEST(SnapshotCompression) { DisableAlwaysOpt(); base::Vector startup_blob; base::Vector read_only_blob; base::Vector shared_space_blob; base::Vector context_blob; SerializeContext(&startup_blob, &read_only_blob, &shared_space_blob, &context_blob); SnapshotData original_snapshot_data(context_blob); SnapshotData compressed = i::SnapshotCompression::Compress(&original_snapshot_data); SnapshotData decompressed = i::SnapshotCompression::Decompress(compressed.RawData()); CHECK_EQ(context_blob, decompressed.RawData()); startup_blob.Dispose(); read_only_blob.Dispose(); shared_space_blob.Dispose(); context_blob.Dispose(); } #endif // SNAPSHOT_COMPRESSION UNINITIALIZED_TEST(ContextSerializerContext) { DisableAlwaysOpt(); base::Vector startup_blob; base::Vector read_only_blob; base::Vector shared_space_blob; base::Vector context_blob; SerializeContext(&startup_blob, &read_only_blob, &shared_space_blob, &context_blob); StartupBlobs blobs = {startup_blob, read_only_blob, shared_space_blob}; v8::Isolate* v8_isolate = TestSerializer::NewIsolateFromBlob(blobs); CHECK(v8_isolate); { v8::Isolate::Scope isolate_scope(v8_isolate); Isolate* isolate = reinterpret_cast(v8_isolate); HandleScope handle_scope(isolate); DirectHandle root; DirectHandle global_proxy = isolate->factory()->NewUninitializedJSGlobalProxy( JSGlobalProxy::SizeWithEmbedderFields(0)); { SnapshotData snapshot_data(context_blob); root = ContextDeserializer::DeserializeContext( isolate, &snapshot_data, 0, false, global_proxy, DeserializeEmbedderFieldsCallback( v8::DeserializeInternalFieldsCallback())) .ToHandleChecked(); CHECK(IsContext(*root)); CHECK(Cast(root)->global_proxy() == *global_proxy); } DirectHandle root2; { SnapshotData snapshot_data(context_blob); root2 = ContextDeserializer::DeserializeContext( isolate, &snapshot_data, 0, false, global_proxy, DeserializeEmbedderFieldsCallback( v8::DeserializeInternalFieldsCallback())) .ToHandleChecked(); CHECK(IsContext(*root2)); CHECK(!root.is_identical_to(root2)); } context_blob.Dispose(); } v8_isolate->Dispose(); blobs.Dispose(); FreeCurrentEmbeddedBlob(); } static void SerializeCustomContext( base::Vector* startup_blob_out, base::Vector* read_only_blob_out, base::Vector* shared_space_blob_out, base::Vector* context_blob_out) { v8::Isolate* isolate = TestSerializer::NewIsolateInitialized(); Isolate* i_isolate = reinterpret_cast(isolate); { v8::Global env; v8::Isolate::Scope isolate_scope(isolate); { HandleScope scope(i_isolate); env.Reset(isolate, v8::Context::New(isolate)); } CHECK(!env.IsEmpty()); { v8::HandleScope handle_scope(isolate); v8::Local::New(isolate, env)->Enter(); // After execution, e's function context refers to the global object. CompileRun( "var e;" "(function() {" " e = function(s) { return eval (s); }" "})();" "var o = this;" "var r = Math.random();" "var c = Math.sin(0) + Math.cos(0);" "var f = (function(a, b) { return a + b; }).bind(1, 2, 3);" "var s = parseInt('12345');" "var p = 0;" "(async ()=>{ p = await 42; })();"); base::Vector source = ConstructSource( base::StaticCharVector("function g() { return [,"), base::StaticCharVector("1,"), base::StaticCharVector("];} a = g(); b = g(); b.push(1);"), 100000); v8::MaybeLocal source_str = v8::String::NewFromUtf8( isolate, source.begin(), v8::NewStringType::kNormal, source.length()); CompileRun(source_str.ToLocalChecked()); source.Dispose(); } // If we don't do this then we end up with a stray root pointing at the // context even after we have disposed of env. { // Note that we need to run a garbage collection without stack at this // point, so that all dead objects are reclaimed. This is required to // avoid conservative stack scanning and guarantee deterministic // behaviour. DisableConservativeStackScanningScopeForTesting no_stack_scanning( i_isolate->heap()); heap::InvokeMemoryReducingMajorGCs(i_isolate->heap()); } { v8::HandleScope handle_scope(isolate); v8::Local::New(isolate, env)->Exit(); } { HandleScope scope(i_isolate); i::Tagged raw_context = i::Cast(*v8::Utils::OpenPersistent(env)); // On purpose we do not reset the global context here --- env.Reset() --- // so that it is found below, during heap verification at the GC before // isolate disposal. SafepointScope safepoint(i_isolate, SafepointKind::kIsolate); DisallowGarbageCollection no_gc; if (i_isolate->heap()->read_only_space()->writable()) { // Promote objects from mutable heap spaces to read-only space prior to // serialization. Objects can be promoted if a) they are themselves // immutable-after-deserialization and b) all objects in the transitive // object graph also satisfy condition a). ReadOnlyPromotion::Promote(i_isolate, safepoint, no_gc); // When creating the snapshot from scratch, we are responsible for // sealing the RO heap here. Note we cannot delegate the responsibility // e.g. to Isolate::Init since it should still be possible to allocate // into RO space after the Isolate has been initialized, for example as // part of Context creation. i_isolate->read_only_heap()->OnCreateHeapObjectsComplete(i_isolate); } SnapshotByteSink read_only_sink; ReadOnlySerializer read_only_serializer( i_isolate, Snapshot::kDefaultSerializerFlags); read_only_serializer.Serialize(); SharedHeapSerializer shared_space_serializer( i_isolate, Snapshot::kDefaultSerializerFlags); SnapshotByteSink startup_sink; StartupSerializer startup_serializer(i_isolate, Snapshot::kDefaultSerializerFlags, &shared_space_serializer); startup_serializer.SerializeStrongReferences(no_gc); SnapshotByteSink context_sink; ContextSerializer context_serializer( i_isolate, Snapshot::kDefaultSerializerFlags, &startup_serializer, SerializeEmbedderFieldsCallback( v8::SerializeInternalFieldsCallback())); context_serializer.Serialize(&raw_context, no_gc); startup_serializer.SerializeWeakReferencesAndDeferred(); shared_space_serializer.FinalizeSerialization(); SnapshotData read_only_snapshot(&read_only_serializer); SnapshotData shared_space_snapshot(&shared_space_serializer); SnapshotData startup_snapshot(&startup_serializer); SnapshotData context_snapshot(&context_serializer); *context_blob_out = WritePayload(context_snapshot.RawData()); *startup_blob_out = WritePayload(startup_snapshot.RawData()); *read_only_blob_out = WritePayload(read_only_snapshot.RawData()); *shared_space_blob_out = WritePayload(shared_space_snapshot.RawData()); } // At this point, the heap must be in a consistent state and this GC must // not crash, even with the live handle to the global environment. heap::InvokeMajorGC(i_isolate->heap()); // We reset the global context, before isolate disposal. } isolate->Dispose(); } UNINITIALIZED_TEST(ContextSerializerCustomContext) { DisableAlwaysOpt(); base::Vector startup_blob; base::Vector read_only_blob; base::Vector shared_space_blob; base::Vector context_blob; SerializeCustomContext(&startup_blob, &read_only_blob, &shared_space_blob, &context_blob); StartupBlobs blobs = {startup_blob, read_only_blob, shared_space_blob}; v8::Isolate* v8_isolate = TestSerializer::NewIsolateFromBlob(blobs); CHECK(v8_isolate); { v8::Isolate::Scope isolate_scope(v8_isolate); Isolate* isolate = reinterpret_cast(v8_isolate); HandleScope handle_scope(isolate); DirectHandle root; Handle global_proxy = isolate->factory()->NewUninitializedJSGlobalProxy( JSGlobalProxy::SizeWithEmbedderFields(0)); { SnapshotData snapshot_data(context_blob); root = ContextDeserializer::DeserializeContext( isolate, &snapshot_data, 0, false, global_proxy, DeserializeEmbedderFieldsCallback( v8::DeserializeInternalFieldsCallback())) .ToHandleChecked(); CHECK(IsContext(*root)); DirectHandle context = Cast(root); // Add context to the weak native context list Cast(context)->set(Context::NEXT_CONTEXT_LINK, isolate->heap()->native_contexts_list(), UPDATE_WRITE_BARRIER); isolate->heap()->set_native_contexts_list(*context); CHECK(context->global_proxy() == *global_proxy); DirectHandle o = isolate->factory()->NewStringFromAsciiChecked("o"); DirectHandle global_object(context->global_object(), isolate); DirectHandle property = JSReceiver::GetDataProperty(isolate, global_object, o); CHECK(property.is_identical_to(global_proxy)); v8::Local v8_context = v8::Utils::ToLocal(context); v8::Context::Scope context_scope(v8_context); double r = CompileRun("r") ->ToNumber(v8_isolate->GetCurrentContext()) .ToLocalChecked() ->Value(); CHECK(0.0 <= r && r < 1.0); // Math.random still works. double random = CompileRun("Math.random()") ->ToNumber(v8_isolate->GetCurrentContext()) .ToLocalChecked() ->Value(); CHECK(0.0 <= random && random < 1.0); double c = CompileRun("c") ->ToNumber(v8_isolate->GetCurrentContext()) .ToLocalChecked() ->Value(); CHECK_EQ(1, c); int f = CompileRun("f()") ->ToNumber(v8_isolate->GetCurrentContext()) .ToLocalChecked() ->Int32Value(v8_isolate->GetCurrentContext()) .FromJust(); CHECK_EQ(5, f); f = CompileRun("e('f()')") ->ToNumber(v8_isolate->GetCurrentContext()) .ToLocalChecked() ->Int32Value(v8_isolate->GetCurrentContext()) .FromJust(); CHECK_EQ(5, f); v8::Local s = CompileRun("s") ->ToString(v8_isolate->GetCurrentContext()) .ToLocalChecked(); CHECK(s->Equals(v8_isolate->GetCurrentContext(), v8_str("12345")) .FromJust()); v8::Local p = CompileRun("p") ->ToString(v8_isolate->GetCurrentContext()) .ToLocalChecked(); CHECK( p->Equals(v8_isolate->GetCurrentContext(), v8_str("42")).FromJust()); int a = CompileRun("a.length") ->ToNumber(v8_isolate->GetCurrentContext()) .ToLocalChecked() ->Int32Value(v8_isolate->GetCurrentContext()) .FromJust(); CHECK_EQ(100001, a); int b = CompileRun("b.length") ->ToNumber(v8_isolate->GetCurrentContext()) .ToLocalChecked() ->Int32Value(v8_isolate->GetCurrentContext()) .FromJust(); CHECK_EQ(100002, b); } context_blob.Dispose(); } v8_isolate->Dispose(); blobs.Dispose(); FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlob1) { DisableAlwaysOpt(); const char* source1 = "function f() { return 42; }"; DisableEmbeddedBlobRefcounting(); v8::StartupData data1 = CreateSnapshotDataBlob(source1); v8::Isolate::CreateParams params1; params1.snapshot_blob = &data1; params1.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate1 = TestSerializer::NewIsolate(params1); { v8::Isolate::Scope i_scope(isolate1); v8::HandleScope h_scope(isolate1); v8::Local context = v8::Context::New(isolate1); v8::Context::Scope c_scope(context); v8::Maybe result = CompileRun("f()")->Int32Value(isolate1->GetCurrentContext()); CHECK_EQ(42, result.FromJust()); CHECK(CompileRun("this.g")->IsUndefined()); } isolate1->Dispose(); delete[] data1.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } static void UnreachableCallback(const FunctionCallbackInfo& info) { UNREACHABLE(); } UNINITIALIZED_TEST(CustomSnapshotDataBlobOverwriteGlobal) { DisableAlwaysOpt(); const char* source1 = "function f() { return 42; }"; DisableEmbeddedBlobRefcounting(); v8::StartupData data1 = CreateSnapshotDataBlob(source1); v8::Isolate::CreateParams params1; params1.snapshot_blob = &data1; params1.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test that the snapshot overwrites the object template when there are // duplicate global properties. v8::Isolate* isolate1 = TestSerializer::NewIsolate(params1); { v8::Isolate::Scope i_scope(isolate1); v8::HandleScope h_scope(isolate1); v8::Local global_template = v8::ObjectTemplate::New(isolate1); global_template->Set( isolate1, "f", v8::FunctionTemplate::New(isolate1, UnreachableCallback)); v8::Local context = v8::Context::New(isolate1, nullptr, global_template); v8::Context::Scope c_scope(context); v8::Maybe result = CompileRun("f()")->Int32Value(isolate1->GetCurrentContext()); CHECK_EQ(42, result.FromJust()); } isolate1->Dispose(); delete[] data1.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlobStringNotInternalized) { DisableAlwaysOpt(); const char* source1 = R"javascript( // String would be internalized if it came from a literal so create "AB" // via a function call. var global = String.fromCharCode(65, 66); function f() { return global; } )javascript"; DisableEmbeddedBlobRefcounting(); v8::StartupData data1 = CreateSnapshotDataBlob(source1); v8::Isolate::CreateParams params1; params1.snapshot_blob = &data1; params1.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate1 = TestSerializer::NewIsolate(params1); { v8::Isolate::Scope i_scope(isolate1); v8::HandleScope h_scope(isolate1); v8::Local context = v8::Context::New(isolate1); v8::Context::Scope c_scope(context); v8::Local result = CompileRun("f()").As(); CHECK(result->IsString()); i::Tagged str = *v8::Utils::OpenDirectHandle(*result.As()); CHECK_EQ(std::string(str->ToCString().get()), "AB"); CHECK(!IsInternalizedString(str)); CHECK(!i::ReadOnlyHeap::Contains(str)); } isolate1->Dispose(); delete[] data1.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } namespace { void TestCustomSnapshotDataBlobWithIrregexpCode( v8::SnapshotCreator::FunctionCodeHandling function_code_handling) { DisableAlwaysOpt(); const char* source = "var re1 = /\\/\\*[^*]*\\*+([^/*][^*]*\\*+)*\\//;\n" "function f() { return '/* a comment */'.search(re1); }\n" "function g() { return 'not a comment'.search(re1); }\n" "function h() { return '// this is a comment'.search(re1); }\n" "var re2 = /a/;\n" "function i() { return '/* a comment */'.search(re2); }\n" "f(); f(); g(); g(); h(); h(); i(); i();\n"; DisableEmbeddedBlobRefcounting(); v8::StartupData data1 = CreateSnapshotDataBlobInternal(function_code_handling, source); v8::Isolate::CreateParams params1; params1.snapshot_blob = &data1; params1.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate1 = TestSerializer::NewIsolate(params1); Isolate* i_isolate1 = reinterpret_cast(isolate1); { v8::Isolate::Scope i_scope(isolate1); v8::HandleScope h_scope(isolate1); v8::Local context = v8::Context::New(isolate1); v8::Context::Scope c_scope(context); { // Check that compiled irregexp code has been flushed prior to // serialization. i::DirectHandle re = Utils::OpenDirectHandle(*CompileRun("re1").As()); CHECK(!re->data(i_isolate1)->HasCompiledCode()); } { v8::Maybe result = CompileRun("f()")->Int32Value(isolate1->GetCurrentContext()); CHECK_EQ(0, result.FromJust()); } { v8::Maybe result = CompileRun("g()")->Int32Value(isolate1->GetCurrentContext()); CHECK_EQ(-1, result.FromJust()); } { v8::Maybe result = CompileRun("h()")->Int32Value(isolate1->GetCurrentContext()); CHECK_EQ(-1, result.FromJust()); } { // Check that ATOM regexp remains valid. i::DirectHandle re = Utils::OpenDirectHandle(*CompileRun("re2").As()); i::Tagged data = re->data(i_isolate1); CHECK_EQ(data->type_tag(), RegExpData::Type::ATOM); CHECK(!data->HasCompiledCode()); } } isolate1->Dispose(); delete[] data1.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } } // namespace UNINITIALIZED_TEST(CustomSnapshotDataBlobWithIrregexpCodeKeepCode) { TestCustomSnapshotDataBlobWithIrregexpCode( v8::SnapshotCreator::FunctionCodeHandling::kKeep); } UNINITIALIZED_TEST(CustomSnapshotDataBlobWithIrregexpCodeClearCode) { TestCustomSnapshotDataBlobWithIrregexpCode( v8::SnapshotCreator::FunctionCodeHandling::kClear); } UNINITIALIZED_TEST(SnapshotChecksum) { DisableAlwaysOpt(); const char* source1 = "function f() { return 42; }"; DisableEmbeddedBlobRefcounting(); v8::StartupData data1 = CreateSnapshotDataBlob(source1); CHECK(i::Snapshot::VerifyChecksum(&data1)); const_cast(data1.data)[142] = data1.data[142] ^ 4; // Flip a bit. CHECK(!i::Snapshot::VerifyChecksum(&data1)); delete[] data1.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } struct InternalFieldData { uint32_t data; }; v8::StartupData SerializeInternalFields(v8::Local holder, int index, void* data) { if (data == reinterpret_cast(2000)) { // Used for SnapshotCreatorTemplates test. We check that none of the fields // have been cleared yet. CHECK_NOT_NULL(holder->GetAlignedPointerFromInternalField(1)); } else { CHECK_EQ(reinterpret_cast(2016), data); } if (index != 1) return {nullptr, 0}; InternalFieldData* embedder_field = static_cast( holder->GetAlignedPointerFromInternalField(index)); if (embedder_field == nullptr) return {nullptr, 0}; int size = sizeof(*embedder_field); char* payload = new char[size]; // We simply use memcpy to serialize the content. memcpy(payload, embedder_field, size); return {payload, size}; } std::vector deserialized_data; void DeserializeInternalFields(v8::Local holder, int index, v8::StartupData payload, void* data) { if (payload.raw_size == 0) { holder->SetAlignedPointerInInternalField(index, nullptr); return; } CHECK_EQ(reinterpret_cast(2017), data); InternalFieldData* embedder_field = new InternalFieldData{0}; memcpy(embedder_field, payload.data, payload.raw_size); holder->SetAlignedPointerInInternalField(index, embedder_field); deserialized_data.push_back(embedder_field); } using Int32Expectations = std::vector>; void TestInt32Expectations(const Int32Expectations& expectations) { for (const auto& e : expectations) { ExpectInt32(std::get<0>(e), std::get<1>(e)); } } struct SnapshotCreatorParams { explicit SnapshotCreatorParams(const intptr_t* external_references = nullptr, const StartupData* existing_blob = nullptr) { allocator.reset(ArrayBuffer::Allocator::NewDefaultAllocator()); create_params.array_buffer_allocator = allocator.get(); create_params.external_references = external_references; create_params.snapshot_blob = existing_blob; } std::unique_ptr allocator; v8::Isolate::CreateParams create_params; }; void TypedArrayTestHelper( const char* code, const Int32Expectations& expectations, const char* code_to_run_after_restore = nullptr, const Int32Expectations& after_restore_expectations = Int32Expectations(), v8::ArrayBuffer::Allocator* allocator = nullptr) { DisableAlwaysOpt(); i::v8_flags.allow_natives_syntax = true; DisableEmbeddedBlobRefcounting(); v8::StartupData blob; { SnapshotCreatorParams testing_params; v8::SnapshotCreator creator(testing_params.create_params); v8::Isolate* isolate = creator.GetIsolate(); { v8::HandleScope handle_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope context_scope(context); CompileRun(code); TestInt32Expectations(expectations); creator.SetDefaultContext( context, v8::SerializeInternalFieldsCallback( SerializeInternalFields, reinterpret_cast(2016))); } blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); } v8::Isolate::CreateParams create_params; create_params.snapshot_blob = &blob; create_params.array_buffer_allocator = allocator != nullptr ? allocator : CcTest::array_buffer_allocator(); v8::Isolate* isolate = TestSerializer::NewIsolate(create_params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New( isolate, nullptr, v8::MaybeLocal(), v8::MaybeLocal(), v8::DeserializeInternalFieldsCallback(DeserializeInternalFields, reinterpret_cast(2017))); CHECK(deserialized_data.empty()); // We do not expect any embedder data. v8::Context::Scope c_scope(context); TestInt32Expectations(expectations); if (code_to_run_after_restore) { CompileRun(code_to_run_after_restore); } TestInt32Expectations(after_restore_expectations); } isolate->Dispose(); delete[] blob.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlobWithOffHeapTypedArray) { const char* code = "var x = new Uint8Array(128);" "x[0] = 12;" "var arr = new Array(17);" "arr[1] = 24;" "var y = new Uint32Array(arr);" "var buffer = new ArrayBuffer(128);" "var z = new Int16Array(buffer);" "z[0] = 48;"; Int32Expectations expectations = {std::make_tuple("x[0]", 12), std::make_tuple("y[1]", 24), std::make_tuple("z[0]", 48)}; TypedArrayTestHelper(code, expectations); } UNINITIALIZED_TEST(CustomSnapshotDataBlobSharedArrayBuffer) { const char* code = "var x = new Int32Array([12, 24, 48, 96]);" "var y = new Uint8Array(x.buffer)"; Int32Expectations expectations = { std::make_tuple("x[0]", 12), std::make_tuple("x[1]", 24), #if !V8_TARGET_BIG_ENDIAN std::make_tuple("y[0]", 12), std::make_tuple("y[1]", 0), std::make_tuple("y[2]", 0), std::make_tuple("y[3]", 0), std::make_tuple("y[4]", 24) #else std::make_tuple("y[3]", 12), std::make_tuple("y[2]", 0), std::make_tuple("y[1]", 0), std::make_tuple("y[0]", 0), std::make_tuple("y[7]", 24) #endif }; TypedArrayTestHelper(code, expectations); } UNINITIALIZED_TEST(CustomSnapshotDataBlobArrayBufferWithOffset) { const char* code = "var x = new Int32Array([12, 24, 48, 96]);" "var y = new Int32Array(x.buffer, 4, 2)"; Int32Expectations expectations = { std::make_tuple("x[1]", 24), std::make_tuple("x[2]", 48), std::make_tuple("y[0]", 24), std::make_tuple("y[1]", 48), }; // Verify that the typed arrays use the same buffer (not independent copies). const char* code_to_run_after_restore = "x[2] = 57; y[0] = 42;"; Int32Expectations after_restore_expectations = { std::make_tuple("x[1]", 42), std::make_tuple("y[1]", 57), }; TypedArrayTestHelper(code, expectations, code_to_run_after_restore, after_restore_expectations); } UNINITIALIZED_TEST(CustomSnapshotDataBlobDataView) { const char* code = "var x = new Int8Array([1, 2, 3, 4]);" "var v = new DataView(x.buffer)"; Int32Expectations expectations = {std::make_tuple("v.getInt8(0)", 1), std::make_tuple("v.getInt8(1)", 2), std::make_tuple("v.getInt16(0)", 258), std::make_tuple("v.getInt16(1)", 515)}; TypedArrayTestHelper(code, expectations); } namespace { class AlternatingArrayBufferAllocator : public v8::ArrayBuffer::Allocator { public: AlternatingArrayBufferAllocator() : allocation_fails_(false), allocator_(v8::ArrayBuffer::Allocator::NewDefaultAllocator()) {} ~AlternatingArrayBufferAllocator() { delete allocator_; } void* Allocate(size_t length) override { allocation_fails_ = !allocation_fails_; if (allocation_fails_) return nullptr; return allocator_->Allocate(length); } void* AllocateUninitialized(size_t length) override { return this->Allocate(length); } void Free(void* data, size_t size) override { allocator_->Free(data, size); } private: bool allocation_fails_; v8::ArrayBuffer::Allocator* allocator_; }; } // anonymous namespace UNINITIALIZED_TEST(CustomSnapshotManyArrayBuffers) { const char* code = "var buffers = [];" "for (let i = 0; i < 70; i++) buffers.push(new Uint8Array(1000));"; Int32Expectations expectations = {std::make_tuple("buffers.length", 70)}; std::unique_ptr allocator( new AlternatingArrayBufferAllocator()); TypedArrayTestHelper(code, expectations, nullptr, Int32Expectations(), allocator.get()); } UNINITIALIZED_TEST(CustomSnapshotDataBlobDetachedArrayBuffer) { const char* code = "var x = new Int16Array([12, 24, 48]);" "%ArrayBufferDetach(x.buffer);"; Int32Expectations expectations = {std::make_tuple("x.buffer.byteLength", 0), std::make_tuple("x.length", 0)}; DisableAlwaysOpt(); i::v8_flags.allow_natives_syntax = true; DisableEmbeddedBlobRefcounting(); v8::StartupData blob; { SnapshotCreatorParams testing_params; v8::SnapshotCreator creator(testing_params.create_params); v8::Isolate* isolate = creator.GetIsolate(); { v8::HandleScope handle_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope context_scope(context); CompileRun(code); TestInt32Expectations(expectations); creator.SetDefaultContext( context, v8::SerializeInternalFieldsCallback( SerializeInternalFields, reinterpret_cast(2016))); } blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); } v8::Isolate::CreateParams create_params; create_params.snapshot_blob = &blob; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate = TestSerializer::NewIsolate(create_params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New( isolate, nullptr, v8::MaybeLocal(), v8::MaybeLocal(), v8::DeserializeInternalFieldsCallback(DeserializeInternalFields, reinterpret_cast(2017))); v8::Context::Scope c_scope(context); TestInt32Expectations(expectations); v8::Local x = CompileRun("x"); CHECK(x->IsTypedArray()); i::DirectHandle array = i::Cast(v8::Utils::OpenDirectHandle(*x)); CHECK(array->WasDetached()); } isolate->Dispose(); delete[] blob.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } i::DirectHandle GetBufferFromTypedArray( v8::Local typed_array) { CHECK(typed_array->IsTypedArray()); i::DirectHandle view = i::Cast(v8::Utils::OpenDirectHandle(*typed_array)); return i::direct_handle(i::Cast(view->buffer()), view->GetIsolate()); } UNINITIALIZED_TEST(CustomSnapshotDataBlobOnOrOffHeapTypedArray) { const char* code = "var x = new Uint8Array(8);" "x[0] = 12;" "x[7] = 24;" "var y = new Int16Array([12, 24, 48]);" "var z = new Int32Array(64);" "z[0] = 96;"; Int32Expectations expectations = { std::make_tuple("x[0]", 12), std::make_tuple("x[7]", 24), std::make_tuple("y[2]", 48), std::make_tuple("z[0]", 96)}; DisableAlwaysOpt(); i::v8_flags.allow_natives_syntax = true; DisableEmbeddedBlobRefcounting(); v8::StartupData blob; { SnapshotCreatorParams testing_params; v8::SnapshotCreator creator(testing_params.create_params); v8::Isolate* isolate = creator.GetIsolate(); { v8::HandleScope handle_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope context_scope(context); CompileRun(code); TestInt32Expectations(expectations); i::DirectHandle buffer = GetBufferFromTypedArray(CompileRun("x")); // The resulting buffer should be on-heap. CHECK(buffer->IsEmpty()); creator.SetDefaultContext( context, v8::SerializeInternalFieldsCallback( SerializeInternalFields, reinterpret_cast(2016))); } blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); } v8::Isolate::CreateParams create_params; create_params.snapshot_blob = &blob; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate = TestSerializer::NewIsolate(create_params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New( isolate, nullptr, v8::MaybeLocal(), v8::MaybeLocal(), v8::DeserializeInternalFieldsCallback(DeserializeInternalFields, reinterpret_cast(2017))); v8::Context::Scope c_scope(context); TestInt32Expectations(expectations); i::DirectHandle buffer = GetBufferFromTypedArray(CompileRun("x")); // The resulting buffer should be on-heap. CHECK(buffer->IsEmpty()); buffer = GetBufferFromTypedArray(CompileRun("y")); CHECK(buffer->IsEmpty()); buffer = GetBufferFromTypedArray(CompileRun("z")); // The resulting buffer should be off-heap. CHECK(!buffer->IsEmpty()); } isolate->Dispose(); delete[] blob.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlobTypedArrayNoEmbedderFieldCallback) { const char* code = "var x = new Uint8Array(8);"; DisableAlwaysOpt(); i::v8_flags.allow_natives_syntax = true; DisableEmbeddedBlobRefcounting(); v8::StartupData blob; { SnapshotCreatorParams testing_params; v8::SnapshotCreator creator(testing_params.create_params); v8::Isolate* isolate = creator.GetIsolate(); { v8::HandleScope handle_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope context_scope(context); CompileRun(code); creator.SetDefaultContext(context, v8::SerializeInternalFieldsCallback()); } blob = creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kClear); } v8::Isolate::CreateParams create_params; create_params.snapshot_blob = &blob; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate = TestSerializer::NewIsolate(create_params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New( isolate, nullptr, v8::MaybeLocal(), v8::MaybeLocal(), v8::DeserializeInternalFieldsCallback()); v8::Context::Scope c_scope(context); } isolate->Dispose(); delete[] blob.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlob2) { DisableAlwaysOpt(); const char* source2 = "function f() { return g() * 2; }" "function g() { return 43; }" "/./.test('a')"; DisableEmbeddedBlobRefcounting(); v8::StartupData data2 = CreateSnapshotDataBlob(source2); v8::Isolate::CreateParams params2; params2.snapshot_blob = &data2; params2.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate2 = TestSerializer::NewIsolate(params2); { v8::Isolate::Scope i_scope(isolate2); v8::HandleScope h_scope(isolate2); v8::Local context = v8::Context::New(isolate2); v8::Context::Scope c_scope(context); v8::Maybe result = CompileRun("f()")->Int32Value(isolate2->GetCurrentContext()); CHECK_EQ(86, result.FromJust()); result = CompileRun("g()")->Int32Value(isolate2->GetCurrentContext()); CHECK_EQ(43, result.FromJust()); } isolate2->Dispose(); delete[] data2.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } static void SerializationFunctionTemplate( const v8::FunctionCallbackInfo& info) { CHECK(i::ValidateCallbackInfo(info)); info.GetReturnValue().Set(info[0]); } UNINITIALIZED_TEST(CustomSnapshotDataBlobOutdatedContextWithOverflow) { DisableAlwaysOpt(); const char* source1 = "var o = {};" "(function() {" " function f1(x) { return f2(x) instanceof Array; }" " function f2(x) { return foo.bar(x); }" " o.a = f2.bind(null);" " o.b = 1;" " o.c = 2;" " o.d = 3;" " o.e = 4;" "})();\n"; const char* source2 = "o.a(42)"; DisableEmbeddedBlobRefcounting(); v8::StartupData data = CreateSnapshotDataBlob(source1); v8::Isolate::CreateParams params; params.snapshot_blob = &data; params.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate = TestSerializer::NewIsolate(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local global = v8::ObjectTemplate::New(isolate); v8::Local property = v8::ObjectTemplate::New(isolate); v8::Local function = v8::FunctionTemplate::New(isolate, SerializationFunctionTemplate); property->Set(isolate, "bar", function); global->Set(isolate, "foo", property); v8::Local context = v8::Context::New(isolate, nullptr, global); v8::Context::Scope c_scope(context); v8::Local result = CompileRun(source2); v8::Maybe compare = v8_str("42")->Equals(isolate->GetCurrentContext(), result); CHECK(compare.FromJust()); } isolate->Dispose(); delete[] data.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlobWithLocker) { DisableAlwaysOpt(); DisableEmbeddedBlobRefcounting(); v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate0 = v8::Isolate::New(create_params); { v8::Locker locker(isolate0); v8::Isolate::Scope i_scope(isolate0); v8::HandleScope h_scope(isolate0); v8::Local context = v8::Context::New(isolate0); v8::Context::Scope c_scope(context); v8::Maybe result = CompileRun("Math.cos(0)")->Int32Value(isolate0->GetCurrentContext()); CHECK_EQ(1, result.FromJust()); } isolate0->Dispose(); const char* source1 = "function f() { return 42; }"; DisableEmbeddedBlobRefcounting(); v8::StartupData data1 = CreateSnapshotDataBlob(source1); v8::Isolate::CreateParams params1; params1.snapshot_blob = &data1; params1.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate1 = TestSerializer::NewIsolate(params1); { v8::Locker locker(isolate1); v8::Isolate::Scope i_scope(isolate1); v8::HandleScope h_scope(isolate1); v8::Local context = v8::Context::New(isolate1); v8::Context::Scope c_scope(context); v8::Maybe result = CompileRun("f()")->Int32Value(context); CHECK_EQ(42, result.FromJust()); } isolate1->Dispose(); delete[] data1.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlobStackOverflow) { DisableAlwaysOpt(); const char* source = "var a = [0];" "var b = a;" "for (var i = 0; i < 10000; i++) {" " var c = [i];" " b.push(c);" " b.push(c);" " b = c;" "}"; DisableEmbeddedBlobRefcounting(); v8::StartupData data = CreateSnapshotDataBlob(source); v8::Isolate::CreateParams params; params.snapshot_blob = &data; params.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate = TestSerializer::NewIsolate(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope c_scope(context); const char* test = "var sum = 0;" "while (a) {" " sum += a[0];" " a = a[1];" "}" "sum"; v8::Maybe result = CompileRun(test)->Int32Value(isolate->GetCurrentContext()); CHECK_EQ(9999 * 5000, result.FromJust()); } isolate->Dispose(); delete[] data.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } bool IsCompiled(const char* name) { return i::Cast(v8::Utils::OpenHandle(*CompileRun(name))) ->shared() ->is_compiled(); } UNINITIALIZED_TEST(SnapshotDataBlobWithWarmup) { DisableAlwaysOpt(); const char* warmup = "Math.abs(1); Math.random = 1;"; DisableEmbeddedBlobRefcounting(); v8::StartupData cold = CreateSnapshotDataBlob(nullptr); v8::StartupData warm = WarmUpSnapshotDataBlobInternal(cold, warmup); delete[] cold.data; v8::Isolate::CreateParams params; params.snapshot_blob = &warm; params.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate = TestSerializer::NewIsolate(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope c_scope(context); // Running the warmup script has effect on whether functions are // pre-compiled, but does not pollute the context. CHECK(IsCompiled("Math.abs")); CHECK(IsCompiled("String.raw")); CHECK(CompileRun("Math.random")->IsFunction()); } isolate->Dispose(); delete[] warm.data; FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlobWithWarmup) { DisableAlwaysOpt(); const char* source = "function f() { return Math.abs(1); }\n" "function g() { return String.raw(1); }\n" "Object.valueOf(1);" "var a = 5"; const char* warmup = "a = f()"; DisableEmbeddedBlobRefcounting(); v8::StartupData cold = CreateSnapshotDataBlob(source); v8::StartupData warm = WarmUpSnapshotDataBlobInternal(cold, warmup); delete[] cold.data; v8::Isolate::CreateParams params; params.snapshot_blob = &warm; params.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate = TestSerializer::NewIsolate(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope c_scope(context); // Running the warmup script has effect on whether functions are // pre-compiled, but does not pollute the context. CHECK(IsCompiled("f")); CHECK(IsCompiled("Math.abs")); CHECK(!IsCompiled("g")); CHECK(IsCompiled("String.raw")); CHECK(IsCompiled("Array.prototype.lastIndexOf")); CHECK_EQ(5, CompileRun("a")->Int32Value(context).FromJust()); } isolate->Dispose(); delete[] warm.data; FreeCurrentEmbeddedBlob(); } namespace { v8::StartupData CreateCustomSnapshotWithKeep() { SnapshotCreatorParams testing_params; v8::SnapshotCreator creator(testing_params.create_params); v8::Isolate* isolate = creator.GetIsolate(); { v8::HandleScope handle_scope(isolate); { v8::Local context = v8::Context::New(isolate); v8::Context::Scope context_scope(context); v8::Local source_str = v8_str( "function f() { return Math.abs(1); }\n" "function g() { return String.raw(1); }"); v8::ScriptOrigin origin(v8_str("test")); v8::ScriptCompiler::Source source(source_str, origin); CompileRun(isolate->GetCurrentContext(), &source, v8::ScriptCompiler::kEagerCompile); creator.SetDefaultContext(context); } } return creator.CreateBlob(v8::SnapshotCreator::FunctionCodeHandling::kKeep); } } // namespace UNINITIALIZED_TEST(CustomSnapshotDataBlobWithKeep) { DisableAlwaysOpt(); DisableEmbeddedBlobRefcounting(); v8::StartupData blob = CreateCustomSnapshotWithKeep(); { v8::Isolate::CreateParams params; params.snapshot_blob = &blob; params.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate = TestSerializer::NewIsolate(params); { v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope context_scope(context); CHECK(IsCompiled("f")); CHECK(IsCompiled("g")); } isolate->Dispose(); } delete[] blob.data; FreeCurrentEmbeddedBlob(); } UNINITIALIZED_TEST(CustomSnapshotDataBlobImmortalImmovableRoots) { DisableAlwaysOpt(); // Flood the startup snapshot with shared function infos. If they are // serialized before the immortal immovable root, the root will no longer end // up on the first page. base::Vector source = ConstructSource(base::StaticCharVector("var a = [];"), base::StaticCharVector("a.push(function() {return 7});"), base::StaticCharVector("\0"), 10000); DisableEmbeddedBlobRefcounting(); v8::StartupData data = CreateSnapshotDataBlob(source.begin()); v8::Isolate::CreateParams params; params.snapshot_blob = &data; params.array_buffer_allocator = CcTest::array_buffer_allocator(); // Test-appropriate equivalent of v8::Isolate::New. v8::Isolate* isolate = TestSerializer::NewIsolate(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New(isolate); v8::Context::Scope c_scope(context); CHECK_EQ(7, CompileRun("a[0]()")->Int32Value(context).FromJust()); } isolate->Dispose(); source.Dispose(); delete[] data.data; // We can dispose of the snapshot blob now. FreeCurrentEmbeddedBlob(); } TEST(TestThatAlwaysSucceeds) {} TEST(TestCheckThatAlwaysFails) { bool ArtificialFailure = false; CHECK(ArtificialFailure); } TEST(TestFatal) { GRACEFUL_FATAL("fatal"); } int CountBuiltins() { // Check that we have not deserialized any additional builtin. HeapObjectIterator iterator(CcTest::heap()); DisallowGarbageCollection no_gc; int counter = 0; for (Tagged obj = iterator.Next(); !obj.is_null(); obj = iterator.Next()) { if (IsCode(obj) && Cast(obj)->kind() == CodeKind::BUILTIN) counter++; } return counter; } static DirectHandle CompileScript( Isolate* isolate, Handle source, const ScriptDetails& script_details, AlignedCachedData* cached_data, v8::ScriptCompiler::CompileOptions options, ScriptCompiler::InMemoryCacheResult expected_lookup_result = ScriptCompiler::InMemoryCacheResult::kMiss) { ScriptCompiler::CompilationDetails compilation_details; auto result = Compiler::GetSharedFunctionInfoForScriptWithCachedData( isolate, source, script_details, cached_data, options, ScriptCompiler::kNoCacheNoReason, NOT_NATIVES_CODE, &compilation_details) .ToHandleChecked(); CHECK_EQ(compilation_details.in_memory_cache_result, expected_lookup_result); return result; } static DirectHandle CompileScriptAndProduceCache( Isolate* isolate, Handle source, const ScriptDetails& script_details, AlignedCachedData** out_cached_data, v8::ScriptCompiler::CompileOptions options, ScriptCompiler::InMemoryCacheResult expected_lookup_result = ScriptCompiler::InMemoryCacheResult::kMiss) { ScriptCompiler::CompilationDetails compilation_details; DirectHandle sfi = Compiler::GetSharedFunctionInfoForScript( isolate, source, script_details, options, ScriptCompiler::kNoCacheNoReason, NOT_NATIVES_CODE, &compilation_details) .ToHandleChecked(); CHECK_EQ(compilation_details.in_memory_cache_result, expected_lookup_result); std::unique_ptr cached_data( ScriptCompiler::CreateCodeCache(ToApiHandle(sfi))); uint8_t* buffer = NewArray(cached_data->length); MemCopy(buffer, cached_data->data, cached_data->length); *out_cached_data = new i::AlignedCachedData(buffer, cached_data->length); (*out_cached_data)->AcquireDataOwnership(); return sfi; } TEST(CodeSerializerWithProfiler) { v8_flags.enable_lazy_source_positions = true; v8_flags.stress_lazy_source_positions = false; LocalContext context; Isolate* isolate = CcTest::i_isolate(); isolate->compilation_cache() ->DisableScriptAndEval(); // Disable same-isolate code cache. v8::HandleScope scope(CcTest::isolate()); const char* source = "1 + 1"; Handle orig_source = isolate->factory() ->NewStringFromUtf8(base::CStrVector(source)) .ToHandleChecked(); Handle copy_source = isolate->factory() ->NewStringFromUtf8(base::CStrVector(source)) .ToHandleChecked(); CHECK(!orig_source.is_identical_to(copy_source)); CHECK(orig_source->Equals(*copy_source)); AlignedCachedData* cache = nullptr; ScriptDetails default_script_details; DirectHandle orig = CompileScriptAndProduceCache( isolate, orig_source, default_script_details, &cache, v8::ScriptCompiler::kNoCompileOptions); CHECK(!orig->GetBytecodeArray(isolate)->HasSourcePositionTable()); isolate->SetIsProfiling(true); // This does not assert that no compilation can happen as source position // collection could trigger it. DirectHandle copy = CompileScript(isolate, copy_source, default_script_details, cache, v8::ScriptCompiler::kConsumeCodeCache); // Since the profiler is now enabled, source positions should be collected // after deserialization. CHECK(copy->GetBytecodeArray(isolate)->HasSourcePositionTable()); delete cache; } void TestCodeSerializerOnePlusOneImpl(bool verify_builtins_count = true) { LocalContext context; Isolate* isolate = CcTest::i_isolate(); isolate->compilation_cache() ->DisableScriptAndEval(); // Disable same-isolate code cache. v8::HandleScope scope(CcTest::isolate()); const char* source = "1 + 1"; Handle orig_source = isolate->factory() ->NewStringFromUtf8(base::CStrVector(source)) .ToHandleChecked(); Handle copy_source = isolate->factory() ->NewStringFromUtf8(base::CStrVector(source)) .ToHandleChecked(); CHECK(!orig_source.is_identical_to(copy_source)); CHECK(orig_source->Equals(*copy_source)); AlignedCachedData* cache = nullptr; ScriptDetails default_script_details; DirectHandle orig = CompileScriptAndProduceCache( isolate, orig_source, default_script_details, &cache, v8::ScriptCompiler::kNoCompileOptions); int builtins_count = CountBuiltins(); DirectHandle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, copy_source, default_script_details, cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); CHECK(Cast(Cast