context-deserializer.cc 3.55 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/common/assert-scope.h"
9
#include "src/heap/heap-inl.h"
10
#include "src/objects/js-array-buffer-inl.h"
11
#include "src/objects/slots.h"
12 13
#include "src/snapshot/snapshot.h"

14
namespace v8 {
15 16
namespace internal {

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

  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>();
}

31
MaybeHandle<Object> ContextDeserializer::Deserialize(
32 33
    Isolate* isolate, Handle<JSGlobalProxy> global_proxy,
    v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
34 35
  // Replace serialized references to the global proxy and its map with the
  // given global proxy and its map.
36
  AddAttachedObject(global_proxy);
37
  AddAttachedObject(handle(global_proxy->map(), isolate));
38

39 40
  Handle<Object> result;
  {
41 42 43 44 45 46
    // 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.
    DisallowCodeAllocation no_code_allocation;

    result = ReadObject();
47 48 49 50
    DeserializeDeferredObjects();
    DeserializeEmbedderFields(embedder_fields_deserializer);

    LogNewMapEvents();
51
    WeakenDescriptorArrays();
52 53
  }

54
  if (FLAG_rehash_snapshot && can_rehash()) Rehash();
55 56 57 58 59
  SetupOffHeapArrayBufferBackingStores();

  return result;
}

60
void ContextDeserializer::SetupOffHeapArrayBufferBackingStores() {
61
  for (Handle<JSArrayBuffer> buffer : new_off_heap_array_buffers()) {
62
    uint32_t store_index = buffer->GetBackingStoreRefForDeserialization();
63
    auto bs = backing_store(store_index);
64 65
    // TODO(v8:11111): Support RAB / GSAB.
    CHECK(!buffer->is_resizable());
66 67
    SharedFlag shared =
        bs && bs->is_shared() ? SharedFlag::kShared : SharedFlag::kNotShared;
68
    buffer->Setup(shared, ResizableFlag::kNotResizable, bs);
69
  }
70 71
}

72
void ContextDeserializer::DeserializeEmbedderFields(
73 74
    v8::DeserializeEmbedderFieldsCallback embedder_fields_deserializer) {
  if (!source()->HasMore() || source()->Get() != kEmbedderFieldsData) return;
75
  DisallowGarbageCollection no_gc;
76 77 78 79 80 81
  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());
82
    Handle<JSObject> obj = Handle<JSObject>::cast(GetBackReferencedObject());
83 84 85 86 87 88 89 90 91 92 93 94
    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
95
}  // namespace v8