startup-serializer.cc 9.17 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/startup-serializer.h"

7
#include "src/api/api.h"
8
#include "src/deoptimizer/deoptimizer.h"
9
#include "src/execution/v8threads.h"
10
#include "src/handles/global-handles.h"
11
#include "src/heap/heap-inl.h"
12
#include "src/heap/read-only-heap.h"
13
#include "src/objects/contexts.h"
14
#include "src/objects/foreign-inl.h"
15
#include "src/objects/objects-inl.h"
16
#include "src/objects/slots.h"
17
#include "src/snapshot/read-only-serializer.h"
18 19 20 21

namespace v8 {
namespace internal {

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
namespace {

// The isolate roots may not point at context-specific objects during
// serialization.
class SanitizeIsolateScope final {
 public:
  SanitizeIsolateScope(Isolate* isolate, bool allow_active_isolate_for_testing,
                       const DisallowHeapAllocation& no_gc)
      : isolate_(isolate),
        feedback_vectors_for_profiling_tools_(
            isolate->heap()->feedback_vectors_for_profiling_tools()),
        detached_contexts_(isolate->heap()->detached_contexts()) {
#ifdef DEBUG
    if (!allow_active_isolate_for_testing) {
      // These should already be empty when creating a real snapshot.
      DCHECK_EQ(feedback_vectors_for_profiling_tools_,
                ReadOnlyRoots(isolate).undefined_value());
      DCHECK_EQ(detached_contexts_,
                ReadOnlyRoots(isolate).empty_weak_array_list());
    }
#endif

    isolate->SetFeedbackVectorsForProfilingTools(
        ReadOnlyRoots(isolate).undefined_value());
    isolate->heap()->SetDetachedContexts(
        ReadOnlyRoots(isolate).empty_weak_array_list());
  }

  ~SanitizeIsolateScope() {
    // Restore saved fields.
    isolate_->SetFeedbackVectorsForProfilingTools(
        feedback_vectors_for_profiling_tools_);
    isolate_->heap()->SetDetachedContexts(detached_contexts_);
  }

 private:
  Isolate* isolate_;
  const Object feedback_vectors_for_profiling_tools_;
  const WeakArrayList detached_contexts_;
};

}  // namespace

65
StartupSerializer::StartupSerializer(Isolate* isolate,
66
                                     Snapshot::SerializerFlags flags,
67
                                     ReadOnlySerializer* read_only_serializer)
68
    : RootsSerializer(isolate, flags, RootIndex::kFirstStrongRoot),
69
      read_only_serializer_(read_only_serializer) {
70
  allocator()->UseCustomChunkSize(FLAG_serialization_chunk_size);
71 72 73 74
  InitializeCodeAddressMap();
}

StartupSerializer::~StartupSerializer() {
75 76
  RestoreExternalReferenceRedirectors(isolate(), accessor_infos_);
  RestoreExternalReferenceRedirectors(isolate(), call_handler_infos_);
77 78 79
  OutputStatistics("StartupSerializer");
}

80
#ifdef DEBUG
81 82
namespace {

83
bool IsUnexpectedCodeObject(Isolate* isolate, HeapObject obj) {
84
  if (!obj.IsCode()) return false;
85

86
  Code code = Code::cast(obj);
87

88
  // TODO(v8:8768): Deopt entry code should not be serialized.
89
  if (code.kind() == Code::STUB && isolate->deoptimizer_data() != nullptr) {
90 91 92
    if (isolate->deoptimizer_data()->IsDeoptEntryCode(code)) return false;
  }

93 94 95
  if (code.kind() == Code::REGEXP) return false;
  if (!code.is_builtin()) return true;
  if (code.is_off_heap_trampoline()) return false;
96 97 98 99 100

  // An on-heap builtin. We only expect this for the interpreter entry
  // trampoline copy stored on the root list and transitively called builtins.
  // See Heap::interpreter_entry_trampoline_for_profiling.

101
  switch (code.builtin_index()) {
102 103 104 105 106 107 108 109 110 111
    case Builtins::kAbort:
    case Builtins::kCEntry_Return1_DontSaveFPRegs_ArgvOnStack_NoBuiltinExit:
    case Builtins::kInterpreterEntryTrampoline:
    case Builtins::kRecordWrite:
      return false;
    default:
      return true;
  }

  UNREACHABLE();
112 113 114
}

}  // namespace
115
#endif  // DEBUG
116

117
void StartupSerializer::SerializeObject(HeapObject obj) {
118 119 120 121 122 123 124 125 126 127
#ifdef DEBUG
  if (obj.IsJSFunction()) {
    v8::base::OS::PrintError("Reference stack:\n");
    PrintStack(std::cerr);
    obj.Print(std::cerr);
    FATAL(
        "JSFunction should be added through the context snapshot instead of "
        "the isolate snapshot");
  }
#endif  // DEBUG
128
  DCHECK(!IsUnexpectedCodeObject(isolate(), obj));
129

130 131 132 133
  if (SerializeHotObject(obj)) return;
  if (IsRootAndHasBeenSerialized(obj) && SerializeRoot(obj)) return;
  if (SerializeUsingReadOnlyObjectCache(&sink_, obj)) return;
  if (SerializeBackReference(obj)) return;
134

135 136 137 138
  bool use_simulator = false;
#ifdef USE_SIMULATOR
  use_simulator = true;
#endif
139

140
  if (use_simulator && obj.IsAccessorInfo()) {
141
    // Wipe external reference redirects in the accessor info.
142
    AccessorInfo info = AccessorInfo::cast(obj);
143 144 145 146
    Address original_address =
        Foreign::cast(info.getter()).foreign_address(isolate());
    Foreign::cast(info.js_getter())
        .set_foreign_address(isolate(), original_address);
147
    accessor_infos_.push_back(info);
148
  } else if (use_simulator && obj.IsCallHandlerInfo()) {
149
    CallHandlerInfo info = CallHandlerInfo::cast(obj);
150 151 152 153
    Address original_address =
        Foreign::cast(info.callback()).foreign_address(isolate());
    Foreign::cast(info.js_callback())
        .set_foreign_address(isolate(), original_address);
154
    call_handler_infos_.push_back(info);
155 156
  } else if (obj.IsScript() && Script::cast(obj).IsUserJavaScript()) {
    Script::cast(obj).set_context_data(
157
        ReadOnlyRoots(isolate()).uninitialized_symbol());
158
  } else if (obj.IsSharedFunctionInfo()) {
159
    // Clear inferred name for native functions.
160
    SharedFunctionInfo shared = SharedFunctionInfo::cast(obj);
161 162
    if (!shared.IsSubjectToDebugging() && shared.HasUncompiledData()) {
      shared.uncompiled_data().set_inferred_name(
163
          ReadOnlyRoots(isolate()).empty_string());
164
    }
165 166
  }

167
  CheckRehashability(obj);
168

169
  // Object has not yet been serialized.  Serialize it here.
170
  DCHECK(!ReadOnlyHeap::Contains(obj));
171
  ObjectSerializer object_serializer(this, obj, &sink_);
172 173 174 175
  object_serializer.Serialize();
}

void StartupSerializer::SerializeWeakReferencesAndDeferred() {
176 177 178
  // This comes right after serialization of the context snapshot, where we
  // add entries to the startup object cache of the startup snapshot. Add
  // one entry with 'undefined' to terminate the startup object cache.
179
  Object undefined = ReadOnlyRoots(isolate()).undefined_value();
180
  VisitRootPointer(Root::kStartupObjectCache, nullptr,
181
                   FullObjectSlot(&undefined));
182 183
  isolate()->heap()->IterateWeakRoots(
      this, base::EnumSet<SkipRoot>{SkipRoot::kUnserializable});
184 185 186 187
  SerializeDeferredObjects();
  Pad();
}

188 189
void StartupSerializer::SerializeStrongReferences(
    const DisallowHeapAllocation& no_gc) {
190 191 192 193
  Isolate* isolate = this->isolate();
  // No active threads.
  CHECK_NULL(isolate->thread_manager()->FirstThreadStateInUse());
  // No active or weak handles.
194
  CHECK_IMPLIES(!allow_active_isolate_for_testing(),
195
                isolate->handle_scope_implementer()->blocks()->empty());
196

197 198 199
  SanitizeIsolateScope sanitize_isolate(
      isolate, allow_active_isolate_for_testing(), no_gc);

200 201
  // Visit smi roots and immortal immovables first to make sure they end up in
  // the first page.
202
  isolate->heap()->IterateSmiRoots(this);
203 204 205
  isolate->heap()->IterateRoots(
      this,
      base::EnumSet<SkipRoot>{SkipRoot::kUnserializable, SkipRoot::kWeak});
206 207
}

208 209
SerializedHandleChecker::SerializedHandleChecker(Isolate* isolate,
                                                 std::vector<Context>* contexts)
210 211 212
    : isolate_(isolate) {
  AddToSet(isolate->heap()->serialized_objects());
  for (auto const& context : *contexts) {
213
    AddToSet(context.serialized_objects());
214 215 216
  }
}

217
bool StartupSerializer::SerializeUsingReadOnlyObjectCache(
218 219
    SnapshotByteSink* sink, HeapObject obj) {
  return read_only_serializer_->SerializeUsingReadOnlyObjectCache(sink, obj);
220 221
}

222 223
void StartupSerializer::SerializeUsingStartupObjectCache(SnapshotByteSink* sink,
                                                         HeapObject obj) {
224
  int cache_index = SerializeInObjectCache(obj);
225 226
  sink->Put(kStartupObjectCache, "StartupObjectCache");
  sink->PutInt(cache_index, "startup_object_cache_index");
227 228
}

229
void StartupSerializer::CheckNoDirtyFinalizationRegistries() {
230
  Isolate* isolate = this->isolate();
231
  CHECK(isolate->heap()->dirty_js_finalization_registries_list().IsUndefined(
232
      isolate));
233 234 235
  CHECK(
      isolate->heap()->dirty_js_finalization_registries_list_tail().IsUndefined(
          isolate));
236 237
}

238
void SerializedHandleChecker::AddToSet(FixedArray serialized) {
239 240
  int length = serialized.length();
  for (int i = 0; i < length; i++) serialized_.insert(serialized.get(i));
241 242
}

243 244
void SerializedHandleChecker::VisitRootPointers(Root root,
                                                const char* description,
245 246 247
                                                FullObjectSlot start,
                                                FullObjectSlot end) {
  for (FullObjectSlot p = start; p < end; ++p) {
248 249 250
    if (serialized_.find(*p) != serialized_.end()) continue;
    PrintF("%s handle not serialized: ",
           root == Root::kGlobalHandles ? "global" : "eternal");
251
    (*p).Print();
252
    PrintF("\n");
253 254 255 256 257 258 259 260 261 262
    ok_ = false;
  }
}

bool SerializedHandleChecker::CheckGlobalAndEternalHandles() {
  isolate_->global_handles()->IterateAllRoots(this);
  isolate_->eternal_handles()->IterateAllRoots(this);
  return ok_;
}

263 264
}  // namespace internal
}  // namespace v8