V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
embedded-data.h
1 // Copyright 2018 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_SNAPSHOT_EMBEDDED_DATA_H_
6 #define V8_SNAPSHOT_EMBEDDED_DATA_H_
7 
8 #include "src/base/macros.h"
9 #include "src/builtins/builtins.h"
10 #include "src/globals.h"
11 #include "src/isolate.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class Code;
17 class Isolate;
18 
19 // Wraps an off-heap instruction stream.
20 // TODO(jgruber,v8:6666): Remove this class.
21 class InstructionStream final : public AllStatic {
22  public:
23  // Returns true, iff the given pc points into an off-heap instruction stream.
24  static bool PcIsOffHeap(Isolate* isolate, Address pc);
25 
26  // Returns the corresponding Code object if it exists, and nullptr otherwise.
27  static Code TryLookupCode(Isolate* isolate, Address address);
28 
29  // During snapshot creation, we first create an executable off-heap area
30  // containing all off-heap code. The area is guaranteed to be contiguous.
31  // Note that this only applies when building the snapshot, e.g. for
32  // mksnapshot. Otherwise, off-heap code is embedded directly into the binary.
33  static void CreateOffHeapInstructionStream(Isolate* isolate, uint8_t** data,
34  uint32_t* size);
35  static void FreeOffHeapInstructionStream(uint8_t* data, uint32_t size);
36 };
37 
38 class EmbeddedData final {
39  public:
40  static EmbeddedData FromIsolate(Isolate* isolate);
41 
42  static EmbeddedData FromBlob() {
43  return EmbeddedData(Isolate::CurrentEmbeddedBlob(),
44  Isolate::CurrentEmbeddedBlobSize());
45  }
46 
47  static EmbeddedData FromBlob(Isolate* isolate) {
48  return EmbeddedData(isolate->embedded_blob(),
49  isolate->embedded_blob_size());
50  }
51 
52  const uint8_t* data() const { return data_; }
53  uint32_t size() const { return size_; }
54 
55  void Dispose() { delete[] data_; }
56 
57  Address InstructionStartOfBuiltin(int i) const;
58  uint32_t InstructionSizeOfBuiltin(int i) const;
59 
60  bool ContainsBuiltin(int i) const { return InstructionSizeOfBuiltin(i) > 0; }
61 
62  uint32_t AddressForHashing(Address addr) {
63  Address start = reinterpret_cast<Address>(data_);
64  DCHECK(IsInRange(addr, start, start + size_));
65  return static_cast<uint32_t>(addr - start);
66  }
67 
68  // Padded with kCodeAlignment.
69  uint32_t PaddedInstructionSizeOfBuiltin(int i) const {
70  uint32_t size = InstructionSizeOfBuiltin(i);
71  return (size == 0) ? 0 : PadAndAlign(size);
72  }
73 
74  size_t CreateHash() const;
75  size_t Hash() const {
76  return *reinterpret_cast<const size_t*>(data_ + HashOffset());
77  }
78 
79  struct Metadata {
80  // Blob layout information.
81  uint32_t instructions_offset;
82  uint32_t instructions_length;
83  };
84  STATIC_ASSERT(offsetof(Metadata, instructions_offset) == 0);
85  STATIC_ASSERT(offsetof(Metadata, instructions_length) == kUInt32Size);
86  STATIC_ASSERT(sizeof(Metadata) == kUInt32Size + kUInt32Size);
87 
88  // The layout of the blob is as follows:
89  //
90  // [0] hash of the remaining blob
91  // [1] metadata of instruction stream 0
92  // ... metadata
93  // ... instruction streams
94 
95  static constexpr uint32_t kTableSize = Builtins::builtin_count;
96  static constexpr uint32_t HashOffset() { return 0; }
97  static constexpr uint32_t HashSize() { return kSizetSize; }
98  static constexpr uint32_t MetadataOffset() {
99  return HashOffset() + HashSize();
100  }
101  static constexpr uint32_t MetadataSize() {
102  return sizeof(struct Metadata) * kTableSize;
103  }
104  static constexpr uint32_t RawDataOffset() {
105  return PadAndAlign(MetadataOffset() + MetadataSize());
106  }
107 
108  private:
109  EmbeddedData(const uint8_t* data, uint32_t size) : data_(data), size_(size) {
110  DCHECK_NOT_NULL(data);
111  DCHECK_LT(0, size);
112  }
113 
114  const Metadata* Metadata() const {
115  return reinterpret_cast<const struct Metadata*>(data_ + MetadataOffset());
116  }
117  const uint8_t* RawData() const { return data_ + RawDataOffset(); }
118 
119  static constexpr int PadAndAlign(int size) {
120  // Ensure we have at least one byte trailing the actual builtin
121  // instructions which we can later fill with int3.
122  return RoundUp<kCodeAlignment>(size + 1);
123  }
124 
125  void PrintStatistics() const;
126 
127  const uint8_t* data_;
128  uint32_t size_;
129 };
130 
131 } // namespace internal
132 } // namespace v8
133 
134 #endif // V8_SNAPSHOT_EMBEDDED_DATA_H_
Definition: libplatform.h:13