V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
partial-serializer.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/partial-serializer.h"
6 #include "src/snapshot/startup-serializer.h"
7 
8 #include "src/api-inl.h"
9 #include "src/math-random.h"
10 #include "src/microtask-queue.h"
11 #include "src/objects-inl.h"
12 #include "src/objects/slots.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 PartialSerializer::PartialSerializer(
18  Isolate* isolate, StartupSerializer* startup_serializer,
19  v8::SerializeEmbedderFieldsCallback callback)
20  : Serializer(isolate),
21  startup_serializer_(startup_serializer),
22  serialize_embedder_fields_(callback),
23  can_be_rehashed_(true) {
24  InitializeCodeAddressMap();
25  allocator()->UseCustomChunkSize(FLAG_serialization_chunk_size);
26 }
27 
28 PartialSerializer::~PartialSerializer() {
29  OutputStatistics("PartialSerializer");
30 }
31 
32 void PartialSerializer::Serialize(Context* o, bool include_global_proxy) {
33  context_ = *o;
34  DCHECK(context_->IsNativeContext());
35  reference_map()->AddAttachedReference(context_->global_proxy());
36  // The bootstrap snapshot has a code-stub context. When serializing the
37  // partial snapshot, it is chained into the weak context list on the isolate
38  // and it's next context pointer may point to the code-stub context. Clear
39  // it before serializing, it will get re-added to the context list
40  // explicitly when it's loaded.
41  context_->set(Context::NEXT_CONTEXT_LINK,
42  ReadOnlyRoots(isolate()).undefined_value());
43  DCHECK(!context_->global_object()->IsUndefined());
44  // Reset math random cache to get fresh random numbers.
45  MathRandom::ResetContext(context_);
46 
47  DCHECK_EQ(0, context_->native_context()->microtask_queue()->size());
48  context_->native_context()->set_microtask_queue(nullptr);
49 
50  VisitRootPointer(Root::kPartialSnapshotCache, nullptr, ObjectSlot(o));
51  SerializeDeferredObjects();
52 
53  // Add section for embedder-serialized embedder fields.
54  if (!embedder_fields_sink_.data()->empty()) {
55  sink_.Put(kEmbedderFieldsData, "embedder fields data");
56  sink_.Append(embedder_fields_sink_);
57  sink_.Put(kSynchronize, "Finished with embedder fields data");
58  }
59 
60  Pad();
61 }
62 
63 void PartialSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
64  WhereToPoint where_to_point, int skip) {
65  DCHECK(!ObjectIsBytecodeHandler(obj)); // Only referenced in dispatch table.
66 
67  if (SerializeHotObject(obj, how_to_code, where_to_point, skip)) return;
68 
69  if (SerializeRoot(obj, how_to_code, where_to_point, skip)) return;
70 
71  if (SerializeBackReference(obj, how_to_code, where_to_point, skip)) return;
72 
73  if (startup_serializer_->SerializeUsingReadOnlyObjectCache(
74  &sink_, obj, how_to_code, where_to_point, skip)) {
75  return;
76  }
77 
78  if (ShouldBeInThePartialSnapshotCache(obj)) {
79  startup_serializer_->SerializeUsingPartialSnapshotCache(
80  &sink_, obj, how_to_code, where_to_point, skip);
81  return;
82  }
83 
84  // Pointers from the partial snapshot to the objects in the startup snapshot
85  // should go through the root array or through the partial snapshot cache.
86  // If this is not the case you may have to add something to the root array.
87  DCHECK(!startup_serializer_->ReferenceMapContains(obj));
88  // All the internalized strings that the partial snapshot needs should be
89  // either in the root table or in the partial snapshot cache.
90  DCHECK(!obj->IsInternalizedString());
91  // Function and object templates are not context specific.
92  DCHECK(!obj->IsTemplateInfo());
93  // We should not end up at another native context.
94  DCHECK_IMPLIES(obj != context_, !obj->IsNativeContext());
95 
96  FlushSkip(skip);
97 
98  // Clear literal boilerplates and feedback.
99  if (obj->IsFeedbackVector()) FeedbackVector::cast(obj)->ClearSlots(isolate());
100 
101  if (SerializeJSObjectWithEmbedderFields(obj, how_to_code, where_to_point)) {
102  return;
103  }
104 
105  if (obj->IsJSFunction()) {
106  // Unconditionally reset the JSFunction to its SFI's code, since we can't
107  // serialize optimized code anyway.
108  JSFunction* closure = JSFunction::cast(obj);
109  if (closure->is_compiled()) closure->set_code(closure->shared()->GetCode());
110  }
111 
112  CheckRehashability(obj);
113 
114  // Object has not yet been serialized. Serialize it here.
115  ObjectSerializer serializer(this, obj, &sink_, how_to_code, where_to_point);
116  serializer.Serialize();
117 }
118 
119 bool PartialSerializer::ShouldBeInThePartialSnapshotCache(HeapObject* o) {
120  // Scripts should be referred only through shared function infos. We can't
121  // allow them to be part of the partial snapshot because they contain a
122  // unique ID, and deserializing several partial snapshots containing script
123  // would cause dupes.
124  DCHECK(!o->IsScript());
125  return o->IsName() || o->IsSharedFunctionInfo() || o->IsHeapNumber() ||
126  o->IsCode() || o->IsScopeInfo() || o->IsAccessorInfo() ||
127  o->IsTemplateInfo() ||
128  o->map() == ReadOnlyRoots(startup_serializer_->isolate())
129  .fixed_cow_array_map();
130 }
131 
132 namespace {
133 bool DataIsEmpty(const StartupData& data) { return data.raw_size == 0; }
134 } // anonymous namespace
135 
136 bool PartialSerializer::SerializeJSObjectWithEmbedderFields(
137  Object* obj, HowToCode how_to_code, WhereToPoint where_to_point) {
138  if (!obj->IsJSObject()) return false;
139  JSObject* js_obj = JSObject::cast(obj);
140  int embedder_fields_count = js_obj->GetEmbedderFieldCount();
141  if (embedder_fields_count == 0) return false;
142  CHECK_GT(embedder_fields_count, 0);
143  DCHECK_NOT_NULL(serialize_embedder_fields_.callback);
144  DCHECK(!js_obj->NeedsRehashing());
145 
146  DisallowHeapAllocation no_gc;
147  DisallowJavascriptExecution no_js(isolate());
148  DisallowCompilation no_compile(isolate());
149 
150  HandleScope scope(isolate());
151  Handle<JSObject> obj_handle(js_obj, isolate());
152  v8::Local<v8::Object> api_obj = v8::Utils::ToLocal(obj_handle);
153 
154  std::vector<EmbedderDataSlot::RawData> original_embedder_values;
155  std::vector<StartupData> serialized_data;
156 
157  // 1) Iterate embedder fields. Hold onto the original value of the fields.
158  // Ignore references to heap objects since these are to be handled by the
159  // serializer. For aligned pointers, call the serialize callback. Hold
160  // onto the result.
161  for (int i = 0; i < embedder_fields_count; i++) {
162  EmbedderDataSlot embedder_data_slot(js_obj, i);
163  original_embedder_values.emplace_back(embedder_data_slot.load_raw(no_gc));
164  Object* object = embedder_data_slot.load_tagged();
165  if (object->IsHeapObject()) {
166  DCHECK(isolate()->heap()->Contains(HeapObject::cast(object)));
167  serialized_data.push_back({nullptr, 0});
168  } else {
169  StartupData data = serialize_embedder_fields_.callback(
170  api_obj, i, serialize_embedder_fields_.data);
171  serialized_data.push_back(data);
172  }
173  }
174 
175  // 2) Embedder fields for which the embedder callback produced non-zero
176  // serialized data should be considered aligned pointers to objects owned
177  // by the embedder. Clear these memory addresses to avoid non-determism
178  // in the snapshot. This is done separately to step 1 to no not interleave
179  // with embedder callbacks.
180  for (int i = 0; i < embedder_fields_count; i++) {
181  if (!DataIsEmpty(serialized_data[i])) {
182  EmbedderDataSlot(js_obj, i).store_raw({kNullAddress}, no_gc);
183  }
184  }
185 
186  // 3) Serialize the object. References from embedder fields to heap objects or
187  // smis are serialized regularly.
188  ObjectSerializer(this, js_obj, &sink_, how_to_code, where_to_point)
189  .Serialize();
190 
191  // 4) Obtain back reference for the serialized object.
192  SerializerReference reference = reference_map()->LookupReference(js_obj);
193  DCHECK(reference.is_back_reference());
194 
195  // 5) Write data returned by the embedder callbacks into a separate sink,
196  // headed by the back reference. Restore the original embedder fields.
197  for (int i = 0; i < embedder_fields_count; i++) {
198  StartupData data = serialized_data[i];
199  if (DataIsEmpty(data)) continue;
200  // Restore original values from cleared fields.
201  EmbedderDataSlot(js_obj, i).store_raw(original_embedder_values[i], no_gc);
202  embedder_fields_sink_.Put(kNewObject + reference.space(),
203  "embedder field holder");
204  embedder_fields_sink_.PutInt(reference.chunk_index(), "BackRefChunkIndex");
205  embedder_fields_sink_.PutInt(reference.chunk_offset(),
206  "BackRefChunkOffset");
207  embedder_fields_sink_.PutInt(i, "embedder field index");
208  embedder_fields_sink_.PutInt(data.raw_size, "embedder fields data size");
209  embedder_fields_sink_.PutRaw(reinterpret_cast<const byte*>(data.data),
210  data.raw_size, "embedder fields data");
211  delete[] data.data;
212  }
213 
214  // 6) The content of the separate sink is appended eventually to the default
215  // sink. The ensures that during deserialization, we call the deserializer
216  // callback at the end, and can guarantee that the deserialized objects are
217  // in a consistent state. See PartialSerializer::Serialize.
218  return true;
219 }
220 
221 void PartialSerializer::CheckRehashability(HeapObject* obj) {
222  if (!can_be_rehashed_) return;
223  if (!obj->NeedsRehashing()) return;
224  if (obj->CanBeRehashed()) return;
225  can_be_rehashed_ = false;
226 }
227 
228 } // namespace internal
229 } // namespace v8
Definition: libplatform.h:13