blob: c4c8a4579fc6b3650b20ed9e507cb18f83da474e [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
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()));
Steve Blocka7e24c12009-10-30 11:49:00 +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()));
Steve Blockd0582a62009-12-15 09:54:21 +0000126 ExternalReference stack_limit_address =
127 ExternalReference::address_of_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 CHECK_EQ(make_code(UNCLASSIFIED, 4),
Steve Blockd0582a62009-12-15 09:54:21 +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()));
Andrei Popescu402d9372010-02-26 13:31:12 +0000134 CHECK_EQ(make_code(UNCLASSIFIED, 12),
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 encoder.Encode(ExternalReference::debug_break().address()));
Steve Blockd0582a62009-12-15 09:54:21 +0000136 CHECK_EQ(make_code(UNCLASSIFIED, 7),
Steve Blocka7e24c12009-10-30 11:49:00 +0000137 encoder.Encode(ExternalReference::new_space_start().address()));
138 CHECK_EQ(make_code(UNCLASSIFIED, 3),
139 encoder.Encode(ExternalReference::roots_address().address()));
140}
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)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 CHECK_EQ(ExternalReference::the_hole_value_location().address(),
162 decoder.Decode(make_code(UNCLASSIFIED, 2)));
Steve Blockd0582a62009-12-15 09:54:21 +0000163 CHECK_EQ(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000164 decoder.Decode(make_code(UNCLASSIFIED, 4)));
Steve Blockd0582a62009-12-15 09:54:21 +0000165 CHECK_EQ(ExternalReference::address_of_real_stack_limit().address(),
166 decoder.Decode(make_code(UNCLASSIFIED, 5)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 CHECK_EQ(ExternalReference::debug_break().address(),
Andrei Popescu402d9372010-02-26 13:31:12 +0000168 decoder.Decode(make_code(UNCLASSIFIED, 12)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000169 CHECK_EQ(ExternalReference::new_space_start().address(),
Steve Blockd0582a62009-12-15 09:54:21 +0000170 decoder.Decode(make_code(UNCLASSIFIED, 7)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000171}
172
173
Leon Clarked91b9f72010-01-27 17:25:45 +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
Steve Blocka7e24c12009-10-30 11:49:00 +0000243static void Serialize() {
Steve Blockd0582a62009-12-15 09:54:21 +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();
Leon Clarked91b9f72010-01-27 17:25:45 +0000250 WriteToFile(FLAG_testing_serialization_file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000251}
252
253
Steve Blockd0582a62009-12-15 09:54:21 +0000254// Test that the whole heap can be serialized.
Steve Blocka7e24c12009-10-30 11:49:00 +0000255TEST(Serialize) {
Steve Blockd0582a62009-12-15 09:54:21 +0000256 Serializer::Enable();
257 v8::V8::Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000258 Serialize();
259}
260
261
Steve Blockd0582a62009-12-15 09:54:21 +0000262// Test that heap serialization is non-destructive.
263TEST(SerializeTwice) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000264 Serializer::Enable();
Steve Blockd0582a62009-12-15 09:54:21 +0000265 v8::V8::Initialize();
266 Serialize();
267 Serialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000268}
269
Steve Blockd0582a62009-12-15 09:54:21 +0000270
Steve Blocka7e24c12009-10-30 11:49:00 +0000271//----------------------------------------------------------------------------
272// Tests that the heap can be deserialized.
273
274static void Deserialize() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000275 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
276}
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());
Steve Blocka7e24c12009-10-30 11:49:00 +0000286 CHECK(Heap::symbol_table()->IsSymbolTable());
287 CHECK(!Factory::LookupAsciiSymbol("Empty")->IsFailure());
288}
289
290
291DEPENDENT_TEST(Deserialize, Serialize) {
Andrei Popescu31002712010-02-23 13:46:05 +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;
Steve Blocka7e24c12009-10-30 11:49:00 +0000297
Andrei Popescu31002712010-02-23 13:46:05 +0000298 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000299
Andrei Popescu31002712010-02-23 13:46:05 +0000300 v8::Persistent<v8::Context> env = v8::Context::New();
301 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000302
Andrei Popescu31002712010-02-23 13:46:05 +0000303 SanityCheck();
304 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000305}
306
Steve Blockd0582a62009-12-15 09:54:21 +0000307
308DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
Andrei Popescu31002712010-02-23 13:46:05 +0000309 if (!Snapshot::IsEnabled()) {
310 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000311
Andrei Popescu31002712010-02-23 13:46:05 +0000312 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000313
Andrei Popescu31002712010-02-23 13:46:05 +0000314 v8::Persistent<v8::Context> env = v8::Context::New();
315 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000316
Andrei Popescu31002712010-02-23 13:46:05 +0000317 SanityCheck();
318 }
Steve Blockd0582a62009-12-15 09:54:21 +0000319}
320
321
322DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
Andrei Popescu31002712010-02-23 13:46:05 +0000323 if (!Snapshot::IsEnabled()) {
324 v8::HandleScope scope;
Steve Blockd0582a62009-12-15 09:54:21 +0000325
Andrei Popescu31002712010-02-23 13:46:05 +0000326 Deserialize();
Steve Blockd0582a62009-12-15 09:54:21 +0000327
Andrei Popescu31002712010-02-23 13:46:05 +0000328 v8::Persistent<v8::Context> env = v8::Context::New();
329 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000330
Andrei Popescu31002712010-02-23 13:46:05 +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 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000336}
337
338
Steve Blockd0582a62009-12-15 09:54:21 +0000339DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
340 SerializeTwice) {
Andrei Popescu31002712010-02-23 13:46:05 +0000341 if (!Snapshot::IsEnabled()) {
342 v8::HandleScope scope;
Steve Blocka7e24c12009-10-30 11:49:00 +0000343
Andrei Popescu31002712010-02-23 13:46:05 +0000344 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000345
Andrei Popescu31002712010-02-23 13:46:05 +0000346 v8::Persistent<v8::Context> env = v8::Context::New();
347 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000348
Andrei Popescu31002712010-02-23 13:46:05 +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 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000354}
355
356
Leon Clarkee46be812010-01-19 14:06:41 +0000357TEST(PartialSerialization) {
358 Serializer::Enable();
359 v8::V8::Initialize();
Leon Clarked91b9f72010-01-27 17:25:45 +0000360
Leon Clarkee46be812010-01-19 14:06:41 +0000361 v8::Persistent<v8::Context> env = v8::Context::New();
Leon Clarked91b9f72010-01-27 17:25:45 +0000362 ASSERT(!env.IsEmpty());
Leon Clarkee46be812010-01-19 14:06:41 +0000363 env->Enter();
Leon Clarked91b9f72010-01-27 17:25:45 +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);
Leon Clarkee46be812010-01-19 14:06:41 +0000372
Leon Clarked91b9f72010-01-27 17:25:45 +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 }
Leon Clarkee46be812010-01-19 14:06:41 +0000380
Leon Clarked91b9f72010-01-27 17:25:45 +0000381 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));
Leon Clarkee46be812010-01-19 14:06:41 +0000403}
404
405
Andrei Popescu31002712010-02-23 13:46:05 +0000406static void ReserveSpaceForPartialSnapshot(const char* file_name) {
407 int file_name_length = StrLength(file_name) + 10;
Leon Clarkee46be812010-01-19 14:06:41 +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);
Andrei Popescu31002712010-02-23 13:46:05 +0000436}
Leon Clarked91b9f72010-01-27 17:25:45 +0000437
Andrei Popescu31002712010-02-23 13:46:05 +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 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000471 }
Andrei Popescu31002712010-02-23 13:46:05 +0000472}
Leon Clarked91b9f72010-01-27 17:25:45 +0000473
Andrei Popescu31002712010-02-23 13:46:05 +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 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000552 }
Leon Clarkee46be812010-01-19 14:06:41 +0000553}
554
555
556TEST(LinearAllocation) {
557 v8::V8::Initialize();
558 int new_space_max = 512 * KB;
Andrei Popescu31002712010-02-23 13:46:05 +0000559
Leon Clarkee46be812010-01-19 14:06:41 +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 =
Steve Block6ded16b2010-05-10 14:33:55 +0100576 (SeqAsciiString::kHeaderSize + kSmallStringLength +
577 kObjectAlignmentMask) & ~kObjectAlignmentMask;
Leon Clarkee46be812010-01-19 14:06:41 +0000578 const int kMapSize = Map::kSize;
579
580 Object* new_last = NULL;
581 for (int i = 0;
582 i + kSmallFixedArraySize <= new_space_size;
583 i += kSmallFixedArraySize) {
584 Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength);
585 if (new_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100586 CHECK(reinterpret_cast<char*>(obj) ==
587 reinterpret_cast<char*>(new_last) + kSmallFixedArraySize);
Leon Clarkee46be812010-01-19 14:06:41 +0000588 }
589 new_last = obj;
590 }
591
592 Object* pointer_last = NULL;
593 for (int i = 0;
594 i + kSmallFixedArraySize <= size;
595 i += kSmallFixedArraySize) {
596 Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength, TENURED);
597 int old_page_fullness = i % Page::kPageSize;
598 int page_fullness = (i + kSmallFixedArraySize) % Page::kPageSize;
599 if (page_fullness < old_page_fullness ||
600 page_fullness > Page::kObjectAreaSize) {
601 i = RoundUp(i, Page::kPageSize);
602 pointer_last = NULL;
603 }
604 if (pointer_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100605 CHECK(reinterpret_cast<char*>(obj) ==
606 reinterpret_cast<char*>(pointer_last) + kSmallFixedArraySize);
Leon Clarkee46be812010-01-19 14:06:41 +0000607 }
608 pointer_last = obj;
609 }
610
611 Object* data_last = NULL;
612 for (int i = 0; i + kSmallStringSize <= size; i += kSmallStringSize) {
613 Object* obj = Heap::AllocateRawAsciiString(kSmallStringLength, TENURED);
614 int old_page_fullness = i % Page::kPageSize;
615 int page_fullness = (i + kSmallStringSize) % Page::kPageSize;
616 if (page_fullness < old_page_fullness ||
617 page_fullness > Page::kObjectAreaSize) {
618 i = RoundUp(i, Page::kPageSize);
619 data_last = NULL;
620 }
621 if (data_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100622 CHECK(reinterpret_cast<char*>(obj) ==
623 reinterpret_cast<char*>(data_last) + kSmallStringSize);
Leon Clarkee46be812010-01-19 14:06:41 +0000624 }
625 data_last = obj;
626 }
627
628 Object* map_last = NULL;
629 for (int i = 0; i + kMapSize <= size; i += kMapSize) {
630 Object* obj = Heap::AllocateMap(JS_OBJECT_TYPE, 42 * kPointerSize);
631 int old_page_fullness = i % Page::kPageSize;
632 int page_fullness = (i + kMapSize) % Page::kPageSize;
633 if (page_fullness < old_page_fullness ||
634 page_fullness > Page::kObjectAreaSize) {
635 i = RoundUp(i, Page::kPageSize);
636 map_last = NULL;
637 }
638 if (map_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100639 CHECK(reinterpret_cast<char*>(obj) ==
640 reinterpret_cast<char*>(map_last) + kMapSize);
Leon Clarkee46be812010-01-19 14:06:41 +0000641 }
642 map_last = obj;
643 }
644
645 if (size > Page::kObjectAreaSize) {
646 // Support for reserving space in large object space is not there yet,
647 // but using an always-allocate scope is fine for now.
648 AlwaysAllocateScope always;
649 int large_object_array_length =
650 (size - FixedArray::kHeaderSize) / kPointerSize;
651 Object* obj = Heap::AllocateFixedArray(large_object_array_length,
652 TENURED);
653 CHECK(!obj->IsFailure());
654 }
655 }
656}
657
658
Steve Block3ce2e202009-11-05 08:53:23 +0000659TEST(TestThatAlwaysSucceeds) {
660}
661
662
663TEST(TestThatAlwaysFails) {
664 bool ArtificialFailure = false;
665 CHECK(ArtificialFailure);
666}
667
668
669DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
670 bool ArtificialFailure2 = false;
671 CHECK(ArtificialFailure2);
672}