2010-01-15 14:20:31 +00:00
|
|
|
// Copyright 2007-2010 the V8 project authors. All rights reserved.
|
2008-08-22 13:33:59 +00:00
|
|
|
// 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 <signal.h>
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include <sys/stat.h>
|
2013-05-02 20:18:42 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/bootstrapper.h"
|
|
|
|
#include "src/debug.h"
|
|
|
|
#include "src/ic-inl.h"
|
|
|
|
#include "src/natives.h"
|
|
|
|
#include "src/objects.h"
|
|
|
|
#include "src/runtime.h"
|
|
|
|
#include "src/scopeinfo.h"
|
|
|
|
#include "src/serialize.h"
|
|
|
|
#include "src/snapshot.h"
|
|
|
|
#include "src/spaces.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
2009-04-27 10:52:44 +00:00
|
|
|
static const unsigned kCounters = 256;
|
|
|
|
static int local_counters[kCounters];
|
|
|
|
static const char* local_counter_names[kCounters];
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned CounterHash(const char* s) {
|
|
|
|
unsigned hash = 0;
|
|
|
|
while (*++s) {
|
|
|
|
hash |= hash << 5;
|
|
|
|
hash += *s;
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
2008-08-22 13:33:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Callback receiver to track counters in test.
|
2008-12-01 07:56:57 +00:00
|
|
|
static int* counter_function(const char* name) {
|
2009-04-27 10:52:44 +00:00
|
|
|
unsigned hash = CounterHash(name) % kCounters;
|
|
|
|
unsigned original_hash = hash;
|
|
|
|
USE(original_hash);
|
|
|
|
while (true) {
|
|
|
|
if (local_counter_names[hash] == name) {
|
|
|
|
return &local_counters[hash];
|
|
|
|
}
|
|
|
|
if (local_counter_names[hash] == 0) {
|
|
|
|
local_counter_names[hash] = name;
|
|
|
|
return &local_counters[hash];
|
|
|
|
}
|
|
|
|
if (strcmp(local_counter_names[hash], name) == 0) {
|
|
|
|
return &local_counters[hash];
|
|
|
|
}
|
|
|
|
hash = (hash + 1) % kCounters;
|
|
|
|
ASSERT(hash != original_hash); // Hash table has been filled up.
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static Address AddressOf(T id) {
|
2013-09-19 09:17:13 +00:00
|
|
|
return ExternalReference(id, CcTest::i_isolate()).address();
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static uint32_t Encode(const ExternalReferenceEncoder& encoder, T id) {
|
|
|
|
return encoder.Encode(AddressOf(id));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int make_code(TypeCode type, int id) {
|
|
|
|
return static_cast<uint32_t>(type) << kReferenceTypeShift | id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ExternalReferenceEncoder) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2011-03-22 13:20:04 +00:00
|
|
|
isolate->stats_table()->SetCounterFunction(counter_function);
|
2011-08-04 15:18:18 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
|
2013-09-03 11:54:08 +00:00
|
|
|
ExternalReferenceEncoder encoder(isolate);
|
2011-03-23 13:40:07 +00:00
|
|
|
CHECK_EQ(make_code(BUILTIN, Builtins::kArrayCode),
|
|
|
|
Encode(encoder, Builtins::kArrayCode));
|
2011-02-16 13:56:23 +00:00
|
|
|
CHECK_EQ(make_code(v8::internal::RUNTIME_FUNCTION, Runtime::kAbort),
|
2008-08-22 13:33:59 +00:00
|
|
|
Encode(encoder, Runtime::kAbort));
|
2013-06-19 16:50:38 +00:00
|
|
|
ExternalReference total_compile_size =
|
|
|
|
ExternalReference(isolate->counters()->total_compile_size());
|
|
|
|
CHECK_EQ(make_code(STATS_COUNTER, Counters::k_total_compile_size),
|
|
|
|
encoder.Encode(total_compile_size.address()));
|
2009-11-05 13:59:40 +00:00
|
|
|
ExternalReference stack_limit_address =
|
2011-03-22 13:20:04 +00:00
|
|
|
ExternalReference::address_of_stack_limit(isolate);
|
2009-08-24 11:57:57 +00:00
|
|
|
CHECK_EQ(make_code(UNCLASSIFIED, 4),
|
2009-11-05 13:59:40 +00:00
|
|
|
encoder.Encode(stack_limit_address.address()));
|
|
|
|
ExternalReference real_stack_limit_address =
|
2011-03-22 13:20:04 +00:00
|
|
|
ExternalReference::address_of_real_stack_limit(isolate);
|
2009-11-05 13:59:40 +00:00
|
|
|
CHECK_EQ(make_code(UNCLASSIFIED, 5),
|
|
|
|
encoder.Encode(real_stack_limit_address.address()));
|
2011-09-19 18:36:47 +00:00
|
|
|
CHECK_EQ(make_code(UNCLASSIFIED, 16),
|
2011-03-22 13:20:04 +00:00
|
|
|
encoder.Encode(ExternalReference::debug_break(isolate).address()));
|
2010-06-14 21:03:59 +00:00
|
|
|
CHECK_EQ(make_code(UNCLASSIFIED, 10),
|
2011-03-22 13:20:04 +00:00
|
|
|
encoder.Encode(
|
|
|
|
ExternalReference::new_space_start(isolate).address()));
|
2009-08-24 11:57:57 +00:00
|
|
|
CHECK_EQ(make_code(UNCLASSIFIED, 3),
|
2011-10-20 12:27:10 +00:00
|
|
|
encoder.Encode(
|
|
|
|
ExternalReference::roots_array_start(isolate).address()));
|
2013-01-23 16:29:48 +00:00
|
|
|
CHECK_EQ(make_code(UNCLASSIFIED, 52),
|
|
|
|
encoder.Encode(ExternalReference::cpu_features().address()));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ExternalReferenceDecoder) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2011-03-22 13:20:04 +00:00
|
|
|
isolate->stats_table()->SetCounterFunction(counter_function);
|
2011-08-04 15:18:18 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
|
2013-09-03 11:54:08 +00:00
|
|
|
ExternalReferenceDecoder decoder(isolate);
|
2011-03-23 13:40:07 +00:00
|
|
|
CHECK_EQ(AddressOf(Builtins::kArrayCode),
|
|
|
|
decoder.Decode(make_code(BUILTIN, Builtins::kArrayCode)));
|
2008-08-22 13:33:59 +00:00
|
|
|
CHECK_EQ(AddressOf(Runtime::kAbort),
|
2011-02-16 13:56:23 +00:00
|
|
|
decoder.Decode(make_code(v8::internal::RUNTIME_FUNCTION,
|
|
|
|
Runtime::kAbort)));
|
2013-06-19 16:50:38 +00:00
|
|
|
ExternalReference total_compile_size =
|
|
|
|
ExternalReference(isolate->counters()->total_compile_size());
|
|
|
|
CHECK_EQ(total_compile_size.address(),
|
2008-08-22 13:33:59 +00:00
|
|
|
decoder.Decode(
|
|
|
|
make_code(STATS_COUNTER,
|
2013-06-19 16:50:38 +00:00
|
|
|
Counters::k_total_compile_size)));
|
2011-03-22 13:20:04 +00:00
|
|
|
CHECK_EQ(ExternalReference::address_of_stack_limit(isolate).address(),
|
2009-08-24 11:57:57 +00:00
|
|
|
decoder.Decode(make_code(UNCLASSIFIED, 4)));
|
2011-03-22 13:20:04 +00:00
|
|
|
CHECK_EQ(ExternalReference::address_of_real_stack_limit(isolate).address(),
|
2009-11-05 13:59:40 +00:00
|
|
|
decoder.Decode(make_code(UNCLASSIFIED, 5)));
|
2011-03-22 13:20:04 +00:00
|
|
|
CHECK_EQ(ExternalReference::debug_break(isolate).address(),
|
2011-09-19 18:36:47 +00:00
|
|
|
decoder.Decode(make_code(UNCLASSIFIED, 16)));
|
2011-03-22 13:20:04 +00:00
|
|
|
CHECK_EQ(ExternalReference::new_space_start(isolate).address(),
|
2010-06-14 21:03:59 +00:00
|
|
|
decoder.Decode(make_code(UNCLASSIFIED, 10)));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-27 08:25:48 +00:00
|
|
|
class FileByteSink : public SnapshotByteSink {
|
|
|
|
public:
|
|
|
|
explicit FileByteSink(const char* snapshot_file) {
|
|
|
|
fp_ = OS::FOpen(snapshot_file, "wb");
|
|
|
|
file_name_ = snapshot_file;
|
|
|
|
if (fp_ == NULL) {
|
|
|
|
PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual ~FileByteSink() {
|
|
|
|
if (fp_ != NULL) {
|
|
|
|
fclose(fp_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual void Put(int byte, const char* description) {
|
|
|
|
if (fp_ != NULL) {
|
|
|
|
fputc(byte, fp_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual int Position() {
|
|
|
|
return ftell(fp_);
|
|
|
|
}
|
|
|
|
void WriteSpaceUsed(
|
|
|
|
int new_space_used,
|
|
|
|
int pointer_space_used,
|
|
|
|
int data_space_used,
|
|
|
|
int code_space_used,
|
|
|
|
int map_space_used,
|
2013-06-12 16:23:17 +00:00
|
|
|
int cell_space_used,
|
|
|
|
int property_cell_space_used);
|
2010-01-27 08:25:48 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
FILE* fp_;
|
|
|
|
const char* file_name_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void FileByteSink::WriteSpaceUsed(
|
|
|
|
int new_space_used,
|
|
|
|
int pointer_space_used,
|
|
|
|
int data_space_used,
|
|
|
|
int code_space_used,
|
|
|
|
int map_space_used,
|
2013-06-12 16:23:17 +00:00
|
|
|
int cell_space_used,
|
|
|
|
int property_cell_space_used) {
|
2010-01-27 16:18:58 +00:00
|
|
|
int file_name_length = StrLength(file_name_) + 10;
|
2010-01-27 08:25:48 +00:00
|
|
|
Vector<char> name = Vector<char>::New(file_name_length + 1);
|
|
|
|
OS::SNPrintF(name, "%s.size", file_name_);
|
|
|
|
FILE* fp = OS::FOpen(name.start(), "w");
|
2010-10-29 07:10:41 +00:00
|
|
|
name.Dispose();
|
2010-01-27 08:25:48 +00:00
|
|
|
fprintf(fp, "new %d\n", new_space_used);
|
|
|
|
fprintf(fp, "pointer %d\n", pointer_space_used);
|
|
|
|
fprintf(fp, "data %d\n", data_space_used);
|
|
|
|
fprintf(fp, "code %d\n", code_space_used);
|
|
|
|
fprintf(fp, "map %d\n", map_space_used);
|
|
|
|
fprintf(fp, "cell %d\n", cell_space_used);
|
2013-06-12 16:23:17 +00:00
|
|
|
fprintf(fp, "property cell %d\n", property_cell_space_used);
|
2010-01-27 08:25:48 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-03 11:54:08 +00:00
|
|
|
static bool WriteToFile(Isolate* isolate, const char* snapshot_file) {
|
2010-01-27 08:25:48 +00:00
|
|
|
FileByteSink file(snapshot_file);
|
2013-09-03 11:54:08 +00:00
|
|
|
StartupSerializer ser(isolate, &file);
|
2010-01-27 08:25:48 +00:00
|
|
|
ser.Serialize();
|
2012-09-14 11:16:56 +00:00
|
|
|
|
|
|
|
file.WriteSpaceUsed(
|
|
|
|
ser.CurrentAllocationAddress(NEW_SPACE),
|
|
|
|
ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
|
|
|
|
ser.CurrentAllocationAddress(OLD_DATA_SPACE),
|
|
|
|
ser.CurrentAllocationAddress(CODE_SPACE),
|
|
|
|
ser.CurrentAllocationAddress(MAP_SPACE),
|
2013-06-12 16:23:17 +00:00
|
|
|
ser.CurrentAllocationAddress(CELL_SPACE),
|
|
|
|
ser.CurrentAllocationAddress(PROPERTY_CELL_SPACE));
|
2012-09-14 11:16:56 +00:00
|
|
|
|
2010-01-27 08:25:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
static void Serialize() {
|
2009-10-27 11:54:01 +00:00
|
|
|
// 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.
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-05-08 07:45:16 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope scope(isolate);
|
|
|
|
v8::Context::New(isolate);
|
|
|
|
}
|
2013-09-19 12:06:27 +00:00
|
|
|
|
|
|
|
Isolate* internal_isolate = CcTest::i_isolate();
|
|
|
|
internal_isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, "serialize");
|
|
|
|
WriteToFile(internal_isolate, FLAG_testing_serialization_file);
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-27 11:54:01 +00:00
|
|
|
// Test that the whole heap can be serialized.
|
2013-09-20 10:52:20 +00:00
|
|
|
TEST(Serialize) {
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2014-05-22 09:36:20 +00:00
|
|
|
CcTest::i_isolate()->enable_serializer();
|
2012-06-19 18:38:03 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
Serialize();
|
|
|
|
}
|
2009-10-27 11:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-25 12:55:33 +00:00
|
|
|
// Test that heap serialization is non-destructive.
|
2013-09-20 10:52:20 +00:00
|
|
|
TEST(SerializeTwice) {
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2014-05-22 09:36:20 +00:00
|
|
|
CcTest::i_isolate()->enable_serializer();
|
2012-06-19 18:38:03 +00:00
|
|
|
v8::V8::Initialize();
|
|
|
|
Serialize();
|
|
|
|
Serialize();
|
|
|
|
}
|
2009-11-25 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-22 13:33:59 +00:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
// Tests that the heap can be deserialized.
|
|
|
|
|
|
|
|
static void Deserialize() {
|
2014-06-04 08:35:42 +00:00
|
|
|
CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void SanityCheck() {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::HandleScope scope(CcTest::isolate());
|
2012-10-15 06:34:22 +00:00
|
|
|
#ifdef VERIFY_HEAP
|
2013-09-19 09:46:15 +00:00
|
|
|
CcTest::heap()->Verify();
|
2008-08-22 13:33:59 +00:00
|
|
|
#endif
|
2013-06-04 10:30:05 +00:00
|
|
|
CHECK(isolate->global_object()->IsJSObject());
|
|
|
|
CHECK(isolate->native_context()->IsContext());
|
2013-09-19 09:46:15 +00:00
|
|
|
CHECK(CcTest::heap()->string_table()->IsStringTable());
|
2014-04-30 12:25:18 +00:00
|
|
|
isolate->factory()->InternalizeOneByteString(STATIC_ASCII_VECTOR("Empty"));
|
2008-08-22 13:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-03 08:35:03 +00:00
|
|
|
DEPENDENT_TEST(Deserialize, Serialize) {
|
2010-03-23 11:40:38 +00:00
|
|
|
// The serialize-deserialize tests only work if the VM is built without
|
|
|
|
// serialization. That doesn't matter. We don't need to be able to
|
|
|
|
// serialize a snapshot in a VM that is booted from a snapshot.
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
Deserialize();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
env->Enter();
|
2009-11-25 12:55:33 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
SanityCheck();
|
|
|
|
}
|
2009-11-25 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
Deserialize();
|
2009-11-25 12:55:33 +00:00
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
env->Enter();
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
SanityCheck();
|
|
|
|
}
|
2009-10-27 11:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-16 12:08:40 +00:00
|
|
|
DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
Deserialize();
|
2008-08-22 13:33:59 +00:00
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
env->Enter();
|
2009-10-27 11:54:01 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
const char* c_source = "\"1234\".length";
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, c_source);
|
2010-03-23 11:40:38 +00:00
|
|
|
v8::Local<v8::Script> script = v8::Script::Compile(source);
|
|
|
|
CHECK_EQ(4, script->Run()->Int32Value());
|
|
|
|
}
|
2009-10-27 11:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-25 12:55:33 +00:00
|
|
|
DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
|
|
|
|
SerializeTwice) {
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::HandleScope scope(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
Deserialize();
|
2009-11-25 12:55:33 +00:00
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Local<v8::Context> env = v8::Context::New(isolate);
|
2010-03-23 11:40:38 +00:00
|
|
|
env->Enter();
|
2009-11-25 12:55:33 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
const char* c_source = "\"1234\".length";
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Local<v8::String> source = v8::String::NewFromUtf8(isolate, c_source);
|
2010-03-23 11:40:38 +00:00
|
|
|
v8::Local<v8::Script> script = v8::Script::Compile(source);
|
|
|
|
CHECK_EQ(4, script->Run()->Int32Value());
|
|
|
|
}
|
2009-11-25 12:55:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-20 10:52:20 +00:00
|
|
|
TEST(PartialSerialization) {
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2014-05-22 09:36:20 +00:00
|
|
|
CcTest::i_isolate()->enable_serializer();
|
2013-09-03 11:54:08 +00:00
|
|
|
v8::V8::Initialize();
|
2013-05-28 11:54:52 +00:00
|
|
|
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
|
2013-02-15 09:27:10 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2010-01-27 08:25:48 +00:00
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Persistent<v8::Context> env;
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
2013-05-28 11:54:52 +00:00
|
|
|
env.Reset(v8_isolate, v8::Context::New(v8_isolate));
|
2013-05-08 07:45:16 +00:00
|
|
|
}
|
2012-06-19 18:38:03 +00:00
|
|
|
ASSERT(!env.IsEmpty());
|
2013-05-28 11:54:52 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Enter();
|
|
|
|
}
|
2012-06-19 18:38:03 +00:00
|
|
|
// Make sure all builtin scripts are cached.
|
2013-02-15 09:27:10 +00:00
|
|
|
{ HandleScope scope(isolate);
|
2012-06-19 18:38:03 +00:00
|
|
|
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
|
2013-02-15 09:27:10 +00:00
|
|
|
isolate->bootstrapper()->NativesSourceLookup(i);
|
2012-06-19 18:38:03 +00:00
|
|
|
}
|
2010-01-14 14:46:31 +00:00
|
|
|
}
|
2013-02-15 09:27:10 +00:00
|
|
|
heap->CollectAllGarbage(Heap::kNoGCFlags);
|
|
|
|
heap->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-01-27 08:25:48 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
Object* raw_foo;
|
|
|
|
{
|
2013-05-28 11:54:52 +00:00
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
2013-11-22 12:43:17 +00:00
|
|
|
v8::Local<v8::String> foo = v8::String::NewFromUtf8(v8_isolate, "foo");
|
2012-06-19 18:38:03 +00:00
|
|
|
ASSERT(!foo.IsEmpty());
|
|
|
|
raw_foo = *(v8::Utils::OpenHandle(*foo));
|
|
|
|
}
|
2010-01-14 14:46:31 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
|
|
|
|
Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
|
|
|
|
OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
|
2010-01-14 14:46:31 +00:00
|
|
|
|
2013-05-28 11:54:52 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
|
|
|
|
}
|
2013-11-22 12:43:17 +00:00
|
|
|
env.Reset();
|
2010-01-14 14:46:31 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
FileByteSink startup_sink(startup_name.start());
|
2013-09-03 11:54:08 +00:00
|
|
|
StartupSerializer startup_serializer(isolate, &startup_sink);
|
2012-06-19 18:38:03 +00:00
|
|
|
startup_serializer.SerializeStrongReferences();
|
|
|
|
|
|
|
|
FileByteSink partial_sink(FLAG_testing_serialization_file);
|
2013-09-03 11:54:08 +00:00
|
|
|
PartialSerializer p_ser(isolate, &startup_serializer, &partial_sink);
|
2012-06-19 18:38:03 +00:00
|
|
|
p_ser.Serialize(&raw_foo);
|
|
|
|
startup_serializer.SerializeWeakReferences();
|
2012-09-14 11:16:56 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
partial_sink.WriteSpaceUsed(
|
|
|
|
p_ser.CurrentAllocationAddress(NEW_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(CODE_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(MAP_SPACE),
|
2013-06-12 16:23:17 +00:00
|
|
|
p_ser.CurrentAllocationAddress(CELL_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(PROPERTY_CELL_SPACE));
|
2012-09-14 11:16:56 +00:00
|
|
|
|
|
|
|
startup_sink.WriteSpaceUsed(
|
|
|
|
startup_serializer.CurrentAllocationAddress(NEW_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(OLD_POINTER_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(OLD_DATA_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(CODE_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(MAP_SPACE),
|
2013-06-12 16:23:17 +00:00
|
|
|
startup_serializer.CurrentAllocationAddress(CELL_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(PROPERTY_CELL_SPACE));
|
2012-09-14 11:16:56 +00:00
|
|
|
startup_name.Dispose();
|
2012-06-19 18:38:03 +00:00
|
|
|
}
|
2010-01-15 14:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-04 08:35:42 +00:00
|
|
|
static void ReserveSpaceForSnapshot(Deserializer* deserializer,
|
|
|
|
const char* file_name) {
|
|
|
|
int file_name_length = StrLength(file_name) + 10;
|
|
|
|
Vector<char> name = Vector<char>::New(file_name_length + 1);
|
|
|
|
OS::SNPrintF(name, "%s.size", file_name);
|
|
|
|
FILE* fp = OS::FOpen(name.start(), "r");
|
|
|
|
name.Dispose();
|
|
|
|
int new_size, pointer_size, data_size, code_size, map_size, cell_size,
|
|
|
|
property_cell_size;
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Avoid warning about unsafe fscanf from MSVC.
|
|
|
|
// Please note that this is only fine if %c and %s are not being used.
|
|
|
|
#define fscanf fscanf_s
|
|
|
|
#endif
|
|
|
|
CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size));
|
|
|
|
CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size));
|
|
|
|
CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size));
|
|
|
|
CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size));
|
|
|
|
CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size));
|
|
|
|
CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size));
|
|
|
|
CHECK_EQ(1, fscanf(fp, "property cell %d\n", &property_cell_size));
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#undef fscanf
|
|
|
|
#endif
|
|
|
|
fclose(fp);
|
|
|
|
deserializer->set_reservation(NEW_SPACE, new_size);
|
|
|
|
deserializer->set_reservation(OLD_POINTER_SPACE, pointer_size);
|
|
|
|
deserializer->set_reservation(OLD_DATA_SPACE, data_size);
|
|
|
|
deserializer->set_reservation(CODE_SPACE, code_size);
|
|
|
|
deserializer->set_reservation(MAP_SPACE, map_size);
|
|
|
|
deserializer->set_reservation(CELL_SPACE, cell_size);
|
|
|
|
deserializer->set_reservation(PROPERTY_CELL_SPACE, property_cell_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
DEPENDENT_TEST(PartialDeserialization, PartialSerialization) {
|
2014-06-04 08:35:42 +00:00
|
|
|
if (!Snapshot::IsEnabled()) {
|
2010-03-23 11:40:38 +00:00
|
|
|
int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
|
|
|
|
Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
|
|
|
|
OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
|
|
|
|
|
2014-06-04 08:35:42 +00:00
|
|
|
CHECK(Snapshot::Initialize(startup_name.start()));
|
2010-10-29 07:10:41 +00:00
|
|
|
startup_name.Dispose();
|
2010-03-23 11:40:38 +00:00
|
|
|
|
|
|
|
const char* file_name = FLAG_testing_serialization_file;
|
|
|
|
|
|
|
|
int snapshot_size = 0;
|
|
|
|
byte* snapshot = ReadBytes(file_name, &snapshot_size);
|
|
|
|
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2010-03-23 11:40:38 +00:00
|
|
|
Object* root;
|
|
|
|
{
|
|
|
|
SnapshotByteSource source(snapshot, snapshot_size);
|
|
|
|
Deserializer deserializer(&source);
|
2012-09-14 11:16:56 +00:00
|
|
|
ReserveSpaceForSnapshot(&deserializer, file_name);
|
2013-09-03 11:54:08 +00:00
|
|
|
deserializer.DeserializePartial(isolate, &root);
|
2010-03-23 11:40:38 +00:00
|
|
|
CHECK(root->IsString());
|
|
|
|
}
|
2013-09-03 11:54:08 +00:00
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<Object> root_handle(root, isolate);
|
2011-08-02 17:43:32 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
|
|
|
|
Object* root2;
|
|
|
|
{
|
|
|
|
SnapshotByteSource source(snapshot, snapshot_size);
|
|
|
|
Deserializer deserializer(&source);
|
2012-09-14 11:16:56 +00:00
|
|
|
ReserveSpaceForSnapshot(&deserializer, file_name);
|
2013-09-03 11:54:08 +00:00
|
|
|
deserializer.DeserializePartial(isolate, &root2);
|
2010-03-23 11:40:38 +00:00
|
|
|
CHECK(root2->IsString());
|
|
|
|
CHECK(*root_handle == root2);
|
|
|
|
}
|
2010-01-27 08:25:48 +00:00
|
|
|
}
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2010-01-27 08:25:48 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2013-09-20 10:52:20 +00:00
|
|
|
TEST(ContextSerialization) {
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2014-05-22 09:36:20 +00:00
|
|
|
CcTest::i_isolate()->enable_serializer();
|
2013-09-03 12:33:52 +00:00
|
|
|
v8::V8::Initialize();
|
2013-05-28 11:54:52 +00:00
|
|
|
v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
|
2013-02-15 09:27:10 +00:00
|
|
|
Heap* heap = isolate->heap();
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::Persistent<v8::Context> env;
|
|
|
|
{
|
|
|
|
HandleScope scope(isolate);
|
2013-05-28 11:54:52 +00:00
|
|
|
env.Reset(v8_isolate, v8::Context::New(v8_isolate));
|
2013-05-08 07:45:16 +00:00
|
|
|
}
|
2012-06-19 18:38:03 +00:00
|
|
|
ASSERT(!env.IsEmpty());
|
2013-05-28 11:54:52 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Enter();
|
|
|
|
}
|
2012-06-19 18:38:03 +00:00
|
|
|
// Make sure all builtin scripts are cached.
|
2013-02-15 09:27:10 +00:00
|
|
|
{ HandleScope scope(isolate);
|
2012-06-19 18:38:03 +00:00
|
|
|
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
|
2013-02-15 09:27:10 +00:00
|
|
|
isolate->bootstrapper()->NativesSourceLookup(i);
|
2012-06-19 18:38:03 +00:00
|
|
|
}
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
2012-06-19 18:38:03 +00:00
|
|
|
// 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.
|
2013-02-15 09:27:10 +00:00
|
|
|
heap->CollectAllGarbage(Heap::kNoGCFlags);
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
|
|
|
|
Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
|
|
|
|
OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2013-05-28 11:54:52 +00:00
|
|
|
{
|
|
|
|
v8::HandleScope handle_scope(v8_isolate);
|
|
|
|
v8::Local<v8::Context>::New(v8_isolate, env)->Exit();
|
|
|
|
}
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2013-06-13 09:27:09 +00:00
|
|
|
i::Object* raw_context = *v8::Utils::OpenPersistent(env);
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2013-11-22 12:43:17 +00:00
|
|
|
env.Reset();
|
2010-03-23 11:40:38 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
FileByteSink startup_sink(startup_name.start());
|
2013-09-03 11:54:08 +00:00
|
|
|
StartupSerializer startup_serializer(isolate, &startup_sink);
|
2012-06-19 18:38:03 +00:00
|
|
|
startup_serializer.SerializeStrongReferences();
|
|
|
|
|
|
|
|
FileByteSink partial_sink(FLAG_testing_serialization_file);
|
2013-09-03 11:54:08 +00:00
|
|
|
PartialSerializer p_ser(isolate, &startup_serializer, &partial_sink);
|
2012-06-19 18:38:03 +00:00
|
|
|
p_ser.Serialize(&raw_context);
|
|
|
|
startup_serializer.SerializeWeakReferences();
|
2012-09-14 11:16:56 +00:00
|
|
|
|
2012-06-19 18:38:03 +00:00
|
|
|
partial_sink.WriteSpaceUsed(
|
|
|
|
p_ser.CurrentAllocationAddress(NEW_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(CODE_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(MAP_SPACE),
|
2013-06-12 16:23:17 +00:00
|
|
|
p_ser.CurrentAllocationAddress(CELL_SPACE),
|
|
|
|
p_ser.CurrentAllocationAddress(PROPERTY_CELL_SPACE));
|
2012-09-14 11:16:56 +00:00
|
|
|
|
|
|
|
startup_sink.WriteSpaceUsed(
|
|
|
|
startup_serializer.CurrentAllocationAddress(NEW_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(OLD_POINTER_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(OLD_DATA_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(CODE_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(MAP_SPACE),
|
2013-06-12 16:23:17 +00:00
|
|
|
startup_serializer.CurrentAllocationAddress(CELL_SPACE),
|
|
|
|
startup_serializer.CurrentAllocationAddress(PROPERTY_CELL_SPACE));
|
2012-09-14 11:16:56 +00:00
|
|
|
startup_name.Dispose();
|
2012-06-19 18:38:03 +00:00
|
|
|
}
|
2010-03-23 11:40:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DEPENDENT_TEST(ContextDeserialization, ContextSerialization) {
|
2012-06-19 18:38:03 +00:00
|
|
|
if (!Snapshot::HaveASnapshotToStartFrom()) {
|
2010-03-23 11:40:38 +00:00
|
|
|
int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
|
|
|
|
Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
|
|
|
|
OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
|
|
|
|
|
2014-06-04 08:35:42 +00:00
|
|
|
CHECK(Snapshot::Initialize(startup_name.start()));
|
2010-10-29 07:10:41 +00:00
|
|
|
startup_name.Dispose();
|
2010-03-23 11:40:38 +00:00
|
|
|
|
|
|
|
const char* file_name = FLAG_testing_serialization_file;
|
|
|
|
|
|
|
|
int snapshot_size = 0;
|
|
|
|
byte* snapshot = ReadBytes(file_name, &snapshot_size);
|
|
|
|
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2010-03-23 11:40:38 +00:00
|
|
|
Object* root;
|
|
|
|
{
|
|
|
|
SnapshotByteSource source(snapshot, snapshot_size);
|
|
|
|
Deserializer deserializer(&source);
|
2012-09-14 11:16:56 +00:00
|
|
|
ReserveSpaceForSnapshot(&deserializer, file_name);
|
2013-09-03 11:54:08 +00:00
|
|
|
deserializer.DeserializePartial(isolate, &root);
|
2010-03-23 11:40:38 +00:00
|
|
|
CHECK(root->IsContext());
|
|
|
|
}
|
2013-09-03 11:54:08 +00:00
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<Object> root_handle(root, isolate);
|
2011-08-02 17:43:32 +00:00
|
|
|
|
2010-03-23 11:40:38 +00:00
|
|
|
|
|
|
|
Object* root2;
|
|
|
|
{
|
|
|
|
SnapshotByteSource source(snapshot, snapshot_size);
|
|
|
|
Deserializer deserializer(&source);
|
2012-09-14 11:16:56 +00:00
|
|
|
ReserveSpaceForSnapshot(&deserializer, file_name);
|
2013-09-03 11:54:08 +00:00
|
|
|
deserializer.DeserializePartial(isolate, &root2);
|
2010-03-23 11:40:38 +00:00
|
|
|
CHECK(root2->IsContext());
|
|
|
|
CHECK(*root_handle != root2);
|
|
|
|
}
|
2010-01-27 08:25:48 +00:00
|
|
|
}
|
2010-01-14 14:46:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-22 19:09:09 +00:00
|
|
|
TEST(TestThatAlwaysSucceeds) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(TestThatAlwaysFails) {
|
|
|
|
bool ArtificialFailure = false;
|
|
|
|
CHECK(ArtificialFailure);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
|
|
|
|
bool ArtificialFailure2 = false;
|
|
|
|
CHECK(ArtificialFailure2);
|
|
|
|
}
|