context-deserializer.cc 4.09 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5
#include "src/snapshot/context-deserializer.h"
6

7
#include "src/api/api-inl.h"
8
#include "src/heap/heap-inl.h"
9
#include "src/objects/slots.h"
10 11
#include "src/snapshot/snapshot.h"

12
namespace v8 {
13 14
namespace internal {

15
MaybeHandle<Context> ContextDeserializer::DeserializeContext(
16 17 18
    Isolate* isolate, const SnapshotData* data, bool can_rehash,
    Handle<JSGlobalProxy> global_proxy,
    v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
19
  ContextDeserializer d(data);
20 21 22 23 24 25 26 27 28 29
  d.SetRehashability(can_rehash);

  MaybeHandle<Object> maybe_result =
      d.Deserialize(isolate, global_proxy, embedder_fields_deserializer);

  Handle<Object> result;
  return maybe_result.ToHandle(&result) ? Handle<Context>::cast(result)
                                        : MaybeHandle<Context>();
}

30
MaybeHandle<Object> ContextDeserializer::Deserialize(
31 32 33
    Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
    v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
  Initialize(isolate);
34
  if (!allocator()->ReserveSpace()) {
35
    V8::FatalProcessOutOfMemory(isolate, "ContextDeserializer");
36
  }
37

38 39
  // Replace serialized references to the global proxy and its map with the
  // given global proxy and its map.
40
  AddAttachedObject(global_proxy);
41
  AddAttachedObject(handle(global_proxy->map(), isolate));
42

43 44 45 46 47 48 49 50
  Handle<Object> result;
  {
    DisallowHeapAllocation no_gc;
    // Keep track of the code space start and end pointers in case new
    // code objects were unserialized
    CodeSpace* code_space = isolate->heap()->code_space();
    Address start_address = code_space->top();
    Object root;
51
    VisitRootPointer(Root::kStartupObjectCache, nullptr, FullObjectSlot(&root));
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    DeserializeDeferredObjects();
    DeserializeEmbedderFields(embedder_fields_deserializer);

    allocator()->RegisterDeserializedObjectsForBlackAllocation();

    // There's no code deserialized here. If this assert fires then that's
    // changed and logging should be added to notify the profiler et al of the
    // new code, which also has to be flushed from instruction cache.
    CHECK_EQ(start_address, code_space->top());

    LogNewMapEvents();

    result = handle(root, isolate);
  }

67
  if (FLAG_rehash_snapshot && can_rehash()) Rehash();
68 69 70 71 72
  SetupOffHeapArrayBufferBackingStores();

  return result;
}

73
void ContextDeserializer::SetupOffHeapArrayBufferBackingStores() {
74
  for (Handle<JSArrayBuffer> buffer : new_off_heap_array_buffers()) {
75
    uint32_t store_index = buffer->GetBackingStoreRefForDeserialization();
76 77 78 79 80
    auto bs = backing_store(store_index);
    SharedFlag shared =
        bs && bs->is_shared() ? SharedFlag::kShared : SharedFlag::kNotShared;
    buffer->Setup(shared, bs);
  }
81 82
}

83
void ContextDeserializer::DeserializeEmbedderFields(
84 85 86 87 88 89 90 91 92 93
    v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
  if (!source()->HasMore() || source()->Get() != kEmbedderFieldsData) return;
  DisallowHeapAllocation no_gc;
  DisallowJavascriptExecution no_js(isolate());
  DisallowCompilation no_compile(isolate());
  DCHECK_NOT_NULL(embedder_fields_deserializer.callback);
  for (int code = source()->Get(); code != kSynchronize;
       code = source()->Get()) {
    HandleScope scope(isolate());
    int space = code & kSpaceMask;
94 95
    DCHECK_LE(space, kNumberOfSpaces);
    DCHECK_EQ(code - space, kNewObject);
96 97
    Handle<JSObject> obj(JSObject::cast(GetBackReferencedObject(
                             static_cast<SnapshotSpace>(space))),
98 99 100 101 102 103 104 105 106 107 108 109 110
                         isolate());
    int index = source()->GetInt();
    int size = source()->GetInt();
    // TODO(yangguo,jgruber): Turn this into a reusable shared buffer.
    byte* data = new byte[size];
    source()->CopyRaw(data, size);
    embedder_fields_deserializer.callback(v8::Utils::ToLocal(obj), index,
                                          {reinterpret_cast<char*>(data), size},
                                          embedder_fields_deserializer.data);
    delete[] data;
  }
}
}  // namespace internal
111
}  // namespace v8