Line data Source code
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 : #include "src/snapshot/code-serializer.h"
6 :
7 : #include "src/counters.h"
8 : #include "src/debug/debug.h"
9 : #include "src/log.h"
10 : #include "src/macro-assembler.h"
11 : #include "src/objects-inl.h"
12 : #include "src/objects/slots.h"
13 : #include "src/snapshot/object-deserializer.h"
14 : #include "src/snapshot/snapshot.h"
15 : #include "src/version.h"
16 : #include "src/visitors.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 :
21 633 : ScriptData::ScriptData(const byte* data, int length)
22 633 : : owns_data_(false), rejected_(false), data_(data), length_(length) {
23 1266 : if (!IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment)) {
24 0 : byte* copy = NewArray<byte>(length);
25 : DCHECK(IsAligned(reinterpret_cast<intptr_t>(copy), kPointerAlignment));
26 0 : CopyBytes(copy, data, length);
27 0 : data_ = copy;
28 : AcquireDataOwnership();
29 : }
30 633 : }
31 :
32 379 : CodeSerializer::CodeSerializer(Isolate* isolate, uint32_t source_hash)
33 379 : : Serializer(isolate), source_hash_(source_hash) {
34 379 : allocator()->UseCustomChunkSize(FLAG_serialization_chunk_size);
35 379 : }
36 :
37 : // static
38 388 : ScriptCompiler::CachedData* CodeSerializer::Serialize(
39 : Handle<SharedFunctionInfo> info) {
40 : Isolate* isolate = info->GetIsolate();
41 776 : TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", "V8.Execute");
42 388 : HistogramTimerScope histogram_timer(isolate->counters()->compile_serialize());
43 : RuntimeCallTimerScope runtimeTimer(isolate,
44 388 : RuntimeCallCounterId::kCompileSerialize);
45 1164 : TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileSerialize");
46 :
47 : base::ElapsedTimer timer;
48 388 : if (FLAG_profile_deserialization) timer.Start();
49 776 : Handle<Script> script(Script::cast(info->script()), isolate);
50 388 : if (FLAG_trace_serializer) {
51 0 : PrintF("[Serializing from");
52 0 : script->name()->ShortPrint();
53 0 : PrintF("]\n");
54 : }
55 : // TODO(7110): Enable serialization of Asm modules once the AsmWasmData is
56 : // context independent.
57 388 : if (script->ContainsAsmModule()) return nullptr;
58 :
59 379 : isolate->heap()->read_only_space()->ClearStringPaddingIfNeeded();
60 :
61 : // Serialize code object.
62 758 : Handle<String> source(String::cast(script->source()), isolate);
63 : CodeSerializer cs(isolate, SerializedCodeData::SourceHash(
64 758 : source, script->origin_options()));
65 : DisallowHeapAllocation no_gc;
66 : cs.reference_map()->AddAttachedReference(
67 379 : reinterpret_cast<void*>(source->ptr()));
68 758 : ScriptData* script_data = cs.SerializeSharedFunctionInfo(info);
69 :
70 379 : if (FLAG_profile_deserialization) {
71 0 : double ms = timer.Elapsed().InMillisecondsF();
72 : int length = script_data->length();
73 0 : PrintF("[Serializing to %d bytes took %0.3f ms]\n", length, ms);
74 : }
75 :
76 : ScriptCompiler::CachedData* result =
77 : new ScriptCompiler::CachedData(script_data->data(), script_data->length(),
78 379 : ScriptCompiler::CachedData::BufferOwned);
79 : script_data->ReleaseDataOwnership();
80 379 : delete script_data;
81 :
82 388 : return result;
83 : }
84 :
85 379 : ScriptData* CodeSerializer::SerializeSharedFunctionInfo(
86 : Handle<SharedFunctionInfo> info) {
87 : DisallowHeapAllocation no_gc;
88 :
89 : VisitRootPointer(Root::kHandleScope, nullptr,
90 758 : FullObjectSlot(info.location()));
91 379 : SerializeDeferredObjects();
92 379 : Pad();
93 :
94 379 : SerializedCodeData data(sink_.data(), this);
95 :
96 758 : return data.GetScriptData();
97 : }
98 :
99 36769 : bool CodeSerializer::SerializeReadOnlyObject(HeapObject obj,
100 : HowToCode how_to_code,
101 : WhereToPoint where_to_point,
102 : int skip) {
103 36769 : PagedSpace* read_only_space = isolate()->heap()->read_only_space();
104 36769 : if (!read_only_space->Contains(obj)) return false;
105 :
106 : // For objects in RO_SPACE, never serialize the object, but instead create a
107 : // back reference that encodes the page number as the chunk_index and the
108 : // offset within the page as the chunk_offset.
109 : Address address = obj->address();
110 : Page* page = Page::FromAddress(address);
111 : uint32_t chunk_index = 0;
112 186 : for (Page* p : *read_only_space) {
113 186 : if (p == page) break;
114 0 : ++chunk_index;
115 : }
116 186 : uint32_t chunk_offset = static_cast<uint32_t>(page->Offset(address));
117 : SerializerReference back_reference =
118 : SerializerReference::BackReference(RO_SPACE, chunk_index, chunk_offset);
119 : reference_map()->Add(reinterpret_cast<void*>(obj->ptr()), back_reference);
120 186 : CHECK(SerializeBackReference(obj, how_to_code, where_to_point, skip));
121 : return true;
122 : }
123 :
124 325603 : void CodeSerializer::SerializeObject(HeapObject obj, HowToCode how_to_code,
125 : WhereToPoint where_to_point, int skip) {
126 362186 : if (SerializeHotObject(obj, how_to_code, where_to_point, skip)) return;
127 :
128 207497 : if (SerializeRoot(obj, how_to_code, where_to_point, skip)) return;
129 :
130 58676 : if (SerializeBackReference(obj, how_to_code, where_to_point, skip)) return;
131 :
132 36769 : if (SerializeReadOnlyObject(obj, how_to_code, where_to_point, skip)) return;
133 :
134 : FlushSkip(skip);
135 :
136 36583 : if (obj->IsCode()) {
137 : Code code_object = Code::cast(obj);
138 0 : switch (code_object->kind()) {
139 : case Code::OPTIMIZED_FUNCTION: // No optimized code compiled yet.
140 : case Code::REGEXP: // No regexp literals initialized yet.
141 : case Code::NUMBER_OF_KINDS: // Pseudo enum value.
142 : case Code::BYTECODE_HANDLER: // No direct references to handlers.
143 : break; // hit UNREACHABLE below.
144 : case Code::STUB:
145 : case Code::BUILTIN:
146 : default:
147 0 : return SerializeCodeObject(code_object, how_to_code, where_to_point);
148 : }
149 0 : UNREACHABLE();
150 : }
151 :
152 : ReadOnlyRoots roots(isolate());
153 36583 : if (ElideObject(obj)) {
154 : return SerializeObject(roots.undefined_value(), how_to_code, where_to_point,
155 0 : skip);
156 : }
157 :
158 36583 : if (obj->IsScript()) {
159 379 : Script script_obj = Script::cast(obj);
160 : DCHECK_NE(script_obj->compilation_type(), Script::COMPILATION_TYPE_EVAL);
161 : // We want to differentiate between undefined and uninitialized_symbol for
162 : // context_data for now. It is hack to allow debugging for scripts that are
163 : // included as a part of custom snapshot. (see debug::Script::IsEmbedded())
164 379 : Object context_data = script_obj->context_data();
165 460 : if (context_data != roots.undefined_value() &&
166 : context_data != roots.uninitialized_symbol()) {
167 81 : script_obj->set_context_data(roots.undefined_value());
168 : }
169 : // We don't want to serialize host options to avoid serializing unnecessary
170 : // object graph.
171 379 : FixedArray host_options = script_obj->host_defined_options();
172 379 : script_obj->set_host_defined_options(roots.empty_fixed_array());
173 : SerializeGeneric(obj, how_to_code, where_to_point);
174 379 : script_obj->set_host_defined_options(host_options);
175 379 : script_obj->set_context_data(context_data);
176 : return;
177 : }
178 :
179 36204 : if (obj->IsSharedFunctionInfo()) {
180 7052 : SharedFunctionInfo sfi = SharedFunctionInfo::cast(obj);
181 : // TODO(7110): Enable serializing of Asm modules once the AsmWasmData
182 : // is context independent.
183 : DCHECK(!sfi->IsApiFunction() && !sfi->HasAsmWasmData());
184 :
185 7052 : DebugInfo debug_info;
186 : BytecodeArray debug_bytecode_array;
187 7052 : if (sfi->HasDebugInfo()) {
188 : // Clear debug info.
189 0 : debug_info = sfi->GetDebugInfo();
190 0 : if (debug_info->HasInstrumentedBytecodeArray()) {
191 0 : debug_bytecode_array = debug_info->DebugBytecodeArray();
192 0 : sfi->SetDebugBytecodeArray(debug_info->OriginalBytecodeArray());
193 : }
194 0 : sfi->set_script_or_debug_info(debug_info->script());
195 : }
196 : DCHECK(!sfi->HasDebugInfo());
197 :
198 : SerializeGeneric(obj, how_to_code, where_to_point);
199 :
200 : // Restore debug info
201 7052 : if (!debug_info.is_null()) {
202 0 : sfi->set_script_or_debug_info(debug_info);
203 0 : if (!debug_bytecode_array.is_null()) {
204 0 : sfi->SetDebugBytecodeArray(debug_bytecode_array);
205 : }
206 : }
207 : return;
208 : }
209 :
210 29152 : if (obj->IsBytecodeArray()) {
211 : // Clear the stack frame cache if present
212 1485 : BytecodeArray::cast(obj)->ClearFrameCacheFromSourcePositionTable();
213 : }
214 :
215 : // Past this point we should not see any (context-specific) maps anymore.
216 29152 : CHECK(!obj->IsMap());
217 : // There should be no references to the global object embedded.
218 58304 : CHECK(!obj->IsJSGlobalProxy() && !obj->IsJSGlobalObject());
219 : // Embedded FixedArrays that need rehashing must support rehashing.
220 29152 : CHECK_IMPLIES(obj->NeedsRehashing(), obj->CanBeRehashed());
221 : // We expect no instantiated function objects or contexts.
222 58304 : CHECK(!obj->IsJSFunction() && !obj->IsContext());
223 :
224 : SerializeGeneric(obj, how_to_code, where_to_point);
225 : }
226 :
227 0 : void CodeSerializer::SerializeGeneric(HeapObject heap_object,
228 : HowToCode how_to_code,
229 : WhereToPoint where_to_point) {
230 : // Object has not yet been serialized. Serialize it here.
231 : ObjectSerializer serializer(this, heap_object, &sink_, how_to_code,
232 36583 : where_to_point);
233 36583 : serializer.Serialize();
234 0 : }
235 :
236 244 : MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize(
237 418 : Isolate* isolate, ScriptData* cached_data, Handle<String> source,
238 : ScriptOriginOptions origin_options) {
239 : base::ElapsedTimer timer;
240 244 : if (FLAG_profile_deserialization || FLAG_log_function_events) timer.Start();
241 :
242 : HandleScope scope(isolate);
243 :
244 : SerializedCodeData::SanityCheckResult sanity_check_result =
245 244 : SerializedCodeData::CHECK_SUCCESS;
246 : const SerializedCodeData scd = SerializedCodeData::FromCachedData(
247 : isolate, cached_data,
248 : SerializedCodeData::SourceHash(source, origin_options),
249 244 : &sanity_check_result);
250 244 : if (sanity_check_result != SerializedCodeData::CHECK_SUCCESS) {
251 30 : if (FLAG_profile_deserialization) PrintF("[Cached code failed check]\n");
252 : DCHECK(cached_data->rejected());
253 : isolate->counters()->code_cache_reject_reason()->AddSample(
254 60 : sanity_check_result);
255 30 : return MaybeHandle<SharedFunctionInfo>();
256 : }
257 :
258 : // Deserialize.
259 : MaybeHandle<SharedFunctionInfo> maybe_result =
260 214 : ObjectDeserializer::DeserializeSharedFunctionInfo(isolate, &scd, source);
261 :
262 : Handle<SharedFunctionInfo> result;
263 214 : if (!maybe_result.ToHandle(&result)) {
264 : // Deserializing may fail if the reservations cannot be fulfilled.
265 0 : if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n");
266 0 : return MaybeHandle<SharedFunctionInfo>();
267 : }
268 :
269 214 : if (FLAG_profile_deserialization) {
270 0 : double ms = timer.Elapsed().InMillisecondsF();
271 : int length = cached_data->length();
272 0 : PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms);
273 : }
274 :
275 : bool log_code_creation =
276 418 : isolate->logger()->is_listening_to_code_events() ||
277 418 : isolate->is_profiling() ||
278 : isolate->code_event_dispatcher()->IsListeningToCodeEvents();
279 214 : if (log_code_creation || FLAG_log_function_events) {
280 15 : String name = ReadOnlyRoots(isolate).empty_string();
281 30 : Script script = Script::cast(result->script());
282 : Handle<Script> script_handle(script, isolate);
283 45 : if (script->name()->IsString()) name = String::cast(script->name());
284 15 : if (FLAG_log_function_events) {
285 0 : LOG(isolate,
286 : FunctionEvent("deserialize", script->id(),
287 : timer.Elapsed().InMillisecondsF(),
288 : result->StartPosition(), result->EndPosition(), name));
289 : }
290 15 : if (log_code_creation) {
291 15 : Script::InitLineEnds(Handle<Script>(script, isolate));
292 : DisallowHeapAllocation no_gc;
293 15 : SharedFunctionInfo::ScriptIterator iter(isolate, script);
294 110 : for (i::SharedFunctionInfo info = iter.Next(); !info.is_null();
295 : info = iter.Next()) {
296 40 : if (info->is_compiled()) {
297 35 : int line_num = script->GetLineNumber(info->StartPosition()) + 1;
298 35 : int column_num = script->GetColumnNumber(info->StartPosition()) + 1;
299 70 : PROFILE(isolate, CodeCreateEvent(CodeEventListener::SCRIPT_TAG,
300 : info->abstract_code(), info, name,
301 : line_num, column_num));
302 : }
303 : }
304 : }
305 : }
306 :
307 214 : if (isolate->NeedsSourcePositionsForProfiling()) {
308 10 : Handle<Script> script(Script::cast(result->script()), isolate);
309 5 : Script::InitLineEnds(script);
310 : }
311 214 : return scope.CloseAndEscape(result);
312 : }
313 :
314 :
315 1516 : SerializedCodeData::SerializedCodeData(const std::vector<byte>* payload,
316 3411 : const CodeSerializer* cs) {
317 : DisallowHeapAllocation no_gc;
318 : std::vector<Reservation> reservations = cs->EncodeReservations();
319 :
320 : // Calculate sizes.
321 : uint32_t reservation_size =
322 758 : static_cast<uint32_t>(reservations.size()) * kUInt32Size;
323 : uint32_t num_stub_keys = 0; // TODO(jgruber): Remove.
324 : uint32_t stub_keys_size = num_stub_keys * kUInt32Size;
325 : uint32_t payload_offset = kHeaderSize + reservation_size + stub_keys_size;
326 379 : uint32_t padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
327 : uint32_t size =
328 379 : padded_payload_offset + static_cast<uint32_t>(payload->size());
329 : DCHECK(IsAligned(size, kPointerAlignment));
330 :
331 : // Allocate backing store and create result data.
332 379 : AllocateData(size);
333 :
334 : // Zero out pre-payload data. Part of that is only used for padding.
335 379 : memset(data_, 0, padded_payload_offset);
336 :
337 : // Set header values.
338 : SetMagicNumber();
339 379 : SetHeaderValue(kVersionHashOffset, Version::Hash());
340 : SetHeaderValue(kSourceHashOffset, cs->source_hash());
341 : SetHeaderValue(kCpuFeaturesOffset,
342 : static_cast<uint32_t>(CpuFeatures::SupportedFeatures()));
343 379 : SetHeaderValue(kFlagHashOffset, FlagList::Hash());
344 : SetHeaderValue(kNumReservationsOffset,
345 758 : static_cast<uint32_t>(reservations.size()));
346 379 : SetHeaderValue(kPayloadLengthOffset, static_cast<uint32_t>(payload->size()));
347 :
348 : // Zero out any padding in the header.
349 379 : memset(data_ + kUnalignedHeaderSize, 0, kHeaderSize - kUnalignedHeaderSize);
350 :
351 : // Copy reservation chunk sizes.
352 : CopyBytes(data_ + kHeaderSize,
353 : reinterpret_cast<const byte*>(reservations.data()),
354 758 : reservation_size);
355 :
356 : // Copy serialized data.
357 : CopyBytes(data_ + padded_payload_offset, payload->data(),
358 379 : static_cast<size_t>(payload->size()));
359 :
360 : Checksum checksum(ChecksummedContent());
361 : SetHeaderValue(kChecksumPartAOffset, checksum.a());
362 : SetHeaderValue(kChecksumPartBOffset, checksum.b());
363 379 : }
364 :
365 244 : SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck(
366 : Isolate* isolate, uint32_t expected_source_hash) const {
367 244 : if (this->size_ < kHeaderSize) return INVALID_HEADER;
368 219 : uint32_t magic_number = GetMagicNumber();
369 234 : if (magic_number != kMagicNumber) return MAGIC_NUMBER_MISMATCH;
370 : uint32_t version_hash = GetHeaderValue(kVersionHashOffset);
371 : uint32_t source_hash = GetHeaderValue(kSourceHashOffset);
372 : uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset);
373 : uint32_t flags_hash = GetHeaderValue(kFlagHashOffset);
374 : uint32_t payload_length = GetHeaderValue(kPayloadLengthOffset);
375 : uint32_t c1 = GetHeaderValue(kChecksumPartAOffset);
376 : uint32_t c2 = GetHeaderValue(kChecksumPartBOffset);
377 234 : if (version_hash != Version::Hash()) return VERSION_MISMATCH;
378 234 : if (source_hash != expected_source_hash) return SOURCE_MISMATCH;
379 224 : if (cpu_features != static_cast<uint32_t>(CpuFeatures::SupportedFeatures())) {
380 : return CPU_FEATURES_MISMATCH;
381 : }
382 224 : if (flags_hash != FlagList::Hash()) return FLAGS_MISMATCH;
383 : uint32_t max_payload_length =
384 : this->size_ -
385 438 : POINTER_SIZE_ALIGN(kHeaderSize +
386 : GetHeaderValue(kNumReservationsOffset) * kInt32Size);
387 219 : if (payload_length > max_payload_length) return LENGTH_MISMATCH;
388 219 : if (!Checksum(ChecksummedContent()).Check(c1, c2)) return CHECKSUM_MISMATCH;
389 214 : return CHECK_SUCCESS;
390 : }
391 :
392 623 : uint32_t SerializedCodeData::SourceHash(Handle<String> source,
393 : ScriptOriginOptions origin_options) {
394 623 : const uint32_t source_length = source->length();
395 :
396 : static constexpr uint32_t kModuleFlagMask = (1 << 31);
397 623 : const uint32_t is_module = origin_options.IsModule() ? kModuleFlagMask : 0;
398 : DCHECK_EQ(0, source_length & kModuleFlagMask);
399 :
400 623 : return source_length | is_module;
401 : }
402 :
403 : // Return ScriptData object and relinquish ownership over it to the caller.
404 379 : ScriptData* SerializedCodeData::GetScriptData() {
405 : DCHECK(owns_data_);
406 379 : ScriptData* result = new ScriptData(data_, size_);
407 : result->AcquireDataOwnership();
408 379 : owns_data_ = false;
409 379 : data_ = nullptr;
410 379 : return result;
411 : }
412 :
413 214 : std::vector<SerializedData::Reservation> SerializedCodeData::Reservations()
414 : const {
415 214 : uint32_t size = GetHeaderValue(kNumReservationsOffset);
416 214 : std::vector<Reservation> reservations(size);
417 : memcpy(reservations.data(), data_ + kHeaderSize,
418 214 : size * sizeof(SerializedData::Reservation));
419 214 : return reservations;
420 : }
421 :
422 214 : Vector<const byte> SerializedCodeData::Payload() const {
423 428 : int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size;
424 214 : int payload_offset = kHeaderSize + reservations_size;
425 214 : int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset);
426 214 : const byte* payload = data_ + padded_payload_offset;
427 : DCHECK(IsAligned(reinterpret_cast<intptr_t>(payload), kPointerAlignment));
428 214 : int length = GetHeaderValue(kPayloadLengthOffset);
429 : DCHECK_EQ(data_ + size_, payload + length);
430 214 : return Vector<const byte>(payload, length);
431 : }
432 :
433 244 : SerializedCodeData::SerializedCodeData(ScriptData* data)
434 244 : : SerializedData(const_cast<byte*>(data->data()), data->length()) {}
435 :
436 244 : SerializedCodeData SerializedCodeData::FromCachedData(
437 : Isolate* isolate, ScriptData* cached_data, uint32_t expected_source_hash,
438 : SanityCheckResult* rejection_result) {
439 : DisallowHeapAllocation no_gc;
440 : SerializedCodeData scd(cached_data);
441 244 : *rejection_result = scd.SanityCheck(isolate, expected_source_hash);
442 244 : if (*rejection_result != CHECK_SUCCESS) {
443 : cached_data->Reject();
444 : return SerializedCodeData(nullptr, 0);
445 : }
446 : return scd;
447 : }
448 :
449 : } // namespace internal
450 183867 : } // namespace v8
|