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