blob: 8e85444eee9faf38951d00d89e5897f3ba094240 [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) {
Steve Block44f0eee2011-05-26 01:26:41 +010086 return ExternalReference(id, i::Isolate::Current()).address();
Steve Blocka7e24c12009-10-30 11:49:00 +000087}
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) {
Steve Block44f0eee2011-05-26 01:26:41 +0100102 Isolate* isolate = i::Isolate::Current();
103 isolate->stats_table()->SetCounterFunction(counter_function);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000104 v8::V8::Initialize();
105
Steve Blocka7e24c12009-10-30 11:49:00 +0000106 ExternalReferenceEncoder encoder;
Steve Block44f0eee2011-05-26 01:26:41 +0100107 CHECK_EQ(make_code(BUILTIN, Builtins::kArrayCode),
108 Encode(encoder, Builtins::kArrayCode));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100109 CHECK_EQ(make_code(v8::internal::RUNTIME_FUNCTION, Runtime::kAbort),
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 Encode(encoder, Runtime::kAbort));
111 CHECK_EQ(make_code(IC_UTILITY, IC::kLoadCallbackProperty),
112 Encode(encoder, IC_Utility(IC::kLoadCallbackProperty)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000113 ExternalReference keyed_load_function_prototype =
Steve Block44f0eee2011-05-26 01:26:41 +0100114 ExternalReference(isolate->counters()->keyed_load_function_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +0000115 CHECK_EQ(make_code(STATS_COUNTER, Counters::k_keyed_load_function_prototype),
116 encoder.Encode(keyed_load_function_prototype.address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000117 ExternalReference the_hole_value_location =
Steve Block44f0eee2011-05-26 01:26:41 +0100118 ExternalReference::the_hole_value_location(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 CHECK_EQ(make_code(UNCLASSIFIED, 2),
120 encoder.Encode(the_hole_value_location.address()));
Steve Blockd0582a62009-12-15 09:54:21 +0000121 ExternalReference stack_limit_address =
Steve Block44f0eee2011-05-26 01:26:41 +0100122 ExternalReference::address_of_stack_limit(isolate);
Steve Blocka7e24c12009-10-30 11:49:00 +0000123 CHECK_EQ(make_code(UNCLASSIFIED, 4),
Steve Blockd0582a62009-12-15 09:54:21 +0000124 encoder.Encode(stack_limit_address.address()));
125 ExternalReference real_stack_limit_address =
Steve Block44f0eee2011-05-26 01:26:41 +0100126 ExternalReference::address_of_real_stack_limit(isolate);
Steve Blockd0582a62009-12-15 09:54:21 +0000127 CHECK_EQ(make_code(UNCLASSIFIED, 5),
128 encoder.Encode(real_stack_limit_address.address()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100129#ifdef ENABLE_DEBUGGER_SUPPORT
130 CHECK_EQ(make_code(UNCLASSIFIED, 15),
Steve Block44f0eee2011-05-26 01:26:41 +0100131 encoder.Encode(ExternalReference::debug_break(isolate).address()));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100132#endif // ENABLE_DEBUGGER_SUPPORT
133 CHECK_EQ(make_code(UNCLASSIFIED, 10),
Steve Block44f0eee2011-05-26 01:26:41 +0100134 encoder.Encode(
135 ExternalReference::new_space_start(isolate).address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000136 CHECK_EQ(make_code(UNCLASSIFIED, 3),
Steve Block44f0eee2011-05-26 01:26:41 +0100137 encoder.Encode(ExternalReference::roots_address(isolate).address()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000138}
139
140
141TEST(ExternalReferenceDecoder) {
Steve Block44f0eee2011-05-26 01:26:41 +0100142 Isolate* isolate = i::Isolate::Current();
143 isolate->stats_table()->SetCounterFunction(counter_function);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000144 v8::V8::Initialize();
145
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 ExternalReferenceDecoder decoder;
Steve Block44f0eee2011-05-26 01:26:41 +0100147 CHECK_EQ(AddressOf(Builtins::kArrayCode),
148 decoder.Decode(make_code(BUILTIN, Builtins::kArrayCode)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000149 CHECK_EQ(AddressOf(Runtime::kAbort),
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100150 decoder.Decode(make_code(v8::internal::RUNTIME_FUNCTION,
151 Runtime::kAbort)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000152 CHECK_EQ(AddressOf(IC_Utility(IC::kLoadCallbackProperty)),
153 decoder.Decode(make_code(IC_UTILITY, IC::kLoadCallbackProperty)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000154 ExternalReference keyed_load_function =
Steve Block44f0eee2011-05-26 01:26:41 +0100155 ExternalReference(isolate->counters()->keyed_load_function_prototype());
Steve Blocka7e24c12009-10-30 11:49:00 +0000156 CHECK_EQ(keyed_load_function.address(),
157 decoder.Decode(
158 make_code(STATS_COUNTER,
159 Counters::k_keyed_load_function_prototype)));
Steve Block44f0eee2011-05-26 01:26:41 +0100160 CHECK_EQ(ExternalReference::the_hole_value_location(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000161 decoder.Decode(make_code(UNCLASSIFIED, 2)));
Steve Block44f0eee2011-05-26 01:26:41 +0100162 CHECK_EQ(ExternalReference::address_of_stack_limit(isolate).address(),
Steve Blocka7e24c12009-10-30 11:49:00 +0000163 decoder.Decode(make_code(UNCLASSIFIED, 4)));
Steve Block44f0eee2011-05-26 01:26:41 +0100164 CHECK_EQ(ExternalReference::address_of_real_stack_limit(isolate).address(),
Steve Blockd0582a62009-12-15 09:54:21 +0000165 decoder.Decode(make_code(UNCLASSIFIED, 5)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100166#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100167 CHECK_EQ(ExternalReference::debug_break(isolate).address(),
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100168 decoder.Decode(make_code(UNCLASSIFIED, 15)));
169#endif // ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100170 CHECK_EQ(ExternalReference::new_space_start(isolate).address(),
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100171 decoder.Decode(make_code(UNCLASSIFIED, 10)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000172}
173
174
Leon Clarked91b9f72010-01-27 17:25:45 +0000175class FileByteSink : public SnapshotByteSink {
176 public:
177 explicit FileByteSink(const char* snapshot_file) {
178 fp_ = OS::FOpen(snapshot_file, "wb");
179 file_name_ = snapshot_file;
180 if (fp_ == NULL) {
181 PrintF("Unable to write to snapshot file \"%s\"\n", snapshot_file);
182 exit(1);
183 }
184 }
185 virtual ~FileByteSink() {
186 if (fp_ != NULL) {
187 fclose(fp_);
188 }
189 }
190 virtual void Put(int byte, const char* description) {
191 if (fp_ != NULL) {
192 fputc(byte, fp_);
193 }
194 }
195 virtual int Position() {
196 return ftell(fp_);
197 }
198 void WriteSpaceUsed(
199 int new_space_used,
200 int pointer_space_used,
201 int data_space_used,
202 int code_space_used,
203 int map_space_used,
204 int cell_space_used,
205 int large_space_used);
206
207 private:
208 FILE* fp_;
209 const char* file_name_;
210};
211
212
213void FileByteSink::WriteSpaceUsed(
214 int new_space_used,
215 int pointer_space_used,
216 int data_space_used,
217 int code_space_used,
218 int map_space_used,
219 int cell_space_used,
220 int large_space_used) {
221 int file_name_length = StrLength(file_name_) + 10;
222 Vector<char> name = Vector<char>::New(file_name_length + 1);
223 OS::SNPrintF(name, "%s.size", file_name_);
224 FILE* fp = OS::FOpen(name.start(), "w");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800225 name.Dispose();
Leon Clarked91b9f72010-01-27 17:25:45 +0000226 fprintf(fp, "new %d\n", new_space_used);
227 fprintf(fp, "pointer %d\n", pointer_space_used);
228 fprintf(fp, "data %d\n", data_space_used);
229 fprintf(fp, "code %d\n", code_space_used);
230 fprintf(fp, "map %d\n", map_space_used);
231 fprintf(fp, "cell %d\n", cell_space_used);
232 fprintf(fp, "large %d\n", large_space_used);
233 fclose(fp);
234}
235
236
237static bool WriteToFile(const char* snapshot_file) {
238 FileByteSink file(snapshot_file);
239 StartupSerializer ser(&file);
240 ser.Serialize();
241 return true;
242}
243
244
Steve Blocka7e24c12009-10-30 11:49:00 +0000245static void Serialize() {
Steve Blockd0582a62009-12-15 09:54:21 +0000246 // We have to create one context. One reason for this is so that the builtins
247 // can be loaded from v8natives.js and their addresses can be processed. This
248 // will clear the pending fixups array, which would otherwise contain GC roots
249 // that would confuse the serialization/deserialization process.
250 v8::Persistent<v8::Context> env = v8::Context::New();
251 env.Dispose();
Leon Clarked91b9f72010-01-27 17:25:45 +0000252 WriteToFile(FLAG_testing_serialization_file);
Steve Blocka7e24c12009-10-30 11:49:00 +0000253}
254
255
Steve Blockd0582a62009-12-15 09:54:21 +0000256// Test that the whole heap can be serialized.
Steve Blocka7e24c12009-10-30 11:49:00 +0000257TEST(Serialize) {
Steve Blockd0582a62009-12-15 09:54:21 +0000258 Serializer::Enable();
259 v8::V8::Initialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000260 Serialize();
261}
262
263
Steve Blockd0582a62009-12-15 09:54:21 +0000264// Test that heap serialization is non-destructive.
265TEST(SerializeTwice) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000266 Serializer::Enable();
Steve Blockd0582a62009-12-15 09:54:21 +0000267 v8::V8::Initialize();
268 Serialize();
269 Serialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000270}
271
Steve Blockd0582a62009-12-15 09:54:21 +0000272
Steve Blocka7e24c12009-10-30 11:49:00 +0000273//----------------------------------------------------------------------------
274// Tests that the heap can be deserialized.
275
276static void Deserialize() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
278}
279
280
281static void SanityCheck() {
282 v8::HandleScope scope;
283#ifdef DEBUG
Steve Block44f0eee2011-05-26 01:26:41 +0100284 HEAP->Verify();
Steve Blocka7e24c12009-10-30 11:49:00 +0000285#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100286 CHECK(Isolate::Current()->global()->IsJSObject());
287 CHECK(Isolate::Current()->global_context()->IsContext());
288 CHECK(HEAP->symbol_table()->IsSymbolTable());
289 CHECK(!FACTORY->LookupAsciiSymbol("Empty")->IsFailure());
Steve Blocka7e24c12009-10-30 11:49:00 +0000290}
291
292
293DEPENDENT_TEST(Deserialize, Serialize) {
Andrei Popescu31002712010-02-23 13:46:05 +0000294 // The serialize-deserialize tests only work if the VM is built without
295 // serialization. That doesn't matter. We don't need to be able to
296 // serialize a snapshot in a VM that is booted from a snapshot.
297 if (!Snapshot::IsEnabled()) {
298 v8::HandleScope scope;
Andrei Popescu31002712010-02-23 13:46:05 +0000299 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000300
Andrei Popescu31002712010-02-23 13:46:05 +0000301 v8::Persistent<v8::Context> env = v8::Context::New();
302 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000303
Andrei Popescu31002712010-02-23 13:46:05 +0000304 SanityCheck();
305 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000306}
307
Steve Blockd0582a62009-12-15 09:54:21 +0000308
309DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
Andrei Popescu31002712010-02-23 13:46:05 +0000310 if (!Snapshot::IsEnabled()) {
311 v8::HandleScope scope;
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;
Andrei Popescu31002712010-02-23 13:46:05 +0000325 Deserialize();
Steve Blockd0582a62009-12-15 09:54:21 +0000326
Andrei Popescu31002712010-02-23 13:46:05 +0000327 v8::Persistent<v8::Context> env = v8::Context::New();
328 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000329
Andrei Popescu31002712010-02-23 13:46:05 +0000330 const char* c_source = "\"1234\".length";
331 v8::Local<v8::String> source = v8::String::New(c_source);
332 v8::Local<v8::Script> script = v8::Script::Compile(source);
333 CHECK_EQ(4, script->Run()->Int32Value());
334 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000335}
336
337
Steve Blockd0582a62009-12-15 09:54:21 +0000338DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
339 SerializeTwice) {
Andrei Popescu31002712010-02-23 13:46:05 +0000340 if (!Snapshot::IsEnabled()) {
341 v8::HandleScope scope;
Andrei Popescu31002712010-02-23 13:46:05 +0000342 Deserialize();
Steve Blocka7e24c12009-10-30 11:49:00 +0000343
Andrei Popescu31002712010-02-23 13:46:05 +0000344 v8::Persistent<v8::Context> env = v8::Context::New();
345 env->Enter();
Steve Blockd0582a62009-12-15 09:54:21 +0000346
Andrei Popescu31002712010-02-23 13:46:05 +0000347 const char* c_source = "\"1234\".length";
348 v8::Local<v8::String> source = v8::String::New(c_source);
349 v8::Local<v8::Script> script = v8::Script::Compile(source);
350 CHECK_EQ(4, script->Run()->Int32Value());
351 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000352}
353
354
Leon Clarkee46be812010-01-19 14:06:41 +0000355TEST(PartialSerialization) {
356 Serializer::Enable();
357 v8::V8::Initialize();
Leon Clarked91b9f72010-01-27 17:25:45 +0000358
Leon Clarkee46be812010-01-19 14:06:41 +0000359 v8::Persistent<v8::Context> env = v8::Context::New();
Leon Clarked91b9f72010-01-27 17:25:45 +0000360 ASSERT(!env.IsEmpty());
Leon Clarkee46be812010-01-19 14:06:41 +0000361 env->Enter();
Leon Clarked91b9f72010-01-27 17:25:45 +0000362 // Make sure all builtin scripts are cached.
363 { HandleScope scope;
364 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100365 Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
Leon Clarked91b9f72010-01-27 17:25:45 +0000366 }
367 }
Steve Block44f0eee2011-05-26 01:26:41 +0100368 HEAP->CollectAllGarbage(true);
369 HEAP->CollectAllGarbage(true);
Leon Clarkee46be812010-01-19 14:06:41 +0000370
Leon Clarked91b9f72010-01-27 17:25:45 +0000371 Object* raw_foo;
372 {
373 v8::HandleScope handle_scope;
374 v8::Local<v8::String> foo = v8::String::New("foo");
375 ASSERT(!foo.IsEmpty());
376 raw_foo = *(v8::Utils::OpenHandle(*foo));
377 }
Leon Clarkee46be812010-01-19 14:06:41 +0000378
Leon Clarked91b9f72010-01-27 17:25:45 +0000379 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
380 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
381 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
382
383 env->Exit();
384 env.Dispose();
385
386 FileByteSink startup_sink(startup_name.start());
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800387 startup_name.Dispose();
Leon Clarked91b9f72010-01-27 17:25:45 +0000388 StartupSerializer startup_serializer(&startup_sink);
389 startup_serializer.SerializeStrongReferences();
390
391 FileByteSink partial_sink(FLAG_testing_serialization_file);
392 PartialSerializer p_ser(&startup_serializer, &partial_sink);
393 p_ser.Serialize(&raw_foo);
394 startup_serializer.SerializeWeakReferences();
395 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE),
396 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
397 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
398 p_ser.CurrentAllocationAddress(CODE_SPACE),
399 p_ser.CurrentAllocationAddress(MAP_SPACE),
400 p_ser.CurrentAllocationAddress(CELL_SPACE),
401 p_ser.CurrentAllocationAddress(LO_SPACE));
Leon Clarkee46be812010-01-19 14:06:41 +0000402}
403
404
Andrei Popescu31002712010-02-23 13:46:05 +0000405static void ReserveSpaceForPartialSnapshot(const char* file_name) {
406 int file_name_length = StrLength(file_name) + 10;
Leon Clarkee46be812010-01-19 14:06:41 +0000407 Vector<char> name = Vector<char>::New(file_name_length + 1);
408 OS::SNPrintF(name, "%s.size", file_name);
409 FILE* fp = OS::FOpen(name.start(), "r");
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800410 name.Dispose();
Leon Clarkee46be812010-01-19 14:06:41 +0000411 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);
Steve Block44f0eee2011-05-26 01:26:41 +0100429 HEAP->ReserveSpace(new_size,
Leon Clarkee46be812010-01-19 14:06:41 +0000430 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()));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800446 startup_name.Dispose();
Andrei Popescu31002712010-02-23 13:46:05 +0000447
448 const char* file_name = FLAG_testing_serialization_file;
449 ReserveSpaceForPartialSnapshot(file_name);
450
451 int snapshot_size = 0;
452 byte* snapshot = ReadBytes(file_name, &snapshot_size);
453
454 Object* root;
455 {
456 SnapshotByteSource source(snapshot, snapshot_size);
457 Deserializer deserializer(&source);
458 deserializer.DeserializePartial(&root);
459 CHECK(root->IsString());
460 }
461 v8::HandleScope handle_scope;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000462 Handle<Object> root_handle(root);
463
464 ReserveSpaceForPartialSnapshot(file_name);
Andrei Popescu31002712010-02-23 13:46:05 +0000465
466 Object* root2;
467 {
468 SnapshotByteSource source(snapshot, snapshot_size);
469 Deserializer deserializer(&source);
470 deserializer.DeserializePartial(&root2);
471 CHECK(root2->IsString());
472 CHECK(*root_handle == root2);
473 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000474 }
Andrei Popescu31002712010-02-23 13:46:05 +0000475}
Leon Clarked91b9f72010-01-27 17:25:45 +0000476
Andrei Popescu31002712010-02-23 13:46:05 +0000477
478TEST(ContextSerialization) {
479 Serializer::Enable();
480 v8::V8::Initialize();
481
482 v8::Persistent<v8::Context> env = v8::Context::New();
483 ASSERT(!env.IsEmpty());
484 env->Enter();
485 // Make sure all builtin scripts are cached.
486 { HandleScope scope;
487 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
Steve Block44f0eee2011-05-26 01:26:41 +0100488 Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
Andrei Popescu31002712010-02-23 13:46:05 +0000489 }
490 }
491 // If we don't do this then we end up with a stray root pointing at the
492 // context even after we have disposed of env.
Steve Block44f0eee2011-05-26 01:26:41 +0100493 HEAP->CollectAllGarbage(true);
Andrei Popescu31002712010-02-23 13:46:05 +0000494
495 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
496 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
497 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
498
499 env->Exit();
500
501 Object* raw_context = *(v8::Utils::OpenHandle(*env));
502
503 env.Dispose();
504
505 FileByteSink startup_sink(startup_name.start());
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800506 startup_name.Dispose();
Andrei Popescu31002712010-02-23 13:46:05 +0000507 StartupSerializer startup_serializer(&startup_sink);
508 startup_serializer.SerializeStrongReferences();
509
510 FileByteSink partial_sink(FLAG_testing_serialization_file);
511 PartialSerializer p_ser(&startup_serializer, &partial_sink);
512 p_ser.Serialize(&raw_context);
513 startup_serializer.SerializeWeakReferences();
514 partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE),
515 p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE),
516 p_ser.CurrentAllocationAddress(OLD_DATA_SPACE),
517 p_ser.CurrentAllocationAddress(CODE_SPACE),
518 p_ser.CurrentAllocationAddress(MAP_SPACE),
519 p_ser.CurrentAllocationAddress(CELL_SPACE),
520 p_ser.CurrentAllocationAddress(LO_SPACE));
521}
522
523
524DEPENDENT_TEST(ContextDeserialization, ContextSerialization) {
525 if (!Snapshot::IsEnabled()) {
526 int file_name_length = StrLength(FLAG_testing_serialization_file) + 10;
527 Vector<char> startup_name = Vector<char>::New(file_name_length + 1);
528 OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file);
529
530 CHECK(Snapshot::Initialize(startup_name.start()));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800531 startup_name.Dispose();
Andrei Popescu31002712010-02-23 13:46:05 +0000532
533 const char* file_name = FLAG_testing_serialization_file;
534 ReserveSpaceForPartialSnapshot(file_name);
535
536 int snapshot_size = 0;
537 byte* snapshot = ReadBytes(file_name, &snapshot_size);
538
539 Object* root;
540 {
541 SnapshotByteSource source(snapshot, snapshot_size);
542 Deserializer deserializer(&source);
543 deserializer.DeserializePartial(&root);
544 CHECK(root->IsContext());
545 }
546 v8::HandleScope handle_scope;
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000547 Handle<Object> root_handle(root);
548
549 ReserveSpaceForPartialSnapshot(file_name);
Andrei Popescu31002712010-02-23 13:46:05 +0000550
551 Object* root2;
552 {
553 SnapshotByteSource source(snapshot, snapshot_size);
554 Deserializer deserializer(&source);
555 deserializer.DeserializePartial(&root2);
556 CHECK(root2->IsContext());
557 CHECK(*root_handle != root2);
558 }
Leon Clarked91b9f72010-01-27 17:25:45 +0000559 }
Leon Clarkee46be812010-01-19 14:06:41 +0000560}
561
562
563TEST(LinearAllocation) {
564 v8::V8::Initialize();
565 int new_space_max = 512 * KB;
Andrei Popescu31002712010-02-23 13:46:05 +0000566
Leon Clarkee46be812010-01-19 14:06:41 +0000567 for (int size = 1000; size < 5 * MB; size += size >> 1) {
568 int new_space_size = (size < new_space_max) ? size : new_space_max;
Steve Block44f0eee2011-05-26 01:26:41 +0100569 HEAP->ReserveSpace(
Leon Clarkee46be812010-01-19 14:06:41 +0000570 new_space_size,
571 size, // Old pointer space.
572 size, // Old data space.
573 size, // Code space.
574 size, // Map space.
575 size, // Cell space.
576 size); // Large object space.
577 LinearAllocationScope linear_allocation_scope;
578 const int kSmallFixedArrayLength = 4;
579 const int kSmallFixedArraySize =
580 FixedArray::kHeaderSize + kSmallFixedArrayLength * kPointerSize;
581 const int kSmallStringLength = 16;
582 const int kSmallStringSize =
Steve Block6ded16b2010-05-10 14:33:55 +0100583 (SeqAsciiString::kHeaderSize + kSmallStringLength +
584 kObjectAlignmentMask) & ~kObjectAlignmentMask;
Leon Clarkee46be812010-01-19 14:06:41 +0000585 const int kMapSize = Map::kSize;
586
587 Object* new_last = NULL;
588 for (int i = 0;
589 i + kSmallFixedArraySize <= new_space_size;
590 i += kSmallFixedArraySize) {
John Reck59135872010-11-02 12:39:01 -0700591 Object* obj =
Steve Block44f0eee2011-05-26 01:26:41 +0100592 HEAP->AllocateFixedArray(kSmallFixedArrayLength)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000593 if (new_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100594 CHECK(reinterpret_cast<char*>(obj) ==
595 reinterpret_cast<char*>(new_last) + kSmallFixedArraySize);
Leon Clarkee46be812010-01-19 14:06:41 +0000596 }
597 new_last = obj;
598 }
599
600 Object* pointer_last = NULL;
601 for (int i = 0;
602 i + kSmallFixedArraySize <= size;
603 i += kSmallFixedArraySize) {
Steve Block44f0eee2011-05-26 01:26:41 +0100604 Object* obj = HEAP->AllocateFixedArray(kSmallFixedArrayLength,
John Reck59135872010-11-02 12:39:01 -0700605 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000606 int old_page_fullness = i % Page::kPageSize;
607 int page_fullness = (i + kSmallFixedArraySize) % Page::kPageSize;
608 if (page_fullness < old_page_fullness ||
609 page_fullness > Page::kObjectAreaSize) {
610 i = RoundUp(i, Page::kPageSize);
611 pointer_last = NULL;
612 }
613 if (pointer_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100614 CHECK(reinterpret_cast<char*>(obj) ==
615 reinterpret_cast<char*>(pointer_last) + kSmallFixedArraySize);
Leon Clarkee46be812010-01-19 14:06:41 +0000616 }
617 pointer_last = obj;
618 }
619
620 Object* data_last = NULL;
621 for (int i = 0; i + kSmallStringSize <= size; i += kSmallStringSize) {
Steve Block44f0eee2011-05-26 01:26:41 +0100622 Object* obj = HEAP->AllocateRawAsciiString(kSmallStringLength,
John Reck59135872010-11-02 12:39:01 -0700623 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000624 int old_page_fullness = i % Page::kPageSize;
625 int page_fullness = (i + kSmallStringSize) % Page::kPageSize;
626 if (page_fullness < old_page_fullness ||
627 page_fullness > Page::kObjectAreaSize) {
628 i = RoundUp(i, Page::kPageSize);
629 data_last = NULL;
630 }
631 if (data_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100632 CHECK(reinterpret_cast<char*>(obj) ==
633 reinterpret_cast<char*>(data_last) + kSmallStringSize);
Leon Clarkee46be812010-01-19 14:06:41 +0000634 }
635 data_last = obj;
636 }
637
638 Object* map_last = NULL;
639 for (int i = 0; i + kMapSize <= size; i += kMapSize) {
Steve Block44f0eee2011-05-26 01:26:41 +0100640 Object* obj = HEAP->AllocateMap(JS_OBJECT_TYPE,
John Reck59135872010-11-02 12:39:01 -0700641 42 * kPointerSize)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000642 int old_page_fullness = i % Page::kPageSize;
643 int page_fullness = (i + kMapSize) % Page::kPageSize;
644 if (page_fullness < old_page_fullness ||
645 page_fullness > Page::kObjectAreaSize) {
646 i = RoundUp(i, Page::kPageSize);
647 map_last = NULL;
648 }
649 if (map_last != NULL) {
Steve Block6ded16b2010-05-10 14:33:55 +0100650 CHECK(reinterpret_cast<char*>(obj) ==
651 reinterpret_cast<char*>(map_last) + kMapSize);
Leon Clarkee46be812010-01-19 14:06:41 +0000652 }
653 map_last = obj;
654 }
655
656 if (size > Page::kObjectAreaSize) {
657 // Support for reserving space in large object space is not there yet,
658 // but using an always-allocate scope is fine for now.
659 AlwaysAllocateScope always;
660 int large_object_array_length =
661 (size - FixedArray::kHeaderSize) / kPointerSize;
Steve Block44f0eee2011-05-26 01:26:41 +0100662 Object* obj = HEAP->AllocateFixedArray(large_object_array_length,
John Reck59135872010-11-02 12:39:01 -0700663 TENURED)->ToObjectChecked();
Leon Clarkee46be812010-01-19 14:06:41 +0000664 CHECK(!obj->IsFailure());
665 }
666 }
667}
668
669
Steve Block3ce2e202009-11-05 08:53:23 +0000670TEST(TestThatAlwaysSucceeds) {
671}
672
673
674TEST(TestThatAlwaysFails) {
675 bool ArtificialFailure = false;
676 CHECK(ArtificialFailure);
677}
678
679
680DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) {
681 bool ArtificialFailure2 = false;
682 CHECK(ArtificialFailure2);
683}