blob: 777c769b24914f58d8b9417b306b6d72e59522c4 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2007-2010 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include <signal.h>
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000029
30#include "sys/stat.h"
31#include "v8.h"
32
33#include "debug.h"
34#include "ic-inl.h"
35#include "runtime.h"
36#include "serialize.h"
37#include "scopeinfo.h"
38#include "snapshot.h"
39#include "cctest.h"
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000040#include "spaces.h"
41#include "objects.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000042#include "natives.h"
43#include "bootstrapper.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000045using namespace v8::internal;
46
ager@chromium.org5ec48922009-05-05 07:25:34 +000047static const unsigned kCounters = 256;
48static int local_counters[kCounters];
49static const char* local_counter_names[kCounters];
50
51
52static unsigned CounterHash(const char* s) {
53 unsigned hash = 0;
54 while (*++s) {
55 hash |= hash << 5;
56 hash += *s;
57 }
58 return hash;
59}
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000060
61
62// Callback receiver to track counters in test.
ager@chromium.orga74f0da2008-12-03 16:05:52 +000063static int* counter_function(const char* name) {
ager@chromium.org5ec48922009-05-05 07:25:34 +000064 unsigned hash = CounterHash(name) % kCounters;
65 unsigned original_hash = hash;
66 USE(original_hash);
67 while (true) {
68 if (local_counter_names[hash] == name) {
69 return &local_counters[hash];
70 }
71 if (local_counter_names[hash] == 0) {
72 local_counter_names[hash] = name;
73 return &local_counters[hash];
74 }
75 if (strcmp(local_counter_names[hash], name) == 0) {
76 return &local_counters[hash];
77 }
78 hash = (hash + 1) % kCounters;
79 ASSERT(hash != original_hash); // Hash table has been filled up.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000080 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000081}
82
83
84template <class T>
85static Address AddressOf(T id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000086 return ExternalReference(id, i::Isolate::Current()).address();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000087}
88
89
90template <class T>
91static uint32_t Encode(const ExternalReferenceEncoder& encoder, T id) {
92 return encoder.Encode(AddressOf(id));
93}
94
95
96static int make_code(TypeCode type, int id) {
97 return static_cast<uint32_t>(type) << kReferenceTypeShift | id;
98}
99
100
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000101TEST(ExternalReferenceEncoder) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000102 Isolate* isolate = i::Isolate::Current();
103 isolate->stats_table()->SetCounterFunction(counter_function);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000104 v8::V8::Initialize();
105
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000106 ExternalReferenceEncoder encoder;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000107 CHECK_EQ(make_code(BUILTIN, Builtins::kArrayCode),
108 Encode(encoder, Builtins::kArrayCode));
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000109 CHECK_EQ(make_code(v8::internal::RUNTIME_FUNCTION, Runtime::kAbort),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000110 Encode(encoder, Runtime::kAbort));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000111 ExternalReference keyed_load_function_prototype =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000112 ExternalReference(isolate->counters()->keyed_load_function_prototype());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000113 CHECK_EQ(make_code(STATS_COUNTER, Counters::k_keyed_load_function_prototype),
114 encoder.Encode(keyed_load_function_prototype.address()));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000115 ExternalReference stack_limit_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116 ExternalReference::address_of_stack_limit(isolate);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000117 CHECK_EQ(make_code(UNCLASSIFIED, 4),
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000118 encoder.Encode(stack_limit_address.address()));
119 ExternalReference real_stack_limit_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000120 ExternalReference::address_of_real_stack_limit(isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000121 CHECK_EQ(make_code(UNCLASSIFIED, 5),
122 encoder.Encode(real_stack_limit_address.address()));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000123#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000124 CHECK_EQ(make_code(UNCLASSIFIED, 16),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000125 encoder.Encode(ExternalReference::debug_break(isolate).address()));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000126#endif // ENABLE_DEBUGGER_SUPPORT
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000127 CHECK_EQ(make_code(UNCLASSIFIED, 10),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000128 encoder.Encode(
129 ExternalReference::new_space_start(isolate).address()));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000130 CHECK_EQ(make_code(UNCLASSIFIED, 3),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000131 encoder.Encode(
132 ExternalReference::roots_array_start(isolate).address()));
yangguo@chromium.org003650e2013-01-24 16:31:08 +0000133 CHECK_EQ(make_code(UNCLASSIFIED, 52),
134 encoder.Encode(ExternalReference::cpu_features().address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000135}
136
137
138TEST(ExternalReferenceDecoder) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000139 Isolate* isolate = i::Isolate::Current();
140 isolate->stats_table()->SetCounterFunction(counter_function);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000141 v8::V8::Initialize();
142
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000143 ExternalReferenceDecoder decoder;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000144 CHECK_EQ(AddressOf(Builtins::kArrayCode),
145 decoder.Decode(make_code(BUILTIN, Builtins::kArrayCode)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000146 CHECK_EQ(AddressOf(Runtime::kAbort),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000147 decoder.Decode(make_code(v8::internal::RUNTIME_FUNCTION,
148 Runtime::kAbort)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000149 ExternalReference keyed_load_function =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000150 ExternalReference(isolate->counters()->keyed_load_function_prototype());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000151 CHECK_EQ(keyed_load_function.address(),
152 decoder.Decode(
153 make_code(STATS_COUNTER,
154 Counters::k_keyed_load_function_prototype)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000155 CHECK_EQ(ExternalReference::address_of_stack_limit(isolate).address(),
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000156 decoder.Decode(make_code(UNCLASSIFIED, 4)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000157 CHECK_EQ(ExternalReference::address_of_real_stack_limit(isolate).address(),
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000158 decoder.Decode(make_code(UNCLASSIFIED, 5)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000159#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000160 CHECK_EQ(ExternalReference::debug_break(isolate).address(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000161 decoder.Decode(make_code(UNCLASSIFIED, 16)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000162#endif // ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000163 CHECK_EQ(ExternalReference::new_space_start(isolate).address(),
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000164 decoder.Decode(make_code(UNCLASSIFIED, 10)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000165}
166
167
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000168class FileByteSink : public SnapshotByteSink {
169 public:
170 explicit FileByteSink(const char* snapshot_file) {
171 fp_ = OS::FOpen(snapshot_file, "wb");
172 file_name_ = snapshot_file;
173 if (fp_ == NULL) {
174 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
175 exit(1);
176 }
177 }
178 virtual ~FileByteSink() {
179 if (fp_ != NULL) {
180 fclose(fp_);
181 }
182 }
183 virtual void Put(int byte, const char* description) {
184 if (fp_ != NULL) {
185 fputc(byte, fp_);
186 }
187 }
188 virtual int Position() {
189 return ftell(fp_);
190 }
191 void WriteSpaceUsed(
192 int new_space_used,
193 int pointer_space_used,
194 int data_space_used,
195 int code_space_used,
196 int map_space_used,
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000197 int cell_space_used);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000198
199 private:
200 FILE* fp_;
201 const char* file_name_;
202};
203
204
205void FileByteSink::WriteSpaceUsed(
206 int new_space_used,
207 int pointer_space_used,
208 int data_space_used,
209 int code_space_used,
210 int map_space_used,
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000211 int cell_space_used) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000212 int file_name_length = StrLength(file_name_) + 10;
213 Vector<char> name = Vector<char>::New(file_name_length + 1);
214 OS::SNPrintF(name, "%s.size", file_name_);
215 FILE* fp = OS::FOpen(name.start(), "w");
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000216 name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000217 fprintf(fp, "new %d\n", new_space_used);
218 fprintf(fp, "pointer %d\n", pointer_space_used);
219 fprintf(fp, "data %d\n", data_space_used);
220 fprintf(fp, "code %d\n", code_space_used);
221 fprintf(fp, "map %d\n", map_space_used);
222 fprintf(fp, "cell %d\n", cell_space_used);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000223 fclose(fp);
224}
225
226
227static bool WriteToFile(const char* snapshot_file) {
228 FileByteSink file(snapshot_file);
229 StartupSerializer ser(&file);
230 ser.Serialize();
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000231
232 file.WriteSpaceUsed(
233 ser.CurrentAllocationAddress(NEW_SPACE),
234 ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
235 ser.CurrentAllocationAddress(OLD_DATA_SPACE),
236 ser.CurrentAllocationAddress(CODE_SPACE),
237 ser.CurrentAllocationAddress(MAP_SPACE),
238 ser.CurrentAllocationAddress(CELL_SPACE));
239
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000240 return true;
241}
242
243
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000244static void Serialize() {
ager@chromium.org3811b432009-10-28 14:53:37 +0000245 // We have to create one context. One reason for this is so that the builtins
246 // can be loaded from v8natives.js and their addresses can be processed. This
247 // will clear the pending fixups array, which would otherwise contain GC roots
248 // that would confuse the serialization/deserialization process.
249 v8::Persistent<v8::Context> env = v8::Context::New();
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000250 env.Dispose(env->GetIsolate());
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000251 WriteToFile(FLAG_testing_serialization_file);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000252}
253
254
ager@chromium.org3811b432009-10-28 14:53:37 +0000255// Test that the whole heap can be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000256TEST(Serialize) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000257 if (!Snapshot::HaveASnapshotToStartFrom()) {
258 Serializer::Enable();
259 v8::V8::Initialize();
260 Serialize();
261 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000262}
263
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000264
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000265// Test that heap serialization is non-destructive.
266TEST(SerializeTwice) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000267 if (!Snapshot::HaveASnapshotToStartFrom()) {
268 Serializer::Enable();
269 v8::V8::Initialize();
270 Serialize();
271 Serialize();
272 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000273}
274
275
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000276//----------------------------------------------------------------------------
277// Tests that the heap can be deserialized.
278
279static void Deserialize() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000280 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000281}
282
283
284static void SanityCheck() {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000285 v8::HandleScope scope(v8::Isolate::GetCurrent());
svenpanne@chromium.orgc859c4f2012-10-15 11:51:39 +0000286#ifdef VERIFY_HEAP
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000287 HEAP->Verify();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000288#endif
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000289 CHECK(Isolate::Current()->global_object()->IsJSObject());
290 CHECK(Isolate::Current()->native_context()->IsContext());
yangguo@chromium.org4a9f6552013-03-04 14:46:33 +0000291 CHECK(HEAP->string_table()->IsStringTable());
292 CHECK(!FACTORY->InternalizeOneByteString(
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000293 STATIC_ASCII_VECTOR("Empty"))->IsFailure());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000294}
295
296
iposva@chromium.org245aa852009-02-10 00:49:54 +0000297DEPENDENT_TEST(Deserialize, Serialize) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000298 // The serialize-deserialize tests only work if the VM is built without
299 // serialization. That doesn't matter. We don't need to be able to
300 // serialize a snapshot in a VM that is booted from a snapshot.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000301 if (!Snapshot::HaveASnapshotToStartFrom()) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000302 v8::HandleScope scope(v8::Isolate::GetCurrent());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000303 Deserialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000304
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000305 v8::Persistent<v8::Context> env = v8::Context::New();
306 env->Enter();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000307
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000308 SanityCheck();
309 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000310}
311
312
313DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000314 if (!Snapshot::HaveASnapshotToStartFrom()) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000315 v8::HandleScope scope(v8::Isolate::GetCurrent());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000316 Deserialize();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000317
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000318 v8::Persistent<v8::Context> env = v8::Context::New();
319 env->Enter();
ager@chromium.org3811b432009-10-28 14:53:37 +0000320
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000321 SanityCheck();
322 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000323}
324
325
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000326DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000327 if (!Snapshot::HaveASnapshotToStartFrom()) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000328 v8::HandleScope scope(v8::Isolate::GetCurrent());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000329 Deserialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000330
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000331 v8::Persistent<v8::Context> env = v8::Context::New();
332 env->Enter();
ager@chromium.org3811b432009-10-28 14:53:37 +0000333
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000334 const char* c_source = "\"1234\".length";
335 v8::Local<v8::String> source = v8::String::New(c_source);
336 v8::Local<v8::Script> script = v8::Script::Compile(source);
337 CHECK_EQ(4, script->Run()->Int32Value());
338 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000339}
340
341
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000342DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
343 SerializeTwice) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000344 if (!Snapshot::HaveASnapshotToStartFrom()) {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000345 v8::HandleScope scope(v8::Isolate::GetCurrent());
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000346 Deserialize();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000347
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000348 v8::Persistent<v8::Context> env = v8::Context::New();
349 env->Enter();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000350
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000351 const char* c_source = "\"1234\".length";
352 v8::Local<v8::String> source = v8::String::New(c_source);
353 v8::Local<v8::Script> script = v8::Script::Compile(source);
354 CHECK_EQ(4, script->Run()->Int32Value());
355 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000356}
357
358
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000359TEST(PartialSerialization) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000360 if (!Snapshot::HaveASnapshotToStartFrom()) {
361 Serializer::Enable();
362 v8::V8::Initialize();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000363 Isolate* isolate = Isolate::Current();
364 Heap* heap = isolate->heap();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000365
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000366 v8::Persistent<v8::Context> env = v8::Context::New();
367 ASSERT(!env.IsEmpty());
368 env->Enter();
369 // Make sure all builtin scripts are cached.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000370 { HandleScope scope(isolate);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000371 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000372 isolate->bootstrapper()->NativesSourceLookup(i);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000373 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000374 }
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000375 heap->CollectAllGarbage(Heap::kNoGCFlags);
376 heap->CollectAllGarbage(Heap::kNoGCFlags);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000377
378 Object* raw_foo;
379 {
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000380 v8::HandleScope handle_scope(env->GetIsolate());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000381 v8::Local<v8::String> foo = v8::String::New("foo");
382 ASSERT(!foo.IsEmpty());
383 raw_foo = *(v8::Utils::OpenHandle(*foo));
384 }
385
386 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
387 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
388 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
389
390 env->Exit();
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000391 env.Dispose(env->GetIsolate());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000392
393 FileByteSink startup_sink(startup_name.start());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000394 StartupSerializer startup_serializer(&startup_sink);
395 startup_serializer.SerializeStrongReferences();
396
397 FileByteSink partial_sink(FLAG_testing_serialization_file);
398 PartialSerializer p_ser(&startup_serializer, &partial_sink);
399 p_ser.Serialize(&raw_foo);
400 startup_serializer.SerializeWeakReferences();
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000401
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000402 partial_sink.WriteSpaceUsed(
403 p_ser.CurrentAllocationAddress(NEW_SPACE),
404 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
405 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
406 p_ser.CurrentAllocationAddress(CODE_SPACE),
407 p_ser.CurrentAllocationAddress(MAP_SPACE),
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000408 p_ser.CurrentAllocationAddress(CELL_SPACE));
409
410 startup_sink.WriteSpaceUsed(
411 startup_serializer.CurrentAllocationAddress(NEW_SPACE),
412 startup_serializer.CurrentAllocationAddress(OLD_POINTER_SPACE),
413 startup_serializer.CurrentAllocationAddress(OLD_DATA_SPACE),
414 startup_serializer.CurrentAllocationAddress(CODE_SPACE),
415 startup_serializer.CurrentAllocationAddress(MAP_SPACE),
416 startup_serializer.CurrentAllocationAddress(CELL_SPACE));
417 startup_name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000418 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000419}
420
421
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000422static void ReserveSpaceForSnapshot(Deserializer* deserializer,
423 const char* file_name) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000424 int file_name_length = StrLength(file_name) + 10;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000425 Vector<char> name = Vector<char>::New(file_name_length + 1);
426 OS::SNPrintF(name, "%s.size", file_name);
427 FILE* fp = OS::FOpen(name.start(), "r");
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000428 name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000429 int new_size, pointer_size, data_size, code_size, map_size, cell_size;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000430#ifdef _MSC_VER
431 // Avoid warning about unsafe fscanf from MSVC.
432 // Please note that this is only fine if %c and %s are not being used.
433#define fscanf fscanf_s
434#endif
435 CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size));
436 CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size));
437 CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size));
438 CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size));
439 CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size));
440 CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000441#ifdef _MSC_VER
442#undef fscanf
443#endif
444 fclose(fp);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000445 deserializer->set_reservation(NEW_SPACE, new_size);
446 deserializer->set_reservation(OLD_POINTER_SPACE, pointer_size);
447 deserializer->set_reservation(OLD_DATA_SPACE, data_size);
448 deserializer->set_reservation(CODE_SPACE, code_size);
449 deserializer->set_reservation(MAP_SPACE, map_size);
450 deserializer->set_reservation(CELL_SPACE, cell_size);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000451}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000452
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000453
454DEPENDENT_TEST(PartialDeserialization, PartialSerialization) {
455 if (!Snapshot::IsEnabled()) {
456 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
457 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
458 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
459
460 CHECK(Snapshot::Initialize(startup_name.start()));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000461 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000462
463 const char* file_name = FLAG_testing_serialization_file;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000464
465 int snapshot_size = 0;
466 byte* snapshot = ReadBytes(file_name, &snapshot_size);
467
468 Object* root;
469 {
470 SnapshotByteSource source(snapshot, snapshot_size);
471 Deserializer deserializer(&source);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000472 ReserveSpaceForSnapshot(&deserializer, file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000473 deserializer.DeserializePartial(&root);
474 CHECK(root->IsString());
475 }
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000476 v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000477 Handle<Object> root_handle(root, Isolate::Current());
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +0000478
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000479
480 Object* root2;
481 {
482 SnapshotByteSource source(snapshot, snapshot_size);
483 Deserializer deserializer(&source);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000484 ReserveSpaceForSnapshot(&deserializer, file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000485 deserializer.DeserializePartial(&root2);
486 CHECK(root2->IsString());
487 CHECK(*root_handle == root2);
488 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000489 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000490}
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000491
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000492
493TEST(ContextSerialization) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000494 if (!Snapshot::HaveASnapshotToStartFrom()) {
495 Serializer::Enable();
496 v8::V8::Initialize();
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000497 Isolate* isolate = Isolate::Current();
498 Heap* heap = isolate->heap();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000499
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000500 v8::Persistent<v8::Context> env = v8::Context::New();
501 ASSERT(!env.IsEmpty());
502 env->Enter();
503 // Make sure all builtin scripts are cached.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000504 { HandleScope scope(isolate);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000505 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000506 isolate->bootstrapper()->NativesSourceLookup(i);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000507 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000508 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000509 // If we don't do this then we end up with a stray root pointing at the
510 // context even after we have disposed of env.
yangguo@chromium.orgc03a1922013-02-19 13:55:47 +0000511 heap->CollectAllGarbage(Heap::kNoGCFlags);
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000512
513 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
514 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
515 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
516
517 env->Exit();
518
519 Object* raw_context = *(v8::Utils::OpenHandle(*env));
520
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000521 env.Dispose(env->GetIsolate());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000522
523 FileByteSink startup_sink(startup_name.start());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000524 StartupSerializer startup_serializer(&startup_sink);
525 startup_serializer.SerializeStrongReferences();
526
527 FileByteSink partial_sink(FLAG_testing_serialization_file);
528 PartialSerializer p_ser(&startup_serializer, &partial_sink);
529 p_ser.Serialize(&raw_context);
530 startup_serializer.SerializeWeakReferences();
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000531
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000532 partial_sink.WriteSpaceUsed(
533 p_ser.CurrentAllocationAddress(NEW_SPACE),
534 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
535 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
536 p_ser.CurrentAllocationAddress(CODE_SPACE),
537 p_ser.CurrentAllocationAddress(MAP_SPACE),
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000538 p_ser.CurrentAllocationAddress(CELL_SPACE));
539
540 startup_sink.WriteSpaceUsed(
541 startup_serializer.CurrentAllocationAddress(NEW_SPACE),
542 startup_serializer.CurrentAllocationAddress(OLD_POINTER_SPACE),
543 startup_serializer.CurrentAllocationAddress(OLD_DATA_SPACE),
544 startup_serializer.CurrentAllocationAddress(CODE_SPACE),
545 startup_serializer.CurrentAllocationAddress(MAP_SPACE),
546 startup_serializer.CurrentAllocationAddress(CELL_SPACE));
547 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000548 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000549}
550
551
552DEPENDENT_TEST(ContextDeserialization, ContextSerialization) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000553 if (!Snapshot::HaveASnapshotToStartFrom()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000554 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
555 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
556 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
557
558 CHECK(Snapshot::Initialize(startup_name.start()));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000559 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000560
561 const char* file_name = FLAG_testing_serialization_file;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000562
563 int snapshot_size = 0;
564 byte* snapshot = ReadBytes(file_name, &snapshot_size);
565
566 Object* root;
567 {
568 SnapshotByteSource source(snapshot, snapshot_size);
569 Deserializer deserializer(&source);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000570 ReserveSpaceForSnapshot(&deserializer, file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000571 deserializer.DeserializePartial(&root);
572 CHECK(root->IsContext());
573 }
svenpanne@chromium.org2bda5432013-03-15 12:39:50 +0000574 v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
ulan@chromium.org09d7ab52013-02-25 15:50:35 +0000575 Handle<Object> root_handle(root, Isolate::Current());
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +0000576
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000577
578 Object* root2;
579 {
580 SnapshotByteSource source(snapshot, snapshot_size);
581 Deserializer deserializer(&source);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000582 ReserveSpaceForSnapshot(&deserializer, file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000583 deserializer.DeserializePartial(&root2);
584 CHECK(root2->IsContext());
585 CHECK(*root_handle != root2);
586 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000587 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000588}
589
590
ager@chromium.org3811b432009-10-28 14:53:37 +0000591TEST(TestThatAlwaysSucceeds) {
592}
593
594
595TEST(TestThatAlwaysFails) {
596 bool ArtificialFailure = false;
597 CHECK(ArtificialFailure);
598}
599
600
601DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
602 bool ArtificialFailure2 = false;
603 CHECK(ArtificialFailure2);
604}