code-serializer.cc 18.7 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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/code-serializer.h"

7
#include "src/codegen/macro-assembler.h"
Marja Hölttä's avatar
Marja Hölttä committed
8
#include "src/debug/debug.h"
9
#include "src/heap/heap-inl.h"
10 11
#include "src/logging/counters.h"
#include "src/logging/log.h"
12
#include "src/objects/objects-inl.h"
13
#include "src/objects/slots.h"
14
#include "src/objects/visitors.h"
15
#include "src/snapshot/object-deserializer.h"
16
#include "src/snapshot/snapshot.h"
17
#include "src/utils/version.h"
18 19 20 21

namespace v8 {
namespace internal {

22 23 24 25 26 27 28 29 30 31 32
ScriptData::ScriptData(const byte* data, int length)
    : owns_data_(false), rejected_(false), data_(data), length_(length) {
  if (!IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment)) {
    byte* copy = NewArray<byte>(length);
    DCHECK(IsAligned(reinterpret_cast<intptr_t>(copy), kPointerAlignment));
    CopyBytes(copy, data, length);
    data_ = copy;
    AcquireDataOwnership();
  }
}

33 34 35 36 37
CodeSerializer::CodeSerializer(Isolate* isolate, uint32_t source_hash)
    : Serializer(isolate), source_hash_(source_hash) {
  allocator()->UseCustomChunkSize(FLAG_serialization_chunk_size);
}

38 39
// static
ScriptCompiler::CachedData* CodeSerializer::Serialize(
40
    Handle<SharedFunctionInfo> info) {
41 42 43 44 45 46 47
  Isolate* isolate = info->GetIsolate();
  TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
  HistogramTimerScope histogram_timer(isolate->counters()->compile_serialize());
  RuntimeCallTimerScope runtimeTimer(isolate,
                                     RuntimeCallCounterId::kCompileSerialize);
  TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileSerialize");

48 49
  base::ElapsedTimer timer;
  if (FLAG_profile_deserialization) timer.Start();
50
  Handle<Script> script(Script::cast(info->script()), isolate);
51 52
  if (FLAG_trace_serializer) {
    PrintF("[Serializing from");
53
    script->name().ShortPrint();
54 55
    PrintF("]\n");
  }
56 57 58
  // TODO(7110): Enable serialization of Asm modules once the AsmWasmData is
  // context independent.
  if (script->ContainsAsmModule()) return nullptr;
59 60

  // Serialize code object.
61
  Handle<String> source(String::cast(script->source()), isolate);
62 63
  CodeSerializer cs(isolate, SerializedCodeData::SourceHash(
                                 source, script->origin_options()));
64
  DisallowHeapAllocation no_gc;
65 66
  cs.reference_map()->AddAttachedReference(
      reinterpret_cast<void*>(source->ptr()));
67
  ScriptData* script_data = cs.SerializeSharedFunctionInfo(info);
68 69 70

  if (FLAG_profile_deserialization) {
    double ms = timer.Elapsed().InMillisecondsF();
71
    int length = script_data->length();
72 73 74
    PrintF("[Serializing to %d bytes took %0.3f ms]\n", length, ms);
  }

75 76 77 78 79 80 81
  ScriptCompiler::CachedData* result =
      new ScriptCompiler::CachedData(script_data->data(), script_data->length(),
                                     ScriptCompiler::CachedData::BufferOwned);
  script_data->ReleaseDataOwnership();
  delete script_data;

  return result;
82 83
}

84 85
ScriptData* CodeSerializer::SerializeSharedFunctionInfo(
    Handle<SharedFunctionInfo> info) {
86 87
  DisallowHeapAllocation no_gc;

88 89
  VisitRootPointer(Root::kHandleScope, nullptr,
                   FullObjectSlot(info.location()));
90 91 92
  SerializeDeferredObjects();
  Pad();

93
  SerializedCodeData data(sink_.data(), this);
94 95

  return data.GetScriptData();
96 97
}

98
bool CodeSerializer::SerializeReadOnlyObject(HeapObject obj) {
99
  if (!ReadOnlyHeap::Contains(obj)) return false;
100

101 102 103
  // For objects on the read-only heap, never serialize the object, but instead
  // create a back reference that encodes the page number as the chunk_index and
  // the offset within the page as the chunk_offset.
104
  Address address = obj.address();
105 106
  Page* page = Page::FromAddress(address);
  uint32_t chunk_index = 0;
107
  ReadOnlySpace* const read_only_space = isolate()->heap()->read_only_space();
108 109 110 111 112
  for (Page* p : *read_only_space) {
    if (p == page) break;
    ++chunk_index;
  }
  uint32_t chunk_offset = static_cast<uint32_t>(page->Offset(address));
113 114
  SerializerReference back_reference = SerializerReference::BackReference(
      SnapshotSpace::kReadOnlyHeap, chunk_index, chunk_offset);
115
  reference_map()->Add(reinterpret_cast<void*>(obj.ptr()), back_reference);
116
  CHECK(SerializeBackReference(obj));
117 118 119
  return true;
}

120 121
void CodeSerializer::SerializeObject(HeapObject obj) {
  if (SerializeHotObject(obj)) return;
122

123
  if (SerializeRoot(obj)) return;
124

125
  if (SerializeBackReference(obj)) return;
126

127
  if (SerializeReadOnlyObject(obj)) return;
128

129
  CHECK(!obj.IsCode());
130

131
  ReadOnlyRoots roots(isolate());
132
  if (ElideObject(obj)) {
133
    return SerializeObject(roots.undefined_value());
134
  }
135

136
  if (obj.IsScript()) {
137
    Script script_obj = Script::cast(obj);
138
    DCHECK_NE(script_obj.compilation_type(), Script::COMPILATION_TYPE_EVAL);
139 140 141
    // We want to differentiate between undefined and uninitialized_symbol for
    // context_data for now. It is hack to allow debugging for scripts that are
    // included as a part of custom snapshot. (see debug::Script::IsEmbedded())
142
    Object context_data = script_obj.context_data();
143 144
    if (context_data != roots.undefined_value() &&
        context_data != roots.uninitialized_symbol()) {
145
      script_obj.set_context_data(roots.undefined_value());
146 147 148
    }
    // We don't want to serialize host options to avoid serializing unnecessary
    // object graph.
149 150
    FixedArray host_options = script_obj.host_defined_options();
    script_obj.set_host_defined_options(roots.empty_fixed_array());
151
    SerializeGeneric(obj);
152 153
    script_obj.set_host_defined_options(host_options);
    script_obj.set_context_data(context_data);
154
    return;
155 156
  }

157
  if (obj.IsSharedFunctionInfo()) {
158
    SharedFunctionInfo sfi = SharedFunctionInfo::cast(obj);
159 160
    // TODO(7110): Enable serializing of Asm modules once the AsmWasmData
    // is context independent.
161
    DCHECK(!sfi.IsApiFunction() && !sfi.HasAsmWasmData());
162

163
    DebugInfo debug_info;
164
    BytecodeArray debug_bytecode_array;
165
    if (sfi.HasDebugInfo()) {
166
      // Clear debug info.
167 168 169 170
      debug_info = sfi.GetDebugInfo();
      if (debug_info.HasInstrumentedBytecodeArray()) {
        debug_bytecode_array = debug_info.DebugBytecodeArray();
        sfi.SetDebugBytecodeArray(debug_info.OriginalBytecodeArray());
171
      }
172
      sfi.set_script_or_debug_info(debug_info.script());
173
    }
174
    DCHECK(!sfi.HasDebugInfo());
175

176
    SerializeGeneric(obj);
177 178

    // Restore debug info
179
    if (!debug_info.is_null()) {
180
      sfi.set_script_or_debug_info(debug_info);
181
      if (!debug_bytecode_array.is_null()) {
182
        sfi.SetDebugBytecodeArray(debug_bytecode_array);
183 184
      }
    }
185 186 187
    return;
  }

188 189 190 191 192 193 194
  // NOTE(mmarchini): If we try to serialize an InterpreterData our process
  // will crash since it stores a code object. Instead, we serialize the
  // bytecode array stored within the InterpreterData, which is the important
  // information. On deserialization we'll create our code objects again, if
  // --interpreted-frames-native-stack is on. See v8:9122 for more context
#ifndef V8_TARGET_ARCH_ARM
  if (V8_UNLIKELY(FLAG_interpreted_frames_native_stack) &&
195 196
      obj.IsInterpreterData()) {
    obj = InterpreterData::cast(obj).bytecode_array();
197 198 199
  }
#endif  // V8_TARGET_ARCH_ARM

200
  if (obj.IsBytecodeArray()) {
201
    // Clear the stack frame cache if present
202
    BytecodeArray::cast(obj).ClearFrameCacheFromSourcePositionTable();
203 204
  }

205
  // Past this point we should not see any (context-specific) maps anymore.
206
  CHECK(!obj.IsMap());
207
  // There should be no references to the global object embedded.
208
  CHECK(!obj.IsJSGlobalProxy() && !obj.IsJSGlobalObject());
209
  // Embedded FixedArrays that need rehashing must support rehashing.
210
  CHECK_IMPLIES(obj.NeedsRehashing(), obj.CanBeRehashed());
211
  // We expect no instantiated function objects or contexts.
212
  CHECK(!obj.IsJSFunction() && !obj.IsContext());
213

214
  SerializeGeneric(obj);
215 216
}

217
void CodeSerializer::SerializeGeneric(HeapObject heap_object) {
218
  // Object has not yet been serialized.  Serialize it here.
219
  ObjectSerializer serializer(this, heap_object, &sink_);
220 221 222
  serializer.Serialize();
}

223 224 225 226 227 228 229 230
#ifndef V8_TARGET_ARCH_ARM
// NOTE(mmarchini): when FLAG_interpreted_frames_native_stack is on, we want to
// create duplicates of InterpreterEntryTrampoline for the deserialized
// functions, otherwise we'll call the builtin IET for those functions (which
// is not what a user of this flag wants).
void CreateInterpreterDataForDeserializedCode(Isolate* isolate,
                                              Handle<SharedFunctionInfo> sfi,
                                              bool log_code_creation) {
231
  Handle<Script> script(Script::cast(sfi->script()), isolate);
232
  String name = ReadOnlyRoots(isolate).empty_string();
233
  if (script->name().IsString()) name = String::cast(script->name());
234 235
  Handle<String> name_handle(name, isolate);

236
  SharedFunctionInfo::ScriptIterator iter(isolate, *script);
237 238 239 240
  for (SharedFunctionInfo shared_info = iter.Next(); !shared_info.is_null();
       shared_info = iter.Next()) {
    if (!shared_info.HasBytecodeArray()) continue;
    Handle<SharedFunctionInfo> info = handle(shared_info, isolate);
241 242 243 244 245 246 247
    Handle<Code> code = isolate->factory()->CopyCode(Handle<Code>::cast(
        isolate->factory()->interpreter_entry_trampoline_for_profiling()));

    Handle<InterpreterData> interpreter_data =
        Handle<InterpreterData>::cast(isolate->factory()->NewStruct(
            INTERPRETER_DATA_TYPE, AllocationType::kOld));

248
    interpreter_data->set_bytecode_array(info->GetBytecodeArray());
249 250
    interpreter_data->set_interpreter_trampoline(*code);

251
    info->set_interpreter_data(*interpreter_data);
252 253 254

    if (!log_code_creation) continue;
    Handle<AbstractCode> abstract_code = Handle<AbstractCode>::cast(code);
255 256
    int line_num = script->GetLineNumber(info->StartPosition()) + 1;
    int column_num = script->GetColumnNumber(info->StartPosition()) + 1;
257 258
    PROFILE(isolate,
            CodeCreateEvent(CodeEventListener::INTERPRETED_FUNCTION_TAG,
259
                            *abstract_code, *info, *name_handle, line_num,
260 261 262 263 264
                            column_num));
  }
}
#endif  // V8_TARGET_ARCH_ARM

265
MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
266 267
    Isolate* isolate, ScriptData* cached_data, Handle<String> source,
    ScriptOriginOptions origin_options) {
268
  base::ElapsedTimer timer;
269
  if (FLAG_profile_deserialization || FLAG_log_function_events) timer.Start();
270 271 272

  HandleScope scope(isolate);

273 274 275
  SerializedCodeData::SanityCheckResult sanity_check_result =
      SerializedCodeData::CHECK_SUCCESS;
  const SerializedCodeData scd = SerializedCodeData::FromCachedData(
276 277
      isolate, cached_data,
      SerializedCodeData::SourceHash(source, origin_options),
278 279
      &sanity_check_result);
  if (sanity_check_result != SerializedCodeData::CHECK_SUCCESS) {
280 281
    if (FLAG_profile_deserialization) PrintF("[Cached code failed check]\n");
    DCHECK(cached_data->rejected());
282
    isolate->counters()->code_cache_reject_reason()->AddSample(
283
        sanity_check_result);
284 285 286 287
    return MaybeHandle<SharedFunctionInfo>();
  }

  // Deserialize.
288 289 290 291 292
  MaybeHandle<SharedFunctionInfo> maybe_result =
      ObjectDeserializer::DeserializeSharedFunctionInfo(isolate, &scd, source);

  Handle<SharedFunctionInfo> result;
  if (!maybe_result.ToHandle(&result)) {
293 294 295 296 297 298 299 300 301 302 303
    // Deserializing may fail if the reservations cannot be fulfilled.
    if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n");
    return MaybeHandle<SharedFunctionInfo>();
  }

  if (FLAG_profile_deserialization) {
    double ms = timer.Elapsed().InMillisecondsF();
    int length = cached_data->length();
    PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms);
  }

304
  const bool log_code_creation =
305 306 307
      isolate->logger()->is_listening_to_code_events() ||
      isolate->is_profiling() ||
      isolate->code_event_dispatcher()->IsListeningToCodeEvents();
308 309 310 311 312 313 314

#ifndef V8_TARGET_ARCH_ARM
  if (V8_UNLIKELY(FLAG_interpreted_frames_native_stack))
    CreateInterpreterDataForDeserializedCode(isolate, result,
                                             log_code_creation);
#endif  // V8_TARGET_ARCH_ARM

315 316
  bool needs_source_positions = isolate->NeedsSourcePositionsForProfiling();

317
  if (log_code_creation || FLAG_log_function_events) {
318 319 320 321 322 323
    Handle<Script> script(Script::cast(result->script()), isolate);
    Handle<String> name(script->name().IsString()
                            ? String::cast(script->name())
                            : ReadOnlyRoots(isolate).empty_string(),
                        isolate);

324
    if (FLAG_log_function_events) {
325 326 327 328
      LOG(isolate,
          FunctionEvent("deserialize", script->id(),
                        timer.Elapsed().InMillisecondsF(),
                        result->StartPosition(), result->EndPosition(), *name));
329 330
    }
    if (log_code_creation) {
331 332 333
      Script::InitLineEnds(script);

      SharedFunctionInfo::ScriptIterator iter(isolate, *script);
334
      for (SharedFunctionInfo info = iter.Next(); !info.is_null();
335
           info = iter.Next()) {
336
        if (info.is_compiled()) {
337 338 339 340 341 342 343 344 345 346
          Handle<SharedFunctionInfo> shared_info(info, isolate);
          if (needs_source_positions) {
            SharedFunctionInfo::EnsureSourcePositionsAvailable(isolate,
                                                               shared_info);
          }
          DisallowHeapAllocation no_gc;
          int line_num =
              script->GetLineNumber(shared_info->StartPosition()) + 1;
          int column_num =
              script->GetColumnNumber(shared_info->StartPosition()) + 1;
347
          PROFILE(isolate, CodeCreateEvent(CodeEventListener::SCRIPT_TAG,
348 349
                                           info.abstract_code(), *shared_info,
                                           *name, line_num, column_num));
350 351
        }
      }
352 353
    }
  }
354

355
  if (needs_source_positions) {
356 357 358
    Handle<Script> script(Script::cast(result->script()), isolate);
    Script::InitLineEnds(script);
  }
359 360 361 362
  return scope.CloseAndEscape(result);
}


363
SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload,
364
                                       const CodeSerializer* cs) {
365
  DisallowHeapAllocation no_gc;
366
  std::vector<Reservation> reservations = cs->EncodeReservations();
367 368

  // Calculate sizes.
369 370
  uint32_t reservation_size =
      static_cast<uint32_t>(reservations.size()) * kUInt32Size;
371
  uint32_t num_stub_keys = 0;  // TODO(jgruber): Remove.
372 373 374 375 376
  uint32_t stub_keys_size = num_stub_keys * kUInt32Size;
  uint32_t payload_offset = kHeaderSize + reservation_size + stub_keys_size;
  uint32_t padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
  uint32_t size =
      padded_payload_offset + static_cast<uint32_t>(payload->size());
377
  DCHECK(IsAligned(size, kPointerAlignment));
378 379 380 381

  // Allocate backing store and create result data.
  AllocateData(size);

382 383 384
  // Zero out pre-payload data. Part of that is only used for padding.
  memset(data_, 0, padded_payload_offset);

385
  // Set header values.
386
  SetMagicNumber();
387
  SetHeaderValue(kVersionHashOffset, Version::Hash());
388
  SetHeaderValue(kSourceHashOffset, cs->source_hash());
389
  SetHeaderValue(kFlagHashOffset, FlagList::Hash());
390 391 392
  SetHeaderValue(kNumReservationsOffset,
                 static_cast<uint32_t>(reservations.size()));
  SetHeaderValue(kPayloadLengthOffset, static_cast<uint32_t>(payload->size()));
393

394 395 396
  // Zero out any padding in the header.
  memset(data_ + kUnalignedHeaderSize, 0, kHeaderSize - kUnalignedHeaderSize);

397
  // Copy reservation chunk sizes.
398 399
  CopyBytes(data_ + kHeaderSize,
            reinterpret_cast<const byte*>(reservations.data()),
400 401 402
            reservation_size);

  // Copy serialized data.
403 404
  CopyBytes(data_ + padded_payload_offset, payload->data(),
            static_cast<size_t>(payload->size()));
405

406
  SetHeaderValue(kChecksumOffset, Checksum(ChecksummedContent()));
407 408 409
}

SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck(
410
    Isolate* isolate, uint32_t expected_source_hash) const {
411
  if (this->size_ < kHeaderSize) return INVALID_HEADER;
412
  uint32_t magic_number = GetMagicNumber();
413
  if (magic_number != kMagicNumber) return MAGIC_NUMBER_MISMATCH;
414 415 416
  uint32_t version_hash = GetHeaderValue(kVersionHashOffset);
  uint32_t source_hash = GetHeaderValue(kSourceHashOffset);
  uint32_t flags_hash = GetHeaderValue(kFlagHashOffset);
417
  uint32_t payload_length = GetHeaderValue(kPayloadLengthOffset);
418
  uint32_t c = GetHeaderValue(kChecksumOffset);
419
  if (version_hash != Version::Hash()) return VERSION_MISMATCH;
420
  if (source_hash != expected_source_hash) return SOURCE_MISMATCH;
421
  if (flags_hash != FlagList::Hash()) return FLAGS_MISMATCH;
422 423 424
  uint32_t max_payload_length =
      this->size_ -
      POINTER_SIZE_ALIGN(kHeaderSize +
425
                         GetHeaderValue(kNumReservationsOffset) * kInt32Size);
426
  if (payload_length > max_payload_length) return LENGTH_MISMATCH;
427
  if (Checksum(ChecksummedContent()) != c) return CHECKSUM_MISMATCH;
428 429 430
  return CHECK_SUCCESS;
}

431 432 433 434 435 436 437 438 439
uint32_t SerializedCodeData::SourceHash(Handle<String> source,
                                        ScriptOriginOptions origin_options) {
  const uint32_t source_length = source->length();

  static constexpr uint32_t kModuleFlagMask = (1 << 31);
  const uint32_t is_module = origin_options.IsModule() ? kModuleFlagMask : 0;
  DCHECK_EQ(0, source_length & kModuleFlagMask);

  return source_length | is_module;
440 441 442 443 444 445 446 447
}

// Return ScriptData object and relinquish ownership over it to the caller.
ScriptData* SerializedCodeData::GetScriptData() {
  DCHECK(owns_data_);
  ScriptData* result = new ScriptData(data_, size_);
  result->AcquireDataOwnership();
  owns_data_ = false;
448
  data_ = nullptr;
449 450 451
  return result;
}

452
std::vector<SerializedData::Reservation> SerializedCodeData::Reservations()
453
    const {
454 455 456 457 458
  uint32_t size = GetHeaderValue(kNumReservationsOffset);
  std::vector<Reservation> reservations(size);
  memcpy(reservations.data(), data_ + kHeaderSize,
         size * sizeof(SerializedData::Reservation));
  return reservations;
459 460 461 462
}

Vector<const byte> SerializedCodeData::Payload() const {
  int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
463
  int payload_offset = kHeaderSize + reservations_size;
464 465 466 467 468 469 470 471 472 473 474
  int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
  const byte* payload = data_ + padded_payload_offset;
  DCHECK(IsAligned(reinterpret_cast<intptr_t>(payload), kPointerAlignment));
  int length = GetHeaderValue(kPayloadLengthOffset);
  DCHECK_EQ(data_ + size_, payload + length);
  return Vector<const byte>(payload, length);
}

SerializedCodeData::SerializedCodeData(ScriptData* data)
    : SerializedData(const_cast<byte*>(data->data()), data->length()) {}

475
SerializedCodeData SerializedCodeData::FromCachedData(
476 477
    Isolate* isolate, ScriptData* cached_data, uint32_t expected_source_hash,
    SanityCheckResult* rejection_result) {
478
  DisallowHeapAllocation no_gc;
479 480 481 482 483 484 485
  SerializedCodeData scd(cached_data);
  *rejection_result = scd.SanityCheck(isolate, expected_source_hash);
  if (*rejection_result != CHECK_SUCCESS) {
    cached_data->Reject();
    return SerializedCodeData(nullptr, 0);
  }
  return scd;
486 487 488 489
}

}  // namespace internal
}  // namespace v8