blob: c309944f3d5499f75d338714d1664f706e031cb3 [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));
111 CHECK_EQ(make_code(IC_UTILITY, IC::kLoadCallbackProperty),
112 Encode(encoder, IC_Utility(IC::kLoadCallbackProperty)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000113 ExternalReference keyed_load_function_prototype =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000114 ExternalReference(isolate->counters()->keyed_load_function_prototype());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000115 CHECK_EQ(make_code(STATS_COUNTER, Counters::k_keyed_load_function_prototype),
116 encoder.Encode(keyed_load_function_prototype.address()));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000117 ExternalReference stack_limit_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 ExternalReference::address_of_stack_limit(isolate);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000119 CHECK_EQ(make_code(UNCLASSIFIED, 4),
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000120 encoder.Encode(stack_limit_address.address()));
121 ExternalReference real_stack_limit_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000122 ExternalReference::address_of_real_stack_limit(isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000123 CHECK_EQ(make_code(UNCLASSIFIED, 5),
124 encoder.Encode(real_stack_limit_address.address()));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000125#ifdef ENABLE_DEBUGGER_SUPPORT
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000126 CHECK_EQ(make_code(UNCLASSIFIED, 16),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000127 encoder.Encode(ExternalReference::debug_break(isolate).address()));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000128#endif // ENABLE_DEBUGGER_SUPPORT
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000129 CHECK_EQ(make_code(UNCLASSIFIED, 10),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000130 encoder.Encode(
131 ExternalReference::new_space_start(isolate).address()));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000132 CHECK_EQ(make_code(UNCLASSIFIED, 3),
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000133 encoder.Encode(
134 ExternalReference::roots_array_start(isolate).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 CHECK_EQ(AddressOf(IC_Utility(IC::kLoadCallbackProperty)),
150 decoder.Decode(make_code(IC_UTILITY, IC::kLoadCallbackProperty)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000151 ExternalReference keyed_load_function =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000152 ExternalReference(isolate->counters()->keyed_load_function_prototype());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000153 CHECK_EQ(keyed_load_function.address(),
154 decoder.Decode(
155 make_code(STATS_COUNTER,
156 Counters::k_keyed_load_function_prototype)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000157 CHECK_EQ(ExternalReference::address_of_stack_limit(isolate).address(),
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000158 decoder.Decode(make_code(UNCLASSIFIED, 4)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000159 CHECK_EQ(ExternalReference::address_of_real_stack_limit(isolate).address(),
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000160 decoder.Decode(make_code(UNCLASSIFIED, 5)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000161#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000162 CHECK_EQ(ExternalReference::debug_break(isolate).address(),
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000163 decoder.Decode(make_code(UNCLASSIFIED, 16)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000164#endif // ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000165 CHECK_EQ(ExternalReference::new_space_start(isolate).address(),
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000166 decoder.Decode(make_code(UNCLASSIFIED, 10)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000167}
168
169
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000170class FileByteSink : public SnapshotByteSink {
171 public:
172 explicit FileByteSink(const char* snapshot_file) {
173 fp_ = OS::FOpen(snapshot_file, "wb");
174 file_name_ = snapshot_file;
175 if (fp_ == NULL) {
176 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
177 exit(1);
178 }
179 }
180 virtual ~FileByteSink() {
181 if (fp_ != NULL) {
182 fclose(fp_);
183 }
184 }
185 virtual void Put(int byte, const char* description) {
186 if (fp_ != NULL) {
187 fputc(byte, fp_);
188 }
189 }
190 virtual int Position() {
191 return ftell(fp_);
192 }
193 void WriteSpaceUsed(
194 int new_space_used,
195 int pointer_space_used,
196 int data_space_used,
197 int code_space_used,
198 int map_space_used,
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000199 int cell_space_used);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000200
201 private:
202 FILE* fp_;
203 const char* file_name_;
204};
205
206
207void FileByteSink::WriteSpaceUsed(
208 int new_space_used,
209 int pointer_space_used,
210 int data_space_used,
211 int code_space_used,
212 int map_space_used,
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000213 int cell_space_used) {
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000214 int file_name_length = StrLength(file_name_) + 10;
215 Vector<char> name = Vector<char>::New(file_name_length + 1);
216 OS::SNPrintF(name, "%s.size", file_name_);
217 FILE* fp = OS::FOpen(name.start(), "w");
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000218 name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000219 fprintf(fp, "new %d\n", new_space_used);
220 fprintf(fp, "pointer %d\n", pointer_space_used);
221 fprintf(fp, "data %d\n", data_space_used);
222 fprintf(fp, "code %d\n", code_space_used);
223 fprintf(fp, "map %d\n", map_space_used);
224 fprintf(fp, "cell %d\n", cell_space_used);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000225 fclose(fp);
226}
227
228
229static bool WriteToFile(const char* snapshot_file) {
230 FileByteSink file(snapshot_file);
231 StartupSerializer ser(&file);
232 ser.Serialize();
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000233
234 file.WriteSpaceUsed(
235 ser.CurrentAllocationAddress(NEW_SPACE),
236 ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
237 ser.CurrentAllocationAddress(OLD_DATA_SPACE),
238 ser.CurrentAllocationAddress(CODE_SPACE),
239 ser.CurrentAllocationAddress(MAP_SPACE),
240 ser.CurrentAllocationAddress(CELL_SPACE));
241
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000242 return true;
243}
244
245
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000246static void Serialize() {
ager@chromium.org3811b432009-10-28 14:53:37 +0000247 // We have to create one context. One reason for this is so that the builtins
248 // can be loaded from v8natives.js and their addresses can be processed. This
249 // will clear the pending fixups array, which would otherwise contain GC roots
250 // that would confuse the serialization/deserialization process.
251 v8::Persistent<v8::Context> env = v8::Context::New();
252 env.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000253 WriteToFile(FLAG_testing_serialization_file);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000254}
255
256
ager@chromium.org3811b432009-10-28 14:53:37 +0000257// Test that the whole heap can be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000258TEST(Serialize) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000259 if (!Snapshot::HaveASnapshotToStartFrom()) {
260 Serializer::Enable();
261 v8::V8::Initialize();
262 Serialize();
263 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000264}
265
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000266
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000267// Test that heap serialization is non-destructive.
268TEST(SerializeTwice) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000269 if (!Snapshot::HaveASnapshotToStartFrom()) {
270 Serializer::Enable();
271 v8::V8::Initialize();
272 Serialize();
273 Serialize();
274 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000275}
276
277
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000278//----------------------------------------------------------------------------
279// Tests that the heap can be deserialized.
280
281static void Deserialize() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000282 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000283}
284
285
286static void SanityCheck() {
287 v8::HandleScope scope;
288#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000289 HEAP->Verify();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000290#endif
yangguo@chromium.org46839fb2012-08-28 09:06:19 +0000291 CHECK(Isolate::Current()->global_object()->IsJSObject());
292 CHECK(Isolate::Current()->native_context()->IsContext());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000293 CHECK(HEAP->symbol_table()->IsSymbolTable());
294 CHECK(!FACTORY->LookupAsciiSymbol("Empty")->IsFailure());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000295}
296
297
iposva@chromium.org245aa852009-02-10 00:49:54 +0000298DEPENDENT_TEST(Deserialize, Serialize) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000299 // The serialize-deserialize tests only work if the VM is built without
300 // serialization. That doesn't matter. We don't need to be able to
301 // serialize a snapshot in a VM that is booted from a snapshot.
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000302 if (!Snapshot::HaveASnapshotToStartFrom()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000303 v8::HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000304 Deserialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000305
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000306 v8::Persistent<v8::Context> env = v8::Context::New();
307 env->Enter();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000308
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000309 SanityCheck();
310 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000311}
312
313
314DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000315 if (!Snapshot::HaveASnapshotToStartFrom()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000316 v8::HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000317 Deserialize();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000318
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000319 v8::Persistent<v8::Context> env = v8::Context::New();
320 env->Enter();
ager@chromium.org3811b432009-10-28 14:53:37 +0000321
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000322 SanityCheck();
323 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000324}
325
326
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000327DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000328 if (!Snapshot::HaveASnapshotToStartFrom()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000329 v8::HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000330 Deserialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000331
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000332 v8::Persistent<v8::Context> env = v8::Context::New();
333 env->Enter();
ager@chromium.org3811b432009-10-28 14:53:37 +0000334
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000335 const char* c_source = "\"1234\".length";
336 v8::Local<v8::String> source = v8::String::New(c_source);
337 v8::Local<v8::Script> script = v8::Script::Compile(source);
338 CHECK_EQ(4, script->Run()->Int32Value());
339 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000340}
341
342
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000343DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
344 SerializeTwice) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000345 if (!Snapshot::HaveASnapshotToStartFrom()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000346 v8::HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000347 Deserialize();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000348
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000349 v8::Persistent<v8::Context> env = v8::Context::New();
350 env->Enter();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000351
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000352 const char* c_source = "\"1234\".length";
353 v8::Local<v8::String> source = v8::String::New(c_source);
354 v8::Local<v8::Script> script = v8::Script::Compile(source);
355 CHECK_EQ(4, script->Run()->Int32Value());
356 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000357}
358
359
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000360TEST(PartialSerialization) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000361 if (!Snapshot::HaveASnapshotToStartFrom()) {
362 Serializer::Enable();
363 v8::V8::Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000364
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000365 v8::Persistent<v8::Context> env = v8::Context::New();
366 ASSERT(!env.IsEmpty());
367 env->Enter();
368 // Make sure all builtin scripts are cached.
369 { HandleScope scope;
370 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
371 Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
372 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000373 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000374 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
375 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
376
377 Object* raw_foo;
378 {
379 v8::HandleScope handle_scope;
380 v8::Local<v8::String> foo = v8::String::New("foo");
381 ASSERT(!foo.IsEmpty());
382 raw_foo = *(v8::Utils::OpenHandle(*foo));
383 }
384
385 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
386 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
387 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
388
389 env->Exit();
390 env.Dispose();
391
392 FileByteSink startup_sink(startup_name.start());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000393 StartupSerializer startup_serializer(&startup_sink);
394 startup_serializer.SerializeStrongReferences();
395
396 FileByteSink partial_sink(FLAG_testing_serialization_file);
397 PartialSerializer p_ser(&startup_serializer, &partial_sink);
398 p_ser.Serialize(&raw_foo);
399 startup_serializer.SerializeWeakReferences();
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000400
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000401 partial_sink.WriteSpaceUsed(
402 p_ser.CurrentAllocationAddress(NEW_SPACE),
403 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
404 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
405 p_ser.CurrentAllocationAddress(CODE_SPACE),
406 p_ser.CurrentAllocationAddress(MAP_SPACE),
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000407 p_ser.CurrentAllocationAddress(CELL_SPACE));
408
409 startup_sink.WriteSpaceUsed(
410 startup_serializer.CurrentAllocationAddress(NEW_SPACE),
411 startup_serializer.CurrentAllocationAddress(OLD_POINTER_SPACE),
412 startup_serializer.CurrentAllocationAddress(OLD_DATA_SPACE),
413 startup_serializer.CurrentAllocationAddress(CODE_SPACE),
414 startup_serializer.CurrentAllocationAddress(MAP_SPACE),
415 startup_serializer.CurrentAllocationAddress(CELL_SPACE));
416 startup_name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000417 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000418}
419
420
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000421static void ReserveSpaceForSnapshot(Deserializer* deserializer,
422 const char* file_name) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000423 int file_name_length = StrLength(file_name) + 10;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000424 Vector<char> name = Vector<char>::New(file_name_length + 1);
425 OS::SNPrintF(name, "%s.size", file_name);
426 FILE* fp = OS::FOpen(name.start(), "r");
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000427 name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000428 int new_size, pointer_size, data_size, code_size, map_size, cell_size;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000429#ifdef _MSC_VER
430 // Avoid warning about unsafe fscanf from MSVC.
431 // Please note that this is only fine if %c and %s are not being used.
432#define fscanf fscanf_s
433#endif
434 CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size));
435 CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size));
436 CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size));
437 CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size));
438 CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size));
439 CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size));
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000440#ifdef _MSC_VER
441#undef fscanf
442#endif
443 fclose(fp);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000444 deserializer->set_reservation(NEW_SPACE, new_size);
445 deserializer->set_reservation(OLD_POINTER_SPACE, pointer_size);
446 deserializer->set_reservation(OLD_DATA_SPACE, data_size);
447 deserializer->set_reservation(CODE_SPACE, code_size);
448 deserializer->set_reservation(MAP_SPACE, map_size);
449 deserializer->set_reservation(CELL_SPACE, cell_size);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000450}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000451
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000452
453DEPENDENT_TEST(PartialDeserialization, PartialSerialization) {
454 if (!Snapshot::IsEnabled()) {
455 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
456 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
457 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
458
459 CHECK(Snapshot::Initialize(startup_name.start()));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000460 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000461
462 const char* file_name = FLAG_testing_serialization_file;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000463
464 int snapshot_size = 0;
465 byte* snapshot = ReadBytes(file_name, &snapshot_size);
466
467 Object* root;
468 {
469 SnapshotByteSource source(snapshot, snapshot_size);
470 Deserializer deserializer(&source);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000471 ReserveSpaceForSnapshot(&deserializer, file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000472 deserializer.DeserializePartial(&root);
473 CHECK(root->IsString());
474 }
475 v8::HandleScope handle_scope;
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +0000476 Handle<Object> root_handle(root);
477
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000478
479 Object* root2;
480 {
481 SnapshotByteSource source(snapshot, snapshot_size);
482 Deserializer deserializer(&source);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000483 ReserveSpaceForSnapshot(&deserializer, file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000484 deserializer.DeserializePartial(&root2);
485 CHECK(root2->IsString());
486 CHECK(*root_handle == root2);
487 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000488 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000489}
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000490
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000491
492TEST(ContextSerialization) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000493 if (!Snapshot::HaveASnapshotToStartFrom()) {
494 Serializer::Enable();
495 v8::V8::Initialize();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000496
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000497 v8::Persistent<v8::Context> env = v8::Context::New();
498 ASSERT(!env.IsEmpty());
499 env->Enter();
500 // Make sure all builtin scripts are cached.
501 { HandleScope scope;
502 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
503 Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
504 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000505 }
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000506 // If we don't do this then we end up with a stray root pointing at the
507 // context even after we have disposed of env.
508 HEAP->CollectAllGarbage(Heap::kNoGCFlags);
509
510 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
511 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
512 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
513
514 env->Exit();
515
516 Object* raw_context = *(v8::Utils::OpenHandle(*env));
517
518 env.Dispose();
519
520 FileByteSink startup_sink(startup_name.start());
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000521 StartupSerializer startup_serializer(&startup_sink);
522 startup_serializer.SerializeStrongReferences();
523
524 FileByteSink partial_sink(FLAG_testing_serialization_file);
525 PartialSerializer p_ser(&startup_serializer, &partial_sink);
526 p_ser.Serialize(&raw_context);
527 startup_serializer.SerializeWeakReferences();
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000528
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000529 partial_sink.WriteSpaceUsed(
530 p_ser.CurrentAllocationAddress(NEW_SPACE),
531 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
532 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
533 p_ser.CurrentAllocationAddress(CODE_SPACE),
534 p_ser.CurrentAllocationAddress(MAP_SPACE),
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000535 p_ser.CurrentAllocationAddress(CELL_SPACE));
536
537 startup_sink.WriteSpaceUsed(
538 startup_serializer.CurrentAllocationAddress(NEW_SPACE),
539 startup_serializer.CurrentAllocationAddress(OLD_POINTER_SPACE),
540 startup_serializer.CurrentAllocationAddress(OLD_DATA_SPACE),
541 startup_serializer.CurrentAllocationAddress(CODE_SPACE),
542 startup_serializer.CurrentAllocationAddress(MAP_SPACE),
543 startup_serializer.CurrentAllocationAddress(CELL_SPACE));
544 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000545 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000546}
547
548
549DEPENDENT_TEST(ContextDeserialization, ContextSerialization) {
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +0000550 if (!Snapshot::HaveASnapshotToStartFrom()) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000551 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
552 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
553 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
554
555 CHECK(Snapshot::Initialize(startup_name.start()));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000556 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000557
558 const char* file_name = FLAG_testing_serialization_file;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000559
560 int snapshot_size = 0;
561 byte* snapshot = ReadBytes(file_name, &snapshot_size);
562
563 Object* root;
564 {
565 SnapshotByteSource source(snapshot, snapshot_size);
566 Deserializer deserializer(&source);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000567 ReserveSpaceForSnapshot(&deserializer, file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000568 deserializer.DeserializePartial(&root);
569 CHECK(root->IsContext());
570 }
571 v8::HandleScope handle_scope;
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +0000572 Handle<Object> root_handle(root);
573
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000574
575 Object* root2;
576 {
577 SnapshotByteSource source(snapshot, snapshot_size);
578 Deserializer deserializer(&source);
ulan@chromium.org56c14af2012-09-20 12:51:09 +0000579 ReserveSpaceForSnapshot(&deserializer, file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000580 deserializer.DeserializePartial(&root2);
581 CHECK(root2->IsContext());
582 CHECK(*root_handle != root2);
583 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000584 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000585}
586
587
ager@chromium.org3811b432009-10-28 14:53:37 +0000588TEST(TestThatAlwaysSucceeds) {
589}
590
591
592TEST(TestThatAlwaysFails) {
593 bool ArtificialFailure = false;
594 CHECK(ArtificialFailure);
595}
596
597
598DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
599 bool ArtificialFailure2 = false;
600 CHECK(ArtificialFailure2);
601}