From 6f17848caa6eb2658bf0e9eb262b060713d32878 Mon Sep 17 00:00:00 2001 From: yangguo Date: Tue, 1 Mar 2016 06:42:57 -0800 Subject: [PATCH] [serializer] split up src/snapshot/serialize.* R=rossberg@chromium.org, ulan@chromium.org, vogelheim@chromium.org Review URL: https://codereview.chromium.org/1751863002 Cr-Commit-Position: refs/heads/master@{#34395} --- BUILD.gn | 15 +- src/assembler.cc | 2 +- src/collector.h | 247 +++ src/compiler.cc | 2 +- src/disassembler.cc | 2 +- src/heap/heap.cc | 7 +- src/isolate.cc | 8 +- src/parsing/preparse-data.h | 1 + src/parsing/scanner.h | 2 +- src/snapshot/code-serializer.cc | 418 ++++ src/snapshot/code-serializer.h | 127 ++ src/snapshot/deserializer.cc | 810 ++++++++ src/snapshot/deserializer.h | 140 ++ src/snapshot/mksnapshot.cc | 4 +- src/snapshot/partial-serializer.cc | 128 ++ src/snapshot/partial-serializer.h | 61 + src/snapshot/serialize.cc | 2874 -------------------------- src/snapshot/serialize.h | 816 -------- src/snapshot/serializer-common.cc | 375 ++++ src/snapshot/serializer-common.h | 322 +++ src/snapshot/serializer.cc | 769 +++++++ src/snapshot/serializer.h | 321 +++ src/snapshot/snapshot-common.cc | 50 + src/snapshot/snapshot-external.cc | 1 - src/snapshot/snapshot-source-sink.cc | 1 - src/snapshot/snapshot.h | 38 +- src/snapshot/startup-serializer.cc | 132 ++ src/snapshot/startup-serializer.h | 41 + src/utils.h | 234 --- src/v8.cc | 1 - test/cctest/test-heap-profiler.cc | 1 + test/cctest/test-serialize.cc | 5 +- test/cctest/test-utils.cc | 1 + tools/external-reference-check.py | 2 +- tools/gyp/v8.gyp | 15 +- 35 files changed, 4026 insertions(+), 3947 deletions(-) create mode 100644 src/collector.h create mode 100644 src/snapshot/code-serializer.cc create mode 100644 src/snapshot/code-serializer.h create mode 100644 src/snapshot/deserializer.cc create mode 100644 src/snapshot/deserializer.h create mode 100644 src/snapshot/partial-serializer.cc create mode 100644 src/snapshot/partial-serializer.h delete mode 100644 src/snapshot/serialize.cc delete mode 100644 src/snapshot/serialize.h create mode 100644 src/snapshot/serializer-common.cc create mode 100644 src/snapshot/serializer-common.h create mode 100644 src/snapshot/serializer.cc create mode 100644 src/snapshot/serializer.h create mode 100644 src/snapshot/startup-serializer.cc create mode 100644 src/snapshot/startup-serializer.h diff --git a/BUILD.gn b/BUILD.gn index e19dd104eb..5306d8759d 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -704,6 +704,7 @@ source_set("v8_base") { "src/code-stubs-hydrogen.cc", "src/codegen.cc", "src/codegen.h", + "src/collector.h", "src/compilation-cache.cc", "src/compilation-cache.h", "src/compilation-dependencies.cc", @@ -1282,13 +1283,23 @@ source_set("v8_base") { "src/signature.h", "src/simulator.h", "src/small-pointer-list.h", + "src/snapshot/code-serializer.cc", + "src/snapshot/code-serializer.h", + "src/snapshot/deserializer.cc", + "src/snapshot/deserializer.h", "src/snapshot/natives.h", "src/snapshot/natives-common.cc", - "src/snapshot/serialize.cc", - "src/snapshot/serialize.h", + "src/snapshot/partial-serializer.cc", + "src/snapshot/partial-serializer.h", + "src/snapshot/serializer.cc", + "src/snapshot/serializer.h", + "src/snapshot/serializer-common.cc", + "src/snapshot/serializer-common.h", "src/snapshot/snapshot-common.cc", "src/snapshot/snapshot-source-sink.cc", "src/snapshot/snapshot-source-sink.h", + "src/snapshot/startup-serializer.cc", + "src/snapshot/startup-serializer.h", "src/source-position.h", "src/splay-tree.h", "src/splay-tree-inl.h", diff --git a/src/assembler.cc b/src/assembler.cc index 5c8c2ce16d..317b0a5fe9 100644 --- a/src/assembler.cc +++ b/src/assembler.cc @@ -60,7 +60,7 @@ #include "src/register-configuration.h" #include "src/runtime/runtime.h" #include "src/simulator.h" // For flushing instruction cache. -#include "src/snapshot/serialize.h" +#include "src/snapshot/serializer-common.h" #if V8_TARGET_ARCH_IA32 #include "src/ia32/assembler-ia32-inl.h" // NOLINT diff --git a/src/collector.h b/src/collector.h new file mode 100644 index 0000000000..8454aae19d --- /dev/null +++ b/src/collector.h @@ -0,0 +1,247 @@ +// Copyright 2016 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_COLLECTOR_H_ +#define V8_COLLECTOR_H_ + +#include "src/checks.h" +#include "src/list.h" +#include "src/vector.h" + +namespace v8 { +namespace internal { + +/* + * A class that collects values into a backing store. + * Specialized versions of the class can allow access to the backing store + * in different ways. + * There is no guarantee that the backing store is contiguous (and, as a + * consequence, no guarantees that consecutively added elements are adjacent + * in memory). The collector may move elements unless it has guaranteed not + * to. + */ +template +class Collector { + public: + explicit Collector(int initial_capacity = kMinCapacity) + : index_(0), size_(0) { + current_chunk_ = Vector::New(initial_capacity); + } + + virtual ~Collector() { + // Free backing store (in reverse allocation order). + current_chunk_.Dispose(); + for (int i = chunks_.length() - 1; i >= 0; i--) { + chunks_.at(i).Dispose(); + } + } + + // Add a single element. + inline void Add(T value) { + if (index_ >= current_chunk_.length()) { + Grow(1); + } + current_chunk_[index_] = value; + index_++; + size_++; + } + + // Add a block of contiguous elements and return a Vector backed by the + // memory area. + // A basic Collector will keep this vector valid as long as the Collector + // is alive. + inline Vector AddBlock(int size, T initial_value) { + DCHECK(size > 0); + if (size > current_chunk_.length() - index_) { + Grow(size); + } + T* position = current_chunk_.start() + index_; + index_ += size; + size_ += size; + for (int i = 0; i < size; i++) { + position[i] = initial_value; + } + return Vector(position, size); + } + + // Add a contiguous block of elements and return a vector backed + // by the added block. + // A basic Collector will keep this vector valid as long as the Collector + // is alive. + inline Vector AddBlock(Vector source) { + if (source.length() > current_chunk_.length() - index_) { + Grow(source.length()); + } + T* position = current_chunk_.start() + index_; + index_ += source.length(); + size_ += source.length(); + for (int i = 0; i < source.length(); i++) { + position[i] = source[i]; + } + return Vector(position, source.length()); + } + + // Write the contents of the collector into the provided vector. + void WriteTo(Vector destination) { + DCHECK(size_ <= destination.length()); + int position = 0; + for (int i = 0; i < chunks_.length(); i++) { + Vector chunk = chunks_.at(i); + for (int j = 0; j < chunk.length(); j++) { + destination[position] = chunk[j]; + position++; + } + } + for (int i = 0; i < index_; i++) { + destination[position] = current_chunk_[i]; + position++; + } + } + + // Allocate a single contiguous vector, copy all the collected + // elements to the vector, and return it. + // The caller is responsible for freeing the memory of the returned + // vector (e.g., using Vector::Dispose). + Vector ToVector() { + Vector new_store = Vector::New(size_); + WriteTo(new_store); + return new_store; + } + + // Resets the collector to be empty. + virtual void Reset() { + for (int i = chunks_.length() - 1; i >= 0; i--) { + chunks_.at(i).Dispose(); + } + chunks_.Rewind(0); + index_ = 0; + size_ = 0; + } + + // Total number of elements added to collector so far. + inline int size() { return size_; } + + protected: + static const int kMinCapacity = 16; + List > chunks_; + Vector current_chunk_; // Block of memory currently being written into. + int index_; // Current index in current chunk. + int size_; // Total number of elements in collector. + + // Creates a new current chunk, and stores the old chunk in the chunks_ list. + void Grow(int min_capacity) { + DCHECK(growth_factor > 1); + int new_capacity; + int current_length = current_chunk_.length(); + if (current_length < kMinCapacity) { + // The collector started out as empty. + new_capacity = min_capacity * growth_factor; + if (new_capacity < kMinCapacity) new_capacity = kMinCapacity; + } else { + int growth = current_length * (growth_factor - 1); + if (growth > max_growth) { + growth = max_growth; + } + new_capacity = current_length + growth; + if (new_capacity < min_capacity) { + new_capacity = min_capacity + growth; + } + } + NewChunk(new_capacity); + DCHECK(index_ + min_capacity <= current_chunk_.length()); + } + + // Before replacing the current chunk, give a subclass the option to move + // some of the current data into the new chunk. The function may update + // the current index_ value to represent data no longer in the current chunk. + // Returns the initial index of the new chunk (after copied data). + virtual void NewChunk(int new_capacity) { + Vector new_chunk = Vector::New(new_capacity); + if (index_ > 0) { + chunks_.Add(current_chunk_.SubVector(0, index_)); + } else { + current_chunk_.Dispose(); + } + current_chunk_ = new_chunk; + index_ = 0; + } +}; + +/* + * A collector that allows sequences of values to be guaranteed to + * stay consecutive. + * If the backing store grows while a sequence is active, the current + * sequence might be moved, but after the sequence is ended, it will + * not move again. + * NOTICE: Blocks allocated using Collector::AddBlock(int) can move + * as well, if inside an active sequence where another element is added. + */ +template +class SequenceCollector : public Collector { + public: + explicit SequenceCollector(int initial_capacity) + : Collector(initial_capacity), + sequence_start_(kNoSequence) {} + + virtual ~SequenceCollector() {} + + void StartSequence() { + DCHECK(sequence_start_ == kNoSequence); + sequence_start_ = this->index_; + } + + Vector EndSequence() { + DCHECK(sequence_start_ != kNoSequence); + int sequence_start = sequence_start_; + sequence_start_ = kNoSequence; + if (sequence_start == this->index_) return Vector(); + return this->current_chunk_.SubVector(sequence_start, this->index_); + } + + // Drops the currently added sequence, and all collected elements in it. + void DropSequence() { + DCHECK(sequence_start_ != kNoSequence); + int sequence_length = this->index_ - sequence_start_; + this->index_ = sequence_start_; + this->size_ -= sequence_length; + sequence_start_ = kNoSequence; + } + + virtual void Reset() { + sequence_start_ = kNoSequence; + this->Collector::Reset(); + } + + private: + static const int kNoSequence = -1; + int sequence_start_; + + // Move the currently active sequence to the new chunk. + virtual void NewChunk(int new_capacity) { + if (sequence_start_ == kNoSequence) { + // Fall back on default behavior if no sequence has been started. + this->Collector::NewChunk(new_capacity); + return; + } + int sequence_length = this->index_ - sequence_start_; + Vector new_chunk = Vector::New(sequence_length + new_capacity); + DCHECK(sequence_length < new_chunk.length()); + for (int i = 0; i < sequence_length; i++) { + new_chunk[i] = this->current_chunk_[sequence_start_ + i]; + } + if (sequence_start_ > 0) { + this->chunks_.Add(this->current_chunk_.SubVector(0, sequence_start_)); + } else { + this->current_chunk_.Dispose(); + } + this->current_chunk_ = new_chunk; + this->index_ = sequence_length; + sequence_start_ = 0; + } +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_COLLECTOR_H_ diff --git a/src/compiler.cc b/src/compiler.cc index 0057a0b9fb..70ebebacc6 100644 --- a/src/compiler.cc +++ b/src/compiler.cc @@ -31,7 +31,7 @@ #include "src/parsing/scanner-character-streams.h" #include "src/profiler/cpu-profiler.h" #include "src/runtime-profiler.h" -#include "src/snapshot/serialize.h" +#include "src/snapshot/code-serializer.h" #include "src/vm-state-inl.h" namespace v8 { diff --git a/src/disassembler.cc b/src/disassembler.cc index 59a57e552e..ed9ca9ac66 100644 --- a/src/disassembler.cc +++ b/src/disassembler.cc @@ -10,7 +10,7 @@ #include "src/deoptimizer.h" #include "src/disasm.h" #include "src/macro-assembler.h" -#include "src/snapshot/serialize.h" +#include "src/snapshot/serializer-common.h" #include "src/string-stream.h" namespace v8 { diff --git a/src/heap/heap.cc b/src/heap/heap.cc index 3b5ee6e81b..ad83dfb8c7 100644 --- a/src/heap/heap.cc +++ b/src/heap/heap.cc @@ -36,7 +36,7 @@ #include "src/regexp/jsregexp.h" #include "src/runtime-profiler.h" #include "src/snapshot/natives.h" -#include "src/snapshot/serialize.h" +#include "src/snapshot/serializer-common.h" #include "src/snapshot/snapshot.h" #include "src/tracing/trace-event.h" #include "src/type-feedback-vector.h" @@ -1143,7 +1143,8 @@ bool Heap::ReserveSpace(Reservation* reservations) { static const int kThreshold = 20; while (gc_performed && counter++ < kThreshold) { gc_performed = false; - for (int space = NEW_SPACE; space < Serializer::kNumberOfSpaces; space++) { + for (int space = NEW_SPACE; space < SerializerDeserializer::kNumberOfSpaces; + space++) { Reservation* reservation = &reservations[space]; DCHECK_LE(1, reservation->length()); if (reservation->at(0).size == 0) continue; @@ -1169,7 +1170,7 @@ bool Heap::ReserveSpace(Reservation* reservations) { Address free_space_address = free_space->address(); CreateFillerObjectAt(free_space_address, size, ClearRecordedSlots::kNo); - DCHECK(space < Serializer::kNumberOfPreallocatedSpaces); + DCHECK(space < SerializerDeserializer::kNumberOfPreallocatedSpaces); chunk.start = free_space_address; chunk.end = free_space_address + size; } else { diff --git a/src/isolate.cc b/src/isolate.cc index 09113c366d..34ddf8d562 100644 --- a/src/isolate.cc +++ b/src/isolate.cc @@ -34,7 +34,8 @@ #include "src/regexp/regexp-stack.h" #include "src/runtime-profiler.h" #include "src/simulator.h" -#include "src/snapshot/serialize.h" +#include "src/snapshot/deserializer.h" +#include "src/snapshot/serializer-common.h" #include "src/v8.h" #include "src/version.h" #include "src/vm-state-inl.h" @@ -2299,9 +2300,8 @@ bool Isolate::Init(Deserializer* des) { // the snapshot. HandleScope scope(this); Deoptimizer::EnsureCodeForDeoptimizationEntry( - this, - Deoptimizer::LAZY, - kDeoptTableSerializeEntryCount - 1); + this, Deoptimizer::LAZY, + ExternalReferenceTable::kDeoptTableSerializeEntryCount - 1); } if (!serializer_enabled()) { diff --git a/src/parsing/preparse-data.h b/src/parsing/preparse-data.h index dbe1022d1e..1c99450810 100644 --- a/src/parsing/preparse-data.h +++ b/src/parsing/preparse-data.h @@ -6,6 +6,7 @@ #define V8_PARSING_PREPARSE_DATA_H_ #include "src/allocation.h" +#include "src/collector.h" #include "src/hashmap.h" #include "src/messages.h" #include "src/parsing/preparse-data-format.h" diff --git a/src/parsing/scanner.h b/src/parsing/scanner.h index 3f9bbb54a4..9f195e3657 100644 --- a/src/parsing/scanner.h +++ b/src/parsing/scanner.h @@ -10,13 +10,13 @@ #include "src/allocation.h" #include "src/base/logging.h" #include "src/char-predicates.h" +#include "src/collector.h" #include "src/globals.h" #include "src/hashmap.h" #include "src/list.h" #include "src/parsing/token.h" #include "src/unicode.h" #include "src/unicode-decoder.h" -#include "src/utils.h" namespace v8 { namespace internal { diff --git a/src/snapshot/code-serializer.cc b/src/snapshot/code-serializer.cc new file mode 100644 index 0000000000..aa12069457 --- /dev/null +++ b/src/snapshot/code-serializer.cc @@ -0,0 +1,418 @@ +// Copyright 2016 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "src/snapshot/code-serializer.h" + +#include "src/code-stubs.h" +#include "src/log.h" +#include "src/macro-assembler.h" +#include "src/profiler/cpu-profiler.h" +#include "src/snapshot/deserializer.h" +#include "src/version.h" + +namespace v8 { +namespace internal { + +ScriptData* CodeSerializer::Serialize(Isolate* isolate, + Handle info, + Handle source) { + base::ElapsedTimer timer; + if (FLAG_profile_deserialization) timer.Start(); + if (FLAG_trace_serializer) { + PrintF("[Serializing from"); + Object* script = info->script(); + if (script->IsScript()) Script::cast(script)->name()->ShortPrint(); + PrintF("]\n"); + } + + // Serialize code object. + SnapshotByteSink sink(info->code()->CodeSize() * 2); + CodeSerializer cs(isolate, &sink, *source); + DisallowHeapAllocation no_gc; + Object** location = Handle::cast(info).location(); + cs.VisitPointer(location); + cs.SerializeDeferredObjects(); + cs.Pad(); + + SerializedCodeData data(sink.data(), cs); + ScriptData* script_data = data.GetScriptData(); + + if (FLAG_profile_deserialization) { + double ms = timer.Elapsed().InMillisecondsF(); + int length = script_data->length(); + PrintF("[Serializing to %d bytes took %0.3f ms]\n", length, ms); + } + + return script_data; +} + +void CodeSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code, + WhereToPoint where_to_point, int skip) { + int root_index = root_index_map_.Lookup(obj); + if (root_index != RootIndexMap::kInvalidRootIndex) { + PutRoot(root_index, obj, how_to_code, where_to_point, skip); + return; + } + + if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return; + + FlushSkip(skip); + + if (obj->IsCode()) { + Code* code_object = Code::cast(obj); + switch (code_object->kind()) { + case Code::OPTIMIZED_FUNCTION: // No optimized code compiled yet. + case Code::HANDLER: // No handlers patched in yet. + case Code::REGEXP: // No regexp literals initialized yet. + case Code::NUMBER_OF_KINDS: // Pseudo enum value. + CHECK(false); + case Code::BUILTIN: + SerializeBuiltin(code_object->builtin_index(), how_to_code, + where_to_point); + return; + case Code::STUB: + SerializeCodeStub(code_object->stub_key(), how_to_code, where_to_point); + return; +#define IC_KIND_CASE(KIND) case Code::KIND: + IC_KIND_LIST(IC_KIND_CASE) +#undef IC_KIND_CASE + SerializeIC(code_object, how_to_code, where_to_point); + return; + case Code::FUNCTION: + DCHECK(code_object->has_reloc_info_for_serialization()); + SerializeGeneric(code_object, how_to_code, where_to_point); + return; + case Code::WASM_FUNCTION: + UNREACHABLE(); + } + UNREACHABLE(); + } + + // Past this point we should not see any (context-specific) maps anymore. + CHECK(!obj->IsMap()); + // There should be no references to the global object embedded. + CHECK(!obj->IsJSGlobalProxy() && !obj->IsJSGlobalObject()); + // There should be no hash table embedded. They would require rehashing. + CHECK(!obj->IsHashTable()); + // We expect no instantiated function objects or contexts. + CHECK(!obj->IsJSFunction() && !obj->IsContext()); + + SerializeGeneric(obj, how_to_code, where_to_point); +} + +void CodeSerializer::SerializeGeneric(HeapObject* heap_object, + HowToCode how_to_code, + WhereToPoint where_to_point) { + // Object has not yet been serialized. Serialize it here. + ObjectSerializer serializer(this, heap_object, sink_, how_to_code, + where_to_point); + serializer.Serialize(); +} + +void CodeSerializer::SerializeBuiltin(int builtin_index, HowToCode how_to_code, + WhereToPoint where_to_point) { + DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) || + (how_to_code == kPlain && where_to_point == kInnerPointer) || + (how_to_code == kFromCode && where_to_point == kInnerPointer)); + DCHECK_LT(builtin_index, Builtins::builtin_count); + DCHECK_LE(0, builtin_index); + + if (FLAG_trace_serializer) { + PrintF(" Encoding builtin: %s\n", + isolate()->builtins()->name(builtin_index)); + } + + sink_->Put(kBuiltin + how_to_code + where_to_point, "Builtin"); + sink_->PutInt(builtin_index, "builtin_index"); +} + +void CodeSerializer::SerializeCodeStub(uint32_t stub_key, HowToCode how_to_code, + WhereToPoint where_to_point) { + DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) || + (how_to_code == kPlain && where_to_point == kInnerPointer) || + (how_to_code == kFromCode && where_to_point == kInnerPointer)); + DCHECK(CodeStub::MajorKeyFromKey(stub_key) != CodeStub::NoCache); + DCHECK(!CodeStub::GetCode(isolate(), stub_key).is_null()); + + int index = AddCodeStubKey(stub_key) + kCodeStubsBaseIndex; + + if (FLAG_trace_serializer) { + PrintF(" Encoding code stub %s as %d\n", + CodeStub::MajorName(CodeStub::MajorKeyFromKey(stub_key)), index); + } + + sink_->Put(kAttachedReference + how_to_code + where_to_point, "CodeStub"); + sink_->PutInt(index, "CodeStub key"); +} + +void CodeSerializer::SerializeIC(Code* ic, HowToCode how_to_code, + WhereToPoint where_to_point) { + // The IC may be implemented as a stub. + uint32_t stub_key = ic->stub_key(); + if (stub_key != CodeStub::NoCacheKey()) { + if (FLAG_trace_serializer) { + PrintF(" %s is a code stub\n", Code::Kind2String(ic->kind())); + } + SerializeCodeStub(stub_key, how_to_code, where_to_point); + return; + } + // The IC may be implemented as builtin. Only real builtins have an + // actual builtin_index value attached (otherwise it's just garbage). + // Compare to make sure we are really dealing with a builtin. + int builtin_index = ic->builtin_index(); + if (builtin_index < Builtins::builtin_count) { + Builtins::Name name = static_cast(builtin_index); + Code* builtin = isolate()->builtins()->builtin(name); + if (builtin == ic) { + if (FLAG_trace_serializer) { + PrintF(" %s is a builtin\n", Code::Kind2String(ic->kind())); + } + DCHECK(ic->kind() == Code::KEYED_LOAD_IC || + ic->kind() == Code::KEYED_STORE_IC); + SerializeBuiltin(builtin_index, how_to_code, where_to_point); + return; + } + } + // The IC may also just be a piece of code kept in the non_monomorphic_cache. + // In that case, just serialize as a normal code object. + if (FLAG_trace_serializer) { + PrintF(" %s has no special handling\n", Code::Kind2String(ic->kind())); + } + DCHECK(ic->kind() == Code::LOAD_IC || ic->kind() == Code::STORE_IC); + SerializeGeneric(ic, how_to_code, where_to_point); +} + +int CodeSerializer::AddCodeStubKey(uint32_t stub_key) { + // TODO(yangguo) Maybe we need a hash table for a faster lookup than O(n^2). + int index = 0; + while (index < stub_keys_.length()) { + if (stub_keys_[index] == stub_key) return index; + index++; + } + stub_keys_.Add(stub_key); + return index; +} + +MaybeHandle CodeSerializer::Deserialize( + Isolate* isolate, ScriptData* cached_data, Handle source) { + base::ElapsedTimer timer; + if (FLAG_profile_deserialization) timer.Start(); + + HandleScope scope(isolate); + + base::SmartPointer scd( + SerializedCodeData::FromCachedData(isolate, cached_data, *source)); + if (scd.is_empty()) { + if (FLAG_profile_deserialization) PrintF("[Cached code failed check]\n"); + DCHECK(cached_data->rejected()); + return MaybeHandle(); + } + + // Prepare and register list of attached objects. + Vector code_stub_keys = scd->CodeStubKeys(); + Vector > attached_objects = Vector >::New( + code_stub_keys.length() + kCodeStubsBaseIndex); + attached_objects[kSourceObjectIndex] = source; + for (int i = 0; i < code_stub_keys.length(); i++) { + attached_objects[i + kCodeStubsBaseIndex] = + CodeStub::GetCode(isolate, code_stub_keys[i]).ToHandleChecked(); + } + + Deserializer deserializer(scd.get()); + deserializer.SetAttachedObjects(attached_objects); + + // Deserialize. + Handle result; + if (!deserializer.DeserializeCode(isolate).ToHandle(&result)) { + // Deserializing may fail if the reservations cannot be fulfilled. + if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n"); + return MaybeHandle(); + } + + if (FLAG_profile_deserialization) { + double ms = timer.Elapsed().InMillisecondsF(); + int length = cached_data->length(); + PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms); + } + result->set_deserialized(true); + + if (isolate->logger()->is_logging_code_events() || + isolate->cpu_profiler()->is_profiling()) { + String* name = isolate->heap()->empty_string(); + if (result->script()->IsScript()) { + Script* script = Script::cast(result->script()); + if (script->name()->IsString()) name = String::cast(script->name()); + } + isolate->logger()->CodeCreateEvent( + Logger::SCRIPT_TAG, result->abstract_code(), *result, NULL, name); + } + return scope.CloseAndEscape(result); +} + +class Checksum { + public: + explicit Checksum(Vector payload) { +#ifdef MEMORY_SANITIZER + // Computing the checksum includes padding bytes for objects like strings. + // Mark every object as initialized in the code serializer. + MSAN_MEMORY_IS_INITIALIZED(payload.start(), payload.length()); +#endif // MEMORY_SANITIZER + // Fletcher's checksum. Modified to reduce 64-bit sums to 32-bit. + uintptr_t a = 1; + uintptr_t b = 0; + const uintptr_t* cur = reinterpret_cast(payload.start()); + DCHECK(IsAligned(payload.length(), kIntptrSize)); + const uintptr_t* end = cur + payload.length() / kIntptrSize; + while (cur < end) { + // Unsigned overflow expected and intended. + a += *cur++; + b += a; + } +#if V8_HOST_ARCH_64_BIT + a ^= a >> 32; + b ^= b >> 32; +#endif // V8_HOST_ARCH_64_BIT + a_ = static_cast(a); + b_ = static_cast(b); + } + + bool Check(uint32_t a, uint32_t b) const { return a == a_ && b == b_; } + + uint32_t a() const { return a_; } + uint32_t b() const { return b_; } + + private: + uint32_t a_; + uint32_t b_; + + DISALLOW_COPY_AND_ASSIGN(Checksum); +}; + +SerializedCodeData::SerializedCodeData(const List& payload, + const CodeSerializer& cs) { + DisallowHeapAllocation no_gc; + const List* stub_keys = cs.stub_keys(); + + List reservations; + cs.EncodeReservations(&reservations); + + // Calculate sizes. + int reservation_size = reservations.length() * kInt32Size; + int num_stub_keys = stub_keys->length(); + int stub_keys_size = stub_keys->length() * kInt32Size; + int payload_offset = kHeaderSize + reservation_size + stub_keys_size; + int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); + int size = padded_payload_offset + payload.length(); + + // Allocate backing store and create result data. + AllocateData(size); + + // Set header values. + SetMagicNumber(cs.isolate()); + SetHeaderValue(kVersionHashOffset, Version::Hash()); + SetHeaderValue(kSourceHashOffset, SourceHash(cs.source())); + SetHeaderValue(kCpuFeaturesOffset, + static_cast(CpuFeatures::SupportedFeatures())); + SetHeaderValue(kFlagHashOffset, FlagList::Hash()); + SetHeaderValue(kNumReservationsOffset, reservations.length()); + SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys); + SetHeaderValue(kPayloadLengthOffset, payload.length()); + + Checksum checksum(payload.ToConstVector()); + SetHeaderValue(kChecksum1Offset, checksum.a()); + SetHeaderValue(kChecksum2Offset, checksum.b()); + + // Copy reservation chunk sizes. + CopyBytes(data_ + kHeaderSize, reinterpret_cast(reservations.begin()), + reservation_size); + + // Copy code stub keys. + CopyBytes(data_ + kHeaderSize + reservation_size, + reinterpret_cast(stub_keys->begin()), stub_keys_size); + + memset(data_ + payload_offset, 0, padded_payload_offset - payload_offset); + + // Copy serialized data. + CopyBytes(data_ + padded_payload_offset, payload.begin(), + static_cast(payload.length())); +} + +SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck( + Isolate* isolate, String* source) const { + uint32_t magic_number = GetMagicNumber(); + if (magic_number != ComputeMagicNumber(isolate)) return MAGIC_NUMBER_MISMATCH; + uint32_t version_hash = GetHeaderValue(kVersionHashOffset); + uint32_t source_hash = GetHeaderValue(kSourceHashOffset); + uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset); + uint32_t flags_hash = GetHeaderValue(kFlagHashOffset); + uint32_t c1 = GetHeaderValue(kChecksum1Offset); + uint32_t c2 = GetHeaderValue(kChecksum2Offset); + if (version_hash != Version::Hash()) return VERSION_MISMATCH; + if (source_hash != SourceHash(source)) return SOURCE_MISMATCH; + if (cpu_features != static_cast(CpuFeatures::SupportedFeatures())) { + return CPU_FEATURES_MISMATCH; + } + if (flags_hash != FlagList::Hash()) return FLAGS_MISMATCH; + if (!Checksum(Payload()).Check(c1, c2)) return CHECKSUM_MISMATCH; + return CHECK_SUCCESS; +} + +uint32_t SerializedCodeData::SourceHash(String* source) const { + return source->length(); +} + +// Return ScriptData object and relinquish ownership over it to the caller. +ScriptData* SerializedCodeData::GetScriptData() { + DCHECK(owns_data_); + ScriptData* result = new ScriptData(data_, size_); + result->AcquireDataOwnership(); + owns_data_ = false; + data_ = NULL; + return result; +} + +Vector SerializedCodeData::Reservations() + const { + return Vector( + reinterpret_cast(data_ + kHeaderSize), + GetHeaderValue(kNumReservationsOffset)); +} + +Vector SerializedCodeData::Payload() const { + int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; + int code_stubs_size = GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size; + int payload_offset = kHeaderSize + reservations_size + code_stubs_size; + int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); + const byte* payload = data_ + padded_payload_offset; + DCHECK(IsAligned(reinterpret_cast(payload), kPointerAlignment)); + int length = GetHeaderValue(kPayloadLengthOffset); + DCHECK_EQ(data_ + size_, payload + length); + return Vector(payload, length); +} + +Vector SerializedCodeData::CodeStubKeys() const { + int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; + const byte* start = data_ + kHeaderSize + reservations_size; + return Vector(reinterpret_cast(start), + GetHeaderValue(kNumCodeStubKeysOffset)); +} + +SerializedCodeData::SerializedCodeData(ScriptData* data) + : SerializedData(const_cast(data->data()), data->length()) {} + +SerializedCodeData* SerializedCodeData::FromCachedData(Isolate* isolate, + ScriptData* cached_data, + String* source) { + DisallowHeapAllocation no_gc; + SerializedCodeData* scd = new SerializedCodeData(cached_data); + SanityCheckResult r = scd->SanityCheck(isolate, source); + if (r == CHECK_SUCCESS) return scd; + cached_data->Reject(); + source->GetIsolate()->counters()->code_cache_reject_reason()->AddSample(r); + delete scd; + return NULL; +} + +} // namespace internal +} // namespace v8 diff --git a/src/snapshot/code-serializer.h b/src/snapshot/code-serializer.h new file mode 100644 index 0000000000..b217fff52b --- /dev/null +++ b/src/snapshot/code-serializer.h @@ -0,0 +1,127 @@ +// Copyright 2016 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef V8_SNAPSHOT_CODE_SERIALIZER_H_ +#define V8_SNAPSHOT_CODE_SERIALIZER_H_ + +#include "src/parsing/preparse-data.h" +#include "src/snapshot/serializer.h" + +namespace v8 { +namespace internal { + +class CodeSerializer : public Serializer { + public: + static ScriptData* Serialize(Isolate* isolate, + Handle info, + Handle source); + + MUST_USE_RESULT static MaybeHandle Deserialize( + Isolate* isolate, ScriptData* cached_data, Handle source); + + static const int kSourceObjectIndex = 0; + STATIC_ASSERT(kSourceObjectReference == kSourceObjectIndex); + + static const int kCodeStubsBaseIndex = 1; + + String* source() const { + DCHECK(!AllowHeapAllocation::IsAllowed()); + return source_; + } + + const List* stub_keys() const { return &stub_keys_; } + + private: + CodeSerializer(Isolate* isolate, SnapshotByteSink* sink, String* source) + : Serializer(isolate, sink), source_(source) { + back_reference_map_.AddSourceString(source); + } + + ~CodeSerializer() override { OutputStatistics("CodeSerializer"); } + + void SerializeObject(HeapObject* o, HowToCode how_to_code, + WhereToPoint where_to_point, int skip) override; + + void SerializeBuiltin(int builtin_index, HowToCode how_to_code, + WhereToPoint where_to_point); + void SerializeIC(Code* ic, HowToCode how_to_code, + WhereToPoint where_to_point); + void SerializeCodeStub(uint32_t stub_key, HowToCode how_to_code, + WhereToPoint where_to_point); + void SerializeGeneric(HeapObject* heap_object, HowToCode how_to_code, + WhereToPoint where_to_point); + int AddCodeStubKey(uint32_t stub_key); + + DisallowHeapAllocation no_gc_; + String* source_; + List stub_keys_; + DISALLOW_COPY_AND_ASSIGN(CodeSerializer); +}; + +// Wrapper around ScriptData to provide code-serializer-specific functionality. +class SerializedCodeData : public SerializedData { + public: + // Used when consuming. + static SerializedCodeData* FromCachedData(Isolate* isolate, + ScriptData* cached_data, + String* source); + + // Used when producing. + SerializedCodeData(const List& payload, const CodeSerializer& cs); + + // Return ScriptData object and relinquish ownership over it to the caller. + ScriptData* GetScriptData(); + + Vector Reservations() const; + Vector Payload() const; + + Vector CodeStubKeys() const; + + private: + explicit SerializedCodeData(ScriptData* data); + + enum SanityCheckResult { + CHECK_SUCCESS = 0, + MAGIC_NUMBER_MISMATCH = 1, + VERSION_MISMATCH = 2, + SOURCE_MISMATCH = 3, + CPU_FEATURES_MISMATCH = 4, + FLAGS_MISMATCH = 5, + CHECKSUM_MISMATCH = 6 + }; + + SanityCheckResult SanityCheck(Isolate* isolate, String* source) const; + + uint32_t SourceHash(String* source) const; + + // The data header consists of uint32_t-sized entries: + // [0] magic number and external reference count + // [1] version hash + // [2] source hash + // [3] cpu features + // [4] flag hash + // [5] number of code stub keys + // [6] number of reservation size entries + // [7] payload length + // [8] payload checksum part 1 + // [9] payload checksum part 2 + // ... reservations + // ... code stub keys + // ... serialized payload + static const int kVersionHashOffset = kMagicNumberOffset + kInt32Size; + static const int kSourceHashOffset = kVersionHashOffset + kInt32Size; + static const int kCpuFeaturesOffset = kSourceHashOffset + kInt32Size; + static const int kFlagHashOffset = kCpuFeaturesOffset + kInt32Size; + static const int kNumReservationsOffset = kFlagHashOffset + kInt32Size; + static const int kNumCodeStubKeysOffset = kNumReservationsOffset + kInt32Size; + static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size; + static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size; + static const int kChecksum2Offset = kChecksum1Offset + kInt32Size; + static const int kHeaderSize = kChecksum2Offset + kInt32Size; +}; + +} // namespace internal +} // namespace v8 + +#endif // V8_SNAPSHOT_CODE_SERIALIZER_H_ diff --git a/src/snapshot/deserializer.cc b/src/snapshot/deserializer.cc new file mode 100644 index 0000000000..6a5b57dfc4 --- /dev/null +++ b/src/snapshot/deserializer.cc @@ -0,0 +1,810 @@ +// Copyright 2016 the V8 project authors. All rights reserved. + +#include "src/snapshot/deserializer.h" + +#include "src/bootstrapper.h" +#include "src/heap/heap.h" +#include "src/isolate.h" +#include "src/macro-assembler.h" +#include "src/snapshot/natives.h" +#include "src/v8.h" + +namespace v8 { +namespace internal { + +void Deserializer::DecodeReservation( + Vector res) { + DCHECK_EQ(0, reservations_[NEW_SPACE].length()); + STATIC_ASSERT(NEW_SPACE == 0); + int current_space = NEW_SPACE; + for (auto& r : res) { + reservations_[current_space].Add({r.chunk_size(), NULL, NULL}); + if (r.is_last()) current_space++; + } + DCHECK_EQ(kNumberOfSpaces, current_space); + for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) current_chunk_[i] = 0; +} + +void Deserializer::FlushICacheForNewIsolate() { + DCHECK(!deserializing_user_code_); + // The entire isolate is newly deserialized. Simply flush all code pages. + PageIterator it(isolate_->heap()->code_space()); + while (it.has_next()) { + Page* p = it.next(); + Assembler::FlushICache(isolate_, p->area_start(), + p->area_end() - p->area_start()); + } +} + +void Deserializer::FlushICacheForNewCodeObjects() { + DCHECK(deserializing_user_code_); + for (Code* code : new_code_objects_) { + Assembler::FlushICache(isolate_, code->instruction_start(), + code->instruction_size()); + } +} + +bool Deserializer::ReserveSpace() { +#ifdef DEBUG + for (int i = NEW_SPACE; i < kNumberOfSpaces; ++i) { + CHECK(reservations_[i].length() > 0); + } +#endif // DEBUG + if (!isolate_->heap()->ReserveSpace(reservations_)) return false; + for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) { + high_water_[i] = reservations_[i][0].start; + } + return true; +} + +void Deserializer::Initialize(Isolate* isolate) { + DCHECK_NULL(isolate_); + DCHECK_NOT_NULL(isolate); + isolate_ = isolate; + DCHECK_NULL(external_reference_table_); + external_reference_table_ = ExternalReferenceTable::instance(isolate); + CHECK_EQ(magic_number_, + SerializedData::ComputeMagicNumber(external_reference_table_)); +} + +void Deserializer::Deserialize(Isolate* isolate) { + Initialize(isolate); + if (!ReserveSpace()) V8::FatalProcessOutOfMemory("deserializing context"); + // No active threads. + DCHECK_NULL(isolate_->thread_manager()->FirstThreadStateInUse()); + // No active handles. + DCHECK(isolate_->handle_scope_implementer()->blocks()->is_empty()); + + { + DisallowHeapAllocation no_gc; + isolate_->heap()->IterateSmiRoots(this); + isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG); + isolate_->heap()->RepairFreeListsAfterDeserialization(); + isolate_->heap()->IterateWeakRoots(this, VISIT_ALL); + DeserializeDeferredObjects(); + FlushICacheForNewIsolate(); + } + + isolate_->heap()->set_native_contexts_list( + isolate_->heap()->undefined_value()); + // The allocation site list is build during root iteration, but if no sites + // were encountered then it needs to be initialized to undefined. + if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) { + isolate_->heap()->set_allocation_sites_list( + isolate_->heap()->undefined_value()); + } + + // Update data pointers to the external strings containing natives sources. + Natives::UpdateSourceCache(isolate_->heap()); + ExtraNatives::UpdateSourceCache(isolate_->heap()); + + // Issue code events for newly deserialized code objects. + LOG_CODE_EVENT(isolate_, LogCodeObjects()); + LOG_CODE_EVENT(isolate_, LogCompiledFunctions()); +} + +MaybeHandle Deserializer::DeserializePartial( + Isolate* isolate, Handle global_proxy) { + Initialize(isolate); + if (!ReserveSpace()) { + V8::FatalProcessOutOfMemory("deserialize context"); + return MaybeHandle(); + } + + Vector > attached_objects = Vector >::New(1); + attached_objects[kGlobalProxyReference] = global_proxy; + SetAttachedObjects(attached_objects); + + DisallowHeapAllocation no_gc; + // Keep track of the code space start and end pointers in case new + // code objects were unserialized + OldSpace* code_space = isolate_->heap()->code_space(); + Address start_address = code_space->top(); + Object* root; + VisitPointer(&root); + DeserializeDeferredObjects(); + + // There's no code deserialized here. If this assert fires then that's + // changed and logging should be added to notify the profiler et al of the + // new code, which also has to be flushed from instruction cache. + CHECK_EQ(start_address, code_space->top()); + return Handle(root, isolate); +} + +MaybeHandle Deserializer::DeserializeCode( + Isolate* isolate) { + Initialize(isolate); + if (!ReserveSpace()) { + return Handle(); + } else { + deserializing_user_code_ = true; + HandleScope scope(isolate); + Handle result; + { + DisallowHeapAllocation no_gc; + Object* root; + VisitPointer(&root); + DeserializeDeferredObjects(); + FlushICacheForNewCodeObjects(); + result = Handle(SharedFunctionInfo::cast(root)); + } + CommitPostProcessedObjects(isolate); + return scope.CloseAndEscape(result); + } +} + +Deserializer::~Deserializer() { + // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed. + // DCHECK(source_.AtEOF()); + attached_objects_.Dispose(); +} + +// This is called on the roots. It is the driver of the deserialization +// process. It is also called on the body of each function. +void Deserializer::VisitPointers(Object** start, Object** end) { + // The space must be new space. Any other space would cause ReadChunk to try + // to update the remembered using NULL as the address. + ReadData(start, end, NEW_SPACE, NULL); +} + +void Deserializer::Synchronize(VisitorSynchronization::SyncTag tag) { + static const byte expected = kSynchronize; + CHECK_EQ(expected, source_.Get()); +} + +void Deserializer::DeserializeDeferredObjects() { + for (int code = source_.Get(); code != kSynchronize; code = source_.Get()) { + switch (code) { + case kAlignmentPrefix: + case kAlignmentPrefix + 1: + case kAlignmentPrefix + 2: + SetAlignment(code); + break; + default: { + int space = code & kSpaceMask; + DCHECK(space <= kNumberOfSpaces); + DCHECK(code - space == kNewObject); + HeapObject* object = GetBackReferencedObject(space); + int size = source_.GetInt() << kPointerSizeLog2; + Address obj_address = object->address(); + Object** start = reinterpret_cast(obj_address + kPointerSize); + Object** end = reinterpret_cast(obj_address + size); + bool filled = ReadData(start, end, space, obj_address); + CHECK(filled); + DCHECK(CanBeDeferred(object)); + PostProcessNewObject(object, space); + } + } + } +} + +// Used to insert a deserialized internalized string into the string table. +class StringTableInsertionKey : public HashTableKey { + public: + explicit StringTableInsertionKey(String* string) + : string_(string), hash_(HashForObject(string)) { + DCHECK(string->IsInternalizedString()); + } + + bool IsMatch(Object* string) override { + // We know that all entries in a hash table had their hash keys created. + // Use that knowledge to have fast failure. + if (hash_ != HashForObject(string)) return false; + // We want to compare the content of two internalized strings here. + return string_->SlowEquals(String::cast(string)); + } + + uint32_t Hash() override { return hash_; } + + uint32_t HashForObject(Object* key) override { + return String::cast(key)->Hash(); + } + + MUST_USE_RESULT Handle AsHandle(Isolate* isolate) override { + return handle(string_, isolate); + } + + private: + String* string_; + uint32_t hash_; + DisallowHeapAllocation no_gc; +}; + +HeapObject* Deserializer::PostProcessNewObject(HeapObject* obj, int space) { + if (deserializing_user_code()) { + if (obj->IsString()) { + String* string = String::cast(obj); + // Uninitialize hash field as the hash seed may have changed. + string->set_hash_field(String::kEmptyHashField); + if (string->IsInternalizedString()) { + // Canonicalize the internalized string. If it already exists in the + // string table, set it to forward to the existing one. + StringTableInsertionKey key(string); + String* canonical = StringTable::LookupKeyIfExists(isolate_, &key); + if (canonical == NULL) { + new_internalized_strings_.Add(handle(string)); + return string; + } else { + string->SetForwardedInternalizedString(canonical); + return canonical; + } + } + } else if (obj->IsScript()) { + new_scripts_.Add(handle(Script::cast(obj))); + } else { + DCHECK(CanBeDeferred(obj)); + } + } + if (obj->IsAllocationSite()) { + DCHECK(obj->IsAllocationSite()); + // Allocation sites are present in the snapshot, and must be linked into + // a list at deserialization time. + AllocationSite* site = AllocationSite::cast(obj); + // TODO(mvstanton): consider treating the heap()->allocation_sites_list() + // as a (weak) root. If this root is relocated correctly, this becomes + // unnecessary. + if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) { + site->set_weak_next(isolate_->heap()->undefined_value()); + } else { + site->set_weak_next(isolate_->heap()->allocation_sites_list()); + } + isolate_->heap()->set_allocation_sites_list(site); + } else if (obj->IsCode()) { + // We flush all code pages after deserializing the startup snapshot. In that + // case, we only need to remember code objects in the large object space. + // When deserializing user code, remember each individual code object. + if (deserializing_user_code() || space == LO_SPACE) { + new_code_objects_.Add(Code::cast(obj)); + } + } + // Check alignment. + DCHECK_EQ(0, Heap::GetFillToAlign(obj->address(), obj->RequiredAlignment())); + return obj; +} + +void Deserializer::CommitPostProcessedObjects(Isolate* isolate) { + StringTable::EnsureCapacityForDeserialization( + isolate, new_internalized_strings_.length()); + for (Handle string : new_internalized_strings_) { + StringTableInsertionKey key(*string); + DCHECK_NULL(StringTable::LookupKeyIfExists(isolate, &key)); + StringTable::LookupKey(isolate, &key); + } + + Heap* heap = isolate->heap(); + Factory* factory = isolate->factory(); + for (Handle