• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/snapshot/serializer-common.h"
6 
7 #include "src/external-reference-table.h"
8 #include "src/objects-inl.h"
9 
10 namespace v8 {
11 namespace internal {
12 
ExternalReferenceEncoder(Isolate * isolate)13 ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) {
14 #ifdef DEBUG
15   api_references_ = isolate->api_external_references();
16   if (api_references_ != nullptr) {
17     for (uint32_t i = 0; api_references_[i] != 0; ++i) count_.push_back(0);
18   }
19 #endif  // DEBUG
20   map_ = isolate->external_reference_map();
21   if (map_ != nullptr) return;
22   map_ = new AddressToIndexHashMap();
23   isolate->set_external_reference_map(map_);
24   // Add V8's external references.
25   ExternalReferenceTable* table = isolate->heap()->external_reference_table();
26   for (uint32_t i = 0; i < table->size(); ++i) {
27     Address addr = table->address(i);
28     // Ignore duplicate references.
29     // This can happen due to ICF. See http://crbug.com/726896.
30     if (map_->Get(addr).IsNothing()) map_->Set(addr, Value::Encode(i, false));
31     DCHECK(map_->Get(addr).IsJust());
32   }
33   // Add external references provided by the embedder.
34   const intptr_t* api_references = isolate->api_external_references();
35   if (api_references == nullptr) return;
36   for (uint32_t i = 0; api_references[i] != 0; ++i) {
37     Address addr = static_cast<Address>(api_references[i]);
38     // Ignore duplicate references.
39     // This can happen due to ICF. See http://crbug.com/726896.
40     if (map_->Get(addr).IsNothing()) map_->Set(addr, Value::Encode(i, true));
41     DCHECK(map_->Get(addr).IsJust());
42   }
43 }
44 
~ExternalReferenceEncoder()45 ExternalReferenceEncoder::~ExternalReferenceEncoder() {
46 #ifdef DEBUG
47   if (!i::FLAG_external_reference_stats) return;
48   if (api_references_ == nullptr) return;
49   for (uint32_t i = 0; api_references_[i] != 0; ++i) {
50     Address addr = static_cast<Address>(api_references_[i]);
51     DCHECK(map_->Get(addr).IsJust());
52     v8::base::OS::Print(
53         "index=%5d count=%5d  %-60s\n", i, count_[i],
54         ExternalReferenceTable::ResolveSymbol(reinterpret_cast<void*>(addr)));
55   }
56 #endif  // DEBUG
57 }
58 
TryEncode(Address address)59 Maybe<ExternalReferenceEncoder::Value> ExternalReferenceEncoder::TryEncode(
60     Address address) {
61   Maybe<uint32_t> maybe_index = map_->Get(address);
62   if (maybe_index.IsNothing()) return Nothing<Value>();
63   Value result(maybe_index.FromJust());
64 #ifdef DEBUG
65   if (result.is_from_api()) count_[result.index()]++;
66 #endif  // DEBUG
67   return Just<Value>(result);
68 }
69 
Encode(Address address)70 ExternalReferenceEncoder::Value ExternalReferenceEncoder::Encode(
71     Address address) {
72   Maybe<uint32_t> maybe_index = map_->Get(address);
73   if (maybe_index.IsNothing()) {
74     void* addr = reinterpret_cast<void*>(address);
75     v8::base::OS::PrintError("Unknown external reference %p.\n", addr);
76     v8::base::OS::PrintError("%s", ExternalReferenceTable::ResolveSymbol(addr));
77     v8::base::OS::Abort();
78   }
79   Value result(maybe_index.FromJust());
80 #ifdef DEBUG
81   if (result.is_from_api()) count_[result.index()]++;
82 #endif  // DEBUG
83   return result;
84 }
85 
NameOfAddress(Isolate * isolate,Address address) const86 const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
87                                                     Address address) const {
88   Maybe<uint32_t> maybe_index = map_->Get(address);
89   if (maybe_index.IsNothing()) return "<unknown>";
90   Value value(maybe_index.FromJust());
91   if (value.is_from_api()) return "<from api>";
92   return isolate->heap()->external_reference_table()->name(value.index());
93 }
94 
AllocateData(uint32_t size)95 void SerializedData::AllocateData(uint32_t size) {
96   DCHECK(!owns_data_);
97   data_ = NewArray<byte>(size);
98   size_ = size;
99   owns_data_ = true;
100   DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment));
101 }
102 
103 // static
ComputeMagicNumber(Isolate * isolate)104 uint32_t SerializedData::ComputeMagicNumber(Isolate* isolate) {
105   return ComputeMagicNumber(isolate->heap()->external_reference_table());
106 }
107 
108 // The partial snapshot cache is terminated by undefined. We visit the
109 // partial snapshot...
110 //  - during deserialization to populate it.
111 //  - during normal GC to keep its content alive.
112 //  - not during serialization. The partial serializer adds to it explicitly.
113 DISABLE_CFI_PERF
Iterate(Isolate * isolate,RootVisitor * visitor)114 void SerializerDeserializer::Iterate(Isolate* isolate, RootVisitor* visitor) {
115   std::vector<Object*>* cache = isolate->partial_snapshot_cache();
116   for (size_t i = 0;; ++i) {
117     // Extend the array ready to get a value when deserializing.
118     if (cache->size() <= i) cache->push_back(Smi::kZero);
119     // During deserialization, the visitor populates the partial snapshot cache
120     // and eventually terminates the cache with undefined.
121     visitor->VisitRootPointer(Root::kPartialSnapshotCache, nullptr,
122                               &cache->at(i));
123     if (cache->at(i)->IsUndefined(isolate)) break;
124   }
125 }
126 
CanBeDeferred(HeapObject * o)127 bool SerializerDeserializer::CanBeDeferred(HeapObject* o) {
128   return !o->IsString() && !o->IsScript() && !o->IsJSTypedArray();
129 }
130 
RestoreExternalReferenceRedirectors(const std::vector<AccessorInfo * > & accessor_infos)131 void SerializerDeserializer::RestoreExternalReferenceRedirectors(
132     const std::vector<AccessorInfo*>& accessor_infos) {
133   // Restore wiped accessor infos.
134   for (AccessorInfo* info : accessor_infos) {
135     Foreign::cast(info->js_getter())
136         ->set_foreign_address(info->redirected_getter());
137   }
138 }
139 
RestoreExternalReferenceRedirectors(const std::vector<CallHandlerInfo * > & call_handler_infos)140 void SerializerDeserializer::RestoreExternalReferenceRedirectors(
141     const std::vector<CallHandlerInfo*>& call_handler_infos) {
142   for (CallHandlerInfo* info : call_handler_infos) {
143     Foreign::cast(info->js_callback())
144         ->set_foreign_address(info->redirected_callback());
145   }
146 }
147 
148 }  // namespace internal
149 }  // namespace v8
150