blob: 8e85444eee9faf38951d00d89e5897f3ba094240 [file] [log] [blame]
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +00001// Copyright 2007-2010 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +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>
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000029
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"
fschneider@chromium.org0c20e672010-01-14 15:28:53 +000040#include "spaces.h"
41#include "objects.h"
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +000042#include "natives.h"
43#include "bootstrapper.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000044
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000045using namespace v8::internal;
46
ager@chromium.org5ec48922009-05-05 07:25:34 +000047static 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}
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000060
61
62// Callback receiver to track counters in test.
ager@chromium.orga74f0da2008-12-03 16:05:52 +000063static int* counter_function(const char* name) {
ager@chromium.org5ec48922009-05-05 07:25:34 +000064 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.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000080 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000081}
82
83
84template <class T>
85static Address AddressOf(T id) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000086 return ExternalReference(id, i::Isolate::Current()).address();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +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
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000101TEST(ExternalReferenceEncoder) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000102 Isolate* isolate = i::Isolate::Current();
103 isolate->stats_table()->SetCounterFunction(counter_function);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000104 v8::V8::Initialize();
105
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000106 ExternalReferenceEncoder encoder;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000107 CHECK_EQ(make_code(BUILTIN, Builtins::kArrayCode),
108 Encode(encoder, Builtins::kArrayCode));
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000109 CHECK_EQ(make_code(v8::internal::RUNTIME_FUNCTION, Runtime::kAbort),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000110 Encode(encoder, Runtime::kAbort));
111 CHECK_EQ(make_code(IC_UTILITY, IC::kLoadCallbackProperty),
112 Encode(encoder, IC_Utility(IC::kLoadCallbackProperty)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000113 ExternalReference keyed_load_function_prototype =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000114 ExternalReference(isolate->counters()->keyed_load_function_prototype());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000115 CHECK_EQ(make_code(STATS_COUNTER, Counters::k_keyed_load_function_prototype),
116 encoder.Encode(keyed_load_function_prototype.address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000117 ExternalReference the_hole_value_location =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000118 ExternalReference::the_hole_value_location(isolate);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000119 CHECK_EQ(make_code(UNCLASSIFIED, 2),
120 encoder.Encode(the_hole_value_location.address()));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000121 ExternalReference stack_limit_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000122 ExternalReference::address_of_stack_limit(isolate);
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000123 CHECK_EQ(make_code(UNCLASSIFIED, 4),
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000124 encoder.Encode(stack_limit_address.address()));
125 ExternalReference real_stack_limit_address =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000126 ExternalReference::address_of_real_stack_limit(isolate);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000127 CHECK_EQ(make_code(UNCLASSIFIED, 5),
128 encoder.Encode(real_stack_limit_address.address()));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000129#ifdef ENABLE_DEBUGGER_SUPPORT
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000130 CHECK_EQ(make_code(UNCLASSIFIED, 15),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000131 encoder.Encode(ExternalReference::debug_break(isolate).address()));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000132#endif // ENABLE_DEBUGGER_SUPPORT
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000133 CHECK_EQ(make_code(UNCLASSIFIED, 10),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000134 encoder.Encode(
135 ExternalReference::new_space_start(isolate).address()));
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000136 CHECK_EQ(make_code(UNCLASSIFIED, 3),
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000137 encoder.Encode(ExternalReference::roots_address(isolate).address()));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000138}
139
140
141TEST(ExternalReferenceDecoder) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000142 Isolate* isolate = i::Isolate::Current();
143 isolate->stats_table()->SetCounterFunction(counter_function);
kmillikin@chromium.org7c2628c2011-08-10 11:27:35 +0000144 v8::V8::Initialize();
145
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000146 ExternalReferenceDecoder decoder;
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000147 CHECK_EQ(AddressOf(Builtins::kArrayCode),
148 decoder.Decode(make_code(BUILTIN, Builtins::kArrayCode)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000149 CHECK_EQ(AddressOf(Runtime::kAbort),
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000150 decoder.Decode(make_code(v8::internal::RUNTIME_FUNCTION,
151 Runtime::kAbort)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000152 CHECK_EQ(AddressOf(IC_Utility(IC::kLoadCallbackProperty)),
153 decoder.Decode(make_code(IC_UTILITY, IC::kLoadCallbackProperty)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000154 ExternalReference keyed_load_function =
fschneider@chromium.org7979bbb2011-03-28 10:47:03 +0000155 ExternalReference(isolate->counters()->keyed_load_function_prototype());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000156 CHECK_EQ(keyed_load_function.address(),
157 decoder.Decode(
158 make_code(STATS_COUNTER,
159 Counters::k_keyed_load_function_prototype)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000160 CHECK_EQ(ExternalReference::the_hole_value_location(isolate).address(),
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000161 decoder.Decode(make_code(UNCLASSIFIED, 2)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000162 CHECK_EQ(ExternalReference::address_of_stack_limit(isolate).address(),
ager@chromium.orgab99eea2009-08-25 07:05:41 +0000163 decoder.Decode(make_code(UNCLASSIFIED, 4)));
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000164 CHECK_EQ(ExternalReference::address_of_real_stack_limit(isolate).address(),
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000165 decoder.Decode(make_code(UNCLASSIFIED, 5)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000166#ifdef ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000167 CHECK_EQ(ExternalReference::debug_break(isolate).address(),
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000168 decoder.Decode(make_code(UNCLASSIFIED, 15)));
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000169#endif // ENABLE_DEBUGGER_SUPPORT
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000170 CHECK_EQ(ExternalReference::new_space_start(isolate).address(),
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000171 decoder.Decode(make_code(UNCLASSIFIED, 10)));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000172}
173
174
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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");
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000225 name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000245static void Serialize() {
ager@chromium.org3811b432009-10-28 14:53:37 +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();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000252 WriteToFile(FLAG_testing_serialization_file);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000253}
254
255
ager@chromium.org3811b432009-10-28 14:53:37 +0000256// Test that the whole heap can be serialized.
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000257TEST(Serialize) {
kasperl@chromium.org061ef742009-02-27 12:16:20 +0000258 Serializer::Enable();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000259 v8::V8::Initialize();
260 Serialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000261}
262
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000263
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000264// Test that heap serialization is non-destructive.
265TEST(SerializeTwice) {
266 Serializer::Enable();
267 v8::V8::Initialize();
268 Serialize();
269 Serialize();
270}
271
272
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000273//----------------------------------------------------------------------------
274// Tests that the heap can be deserialized.
275
276static void Deserialize() {
kasperl@chromium.orgb9123622008-09-17 14:05:56 +0000277 CHECK(Snapshot::Initialize(FLAG_testing_serialization_file));
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000278}
279
280
281static void SanityCheck() {
282 v8::HandleScope scope;
283#ifdef DEBUG
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000284 HEAP->Verify();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000285#endif
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000286 CHECK(Isolate::Current()->global()->IsJSObject());
287 CHECK(Isolate::Current()->global_context()->IsContext());
288 CHECK(HEAP->symbol_table()->IsSymbolTable());
289 CHECK(!FACTORY->LookupAsciiSymbol("Empty")->IsFailure());
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000290}
291
292
iposva@chromium.org245aa852009-02-10 00:49:54 +0000293DEPENDENT_TEST(Deserialize, Serialize) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000299 Deserialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000300
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000301 v8::Persistent<v8::Context> env = v8::Context::New();
302 env->Enter();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000303
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000304 SanityCheck();
305 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000306}
307
308
309DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000310 if (!Snapshot::IsEnabled()) {
311 v8::HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000312 Deserialize();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000313
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000314 v8::Persistent<v8::Context> env = v8::Context::New();
315 env->Enter();
ager@chromium.org3811b432009-10-28 14:53:37 +0000316
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000317 SanityCheck();
318 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000319}
320
321
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000322DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000323 if (!Snapshot::IsEnabled()) {
324 v8::HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000325 Deserialize();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000326
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000327 v8::Persistent<v8::Context> env = v8::Context::New();
328 env->Enter();
ager@chromium.org3811b432009-10-28 14:53:37 +0000329
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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 }
ager@chromium.org3811b432009-10-28 14:53:37 +0000335}
336
337
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000338DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2,
339 SerializeTwice) {
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000340 if (!Snapshot::IsEnabled()) {
341 v8::HandleScope scope;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000342 Deserialize();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000343
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000344 v8::Persistent<v8::Context> env = v8::Context::New();
345 env->Enter();
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000346
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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 }
sgjesse@chromium.orgac6aa172009-12-04 12:29:05 +0000352}
353
354
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000355TEST(PartialSerialization) {
356 Serializer::Enable();
357 v8::V8::Initialize();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000358
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000359 v8::Persistent<v8::Context> env = v8::Context::New();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000360 ASSERT(!env.IsEmpty());
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000361 env->Enter();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000362 // Make sure all builtin scripts are cached.
363 { HandleScope scope;
364 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000365 Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000366 }
367 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000368 HEAP->CollectAllGarbage(true);
369 HEAP->CollectAllGarbage(true);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000370
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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 }
378
379 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());
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000387 startup_name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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));
402}
403
404
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000405static void ReserveSpaceForPartialSnapshot(const char* file_name) {
406 int file_name_length = StrLength(file_name) + 10;
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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");
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000410 name.Dispose();
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +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);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000429 HEAP->ReserveSpace(new_size,
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000430 pointer_size,
431 data_size,
432 code_size,
433 map_size,
434 cell_size,
435 large_size);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000436}
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000437
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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()));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000446 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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;
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +0000462 Handle<Object> root_handle(root);
463
464 ReserveSpaceForPartialSnapshot(file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000474 }
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000475}
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000476
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000488 Isolate::Current()->bootstrapper()->NativesSourceLookup(i);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000493 HEAP->CollectAllGarbage(true);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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());
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000506 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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()));
fschneider@chromium.orge03fb642010-11-01 12:34:09 +0000531 startup_name.Dispose();
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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;
lrn@chromium.orgd4e9e222011-08-03 12:01:58 +0000547 Handle<Object> root_handle(root);
548
549 ReserveSpaceForPartialSnapshot(file_name);
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +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 }
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000559 }
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000560}
561
562
563TEST(LinearAllocation) {
564 v8::V8::Initialize();
565 int new_space_max = 512 * KB;
kmillikin@chromium.org5d8f0e62010-03-24 08:21:20 +0000566
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000567 for (int size = 1000; size < 5 * MB; size += size >> 1) {
568 int new_space_size = (size < new_space_max) ? size : new_space_max;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000569 HEAP->ReserveSpace(
fschneider@chromium.org0c20e672010-01-14 15:28:53 +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 =
ager@chromium.orgac091b72010-05-05 07:34:42 +0000583 (SeqAsciiString::kHeaderSize + kSmallStringLength +
584 kObjectAlignmentMask) & ~kObjectAlignmentMask;
fschneider@chromium.org0c20e672010-01-14 15:28:53 +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) {
lrn@chromium.org303ada72010-10-27 09:33:13 +0000591 Object* obj =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000592 HEAP->AllocateFixedArray(kSmallFixedArrayLength)->ToObjectChecked();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000593 if (new_last != NULL) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000594 CHECK(reinterpret_cast<char*>(obj) ==
595 reinterpret_cast<char*>(new_last) + kSmallFixedArraySize);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000596 }
597 new_last = obj;
598 }
599
600 Object* pointer_last = NULL;
601 for (int i = 0;
602 i + kSmallFixedArraySize <= size;
603 i += kSmallFixedArraySize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000604 Object* obj = HEAP->AllocateFixedArray(kSmallFixedArrayLength,
lrn@chromium.org303ada72010-10-27 09:33:13 +0000605 TENURED)->ToObjectChecked();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +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) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000614 CHECK(reinterpret_cast<char*>(obj) ==
615 reinterpret_cast<char*>(pointer_last) + kSmallFixedArraySize);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000616 }
617 pointer_last = obj;
618 }
619
620 Object* data_last = NULL;
621 for (int i = 0; i + kSmallStringSize <= size; i += kSmallStringSize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000622 Object* obj = HEAP->AllocateRawAsciiString(kSmallStringLength,
lrn@chromium.org303ada72010-10-27 09:33:13 +0000623 TENURED)->ToObjectChecked();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +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) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000632 CHECK(reinterpret_cast<char*>(obj) ==
633 reinterpret_cast<char*>(data_last) + kSmallStringSize);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000634 }
635 data_last = obj;
636 }
637
638 Object* map_last = NULL;
639 for (int i = 0; i + kMapSize <= size; i += kMapSize) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000640 Object* obj = HEAP->AllocateMap(JS_OBJECT_TYPE,
lrn@chromium.org303ada72010-10-27 09:33:13 +0000641 42 * kPointerSize)->ToObjectChecked();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +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) {
ager@chromium.orgce5e87b2010-03-10 10:24:18 +0000650 CHECK(reinterpret_cast<char*>(obj) ==
651 reinterpret_cast<char*>(map_last) + kMapSize);
fschneider@chromium.org0c20e672010-01-14 15:28:53 +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;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000662 Object* obj = HEAP->AllocateFixedArray(large_object_array_length,
lrn@chromium.org303ada72010-10-27 09:33:13 +0000663 TENURED)->ToObjectChecked();
fschneider@chromium.org0c20e672010-01-14 15:28:53 +0000664 CHECK(!obj->IsFailure());
665 }
666 }
667}
668
669
ager@chromium.org3811b432009-10-28 14:53:37 +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}