V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
code-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/code-serializer.h"
6 
7 #include "src/code-stubs.h"
8 #include "src/counters.h"
9 #include "src/debug/debug.h"
10 #include "src/log.h"
11 #include "src/macro-assembler.h"
12 #include "src/objects-inl.h"
13 #include "src/objects/slots.h"
14 #include "src/snapshot/object-deserializer.h"
15 #include "src/snapshot/snapshot.h"
16 #include "src/version.h"
17 #include "src/visitors.h"
18 
19 namespace v8 {
20 namespace internal {
21 
22 ScriptData::ScriptData(const byte* data, int length)
23  : owns_data_(false), rejected_(false), data_(data), length_(length) {
24  if (!IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment)) {
25  byte* copy = NewArray<byte>(length);
26  DCHECK(IsAligned(reinterpret_cast<intptr_t>(copy), kPointerAlignment));
27  CopyBytes(copy, data, length);
28  data_ = copy;
29  AcquireDataOwnership();
30  }
31 }
32 
33 CodeSerializer::CodeSerializer(Isolate* isolate, uint32_t source_hash)
34  : Serializer(isolate), source_hash_(source_hash) {
35  allocator()->UseCustomChunkSize(FLAG_serialization_chunk_size);
36 }
37 
38 // static
39 ScriptCompiler::CachedData* CodeSerializer::Serialize(
40  Handle<SharedFunctionInfo> info) {
41  Isolate* isolate = info->GetIsolate();
42  TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
43  HistogramTimerScope histogram_timer(isolate->counters()->compile_serialize());
44  RuntimeCallTimerScope runtimeTimer(isolate,
45  RuntimeCallCounterId::kCompileSerialize);
46  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileSerialize");
47 
48  base::ElapsedTimer timer;
49  if (FLAG_profile_deserialization) timer.Start();
50  Handle<Script> script(Script::cast(info->script()), isolate);
51  if (FLAG_trace_serializer) {
52  PrintF("[Serializing from");
53  script->name()->ShortPrint();
54  PrintF("]\n");
55  }
56  // TODO(7110): Enable serialization of Asm modules once the AsmWasmData is
57  // context independent.
58  if (script->ContainsAsmModule()) return nullptr;
59 
60  isolate->heap()->read_only_space()->ClearStringPaddingIfNeeded();
61 
62  // Serialize code object.
63  Handle<String> source(String::cast(script->source()), isolate);
64  CodeSerializer cs(isolate, SerializedCodeData::SourceHash(
65  source, script->origin_options()));
66  DisallowHeapAllocation no_gc;
67  cs.reference_map()->AddAttachedReference(*source);
68  ScriptData* script_data = cs.SerializeSharedFunctionInfo(info);
69 
70  if (FLAG_profile_deserialization) {
71  double ms = timer.Elapsed().InMillisecondsF();
72  int length = script_data->length();
73  PrintF("[Serializing to %d bytes took %0.3f ms]\n", length, ms);
74  }
75 
76  ScriptCompiler::CachedData* result =
77  new ScriptCompiler::CachedData(script_data->data(), script_data->length(),
78  ScriptCompiler::CachedData::BufferOwned);
79  script_data->ReleaseDataOwnership();
80  delete script_data;
81 
82  return result;
83 }
84 
85 ScriptData* CodeSerializer::SerializeSharedFunctionInfo(
86  Handle<SharedFunctionInfo> info) {
87  DisallowHeapAllocation no_gc;
88 
89  VisitRootPointer(Root::kHandleScope, nullptr, ObjectSlot(info.location()));
90  SerializeDeferredObjects();
91  Pad();
92 
93  SerializedCodeData data(sink_.data(), this);
94 
95  return data.GetScriptData();
96 }
97 
98 bool CodeSerializer::SerializeReadOnlyObject(HeapObject* obj,
99  HowToCode how_to_code,
100  WhereToPoint where_to_point,
101  int skip) {
102  PagedSpace* read_only_space = isolate()->heap()->read_only_space();
103  if (!read_only_space->Contains(obj)) return false;
104 
105  // For objects in RO_SPACE, never serialize the object, but instead create a
106  // back reference that encodes the page number as the chunk_index and the
107  // offset within the page as the chunk_offset.
108  Address address = obj->address();
109  Page* page = Page::FromAddress(address);
110  uint32_t chunk_index = 0;
111  for (Page* p : *read_only_space) {
112  if (p == page) break;
113  ++chunk_index;
114  }
115  uint32_t chunk_offset = static_cast<uint32_t>(page->Offset(address));
116  SerializerReference back_reference =
117  SerializerReference::BackReference(RO_SPACE, chunk_index, chunk_offset);
118  reference_map()->Add(obj, back_reference);
119  CHECK(SerializeBackReference(obj, how_to_code, where_to_point, skip));
120  return true;
121 }
122 
123 void CodeSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
124  WhereToPoint where_to_point, int skip) {
125  if (SerializeHotObject(obj, how_to_code, where_to_point, skip)) return;
126 
127  if (SerializeRoot(obj, how_to_code, where_to_point, skip)) return;
128 
129  if (SerializeBackReference(obj, how_to_code, where_to_point, skip)) return;
130 
131  if (SerializeReadOnlyObject(obj, how_to_code, where_to_point, skip)) return;
132 
133  FlushSkip(skip);
134 
135  if (obj->IsCode()) {
136  Code code_object = Code::cast(obj);
137  switch (code_object->kind()) {
138  case Code::OPTIMIZED_FUNCTION: // No optimized code compiled yet.
139  case Code::REGEXP: // No regexp literals initialized yet.
140  case Code::NUMBER_OF_KINDS: // Pseudo enum value.
141  case Code::BYTECODE_HANDLER: // No direct references to handlers.
142  break; // hit UNREACHABLE below.
143  case Code::STUB:
144  if (code_object->builtin_index() == -1) {
145  return SerializeCodeStub(code_object, how_to_code, where_to_point);
146  } else {
147  return SerializeCodeObject(code_object, how_to_code, where_to_point);
148  }
149  case Code::BUILTIN:
150  default:
151  return SerializeCodeObject(code_object, how_to_code, where_to_point);
152  }
153  UNREACHABLE();
154  }
155 
156  ReadOnlyRoots roots(isolate());
157  if (ElideObject(obj)) {
158  return SerializeObject(roots.undefined_value(), how_to_code, where_to_point,
159  skip);
160  }
161 
162  if (obj->IsScript()) {
163  Script* script_obj = Script::cast(obj);
164  DCHECK_NE(script_obj->compilation_type(), Script::COMPILATION_TYPE_EVAL);
165  // We want to differentiate between undefined and uninitialized_symbol for
166  // context_data for now. It is hack to allow debugging for scripts that are
167  // included as a part of custom snapshot. (see debug::Script::IsEmbedded())
168  Object* context_data = script_obj->context_data();
169  if (context_data != roots.undefined_value() &&
170  context_data != roots.uninitialized_symbol()) {
171  script_obj->set_context_data(roots.undefined_value());
172  }
173  // We don't want to serialize host options to avoid serializing unnecessary
174  // object graph.
175  FixedArray host_options = script_obj->host_defined_options();
176  script_obj->set_host_defined_options(roots.empty_fixed_array());
177  SerializeGeneric(obj, how_to_code, where_to_point);
178  script_obj->set_host_defined_options(host_options);
179  script_obj->set_context_data(context_data);
180  return;
181  }
182 
183  if (obj->IsSharedFunctionInfo()) {
184  SharedFunctionInfo* sfi = SharedFunctionInfo::cast(obj);
185  // TODO(7110): Enable serializing of Asm modules once the AsmWasmData
186  // is context independent.
187  DCHECK(!sfi->IsApiFunction() && !sfi->HasAsmWasmData());
188 
189  DebugInfo* debug_info = nullptr;
190  BytecodeArray debug_bytecode_array;
191  if (sfi->HasDebugInfo()) {
192  // Clear debug info.
193  debug_info = sfi->GetDebugInfo();
194  if (debug_info->HasInstrumentedBytecodeArray()) {
195  debug_bytecode_array = debug_info->DebugBytecodeArray();
196  sfi->SetDebugBytecodeArray(debug_info->OriginalBytecodeArray());
197  }
198  sfi->set_script_or_debug_info(debug_info->script());
199  }
200  DCHECK(!sfi->HasDebugInfo());
201 
202  // Mark SFI to indicate whether the code is cached.
203  bool was_deserialized = sfi->deserialized();
204  sfi->set_deserialized(sfi->is_compiled());
205  SerializeGeneric(obj, how_to_code, where_to_point);
206  sfi->set_deserialized(was_deserialized);
207 
208  // Restore debug info
209  if (debug_info != nullptr) {
210  sfi->set_script_or_debug_info(debug_info);
211  if (!debug_bytecode_array.is_null()) {
212  sfi->SetDebugBytecodeArray(debug_bytecode_array);
213  }
214  }
215  return;
216  }
217 
218  if (obj->IsBytecodeArray()) {
219  // Clear the stack frame cache if present
220  BytecodeArray::cast(obj)->ClearFrameCacheFromSourcePositionTable();
221  }
222 
223  // Past this point we should not see any (context-specific) maps anymore.
224  CHECK(!obj->IsMap());
225  // There should be no references to the global object embedded.
226  CHECK(!obj->IsJSGlobalProxy() && !obj->IsJSGlobalObject());
227  // Embedded FixedArrays that need rehashing must support rehashing.
228  CHECK_IMPLIES(obj->NeedsRehashing(), obj->CanBeRehashed());
229  // We expect no instantiated function objects or contexts.
230  CHECK(!obj->IsJSFunction() && !obj->IsContext());
231 
232  SerializeGeneric(obj, how_to_code, where_to_point);
233 }
234 
235 void CodeSerializer::SerializeGeneric(HeapObject* heap_object,
236  HowToCode how_to_code,
237  WhereToPoint where_to_point) {
238  // Object has not yet been serialized. Serialize it here.
239  ObjectSerializer serializer(this, heap_object, &sink_, how_to_code,
240  where_to_point);
241  serializer.Serialize();
242 }
243 
244 void CodeSerializer::SerializeCodeStub(Code code_stub, HowToCode how_to_code,
245  WhereToPoint where_to_point) {
246  // We only arrive here if we have not encountered this code stub before.
247  DCHECK(!reference_map()->LookupReference(code_stub).is_valid());
248  uint32_t stub_key = code_stub->stub_key();
249  DCHECK(CodeStub::MajorKeyFromKey(stub_key) != CodeStub::NoCache);
250  DCHECK(!CodeStub::GetCode(isolate(), stub_key).is_null());
251  stub_keys_.push_back(stub_key);
252 
253  SerializerReference reference =
254  reference_map()->AddAttachedReference(code_stub);
255  if (FLAG_trace_serializer) {
256  PrintF(" Encoding code stub %s as attached reference %d\n",
257  CodeStub::MajorName(CodeStub::MajorKeyFromKey(stub_key)),
258  reference.attached_reference_index());
259  }
260  PutAttachedReference(reference, how_to_code, where_to_point);
261 }
262 
263 MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
264  Isolate* isolate, ScriptData* cached_data, Handle<String> source,
265  ScriptOriginOptions origin_options) {
266  base::ElapsedTimer timer;
267  if (FLAG_profile_deserialization || FLAG_log_function_events) timer.Start();
268 
269  HandleScope scope(isolate);
270 
271  SerializedCodeData::SanityCheckResult sanity_check_result =
272  SerializedCodeData::CHECK_SUCCESS;
273  const SerializedCodeData scd = SerializedCodeData::FromCachedData(
274  isolate, cached_data,
275  SerializedCodeData::SourceHash(source, origin_options),
276  &sanity_check_result);
277  if (sanity_check_result != SerializedCodeData::CHECK_SUCCESS) {
278  if (FLAG_profile_deserialization) PrintF("[Cached code failed check]\n");
279  DCHECK(cached_data->rejected());
280  isolate->counters()->code_cache_reject_reason()->AddSample(
281  sanity_check_result);
282  return MaybeHandle<SharedFunctionInfo>();
283  }
284 
285  // Deserialize.
286  MaybeHandle<SharedFunctionInfo> maybe_result =
287  ObjectDeserializer::DeserializeSharedFunctionInfo(isolate, &scd, source);
288 
289  Handle<SharedFunctionInfo> result;
290  if (!maybe_result.ToHandle(&result)) {
291  // Deserializing may fail if the reservations cannot be fulfilled.
292  if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n");
293  return MaybeHandle<SharedFunctionInfo>();
294  }
295 
296  if (FLAG_profile_deserialization) {
297  double ms = timer.Elapsed().InMillisecondsF();
298  int length = cached_data->length();
299  PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms);
300  }
301 
302  bool log_code_creation = isolate->logger()->is_listening_to_code_events() ||
303  isolate->is_profiling();
304  if (log_code_creation || FLAG_log_function_events) {
305  String name = ReadOnlyRoots(isolate).empty_string();
306  if (result->script()->IsScript()) {
307  Script* script = Script::cast(result->script());
308  if (script->name()->IsString()) name = String::cast(script->name());
309  if (FLAG_log_function_events) {
310  LOG(isolate, FunctionEvent("deserialize", script->id(),
311  timer.Elapsed().InMillisecondsF(),
312  result->StartPosition(),
313  result->EndPosition(), name));
314  }
315  }
316  if (log_code_creation) {
317  PROFILE(isolate, CodeCreateEvent(CodeEventListener::SCRIPT_TAG,
318  result->abstract_code(), *result, name));
319  }
320  }
321 
322  if (isolate->NeedsSourcePositionsForProfiling()) {
323  Handle<Script> script(Script::cast(result->script()), isolate);
324  Script::InitLineEnds(script);
325  }
326  return scope.CloseAndEscape(result);
327 }
328 
329 
330 SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload,
331  const CodeSerializer* cs) {
332  DisallowHeapAllocation no_gc;
333  const std::vector<uint32_t>* stub_keys = cs->stub_keys();
334  std::vector<Reservation> reservations = cs->EncodeReservations();
335 
336  // Calculate sizes.
337  uint32_t reservation_size =
338  static_cast<uint32_t>(reservations.size()) * kUInt32Size;
339  uint32_t num_stub_keys = static_cast<uint32_t>(stub_keys->size());
340  uint32_t stub_keys_size = num_stub_keys * kUInt32Size;
341  uint32_t payload_offset = kHeaderSize + reservation_size + stub_keys_size;
342  uint32_t padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
343  uint32_t size =
344  padded_payload_offset + static_cast<uint32_t>(payload->size());
345  DCHECK(IsAligned(size, kPointerAlignment));
346 
347  // Allocate backing store and create result data.
348  AllocateData(size);
349 
350  // Zero out pre-payload data. Part of that is only used for padding.
351  memset(data_, 0, padded_payload_offset);
352 
353  // Set header values.
354  SetMagicNumber(cs->isolate());
355  SetHeaderValue(kVersionHashOffset, Version::Hash());
356  SetHeaderValue(kSourceHashOffset, cs->source_hash());
357  SetHeaderValue(kCpuFeaturesOffset,
358  static_cast<uint32_t>(CpuFeatures::SupportedFeatures()));
359  SetHeaderValue(kFlagHashOffset, FlagList::Hash());
360  SetHeaderValue(kNumReservationsOffset,
361  static_cast<uint32_t>(reservations.size()));
362  SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys);
363  SetHeaderValue(kPayloadLengthOffset, static_cast<uint32_t>(payload->size()));
364 
365  // Zero out any padding in the header.
366  memset(data_ + kUnalignedHeaderSize, 0, kHeaderSize - kUnalignedHeaderSize);
367 
368  // Copy reservation chunk sizes.
369  CopyBytes(data_ + kHeaderSize,
370  reinterpret_cast<const byte*>(reservations.data()),
371  reservation_size);
372 
373  // Copy code stub keys.
374  CopyBytes(data_ + kHeaderSize + reservation_size,
375  reinterpret_cast<const byte*>(stub_keys->data()), stub_keys_size);
376 
377  // Copy serialized data.
378  CopyBytes(data_ + padded_payload_offset, payload->data(),
379  static_cast<size_t>(payload->size()));
380 
381  Checksum checksum(ChecksummedContent());
382  SetHeaderValue(kChecksumPartAOffset, checksum.a());
383  SetHeaderValue(kChecksumPartBOffset, checksum.b());
384 }
385 
386 SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck(
387  Isolate* isolate, uint32_t expected_source_hash) const {
388  if (this->size_ < kHeaderSize) return INVALID_HEADER;
389  uint32_t magic_number = GetMagicNumber();
390  if (magic_number != ComputeMagicNumber(isolate)) return MAGIC_NUMBER_MISMATCH;
391  uint32_t version_hash = GetHeaderValue(kVersionHashOffset);
392  uint32_t source_hash = GetHeaderValue(kSourceHashOffset);
393  uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset);
394  uint32_t flags_hash = GetHeaderValue(kFlagHashOffset);
395  uint32_t payload_length = GetHeaderValue(kPayloadLengthOffset);
396  uint32_t c1 = GetHeaderValue(kChecksumPartAOffset);
397  uint32_t c2 = GetHeaderValue(kChecksumPartBOffset);
398  if (version_hash != Version::Hash()) return VERSION_MISMATCH;
399  if (source_hash != expected_source_hash) return SOURCE_MISMATCH;
400  if (cpu_features != static_cast<uint32_t>(CpuFeatures::SupportedFeatures())) {
401  return CPU_FEATURES_MISMATCH;
402  }
403  if (flags_hash != FlagList::Hash()) return FLAGS_MISMATCH;
404  uint32_t max_payload_length =
405  this->size_ -
406  POINTER_SIZE_ALIGN(kHeaderSize +
407  GetHeaderValue(kNumReservationsOffset) * kInt32Size +
408  GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size);
409  if (payload_length > max_payload_length) return LENGTH_MISMATCH;
410  if (!Checksum(ChecksummedContent()).Check(c1, c2)) return CHECKSUM_MISMATCH;
411  return CHECK_SUCCESS;
412 }
413 
414 uint32_t SerializedCodeData::SourceHash(Handle<String> source,
415  ScriptOriginOptions origin_options) {
416  const uint32_t source_length = source->length();
417 
418  static constexpr uint32_t kModuleFlagMask = (1 << 31);
419  const uint32_t is_module = origin_options.IsModule() ? kModuleFlagMask : 0;
420  DCHECK_EQ(0, source_length & kModuleFlagMask);
421 
422  return source_length | is_module;
423 }
424 
425 // Return ScriptData object and relinquish ownership over it to the caller.
426 ScriptData* SerializedCodeData::GetScriptData() {
427  DCHECK(owns_data_);
428  ScriptData* result = new ScriptData(data_, size_);
429  result->AcquireDataOwnership();
430  owns_data_ = false;
431  data_ = nullptr;
432  return result;
433 }
434 
435 std::vector<SerializedData::Reservation> SerializedCodeData::Reservations()
436  const {
437  uint32_t size = GetHeaderValue(kNumReservationsOffset);
438  std::vector<Reservation> reservations(size);
439  memcpy(reservations.data(), data_ + kHeaderSize,
440  size * sizeof(SerializedData::Reservation));
441  return reservations;
442 }
443 
444 Vector<const byte> SerializedCodeData::Payload() const {
445  int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
446  int code_stubs_size = GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size;
447  int payload_offset = kHeaderSize + reservations_size + code_stubs_size;
448  int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
449  const byte* payload = data_ + padded_payload_offset;
450  DCHECK(IsAligned(reinterpret_cast<intptr_t>(payload), kPointerAlignment));
451  int length = GetHeaderValue(kPayloadLengthOffset);
452  DCHECK_EQ(data_ + size_, payload + length);
453  return Vector<const byte>(payload, length);
454 }
455 
456 Vector<const uint32_t> SerializedCodeData::CodeStubKeys() const {
457  int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
458  const byte* start = data_ + kHeaderSize + reservations_size;
459  return Vector<const uint32_t>(reinterpret_cast<const uint32_t*>(start),
460  GetHeaderValue(kNumCodeStubKeysOffset));
461 }
462 
463 SerializedCodeData::SerializedCodeData(ScriptData* data)
464  : SerializedData(const_cast<byte*>(data->data()), data->length()) {}
465 
466 SerializedCodeData SerializedCodeData::FromCachedData(
467  Isolate* isolate, ScriptData* cached_data, uint32_t expected_source_hash,
468  SanityCheckResult* rejection_result) {
469  DisallowHeapAllocation no_gc;
470  SerializedCodeData scd(cached_data);
471  *rejection_result = scd.SanityCheck(isolate, expected_source_hash);
472  if (*rejection_result != CHECK_SUCCESS) {
473  cached_data->Reject();
474  return SerializedCodeData(nullptr, 0);
475  }
476  return scd;
477 }
478 
479 } // namespace internal
480 } // namespace v8
Definition: libplatform.h:13