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 MUST_USE_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize( 21 Isolate* isolate, ScriptData* cached_data, Handle<String> source); 22 23 String* source() const { 24 DCHECK(!AllowHeapAllocation::IsAllowed()); 25 return source_; 26 } 27 28 const List<uint32_t>* stub_keys() const { return &stub_keys_; } 29 30 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, 39 WhereToPoint where_to_point, int skip) override; 40 41 void SerializeBuiltin(int builtin_index, HowToCode how_to_code, 42 WhereToPoint where_to_point); 43 void SerializeCodeStub(Code* code_stub, HowToCode how_to_code, 44 WhereToPoint where_to_point); 45 void SerializeGeneric(HeapObject* heap_object, HowToCode how_to_code, 46 WhereToPoint where_to_point); 47 48 DisallowHeapAllocation no_gc_; 49 String* source_; 50 List<uint32_t> stub_keys_; 51 DISALLOW_COPY_AND_ASSIGN(CodeSerializer); 52 }; 53 54 // Wrapper around ScriptData to provide code-serializer-specific functionality. 55 class SerializedCodeData : public SerializedData { 56 public: 57 // Used when consuming. 58 static SerializedCodeData* FromCachedData(Isolate* isolate, 59 ScriptData* cached_data, 60 String* source); 61 62 // Used when producing. 63 SerializedCodeData(const List<byte>* payload, const CodeSerializer* cs); 64 65 // Return ScriptData object and relinquish ownership over it to the caller. 66 ScriptData* GetScriptData(); 67 68 Vector<const Reservation> Reservations() const; 69 Vector<const byte> Payload() const; 70 71 Vector<const uint32_t> CodeStubKeys() const; 72 73 private: 74 explicit SerializedCodeData(ScriptData* data); 75 76 enum SanityCheckResult { 77 CHECK_SUCCESS = 0, 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: 91 // [0] magic number and external reference count 92 // [1] version hash 93 // [2] source hash 94 // [3] cpu features 95 // [4] flag hash 96 // [5] number of code stub keys 97 // [6] number of reservation size entries 98 // [7] payload length 99 // [8] payload checksum part 1 100 // [9] payload checksum part 2 101 // ... reservations 102 // ... code stub keys 103 // ... serialized payload 104 static const int kVersionHashOffset = kMagicNumberOffset + kInt32Size; 105 static const int kSourceHashOffset = kVersionHashOffset + kInt32Size; 106 static const int kCpuFeaturesOffset = kSourceHashOffset + kInt32Size; 107 static const int kFlagHashOffset = kCpuFeaturesOffset + kInt32Size; 108 static const int kNumReservationsOffset = kFlagHashOffset + kInt32Size; 109 static const int kNumCodeStubKeysOffset = kNumReservationsOffset + kInt32Size; 110 static const int kPayloadLengthOffset = kNumCodeStubKeysOffset + kInt32Size; 111 static const int kChecksum1Offset = kPayloadLengthOffset + kInt32Size; 112 static const int kChecksum2Offset = kChecksum1Offset + kInt32Size; 113 static const int kHeaderSize = kChecksum2Offset + kInt32Size; 114 }; 115 116 } // namespace internal 117 } // namespace v8 118 119 #endif // V8_SNAPSHOT_CODE_SERIALIZER_H_ 120