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