V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
code-serializer.h
1 // Copyright 2016 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_CODE_SERIALIZER_H_
6 #define V8_SNAPSHOT_CODE_SERIALIZER_H_
7 
8 #include "src/snapshot/serializer.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 class ScriptData {
14  public:
15  ScriptData(const byte* data, int length);
16  ~ScriptData() {
17  if (owns_data_) DeleteArray(data_);
18  }
19 
20  const byte* data() const { return data_; }
21  int length() const { return length_; }
22  bool rejected() const { return rejected_; }
23 
24  void Reject() { rejected_ = true; }
25 
26  void AcquireDataOwnership() {
27  DCHECK(!owns_data_);
28  owns_data_ = true;
29  }
30 
31  void ReleaseDataOwnership() {
32  DCHECK(owns_data_);
33  owns_data_ = false;
34  }
35 
36  private:
37  bool owns_data_ : 1;
38  bool rejected_ : 1;
39  const byte* data_;
40  int length_;
41 
42  DISALLOW_COPY_AND_ASSIGN(ScriptData);
43 };
44 
45 class CodeSerializer : public Serializer {
46  public:
48 
49  ScriptData* SerializeSharedFunctionInfo(Handle<SharedFunctionInfo> info);
50 
51  V8_WARN_UNUSED_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize(
52  Isolate* isolate, ScriptData* cached_data, Handle<String> source,
53  ScriptOriginOptions origin_options);
54 
55  const std::vector<uint32_t>* stub_keys() const { return &stub_keys_; }
56 
57  uint32_t source_hash() const { return source_hash_; }
58 
59  protected:
60  CodeSerializer(Isolate* isolate, uint32_t source_hash);
61  ~CodeSerializer() override { OutputStatistics("CodeSerializer"); }
62 
63  virtual void SerializeCodeObject(Code code_object, HowToCode how_to_code,
64  WhereToPoint where_to_point) {
65  UNREACHABLE();
66  }
67 
68  virtual bool ElideObject(Object* obj) { return false; }
69  void SerializeGeneric(HeapObject* heap_object, HowToCode how_to_code,
70  WhereToPoint where_to_point);
71 
72  private:
73  void SerializeObject(HeapObject* o, HowToCode how_to_code,
74  WhereToPoint where_to_point, int skip) override;
75 
76  void SerializeCodeStub(Code code_stub, HowToCode how_to_code,
77  WhereToPoint where_to_point);
78 
79  bool SerializeReadOnlyObject(HeapObject* obj, HowToCode how_to_code,
80  WhereToPoint where_to_point, int skip);
81 
82  DISALLOW_HEAP_ALLOCATION(no_gc_);
83  uint32_t source_hash_;
84  std::vector<uint32_t> stub_keys_;
85  DISALLOW_COPY_AND_ASSIGN(CodeSerializer);
86 };
87 
88 // Wrapper around ScriptData to provide code-serializer-specific functionality.
90  public:
91  enum SanityCheckResult {
92  CHECK_SUCCESS = 0,
93  MAGIC_NUMBER_MISMATCH = 1,
94  VERSION_MISMATCH = 2,
95  SOURCE_MISMATCH = 3,
96  CPU_FEATURES_MISMATCH = 4,
97  FLAGS_MISMATCH = 5,
98  CHECKSUM_MISMATCH = 6,
99  INVALID_HEADER = 7,
100  LENGTH_MISMATCH = 8
101  };
102 
103  // The data header consists of uint32_t-sized entries:
104  // [0] magic number and (internally provided) external reference count
105  // [1] version hash
106  // [2] source hash
107  // [3] cpu features
108  // [4] flag hash
109  // [5] number of reservation size entries
110  // [6] number of code stub keys
111  // [7] payload length
112  // [8] payload checksum part A
113  // [9] payload checksum part B
114  // ... reservations
115  // ... code stub keys
116  // ... serialized payload
117  static const uint32_t kVersionHashOffset = kMagicNumberOffset + kUInt32Size;
118  static const uint32_t kSourceHashOffset = kVersionHashOffset + kUInt32Size;
119  static const uint32_t kCpuFeaturesOffset = kSourceHashOffset + kUInt32Size;
120  static const uint32_t kFlagHashOffset = kCpuFeaturesOffset + kUInt32Size;
121  static const uint32_t kNumReservationsOffset = kFlagHashOffset + kUInt32Size;
122  static const uint32_t kNumCodeStubKeysOffset =
123  kNumReservationsOffset + kUInt32Size;
124  static const uint32_t kPayloadLengthOffset =
125  kNumCodeStubKeysOffset + kUInt32Size;
126  static const uint32_t kChecksumPartAOffset =
127  kPayloadLengthOffset + kUInt32Size;
128  static const uint32_t kChecksumPartBOffset =
129  kChecksumPartAOffset + kUInt32Size;
130  static const uint32_t kUnalignedHeaderSize =
131  kChecksumPartBOffset + kUInt32Size;
132  static const uint32_t kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize);
133 
134  // Used when consuming.
135  static SerializedCodeData FromCachedData(Isolate* isolate,
136  ScriptData* cached_data,
137  uint32_t expected_source_hash,
138  SanityCheckResult* rejection_result);
139 
140  // Used when producing.
141  SerializedCodeData(const std::vector<byte>* payload,
142  const CodeSerializer* cs);
143 
144  // Return ScriptData object and relinquish ownership over it to the caller.
145  ScriptData* GetScriptData();
146 
147  std::vector<Reservation> Reservations() const;
148  Vector<const byte> Payload() const;
149 
150  Vector<const uint32_t> CodeStubKeys() const;
151 
152  static uint32_t SourceHash(Handle<String> source,
153  ScriptOriginOptions origin_options);
154 
155  private:
156  explicit SerializedCodeData(ScriptData* data);
157  SerializedCodeData(const byte* data, int size)
158  : SerializedData(const_cast<byte*>(data), size) {}
159 
160  Vector<const byte> ChecksummedContent() const {
161  return Vector<const byte>(data_ + kHeaderSize, size_ - kHeaderSize);
162  }
163 
164  SanityCheckResult SanityCheck(Isolate* isolate,
165  uint32_t expected_source_hash) const;
166 };
167 
168 } // namespace internal
169 } // namespace v8
170 
171 #endif // V8_SNAPSHOT_CODE_SERIALIZER_H_
Definition: libplatform.h:13