5 #include "src/snapshot/code-serializer.h" 7 #include "src/code-stubs.h" 8 #include "src/counters.h" 9 #include "src/debug/debug.h" 11 #include "src/macro-assembler.h" 12 #include "src/objects-inl.h" 13 #include "src/objects/slots.h" 14 #include "src/snapshot/object-deserializer.h" 15 #include "src/snapshot/snapshot.h" 16 #include "src/version.h" 17 #include "src/visitors.h" 22 ScriptData::ScriptData(
const byte* data,
int length)
23 : owns_data_(false), rejected_(false), data_(data), length_(length) {
24 if (!IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment)) {
25 byte* copy = NewArray<byte>(length);
26 DCHECK(IsAligned(reinterpret_cast<intptr_t>(copy), kPointerAlignment));
27 CopyBytes(copy, data, length);
29 AcquireDataOwnership();
33 CodeSerializer::CodeSerializer(Isolate* isolate,
uint32_t source_hash)
34 : Serializer(isolate), source_hash_(source_hash) {
35 allocator()->UseCustomChunkSize(FLAG_serialization_chunk_size);
39 ScriptCompiler::CachedData* CodeSerializer::Serialize(
40 Handle<SharedFunctionInfo> info) {
41 Isolate* isolate = info->GetIsolate();
42 TRACE_EVENT_CALL_STATS_SCOPED(isolate,
"v8",
"V8.Execute");
43 HistogramTimerScope histogram_timer(isolate->counters()->compile_serialize());
44 RuntimeCallTimerScope runtimeTimer(isolate,
45 RuntimeCallCounterId::kCompileSerialize);
46 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT(
"v8.compile"),
"V8.CompileSerialize");
48 base::ElapsedTimer timer;
49 if (FLAG_profile_deserialization) timer.Start();
50 Handle<Script> script(Script::cast(info->script()), isolate);
51 if (FLAG_trace_serializer) {
52 PrintF(
"[Serializing from");
53 script->name()->ShortPrint();
58 if (script->ContainsAsmModule())
return nullptr;
60 isolate->heap()->read_only_space()->ClearStringPaddingIfNeeded();
63 Handle<String> source(String::cast(script->source()), isolate);
64 CodeSerializer cs(isolate, SerializedCodeData::SourceHash(
65 source, script->origin_options()));
66 DisallowHeapAllocation no_gc;
67 cs.reference_map()->AddAttachedReference(*source);
68 ScriptData* script_data = cs.SerializeSharedFunctionInfo(info);
70 if (FLAG_profile_deserialization) {
71 double ms = timer.Elapsed().InMillisecondsF();
72 int length = script_data->length();
73 PrintF(
"[Serializing to %d bytes took %0.3f ms]\n", length, ms);
76 ScriptCompiler::CachedData* result =
77 new ScriptCompiler::CachedData(script_data->data(), script_data->length(),
78 ScriptCompiler::CachedData::BufferOwned);
79 script_data->ReleaseDataOwnership();
85 ScriptData* CodeSerializer::SerializeSharedFunctionInfo(
86 Handle<SharedFunctionInfo> info) {
87 DisallowHeapAllocation no_gc;
89 VisitRootPointer(Root::kHandleScope,
nullptr, ObjectSlot(info.location()));
90 SerializeDeferredObjects();
93 SerializedCodeData data(sink_.data(),
this);
95 return data.GetScriptData();
98 bool CodeSerializer::SerializeReadOnlyObject(HeapObject* obj,
99 HowToCode how_to_code,
100 WhereToPoint where_to_point,
102 PagedSpace* read_only_space = isolate()->heap()->read_only_space();
103 if (!read_only_space->Contains(obj))
return false;
108 Address address = obj->address();
109 Page* page = Page::FromAddress(address);
111 for (Page* p : *read_only_space) {
112 if (p == page)
break;
116 SerializerReference back_reference =
117 SerializerReference::BackReference(RO_SPACE, chunk_index, chunk_offset);
118 reference_map()->Add(obj, back_reference);
119 CHECK(SerializeBackReference(obj, how_to_code, where_to_point, skip));
123 void CodeSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code,
124 WhereToPoint where_to_point,
int skip) {
125 if (SerializeHotObject(obj, how_to_code, where_to_point, skip))
return;
127 if (SerializeRoot(obj, how_to_code, where_to_point, skip))
return;
129 if (SerializeBackReference(obj, how_to_code, where_to_point, skip))
return;
131 if (SerializeReadOnlyObject(obj, how_to_code, where_to_point, skip))
return;
136 Code code_object = Code::cast(obj);
137 switch (code_object->kind()) {
138 case Code::OPTIMIZED_FUNCTION:
140 case Code::NUMBER_OF_KINDS:
141 case Code::BYTECODE_HANDLER:
144 if (code_object->builtin_index() == -1) {
145 return SerializeCodeStub(code_object, how_to_code, where_to_point);
147 return SerializeCodeObject(code_object, how_to_code, where_to_point);
151 return SerializeCodeObject(code_object, how_to_code, where_to_point);
156 ReadOnlyRoots roots(isolate());
157 if (ElideObject(obj)) {
158 return SerializeObject(roots.undefined_value(), how_to_code, where_to_point,
162 if (obj->IsScript()) {
163 Script* script_obj = Script::cast(obj);
164 DCHECK_NE(script_obj->compilation_type(), Script::COMPILATION_TYPE_EVAL);
168 Object* context_data = script_obj->context_data();
169 if (context_data != roots.undefined_value() &&
170 context_data != roots.uninitialized_symbol()) {
171 script_obj->set_context_data(roots.undefined_value());
175 FixedArray host_options = script_obj->host_defined_options();
176 script_obj->set_host_defined_options(roots.empty_fixed_array());
177 SerializeGeneric(obj, how_to_code, where_to_point);
178 script_obj->set_host_defined_options(host_options);
179 script_obj->set_context_data(context_data);
183 if (obj->IsSharedFunctionInfo()) {
184 SharedFunctionInfo* sfi = SharedFunctionInfo::cast(obj);
187 DCHECK(!sfi->IsApiFunction() && !sfi->HasAsmWasmData());
189 DebugInfo* debug_info =
nullptr;
190 BytecodeArray debug_bytecode_array;
191 if (sfi->HasDebugInfo()) {
193 debug_info = sfi->GetDebugInfo();
194 if (debug_info->HasInstrumentedBytecodeArray()) {
195 debug_bytecode_array = debug_info->DebugBytecodeArray();
196 sfi->SetDebugBytecodeArray(debug_info->OriginalBytecodeArray());
198 sfi->set_script_or_debug_info(debug_info->script());
200 DCHECK(!sfi->HasDebugInfo());
203 bool was_deserialized = sfi->deserialized();
204 sfi->set_deserialized(sfi->is_compiled());
205 SerializeGeneric(obj, how_to_code, where_to_point);
206 sfi->set_deserialized(was_deserialized);
209 if (debug_info !=
nullptr) {
210 sfi->set_script_or_debug_info(debug_info);
211 if (!debug_bytecode_array.is_null()) {
212 sfi->SetDebugBytecodeArray(debug_bytecode_array);
218 if (obj->IsBytecodeArray()) {
220 BytecodeArray::cast(obj)->ClearFrameCacheFromSourcePositionTable();
224 CHECK(!obj->IsMap());
226 CHECK(!obj->IsJSGlobalProxy() && !obj->IsJSGlobalObject());
228 CHECK_IMPLIES(obj->NeedsRehashing(), obj->CanBeRehashed());
230 CHECK(!obj->IsJSFunction() && !obj->IsContext());
232 SerializeGeneric(obj, how_to_code, where_to_point);
235 void CodeSerializer::SerializeGeneric(HeapObject* heap_object,
236 HowToCode how_to_code,
237 WhereToPoint where_to_point) {
239 ObjectSerializer serializer(
this, heap_object, &sink_, how_to_code,
241 serializer.Serialize();
244 void CodeSerializer::SerializeCodeStub(Code code_stub, HowToCode how_to_code,
245 WhereToPoint where_to_point) {
247 DCHECK(!reference_map()->LookupReference(code_stub).is_valid());
248 uint32_t stub_key = code_stub->stub_key();
249 DCHECK(CodeStub::MajorKeyFromKey(stub_key) != CodeStub::NoCache);
250 DCHECK(!CodeStub::GetCode(isolate(), stub_key).is_null());
251 stub_keys_.push_back(stub_key);
253 SerializerReference reference =
254 reference_map()->AddAttachedReference(code_stub);
255 if (FLAG_trace_serializer) {
256 PrintF(
" Encoding code stub %s as attached reference %d\n",
257 CodeStub::MajorName(CodeStub::MajorKeyFromKey(stub_key)),
258 reference.attached_reference_index());
260 PutAttachedReference(reference, how_to_code, where_to_point);
263 MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
264 Isolate* isolate, ScriptData* cached_data, Handle<String> source,
265 ScriptOriginOptions origin_options) {
266 base::ElapsedTimer timer;
267 if (FLAG_profile_deserialization || FLAG_log_function_events) timer.Start();
269 HandleScope scope(isolate);
271 SerializedCodeData::SanityCheckResult sanity_check_result =
272 SerializedCodeData::CHECK_SUCCESS;
273 const SerializedCodeData scd = SerializedCodeData::FromCachedData(
274 isolate, cached_data,
275 SerializedCodeData::SourceHash(source, origin_options),
276 &sanity_check_result);
277 if (sanity_check_result != SerializedCodeData::CHECK_SUCCESS) {
278 if (FLAG_profile_deserialization) PrintF(
"[Cached code failed check]\n");
279 DCHECK(cached_data->rejected());
280 isolate->counters()->code_cache_reject_reason()->AddSample(
281 sanity_check_result);
282 return MaybeHandle<SharedFunctionInfo>();
286 MaybeHandle<SharedFunctionInfo> maybe_result =
287 ObjectDeserializer::DeserializeSharedFunctionInfo(isolate, &scd, source);
289 Handle<SharedFunctionInfo> result;
290 if (!maybe_result.ToHandle(&result)) {
292 if (FLAG_profile_deserialization) PrintF(
"[Deserializing failed]\n");
293 return MaybeHandle<SharedFunctionInfo>();
296 if (FLAG_profile_deserialization) {
297 double ms = timer.Elapsed().InMillisecondsF();
298 int length = cached_data->length();
299 PrintF(
"[Deserializing from %d bytes took %0.3f ms]\n", length, ms);
302 bool log_code_creation = isolate->logger()->is_listening_to_code_events() ||
303 isolate->is_profiling();
304 if (log_code_creation || FLAG_log_function_events) {
305 String name = ReadOnlyRoots(isolate).empty_string();
306 if (result->script()->IsScript()) {
307 Script* script = Script::cast(result->script());
308 if (script->name()->IsString()) name = String::cast(script->name());
309 if (FLAG_log_function_events) {
310 LOG(isolate, FunctionEvent(
"deserialize", script->id(),
311 timer.Elapsed().InMillisecondsF(),
312 result->StartPosition(),
313 result->EndPosition(), name));
316 if (log_code_creation) {
317 PROFILE(isolate, CodeCreateEvent(CodeEventListener::SCRIPT_TAG,
318 result->abstract_code(), *result, name));
322 if (isolate->NeedsSourcePositionsForProfiling()) {
323 Handle<Script> script(Script::cast(result->script()), isolate);
324 Script::InitLineEnds(script);
326 return scope.CloseAndEscape(result);
330 SerializedCodeData::SerializedCodeData(
const std::vector<byte>* payload,
331 const CodeSerializer* cs) {
332 DisallowHeapAllocation no_gc;
333 const std::vector<uint32_t>* stub_keys = cs->stub_keys();
334 std::vector<Reservation> reservations = cs->EncodeReservations();
338 static_cast<uint32_t>(reservations.size()) * kUInt32Size;
340 uint32_t stub_keys_size = num_stub_keys * kUInt32Size;
341 uint32_t payload_offset = kHeaderSize + reservation_size + stub_keys_size;
342 uint32_t padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
344 padded_payload_offset +
static_cast<uint32_t>(payload->size());
345 DCHECK(IsAligned(size, kPointerAlignment));
351 memset(data_, 0, padded_payload_offset);
354 SetMagicNumber(cs->isolate());
355 SetHeaderValue(kVersionHashOffset, Version::Hash());
356 SetHeaderValue(kSourceHashOffset, cs->source_hash());
357 SetHeaderValue(kCpuFeaturesOffset,
358 static_cast<uint32_t>(CpuFeatures::SupportedFeatures()));
359 SetHeaderValue(kFlagHashOffset, FlagList::Hash());
360 SetHeaderValue(kNumReservationsOffset,
361 static_cast<uint32_t>(reservations.size()));
362 SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys);
363 SetHeaderValue(kPayloadLengthOffset, static_cast<uint32_t>(payload->size()));
366 memset(data_ + kUnalignedHeaderSize, 0, kHeaderSize - kUnalignedHeaderSize);
369 CopyBytes(data_ + kHeaderSize,
370 reinterpret_cast<const byte*>(reservations.data()),
374 CopyBytes(data_ + kHeaderSize + reservation_size,
375 reinterpret_cast<const byte*>(stub_keys->data()), stub_keys_size);
378 CopyBytes(data_ + padded_payload_offset, payload->data(),
379 static_cast<size_t>(payload->size()));
381 Checksum checksum(ChecksummedContent());
382 SetHeaderValue(kChecksumPartAOffset, checksum.a());
383 SetHeaderValue(kChecksumPartBOffset, checksum.b());
386 SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck(
387 Isolate* isolate,
uint32_t expected_source_hash)
const {
388 if (this->size_ < kHeaderSize)
return INVALID_HEADER;
389 uint32_t magic_number = GetMagicNumber();
390 if (magic_number != ComputeMagicNumber(isolate))
return MAGIC_NUMBER_MISMATCH;
391 uint32_t version_hash = GetHeaderValue(kVersionHashOffset);
392 uint32_t source_hash = GetHeaderValue(kSourceHashOffset);
393 uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset);
394 uint32_t flags_hash = GetHeaderValue(kFlagHashOffset);
395 uint32_t payload_length = GetHeaderValue(kPayloadLengthOffset);
396 uint32_t c1 = GetHeaderValue(kChecksumPartAOffset);
397 uint32_t c2 = GetHeaderValue(kChecksumPartBOffset);
398 if (version_hash != Version::Hash())
return VERSION_MISMATCH;
399 if (source_hash != expected_source_hash)
return SOURCE_MISMATCH;
400 if (cpu_features != static_cast<uint32_t>(CpuFeatures::SupportedFeatures())) {
401 return CPU_FEATURES_MISMATCH;
403 if (flags_hash != FlagList::Hash())
return FLAGS_MISMATCH;
406 POINTER_SIZE_ALIGN(kHeaderSize +
407 GetHeaderValue(kNumReservationsOffset) * kInt32Size +
408 GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size);
409 if (payload_length > max_payload_length)
return LENGTH_MISMATCH;
410 if (!Checksum(ChecksummedContent()).Check(c1, c2))
return CHECKSUM_MISMATCH;
411 return CHECK_SUCCESS;
414 uint32_t SerializedCodeData::SourceHash(Handle<String> source,
415 ScriptOriginOptions origin_options) {
416 const uint32_t source_length = source->length();
418 static constexpr
uint32_t kModuleFlagMask = (1 << 31);
419 const uint32_t is_module = origin_options.IsModule() ? kModuleFlagMask : 0;
420 DCHECK_EQ(0, source_length & kModuleFlagMask);
422 return source_length | is_module;
426 ScriptData* SerializedCodeData::GetScriptData() {
428 ScriptData* result =
new ScriptData(data_, size_);
429 result->AcquireDataOwnership();
435 std::vector<SerializedData::Reservation> SerializedCodeData::Reservations()
437 uint32_t size = GetHeaderValue(kNumReservationsOffset);
438 std::vector<Reservation> reservations(size);
439 memcpy(reservations.data(), data_ + kHeaderSize,
440 size *
sizeof(SerializedData::Reservation));
444 Vector<const byte> SerializedCodeData::Payload()
const {
445 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
446 int code_stubs_size = GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size;
447 int payload_offset = kHeaderSize + reservations_size + code_stubs_size;
448 int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
449 const byte* payload = data_ + padded_payload_offset;
450 DCHECK(IsAligned(reinterpret_cast<intptr_t>(payload), kPointerAlignment));
451 int length = GetHeaderValue(kPayloadLengthOffset);
452 DCHECK_EQ(data_ + size_, payload + length);
453 return Vector<const byte>(payload, length);
456 Vector<const uint32_t> SerializedCodeData::CodeStubKeys()
const {
457 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
458 const byte* start = data_ + kHeaderSize + reservations_size;
459 return Vector<const uint32_t>(
reinterpret_cast<const uint32_t*
>(start),
460 GetHeaderValue(kNumCodeStubKeysOffset));
463 SerializedCodeData::SerializedCodeData(ScriptData* data)
464 : SerializedData(const_cast<byte*>(data->data()), data->length()) {}
466 SerializedCodeData SerializedCodeData::FromCachedData(
467 Isolate* isolate, ScriptData* cached_data,
uint32_t expected_source_hash,
468 SanityCheckResult* rejection_result) {
469 DisallowHeapAllocation no_gc;
470 SerializedCodeData scd(cached_data);
471 *rejection_result = scd.SanityCheck(isolate, expected_source_hash);
472 if (*rejection_result != CHECK_SUCCESS) {
473 cached_data->Reject();
474 return SerializedCodeData(
nullptr, 0);