Line data Source code
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 249 : if (owns_data_) DeleteArray(data_);
18 : }
19 :
20 : const byte* data() const { return data_; }
21 : int length() const { return length_; }
22 169 : bool rejected() const { return rejected_; }
23 :
24 30 : void Reject() { rejected_ = true; }
25 :
26 : void AcquireDataOwnership() {
27 : DCHECK(!owns_data_);
28 454 : owns_data_ = true;
29 : }
30 :
31 : void ReleaseDataOwnership() {
32 : DCHECK(owns_data_);
33 374 : 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:
47 : static ScriptCompiler::CachedData* Serialize(Handle<SharedFunctionInfo> info);
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 : uint32_t source_hash() const { return source_hash_; }
56 :
57 : protected:
58 : CodeSerializer(Isolate* isolate, uint32_t source_hash);
59 374 : ~CodeSerializer() override { OutputStatistics("CodeSerializer"); }
60 :
61 37354 : virtual bool ElideObject(Object obj) { return false; }
62 : void SerializeGeneric(HeapObject heap_object);
63 :
64 : private:
65 : void SerializeObject(HeapObject o) override;
66 :
67 : bool SerializeReadOnlyObject(HeapObject obj);
68 :
69 : DISALLOW_HEAP_ALLOCATION(no_gc_)
70 : uint32_t source_hash_;
71 : DISALLOW_COPY_AND_ASSIGN(CodeSerializer);
72 : };
73 :
74 : // Wrapper around ScriptData to provide code-serializer-specific functionality.
75 822 : class SerializedCodeData : public SerializedData {
76 : public:
77 : enum SanityCheckResult {
78 : CHECK_SUCCESS = 0,
79 : MAGIC_NUMBER_MISMATCH = 1,
80 : VERSION_MISMATCH = 2,
81 : SOURCE_MISMATCH = 3,
82 : FLAGS_MISMATCH = 5,
83 : CHECKSUM_MISMATCH = 6,
84 : INVALID_HEADER = 7,
85 : LENGTH_MISMATCH = 8
86 : };
87 :
88 : // The data header consists of uint32_t-sized entries:
89 : // [0] magic number and (internally provided) external reference count
90 : // [1] version hash
91 : // [2] source hash
92 : // [3] flag hash
93 : // [4] number of reservation size entries
94 : // [5] payload length
95 : // [6] payload checksum part A
96 : // [7] payload checksum part B
97 : // ... reservations
98 : // ... code stub keys
99 : // ... serialized payload
100 : static const uint32_t kVersionHashOffset = kMagicNumberOffset + kUInt32Size;
101 : static const uint32_t kSourceHashOffset = kVersionHashOffset + kUInt32Size;
102 : static const uint32_t kFlagHashOffset = kSourceHashOffset + kUInt32Size;
103 : static const uint32_t kNumReservationsOffset = kFlagHashOffset + kUInt32Size;
104 : static const uint32_t kPayloadLengthOffset =
105 : kNumReservationsOffset + kUInt32Size;
106 : static const uint32_t kChecksumPartAOffset =
107 : kPayloadLengthOffset + kUInt32Size;
108 : static const uint32_t kChecksumPartBOffset =
109 : kChecksumPartAOffset + kUInt32Size;
110 : static const uint32_t kUnalignedHeaderSize =
111 : kChecksumPartBOffset + kUInt32Size;
112 : static const uint32_t kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize);
113 :
114 : // Used when consuming.
115 : static SerializedCodeData FromCachedData(Isolate* isolate,
116 : ScriptData* cached_data,
117 : uint32_t expected_source_hash,
118 : SanityCheckResult* rejection_result);
119 :
120 : // Used when producing.
121 : SerializedCodeData(const std::vector<byte>* payload,
122 : const CodeSerializer* cs);
123 :
124 : // Return ScriptData object and relinquish ownership over it to the caller.
125 : ScriptData* GetScriptData();
126 :
127 : std::vector<Reservation> Reservations() const;
128 : Vector<const byte> Payload() const;
129 :
130 : static uint32_t SourceHash(Handle<String> source,
131 : ScriptOriginOptions origin_options);
132 :
133 : private:
134 : explicit SerializedCodeData(ScriptData* data);
135 : SerializedCodeData(const byte* data, int size)
136 30 : : SerializedData(const_cast<byte*>(data), size) {}
137 :
138 : Vector<const byte> ChecksummedContent() const {
139 588 : return Vector<const byte>(data_ + kHeaderSize, size_ - kHeaderSize);
140 : }
141 :
142 : SanityCheckResult SanityCheck(Isolate* isolate,
143 : uint32_t expected_source_hash) const;
144 : };
145 :
146 : } // namespace internal
147 : } // namespace v8
148 :
149 : #endif // V8_SNAPSHOT_CODE_SERIALIZER_H_
|