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