serializer-common.cc 3.48 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/serializer-common.h"

7
#include "src/external-reference-table.h"
8 9
#include "src/ic/stub-cache.h"
#include "src/list-inl.h"
10
#include "src/objects-inl.h"
11 12 13 14 15 16

namespace v8 {
namespace internal {

ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) {
  map_ = isolate->external_reference_map();
17 18 19
#ifdef DEBUG
  table_ = ExternalReferenceTable::instance(isolate);
#endif  // DEBUG
20 21
  if (map_ != nullptr) return;
  map_ = new AddressToIndexHashMap();
22
  ExternalReferenceTable* table = ExternalReferenceTable::instance(isolate);
23
  for (uint32_t i = 0; i < table->size(); ++i) {
24
    Address addr = table->address(i);
25 26
    // Ignore duplicate API references.
    if (table->is_api_reference(i) && !map_->Get(addr).IsNothing()) continue;
27 28 29
#ifndef V8_OS_WIN
    // TODO(yangguo): On Windows memcpy and memmove can end up at the same
    // address due to ICF. See http://crbug.com/726896.
30
    DCHECK(map_->Get(addr).IsNothing());
31
#endif
32 33
    map_->Set(addr, i);
    DCHECK(map_->Get(addr).IsJust());
34 35 36 37 38
  }
  isolate->set_external_reference_map(map_);
}

uint32_t ExternalReferenceEncoder::Encode(Address address) const {
39 40
  Maybe<uint32_t> maybe_index = map_->Get(address);
  if (maybe_index.IsNothing()) {
41 42 43
    void* addr = address;
    v8::base::OS::PrintError("Unknown external reference %p.\n", addr);
    v8::base::OS::PrintError("%s", ExternalReferenceTable::ResolveSymbol(addr));
44 45
    v8::base::OS::Abort();
  }
46 47 48
#ifdef DEBUG
  table_->increment_count(maybe_index.FromJust());
#endif  // DEBUG
49
  return maybe_index.FromJust();
50 51 52 53
}

const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate,
                                                    Address address) const {
54 55 56 57
  Maybe<uint32_t> maybe_index = map_->Get(address);
  if (maybe_index.IsNothing()) return "<unknown>";
  return ExternalReferenceTable::instance(isolate)->name(
      maybe_index.FromJust());
58 59 60 61 62 63 64 65 66 67
}

void SerializedData::AllocateData(int size) {
  DCHECK(!owns_data_);
  data_ = NewArray<byte>(size);
  size_ = size;
  owns_data_ = true;
  DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment));
}

68 69 70 71 72
// The partial snapshot cache is terminated by undefined. We visit the
// partial snapshot...
//  - during deserialization to populate it.
//  - during normal GC to keep its content alive.
//  - not during serialization. The partial serializer adds to it explicitly.
krasin's avatar
krasin committed
73
DISABLE_CFI_PERF
74
void SerializerDeserializer::Iterate(Isolate* isolate, RootVisitor* visitor) {
75 76 77
  List<Object*>* cache = isolate->partial_snapshot_cache();
  for (int i = 0;; ++i) {
    // Extend the array ready to get a value when deserializing.
78
    if (cache->length() <= i) cache->Add(Smi::kZero);
79 80
    // During deserialization, the visitor populates the partial snapshot cache
    // and eventually terminates the cache with undefined.
81
    visitor->VisitRootPointer(Root::kPartialSnapshotCache, &cache->at(i));
82
    if (cache->at(i)->IsUndefined(isolate)) break;
83 84 85 86 87 88 89
  }
}

bool SerializerDeserializer::CanBeDeferred(HeapObject* o) {
  return !o->IsString() && !o->IsScript();
}

90 91 92 93 94 95 96 97 98
void SerializerDeserializer::RestoreExternalReferenceRedirectors(
    List<AccessorInfo*>* accessor_infos) {
  // Restore wiped accessor infos.
  for (AccessorInfo* info : *accessor_infos) {
    Foreign::cast(info->js_getter())
        ->set_foreign_address(info->redirected_getter());
  }
}

99 100
}  // namespace internal
}  // namespace v8