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 List<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 557 : : Serializer(isolate), source_hash_(source_hash) {}
32 1114 : ~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 49058 : virtual bool ElideObject(Object* obj) { return false; }
40 : void SerializeGeneric(HeapObject* heap_object, HowToCode how_to_code,
41 : WhereToPoint where_to_point);
42 : void SerializeBuiltin(int builtin_index, HowToCode how_to_code,
43 : WhereToPoint where_to_point);
44 :
45 : private:
46 : void SerializeObject(HeapObject* o, HowToCode how_to_code,
47 : WhereToPoint where_to_point, int skip) override;
48 :
49 : void SerializeCodeStub(Code* code_stub, HowToCode how_to_code,
50 : WhereToPoint where_to_point);
51 :
52 : DisallowHeapAllocation no_gc_;
53 : uint32_t source_hash_;
54 : List<uint32_t> stub_keys_;
55 : DISALLOW_COPY_AND_ASSIGN(CodeSerializer);
56 : };
57 :
58 150 : class WasmCompiledModuleSerializer : public CodeSerializer {
59 : public:
60 : static std::unique_ptr<ScriptData> SerializeWasmModule(
61 : Isolate* isolate, Handle<FixedArray> compiled_module);
62 : static MaybeHandle<FixedArray> DeserializeWasmModule(
63 : Isolate* isolate, ScriptData* data, Vector<const byte> wire_bytes);
64 :
65 : protected:
66 : void SerializeCodeObject(Code* code_object, HowToCode how_to_code,
67 : WhereToPoint where_to_point) override;
68 : bool ElideObject(Object* obj) override;
69 :
70 : private:
71 : WasmCompiledModuleSerializer(Isolate* isolate, uint32_t source_hash,
72 : Handle<Context> native_context,
73 : Handle<SeqOneByteString> module_bytes);
74 : DISALLOW_COPY_AND_ASSIGN(WasmCompiledModuleSerializer);
75 : };
76 :
77 : // Wrapper around ScriptData to provide code-serializer-specific functionality.
78 : class SerializedCodeData : public SerializedData {
79 : public:
80 : enum SanityCheckResult {
81 : CHECK_SUCCESS = 0,
82 : MAGIC_NUMBER_MISMATCH = 1,
83 : VERSION_MISMATCH = 2,
84 : SOURCE_MISMATCH = 3,
85 : CPU_FEATURES_MISMATCH = 4,
86 : FLAGS_MISMATCH = 5,
87 : CHECKSUM_MISMATCH = 6,
88 : INVALID_HEADER = 7,
89 : LENGTH_MISMATCH = 8
90 : };
91 :
92 : // The data header consists of uint32_t-sized entries:
93 : // [0] magic number and (internally provided) external reference count
94 : // [1] extra (API-provided) external reference count
95 : // [2] version hash
96 : // [3] source hash
97 : // [4] cpu features
98 : // [5] flag hash
99 : // [6] number of code stub keys
100 : // [7] number of reservation size entries
101 : // [8] payload length
102 : // [9] payload checksum part 1
103 : // [10] payload checksum part 2
104 : // ... reservations
105 : // ... code stub keys
106 : // ... serialized payload
107 : static const int kSourceHashOffset = kVersionHashOffset + kInt32Size;
108 : static const int kCpuFeaturesOffset = kSourceHashOffset + kInt32Size;
109 : static const int kFlagHashOffset = kCpuFeaturesOffset + kInt32Size;
110 : static const int kNumReservationsOffset = kFlagHashOffset + kInt32Size;
111 : static const int kNumCodeStubKeysOffset = kNumReservationsOffset + kInt32Size;
112 : static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size;
113 : static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size;
114 : static const int kChecksum2Offset = kChecksum1Offset + kInt32Size;
115 : static const int kUnalignedHeaderSize = kChecksum2Offset + kInt32Size;
116 : static const int kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize);
117 :
118 : // Used when consuming.
119 : static SerializedCodeData FromCachedData(Isolate* isolate,
120 : ScriptData* cached_data,
121 : uint32_t expected_source_hash,
122 : SanityCheckResult* rejection_result);
123 :
124 : // Used when producing.
125 : SerializedCodeData(const List<byte>* payload, const CodeSerializer* cs);
126 :
127 : // Return ScriptData object and relinquish ownership over it to the caller.
128 : ScriptData* GetScriptData();
129 :
130 : Vector<const Reservation> Reservations() const;
131 : Vector<const byte> Payload() const;
132 :
133 : Vector<const uint32_t> CodeStubKeys() const;
134 :
135 : static uint32_t SourceHash(Handle<String> source);
136 :
137 : private:
138 : explicit SerializedCodeData(ScriptData* data);
139 : SerializedCodeData(const byte* data, int size)
140 : : SerializedData(const_cast<byte*>(data), size) {}
141 :
142 : Vector<const byte> DataWithoutHeader() const {
143 948 : return Vector<const byte>(data_ + kHeaderSize, size_ - kHeaderSize);
144 : }
145 :
146 : SanityCheckResult SanityCheck(Isolate* isolate,
147 : uint32_t expected_source_hash) const;
148 : };
149 :
150 : } // namespace internal
151 : } // namespace v8
152 :
153 : #endif // V8_SNAPSHOT_CODE_SERIALIZER_H_
|