blob: 54e69a1d54c6b7b22fc033b93f6405fc1af9bec5 [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) {
86 return ExternalReference(id).address();
87}
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
101static int register_code(int reg) {
102 return Debug::k_register_address << kDebugIdShift | reg;
103}
104
105
106TEST(ExternalReferenceEncoder) {
107 StatsTable::SetCounterFunction(counter_function);
108 Heap::Setup(false);
109 ExternalReferenceEncoder encoder;
110 CHECK_EQ(make_code(BUILTIN, Builtins::ArrayCode),
111 Encode(encoder, Builtins::ArrayCode));
112 CHECK_EQ(make_code(RUNTIME_FUNCTION, Runtime::kAbort),
113 Encode(encoder, Runtime::kAbort));
114 CHECK_EQ(make_code(IC_UTILITY, IC::kLoadCallbackProperty),
115 Encode(encoder, IC_Utility(IC::kLoadCallbackProperty)));
116 CHECK_EQ(make_code(DEBUG_ADDRESS, register_code(3)),
117 Encode(encoder, Debug_Address(Debug::k_register_address, 3)));
118 ExternalReference keyed_load_function_prototype =
119 ExternalReference(&Counters::keyed_load_function_prototype);
120 CHECK_EQ(make_code(STATS_COUNTER, Counters::k_keyed_load_function_prototype),
121 encoder.Encode(keyed_load_function_prototype.address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000122 ExternalReference the_hole_value_location =
123 ExternalReference::the_hole_value_location();
124 CHECK_EQ(make_code(UNCLASSIFIED, 2),
125 encoder.Encode(the_hole_value_location.address()));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000126 ExternalReference stack_limit_address =
127 ExternalReference::address_of_stack_limit();
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000128 CHECK_EQ(make_code(UNCLASSIFIED, 4),
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000129 encoder.Encode(stack_limit_address.address()));
130 ExternalReference real_stack_limit_address =
131 ExternalReference::address_of_real_stack_limit();
132 CHECK_EQ(make_code(UNCLASSIFIED, 5),
133 encoder.Encode(real_stack_limit_address.address()));
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000134 CHECK_EQ(make_code(UNCLASSIFIED, 12),
ager@chromium.org32912102009-01-16 10:38:43 +0000135 encoder.Encode(ExternalReference::debug_break().address()));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000136 CHECK_EQ(make_code(UNCLASSIFIED, 7),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000137 encoder.Encode(ExternalReference::new_space_start().address()));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000138 CHECK_EQ(make_code(UNCLASSIFIED, 3),
139 encoder.Encode(ExternalReference::roots_address().address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000140}
141
142
143TEST(ExternalReferenceDecoder) {
144 StatsTable::SetCounterFunction(counter_function);
145 Heap::Setup(false);
146 ExternalReferenceDecoder decoder;
147 CHECK_EQ(AddressOf(Builtins::ArrayCode),
148 decoder.Decode(make_code(BUILTIN, Builtins::ArrayCode)));
149 CHECK_EQ(AddressOf(Runtime::kAbort),
150 decoder.Decode(make_code(RUNTIME_FUNCTION, Runtime::kAbort)));
151 CHECK_EQ(AddressOf(IC_Utility(IC::kLoadCallbackProperty)),
152 decoder.Decode(make_code(IC_UTILITY, IC::kLoadCallbackProperty)));
153 CHECK_EQ(AddressOf(Debug_Address(Debug::k_register_address, 3)),
154 decoder.Decode(make_code(DEBUG_ADDRESS, register_code(3))));
155 ExternalReference keyed_load_function =
156 ExternalReference(&Counters::keyed_load_function_prototype);
157 CHECK_EQ(keyed_load_function.address(),
158 decoder.Decode(
159 make_code(STATS_COUNTER,
160 Counters::k_keyed_load_function_prototype)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000161 CHECK_EQ(ExternalReference::the_hole_value_location().address(),
162 decoder.Decode(make_code(UNCLASSIFIED, 2)));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000163 CHECK_EQ(ExternalReference::address_of_stack_limit().address(),
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000164 decoder.Decode(make_code(UNCLASSIFIED, 4)));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000165 CHECK_EQ(ExternalReference::address_of_real_stack_limit().address(),
166 decoder.Decode(make_code(UNCLASSIFIED, 5)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000167 CHECK_EQ(ExternalReference::debug_break().address(),
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000168 decoder.Decode(make_code(UNCLASSIFIED, 12)));
ager@chromium.org32912102009-01-16 10:38:43 +0000169 CHECK_EQ(ExternalReference::new_space_start().address(),
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000170 decoder.Decode(make_code(UNCLASSIFIED, 7)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000171}
172
173
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000174class FileByteSink : public SnapshotByteSink {
175 public:
176 explicit FileByteSink(const char* snapshot_file) {
177 fp_ = OS::FOpen(snapshot_file, "wb");
178 file_name_ = snapshot_file;
179 if (fp_ == NULL) {
180 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
181 exit(1);
182 }
183 }
184 virtual ~FileByteSink() {
185 if (fp_ != NULL) {
186 fclose(fp_);
187 }
188 }
189 virtual void Put(int byte, const char* description) {
190 if (fp_ != NULL) {
191 fputc(byte, fp_);
192 }
193 }
194 virtual int Position() {
195 return ftell(fp_);
196 }
197 void WriteSpaceUsed(
198 int new_space_used,
199 int pointer_space_used,
200 int data_space_used,
201 int code_space_used,
202 int map_space_used,
203 int cell_space_used,
204 int large_space_used);
205
206 private:
207 FILE* fp_;
208 const char* file_name_;
209};
210
211
212void FileByteSink::WriteSpaceUsed(
213 int new_space_used,
214 int pointer_space_used,
215 int data_space_used,
216 int code_space_used,
217 int map_space_used,
218 int cell_space_used,
219 int large_space_used) {
220 int file_name_length = StrLength(file_name_) + 10;
221 Vector<char> name = Vector<char>::New(file_name_length + 1);
222 OS::SNPrintF(name, "%s.size", file_name_);
223 FILE* fp = OS::FOpen(name.start(), "w");
224 fprintf(fp, "new %d\n", new_space_used);
225 fprintf(fp, "pointer %d\n", pointer_space_used);
226 fprintf(fp, "data %d\n", data_space_used);
227 fprintf(fp, "code %d\n", code_space_used);
228 fprintf(fp, "map %d\n", map_space_used);
229 fprintf(fp, "cell %d\n", cell_space_used);
230 fprintf(fp, "large %d\n", large_space_used);
231 fclose(fp);
232}
233
234
235static bool WriteToFile(const char* snapshot_file) {
236 FileByteSink file(snapshot_file);
237 StartupSerializer ser(&file);
238 ser.Serialize();
239 return true;
240}
241
242
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000243static void Serialize() {
ager@chromium.org3811b432009-10-28 14:53:37 +0000244 // We have to create one context. One reason for this is so that the builtins
245 // can be loaded from v8natives.js and their addresses can be processed. This
246 // will clear the pending fixups array, which would otherwise contain GC roots
247 // that would confuse the serialization/deserialization process.
248 v8::Persistent<v8::Context> env = v8::Context::New();
249 env.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000250 WriteToFile(FLAG_testing_serialization_file);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000251}
252
253
ager@chromium.org3811b432009-10-28 14:53:37 +0000254// Test that the whole heap can be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000255TEST(Serialize) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000256 Serializer::Enable();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000257 v8::V8::Initialize();
258 Serialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000259}
260
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000261
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000262// Test that heap serialization is non-destructive.
263TEST(SerializeTwice) {
264 Serializer::Enable();
265 v8::V8::Initialize();
266 Serialize();
267 Serialize();
268}
269
270
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000271//----------------------------------------------------------------------------
272// Tests that the heap can be deserialized.
273
274static void Deserialize() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000275 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000276}
277
278
279static void SanityCheck() {
280 v8::HandleScope scope;
281#ifdef DEBUG
282 Heap::Verify();
283#endif
284 CHECK(Top::global()->IsJSObject());
285 CHECK(Top::global_context()->IsContext());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000286 CHECK(Heap::symbol_table()->IsSymbolTable());
287 CHECK(!Factory::LookupAsciiSymbol("Empty")->IsFailure());
288}
289
290
iposva@chromium.org245aa852009-02-10 00:49:54 +0000291DEPENDENT_TEST(Deserialize, Serialize) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000292 // The serialize-deserialize tests only work if the VM is built without
293 // serialization. That doesn't matter. We don't need to be able to
294 // serialize a snapshot in a VM that is booted from a snapshot.
295 if (!Snapshot::IsEnabled()) {
296 v8::HandleScope scope;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000297
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000298 Deserialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000299
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000300 v8::Persistent<v8::Context> env = v8::Context::New();
301 env->Enter();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000302
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000303 SanityCheck();
304 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000305}
306
307
308DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000309 if (!Snapshot::IsEnabled()) {
310 v8::HandleScope scope;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000311
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000312 Deserialize();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000313
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000314 v8::Persistent<v8::Context> env = v8::Context::New();
315 env->Enter();
ager@chromium.org3811b432009-10-28 14:53:37 +0000316
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000317 SanityCheck();
318 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000319}
320
321
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000322DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000323 if (!Snapshot::IsEnabled()) {
324 v8::HandleScope scope;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000325
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000326 Deserialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000327
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000328 v8::Persistent<v8::Context> env = v8::Context::New();
329 env->Enter();
ager@chromium.org3811b432009-10-28 14:53:37 +0000330
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000331 const char* c_source = "\"1234\".length";
332 v8::Local<v8::String> source = v8::String::New(c_source);
333 v8::Local<v8::Script> script = v8::Script::Compile(source);
334 CHECK_EQ(4, script->Run()->Int32Value());
335 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000336}
337
338
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000339DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
340 SerializeTwice) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000341 if (!Snapshot::IsEnabled()) {
342 v8::HandleScope scope;
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000343
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000344 Deserialize();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000345
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000346 v8::Persistent<v8::Context> env = v8::Context::New();
347 env->Enter();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000348
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000349 const char* c_source = "\"1234\".length";
350 v8::Local<v8::String> source = v8::String::New(c_source);
351 v8::Local<v8::Script> script = v8::Script::Compile(source);
352 CHECK_EQ(4, script->Run()->Int32Value());
353 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000354}
355
356
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000357TEST(PartialSerialization) {
358 Serializer::Enable();
359 v8::V8::Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000360
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000361 v8::Persistent<v8::Context> env = v8::Context::New();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000362 ASSERT(!env.IsEmpty());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000363 env->Enter();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000364 // Make sure all builtin scripts are cached.
365 { HandleScope scope;
366 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
367 Bootstrapper::NativesSourceLookup(i);
368 }
369 }
370 Heap::CollectAllGarbage(true);
371 Heap::CollectAllGarbage(true);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000372
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000373 Object* raw_foo;
374 {
375 v8::HandleScope handle_scope;
376 v8::Local<v8::String> foo = v8::String::New("foo");
377 ASSERT(!foo.IsEmpty());
378 raw_foo = *(v8::Utils::OpenHandle(*foo));
379 }
380
381 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
382 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
383 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
384
385 env->Exit();
386 env.Dispose();
387
388 FileByteSink startup_sink(startup_name.start());
389 StartupSerializer startup_serializer(&startup_sink);
390 startup_serializer.SerializeStrongReferences();
391
392 FileByteSink partial_sink(FLAG_testing_serialization_file);
393 PartialSerializer p_ser(&startup_serializer, &partial_sink);
394 p_ser.Serialize(&raw_foo);
395 startup_serializer.SerializeWeakReferences();
396 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE),
397 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
398 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
399 p_ser.CurrentAllocationAddress(CODE_SPACE),
400 p_ser.CurrentAllocationAddress(MAP_SPACE),
401 p_ser.CurrentAllocationAddress(CELL_SPACE),
402 p_ser.CurrentAllocationAddress(LO_SPACE));
403}
404
405
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000406static void ReserveSpaceForPartialSnapshot(const char* file_name) {
407 int file_name_length = StrLength(file_name) + 10;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000408 Vector<char> name = Vector<char>::New(file_name_length + 1);
409 OS::SNPrintF(name, "%s.size", file_name);
410 FILE* fp = OS::FOpen(name.start(), "r");
411 int new_size, pointer_size, data_size, code_size, map_size, cell_size;
412 int large_size;
413#ifdef _MSC_VER
414 // Avoid warning about unsafe fscanf from MSVC.
415 // Please note that this is only fine if %c and %s are not being used.
416#define fscanf fscanf_s
417#endif
418 CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size));
419 CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size));
420 CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size));
421 CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size));
422 CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size));
423 CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size));
424 CHECK_EQ(1, fscanf(fp, "large %d\n", &large_size));
425#ifdef _MSC_VER
426#undef fscanf
427#endif
428 fclose(fp);
429 Heap::ReserveSpace(new_size,
430 pointer_size,
431 data_size,
432 code_size,
433 map_size,
434 cell_size,
435 large_size);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000436}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000437
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000438
439DEPENDENT_TEST(PartialDeserialization, PartialSerialization) {
440 if (!Snapshot::IsEnabled()) {
441 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
442 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
443 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
444
445 CHECK(Snapshot::Initialize(startup_name.start()));
446
447 const char* file_name = FLAG_testing_serialization_file;
448 ReserveSpaceForPartialSnapshot(file_name);
449
450 int snapshot_size = 0;
451 byte* snapshot = ReadBytes(file_name, &snapshot_size);
452
453 Object* root;
454 {
455 SnapshotByteSource source(snapshot, snapshot_size);
456 Deserializer deserializer(&source);
457 deserializer.DeserializePartial(&root);
458 CHECK(root->IsString());
459 }
460 v8::HandleScope handle_scope;
461 Handle<Object>root_handle(root);
462
463 Object* root2;
464 {
465 SnapshotByteSource source(snapshot, snapshot_size);
466 Deserializer deserializer(&source);
467 deserializer.DeserializePartial(&root2);
468 CHECK(root2->IsString());
469 CHECK(*root_handle == root2);
470 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000471 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000472}
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000473
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000474
475TEST(ContextSerialization) {
476 Serializer::Enable();
477 v8::V8::Initialize();
478
479 v8::Persistent<v8::Context> env = v8::Context::New();
480 ASSERT(!env.IsEmpty());
481 env->Enter();
482 // Make sure all builtin scripts are cached.
483 { HandleScope scope;
484 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
485 Bootstrapper::NativesSourceLookup(i);
486 }
487 }
488 // If we don't do this then we end up with a stray root pointing at the
489 // context even after we have disposed of env.
490 Heap::CollectAllGarbage(true);
491
492 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
493 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
494 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
495
496 env->Exit();
497
498 Object* raw_context = *(v8::Utils::OpenHandle(*env));
499
500 env.Dispose();
501
502 FileByteSink startup_sink(startup_name.start());
503 StartupSerializer startup_serializer(&startup_sink);
504 startup_serializer.SerializeStrongReferences();
505
506 FileByteSink partial_sink(FLAG_testing_serialization_file);
507 PartialSerializer p_ser(&startup_serializer, &partial_sink);
508 p_ser.Serialize(&raw_context);
509 startup_serializer.SerializeWeakReferences();
510 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE),
511 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
512 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
513 p_ser.CurrentAllocationAddress(CODE_SPACE),
514 p_ser.CurrentAllocationAddress(MAP_SPACE),
515 p_ser.CurrentAllocationAddress(CELL_SPACE),
516 p_ser.CurrentAllocationAddress(LO_SPACE));
517}
518
519
520DEPENDENT_TEST(ContextDeserialization, ContextSerialization) {
521 if (!Snapshot::IsEnabled()) {
522 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
523 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
524 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
525
526 CHECK(Snapshot::Initialize(startup_name.start()));
527
528 const char* file_name = FLAG_testing_serialization_file;
529 ReserveSpaceForPartialSnapshot(file_name);
530
531 int snapshot_size = 0;
532 byte* snapshot = ReadBytes(file_name, &snapshot_size);
533
534 Object* root;
535 {
536 SnapshotByteSource source(snapshot, snapshot_size);
537 Deserializer deserializer(&source);
538 deserializer.DeserializePartial(&root);
539 CHECK(root->IsContext());
540 }
541 v8::HandleScope handle_scope;
542 Handle<Object>root_handle(root);
543
544 Object* root2;
545 {
546 SnapshotByteSource source(snapshot, snapshot_size);
547 Deserializer deserializer(&source);
548 deserializer.DeserializePartial(&root2);
549 CHECK(root2->IsContext());
550 CHECK(*root_handle != root2);
551 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000552 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000553}
554
555
556TEST(LinearAllocation) {
557 v8::V8::Initialize();
558 int new_space_max = 512 * KB;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000559
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000560 for (int size = 1000; size < 5 * MB; size += size >> 1) {
561 int new_space_size = (size < new_space_max) ? size : new_space_max;
562 Heap::ReserveSpace(
563 new_space_size,
564 size, // Old pointer space.
565 size, // Old data space.
566 size, // Code space.
567 size, // Map space.
568 size, // Cell space.
569 size); // Large object space.
570 LinearAllocationScope linear_allocation_scope;
571 const int kSmallFixedArrayLength = 4;
572 const int kSmallFixedArraySize =
573 FixedArray::kHeaderSize + kSmallFixedArrayLength * kPointerSize;
574 const int kSmallStringLength = 16;
575 const int kSmallStringSize =
kmillikin@chromium.orgf8253d72010-05-03 09:56:08 +0000576 SeqAsciiString::kHeaderSize + kSmallStringLength;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000577 const int kMapSize = Map::kSize;
578
579 Object* new_last = NULL;
580 for (int i = 0;
581 i + kSmallFixedArraySize <= new_space_size;
582 i += kSmallFixedArraySize) {
583 Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength);
584 if (new_last != NULL) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000585 CHECK(reinterpret_cast<char*>(obj) ==
586 reinterpret_cast<char*>(new_last) + kSmallFixedArraySize);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000587 }
588 new_last = obj;
589 }
590
591 Object* pointer_last = NULL;
592 for (int i = 0;
593 i + kSmallFixedArraySize <= size;
594 i += kSmallFixedArraySize) {
595 Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength, TENURED);
596 int old_page_fullness = i % Page::kPageSize;
597 int page_fullness = (i + kSmallFixedArraySize) % Page::kPageSize;
598 if (page_fullness < old_page_fullness ||
599 page_fullness > Page::kObjectAreaSize) {
600 i = RoundUp(i, Page::kPageSize);
601 pointer_last = NULL;
602 }
603 if (pointer_last != NULL) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000604 CHECK(reinterpret_cast<char*>(obj) ==
605 reinterpret_cast<char*>(pointer_last) + kSmallFixedArraySize);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000606 }
607 pointer_last = obj;
608 }
609
610 Object* data_last = NULL;
611 for (int i = 0; i + kSmallStringSize <= size; i += kSmallStringSize) {
612 Object* obj = Heap::AllocateRawAsciiString(kSmallStringLength, TENURED);
613 int old_page_fullness = i % Page::kPageSize;
614 int page_fullness = (i + kSmallStringSize) % Page::kPageSize;
615 if (page_fullness < old_page_fullness ||
616 page_fullness > Page::kObjectAreaSize) {
617 i = RoundUp(i, Page::kPageSize);
618 data_last = NULL;
619 }
620 if (data_last != NULL) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000621 CHECK(reinterpret_cast<char*>(obj) ==
622 reinterpret_cast<char*>(data_last) + kSmallStringSize);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000623 }
624 data_last = obj;
625 }
626
627 Object* map_last = NULL;
628 for (int i = 0; i + kMapSize <= size; i += kMapSize) {
629 Object* obj = Heap::AllocateMap(JS_OBJECT_TYPE, 42 * kPointerSize);
630 int old_page_fullness = i % Page::kPageSize;
631 int page_fullness = (i + kMapSize) % Page::kPageSize;
632 if (page_fullness < old_page_fullness ||
633 page_fullness > Page::kObjectAreaSize) {
634 i = RoundUp(i, Page::kPageSize);
635 map_last = NULL;
636 }
637 if (map_last != NULL) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000638 CHECK(reinterpret_cast<char*>(obj) ==
639 reinterpret_cast<char*>(map_last) + kMapSize);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000640 }
641 map_last = obj;
642 }
643
644 if (size > Page::kObjectAreaSize) {
645 // Support for reserving space in large object space is not there yet,
646 // but using an always-allocate scope is fine for now.
647 AlwaysAllocateScope always;
648 int large_object_array_length =
649 (size - FixedArray::kHeaderSize) / kPointerSize;
650 Object* obj = Heap::AllocateFixedArray(large_object_array_length,
651 TENURED);
652 CHECK(!obj->IsFailure());
653 }
654 }
655}
656
657
ager@chromium.org3811b432009-10-28 14:53:37 +0000658TEST(TestThatAlwaysSucceeds) {
659}
660
661
662TEST(TestThatAlwaysFails) {
663 bool ArtificialFailure = false;
664 CHECK(ArtificialFailure);
665}
666
667
668DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
669 bool ArtificialFailure2 = false;
670 CHECK(ArtificialFailure2);
671}