Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 1 | // Copyright 2007-2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // 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 Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 40 | #include "spaces.h" |
| 41 | #include "objects.h" |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 42 | #include "natives.h" |
| 43 | #include "bootstrapper.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace v8::internal; |
| 46 | |
| 47 | static const unsigned kCounters = 256; |
| 48 | static int local_counters[kCounters]; |
| 49 | static const char* local_counter_names[kCounters]; |
| 50 | |
| 51 | |
| 52 | static 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. |
| 63 | static 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 | |
| 84 | template <class T> |
| 85 | static Address AddressOf(T id) { |
| 86 | return ExternalReference(id).address(); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | template <class T> |
| 91 | static uint32_t Encode(const ExternalReferenceEncoder& encoder, T id) { |
| 92 | return encoder.Encode(AddressOf(id)); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static int make_code(TypeCode type, int id) { |
| 97 | return static_cast<uint32_t>(type) << kReferenceTypeShift | id; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | static int register_code(int reg) { |
| 102 | return Debug::k_register_address << kDebugIdShift | reg; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | TEST(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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | 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 Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 126 | ExternalReference stack_limit_address = |
| 127 | ExternalReference::address_of_stack_limit(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 128 | CHECK_EQ(make_code(UNCLASSIFIED, 4), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 129 | 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())); |
| 134 | CHECK_EQ(make_code(UNCLASSIFIED, 11), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 135 | encoder.Encode(ExternalReference::debug_break().address())); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 136 | CHECK_EQ(make_code(UNCLASSIFIED, 7), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 137 | encoder.Encode(ExternalReference::new_space_start().address())); |
| 138 | CHECK_EQ(make_code(UNCLASSIFIED, 3), |
| 139 | encoder.Encode(ExternalReference::roots_address().address())); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | TEST(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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | CHECK_EQ(ExternalReference::the_hole_value_location().address(), |
| 162 | decoder.Decode(make_code(UNCLASSIFIED, 2))); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 163 | CHECK_EQ(ExternalReference::address_of_stack_limit().address(), |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 164 | decoder.Decode(make_code(UNCLASSIFIED, 4))); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 165 | CHECK_EQ(ExternalReference::address_of_real_stack_limit().address(), |
| 166 | decoder.Decode(make_code(UNCLASSIFIED, 5))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 167 | CHECK_EQ(ExternalReference::debug_break().address(), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 168 | decoder.Decode(make_code(UNCLASSIFIED, 11))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 169 | CHECK_EQ(ExternalReference::new_space_start().address(), |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 170 | decoder.Decode(make_code(UNCLASSIFIED, 7))); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 174 | class 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 | |
| 212 | void 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 | |
| 235 | static 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 Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | static void Serialize() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 244 | // 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 Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 250 | WriteToFile(FLAG_testing_serialization_file); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 254 | // Test that the whole heap can be serialized. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | TEST(Serialize) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 256 | Serializer::Enable(); |
| 257 | v8::V8::Initialize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 258 | Serialize(); |
| 259 | } |
| 260 | |
| 261 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 262 | // Test that heap serialization is non-destructive. |
| 263 | TEST(SerializeTwice) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 264 | Serializer::Enable(); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 265 | v8::V8::Initialize(); |
| 266 | Serialize(); |
| 267 | Serialize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 270 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | //---------------------------------------------------------------------------- |
| 272 | // Tests that the heap can be deserialized. |
| 273 | |
| 274 | static void Deserialize() { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 275 | CHECK(Snapshot::Initialize(FLAG_testing_serialization_file)); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | static 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()); |
| 286 | CHECK(Top::special_function_table()->IsFixedArray()); |
| 287 | CHECK(Heap::symbol_table()->IsSymbolTable()); |
| 288 | CHECK(!Factory::LookupAsciiSymbol("Empty")->IsFailure()); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | DEPENDENT_TEST(Deserialize, Serialize) { |
| 293 | v8::HandleScope scope; |
| 294 | |
| 295 | Deserialize(); |
| 296 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 297 | v8::Persistent<v8::Context> env = v8::Context::New(); |
| 298 | env->Enter(); |
| 299 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 300 | SanityCheck(); |
| 301 | } |
| 302 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 303 | |
| 304 | DEPENDENT_TEST(DeserializeFromSecondSerialization, SerializeTwice) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 305 | v8::HandleScope scope; |
| 306 | |
| 307 | Deserialize(); |
| 308 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 309 | v8::Persistent<v8::Context> env = v8::Context::New(); |
| 310 | env->Enter(); |
| 311 | |
| 312 | SanityCheck(); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | DEPENDENT_TEST(DeserializeAndRunScript2, Serialize) { |
| 317 | v8::HandleScope scope; |
| 318 | |
| 319 | Deserialize(); |
| 320 | |
| 321 | v8::Persistent<v8::Context> env = v8::Context::New(); |
| 322 | env->Enter(); |
| 323 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 324 | const char* c_source = "\"1234\".length"; |
| 325 | v8::Local<v8::String> source = v8::String::New(c_source); |
| 326 | v8::Local<v8::Script> script = v8::Script::Compile(source); |
| 327 | CHECK_EQ(4, script->Run()->Int32Value()); |
| 328 | } |
| 329 | |
| 330 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 331 | DEPENDENT_TEST(DeserializeFromSecondSerializationAndRunScript2, |
| 332 | SerializeTwice) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 333 | v8::HandleScope scope; |
| 334 | |
| 335 | Deserialize(); |
| 336 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 337 | v8::Persistent<v8::Context> env = v8::Context::New(); |
| 338 | env->Enter(); |
| 339 | |
| 340 | const char* c_source = "\"1234\".length"; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 341 | v8::Local<v8::String> source = v8::String::New(c_source); |
| 342 | v8::Local<v8::Script> script = v8::Script::Compile(source); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 343 | CHECK_EQ(4, script->Run()->Int32Value()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 347 | TEST(PartialSerialization) { |
| 348 | Serializer::Enable(); |
| 349 | v8::V8::Initialize(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 350 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 351 | v8::Persistent<v8::Context> env = v8::Context::New(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 352 | ASSERT(!env.IsEmpty()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 353 | env->Enter(); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 354 | // Make sure all builtin scripts are cached. |
| 355 | { HandleScope scope; |
| 356 | for (int i = 0; i < Natives::GetBuiltinsCount(); i++) { |
| 357 | Bootstrapper::NativesSourceLookup(i); |
| 358 | } |
| 359 | } |
| 360 | Heap::CollectAllGarbage(true); |
| 361 | Heap::CollectAllGarbage(true); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 362 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 363 | Object* raw_foo; |
| 364 | { |
| 365 | v8::HandleScope handle_scope; |
| 366 | v8::Local<v8::String> foo = v8::String::New("foo"); |
| 367 | ASSERT(!foo.IsEmpty()); |
| 368 | raw_foo = *(v8::Utils::OpenHandle(*foo)); |
| 369 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 370 | |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 371 | int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; |
| 372 | Vector<char> startup_name = Vector<char>::New(file_name_length + 1); |
| 373 | OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); |
| 374 | |
| 375 | env->Exit(); |
| 376 | env.Dispose(); |
| 377 | |
| 378 | FileByteSink startup_sink(startup_name.start()); |
| 379 | StartupSerializer startup_serializer(&startup_sink); |
| 380 | startup_serializer.SerializeStrongReferences(); |
| 381 | |
| 382 | FileByteSink partial_sink(FLAG_testing_serialization_file); |
| 383 | PartialSerializer p_ser(&startup_serializer, &partial_sink); |
| 384 | p_ser.Serialize(&raw_foo); |
| 385 | startup_serializer.SerializeWeakReferences(); |
| 386 | partial_sink.WriteSpaceUsed(p_ser.CurrentAllocationAddress(NEW_SPACE), |
| 387 | p_ser.CurrentAllocationAddress(OLD_POINTER_SPACE), |
| 388 | p_ser.CurrentAllocationAddress(OLD_DATA_SPACE), |
| 389 | p_ser.CurrentAllocationAddress(CODE_SPACE), |
| 390 | p_ser.CurrentAllocationAddress(MAP_SPACE), |
| 391 | p_ser.CurrentAllocationAddress(CELL_SPACE), |
| 392 | p_ser.CurrentAllocationAddress(LO_SPACE)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | |
| 396 | DEPENDENT_TEST(PartialDeserialization, PartialSerialization) { |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 397 | int file_name_length = StrLength(FLAG_testing_serialization_file) + 10; |
| 398 | Vector<char> startup_name = Vector<char>::New(file_name_length + 1); |
| 399 | OS::SNPrintF(startup_name, "%s.startup", FLAG_testing_serialization_file); |
| 400 | |
| 401 | CHECK(Snapshot::Initialize(startup_name.start())); |
| 402 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 403 | const char* file_name = FLAG_testing_serialization_file; |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 404 | Vector<char> name = Vector<char>::New(file_name_length + 1); |
| 405 | OS::SNPrintF(name, "%s.size", file_name); |
| 406 | FILE* fp = OS::FOpen(name.start(), "r"); |
| 407 | int new_size, pointer_size, data_size, code_size, map_size, cell_size; |
| 408 | int large_size; |
| 409 | #ifdef _MSC_VER |
| 410 | // Avoid warning about unsafe fscanf from MSVC. |
| 411 | // Please note that this is only fine if %c and %s are not being used. |
| 412 | #define fscanf fscanf_s |
| 413 | #endif |
| 414 | CHECK_EQ(1, fscanf(fp, "new %d\n", &new_size)); |
| 415 | CHECK_EQ(1, fscanf(fp, "pointer %d\n", &pointer_size)); |
| 416 | CHECK_EQ(1, fscanf(fp, "data %d\n", &data_size)); |
| 417 | CHECK_EQ(1, fscanf(fp, "code %d\n", &code_size)); |
| 418 | CHECK_EQ(1, fscanf(fp, "map %d\n", &map_size)); |
| 419 | CHECK_EQ(1, fscanf(fp, "cell %d\n", &cell_size)); |
| 420 | CHECK_EQ(1, fscanf(fp, "large %d\n", &large_size)); |
| 421 | #ifdef _MSC_VER |
| 422 | #undef fscanf |
| 423 | #endif |
| 424 | fclose(fp); |
| 425 | Heap::ReserveSpace(new_size, |
| 426 | pointer_size, |
| 427 | data_size, |
| 428 | code_size, |
| 429 | map_size, |
| 430 | cell_size, |
| 431 | large_size); |
| 432 | int snapshot_size = 0; |
| 433 | byte* snapshot = ReadBytes(file_name, &snapshot_size); |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 434 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 435 | Object* root; |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame^] | 436 | { |
| 437 | SnapshotByteSource source(snapshot, snapshot_size); |
| 438 | Deserializer deserializer(&source); |
| 439 | deserializer.DeserializePartial(&root); |
| 440 | CHECK(root->IsString()); |
| 441 | } |
| 442 | v8::HandleScope handle_scope; |
| 443 | Handle<Object>root_handle(root); |
| 444 | |
| 445 | Object* root2; |
| 446 | { |
| 447 | SnapshotByteSource source(snapshot, snapshot_size); |
| 448 | Deserializer deserializer(&source); |
| 449 | deserializer.DeserializePartial(&root2); |
| 450 | CHECK(root2->IsString()); |
| 451 | CHECK(*root_handle == root2); |
| 452 | } |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
| 456 | TEST(LinearAllocation) { |
| 457 | v8::V8::Initialize(); |
| 458 | int new_space_max = 512 * KB; |
| 459 | for (int size = 1000; size < 5 * MB; size += size >> 1) { |
| 460 | int new_space_size = (size < new_space_max) ? size : new_space_max; |
| 461 | Heap::ReserveSpace( |
| 462 | new_space_size, |
| 463 | size, // Old pointer space. |
| 464 | size, // Old data space. |
| 465 | size, // Code space. |
| 466 | size, // Map space. |
| 467 | size, // Cell space. |
| 468 | size); // Large object space. |
| 469 | LinearAllocationScope linear_allocation_scope; |
| 470 | const int kSmallFixedArrayLength = 4; |
| 471 | const int kSmallFixedArraySize = |
| 472 | FixedArray::kHeaderSize + kSmallFixedArrayLength * kPointerSize; |
| 473 | const int kSmallStringLength = 16; |
| 474 | const int kSmallStringSize = |
| 475 | SeqAsciiString::kHeaderSize + kSmallStringLength; |
| 476 | const int kMapSize = Map::kSize; |
| 477 | |
| 478 | Object* new_last = NULL; |
| 479 | for (int i = 0; |
| 480 | i + kSmallFixedArraySize <= new_space_size; |
| 481 | i += kSmallFixedArraySize) { |
| 482 | Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength); |
| 483 | if (new_last != NULL) { |
| 484 | CHECK_EQ(reinterpret_cast<char*>(obj), |
| 485 | reinterpret_cast<char*>(new_last) + kSmallFixedArraySize); |
| 486 | } |
| 487 | new_last = obj; |
| 488 | } |
| 489 | |
| 490 | Object* pointer_last = NULL; |
| 491 | for (int i = 0; |
| 492 | i + kSmallFixedArraySize <= size; |
| 493 | i += kSmallFixedArraySize) { |
| 494 | Object* obj = Heap::AllocateFixedArray(kSmallFixedArrayLength, TENURED); |
| 495 | int old_page_fullness = i % Page::kPageSize; |
| 496 | int page_fullness = (i + kSmallFixedArraySize) % Page::kPageSize; |
| 497 | if (page_fullness < old_page_fullness || |
| 498 | page_fullness > Page::kObjectAreaSize) { |
| 499 | i = RoundUp(i, Page::kPageSize); |
| 500 | pointer_last = NULL; |
| 501 | } |
| 502 | if (pointer_last != NULL) { |
| 503 | CHECK_EQ(reinterpret_cast<char*>(obj), |
| 504 | reinterpret_cast<char*>(pointer_last) + kSmallFixedArraySize); |
| 505 | } |
| 506 | pointer_last = obj; |
| 507 | } |
| 508 | |
| 509 | Object* data_last = NULL; |
| 510 | for (int i = 0; i + kSmallStringSize <= size; i += kSmallStringSize) { |
| 511 | Object* obj = Heap::AllocateRawAsciiString(kSmallStringLength, TENURED); |
| 512 | int old_page_fullness = i % Page::kPageSize; |
| 513 | int page_fullness = (i + kSmallStringSize) % Page::kPageSize; |
| 514 | if (page_fullness < old_page_fullness || |
| 515 | page_fullness > Page::kObjectAreaSize) { |
| 516 | i = RoundUp(i, Page::kPageSize); |
| 517 | data_last = NULL; |
| 518 | } |
| 519 | if (data_last != NULL) { |
| 520 | CHECK_EQ(reinterpret_cast<char*>(obj), |
| 521 | reinterpret_cast<char*>(data_last) + kSmallStringSize); |
| 522 | } |
| 523 | data_last = obj; |
| 524 | } |
| 525 | |
| 526 | Object* map_last = NULL; |
| 527 | for (int i = 0; i + kMapSize <= size; i += kMapSize) { |
| 528 | Object* obj = Heap::AllocateMap(JS_OBJECT_TYPE, 42 * kPointerSize); |
| 529 | int old_page_fullness = i % Page::kPageSize; |
| 530 | int page_fullness = (i + kMapSize) % Page::kPageSize; |
| 531 | if (page_fullness < old_page_fullness || |
| 532 | page_fullness > Page::kObjectAreaSize) { |
| 533 | i = RoundUp(i, Page::kPageSize); |
| 534 | map_last = NULL; |
| 535 | } |
| 536 | if (map_last != NULL) { |
| 537 | CHECK_EQ(reinterpret_cast<char*>(obj), |
| 538 | reinterpret_cast<char*>(map_last) + kMapSize); |
| 539 | } |
| 540 | map_last = obj; |
| 541 | } |
| 542 | |
| 543 | if (size > Page::kObjectAreaSize) { |
| 544 | // Support for reserving space in large object space is not there yet, |
| 545 | // but using an always-allocate scope is fine for now. |
| 546 | AlwaysAllocateScope always; |
| 547 | int large_object_array_length = |
| 548 | (size - FixedArray::kHeaderSize) / kPointerSize; |
| 549 | Object* obj = Heap::AllocateFixedArray(large_object_array_length, |
| 550 | TENURED); |
| 551 | CHECK(!obj->IsFailure()); |
| 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 557 | TEST(TestThatAlwaysSucceeds) { |
| 558 | } |
| 559 | |
| 560 | |
| 561 | TEST(TestThatAlwaysFails) { |
| 562 | bool ArtificialFailure = false; |
| 563 | CHECK(ArtificialFailure); |
| 564 | } |
| 565 | |
| 566 | |
| 567 | DEPENDENT_TEST(DependentTestThatAlwaysFails, TestThatAlwaysSucceeds) { |
| 568 | bool ArtificialFailure2 = false; |
| 569 | CHECK(ArtificialFailure2); |
| 570 | } |