blob: 6d07426607e3a4cc043a5e019ed206f53ddfb373 [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"
Steve Blocka7e24c12009-10-30 11:49:00 +000042
43using namespace v8::internal;
44
45static const unsigned kCounters = 256;
46static int local_counters[kCounters];
47static const char* local_counter_names[kCounters];
48
49
50static unsigned CounterHash(const char* s) {
51 unsigned hash = 0;
52 while (*++s) {
53 hash |= hash << 5;
54 hash += *s;
55 }
56 return hash;
57}
58
59
60// Callback receiver to track counters in test.
61static int* counter_function(const char* name) {
62 unsigned hash = CounterHash(name) % kCounters;
63 unsigned original_hash = hash;
64 USE(original_hash);
65 while (true) {
66 if (local_counter_names[hash] == name) {
67 return &local_counters[hash];
68 }
69 if (local_counter_names[hash] == 0) {
70 local_counter_names[hash] = name;
71 return &local_counters[hash];
72 }
73 if (strcmp(local_counter_names[hash], name) == 0) {
74 return &local_counters[hash];
75 }
76 hash = (hash + 1) % kCounters;
77 ASSERT(hash != original_hash); // Hash table has been filled up.
78 }
79}
80
81
82template <class T>
83static Address AddressOf(T id) {
84 return ExternalReference(id).address();
85}
86
87
88template <class T>
89static uint32_t Encode(const ExternalReferenceEncoder& encoder, T id) {
90 return encoder.Encode(AddressOf(id));
91}
92
93
94static int make_code(TypeCode type, int id) {
95 return static_cast<uint32_t>(type) << kReferenceTypeShift | id;
96}
97
98
99static int register_code(int reg) {
100 return Debug::k_register_address << kDebugIdShift | reg;
101}
102
103
104TEST(ExternalReferenceEncoder) {
105 StatsTable::SetCounterFunction(counter_function);
106 Heap::Setup(false);
107 ExternalReferenceEncoder encoder;
108 CHECK_EQ(make_code(BUILTIN, Builtins::ArrayCode),
109 Encode(encoder, Builtins::ArrayCode));
110 CHECK_EQ(make_code(RUNTIME_FUNCTION, Runtime::kAbort),
111 Encode(encoder, Runtime::kAbort));
112 CHECK_EQ(make_code(IC_UTILITY, IC::kLoadCallbackProperty),
113 Encode(encoder, IC_Utility(IC::kLoadCallbackProperty)));
114 CHECK_EQ(make_code(DEBUG_ADDRESS, register_code(3)),
115 Encode(encoder, Debug_Address(Debug::k_register_address, 3)));
116 ExternalReference keyed_load_function_prototype =
117 ExternalReference(&Counters::keyed_load_function_prototype);
118 CHECK_EQ(make_code(STATS_COUNTER, Counters::k_keyed_load_function_prototype),
119 encoder.Encode(keyed_load_function_prototype.address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 ExternalReference the_hole_value_location =
121 ExternalReference::the_hole_value_location();
122 CHECK_EQ(make_code(UNCLASSIFIED, 2),
123 encoder.Encode(the_hole_value_location.address()));
Steve Blockd0582a62009-12-15 09:54:21 +0000124 ExternalReference stack_limit_address =
125 ExternalReference::address_of_stack_limit();
Steve Blocka7e24c12009-10-30 11:49:00 +0000126 CHECK_EQ(make_code(UNCLASSIFIED, 4),
Steve Blockd0582a62009-12-15 09:54:21 +0000127 encoder.Encode(stack_limit_address.address()));
128 ExternalReference real_stack_limit_address =
129 ExternalReference::address_of_real_stack_limit();
130 CHECK_EQ(make_code(UNCLASSIFIED, 5),
131 encoder.Encode(real_stack_limit_address.address()));
132 CHECK_EQ(make_code(UNCLASSIFIED, 11),
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 encoder.Encode(ExternalReference::debug_break().address()));
Steve Blockd0582a62009-12-15 09:54:21 +0000134 CHECK_EQ(make_code(UNCLASSIFIED, 7),
Steve Blocka7e24c12009-10-30 11:49:00 +0000135 encoder.Encode(ExternalReference::new_space_start().address()));
136 CHECK_EQ(make_code(UNCLASSIFIED, 3),
137 encoder.Encode(ExternalReference::roots_address().address()));
138}
139
140
141TEST(ExternalReferenceDecoder) {
142 StatsTable::SetCounterFunction(counter_function);
143 Heap::Setup(false);
144 ExternalReferenceDecoder decoder;
145 CHECK_EQ(AddressOf(Builtins::ArrayCode),
146 decoder.Decode(make_code(BUILTIN, Builtins::ArrayCode)));
147 CHECK_EQ(AddressOf(Runtime::kAbort),
148 decoder.Decode(make_code(RUNTIME_FUNCTION, Runtime::kAbort)));
149 CHECK_EQ(AddressOf(IC_Utility(IC::kLoadCallbackProperty)),
150 decoder.Decode(make_code(IC_UTILITY, IC::kLoadCallbackProperty)));
151 CHECK_EQ(AddressOf(Debug_Address(Debug::k_register_address, 3)),
152 decoder.Decode(make_code(DEBUG_ADDRESS, register_code(3))));
153 ExternalReference keyed_load_function =
154 ExternalReference(&Counters::keyed_load_function_prototype);
155 CHECK_EQ(keyed_load_function.address(),
156 decoder.Decode(
157 make_code(STATS_COUNTER,
158 Counters::k_keyed_load_function_prototype)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000159 CHECK_EQ(ExternalReference::the_hole_value_location().address(),
160 decoder.Decode(make_code(UNCLASSIFIED, 2)));
Steve Blockd0582a62009-12-15 09:54:21 +0000161 CHECK_EQ(ExternalReference::address_of_stack_limit().address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000162 decoder.Decode(make_code(UNCLASSIFIED, 4)));
Steve Blockd0582a62009-12-15 09:54:21 +0000163 CHECK_EQ(ExternalReference::address_of_real_stack_limit().address(),
164 decoder.Decode(make_code(UNCLASSIFIED, 5)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 CHECK_EQ(ExternalReference::debug_break().address(),
Steve Blockd0582a62009-12-15 09:54:21 +0000166 decoder.Decode(make_code(UNCLASSIFIED, 11)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000167 CHECK_EQ(ExternalReference::new_space_start().address(),
Steve Blockd0582a62009-12-15 09:54:21 +0000168 decoder.Decode(make_code(UNCLASSIFIED, 7)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000169}
170
171
172static void Serialize() {
Steve Blockd0582a62009-12-15 09:54:21 +0000173 // We have to create one context. One reason for this is so that the builtins
174 // can be loaded from v8natives.js and their addresses can be processed. This
175 // will clear the pending fixups array, which would otherwise contain GC roots
176 // that would confuse the serialization/deserialization process.
177 v8::Persistent<v8::Context> env = v8::Context::New();
178 env.Dispose();
Steve Blocka7e24c12009-10-30 11:49:00 +0000179 Snapshot::WriteToFile(FLAG_testing_serialization_file);
180}
181
182
Steve Blockd0582a62009-12-15 09:54:21 +0000183// Test that the whole heap can be serialized.
Steve Blocka7e24c12009-10-30 11:49:00 +0000184TEST(Serialize) {
Steve Blockd0582a62009-12-15 09:54:21 +0000185 Serializer::Enable();
186 v8::V8::Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000187 Serialize();
188}
189
190
Steve Blockd0582a62009-12-15 09:54:21 +0000191// Test that heap serialization is non-destructive.
192TEST(SerializeTwice) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000193 Serializer::Enable();
Steve Blockd0582a62009-12-15 09:54:21 +0000194 v8::V8::Initialize();
195 Serialize();
196 Serialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000197}
198
Steve Blockd0582a62009-12-15 09:54:21 +0000199
Steve Blocka7e24c12009-10-30 11:49:00 +0000200//----------------------------------------------------------------------------
201// Tests that the heap can be deserialized.
202
203static void Deserialize() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
205}
206
207
208static void SanityCheck() {
209 v8::HandleScope scope;
210#ifdef DEBUG
211 Heap::Verify();
212#endif
213 CHECK(Top::global()->IsJSObject());
214 CHECK(Top::global_context()->IsContext());
215 CHECK(Top::special_function_table()->IsFixedArray());
216 CHECK(Heap::symbol_table()->IsSymbolTable());
217 CHECK(!Factory::LookupAsciiSymbol("Empty")->IsFailure());
218}
219
220
221DEPENDENT_TEST(Deserialize, Serialize) {
222 v8::HandleScope scope;
223
224 Deserialize();
225
Steve Blockd0582a62009-12-15 09:54:21 +0000226 v8::Persistent<v8::Context> env = v8::Context::New();
227 env->Enter();
228
Steve Blocka7e24c12009-10-30 11:49:00 +0000229 SanityCheck();
230}
231
Steve Blockd0582a62009-12-15 09:54:21 +0000232
233DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000234 v8::HandleScope scope;
235
236 Deserialize();
237
Steve Blockd0582a62009-12-15 09:54:21 +0000238 v8::Persistent<v8::Context> env = v8::Context::New();
239 env->Enter();
240
241 SanityCheck();
242}
243
244
245DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
246 v8::HandleScope scope;
247
248 Deserialize();
249
250 v8::Persistent<v8::Context> env = v8::Context::New();
251 env->Enter();
252
Steve Blocka7e24c12009-10-30 11:49:00 +0000253 const char* c_source = "\"1234\".length";
254 v8::Local<v8::String> source = v8::String::New(c_source);
255 v8::Local<v8::Script> script = v8::Script::Compile(source);
256 CHECK_EQ(4, script->Run()->Int32Value());
257}
258
259
Steve Blockd0582a62009-12-15 09:54:21 +0000260DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
261 SerializeTwice) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 v8::HandleScope scope;
263
264 Deserialize();
265
Steve Blockd0582a62009-12-15 09:54:21 +0000266 v8::Persistent<v8::Context> env = v8::Context::New();
267 env->Enter();
268
269 const char* c_source = "\"1234\".length";
Steve Blocka7e24c12009-10-30 11:49:00 +0000270 v8::Local<v8::String> source = v8::String::New(c_source);
271 v8::Local<v8::Script> script = v8::Script::Compile(source);
Steve Blockd0582a62009-12-15 09:54:21 +0000272 CHECK_EQ(4, script->Run()->Int32Value());
Steve Blocka7e24c12009-10-30 11:49:00 +0000273}
274
275
Leon Clarkee46be812010-01-19 14:06:41 +0000276class FileByteSink : public SnapshotByteSink {
277 public:
278 explicit FileByteSink(const char* snapshot_file) {
279 fp_ = OS::FOpen(snapshot_file, "wb");
280 file_name_ = snapshot_file;
281 if (fp_ == NULL) {
282 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
283 exit(1);
284 }
285 }
286 virtual ~FileByteSink() {
287 if (fp_ != NULL) {
288 fclose(fp_);
289 }
290 }
291 virtual void Put(int byte, const char* description) {
292 if (fp_ != NULL) {
293 fputc(byte, fp_);
294 }
295 }
296 virtual int Position() {
297 return ftell(fp_);
298 }
299 void WriteSpaceUsed(
300 int new_space_used,
301 int pointer_space_used,
302 int data_space_used,
303 int code_space_used,
304 int map_space_used,
305 int cell_space_used,
306 int large_space_used);
307
308 private:
309 FILE* fp_;
310 const char* file_name_;
311};
312
313
314void FileByteSink::WriteSpaceUsed(
315 int new_space_used,
316 int pointer_space_used,
317 int data_space_used,
318 int code_space_used,
319 int map_space_used,
320 int cell_space_used,
321 int large_space_used) {
322 int file_name_length = strlen(file_name_) + 10;
323 Vector<char> name = Vector<char>::New(file_name_length + 1);
324 OS::SNPrintF(name, "%s.size", file_name_);
325 FILE* fp = OS::FOpen(name.start(), "w");
326 fprintf(fp, "new %d\n", new_space_used);
327 fprintf(fp, "pointer %d\n", pointer_space_used);
328 fprintf(fp, "data %d\n", data_space_used);
329 fprintf(fp, "code %d\n", code_space_used);
330 fprintf(fp, "map %d\n", map_space_used);
331 fprintf(fp, "cell %d\n", cell_space_used);
332 fprintf(fp, "large %d\n", large_space_used);
333 fclose(fp);
334}
335
336
337TEST(PartialSerialization) {
338 Serializer::Enable();
339 v8::V8::Initialize();
340 v8::Persistent<v8::Context> env = v8::Context::New();
341 env->Enter();
342
343 v8::HandleScope handle_scope;
344 v8::Local<v8::String> foo = v8::String::New("foo");
345
346 FileByteSink file(FLAG_testing_serialization_file);
347 Serializer ser(&file);
348 i::Handle<i::String> internal_foo = v8::Utils::OpenHandle(*foo);
349 Object* raw_foo = *internal_foo;
350 ser.SerializePartial(&raw_foo);
351 file.WriteSpaceUsed(ser.CurrentAllocationAddress(NEW_SPACE),
352 ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
353 ser.CurrentAllocationAddress(OLD_DATA_SPACE),
354 ser.CurrentAllocationAddress(CODE_SPACE),
355 ser.CurrentAllocationAddress(MAP_SPACE),
356 ser.CurrentAllocationAddress(CELL_SPACE),
357 ser.CurrentAllocationAddress(LO_SPACE));
358}
359
360
361DEPENDENT_TEST(PartialDeserialization, PartialSerialization) {
362 v8::V8::Initialize();
363 const char* file_name = FLAG_testing_serialization_file;
364 int file_name_length = strlen(file_name) + 10;
365 Vector<char> name = Vector<char>::New(file_name_length + 1);
366 OS::SNPrintF(name, "%s.size", file_name);
367 FILE* fp = OS::FOpen(name.start(), "r");
368 int new_size, pointer_size, data_size, code_size, map_size, cell_size;
369 int large_size;
370#ifdef _MSC_VER
371 // Avoid warning about unsafe fscanf from MSVC.
372 // Please note that this is only fine if %c and %s are not being used.
373#define fscanf fscanf_s
374#endif
375 CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size));
376 CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size));
377 CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size));
378 CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size));
379 CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size));
380 CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size));
381 CHECK_EQ(1, fscanf(fp, "large %d\n", &large_size));
382#ifdef _MSC_VER
383#undef fscanf
384#endif
385 fclose(fp);
386 Heap::ReserveSpace(new_size,
387 pointer_size,
388 data_size,
389 code_size,
390 map_size,
391 cell_size,
392 large_size);
393 int snapshot_size = 0;
394 byte* snapshot = ReadBytes(file_name, &snapshot_size);
395 SnapshotByteSource source(snapshot, snapshot_size);
396 Deserializer deserializer(&source);
397 Object* root;
398 deserializer.DeserializePartial(&root);
399 CHECK(root->IsString());
400}
401
402
403TEST(LinearAllocation) {
404 v8::V8::Initialize();
405 int new_space_max = 512 * KB;
406 for (int size = 1000; size < 5 * MB; size += size >> 1) {
407 int new_space_size = (size < new_space_max) ? size : new_space_max;
408 Heap::ReserveSpace(
409 new_space_size,
410 size, // Old pointer space.
411 size, // Old data space.
412 size, // Code space.
413 size, // Map space.
414 size, // Cell space.
415 size); // Large object space.
416 LinearAllocationScope linear_allocation_scope;
417 const int kSmallFixedArrayLength = 4;
418 const int kSmallFixedArraySize =
419 FixedArray::kHeaderSize + kSmallFixedArrayLength * kPointerSize;
420 const int kSmallStringLength = 16;
421 const int kSmallStringSize =
422 SeqAsciiString::kHeaderSize + kSmallStringLength;
423 const int kMapSize = Map::kSize;
424
425 Object* new_last = NULL;
426 for (int i = 0;
427 i + kSmallFixedArraySize <= new_space_size;
428 i += kSmallFixedArraySize) {
429 Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength);
430 if (new_last != NULL) {
431 CHECK_EQ(reinterpret_cast<char*>(obj),
432 reinterpret_cast<char*>(new_last) + kSmallFixedArraySize);
433 }
434 new_last = obj;
435 }
436
437 Object* pointer_last = NULL;
438 for (int i = 0;
439 i + kSmallFixedArraySize <= size;
440 i += kSmallFixedArraySize) {
441 Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength, TENURED);
442 int old_page_fullness = i % Page::kPageSize;
443 int page_fullness = (i + kSmallFixedArraySize) % Page::kPageSize;
444 if (page_fullness < old_page_fullness ||
445 page_fullness > Page::kObjectAreaSize) {
446 i = RoundUp(i, Page::kPageSize);
447 pointer_last = NULL;
448 }
449 if (pointer_last != NULL) {
450 CHECK_EQ(reinterpret_cast<char*>(obj),
451 reinterpret_cast<char*>(pointer_last) + kSmallFixedArraySize);
452 }
453 pointer_last = obj;
454 }
455
456 Object* data_last = NULL;
457 for (int i = 0; i + kSmallStringSize <= size; i += kSmallStringSize) {
458 Object* obj = Heap::AllocateRawAsciiString(kSmallStringLength, TENURED);
459 int old_page_fullness = i % Page::kPageSize;
460 int page_fullness = (i + kSmallStringSize) % Page::kPageSize;
461 if (page_fullness < old_page_fullness ||
462 page_fullness > Page::kObjectAreaSize) {
463 i = RoundUp(i, Page::kPageSize);
464 data_last = NULL;
465 }
466 if (data_last != NULL) {
467 CHECK_EQ(reinterpret_cast<char*>(obj),
468 reinterpret_cast<char*>(data_last) + kSmallStringSize);
469 }
470 data_last = obj;
471 }
472
473 Object* map_last = NULL;
474 for (int i = 0; i + kMapSize <= size; i += kMapSize) {
475 Object* obj = Heap::AllocateMap(JS_OBJECT_TYPE, 42 * kPointerSize);
476 int old_page_fullness = i % Page::kPageSize;
477 int page_fullness = (i + kMapSize) % Page::kPageSize;
478 if (page_fullness < old_page_fullness ||
479 page_fullness > Page::kObjectAreaSize) {
480 i = RoundUp(i, Page::kPageSize);
481 map_last = NULL;
482 }
483 if (map_last != NULL) {
484 CHECK_EQ(reinterpret_cast<char*>(obj),
485 reinterpret_cast<char*>(map_last) + kMapSize);
486 }
487 map_last = obj;
488 }
489
490 if (size > Page::kObjectAreaSize) {
491 // Support for reserving space in large object space is not there yet,
492 // but using an always-allocate scope is fine for now.
493 AlwaysAllocateScope always;
494 int large_object_array_length =
495 (size - FixedArray::kHeaderSize) / kPointerSize;
496 Object* obj = Heap::AllocateFixedArray(large_object_array_length,
497 TENURED);
498 CHECK(!obj->IsFailure());
499 }
500 }
501}
502
503
Steve Block3ce2e202009-11-05 08:53:23 +0000504TEST(TestThatAlwaysSucceeds) {
505}
506
507
508TEST(TestThatAlwaysFails) {
509 bool ArtificialFailure = false;
510 CHECK(ArtificialFailure);
511}
512
513
514DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
515 bool ArtificialFailure2 = false;
516 CHECK(ArtificialFailure2);
517}