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/ic/stub-cache.h"
9 #include "src/list-inl.h"
10
11 namespace v8 {
12 namespace internal {
13
ExternalReferenceEncoder(Isolate * isolate)14 ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) {
15 map_ = isolate->external_reference_map();
16 #ifdef DEBUG
17 table_ = ExternalReferenceTable::instance(isolate);
18 #endif // DEBUG
19 if (map_ != nullptr) return;
20 map_ = new AddressToIndexHashMap();
21 ExternalReferenceTable* table = ExternalReferenceTable::instance(isolate);
22 for (uint32_t i = 0; i < table->size(); ++i) {
23 Address addr = table->address(i);
24 DCHECK(map_->Get(addr).IsNothing() ||
25 strncmp(table->name(i), "Redirect to ", 12) == 0);
26 map_->Set(addr, i);
27 DCHECK(map_->Get(addr).IsJust());
28 }
29 isolate->set_external_reference_map(map_);
30 }
31
Encode(Address address) const32 uint32_t ExternalReferenceEncoder::Encode(Address address) const {
33 Maybe<uint32_t> maybe_index = map_->Get(address);
34 if (maybe_index.IsNothing()) {
35 void* addr = address;
36 v8::base::OS::PrintError("Unknown external reference %p.\n", addr);
37 v8::base::OS::PrintError("%s", ExternalReferenceTable::ResolveSymbol(addr));
38 v8::base::OS::Abort();
39 }
40 #ifdef DEBUG
41 table_->increment_count(maybe_index.FromJust());
42 #endif // DEBUG
43 return maybe_index.FromJust();
44 }
45
NameOfAddress(Isolate * isolate,Address address) const46 const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
47 Address address) const {
48 Maybe<uint32_t> maybe_index = map_->Get(address);
49 if (maybe_index.IsNothing()) return "<unknown>";
50 return ExternalReferenceTable::instance(isolate)->name(
51 maybe_index.FromJust());
52 }
53
AllocateData(int size)54 void SerializedData::AllocateData(int size) {
55 DCHECK(!owns_data_);
56 data_ = NewArray<byte>(size);
57 size_ = size;
58 owns_data_ = true;
59 DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment));
60 }
61
62 // The partial snapshot cache is terminated by undefined. We visit the
63 // partial snapshot...
64 // - during deserialization to populate it.
65 // - during normal GC to keep its content alive.
66 // - not during serialization. The partial serializer adds to it explicitly.
67 DISABLE_CFI_PERF
Iterate(Isolate * isolate,ObjectVisitor * visitor)68 void SerializerDeserializer::Iterate(Isolate* isolate, ObjectVisitor* visitor) {
69 List<Object*>* cache = isolate->partial_snapshot_cache();
70 for (int i = 0;; ++i) {
71 // Extend the array ready to get a value when deserializing.
72 if (cache->length() <= i) cache->Add(Smi::kZero);
73 // During deserialization, the visitor populates the partial snapshot cache
74 // and eventually terminates the cache with undefined.
75 visitor->VisitPointer(&cache->at(i));
76 if (cache->at(i)->IsUndefined(isolate)) break;
77 }
78 }
79
CanBeDeferred(HeapObject * o)80 bool SerializerDeserializer::CanBeDeferred(HeapObject* o) {
81 return !o->IsString() && !o->IsScript();
82 }
83
84 } // namespace internal
85 } // namespace v8
86