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/parsing/preparse-data.h"
9 : #include "src/snapshot/serializer.h"
10 :
11 : namespace v8 {
12 : namespace internal {
13 :
14 : class CodeSerializer : public Serializer<> {
15 : public:
16 : static ScriptData* Serialize(Isolate* isolate,
17 : Handle<SharedFunctionInfo> info,
18 : Handle<String> source);
19 :
20 : ScriptData* Serialize(Handle<HeapObject> obj);
21 :
22 : MUST_USE_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize(
23 : Isolate* isolate, ScriptData* cached_data, Handle<String> source);
24 :
25 : const std::vector<uint32_t>* stub_keys() const { return &stub_keys_; }
26 :
27 : uint32_t source_hash() const { return source_hash_; }
28 :
29 : protected:
30 : explicit CodeSerializer(Isolate* isolate, uint32_t source_hash)
31 418 : : Serializer(isolate), source_hash_(source_hash) {}
32 836 : ~CodeSerializer() override { OutputStatistics("CodeSerializer"); }
33 :
34 0 : virtual void SerializeCodeObject(Code* code_object, HowToCode how_to_code,
35 : WhereToPoint where_to_point) {
36 0 : UNREACHABLE();
37 : }
38 :
39 48837 : virtual bool ElideObject(Object* obj) { return false; }
40 : void SerializeGeneric(HeapObject* heap_object, HowToCode how_to_code,
41 : WhereToPoint where_to_point);
42 :
43 : private:
44 : void SerializeObject(HeapObject* o, HowToCode how_to_code,
45 : WhereToPoint where_to_point, int skip) override;
46 :
47 : void SerializeCodeStub(Code* code_stub, HowToCode how_to_code,
48 : WhereToPoint where_to_point);
49 :
50 : DisallowHeapAllocation no_gc_;
51 : uint32_t source_hash_;
52 : std::vector<uint32_t> stub_keys_;
53 : DISALLOW_COPY_AND_ASSIGN(CodeSerializer);
54 : };
55 :
56 113 : class WasmCompiledModuleSerializer : public CodeSerializer {
57 : public:
58 : static std::unique_ptr<ScriptData> SerializeWasmModule(
59 : Isolate* isolate, Handle<FixedArray> compiled_module);
60 : static MaybeHandle<FixedArray> DeserializeWasmModule(
61 : Isolate* isolate, ScriptData* data, Vector<const byte> wire_bytes);
62 :
63 : protected:
64 : void SerializeCodeObject(Code* code_object, HowToCode how_to_code,
65 : WhereToPoint where_to_point) override;
66 : bool ElideObject(Object* obj) override;
67 :
68 : private:
69 : WasmCompiledModuleSerializer(Isolate* isolate, uint32_t source_hash,
70 : Handle<Context> native_context,
71 : Handle<SeqOneByteString> module_bytes);
72 : DISALLOW_COPY_AND_ASSIGN(WasmCompiledModuleSerializer);
73 : };
74 :
75 : // Wrapper around ScriptData to provide code-serializer-specific functionality.
76 1079 : class SerializedCodeData : public SerializedData {
77 : public:
78 : enum SanityCheckResult {
79 : CHECK_SUCCESS = 0,
80 : MAGIC_NUMBER_MISMATCH = 1,
81 : VERSION_MISMATCH = 2,
82 : SOURCE_MISMATCH = 3,
83 : CPU_FEATURES_MISMATCH = 4,
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] extra (API-provided) external reference count
93 : // [2] version hash
94 : // [3] source hash
95 : // [4] cpu features
96 : // [5] flag hash
97 : // [6] number of code stub keys
98 : // [7] number of reservation size entries
99 : // [8] payload length
100 : // [9] payload checksum part 1
101 : // [10] payload checksum part 2
102 : // ... reservations
103 : // ... code stub keys
104 : // ... serialized payload
105 : static const uint32_t kVersionHashOffset = kMagicNumberOffset + kUInt32Size;
106 : static const uint32_t kSourceHashOffset = kVersionHashOffset + kUInt32Size;
107 : static const uint32_t kCpuFeaturesOffset = kSourceHashOffset + kUInt32Size;
108 : static const uint32_t kFlagHashOffset = kCpuFeaturesOffset + kUInt32Size;
109 : static const uint32_t kNumReservationsOffset = kFlagHashOffset + kUInt32Size;
110 : static const uint32_t kNumCodeStubKeysOffset =
111 : kNumReservationsOffset + kUInt32Size;
112 : static const uint32_t kPayloadLengthOffset =
113 : kNumCodeStubKeysOffset + kUInt32Size;
114 : static const uint32_t kChecksum1Offset = kPayloadLengthOffset + kUInt32Size;
115 : static const uint32_t kChecksum2Offset = kChecksum1Offset + kUInt32Size;
116 : static const uint32_t kUnalignedHeaderSize = kChecksum2Offset + kUInt32Size;
117 : static const uint32_t kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize);
118 :
119 : // Used when consuming.
120 : static SerializedCodeData FromCachedData(Isolate* isolate,
121 : ScriptData* cached_data,
122 : uint32_t expected_source_hash,
123 : SanityCheckResult* rejection_result);
124 :
125 : // Used when producing.
126 : SerializedCodeData(const std::vector<byte>* payload,
127 : const CodeSerializer* cs);
128 :
129 : // Return ScriptData object and relinquish ownership over it to the caller.
130 : ScriptData* GetScriptData();
131 :
132 : Vector<const Reservation> Reservations() const;
133 : Vector<const byte> Payload() const;
134 :
135 : Vector<const uint32_t> CodeStubKeys() const;
136 :
137 : static uint32_t SourceHash(Handle<String> source);
138 :
139 : private:
140 : explicit SerializedCodeData(ScriptData* data);
141 : SerializedCodeData(const byte* data, int size)
142 61 : : SerializedData(const_cast<byte*>(data), size) {}
143 :
144 : Vector<const byte> DataWithoutHeader() const {
145 723 : return Vector<const byte>(data_ + kHeaderSize, size_ - kHeaderSize);
146 : }
147 :
148 : SanityCheckResult SanityCheck(Isolate* isolate,
149 : uint32_t expected_source_hash) const;
150 : };
151 :
152 : } // namespace internal
153 : } // namespace v8
154 :
155 : #endif // V8_SNAPSHOT_CODE_SERIALIZER_H_
|