// 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 "src/v8.h" #include "src/api.h" #include "src/assembler-inl.h" #include "src/bootstrapper.h" #include "src/compilation-cache.h" #include "src/compiler.h" #include "src/debug/debug.h" #include "src/heap/spaces.h" #include "src/macro-assembler.h" #include "src/objects-inl.h" #include "src/runtime/runtime.h" #include "src/snapshot/code-serializer.h" #include "src/snapshot/deserializer.h" #include "src/snapshot/natives.h" #include "src/snapshot/partial-serializer.h" #include "src/snapshot/snapshot.h" #include "src/snapshot/startup-serializer.h" #include "test/cctest/cctest.h" #include "test/cctest/heap/heap-utils.h" using namespace v8::internal; void DisableAlwaysOpt() { // Isolates prepared for serialization do not optimize. The only exception is // with the flag --always-opt. FLAG_always_opt = false; } // TestIsolate is used for testing isolate serialization. class TestIsolate : public Isolate { public: static v8::Isolate* NewInitialized(bool enable_serializer) { i::Isolate* isolate = new TestIsolate(enable_serializer); v8::Isolate* v8_isolate = reinterpret_cast(isolate); v8::Isolate::Scope isolate_scope(v8_isolate); isolate->Init(NULL); return v8_isolate; } explicit TestIsolate(bool enable_serializer) : Isolate(enable_serializer) { set_array_buffer_allocator(CcTest::array_buffer_allocator()); } }; static Vector WritePayload(const Vector& payload) { int length = payload.length(); byte* blob = NewArray(length); memcpy(blob, payload.begin(), length); return Vector(const_cast(blob), length); } static Vector Serialize(v8::Isolate* isolate) { // We have to create one context. One reason for this is so that the builtins // can be loaded from v8natives.js 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* internal_isolate = reinterpret_cast(isolate); internal_isolate->heap()->CollectAllAvailableGarbage( i::GarbageCollectionReason::kTesting); StartupSerializer ser(internal_isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); ser.SerializeStrongReferences(); ser.SerializeWeakReferencesAndDeferred(); SnapshotData snapshot_data(&ser); return WritePayload(snapshot_data.RawData()); } Vector ConstructSource(Vector head, Vector body, Vector tail, int repeats) { int source_length = head.length() + body.length() * repeats + tail.length(); uint8_t* source = NewArray(static_cast(source_length)); CopyChars(source, head.start(), head.length()); for (int i = 0; i < repeats; i++) { CopyChars(source + head.length() + i * body.length(), body.start(), body.length()); } CopyChars(source + head.length() + repeats * body.length(), tail.start(), tail.length()); return Vector(const_cast(source), source_length); } v8::Isolate* InitializeFromBlob(Vector blob) { v8::Isolate* v8_isolate = NULL; { SnapshotData snapshot_data(blob); Deserializer deserializer(&snapshot_data); Isolate* isolate = new TestIsolate(false); v8_isolate = reinterpret_cast(isolate); v8::Isolate::Scope isolate_scope(v8_isolate); isolate->Init(&deserializer); } return v8_isolate; } static v8::Isolate* Deserialize(Vector blob) { v8::Isolate* isolate = InitializeFromBlob(blob); CHECK(isolate); return isolate; } static void SanityCheck(v8::Isolate* v8_isolate) { Isolate* isolate = reinterpret_cast(v8_isolate); v8::HandleScope scope(v8_isolate); #ifdef VERIFY_HEAP isolate->heap()->Verify(); #endif CHECK(isolate->global_object()->IsJSObject()); CHECK(isolate->native_context()->IsContext()); CHECK(isolate->heap()->string_table()->IsStringTable()); isolate->factory()->InternalizeOneByteString(STATIC_CHAR_VECTOR("Empty")); } UNINITIALIZED_TEST(StartupSerializerOnce) { DisableAlwaysOpt(); v8::Isolate* isolate = TestIsolate::NewInitialized(true); Vector blob = Serialize(isolate); isolate = Deserialize(blob); blob.Dispose(); { v8::HandleScope handle_scope(isolate); v8::Isolate::Scope isolate_scope(isolate); v8::Local env = v8::Context::New(isolate); env->Enter(); SanityCheck(isolate); } isolate->Dispose(); } UNINITIALIZED_TEST(StartupSerializerTwice) { DisableAlwaysOpt(); v8::Isolate* isolate = TestIsolate::NewInitialized(true); Vector blob1 = Serialize(isolate); Vector blob2 = Serialize(isolate); blob1.Dispose(); isolate = Deserialize(blob2); blob2.Dispose(); { v8::Isolate::Scope isolate_scope(isolate); v8::HandleScope handle_scope(isolate); v8::Local env = v8::Context::New(isolate); env->Enter(); SanityCheck(isolate); } isolate->Dispose(); } UNINITIALIZED_TEST(StartupSerializerOnceRunScript) { DisableAlwaysOpt(); v8::Isolate* isolate = TestIsolate::NewInitialized(true); Vector blob = Serialize(isolate); isolate = Deserialize(blob); blob.Dispose(); { 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(); } UNINITIALIZED_TEST(StartupSerializerTwiceRunScript) { DisableAlwaysOpt(); v8::Isolate* isolate = TestIsolate::NewInitialized(true); Vector blob1 = Serialize(isolate); Vector blob2 = Serialize(isolate); blob1.Dispose(); isolate = Deserialize(blob2); blob2.Dispose(); { 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(); } static void PartiallySerializeObject(Vector* startup_blob_out, Vector* partial_blob_out) { v8::Isolate* v8_isolate = TestIsolate::NewInitialized(true); Isolate* isolate = reinterpret_cast(v8_isolate); v8_isolate->Enter(); { Heap* heap = isolate->heap(); 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(); } // Make sure all builtin scripts are cached. { HandleScope scope(isolate); for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { isolate->bootstrapper()->SourceLookup(i); } } heap->CollectAllAvailableGarbage(i::GarbageCollectionReason::kTesting); heap->CollectAllAvailableGarbage(i::GarbageCollectionReason::kTesting); Object* raw_foo; { v8::HandleScope handle_scope(v8_isolate); v8::Local foo = v8_str("foo"); CHECK(!foo.IsEmpty()); raw_foo = *(v8::Utils::OpenHandle(*foo)); } { v8::HandleScope handle_scope(v8_isolate); v8::Local::New(v8_isolate, env)->Exit(); } env.Reset(); StartupSerializer startup_serializer( isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); startup_serializer.SerializeStrongReferences(); PartialSerializer partial_serializer(isolate, &startup_serializer, v8::SerializeInternalFieldsCallback()); partial_serializer.Serialize(&raw_foo, false); startup_serializer.SerializeWeakReferencesAndDeferred(); SnapshotData startup_snapshot(&startup_serializer); SnapshotData partial_snapshot(&partial_serializer); *partial_blob_out = WritePayload(partial_snapshot.RawData()); *startup_blob_out = WritePayload(startup_snapshot.RawData()); } v8_isolate->Exit(); v8_isolate->Dispose(); } UNINITIALIZED_TEST(PartialSerializerObject) { DisableAlwaysOpt(); Vector startup_blob; Vector partial_blob; PartiallySerializeObject(&startup_blob, &partial_blob); v8::Isolate* v8_isolate = InitializeFromBlob(startup_blob); startup_blob.Dispose(); CHECK(v8_isolate); { v8::Isolate::Scope isolate_scope(v8_isolate); Isolate* isolate = reinterpret_cast(v8_isolate); HandleScope handle_scope(isolate); Handle root; // Intentionally empty handle. The deserializer should not come across // any references to the global proxy in this test. Handle global_proxy = Handle::null(); { SnapshotData snapshot_data(partial_blob); Deserializer deserializer(&snapshot_data); root = deserializer .DeserializePartial(isolate, global_proxy, v8::DeserializeInternalFieldsCallback()) .ToHandleChecked(); CHECK(root->IsString()); } Handle root2; { SnapshotData snapshot_data(partial_blob); Deserializer deserializer(&snapshot_data); root2 = deserializer .DeserializePartial(isolate, global_proxy, v8::DeserializeInternalFieldsCallback()) .ToHandleChecked(); CHECK(root2->IsString()); CHECK(root.is_identical_to(root2)); } partial_blob.Dispose(); } v8_isolate->Dispose(); } static void PartiallySerializeContext(Vector* startup_blob_out, Vector* partial_blob_out) { v8::Isolate* v8_isolate = TestIsolate::NewInitialized(true); 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(); } // Make sure all builtin scripts are cached. { HandleScope scope(isolate); for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { isolate->bootstrapper()->SourceLookup(i); } } // 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. heap->CollectAllAvailableGarbage(i::GarbageCollectionReason::kTesting); { v8::HandleScope handle_scope(v8_isolate); v8::Local::New(v8_isolate, env)->Exit(); } i::Object* raw_context = *v8::Utils::OpenPersistent(env); env.Reset(); SnapshotByteSink startup_sink; StartupSerializer startup_serializer( isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); startup_serializer.SerializeStrongReferences(); SnapshotByteSink partial_sink; PartialSerializer partial_serializer(isolate, &startup_serializer, v8::SerializeInternalFieldsCallback()); partial_serializer.Serialize(&raw_context, false); startup_serializer.SerializeWeakReferencesAndDeferred(); SnapshotData startup_snapshot(&startup_serializer); SnapshotData partial_snapshot(&partial_serializer); *partial_blob_out = WritePayload(partial_snapshot.RawData()); *startup_blob_out = WritePayload(startup_snapshot.RawData()); } v8_isolate->Dispose(); } UNINITIALIZED_TEST(PartialSerializerContext) { DisableAlwaysOpt(); Vector startup_blob; Vector partial_blob; PartiallySerializeContext(&startup_blob, &partial_blob); v8::Isolate* v8_isolate = InitializeFromBlob(startup_blob); CHECK(v8_isolate); startup_blob.Dispose(); { v8::Isolate::Scope isolate_scope(v8_isolate); Isolate* isolate = reinterpret_cast(v8_isolate); HandleScope handle_scope(isolate); Handle root; Handle global_proxy = isolate->factory()->NewUninitializedJSGlobalProxy( JSGlobalProxy::SizeWithInternalFields(0)); { SnapshotData snapshot_data(partial_blob); Deserializer deserializer(&snapshot_data); root = deserializer .DeserializePartial(isolate, global_proxy, v8::DeserializeInternalFieldsCallback()) .ToHandleChecked(); CHECK(root->IsContext()); CHECK(Handle::cast(root)->global_proxy() == *global_proxy); } Handle root2; { SnapshotData snapshot_data(partial_blob); Deserializer deserializer(&snapshot_data); root2 = deserializer .DeserializePartial(isolate, global_proxy, v8::DeserializeInternalFieldsCallback()) .ToHandleChecked(); CHECK(root2->IsContext()); CHECK(!root.is_identical_to(root2)); } partial_blob.Dispose(); } v8_isolate->Dispose(); } static void PartiallySerializeCustomContext( Vector* startup_blob_out, Vector* partial_blob_out) { v8::Isolate* v8_isolate = TestIsolate::NewInitialized(true); Isolate* isolate = reinterpret_cast(v8_isolate); { 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(); // 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');"); Vector source = ConstructSource( STATIC_CHAR_VECTOR("function g() { return [,"), STATIC_CHAR_VECTOR("1,"), STATIC_CHAR_VECTOR("];} a = g(); b = g(); b.push(1);"), 100000); v8::MaybeLocal source_str = v8::String::NewFromOneByte( v8_isolate, source.start(), v8::NewStringType::kNormal, source.length()); CompileRun(source_str.ToLocalChecked()); source.Dispose(); } // Make sure all builtin scripts are cached. { HandleScope scope(isolate); for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { isolate->bootstrapper()->SourceLookup(i); } } // 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. isolate->heap()->CollectAllAvailableGarbage( i::GarbageCollectionReason::kTesting); { v8::HandleScope handle_scope(v8_isolate); v8::Local::New(v8_isolate, env)->Exit(); } i::Object* raw_context = *v8::Utils::OpenPersistent(env); env.Reset(); SnapshotByteSink startup_sink; StartupSerializer startup_serializer( isolate, v8::SnapshotCreator::FunctionCodeHandling::kClear); startup_serializer.SerializeStrongReferences(); SnapshotByteSink partial_sink; PartialSerializer partial_serializer(isolate, &startup_serializer, v8::SerializeInternalFieldsCallback()); partial_serializer.Serialize(&raw_context, false); startup_serializer.SerializeWeakReferencesAndDeferred(); SnapshotData startup_snapshot(&startup_serializer); SnapshotData partial_snapshot(&partial_serializer); *partial_blob_out = WritePayload(partial_snapshot.RawData()); *startup_blob_out = WritePayload(startup_snapshot.RawData()); } v8_isolate->Dispose(); } UNINITIALIZED_TEST(PartialSerializerCustomContext) { DisableAlwaysOpt(); Vector startup_blob; Vector partial_blob; PartiallySerializeCustomContext(&startup_blob, &partial_blob); v8::Isolate* v8_isolate = InitializeFromBlob(startup_blob); CHECK(v8_isolate); startup_blob.Dispose(); { v8::Isolate::Scope isolate_scope(v8_isolate); Isolate* isolate = reinterpret_cast(v8_isolate); HandleScope handle_scope(isolate); Handle root; Handle global_proxy = isolate->factory()->NewUninitializedJSGlobalProxy( JSGlobalProxy::SizeWithInternalFields(0)); { SnapshotData snapshot_data(partial_blob); Deserializer deserializer(&snapshot_data); root = deserializer .DeserializePartial(isolate, global_proxy, v8::DeserializeInternalFieldsCallback()) .ToHandleChecked(); CHECK(root->IsContext()); Handle context = Handle::cast(root); // Add context to the weak native context list context->set(Context::NEXT_CONTEXT_LINK, isolate->heap()->native_contexts_list(), UPDATE_WEAK_WRITE_BARRIER); isolate->heap()->set_native_contexts_list(*context); CHECK(context->global_proxy() == *global_proxy); Handle o = isolate->factory()->NewStringFromAsciiChecked("o"); Handle global_object(context->global_object(), isolate); Handle property = JSReceiver::GetDataProperty(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()); 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); } partial_blob.Dispose(); } v8_isolate->Dispose(); } TEST(CustomSnapshotDataBlob1) { DisableAlwaysOpt(); const char* source1 = "function f() { return 42; }"; v8::StartupData data1 = v8::V8::CreateSnapshotDataBlob(source1); v8::Isolate::CreateParams params1; params1.snapshot_blob = &data1; params1.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate1 = v8::Isolate::New(params1); { v8::Isolate::Scope i_scope(isolate1); v8::HandleScope h_scope(isolate1); v8::Local context = v8::Context::New(isolate1); delete[] data1.data; // We can dispose of the snapshot blob now. 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(); } TEST(CustomSnapshotDataBlob2) { DisableAlwaysOpt(); const char* source2 = "function f() { return g() * 2; }" "function g() { return 43; }" "/./.test('a')"; v8::StartupData data2 = v8::V8::CreateSnapshotDataBlob(source2); v8::Isolate::CreateParams params2; params2.snapshot_blob = &data2; params2.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate2 = v8::Isolate::New(params2); { v8::Isolate::Scope i_scope(isolate2); v8::HandleScope h_scope(isolate2); v8::Local context = v8::Context::New(isolate2); delete[] data2.data; // We can dispose of the snapshot blob now. 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(); } static void SerializationFunctionTemplate( const v8::FunctionCallbackInfo& args) { args.GetReturnValue().Set(args[0]); } 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)"; v8::StartupData data = v8::V8::CreateSnapshotDataBlob(source1); v8::Isolate::CreateParams params; params.snapshot_blob = &data; params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate = v8::Isolate::New(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, NULL, global); delete[] data.data; // We can dispose of the snapshot blob now. v8::Context::Scope c_scope(context); v8::Local result = CompileRun(source2); v8::Maybe compare = v8_str("42")->Equals( v8::Isolate::GetCurrent()->GetCurrentContext(), result); CHECK(compare.FromJust()); } isolate->Dispose(); } TEST(CustomSnapshotDataBlobWithLocker) { DisableAlwaysOpt(); 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; }"; v8::StartupData data1 = v8::V8::CreateSnapshotDataBlob(source1); v8::Isolate::CreateParams params1; params1.snapshot_blob = &data1; params1.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate1 = v8::Isolate::New(params1); { v8::Locker locker(isolate1); v8::Isolate::Scope i_scope(isolate1); v8::HandleScope h_scope(isolate1); v8::Local context = v8::Context::New(isolate1); delete[] data1.data; // We can dispose of the snapshot blob now. v8::Context::Scope c_scope(context); v8::Maybe result = CompileRun("f()")->Int32Value(context); CHECK_EQ(42, result.FromJust()); } isolate1->Dispose(); } 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;" "}"; v8::StartupData data = v8::V8::CreateSnapshotDataBlob(source); v8::Isolate::CreateParams params; params.snapshot_blob = &data; params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate = v8::Isolate::New(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New(isolate); delete[] data.data; // We can dispose of the snapshot blob now. 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(); } bool IsCompiled(const char* name) { return i::Handle::cast( v8::Utils::OpenHandle(*CompileRun(name))) ->shared() ->is_compiled(); } TEST(SnapshotDataBlobWithWarmup) { DisableAlwaysOpt(); const char* warmup = "Math.abs(1); Math.random = 1;"; v8::StartupData cold = v8::V8::CreateSnapshotDataBlob(); v8::StartupData warm = v8::V8::WarmUpSnapshotDataBlob(cold, warmup); delete[] cold.data; v8::Isolate::CreateParams params; params.snapshot_blob = &warm; params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate = v8::Isolate::New(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New(isolate); delete[] warm.data; 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(); } 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()"; v8::StartupData cold = v8::V8::CreateSnapshotDataBlob(source); v8::StartupData warm = v8::V8::WarmUpSnapshotDataBlob(cold, warmup); delete[] cold.data; v8::Isolate::CreateParams params; params.snapshot_blob = &warm; params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate = v8::Isolate::New(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New(isolate); delete[] warm.data; 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("Object.valueOf")); CHECK_EQ(5, CompileRun("a")->Int32Value(context).FromJust()); } isolate->Dispose(); } 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. Vector source = ConstructSource(STATIC_CHAR_VECTOR("var a = [];"), STATIC_CHAR_VECTOR("a.push(function() {return 7});"), STATIC_CHAR_VECTOR("\0"), 10000); v8::StartupData data = v8::V8::CreateSnapshotDataBlob( reinterpret_cast(source.start())); v8::Isolate::CreateParams params; params.snapshot_blob = &data; params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate = v8::Isolate::New(params); { v8::Isolate::Scope i_scope(isolate); v8::HandleScope h_scope(isolate); v8::Local context = v8::Context::New(isolate); delete[] data.data; // We can dispose of the snapshot blob now. v8::Context::Scope c_scope(context); CHECK_EQ(7, CompileRun("a[0]()")->Int32Value(context).FromJust()); } isolate->Dispose(); source.Dispose(); } TEST(TestThatAlwaysSucceeds) { } TEST(TestThatAlwaysFails) { bool ArtificialFailure = false; CHECK(ArtificialFailure); } int CountBuiltins() { // Check that we have not deserialized any additional builtin. HeapIterator iterator(CcTest::heap()); DisallowHeapAllocation no_allocation; int counter = 0; for (HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) { if (obj->IsCode() && Code::cast(obj)->kind() == Code::BUILTIN) counter++; } return counter; } static Handle CompileScript( Isolate* isolate, Handle source, Handle name, ScriptData** cached_data, v8::ScriptCompiler::CompileOptions options) { return Compiler::GetSharedFunctionInfoForScript( source, name, 0, 0, v8::ScriptOriginOptions(), Handle(), Handle(isolate->native_context()), NULL, cached_data, options, NOT_NATIVES_CODE); } TEST(CodeSerializerOnePlusOne) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. v8::HandleScope scope(CcTest::isolate()); const char* source = "1 + 1"; Handle orig_source = isolate->factory() ->NewStringFromUtf8(CStrVector(source)) .ToHandleChecked(); Handle copy_source = isolate->factory() ->NewStringFromUtf8(CStrVector(source)) .ToHandleChecked(); CHECK(!orig_source.is_identical_to(copy_source)); CHECK(orig_source->Equals(*copy_source)); ScriptData* cache = NULL; Handle orig = CompileScript(isolate, orig_source, Handle(), &cache, v8::ScriptCompiler::kProduceCodeCache); int builtins_count = CountBuiltins(); Handle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, copy_source, Handle(), &cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); CHECK(Script::cast(copy->script())->source() == *copy_source); Handle copy_fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( copy, isolate->native_context()); Handle global(isolate->context()->global_object()); Handle copy_result = Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); CHECK_EQ(2, Handle::cast(copy_result)->value()); CHECK_EQ(builtins_count, CountBuiltins()); delete cache; } TEST(CodeSerializerPromotedToCompilationCache) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); v8::HandleScope scope(CcTest::isolate()); const char* source = "1 + 1"; Handle src = isolate->factory() ->NewStringFromUtf8(CStrVector(source)) .ToHandleChecked(); ScriptData* cache = NULL; CompileScript(isolate, src, src, &cache, v8::ScriptCompiler::kProduceCodeCache); DisallowCompilation no_compile_expected(isolate); Handle copy = CompileScript( isolate, src, src, &cache, v8::ScriptCompiler::kConsumeCodeCache); InfoVectorPair pair = isolate->compilation_cache()->LookupScript( src, src, 0, 0, v8::ScriptOriginOptions(), isolate->native_context(), SLOPPY); CHECK(pair.shared() == *copy); delete cache; } TEST(CodeSerializerInternalizedString) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. v8::HandleScope scope(CcTest::isolate()); const char* source = "'string1'"; Handle orig_source = isolate->factory() ->NewStringFromUtf8(CStrVector(source)) .ToHandleChecked(); Handle copy_source = isolate->factory() ->NewStringFromUtf8(CStrVector(source)) .ToHandleChecked(); CHECK(!orig_source.is_identical_to(copy_source)); CHECK(orig_source->Equals(*copy_source)); Handle global(isolate->context()->global_object()); ScriptData* cache = NULL; Handle orig = CompileScript(isolate, orig_source, Handle(), &cache, v8::ScriptCompiler::kProduceCodeCache); Handle orig_fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( orig, isolate->native_context()); Handle orig_result = Execution::Call(isolate, orig_fun, global, 0, NULL).ToHandleChecked(); CHECK(orig_result->IsInternalizedString()); int builtins_count = CountBuiltins(); Handle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, copy_source, Handle(), &cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); CHECK(Script::cast(copy->script())->source() == *copy_source); Handle copy_fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( copy, isolate->native_context()); CHECK_NE(*orig_fun, *copy_fun); Handle copy_result = Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); CHECK(orig_result.is_identical_to(copy_result)); Handle expected = isolate->factory()->NewStringFromAsciiChecked("string1"); CHECK(Handle::cast(copy_result)->Equals(*expected)); CHECK_EQ(builtins_count, CountBuiltins()); delete cache; } TEST(CodeSerializerLargeCodeObject) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. v8::HandleScope scope(CcTest::isolate()); // The serializer only tests the shared code, which is always the unoptimized // code. Don't even bother generating optimized code to avoid timeouts. FLAG_always_opt = false; Vector source = ConstructSource(STATIC_CHAR_VECTOR("var j=1; if (j == 0) {"), STATIC_CHAR_VECTOR("for (let i of Object.prototype);"), STATIC_CHAR_VECTOR("} j=7; j"), 1050); Handle source_str = isolate->factory()->NewStringFromOneByte(source).ToHandleChecked(); Handle global(isolate->context()->global_object()); ScriptData* cache = NULL; Handle orig = CompileScript(isolate, source_str, Handle(), &cache, v8::ScriptCompiler::kProduceCodeCache); CHECK(isolate->heap()->InSpace(orig->abstract_code(), LO_SPACE)); Handle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, source_str, Handle(), &cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); Handle copy_fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( copy, isolate->native_context()); Handle copy_result = Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); int result_int; CHECK(copy_result->ToInt32(&result_int)); CHECK_EQ(7, result_int); delete cache; source.Dispose(); } TEST(CodeSerializerLargeStrings) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); Factory* f = isolate->factory(); isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. v8::HandleScope scope(CcTest::isolate()); Vector source_s = ConstructSource( STATIC_CHAR_VECTOR("var s = \""), STATIC_CHAR_VECTOR("abcdef"), STATIC_CHAR_VECTOR("\";"), 1000000); Vector source_t = ConstructSource( STATIC_CHAR_VECTOR("var t = \""), STATIC_CHAR_VECTOR("uvwxyz"), STATIC_CHAR_VECTOR("\"; s + t"), 999999); Handle source_str = f->NewConsString(f->NewStringFromOneByte(source_s).ToHandleChecked(), f->NewStringFromOneByte(source_t).ToHandleChecked()) .ToHandleChecked(); Handle global(isolate->context()->global_object()); ScriptData* cache = NULL; Handle orig = CompileScript(isolate, source_str, Handle(), &cache, v8::ScriptCompiler::kProduceCodeCache); Handle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, source_str, Handle(), &cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); Handle copy_fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( copy, isolate->native_context()); Handle copy_result = Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); CHECK_EQ(6 * 1999999, Handle::cast(copy_result)->length()); Handle property = JSReceiver::GetDataProperty( isolate->global_object(), f->NewStringFromAsciiChecked("s")); CHECK(isolate->heap()->InSpace(HeapObject::cast(*property), LO_SPACE)); property = JSReceiver::GetDataProperty(isolate->global_object(), f->NewStringFromAsciiChecked("t")); CHECK(isolate->heap()->InSpace(HeapObject::cast(*property), LO_SPACE)); // Make sure we do not serialize too much, e.g. include the source string. CHECK_LT(cache->length(), 13000000); delete cache; source_s.Dispose(); source_t.Dispose(); } TEST(CodeSerializerThreeBigStrings) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); Factory* f = isolate->factory(); isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. v8::HandleScope scope(CcTest::isolate()); Vector source_a = ConstructSource(STATIC_CHAR_VECTOR("var a = \""), STATIC_CHAR_VECTOR("a"), STATIC_CHAR_VECTOR("\";"), 700000); Handle source_a_str = f->NewStringFromOneByte(source_a).ToHandleChecked(); Vector source_b = ConstructSource(STATIC_CHAR_VECTOR("var b = \""), STATIC_CHAR_VECTOR("b"), STATIC_CHAR_VECTOR("\";"), 400000); Handle source_b_str = f->NewStringFromOneByte(source_b).ToHandleChecked(); Vector source_c = ConstructSource(STATIC_CHAR_VECTOR("var c = \""), STATIC_CHAR_VECTOR("c"), STATIC_CHAR_VECTOR("\";"), 400000); Handle source_c_str = f->NewStringFromOneByte(source_c).ToHandleChecked(); Handle source_str = f->NewConsString( f->NewConsString(source_a_str, source_b_str).ToHandleChecked(), source_c_str).ToHandleChecked(); Handle global(isolate->context()->global_object()); ScriptData* cache = NULL; Handle orig = CompileScript(isolate, source_str, Handle(), &cache, v8::ScriptCompiler::kProduceCodeCache); Handle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, source_str, Handle(), &cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); Handle copy_fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( copy, isolate->native_context()); USE(Execution::Call(isolate, copy_fun, global, 0, NULL)); v8::Maybe result = CompileRun("(a + b).length") ->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext()); CHECK_EQ(400000 + 700000, result.FromJust()); result = CompileRun("(b + c).length") ->Int32Value(v8::Isolate::GetCurrent()->GetCurrentContext()); CHECK_EQ(400000 + 400000, result.FromJust()); Heap* heap = isolate->heap(); v8::Local result_str = CompileRun("a") ->ToString(CcTest::isolate()->GetCurrentContext()) .ToLocalChecked(); CHECK(heap->InSpace(*v8::Utils::OpenHandle(*result_str), LO_SPACE)); result_str = CompileRun("b") ->ToString(CcTest::isolate()->GetCurrentContext()) .ToLocalChecked(); CHECK(heap->InSpace(*v8::Utils::OpenHandle(*result_str), OLD_SPACE)); result_str = CompileRun("c") ->ToString(CcTest::isolate()->GetCurrentContext()) .ToLocalChecked(); CHECK(heap->InSpace(*v8::Utils::OpenHandle(*result_str), OLD_SPACE)); delete cache; source_a.Dispose(); source_b.Dispose(); source_c.Dispose(); } class SerializerOneByteResource : public v8::String::ExternalOneByteStringResource { public: SerializerOneByteResource(const char* data, size_t length) : data_(data), length_(length) {} virtual const char* data() const { return data_; } virtual size_t length() const { return length_; } private: const char* data_; size_t length_; }; class SerializerTwoByteResource : public v8::String::ExternalStringResource { public: SerializerTwoByteResource(const char* data, size_t length) : data_(AsciiToTwoByteString(data)), length_(length) {} ~SerializerTwoByteResource() { DeleteArray(data_); } virtual const uint16_t* data() const { return data_; } virtual size_t length() const { return length_; } private: const uint16_t* data_; size_t length_; }; TEST(CodeSerializerExternalString) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. v8::HandleScope scope(CcTest::isolate()); // Obtain external internalized one-byte string. SerializerOneByteResource one_byte_resource("one_byte", 8); Handle one_byte_string = isolate->factory()->NewStringFromAsciiChecked("one_byte"); one_byte_string = isolate->factory()->InternalizeString(one_byte_string); one_byte_string->MakeExternal(&one_byte_resource); CHECK(one_byte_string->IsExternalOneByteString()); CHECK(one_byte_string->IsInternalizedString()); // Obtain external internalized two-byte string. SerializerTwoByteResource two_byte_resource("two_byte", 8); Handle two_byte_string = isolate->factory()->NewStringFromAsciiChecked("two_byte"); two_byte_string = isolate->factory()->InternalizeString(two_byte_string); two_byte_string->MakeExternal(&two_byte_resource); CHECK(two_byte_string->IsExternalTwoByteString()); CHECK(two_byte_string->IsInternalizedString()); const char* source = "var o = {} \n" "o.one_byte = 7; \n" "o.two_byte = 8; \n" "o.one_byte + o.two_byte; \n"; Handle source_string = isolate->factory() ->NewStringFromUtf8(CStrVector(source)) .ToHandleChecked(); Handle global(isolate->context()->global_object()); ScriptData* cache = NULL; Handle orig = CompileScript(isolate, source_string, Handle(), &cache, v8::ScriptCompiler::kProduceCodeCache); Handle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, source_string, Handle(), &cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); Handle copy_fun = isolate->factory()->NewFunctionFromSharedFunctionInfo( copy, isolate->native_context()); Handle copy_result = Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); CHECK_EQ(15.0, copy_result->Number()); delete cache; } TEST(CodeSerializerLargeExternalString) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. Factory* f = isolate->factory(); v8::HandleScope scope(CcTest::isolate()); // Create a huge external internalized string to use as variable name. Vector string = ConstructSource(STATIC_CHAR_VECTOR(""), STATIC_CHAR_VECTOR("abcdef"), STATIC_CHAR_VECTOR(""), 999999); Handle name = f->NewStringFromOneByte(string).ToHandleChecked(); SerializerOneByteResource one_byte_resource( reinterpret_cast(string.start()), string.length()); name = f->InternalizeString(name); name->MakeExternal(&one_byte_resource); CHECK(name->IsExternalOneByteString()); CHECK(name->IsInternalizedString()); CHECK(isolate->heap()->InSpace(*name, LO_SPACE)); // Create the source, which is "var = 42; ". Handle source_str = f->NewConsString( f->NewConsString(f->NewStringFromAsciiChecked("var "), name) .ToHandleChecked(), f->NewConsString(f->NewStringFromAsciiChecked(" = 42; "), name) .ToHandleChecked()).ToHandleChecked(); Handle global(isolate->context()->global_object()); ScriptData* cache = NULL; Handle orig = CompileScript(isolate, source_str, Handle(), &cache, v8::ScriptCompiler::kProduceCodeCache); Handle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, source_str, Handle(), &cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); Handle copy_fun = f->NewFunctionFromSharedFunctionInfo(copy, isolate->native_context()); Handle copy_result = Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); CHECK_EQ(42.0, copy_result->Number()); delete cache; string.Dispose(); } TEST(CodeSerializerExternalScriptName) { FLAG_serialize_toplevel = true; LocalContext context; Isolate* isolate = CcTest::i_isolate(); isolate->compilation_cache()->Disable(); // Disable same-isolate code cache. Factory* f = isolate->factory(); v8::HandleScope scope(CcTest::isolate()); const char* source = "var a = [1, 2, 3, 4];" "a.reduce(function(x, y) { return x + y }, 0)"; Handle source_string = f->NewStringFromUtf8(CStrVector(source)).ToHandleChecked(); const SerializerOneByteResource one_byte_resource("one_byte", 8); Handle name = f->NewExternalStringFromOneByte(&one_byte_resource).ToHandleChecked(); CHECK(name->IsExternalOneByteString()); CHECK(!name->IsInternalizedString()); Handle global(isolate->context()->global_object()); ScriptData* cache = NULL; Handle orig = CompileScript(isolate, source_string, name, &cache, v8::ScriptCompiler::kProduceCodeCache); Handle copy; { DisallowCompilation no_compile_expected(isolate); copy = CompileScript(isolate, source_string, name, &cache, v8::ScriptCompiler::kConsumeCodeCache); } CHECK_NE(*orig, *copy); Handle copy_fun = f->NewFunctionFromSharedFunctionInfo(copy, isolate->native_context()); Handle copy_result = Execution::Call(isolate, copy_fun, global, 0, NULL).ToHandleChecked(); CHECK_EQ(10.0, copy_result->Number()); delete cache; } static bool toplevel_test_code_event_found = false; static void SerializerCodeEventListener(const v8::JitCodeEvent* event) { if (event->type == v8::JitCodeEvent::CODE_ADDED && memcmp(event->name.str, "Script:~test", 12) == 0) { toplevel_test_code_event_found = true; } } v8::ScriptCompiler::CachedData* ProduceCache(const char* source) { v8::ScriptCompiler::CachedData* cache; v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate1 = v8::Isolate::New(create_params); { v8::Isolate::Scope iscope(isolate1); v8::HandleScope scope(isolate1); v8::Local context = v8::Context::New(isolate1); v8::Context::Scope context_scope(context); v8::Local source_str = v8_str(source); v8::ScriptOrigin origin(v8_str("test")); v8::ScriptCompiler::Source source(source_str, origin); v8::Local script = v8::ScriptCompiler::CompileUnboundScript( isolate1, &source, v8::ScriptCompiler::kProduceCodeCache) .ToLocalChecked(); const v8::ScriptCompiler::CachedData* data = source.GetCachedData(); CHECK(data); // Persist cached data. uint8_t* buffer = NewArray(data->length); MemCopy(buffer, data->data, data->length); cache = new v8::ScriptCompiler::CachedData( buffer, data->length, v8::ScriptCompiler::CachedData::BufferOwned); v8::Local result = script->BindToCurrentContext() ->Run(isolate1->GetCurrentContext()) .ToLocalChecked(); v8::Local result_string = result->ToString(isolate1->GetCurrentContext()).ToLocalChecked(); CHECK(result_string->Equals(isolate1->GetCurrentContext(), v8_str("abcdef")) .FromJust()); } isolate1->Dispose(); return cache; } TEST(CodeSerializerIsolates) { FLAG_serialize_toplevel = true; const char* source = "function f() { return 'abc'; }; f() + 'def'"; v8::ScriptCompiler::CachedData* cache = ProduceCache(source); v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate2 = v8::Isolate::New(create_params); isolate2->SetJitCodeEventHandler(v8::kJitCodeEventDefault, SerializerCodeEventListener); toplevel_test_code_event_found = false; { v8::Isolate::Scope iscope(isolate2); v8::HandleScope scope(isolate2); v8::Local context = v8::Context::New(isolate2); v8::Context::Scope context_scope(context); v8::Local source_str = v8_str(source); v8::ScriptOrigin origin(v8_str("test")); v8::ScriptCompiler::Source source(source_str, origin, cache); v8::Local script; { DisallowCompilation no_compile(reinterpret_cast(isolate2)); script = v8::ScriptCompiler::CompileUnboundScript( isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache) .ToLocalChecked(); } CHECK(!cache->rejected); v8::Local result = script->BindToCurrentContext() ->Run(isolate2->GetCurrentContext()) .ToLocalChecked(); CHECK(result->ToString(isolate2->GetCurrentContext()) .ToLocalChecked() ->Equals(isolate2->GetCurrentContext(), v8_str("abcdef")) .FromJust()); } CHECK(toplevel_test_code_event_found); isolate2->Dispose(); } TEST(CodeSerializerFlagChange) { FLAG_serialize_toplevel = true; const char* source = "function f() { return 'abc'; }; f() + 'def'"; v8::ScriptCompiler::CachedData* cache = ProduceCache(source); v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate2 = v8::Isolate::New(create_params); FLAG_allow_natives_syntax = true; // Flag change should trigger cache reject. FlagList::EnforceFlagImplications(); { v8::Isolate::Scope iscope(isolate2); v8::HandleScope scope(isolate2); v8::Local context = v8::Context::New(isolate2); v8::Context::Scope context_scope(context); v8::Local source_str = v8_str(source); v8::ScriptOrigin origin(v8_str("test")); v8::ScriptCompiler::Source source(source_str, origin, cache); v8::ScriptCompiler::CompileUnboundScript( isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache) .ToLocalChecked(); CHECK(cache->rejected); } isolate2->Dispose(); } TEST(CodeSerializerBitFlip) { FLAG_serialize_toplevel = true; const char* source = "function f() { return 'abc'; }; f() + 'def'"; v8::ScriptCompiler::CachedData* cache = ProduceCache(source); // Random bit flip. const_cast(cache->data)[337] ^= 0x40; v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate2 = v8::Isolate::New(create_params); { v8::Isolate::Scope iscope(isolate2); v8::HandleScope scope(isolate2); v8::Local context = v8::Context::New(isolate2); v8::Context::Scope context_scope(context); v8::Local source_str = v8_str(source); v8::ScriptOrigin origin(v8_str("test")); v8::ScriptCompiler::Source source(source_str, origin, cache); v8::ScriptCompiler::CompileUnboundScript( isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache) .ToLocalChecked(); CHECK(cache->rejected); } isolate2->Dispose(); } TEST(CodeSerializerWithHarmonyScoping) { FLAG_serialize_toplevel = true; const char* source1 = "'use strict'; let x = 'X'"; const char* source2 = "'use strict'; let y = 'Y'"; const char* source3 = "'use strict'; x + y"; v8::ScriptCompiler::CachedData* cache; v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate1 = v8::Isolate::New(create_params); { v8::Isolate::Scope iscope(isolate1); v8::HandleScope scope(isolate1); v8::Local context = v8::Context::New(isolate1); v8::Context::Scope context_scope(context); CompileRun(source1); CompileRun(source2); v8::Local source_str = v8_str(source3); v8::ScriptOrigin origin(v8_str("test")); v8::ScriptCompiler::Source source(source_str, origin); v8::Local script = v8::ScriptCompiler::CompileUnboundScript( isolate1, &source, v8::ScriptCompiler::kProduceCodeCache) .ToLocalChecked(); const v8::ScriptCompiler::CachedData* data = source.GetCachedData(); CHECK(data); // Persist cached data. uint8_t* buffer = NewArray(data->length); MemCopy(buffer, data->data, data->length); cache = new v8::ScriptCompiler::CachedData( buffer, data->length, v8::ScriptCompiler::CachedData::BufferOwned); v8::Local result = script->BindToCurrentContext() ->Run(isolate1->GetCurrentContext()) .ToLocalChecked(); v8::Local result_str = result->ToString(isolate1->GetCurrentContext()).ToLocalChecked(); CHECK(result_str->Equals(isolate1->GetCurrentContext(), v8_str("XY")) .FromJust()); } isolate1->Dispose(); v8::Isolate* isolate2 = v8::Isolate::New(create_params); { v8::Isolate::Scope iscope(isolate2); v8::HandleScope scope(isolate2); v8::Local context = v8::Context::New(isolate2); v8::Context::Scope context_scope(context); // Reverse order of prior running scripts. CompileRun(source2); CompileRun(source1); v8::Local source_str = v8_str(source3); v8::ScriptOrigin origin(v8_str("test")); v8::ScriptCompiler::Source source(source_str, origin, cache); v8::Local script; { DisallowCompilation no_compile(reinterpret_cast(isolate2)); script = v8::ScriptCompiler::CompileUnboundScript( isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache) .ToLocalChecked(); } v8::Local result = script->BindToCurrentContext() ->Run(isolate2->GetCurrentContext()) .ToLocalChecked(); v8::Local result_str = result->ToString(isolate2->GetCurrentContext()).ToLocalChecked(); CHECK(result_str->Equals(isolate2->GetCurrentContext(), v8_str("XY")) .FromJust()); } isolate2->Dispose(); } TEST(CodeSerializerEagerCompilationAndPreAge) { if (FLAG_ignition || FLAG_turbo) return; FLAG_lazy = true; FLAG_serialize_toplevel = true; FLAG_serialize_age_code = true; FLAG_serialize_eager = true; static const char* source = "function f() {" " function g() {" " return 1;" " }" " return g();" "}" "'abcdef';"; v8::ScriptCompiler::CachedData* cache = ProduceCache(source); v8::Isolate::CreateParams create_params; create_params.array_buffer_allocator = CcTest::array_buffer_allocator(); v8::Isolate* isolate2 = v8::Isolate::New(create_params); { v8::Isolate::Scope iscope(isolate2); v8::HandleScope scope(isolate2); v8::Local context = v8::Context::New(isolate2); v8::Context::Scope context_scope(context); v8::Local source_str = v8_str(source); v8::ScriptOrigin origin(v8_str("test")); v8::ScriptCompiler::Source source(source_str, origin, cache); v8::Local unbound = v8::ScriptCompiler::CompileUnboundScript( isolate2, &source, v8::ScriptCompiler::kConsumeCodeCache) .ToLocalChecked(); CHECK(!cache->rejected); Isolate* i_isolate = reinterpret_cast(isolate2); HandleScope i_scope(i_isolate); Handle toplevel = v8::Utils::OpenHandle(*unbound); Handle