OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_SNAPSHOT_CODE_SERIALIZER_H_ | 5 #ifndef V8_SNAPSHOT_CODE_SERIALIZER_H_ |
6 #define V8_SNAPSHOT_CODE_SERIALIZER_H_ | 6 #define V8_SNAPSHOT_CODE_SERIALIZER_H_ |
7 | 7 |
8 #include "src/parsing/preparse-data.h" | 8 #include "src/parsing/preparse-data.h" |
9 #include "src/snapshot/serializer.h" | 9 #include "src/snapshot/serializer.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 class CodeSerializer : public Serializer { | 14 class CodeSerializer : public Serializer { |
15 public: | 15 public: |
16 static ScriptData* Serialize(Isolate* isolate, | 16 static ScriptData* Serialize(Isolate* isolate, |
17 Handle<SharedFunctionInfo> info, | 17 Handle<SharedFunctionInfo> info, |
18 Handle<String> source); | 18 Handle<String> source); |
19 | 19 |
| 20 ScriptData* Serialize(Handle<HeapObject> obj); |
| 21 |
20 MUST_USE_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize( | 22 MUST_USE_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize( |
21 Isolate* isolate, ScriptData* cached_data, Handle<String> source); | 23 Isolate* isolate, ScriptData* cached_data, Handle<String> source); |
22 | 24 |
23 String* source() const { | 25 const List<uint32_t>* stub_keys() const { return &stub_keys_; } |
24 DCHECK(!AllowHeapAllocation::IsAllowed()); | 26 |
25 return source_; | 27 uint32_t source_hash() const { return source_hash_; } |
| 28 |
| 29 protected: |
| 30 explicit CodeSerializer(Isolate* isolate, uint32_t source_hash) |
| 31 : Serializer(isolate), source_hash_(source_hash) {} |
| 32 ~CodeSerializer() override { OutputStatistics("CodeSerializer"); } |
| 33 |
| 34 virtual void SerializeCodeObject(Code* code_object, HowToCode how_to_code, |
| 35 WhereToPoint where_to_point) { |
| 36 UNREACHABLE(); |
26 } | 37 } |
27 | 38 |
28 const List<uint32_t>* stub_keys() const { return &stub_keys_; } | 39 void SerializeGeneric(HeapObject* heap_object, HowToCode how_to_code, |
| 40 WhereToPoint where_to_point); |
29 | 41 |
30 private: | 42 private: |
31 CodeSerializer(Isolate* isolate, String* source) | |
32 : Serializer(isolate), source_(source) { | |
33 reference_map_.AddAttachedReference(source); | |
34 } | |
35 | |
36 ~CodeSerializer() override { OutputStatistics("CodeSerializer"); } | |
37 | |
38 void SerializeObject(HeapObject* o, HowToCode how_to_code, | 43 void SerializeObject(HeapObject* o, HowToCode how_to_code, |
39 WhereToPoint where_to_point, int skip) override; | 44 WhereToPoint where_to_point, int skip) override; |
40 | 45 |
41 void SerializeBuiltin(int builtin_index, HowToCode how_to_code, | 46 void SerializeBuiltin(int builtin_index, HowToCode how_to_code, |
42 WhereToPoint where_to_point); | 47 WhereToPoint where_to_point); |
43 void SerializeCodeStub(Code* code_stub, HowToCode how_to_code, | 48 void SerializeCodeStub(Code* code_stub, HowToCode how_to_code, |
44 WhereToPoint where_to_point); | 49 WhereToPoint where_to_point); |
45 void SerializeGeneric(HeapObject* heap_object, HowToCode how_to_code, | |
46 WhereToPoint where_to_point); | |
47 | 50 |
48 DisallowHeapAllocation no_gc_; | 51 DisallowHeapAllocation no_gc_; |
49 String* source_; | 52 uint32_t source_hash_; |
50 List<uint32_t> stub_keys_; | 53 List<uint32_t> stub_keys_; |
51 DISALLOW_COPY_AND_ASSIGN(CodeSerializer); | 54 DISALLOW_COPY_AND_ASSIGN(CodeSerializer); |
52 }; | 55 }; |
53 | 56 |
| 57 class WasmCompiledModuleSerializer : public CodeSerializer { |
| 58 public: |
| 59 static std::unique_ptr<ScriptData> SerializeWasmModule( |
| 60 Isolate* isolate, Handle<FixedArray> compiled_module); |
| 61 static MaybeHandle<FixedArray> DeserializeWasmModule(Isolate* isolate, |
| 62 ScriptData* data); |
| 63 |
| 64 protected: |
| 65 void SerializeCodeObject(Code* code_object, HowToCode how_to_code, |
| 66 WhereToPoint where_to_point) override { |
| 67 Code::Kind kind = code_object->kind(); |
| 68 if (kind == Code::WASM_FUNCTION || kind == Code::WASM_TO_JS_FUNCTION || |
| 69 kind == Code::JS_TO_WASM_FUNCTION) { |
| 70 SerializeGeneric(code_object, how_to_code, where_to_point); |
| 71 } else { |
| 72 UNREACHABLE(); |
| 73 } |
| 74 } |
| 75 |
| 76 private: |
| 77 WasmCompiledModuleSerializer(Isolate* isolate, uint32_t source_hash) |
| 78 : CodeSerializer(isolate, source_hash) {} |
| 79 DISALLOW_COPY_AND_ASSIGN(WasmCompiledModuleSerializer); |
| 80 }; |
| 81 |
54 // Wrapper around ScriptData to provide code-serializer-specific functionality. | 82 // Wrapper around ScriptData to provide code-serializer-specific functionality. |
55 class SerializedCodeData : public SerializedData { | 83 class SerializedCodeData : public SerializedData { |
56 public: | 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 }; |
| 94 |
57 // Used when consuming. | 95 // Used when consuming. |
58 static SerializedCodeData* FromCachedData(Isolate* isolate, | 96 static const SerializedCodeData FromCachedData( |
59 ScriptData* cached_data, | 97 Isolate* isolate, ScriptData* cached_data, uint32_t expected_source_hash, |
60 String* source); | 98 SanityCheckResult* rejection_result); |
61 | 99 |
62 // Used when producing. | 100 // Used when producing. |
63 SerializedCodeData(const List<byte>* payload, const CodeSerializer* cs); | 101 SerializedCodeData(const List<byte>* payload, const CodeSerializer* cs); |
64 | 102 |
65 // Return ScriptData object and relinquish ownership over it to the caller. | 103 // Return ScriptData object and relinquish ownership over it to the caller. |
66 ScriptData* GetScriptData(); | 104 ScriptData* GetScriptData(); |
67 | 105 |
68 Vector<const Reservation> Reservations() const; | 106 Vector<const Reservation> Reservations() const; |
69 Vector<const byte> Payload() const; | 107 Vector<const byte> Payload() const; |
70 | 108 |
71 Vector<const uint32_t> CodeStubKeys() const; | 109 Vector<const uint32_t> CodeStubKeys() const; |
72 | 110 |
| 111 static uint32_t SourceHash(Handle<String> source); |
| 112 |
73 private: | 113 private: |
74 explicit SerializedCodeData(ScriptData* data); | 114 explicit SerializedCodeData(ScriptData* data); |
| 115 SerializedCodeData(const byte* data, int size) |
| 116 : SerializedData(const_cast<byte*>(data), size) {} |
75 | 117 |
76 enum SanityCheckResult { | 118 SanityCheckResult SanityCheck(Isolate* isolate, |
77 CHECK_SUCCESS = 0, | 119 uint32_t expected_source_hash) const; |
78 MAGIC_NUMBER_MISMATCH = 1, | |
79 VERSION_MISMATCH = 2, | |
80 SOURCE_MISMATCH = 3, | |
81 CPU_FEATURES_MISMATCH = 4, | |
82 FLAGS_MISMATCH = 5, | |
83 CHECKSUM_MISMATCH = 6 | |
84 }; | |
85 | |
86 SanityCheckResult SanityCheck(Isolate* isolate, String* source) const; | |
87 | |
88 uint32_t SourceHash(String* source) const; | |
89 | |
90 // The data header consists of uint32_t-sized entries: | 120 // The data header consists of uint32_t-sized entries: |
91 // [0] magic number and external reference count | 121 // [0] magic number and external reference count |
92 // [1] version hash | 122 // [1] version hash |
93 // [2] source hash | 123 // [2] source hash |
94 // [3] cpu features | 124 // [3] cpu features |
95 // [4] flag hash | 125 // [4] flag hash |
96 // [5] number of code stub keys | 126 // [5] number of code stub keys |
97 // [6] number of reservation size entries | 127 // [6] number of reservation size entries |
98 // [7] payload length | 128 // [7] payload length |
99 // [8] payload checksum part 1 | 129 // [8] payload checksum part 1 |
(...skipping 10 matching lines...) Expand all Loading... |
110 static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size; | 140 static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size; |
111 static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size; | 141 static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size; |
112 static const int kChecksum2Offset = kChecksum1Offset + kInt32Size; | 142 static const int kChecksum2Offset = kChecksum1Offset + kInt32Size; |
113 static const int kHeaderSize = kChecksum2Offset + kInt32Size; | 143 static const int kHeaderSize = kChecksum2Offset + kInt32Size; |
114 }; | 144 }; |
115 | 145 |
116 } // namespace internal | 146 } // namespace internal |
117 } // namespace v8 | 147 } // namespace v8 |
118 | 148 |
119 #endif // V8_SNAPSHOT_CODE_SERIALIZER_H_ | 149 #endif // V8_SNAPSHOT_CODE_SERIALIZER_H_ |
OLD | NEW |