blob: 1cbaf2bf665977d0c105526b56b332dd12f1770b [file] [log] [blame]
Leon Clarkee46be812010-01-19 14:06:41 +00001// Copyright 2007-2010 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +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>
29
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"
Leon Clarkee46be812010-01-19 14:06:41 +000040#include "spaces.h"
41#include "objects.h"
Leon Clarked91b9f72010-01-27 17:25:45 +000042#include "natives.h"
43#include "bootstrapper.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044
45using namespace v8::internal;
46
47static 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}
60
61
62// Callback receiver to track counters in test.
63static int* counter_function(const char* name) {
64 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.
80 }
81}
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
Steve Blocka7e24c12009-10-30 11:49:00 +0000101TEST(ExternalReferenceEncoder) {
102 StatsTable::SetCounterFunction(counter_function);
103 Heap::Setup(false);
104 ExternalReferenceEncoder encoder;
105 CHECK_EQ(make_code(BUILTIN, Builtins::ArrayCode),
106 Encode(encoder, Builtins::ArrayCode));
107 CHECK_EQ(make_code(RUNTIME_FUNCTION, Runtime::kAbort),
108 Encode(encoder, Runtime::kAbort));
109 CHECK_EQ(make_code(IC_UTILITY, IC::kLoadCallbackProperty),
110 Encode(encoder, IC_Utility(IC::kLoadCallbackProperty)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000111 ExternalReference keyed_load_function_prototype =
112 ExternalReference(&Counters::keyed_load_function_prototype);
113 CHECK_EQ(make_code(STATS_COUNTER, Counters::k_keyed_load_function_prototype),
114 encoder.Encode(keyed_load_function_prototype.address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 ExternalReference the_hole_value_location =
116 ExternalReference::the_hole_value_location();
117 CHECK_EQ(make_code(UNCLASSIFIED, 2),
118 encoder.Encode(the_hole_value_location.address()));
Steve Blockd0582a62009-12-15 09:54:21 +0000119 ExternalReference stack_limit_address =
120 ExternalReference::address_of_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 CHECK_EQ(make_code(UNCLASSIFIED, 4),
Steve Blockd0582a62009-12-15 09:54:21 +0000122 encoder.Encode(stack_limit_address.address()));
123 ExternalReference real_stack_limit_address =
124 ExternalReference::address_of_real_stack_limit();
125 CHECK_EQ(make_code(UNCLASSIFIED, 5),
126 encoder.Encode(real_stack_limit_address.address()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100127#ifdef ENABLE_DEBUGGER_SUPPORT
128 CHECK_EQ(make_code(UNCLASSIFIED, 15),
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 encoder.Encode(ExternalReference::debug_break().address()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100130#endif // ENABLE_DEBUGGER_SUPPORT
131 CHECK_EQ(make_code(UNCLASSIFIED, 10),
Steve Blocka7e24c12009-10-30 11:49:00 +0000132 encoder.Encode(ExternalReference::new_space_start().address()));
133 CHECK_EQ(make_code(UNCLASSIFIED, 3),
134 encoder.Encode(ExternalReference::roots_address().address()));
135}
136
137
138TEST(ExternalReferenceDecoder) {
139 StatsTable::SetCounterFunction(counter_function);
140 Heap::Setup(false);
141 ExternalReferenceDecoder decoder;
142 CHECK_EQ(AddressOf(Builtins::ArrayCode),
143 decoder.Decode(make_code(BUILTIN, Builtins::ArrayCode)));
144 CHECK_EQ(AddressOf(Runtime::kAbort),
145 decoder.Decode(make_code(RUNTIME_FUNCTION, Runtime::kAbort)));
146 CHECK_EQ(AddressOf(IC_Utility(IC::kLoadCallbackProperty)),
147 decoder.Decode(make_code(IC_UTILITY, IC::kLoadCallbackProperty)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000148 ExternalReference keyed_load_function =
149 ExternalReference(&Counters::keyed_load_function_prototype);
150 CHECK_EQ(keyed_load_function.address(),
151 decoder.Decode(
152 make_code(STATS_COUNTER,
153 Counters::k_keyed_load_function_prototype)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000154 CHECK_EQ(ExternalReference::the_hole_value_location().address(),
155 decoder.Decode(make_code(UNCLASSIFIED, 2)));
Steve Blockd0582a62009-12-15 09:54:21 +0000156 CHECK_EQ(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000157 decoder.Decode(make_code(UNCLASSIFIED, 4)));
Steve Blockd0582a62009-12-15 09:54:21 +0000158 CHECK_EQ(ExternalReference::address_of_real_stack_limit().address(),
159 decoder.Decode(make_code(UNCLASSIFIED, 5)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100160#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 CHECK_EQ(ExternalReference::debug_break().address(),
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100162 decoder.Decode(make_code(UNCLASSIFIED, 15)));
163#endif // ENABLE_DEBUGGER_SUPPORT
Steve Blocka7e24c12009-10-30 11:49:00 +0000164 CHECK_EQ(ExternalReference::new_space_start().address(),
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100165 decoder.Decode(make_code(UNCLASSIFIED, 10)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000166}
167
168
Leon Clarked91b9f72010-01-27 17:25:45 +0000169class FileByteSink : public SnapshotByteSink {
170 public:
171 explicit FileByteSink(const char* snapshot_file) {
172 fp_ = OS::FOpen(snapshot_file, "wb");
173 file_name_ = snapshot_file;
174 if (fp_ == NULL) {
175 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
176 exit(1);
177 }
178 }
179 virtual ~FileByteSink() {
180 if (fp_ != NULL) {
181 fclose(fp_);
182 }
183 }
184 virtual void Put(int byte, const char* description) {
185 if (fp_ != NULL) {
186 fputc(byte, fp_);
187 }
188 }
189 virtual int Position() {
190 return ftell(fp_);
191 }
192 void WriteSpaceUsed(
193 int new_space_used,
194 int pointer_space_used,
195 int data_space_used,
196 int code_space_used,
197 int map_space_used,
198 int cell_space_used,
199 int large_space_used);
200
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,
213 int cell_space_used,
214 int large_space_used) {
215 int file_name_length = StrLength(file_name_) + 10;
216 Vector<char> name = Vector<char>::New(file_name_length + 1);
217 OS::SNPrintF(name, "%s.size", file_name_);
218 FILE* fp = OS::FOpen(name.start(), "w");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800219 name.Dispose();
Leon Clarked91b9f72010-01-27 17:25:45 +0000220 fprintf(fp, "new %d\n", new_space_used);
221 fprintf(fp, "pointer %d\n", pointer_space_used);
222 fprintf(fp, "data %d\n", data_space_used);
223 fprintf(fp, "code %d\n", code_space_used);
224 fprintf(fp, "map %d\n", map_space_used);
225 fprintf(fp, "cell %d\n", cell_space_used);
226 fprintf(fp, "large %d\n", large_space_used);
227 fclose(fp);
228}
229
230
231static bool WriteToFile(const char* snapshot_file) {
232 FileByteSink file(snapshot_file);
233 StartupSerializer ser(&file);
234 ser.Serialize();
235 return true;
236}
237
238
Steve Blocka7e24c12009-10-30 11:49:00 +0000239static void Serialize() {
Steve Blockd0582a62009-12-15 09:54:21 +0000240 // We have to create one context. One reason for this is so that the builtins
241 // can be loaded from v8natives.js and their addresses can be processed. This
242 // will clear the pending fixups array, which would otherwise contain GC roots
243 // that would confuse the serialization/deserialization process.
244 v8::Persistent<v8::Context> env = v8::Context::New();
245 env.Dispose();
Leon Clarked91b9f72010-01-27 17:25:45 +0000246 WriteToFile(FLAG_testing_serialization_file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000247}
248
249
Steve Blockd0582a62009-12-15 09:54:21 +0000250// Test that the whole heap can be serialized.
Steve Blocka7e24c12009-10-30 11:49:00 +0000251TEST(Serialize) {
Steve Blockd0582a62009-12-15 09:54:21 +0000252 Serializer::Enable();
253 v8::V8::Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000254 Serialize();
255}
256
257
Steve Blockd0582a62009-12-15 09:54:21 +0000258// Test that heap serialization is non-destructive.
259TEST(SerializeTwice) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 Serializer::Enable();
Steve Blockd0582a62009-12-15 09:54:21 +0000261 v8::V8::Initialize();
262 Serialize();
263 Serialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000264}
265
Steve Blockd0582a62009-12-15 09:54:21 +0000266
Steve Blocka7e24c12009-10-30 11:49:00 +0000267//----------------------------------------------------------------------------
268// Tests that the heap can be deserialized.
269
270static void Deserialize() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000271 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
272}
273
274
275static void SanityCheck() {
276 v8::HandleScope scope;
277#ifdef DEBUG
278 Heap::Verify();
279#endif
280 CHECK(Top::global()->IsJSObject());
281 CHECK(Top::global_context()->IsContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000282 CHECK(Heap::symbol_table()->IsSymbolTable());
283 CHECK(!Factory::LookupAsciiSymbol("Empty")->IsFailure());
284}
285
286
287DEPENDENT_TEST(Deserialize, Serialize) {
Andrei Popescu31002712010-02-23 13:46:05 +0000288 // The serialize-deserialize tests only work if the VM is built without
289 // serialization. That doesn't matter. We don't need to be able to
290 // serialize a snapshot in a VM that is booted from a snapshot.
291 if (!Snapshot::IsEnabled()) {
292 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000293
Andrei Popescu31002712010-02-23 13:46:05 +0000294 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000295
Andrei Popescu31002712010-02-23 13:46:05 +0000296 v8::Persistent<v8::Context> env = v8::Context::New();
297 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000298
Andrei Popescu31002712010-02-23 13:46:05 +0000299 SanityCheck();
300 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000301}
302
Steve Blockd0582a62009-12-15 09:54:21 +0000303
304DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
Andrei Popescu31002712010-02-23 13:46:05 +0000305 if (!Snapshot::IsEnabled()) {
306 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000307
Andrei Popescu31002712010-02-23 13:46:05 +0000308 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000309
Andrei Popescu31002712010-02-23 13:46:05 +0000310 v8::Persistent<v8::Context> env = v8::Context::New();
311 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000312
Andrei Popescu31002712010-02-23 13:46:05 +0000313 SanityCheck();
314 }
Steve Blockd0582a62009-12-15 09:54:21 +0000315}
316
317
318DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
Andrei Popescu31002712010-02-23 13:46:05 +0000319 if (!Snapshot::IsEnabled()) {
320 v8::HandleScope scope;
Steve Blockd0582a62009-12-15 09:54:21 +0000321
Andrei Popescu31002712010-02-23 13:46:05 +0000322 Deserialize();
Steve Blockd0582a62009-12-15 09:54:21 +0000323
Andrei Popescu31002712010-02-23 13:46:05 +0000324 v8::Persistent<v8::Context> env = v8::Context::New();
325 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000326
Andrei Popescu31002712010-02-23 13:46:05 +0000327 const char* c_source = "\"1234\".length";
328 v8::Local<v8::String> source = v8::String::New(c_source);
329 v8::Local<v8::Script> script = v8::Script::Compile(source);
330 CHECK_EQ(4, script->Run()->Int32Value());
331 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000332}
333
334
Steve Blockd0582a62009-12-15 09:54:21 +0000335DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
336 SerializeTwice) {
Andrei Popescu31002712010-02-23 13:46:05 +0000337 if (!Snapshot::IsEnabled()) {
338 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000339
Andrei Popescu31002712010-02-23 13:46:05 +0000340 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000341
Andrei Popescu31002712010-02-23 13:46:05 +0000342 v8::Persistent<v8::Context> env = v8::Context::New();
343 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000344
Andrei Popescu31002712010-02-23 13:46:05 +0000345 const char* c_source = "\"1234\".length";
346 v8::Local<v8::String> source = v8::String::New(c_source);
347 v8::Local<v8::Script> script = v8::Script::Compile(source);
348 CHECK_EQ(4, script->Run()->Int32Value());
349 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000350}
351
352
Leon Clarkee46be812010-01-19 14:06:41 +0000353TEST(PartialSerialization) {
354 Serializer::Enable();
355 v8::V8::Initialize();
Leon Clarked91b9f72010-01-27 17:25:45 +0000356
Leon Clarkee46be812010-01-19 14:06:41 +0000357 v8::Persistent<v8::Context> env = v8::Context::New();
Leon Clarked91b9f72010-01-27 17:25:45 +0000358 ASSERT(!env.IsEmpty());
Leon Clarkee46be812010-01-19 14:06:41 +0000359 env->Enter();
Leon Clarked91b9f72010-01-27 17:25:45 +0000360 // Make sure all builtin scripts are cached.
361 { HandleScope scope;
362 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
363 Bootstrapper::NativesSourceLookup(i);
364 }
365 }
366 Heap::CollectAllGarbage(true);
367 Heap::CollectAllGarbage(true);
Leon Clarkee46be812010-01-19 14:06:41 +0000368
Leon Clarked91b9f72010-01-27 17:25:45 +0000369 Object* raw_foo;
370 {
371 v8::HandleScope handle_scope;
372 v8::Local<v8::String> foo = v8::String::New("foo");
373 ASSERT(!foo.IsEmpty());
374 raw_foo = *(v8::Utils::OpenHandle(*foo));
375 }
Leon Clarkee46be812010-01-19 14:06:41 +0000376
Leon Clarked91b9f72010-01-27 17:25:45 +0000377 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
378 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
379 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
380
381 env->Exit();
382 env.Dispose();
383
384 FileByteSink startup_sink(startup_name.start());
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800385 startup_name.Dispose();
Leon Clarked91b9f72010-01-27 17:25:45 +0000386 StartupSerializer startup_serializer(&startup_sink);
387 startup_serializer.SerializeStrongReferences();
388
389 FileByteSink partial_sink(FLAG_testing_serialization_file);
390 PartialSerializer p_ser(&startup_serializer, &partial_sink);
391 p_ser.Serialize(&raw_foo);
392 startup_serializer.SerializeWeakReferences();
393 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE),
394 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
395 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
396 p_ser.CurrentAllocationAddress(CODE_SPACE),
397 p_ser.CurrentAllocationAddress(MAP_SPACE),
398 p_ser.CurrentAllocationAddress(CELL_SPACE),
399 p_ser.CurrentAllocationAddress(LO_SPACE));
Leon Clarkee46be812010-01-19 14:06:41 +0000400}
401
402
Andrei Popescu31002712010-02-23 13:46:05 +0000403static void ReserveSpaceForPartialSnapshot(const char* file_name) {
404 int file_name_length = StrLength(file_name) + 10;
Leon Clarkee46be812010-01-19 14:06:41 +0000405 Vector<char> name = Vector<char>::New(file_name_length + 1);
406 OS::SNPrintF(name, "%s.size", file_name);
407 FILE* fp = OS::FOpen(name.start(), "r");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800408 name.Dispose();
Leon Clarkee46be812010-01-19 14:06:41 +0000409 int new_size, pointer_size, data_size, code_size, map_size, cell_size;
410 int large_size;
411#ifdef _MSC_VER
412 // Avoid warning about unsafe fscanf from MSVC.
413 // Please note that this is only fine if %c and %s are not being used.
414#define fscanf fscanf_s
415#endif
416 CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size));
417 CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size));
418 CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size));
419 CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size));
420 CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size));
421 CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size));
422 CHECK_EQ(1, fscanf(fp, "large %d\n", &large_size));
423#ifdef _MSC_VER
424#undef fscanf
425#endif
426 fclose(fp);
427 Heap::ReserveSpace(new_size,
428 pointer_size,
429 data_size,
430 code_size,
431 map_size,
432 cell_size,
433 large_size);
Andrei Popescu31002712010-02-23 13:46:05 +0000434}
Leon Clarked91b9f72010-01-27 17:25:45 +0000435
Andrei Popescu31002712010-02-23 13:46:05 +0000436
437DEPENDENT_TEST(PartialDeserialization, PartialSerialization) {
438 if (!Snapshot::IsEnabled()) {
439 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
440 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
441 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
442
443 CHECK(Snapshot::Initialize(startup_name.start()));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800444 startup_name.Dispose();
Andrei Popescu31002712010-02-23 13:46:05 +0000445
446 const char* file_name = FLAG_testing_serialization_file;
447 ReserveSpaceForPartialSnapshot(file_name);
448
449 int snapshot_size = 0;
450 byte* snapshot = ReadBytes(file_name, &snapshot_size);
451
452 Object* root;
453 {
454 SnapshotByteSource source(snapshot, snapshot_size);
455 Deserializer deserializer(&source);
456 deserializer.DeserializePartial(&root);
457 CHECK(root->IsString());
458 }
459 v8::HandleScope handle_scope;
460 Handle<Object>root_handle(root);
461
462 Object* root2;
463 {
464 SnapshotByteSource source(snapshot, snapshot_size);
465 Deserializer deserializer(&source);
466 deserializer.DeserializePartial(&root2);
467 CHECK(root2->IsString());
468 CHECK(*root_handle == root2);
469 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000470 }
Andrei Popescu31002712010-02-23 13:46:05 +0000471}
Leon Clarked91b9f72010-01-27 17:25:45 +0000472
Andrei Popescu31002712010-02-23 13:46:05 +0000473
474TEST(ContextSerialization) {
475 Serializer::Enable();
476 v8::V8::Initialize();
477
478 v8::Persistent<v8::Context> env = v8::Context::New();
479 ASSERT(!env.IsEmpty());
480 env->Enter();
481 // Make sure all builtin scripts are cached.
482 { HandleScope scope;
483 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
484 Bootstrapper::NativesSourceLookup(i);
485 }
486 }
487 // If we don't do this then we end up with a stray root pointing at the
488 // context even after we have disposed of env.
489 Heap::CollectAllGarbage(true);
490
491 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
492 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
493 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
494
495 env->Exit();
496
497 Object* raw_context = *(v8::Utils::OpenHandle(*env));
498
499 env.Dispose();
500
501 FileByteSink startup_sink(startup_name.start());
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800502 startup_name.Dispose();
Andrei Popescu31002712010-02-23 13:46:05 +0000503 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()));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800527 startup_name.Dispose();
Andrei Popescu31002712010-02-23 13:46:05 +0000528
529 const char* file_name = FLAG_testing_serialization_file;
530 ReserveSpaceForPartialSnapshot(file_name);
531
532 int snapshot_size = 0;
533 byte* snapshot = ReadBytes(file_name, &snapshot_size);
534
535 Object* root;
536 {
537 SnapshotByteSource source(snapshot, snapshot_size);
538 Deserializer deserializer(&source);
539 deserializer.DeserializePartial(&root);
540 CHECK(root->IsContext());
541 }
542 v8::HandleScope handle_scope;
543 Handle<Object>root_handle(root);
544
545 Object* root2;
546 {
547 SnapshotByteSource source(snapshot, snapshot_size);
548 Deserializer deserializer(&source);
549 deserializer.DeserializePartial(&root2);
550 CHECK(root2->IsContext());
551 CHECK(*root_handle != root2);
552 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000553 }
Leon Clarkee46be812010-01-19 14:06:41 +0000554}
555
556
557TEST(LinearAllocation) {
558 v8::V8::Initialize();
559 int new_space_max = 512 * KB;
Andrei Popescu31002712010-02-23 13:46:05 +0000560
Leon Clarkee46be812010-01-19 14:06:41 +0000561 for (int size = 1000; size < 5 * MB; size += size >> 1) {
562 int new_space_size = (size < new_space_max) ? size : new_space_max;
563 Heap::ReserveSpace(
564 new_space_size,
565 size, // Old pointer space.
566 size, // Old data space.
567 size, // Code space.
568 size, // Map space.
569 size, // Cell space.
570 size); // Large object space.
571 LinearAllocationScope linear_allocation_scope;
572 const int kSmallFixedArrayLength = 4;
573 const int kSmallFixedArraySize =
574 FixedArray::kHeaderSize + kSmallFixedArrayLength * kPointerSize;
575 const int kSmallStringLength = 16;
576 const int kSmallStringSize =
Steve Block6ded16b2010-05-10 14:33:55 +0100577 (SeqAsciiString::kHeaderSize + kSmallStringLength +
578 kObjectAlignmentMask) & ~kObjectAlignmentMask;
Leon Clarkee46be812010-01-19 14:06:41 +0000579 const int kMapSize = Map::kSize;
580
581 Object* new_last = NULL;
582 for (int i = 0;
583 i + kSmallFixedArraySize <= new_space_size;
584 i += kSmallFixedArraySize) {
John Reck59135872010-11-02 12:39:01 -0700585 Object* obj =
586 Heap::AllocateFixedArray(kSmallFixedArrayLength)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000587 if (new_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100588 CHECK(reinterpret_cast<char*>(obj) ==
589 reinterpret_cast<char*>(new_last) + kSmallFixedArraySize);
Leon Clarkee46be812010-01-19 14:06:41 +0000590 }
591 new_last = obj;
592 }
593
594 Object* pointer_last = NULL;
595 for (int i = 0;
596 i + kSmallFixedArraySize <= size;
597 i += kSmallFixedArraySize) {
John Reck59135872010-11-02 12:39:01 -0700598 Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength,
599 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000600 int old_page_fullness = i % Page::kPageSize;
601 int page_fullness = (i + kSmallFixedArraySize) % Page::kPageSize;
602 if (page_fullness < old_page_fullness ||
603 page_fullness > Page::kObjectAreaSize) {
604 i = RoundUp(i, Page::kPageSize);
605 pointer_last = NULL;
606 }
607 if (pointer_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100608 CHECK(reinterpret_cast<char*>(obj) ==
609 reinterpret_cast<char*>(pointer_last) + kSmallFixedArraySize);
Leon Clarkee46be812010-01-19 14:06:41 +0000610 }
611 pointer_last = obj;
612 }
613
614 Object* data_last = NULL;
615 for (int i = 0; i + kSmallStringSize <= size; i += kSmallStringSize) {
John Reck59135872010-11-02 12:39:01 -0700616 Object* obj = Heap::AllocateRawAsciiString(kSmallStringLength,
617 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000618 int old_page_fullness = i % Page::kPageSize;
619 int page_fullness = (i + kSmallStringSize) % Page::kPageSize;
620 if (page_fullness < old_page_fullness ||
621 page_fullness > Page::kObjectAreaSize) {
622 i = RoundUp(i, Page::kPageSize);
623 data_last = NULL;
624 }
625 if (data_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100626 CHECK(reinterpret_cast<char*>(obj) ==
627 reinterpret_cast<char*>(data_last) + kSmallStringSize);
Leon Clarkee46be812010-01-19 14:06:41 +0000628 }
629 data_last = obj;
630 }
631
632 Object* map_last = NULL;
633 for (int i = 0; i + kMapSize <= size; i += kMapSize) {
John Reck59135872010-11-02 12:39:01 -0700634 Object* obj = Heap::AllocateMap(JS_OBJECT_TYPE,
635 42 * kPointerSize)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000636 int old_page_fullness = i % Page::kPageSize;
637 int page_fullness = (i + kMapSize) % Page::kPageSize;
638 if (page_fullness < old_page_fullness ||
639 page_fullness > Page::kObjectAreaSize) {
640 i = RoundUp(i, Page::kPageSize);
641 map_last = NULL;
642 }
643 if (map_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100644 CHECK(reinterpret_cast<char*>(obj) ==
645 reinterpret_cast<char*>(map_last) + kMapSize);
Leon Clarkee46be812010-01-19 14:06:41 +0000646 }
647 map_last = obj;
648 }
649
650 if (size > Page::kObjectAreaSize) {
651 // Support for reserving space in large object space is not there yet,
652 // but using an always-allocate scope is fine for now.
653 AlwaysAllocateScope always;
654 int large_object_array_length =
655 (size - FixedArray::kHeaderSize) / kPointerSize;
656 Object* obj = Heap::AllocateFixedArray(large_object_array_length,
John Reck59135872010-11-02 12:39:01 -0700657 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000658 CHECK(!obj->IsFailure());
659 }
660 }
661}
662
663
Steve Block3ce2e202009-11-05 08:53:23 +0000664TEST(TestThatAlwaysSucceeds) {
665}
666
667
668TEST(TestThatAlwaysFails) {
669 bool ArtificialFailure = false;
670 CHECK(ArtificialFailure);
671}
672
673
674DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
675 bool ArtificialFailure2 = false;
676 CHECK(ArtificialFailure2);
677}