v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
code-serializer.h
Go to the documentation of this file.
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"
12
13namespace v8 {
14namespace internal {
15
16class PersistentHandles;
17class BackgroundMergeTask;
18
20 public:
21 AlignedCachedData(const uint8_t* data, int length);
23 if (owns_data_) DeleteArray(data_);
24 }
27
28 const uint8_t* data() const { return data_; }
29 int length() const { return length_; }
30 bool rejected() const { return rejected_; }
31
32 void Reject() { rejected_ = true; }
33
34 bool HasDataOwnership() const { return owns_data_; }
35
37 DCHECK(!owns_data_);
38 owns_data_ = true;
39 }
40
42 DCHECK(owns_data_);
43 owns_data_ = false;
44 }
45
46 private:
47 bool owns_data_ : 1;
48 bool rejected_ : 1;
49 const uint8_t* data_;
51};
52
55
56// If this fails, update the static_assert AND the code_cache_reject_reason
57// histogram definition.
58static_assert(static_cast<int>(SerializedCodeSanityCheckResult::kLast) == 9);
59
60class CodeSerializer : public Serializer {
61 public:
63 public:
64 bool HasResult() const { return !maybe_result.is_null(); }
66
67 private:
68 friend class CodeSerializer;
70 std::vector<IndirectHandle<Script>> scripts;
71 std::unique_ptr<PersistentHandles> persistent_handles;
73 };
74
78 Isolate* isolate, Handle<SharedFunctionInfo> info);
79
82
84 Deserialize(Isolate* isolate, AlignedCachedData* cached_data,
85 DirectHandle<String> source, const ScriptDetails& script_details,
86 MaybeDirectHandle<Script> maybe_cached_script = {});
87
88 V8_WARN_UNUSED_RESULT static OffThreadDeserializeData
90 AlignedCachedData* cached_data);
91
94 Isolate* isolate, OffThreadDeserializeData&& data,
95 AlignedCachedData* cached_data, DirectHandle<String> source,
96 const ScriptDetails& script_details,
97 BackgroundMergeTask* background_merge_task = nullptr);
98
99 uint32_t source_hash() const { return source_hash_; }
100
101 protected:
102 CodeSerializer(Isolate* isolate, uint32_t source_hash);
103 ~CodeSerializer() override { OutputStatistics("CodeSerializer"); }
104
105 void SerializeGeneric(Handle<HeapObject> heap_object, SlotType slot_type);
106
107 private:
108 void SerializeObjectImpl(Handle<HeapObject> o, SlotType slot_type) override;
109
111 uint32_t source_hash_;
112};
113
114// Wrapper around ScriptData to provide code-serializer-specific functionality.
116 public:
117 // The data header consists of uint32_t-sized entries:
118 static const uint32_t kVersionHashOffset = kMagicNumberOffset + kUInt32Size;
119 static const uint32_t kSourceHashOffset = kVersionHashOffset + kUInt32Size;
120 static const uint32_t kFlagHashOffset = kSourceHashOffset + kUInt32Size;
121 static const uint32_t kReadOnlySnapshotChecksumOffset =
122 kFlagHashOffset + kUInt32Size;
123 static const uint32_t kPayloadLengthOffset =
125 static const uint32_t kChecksumOffset = kPayloadLengthOffset + kUInt32Size;
126 static const uint32_t kUnalignedHeaderSize = kChecksumOffset + kUInt32Size;
127 static const uint32_t kHeaderSize = POINTER_SIZE_ALIGN(kUnalignedHeaderSize);
128
129 // Used when consuming.
130 static SerializedCodeData FromCachedData(
131 Isolate* isolate, AlignedCachedData* cached_data,
132 uint32_t expected_source_hash,
133 SerializedCodeSanityCheckResult* rejection_result);
134 // For cached data which is consumed before the source is available (e.g.
135 // off-thread).
136 static SerializedCodeData FromCachedDataWithoutSource(
137 LocalIsolate* local_isolate, AlignedCachedData* cached_data,
138 SerializedCodeSanityCheckResult* rejection_result);
139 // For cached data which was previously already sanity checked by
140 // FromCachedDataWithoutSource. The rejection result from that call should be
141 // passed into this one.
142 static SerializedCodeData FromPartiallySanityCheckedCachedData(
143 AlignedCachedData* cached_data, uint32_t expected_source_hash,
144 SerializedCodeSanityCheckResult* rejection_result);
145
146 // Used when producing.
147 SerializedCodeData(const std::vector<uint8_t>* payload,
148 const CodeSerializer* cs);
149
150 // Return ScriptData object and relinquish ownership over it to the caller.
151 AlignedCachedData* GetScriptData();
152
154
155 static uint32_t SourceHash(DirectHandle<String> source,
156 DirectHandle<FixedArray> wrapped_arguments,
157 ScriptOriginOptions origin_options);
158
159 private:
161 SerializedCodeData(const uint8_t* data, int size)
162 : SerializedData(const_cast<uint8_t*>(data), size) {}
163
165 return base::Vector<const uint8_t>(data_ + kHeaderSize,
166 size_ - kHeaderSize);
167 }
168
170 uint32_t expected_ro_snapshot_checksum,
171 uint32_t expected_source_hash) const;
172 SerializedCodeSanityCheckResult SanityCheckJustSource(
173 uint32_t expected_source_hash) const;
174 SerializedCodeSanityCheckResult SanityCheckWithoutSource(
175 uint32_t expected_ro_snapshot_checksum) const;
176};
177
178} // namespace internal
179} // namespace v8
180
181#endif // V8_SNAPSHOT_CODE_SERIALIZER_H_
#define DISALLOW_GARBAGE_COLLECTION(name)
uint8_t data_[MAX_STACK_LENGTH]
const uint8_t * data() const
AlignedCachedData & operator=(const AlignedCachedData &)=delete
AlignedCachedData(const AlignedCachedData &)=delete
static V8_WARN_UNUSED_RESULT MaybeDirectHandle< SharedFunctionInfo > FinishOffThreadDeserialize(Isolate *isolate, OffThreadDeserializeData &&data, AlignedCachedData *cached_data, DirectHandle< String > source, const ScriptDetails &script_details, BackgroundMergeTask *background_merge_task=nullptr)
void SerializeGeneric(Handle< HeapObject > heap_object, SlotType slot_type)
void SerializeObjectImpl(Handle< HeapObject > o, SlotType slot_type) override
CodeSerializer & operator=(const CodeSerializer &)=delete
static V8_EXPORT_PRIVATE ScriptCompiler::CachedData * Serialize(Isolate *isolate, Handle< SharedFunctionInfo > info)
CodeSerializer(const CodeSerializer &)=delete
AlignedCachedData * SerializeSharedFunctionInfo(Handle< SharedFunctionInfo > info)
static V8_WARN_UNUSED_RESULT OffThreadDeserializeData StartDeserializeOffThread(LocalIsolate *isolate, AlignedCachedData *cached_data)
static V8_WARN_UNUSED_RESULT MaybeDirectHandle< SharedFunctionInfo > Deserialize(Isolate *isolate, AlignedCachedData *cached_data, DirectHandle< String > source, const ScriptDetails &script_details, MaybeDirectHandle< Script > maybe_cached_script={})
base::Vector< const uint8_t > ChecksummedContent() const
SerializedCodeData(const uint8_t *data, int size)
const std::vector< uint8_t > * Payload() const
Definition serializer.h:189
void OutputStatistics(const char *name)
const int size_
Definition assembler.cc:132
#define POINTER_SIZE_ALIGN(value)
Definition globals.h:1804
DisallowGarbageCollection no_gc_
const int length_
Definition mul-fft.cc:473
void DeleteArray(T *array)
Definition allocation.h:63
v8::ScriptCompiler::CachedData::CompatibilityCheckResult SerializedCodeSanityCheckResult
constexpr int kUInt32Size
Definition globals.h:403
static const uint32_t kReadOnlySnapshotChecksumOffset
Definition snapshot.cc:96
static const uint32_t kChecksumOffset
Definition snapshot.cc:95
#define DCHECK(condition)
Definition logging.h:482
#define V8_EXPORT_PRIVATE
Definition macros.h:460
std::vector< IndirectHandle< Script > > scripts
MaybeIndirectHandle< SharedFunctionInfo > maybe_result
std::unique_ptr< PersistentHandles > persistent_handles
DirectHandle< Script > GetOnlyScript(LocalHeap *heap)
#define V8_WARN_UNUSED_RESULT
Definition v8config.h:671