• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/macros.h"
9 #include "src/snapshot/serializer.h"
10 #include "src/snapshot/snapshot-data.h"
11 
12 namespace v8 {
13 namespace internal {
14 
15 class V8_EXPORT_PRIVATE ScriptData {
16  public:
17   ScriptData(const byte* data, int length);
~ScriptData()18   ~ScriptData() {
19     if (owns_data_) DeleteArray(data_);
20   }
21   ScriptData(const ScriptData&) = delete;
22   ScriptData& operator=(const ScriptData&) = delete;
23 
data()24   const byte* data() const { return data_; }
length()25   int length() const { return length_; }
rejected()26   bool rejected() const { return rejected_; }
27 
Reject()28   void Reject() { rejected_ = true; }
29 
AcquireDataOwnership()30   void AcquireDataOwnership() {
31     DCHECK(!owns_data_);
32     owns_data_ = true;
33   }
34 
ReleaseDataOwnership()35   void ReleaseDataOwnership() {
36     DCHECK(owns_data_);
37     owns_data_ = false;
38   }
39 
40  private:
41   bool owns_data_ : 1;
42   bool rejected_ : 1;
43   const byte* data_;
44   int length_;
45 };
46 
47 class CodeSerializer : public Serializer {
48  public:
49   CodeSerializer(const CodeSerializer&) = delete;
50   CodeSerializer& operator=(const CodeSerializer&) = delete;
51   V8_EXPORT_PRIVATE static ScriptCompiler::CachedData* Serialize(
52       Handle<SharedFunctionInfo> info);
53 
54   ScriptData* SerializeSharedFunctionInfo(Handle<SharedFunctionInfo> info);
55 
56   V8_WARN_UNUSED_RESULT static MaybeHandle<SharedFunctionInfo> Deserialize(
57       Isolate* isolate, ScriptData* cached_data, Handle<String> source,
58       ScriptOriginOptions origin_options);
59 
source_hash()60   uint32_t source_hash() const { return source_hash_; }
61 
62  protected:
63   CodeSerializer(Isolate* isolate, uint32_t source_hash);
~CodeSerializer()64   ~CodeSerializer() override { OutputStatistics("CodeSerializer"); }
65 
ElideObject(Object obj)66   virtual bool ElideObject(Object obj) { return false; }
67   void SerializeGeneric(Handle<HeapObject> heap_object);
68 
69  private:
70   void SerializeObjectImpl(Handle<HeapObject> o) override;
71 
72   bool SerializeReadOnlyObject(Handle<HeapObject> obj);
73 
74   DISALLOW_HEAP_ALLOCATION(no_gc_)
75   uint32_t source_hash_;
76 };
77 
78 // Wrapper around ScriptData to provide code-serializer-specific functionality.
79 class SerializedCodeData : public SerializedData {
80  public:
81   enum SanityCheckResult {
82     CHECK_SUCCESS = 0,
83     MAGIC_NUMBER_MISMATCH = 1,
84     VERSION_MISMATCH = 2,
85     SOURCE_MISMATCH = 3,
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] version hash
95   // [2] source hash
96   // [3] flag hash
97   // [4] payload length
98   // [5] payload checksum
99   // ...  serialized payload
100   static const uint32_t kVersionHashOffset = kMagicNumberOffset + kUInt32Size;
101   static const uint32_t kSourceHashOffset = kVersionHashOffset + kUInt32Size;
102   static const uint32_t kFlagHashOffset = kSourceHashOffset + kUInt32Size;
103   static const uint32_t kPayloadLengthOffset = kFlagHashOffset + kUInt32Size;
104   static const uint32_t kChecksumOffset = kPayloadLengthOffset + kUInt32Size;
105   static const uint32_t kUnalignedHeaderSize = kChecksumOffset + kUInt32Size;
106   static const uint32_t kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize);
107 
108   // Used when consuming.
109   static SerializedCodeData FromCachedData(ScriptData* cached_data,
110                                            uint32_t expected_source_hash,
111                                            SanityCheckResult* rejection_result);
112 
113   // Used when producing.
114   SerializedCodeData(const std::vector<byte>* payload,
115                      const CodeSerializer* cs);
116 
117   // Return ScriptData object and relinquish ownership over it to the caller.
118   ScriptData* GetScriptData();
119 
120   Vector<const byte> Payload() const;
121 
122   static uint32_t SourceHash(Handle<String> source,
123                              ScriptOriginOptions origin_options);
124 
125  private:
126   explicit SerializedCodeData(ScriptData* data);
127   SerializedCodeData(const byte* data, int size)
128       : SerializedData(const_cast<byte*>(data), size) {}
129 
130   Vector<const byte> ChecksummedContent() const {
131     return Vector<const byte>(data_ + kHeaderSize, size_ - kHeaderSize);
132   }
133 
134   SanityCheckResult SanityCheck(uint32_t expected_source_hash) const;
135 };
136 
137 }  // namespace internal
138 }  // namespace v8
139 
140 #endif  // V8_SNAPSHOT_CODE_SERIALIZER_H_
141