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