blob: 6a513e5fa565098272bb485fc4482d7190f8be45 [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");
219 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);
225 fprintf(fp, "large %d\n", large_space_used);
226 fclose(fp);
227}
228
229
230static bool WriteToFile(const char* snapshot_file) {
231 FileByteSink file(snapshot_file);
232 StartupSerializer ser(&file);
233 ser.Serialize();
234 return true;
235}
236
237
Steve Blocka7e24c12009-10-30 11:49:00 +0000238static void Serialize() {
Steve Blockd0582a62009-12-15 09:54:21 +0000239 // We have to create one context. One reason for this is so that the builtins
240 // can be loaded from v8natives.js and their addresses can be processed. This
241 // will clear the pending fixups array, which would otherwise contain GC roots
242 // that would confuse the serialization/deserialization process.
243 v8::Persistent<v8::Context> env = v8::Context::New();
244 env.Dispose();
Leon Clarked91b9f72010-01-27 17:25:45 +0000245 WriteToFile(FLAG_testing_serialization_file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000246}
247
248
Steve Blockd0582a62009-12-15 09:54:21 +0000249// Test that the whole heap can be serialized.
Steve Blocka7e24c12009-10-30 11:49:00 +0000250TEST(Serialize) {
Steve Blockd0582a62009-12-15 09:54:21 +0000251 Serializer::Enable();
252 v8::V8::Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 Serialize();
254}
255
256
Steve Blockd0582a62009-12-15 09:54:21 +0000257// Test that heap serialization is non-destructive.
258TEST(SerializeTwice) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000259 Serializer::Enable();
Steve Blockd0582a62009-12-15 09:54:21 +0000260 v8::V8::Initialize();
261 Serialize();
262 Serialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000263}
264
Steve Blockd0582a62009-12-15 09:54:21 +0000265
Steve Blocka7e24c12009-10-30 11:49:00 +0000266//----------------------------------------------------------------------------
267// Tests that the heap can be deserialized.
268
269static void Deserialize() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
271}
272
273
274static void SanityCheck() {
275 v8::HandleScope scope;
276#ifdef DEBUG
277 Heap::Verify();
278#endif
279 CHECK(Top::global()->IsJSObject());
280 CHECK(Top::global_context()->IsContext());
Steve Blocka7e24c12009-10-30 11:49:00 +0000281 CHECK(Heap::symbol_table()->IsSymbolTable());
282 CHECK(!Factory::LookupAsciiSymbol("Empty")->IsFailure());
283}
284
285
286DEPENDENT_TEST(Deserialize, Serialize) {
Andrei Popescu31002712010-02-23 13:46:05 +0000287 // The serialize-deserialize tests only work if the VM is built without
288 // serialization. That doesn't matter. We don't need to be able to
289 // serialize a snapshot in a VM that is booted from a snapshot.
290 if (!Snapshot::IsEnabled()) {
291 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000292
Andrei Popescu31002712010-02-23 13:46:05 +0000293 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000294
Andrei Popescu31002712010-02-23 13:46:05 +0000295 v8::Persistent<v8::Context> env = v8::Context::New();
296 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000297
Andrei Popescu31002712010-02-23 13:46:05 +0000298 SanityCheck();
299 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000300}
301
Steve Blockd0582a62009-12-15 09:54:21 +0000302
303DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
Andrei Popescu31002712010-02-23 13:46:05 +0000304 if (!Snapshot::IsEnabled()) {
305 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000306
Andrei Popescu31002712010-02-23 13:46:05 +0000307 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000308
Andrei Popescu31002712010-02-23 13:46:05 +0000309 v8::Persistent<v8::Context> env = v8::Context::New();
310 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000311
Andrei Popescu31002712010-02-23 13:46:05 +0000312 SanityCheck();
313 }
Steve Blockd0582a62009-12-15 09:54:21 +0000314}
315
316
317DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
Andrei Popescu31002712010-02-23 13:46:05 +0000318 if (!Snapshot::IsEnabled()) {
319 v8::HandleScope scope;
Steve Blockd0582a62009-12-15 09:54:21 +0000320
Andrei Popescu31002712010-02-23 13:46:05 +0000321 Deserialize();
Steve Blockd0582a62009-12-15 09:54:21 +0000322
Andrei Popescu31002712010-02-23 13:46:05 +0000323 v8::Persistent<v8::Context> env = v8::Context::New();
324 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000325
Andrei Popescu31002712010-02-23 13:46:05 +0000326 const char* c_source = "\"1234\".length";
327 v8::Local<v8::String> source = v8::String::New(c_source);
328 v8::Local<v8::Script> script = v8::Script::Compile(source);
329 CHECK_EQ(4, script->Run()->Int32Value());
330 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000331}
332
333
Steve Blockd0582a62009-12-15 09:54:21 +0000334DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
335 SerializeTwice) {
Andrei Popescu31002712010-02-23 13:46:05 +0000336 if (!Snapshot::IsEnabled()) {
337 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000338
Andrei Popescu31002712010-02-23 13:46:05 +0000339 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000340
Andrei Popescu31002712010-02-23 13:46:05 +0000341 v8::Persistent<v8::Context> env = v8::Context::New();
342 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000343
Andrei Popescu31002712010-02-23 13:46:05 +0000344 const char* c_source = "\"1234\".length";
345 v8::Local<v8::String> source = v8::String::New(c_source);
346 v8::Local<v8::Script> script = v8::Script::Compile(source);
347 CHECK_EQ(4, script->Run()->Int32Value());
348 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000349}
350
351
Leon Clarkee46be812010-01-19 14:06:41 +0000352TEST(PartialSerialization) {
353 Serializer::Enable();
354 v8::V8::Initialize();
Leon Clarked91b9f72010-01-27 17:25:45 +0000355
Leon Clarkee46be812010-01-19 14:06:41 +0000356 v8::Persistent<v8::Context> env = v8::Context::New();
Leon Clarked91b9f72010-01-27 17:25:45 +0000357 ASSERT(!env.IsEmpty());
Leon Clarkee46be812010-01-19 14:06:41 +0000358 env->Enter();
Leon Clarked91b9f72010-01-27 17:25:45 +0000359 // Make sure all builtin scripts are cached.
360 { HandleScope scope;
361 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
362 Bootstrapper::NativesSourceLookup(i);
363 }
364 }
365 Heap::CollectAllGarbage(true);
366 Heap::CollectAllGarbage(true);
Leon Clarkee46be812010-01-19 14:06:41 +0000367
Leon Clarked91b9f72010-01-27 17:25:45 +0000368 Object* raw_foo;
369 {
370 v8::HandleScope handle_scope;
371 v8::Local<v8::String> foo = v8::String::New("foo");
372 ASSERT(!foo.IsEmpty());
373 raw_foo = *(v8::Utils::OpenHandle(*foo));
374 }
Leon Clarkee46be812010-01-19 14:06:41 +0000375
Leon Clarked91b9f72010-01-27 17:25:45 +0000376 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
377 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
378 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
379
380 env->Exit();
381 env.Dispose();
382
383 FileByteSink startup_sink(startup_name.start());
384 StartupSerializer startup_serializer(&startup_sink);
385 startup_serializer.SerializeStrongReferences();
386
387 FileByteSink partial_sink(FLAG_testing_serialization_file);
388 PartialSerializer p_ser(&startup_serializer, &partial_sink);
389 p_ser.Serialize(&raw_foo);
390 startup_serializer.SerializeWeakReferences();
391 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE),
392 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
393 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
394 p_ser.CurrentAllocationAddress(CODE_SPACE),
395 p_ser.CurrentAllocationAddress(MAP_SPACE),
396 p_ser.CurrentAllocationAddress(CELL_SPACE),
397 p_ser.CurrentAllocationAddress(LO_SPACE));
Leon Clarkee46be812010-01-19 14:06:41 +0000398}
399
400
Andrei Popescu31002712010-02-23 13:46:05 +0000401static void ReserveSpaceForPartialSnapshot(const char* file_name) {
402 int file_name_length = StrLength(file_name) + 10;
Leon Clarkee46be812010-01-19 14:06:41 +0000403 Vector<char> name = Vector<char>::New(file_name_length + 1);
404 OS::SNPrintF(name, "%s.size", file_name);
405 FILE* fp = OS::FOpen(name.start(), "r");
406 int new_size, pointer_size, data_size, code_size, map_size, cell_size;
407 int large_size;
408#ifdef _MSC_VER
409 // Avoid warning about unsafe fscanf from MSVC.
410 // Please note that this is only fine if %c and %s are not being used.
411#define fscanf fscanf_s
412#endif
413 CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size));
414 CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size));
415 CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size));
416 CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size));
417 CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size));
418 CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size));
419 CHECK_EQ(1, fscanf(fp, "large %d\n", &large_size));
420#ifdef _MSC_VER
421#undef fscanf
422#endif
423 fclose(fp);
424 Heap::ReserveSpace(new_size,
425 pointer_size,
426 data_size,
427 code_size,
428 map_size,
429 cell_size,
430 large_size);
Andrei Popescu31002712010-02-23 13:46:05 +0000431}
Leon Clarked91b9f72010-01-27 17:25:45 +0000432
Andrei Popescu31002712010-02-23 13:46:05 +0000433
434DEPENDENT_TEST(PartialDeserialization, PartialSerialization) {
435 if (!Snapshot::IsEnabled()) {
436 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
437 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
438 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
439
440 CHECK(Snapshot::Initialize(startup_name.start()));
441
442 const char* file_name = FLAG_testing_serialization_file;
443 ReserveSpaceForPartialSnapshot(file_name);
444
445 int snapshot_size = 0;
446 byte* snapshot = ReadBytes(file_name, &snapshot_size);
447
448 Object* root;
449 {
450 SnapshotByteSource source(snapshot, snapshot_size);
451 Deserializer deserializer(&source);
452 deserializer.DeserializePartial(&root);
453 CHECK(root->IsString());
454 }
455 v8::HandleScope handle_scope;
456 Handle<Object>root_handle(root);
457
458 Object* root2;
459 {
460 SnapshotByteSource source(snapshot, snapshot_size);
461 Deserializer deserializer(&source);
462 deserializer.DeserializePartial(&root2);
463 CHECK(root2->IsString());
464 CHECK(*root_handle == root2);
465 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000466 }
Andrei Popescu31002712010-02-23 13:46:05 +0000467}
Leon Clarked91b9f72010-01-27 17:25:45 +0000468
Andrei Popescu31002712010-02-23 13:46:05 +0000469
470TEST(ContextSerialization) {
471 Serializer::Enable();
472 v8::V8::Initialize();
473
474 v8::Persistent<v8::Context> env = v8::Context::New();
475 ASSERT(!env.IsEmpty());
476 env->Enter();
477 // Make sure all builtin scripts are cached.
478 { HandleScope scope;
479 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
480 Bootstrapper::NativesSourceLookup(i);
481 }
482 }
483 // If we don't do this then we end up with a stray root pointing at the
484 // context even after we have disposed of env.
485 Heap::CollectAllGarbage(true);
486
487 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
488 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
489 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
490
491 env->Exit();
492
493 Object* raw_context = *(v8::Utils::OpenHandle(*env));
494
495 env.Dispose();
496
497 FileByteSink startup_sink(startup_name.start());
498 StartupSerializer startup_serializer(&startup_sink);
499 startup_serializer.SerializeStrongReferences();
500
501 FileByteSink partial_sink(FLAG_testing_serialization_file);
502 PartialSerializer p_ser(&startup_serializer, &partial_sink);
503 p_ser.Serialize(&raw_context);
504 startup_serializer.SerializeWeakReferences();
505 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE),
506 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
507 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
508 p_ser.CurrentAllocationAddress(CODE_SPACE),
509 p_ser.CurrentAllocationAddress(MAP_SPACE),
510 p_ser.CurrentAllocationAddress(CELL_SPACE),
511 p_ser.CurrentAllocationAddress(LO_SPACE));
512}
513
514
515DEPENDENT_TEST(ContextDeserialization, ContextSerialization) {
516 if (!Snapshot::IsEnabled()) {
517 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
518 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
519 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
520
521 CHECK(Snapshot::Initialize(startup_name.start()));
522
523 const char* file_name = FLAG_testing_serialization_file;
524 ReserveSpaceForPartialSnapshot(file_name);
525
526 int snapshot_size = 0;
527 byte* snapshot = ReadBytes(file_name, &snapshot_size);
528
529 Object* root;
530 {
531 SnapshotByteSource source(snapshot, snapshot_size);
532 Deserializer deserializer(&source);
533 deserializer.DeserializePartial(&root);
534 CHECK(root->IsContext());
535 }
536 v8::HandleScope handle_scope;
537 Handle<Object>root_handle(root);
538
539 Object* root2;
540 {
541 SnapshotByteSource source(snapshot, snapshot_size);
542 Deserializer deserializer(&source);
543 deserializer.DeserializePartial(&root2);
544 CHECK(root2->IsContext());
545 CHECK(*root_handle != root2);
546 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000547 }
Leon Clarkee46be812010-01-19 14:06:41 +0000548}
549
550
551TEST(LinearAllocation) {
552 v8::V8::Initialize();
553 int new_space_max = 512 * KB;
Andrei Popescu31002712010-02-23 13:46:05 +0000554
Leon Clarkee46be812010-01-19 14:06:41 +0000555 for (int size = 1000; size < 5 * MB; size += size >> 1) {
556 int new_space_size = (size < new_space_max) ? size : new_space_max;
557 Heap::ReserveSpace(
558 new_space_size,
559 size, // Old pointer space.
560 size, // Old data space.
561 size, // Code space.
562 size, // Map space.
563 size, // Cell space.
564 size); // Large object space.
565 LinearAllocationScope linear_allocation_scope;
566 const int kSmallFixedArrayLength = 4;
567 const int kSmallFixedArraySize =
568 FixedArray::kHeaderSize + kSmallFixedArrayLength * kPointerSize;
569 const int kSmallStringLength = 16;
570 const int kSmallStringSize =
Steve Block6ded16b2010-05-10 14:33:55 +0100571 (SeqAsciiString::kHeaderSize + kSmallStringLength +
572 kObjectAlignmentMask) & ~kObjectAlignmentMask;
Leon Clarkee46be812010-01-19 14:06:41 +0000573 const int kMapSize = Map::kSize;
574
575 Object* new_last = NULL;
576 for (int i = 0;
577 i + kSmallFixedArraySize <= new_space_size;
578 i += kSmallFixedArraySize) {
John Reck59135872010-11-02 12:39:01 -0700579 Object* obj =
580 Heap::AllocateFixedArray(kSmallFixedArrayLength)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000581 if (new_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100582 CHECK(reinterpret_cast<char*>(obj) ==
583 reinterpret_cast<char*>(new_last) + kSmallFixedArraySize);
Leon Clarkee46be812010-01-19 14:06:41 +0000584 }
585 new_last = obj;
586 }
587
588 Object* pointer_last = NULL;
589 for (int i = 0;
590 i + kSmallFixedArraySize <= size;
591 i += kSmallFixedArraySize) {
John Reck59135872010-11-02 12:39:01 -0700592 Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength,
593 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000594 int old_page_fullness = i % Page::kPageSize;
595 int page_fullness = (i + kSmallFixedArraySize) % Page::kPageSize;
596 if (page_fullness < old_page_fullness ||
597 page_fullness > Page::kObjectAreaSize) {
598 i = RoundUp(i, Page::kPageSize);
599 pointer_last = NULL;
600 }
601 if (pointer_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100602 CHECK(reinterpret_cast<char*>(obj) ==
603 reinterpret_cast<char*>(pointer_last) + kSmallFixedArraySize);
Leon Clarkee46be812010-01-19 14:06:41 +0000604 }
605 pointer_last = obj;
606 }
607
608 Object* data_last = NULL;
609 for (int i = 0; i + kSmallStringSize <= size; i += kSmallStringSize) {
John Reck59135872010-11-02 12:39:01 -0700610 Object* obj = Heap::AllocateRawAsciiString(kSmallStringLength,
611 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000612 int old_page_fullness = i % Page::kPageSize;
613 int page_fullness = (i + kSmallStringSize) % Page::kPageSize;
614 if (page_fullness < old_page_fullness ||
615 page_fullness > Page::kObjectAreaSize) {
616 i = RoundUp(i, Page::kPageSize);
617 data_last = NULL;
618 }
619 if (data_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100620 CHECK(reinterpret_cast<char*>(obj) ==
621 reinterpret_cast<char*>(data_last) + kSmallStringSize);
Leon Clarkee46be812010-01-19 14:06:41 +0000622 }
623 data_last = obj;
624 }
625
626 Object* map_last = NULL;
627 for (int i = 0; i + kMapSize <= size; i += kMapSize) {
John Reck59135872010-11-02 12:39:01 -0700628 Object* obj = Heap::AllocateMap(JS_OBJECT_TYPE,
629 42 * kPointerSize)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000630 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) {
Steve Block6ded16b2010-05-10 14:33:55 +0100638 CHECK(reinterpret_cast<char*>(obj) ==
639 reinterpret_cast<char*>(map_last) + kMapSize);
Leon Clarkee46be812010-01-19 14:06:41 +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,
John Reck59135872010-11-02 12:39:01 -0700651 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000652 CHECK(!obj->IsFailure());
653 }
654 }
655}
656
657
Steve Block3ce2e202009-11-05 08:53:23 +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}