sources = [
"$target_gen_dir/libraries.cc",
"$target_gen_dir/experimental-libraries.cc",
- "src/snapshot-empty.cc",
+ "src/snapshot/snapshot-empty.cc",
]
configs -= [ "//build/config/compiler:chromium_code" ]
]
sources = [
- "src/natives-external.cc",
- "src/snapshot-external.cc",
+ "src/snapshot/natives-external.cc",
+ "src/snapshot/snapshot-external.cc",
]
configs -= [ "//build/config/compiler:chromium_code" ]
"src/modules.cc",
"src/modules.h",
"src/msan.h",
- "src/natives.h",
"src/objects-debug.cc",
"src/objects-inl.h",
"src/objects-printer.cc",
"src/scopeinfo.h",
"src/scopes.cc",
"src/scopes.h",
- "src/serialize.cc",
- "src/serialize.h",
"src/small-pointer-list.h",
"src/smart-pointers.h",
- "src/snapshot-common.cc",
- "src/snapshot-source-sink.cc",
- "src/snapshot-source-sink.h",
- "src/snapshot.h",
+ "src/snapshot/natives.h",
+ "src/snapshot/serialize.cc",
+ "src/snapshot/serialize.h",
+ "src/snapshot/snapshot-common.cc",
+ "src/snapshot/snapshot-source-sink.cc",
+ "src/snapshot/snapshot-source-sink.h",
+ "src/snapshot/snapshot.h",
"src/string-builder.cc",
"src/string-builder.h",
"src/string-search.cc",
visibility = [ ":*" ] # Only targets in this file can depend on this.
sources = [
- "src/mksnapshot.cc",
+ "src/snapshot/mksnapshot.cc",
]
configs -= [ "//build/config/compiler:chromium_code" ]
]
specific_include_rules = {
- "(mksnapshot|d8)\.cc": [
+ "d8\.cc": [
"+include/libplatform/libplatform.h",
],
}
#include "src/icu_util.h"
#include "src/json-parser.h"
#include "src/messages.h"
-#include "src/natives.h"
#include "src/parser.h"
#include "src/pending-compilation-error-handler.h"
#include "src/profile-generator-inl.h"
#include "src/sampler.h"
#include "src/scanner-character-streams.h"
#include "src/simulator.h"
-#include "src/snapshot.h"
+#include "src/snapshot/natives.h"
+#include "src/snapshot/snapshot.h"
#include "src/unicode-inl.h"
#include "src/v8threads.h"
#include "src/version.h"
#include "src/base/bits.h"
#include "src/base/cpu.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/arm/constants-arm.h"
#include "src/assembler.h"
#include "src/compiler.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/assembler.h"
#include "src/compiler.h"
#include "src/globals.h"
-#include "src/serialize.h"
#include "src/utils.h"
#include "src/regexp-macro-assembler.h"
#include "src/regexp-stack.h"
#include "src/runtime/runtime.h"
-#include "src/serialize.h"
+#include "src/snapshot/serialize.h"
#include "src/token.h"
#if V8_TARGET_ARCH_IA32
#include "src/extensions/statistics-extension.h"
#include "src/extensions/trigger-failure-extension.h"
#include "src/isolate-inl.h"
-#include "src/natives.h"
-#include "src/snapshot.h"
+#include "src/snapshot/natives.h"
+#include "src/snapshot/snapshot.h"
#include "third_party/fdlibm/fdlibm.h"
namespace v8 {
#include "src/assembler.h"
#include "src/compilation-cache.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/scanner-character-streams.h"
#include "src/scopeinfo.h"
#include "src/scopes.h"
+#include "src/snapshot/serialize.h"
#include "src/typing.h"
#include "src/vm-state-inl.h"
#include "src/basic-block-profiler.h"
#include "src/d8-debug.h"
#include "src/debug.h"
-#include "src/natives.h"
+#include "src/snapshot/natives.h"
#include "src/v8.h"
#endif // !V8_SHARED
#include "src/list.h"
#include "src/log.h"
#include "src/messages.h"
-#include "src/natives.h"
+#include "src/snapshot/natives.h"
#include "include/v8-debug.h"
#include "src/disasm.h"
#include "src/disassembler.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
+#include "src/snapshot/serialize.h"
#include "src/string-stream.h"
namespace v8 {
#include "src/prettyprinter.h"
#include "src/scopeinfo.h"
#include "src/scopes.h"
-#include "src/snapshot.h"
+#include "src/snapshot/snapshot.h"
namespace v8 {
namespace internal {
#include "src/gdb-jit.h"
#include "src/global-handles.h"
#include "src/messages.h"
-#include "src/natives.h"
#include "src/objects.h"
#include "src/ostreams.h"
+#include "src/snapshot/natives.h"
namespace v8 {
namespace internal {
#include "src/heap/store-buffer.h"
#include "src/heap-profiler.h"
#include "src/isolate-inl.h"
-#include "src/natives.h"
#include "src/runtime-profiler.h"
#include "src/scopeinfo.h"
-#include "src/serialize.h"
-#include "src/snapshot.h"
+#include "src/snapshot/natives.h"
+#include "src/snapshot/serialize.h"
+#include "src/snapshot/snapshot.h"
#include "src/utils.h"
#include "src/v8threads.h"
#include "src/vm-state-inl.h"
#include "src/heap/mark-compact.h"
#include "src/macro-assembler.h"
#include "src/msan.h"
-#include "src/snapshot.h"
+#include "src/snapshot/snapshot.h"
namespace v8 {
namespace internal {
#include "src/assembler.h"
#include "src/compiler.h"
#include "src/isolate.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/debug.h"
#include "src/isolate-inl.h"
#include "src/runtime/runtime.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/runtime-profiler.h"
#include "src/sampler.h"
#include "src/scopeinfo.h"
-#include "src/serialize.h"
#include "src/simulator.h"
+#include "src/snapshot/serialize.h"
#include "src/version.h"
#include "src/vm-state-inl.h"
#include "src/v8.h"
#include "src/scopes.h"
-#include "src/serialize.h"
#if V8_TARGET_ARCH_IA32
#include "src/ia32/lithium-ia32.h" // NOLINT
#include "src/macro-assembler.h"
#include "src/perf-jit.h"
#include "src/runtime-profiler.h"
-#include "src/serialize.h"
#include "src/string-stream.h"
#include "src/vm-state-inl.h"
#include "src/base/bits.h"
#include "src/base/cpu.h"
#include "src/mips/assembler-mips-inl.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/assembler.h"
#include "src/compiler.h"
#include "src/mips/constants-mips.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/base/cpu.h"
#include "src/mips64/assembler-mips64-inl.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/assembler.h"
#include "src/compiler.h"
#include "src/mips64/constants-mips64.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
+++ /dev/null
-// Copyright 2006-2008 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 <errno.h>
-#include <signal.h>
-#include <stdio.h>
-
-#include "src/v8.h"
-
-#include "include/libplatform/libplatform.h"
-#include "src/assembler.h"
-#include "src/base/platform/platform.h"
-#include "src/bootstrapper.h"
-#include "src/flags.h"
-#include "src/list.h"
-#include "src/natives.h"
-#include "src/serialize.h"
-
-
-using namespace v8;
-
-class SnapshotWriter {
- public:
- explicit SnapshotWriter(const char* snapshot_file)
- : fp_(GetFileDescriptorOrDie(snapshot_file)),
- startup_blob_file_(NULL) {}
-
- ~SnapshotWriter() {
- fclose(fp_);
- if (startup_blob_file_) fclose(startup_blob_file_);
- }
-
- void SetStartupBlobFile(const char* startup_blob_file) {
- if (startup_blob_file != NULL)
- startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
- }
-
- void WriteSnapshot(v8::StartupData blob) const {
- i::Vector<const i::byte> blob_vector(
- reinterpret_cast<const i::byte*>(blob.data), blob.raw_size);
- WriteSnapshotFile(blob_vector);
- MaybeWriteStartupBlob(blob_vector);
- }
-
- private:
- void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const {
- if (!startup_blob_file_) return;
-
- size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_);
- if (written != static_cast<size_t>(blob.length())) {
- i::PrintF("Writing snapshot file failed.. Aborting.\n");
- exit(1);
- }
- }
-
- void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const {
- WriteFilePrefix();
- WriteData(blob);
- WriteFileSuffix();
- }
-
- void WriteFilePrefix() const {
- fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
- fprintf(fp_, "#include \"src/v8.h\"\n");
- fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n");
- fprintf(fp_, "#include \"src/snapshot.h\"\n\n");
- fprintf(fp_, "namespace v8 {\n");
- fprintf(fp_, "namespace internal {\n\n");
- }
-
- void WriteFileSuffix() const {
- fprintf(fp_, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n");
- fprintf(fp_, " return &blob;\n");
- fprintf(fp_, "}\n\n");
- fprintf(fp_, "} // namespace internal\n");
- fprintf(fp_, "} // namespace v8\n");
- }
-
- void WriteData(const i::Vector<const i::byte>& blob) const {
- fprintf(fp_, "static const byte blob_data[] = {\n");
- WriteSnapshotData(blob);
- fprintf(fp_, "};\n");
- fprintf(fp_, "static const int blob_size = %d;\n", blob.length());
- fprintf(fp_, "static const v8::StartupData blob =\n");
- fprintf(fp_, "{ (const char*) blob_data, blob_size };\n");
- }
-
- void WriteSnapshotData(const i::Vector<const i::byte>& blob) const {
- for (int i = 0; i < blob.length(); i++) {
- if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n");
- if (i > 0) fprintf(fp_, ",");
- fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i)));
- }
- fprintf(fp_, "\n");
- }
-
- FILE* GetFileDescriptorOrDie(const char* filename) {
- FILE* fp = base::OS::FOpen(filename, "wb");
- if (fp == NULL) {
- i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
- exit(1);
- }
- return fp;
- }
-
- FILE* fp_;
- FILE* startup_blob_file_;
-};
-
-
-char* GetExtraCode(char* filename) {
- if (filename == NULL || strlen(filename) == 0) return NULL;
- ::printf("Embedding extra script: %s\n", filename);
- FILE* file = base::OS::FOpen(filename, "rb");
- if (file == NULL) {
- fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno);
- exit(1);
- }
- fseek(file, 0, SEEK_END);
- int size = ftell(file);
- rewind(file);
- char* chars = new char[size + 1];
- chars[size] = '\0';
- for (int i = 0; i < size;) {
- int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
- if (read < 0) {
- fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno);
- exit(1);
- }
- i += read;
- }
- fclose(file);
- return chars;
-}
-
-
-int main(int argc, char** argv) {
- // By default, log code create information in the snapshot.
- i::FLAG_log_code = true;
- i::FLAG_logfile_per_isolate = false;
-
- // Print the usage if an error occurs when parsing the command line
- // flags or if the help flag is set.
- int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
- if (result > 0 || (argc != 2 && argc != 3) || i::FLAG_help) {
- ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
- i::FlagList::PrintHelp();
- return !i::FLAG_help;
- }
-
- i::CpuFeatures::Probe(true);
- V8::InitializeICU();
- v8::Platform* platform = v8::platform::CreateDefaultPlatform();
- v8::V8::InitializePlatform(platform);
- v8::V8::Initialize();
-
- {
- SnapshotWriter writer(argv[1]);
- if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob);
- char* extra_code = GetExtraCode(argc == 3 ? argv[2] : NULL);
- StartupData blob = v8::V8::CreateSnapshotDataBlob(extra_code);
- CHECK(blob.data);
- writer.WriteSnapshot(blob);
- delete[] extra_code;
- delete[] blob.data;
- }
-
- V8::Dispose();
- V8::ShutdownPlatform();
- delete platform;
- return 0;
-}
+++ /dev/null
-// Copyright 2014 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/natives.h"
-
-#include "src/base/logging.h"
-#include "src/list.h"
-#include "src/list-inl.h"
-#include "src/snapshot-source-sink.h"
-#include "src/vector.h"
-
-#ifndef V8_USE_EXTERNAL_STARTUP_DATA
-#error natives-external.cc is used only for the external snapshot build.
-#endif // V8_USE_EXTERNAL_STARTUP_DATA
-
-
-namespace v8 {
-namespace internal {
-
-
-/**
- * NativesStore stores the 'native' (builtin) JS libraries.
- *
- * NativesStore needs to be initialized before using V8, usually by the
- * embedder calling v8::SetNativesDataBlob, which calls SetNativesFromFile
- * below.
- */
-class NativesStore {
- public:
- ~NativesStore() {
- for (int i = 0; i < native_names_.length(); i++) {
- native_names_[i].Dispose();
- }
- }
-
- int GetBuiltinsCount() { return native_ids_.length(); }
- int GetDebuggerCount() { return debugger_count_; }
-
- Vector<const char> GetScriptSource(int index) {
- return native_source_[index];
- }
-
- Vector<const char> GetScriptName(int index) { return native_names_[index]; }
-
- int GetIndex(const char* id) {
- for (int i = 0; i < native_ids_.length(); ++i) {
- int native_id_length = native_ids_[i].length();
- if ((static_cast<int>(strlen(id)) == native_id_length) &&
- (strncmp(id, native_ids_[i].start(), native_id_length) == 0)) {
- return i;
- }
- }
- DCHECK(false);
- return -1;
- }
-
- Vector<const char> GetScriptsSource() {
- DCHECK(false); // Not implemented.
- return Vector<const char>();
- }
-
- static NativesStore* MakeFromScriptsSource(SnapshotByteSource* source) {
- NativesStore* store = new NativesStore;
-
- // We expect the libraries in the following format:
- // int: # of debugger sources.
- // 2N blobs: N pairs of source name + actual source.
- // then, repeat for non-debugger sources.
- int debugger_count = source->GetInt();
- for (int i = 0; i < debugger_count; ++i)
- store->ReadNameAndContentPair(source);
- int library_count = source->GetInt();
- for (int i = 0; i < library_count; ++i)
- store->ReadNameAndContentPair(source);
-
- store->debugger_count_ = debugger_count;
- return store;
- }
-
- private:
- NativesStore() : debugger_count_(0) {}
-
- Vector<const char> NameFromId(const byte* id, int id_length) {
- const char native[] = "native ";
- const char extension[] = ".js";
- Vector<char> name(Vector<char>::New(id_length + sizeof(native) - 1 +
- sizeof(extension) - 1));
- memcpy(name.start(), native, sizeof(native) - 1);
- memcpy(name.start() + sizeof(native) - 1, id, id_length);
- memcpy(name.start() + sizeof(native) - 1 + id_length, extension,
- sizeof(extension) - 1);
- return Vector<const char>::cast(name);
- }
-
- bool ReadNameAndContentPair(SnapshotByteSource* bytes) {
- const byte* id;
- int id_length;
- const byte* source;
- int source_length;
- bool success = bytes->GetBlob(&id, &id_length) &&
- bytes->GetBlob(&source, &source_length);
- if (success) {
- Vector<const char> id_vector(reinterpret_cast<const char*>(id),
- id_length);
- Vector<const char> source_vector(
- reinterpret_cast<const char*>(source), source_length);
- native_ids_.Add(id_vector);
- native_source_.Add(source_vector);
- native_names_.Add(NameFromId(id, id_length));
- }
- return success;
- }
-
- List<Vector<const char> > native_ids_;
- List<Vector<const char> > native_names_;
- List<Vector<const char> > native_source_;
- int debugger_count_;
-
- DISALLOW_COPY_AND_ASSIGN(NativesStore);
-};
-
-
-template<NativeType type>
-class NativesHolder {
- public:
- static NativesStore* get() {
- DCHECK(holder_);
- return holder_;
- }
- static void set(NativesStore* store) {
- DCHECK(store);
- holder_ = store;
- }
- static bool empty() { return holder_ == NULL; }
- static void Dispose() {
- delete holder_;
- holder_ = NULL;
- }
-
- private:
- static NativesStore* holder_;
-};
-
-template<NativeType type>
-NativesStore* NativesHolder<type>::holder_ = NULL;
-
-
-// The natives blob. Memory is owned by caller.
-static StartupData* natives_blob_ = NULL;
-
-
-/**
- * Read the Natives blob, as previously set by SetNativesFromFile.
- */
-void ReadNatives() {
- if (natives_blob_ && NativesHolder<CORE>::empty()) {
- SnapshotByteSource bytes(natives_blob_->data, natives_blob_->raw_size);
- NativesHolder<CORE>::set(NativesStore::MakeFromScriptsSource(&bytes));
- NativesHolder<EXPERIMENTAL>::set(
- NativesStore::MakeFromScriptsSource(&bytes));
- DCHECK(!bytes.HasMore());
- }
-}
-
-
-/**
- * Set the Natives (library sources) blob, as generated by js2c + the build
- * system.
- */
-void SetNativesFromFile(StartupData* natives_blob) {
- DCHECK(!natives_blob_);
- DCHECK(natives_blob);
- DCHECK(natives_blob->data);
- DCHECK(natives_blob->raw_size > 0);
-
- natives_blob_ = natives_blob;
- ReadNatives();
-}
-
-
-/**
- * Release memory allocated by SetNativesFromFile.
- */
-void DisposeNatives() {
- NativesHolder<CORE>::Dispose();
- NativesHolder<EXPERIMENTAL>::Dispose();
-}
-
-
-// Implement NativesCollection<T> bsaed on NativesHolder + NativesStore.
-//
-// (The callers expect a purely static interface, since this is how the
-// natives are usually compiled in. Since we implement them based on
-// runtime content, we have to implement this indirection to offer
-// a static interface.)
-template<NativeType type>
-int NativesCollection<type>::GetBuiltinsCount() {
- return NativesHolder<type>::get()->GetBuiltinsCount();
-}
-
-template<NativeType type>
-int NativesCollection<type>::GetDebuggerCount() {
- return NativesHolder<type>::get()->GetDebuggerCount();
-}
-
-template<NativeType type>
-int NativesCollection<type>::GetIndex(const char* name) {
- return NativesHolder<type>::get()->GetIndex(name);
-}
-
-template <NativeType type>
-Vector<const char> NativesCollection<type>::GetScriptSource(int index) {
- return NativesHolder<type>::get()->GetScriptSource(index);
-}
-
-template<NativeType type>
-Vector<const char> NativesCollection<type>::GetScriptName(int index) {
- return NativesHolder<type>::get()->GetScriptName(index);
-}
-
-template <NativeType type>
-Vector<const char> NativesCollection<type>::GetScriptsSource() {
- return NativesHolder<type>::get()->GetScriptsSource();
-}
-
-
-// The compiler can't 'see' all uses of the static methods and hence
-// my choice to elide them. This we'll explicitly instantiate these.
-template class NativesCollection<CORE>;
-template class NativesCollection<EXPERIMENTAL>;
-
-} // namespace v8::internal
-} // namespace v8
+++ /dev/null
-// Copyright 2011 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_NATIVES_H_
-#define V8_NATIVES_H_
-
-#include "src/vector.h"
-
-namespace v8 { class StartupData; } // Forward declaration.
-
-namespace v8 {
-namespace internal {
-
-enum NativeType {
- CORE, EXPERIMENTAL, D8, TEST
-};
-
-template <NativeType type>
-class NativesCollection {
- public:
- // Number of built-in scripts.
- static int GetBuiltinsCount();
- // Number of debugger implementation scripts.
- static int GetDebuggerCount();
-
- // These are used to access built-in scripts. The debugger implementation
- // scripts have an index in the interval [0, GetDebuggerCount()). The
- // non-debugger scripts have an index in the interval [GetDebuggerCount(),
- // GetNativesCount()).
- static int GetIndex(const char* name);
- static Vector<const char> GetScriptSource(int index);
- static Vector<const char> GetScriptName(int index);
- static Vector<const char> GetScriptsSource();
-};
-
-typedef NativesCollection<CORE> Natives;
-typedef NativesCollection<EXPERIMENTAL> ExperimentalNatives;
-
-#ifdef V8_USE_EXTERNAL_STARTUP_DATA
-// Used for reading the natives at runtime. Implementation in natives-empty.cc
-void SetNativesFromFile(StartupData* natives_blob);
-void ReadNatives();
-void DisposeNatives();
-#endif
-
-} } // namespace v8::internal
-
-#endif // V8_NATIVES_H_
#include "src/arguments.h"
#include "src/deoptimizer.h"
#include "src/full-codegen.h"
-#include "src/natives.h"
#include "src/runtime/runtime-utils.h"
+#include "src/snapshot/natives.h"
namespace v8 {
namespace internal {
+++ /dev/null
-// Copyright 2012 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/v8.h"
-
-#include "src/accessors.h"
-#include "src/api.h"
-#include "src/base/platform/platform.h"
-#include "src/bootstrapper.h"
-#include "src/code-stubs.h"
-#include "src/cpu-profiler.h"
-#include "src/deoptimizer.h"
-#include "src/execution.h"
-#include "src/global-handles.h"
-#include "src/ic/ic.h"
-#include "src/ic/stub-cache.h"
-#include "src/natives.h"
-#include "src/objects.h"
-#include "src/parser.h"
-#include "src/runtime/runtime.h"
-#include "src/serialize.h"
-#include "src/snapshot.h"
-#include "src/snapshot-source-sink.h"
-#include "src/v8threads.h"
-#include "src/version.h"
-
-namespace v8 {
-namespace internal {
-
-
-// -----------------------------------------------------------------------------
-// Coding of external references.
-
-
-ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
- ExternalReferenceTable* external_reference_table =
- isolate->external_reference_table();
- if (external_reference_table == NULL) {
- external_reference_table = new ExternalReferenceTable(isolate);
- isolate->set_external_reference_table(external_reference_table);
- }
- return external_reference_table;
-}
-
-
-ExternalReferenceTable::ExternalReferenceTable(Isolate* isolate) {
- // Miscellaneous
- Add(ExternalReference::roots_array_start(isolate).address(),
- "Heap::roots_array_start()");
- Add(ExternalReference::address_of_stack_limit(isolate).address(),
- "StackGuard::address_of_jslimit()");
- Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
- "StackGuard::address_of_real_jslimit()");
- Add(ExternalReference::new_space_start(isolate).address(),
- "Heap::NewSpaceStart()");
- Add(ExternalReference::new_space_mask(isolate).address(),
- "Heap::NewSpaceMask()");
- Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
- "Heap::NewSpaceAllocationLimitAddress()");
- Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
- "Heap::NewSpaceAllocationTopAddress()");
- Add(ExternalReference::debug_break(isolate).address(), "Debug::Break()");
- Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
- "Debug::step_in_fp_addr()");
- Add(ExternalReference::mod_two_doubles_operation(isolate).address(),
- "mod_two_doubles");
- // Keyed lookup cache.
- Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
- "KeyedLookupCache::keys()");
- Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
- "KeyedLookupCache::field_offsets()");
- Add(ExternalReference::handle_scope_next_address(isolate).address(),
- "HandleScope::next");
- Add(ExternalReference::handle_scope_limit_address(isolate).address(),
- "HandleScope::limit");
- Add(ExternalReference::handle_scope_level_address(isolate).address(),
- "HandleScope::level");
- Add(ExternalReference::new_deoptimizer_function(isolate).address(),
- "Deoptimizer::New()");
- Add(ExternalReference::compute_output_frames_function(isolate).address(),
- "Deoptimizer::ComputeOutputFrames()");
- Add(ExternalReference::address_of_min_int().address(),
- "LDoubleConstant::min_int");
- Add(ExternalReference::address_of_one_half().address(),
- "LDoubleConstant::one_half");
- Add(ExternalReference::isolate_address(isolate).address(), "isolate");
- Add(ExternalReference::address_of_negative_infinity().address(),
- "LDoubleConstant::negative_infinity");
- Add(ExternalReference::power_double_double_function(isolate).address(),
- "power_double_double_function");
- Add(ExternalReference::power_double_int_function(isolate).address(),
- "power_double_int_function");
- Add(ExternalReference::math_log_double_function(isolate).address(),
- "std::log");
- Add(ExternalReference::store_buffer_top(isolate).address(),
- "store_buffer_top");
- Add(ExternalReference::address_of_the_hole_nan().address(), "the_hole_nan");
- Add(ExternalReference::get_date_field_function(isolate).address(),
- "JSDate::GetField");
- Add(ExternalReference::date_cache_stamp(isolate).address(),
- "date_cache_stamp");
- Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
- "address_of_pending_message_obj");
- Add(ExternalReference::get_make_code_young_function(isolate).address(),
- "Code::MakeCodeYoung");
- Add(ExternalReference::cpu_features().address(), "cpu_features");
- Add(ExternalReference::old_pointer_space_allocation_top_address(isolate)
- .address(),
- "Heap::OldPointerSpaceAllocationTopAddress");
- Add(ExternalReference::old_pointer_space_allocation_limit_address(isolate)
- .address(),
- "Heap::OldPointerSpaceAllocationLimitAddress");
- Add(ExternalReference::old_data_space_allocation_top_address(isolate)
- .address(),
- "Heap::OldDataSpaceAllocationTopAddress");
- Add(ExternalReference::old_data_space_allocation_limit_address(isolate)
- .address(),
- "Heap::OldDataSpaceAllocationLimitAddress");
- Add(ExternalReference::allocation_sites_list_address(isolate).address(),
- "Heap::allocation_sites_list_address()");
- Add(ExternalReference::address_of_uint32_bias().address(), "uint32_bias");
- Add(ExternalReference::get_mark_code_as_executed_function(isolate).address(),
- "Code::MarkCodeAsExecuted");
- Add(ExternalReference::is_profiling_address(isolate).address(),
- "CpuProfiler::is_profiling");
- Add(ExternalReference::scheduled_exception_address(isolate).address(),
- "Isolate::scheduled_exception");
- Add(ExternalReference::invoke_function_callback(isolate).address(),
- "InvokeFunctionCallback");
- Add(ExternalReference::invoke_accessor_getter_callback(isolate).address(),
- "InvokeAccessorGetterCallback");
- Add(ExternalReference::flush_icache_function(isolate).address(),
- "CpuFeatures::FlushICache");
- Add(ExternalReference::log_enter_external_function(isolate).address(),
- "Logger::EnterExternal");
- Add(ExternalReference::log_leave_external_function(isolate).address(),
- "Logger::LeaveExternal");
- Add(ExternalReference::address_of_minus_one_half().address(),
- "double_constants.minus_one_half");
- Add(ExternalReference::stress_deopt_count(isolate).address(),
- "Isolate::stress_deopt_count_address()");
-
- // Debug addresses
- Add(ExternalReference::debug_after_break_target_address(isolate).address(),
- "Debug::after_break_target_address()");
- Add(ExternalReference::debug_restarter_frame_function_pointer_address(isolate)
- .address(),
- "Debug::restarter_frame_function_pointer_address()");
- Add(ExternalReference::debug_is_active_address(isolate).address(),
- "Debug::is_active_address()");
-
-#ifndef V8_INTERPRETED_REGEXP
- Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
- "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
- Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
- "RegExpMacroAssembler*::CheckStackGuardState()");
- Add(ExternalReference::re_grow_stack(isolate).address(),
- "NativeRegExpMacroAssembler::GrowStack()");
- Add(ExternalReference::re_word_character_map().address(),
- "NativeRegExpMacroAssembler::word_character_map");
- Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
- "RegExpStack::limit_address()");
- Add(ExternalReference::address_of_regexp_stack_memory_address(isolate)
- .address(),
- "RegExpStack::memory_address()");
- Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
- "RegExpStack::memory_size()");
- Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
- "OffsetsVector::static_offsets_vector");
-#endif // V8_INTERPRETED_REGEXP
-
- // The following populates all of the different type of external references
- // into the ExternalReferenceTable.
- //
- // NOTE: This function was originally 100k of code. It has since been
- // rewritten to be mostly table driven, as the callback macro style tends to
- // very easily cause code bloat. Please be careful in the future when adding
- // new references.
-
- struct RefTableEntry {
- uint16_t id;
- const char* name;
- };
-
- static const RefTableEntry c_builtins[] = {
-#define DEF_ENTRY_C(name, ignored) \
- { Builtins::c_##name, "Builtins::" #name } \
- ,
- BUILTIN_LIST_C(DEF_ENTRY_C)
-#undef DEF_ENTRY_C
- };
-
- for (unsigned i = 0; i < arraysize(c_builtins); ++i) {
- ExternalReference ref(static_cast<Builtins::CFunctionId>(c_builtins[i].id),
- isolate);
- Add(ref.address(), c_builtins[i].name);
- }
-
- static const RefTableEntry builtins[] = {
-#define DEF_ENTRY_C(name, ignored) \
- { Builtins::k##name, "Builtins::" #name } \
- ,
-#define DEF_ENTRY_A(name, i1, i2, i3) \
- { Builtins::k##name, "Builtins::" #name } \
- ,
- BUILTIN_LIST_C(DEF_ENTRY_C) BUILTIN_LIST_A(DEF_ENTRY_A)
- BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
-#undef DEF_ENTRY_C
-#undef DEF_ENTRY_A
- };
-
- for (unsigned i = 0; i < arraysize(builtins); ++i) {
- ExternalReference ref(static_cast<Builtins::Name>(builtins[i].id), isolate);
- Add(ref.address(), builtins[i].name);
- }
-
- static const RefTableEntry runtime_functions[] = {
-#define RUNTIME_ENTRY(name, i1, i2) \
- { Runtime::k##name, "Runtime::" #name } \
- ,
- FOR_EACH_INTRINSIC(RUNTIME_ENTRY)
-#undef RUNTIME_ENTRY
- };
-
- for (unsigned i = 0; i < arraysize(runtime_functions); ++i) {
- ExternalReference ref(
- static_cast<Runtime::FunctionId>(runtime_functions[i].id), isolate);
- Add(ref.address(), runtime_functions[i].name);
- }
-
- static const RefTableEntry inline_caches[] = {
-#define IC_ENTRY(name) \
- { IC::k##name, "IC::" #name } \
- ,
- IC_UTIL_LIST(IC_ENTRY)
-#undef IC_ENTRY
- };
-
- for (unsigned i = 0; i < arraysize(inline_caches); ++i) {
- ExternalReference ref(
- IC_Utility(static_cast<IC::UtilityId>(inline_caches[i].id)), isolate);
- Add(ref.address(), runtime_functions[i].name);
- }
-
- // Stat counters
- struct StatsRefTableEntry {
- StatsCounter* (Counters::*counter)();
- const char* name;
- };
-
- static const StatsRefTableEntry stats_ref_table[] = {
-#define COUNTER_ENTRY(name, caption) \
- { &Counters::name, "Counters::" #name } \
- ,
- STATS_COUNTER_LIST_1(COUNTER_ENTRY) STATS_COUNTER_LIST_2(COUNTER_ENTRY)
-#undef COUNTER_ENTRY
- };
-
- Counters* counters = isolate->counters();
- for (unsigned i = 0; i < arraysize(stats_ref_table); ++i) {
- // To make sure the indices are not dependent on whether counters are
- // enabled, use a dummy address as filler.
- Address address = NotAvailable();
- StatsCounter* counter = (counters->*(stats_ref_table[i].counter))();
- if (counter->Enabled()) {
- address = reinterpret_cast<Address>(counter->GetInternalPointer());
- }
- Add(address, stats_ref_table[i].name);
- }
-
- // Top addresses
- static const char* address_names[] = {
-#define BUILD_NAME_LITERAL(Name, name) "Isolate::" #name "_address",
- FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL) NULL
-#undef BUILD_NAME_LITERAL
- };
-
- for (int i = 0; i < Isolate::kIsolateAddressCount; ++i) {
- Add(isolate->get_address_from_id(static_cast<Isolate::AddressId>(i)),
- address_names[i]);
- }
-
- // Accessors
- struct AccessorRefTable {
- Address address;
- const char* name;
- };
-
- static const AccessorRefTable accessors[] = {
-#define ACCESSOR_INFO_DECLARATION(name) \
- { FUNCTION_ADDR(&Accessors::name##Getter), "Accessors::" #name "Getter" } \
- , {FUNCTION_ADDR(&Accessors::name##Setter), "Accessors::" #name "Setter"},
- ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
-#undef ACCESSOR_INFO_DECLARATION
- };
-
- for (unsigned i = 0; i < arraysize(accessors); ++i) {
- Add(accessors[i].address, accessors[i].name);
- }
-
- StubCache* stub_cache = isolate->stub_cache();
-
- // Stub cache tables
- Add(stub_cache->key_reference(StubCache::kPrimary).address(),
- "StubCache::primary_->key");
- Add(stub_cache->value_reference(StubCache::kPrimary).address(),
- "StubCache::primary_->value");
- Add(stub_cache->map_reference(StubCache::kPrimary).address(),
- "StubCache::primary_->map");
- Add(stub_cache->key_reference(StubCache::kSecondary).address(),
- "StubCache::secondary_->key");
- Add(stub_cache->value_reference(StubCache::kSecondary).address(),
- "StubCache::secondary_->value");
- Add(stub_cache->map_reference(StubCache::kSecondary).address(),
- "StubCache::secondary_->map");
-
- // Runtime entries
- Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
- "HandleScope::DeleteExtensions");
- Add(ExternalReference::incremental_marking_record_write_function(isolate)
- .address(),
- "IncrementalMarking::RecordWrite");
- Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
- "StoreBuffer::StoreBufferOverflow");
-
- // Add a small set of deopt entry addresses to encoder without generating the
- // deopt table code, which isn't possible at deserialization time.
- HandleScope scope(isolate);
- for (int entry = 0; entry < kDeoptTableSerializeEntryCount; ++entry) {
- Address address = Deoptimizer::GetDeoptimizationEntry(
- isolate,
- entry,
- Deoptimizer::LAZY,
- Deoptimizer::CALCULATE_ENTRY_ADDRESS);
- Add(address, "lazy_deopt");
- }
-}
-
-
-ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) {
- map_ = isolate->external_reference_map();
- if (map_ != NULL) return;
- map_ = new HashMap(HashMap::PointersMatch);
- ExternalReferenceTable* table = ExternalReferenceTable::instance(isolate);
- for (int i = 0; i < table->size(); ++i) {
- Address addr = table->address(i);
- if (addr == ExternalReferenceTable::NotAvailable()) continue;
- // We expect no duplicate external references entries in the table.
- DCHECK_NULL(map_->Lookup(addr, Hash(addr), false));
- map_->Lookup(addr, Hash(addr), true)->value = reinterpret_cast<void*>(i);
- }
- isolate->set_external_reference_map(map_);
-}
-
-
-uint32_t ExternalReferenceEncoder::Encode(Address address) const {
- DCHECK_NOT_NULL(address);
- HashMap::Entry* entry =
- const_cast<HashMap*>(map_)->Lookup(address, Hash(address), false);
- DCHECK_NOT_NULL(entry);
- return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value));
-}
-
-
-const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
- Address address) const {
- HashMap::Entry* entry =
- const_cast<HashMap*>(map_)->Lookup(address, Hash(address), false);
- if (entry == NULL) return "<unknown>";
- uint32_t i = static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value));
- return ExternalReferenceTable::instance(isolate)->name(i);
-}
-
-
-RootIndexMap::RootIndexMap(Isolate* isolate) {
- map_ = isolate->root_index_map();
- if (map_ != NULL) return;
- map_ = new HashMap(HashMap::PointersMatch);
- Object** root_array = isolate->heap()->roots_array_start();
- for (uint32_t i = 0; i < Heap::kStrongRootListLength; i++) {
- Heap::RootListIndex root_index = static_cast<Heap::RootListIndex>(i);
- Object* root = root_array[root_index];
- // Omit root entries that can be written after initialization. They must
- // not be referenced through the root list in the snapshot.
- if (root->IsHeapObject() &&
- isolate->heap()->RootCanBeTreatedAsConstant(root_index)) {
- HeapObject* heap_object = HeapObject::cast(root);
- HashMap::Entry* entry = LookupEntry(map_, heap_object, false);
- if (entry != NULL) {
- // Some are initialized to a previous value in the root list.
- DCHECK_LT(GetValue(entry), i);
- } else {
- SetValue(LookupEntry(map_, heap_object, true), i);
- }
- }
- }
- isolate->set_root_index_map(map_);
-}
-
-
-class CodeAddressMap: public CodeEventLogger {
- public:
- explicit CodeAddressMap(Isolate* isolate)
- : isolate_(isolate) {
- isolate->logger()->addCodeEventListener(this);
- }
-
- virtual ~CodeAddressMap() {
- isolate_->logger()->removeCodeEventListener(this);
- }
-
- virtual void CodeMoveEvent(Address from, Address to) {
- address_to_name_map_.Move(from, to);
- }
-
- virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) {
- }
-
- virtual void CodeDeleteEvent(Address from) {
- address_to_name_map_.Remove(from);
- }
-
- const char* Lookup(Address address) {
- return address_to_name_map_.Lookup(address);
- }
-
- private:
- class NameMap {
- public:
- NameMap() : impl_(HashMap::PointersMatch) {}
-
- ~NameMap() {
- for (HashMap::Entry* p = impl_.Start(); p != NULL; p = impl_.Next(p)) {
- DeleteArray(static_cast<const char*>(p->value));
- }
- }
-
- void Insert(Address code_address, const char* name, int name_size) {
- HashMap::Entry* entry = FindOrCreateEntry(code_address);
- if (entry->value == NULL) {
- entry->value = CopyName(name, name_size);
- }
- }
-
- const char* Lookup(Address code_address) {
- HashMap::Entry* entry = FindEntry(code_address);
- return (entry != NULL) ? static_cast<const char*>(entry->value) : NULL;
- }
-
- void Remove(Address code_address) {
- HashMap::Entry* entry = FindEntry(code_address);
- if (entry != NULL) {
- DeleteArray(static_cast<char*>(entry->value));
- RemoveEntry(entry);
- }
- }
-
- void Move(Address from, Address to) {
- if (from == to) return;
- HashMap::Entry* from_entry = FindEntry(from);
- DCHECK(from_entry != NULL);
- void* value = from_entry->value;
- RemoveEntry(from_entry);
- HashMap::Entry* to_entry = FindOrCreateEntry(to);
- DCHECK(to_entry->value == NULL);
- to_entry->value = value;
- }
-
- private:
- static char* CopyName(const char* name, int name_size) {
- char* result = NewArray<char>(name_size + 1);
- for (int i = 0; i < name_size; ++i) {
- char c = name[i];
- if (c == '\0') c = ' ';
- result[i] = c;
- }
- result[name_size] = '\0';
- return result;
- }
-
- HashMap::Entry* FindOrCreateEntry(Address code_address) {
- return impl_.Lookup(code_address, ComputePointerHash(code_address), true);
- }
-
- HashMap::Entry* FindEntry(Address code_address) {
- return impl_.Lookup(code_address,
- ComputePointerHash(code_address),
- false);
- }
-
- void RemoveEntry(HashMap::Entry* entry) {
- impl_.Remove(entry->key, entry->hash);
- }
-
- HashMap impl_;
-
- DISALLOW_COPY_AND_ASSIGN(NameMap);
- };
-
- virtual void LogRecordedBuffer(Code* code,
- SharedFunctionInfo*,
- const char* name,
- int length) {
- address_to_name_map_.Insert(code->address(), name, length);
- }
-
- NameMap address_to_name_map_;
- Isolate* isolate_;
-};
-
-
-void Deserializer::DecodeReservation(
- Vector<const SerializedData::Reservation> 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::FlushICacheForNewCodeObjects() {
- PageIterator it(isolate_->heap()->code_space());
- while (it.has_next()) {
- Page* p = it.next();
- CpuFeatures::FlushICache(p->area_start(), p->area_end() - p->area_start());
- }
-}
-
-
-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());
- isolate_->heap()->IterateSmiRoots(this);
- isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
- isolate_->heap()->RepairFreeListsAfterDeserialization();
- isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
-
- isolate_->heap()->set_native_contexts_list(
- isolate_->heap()->undefined_value());
- isolate_->heap()->set_array_buffers_list(
- isolate_->heap()->undefined_value());
- isolate->heap()->set_new_array_buffer_views_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.
- for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
- Object* source = isolate_->heap()->natives_source_cache()->get(i);
- if (!source->IsUndefined()) {
- ExternalOneByteString::cast(source)->update_data_cache();
- }
- }
-
- FlushICacheForNewCodeObjects();
-
- // Issue code events for newly deserialized code objects.
- LOG_CODE_EVENT(isolate_, LogCodeObjects());
- LOG_CODE_EVENT(isolate_, LogCompiledFunctions());
-}
-
-
-MaybeHandle<Object> Deserializer::DeserializePartial(
- Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
- Handle<FixedArray>* outdated_contexts_out) {
- Initialize(isolate);
- if (!ReserveSpace()) {
- V8::FatalProcessOutOfMemory("deserialize context");
- return MaybeHandle<Object>();
- }
-
- Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::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;
- Object* outdated_contexts;
- VisitPointer(&root);
- VisitPointer(&outdated_contexts);
-
- // 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.
- CHECK_EQ(start_address, code_space->top());
- CHECK(outdated_contexts->IsFixedArray());
- *outdated_contexts_out =
- Handle<FixedArray>(FixedArray::cast(outdated_contexts), isolate);
- return Handle<Object>(root, isolate);
-}
-
-
-MaybeHandle<SharedFunctionInfo> Deserializer::DeserializeCode(
- Isolate* isolate) {
- Initialize(isolate);
- if (!ReserveSpace()) {
- return Handle<SharedFunctionInfo>();
- } else {
- deserializing_user_code_ = true;
- DisallowHeapAllocation no_gc;
- Object* root;
- VisitPointer(&root);
- return Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(root));
- }
-}
-
-
-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::RelinkAllocationSite(AllocationSite* site) {
- 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);
-}
-
-
-// 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 virtual Handle<Object> AsHandle(Isolate* isolate)
- OVERRIDE {
- return handle(string_, isolate);
- }
-
- String* string_;
- uint32_t hash_;
-};
-
-
-HeapObject* Deserializer::ProcessNewObjectFromSerializedCode(HeapObject* obj) {
- 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()) {
- DisallowHeapAllocation no_gc;
- HandleScope scope(isolate_);
- StringTableInsertionKey key(string);
- String* canonical = *StringTable::LookupKey(isolate_, &key);
- string->SetForwardedInternalizedString(canonical);
- return canonical;
- }
- } else if (obj->IsScript()) {
- Script::cast(obj)->set_id(isolate_->heap()->NextScriptId());
- }
- return obj;
-}
-
-
-HeapObject* Deserializer::GetBackReferencedObject(int space) {
- HeapObject* obj;
- BackReference back_reference(source_.GetInt());
- if (space == LO_SPACE) {
- CHECK(back_reference.chunk_index() == 0);
- uint32_t index = back_reference.large_object_index();
- obj = deserialized_large_objects_[index];
- } else {
- DCHECK(space < kNumberOfPreallocatedSpaces);
- uint32_t chunk_index = back_reference.chunk_index();
- DCHECK_LE(chunk_index, current_chunk_[space]);
- uint32_t chunk_offset = back_reference.chunk_offset();
- obj = HeapObject::FromAddress(reservations_[space][chunk_index].start +
- chunk_offset);
- }
- if (deserializing_user_code() && obj->IsInternalizedString()) {
- obj = String::cast(obj)->GetForwardedInternalizedString();
- }
- hot_objects_.Add(obj);
- return obj;
-}
-
-
-// This routine writes the new object into the pointer provided and then
-// returns true if the new object was in young space and false otherwise.
-// The reason for this strange interface is that otherwise the object is
-// written very late, which means the FreeSpace map is not set up by the
-// time we need to use it to mark the space at the end of a page free.
-void Deserializer::ReadObject(int space_number, Object** write_back) {
- Address address;
- HeapObject* obj;
- int next_int = source_.GetInt();
-
- bool double_align = false;
-#ifndef V8_HOST_ARCH_64_BIT
- double_align = next_int == kDoubleAlignmentSentinel;
- if (double_align) next_int = source_.GetInt();
-#endif
-
- DCHECK_NE(kDoubleAlignmentSentinel, next_int);
- int size = next_int << kObjectAlignmentBits;
- int reserved_size = size + (double_align ? kPointerSize : 0);
- address = Allocate(space_number, reserved_size);
- obj = HeapObject::FromAddress(address);
- if (double_align) {
- obj = isolate_->heap()->DoubleAlignForDeserialization(obj, reserved_size);
- address = obj->address();
- }
-
- isolate_->heap()->OnAllocationEvent(obj, size);
- Object** current = reinterpret_cast<Object**>(address);
- Object** limit = current + (size >> kPointerSizeLog2);
- if (FLAG_log_snapshot_positions) {
- LOG(isolate_, SnapshotPositionEvent(address, source_.position()));
- }
- ReadData(current, limit, space_number, address);
-
- // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
- // as a (weak) root. If this root is relocated correctly,
- // RelinkAllocationSite() isn't necessary.
- if (obj->IsAllocationSite()) RelinkAllocationSite(AllocationSite::cast(obj));
-
- // Fix up strings from serialized user code.
- if (deserializing_user_code()) obj = ProcessNewObjectFromSerializedCode(obj);
-
- Object* write_back_obj = obj;
- UnalignedCopy(write_back, &write_back_obj);
-#ifdef DEBUG
- if (obj->IsCode()) {
- DCHECK(space_number == CODE_SPACE || space_number == LO_SPACE);
-#ifdef VERIFY_HEAP
- obj->ObjectVerify();
-#endif // VERIFY_HEAP
- } else {
- DCHECK(space_number != CODE_SPACE);
- }
-#endif // DEBUG
-}
-
-
-// We know the space requirements before deserialization and can
-// pre-allocate that reserved space. During deserialization, all we need
-// to do is to bump up the pointer for each space in the reserved
-// space. This is also used for fixing back references.
-// We may have to split up the pre-allocation into several chunks
-// because it would not fit onto a single page. We do not have to keep
-// track of when to move to the next chunk. An opcode will signal this.
-// Since multiple large objects cannot be folded into one large object
-// space allocation, we have to do an actual allocation when deserializing
-// each large object. Instead of tracking offset for back references, we
-// reference large objects by index.
-Address Deserializer::Allocate(int space_index, int size) {
- if (space_index == LO_SPACE) {
- AlwaysAllocateScope scope(isolate_);
- LargeObjectSpace* lo_space = isolate_->heap()->lo_space();
- Executability exec = static_cast<Executability>(source_.Get());
- AllocationResult result = lo_space->AllocateRaw(size, exec);
- HeapObject* obj = HeapObject::cast(result.ToObjectChecked());
- deserialized_large_objects_.Add(obj);
- return obj->address();
- } else {
- DCHECK(space_index < kNumberOfPreallocatedSpaces);
- Address address = high_water_[space_index];
- DCHECK_NOT_NULL(address);
- high_water_[space_index] += size;
-#ifdef DEBUG
- // Assert that the current reserved chunk is still big enough.
- const Heap::Reservation& reservation = reservations_[space_index];
- int chunk_index = current_chunk_[space_index];
- CHECK_LE(high_water_[space_index], reservation[chunk_index].end);
-#endif
- return address;
- }
-}
-
-
-void Deserializer::ReadData(Object** current, Object** limit, int source_space,
- Address current_object_address) {
- Isolate* const isolate = isolate_;
- // Write barrier support costs around 1% in startup time. In fact there
- // are no new space objects in current boot snapshots, so it's not needed,
- // but that may change.
- bool write_barrier_needed =
- (current_object_address != NULL && source_space != NEW_SPACE &&
- source_space != CELL_SPACE && source_space != CODE_SPACE &&
- source_space != OLD_DATA_SPACE);
- while (current < limit) {
- byte data = source_.Get();
- switch (data) {
-#define CASE_STATEMENT(where, how, within, space_number) \
- case where + how + within + space_number: \
- STATIC_ASSERT((where & ~kWhereMask) == 0); \
- STATIC_ASSERT((how & ~kHowToCodeMask) == 0); \
- STATIC_ASSERT((within & ~kWhereToPointMask) == 0); \
- STATIC_ASSERT((space_number & ~kSpaceMask) == 0);
-
-#define CASE_BODY(where, how, within, space_number_if_any) \
- { \
- bool emit_write_barrier = false; \
- bool current_was_incremented = false; \
- int space_number = space_number_if_any == kAnyOldSpace \
- ? (data & kSpaceMask) \
- : space_number_if_any; \
- if (where == kNewObject && how == kPlain && within == kStartOfObject) { \
- ReadObject(space_number, current); \
- emit_write_barrier = (space_number == NEW_SPACE); \
- } else { \
- Object* new_object = NULL; /* May not be a real Object pointer. */ \
- if (where == kNewObject) { \
- ReadObject(space_number, &new_object); \
- } else if (where == kBackref) { \
- emit_write_barrier = (space_number == NEW_SPACE); \
- new_object = GetBackReferencedObject(data & kSpaceMask); \
- } else if (where == kBackrefWithSkip) { \
- int skip = source_.GetInt(); \
- current = reinterpret_cast<Object**>( \
- reinterpret_cast<Address>(current) + skip); \
- emit_write_barrier = (space_number == NEW_SPACE); \
- new_object = GetBackReferencedObject(data & kSpaceMask); \
- } else if (where == kRootArray) { \
- int root_id = source_.GetInt(); \
- new_object = isolate->heap()->roots_array_start()[root_id]; \
- emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
- } else if (where == kPartialSnapshotCache) { \
- int cache_index = source_.GetInt(); \
- new_object = isolate->partial_snapshot_cache()->at(cache_index); \
- emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
- } else if (where == kExternalReference) { \
- int skip = source_.GetInt(); \
- current = reinterpret_cast<Object**>( \
- reinterpret_cast<Address>(current) + skip); \
- int reference_id = source_.GetInt(); \
- Address address = external_reference_table_->address(reference_id); \
- new_object = reinterpret_cast<Object*>(address); \
- } else if (where == kAttachedReference) { \
- int index = source_.GetInt(); \
- DCHECK(deserializing_user_code() || index == kGlobalProxyReference); \
- new_object = *attached_objects_[index]; \
- emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
- } else { \
- DCHECK(where == kBuiltin); \
- DCHECK(deserializing_user_code()); \
- int builtin_id = source_.GetInt(); \
- DCHECK_LE(0, builtin_id); \
- DCHECK_LT(builtin_id, Builtins::builtin_count); \
- Builtins::Name name = static_cast<Builtins::Name>(builtin_id); \
- new_object = isolate->builtins()->builtin(name); \
- emit_write_barrier = false; \
- } \
- if (within == kInnerPointer) { \
- if (space_number != CODE_SPACE || new_object->IsCode()) { \
- Code* new_code_object = reinterpret_cast<Code*>(new_object); \
- new_object = \
- reinterpret_cast<Object*>(new_code_object->instruction_start()); \
- } else { \
- DCHECK(space_number == CODE_SPACE); \
- Cell* cell = Cell::cast(new_object); \
- new_object = reinterpret_cast<Object*>(cell->ValueAddress()); \
- } \
- } \
- if (how == kFromCode) { \
- Address location_of_branch_data = reinterpret_cast<Address>(current); \
- Assembler::deserialization_set_special_target_at( \
- location_of_branch_data, \
- Code::cast(HeapObject::FromAddress(current_object_address)), \
- reinterpret_cast<Address>(new_object)); \
- location_of_branch_data += Assembler::kSpecialTargetSize; \
- current = reinterpret_cast<Object**>(location_of_branch_data); \
- current_was_incremented = true; \
- } else { \
- UnalignedCopy(current, &new_object); \
- } \
- } \
- if (emit_write_barrier && write_barrier_needed) { \
- Address current_address = reinterpret_cast<Address>(current); \
- isolate->heap()->RecordWrite( \
- current_object_address, \
- static_cast<int>(current_address - current_object_address)); \
- } \
- if (!current_was_incremented) { \
- current++; \
- } \
- break; \
- }
-
-// This generates a case and a body for the new space (which has to do extra
-// write barrier handling) and handles the other spaces with fall-through cases
-// and one body.
-#define ALL_SPACES(where, how, within) \
- CASE_STATEMENT(where, how, within, NEW_SPACE) \
- CASE_BODY(where, how, within, NEW_SPACE) \
- CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
- CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
- CASE_STATEMENT(where, how, within, CODE_SPACE) \
- CASE_STATEMENT(where, how, within, MAP_SPACE) \
- CASE_STATEMENT(where, how, within, CELL_SPACE) \
- CASE_STATEMENT(where, how, within, LO_SPACE) \
- CASE_BODY(where, how, within, kAnyOldSpace)
-
-#define FOUR_CASES(byte_code) \
- case byte_code: \
- case byte_code + 1: \
- case byte_code + 2: \
- case byte_code + 3:
-
-#define SIXTEEN_CASES(byte_code) \
- FOUR_CASES(byte_code) \
- FOUR_CASES(byte_code + 4) \
- FOUR_CASES(byte_code + 8) \
- FOUR_CASES(byte_code + 12)
-
- // Deserialize a new object and write a pointer to it to the current
- // object.
- ALL_SPACES(kNewObject, kPlain, kStartOfObject)
- // Support for direct instruction pointers in functions. It's an inner
- // pointer because it points at the entry point, not at the start of the
- // code object.
- CASE_STATEMENT(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
- CASE_BODY(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
- // Deserialize a new code object and write a pointer to its first
- // instruction to the current code object.
- ALL_SPACES(kNewObject, kFromCode, kInnerPointer)
- // Find a recently deserialized object using its offset from the current
- // allocation point and write a pointer to it to the current object.
- ALL_SPACES(kBackref, kPlain, kStartOfObject)
- ALL_SPACES(kBackrefWithSkip, kPlain, kStartOfObject)
-#if defined(V8_TARGET_ARCH_MIPS) || defined(V8_TARGET_ARCH_MIPS64) || \
- defined(V8_TARGET_ARCH_PPC) || V8_OOL_CONSTANT_POOL
- // Deserialize a new object from pointer found in code and write
- // a pointer to it to the current object. Required only for MIPS, PPC or
- // ARM with ool constant pool, and omitted on the other architectures
- // because it is fully unrolled and would cause bloat.
- ALL_SPACES(kNewObject, kFromCode, kStartOfObject)
- // Find a recently deserialized code object using its offset from the
- // current allocation point and write a pointer to it to the current
- // object. Required only for MIPS, PPC or ARM with ool constant pool.
- ALL_SPACES(kBackref, kFromCode, kStartOfObject)
- ALL_SPACES(kBackrefWithSkip, kFromCode, kStartOfObject)
-#endif
- // Find a recently deserialized code object using its offset from the
- // current allocation point and write a pointer to its first instruction
- // to the current code object or the instruction pointer in a function
- // object.
- ALL_SPACES(kBackref, kFromCode, kInnerPointer)
- ALL_SPACES(kBackrefWithSkip, kFromCode, kInnerPointer)
- ALL_SPACES(kBackref, kPlain, kInnerPointer)
- ALL_SPACES(kBackrefWithSkip, kPlain, kInnerPointer)
- // Find an object in the roots array and write a pointer to it to the
- // current object.
- CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
- CASE_BODY(kRootArray, kPlain, kStartOfObject, 0)
-#if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL || \
- defined(V8_TARGET_ARCH_MIPS64) || defined(V8_TARGET_ARCH_PPC)
- // Find an object in the roots array and write a pointer to it to in code.
- CASE_STATEMENT(kRootArray, kFromCode, kStartOfObject, 0)
- CASE_BODY(kRootArray, kFromCode, kStartOfObject, 0)
-#endif
- // Find an object in the partial snapshots cache and write a pointer to it
- // to the current object.
- CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
- CASE_BODY(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
- // Find an code entry in the partial snapshots cache and
- // write a pointer to it to the current object.
- CASE_STATEMENT(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
- CASE_BODY(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
- // Find an external reference and write a pointer to it to the current
- // object.
- CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
- CASE_BODY(kExternalReference, kPlain, kStartOfObject, 0)
- // Find an external reference and write a pointer to it in the current
- // code object.
- CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
- CASE_BODY(kExternalReference, kFromCode, kStartOfObject, 0)
- // Find an object in the attached references and write a pointer to it to
- // the current object.
- CASE_STATEMENT(kAttachedReference, kPlain, kStartOfObject, 0)
- CASE_BODY(kAttachedReference, kPlain, kStartOfObject, 0)
- CASE_STATEMENT(kAttachedReference, kPlain, kInnerPointer, 0)
- CASE_BODY(kAttachedReference, kPlain, kInnerPointer, 0)
- CASE_STATEMENT(kAttachedReference, kFromCode, kInnerPointer, 0)
- CASE_BODY(kAttachedReference, kFromCode, kInnerPointer, 0)
- // Find a builtin and write a pointer to it to the current object.
- CASE_STATEMENT(kBuiltin, kPlain, kStartOfObject, 0)
- CASE_BODY(kBuiltin, kPlain, kStartOfObject, 0)
- CASE_STATEMENT(kBuiltin, kPlain, kInnerPointer, 0)
- CASE_BODY(kBuiltin, kPlain, kInnerPointer, 0)
- CASE_STATEMENT(kBuiltin, kFromCode, kInnerPointer, 0)
- CASE_BODY(kBuiltin, kFromCode, kInnerPointer, 0)
-
-#undef CASE_STATEMENT
-#undef CASE_BODY
-#undef ALL_SPACES
-
- case kSkip: {
- int size = source_.GetInt();
- current = reinterpret_cast<Object**>(
- reinterpret_cast<intptr_t>(current) + size);
- break;
- }
-
- case kInternalReferenceEncoded:
- case kInternalReference: {
- // Internal reference address is not encoded via skip, but by offset
- // from code entry.
- int pc_offset = source_.GetInt();
- int target_offset = source_.GetInt();
- Code* code =
- Code::cast(HeapObject::FromAddress(current_object_address));
- DCHECK(0 <= pc_offset && pc_offset <= code->instruction_size());
- DCHECK(0 <= target_offset && target_offset <= code->instruction_size());
- Address pc = code->entry() + pc_offset;
- Address target = code->entry() + target_offset;
- Assembler::deserialization_set_target_internal_reference_at(
- pc, target, data == kInternalReference
- ? RelocInfo::INTERNAL_REFERENCE
- : RelocInfo::INTERNAL_REFERENCE_ENCODED);
- break;
- }
-
- case kNop:
- break;
-
- case kNextChunk: {
- int space = source_.Get();
- DCHECK(space < kNumberOfPreallocatedSpaces);
- int chunk_index = current_chunk_[space];
- const Heap::Reservation& reservation = reservations_[space];
- // Make sure the current chunk is indeed exhausted.
- CHECK_EQ(reservation[chunk_index].end, high_water_[space]);
- // Move to next reserved chunk.
- chunk_index = ++current_chunk_[space];
- CHECK_LT(chunk_index, reservation.length());
- high_water_[space] = reservation[chunk_index].start;
- break;
- }
-
- case kSynchronize:
- // If we get here then that indicates that you have a mismatch between
- // the number of GC roots when serializing and deserializing.
- CHECK(false);
- break;
-
- case kNativesStringResource: {
- DCHECK(!isolate_->heap()->deserialization_complete());
- int index = source_.Get();
- Vector<const char> source_vector = Natives::GetScriptSource(index);
- NativesExternalStringResource* resource =
- new NativesExternalStringResource(source_vector.start(),
- source_vector.length());
- Object* resource_obj = reinterpret_cast<Object*>(resource);
- UnalignedCopy(current++, &resource_obj);
- break;
- }
-
- // Deserialize raw data of variable length.
- case kVariableRawData: {
- int size_in_bytes = source_.GetInt();
- byte* raw_data_out = reinterpret_cast<byte*>(current);
- source_.CopyRaw(raw_data_out, size_in_bytes);
- break;
- }
-
- case kVariableRepeat: {
- int repeats = source_.GetInt();
- Object* object = current[-1];
- DCHECK(!isolate->heap()->InNewSpace(object));
- for (int i = 0; i < repeats; i++) UnalignedCopy(current++, &object);
- break;
- }
-
- STATIC_ASSERT(kNumberOfRootArrayConstants == Heap::kOldSpaceRoots);
- STATIC_ASSERT(kNumberOfRootArrayConstants == 32);
- SIXTEEN_CASES(kRootArrayConstantsWithSkip)
- SIXTEEN_CASES(kRootArrayConstantsWithSkip + 16) {
- int skip = source_.GetInt();
- current = reinterpret_cast<Object**>(
- reinterpret_cast<intptr_t>(current) + skip);
- // Fall through.
- }
-
- SIXTEEN_CASES(kRootArrayConstants)
- SIXTEEN_CASES(kRootArrayConstants + 16) {
- int root_id = data & kRootArrayConstantsMask;
- Object* object = isolate->heap()->roots_array_start()[root_id];
- DCHECK(!isolate->heap()->InNewSpace(object));
- UnalignedCopy(current++, &object);
- break;
- }
-
- STATIC_ASSERT(kNumberOfHotObjects == 8);
- FOUR_CASES(kHotObjectWithSkip)
- FOUR_CASES(kHotObjectWithSkip + 4) {
- int skip = source_.GetInt();
- current = reinterpret_cast<Object**>(
- reinterpret_cast<Address>(current) + skip);
- // Fall through.
- }
-
- FOUR_CASES(kHotObject)
- FOUR_CASES(kHotObject + 4) {
- int index = data & kHotObjectMask;
- Object* hot_object = hot_objects_.Get(index);
- UnalignedCopy(current, &hot_object);
- if (write_barrier_needed && isolate->heap()->InNewSpace(hot_object)) {
- Address current_address = reinterpret_cast<Address>(current);
- isolate->heap()->RecordWrite(
- current_object_address,
- static_cast<int>(current_address - current_object_address));
- }
- current++;
- break;
- }
-
- // Deserialize raw data of fixed length from 1 to 32 words.
- STATIC_ASSERT(kNumberOfFixedRawData == 32);
- SIXTEEN_CASES(kFixedRawData)
- SIXTEEN_CASES(kFixedRawData + 16) {
- byte* raw_data_out = reinterpret_cast<byte*>(current);
- int size_in_bytes = (data - kFixedRawDataStart) << kPointerSizeLog2;
- source_.CopyRaw(raw_data_out, size_in_bytes);
- current = reinterpret_cast<Object**>(raw_data_out + size_in_bytes);
- break;
- }
-
- STATIC_ASSERT(kNumberOfFixedRepeat == 16);
- SIXTEEN_CASES(kFixedRepeat) {
- int repeats = data - kFixedRepeatStart;
- Object* object;
- UnalignedCopy(&object, current - 1);
- DCHECK(!isolate->heap()->InNewSpace(object));
- for (int i = 0; i < repeats; i++) UnalignedCopy(current++, &object);
- break;
- }
-
-#undef SIXTEEN_CASES
-#undef FOUR_CASES
-
- default:
- CHECK(false);
- }
- }
- CHECK_EQ(limit, current);
-}
-
-
-Serializer::Serializer(Isolate* isolate, SnapshotByteSink* sink)
- : isolate_(isolate),
- sink_(sink),
- external_reference_encoder_(isolate),
- root_index_map_(isolate),
- code_address_map_(NULL),
- large_objects_total_size_(0),
- seen_large_objects_index_(0) {
- // The serializer is meant to be used only to generate initial heap images
- // from a context in which there is only one isolate.
- for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) {
- pending_chunk_[i] = 0;
- max_chunk_size_[i] = static_cast<uint32_t>(
- MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(i)));
- }
-}
-
-
-Serializer::~Serializer() {
- if (code_address_map_ != NULL) delete code_address_map_;
-}
-
-
-void StartupSerializer::SerializeStrongReferences() {
- Isolate* isolate = this->isolate();
- // No active threads.
- CHECK_NULL(isolate->thread_manager()->FirstThreadStateInUse());
- // No active or weak handles.
- CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
- CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
- CHECK_EQ(0, isolate->eternal_handles()->NumberOfHandles());
- // We don't support serializing installed extensions.
- CHECK(!isolate->has_installed_extensions());
- isolate->heap()->IterateSmiRoots(this);
- isolate->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
-}
-
-
-void StartupSerializer::VisitPointers(Object** start, Object** end) {
- for (Object** current = start; current < end; current++) {
- if (start == isolate()->heap()->roots_array_start()) {
- root_index_wave_front_ =
- Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
- }
- if (ShouldBeSkipped(current)) {
- sink_->Put(kSkip, "Skip");
- sink_->PutInt(kPointerSize, "SkipOneWord");
- } else if ((*current)->IsSmi()) {
- sink_->Put(kOnePointerRawData, "Smi");
- for (int i = 0; i < kPointerSize; i++) {
- sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
- }
- } else {
- SerializeObject(HeapObject::cast(*current), kPlain, kStartOfObject, 0);
- }
- }
-}
-
-
-void PartialSerializer::Serialize(Object** o) {
- if ((*o)->IsContext()) {
- Context* context = Context::cast(*o);
- global_object_ = context->global_object();
- back_reference_map()->AddGlobalProxy(context->global_proxy());
- }
- VisitPointer(o);
- SerializeOutdatedContextsAsFixedArray();
- Pad();
-}
-
-
-void PartialSerializer::SerializeOutdatedContextsAsFixedArray() {
- int length = outdated_contexts_.length();
- if (length == 0) {
- FixedArray* empty = isolate_->heap()->empty_fixed_array();
- SerializeObject(empty, kPlain, kStartOfObject, 0);
- } else {
- // Serialize an imaginary fixed array containing outdated contexts.
- int size = FixedArray::SizeFor(length);
- Allocate(NEW_SPACE, size);
- sink_->Put(kNewObject + NEW_SPACE, "emulated FixedArray");
- sink_->PutInt(size >> kObjectAlignmentBits, "FixedArray size in words");
- Map* map = isolate_->heap()->fixed_array_map();
- SerializeObject(map, kPlain, kStartOfObject, 0);
- Smi* length_smi = Smi::FromInt(length);
- sink_->Put(kOnePointerRawData, "Smi");
- for (int i = 0; i < kPointerSize; i++) {
- sink_->Put(reinterpret_cast<byte*>(&length_smi)[i], "Byte");
- }
- for (int i = 0; i < length; i++) {
- BackReference back_ref = outdated_contexts_[i];
- DCHECK(BackReferenceIsAlreadyAllocated(back_ref));
- sink_->Put(kBackref + back_ref.space(), "BackRef");
- sink_->PutInt(back_ref.reference(), "BackRefValue");
- }
- }
-}
-
-
-bool Serializer::ShouldBeSkipped(Object** current) {
- Object** roots = isolate()->heap()->roots_array_start();
- return current == &roots[Heap::kStoreBufferTopRootIndex]
- || current == &roots[Heap::kStackLimitRootIndex]
- || current == &roots[Heap::kRealStackLimitRootIndex];
-}
-
-
-void Serializer::VisitPointers(Object** start, Object** end) {
- for (Object** current = start; current < end; current++) {
- if ((*current)->IsSmi()) {
- sink_->Put(kOnePointerRawData, "Smi");
- for (int i = 0; i < kPointerSize; i++) {
- sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
- }
- } else {
- SerializeObject(HeapObject::cast(*current), kPlain, kStartOfObject, 0);
- }
- }
-}
-
-
-void Serializer::EncodeReservations(
- List<SerializedData::Reservation>* out) const {
- for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) {
- for (int j = 0; j < completed_chunks_[i].length(); j++) {
- out->Add(SerializedData::Reservation(completed_chunks_[i][j]));
- }
-
- if (pending_chunk_[i] > 0 || completed_chunks_[i].length() == 0) {
- out->Add(SerializedData::Reservation(pending_chunk_[i]));
- }
- out->last().mark_as_last();
- }
-
- out->Add(SerializedData::Reservation(large_objects_total_size_));
- out->last().mark_as_last();
-}
-
-
-// This ensures that the partial snapshot cache keeps things alive during GC and
-// tracks their movement. When it is called during serialization of the startup
-// snapshot nothing happens. When the partial (context) snapshot is created,
-// this array is populated with the pointers that the partial snapshot will
-// need. As that happens we emit serialized objects to the startup snapshot
-// that correspond to the elements of this cache array. On deserialization we
-// therefore need to visit the cache array. This fills it up with pointers to
-// deserialized objects.
-void SerializerDeserializer::Iterate(Isolate* isolate,
- ObjectVisitor* visitor) {
- if (isolate->serializer_enabled()) return;
- List<Object*>* cache = isolate->partial_snapshot_cache();
- for (int i = 0;; ++i) {
- // Extend the array ready to get a value when deserializing.
- if (cache->length() <= i) cache->Add(Smi::FromInt(0));
- visitor->VisitPointer(&cache->at(i));
- // Sentinel is the undefined object, which is a root so it will not normally
- // be found in the cache.
- if (cache->at(i)->IsUndefined()) break;
- }
-}
-
-
-int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
- Isolate* isolate = this->isolate();
- List<Object*>* cache = isolate->partial_snapshot_cache();
- int new_index = cache->length();
-
- int index = partial_cache_index_map_.LookupOrInsert(heap_object, new_index);
- if (index == PartialCacheIndexMap::kInvalidIndex) {
- // We didn't find the object in the cache. So we add it to the cache and
- // then visit the pointer so that it becomes part of the startup snapshot
- // and we can refer to it from the partial snapshot.
- cache->Add(heap_object);
- startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object));
- // We don't recurse from the startup snapshot generator into the partial
- // snapshot generator.
- return new_index;
- }
- return index;
-}
-
-
-#ifdef DEBUG
-bool Serializer::BackReferenceIsAlreadyAllocated(BackReference reference) {
- DCHECK(reference.is_valid());
- DCHECK(!reference.is_source());
- DCHECK(!reference.is_global_proxy());
- AllocationSpace space = reference.space();
- int chunk_index = reference.chunk_index();
- if (space == LO_SPACE) {
- return chunk_index == 0 &&
- reference.large_object_index() < seen_large_objects_index_;
- } else if (chunk_index == completed_chunks_[space].length()) {
- return reference.chunk_offset() < pending_chunk_[space];
- } else {
- return chunk_index < completed_chunks_[space].length() &&
- reference.chunk_offset() < completed_chunks_[space][chunk_index];
- }
-}
-#endif // DEBUG
-
-
-bool Serializer::SerializeKnownObject(HeapObject* obj, HowToCode how_to_code,
- WhereToPoint where_to_point, int skip) {
- if (how_to_code == kPlain && where_to_point == kStartOfObject) {
- // Encode a reference to a hot object by its index in the working set.
- int index = hot_objects_.Find(obj);
- if (index != HotObjectsList::kNotFound) {
- DCHECK(index >= 0 && index < kNumberOfHotObjects);
- if (FLAG_trace_serializer) {
- PrintF(" Encoding hot object %d:", index);
- obj->ShortPrint();
- PrintF("\n");
- }
- if (skip != 0) {
- sink_->Put(kHotObjectWithSkip + index, "HotObjectWithSkip");
- sink_->PutInt(skip, "HotObjectSkipDistance");
- } else {
- sink_->Put(kHotObject + index, "HotObject");
- }
- return true;
- }
- }
- BackReference back_reference = back_reference_map_.Lookup(obj);
- if (back_reference.is_valid()) {
- // Encode the location of an already deserialized object in order to write
- // its location into a later object. We can encode the location as an
- // offset fromthe start of the deserialized objects or as an offset
- // backwards from thecurrent allocation pointer.
- if (back_reference.is_source()) {
- FlushSkip(skip);
- if (FLAG_trace_serializer) PrintF(" Encoding source object\n");
- DCHECK(how_to_code == kPlain && where_to_point == kStartOfObject);
- sink_->Put(kAttachedReference + kPlain + kStartOfObject, "Source");
- sink_->PutInt(kSourceObjectReference, "kSourceObjectReference");
- } else if (back_reference.is_global_proxy()) {
- FlushSkip(skip);
- if (FLAG_trace_serializer) PrintF(" Encoding global proxy\n");
- DCHECK(how_to_code == kPlain && where_to_point == kStartOfObject);
- sink_->Put(kAttachedReference + kPlain + kStartOfObject, "Global Proxy");
- sink_->PutInt(kGlobalProxyReference, "kGlobalProxyReference");
- } else {
- if (FLAG_trace_serializer) {
- PrintF(" Encoding back reference to: ");
- obj->ShortPrint();
- PrintF("\n");
- }
-
- AllocationSpace space = back_reference.space();
- if (skip == 0) {
- sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRef");
- } else {
- sink_->Put(kBackrefWithSkip + how_to_code + where_to_point + space,
- "BackRefWithSkip");
- sink_->PutInt(skip, "BackRefSkipDistance");
- }
- DCHECK(BackReferenceIsAlreadyAllocated(back_reference));
- sink_->PutInt(back_reference.reference(), "BackRefValue");
-
- hot_objects_.Add(obj);
- }
- return true;
- }
- return false;
-}
-
-
-void StartupSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
- WhereToPoint where_to_point, int skip) {
- DCHECK(!obj->IsJSFunction());
-
- int root_index = root_index_map_.Lookup(obj);
- // We can only encode roots as such if it has already been serialized.
- // That applies to root indices below the wave front.
- if (root_index != RootIndexMap::kInvalidRootIndex &&
- root_index < root_index_wave_front_) {
- PutRoot(root_index, obj, how_to_code, where_to_point, skip);
- return;
- }
-
- if (obj->IsCode() && Code::cast(obj)->kind() == Code::FUNCTION) {
- obj = isolate()->builtins()->builtin(Builtins::kCompileLazy);
- }
-
- if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return;
-
- FlushSkip(skip);
-
- // Object has not yet been serialized. Serialize it here.
- ObjectSerializer object_serializer(this, obj, sink_, how_to_code,
- where_to_point);
- object_serializer.Serialize();
-}
-
-
-void StartupSerializer::SerializeWeakReferences() {
- // This phase comes right after the serialization (of the snapshot).
- // After we have done the partial serialization the partial snapshot cache
- // will contain some references needed to decode the partial snapshot. We
- // add one entry with 'undefined' which is the sentinel that the deserializer
- // uses to know it is done deserializing the array.
- Object* undefined = isolate()->heap()->undefined_value();
- VisitPointer(&undefined);
- isolate()->heap()->IterateWeakRoots(this, VISIT_ALL);
- Pad();
-}
-
-
-void Serializer::PutRoot(int root_index,
- HeapObject* object,
- SerializerDeserializer::HowToCode how_to_code,
- SerializerDeserializer::WhereToPoint where_to_point,
- int skip) {
- if (FLAG_trace_serializer) {
- PrintF(" Encoding root %d:", root_index);
- object->ShortPrint();
- PrintF("\n");
- }
-
- if (how_to_code == kPlain && where_to_point == kStartOfObject &&
- root_index < kNumberOfRootArrayConstants &&
- !isolate()->heap()->InNewSpace(object)) {
- if (skip == 0) {
- sink_->Put(kRootArrayConstants + root_index, "RootConstant");
- } else {
- sink_->Put(kRootArrayConstantsWithSkip + root_index, "RootConstant");
- sink_->PutInt(skip, "SkipInPutRoot");
- }
- } else {
- FlushSkip(skip);
- sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
- sink_->PutInt(root_index, "root_index");
- }
-}
-
-
-void PartialSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
- WhereToPoint where_to_point, int skip) {
- if (obj->IsMap()) {
- // The code-caches link to context-specific code objects, which
- // the startup and context serializes cannot currently handle.
- DCHECK(Map::cast(obj)->code_cache() == obj->GetHeap()->empty_fixed_array());
- }
-
- // Replace typed arrays by undefined.
- if (obj->IsJSTypedArray()) obj = isolate_->heap()->undefined_value();
-
- 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 (ShouldBeInThePartialSnapshotCache(obj)) {
- FlushSkip(skip);
-
- int cache_index = PartialSnapshotCacheIndex(obj);
- sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
- "PartialSnapshotCache");
- sink_->PutInt(cache_index, "partial_snapshot_cache_index");
- return;
- }
-
- // Pointers from the partial snapshot to the objects in the startup snapshot
- // should go through the root array or through the partial snapshot cache.
- // If this is not the case you may have to add something to the root array.
- DCHECK(!startup_serializer_->back_reference_map()->Lookup(obj).is_valid());
- // All the internalized strings that the partial snapshot needs should be
- // either in the root table or in the partial snapshot cache.
- DCHECK(!obj->IsInternalizedString());
-
- if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return;
-
- FlushSkip(skip);
-
- // Object has not yet been serialized. Serialize it here.
- ObjectSerializer serializer(this, obj, sink_, how_to_code, where_to_point);
- serializer.Serialize();
-
- if (obj->IsContext() &&
- Context::cast(obj)->global_object() == global_object_) {
- // Context refers to the current global object. This reference will
- // become outdated after deserialization.
- BackReference back_reference = back_reference_map_.Lookup(obj);
- DCHECK(back_reference.is_valid());
- outdated_contexts_.Add(back_reference);
- }
-}
-
-
-void Serializer::ObjectSerializer::SerializePrologue(AllocationSpace space,
- int size, Map* map) {
- if (serializer_->code_address_map_) {
- const char* code_name =
- serializer_->code_address_map_->Lookup(object_->address());
- LOG(serializer_->isolate_,
- CodeNameEvent(object_->address(), sink_->Position(), code_name));
- LOG(serializer_->isolate_,
- SnapshotPositionEvent(object_->address(), sink_->Position()));
- }
-
- BackReference back_reference;
- if (space == LO_SPACE) {
- sink_->Put(kNewObject + reference_representation_ + space,
- "NewLargeObject");
- sink_->PutInt(size >> kObjectAlignmentBits, "ObjectSizeInWords");
- if (object_->IsCode()) {
- sink_->Put(EXECUTABLE, "executable large object");
- } else {
- sink_->Put(NOT_EXECUTABLE, "not executable large object");
- }
- back_reference = serializer_->AllocateLargeObject(size);
- } else {
- bool needs_double_align = false;
- if (object_->NeedsToEnsureDoubleAlignment()) {
- // Add wriggle room for double alignment padding.
- back_reference = serializer_->Allocate(space, size + kPointerSize);
- needs_double_align = true;
- } else {
- back_reference = serializer_->Allocate(space, size);
- }
- sink_->Put(kNewObject + reference_representation_ + space, "NewObject");
- if (needs_double_align)
- sink_->PutInt(kDoubleAlignmentSentinel, "DoubleAlignSentinel");
- int encoded_size = size >> kObjectAlignmentBits;
- DCHECK_NE(kDoubleAlignmentSentinel, encoded_size);
- sink_->PutInt(encoded_size, "ObjectSizeInWords");
- }
-
- // Mark this object as already serialized.
- serializer_->back_reference_map()->Add(object_, back_reference);
-
- // Serialize the map (first word of the object).
- serializer_->SerializeObject(map, kPlain, kStartOfObject, 0);
-}
-
-
-void Serializer::ObjectSerializer::SerializeExternalString() {
- // Instead of serializing this as an external string, we serialize
- // an imaginary sequential string with the same content.
- Isolate* isolate = serializer_->isolate();
- DCHECK(object_->IsExternalString());
- DCHECK(object_->map() != isolate->heap()->native_source_string_map());
- ExternalString* string = ExternalString::cast(object_);
- int length = string->length();
- Map* map;
- int content_size;
- int allocation_size;
- const byte* resource;
- // Find the map and size for the imaginary sequential string.
- bool internalized = object_->IsInternalizedString();
- if (object_->IsExternalOneByteString()) {
- map = internalized ? isolate->heap()->one_byte_internalized_string_map()
- : isolate->heap()->one_byte_string_map();
- allocation_size = SeqOneByteString::SizeFor(length);
- content_size = length * kCharSize;
- resource = reinterpret_cast<const byte*>(
- ExternalOneByteString::cast(string)->resource()->data());
- } else {
- map = internalized ? isolate->heap()->internalized_string_map()
- : isolate->heap()->string_map();
- allocation_size = SeqTwoByteString::SizeFor(length);
- content_size = length * kShortSize;
- resource = reinterpret_cast<const byte*>(
- ExternalTwoByteString::cast(string)->resource()->data());
- }
-
- AllocationSpace space = (allocation_size > Page::kMaxRegularHeapObjectSize)
- ? LO_SPACE
- : OLD_DATA_SPACE;
- SerializePrologue(space, allocation_size, map);
-
- // Output the rest of the imaginary string.
- int bytes_to_output = allocation_size - HeapObject::kHeaderSize;
-
- // Output raw data header. Do not bother with common raw length cases here.
- sink_->Put(kVariableRawData, "RawDataForString");
- sink_->PutInt(bytes_to_output, "length");
-
- // Serialize string header (except for map).
- Address string_start = string->address();
- for (int i = HeapObject::kHeaderSize; i < SeqString::kHeaderSize; i++) {
- sink_->PutSection(string_start[i], "StringHeader");
- }
-
- // Serialize string content.
- sink_->PutRaw(resource, content_size, "StringContent");
-
- // Since the allocation size is rounded up to object alignment, there
- // maybe left-over bytes that need to be padded.
- int padding_size = allocation_size - SeqString::kHeaderSize - content_size;
- DCHECK(0 <= padding_size && padding_size < kObjectAlignment);
- for (int i = 0; i < padding_size; i++) sink_->PutSection(0, "StringPadding");
-
- sink_->Put(kSkip, "SkipAfterString");
- sink_->PutInt(bytes_to_output, "SkipDistance");
-}
-
-
-void Serializer::ObjectSerializer::Serialize() {
- if (FLAG_trace_serializer) {
- PrintF(" Encoding heap object: ");
- object_->ShortPrint();
- PrintF("\n");
- }
-
- // We cannot serialize typed array objects correctly.
- DCHECK(!object_->IsJSTypedArray());
-
- if (object_->IsScript()) {
- // Clear cached line ends.
- Object* undefined = serializer_->isolate()->heap()->undefined_value();
- Script::cast(object_)->set_line_ends(undefined);
- }
-
- if (object_->IsExternalString()) {
- Heap* heap = serializer_->isolate()->heap();
- if (object_->map() != heap->native_source_string_map()) {
- // Usually we cannot recreate resources for external strings. To work
- // around this, external strings are serialized to look like ordinary
- // sequential strings.
- // The exception are native source code strings, since we can recreate
- // their resources. In that case we fall through and leave it to
- // VisitExternalOneByteString further down.
- SerializeExternalString();
- return;
- }
- }
-
- int size = object_->Size();
- Map* map = object_->map();
- AllocationSpace space =
- MemoryChunk::FromAddress(object_->address())->owner()->identity();
- SerializePrologue(space, size, map);
-
- // Serialize the rest of the object.
- CHECK_EQ(0, bytes_processed_so_far_);
- bytes_processed_so_far_ = kPointerSize;
-
- object_->IterateBody(map->instance_type(), size, this);
- OutputRawData(object_->address() + size);
-}
-
-
-void Serializer::ObjectSerializer::VisitPointers(Object** start,
- Object** end) {
- Object** current = start;
- while (current < end) {
- while (current < end && (*current)->IsSmi()) current++;
- if (current < end) OutputRawData(reinterpret_cast<Address>(current));
-
- while (current < end && !(*current)->IsSmi()) {
- HeapObject* current_contents = HeapObject::cast(*current);
- int root_index = serializer_->root_index_map()->Lookup(current_contents);
- // Repeats are not subject to the write barrier so we can only use
- // immortal immovable root members. They are never in new space.
- if (current != start && root_index != RootIndexMap::kInvalidRootIndex &&
- Heap::RootIsImmortalImmovable(root_index) &&
- current_contents == current[-1]) {
- DCHECK(!serializer_->isolate()->heap()->InNewSpace(current_contents));
- int repeat_count = 1;
- while (¤t[repeat_count] < end - 1 &&
- current[repeat_count] == current_contents) {
- repeat_count++;
- }
- current += repeat_count;
- bytes_processed_so_far_ += repeat_count * kPointerSize;
- if (repeat_count > kNumberOfFixedRepeat) {
- sink_->Put(kVariableRepeat, "VariableRepeat");
- sink_->PutInt(repeat_count, "repeat count");
- } else {
- sink_->Put(kFixedRepeatStart + repeat_count, "FixedRepeat");
- }
- } else {
- serializer_->SerializeObject(
- current_contents, kPlain, kStartOfObject, 0);
- bytes_processed_so_far_ += kPointerSize;
- current++;
- }
- }
- }
-}
-
-
-void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
- // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
- if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
-
- int skip = OutputRawData(rinfo->target_address_address(),
- kCanReturnSkipInsteadOfSkipping);
- HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
- Object* object = rinfo->target_object();
- serializer_->SerializeObject(HeapObject::cast(object), how_to_code,
- kStartOfObject, skip);
- bytes_processed_so_far_ += rinfo->target_address_size();
-}
-
-
-void Serializer::ObjectSerializer::VisitExternalReference(Address* p) {
- int skip = OutputRawData(reinterpret_cast<Address>(p),
- kCanReturnSkipInsteadOfSkipping);
- sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
- sink_->PutInt(skip, "SkipB4ExternalRef");
- Address target = *p;
- sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
- bytes_processed_so_far_ += kPointerSize;
-}
-
-
-void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
- int skip = OutputRawData(rinfo->target_address_address(),
- kCanReturnSkipInsteadOfSkipping);
- HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
- sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
- sink_->PutInt(skip, "SkipB4ExternalRef");
- Address target = rinfo->target_external_reference();
- sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
- bytes_processed_so_far_ += rinfo->target_address_size();
-}
-
-
-void Serializer::ObjectSerializer::VisitInternalReference(RelocInfo* rinfo) {
- // We can only reference to internal references of code that has been output.
- DCHECK(is_code_object_ && code_has_been_output_);
- // We do not use skip from last patched pc to find the pc to patch, since
- // target_address_address may not return addresses in ascending order when
- // used for internal references. External references may be stored at the
- // end of the code in the constant pool, whereas internal references are
- // inline. That would cause the skip to be negative. Instead, we store the
- // offset from code entry.
- Address entry = Code::cast(object_)->entry();
- intptr_t pc_offset = rinfo->target_internal_reference_address() - entry;
- intptr_t target_offset = rinfo->target_internal_reference() - entry;
- DCHECK(0 <= pc_offset &&
- pc_offset <= Code::cast(object_)->instruction_size());
- DCHECK(0 <= target_offset &&
- target_offset <= Code::cast(object_)->instruction_size());
- sink_->Put(rinfo->rmode() == RelocInfo::INTERNAL_REFERENCE
- ? kInternalReference
- : kInternalReferenceEncoded,
- "InternalRef");
- sink_->PutInt(static_cast<uintptr_t>(pc_offset), "internal ref address");
- sink_->PutInt(static_cast<uintptr_t>(target_offset), "internal ref value");
-}
-
-
-void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
- int skip = OutputRawData(rinfo->target_address_address(),
- kCanReturnSkipInsteadOfSkipping);
- HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
- sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
- sink_->PutInt(skip, "SkipB4ExternalRef");
- Address target = rinfo->target_address();
- sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
- bytes_processed_so_far_ += rinfo->target_address_size();
-}
-
-
-void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
- // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
- if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
-
- int skip = OutputRawData(rinfo->target_address_address(),
- kCanReturnSkipInsteadOfSkipping);
- Code* object = Code::GetCodeFromTargetAddress(rinfo->target_address());
- serializer_->SerializeObject(object, kFromCode, kInnerPointer, skip);
- bytes_processed_so_far_ += rinfo->target_address_size();
-}
-
-
-void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
- int skip = OutputRawData(entry_address, kCanReturnSkipInsteadOfSkipping);
- Code* object = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
- serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
- bytes_processed_so_far_ += kPointerSize;
-}
-
-
-void Serializer::ObjectSerializer::VisitCell(RelocInfo* rinfo) {
- // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
- if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
-
- int skip = OutputRawData(rinfo->pc(), kCanReturnSkipInsteadOfSkipping);
- Cell* object = Cell::cast(rinfo->target_cell());
- serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
- bytes_processed_so_far_ += kPointerSize;
-}
-
-
-void Serializer::ObjectSerializer::VisitExternalOneByteString(
- v8::String::ExternalOneByteStringResource** resource_pointer) {
- Address references_start = reinterpret_cast<Address>(resource_pointer);
- OutputRawData(references_start);
- for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
- Object* source =
- serializer_->isolate()->heap()->natives_source_cache()->get(i);
- if (!source->IsUndefined()) {
- ExternalOneByteString* string = ExternalOneByteString::cast(source);
- typedef v8::String::ExternalOneByteStringResource Resource;
- const Resource* resource = string->resource();
- if (resource == *resource_pointer) {
- sink_->Put(kNativesStringResource, "NativesStringResource");
- sink_->PutSection(i, "NativesStringResourceEnd");
- bytes_processed_so_far_ += sizeof(resource);
- return;
- }
- }
- }
- // One of the strings in the natives cache should match the resource. We
- // don't expect any other kinds of external strings here.
- UNREACHABLE();
-}
-
-
-Address Serializer::ObjectSerializer::PrepareCode() {
- // To make snapshots reproducible, we make a copy of the code object
- // and wipe all pointers in the copy, which we then serialize.
- Code* original = Code::cast(object_);
- Code* code = serializer_->CopyCode(original);
- // Code age headers are not serializable.
- code->MakeYoung(serializer_->isolate());
- int mode_mask = RelocInfo::kCodeTargetMask |
- RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
- RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE) |
- RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) |
- RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) |
- RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE_ENCODED);
- for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
- RelocInfo* rinfo = it.rinfo();
- if (!(FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool())) {
- rinfo->WipeOut();
- }
- }
- // We need to wipe out the header fields *after* wiping out the
- // relocations, because some of these fields are needed for the latter.
- code->WipeOutHeader();
- return code->address();
-}
-
-
-int Serializer::ObjectSerializer::OutputRawData(
- Address up_to, Serializer::ObjectSerializer::ReturnSkip return_skip) {
- Address object_start = object_->address();
- int base = bytes_processed_so_far_;
- int up_to_offset = static_cast<int>(up_to - object_start);
- int to_skip = up_to_offset - bytes_processed_so_far_;
- int bytes_to_output = to_skip;
- bytes_processed_so_far_ += to_skip;
- // This assert will fail if the reloc info gives us the target_address_address
- // locations in a non-ascending order. Luckily that doesn't happen.
- DCHECK(to_skip >= 0);
- bool outputting_code = false;
- if (to_skip != 0 && is_code_object_ && !code_has_been_output_) {
- // Output the code all at once and fix later.
- bytes_to_output = object_->Size() + to_skip - bytes_processed_so_far_;
- outputting_code = true;
- code_has_been_output_ = true;
- }
- if (bytes_to_output != 0 && (!is_code_object_ || outputting_code)) {
- if (!outputting_code && bytes_to_output == to_skip &&
- IsAligned(bytes_to_output, kPointerAlignment) &&
- bytes_to_output <= kNumberOfFixedRawData * kPointerSize) {
- int size_in_words = bytes_to_output >> kPointerSizeLog2;
- sink_->PutSection(kFixedRawDataStart + size_in_words, "FixedRawData");
- to_skip = 0; // This instruction includes skip.
- } else {
- // We always end up here if we are outputting the code of a code object.
- sink_->Put(kVariableRawData, "VariableRawData");
- sink_->PutInt(bytes_to_output, "length");
- }
-
- if (is_code_object_) object_start = PrepareCode();
-
- const char* description = is_code_object_ ? "Code" : "Byte";
-#ifdef MEMORY_SANITIZER
- // Object sizes are usually rounded up with uninitialized padding space.
- MSAN_MEMORY_IS_INITIALIZED(object_start + base, bytes_to_output);
-#endif // MEMORY_SANITIZER
- sink_->PutRaw(object_start + base, bytes_to_output, description);
- }
- if (to_skip != 0 && return_skip == kIgnoringReturn) {
- sink_->Put(kSkip, "Skip");
- sink_->PutInt(to_skip, "SkipDistance");
- to_skip = 0;
- }
- return to_skip;
-}
-
-
-BackReference Serializer::AllocateLargeObject(int size) {
- // Large objects are allocated one-by-one when deserializing. We do not
- // have to keep track of multiple chunks.
- large_objects_total_size_ += size;
- return BackReference::LargeObjectReference(seen_large_objects_index_++);
-}
-
-
-BackReference Serializer::Allocate(AllocationSpace space, int size) {
- DCHECK(space >= 0 && space < kNumberOfPreallocatedSpaces);
- DCHECK(size > 0 && size <= static_cast<int>(max_chunk_size(space)));
- uint32_t new_chunk_size = pending_chunk_[space] + size;
- if (new_chunk_size > max_chunk_size(space)) {
- // The new chunk size would not fit onto a single page. Complete the
- // current chunk and start a new one.
- sink_->Put(kNextChunk, "NextChunk");
- sink_->Put(space, "NextChunkSpace");
- completed_chunks_[space].Add(pending_chunk_[space]);
- DCHECK_LE(completed_chunks_[space].length(), BackReference::kMaxChunkIndex);
- pending_chunk_[space] = 0;
- new_chunk_size = size;
- }
- uint32_t offset = pending_chunk_[space];
- pending_chunk_[space] = new_chunk_size;
- return BackReference::Reference(space, completed_chunks_[space].length(),
- offset);
-}
-
-
-void Serializer::Pad() {
- // The non-branching GetInt will read up to 3 bytes too far, so we need
- // to pad the snapshot to make sure we don't read over the end.
- for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) {
- sink_->Put(kNop, "Padding");
- }
- // Pad up to pointer size for checksum.
- while (!IsAligned(sink_->Position(), kPointerAlignment)) {
- sink_->Put(kNop, "Padding");
- }
-}
-
-
-void Serializer::InitializeCodeAddressMap() {
- isolate_->InitializeLoggingAndCounters();
- code_address_map_ = new CodeAddressMap(isolate_);
-}
-
-
-Code* Serializer::CopyCode(Code* code) {
- code_buffer_.Rewind(0); // Clear buffer without deleting backing store.
- int size = code->CodeSize();
- code_buffer_.AddAll(Vector<byte>(code->address(), size));
- return Code::cast(HeapObject::FromAddress(&code_buffer_.first()));
-}
-
-
-ScriptData* CodeSerializer::Serialize(Isolate* isolate,
- Handle<SharedFunctionInfo> info,
- Handle<String> 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, info->code());
- DisallowHeapAllocation no_gc;
- Object** location = Handle<Object>::cast(info).location();
- cs.VisitPointer(location);
- 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());
- // Only serialize the code for the toplevel function unless specified
- // by flag. Replace code of inner functions by the lazy compile builtin.
- // This is safe, as checked in Compiler::BuildFunctionInfo.
- if (code_object != main_code_ && !FLAG_serialize_inner) {
- SerializeBuiltin(Builtins::kCompileLazy, how_to_code, where_to_point);
- } else {
- SerializeGeneric(code_object, how_to_code, where_to_point);
- }
- return;
- }
- 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->IsGlobalObject());
- // 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) {
- if (heap_object->IsInternalizedString()) num_internalized_strings_++;
-
- // 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), false),
- 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<Builtins::Name>(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<SharedFunctionInfo> CodeSerializer::Deserialize(
- Isolate* isolate, ScriptData* cached_data, Handle<String> source) {
- base::ElapsedTimer timer;
- if (FLAG_profile_deserialization) timer.Start();
-
- HandleScope scope(isolate);
-
- SmartPointer<SerializedCodeData> 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<SharedFunctionInfo>();
- }
-
- // Eagerly expand string table to avoid allocations during deserialization.
- StringTable::EnsureCapacityForDeserialization(isolate,
- scd->NumInternalizedStrings());
-
- // Prepare and register list of attached objects.
- Vector<const uint32_t> code_stub_keys = scd->CodeStubKeys();
- Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::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<SharedFunctionInfo> 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<SharedFunctionInfo>();
- }
- deserializer.FlushICacheForNewCodeObjects();
-
- 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->code(),
- *result, NULL, name);
- }
- return scope.CloseAndEscape(result);
-}
-
-
-void SerializedData::AllocateData(int size) {
- DCHECK(!owns_data_);
- data_ = NewArray<byte>(size);
- size_ = size;
- owns_data_ = true;
- DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment));
-}
-
-
-SnapshotData::SnapshotData(const Serializer& ser) {
- DisallowHeapAllocation no_gc;
- List<Reservation> reservations;
- ser.EncodeReservations(&reservations);
- const List<byte>& payload = ser.sink()->data();
-
- // Calculate sizes.
- int reservation_size = reservations.length() * kInt32Size;
- int size = kHeaderSize + reservation_size + payload.length();
-
- // Allocate backing store and create result data.
- AllocateData(size);
-
- // Set header values.
- SetMagicNumber(ser.isolate());
- SetHeaderValue(kCheckSumOffset, Version::Hash());
- SetHeaderValue(kNumReservationsOffset, reservations.length());
- SetHeaderValue(kPayloadLengthOffset, payload.length());
-
- // Copy reservation chunk sizes.
- CopyBytes(data_ + kHeaderSize, reinterpret_cast<byte*>(reservations.begin()),
- reservation_size);
-
- // Copy serialized data.
- CopyBytes(data_ + kHeaderSize + reservation_size, payload.begin(),
- static_cast<size_t>(payload.length()));
-}
-
-
-bool SnapshotData::IsSane() {
- return GetHeaderValue(kCheckSumOffset) == Version::Hash();
-}
-
-
-Vector<const SerializedData::Reservation> SnapshotData::Reservations() const {
- return Vector<const Reservation>(
- reinterpret_cast<const Reservation*>(data_ + kHeaderSize),
- GetHeaderValue(kNumReservationsOffset));
-}
-
-
-Vector<const byte> SnapshotData::Payload() const {
- int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
- const byte* payload = data_ + kHeaderSize + reservations_size;
- int length = GetHeaderValue(kPayloadLengthOffset);
- DCHECK_EQ(data_ + size_, payload + length);
- return Vector<const byte>(payload, length);
-}
-
-
-class Checksum {
- public:
- explicit Checksum(Vector<const byte> payload) {
- // 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<const uintptr_t*>(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<uint32_t>(a);
- b_ = static_cast<uint32_t>(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<byte>& payload,
- const CodeSerializer& cs) {
- DisallowHeapAllocation no_gc;
- const List<uint32_t>* stub_keys = cs.stub_keys();
-
- List<Reservation> 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<uint32_t>(CpuFeatures::SupportedFeatures()));
- SetHeaderValue(kFlagHashOffset, FlagList::Hash());
- SetHeaderValue(kNumInternalizedStringsOffset, cs.num_internalized_strings());
- 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<byte*>(reservations.begin()),
- reservation_size);
-
- // Copy code stub keys.
- CopyBytes(data_ + kHeaderSize + reservation_size,
- reinterpret_cast<byte*>(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<size_t>(payload.length()));
-}
-
-
-SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck(
- Isolate* isolate, String* source) const {
- uint32_t magic_number = GetMagicNumber();
- 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 (magic_number != ComputeMagicNumber(isolate)) return MAGIC_NUMBER_MISMATCH;
- if (version_hash != Version::Hash()) return VERSION_MISMATCH;
- if (source_hash != SourceHash(source)) return SOURCE_MISMATCH;
- if (cpu_features != static_cast<uint32_t>(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;
-}
-
-
-// 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<const SerializedData::Reservation> SerializedCodeData::Reservations()
- const {
- return Vector<const Reservation>(
- reinterpret_cast<const Reservation*>(data_ + kHeaderSize),
- GetHeaderValue(kNumReservationsOffset));
-}
-
-
-Vector<const byte> 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<intptr_t>(payload), kPointerAlignment));
- int length = GetHeaderValue(kPayloadLengthOffset);
- DCHECK_EQ(data_ + size_, payload + length);
- return Vector<const byte>(payload, length);
-}
-
-
-int SerializedCodeData::NumInternalizedStrings() const {
- return GetHeaderValue(kNumInternalizedStringsOffset);
-}
-
-Vector<const uint32_t> SerializedCodeData::CodeStubKeys() const {
- int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
- const byte* start = data_ + kHeaderSize + reservations_size;
- return Vector<const uint32_t>(reinterpret_cast<const uint32_t*>(start),
- GetHeaderValue(kNumCodeStubKeysOffset));
-}
-
-
-SerializedCodeData::SerializedCodeData(ScriptData* data)
- : SerializedData(const_cast<byte*>(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 v8::internal
+++ /dev/null
-// Copyright 2012 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_SERIALIZE_H_
-#define V8_SERIALIZE_H_
-
-#include "src/hashmap.h"
-#include "src/heap-profiler.h"
-#include "src/isolate.h"
-#include "src/snapshot-source-sink.h"
-
-namespace v8 {
-namespace internal {
-
-class ScriptData;
-
-static const int kDeoptTableSerializeEntryCount = 64;
-
-// ExternalReferenceTable is a helper class that defines the relationship
-// between external references and their encodings. It is used to build
-// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
-class ExternalReferenceTable {
- public:
- static ExternalReferenceTable* instance(Isolate* isolate);
-
- int size() const { return refs_.length(); }
- Address address(int i) { return refs_[i].address; }
- const char* name(int i) { return refs_[i].name; }
-
- inline static Address NotAvailable() { return NULL; }
-
- private:
- struct ExternalReferenceEntry {
- Address address;
- const char* name;
- };
-
- explicit ExternalReferenceTable(Isolate* isolate);
-
- void Add(Address address, const char* name) {
- ExternalReferenceEntry entry = {address, name};
- refs_.Add(entry);
- }
-
- List<ExternalReferenceEntry> refs_;
-
- DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable);
-};
-
-
-class ExternalReferenceEncoder {
- public:
- explicit ExternalReferenceEncoder(Isolate* isolate);
-
- uint32_t Encode(Address key) const;
-
- const char* NameOfAddress(Isolate* isolate, Address address) const;
-
- private:
- static uint32_t Hash(Address key) {
- return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >>
- kPointerSizeLog2);
- }
-
- HashMap* map_;
-
- DISALLOW_COPY_AND_ASSIGN(ExternalReferenceEncoder);
-};
-
-
-class AddressMapBase {
- protected:
- static void SetValue(HashMap::Entry* entry, uint32_t v) {
- entry->value = reinterpret_cast<void*>(v);
- }
-
- static uint32_t GetValue(HashMap::Entry* entry) {
- return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value));
- }
-
- inline static HashMap::Entry* LookupEntry(HashMap* map, HeapObject* obj,
- bool insert) {
- return map->Lookup(Key(obj), Hash(obj), insert);
- }
-
- private:
- static uint32_t Hash(HeapObject* obj) {
- return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address()));
- }
-
- static void* Key(HeapObject* obj) {
- return reinterpret_cast<void*>(obj->address());
- }
-};
-
-
-class RootIndexMap : public AddressMapBase {
- public:
- explicit RootIndexMap(Isolate* isolate);
-
- static const int kInvalidRootIndex = -1;
-
- int Lookup(HeapObject* obj) {
- HashMap::Entry* entry = LookupEntry(map_, obj, false);
- if (entry) return GetValue(entry);
- return kInvalidRootIndex;
- }
-
- private:
- HashMap* map_;
-
- DISALLOW_COPY_AND_ASSIGN(RootIndexMap);
-};
-
-
-class PartialCacheIndexMap : public AddressMapBase {
- public:
- PartialCacheIndexMap() : map_(HashMap::PointersMatch) {}
-
- static const int kInvalidIndex = -1;
-
- // Lookup object in the map. Return its index if found, or create
- // a new entry with new_index as value, and return kInvalidIndex.
- int LookupOrInsert(HeapObject* obj, int new_index) {
- HashMap::Entry* entry = LookupEntry(&map_, obj, false);
- if (entry != NULL) return GetValue(entry);
- SetValue(LookupEntry(&map_, obj, true), static_cast<uint32_t>(new_index));
- return kInvalidIndex;
- }
-
- private:
- HashMap map_;
-
- DISALLOW_COPY_AND_ASSIGN(PartialCacheIndexMap);
-};
-
-
-class BackReference {
- public:
- explicit BackReference(uint32_t bitfield) : bitfield_(bitfield) {}
-
- BackReference() : bitfield_(kInvalidValue) {}
-
- static BackReference SourceReference() { return BackReference(kSourceValue); }
-
- static BackReference GlobalProxyReference() {
- return BackReference(kGlobalProxyValue);
- }
-
- static BackReference LargeObjectReference(uint32_t index) {
- return BackReference(SpaceBits::encode(LO_SPACE) |
- ChunkOffsetBits::encode(index));
- }
-
- static BackReference Reference(AllocationSpace space, uint32_t chunk_index,
- uint32_t chunk_offset) {
- DCHECK(IsAligned(chunk_offset, kObjectAlignment));
- DCHECK_NE(LO_SPACE, space);
- return BackReference(
- SpaceBits::encode(space) | ChunkIndexBits::encode(chunk_index) |
- ChunkOffsetBits::encode(chunk_offset >> kObjectAlignmentBits));
- }
-
- bool is_valid() const { return bitfield_ != kInvalidValue; }
- bool is_source() const { return bitfield_ == kSourceValue; }
- bool is_global_proxy() const { return bitfield_ == kGlobalProxyValue; }
-
- AllocationSpace space() const {
- DCHECK(is_valid());
- return SpaceBits::decode(bitfield_);
- }
-
- uint32_t chunk_offset() const {
- DCHECK(is_valid());
- return ChunkOffsetBits::decode(bitfield_) << kObjectAlignmentBits;
- }
-
- uint32_t large_object_index() const {
- DCHECK(is_valid());
- DCHECK(chunk_index() == 0);
- return ChunkOffsetBits::decode(bitfield_);
- }
-
- uint32_t chunk_index() const {
- DCHECK(is_valid());
- return ChunkIndexBits::decode(bitfield_);
- }
-
- uint32_t reference() const {
- DCHECK(is_valid());
- return bitfield_ & (ChunkOffsetBits::kMask | ChunkIndexBits::kMask);
- }
-
- uint32_t bitfield() const { return bitfield_; }
-
- private:
- static const uint32_t kInvalidValue = 0xFFFFFFFF;
- static const uint32_t kSourceValue = 0xFFFFFFFE;
- static const uint32_t kGlobalProxyValue = 0xFFFFFFFD;
- static const int kChunkOffsetSize = kPageSizeBits - kObjectAlignmentBits;
- static const int kChunkIndexSize = 32 - kChunkOffsetSize - kSpaceTagSize;
-
- public:
- static const int kMaxChunkIndex = (1 << kChunkIndexSize) - 1;
-
- private:
- class ChunkOffsetBits : public BitField<uint32_t, 0, kChunkOffsetSize> {};
- class ChunkIndexBits
- : public BitField<uint32_t, ChunkOffsetBits::kNext, kChunkIndexSize> {};
- class SpaceBits
- : public BitField<AllocationSpace, ChunkIndexBits::kNext, kSpaceTagSize> {
- };
-
- uint32_t bitfield_;
-};
-
-
-// Mapping objects to their location after deserialization.
-// This is used during building, but not at runtime by V8.
-class BackReferenceMap : public AddressMapBase {
- public:
- BackReferenceMap()
- : no_allocation_(), map_(new HashMap(HashMap::PointersMatch)) {}
-
- ~BackReferenceMap() { delete map_; }
-
- BackReference Lookup(HeapObject* obj) {
- HashMap::Entry* entry = LookupEntry(map_, obj, false);
- return entry ? BackReference(GetValue(entry)) : BackReference();
- }
-
- void Add(HeapObject* obj, BackReference b) {
- DCHECK(b.is_valid());
- DCHECK_NULL(LookupEntry(map_, obj, false));
- HashMap::Entry* entry = LookupEntry(map_, obj, true);
- SetValue(entry, b.bitfield());
- }
-
- void AddSourceString(String* string) {
- Add(string, BackReference::SourceReference());
- }
-
- void AddGlobalProxy(HeapObject* global_proxy) {
- Add(global_proxy, BackReference::GlobalProxyReference());
- }
-
- private:
- DisallowHeapAllocation no_allocation_;
- HashMap* map_;
- DISALLOW_COPY_AND_ASSIGN(BackReferenceMap);
-};
-
-
-class HotObjectsList {
- public:
- HotObjectsList() : index_(0) {
- for (int i = 0; i < kSize; i++) circular_queue_[i] = NULL;
- }
-
- void Add(HeapObject* object) {
- circular_queue_[index_] = object;
- index_ = (index_ + 1) & kSizeMask;
- }
-
- HeapObject* Get(int index) {
- DCHECK_NOT_NULL(circular_queue_[index]);
- return circular_queue_[index];
- }
-
- static const int kNotFound = -1;
-
- int Find(HeapObject* object) {
- for (int i = 0; i < kSize; i++) {
- if (circular_queue_[i] == object) return i;
- }
- return kNotFound;
- }
-
- static const int kSize = 8;
-
- private:
- STATIC_ASSERT(IS_POWER_OF_TWO(kSize));
- static const int kSizeMask = kSize - 1;
- HeapObject* circular_queue_[kSize];
- int index_;
-
- DISALLOW_COPY_AND_ASSIGN(HotObjectsList);
-};
-
-
-// The Serializer/Deserializer class is a common superclass for Serializer and
-// Deserializer which is used to store common constants and methods used by
-// both.
-class SerializerDeserializer: public ObjectVisitor {
- public:
- static void Iterate(Isolate* isolate, ObjectVisitor* visitor);
-
- static int nop() { return kNop; }
-
- // No reservation for large object space necessary.
- static const int kNumberOfPreallocatedSpaces = LO_SPACE;
- static const int kNumberOfSpaces = LAST_SPACE + 1;
-
- protected:
- // ---------- byte code range 0x00..0x7f ----------
- // Byte codes in this range represent Where, HowToCode and WhereToPoint.
- // Where the pointed-to object can be found:
- enum Where {
- // 0x00..0x05 Allocate new object, in specified space.
- kNewObject = 0,
- // 0x06 Unused (including 0x26, 0x46, 0x66).
- // 0x07 Unused (including 0x27, 0x47, 0x67).
- // 0x08..0x0d Reference to previous object from space.
- kBackref = 0x08,
- // 0x0e Unused (including 0x2e, 0x4e, 0x6e).
- // 0x0f Unused (including 0x2f, 0x4f, 0x6f).
- // 0x10..0x15 Reference to previous object from space after skip.
- kBackrefWithSkip = 0x10,
- // 0x16 Unused (including 0x36, 0x56, 0x76).
- // 0x17 Unused (including 0x37, 0x57, 0x77).
- // 0x18 Root array item.
- kRootArray = 0x18,
- // 0x19 Object in the partial snapshot cache.
- kPartialSnapshotCache = 0x19,
- // 0x1a External reference referenced by id.
- kExternalReference = 0x1a,
- // 0x1b Object provided in the attached list.
- kAttachedReference = 0x1b,
- // 0x1c Builtin code referenced by index.
- kBuiltin = 0x1c
- // 0x1d..0x1f Misc (including 0x3d..0x3f, 0x5d..0x5f, 0x7d..0x7f)
- };
-
- static const int kWhereMask = 0x1f;
- static const int kSpaceMask = 7;
- STATIC_ASSERT(kNumberOfSpaces <= kSpaceMask + 1);
-
- // How to code the pointer to the object.
- enum HowToCode {
- // Straight pointer.
- kPlain = 0,
- // A pointer inlined in code. What this means depends on the architecture.
- kFromCode = 0x20
- };
-
- static const int kHowToCodeMask = 0x20;
-
- // Where to point within the object.
- enum WhereToPoint {
- // Points to start of object
- kStartOfObject = 0,
- // Points to instruction in code object or payload of cell.
- kInnerPointer = 0x40
- };
-
- static const int kWhereToPointMask = 0x40;
-
- // ---------- Misc ----------
- // Skip.
- static const int kSkip = 0x1d;
- // Internal reference encoded as offsets of pc and target from code entry.
- static const int kInternalReference = 0x1e;
- static const int kInternalReferenceEncoded = 0x1f;
- // Do nothing, used for padding.
- static const int kNop = 0x3d;
- // Move to next reserved chunk.
- static const int kNextChunk = 0x3e;
- // A tag emitted at strategic points in the snapshot to delineate sections.
- // If the deserializer does not find these at the expected moments then it
- // is an indication that the snapshot and the VM do not fit together.
- // Examine the build process for architecture, version or configuration
- // mismatches.
- static const int kSynchronize = 0x5d;
- // Used for the source code of the natives, which is in the executable, but
- // is referred to from external strings in the snapshot.
- static const int kNativesStringResource = 0x5e;
- // Raw data of variable length.
- static const int kVariableRawData = 0x7d;
- // Repeats of variable length.
- static const int kVariableRepeat = 0x7e;
-
- // ---------- byte code range 0x80..0xff ----------
- // First 32 root array items.
- static const int kNumberOfRootArrayConstants = 0x20;
- // 0x80..0x9f
- static const int kRootArrayConstants = 0x80;
- // 0xa0..0xbf
- static const int kRootArrayConstantsWithSkip = 0xa0;
- static const int kRootArrayConstantsMask = 0x1f;
-
- // 8 hot (recently seen or back-referenced) objects with optional skip.
- static const int kNumberOfHotObjects = 0x08;
- // 0xc0..0xc7
- static const int kHotObject = 0xc0;
- // 0xc8..0xcf
- static const int kHotObjectWithSkip = 0xc8;
- static const int kHotObjectMask = 0x07;
-
- // 32 common raw data lengths.
- static const int kNumberOfFixedRawData = 0x20;
- // 0xd0..0xef
- static const int kFixedRawData = 0xd0;
- static const int kOnePointerRawData = kFixedRawData;
- static const int kFixedRawDataStart = kFixedRawData - 1;
-
- // 16 repeats lengths.
- static const int kNumberOfFixedRepeat = 0x10;
- // 0xf0..0xff
- static const int kFixedRepeat = 0xf0;
- static const int kFixedRepeatStart = kFixedRepeat - 1;
-
- // ---------- special values ----------
- static const int kAnyOldSpace = -1;
-
- // Sentinel after a new object to indicate that double alignment is needed.
- static const int kDoubleAlignmentSentinel = 0;
-
- // Used as index for the attached reference representing the source object.
- static const int kSourceObjectReference = 0;
-
- // Used as index for the attached reference representing the global proxy.
- static const int kGlobalProxyReference = 0;
-
- // ---------- member variable ----------
- HotObjectsList hot_objects_;
-};
-
-
-class SerializedData {
- public:
- class Reservation {
- public:
- explicit Reservation(uint32_t size)
- : reservation_(ChunkSizeBits::encode(size)) {}
-
- uint32_t chunk_size() const { return ChunkSizeBits::decode(reservation_); }
- bool is_last() const { return IsLastChunkBits::decode(reservation_); }
-
- void mark_as_last() { reservation_ |= IsLastChunkBits::encode(true); }
-
- private:
- uint32_t reservation_;
- };
-
- SerializedData(byte* data, int size)
- : data_(data), size_(size), owns_data_(false) {}
- SerializedData() : data_(NULL), size_(0), owns_data_(false) {}
-
- ~SerializedData() {
- if (owns_data_) DeleteArray<byte>(data_);
- }
-
- uint32_t GetMagicNumber() const { return GetHeaderValue(kMagicNumberOffset); }
-
- class ChunkSizeBits : public BitField<uint32_t, 0, 31> {};
- class IsLastChunkBits : public BitField<bool, 31, 1> {};
-
- static uint32_t ComputeMagicNumber(ExternalReferenceTable* table) {
- uint32_t external_refs = table->size();
- return 0xC0DE0000 ^ external_refs;
- }
-
- protected:
- void SetHeaderValue(int offset, uint32_t value) {
- uint32_t* address = reinterpret_cast<uint32_t*>(data_ + offset);
- memcpy(reinterpret_cast<uint32_t*>(address), &value, sizeof(value));
- }
-
- uint32_t GetHeaderValue(int offset) const {
- uint32_t value;
- memcpy(&value, reinterpret_cast<int*>(data_ + offset), sizeof(value));
- return value;
- }
-
- void AllocateData(int size);
-
- static uint32_t ComputeMagicNumber(Isolate* isolate) {
- return ComputeMagicNumber(ExternalReferenceTable::instance(isolate));
- }
-
- void SetMagicNumber(Isolate* isolate) {
- SetHeaderValue(kMagicNumberOffset, ComputeMagicNumber(isolate));
- }
-
- static const int kMagicNumberOffset = 0;
-
- byte* data_;
- int size_;
- bool owns_data_;
-};
-
-
-// A Deserializer reads a snapshot and reconstructs the Object graph it defines.
-class Deserializer: public SerializerDeserializer {
- public:
- // Create a deserializer from a snapshot byte source.
- template <class Data>
- explicit Deserializer(Data* data)
- : isolate_(NULL),
- source_(data->Payload()),
- magic_number_(data->GetMagicNumber()),
- external_reference_table_(NULL),
- deserialized_large_objects_(0),
- deserializing_user_code_(false) {
- DecodeReservation(data->Reservations());
- }
-
- virtual ~Deserializer();
-
- // Deserialize the snapshot into an empty heap.
- void Deserialize(Isolate* isolate);
-
- // Deserialize a single object and the objects reachable from it.
- MaybeHandle<Object> DeserializePartial(
- Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
- Handle<FixedArray>* outdated_contexts_out);
-
- // Deserialize a shared function info. Fail gracefully.
- MaybeHandle<SharedFunctionInfo> DeserializeCode(Isolate* isolate);
-
- void FlushICacheForNewCodeObjects();
-
- // Pass a vector of externally-provided objects referenced by the snapshot.
- // The ownership to its backing store is handed over as well.
- void SetAttachedObjects(Vector<Handle<Object> > attached_objects) {
- attached_objects_ = attached_objects;
- }
-
- private:
- virtual void VisitPointers(Object** start, Object** end);
-
- virtual void VisitRuntimeEntry(RelocInfo* rinfo) {
- UNREACHABLE();
- }
-
- void Initialize(Isolate* isolate);
-
- bool deserializing_user_code() { return deserializing_user_code_; }
-
- void DecodeReservation(Vector<const SerializedData::Reservation> res);
-
- bool ReserveSpace();
-
- void UnalignedCopy(Object** dest, Object** src) {
- memcpy(dest, src, sizeof(*src));
- }
-
- // Allocation sites are present in the snapshot, and must be linked into
- // a list at deserialization time.
- void RelinkAllocationSite(AllocationSite* site);
-
- // Fills in some heap data in an area from start to end (non-inclusive). The
- // space id is used for the write barrier. The object_address is the address
- // of the object we are writing into, or NULL if we are not writing into an
- // object, i.e. if we are writing a series of tagged values that are not on
- // the heap.
- void ReadData(Object** start, Object** end, int space,
- Address object_address);
- void ReadObject(int space_number, Object** write_back);
- Address Allocate(int space_index, int size);
-
- // Special handling for serialized code like hooking up internalized strings.
- HeapObject* ProcessNewObjectFromSerializedCode(HeapObject* obj);
-
- // This returns the address of an object that has been described in the
- // snapshot by chunk index and offset.
- HeapObject* GetBackReferencedObject(int space);
-
- // Cached current isolate.
- Isolate* isolate_;
-
- // Objects from the attached object descriptions in the serialized user code.
- Vector<Handle<Object> > attached_objects_;
-
- SnapshotByteSource source_;
- uint32_t magic_number_;
-
- // The address of the next object that will be allocated in each space.
- // Each space has a number of chunks reserved by the GC, with each chunk
- // fitting into a page. Deserialized objects are allocated into the
- // current chunk of the target space by bumping up high water mark.
- Heap::Reservation reservations_[kNumberOfSpaces];
- uint32_t current_chunk_[kNumberOfPreallocatedSpaces];
- Address high_water_[kNumberOfPreallocatedSpaces];
-
- ExternalReferenceTable* external_reference_table_;
-
- List<HeapObject*> deserialized_large_objects_;
-
- bool deserializing_user_code_;
-
- DISALLOW_COPY_AND_ASSIGN(Deserializer);
-};
-
-
-class CodeAddressMap;
-
-// There can be only one serializer per V8 process.
-class Serializer : public SerializerDeserializer {
- public:
- Serializer(Isolate* isolate, SnapshotByteSink* sink);
- ~Serializer();
- void VisitPointers(Object** start, Object** end) OVERRIDE;
-
- void EncodeReservations(List<SerializedData::Reservation>* out) const;
-
- Isolate* isolate() const { return isolate_; }
-
- BackReferenceMap* back_reference_map() { return &back_reference_map_; }
- RootIndexMap* root_index_map() { return &root_index_map_; }
-
- protected:
- class ObjectSerializer : public ObjectVisitor {
- public:
- ObjectSerializer(Serializer* serializer, Object* o, SnapshotByteSink* sink,
- HowToCode how_to_code, WhereToPoint where_to_point)
- : serializer_(serializer),
- object_(HeapObject::cast(o)),
- sink_(sink),
- reference_representation_(how_to_code + where_to_point),
- bytes_processed_so_far_(0),
- is_code_object_(o->IsCode()),
- code_has_been_output_(false) {}
- void Serialize();
- void VisitPointers(Object** start, Object** end);
- void VisitEmbeddedPointer(RelocInfo* target);
- void VisitExternalReference(Address* p);
- void VisitExternalReference(RelocInfo* rinfo);
- void VisitInternalReference(RelocInfo* rinfo);
- void VisitCodeTarget(RelocInfo* target);
- void VisitCodeEntry(Address entry_address);
- void VisitCell(RelocInfo* rinfo);
- void VisitRuntimeEntry(RelocInfo* reloc);
- // Used for seralizing the external strings that hold the natives source.
- void VisitExternalOneByteString(
- v8::String::ExternalOneByteStringResource** resource);
- // We can't serialize a heap with external two byte strings.
- void VisitExternalTwoByteString(
- v8::String::ExternalStringResource** resource) {
- UNREACHABLE();
- }
-
- private:
- void SerializePrologue(AllocationSpace space, int size, Map* map);
-
- enum ReturnSkip { kCanReturnSkipInsteadOfSkipping, kIgnoringReturn };
- // This function outputs or skips the raw data between the last pointer and
- // up to the current position. It optionally can just return the number of
- // bytes to skip instead of performing a skip instruction, in case the skip
- // can be merged into the next instruction.
- int OutputRawData(Address up_to, ReturnSkip return_skip = kIgnoringReturn);
- // External strings are serialized in a way to resemble sequential strings.
- void SerializeExternalString();
-
- Address PrepareCode();
-
- Serializer* serializer_;
- HeapObject* object_;
- SnapshotByteSink* sink_;
- int reference_representation_;
- int bytes_processed_so_far_;
- bool is_code_object_;
- bool code_has_been_output_;
- };
-
- virtual void SerializeObject(HeapObject* o, HowToCode how_to_code,
- WhereToPoint where_to_point, int skip) = 0;
-
- void PutRoot(int index, HeapObject* object, HowToCode how, WhereToPoint where,
- int skip);
-
- // Returns true if the object was successfully serialized.
- bool SerializeKnownObject(HeapObject* obj, HowToCode how_to_code,
- WhereToPoint where_to_point, int skip);
-
- inline void FlushSkip(int skip) {
- if (skip != 0) {
- sink_->Put(kSkip, "SkipFromSerializeObject");
- sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
- }
- }
-
- bool BackReferenceIsAlreadyAllocated(BackReference back_reference);
-
- // This will return the space for an object.
- BackReference AllocateLargeObject(int size);
- BackReference Allocate(AllocationSpace space, int size);
- int EncodeExternalReference(Address addr) {
- return external_reference_encoder_.Encode(addr);
- }
-
- // GetInt reads 4 bytes at once, requiring padding at the end.
- void Pad();
-
- // Some roots should not be serialized, because their actual value depends on
- // absolute addresses and they are reset after deserialization, anyway.
- bool ShouldBeSkipped(Object** current);
-
- // We may not need the code address map for logging for every instance
- // of the serializer. Initialize it on demand.
- void InitializeCodeAddressMap();
-
- Code* CopyCode(Code* code);
-
- inline uint32_t max_chunk_size(int space) const {
- DCHECK_LE(0, space);
- DCHECK_LT(space, kNumberOfSpaces);
- return max_chunk_size_[space];
- }
-
- SnapshotByteSink* sink() const { return sink_; }
-
- Isolate* isolate_;
-
- SnapshotByteSink* sink_;
- ExternalReferenceEncoder external_reference_encoder_;
-
- BackReferenceMap back_reference_map_;
- RootIndexMap root_index_map_;
-
- friend class Deserializer;
- friend class ObjectSerializer;
- friend class SnapshotData;
-
- private:
- CodeAddressMap* code_address_map_;
- // Objects from the same space are put into chunks for bulk-allocation
- // when deserializing. We have to make sure that each chunk fits into a
- // page. So we track the chunk size in pending_chunk_ of a space, but
- // when it exceeds a page, we complete the current chunk and start a new one.
- uint32_t pending_chunk_[kNumberOfPreallocatedSpaces];
- List<uint32_t> completed_chunks_[kNumberOfPreallocatedSpaces];
- uint32_t max_chunk_size_[kNumberOfPreallocatedSpaces];
-
- // We map serialized large objects to indexes for back-referencing.
- uint32_t large_objects_total_size_;
- uint32_t seen_large_objects_index_;
-
- List<byte> code_buffer_;
-
- DISALLOW_COPY_AND_ASSIGN(Serializer);
-};
-
-
-class PartialSerializer : public Serializer {
- public:
- PartialSerializer(Isolate* isolate, Serializer* startup_snapshot_serializer,
- SnapshotByteSink* sink)
- : Serializer(isolate, sink),
- startup_serializer_(startup_snapshot_serializer),
- outdated_contexts_(0),
- global_object_(NULL) {
- InitializeCodeAddressMap();
- }
-
- // Serialize the objects reachable from a single object pointer.
- void Serialize(Object** o);
- virtual void SerializeObject(HeapObject* o, HowToCode how_to_code,
- WhereToPoint where_to_point, int skip) OVERRIDE;
-
- private:
- int PartialSnapshotCacheIndex(HeapObject* o);
- bool ShouldBeInThePartialSnapshotCache(HeapObject* o) {
- // Scripts should be referred only through shared function infos. We can't
- // allow them to be part of the partial snapshot because they contain a
- // unique ID, and deserializing several partial snapshots containing script
- // would cause dupes.
- DCHECK(!o->IsScript());
- return o->IsName() || o->IsSharedFunctionInfo() ||
- o->IsHeapNumber() || o->IsCode() ||
- o->IsScopeInfo() ||
- o->map() ==
- startup_serializer_->isolate()->heap()->fixed_cow_array_map();
- }
-
- void SerializeOutdatedContextsAsFixedArray();
-
- Serializer* startup_serializer_;
- List<BackReference> outdated_contexts_;
- Object* global_object_;
- PartialCacheIndexMap partial_cache_index_map_;
- DISALLOW_COPY_AND_ASSIGN(PartialSerializer);
-};
-
-
-class StartupSerializer : public Serializer {
- public:
- StartupSerializer(Isolate* isolate, SnapshotByteSink* sink)
- : Serializer(isolate, sink), root_index_wave_front_(0) {
- // Clear the cache of objects used by the partial snapshot. After the
- // strong roots have been serialized we can create a partial snapshot
- // which will repopulate the cache with objects needed by that partial
- // snapshot.
- isolate->partial_snapshot_cache()->Clear();
- InitializeCodeAddressMap();
- }
-
- // The StartupSerializer has to serialize the root array, which is slightly
- // different.
- void VisitPointers(Object** start, Object** end) OVERRIDE;
-
- // Serialize the current state of the heap. The order is:
- // 1) Strong references.
- // 2) Partial snapshot cache.
- // 3) Weak references (e.g. the string table).
- virtual void SerializeStrongReferences();
- virtual void SerializeObject(HeapObject* o, HowToCode how_to_code,
- WhereToPoint where_to_point, int skip) OVERRIDE;
- void SerializeWeakReferences();
- void Serialize() {
- SerializeStrongReferences();
- SerializeWeakReferences();
- Pad();
- }
-
- private:
- intptr_t root_index_wave_front_;
- DISALLOW_COPY_AND_ASSIGN(StartupSerializer);
-};
-
-
-class CodeSerializer : public Serializer {
- public:
- static ScriptData* Serialize(Isolate* isolate,
- Handle<SharedFunctionInfo> info,
- Handle<String> source);
-
- MUST_USE_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize(
- Isolate* isolate, ScriptData* cached_data, Handle<String> 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<uint32_t>* stub_keys() const { return &stub_keys_; }
- int num_internalized_strings() const { return num_internalized_strings_; }
-
- private:
- CodeSerializer(Isolate* isolate, SnapshotByteSink* sink, String* source,
- Code* main_code)
- : Serializer(isolate, sink),
- source_(source),
- main_code_(main_code),
- num_internalized_strings_(0) {
- back_reference_map_.AddSourceString(source);
- }
-
- virtual 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_;
- Code* main_code_;
- int num_internalized_strings_;
- List<uint32_t> stub_keys_;
- DISALLOW_COPY_AND_ASSIGN(CodeSerializer);
-};
-
-
-// Wrapper around reservation sizes and the serialization payload.
-class SnapshotData : public SerializedData {
- public:
- // Used when producing.
- explicit SnapshotData(const Serializer& ser);
-
- // Used when consuming.
- explicit SnapshotData(const Vector<const byte> snapshot)
- : SerializedData(const_cast<byte*>(snapshot.begin()), snapshot.length()) {
- CHECK(IsSane());
- }
-
- Vector<const Reservation> Reservations() const;
- Vector<const byte> Payload() const;
-
- Vector<const byte> RawData() const {
- return Vector<const byte>(data_, size_);
- }
-
- private:
- bool IsSane();
-
- // The data header consists of uint32_t-sized entries:
- // [0] magic number and external reference count
- // [1] version hash
- // [2] number of reservation size entries
- // [3] payload length
- // ... reservations
- // ... serialized payload
- static const int kCheckSumOffset = kMagicNumberOffset + kInt32Size;
- static const int kNumReservationsOffset = kCheckSumOffset + kInt32Size;
- static const int kPayloadLengthOffset = kNumReservationsOffset + kInt32Size;
- static const int kHeaderSize = kPayloadLengthOffset + kInt32Size;
-};
-
-
-// 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<byte>& payload, const CodeSerializer& cs);
-
- // Return ScriptData object and relinquish ownership over it to the caller.
- ScriptData* GetScriptData();
-
- Vector<const Reservation> Reservations() const;
- Vector<const byte> Payload() const;
-
- int NumInternalizedStrings() const;
- Vector<const uint32_t> 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 { return source->length(); }
-
- // 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 internalized strings
- // [ 6] number of code stub keys
- // [ 7] number of reservation size entries
- // [ 8] payload length
- // [ 9] payload checksum part 1
- // [10] 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 kNumInternalizedStringsOffset = kFlagHashOffset + kInt32Size;
- static const int kNumReservationsOffset =
- kNumInternalizedStringsOffset + 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 v8::internal
-
-#endif // V8_SERIALIZE_H_
+++ /dev/null
-// Copyright 2006-2008 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.
-
-// The common functionality when building with or without snapshots.
-
-#include "src/v8.h"
-
-#include "src/api.h"
-#include "src/base/platform/platform.h"
-#include "src/full-codegen.h"
-#include "src/snapshot.h"
-
-namespace v8 {
-namespace internal {
-
-#ifdef DEBUG
-bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) {
- return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() &&
- !Snapshot::ExtractContextData(snapshot_blob).is_empty();
-}
-#endif // DEBUG
-
-
-bool Snapshot::EmbedsScript(Isolate* isolate) {
- if (!isolate->snapshot_available()) return false;
- return ExtractMetadata(isolate->snapshot_blob()).embeds_script();
-}
-
-
-uint32_t Snapshot::SizeOfFirstPage(Isolate* isolate, AllocationSpace space) {
- DCHECK(space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE);
- if (!isolate->snapshot_available()) {
- return static_cast<uint32_t>(MemoryAllocator::PageAreaSize(space));
- }
- uint32_t size;
- int offset = kFirstPageSizesOffset + (space - FIRST_PAGED_SPACE) * kInt32Size;
- memcpy(&size, isolate->snapshot_blob()->data + offset, kInt32Size);
- return size;
-}
-
-
-bool Snapshot::Initialize(Isolate* isolate) {
- if (!isolate->snapshot_available()) return false;
- base::ElapsedTimer timer;
- if (FLAG_profile_deserialization) timer.Start();
-
- const v8::StartupData* blob = isolate->snapshot_blob();
- Vector<const byte> startup_data = ExtractStartupData(blob);
- SnapshotData snapshot_data(startup_data);
- Deserializer deserializer(&snapshot_data);
- bool success = isolate->Init(&deserializer);
- if (FLAG_profile_deserialization) {
- double ms = timer.Elapsed().InMillisecondsF();
- int bytes = startup_data.length();
- PrintF("[Deserializing isolate (%d bytes) took %0.3f ms]\n", bytes, ms);
- }
- return success;
-}
-
-
-MaybeHandle<Context> Snapshot::NewContextFromSnapshot(
- Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
- Handle<FixedArray>* outdated_contexts_out) {
- if (!isolate->snapshot_available()) return Handle<Context>();
- base::ElapsedTimer timer;
- if (FLAG_profile_deserialization) timer.Start();
-
- const v8::StartupData* blob = isolate->snapshot_blob();
- Vector<const byte> context_data = ExtractContextData(blob);
- SnapshotData snapshot_data(context_data);
- Deserializer deserializer(&snapshot_data);
-
- MaybeHandle<Object> maybe_context = deserializer.DeserializePartial(
- isolate, global_proxy, outdated_contexts_out);
- Handle<Object> result;
- if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>();
- CHECK(result->IsContext());
- // If the snapshot does not contain a custom script, we need to update
- // the global object for exactly one context.
- CHECK(EmbedsScript(isolate) || (*outdated_contexts_out)->length() == 1);
- if (FLAG_profile_deserialization) {
- double ms = timer.Elapsed().InMillisecondsF();
- int bytes = context_data.length();
- PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms);
- }
- return Handle<Context>::cast(result);
-}
-
-
-void CalculateFirstPageSizes(bool is_default_snapshot,
- const SnapshotData& startup_snapshot,
- const SnapshotData& context_snapshot,
- uint32_t* sizes_out) {
- Vector<const SerializedData::Reservation> startup_reservations =
- startup_snapshot.Reservations();
- Vector<const SerializedData::Reservation> context_reservations =
- context_snapshot.Reservations();
- int startup_index = 0;
- int context_index = 0;
-
- if (FLAG_profile_deserialization) {
- int startup_total = 0;
- int context_total = 0;
- for (auto& reservation : startup_reservations) {
- startup_total += reservation.chunk_size();
- }
- for (auto& reservation : context_reservations) {
- context_total += reservation.chunk_size();
- }
- PrintF(
- "Deserialization will reserve:\n"
- "%10d bytes for startup\n"
- "%10d bytes per context\n",
- startup_total, context_total);
- }
-
- for (int space = 0; space < i::Serializer::kNumberOfSpaces; space++) {
- bool single_chunk = true;
- while (!startup_reservations[startup_index].is_last()) {
- single_chunk = false;
- startup_index++;
- }
- while (!context_reservations[context_index].is_last()) {
- single_chunk = false;
- context_index++;
- }
-
- uint32_t required = kMaxUInt32;
- if (single_chunk) {
- // If both the startup snapshot data and the context snapshot data on
- // this space fit in a single page, then we consider limiting the size
- // of the first page. For this, we add the chunk sizes and some extra
- // allowance. This way we achieve a smaller startup memory footprint.
- required = (startup_reservations[startup_index].chunk_size() +
- 2 * context_reservations[context_index].chunk_size()) +
- Page::kObjectStartOffset;
- // Add a small allowance to the code space for small scripts.
- if (space == CODE_SPACE) required += 32 * KB;
- } else {
- // We expect the vanilla snapshot to only require on page per space.
- DCHECK(!is_default_snapshot);
- }
-
- if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) {
- uint32_t max_size =
- MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space));
- sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size);
- } else {
- DCHECK(single_chunk);
- }
- startup_index++;
- context_index++;
- }
-
- DCHECK_EQ(startup_reservations.length(), startup_index);
- DCHECK_EQ(context_reservations.length(), context_index);
-}
-
-
-v8::StartupData Snapshot::CreateSnapshotBlob(
- const i::StartupSerializer& startup_ser,
- const i::PartialSerializer& context_ser, Snapshot::Metadata metadata) {
- SnapshotData startup_snapshot(startup_ser);
- SnapshotData context_snapshot(context_ser);
- Vector<const byte> startup_data = startup_snapshot.RawData();
- Vector<const byte> context_data = context_snapshot.RawData();
-
- uint32_t first_page_sizes[kNumPagedSpaces];
-
- CalculateFirstPageSizes(!metadata.embeds_script(), startup_snapshot,
- context_snapshot, first_page_sizes);
-
- int startup_length = startup_data.length();
- int context_length = context_data.length();
- int context_offset = ContextOffset(startup_length);
-
- int length = context_offset + context_length;
- char* data = new char[length];
-
- memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size);
- memcpy(data + kFirstPageSizesOffset, first_page_sizes,
- kNumPagedSpaces * kInt32Size);
- memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size);
- memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length);
- memcpy(data + context_offset, context_data.begin(), context_length);
- v8::StartupData result = {data, length};
-
- if (FLAG_profile_deserialization) {
- PrintF(
- "Snapshot blob consists of:\n"
- "%10d bytes for startup\n"
- "%10d bytes for context\n",
- startup_length, context_length);
- }
- return result;
-}
-
-
-Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) {
- uint32_t raw;
- memcpy(&raw, data->data + kMetadataOffset, kInt32Size);
- return Metadata(raw);
-}
-
-
-Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) {
- DCHECK_LT(kIntSize, data->raw_size);
- int startup_length;
- memcpy(&startup_length, data->data + kStartupLengthOffset, kInt32Size);
- DCHECK_LT(startup_length, data->raw_size);
- const byte* startup_data =
- reinterpret_cast<const byte*>(data->data + kStartupDataOffset);
- return Vector<const byte>(startup_data, startup_length);
-}
-
-
-Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) {
- DCHECK_LT(kIntSize, data->raw_size);
- int startup_length;
- memcpy(&startup_length, data->data + kStartupLengthOffset, kIntSize);
- int context_offset = ContextOffset(startup_length);
- const byte* context_data =
- reinterpret_cast<const byte*>(data->data + context_offset);
- DCHECK_LT(context_offset, data->raw_size);
- int context_length = data->raw_size - context_offset;
- return Vector<const byte>(context_data, context_length);
-}
-} } // namespace v8::internal
+++ /dev/null
-// Copyright 2006-2008 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.
-
-// Used for building without snapshots.
-
-#include "src/v8.h"
-
-#include "src/snapshot.h"
-
-namespace v8 {
-namespace internal {
-
-#ifdef V8_USE_EXTERNAL_STARTUP_DATA
-// Dummy implementations of Set*FromFile(..) APIs.
-//
-// These are meant for use with snapshot-external.cc. Should this file
-// be compiled with those options we just supply these dummy implementations
-// below. This happens when compiling the mksnapshot utility.
-void SetNativesFromFile(StartupData* data) { CHECK(false); }
-void SetSnapshotFromFile(StartupData* data) { CHECK(false); }
-void ReadNatives() {}
-void DisposeNatives() {}
-#endif // V8_USE_EXTERNAL_STARTUP_DATA
-
-
-const v8::StartupData* Snapshot::DefaultSnapshotBlob() { return NULL; }
-} } // namespace v8::internal
+++ /dev/null
-// Copyright 2006-2008 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.
-
-// Used for building with external snapshots.
-
-#include "src/snapshot.h"
-
-#include "src/base/platform/mutex.h"
-#include "src/serialize.h"
-#include "src/snapshot-source-sink.h"
-#include "src/v8.h" // for V8::Initialize
-
-
-#ifndef V8_USE_EXTERNAL_STARTUP_DATA
-#error snapshot-external.cc is used only for the external snapshot build.
-#endif // V8_USE_EXTERNAL_STARTUP_DATA
-
-
-namespace v8 {
-namespace internal {
-
-static base::LazyMutex external_startup_data_mutex = LAZY_MUTEX_INITIALIZER;
-static v8::StartupData external_startup_blob = {NULL, 0};
-
-void SetSnapshotFromFile(StartupData* snapshot_blob) {
- base::LockGuard<base::Mutex> lock_guard(
- external_startup_data_mutex.Pointer());
- DCHECK(snapshot_blob);
- DCHECK(snapshot_blob->data);
- DCHECK(snapshot_blob->raw_size > 0);
- DCHECK(!external_startup_blob.data);
- DCHECK(Snapshot::SnapshotIsValid(snapshot_blob));
- external_startup_blob = *snapshot_blob;
-}
-
-
-const v8::StartupData* Snapshot::DefaultSnapshotBlob() {
- base::LockGuard<base::Mutex> lock_guard(
- external_startup_data_mutex.Pointer());
- return &external_startup_blob;
-}
-} } // namespace v8::internal
+++ /dev/null
-// Copyright 2014 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-source-sink.h"
-
-#include "src/base/logging.h"
-#include "src/handles-inl.h"
-#include "src/serialize.h" // for SerializerDeserializer::nop() in AtEOF()
-
-
-namespace v8 {
-namespace internal {
-
-void SnapshotByteSource::CopyRaw(byte* to, int number_of_bytes) {
- memcpy(to, data_ + position_, number_of_bytes);
- position_ += number_of_bytes;
-}
-
-
-void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
- DCHECK(integer < 1 << 30);
- integer <<= 2;
- int bytes = 1;
- if (integer > 0xff) bytes = 2;
- if (integer > 0xffff) bytes = 3;
- if (integer > 0xffffff) bytes = 4;
- integer |= (bytes - 1);
- Put(static_cast<int>(integer & 0xff), "IntPart1");
- if (bytes > 1) Put(static_cast<int>((integer >> 8) & 0xff), "IntPart2");
- if (bytes > 2) Put(static_cast<int>((integer >> 16) & 0xff), "IntPart3");
- if (bytes > 3) Put(static_cast<int>((integer >> 24) & 0xff), "IntPart4");
-}
-
-
-void SnapshotByteSink::PutRaw(const byte* data, int number_of_bytes,
- const char* description) {
- data_.AddAll(Vector<byte>(const_cast<byte*>(data), number_of_bytes));
-}
-
-
-bool SnapshotByteSource::AtEOF() {
- if (0u + length_ - position_ > 2 * sizeof(uint32_t)) return false;
- for (int x = position_; x < length_; x++) {
- if (data_[x] != SerializerDeserializer::nop()) return false;
- }
- return true;
-}
-
-
-bool SnapshotByteSource::GetBlob(const byte** data, int* number_of_bytes) {
- int size = GetInt();
- *number_of_bytes = size;
-
- if (position_ + size <= length_) {
- *data = &data_[position_];
- Advance(size);
- return true;
- } else {
- Advance(length_ - position_); // proceed until end.
- return false;
- }
-}
-
-} // namespace v8::internal
-} // namespace v8
+++ /dev/null
-// Copyright 2012 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_SOURCE_SINK_H_
-#define V8_SNAPSHOT_SOURCE_SINK_H_
-
-#include "src/base/logging.h"
-#include "src/utils.h"
-
-namespace v8 {
-namespace internal {
-
-
-/**
- * Source to read snapshot and builtins files from.
- *
- * Note: Memory ownership remains with callee.
- */
-class SnapshotByteSource FINAL {
- public:
- SnapshotByteSource(const char* data, int length)
- : data_(reinterpret_cast<const byte*>(data)),
- length_(length),
- position_(0) {}
-
- explicit SnapshotByteSource(Vector<const byte> payload)
- : data_(payload.start()), length_(payload.length()), position_(0) {}
-
- ~SnapshotByteSource() {}
-
- bool HasMore() { return position_ < length_; }
-
- byte Get() {
- DCHECK(position_ < length_);
- return data_[position_++];
- }
-
- void Advance(int by) { position_ += by; }
-
- void CopyRaw(byte* to, int number_of_bytes);
-
- inline int GetInt() {
- // This way of decoding variable-length encoded integers does not
- // suffer from branch mispredictions.
- DCHECK(position_ + 3 < length_);
- uint32_t answer = data_[position_];
- answer |= data_[position_ + 1] << 8;
- answer |= data_[position_ + 2] << 16;
- answer |= data_[position_ + 3] << 24;
- int bytes = (answer & 3) + 1;
- Advance(bytes);
- uint32_t mask = 0xffffffffu;
- mask >>= 32 - (bytes << 3);
- answer &= mask;
- answer >>= 2;
- return answer;
- }
-
- bool GetBlob(const byte** data, int* number_of_bytes);
-
- bool AtEOF();
-
- int position() { return position_; }
-
- private:
- const byte* data_;
- int length_;
- int position_;
-
- DISALLOW_COPY_AND_ASSIGN(SnapshotByteSource);
-};
-
-
-/**
- * Sink to write snapshot files to.
- *
- * Subclasses must implement actual storage or i/o.
- */
-class SnapshotByteSink {
- public:
- SnapshotByteSink() {}
- explicit SnapshotByteSink(int initial_size) : data_(initial_size) {}
-
- ~SnapshotByteSink() {}
-
- void Put(byte b, const char* description) { data_.Add(b); }
-
- void PutSection(int b, const char* description) {
- DCHECK_LE(b, kMaxUInt8);
- Put(static_cast<byte>(b), description);
- }
-
- void PutInt(uintptr_t integer, const char* description);
- void PutRaw(const byte* data, int number_of_bytes, const char* description);
- int Position() { return data_.length(); }
-
- const List<byte>& data() const { return data_; }
-
- private:
- List<byte> data_;
-};
-
-} // namespace v8::internal
-} // namespace v8
-
-#endif // V8_SNAPSHOT_SOURCE_SINK_H_
+++ /dev/null
-// Copyright 2006-2008 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/isolate.h"
-#include "src/serialize.h"
-
-#ifndef V8_SNAPSHOT_H_
-#define V8_SNAPSHOT_H_
-
-namespace v8 {
-namespace internal {
-
-class Snapshot : public AllStatic {
- public:
- class Metadata {
- public:
- explicit Metadata(uint32_t data = 0) : data_(data) {}
- bool embeds_script() { return EmbedsScriptBits::decode(data_); }
- void set_embeds_script(bool v) {
- data_ = EmbedsScriptBits::update(data_, v);
- }
-
- uint32_t& RawValue() { return data_; }
-
- private:
- class EmbedsScriptBits : public BitField<bool, 0, 1> {};
- uint32_t data_;
- };
-
- // Initialize the Isolate from the internal snapshot. Returns false if no
- // snapshot could be found.
- static bool Initialize(Isolate* isolate);
- // Create a new context using the internal partial snapshot.
- static MaybeHandle<Context> NewContextFromSnapshot(
- Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
- Handle<FixedArray>* outdated_contexts_out);
-
- static bool HaveASnapshotToStartFrom(Isolate* isolate) {
- // Do not use snapshots if the isolate is used to create snapshots.
- return isolate->snapshot_blob() != NULL;
- }
-
- static bool EmbedsScript(Isolate* isolate);
-
- static uint32_t SizeOfFirstPage(Isolate* isolate, AllocationSpace space);
-
-
- // To be implemented by the snapshot source.
- static const v8::StartupData* DefaultSnapshotBlob();
-
- static v8::StartupData CreateSnapshotBlob(
- const StartupSerializer& startup_ser,
- const PartialSerializer& context_ser, Snapshot::Metadata metadata);
-
-#ifdef DEBUG
- static bool SnapshotIsValid(v8::StartupData* snapshot_blob);
-#endif // DEBUG
-
- private:
- static Vector<const byte> ExtractStartupData(const v8::StartupData* data);
- static Vector<const byte> ExtractContextData(const v8::StartupData* data);
- static Metadata ExtractMetadata(const v8::StartupData* data);
-
- // Snapshot blob layout:
- // [0] metadata
- // [1 - 6] pre-calculated first page sizes for paged spaces
- // [7] serialized start up data length
- // ... serialized start up data
- // ... serialized context data
-
- static const int kNumPagedSpaces = LAST_PAGED_SPACE - FIRST_PAGED_SPACE + 1;
-
- static const int kMetadataOffset = 0;
- static const int kFirstPageSizesOffset = kMetadataOffset + kInt32Size;
- static const int kStartupLengthOffset =
- kFirstPageSizesOffset + kNumPagedSpaces * kInt32Size;
- static const int kStartupDataOffset = kStartupLengthOffset + kInt32Size;
-
- static int ContextOffset(int startup_length) {
- return kStartupDataOffset + startup_length;
- }
-
- DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot);
-};
-
-#ifdef V8_USE_EXTERNAL_STARTUP_DATA
-void SetSnapshotFromFile(StartupData* snapshot_blob);
-#endif
-
-} } // namespace v8::internal
-
-#endif // V8_SNAPSHOT_H_
--- /dev/null
+specific_include_rules = {
+ "mksnapshot\.cc": [
+ "+include/libplatform/libplatform.h",
+ ],
+}
--- /dev/null
+// Copyright 2006-2008 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 <errno.h>
+#include <signal.h>
+#include <stdio.h>
+
+#include "src/v8.h"
+
+#include "include/libplatform/libplatform.h"
+#include "src/assembler.h"
+#include "src/base/platform/platform.h"
+#include "src/bootstrapper.h"
+#include "src/flags.h"
+#include "src/list.h"
+#include "src/snapshot/natives.h"
+#include "src/snapshot/serialize.h"
+
+
+using namespace v8;
+
+class SnapshotWriter {
+ public:
+ explicit SnapshotWriter(const char* snapshot_file)
+ : fp_(GetFileDescriptorOrDie(snapshot_file)),
+ startup_blob_file_(NULL) {}
+
+ ~SnapshotWriter() {
+ fclose(fp_);
+ if (startup_blob_file_) fclose(startup_blob_file_);
+ }
+
+ void SetStartupBlobFile(const char* startup_blob_file) {
+ if (startup_blob_file != NULL)
+ startup_blob_file_ = GetFileDescriptorOrDie(startup_blob_file);
+ }
+
+ void WriteSnapshot(v8::StartupData blob) const {
+ i::Vector<const i::byte> blob_vector(
+ reinterpret_cast<const i::byte*>(blob.data), blob.raw_size);
+ WriteSnapshotFile(blob_vector);
+ MaybeWriteStartupBlob(blob_vector);
+ }
+
+ private:
+ void MaybeWriteStartupBlob(const i::Vector<const i::byte>& blob) const {
+ if (!startup_blob_file_) return;
+
+ size_t written = fwrite(blob.begin(), 1, blob.length(), startup_blob_file_);
+ if (written != static_cast<size_t>(blob.length())) {
+ i::PrintF("Writing snapshot file failed.. Aborting.\n");
+ exit(1);
+ }
+ }
+
+ void WriteSnapshotFile(const i::Vector<const i::byte>& blob) const {
+ WriteFilePrefix();
+ WriteData(blob);
+ WriteFileSuffix();
+ }
+
+ void WriteFilePrefix() const {
+ fprintf(fp_, "// Autogenerated snapshot file. Do not edit.\n\n");
+ fprintf(fp_, "#include \"src/v8.h\"\n");
+ fprintf(fp_, "#include \"src/base/platform/platform.h\"\n\n");
+ fprintf(fp_, "#include \"src/snapshot/snapshot.h\"\n\n");
+ fprintf(fp_, "namespace v8 {\n");
+ fprintf(fp_, "namespace internal {\n\n");
+ }
+
+ void WriteFileSuffix() const {
+ fprintf(fp_, "const v8::StartupData* Snapshot::DefaultSnapshotBlob() {\n");
+ fprintf(fp_, " return &blob;\n");
+ fprintf(fp_, "}\n\n");
+ fprintf(fp_, "} // namespace internal\n");
+ fprintf(fp_, "} // namespace v8\n");
+ }
+
+ void WriteData(const i::Vector<const i::byte>& blob) const {
+ fprintf(fp_, "static const byte blob_data[] = {\n");
+ WriteSnapshotData(blob);
+ fprintf(fp_, "};\n");
+ fprintf(fp_, "static const int blob_size = %d;\n", blob.length());
+ fprintf(fp_, "static const v8::StartupData blob =\n");
+ fprintf(fp_, "{ (const char*) blob_data, blob_size };\n");
+ }
+
+ void WriteSnapshotData(const i::Vector<const i::byte>& blob) const {
+ for (int i = 0; i < blob.length(); i++) {
+ if ((i & 0x1f) == 0x1f) fprintf(fp_, "\n");
+ if (i > 0) fprintf(fp_, ",");
+ fprintf(fp_, "%u", static_cast<unsigned char>(blob.at(i)));
+ }
+ fprintf(fp_, "\n");
+ }
+
+ FILE* GetFileDescriptorOrDie(const char* filename) {
+ FILE* fp = base::OS::FOpen(filename, "wb");
+ if (fp == NULL) {
+ i::PrintF("Unable to open file \"%s\" for writing.\n", filename);
+ exit(1);
+ }
+ return fp;
+ }
+
+ FILE* fp_;
+ FILE* startup_blob_file_;
+};
+
+
+char* GetExtraCode(char* filename) {
+ if (filename == NULL || strlen(filename) == 0) return NULL;
+ ::printf("Embedding extra script: %s\n", filename);
+ FILE* file = base::OS::FOpen(filename, "rb");
+ if (file == NULL) {
+ fprintf(stderr, "Failed to open '%s': errno %d\n", filename, errno);
+ exit(1);
+ }
+ fseek(file, 0, SEEK_END);
+ int size = ftell(file);
+ rewind(file);
+ char* chars = new char[size + 1];
+ chars[size] = '\0';
+ for (int i = 0; i < size;) {
+ int read = static_cast<int>(fread(&chars[i], 1, size - i, file));
+ if (read < 0) {
+ fprintf(stderr, "Failed to read '%s': errno %d\n", filename, errno);
+ exit(1);
+ }
+ i += read;
+ }
+ fclose(file);
+ return chars;
+}
+
+
+int main(int argc, char** argv) {
+ // By default, log code create information in the snapshot.
+ i::FLAG_log_code = true;
+ i::FLAG_logfile_per_isolate = false;
+
+ // Print the usage if an error occurs when parsing the command line
+ // flags or if the help flag is set.
+ int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
+ if (result > 0 || (argc != 2 && argc != 3) || i::FLAG_help) {
+ ::printf("Usage: %s [flag] ... outfile\n", argv[0]);
+ i::FlagList::PrintHelp();
+ return !i::FLAG_help;
+ }
+
+ i::CpuFeatures::Probe(true);
+ V8::InitializeICU();
+ v8::Platform* platform = v8::platform::CreateDefaultPlatform();
+ v8::V8::InitializePlatform(platform);
+ v8::V8::Initialize();
+
+ {
+ SnapshotWriter writer(argv[1]);
+ if (i::FLAG_startup_blob) writer.SetStartupBlobFile(i::FLAG_startup_blob);
+ char* extra_code = GetExtraCode(argc == 3 ? argv[2] : NULL);
+ StartupData blob = v8::V8::CreateSnapshotDataBlob(extra_code);
+ CHECK(blob.data);
+ writer.WriteSnapshot(blob);
+ delete[] extra_code;
+ delete[] blob.data;
+ }
+
+ V8::Dispose();
+ V8::ShutdownPlatform();
+ delete platform;
+ return 0;
+}
--- /dev/null
+// Copyright 2014 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/natives.h"
+
+#include "src/base/logging.h"
+#include "src/list.h"
+#include "src/list-inl.h"
+#include "src/snapshot/snapshot-source-sink.h"
+#include "src/vector.h"
+
+#ifndef V8_USE_EXTERNAL_STARTUP_DATA
+#error natives-external.cc is used only for the external snapshot build.
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
+
+namespace v8 {
+namespace internal {
+
+
+/**
+ * NativesStore stores the 'native' (builtin) JS libraries.
+ *
+ * NativesStore needs to be initialized before using V8, usually by the
+ * embedder calling v8::SetNativesDataBlob, which calls SetNativesFromFile
+ * below.
+ */
+class NativesStore {
+ public:
+ ~NativesStore() {
+ for (int i = 0; i < native_names_.length(); i++) {
+ native_names_[i].Dispose();
+ }
+ }
+
+ int GetBuiltinsCount() { return native_ids_.length(); }
+ int GetDebuggerCount() { return debugger_count_; }
+
+ Vector<const char> GetScriptSource(int index) {
+ return native_source_[index];
+ }
+
+ Vector<const char> GetScriptName(int index) { return native_names_[index]; }
+
+ int GetIndex(const char* id) {
+ for (int i = 0; i < native_ids_.length(); ++i) {
+ int native_id_length = native_ids_[i].length();
+ if ((static_cast<int>(strlen(id)) == native_id_length) &&
+ (strncmp(id, native_ids_[i].start(), native_id_length) == 0)) {
+ return i;
+ }
+ }
+ DCHECK(false);
+ return -1;
+ }
+
+ Vector<const char> GetScriptsSource() {
+ DCHECK(false); // Not implemented.
+ return Vector<const char>();
+ }
+
+ static NativesStore* MakeFromScriptsSource(SnapshotByteSource* source) {
+ NativesStore* store = new NativesStore;
+
+ // We expect the libraries in the following format:
+ // int: # of debugger sources.
+ // 2N blobs: N pairs of source name + actual source.
+ // then, repeat for non-debugger sources.
+ int debugger_count = source->GetInt();
+ for (int i = 0; i < debugger_count; ++i)
+ store->ReadNameAndContentPair(source);
+ int library_count = source->GetInt();
+ for (int i = 0; i < library_count; ++i)
+ store->ReadNameAndContentPair(source);
+
+ store->debugger_count_ = debugger_count;
+ return store;
+ }
+
+ private:
+ NativesStore() : debugger_count_(0) {}
+
+ Vector<const char> NameFromId(const byte* id, int id_length) {
+ const char native[] = "native ";
+ const char extension[] = ".js";
+ Vector<char> name(Vector<char>::New(id_length + sizeof(native) - 1 +
+ sizeof(extension) - 1));
+ memcpy(name.start(), native, sizeof(native) - 1);
+ memcpy(name.start() + sizeof(native) - 1, id, id_length);
+ memcpy(name.start() + sizeof(native) - 1 + id_length, extension,
+ sizeof(extension) - 1);
+ return Vector<const char>::cast(name);
+ }
+
+ bool ReadNameAndContentPair(SnapshotByteSource* bytes) {
+ const byte* id;
+ int id_length;
+ const byte* source;
+ int source_length;
+ bool success = bytes->GetBlob(&id, &id_length) &&
+ bytes->GetBlob(&source, &source_length);
+ if (success) {
+ Vector<const char> id_vector(reinterpret_cast<const char*>(id),
+ id_length);
+ Vector<const char> source_vector(
+ reinterpret_cast<const char*>(source), source_length);
+ native_ids_.Add(id_vector);
+ native_source_.Add(source_vector);
+ native_names_.Add(NameFromId(id, id_length));
+ }
+ return success;
+ }
+
+ List<Vector<const char> > native_ids_;
+ List<Vector<const char> > native_names_;
+ List<Vector<const char> > native_source_;
+ int debugger_count_;
+
+ DISALLOW_COPY_AND_ASSIGN(NativesStore);
+};
+
+
+template<NativeType type>
+class NativesHolder {
+ public:
+ static NativesStore* get() {
+ DCHECK(holder_);
+ return holder_;
+ }
+ static void set(NativesStore* store) {
+ DCHECK(store);
+ holder_ = store;
+ }
+ static bool empty() { return holder_ == NULL; }
+ static void Dispose() {
+ delete holder_;
+ holder_ = NULL;
+ }
+
+ private:
+ static NativesStore* holder_;
+};
+
+template<NativeType type>
+NativesStore* NativesHolder<type>::holder_ = NULL;
+
+
+// The natives blob. Memory is owned by caller.
+static StartupData* natives_blob_ = NULL;
+
+
+/**
+ * Read the Natives blob, as previously set by SetNativesFromFile.
+ */
+void ReadNatives() {
+ if (natives_blob_ && NativesHolder<CORE>::empty()) {
+ SnapshotByteSource bytes(natives_blob_->data, natives_blob_->raw_size);
+ NativesHolder<CORE>::set(NativesStore::MakeFromScriptsSource(&bytes));
+ NativesHolder<EXPERIMENTAL>::set(
+ NativesStore::MakeFromScriptsSource(&bytes));
+ DCHECK(!bytes.HasMore());
+ }
+}
+
+
+/**
+ * Set the Natives (library sources) blob, as generated by js2c + the build
+ * system.
+ */
+void SetNativesFromFile(StartupData* natives_blob) {
+ DCHECK(!natives_blob_);
+ DCHECK(natives_blob);
+ DCHECK(natives_blob->data);
+ DCHECK(natives_blob->raw_size > 0);
+
+ natives_blob_ = natives_blob;
+ ReadNatives();
+}
+
+
+/**
+ * Release memory allocated by SetNativesFromFile.
+ */
+void DisposeNatives() {
+ NativesHolder<CORE>::Dispose();
+ NativesHolder<EXPERIMENTAL>::Dispose();
+}
+
+
+// Implement NativesCollection<T> bsaed on NativesHolder + NativesStore.
+//
+// (The callers expect a purely static interface, since this is how the
+// natives are usually compiled in. Since we implement them based on
+// runtime content, we have to implement this indirection to offer
+// a static interface.)
+template<NativeType type>
+int NativesCollection<type>::GetBuiltinsCount() {
+ return NativesHolder<type>::get()->GetBuiltinsCount();
+}
+
+template<NativeType type>
+int NativesCollection<type>::GetDebuggerCount() {
+ return NativesHolder<type>::get()->GetDebuggerCount();
+}
+
+template<NativeType type>
+int NativesCollection<type>::GetIndex(const char* name) {
+ return NativesHolder<type>::get()->GetIndex(name);
+}
+
+template <NativeType type>
+Vector<const char> NativesCollection<type>::GetScriptSource(int index) {
+ return NativesHolder<type>::get()->GetScriptSource(index);
+}
+
+template<NativeType type>
+Vector<const char> NativesCollection<type>::GetScriptName(int index) {
+ return NativesHolder<type>::get()->GetScriptName(index);
+}
+
+template <NativeType type>
+Vector<const char> NativesCollection<type>::GetScriptsSource() {
+ return NativesHolder<type>::get()->GetScriptsSource();
+}
+
+
+// The compiler can't 'see' all uses of the static methods and hence
+// my choice to elide them. This we'll explicitly instantiate these.
+template class NativesCollection<CORE>;
+template class NativesCollection<EXPERIMENTAL>;
+
+} // namespace v8::internal
+} // namespace v8
--- /dev/null
+// Copyright 2011 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_NATIVES_H_
+#define V8_NATIVES_H_
+
+#include "src/vector.h"
+
+namespace v8 { class StartupData; } // Forward declaration.
+
+namespace v8 {
+namespace internal {
+
+enum NativeType {
+ CORE, EXPERIMENTAL, D8, TEST
+};
+
+template <NativeType type>
+class NativesCollection {
+ public:
+ // Number of built-in scripts.
+ static int GetBuiltinsCount();
+ // Number of debugger implementation scripts.
+ static int GetDebuggerCount();
+
+ // These are used to access built-in scripts. The debugger implementation
+ // scripts have an index in the interval [0, GetDebuggerCount()). The
+ // non-debugger scripts have an index in the interval [GetDebuggerCount(),
+ // GetNativesCount()).
+ static int GetIndex(const char* name);
+ static Vector<const char> GetScriptSource(int index);
+ static Vector<const char> GetScriptName(int index);
+ static Vector<const char> GetScriptsSource();
+};
+
+typedef NativesCollection<CORE> Natives;
+typedef NativesCollection<EXPERIMENTAL> ExperimentalNatives;
+
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+// Used for reading the natives at runtime. Implementation in natives-empty.cc
+void SetNativesFromFile(StartupData* natives_blob);
+void ReadNatives();
+void DisposeNatives();
+#endif
+
+} } // namespace v8::internal
+
+#endif // V8_NATIVES_H_
--- /dev/null
+// Copyright 2012 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/v8.h"
+
+#include "src/accessors.h"
+#include "src/api.h"
+#include "src/base/platform/platform.h"
+#include "src/bootstrapper.h"
+#include "src/code-stubs.h"
+#include "src/cpu-profiler.h"
+#include "src/deoptimizer.h"
+#include "src/execution.h"
+#include "src/global-handles.h"
+#include "src/ic/ic.h"
+#include "src/ic/stub-cache.h"
+#include "src/objects.h"
+#include "src/parser.h"
+#include "src/runtime/runtime.h"
+#include "src/snapshot/natives.h"
+#include "src/snapshot/serialize.h"
+#include "src/snapshot/snapshot.h"
+#include "src/snapshot/snapshot-source-sink.h"
+#include "src/v8threads.h"
+#include "src/version.h"
+
+namespace v8 {
+namespace internal {
+
+
+// -----------------------------------------------------------------------------
+// Coding of external references.
+
+
+ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
+ ExternalReferenceTable* external_reference_table =
+ isolate->external_reference_table();
+ if (external_reference_table == NULL) {
+ external_reference_table = new ExternalReferenceTable(isolate);
+ isolate->set_external_reference_table(external_reference_table);
+ }
+ return external_reference_table;
+}
+
+
+ExternalReferenceTable::ExternalReferenceTable(Isolate* isolate) {
+ // Miscellaneous
+ Add(ExternalReference::roots_array_start(isolate).address(),
+ "Heap::roots_array_start()");
+ Add(ExternalReference::address_of_stack_limit(isolate).address(),
+ "StackGuard::address_of_jslimit()");
+ Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
+ "StackGuard::address_of_real_jslimit()");
+ Add(ExternalReference::new_space_start(isolate).address(),
+ "Heap::NewSpaceStart()");
+ Add(ExternalReference::new_space_mask(isolate).address(),
+ "Heap::NewSpaceMask()");
+ Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
+ "Heap::NewSpaceAllocationLimitAddress()");
+ Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
+ "Heap::NewSpaceAllocationTopAddress()");
+ Add(ExternalReference::debug_break(isolate).address(), "Debug::Break()");
+ Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
+ "Debug::step_in_fp_addr()");
+ Add(ExternalReference::mod_two_doubles_operation(isolate).address(),
+ "mod_two_doubles");
+ // Keyed lookup cache.
+ Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
+ "KeyedLookupCache::keys()");
+ Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
+ "KeyedLookupCache::field_offsets()");
+ Add(ExternalReference::handle_scope_next_address(isolate).address(),
+ "HandleScope::next");
+ Add(ExternalReference::handle_scope_limit_address(isolate).address(),
+ "HandleScope::limit");
+ Add(ExternalReference::handle_scope_level_address(isolate).address(),
+ "HandleScope::level");
+ Add(ExternalReference::new_deoptimizer_function(isolate).address(),
+ "Deoptimizer::New()");
+ Add(ExternalReference::compute_output_frames_function(isolate).address(),
+ "Deoptimizer::ComputeOutputFrames()");
+ Add(ExternalReference::address_of_min_int().address(),
+ "LDoubleConstant::min_int");
+ Add(ExternalReference::address_of_one_half().address(),
+ "LDoubleConstant::one_half");
+ Add(ExternalReference::isolate_address(isolate).address(), "isolate");
+ Add(ExternalReference::address_of_negative_infinity().address(),
+ "LDoubleConstant::negative_infinity");
+ Add(ExternalReference::power_double_double_function(isolate).address(),
+ "power_double_double_function");
+ Add(ExternalReference::power_double_int_function(isolate).address(),
+ "power_double_int_function");
+ Add(ExternalReference::math_log_double_function(isolate).address(),
+ "std::log");
+ Add(ExternalReference::store_buffer_top(isolate).address(),
+ "store_buffer_top");
+ Add(ExternalReference::address_of_the_hole_nan().address(), "the_hole_nan");
+ Add(ExternalReference::get_date_field_function(isolate).address(),
+ "JSDate::GetField");
+ Add(ExternalReference::date_cache_stamp(isolate).address(),
+ "date_cache_stamp");
+ Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
+ "address_of_pending_message_obj");
+ Add(ExternalReference::get_make_code_young_function(isolate).address(),
+ "Code::MakeCodeYoung");
+ Add(ExternalReference::cpu_features().address(), "cpu_features");
+ Add(ExternalReference::old_pointer_space_allocation_top_address(isolate)
+ .address(),
+ "Heap::OldPointerSpaceAllocationTopAddress");
+ Add(ExternalReference::old_pointer_space_allocation_limit_address(isolate)
+ .address(),
+ "Heap::OldPointerSpaceAllocationLimitAddress");
+ Add(ExternalReference::old_data_space_allocation_top_address(isolate)
+ .address(),
+ "Heap::OldDataSpaceAllocationTopAddress");
+ Add(ExternalReference::old_data_space_allocation_limit_address(isolate)
+ .address(),
+ "Heap::OldDataSpaceAllocationLimitAddress");
+ Add(ExternalReference::allocation_sites_list_address(isolate).address(),
+ "Heap::allocation_sites_list_address()");
+ Add(ExternalReference::address_of_uint32_bias().address(), "uint32_bias");
+ Add(ExternalReference::get_mark_code_as_executed_function(isolate).address(),
+ "Code::MarkCodeAsExecuted");
+ Add(ExternalReference::is_profiling_address(isolate).address(),
+ "CpuProfiler::is_profiling");
+ Add(ExternalReference::scheduled_exception_address(isolate).address(),
+ "Isolate::scheduled_exception");
+ Add(ExternalReference::invoke_function_callback(isolate).address(),
+ "InvokeFunctionCallback");
+ Add(ExternalReference::invoke_accessor_getter_callback(isolate).address(),
+ "InvokeAccessorGetterCallback");
+ Add(ExternalReference::flush_icache_function(isolate).address(),
+ "CpuFeatures::FlushICache");
+ Add(ExternalReference::log_enter_external_function(isolate).address(),
+ "Logger::EnterExternal");
+ Add(ExternalReference::log_leave_external_function(isolate).address(),
+ "Logger::LeaveExternal");
+ Add(ExternalReference::address_of_minus_one_half().address(),
+ "double_constants.minus_one_half");
+ Add(ExternalReference::stress_deopt_count(isolate).address(),
+ "Isolate::stress_deopt_count_address()");
+
+ // Debug addresses
+ Add(ExternalReference::debug_after_break_target_address(isolate).address(),
+ "Debug::after_break_target_address()");
+ Add(ExternalReference::debug_restarter_frame_function_pointer_address(isolate)
+ .address(),
+ "Debug::restarter_frame_function_pointer_address()");
+ Add(ExternalReference::debug_is_active_address(isolate).address(),
+ "Debug::is_active_address()");
+
+#ifndef V8_INTERPRETED_REGEXP
+ Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
+ "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
+ Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
+ "RegExpMacroAssembler*::CheckStackGuardState()");
+ Add(ExternalReference::re_grow_stack(isolate).address(),
+ "NativeRegExpMacroAssembler::GrowStack()");
+ Add(ExternalReference::re_word_character_map().address(),
+ "NativeRegExpMacroAssembler::word_character_map");
+ Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
+ "RegExpStack::limit_address()");
+ Add(ExternalReference::address_of_regexp_stack_memory_address(isolate)
+ .address(),
+ "RegExpStack::memory_address()");
+ Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
+ "RegExpStack::memory_size()");
+ Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
+ "OffsetsVector::static_offsets_vector");
+#endif // V8_INTERPRETED_REGEXP
+
+ // The following populates all of the different type of external references
+ // into the ExternalReferenceTable.
+ //
+ // NOTE: This function was originally 100k of code. It has since been
+ // rewritten to be mostly table driven, as the callback macro style tends to
+ // very easily cause code bloat. Please be careful in the future when adding
+ // new references.
+
+ struct RefTableEntry {
+ uint16_t id;
+ const char* name;
+ };
+
+ static const RefTableEntry c_builtins[] = {
+#define DEF_ENTRY_C(name, ignored) \
+ { Builtins::c_##name, "Builtins::" #name } \
+ ,
+ BUILTIN_LIST_C(DEF_ENTRY_C)
+#undef DEF_ENTRY_C
+ };
+
+ for (unsigned i = 0; i < arraysize(c_builtins); ++i) {
+ ExternalReference ref(static_cast<Builtins::CFunctionId>(c_builtins[i].id),
+ isolate);
+ Add(ref.address(), c_builtins[i].name);
+ }
+
+ static const RefTableEntry builtins[] = {
+#define DEF_ENTRY_C(name, ignored) \
+ { Builtins::k##name, "Builtins::" #name } \
+ ,
+#define DEF_ENTRY_A(name, i1, i2, i3) \
+ { Builtins::k##name, "Builtins::" #name } \
+ ,
+ BUILTIN_LIST_C(DEF_ENTRY_C) BUILTIN_LIST_A(DEF_ENTRY_A)
+ BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
+#undef DEF_ENTRY_C
+#undef DEF_ENTRY_A
+ };
+
+ for (unsigned i = 0; i < arraysize(builtins); ++i) {
+ ExternalReference ref(static_cast<Builtins::Name>(builtins[i].id), isolate);
+ Add(ref.address(), builtins[i].name);
+ }
+
+ static const RefTableEntry runtime_functions[] = {
+#define RUNTIME_ENTRY(name, i1, i2) \
+ { Runtime::k##name, "Runtime::" #name } \
+ ,
+ FOR_EACH_INTRINSIC(RUNTIME_ENTRY)
+#undef RUNTIME_ENTRY
+ };
+
+ for (unsigned i = 0; i < arraysize(runtime_functions); ++i) {
+ ExternalReference ref(
+ static_cast<Runtime::FunctionId>(runtime_functions[i].id), isolate);
+ Add(ref.address(), runtime_functions[i].name);
+ }
+
+ static const RefTableEntry inline_caches[] = {
+#define IC_ENTRY(name) \
+ { IC::k##name, "IC::" #name } \
+ ,
+ IC_UTIL_LIST(IC_ENTRY)
+#undef IC_ENTRY
+ };
+
+ for (unsigned i = 0; i < arraysize(inline_caches); ++i) {
+ ExternalReference ref(
+ IC_Utility(static_cast<IC::UtilityId>(inline_caches[i].id)), isolate);
+ Add(ref.address(), runtime_functions[i].name);
+ }
+
+ // Stat counters
+ struct StatsRefTableEntry {
+ StatsCounter* (Counters::*counter)();
+ const char* name;
+ };
+
+ static const StatsRefTableEntry stats_ref_table[] = {
+#define COUNTER_ENTRY(name, caption) \
+ { &Counters::name, "Counters::" #name } \
+ ,
+ STATS_COUNTER_LIST_1(COUNTER_ENTRY) STATS_COUNTER_LIST_2(COUNTER_ENTRY)
+#undef COUNTER_ENTRY
+ };
+
+ Counters* counters = isolate->counters();
+ for (unsigned i = 0; i < arraysize(stats_ref_table); ++i) {
+ // To make sure the indices are not dependent on whether counters are
+ // enabled, use a dummy address as filler.
+ Address address = NotAvailable();
+ StatsCounter* counter = (counters->*(stats_ref_table[i].counter))();
+ if (counter->Enabled()) {
+ address = reinterpret_cast<Address>(counter->GetInternalPointer());
+ }
+ Add(address, stats_ref_table[i].name);
+ }
+
+ // Top addresses
+ static const char* address_names[] = {
+#define BUILD_NAME_LITERAL(Name, name) "Isolate::" #name "_address",
+ FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL) NULL
+#undef BUILD_NAME_LITERAL
+ };
+
+ for (int i = 0; i < Isolate::kIsolateAddressCount; ++i) {
+ Add(isolate->get_address_from_id(static_cast<Isolate::AddressId>(i)),
+ address_names[i]);
+ }
+
+ // Accessors
+ struct AccessorRefTable {
+ Address address;
+ const char* name;
+ };
+
+ static const AccessorRefTable accessors[] = {
+#define ACCESSOR_INFO_DECLARATION(name) \
+ { FUNCTION_ADDR(&Accessors::name##Getter), "Accessors::" #name "Getter" } \
+ , {FUNCTION_ADDR(&Accessors::name##Setter), "Accessors::" #name "Setter"},
+ ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
+#undef ACCESSOR_INFO_DECLARATION
+ };
+
+ for (unsigned i = 0; i < arraysize(accessors); ++i) {
+ Add(accessors[i].address, accessors[i].name);
+ }
+
+ StubCache* stub_cache = isolate->stub_cache();
+
+ // Stub cache tables
+ Add(stub_cache->key_reference(StubCache::kPrimary).address(),
+ "StubCache::primary_->key");
+ Add(stub_cache->value_reference(StubCache::kPrimary).address(),
+ "StubCache::primary_->value");
+ Add(stub_cache->map_reference(StubCache::kPrimary).address(),
+ "StubCache::primary_->map");
+ Add(stub_cache->key_reference(StubCache::kSecondary).address(),
+ "StubCache::secondary_->key");
+ Add(stub_cache->value_reference(StubCache::kSecondary).address(),
+ "StubCache::secondary_->value");
+ Add(stub_cache->map_reference(StubCache::kSecondary).address(),
+ "StubCache::secondary_->map");
+
+ // Runtime entries
+ Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
+ "HandleScope::DeleteExtensions");
+ Add(ExternalReference::incremental_marking_record_write_function(isolate)
+ .address(),
+ "IncrementalMarking::RecordWrite");
+ Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
+ "StoreBuffer::StoreBufferOverflow");
+
+ // Add a small set of deopt entry addresses to encoder without generating the
+ // deopt table code, which isn't possible at deserialization time.
+ HandleScope scope(isolate);
+ for (int entry = 0; entry < kDeoptTableSerializeEntryCount; ++entry) {
+ Address address = Deoptimizer::GetDeoptimizationEntry(
+ isolate,
+ entry,
+ Deoptimizer::LAZY,
+ Deoptimizer::CALCULATE_ENTRY_ADDRESS);
+ Add(address, "lazy_deopt");
+ }
+}
+
+
+ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) {
+ map_ = isolate->external_reference_map();
+ if (map_ != NULL) return;
+ map_ = new HashMap(HashMap::PointersMatch);
+ ExternalReferenceTable* table = ExternalReferenceTable::instance(isolate);
+ for (int i = 0; i < table->size(); ++i) {
+ Address addr = table->address(i);
+ if (addr == ExternalReferenceTable::NotAvailable()) continue;
+ // We expect no duplicate external references entries in the table.
+ DCHECK_NULL(map_->Lookup(addr, Hash(addr), false));
+ map_->Lookup(addr, Hash(addr), true)->value = reinterpret_cast<void*>(i);
+ }
+ isolate->set_external_reference_map(map_);
+}
+
+
+uint32_t ExternalReferenceEncoder::Encode(Address address) const {
+ DCHECK_NOT_NULL(address);
+ HashMap::Entry* entry =
+ const_cast<HashMap*>(map_)->Lookup(address, Hash(address), false);
+ DCHECK_NOT_NULL(entry);
+ return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value));
+}
+
+
+const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
+ Address address) const {
+ HashMap::Entry* entry =
+ const_cast<HashMap*>(map_)->Lookup(address, Hash(address), false);
+ if (entry == NULL) return "<unknown>";
+ uint32_t i = static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value));
+ return ExternalReferenceTable::instance(isolate)->name(i);
+}
+
+
+RootIndexMap::RootIndexMap(Isolate* isolate) {
+ map_ = isolate->root_index_map();
+ if (map_ != NULL) return;
+ map_ = new HashMap(HashMap::PointersMatch);
+ Object** root_array = isolate->heap()->roots_array_start();
+ for (uint32_t i = 0; i < Heap::kStrongRootListLength; i++) {
+ Heap::RootListIndex root_index = static_cast<Heap::RootListIndex>(i);
+ Object* root = root_array[root_index];
+ // Omit root entries that can be written after initialization. They must
+ // not be referenced through the root list in the snapshot.
+ if (root->IsHeapObject() &&
+ isolate->heap()->RootCanBeTreatedAsConstant(root_index)) {
+ HeapObject* heap_object = HeapObject::cast(root);
+ HashMap::Entry* entry = LookupEntry(map_, heap_object, false);
+ if (entry != NULL) {
+ // Some are initialized to a previous value in the root list.
+ DCHECK_LT(GetValue(entry), i);
+ } else {
+ SetValue(LookupEntry(map_, heap_object, true), i);
+ }
+ }
+ }
+ isolate->set_root_index_map(map_);
+}
+
+
+class CodeAddressMap: public CodeEventLogger {
+ public:
+ explicit CodeAddressMap(Isolate* isolate)
+ : isolate_(isolate) {
+ isolate->logger()->addCodeEventListener(this);
+ }
+
+ virtual ~CodeAddressMap() {
+ isolate_->logger()->removeCodeEventListener(this);
+ }
+
+ virtual void CodeMoveEvent(Address from, Address to) {
+ address_to_name_map_.Move(from, to);
+ }
+
+ virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) {
+ }
+
+ virtual void CodeDeleteEvent(Address from) {
+ address_to_name_map_.Remove(from);
+ }
+
+ const char* Lookup(Address address) {
+ return address_to_name_map_.Lookup(address);
+ }
+
+ private:
+ class NameMap {
+ public:
+ NameMap() : impl_(HashMap::PointersMatch) {}
+
+ ~NameMap() {
+ for (HashMap::Entry* p = impl_.Start(); p != NULL; p = impl_.Next(p)) {
+ DeleteArray(static_cast<const char*>(p->value));
+ }
+ }
+
+ void Insert(Address code_address, const char* name, int name_size) {
+ HashMap::Entry* entry = FindOrCreateEntry(code_address);
+ if (entry->value == NULL) {
+ entry->value = CopyName(name, name_size);
+ }
+ }
+
+ const char* Lookup(Address code_address) {
+ HashMap::Entry* entry = FindEntry(code_address);
+ return (entry != NULL) ? static_cast<const char*>(entry->value) : NULL;
+ }
+
+ void Remove(Address code_address) {
+ HashMap::Entry* entry = FindEntry(code_address);
+ if (entry != NULL) {
+ DeleteArray(static_cast<char*>(entry->value));
+ RemoveEntry(entry);
+ }
+ }
+
+ void Move(Address from, Address to) {
+ if (from == to) return;
+ HashMap::Entry* from_entry = FindEntry(from);
+ DCHECK(from_entry != NULL);
+ void* value = from_entry->value;
+ RemoveEntry(from_entry);
+ HashMap::Entry* to_entry = FindOrCreateEntry(to);
+ DCHECK(to_entry->value == NULL);
+ to_entry->value = value;
+ }
+
+ private:
+ static char* CopyName(const char* name, int name_size) {
+ char* result = NewArray<char>(name_size + 1);
+ for (int i = 0; i < name_size; ++i) {
+ char c = name[i];
+ if (c == '\0') c = ' ';
+ result[i] = c;
+ }
+ result[name_size] = '\0';
+ return result;
+ }
+
+ HashMap::Entry* FindOrCreateEntry(Address code_address) {
+ return impl_.Lookup(code_address, ComputePointerHash(code_address), true);
+ }
+
+ HashMap::Entry* FindEntry(Address code_address) {
+ return impl_.Lookup(code_address,
+ ComputePointerHash(code_address),
+ false);
+ }
+
+ void RemoveEntry(HashMap::Entry* entry) {
+ impl_.Remove(entry->key, entry->hash);
+ }
+
+ HashMap impl_;
+
+ DISALLOW_COPY_AND_ASSIGN(NameMap);
+ };
+
+ virtual void LogRecordedBuffer(Code* code,
+ SharedFunctionInfo*,
+ const char* name,
+ int length) {
+ address_to_name_map_.Insert(code->address(), name, length);
+ }
+
+ NameMap address_to_name_map_;
+ Isolate* isolate_;
+};
+
+
+void Deserializer::DecodeReservation(
+ Vector<const SerializedData::Reservation> 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::FlushICacheForNewCodeObjects() {
+ PageIterator it(isolate_->heap()->code_space());
+ while (it.has_next()) {
+ Page* p = it.next();
+ CpuFeatures::FlushICache(p->area_start(), p->area_end() - p->area_start());
+ }
+}
+
+
+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());
+ isolate_->heap()->IterateSmiRoots(this);
+ isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
+ isolate_->heap()->RepairFreeListsAfterDeserialization();
+ isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
+
+ isolate_->heap()->set_native_contexts_list(
+ isolate_->heap()->undefined_value());
+ isolate_->heap()->set_array_buffers_list(
+ isolate_->heap()->undefined_value());
+ isolate->heap()->set_new_array_buffer_views_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.
+ for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
+ Object* source = isolate_->heap()->natives_source_cache()->get(i);
+ if (!source->IsUndefined()) {
+ ExternalOneByteString::cast(source)->update_data_cache();
+ }
+ }
+
+ FlushICacheForNewCodeObjects();
+
+ // Issue code events for newly deserialized code objects.
+ LOG_CODE_EVENT(isolate_, LogCodeObjects());
+ LOG_CODE_EVENT(isolate_, LogCompiledFunctions());
+}
+
+
+MaybeHandle<Object> Deserializer::DeserializePartial(
+ Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
+ Handle<FixedArray>* outdated_contexts_out) {
+ Initialize(isolate);
+ if (!ReserveSpace()) {
+ V8::FatalProcessOutOfMemory("deserialize context");
+ return MaybeHandle<Object>();
+ }
+
+ Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::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;
+ Object* outdated_contexts;
+ VisitPointer(&root);
+ VisitPointer(&outdated_contexts);
+
+ // 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.
+ CHECK_EQ(start_address, code_space->top());
+ CHECK(outdated_contexts->IsFixedArray());
+ *outdated_contexts_out =
+ Handle<FixedArray>(FixedArray::cast(outdated_contexts), isolate);
+ return Handle<Object>(root, isolate);
+}
+
+
+MaybeHandle<SharedFunctionInfo> Deserializer::DeserializeCode(
+ Isolate* isolate) {
+ Initialize(isolate);
+ if (!ReserveSpace()) {
+ return Handle<SharedFunctionInfo>();
+ } else {
+ deserializing_user_code_ = true;
+ DisallowHeapAllocation no_gc;
+ Object* root;
+ VisitPointer(&root);
+ return Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(root));
+ }
+}
+
+
+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::RelinkAllocationSite(AllocationSite* site) {
+ 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);
+}
+
+
+// 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 virtual Handle<Object> AsHandle(Isolate* isolate)
+ OVERRIDE {
+ return handle(string_, isolate);
+ }
+
+ String* string_;
+ uint32_t hash_;
+};
+
+
+HeapObject* Deserializer::ProcessNewObjectFromSerializedCode(HeapObject* obj) {
+ 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()) {
+ DisallowHeapAllocation no_gc;
+ HandleScope scope(isolate_);
+ StringTableInsertionKey key(string);
+ String* canonical = *StringTable::LookupKey(isolate_, &key);
+ string->SetForwardedInternalizedString(canonical);
+ return canonical;
+ }
+ } else if (obj->IsScript()) {
+ Script::cast(obj)->set_id(isolate_->heap()->NextScriptId());
+ }
+ return obj;
+}
+
+
+HeapObject* Deserializer::GetBackReferencedObject(int space) {
+ HeapObject* obj;
+ BackReference back_reference(source_.GetInt());
+ if (space == LO_SPACE) {
+ CHECK(back_reference.chunk_index() == 0);
+ uint32_t index = back_reference.large_object_index();
+ obj = deserialized_large_objects_[index];
+ } else {
+ DCHECK(space < kNumberOfPreallocatedSpaces);
+ uint32_t chunk_index = back_reference.chunk_index();
+ DCHECK_LE(chunk_index, current_chunk_[space]);
+ uint32_t chunk_offset = back_reference.chunk_offset();
+ obj = HeapObject::FromAddress(reservations_[space][chunk_index].start +
+ chunk_offset);
+ }
+ if (deserializing_user_code() && obj->IsInternalizedString()) {
+ obj = String::cast(obj)->GetForwardedInternalizedString();
+ }
+ hot_objects_.Add(obj);
+ return obj;
+}
+
+
+// This routine writes the new object into the pointer provided and then
+// returns true if the new object was in young space and false otherwise.
+// The reason for this strange interface is that otherwise the object is
+// written very late, which means the FreeSpace map is not set up by the
+// time we need to use it to mark the space at the end of a page free.
+void Deserializer::ReadObject(int space_number, Object** write_back) {
+ Address address;
+ HeapObject* obj;
+ int next_int = source_.GetInt();
+
+ bool double_align = false;
+#ifndef V8_HOST_ARCH_64_BIT
+ double_align = next_int == kDoubleAlignmentSentinel;
+ if (double_align) next_int = source_.GetInt();
+#endif
+
+ DCHECK_NE(kDoubleAlignmentSentinel, next_int);
+ int size = next_int << kObjectAlignmentBits;
+ int reserved_size = size + (double_align ? kPointerSize : 0);
+ address = Allocate(space_number, reserved_size);
+ obj = HeapObject::FromAddress(address);
+ if (double_align) {
+ obj = isolate_->heap()->DoubleAlignForDeserialization(obj, reserved_size);
+ address = obj->address();
+ }
+
+ isolate_->heap()->OnAllocationEvent(obj, size);
+ Object** current = reinterpret_cast<Object**>(address);
+ Object** limit = current + (size >> kPointerSizeLog2);
+ if (FLAG_log_snapshot_positions) {
+ LOG(isolate_, SnapshotPositionEvent(address, source_.position()));
+ }
+ ReadData(current, limit, space_number, address);
+
+ // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
+ // as a (weak) root. If this root is relocated correctly,
+ // RelinkAllocationSite() isn't necessary.
+ if (obj->IsAllocationSite()) RelinkAllocationSite(AllocationSite::cast(obj));
+
+ // Fix up strings from serialized user code.
+ if (deserializing_user_code()) obj = ProcessNewObjectFromSerializedCode(obj);
+
+ Object* write_back_obj = obj;
+ UnalignedCopy(write_back, &write_back_obj);
+#ifdef DEBUG
+ if (obj->IsCode()) {
+ DCHECK(space_number == CODE_SPACE || space_number == LO_SPACE);
+#ifdef VERIFY_HEAP
+ obj->ObjectVerify();
+#endif // VERIFY_HEAP
+ } else {
+ DCHECK(space_number != CODE_SPACE);
+ }
+#endif // DEBUG
+}
+
+
+// We know the space requirements before deserialization and can
+// pre-allocate that reserved space. During deserialization, all we need
+// to do is to bump up the pointer for each space in the reserved
+// space. This is also used for fixing back references.
+// We may have to split up the pre-allocation into several chunks
+// because it would not fit onto a single page. We do not have to keep
+// track of when to move to the next chunk. An opcode will signal this.
+// Since multiple large objects cannot be folded into one large object
+// space allocation, we have to do an actual allocation when deserializing
+// each large object. Instead of tracking offset for back references, we
+// reference large objects by index.
+Address Deserializer::Allocate(int space_index, int size) {
+ if (space_index == LO_SPACE) {
+ AlwaysAllocateScope scope(isolate_);
+ LargeObjectSpace* lo_space = isolate_->heap()->lo_space();
+ Executability exec = static_cast<Executability>(source_.Get());
+ AllocationResult result = lo_space->AllocateRaw(size, exec);
+ HeapObject* obj = HeapObject::cast(result.ToObjectChecked());
+ deserialized_large_objects_.Add(obj);
+ return obj->address();
+ } else {
+ DCHECK(space_index < kNumberOfPreallocatedSpaces);
+ Address address = high_water_[space_index];
+ DCHECK_NOT_NULL(address);
+ high_water_[space_index] += size;
+#ifdef DEBUG
+ // Assert that the current reserved chunk is still big enough.
+ const Heap::Reservation& reservation = reservations_[space_index];
+ int chunk_index = current_chunk_[space_index];
+ CHECK_LE(high_water_[space_index], reservation[chunk_index].end);
+#endif
+ return address;
+ }
+}
+
+
+void Deserializer::ReadData(Object** current, Object** limit, int source_space,
+ Address current_object_address) {
+ Isolate* const isolate = isolate_;
+ // Write barrier support costs around 1% in startup time. In fact there
+ // are no new space objects in current boot snapshots, so it's not needed,
+ // but that may change.
+ bool write_barrier_needed =
+ (current_object_address != NULL && source_space != NEW_SPACE &&
+ source_space != CELL_SPACE && source_space != CODE_SPACE &&
+ source_space != OLD_DATA_SPACE);
+ while (current < limit) {
+ byte data = source_.Get();
+ switch (data) {
+#define CASE_STATEMENT(where, how, within, space_number) \
+ case where + how + within + space_number: \
+ STATIC_ASSERT((where & ~kWhereMask) == 0); \
+ STATIC_ASSERT((how & ~kHowToCodeMask) == 0); \
+ STATIC_ASSERT((within & ~kWhereToPointMask) == 0); \
+ STATIC_ASSERT((space_number & ~kSpaceMask) == 0);
+
+#define CASE_BODY(where, how, within, space_number_if_any) \
+ { \
+ bool emit_write_barrier = false; \
+ bool current_was_incremented = false; \
+ int space_number = space_number_if_any == kAnyOldSpace \
+ ? (data & kSpaceMask) \
+ : space_number_if_any; \
+ if (where == kNewObject && how == kPlain && within == kStartOfObject) { \
+ ReadObject(space_number, current); \
+ emit_write_barrier = (space_number == NEW_SPACE); \
+ } else { \
+ Object* new_object = NULL; /* May not be a real Object pointer. */ \
+ if (where == kNewObject) { \
+ ReadObject(space_number, &new_object); \
+ } else if (where == kBackref) { \
+ emit_write_barrier = (space_number == NEW_SPACE); \
+ new_object = GetBackReferencedObject(data & kSpaceMask); \
+ } else if (where == kBackrefWithSkip) { \
+ int skip = source_.GetInt(); \
+ current = reinterpret_cast<Object**>( \
+ reinterpret_cast<Address>(current) + skip); \
+ emit_write_barrier = (space_number == NEW_SPACE); \
+ new_object = GetBackReferencedObject(data & kSpaceMask); \
+ } else if (where == kRootArray) { \
+ int root_id = source_.GetInt(); \
+ new_object = isolate->heap()->roots_array_start()[root_id]; \
+ emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
+ } else if (where == kPartialSnapshotCache) { \
+ int cache_index = source_.GetInt(); \
+ new_object = isolate->partial_snapshot_cache()->at(cache_index); \
+ emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
+ } else if (where == kExternalReference) { \
+ int skip = source_.GetInt(); \
+ current = reinterpret_cast<Object**>( \
+ reinterpret_cast<Address>(current) + skip); \
+ int reference_id = source_.GetInt(); \
+ Address address = external_reference_table_->address(reference_id); \
+ new_object = reinterpret_cast<Object*>(address); \
+ } else if (where == kAttachedReference) { \
+ int index = source_.GetInt(); \
+ DCHECK(deserializing_user_code() || index == kGlobalProxyReference); \
+ new_object = *attached_objects_[index]; \
+ emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
+ } else { \
+ DCHECK(where == kBuiltin); \
+ DCHECK(deserializing_user_code()); \
+ int builtin_id = source_.GetInt(); \
+ DCHECK_LE(0, builtin_id); \
+ DCHECK_LT(builtin_id, Builtins::builtin_count); \
+ Builtins::Name name = static_cast<Builtins::Name>(builtin_id); \
+ new_object = isolate->builtins()->builtin(name); \
+ emit_write_barrier = false; \
+ } \
+ if (within == kInnerPointer) { \
+ if (space_number != CODE_SPACE || new_object->IsCode()) { \
+ Code* new_code_object = reinterpret_cast<Code*>(new_object); \
+ new_object = \
+ reinterpret_cast<Object*>(new_code_object->instruction_start()); \
+ } else { \
+ DCHECK(space_number == CODE_SPACE); \
+ Cell* cell = Cell::cast(new_object); \
+ new_object = reinterpret_cast<Object*>(cell->ValueAddress()); \
+ } \
+ } \
+ if (how == kFromCode) { \
+ Address location_of_branch_data = reinterpret_cast<Address>(current); \
+ Assembler::deserialization_set_special_target_at( \
+ location_of_branch_data, \
+ Code::cast(HeapObject::FromAddress(current_object_address)), \
+ reinterpret_cast<Address>(new_object)); \
+ location_of_branch_data += Assembler::kSpecialTargetSize; \
+ current = reinterpret_cast<Object**>(location_of_branch_data); \
+ current_was_incremented = true; \
+ } else { \
+ UnalignedCopy(current, &new_object); \
+ } \
+ } \
+ if (emit_write_barrier && write_barrier_needed) { \
+ Address current_address = reinterpret_cast<Address>(current); \
+ isolate->heap()->RecordWrite( \
+ current_object_address, \
+ static_cast<int>(current_address - current_object_address)); \
+ } \
+ if (!current_was_incremented) { \
+ current++; \
+ } \
+ break; \
+ }
+
+// This generates a case and a body for the new space (which has to do extra
+// write barrier handling) and handles the other spaces with fall-through cases
+// and one body.
+#define ALL_SPACES(where, how, within) \
+ CASE_STATEMENT(where, how, within, NEW_SPACE) \
+ CASE_BODY(where, how, within, NEW_SPACE) \
+ CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
+ CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
+ CASE_STATEMENT(where, how, within, CODE_SPACE) \
+ CASE_STATEMENT(where, how, within, MAP_SPACE) \
+ CASE_STATEMENT(where, how, within, CELL_SPACE) \
+ CASE_STATEMENT(where, how, within, LO_SPACE) \
+ CASE_BODY(where, how, within, kAnyOldSpace)
+
+#define FOUR_CASES(byte_code) \
+ case byte_code: \
+ case byte_code + 1: \
+ case byte_code + 2: \
+ case byte_code + 3:
+
+#define SIXTEEN_CASES(byte_code) \
+ FOUR_CASES(byte_code) \
+ FOUR_CASES(byte_code + 4) \
+ FOUR_CASES(byte_code + 8) \
+ FOUR_CASES(byte_code + 12)
+
+ // Deserialize a new object and write a pointer to it to the current
+ // object.
+ ALL_SPACES(kNewObject, kPlain, kStartOfObject)
+ // Support for direct instruction pointers in functions. It's an inner
+ // pointer because it points at the entry point, not at the start of the
+ // code object.
+ CASE_STATEMENT(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
+ CASE_BODY(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
+ // Deserialize a new code object and write a pointer to its first
+ // instruction to the current code object.
+ ALL_SPACES(kNewObject, kFromCode, kInnerPointer)
+ // Find a recently deserialized object using its offset from the current
+ // allocation point and write a pointer to it to the current object.
+ ALL_SPACES(kBackref, kPlain, kStartOfObject)
+ ALL_SPACES(kBackrefWithSkip, kPlain, kStartOfObject)
+#if defined(V8_TARGET_ARCH_MIPS) || defined(V8_TARGET_ARCH_MIPS64) || \
+ defined(V8_TARGET_ARCH_PPC) || V8_OOL_CONSTANT_POOL
+ // Deserialize a new object from pointer found in code and write
+ // a pointer to it to the current object. Required only for MIPS, PPC or
+ // ARM with ool constant pool, and omitted on the other architectures
+ // because it is fully unrolled and would cause bloat.
+ ALL_SPACES(kNewObject, kFromCode, kStartOfObject)
+ // Find a recently deserialized code object using its offset from the
+ // current allocation point and write a pointer to it to the current
+ // object. Required only for MIPS, PPC or ARM with ool constant pool.
+ ALL_SPACES(kBackref, kFromCode, kStartOfObject)
+ ALL_SPACES(kBackrefWithSkip, kFromCode, kStartOfObject)
+#endif
+ // Find a recently deserialized code object using its offset from the
+ // current allocation point and write a pointer to its first instruction
+ // to the current code object or the instruction pointer in a function
+ // object.
+ ALL_SPACES(kBackref, kFromCode, kInnerPointer)
+ ALL_SPACES(kBackrefWithSkip, kFromCode, kInnerPointer)
+ ALL_SPACES(kBackref, kPlain, kInnerPointer)
+ ALL_SPACES(kBackrefWithSkip, kPlain, kInnerPointer)
+ // Find an object in the roots array and write a pointer to it to the
+ // current object.
+ CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
+ CASE_BODY(kRootArray, kPlain, kStartOfObject, 0)
+#if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL || \
+ defined(V8_TARGET_ARCH_MIPS64) || defined(V8_TARGET_ARCH_PPC)
+ // Find an object in the roots array and write a pointer to it to in code.
+ CASE_STATEMENT(kRootArray, kFromCode, kStartOfObject, 0)
+ CASE_BODY(kRootArray, kFromCode, kStartOfObject, 0)
+#endif
+ // Find an object in the partial snapshots cache and write a pointer to it
+ // to the current object.
+ CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
+ CASE_BODY(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
+ // Find an code entry in the partial snapshots cache and
+ // write a pointer to it to the current object.
+ CASE_STATEMENT(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
+ CASE_BODY(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
+ // Find an external reference and write a pointer to it to the current
+ // object.
+ CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
+ CASE_BODY(kExternalReference, kPlain, kStartOfObject, 0)
+ // Find an external reference and write a pointer to it in the current
+ // code object.
+ CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
+ CASE_BODY(kExternalReference, kFromCode, kStartOfObject, 0)
+ // Find an object in the attached references and write a pointer to it to
+ // the current object.
+ CASE_STATEMENT(kAttachedReference, kPlain, kStartOfObject, 0)
+ CASE_BODY(kAttachedReference, kPlain, kStartOfObject, 0)
+ CASE_STATEMENT(kAttachedReference, kPlain, kInnerPointer, 0)
+ CASE_BODY(kAttachedReference, kPlain, kInnerPointer, 0)
+ CASE_STATEMENT(kAttachedReference, kFromCode, kInnerPointer, 0)
+ CASE_BODY(kAttachedReference, kFromCode, kInnerPointer, 0)
+ // Find a builtin and write a pointer to it to the current object.
+ CASE_STATEMENT(kBuiltin, kPlain, kStartOfObject, 0)
+ CASE_BODY(kBuiltin, kPlain, kStartOfObject, 0)
+ CASE_STATEMENT(kBuiltin, kPlain, kInnerPointer, 0)
+ CASE_BODY(kBuiltin, kPlain, kInnerPointer, 0)
+ CASE_STATEMENT(kBuiltin, kFromCode, kInnerPointer, 0)
+ CASE_BODY(kBuiltin, kFromCode, kInnerPointer, 0)
+
+#undef CASE_STATEMENT
+#undef CASE_BODY
+#undef ALL_SPACES
+
+ case kSkip: {
+ int size = source_.GetInt();
+ current = reinterpret_cast<Object**>(
+ reinterpret_cast<intptr_t>(current) + size);
+ break;
+ }
+
+ case kInternalReferenceEncoded:
+ case kInternalReference: {
+ // Internal reference address is not encoded via skip, but by offset
+ // from code entry.
+ int pc_offset = source_.GetInt();
+ int target_offset = source_.GetInt();
+ Code* code =
+ Code::cast(HeapObject::FromAddress(current_object_address));
+ DCHECK(0 <= pc_offset && pc_offset <= code->instruction_size());
+ DCHECK(0 <= target_offset && target_offset <= code->instruction_size());
+ Address pc = code->entry() + pc_offset;
+ Address target = code->entry() + target_offset;
+ Assembler::deserialization_set_target_internal_reference_at(
+ pc, target, data == kInternalReference
+ ? RelocInfo::INTERNAL_REFERENCE
+ : RelocInfo::INTERNAL_REFERENCE_ENCODED);
+ break;
+ }
+
+ case kNop:
+ break;
+
+ case kNextChunk: {
+ int space = source_.Get();
+ DCHECK(space < kNumberOfPreallocatedSpaces);
+ int chunk_index = current_chunk_[space];
+ const Heap::Reservation& reservation = reservations_[space];
+ // Make sure the current chunk is indeed exhausted.
+ CHECK_EQ(reservation[chunk_index].end, high_water_[space]);
+ // Move to next reserved chunk.
+ chunk_index = ++current_chunk_[space];
+ CHECK_LT(chunk_index, reservation.length());
+ high_water_[space] = reservation[chunk_index].start;
+ break;
+ }
+
+ case kSynchronize:
+ // If we get here then that indicates that you have a mismatch between
+ // the number of GC roots when serializing and deserializing.
+ CHECK(false);
+ break;
+
+ case kNativesStringResource: {
+ DCHECK(!isolate_->heap()->deserialization_complete());
+ int index = source_.Get();
+ Vector<const char> source_vector = Natives::GetScriptSource(index);
+ NativesExternalStringResource* resource =
+ new NativesExternalStringResource(source_vector.start(),
+ source_vector.length());
+ Object* resource_obj = reinterpret_cast<Object*>(resource);
+ UnalignedCopy(current++, &resource_obj);
+ break;
+ }
+
+ // Deserialize raw data of variable length.
+ case kVariableRawData: {
+ int size_in_bytes = source_.GetInt();
+ byte* raw_data_out = reinterpret_cast<byte*>(current);
+ source_.CopyRaw(raw_data_out, size_in_bytes);
+ break;
+ }
+
+ case kVariableRepeat: {
+ int repeats = source_.GetInt();
+ Object* object = current[-1];
+ DCHECK(!isolate->heap()->InNewSpace(object));
+ for (int i = 0; i < repeats; i++) UnalignedCopy(current++, &object);
+ break;
+ }
+
+ STATIC_ASSERT(kNumberOfRootArrayConstants == Heap::kOldSpaceRoots);
+ STATIC_ASSERT(kNumberOfRootArrayConstants == 32);
+ SIXTEEN_CASES(kRootArrayConstantsWithSkip)
+ SIXTEEN_CASES(kRootArrayConstantsWithSkip + 16) {
+ int skip = source_.GetInt();
+ current = reinterpret_cast<Object**>(
+ reinterpret_cast<intptr_t>(current) + skip);
+ // Fall through.
+ }
+
+ SIXTEEN_CASES(kRootArrayConstants)
+ SIXTEEN_CASES(kRootArrayConstants + 16) {
+ int root_id = data & kRootArrayConstantsMask;
+ Object* object = isolate->heap()->roots_array_start()[root_id];
+ DCHECK(!isolate->heap()->InNewSpace(object));
+ UnalignedCopy(current++, &object);
+ break;
+ }
+
+ STATIC_ASSERT(kNumberOfHotObjects == 8);
+ FOUR_CASES(kHotObjectWithSkip)
+ FOUR_CASES(kHotObjectWithSkip + 4) {
+ int skip = source_.GetInt();
+ current = reinterpret_cast<Object**>(
+ reinterpret_cast<Address>(current) + skip);
+ // Fall through.
+ }
+
+ FOUR_CASES(kHotObject)
+ FOUR_CASES(kHotObject + 4) {
+ int index = data & kHotObjectMask;
+ Object* hot_object = hot_objects_.Get(index);
+ UnalignedCopy(current, &hot_object);
+ if (write_barrier_needed && isolate->heap()->InNewSpace(hot_object)) {
+ Address current_address = reinterpret_cast<Address>(current);
+ isolate->heap()->RecordWrite(
+ current_object_address,
+ static_cast<int>(current_address - current_object_address));
+ }
+ current++;
+ break;
+ }
+
+ // Deserialize raw data of fixed length from 1 to 32 words.
+ STATIC_ASSERT(kNumberOfFixedRawData == 32);
+ SIXTEEN_CASES(kFixedRawData)
+ SIXTEEN_CASES(kFixedRawData + 16) {
+ byte* raw_data_out = reinterpret_cast<byte*>(current);
+ int size_in_bytes = (data - kFixedRawDataStart) << kPointerSizeLog2;
+ source_.CopyRaw(raw_data_out, size_in_bytes);
+ current = reinterpret_cast<Object**>(raw_data_out + size_in_bytes);
+ break;
+ }
+
+ STATIC_ASSERT(kNumberOfFixedRepeat == 16);
+ SIXTEEN_CASES(kFixedRepeat) {
+ int repeats = data - kFixedRepeatStart;
+ Object* object;
+ UnalignedCopy(&object, current - 1);
+ DCHECK(!isolate->heap()->InNewSpace(object));
+ for (int i = 0; i < repeats; i++) UnalignedCopy(current++, &object);
+ break;
+ }
+
+#undef SIXTEEN_CASES
+#undef FOUR_CASES
+
+ default:
+ CHECK(false);
+ }
+ }
+ CHECK_EQ(limit, current);
+}
+
+
+Serializer::Serializer(Isolate* isolate, SnapshotByteSink* sink)
+ : isolate_(isolate),
+ sink_(sink),
+ external_reference_encoder_(isolate),
+ root_index_map_(isolate),
+ code_address_map_(NULL),
+ large_objects_total_size_(0),
+ seen_large_objects_index_(0) {
+ // The serializer is meant to be used only to generate initial heap images
+ // from a context in which there is only one isolate.
+ for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) {
+ pending_chunk_[i] = 0;
+ max_chunk_size_[i] = static_cast<uint32_t>(
+ MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(i)));
+ }
+}
+
+
+Serializer::~Serializer() {
+ if (code_address_map_ != NULL) delete code_address_map_;
+}
+
+
+void StartupSerializer::SerializeStrongReferences() {
+ Isolate* isolate = this->isolate();
+ // No active threads.
+ CHECK_NULL(isolate->thread_manager()->FirstThreadStateInUse());
+ // No active or weak handles.
+ CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
+ CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
+ CHECK_EQ(0, isolate->eternal_handles()->NumberOfHandles());
+ // We don't support serializing installed extensions.
+ CHECK(!isolate->has_installed_extensions());
+ isolate->heap()->IterateSmiRoots(this);
+ isolate->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
+}
+
+
+void StartupSerializer::VisitPointers(Object** start, Object** end) {
+ for (Object** current = start; current < end; current++) {
+ if (start == isolate()->heap()->roots_array_start()) {
+ root_index_wave_front_ =
+ Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
+ }
+ if (ShouldBeSkipped(current)) {
+ sink_->Put(kSkip, "Skip");
+ sink_->PutInt(kPointerSize, "SkipOneWord");
+ } else if ((*current)->IsSmi()) {
+ sink_->Put(kOnePointerRawData, "Smi");
+ for (int i = 0; i < kPointerSize; i++) {
+ sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
+ }
+ } else {
+ SerializeObject(HeapObject::cast(*current), kPlain, kStartOfObject, 0);
+ }
+ }
+}
+
+
+void PartialSerializer::Serialize(Object** o) {
+ if ((*o)->IsContext()) {
+ Context* context = Context::cast(*o);
+ global_object_ = context->global_object();
+ back_reference_map()->AddGlobalProxy(context->global_proxy());
+ }
+ VisitPointer(o);
+ SerializeOutdatedContextsAsFixedArray();
+ Pad();
+}
+
+
+void PartialSerializer::SerializeOutdatedContextsAsFixedArray() {
+ int length = outdated_contexts_.length();
+ if (length == 0) {
+ FixedArray* empty = isolate_->heap()->empty_fixed_array();
+ SerializeObject(empty, kPlain, kStartOfObject, 0);
+ } else {
+ // Serialize an imaginary fixed array containing outdated contexts.
+ int size = FixedArray::SizeFor(length);
+ Allocate(NEW_SPACE, size);
+ sink_->Put(kNewObject + NEW_SPACE, "emulated FixedArray");
+ sink_->PutInt(size >> kObjectAlignmentBits, "FixedArray size in words");
+ Map* map = isolate_->heap()->fixed_array_map();
+ SerializeObject(map, kPlain, kStartOfObject, 0);
+ Smi* length_smi = Smi::FromInt(length);
+ sink_->Put(kOnePointerRawData, "Smi");
+ for (int i = 0; i < kPointerSize; i++) {
+ sink_->Put(reinterpret_cast<byte*>(&length_smi)[i], "Byte");
+ }
+ for (int i = 0; i < length; i++) {
+ BackReference back_ref = outdated_contexts_[i];
+ DCHECK(BackReferenceIsAlreadyAllocated(back_ref));
+ sink_->Put(kBackref + back_ref.space(), "BackRef");
+ sink_->PutInt(back_ref.reference(), "BackRefValue");
+ }
+ }
+}
+
+
+bool Serializer::ShouldBeSkipped(Object** current) {
+ Object** roots = isolate()->heap()->roots_array_start();
+ return current == &roots[Heap::kStoreBufferTopRootIndex]
+ || current == &roots[Heap::kStackLimitRootIndex]
+ || current == &roots[Heap::kRealStackLimitRootIndex];
+}
+
+
+void Serializer::VisitPointers(Object** start, Object** end) {
+ for (Object** current = start; current < end; current++) {
+ if ((*current)->IsSmi()) {
+ sink_->Put(kOnePointerRawData, "Smi");
+ for (int i = 0; i < kPointerSize; i++) {
+ sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
+ }
+ } else {
+ SerializeObject(HeapObject::cast(*current), kPlain, kStartOfObject, 0);
+ }
+ }
+}
+
+
+void Serializer::EncodeReservations(
+ List<SerializedData::Reservation>* out) const {
+ for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) {
+ for (int j = 0; j < completed_chunks_[i].length(); j++) {
+ out->Add(SerializedData::Reservation(completed_chunks_[i][j]));
+ }
+
+ if (pending_chunk_[i] > 0 || completed_chunks_[i].length() == 0) {
+ out->Add(SerializedData::Reservation(pending_chunk_[i]));
+ }
+ out->last().mark_as_last();
+ }
+
+ out->Add(SerializedData::Reservation(large_objects_total_size_));
+ out->last().mark_as_last();
+}
+
+
+// This ensures that the partial snapshot cache keeps things alive during GC and
+// tracks their movement. When it is called during serialization of the startup
+// snapshot nothing happens. When the partial (context) snapshot is created,
+// this array is populated with the pointers that the partial snapshot will
+// need. As that happens we emit serialized objects to the startup snapshot
+// that correspond to the elements of this cache array. On deserialization we
+// therefore need to visit the cache array. This fills it up with pointers to
+// deserialized objects.
+void SerializerDeserializer::Iterate(Isolate* isolate,
+ ObjectVisitor* visitor) {
+ if (isolate->serializer_enabled()) return;
+ List<Object*>* cache = isolate->partial_snapshot_cache();
+ for (int i = 0;; ++i) {
+ // Extend the array ready to get a value when deserializing.
+ if (cache->length() <= i) cache->Add(Smi::FromInt(0));
+ visitor->VisitPointer(&cache->at(i));
+ // Sentinel is the undefined object, which is a root so it will not normally
+ // be found in the cache.
+ if (cache->at(i)->IsUndefined()) break;
+ }
+}
+
+
+int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
+ Isolate* isolate = this->isolate();
+ List<Object*>* cache = isolate->partial_snapshot_cache();
+ int new_index = cache->length();
+
+ int index = partial_cache_index_map_.LookupOrInsert(heap_object, new_index);
+ if (index == PartialCacheIndexMap::kInvalidIndex) {
+ // We didn't find the object in the cache. So we add it to the cache and
+ // then visit the pointer so that it becomes part of the startup snapshot
+ // and we can refer to it from the partial snapshot.
+ cache->Add(heap_object);
+ startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object));
+ // We don't recurse from the startup snapshot generator into the partial
+ // snapshot generator.
+ return new_index;
+ }
+ return index;
+}
+
+
+#ifdef DEBUG
+bool Serializer::BackReferenceIsAlreadyAllocated(BackReference reference) {
+ DCHECK(reference.is_valid());
+ DCHECK(!reference.is_source());
+ DCHECK(!reference.is_global_proxy());
+ AllocationSpace space = reference.space();
+ int chunk_index = reference.chunk_index();
+ if (space == LO_SPACE) {
+ return chunk_index == 0 &&
+ reference.large_object_index() < seen_large_objects_index_;
+ } else if (chunk_index == completed_chunks_[space].length()) {
+ return reference.chunk_offset() < pending_chunk_[space];
+ } else {
+ return chunk_index < completed_chunks_[space].length() &&
+ reference.chunk_offset() < completed_chunks_[space][chunk_index];
+ }
+}
+#endif // DEBUG
+
+
+bool Serializer::SerializeKnownObject(HeapObject* obj, HowToCode how_to_code,
+ WhereToPoint where_to_point, int skip) {
+ if (how_to_code == kPlain && where_to_point == kStartOfObject) {
+ // Encode a reference to a hot object by its index in the working set.
+ int index = hot_objects_.Find(obj);
+ if (index != HotObjectsList::kNotFound) {
+ DCHECK(index >= 0 && index < kNumberOfHotObjects);
+ if (FLAG_trace_serializer) {
+ PrintF(" Encoding hot object %d:", index);
+ obj->ShortPrint();
+ PrintF("\n");
+ }
+ if (skip != 0) {
+ sink_->Put(kHotObjectWithSkip + index, "HotObjectWithSkip");
+ sink_->PutInt(skip, "HotObjectSkipDistance");
+ } else {
+ sink_->Put(kHotObject + index, "HotObject");
+ }
+ return true;
+ }
+ }
+ BackReference back_reference = back_reference_map_.Lookup(obj);
+ if (back_reference.is_valid()) {
+ // Encode the location of an already deserialized object in order to write
+ // its location into a later object. We can encode the location as an
+ // offset fromthe start of the deserialized objects or as an offset
+ // backwards from thecurrent allocation pointer.
+ if (back_reference.is_source()) {
+ FlushSkip(skip);
+ if (FLAG_trace_serializer) PrintF(" Encoding source object\n");
+ DCHECK(how_to_code == kPlain && where_to_point == kStartOfObject);
+ sink_->Put(kAttachedReference + kPlain + kStartOfObject, "Source");
+ sink_->PutInt(kSourceObjectReference, "kSourceObjectReference");
+ } else if (back_reference.is_global_proxy()) {
+ FlushSkip(skip);
+ if (FLAG_trace_serializer) PrintF(" Encoding global proxy\n");
+ DCHECK(how_to_code == kPlain && where_to_point == kStartOfObject);
+ sink_->Put(kAttachedReference + kPlain + kStartOfObject, "Global Proxy");
+ sink_->PutInt(kGlobalProxyReference, "kGlobalProxyReference");
+ } else {
+ if (FLAG_trace_serializer) {
+ PrintF(" Encoding back reference to: ");
+ obj->ShortPrint();
+ PrintF("\n");
+ }
+
+ AllocationSpace space = back_reference.space();
+ if (skip == 0) {
+ sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRef");
+ } else {
+ sink_->Put(kBackrefWithSkip + how_to_code + where_to_point + space,
+ "BackRefWithSkip");
+ sink_->PutInt(skip, "BackRefSkipDistance");
+ }
+ DCHECK(BackReferenceIsAlreadyAllocated(back_reference));
+ sink_->PutInt(back_reference.reference(), "BackRefValue");
+
+ hot_objects_.Add(obj);
+ }
+ return true;
+ }
+ return false;
+}
+
+
+void StartupSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
+ WhereToPoint where_to_point, int skip) {
+ DCHECK(!obj->IsJSFunction());
+
+ int root_index = root_index_map_.Lookup(obj);
+ // We can only encode roots as such if it has already been serialized.
+ // That applies to root indices below the wave front.
+ if (root_index != RootIndexMap::kInvalidRootIndex &&
+ root_index < root_index_wave_front_) {
+ PutRoot(root_index, obj, how_to_code, where_to_point, skip);
+ return;
+ }
+
+ if (obj->IsCode() && Code::cast(obj)->kind() == Code::FUNCTION) {
+ obj = isolate()->builtins()->builtin(Builtins::kCompileLazy);
+ }
+
+ if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return;
+
+ FlushSkip(skip);
+
+ // Object has not yet been serialized. Serialize it here.
+ ObjectSerializer object_serializer(this, obj, sink_, how_to_code,
+ where_to_point);
+ object_serializer.Serialize();
+}
+
+
+void StartupSerializer::SerializeWeakReferences() {
+ // This phase comes right after the serialization (of the snapshot).
+ // After we have done the partial serialization the partial snapshot cache
+ // will contain some references needed to decode the partial snapshot. We
+ // add one entry with 'undefined' which is the sentinel that the deserializer
+ // uses to know it is done deserializing the array.
+ Object* undefined = isolate()->heap()->undefined_value();
+ VisitPointer(&undefined);
+ isolate()->heap()->IterateWeakRoots(this, VISIT_ALL);
+ Pad();
+}
+
+
+void Serializer::PutRoot(int root_index,
+ HeapObject* object,
+ SerializerDeserializer::HowToCode how_to_code,
+ SerializerDeserializer::WhereToPoint where_to_point,
+ int skip) {
+ if (FLAG_trace_serializer) {
+ PrintF(" Encoding root %d:", root_index);
+ object->ShortPrint();
+ PrintF("\n");
+ }
+
+ if (how_to_code == kPlain && where_to_point == kStartOfObject &&
+ root_index < kNumberOfRootArrayConstants &&
+ !isolate()->heap()->InNewSpace(object)) {
+ if (skip == 0) {
+ sink_->Put(kRootArrayConstants + root_index, "RootConstant");
+ } else {
+ sink_->Put(kRootArrayConstantsWithSkip + root_index, "RootConstant");
+ sink_->PutInt(skip, "SkipInPutRoot");
+ }
+ } else {
+ FlushSkip(skip);
+ sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
+ sink_->PutInt(root_index, "root_index");
+ }
+}
+
+
+void PartialSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
+ WhereToPoint where_to_point, int skip) {
+ if (obj->IsMap()) {
+ // The code-caches link to context-specific code objects, which
+ // the startup and context serializes cannot currently handle.
+ DCHECK(Map::cast(obj)->code_cache() == obj->GetHeap()->empty_fixed_array());
+ }
+
+ // Replace typed arrays by undefined.
+ if (obj->IsJSTypedArray()) obj = isolate_->heap()->undefined_value();
+
+ 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 (ShouldBeInThePartialSnapshotCache(obj)) {
+ FlushSkip(skip);
+
+ int cache_index = PartialSnapshotCacheIndex(obj);
+ sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
+ "PartialSnapshotCache");
+ sink_->PutInt(cache_index, "partial_snapshot_cache_index");
+ return;
+ }
+
+ // Pointers from the partial snapshot to the objects in the startup snapshot
+ // should go through the root array or through the partial snapshot cache.
+ // If this is not the case you may have to add something to the root array.
+ DCHECK(!startup_serializer_->back_reference_map()->Lookup(obj).is_valid());
+ // All the internalized strings that the partial snapshot needs should be
+ // either in the root table or in the partial snapshot cache.
+ DCHECK(!obj->IsInternalizedString());
+
+ if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return;
+
+ FlushSkip(skip);
+
+ // Object has not yet been serialized. Serialize it here.
+ ObjectSerializer serializer(this, obj, sink_, how_to_code, where_to_point);
+ serializer.Serialize();
+
+ if (obj->IsContext() &&
+ Context::cast(obj)->global_object() == global_object_) {
+ // Context refers to the current global object. This reference will
+ // become outdated after deserialization.
+ BackReference back_reference = back_reference_map_.Lookup(obj);
+ DCHECK(back_reference.is_valid());
+ outdated_contexts_.Add(back_reference);
+ }
+}
+
+
+void Serializer::ObjectSerializer::SerializePrologue(AllocationSpace space,
+ int size, Map* map) {
+ if (serializer_->code_address_map_) {
+ const char* code_name =
+ serializer_->code_address_map_->Lookup(object_->address());
+ LOG(serializer_->isolate_,
+ CodeNameEvent(object_->address(), sink_->Position(), code_name));
+ LOG(serializer_->isolate_,
+ SnapshotPositionEvent(object_->address(), sink_->Position()));
+ }
+
+ BackReference back_reference;
+ if (space == LO_SPACE) {
+ sink_->Put(kNewObject + reference_representation_ + space,
+ "NewLargeObject");
+ sink_->PutInt(size >> kObjectAlignmentBits, "ObjectSizeInWords");
+ if (object_->IsCode()) {
+ sink_->Put(EXECUTABLE, "executable large object");
+ } else {
+ sink_->Put(NOT_EXECUTABLE, "not executable large object");
+ }
+ back_reference = serializer_->AllocateLargeObject(size);
+ } else {
+ bool needs_double_align = false;
+ if (object_->NeedsToEnsureDoubleAlignment()) {
+ // Add wriggle room for double alignment padding.
+ back_reference = serializer_->Allocate(space, size + kPointerSize);
+ needs_double_align = true;
+ } else {
+ back_reference = serializer_->Allocate(space, size);
+ }
+ sink_->Put(kNewObject + reference_representation_ + space, "NewObject");
+ if (needs_double_align)
+ sink_->PutInt(kDoubleAlignmentSentinel, "DoubleAlignSentinel");
+ int encoded_size = size >> kObjectAlignmentBits;
+ DCHECK_NE(kDoubleAlignmentSentinel, encoded_size);
+ sink_->PutInt(encoded_size, "ObjectSizeInWords");
+ }
+
+ // Mark this object as already serialized.
+ serializer_->back_reference_map()->Add(object_, back_reference);
+
+ // Serialize the map (first word of the object).
+ serializer_->SerializeObject(map, kPlain, kStartOfObject, 0);
+}
+
+
+void Serializer::ObjectSerializer::SerializeExternalString() {
+ // Instead of serializing this as an external string, we serialize
+ // an imaginary sequential string with the same content.
+ Isolate* isolate = serializer_->isolate();
+ DCHECK(object_->IsExternalString());
+ DCHECK(object_->map() != isolate->heap()->native_source_string_map());
+ ExternalString* string = ExternalString::cast(object_);
+ int length = string->length();
+ Map* map;
+ int content_size;
+ int allocation_size;
+ const byte* resource;
+ // Find the map and size for the imaginary sequential string.
+ bool internalized = object_->IsInternalizedString();
+ if (object_->IsExternalOneByteString()) {
+ map = internalized ? isolate->heap()->one_byte_internalized_string_map()
+ : isolate->heap()->one_byte_string_map();
+ allocation_size = SeqOneByteString::SizeFor(length);
+ content_size = length * kCharSize;
+ resource = reinterpret_cast<const byte*>(
+ ExternalOneByteString::cast(string)->resource()->data());
+ } else {
+ map = internalized ? isolate->heap()->internalized_string_map()
+ : isolate->heap()->string_map();
+ allocation_size = SeqTwoByteString::SizeFor(length);
+ content_size = length * kShortSize;
+ resource = reinterpret_cast<const byte*>(
+ ExternalTwoByteString::cast(string)->resource()->data());
+ }
+
+ AllocationSpace space = (allocation_size > Page::kMaxRegularHeapObjectSize)
+ ? LO_SPACE
+ : OLD_DATA_SPACE;
+ SerializePrologue(space, allocation_size, map);
+
+ // Output the rest of the imaginary string.
+ int bytes_to_output = allocation_size - HeapObject::kHeaderSize;
+
+ // Output raw data header. Do not bother with common raw length cases here.
+ sink_->Put(kVariableRawData, "RawDataForString");
+ sink_->PutInt(bytes_to_output, "length");
+
+ // Serialize string header (except for map).
+ Address string_start = string->address();
+ for (int i = HeapObject::kHeaderSize; i < SeqString::kHeaderSize; i++) {
+ sink_->PutSection(string_start[i], "StringHeader");
+ }
+
+ // Serialize string content.
+ sink_->PutRaw(resource, content_size, "StringContent");
+
+ // Since the allocation size is rounded up to object alignment, there
+ // maybe left-over bytes that need to be padded.
+ int padding_size = allocation_size - SeqString::kHeaderSize - content_size;
+ DCHECK(0 <= padding_size && padding_size < kObjectAlignment);
+ for (int i = 0; i < padding_size; i++) sink_->PutSection(0, "StringPadding");
+
+ sink_->Put(kSkip, "SkipAfterString");
+ sink_->PutInt(bytes_to_output, "SkipDistance");
+}
+
+
+void Serializer::ObjectSerializer::Serialize() {
+ if (FLAG_trace_serializer) {
+ PrintF(" Encoding heap object: ");
+ object_->ShortPrint();
+ PrintF("\n");
+ }
+
+ // We cannot serialize typed array objects correctly.
+ DCHECK(!object_->IsJSTypedArray());
+
+ if (object_->IsScript()) {
+ // Clear cached line ends.
+ Object* undefined = serializer_->isolate()->heap()->undefined_value();
+ Script::cast(object_)->set_line_ends(undefined);
+ }
+
+ if (object_->IsExternalString()) {
+ Heap* heap = serializer_->isolate()->heap();
+ if (object_->map() != heap->native_source_string_map()) {
+ // Usually we cannot recreate resources for external strings. To work
+ // around this, external strings are serialized to look like ordinary
+ // sequential strings.
+ // The exception are native source code strings, since we can recreate
+ // their resources. In that case we fall through and leave it to
+ // VisitExternalOneByteString further down.
+ SerializeExternalString();
+ return;
+ }
+ }
+
+ int size = object_->Size();
+ Map* map = object_->map();
+ AllocationSpace space =
+ MemoryChunk::FromAddress(object_->address())->owner()->identity();
+ SerializePrologue(space, size, map);
+
+ // Serialize the rest of the object.
+ CHECK_EQ(0, bytes_processed_so_far_);
+ bytes_processed_so_far_ = kPointerSize;
+
+ object_->IterateBody(map->instance_type(), size, this);
+ OutputRawData(object_->address() + size);
+}
+
+
+void Serializer::ObjectSerializer::VisitPointers(Object** start,
+ Object** end) {
+ Object** current = start;
+ while (current < end) {
+ while (current < end && (*current)->IsSmi()) current++;
+ if (current < end) OutputRawData(reinterpret_cast<Address>(current));
+
+ while (current < end && !(*current)->IsSmi()) {
+ HeapObject* current_contents = HeapObject::cast(*current);
+ int root_index = serializer_->root_index_map()->Lookup(current_contents);
+ // Repeats are not subject to the write barrier so we can only use
+ // immortal immovable root members. They are never in new space.
+ if (current != start && root_index != RootIndexMap::kInvalidRootIndex &&
+ Heap::RootIsImmortalImmovable(root_index) &&
+ current_contents == current[-1]) {
+ DCHECK(!serializer_->isolate()->heap()->InNewSpace(current_contents));
+ int repeat_count = 1;
+ while (¤t[repeat_count] < end - 1 &&
+ current[repeat_count] == current_contents) {
+ repeat_count++;
+ }
+ current += repeat_count;
+ bytes_processed_so_far_ += repeat_count * kPointerSize;
+ if (repeat_count > kNumberOfFixedRepeat) {
+ sink_->Put(kVariableRepeat, "VariableRepeat");
+ sink_->PutInt(repeat_count, "repeat count");
+ } else {
+ sink_->Put(kFixedRepeatStart + repeat_count, "FixedRepeat");
+ }
+ } else {
+ serializer_->SerializeObject(
+ current_contents, kPlain, kStartOfObject, 0);
+ bytes_processed_so_far_ += kPointerSize;
+ current++;
+ }
+ }
+ }
+}
+
+
+void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
+ // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
+ if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
+
+ int skip = OutputRawData(rinfo->target_address_address(),
+ kCanReturnSkipInsteadOfSkipping);
+ HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
+ Object* object = rinfo->target_object();
+ serializer_->SerializeObject(HeapObject::cast(object), how_to_code,
+ kStartOfObject, skip);
+ bytes_processed_so_far_ += rinfo->target_address_size();
+}
+
+
+void Serializer::ObjectSerializer::VisitExternalReference(Address* p) {
+ int skip = OutputRawData(reinterpret_cast<Address>(p),
+ kCanReturnSkipInsteadOfSkipping);
+ sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
+ sink_->PutInt(skip, "SkipB4ExternalRef");
+ Address target = *p;
+ sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
+ bytes_processed_so_far_ += kPointerSize;
+}
+
+
+void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
+ int skip = OutputRawData(rinfo->target_address_address(),
+ kCanReturnSkipInsteadOfSkipping);
+ HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
+ sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
+ sink_->PutInt(skip, "SkipB4ExternalRef");
+ Address target = rinfo->target_external_reference();
+ sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
+ bytes_processed_so_far_ += rinfo->target_address_size();
+}
+
+
+void Serializer::ObjectSerializer::VisitInternalReference(RelocInfo* rinfo) {
+ // We can only reference to internal references of code that has been output.
+ DCHECK(is_code_object_ && code_has_been_output_);
+ // We do not use skip from last patched pc to find the pc to patch, since
+ // target_address_address may not return addresses in ascending order when
+ // used for internal references. External references may be stored at the
+ // end of the code in the constant pool, whereas internal references are
+ // inline. That would cause the skip to be negative. Instead, we store the
+ // offset from code entry.
+ Address entry = Code::cast(object_)->entry();
+ intptr_t pc_offset = rinfo->target_internal_reference_address() - entry;
+ intptr_t target_offset = rinfo->target_internal_reference() - entry;
+ DCHECK(0 <= pc_offset &&
+ pc_offset <= Code::cast(object_)->instruction_size());
+ DCHECK(0 <= target_offset &&
+ target_offset <= Code::cast(object_)->instruction_size());
+ sink_->Put(rinfo->rmode() == RelocInfo::INTERNAL_REFERENCE
+ ? kInternalReference
+ : kInternalReferenceEncoded,
+ "InternalRef");
+ sink_->PutInt(static_cast<uintptr_t>(pc_offset), "internal ref address");
+ sink_->PutInt(static_cast<uintptr_t>(target_offset), "internal ref value");
+}
+
+
+void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
+ int skip = OutputRawData(rinfo->target_address_address(),
+ kCanReturnSkipInsteadOfSkipping);
+ HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
+ sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
+ sink_->PutInt(skip, "SkipB4ExternalRef");
+ Address target = rinfo->target_address();
+ sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
+ bytes_processed_so_far_ += rinfo->target_address_size();
+}
+
+
+void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
+ // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
+ if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
+
+ int skip = OutputRawData(rinfo->target_address_address(),
+ kCanReturnSkipInsteadOfSkipping);
+ Code* object = Code::GetCodeFromTargetAddress(rinfo->target_address());
+ serializer_->SerializeObject(object, kFromCode, kInnerPointer, skip);
+ bytes_processed_so_far_ += rinfo->target_address_size();
+}
+
+
+void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
+ int skip = OutputRawData(entry_address, kCanReturnSkipInsteadOfSkipping);
+ Code* object = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
+ serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
+ bytes_processed_so_far_ += kPointerSize;
+}
+
+
+void Serializer::ObjectSerializer::VisitCell(RelocInfo* rinfo) {
+ // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
+ if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
+
+ int skip = OutputRawData(rinfo->pc(), kCanReturnSkipInsteadOfSkipping);
+ Cell* object = Cell::cast(rinfo->target_cell());
+ serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
+ bytes_processed_so_far_ += kPointerSize;
+}
+
+
+void Serializer::ObjectSerializer::VisitExternalOneByteString(
+ v8::String::ExternalOneByteStringResource** resource_pointer) {
+ Address references_start = reinterpret_cast<Address>(resource_pointer);
+ OutputRawData(references_start);
+ for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
+ Object* source =
+ serializer_->isolate()->heap()->natives_source_cache()->get(i);
+ if (!source->IsUndefined()) {
+ ExternalOneByteString* string = ExternalOneByteString::cast(source);
+ typedef v8::String::ExternalOneByteStringResource Resource;
+ const Resource* resource = string->resource();
+ if (resource == *resource_pointer) {
+ sink_->Put(kNativesStringResource, "NativesStringResource");
+ sink_->PutSection(i, "NativesStringResourceEnd");
+ bytes_processed_so_far_ += sizeof(resource);
+ return;
+ }
+ }
+ }
+ // One of the strings in the natives cache should match the resource. We
+ // don't expect any other kinds of external strings here.
+ UNREACHABLE();
+}
+
+
+Address Serializer::ObjectSerializer::PrepareCode() {
+ // To make snapshots reproducible, we make a copy of the code object
+ // and wipe all pointers in the copy, which we then serialize.
+ Code* original = Code::cast(object_);
+ Code* code = serializer_->CopyCode(original);
+ // Code age headers are not serializable.
+ code->MakeYoung(serializer_->isolate());
+ int mode_mask = RelocInfo::kCodeTargetMask |
+ RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
+ RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE) |
+ RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) |
+ RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) |
+ RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE_ENCODED);
+ for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
+ RelocInfo* rinfo = it.rinfo();
+ if (!(FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool())) {
+ rinfo->WipeOut();
+ }
+ }
+ // We need to wipe out the header fields *after* wiping out the
+ // relocations, because some of these fields are needed for the latter.
+ code->WipeOutHeader();
+ return code->address();
+}
+
+
+int Serializer::ObjectSerializer::OutputRawData(
+ Address up_to, Serializer::ObjectSerializer::ReturnSkip return_skip) {
+ Address object_start = object_->address();
+ int base = bytes_processed_so_far_;
+ int up_to_offset = static_cast<int>(up_to - object_start);
+ int to_skip = up_to_offset - bytes_processed_so_far_;
+ int bytes_to_output = to_skip;
+ bytes_processed_so_far_ += to_skip;
+ // This assert will fail if the reloc info gives us the target_address_address
+ // locations in a non-ascending order. Luckily that doesn't happen.
+ DCHECK(to_skip >= 0);
+ bool outputting_code = false;
+ if (to_skip != 0 && is_code_object_ && !code_has_been_output_) {
+ // Output the code all at once and fix later.
+ bytes_to_output = object_->Size() + to_skip - bytes_processed_so_far_;
+ outputting_code = true;
+ code_has_been_output_ = true;
+ }
+ if (bytes_to_output != 0 && (!is_code_object_ || outputting_code)) {
+ if (!outputting_code && bytes_to_output == to_skip &&
+ IsAligned(bytes_to_output, kPointerAlignment) &&
+ bytes_to_output <= kNumberOfFixedRawData * kPointerSize) {
+ int size_in_words = bytes_to_output >> kPointerSizeLog2;
+ sink_->PutSection(kFixedRawDataStart + size_in_words, "FixedRawData");
+ to_skip = 0; // This instruction includes skip.
+ } else {
+ // We always end up here if we are outputting the code of a code object.
+ sink_->Put(kVariableRawData, "VariableRawData");
+ sink_->PutInt(bytes_to_output, "length");
+ }
+
+ if (is_code_object_) object_start = PrepareCode();
+
+ const char* description = is_code_object_ ? "Code" : "Byte";
+#ifdef MEMORY_SANITIZER
+ // Object sizes are usually rounded up with uninitialized padding space.
+ MSAN_MEMORY_IS_INITIALIZED(object_start + base, bytes_to_output);
+#endif // MEMORY_SANITIZER
+ sink_->PutRaw(object_start + base, bytes_to_output, description);
+ }
+ if (to_skip != 0 && return_skip == kIgnoringReturn) {
+ sink_->Put(kSkip, "Skip");
+ sink_->PutInt(to_skip, "SkipDistance");
+ to_skip = 0;
+ }
+ return to_skip;
+}
+
+
+BackReference Serializer::AllocateLargeObject(int size) {
+ // Large objects are allocated one-by-one when deserializing. We do not
+ // have to keep track of multiple chunks.
+ large_objects_total_size_ += size;
+ return BackReference::LargeObjectReference(seen_large_objects_index_++);
+}
+
+
+BackReference Serializer::Allocate(AllocationSpace space, int size) {
+ DCHECK(space >= 0 && space < kNumberOfPreallocatedSpaces);
+ DCHECK(size > 0 && size <= static_cast<int>(max_chunk_size(space)));
+ uint32_t new_chunk_size = pending_chunk_[space] + size;
+ if (new_chunk_size > max_chunk_size(space)) {
+ // The new chunk size would not fit onto a single page. Complete the
+ // current chunk and start a new one.
+ sink_->Put(kNextChunk, "NextChunk");
+ sink_->Put(space, "NextChunkSpace");
+ completed_chunks_[space].Add(pending_chunk_[space]);
+ DCHECK_LE(completed_chunks_[space].length(), BackReference::kMaxChunkIndex);
+ pending_chunk_[space] = 0;
+ new_chunk_size = size;
+ }
+ uint32_t offset = pending_chunk_[space];
+ pending_chunk_[space] = new_chunk_size;
+ return BackReference::Reference(space, completed_chunks_[space].length(),
+ offset);
+}
+
+
+void Serializer::Pad() {
+ // The non-branching GetInt will read up to 3 bytes too far, so we need
+ // to pad the snapshot to make sure we don't read over the end.
+ for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) {
+ sink_->Put(kNop, "Padding");
+ }
+ // Pad up to pointer size for checksum.
+ while (!IsAligned(sink_->Position(), kPointerAlignment)) {
+ sink_->Put(kNop, "Padding");
+ }
+}
+
+
+void Serializer::InitializeCodeAddressMap() {
+ isolate_->InitializeLoggingAndCounters();
+ code_address_map_ = new CodeAddressMap(isolate_);
+}
+
+
+Code* Serializer::CopyCode(Code* code) {
+ code_buffer_.Rewind(0); // Clear buffer without deleting backing store.
+ int size = code->CodeSize();
+ code_buffer_.AddAll(Vector<byte>(code->address(), size));
+ return Code::cast(HeapObject::FromAddress(&code_buffer_.first()));
+}
+
+
+ScriptData* CodeSerializer::Serialize(Isolate* isolate,
+ Handle<SharedFunctionInfo> info,
+ Handle<String> 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, info->code());
+ DisallowHeapAllocation no_gc;
+ Object** location = Handle<Object>::cast(info).location();
+ cs.VisitPointer(location);
+ 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());
+ // Only serialize the code for the toplevel function unless specified
+ // by flag. Replace code of inner functions by the lazy compile builtin.
+ // This is safe, as checked in Compiler::BuildFunctionInfo.
+ if (code_object != main_code_ && !FLAG_serialize_inner) {
+ SerializeBuiltin(Builtins::kCompileLazy, how_to_code, where_to_point);
+ } else {
+ SerializeGeneric(code_object, how_to_code, where_to_point);
+ }
+ return;
+ }
+ 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->IsGlobalObject());
+ // 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) {
+ if (heap_object->IsInternalizedString()) num_internalized_strings_++;
+
+ // 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), false),
+ 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<Builtins::Name>(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<SharedFunctionInfo> CodeSerializer::Deserialize(
+ Isolate* isolate, ScriptData* cached_data, Handle<String> source) {
+ base::ElapsedTimer timer;
+ if (FLAG_profile_deserialization) timer.Start();
+
+ HandleScope scope(isolate);
+
+ SmartPointer<SerializedCodeData> 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<SharedFunctionInfo>();
+ }
+
+ // Eagerly expand string table to avoid allocations during deserialization.
+ StringTable::EnsureCapacityForDeserialization(isolate,
+ scd->NumInternalizedStrings());
+
+ // Prepare and register list of attached objects.
+ Vector<const uint32_t> code_stub_keys = scd->CodeStubKeys();
+ Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::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<SharedFunctionInfo> 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<SharedFunctionInfo>();
+ }
+ deserializer.FlushICacheForNewCodeObjects();
+
+ 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->code(),
+ *result, NULL, name);
+ }
+ return scope.CloseAndEscape(result);
+}
+
+
+void SerializedData::AllocateData(int size) {
+ DCHECK(!owns_data_);
+ data_ = NewArray<byte>(size);
+ size_ = size;
+ owns_data_ = true;
+ DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment));
+}
+
+
+SnapshotData::SnapshotData(const Serializer& ser) {
+ DisallowHeapAllocation no_gc;
+ List<Reservation> reservations;
+ ser.EncodeReservations(&reservations);
+ const List<byte>& payload = ser.sink()->data();
+
+ // Calculate sizes.
+ int reservation_size = reservations.length() * kInt32Size;
+ int size = kHeaderSize + reservation_size + payload.length();
+
+ // Allocate backing store and create result data.
+ AllocateData(size);
+
+ // Set header values.
+ SetMagicNumber(ser.isolate());
+ SetHeaderValue(kCheckSumOffset, Version::Hash());
+ SetHeaderValue(kNumReservationsOffset, reservations.length());
+ SetHeaderValue(kPayloadLengthOffset, payload.length());
+
+ // Copy reservation chunk sizes.
+ CopyBytes(data_ + kHeaderSize, reinterpret_cast<byte*>(reservations.begin()),
+ reservation_size);
+
+ // Copy serialized data.
+ CopyBytes(data_ + kHeaderSize + reservation_size, payload.begin(),
+ static_cast<size_t>(payload.length()));
+}
+
+
+bool SnapshotData::IsSane() {
+ return GetHeaderValue(kCheckSumOffset) == Version::Hash();
+}
+
+
+Vector<const SerializedData::Reservation> SnapshotData::Reservations() const {
+ return Vector<const Reservation>(
+ reinterpret_cast<const Reservation*>(data_ + kHeaderSize),
+ GetHeaderValue(kNumReservationsOffset));
+}
+
+
+Vector<const byte> SnapshotData::Payload() const {
+ int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
+ const byte* payload = data_ + kHeaderSize + reservations_size;
+ int length = GetHeaderValue(kPayloadLengthOffset);
+ DCHECK_EQ(data_ + size_, payload + length);
+ return Vector<const byte>(payload, length);
+}
+
+
+class Checksum {
+ public:
+ explicit Checksum(Vector<const byte> payload) {
+ // 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<const uintptr_t*>(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<uint32_t>(a);
+ b_ = static_cast<uint32_t>(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<byte>& payload,
+ const CodeSerializer& cs) {
+ DisallowHeapAllocation no_gc;
+ const List<uint32_t>* stub_keys = cs.stub_keys();
+
+ List<Reservation> 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<uint32_t>(CpuFeatures::SupportedFeatures()));
+ SetHeaderValue(kFlagHashOffset, FlagList::Hash());
+ SetHeaderValue(kNumInternalizedStringsOffset, cs.num_internalized_strings());
+ 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<byte*>(reservations.begin()),
+ reservation_size);
+
+ // Copy code stub keys.
+ CopyBytes(data_ + kHeaderSize + reservation_size,
+ reinterpret_cast<byte*>(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<size_t>(payload.length()));
+}
+
+
+SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck(
+ Isolate* isolate, String* source) const {
+ uint32_t magic_number = GetMagicNumber();
+ 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 (magic_number != ComputeMagicNumber(isolate)) return MAGIC_NUMBER_MISMATCH;
+ if (version_hash != Version::Hash()) return VERSION_MISMATCH;
+ if (source_hash != SourceHash(source)) return SOURCE_MISMATCH;
+ if (cpu_features != static_cast<uint32_t>(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;
+}
+
+
+// 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<const SerializedData::Reservation> SerializedCodeData::Reservations()
+ const {
+ return Vector<const Reservation>(
+ reinterpret_cast<const Reservation*>(data_ + kHeaderSize),
+ GetHeaderValue(kNumReservationsOffset));
+}
+
+
+Vector<const byte> 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<intptr_t>(payload), kPointerAlignment));
+ int length = GetHeaderValue(kPayloadLengthOffset);
+ DCHECK_EQ(data_ + size_, payload + length);
+ return Vector<const byte>(payload, length);
+}
+
+
+int SerializedCodeData::NumInternalizedStrings() const {
+ return GetHeaderValue(kNumInternalizedStringsOffset);
+}
+
+Vector<const uint32_t> SerializedCodeData::CodeStubKeys() const {
+ int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
+ const byte* start = data_ + kHeaderSize + reservations_size;
+ return Vector<const uint32_t>(reinterpret_cast<const uint32_t*>(start),
+ GetHeaderValue(kNumCodeStubKeysOffset));
+}
+
+
+SerializedCodeData::SerializedCodeData(ScriptData* data)
+ : SerializedData(const_cast<byte*>(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 v8::internal
--- /dev/null
+// Copyright 2012 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_SERIALIZE_H_
+#define V8_SERIALIZE_H_
+
+#include "src/hashmap.h"
+#include "src/heap-profiler.h"
+#include "src/isolate.h"
+#include "src/snapshot/snapshot-source-sink.h"
+
+namespace v8 {
+namespace internal {
+
+class ScriptData;
+
+static const int kDeoptTableSerializeEntryCount = 64;
+
+// ExternalReferenceTable is a helper class that defines the relationship
+// between external references and their encodings. It is used to build
+// hashmaps in ExternalReferenceEncoder and ExternalReferenceDecoder.
+class ExternalReferenceTable {
+ public:
+ static ExternalReferenceTable* instance(Isolate* isolate);
+
+ int size() const { return refs_.length(); }
+ Address address(int i) { return refs_[i].address; }
+ const char* name(int i) { return refs_[i].name; }
+
+ inline static Address NotAvailable() { return NULL; }
+
+ private:
+ struct ExternalReferenceEntry {
+ Address address;
+ const char* name;
+ };
+
+ explicit ExternalReferenceTable(Isolate* isolate);
+
+ void Add(Address address, const char* name) {
+ ExternalReferenceEntry entry = {address, name};
+ refs_.Add(entry);
+ }
+
+ List<ExternalReferenceEntry> refs_;
+
+ DISALLOW_COPY_AND_ASSIGN(ExternalReferenceTable);
+};
+
+
+class ExternalReferenceEncoder {
+ public:
+ explicit ExternalReferenceEncoder(Isolate* isolate);
+
+ uint32_t Encode(Address key) const;
+
+ const char* NameOfAddress(Isolate* isolate, Address address) const;
+
+ private:
+ static uint32_t Hash(Address key) {
+ return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key) >>
+ kPointerSizeLog2);
+ }
+
+ HashMap* map_;
+
+ DISALLOW_COPY_AND_ASSIGN(ExternalReferenceEncoder);
+};
+
+
+class AddressMapBase {
+ protected:
+ static void SetValue(HashMap::Entry* entry, uint32_t v) {
+ entry->value = reinterpret_cast<void*>(v);
+ }
+
+ static uint32_t GetValue(HashMap::Entry* entry) {
+ return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value));
+ }
+
+ inline static HashMap::Entry* LookupEntry(HashMap* map, HeapObject* obj,
+ bool insert) {
+ return map->Lookup(Key(obj), Hash(obj), insert);
+ }
+
+ private:
+ static uint32_t Hash(HeapObject* obj) {
+ return static_cast<int32_t>(reinterpret_cast<intptr_t>(obj->address()));
+ }
+
+ static void* Key(HeapObject* obj) {
+ return reinterpret_cast<void*>(obj->address());
+ }
+};
+
+
+class RootIndexMap : public AddressMapBase {
+ public:
+ explicit RootIndexMap(Isolate* isolate);
+
+ static const int kInvalidRootIndex = -1;
+
+ int Lookup(HeapObject* obj) {
+ HashMap::Entry* entry = LookupEntry(map_, obj, false);
+ if (entry) return GetValue(entry);
+ return kInvalidRootIndex;
+ }
+
+ private:
+ HashMap* map_;
+
+ DISALLOW_COPY_AND_ASSIGN(RootIndexMap);
+};
+
+
+class PartialCacheIndexMap : public AddressMapBase {
+ public:
+ PartialCacheIndexMap() : map_(HashMap::PointersMatch) {}
+
+ static const int kInvalidIndex = -1;
+
+ // Lookup object in the map. Return its index if found, or create
+ // a new entry with new_index as value, and return kInvalidIndex.
+ int LookupOrInsert(HeapObject* obj, int new_index) {
+ HashMap::Entry* entry = LookupEntry(&map_, obj, false);
+ if (entry != NULL) return GetValue(entry);
+ SetValue(LookupEntry(&map_, obj, true), static_cast<uint32_t>(new_index));
+ return kInvalidIndex;
+ }
+
+ private:
+ HashMap map_;
+
+ DISALLOW_COPY_AND_ASSIGN(PartialCacheIndexMap);
+};
+
+
+class BackReference {
+ public:
+ explicit BackReference(uint32_t bitfield) : bitfield_(bitfield) {}
+
+ BackReference() : bitfield_(kInvalidValue) {}
+
+ static BackReference SourceReference() { return BackReference(kSourceValue); }
+
+ static BackReference GlobalProxyReference() {
+ return BackReference(kGlobalProxyValue);
+ }
+
+ static BackReference LargeObjectReference(uint32_t index) {
+ return BackReference(SpaceBits::encode(LO_SPACE) |
+ ChunkOffsetBits::encode(index));
+ }
+
+ static BackReference Reference(AllocationSpace space, uint32_t chunk_index,
+ uint32_t chunk_offset) {
+ DCHECK(IsAligned(chunk_offset, kObjectAlignment));
+ DCHECK_NE(LO_SPACE, space);
+ return BackReference(
+ SpaceBits::encode(space) | ChunkIndexBits::encode(chunk_index) |
+ ChunkOffsetBits::encode(chunk_offset >> kObjectAlignmentBits));
+ }
+
+ bool is_valid() const { return bitfield_ != kInvalidValue; }
+ bool is_source() const { return bitfield_ == kSourceValue; }
+ bool is_global_proxy() const { return bitfield_ == kGlobalProxyValue; }
+
+ AllocationSpace space() const {
+ DCHECK(is_valid());
+ return SpaceBits::decode(bitfield_);
+ }
+
+ uint32_t chunk_offset() const {
+ DCHECK(is_valid());
+ return ChunkOffsetBits::decode(bitfield_) << kObjectAlignmentBits;
+ }
+
+ uint32_t large_object_index() const {
+ DCHECK(is_valid());
+ DCHECK(chunk_index() == 0);
+ return ChunkOffsetBits::decode(bitfield_);
+ }
+
+ uint32_t chunk_index() const {
+ DCHECK(is_valid());
+ return ChunkIndexBits::decode(bitfield_);
+ }
+
+ uint32_t reference() const {
+ DCHECK(is_valid());
+ return bitfield_ & (ChunkOffsetBits::kMask | ChunkIndexBits::kMask);
+ }
+
+ uint32_t bitfield() const { return bitfield_; }
+
+ private:
+ static const uint32_t kInvalidValue = 0xFFFFFFFF;
+ static const uint32_t kSourceValue = 0xFFFFFFFE;
+ static const uint32_t kGlobalProxyValue = 0xFFFFFFFD;
+ static const int kChunkOffsetSize = kPageSizeBits - kObjectAlignmentBits;
+ static const int kChunkIndexSize = 32 - kChunkOffsetSize - kSpaceTagSize;
+
+ public:
+ static const int kMaxChunkIndex = (1 << kChunkIndexSize) - 1;
+
+ private:
+ class ChunkOffsetBits : public BitField<uint32_t, 0, kChunkOffsetSize> {};
+ class ChunkIndexBits
+ : public BitField<uint32_t, ChunkOffsetBits::kNext, kChunkIndexSize> {};
+ class SpaceBits
+ : public BitField<AllocationSpace, ChunkIndexBits::kNext, kSpaceTagSize> {
+ };
+
+ uint32_t bitfield_;
+};
+
+
+// Mapping objects to their location after deserialization.
+// This is used during building, but not at runtime by V8.
+class BackReferenceMap : public AddressMapBase {
+ public:
+ BackReferenceMap()
+ : no_allocation_(), map_(new HashMap(HashMap::PointersMatch)) {}
+
+ ~BackReferenceMap() { delete map_; }
+
+ BackReference Lookup(HeapObject* obj) {
+ HashMap::Entry* entry = LookupEntry(map_, obj, false);
+ return entry ? BackReference(GetValue(entry)) : BackReference();
+ }
+
+ void Add(HeapObject* obj, BackReference b) {
+ DCHECK(b.is_valid());
+ DCHECK_NULL(LookupEntry(map_, obj, false));
+ HashMap::Entry* entry = LookupEntry(map_, obj, true);
+ SetValue(entry, b.bitfield());
+ }
+
+ void AddSourceString(String* string) {
+ Add(string, BackReference::SourceReference());
+ }
+
+ void AddGlobalProxy(HeapObject* global_proxy) {
+ Add(global_proxy, BackReference::GlobalProxyReference());
+ }
+
+ private:
+ DisallowHeapAllocation no_allocation_;
+ HashMap* map_;
+ DISALLOW_COPY_AND_ASSIGN(BackReferenceMap);
+};
+
+
+class HotObjectsList {
+ public:
+ HotObjectsList() : index_(0) {
+ for (int i = 0; i < kSize; i++) circular_queue_[i] = NULL;
+ }
+
+ void Add(HeapObject* object) {
+ circular_queue_[index_] = object;
+ index_ = (index_ + 1) & kSizeMask;
+ }
+
+ HeapObject* Get(int index) {
+ DCHECK_NOT_NULL(circular_queue_[index]);
+ return circular_queue_[index];
+ }
+
+ static const int kNotFound = -1;
+
+ int Find(HeapObject* object) {
+ for (int i = 0; i < kSize; i++) {
+ if (circular_queue_[i] == object) return i;
+ }
+ return kNotFound;
+ }
+
+ static const int kSize = 8;
+
+ private:
+ STATIC_ASSERT(IS_POWER_OF_TWO(kSize));
+ static const int kSizeMask = kSize - 1;
+ HeapObject* circular_queue_[kSize];
+ int index_;
+
+ DISALLOW_COPY_AND_ASSIGN(HotObjectsList);
+};
+
+
+// The Serializer/Deserializer class is a common superclass for Serializer and
+// Deserializer which is used to store common constants and methods used by
+// both.
+class SerializerDeserializer: public ObjectVisitor {
+ public:
+ static void Iterate(Isolate* isolate, ObjectVisitor* visitor);
+
+ static int nop() { return kNop; }
+
+ // No reservation for large object space necessary.
+ static const int kNumberOfPreallocatedSpaces = LO_SPACE;
+ static const int kNumberOfSpaces = LAST_SPACE + 1;
+
+ protected:
+ // ---------- byte code range 0x00..0x7f ----------
+ // Byte codes in this range represent Where, HowToCode and WhereToPoint.
+ // Where the pointed-to object can be found:
+ enum Where {
+ // 0x00..0x05 Allocate new object, in specified space.
+ kNewObject = 0,
+ // 0x06 Unused (including 0x26, 0x46, 0x66).
+ // 0x07 Unused (including 0x27, 0x47, 0x67).
+ // 0x08..0x0d Reference to previous object from space.
+ kBackref = 0x08,
+ // 0x0e Unused (including 0x2e, 0x4e, 0x6e).
+ // 0x0f Unused (including 0x2f, 0x4f, 0x6f).
+ // 0x10..0x15 Reference to previous object from space after skip.
+ kBackrefWithSkip = 0x10,
+ // 0x16 Unused (including 0x36, 0x56, 0x76).
+ // 0x17 Unused (including 0x37, 0x57, 0x77).
+ // 0x18 Root array item.
+ kRootArray = 0x18,
+ // 0x19 Object in the partial snapshot cache.
+ kPartialSnapshotCache = 0x19,
+ // 0x1a External reference referenced by id.
+ kExternalReference = 0x1a,
+ // 0x1b Object provided in the attached list.
+ kAttachedReference = 0x1b,
+ // 0x1c Builtin code referenced by index.
+ kBuiltin = 0x1c
+ // 0x1d..0x1f Misc (including 0x3d..0x3f, 0x5d..0x5f, 0x7d..0x7f)
+ };
+
+ static const int kWhereMask = 0x1f;
+ static const int kSpaceMask = 7;
+ STATIC_ASSERT(kNumberOfSpaces <= kSpaceMask + 1);
+
+ // How to code the pointer to the object.
+ enum HowToCode {
+ // Straight pointer.
+ kPlain = 0,
+ // A pointer inlined in code. What this means depends on the architecture.
+ kFromCode = 0x20
+ };
+
+ static const int kHowToCodeMask = 0x20;
+
+ // Where to point within the object.
+ enum WhereToPoint {
+ // Points to start of object
+ kStartOfObject = 0,
+ // Points to instruction in code object or payload of cell.
+ kInnerPointer = 0x40
+ };
+
+ static const int kWhereToPointMask = 0x40;
+
+ // ---------- Misc ----------
+ // Skip.
+ static const int kSkip = 0x1d;
+ // Internal reference encoded as offsets of pc and target from code entry.
+ static const int kInternalReference = 0x1e;
+ static const int kInternalReferenceEncoded = 0x1f;
+ // Do nothing, used for padding.
+ static const int kNop = 0x3d;
+ // Move to next reserved chunk.
+ static const int kNextChunk = 0x3e;
+ // A tag emitted at strategic points in the snapshot to delineate sections.
+ // If the deserializer does not find these at the expected moments then it
+ // is an indication that the snapshot and the VM do not fit together.
+ // Examine the build process for architecture, version or configuration
+ // mismatches.
+ static const int kSynchronize = 0x5d;
+ // Used for the source code of the natives, which is in the executable, but
+ // is referred to from external strings in the snapshot.
+ static const int kNativesStringResource = 0x5e;
+ // Raw data of variable length.
+ static const int kVariableRawData = 0x7d;
+ // Repeats of variable length.
+ static const int kVariableRepeat = 0x7e;
+
+ // ---------- byte code range 0x80..0xff ----------
+ // First 32 root array items.
+ static const int kNumberOfRootArrayConstants = 0x20;
+ // 0x80..0x9f
+ static const int kRootArrayConstants = 0x80;
+ // 0xa0..0xbf
+ static const int kRootArrayConstantsWithSkip = 0xa0;
+ static const int kRootArrayConstantsMask = 0x1f;
+
+ // 8 hot (recently seen or back-referenced) objects with optional skip.
+ static const int kNumberOfHotObjects = 0x08;
+ // 0xc0..0xc7
+ static const int kHotObject = 0xc0;
+ // 0xc8..0xcf
+ static const int kHotObjectWithSkip = 0xc8;
+ static const int kHotObjectMask = 0x07;
+
+ // 32 common raw data lengths.
+ static const int kNumberOfFixedRawData = 0x20;
+ // 0xd0..0xef
+ static const int kFixedRawData = 0xd0;
+ static const int kOnePointerRawData = kFixedRawData;
+ static const int kFixedRawDataStart = kFixedRawData - 1;
+
+ // 16 repeats lengths.
+ static const int kNumberOfFixedRepeat = 0x10;
+ // 0xf0..0xff
+ static const int kFixedRepeat = 0xf0;
+ static const int kFixedRepeatStart = kFixedRepeat - 1;
+
+ // ---------- special values ----------
+ static const int kAnyOldSpace = -1;
+
+ // Sentinel after a new object to indicate that double alignment is needed.
+ static const int kDoubleAlignmentSentinel = 0;
+
+ // Used as index for the attached reference representing the source object.
+ static const int kSourceObjectReference = 0;
+
+ // Used as index for the attached reference representing the global proxy.
+ static const int kGlobalProxyReference = 0;
+
+ // ---------- member variable ----------
+ HotObjectsList hot_objects_;
+};
+
+
+class SerializedData {
+ public:
+ class Reservation {
+ public:
+ explicit Reservation(uint32_t size)
+ : reservation_(ChunkSizeBits::encode(size)) {}
+
+ uint32_t chunk_size() const { return ChunkSizeBits::decode(reservation_); }
+ bool is_last() const { return IsLastChunkBits::decode(reservation_); }
+
+ void mark_as_last() { reservation_ |= IsLastChunkBits::encode(true); }
+
+ private:
+ uint32_t reservation_;
+ };
+
+ SerializedData(byte* data, int size)
+ : data_(data), size_(size), owns_data_(false) {}
+ SerializedData() : data_(NULL), size_(0), owns_data_(false) {}
+
+ ~SerializedData() {
+ if (owns_data_) DeleteArray<byte>(data_);
+ }
+
+ uint32_t GetMagicNumber() const { return GetHeaderValue(kMagicNumberOffset); }
+
+ class ChunkSizeBits : public BitField<uint32_t, 0, 31> {};
+ class IsLastChunkBits : public BitField<bool, 31, 1> {};
+
+ static uint32_t ComputeMagicNumber(ExternalReferenceTable* table) {
+ uint32_t external_refs = table->size();
+ return 0xC0DE0000 ^ external_refs;
+ }
+
+ protected:
+ void SetHeaderValue(int offset, uint32_t value) {
+ uint32_t* address = reinterpret_cast<uint32_t*>(data_ + offset);
+ memcpy(reinterpret_cast<uint32_t*>(address), &value, sizeof(value));
+ }
+
+ uint32_t GetHeaderValue(int offset) const {
+ uint32_t value;
+ memcpy(&value, reinterpret_cast<int*>(data_ + offset), sizeof(value));
+ return value;
+ }
+
+ void AllocateData(int size);
+
+ static uint32_t ComputeMagicNumber(Isolate* isolate) {
+ return ComputeMagicNumber(ExternalReferenceTable::instance(isolate));
+ }
+
+ void SetMagicNumber(Isolate* isolate) {
+ SetHeaderValue(kMagicNumberOffset, ComputeMagicNumber(isolate));
+ }
+
+ static const int kMagicNumberOffset = 0;
+
+ byte* data_;
+ int size_;
+ bool owns_data_;
+};
+
+
+// A Deserializer reads a snapshot and reconstructs the Object graph it defines.
+class Deserializer: public SerializerDeserializer {
+ public:
+ // Create a deserializer from a snapshot byte source.
+ template <class Data>
+ explicit Deserializer(Data* data)
+ : isolate_(NULL),
+ source_(data->Payload()),
+ magic_number_(data->GetMagicNumber()),
+ external_reference_table_(NULL),
+ deserialized_large_objects_(0),
+ deserializing_user_code_(false) {
+ DecodeReservation(data->Reservations());
+ }
+
+ virtual ~Deserializer();
+
+ // Deserialize the snapshot into an empty heap.
+ void Deserialize(Isolate* isolate);
+
+ // Deserialize a single object and the objects reachable from it.
+ MaybeHandle<Object> DeserializePartial(
+ Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
+ Handle<FixedArray>* outdated_contexts_out);
+
+ // Deserialize a shared function info. Fail gracefully.
+ MaybeHandle<SharedFunctionInfo> DeserializeCode(Isolate* isolate);
+
+ void FlushICacheForNewCodeObjects();
+
+ // Pass a vector of externally-provided objects referenced by the snapshot.
+ // The ownership to its backing store is handed over as well.
+ void SetAttachedObjects(Vector<Handle<Object> > attached_objects) {
+ attached_objects_ = attached_objects;
+ }
+
+ private:
+ virtual void VisitPointers(Object** start, Object** end);
+
+ virtual void VisitRuntimeEntry(RelocInfo* rinfo) {
+ UNREACHABLE();
+ }
+
+ void Initialize(Isolate* isolate);
+
+ bool deserializing_user_code() { return deserializing_user_code_; }
+
+ void DecodeReservation(Vector<const SerializedData::Reservation> res);
+
+ bool ReserveSpace();
+
+ void UnalignedCopy(Object** dest, Object** src) {
+ memcpy(dest, src, sizeof(*src));
+ }
+
+ // Allocation sites are present in the snapshot, and must be linked into
+ // a list at deserialization time.
+ void RelinkAllocationSite(AllocationSite* site);
+
+ // Fills in some heap data in an area from start to end (non-inclusive). The
+ // space id is used for the write barrier. The object_address is the address
+ // of the object we are writing into, or NULL if we are not writing into an
+ // object, i.e. if we are writing a series of tagged values that are not on
+ // the heap.
+ void ReadData(Object** start, Object** end, int space,
+ Address object_address);
+ void ReadObject(int space_number, Object** write_back);
+ Address Allocate(int space_index, int size);
+
+ // Special handling for serialized code like hooking up internalized strings.
+ HeapObject* ProcessNewObjectFromSerializedCode(HeapObject* obj);
+
+ // This returns the address of an object that has been described in the
+ // snapshot by chunk index and offset.
+ HeapObject* GetBackReferencedObject(int space);
+
+ // Cached current isolate.
+ Isolate* isolate_;
+
+ // Objects from the attached object descriptions in the serialized user code.
+ Vector<Handle<Object> > attached_objects_;
+
+ SnapshotByteSource source_;
+ uint32_t magic_number_;
+
+ // The address of the next object that will be allocated in each space.
+ // Each space has a number of chunks reserved by the GC, with each chunk
+ // fitting into a page. Deserialized objects are allocated into the
+ // current chunk of the target space by bumping up high water mark.
+ Heap::Reservation reservations_[kNumberOfSpaces];
+ uint32_t current_chunk_[kNumberOfPreallocatedSpaces];
+ Address high_water_[kNumberOfPreallocatedSpaces];
+
+ ExternalReferenceTable* external_reference_table_;
+
+ List<HeapObject*> deserialized_large_objects_;
+
+ bool deserializing_user_code_;
+
+ DISALLOW_COPY_AND_ASSIGN(Deserializer);
+};
+
+
+class CodeAddressMap;
+
+// There can be only one serializer per V8 process.
+class Serializer : public SerializerDeserializer {
+ public:
+ Serializer(Isolate* isolate, SnapshotByteSink* sink);
+ ~Serializer();
+ void VisitPointers(Object** start, Object** end) OVERRIDE;
+
+ void EncodeReservations(List<SerializedData::Reservation>* out) const;
+
+ Isolate* isolate() const { return isolate_; }
+
+ BackReferenceMap* back_reference_map() { return &back_reference_map_; }
+ RootIndexMap* root_index_map() { return &root_index_map_; }
+
+ protected:
+ class ObjectSerializer : public ObjectVisitor {
+ public:
+ ObjectSerializer(Serializer* serializer, Object* o, SnapshotByteSink* sink,
+ HowToCode how_to_code, WhereToPoint where_to_point)
+ : serializer_(serializer),
+ object_(HeapObject::cast(o)),
+ sink_(sink),
+ reference_representation_(how_to_code + where_to_point),
+ bytes_processed_so_far_(0),
+ is_code_object_(o->IsCode()),
+ code_has_been_output_(false) {}
+ void Serialize();
+ void VisitPointers(Object** start, Object** end);
+ void VisitEmbeddedPointer(RelocInfo* target);
+ void VisitExternalReference(Address* p);
+ void VisitExternalReference(RelocInfo* rinfo);
+ void VisitInternalReference(RelocInfo* rinfo);
+ void VisitCodeTarget(RelocInfo* target);
+ void VisitCodeEntry(Address entry_address);
+ void VisitCell(RelocInfo* rinfo);
+ void VisitRuntimeEntry(RelocInfo* reloc);
+ // Used for seralizing the external strings that hold the natives source.
+ void VisitExternalOneByteString(
+ v8::String::ExternalOneByteStringResource** resource);
+ // We can't serialize a heap with external two byte strings.
+ void VisitExternalTwoByteString(
+ v8::String::ExternalStringResource** resource) {
+ UNREACHABLE();
+ }
+
+ private:
+ void SerializePrologue(AllocationSpace space, int size, Map* map);
+
+ enum ReturnSkip { kCanReturnSkipInsteadOfSkipping, kIgnoringReturn };
+ // This function outputs or skips the raw data between the last pointer and
+ // up to the current position. It optionally can just return the number of
+ // bytes to skip instead of performing a skip instruction, in case the skip
+ // can be merged into the next instruction.
+ int OutputRawData(Address up_to, ReturnSkip return_skip = kIgnoringReturn);
+ // External strings are serialized in a way to resemble sequential strings.
+ void SerializeExternalString();
+
+ Address PrepareCode();
+
+ Serializer* serializer_;
+ HeapObject* object_;
+ SnapshotByteSink* sink_;
+ int reference_representation_;
+ int bytes_processed_so_far_;
+ bool is_code_object_;
+ bool code_has_been_output_;
+ };
+
+ virtual void SerializeObject(HeapObject* o, HowToCode how_to_code,
+ WhereToPoint where_to_point, int skip) = 0;
+
+ void PutRoot(int index, HeapObject* object, HowToCode how, WhereToPoint where,
+ int skip);
+
+ // Returns true if the object was successfully serialized.
+ bool SerializeKnownObject(HeapObject* obj, HowToCode how_to_code,
+ WhereToPoint where_to_point, int skip);
+
+ inline void FlushSkip(int skip) {
+ if (skip != 0) {
+ sink_->Put(kSkip, "SkipFromSerializeObject");
+ sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
+ }
+ }
+
+ bool BackReferenceIsAlreadyAllocated(BackReference back_reference);
+
+ // This will return the space for an object.
+ BackReference AllocateLargeObject(int size);
+ BackReference Allocate(AllocationSpace space, int size);
+ int EncodeExternalReference(Address addr) {
+ return external_reference_encoder_.Encode(addr);
+ }
+
+ // GetInt reads 4 bytes at once, requiring padding at the end.
+ void Pad();
+
+ // Some roots should not be serialized, because their actual value depends on
+ // absolute addresses and they are reset after deserialization, anyway.
+ bool ShouldBeSkipped(Object** current);
+
+ // We may not need the code address map for logging for every instance
+ // of the serializer. Initialize it on demand.
+ void InitializeCodeAddressMap();
+
+ Code* CopyCode(Code* code);
+
+ inline uint32_t max_chunk_size(int space) const {
+ DCHECK_LE(0, space);
+ DCHECK_LT(space, kNumberOfSpaces);
+ return max_chunk_size_[space];
+ }
+
+ SnapshotByteSink* sink() const { return sink_; }
+
+ Isolate* isolate_;
+
+ SnapshotByteSink* sink_;
+ ExternalReferenceEncoder external_reference_encoder_;
+
+ BackReferenceMap back_reference_map_;
+ RootIndexMap root_index_map_;
+
+ friend class Deserializer;
+ friend class ObjectSerializer;
+ friend class SnapshotData;
+
+ private:
+ CodeAddressMap* code_address_map_;
+ // Objects from the same space are put into chunks for bulk-allocation
+ // when deserializing. We have to make sure that each chunk fits into a
+ // page. So we track the chunk size in pending_chunk_ of a space, but
+ // when it exceeds a page, we complete the current chunk and start a new one.
+ uint32_t pending_chunk_[kNumberOfPreallocatedSpaces];
+ List<uint32_t> completed_chunks_[kNumberOfPreallocatedSpaces];
+ uint32_t max_chunk_size_[kNumberOfPreallocatedSpaces];
+
+ // We map serialized large objects to indexes for back-referencing.
+ uint32_t large_objects_total_size_;
+ uint32_t seen_large_objects_index_;
+
+ List<byte> code_buffer_;
+
+ DISALLOW_COPY_AND_ASSIGN(Serializer);
+};
+
+
+class PartialSerializer : public Serializer {
+ public:
+ PartialSerializer(Isolate* isolate, Serializer* startup_snapshot_serializer,
+ SnapshotByteSink* sink)
+ : Serializer(isolate, sink),
+ startup_serializer_(startup_snapshot_serializer),
+ outdated_contexts_(0),
+ global_object_(NULL) {
+ InitializeCodeAddressMap();
+ }
+
+ // Serialize the objects reachable from a single object pointer.
+ void Serialize(Object** o);
+ virtual void SerializeObject(HeapObject* o, HowToCode how_to_code,
+ WhereToPoint where_to_point, int skip) OVERRIDE;
+
+ private:
+ int PartialSnapshotCacheIndex(HeapObject* o);
+ bool ShouldBeInThePartialSnapshotCache(HeapObject* o) {
+ // Scripts should be referred only through shared function infos. We can't
+ // allow them to be part of the partial snapshot because they contain a
+ // unique ID, and deserializing several partial snapshots containing script
+ // would cause dupes.
+ DCHECK(!o->IsScript());
+ return o->IsName() || o->IsSharedFunctionInfo() ||
+ o->IsHeapNumber() || o->IsCode() ||
+ o->IsScopeInfo() ||
+ o->map() ==
+ startup_serializer_->isolate()->heap()->fixed_cow_array_map();
+ }
+
+ void SerializeOutdatedContextsAsFixedArray();
+
+ Serializer* startup_serializer_;
+ List<BackReference> outdated_contexts_;
+ Object* global_object_;
+ PartialCacheIndexMap partial_cache_index_map_;
+ DISALLOW_COPY_AND_ASSIGN(PartialSerializer);
+};
+
+
+class StartupSerializer : public Serializer {
+ public:
+ StartupSerializer(Isolate* isolate, SnapshotByteSink* sink)
+ : Serializer(isolate, sink), root_index_wave_front_(0) {
+ // Clear the cache of objects used by the partial snapshot. After the
+ // strong roots have been serialized we can create a partial snapshot
+ // which will repopulate the cache with objects needed by that partial
+ // snapshot.
+ isolate->partial_snapshot_cache()->Clear();
+ InitializeCodeAddressMap();
+ }
+
+ // The StartupSerializer has to serialize the root array, which is slightly
+ // different.
+ void VisitPointers(Object** start, Object** end) OVERRIDE;
+
+ // Serialize the current state of the heap. The order is:
+ // 1) Strong references.
+ // 2) Partial snapshot cache.
+ // 3) Weak references (e.g. the string table).
+ virtual void SerializeStrongReferences();
+ virtual void SerializeObject(HeapObject* o, HowToCode how_to_code,
+ WhereToPoint where_to_point, int skip) OVERRIDE;
+ void SerializeWeakReferences();
+ void Serialize() {
+ SerializeStrongReferences();
+ SerializeWeakReferences();
+ Pad();
+ }
+
+ private:
+ intptr_t root_index_wave_front_;
+ DISALLOW_COPY_AND_ASSIGN(StartupSerializer);
+};
+
+
+class CodeSerializer : public Serializer {
+ public:
+ static ScriptData* Serialize(Isolate* isolate,
+ Handle<SharedFunctionInfo> info,
+ Handle<String> source);
+
+ MUST_USE_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize(
+ Isolate* isolate, ScriptData* cached_data, Handle<String> 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<uint32_t>* stub_keys() const { return &stub_keys_; }
+ int num_internalized_strings() const { return num_internalized_strings_; }
+
+ private:
+ CodeSerializer(Isolate* isolate, SnapshotByteSink* sink, String* source,
+ Code* main_code)
+ : Serializer(isolate, sink),
+ source_(source),
+ main_code_(main_code),
+ num_internalized_strings_(0) {
+ back_reference_map_.AddSourceString(source);
+ }
+
+ virtual 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_;
+ Code* main_code_;
+ int num_internalized_strings_;
+ List<uint32_t> stub_keys_;
+ DISALLOW_COPY_AND_ASSIGN(CodeSerializer);
+};
+
+
+// Wrapper around reservation sizes and the serialization payload.
+class SnapshotData : public SerializedData {
+ public:
+ // Used when producing.
+ explicit SnapshotData(const Serializer& ser);
+
+ // Used when consuming.
+ explicit SnapshotData(const Vector<const byte> snapshot)
+ : SerializedData(const_cast<byte*>(snapshot.begin()), snapshot.length()) {
+ CHECK(IsSane());
+ }
+
+ Vector<const Reservation> Reservations() const;
+ Vector<const byte> Payload() const;
+
+ Vector<const byte> RawData() const {
+ return Vector<const byte>(data_, size_);
+ }
+
+ private:
+ bool IsSane();
+
+ // The data header consists of uint32_t-sized entries:
+ // [0] magic number and external reference count
+ // [1] version hash
+ // [2] number of reservation size entries
+ // [3] payload length
+ // ... reservations
+ // ... serialized payload
+ static const int kCheckSumOffset = kMagicNumberOffset + kInt32Size;
+ static const int kNumReservationsOffset = kCheckSumOffset + kInt32Size;
+ static const int kPayloadLengthOffset = kNumReservationsOffset + kInt32Size;
+ static const int kHeaderSize = kPayloadLengthOffset + kInt32Size;
+};
+
+
+// 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<byte>& payload, const CodeSerializer& cs);
+
+ // Return ScriptData object and relinquish ownership over it to the caller.
+ ScriptData* GetScriptData();
+
+ Vector<const Reservation> Reservations() const;
+ Vector<const byte> Payload() const;
+
+ int NumInternalizedStrings() const;
+ Vector<const uint32_t> 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 { return source->length(); }
+
+ // 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 internalized strings
+ // [ 6] number of code stub keys
+ // [ 7] number of reservation size entries
+ // [ 8] payload length
+ // [ 9] payload checksum part 1
+ // [10] 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 kNumInternalizedStringsOffset = kFlagHashOffset + kInt32Size;
+ static const int kNumReservationsOffset =
+ kNumInternalizedStringsOffset + 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 v8::internal
+
+#endif // V8_SERIALIZE_H_
--- /dev/null
+// Copyright 2006-2008 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.
+
+// The common functionality when building with or without snapshots.
+
+#include "src/v8.h"
+
+#include "src/api.h"
+#include "src/base/platform/platform.h"
+#include "src/full-codegen.h"
+#include "src/snapshot/snapshot.h"
+
+namespace v8 {
+namespace internal {
+
+#ifdef DEBUG
+bool Snapshot::SnapshotIsValid(v8::StartupData* snapshot_blob) {
+ return !Snapshot::ExtractStartupData(snapshot_blob).is_empty() &&
+ !Snapshot::ExtractContextData(snapshot_blob).is_empty();
+}
+#endif // DEBUG
+
+
+bool Snapshot::EmbedsScript(Isolate* isolate) {
+ if (!isolate->snapshot_available()) return false;
+ return ExtractMetadata(isolate->snapshot_blob()).embeds_script();
+}
+
+
+uint32_t Snapshot::SizeOfFirstPage(Isolate* isolate, AllocationSpace space) {
+ DCHECK(space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE);
+ if (!isolate->snapshot_available()) {
+ return static_cast<uint32_t>(MemoryAllocator::PageAreaSize(space));
+ }
+ uint32_t size;
+ int offset = kFirstPageSizesOffset + (space - FIRST_PAGED_SPACE) * kInt32Size;
+ memcpy(&size, isolate->snapshot_blob()->data + offset, kInt32Size);
+ return size;
+}
+
+
+bool Snapshot::Initialize(Isolate* isolate) {
+ if (!isolate->snapshot_available()) return false;
+ base::ElapsedTimer timer;
+ if (FLAG_profile_deserialization) timer.Start();
+
+ const v8::StartupData* blob = isolate->snapshot_blob();
+ Vector<const byte> startup_data = ExtractStartupData(blob);
+ SnapshotData snapshot_data(startup_data);
+ Deserializer deserializer(&snapshot_data);
+ bool success = isolate->Init(&deserializer);
+ if (FLAG_profile_deserialization) {
+ double ms = timer.Elapsed().InMillisecondsF();
+ int bytes = startup_data.length();
+ PrintF("[Deserializing isolate (%d bytes) took %0.3f ms]\n", bytes, ms);
+ }
+ return success;
+}
+
+
+MaybeHandle<Context> Snapshot::NewContextFromSnapshot(
+ Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
+ Handle<FixedArray>* outdated_contexts_out) {
+ if (!isolate->snapshot_available()) return Handle<Context>();
+ base::ElapsedTimer timer;
+ if (FLAG_profile_deserialization) timer.Start();
+
+ const v8::StartupData* blob = isolate->snapshot_blob();
+ Vector<const byte> context_data = ExtractContextData(blob);
+ SnapshotData snapshot_data(context_data);
+ Deserializer deserializer(&snapshot_data);
+
+ MaybeHandle<Object> maybe_context = deserializer.DeserializePartial(
+ isolate, global_proxy, outdated_contexts_out);
+ Handle<Object> result;
+ if (!maybe_context.ToHandle(&result)) return MaybeHandle<Context>();
+ CHECK(result->IsContext());
+ // If the snapshot does not contain a custom script, we need to update
+ // the global object for exactly one context.
+ CHECK(EmbedsScript(isolate) || (*outdated_contexts_out)->length() == 1);
+ if (FLAG_profile_deserialization) {
+ double ms = timer.Elapsed().InMillisecondsF();
+ int bytes = context_data.length();
+ PrintF("[Deserializing context (%d bytes) took %0.3f ms]\n", bytes, ms);
+ }
+ return Handle<Context>::cast(result);
+}
+
+
+void CalculateFirstPageSizes(bool is_default_snapshot,
+ const SnapshotData& startup_snapshot,
+ const SnapshotData& context_snapshot,
+ uint32_t* sizes_out) {
+ Vector<const SerializedData::Reservation> startup_reservations =
+ startup_snapshot.Reservations();
+ Vector<const SerializedData::Reservation> context_reservations =
+ context_snapshot.Reservations();
+ int startup_index = 0;
+ int context_index = 0;
+
+ if (FLAG_profile_deserialization) {
+ int startup_total = 0;
+ int context_total = 0;
+ for (auto& reservation : startup_reservations) {
+ startup_total += reservation.chunk_size();
+ }
+ for (auto& reservation : context_reservations) {
+ context_total += reservation.chunk_size();
+ }
+ PrintF(
+ "Deserialization will reserve:\n"
+ "%10d bytes for startup\n"
+ "%10d bytes per context\n",
+ startup_total, context_total);
+ }
+
+ for (int space = 0; space < i::Serializer::kNumberOfSpaces; space++) {
+ bool single_chunk = true;
+ while (!startup_reservations[startup_index].is_last()) {
+ single_chunk = false;
+ startup_index++;
+ }
+ while (!context_reservations[context_index].is_last()) {
+ single_chunk = false;
+ context_index++;
+ }
+
+ uint32_t required = kMaxUInt32;
+ if (single_chunk) {
+ // If both the startup snapshot data and the context snapshot data on
+ // this space fit in a single page, then we consider limiting the size
+ // of the first page. For this, we add the chunk sizes and some extra
+ // allowance. This way we achieve a smaller startup memory footprint.
+ required = (startup_reservations[startup_index].chunk_size() +
+ 2 * context_reservations[context_index].chunk_size()) +
+ Page::kObjectStartOffset;
+ // Add a small allowance to the code space for small scripts.
+ if (space == CODE_SPACE) required += 32 * KB;
+ } else {
+ // We expect the vanilla snapshot to only require on page per space.
+ DCHECK(!is_default_snapshot);
+ }
+
+ if (space >= FIRST_PAGED_SPACE && space <= LAST_PAGED_SPACE) {
+ uint32_t max_size =
+ MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(space));
+ sizes_out[space - FIRST_PAGED_SPACE] = Min(required, max_size);
+ } else {
+ DCHECK(single_chunk);
+ }
+ startup_index++;
+ context_index++;
+ }
+
+ DCHECK_EQ(startup_reservations.length(), startup_index);
+ DCHECK_EQ(context_reservations.length(), context_index);
+}
+
+
+v8::StartupData Snapshot::CreateSnapshotBlob(
+ const i::StartupSerializer& startup_ser,
+ const i::PartialSerializer& context_ser, Snapshot::Metadata metadata) {
+ SnapshotData startup_snapshot(startup_ser);
+ SnapshotData context_snapshot(context_ser);
+ Vector<const byte> startup_data = startup_snapshot.RawData();
+ Vector<const byte> context_data = context_snapshot.RawData();
+
+ uint32_t first_page_sizes[kNumPagedSpaces];
+
+ CalculateFirstPageSizes(!metadata.embeds_script(), startup_snapshot,
+ context_snapshot, first_page_sizes);
+
+ int startup_length = startup_data.length();
+ int context_length = context_data.length();
+ int context_offset = ContextOffset(startup_length);
+
+ int length = context_offset + context_length;
+ char* data = new char[length];
+
+ memcpy(data + kMetadataOffset, &metadata.RawValue(), kInt32Size);
+ memcpy(data + kFirstPageSizesOffset, first_page_sizes,
+ kNumPagedSpaces * kInt32Size);
+ memcpy(data + kStartupLengthOffset, &startup_length, kInt32Size);
+ memcpy(data + kStartupDataOffset, startup_data.begin(), startup_length);
+ memcpy(data + context_offset, context_data.begin(), context_length);
+ v8::StartupData result = {data, length};
+
+ if (FLAG_profile_deserialization) {
+ PrintF(
+ "Snapshot blob consists of:\n"
+ "%10d bytes for startup\n"
+ "%10d bytes for context\n",
+ startup_length, context_length);
+ }
+ return result;
+}
+
+
+Snapshot::Metadata Snapshot::ExtractMetadata(const v8::StartupData* data) {
+ uint32_t raw;
+ memcpy(&raw, data->data + kMetadataOffset, kInt32Size);
+ return Metadata(raw);
+}
+
+
+Vector<const byte> Snapshot::ExtractStartupData(const v8::StartupData* data) {
+ DCHECK_LT(kIntSize, data->raw_size);
+ int startup_length;
+ memcpy(&startup_length, data->data + kStartupLengthOffset, kInt32Size);
+ DCHECK_LT(startup_length, data->raw_size);
+ const byte* startup_data =
+ reinterpret_cast<const byte*>(data->data + kStartupDataOffset);
+ return Vector<const byte>(startup_data, startup_length);
+}
+
+
+Vector<const byte> Snapshot::ExtractContextData(const v8::StartupData* data) {
+ DCHECK_LT(kIntSize, data->raw_size);
+ int startup_length;
+ memcpy(&startup_length, data->data + kStartupLengthOffset, kIntSize);
+ int context_offset = ContextOffset(startup_length);
+ const byte* context_data =
+ reinterpret_cast<const byte*>(data->data + context_offset);
+ DCHECK_LT(context_offset, data->raw_size);
+ int context_length = data->raw_size - context_offset;
+ return Vector<const byte>(context_data, context_length);
+}
+} } // namespace v8::internal
--- /dev/null
+// Copyright 2006-2008 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.
+
+// Used for building without snapshots.
+
+#include "src/v8.h"
+
+#include "src/snapshot/snapshot.h"
+
+namespace v8 {
+namespace internal {
+
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+// Dummy implementations of Set*FromFile(..) APIs.
+//
+// These are meant for use with snapshot-external.cc. Should this file
+// be compiled with those options we just supply these dummy implementations
+// below. This happens when compiling the mksnapshot utility.
+void SetNativesFromFile(StartupData* data) { CHECK(false); }
+void SetSnapshotFromFile(StartupData* data) { CHECK(false); }
+void ReadNatives() {}
+void DisposeNatives() {}
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
+
+const v8::StartupData* Snapshot::DefaultSnapshotBlob() { return NULL; }
+} } // namespace v8::internal
--- /dev/null
+// Copyright 2006-2008 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.
+
+// Used for building with external snapshots.
+
+#include "src/snapshot/snapshot.h"
+
+#include "src/base/platform/mutex.h"
+#include "src/snapshot/serialize.h"
+#include "src/snapshot/snapshot-source-sink.h"
+#include "src/v8.h" // for V8::Initialize
+
+
+#ifndef V8_USE_EXTERNAL_STARTUP_DATA
+#error snapshot-external.cc is used only for the external snapshot build.
+#endif // V8_USE_EXTERNAL_STARTUP_DATA
+
+
+namespace v8 {
+namespace internal {
+
+static base::LazyMutex external_startup_data_mutex = LAZY_MUTEX_INITIALIZER;
+static v8::StartupData external_startup_blob = {NULL, 0};
+
+void SetSnapshotFromFile(StartupData* snapshot_blob) {
+ base::LockGuard<base::Mutex> lock_guard(
+ external_startup_data_mutex.Pointer());
+ DCHECK(snapshot_blob);
+ DCHECK(snapshot_blob->data);
+ DCHECK(snapshot_blob->raw_size > 0);
+ DCHECK(!external_startup_blob.data);
+ DCHECK(Snapshot::SnapshotIsValid(snapshot_blob));
+ external_startup_blob = *snapshot_blob;
+}
+
+
+const v8::StartupData* Snapshot::DefaultSnapshotBlob() {
+ base::LockGuard<base::Mutex> lock_guard(
+ external_startup_data_mutex.Pointer());
+ return &external_startup_blob;
+}
+} } // namespace v8::internal
--- /dev/null
+// Copyright 2014 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/snapshot-source-sink.h"
+
+#include "src/base/logging.h"
+#include "src/handles-inl.h"
+#include "src/snapshot/serialize.h" // for SerializerDeserializer::nop()
+
+
+namespace v8 {
+namespace internal {
+
+void SnapshotByteSource::CopyRaw(byte* to, int number_of_bytes) {
+ memcpy(to, data_ + position_, number_of_bytes);
+ position_ += number_of_bytes;
+}
+
+
+void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
+ DCHECK(integer < 1 << 30);
+ integer <<= 2;
+ int bytes = 1;
+ if (integer > 0xff) bytes = 2;
+ if (integer > 0xffff) bytes = 3;
+ if (integer > 0xffffff) bytes = 4;
+ integer |= (bytes - 1);
+ Put(static_cast<int>(integer & 0xff), "IntPart1");
+ if (bytes > 1) Put(static_cast<int>((integer >> 8) & 0xff), "IntPart2");
+ if (bytes > 2) Put(static_cast<int>((integer >> 16) & 0xff), "IntPart3");
+ if (bytes > 3) Put(static_cast<int>((integer >> 24) & 0xff), "IntPart4");
+}
+
+
+void SnapshotByteSink::PutRaw(const byte* data, int number_of_bytes,
+ const char* description) {
+ data_.AddAll(Vector<byte>(const_cast<byte*>(data), number_of_bytes));
+}
+
+
+bool SnapshotByteSource::AtEOF() {
+ if (0u + length_ - position_ > 2 * sizeof(uint32_t)) return false;
+ for (int x = position_; x < length_; x++) {
+ if (data_[x] != SerializerDeserializer::nop()) return false;
+ }
+ return true;
+}
+
+
+bool SnapshotByteSource::GetBlob(const byte** data, int* number_of_bytes) {
+ int size = GetInt();
+ *number_of_bytes = size;
+
+ if (position_ + size <= length_) {
+ *data = &data_[position_];
+ Advance(size);
+ return true;
+ } else {
+ Advance(length_ - position_); // proceed until end.
+ return false;
+ }
+}
+
+} // namespace v8::internal
+} // namespace v8
--- /dev/null
+// Copyright 2012 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_SOURCE_SINK_H_
+#define V8_SNAPSHOT_SOURCE_SINK_H_
+
+#include "src/base/logging.h"
+#include "src/utils.h"
+
+namespace v8 {
+namespace internal {
+
+
+/**
+ * Source to read snapshot and builtins files from.
+ *
+ * Note: Memory ownership remains with callee.
+ */
+class SnapshotByteSource FINAL {
+ public:
+ SnapshotByteSource(const char* data, int length)
+ : data_(reinterpret_cast<const byte*>(data)),
+ length_(length),
+ position_(0) {}
+
+ explicit SnapshotByteSource(Vector<const byte> payload)
+ : data_(payload.start()), length_(payload.length()), position_(0) {}
+
+ ~SnapshotByteSource() {}
+
+ bool HasMore() { return position_ < length_; }
+
+ byte Get() {
+ DCHECK(position_ < length_);
+ return data_[position_++];
+ }
+
+ void Advance(int by) { position_ += by; }
+
+ void CopyRaw(byte* to, int number_of_bytes);
+
+ inline int GetInt() {
+ // This way of decoding variable-length encoded integers does not
+ // suffer from branch mispredictions.
+ DCHECK(position_ + 3 < length_);
+ uint32_t answer = data_[position_];
+ answer |= data_[position_ + 1] << 8;
+ answer |= data_[position_ + 2] << 16;
+ answer |= data_[position_ + 3] << 24;
+ int bytes = (answer & 3) + 1;
+ Advance(bytes);
+ uint32_t mask = 0xffffffffu;
+ mask >>= 32 - (bytes << 3);
+ answer &= mask;
+ answer >>= 2;
+ return answer;
+ }
+
+ bool GetBlob(const byte** data, int* number_of_bytes);
+
+ bool AtEOF();
+
+ int position() { return position_; }
+
+ private:
+ const byte* data_;
+ int length_;
+ int position_;
+
+ DISALLOW_COPY_AND_ASSIGN(SnapshotByteSource);
+};
+
+
+/**
+ * Sink to write snapshot files to.
+ *
+ * Subclasses must implement actual storage or i/o.
+ */
+class SnapshotByteSink {
+ public:
+ SnapshotByteSink() {}
+ explicit SnapshotByteSink(int initial_size) : data_(initial_size) {}
+
+ ~SnapshotByteSink() {}
+
+ void Put(byte b, const char* description) { data_.Add(b); }
+
+ void PutSection(int b, const char* description) {
+ DCHECK_LE(b, kMaxUInt8);
+ Put(static_cast<byte>(b), description);
+ }
+
+ void PutInt(uintptr_t integer, const char* description);
+ void PutRaw(const byte* data, int number_of_bytes, const char* description);
+ int Position() { return data_.length(); }
+
+ const List<byte>& data() const { return data_; }
+
+ private:
+ List<byte> data_;
+};
+
+} // namespace v8::internal
+} // namespace v8
+
+#endif // V8_SNAPSHOT_SOURCE_SINK_H_
--- /dev/null
+// Copyright 2006-2008 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/isolate.h"
+#include "src/snapshot/serialize.h"
+
+#ifndef V8_SNAPSHOT_H_
+#define V8_SNAPSHOT_H_
+
+namespace v8 {
+namespace internal {
+
+class Snapshot : public AllStatic {
+ public:
+ class Metadata {
+ public:
+ explicit Metadata(uint32_t data = 0) : data_(data) {}
+ bool embeds_script() { return EmbedsScriptBits::decode(data_); }
+ void set_embeds_script(bool v) {
+ data_ = EmbedsScriptBits::update(data_, v);
+ }
+
+ uint32_t& RawValue() { return data_; }
+
+ private:
+ class EmbedsScriptBits : public BitField<bool, 0, 1> {};
+ uint32_t data_;
+ };
+
+ // Initialize the Isolate from the internal snapshot. Returns false if no
+ // snapshot could be found.
+ static bool Initialize(Isolate* isolate);
+ // Create a new context using the internal partial snapshot.
+ static MaybeHandle<Context> NewContextFromSnapshot(
+ Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
+ Handle<FixedArray>* outdated_contexts_out);
+
+ static bool HaveASnapshotToStartFrom(Isolate* isolate) {
+ // Do not use snapshots if the isolate is used to create snapshots.
+ return isolate->snapshot_blob() != NULL;
+ }
+
+ static bool EmbedsScript(Isolate* isolate);
+
+ static uint32_t SizeOfFirstPage(Isolate* isolate, AllocationSpace space);
+
+
+ // To be implemented by the snapshot source.
+ static const v8::StartupData* DefaultSnapshotBlob();
+
+ static v8::StartupData CreateSnapshotBlob(
+ const StartupSerializer& startup_ser,
+ const PartialSerializer& context_ser, Snapshot::Metadata metadata);
+
+#ifdef DEBUG
+ static bool SnapshotIsValid(v8::StartupData* snapshot_blob);
+#endif // DEBUG
+
+ private:
+ static Vector<const byte> ExtractStartupData(const v8::StartupData* data);
+ static Vector<const byte> ExtractContextData(const v8::StartupData* data);
+ static Metadata ExtractMetadata(const v8::StartupData* data);
+
+ // Snapshot blob layout:
+ // [0] metadata
+ // [1 - 6] pre-calculated first page sizes for paged spaces
+ // [7] serialized start up data length
+ // ... serialized start up data
+ // ... serialized context data
+
+ static const int kNumPagedSpaces = LAST_PAGED_SPACE - FIRST_PAGED_SPACE + 1;
+
+ static const int kMetadataOffset = 0;
+ static const int kFirstPageSizesOffset = kMetadataOffset + kInt32Size;
+ static const int kStartupLengthOffset =
+ kFirstPageSizesOffset + kNumPagedSpaces * kInt32Size;
+ static const int kStartupDataOffset = kStartupLengthOffset + kInt32Size;
+
+ static int ContextOffset(int startup_length) {
+ return kStartupDataOffset + startup_length;
+ }
+
+ DISALLOW_IMPLICIT_CONSTRUCTORS(Snapshot);
+};
+
+#ifdef V8_USE_EXTERNAL_STARTUP_DATA
+void SetSnapshotFromFile(StartupData* snapshot_blob);
+#endif
+
+} } // namespace v8::internal
+
+#endif // V8_SNAPSHOT_H_
#include "src/hydrogen.h"
#include "src/isolate.h"
#include "src/lithium-allocator.h"
-#include "src/natives.h"
#include "src/objects.h"
#include "src/runtime-profiler.h"
#include "src/sampler.h"
-#include "src/serialize.h"
-#include "src/snapshot.h"
+#include "src/snapshot/natives.h"
+#include "src/snapshot/serialize.h"
+#include "src/snapshot/snapshot.h"
namespace v8 {
#include "src/assembler.h"
#include "src/compiler.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/debug.h"
#include "src/heap/heap.h"
#include "src/isolate-inl.h"
-#include "src/serialize.h"
#include "src/x64/assembler-x64.h"
#include "src/x64/macro-assembler-x64.h"
#include "src/macro-assembler.h"
#include "src/regexp-macro-assembler.h"
#include "src/regexp-stack.h"
-#include "src/serialize.h"
#include "src/unicode.h"
#include "src/x64/regexp-macro-assembler-x64.h"
#include "src/base/cpu.h"
#include "src/disassembler.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/assembler.h"
#include "src/compiler.h"
#include "src/isolate.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/debug.h"
#include "src/isolate-inl.h"
#include "src/runtime/runtime.h"
-#include "src/serialize.h"
namespace v8 {
namespace internal {
#include "src/objects.h"
#include "src/parser.h"
#include "src/smart-pointers.h"
-#include "src/snapshot.h"
#include "src/unicode-inl.h"
#include "src/utils.h"
#include "src/vm-state.h"
#include "src/objects.h"
#include "src/parser.h"
#include "src/smart-pointers.h"
-#include "src/snapshot.h"
#include "src/unicode-inl.h"
#include "src/utils.h"
#include "src/vm-state.h"
#include "src/factory.h"
#include "src/macro-assembler.h"
#include "src/ostreams.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/factory.h"
#include "src/macro-assembler.h"
#include "src/ostreams.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/factory.h"
#include "src/macro-assembler.h"
#include "src/ostreams.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/v8.h"
#include "src/global-handles.h"
-#include "src/snapshot.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/disasm.h"
#include "src/disassembler.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/disassembler.h"
#include "src/ic/ic.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/disasm.h"
#include "src/disassembler.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/disasm.h"
#include "src/disassembler.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/disasm.h"
#include "src/disassembler.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/disassembler.h"
#include "src/ic/ic.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/disassembler.h"
#include "src/ic/ic.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/debug.h"
#include "src/hashmap.h"
#include "src/heap-profiler.h"
-#include "src/snapshot.h"
#include "src/utils-inl.h"
#include "test/cctest/cctest.h"
#include "src/global-handles.h"
#include "src/ic/ic.h"
#include "src/macro-assembler.h"
-#include "src/snapshot.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/execution.h"
#include "src/isolate.h"
#include "src/parser.h"
-#include "src/snapshot.h"
#include "src/unicode-inl.h"
#include "src/utils.h"
#include "test/cctest/cctest.h"
#include "src/execution.h"
#include "src/isolate.h"
#include "src/parser.h"
-#include "src/snapshot.h"
#include "src/unicode-inl.h"
#include "src/utils.h"
#include "test/cctest/cctest.h"
#include "src/isolate.h"
#include "src/parser.h"
#include "src/smart-pointers.h"
-#include "src/snapshot.h"
#include "src/unicode-inl.h"
#include "src/utils.h"
#include "test/cctest/cctest.h"
#include "src/cpu-profiler.h"
#include "src/log.h"
#include "src/log-utils.h"
-#include "src/natives.h"
+#include "src/snapshot/natives.h"
#include "src/utils.h"
#include "src/v8threads.h"
#include "src/version.h"
#include "src/base/platform/platform.h"
#include "src/factory.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
using namespace v8::internal;
#include "src/base/platform/platform.h"
#include "src/factory.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
#include "test/cctest/cctest.h"
namespace i = v8::internal;
#include "src/base/platform/platform.h"
#include "src/factory.h"
#include "src/macro-assembler.h"
-#include "src/serialize.h"
using namespace v8::internal;
#include "src/full-codegen.h"
#include "src/global-handles.h"
-#include "src/snapshot.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/compilation-cache.h"
#include "src/debug.h"
#include "src/heap/spaces.h"
-#include "src/natives.h"
#include "src/objects.h"
#include "src/parser.h"
#include "src/runtime/runtime.h"
#include "src/scopeinfo.h"
-#include "src/serialize.h"
-#include "src/snapshot.h"
+#include "src/snapshot/natives.h"
+#include "src/snapshot/serialize.h"
+#include "src/snapshot/snapshot.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include <stdlib.h>
#include "src/base/platform/platform.h"
-#include "src/snapshot.h"
+#include "src/snapshot/snapshot.h"
#include "src/v8.h"
#include "test/cctest/cctest.h"
#include "src/v8.h"
#include "src/global-handles.h"
-#include "src/snapshot.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
#include "src/v8.h"
#include "src/global-handles.h"
-#include "src/snapshot.h"
#include "test/cctest/cctest.h"
using namespace v8::internal;
import sys
DECLARE_FILE = "src/assembler.h"
-REGISTER_FILE = "src/serialize.cc"
+REGISTER_FILE = "src/snapshot/serialize.cc"
DECLARE_RE = re.compile("\s*static ExternalReference ([^(]+)\(")
REGISTER_RE = re.compile("\s*Add\(ExternalReference::([^(]+)\(")
'sources': [
'<(SHARED_INTERMEDIATE_DIR)/libraries.cc',
'<(SHARED_INTERMEDIATE_DIR)/experimental-libraries.cc',
- '../../src/snapshot-empty.cc',
+ '../../src/snapshot/snapshot-empty.cc',
],
'conditions': [
['want_separate_host_toolset==1', {
'../..',
],
'sources': [
- '../../src/natives-external.cc',
- '../../src/snapshot-external.cc',
+ '../../src/snapshot/natives-external.cc',
+ '../../src/snapshot/snapshot-external.cc',
],
'actions': [
{
'../../src/modules.cc',
'../../src/modules.h',
'../../src/msan.h',
- '../../src/natives.h',
'../../src/objects-debug.cc',
'../../src/objects-inl.h',
'../../src/objects-printer.cc',
'../../src/scopeinfo.h',
'../../src/scopes.cc',
'../../src/scopes.h',
- '../../src/serialize.cc',
- '../../src/serialize.h',
'../../src/small-pointer-list.h',
'../../src/smart-pointers.h',
- '../../src/snapshot.h',
- '../../src/snapshot-common.cc',
- '../../src/snapshot-source-sink.cc',
- '../../src/snapshot-source-sink.h',
+ '../../src/snapshot/natives.h',
+ '../../src/snapshot/serialize.cc',
+ '../../src/snapshot/serialize.h',
+ '../../src/snapshot/snapshot.h',
+ '../../src/snapshot/snapshot-common.cc',
+ '../../src/snapshot/snapshot-source-sink.cc',
+ '../../src/snapshot/snapshot-source-sink.h',
'../../src/string-builder.cc',
'../../src/string-builder.h',
'../../src/string-search.cc',
'../..',
],
'sources': [
- '../../src/mksnapshot.cc',
+ '../../src/snapshot/mksnapshot.cc',
],
'conditions': [
['v8_enable_i18n_support==1', {
// javascript source files or the GYP script.
#include "src/v8.h"
-#include "src/natives.h"
+#include "src/snapshot/natives.h"
#include "src/utils.h"
namespace v8 {