1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
// Copyright 2018 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/read-only-serializer.h"
#include "src/api/api.h"
#include "src/diagnostics/code-tracer.h"
#include "src/execution/v8threads.h"
#include "src/handles/global-handles.h"
#include "src/heap/read-only-heap.h"
#include "src/objects/objects-inl.h"
#include "src/objects/slots.h"
#include "src/snapshot/serializer-inl.h"
#include "src/snapshot/startup-serializer.h"
namespace v8 {
namespace internal {
ReadOnlySerializer::ReadOnlySerializer(Isolate* isolate,
Snapshot::SerializerFlags flags)
: RootsSerializer(isolate, flags, RootIndex::kFirstReadOnlyRoot)
#ifdef DEBUG
,
serialized_objects_(isolate->heap()),
did_serialize_not_mapped_symbol_(false)
#endif
{
STATIC_ASSERT(RootIndex::kFirstReadOnlyRoot == RootIndex::kFirstRoot);
}
ReadOnlySerializer::~ReadOnlySerializer() {
OutputStatistics("ReadOnlySerializer");
}
void ReadOnlySerializer::SerializeObjectImpl(Handle<HeapObject> obj) {
CHECK(ReadOnlyHeap::Contains(*obj));
CHECK_IMPLIES(obj->IsString(), obj->IsInternalizedString());
// There should be no references to the not_mapped_symbol except for the entry
// in the root table, so don't try to serialize a reference and rely on the
// below CHECK(!did_serialize_not_mapped_symbol_) to make sure it doesn't
// serialize twice.
{
DisallowGarbageCollection no_gc;
HeapObject raw = *obj;
if (!IsNotMappedSymbol(raw)) {
if (SerializeHotObject(raw)) return;
if (IsRootAndHasBeenSerialized(raw) && SerializeRoot(raw)) return;
if (SerializeBackReference(raw)) return;
}
CheckRehashability(raw);
}
// Object has not yet been serialized. Serialize it here.
ObjectSerializer object_serializer(this, obj, &sink_);
object_serializer.Serialize();
#ifdef DEBUG
if (IsNotMappedSymbol(*obj)) {
CHECK(!did_serialize_not_mapped_symbol_);
did_serialize_not_mapped_symbol_ = true;
} else {
CHECK_NULL(serialized_objects_.Find(obj));
// There's no "IdentitySet", so use an IdentityMap with a value that is
// later ignored.
serialized_objects_.Insert(obj, 0);
}
#endif
}
void ReadOnlySerializer::SerializeReadOnlyRoots() {
// No active threads.
CHECK_NULL(isolate()->thread_manager()->FirstThreadStateInUse());
// No active or weak handles.
CHECK_IMPLIES(!allow_active_isolate_for_testing(),
isolate()->handle_scope_implementer()->blocks()->empty());
ReadOnlyRoots(isolate()).Iterate(this);
if (reconstruct_read_only_and_shared_object_caches_for_testing()) {
ReconstructReadOnlyObjectCacheForTesting();
}
}
void ReadOnlySerializer::FinalizeSerialization() {
// This comes right after serialization of the other snapshots, where we
// add entries to the read-only object cache. Add one entry with 'undefined'
// to terminate the read-only object cache.
Object undefined = ReadOnlyRoots(isolate()).undefined_value();
VisitRootPointer(Root::kReadOnlyObjectCache, nullptr,
FullObjectSlot(&undefined));
SerializeDeferredObjects();
Pad();
#ifdef DEBUG
// Check that every object on read-only heap is reachable (and was
// serialized).
ReadOnlyHeapObjectIterator iterator(isolate()->read_only_heap());
for (HeapObject object = iterator.Next(); !object.is_null();
object = iterator.Next()) {
if (IsNotMappedSymbol(object)) {
CHECK(did_serialize_not_mapped_symbol_);
} else {
CHECK_NOT_NULL(serialized_objects_.Find(object));
}
}
#endif
}
bool ReadOnlySerializer::MustBeDeferred(HeapObject object) {
if (root_has_been_serialized(RootIndex::kFreeSpaceMap) &&
root_has_been_serialized(RootIndex::kOnePointerFillerMap) &&
root_has_been_serialized(RootIndex::kTwoPointerFillerMap)) {
// All required root objects are serialized, so any aligned objects can
// be saved without problems.
return false;
}
// Defer objects with special alignment requirements until the filler roots
// are serialized.
return HeapObject::RequiredAlignment(object.map()) != kTaggedAligned;
}
bool ReadOnlySerializer::SerializeUsingReadOnlyObjectCache(
SnapshotByteSink* sink, Handle<HeapObject> obj) {
if (!ReadOnlyHeap::Contains(*obj)) return false;
// Get the cache index and serialize it into the read-only snapshot if
// necessary.
int cache_index = SerializeInObjectCache(obj);
// Writing out the cache entry into the calling serializer's sink.
sink->Put(kReadOnlyObjectCache, "ReadOnlyObjectCache");
sink->PutInt(cache_index, "read_only_object_cache_index");
return true;
}
void ReadOnlySerializer::ReconstructReadOnlyObjectCacheForTesting() {
ReadOnlyHeap* ro_heap = isolate()->read_only_heap();
DCHECK(ro_heap->read_only_object_cache_is_initialized());
for (size_t i = 0, size = ro_heap->read_only_object_cache_size(); i < size;
i++) {
Handle<HeapObject> obj(
HeapObject::cast(ro_heap->cached_read_only_object(i)), isolate());
int cache_index = SerializeInObjectCache(obj);
USE(cache_index);
DCHECK_EQ(cache_index, i);
}
}
} // namespace internal
} // namespace v8
|