OLD | NEW |
| (Empty) |
1 // Copyright 2012 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/serialize.h" | |
6 | |
7 #include "src/accessors.h" | |
8 #include "src/api.h" | |
9 #include "src/base/platform/platform.h" | |
10 #include "src/bootstrapper.h" | |
11 #include "src/code-stubs.h" | |
12 #include "src/deoptimizer.h" | |
13 #include "src/execution.h" | |
14 #include "src/global-handles.h" | |
15 #include "src/ic/ic.h" | |
16 #include "src/ic/stub-cache.h" | |
17 #include "src/objects.h" | |
18 #include "src/parsing/parser.h" | |
19 #include "src/profiler/cpu-profiler.h" | |
20 #include "src/runtime/runtime.h" | |
21 #include "src/snapshot/natives.h" | |
22 #include "src/snapshot/snapshot.h" | |
23 #include "src/snapshot/snapshot-source-sink.h" | |
24 #include "src/v8.h" | |
25 #include "src/v8threads.h" | |
26 #include "src/version.h" | |
27 | |
28 namespace v8 { | |
29 namespace internal { | |
30 | |
31 | |
32 // ----------------------------------------------------------------------------- | |
33 // Coding of external references. | |
34 | |
35 | |
36 ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) { | |
37 ExternalReferenceTable* external_reference_table = | |
38 isolate->external_reference_table(); | |
39 if (external_reference_table == NULL) { | |
40 external_reference_table = new ExternalReferenceTable(isolate); | |
41 isolate->set_external_reference_table(external_reference_table); | |
42 } | |
43 return external_reference_table; | |
44 } | |
45 | |
46 | |
47 ExternalReferenceTable::ExternalReferenceTable(Isolate* isolate) { | |
48 // Miscellaneous | |
49 Add(ExternalReference::roots_array_start(isolate).address(), | |
50 "Heap::roots_array_start()"); | |
51 Add(ExternalReference::address_of_stack_limit(isolate).address(), | |
52 "StackGuard::address_of_jslimit()"); | |
53 Add(ExternalReference::address_of_real_stack_limit(isolate).address(), | |
54 "StackGuard::address_of_real_jslimit()"); | |
55 Add(ExternalReference::new_space_start(isolate).address(), | |
56 "Heap::NewSpaceStart()"); | |
57 Add(ExternalReference::new_space_allocation_limit_address(isolate).address(), | |
58 "Heap::NewSpaceAllocationLimitAddress()"); | |
59 Add(ExternalReference::new_space_allocation_top_address(isolate).address(), | |
60 "Heap::NewSpaceAllocationTopAddress()"); | |
61 Add(ExternalReference::mod_two_doubles_operation(isolate).address(), | |
62 "mod_two_doubles"); | |
63 // Keyed lookup cache. | |
64 Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(), | |
65 "KeyedLookupCache::keys()"); | |
66 Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(), | |
67 "KeyedLookupCache::field_offsets()"); | |
68 Add(ExternalReference::handle_scope_next_address(isolate).address(), | |
69 "HandleScope::next"); | |
70 Add(ExternalReference::handle_scope_limit_address(isolate).address(), | |
71 "HandleScope::limit"); | |
72 Add(ExternalReference::handle_scope_level_address(isolate).address(), | |
73 "HandleScope::level"); | |
74 Add(ExternalReference::new_deoptimizer_function(isolate).address(), | |
75 "Deoptimizer::New()"); | |
76 Add(ExternalReference::compute_output_frames_function(isolate).address(), | |
77 "Deoptimizer::ComputeOutputFrames()"); | |
78 Add(ExternalReference::address_of_min_int().address(), | |
79 "LDoubleConstant::min_int"); | |
80 Add(ExternalReference::address_of_one_half().address(), | |
81 "LDoubleConstant::one_half"); | |
82 Add(ExternalReference::isolate_address(isolate).address(), "isolate"); | |
83 Add(ExternalReference::interpreter_dispatch_table_address(isolate).address(), | |
84 "Interpreter::dispatch_table_address"); | |
85 Add(ExternalReference::address_of_negative_infinity().address(), | |
86 "LDoubleConstant::negative_infinity"); | |
87 Add(ExternalReference::power_double_double_function(isolate).address(), | |
88 "power_double_double_function"); | |
89 Add(ExternalReference::power_double_int_function(isolate).address(), | |
90 "power_double_int_function"); | |
91 Add(ExternalReference::math_log_double_function(isolate).address(), | |
92 "std::log"); | |
93 Add(ExternalReference::store_buffer_top(isolate).address(), | |
94 "store_buffer_top"); | |
95 Add(ExternalReference::address_of_the_hole_nan().address(), "the_hole_nan"); | |
96 Add(ExternalReference::get_date_field_function(isolate).address(), | |
97 "JSDate::GetField"); | |
98 Add(ExternalReference::date_cache_stamp(isolate).address(), | |
99 "date_cache_stamp"); | |
100 Add(ExternalReference::address_of_pending_message_obj(isolate).address(), | |
101 "address_of_pending_message_obj"); | |
102 Add(ExternalReference::get_make_code_young_function(isolate).address(), | |
103 "Code::MakeCodeYoung"); | |
104 Add(ExternalReference::cpu_features().address(), "cpu_features"); | |
105 Add(ExternalReference::old_space_allocation_top_address(isolate).address(), | |
106 "Heap::OldSpaceAllocationTopAddress"); | |
107 Add(ExternalReference::old_space_allocation_limit_address(isolate).address(), | |
108 "Heap::OldSpaceAllocationLimitAddress"); | |
109 Add(ExternalReference::allocation_sites_list_address(isolate).address(), | |
110 "Heap::allocation_sites_list_address()"); | |
111 Add(ExternalReference::address_of_uint32_bias().address(), "uint32_bias"); | |
112 Add(ExternalReference::get_mark_code_as_executed_function(isolate).address(), | |
113 "Code::MarkCodeAsExecuted"); | |
114 Add(ExternalReference::is_profiling_address(isolate).address(), | |
115 "CpuProfiler::is_profiling"); | |
116 Add(ExternalReference::scheduled_exception_address(isolate).address(), | |
117 "Isolate::scheduled_exception"); | |
118 Add(ExternalReference::invoke_function_callback(isolate).address(), | |
119 "InvokeFunctionCallback"); | |
120 Add(ExternalReference::invoke_accessor_getter_callback(isolate).address(), | |
121 "InvokeAccessorGetterCallback"); | |
122 Add(ExternalReference::f32_trunc_wrapper_function(isolate).address(), | |
123 "f32_trunc_wrapper"); | |
124 Add(ExternalReference::f32_floor_wrapper_function(isolate).address(), | |
125 "f32_floor_wrapper"); | |
126 Add(ExternalReference::f32_ceil_wrapper_function(isolate).address(), | |
127 "f32_ceil_wrapper"); | |
128 Add(ExternalReference::f32_nearest_int_wrapper_function(isolate).address(), | |
129 "f32_nearest_int_wrapper"); | |
130 Add(ExternalReference::f64_trunc_wrapper_function(isolate).address(), | |
131 "f64_trunc_wrapper"); | |
132 Add(ExternalReference::f64_floor_wrapper_function(isolate).address(), | |
133 "f64_floor_wrapper"); | |
134 Add(ExternalReference::f64_ceil_wrapper_function(isolate).address(), | |
135 "f64_ceil_wrapper"); | |
136 Add(ExternalReference::f64_nearest_int_wrapper_function(isolate).address(), | |
137 "f64_nearest_int_wrapper"); | |
138 Add(ExternalReference::log_enter_external_function(isolate).address(), | |
139 "Logger::EnterExternal"); | |
140 Add(ExternalReference::log_leave_external_function(isolate).address(), | |
141 "Logger::LeaveExternal"); | |
142 Add(ExternalReference::address_of_minus_one_half().address(), | |
143 "double_constants.minus_one_half"); | |
144 Add(ExternalReference::stress_deopt_count(isolate).address(), | |
145 "Isolate::stress_deopt_count_address()"); | |
146 Add(ExternalReference::virtual_handler_register(isolate).address(), | |
147 "Isolate::virtual_handler_register()"); | |
148 Add(ExternalReference::virtual_slot_register(isolate).address(), | |
149 "Isolate::virtual_slot_register()"); | |
150 Add(ExternalReference::runtime_function_table_address(isolate).address(), | |
151 "Runtime::runtime_function_table_address()"); | |
152 | |
153 // Debug addresses | |
154 Add(ExternalReference::debug_after_break_target_address(isolate).address(), | |
155 "Debug::after_break_target_address()"); | |
156 Add(ExternalReference::debug_is_active_address(isolate).address(), | |
157 "Debug::is_active_address()"); | |
158 Add(ExternalReference::debug_step_in_enabled_address(isolate).address(), | |
159 "Debug::step_in_enabled_address()"); | |
160 | |
161 #ifndef V8_INTERPRETED_REGEXP | |
162 Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(), | |
163 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()"); | |
164 Add(ExternalReference::re_check_stack_guard_state(isolate).address(), | |
165 "RegExpMacroAssembler*::CheckStackGuardState()"); | |
166 Add(ExternalReference::re_grow_stack(isolate).address(), | |
167 "NativeRegExpMacroAssembler::GrowStack()"); | |
168 Add(ExternalReference::re_word_character_map().address(), | |
169 "NativeRegExpMacroAssembler::word_character_map"); | |
170 Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(), | |
171 "RegExpStack::limit_address()"); | |
172 Add(ExternalReference::address_of_regexp_stack_memory_address(isolate) | |
173 .address(), | |
174 "RegExpStack::memory_address()"); | |
175 Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(), | |
176 "RegExpStack::memory_size()"); | |
177 Add(ExternalReference::address_of_static_offsets_vector(isolate).address(), | |
178 "OffsetsVector::static_offsets_vector"); | |
179 #endif // V8_INTERPRETED_REGEXP | |
180 | |
181 // The following populates all of the different type of external references | |
182 // into the ExternalReferenceTable. | |
183 // | |
184 // NOTE: This function was originally 100k of code. It has since been | |
185 // rewritten to be mostly table driven, as the callback macro style tends to | |
186 // very easily cause code bloat. Please be careful in the future when adding | |
187 // new references. | |
188 | |
189 struct RefTableEntry { | |
190 uint16_t id; | |
191 const char* name; | |
192 }; | |
193 | |
194 static const RefTableEntry c_builtins[] = { | |
195 #define DEF_ENTRY_C(name, ignored) \ | |
196 { Builtins::c_##name, "Builtins::" #name } \ | |
197 , | |
198 BUILTIN_LIST_C(DEF_ENTRY_C) | |
199 #undef DEF_ENTRY_C | |
200 }; | |
201 | |
202 for (unsigned i = 0; i < arraysize(c_builtins); ++i) { | |
203 ExternalReference ref(static_cast<Builtins::CFunctionId>(c_builtins[i].id), | |
204 isolate); | |
205 Add(ref.address(), c_builtins[i].name); | |
206 } | |
207 | |
208 static const RefTableEntry builtins[] = { | |
209 #define DEF_ENTRY_C(name, ignored) \ | |
210 { Builtins::k##name, "Builtins::" #name } \ | |
211 , | |
212 #define DEF_ENTRY_A(name, i1, i2, i3) \ | |
213 { Builtins::k##name, "Builtins::" #name } \ | |
214 , | |
215 BUILTIN_LIST_C(DEF_ENTRY_C) BUILTIN_LIST_A(DEF_ENTRY_A) | |
216 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A) | |
217 #undef DEF_ENTRY_C | |
218 #undef DEF_ENTRY_A | |
219 }; | |
220 | |
221 for (unsigned i = 0; i < arraysize(builtins); ++i) { | |
222 ExternalReference ref(static_cast<Builtins::Name>(builtins[i].id), isolate); | |
223 Add(ref.address(), builtins[i].name); | |
224 } | |
225 | |
226 static const RefTableEntry runtime_functions[] = { | |
227 #define RUNTIME_ENTRY(name, i1, i2) \ | |
228 { Runtime::k##name, "Runtime::" #name } \ | |
229 , | |
230 FOR_EACH_INTRINSIC(RUNTIME_ENTRY) | |
231 #undef RUNTIME_ENTRY | |
232 }; | |
233 | |
234 for (unsigned i = 0; i < arraysize(runtime_functions); ++i) { | |
235 ExternalReference ref( | |
236 static_cast<Runtime::FunctionId>(runtime_functions[i].id), isolate); | |
237 Add(ref.address(), runtime_functions[i].name); | |
238 } | |
239 | |
240 // Stat counters | |
241 struct StatsRefTableEntry { | |
242 StatsCounter* (Counters::*counter)(); | |
243 const char* name; | |
244 }; | |
245 | |
246 static const StatsRefTableEntry stats_ref_table[] = { | |
247 #define COUNTER_ENTRY(name, caption) \ | |
248 { &Counters::name, "Counters::" #name } \ | |
249 , | |
250 STATS_COUNTER_LIST_1(COUNTER_ENTRY) STATS_COUNTER_LIST_2(COUNTER_ENTRY) | |
251 #undef COUNTER_ENTRY | |
252 }; | |
253 | |
254 Counters* counters = isolate->counters(); | |
255 for (unsigned i = 0; i < arraysize(stats_ref_table); ++i) { | |
256 // To make sure the indices are not dependent on whether counters are | |
257 // enabled, use a dummy address as filler. | |
258 Address address = NotAvailable(); | |
259 StatsCounter* counter = (counters->*(stats_ref_table[i].counter))(); | |
260 if (counter->Enabled()) { | |
261 address = reinterpret_cast<Address>(counter->GetInternalPointer()); | |
262 } | |
263 Add(address, stats_ref_table[i].name); | |
264 } | |
265 | |
266 // Top addresses | |
267 static const char* address_names[] = { | |
268 #define BUILD_NAME_LITERAL(Name, name) "Isolate::" #name "_address", | |
269 FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL) NULL | |
270 #undef BUILD_NAME_LITERAL | |
271 }; | |
272 | |
273 for (int i = 0; i < Isolate::kIsolateAddressCount; ++i) { | |
274 Add(isolate->get_address_from_id(static_cast<Isolate::AddressId>(i)), | |
275 address_names[i]); | |
276 } | |
277 | |
278 // Accessors | |
279 struct AccessorRefTable { | |
280 Address address; | |
281 const char* name; | |
282 }; | |
283 | |
284 static const AccessorRefTable accessors[] = { | |
285 #define ACCESSOR_INFO_DECLARATION(name) \ | |
286 { FUNCTION_ADDR(&Accessors::name##Getter), "Accessors::" #name "Getter" } \ | |
287 , | |
288 ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION) | |
289 #undef ACCESSOR_INFO_DECLARATION | |
290 #define ACCESSOR_SETTER_DECLARATION(name) \ | |
291 { FUNCTION_ADDR(&Accessors::name), "Accessors::" #name } \ | |
292 , | |
293 ACCESSOR_SETTER_LIST(ACCESSOR_SETTER_DECLARATION) | |
294 #undef ACCESSOR_INFO_DECLARATION | |
295 }; | |
296 | |
297 for (unsigned i = 0; i < arraysize(accessors); ++i) { | |
298 Add(accessors[i].address, accessors[i].name); | |
299 } | |
300 | |
301 StubCache* stub_cache = isolate->stub_cache(); | |
302 | |
303 // Stub cache tables | |
304 Add(stub_cache->key_reference(StubCache::kPrimary).address(), | |
305 "StubCache::primary_->key"); | |
306 Add(stub_cache->value_reference(StubCache::kPrimary).address(), | |
307 "StubCache::primary_->value"); | |
308 Add(stub_cache->map_reference(StubCache::kPrimary).address(), | |
309 "StubCache::primary_->map"); | |
310 Add(stub_cache->key_reference(StubCache::kSecondary).address(), | |
311 "StubCache::secondary_->key"); | |
312 Add(stub_cache->value_reference(StubCache::kSecondary).address(), | |
313 "StubCache::secondary_->value"); | |
314 Add(stub_cache->map_reference(StubCache::kSecondary).address(), | |
315 "StubCache::secondary_->map"); | |
316 | |
317 // Runtime entries | |
318 Add(ExternalReference::delete_handle_scope_extensions(isolate).address(), | |
319 "HandleScope::DeleteExtensions"); | |
320 Add(ExternalReference::incremental_marking_record_write_function(isolate) | |
321 .address(), | |
322 "IncrementalMarking::RecordWrite"); | |
323 Add(ExternalReference::incremental_marking_record_write_code_entry_function( | |
324 isolate) | |
325 .address(), | |
326 "IncrementalMarking::RecordWriteOfCodeEntryFromCode"); | |
327 Add(ExternalReference::store_buffer_overflow_function(isolate).address(), | |
328 "StoreBuffer::StoreBufferOverflow"); | |
329 | |
330 // Add a small set of deopt entry addresses to encoder without generating the | |
331 // deopt table code, which isn't possible at deserialization time. | |
332 HandleScope scope(isolate); | |
333 for (int entry = 0; entry < kDeoptTableSerializeEntryCount; ++entry) { | |
334 Address address = Deoptimizer::GetDeoptimizationEntry( | |
335 isolate, | |
336 entry, | |
337 Deoptimizer::LAZY, | |
338 Deoptimizer::CALCULATE_ENTRY_ADDRESS); | |
339 Add(address, "lazy_deopt"); | |
340 } | |
341 } | |
342 | |
343 | |
344 ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate) { | |
345 map_ = isolate->external_reference_map(); | |
346 if (map_ != NULL) return; | |
347 map_ = new HashMap(HashMap::PointersMatch); | |
348 ExternalReferenceTable* table = ExternalReferenceTable::instance(isolate); | |
349 for (int i = 0; i < table->size(); ++i) { | |
350 Address addr = table->address(i); | |
351 if (addr == ExternalReferenceTable::NotAvailable()) continue; | |
352 // We expect no duplicate external references entries in the table. | |
353 DCHECK_NULL(map_->Lookup(addr, Hash(addr))); | |
354 map_->LookupOrInsert(addr, Hash(addr))->value = reinterpret_cast<void*>(i); | |
355 } | |
356 isolate->set_external_reference_map(map_); | |
357 } | |
358 | |
359 | |
360 uint32_t ExternalReferenceEncoder::Encode(Address address) const { | |
361 DCHECK_NOT_NULL(address); | |
362 HashMap::Entry* entry = | |
363 const_cast<HashMap*>(map_)->Lookup(address, Hash(address)); | |
364 DCHECK_NOT_NULL(entry); | |
365 return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value)); | |
366 } | |
367 | |
368 | |
369 const char* ExternalReferenceEncoder::NameOfAddress(Isolate* isolate, | |
370 Address address) const { | |
371 HashMap::Entry* entry = | |
372 const_cast<HashMap*>(map_)->Lookup(address, Hash(address)); | |
373 if (entry == NULL) return "<unknown>"; | |
374 uint32_t i = static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry->value)); | |
375 return ExternalReferenceTable::instance(isolate)->name(i); | |
376 } | |
377 | |
378 | |
379 class CodeAddressMap: public CodeEventLogger { | |
380 public: | |
381 explicit CodeAddressMap(Isolate* isolate) | |
382 : isolate_(isolate) { | |
383 isolate->logger()->addCodeEventListener(this); | |
384 } | |
385 | |
386 ~CodeAddressMap() override { | |
387 isolate_->logger()->removeCodeEventListener(this); | |
388 } | |
389 | |
390 void CodeMoveEvent(AbstractCode* from, Address to) override { | |
391 address_to_name_map_.Move(from->address(), to); | |
392 } | |
393 | |
394 void CodeDisableOptEvent(AbstractCode* code, | |
395 SharedFunctionInfo* shared) override {} | |
396 | |
397 const char* Lookup(Address address) { | |
398 return address_to_name_map_.Lookup(address); | |
399 } | |
400 | |
401 private: | |
402 class NameMap { | |
403 public: | |
404 NameMap() : impl_(HashMap::PointersMatch) {} | |
405 | |
406 ~NameMap() { | |
407 for (HashMap::Entry* p = impl_.Start(); p != NULL; p = impl_.Next(p)) { | |
408 DeleteArray(static_cast<const char*>(p->value)); | |
409 } | |
410 } | |
411 | |
412 void Insert(Address code_address, const char* name, int name_size) { | |
413 HashMap::Entry* entry = FindOrCreateEntry(code_address); | |
414 if (entry->value == NULL) { | |
415 entry->value = CopyName(name, name_size); | |
416 } | |
417 } | |
418 | |
419 const char* Lookup(Address code_address) { | |
420 HashMap::Entry* entry = FindEntry(code_address); | |
421 return (entry != NULL) ? static_cast<const char*>(entry->value) : NULL; | |
422 } | |
423 | |
424 void Remove(Address code_address) { | |
425 HashMap::Entry* entry = FindEntry(code_address); | |
426 if (entry != NULL) { | |
427 DeleteArray(static_cast<char*>(entry->value)); | |
428 RemoveEntry(entry); | |
429 } | |
430 } | |
431 | |
432 void Move(Address from, Address to) { | |
433 if (from == to) return; | |
434 HashMap::Entry* from_entry = FindEntry(from); | |
435 DCHECK(from_entry != NULL); | |
436 void* value = from_entry->value; | |
437 RemoveEntry(from_entry); | |
438 HashMap::Entry* to_entry = FindOrCreateEntry(to); | |
439 DCHECK(to_entry->value == NULL); | |
440 to_entry->value = value; | |
441 } | |
442 | |
443 private: | |
444 static char* CopyName(const char* name, int name_size) { | |
445 char* result = NewArray<char>(name_size + 1); | |
446 for (int i = 0; i < name_size; ++i) { | |
447 char c = name[i]; | |
448 if (c == '\0') c = ' '; | |
449 result[i] = c; | |
450 } | |
451 result[name_size] = '\0'; | |
452 return result; | |
453 } | |
454 | |
455 HashMap::Entry* FindOrCreateEntry(Address code_address) { | |
456 return impl_.LookupOrInsert(code_address, | |
457 ComputePointerHash(code_address)); | |
458 } | |
459 | |
460 HashMap::Entry* FindEntry(Address code_address) { | |
461 return impl_.Lookup(code_address, ComputePointerHash(code_address)); | |
462 } | |
463 | |
464 void RemoveEntry(HashMap::Entry* entry) { | |
465 impl_.Remove(entry->key, entry->hash); | |
466 } | |
467 | |
468 HashMap impl_; | |
469 | |
470 DISALLOW_COPY_AND_ASSIGN(NameMap); | |
471 }; | |
472 | |
473 void LogRecordedBuffer(AbstractCode* code, SharedFunctionInfo*, | |
474 const char* name, int length) override { | |
475 address_to_name_map_.Insert(code->address(), name, length); | |
476 } | |
477 | |
478 NameMap address_to_name_map_; | |
479 Isolate* isolate_; | |
480 }; | |
481 | |
482 | |
483 void Deserializer::DecodeReservation( | |
484 Vector<const SerializedData::Reservation> res) { | |
485 DCHECK_EQ(0, reservations_[NEW_SPACE].length()); | |
486 STATIC_ASSERT(NEW_SPACE == 0); | |
487 int current_space = NEW_SPACE; | |
488 for (auto& r : res) { | |
489 reservations_[current_space].Add({r.chunk_size(), NULL, NULL}); | |
490 if (r.is_last()) current_space++; | |
491 } | |
492 DCHECK_EQ(kNumberOfSpaces, current_space); | |
493 for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) current_chunk_[i] = 0; | |
494 } | |
495 | |
496 | |
497 void Deserializer::FlushICacheForNewIsolate() { | |
498 DCHECK(!deserializing_user_code_); | |
499 // The entire isolate is newly deserialized. Simply flush all code pages. | |
500 PageIterator it(isolate_->heap()->code_space()); | |
501 while (it.has_next()) { | |
502 Page* p = it.next(); | |
503 Assembler::FlushICache(isolate_, p->area_start(), | |
504 p->area_end() - p->area_start()); | |
505 } | |
506 } | |
507 | |
508 | |
509 void Deserializer::FlushICacheForNewCodeObjects() { | |
510 DCHECK(deserializing_user_code_); | |
511 for (Code* code : new_code_objects_) { | |
512 Assembler::FlushICache(isolate_, code->instruction_start(), | |
513 code->instruction_size()); | |
514 } | |
515 } | |
516 | |
517 | |
518 bool Deserializer::ReserveSpace() { | |
519 #ifdef DEBUG | |
520 for (int i = NEW_SPACE; i < kNumberOfSpaces; ++i) { | |
521 CHECK(reservations_[i].length() > 0); | |
522 } | |
523 #endif // DEBUG | |
524 if (!isolate_->heap()->ReserveSpace(reservations_)) return false; | |
525 for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) { | |
526 high_water_[i] = reservations_[i][0].start; | |
527 } | |
528 return true; | |
529 } | |
530 | |
531 | |
532 void Deserializer::Initialize(Isolate* isolate) { | |
533 DCHECK_NULL(isolate_); | |
534 DCHECK_NOT_NULL(isolate); | |
535 isolate_ = isolate; | |
536 DCHECK_NULL(external_reference_table_); | |
537 external_reference_table_ = ExternalReferenceTable::instance(isolate); | |
538 CHECK_EQ(magic_number_, | |
539 SerializedData::ComputeMagicNumber(external_reference_table_)); | |
540 } | |
541 | |
542 | |
543 void Deserializer::Deserialize(Isolate* isolate) { | |
544 Initialize(isolate); | |
545 if (!ReserveSpace()) V8::FatalProcessOutOfMemory("deserializing context"); | |
546 // No active threads. | |
547 DCHECK_NULL(isolate_->thread_manager()->FirstThreadStateInUse()); | |
548 // No active handles. | |
549 DCHECK(isolate_->handle_scope_implementer()->blocks()->is_empty()); | |
550 | |
551 { | |
552 DisallowHeapAllocation no_gc; | |
553 isolate_->heap()->IterateSmiRoots(this); | |
554 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG); | |
555 isolate_->heap()->RepairFreeListsAfterDeserialization(); | |
556 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL); | |
557 DeserializeDeferredObjects(); | |
558 FlushICacheForNewIsolate(); | |
559 } | |
560 | |
561 isolate_->heap()->set_native_contexts_list( | |
562 isolate_->heap()->undefined_value()); | |
563 // The allocation site list is build during root iteration, but if no sites | |
564 // were encountered then it needs to be initialized to undefined. | |
565 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) { | |
566 isolate_->heap()->set_allocation_sites_list( | |
567 isolate_->heap()->undefined_value()); | |
568 } | |
569 | |
570 // Update data pointers to the external strings containing natives sources. | |
571 Natives::UpdateSourceCache(isolate_->heap()); | |
572 ExtraNatives::UpdateSourceCache(isolate_->heap()); | |
573 | |
574 // Issue code events for newly deserialized code objects. | |
575 LOG_CODE_EVENT(isolate_, LogCodeObjects()); | |
576 LOG_CODE_EVENT(isolate_, LogCompiledFunctions()); | |
577 } | |
578 | |
579 | |
580 MaybeHandle<Object> Deserializer::DeserializePartial( | |
581 Isolate* isolate, Handle<JSGlobalProxy> global_proxy) { | |
582 Initialize(isolate); | |
583 if (!ReserveSpace()) { | |
584 V8::FatalProcessOutOfMemory("deserialize context"); | |
585 return MaybeHandle<Object>(); | |
586 } | |
587 | |
588 Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::New(1); | |
589 attached_objects[kGlobalProxyReference] = global_proxy; | |
590 SetAttachedObjects(attached_objects); | |
591 | |
592 DisallowHeapAllocation no_gc; | |
593 // Keep track of the code space start and end pointers in case new | |
594 // code objects were unserialized | |
595 OldSpace* code_space = isolate_->heap()->code_space(); | |
596 Address start_address = code_space->top(); | |
597 Object* root; | |
598 VisitPointer(&root); | |
599 DeserializeDeferredObjects(); | |
600 | |
601 // There's no code deserialized here. If this assert fires then that's | |
602 // changed and logging should be added to notify the profiler et al of the | |
603 // new code, which also has to be flushed from instruction cache. | |
604 CHECK_EQ(start_address, code_space->top()); | |
605 return Handle<Object>(root, isolate); | |
606 } | |
607 | |
608 | |
609 MaybeHandle<SharedFunctionInfo> Deserializer::DeserializeCode( | |
610 Isolate* isolate) { | |
611 Initialize(isolate); | |
612 if (!ReserveSpace()) { | |
613 return Handle<SharedFunctionInfo>(); | |
614 } else { | |
615 deserializing_user_code_ = true; | |
616 HandleScope scope(isolate); | |
617 Handle<SharedFunctionInfo> result; | |
618 { | |
619 DisallowHeapAllocation no_gc; | |
620 Object* root; | |
621 VisitPointer(&root); | |
622 DeserializeDeferredObjects(); | |
623 FlushICacheForNewCodeObjects(); | |
624 result = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(root)); | |
625 } | |
626 CommitPostProcessedObjects(isolate); | |
627 return scope.CloseAndEscape(result); | |
628 } | |
629 } | |
630 | |
631 | |
632 Deserializer::~Deserializer() { | |
633 // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed. | |
634 // DCHECK(source_.AtEOF()); | |
635 attached_objects_.Dispose(); | |
636 } | |
637 | |
638 | |
639 // This is called on the roots. It is the driver of the deserialization | |
640 // process. It is also called on the body of each function. | |
641 void Deserializer::VisitPointers(Object** start, Object** end) { | |
642 // The space must be new space. Any other space would cause ReadChunk to try | |
643 // to update the remembered using NULL as the address. | |
644 ReadData(start, end, NEW_SPACE, NULL); | |
645 } | |
646 | |
647 void Deserializer::Synchronize(VisitorSynchronization::SyncTag tag) { | |
648 static const byte expected = kSynchronize; | |
649 CHECK_EQ(expected, source_.Get()); | |
650 } | |
651 | |
652 void Deserializer::DeserializeDeferredObjects() { | |
653 for (int code = source_.Get(); code != kSynchronize; code = source_.Get()) { | |
654 switch (code) { | |
655 case kAlignmentPrefix: | |
656 case kAlignmentPrefix + 1: | |
657 case kAlignmentPrefix + 2: | |
658 SetAlignment(code); | |
659 break; | |
660 default: { | |
661 int space = code & kSpaceMask; | |
662 DCHECK(space <= kNumberOfSpaces); | |
663 DCHECK(code - space == kNewObject); | |
664 HeapObject* object = GetBackReferencedObject(space); | |
665 int size = source_.GetInt() << kPointerSizeLog2; | |
666 Address obj_address = object->address(); | |
667 Object** start = reinterpret_cast<Object**>(obj_address + kPointerSize); | |
668 Object** end = reinterpret_cast<Object**>(obj_address + size); | |
669 bool filled = ReadData(start, end, space, obj_address); | |
670 CHECK(filled); | |
671 DCHECK(CanBeDeferred(object)); | |
672 PostProcessNewObject(object, space); | |
673 } | |
674 } | |
675 } | |
676 } | |
677 | |
678 | |
679 // Used to insert a deserialized internalized string into the string table. | |
680 class StringTableInsertionKey : public HashTableKey { | |
681 public: | |
682 explicit StringTableInsertionKey(String* string) | |
683 : string_(string), hash_(HashForObject(string)) { | |
684 DCHECK(string->IsInternalizedString()); | |
685 } | |
686 | |
687 bool IsMatch(Object* string) override { | |
688 // We know that all entries in a hash table had their hash keys created. | |
689 // Use that knowledge to have fast failure. | |
690 if (hash_ != HashForObject(string)) return false; | |
691 // We want to compare the content of two internalized strings here. | |
692 return string_->SlowEquals(String::cast(string)); | |
693 } | |
694 | |
695 uint32_t Hash() override { return hash_; } | |
696 | |
697 uint32_t HashForObject(Object* key) override { | |
698 return String::cast(key)->Hash(); | |
699 } | |
700 | |
701 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override { | |
702 return handle(string_, isolate); | |
703 } | |
704 | |
705 private: | |
706 String* string_; | |
707 uint32_t hash_; | |
708 DisallowHeapAllocation no_gc; | |
709 }; | |
710 | |
711 | |
712 HeapObject* Deserializer::PostProcessNewObject(HeapObject* obj, int space) { | |
713 if (deserializing_user_code()) { | |
714 if (obj->IsString()) { | |
715 String* string = String::cast(obj); | |
716 // Uninitialize hash field as the hash seed may have changed. | |
717 string->set_hash_field(String::kEmptyHashField); | |
718 if (string->IsInternalizedString()) { | |
719 // Canonicalize the internalized string. If it already exists in the | |
720 // string table, set it to forward to the existing one. | |
721 StringTableInsertionKey key(string); | |
722 String* canonical = StringTable::LookupKeyIfExists(isolate_, &key); | |
723 if (canonical == NULL) { | |
724 new_internalized_strings_.Add(handle(string)); | |
725 return string; | |
726 } else { | |
727 string->SetForwardedInternalizedString(canonical); | |
728 return canonical; | |
729 } | |
730 } | |
731 } else if (obj->IsScript()) { | |
732 new_scripts_.Add(handle(Script::cast(obj))); | |
733 } else { | |
734 DCHECK(CanBeDeferred(obj)); | |
735 } | |
736 } | |
737 if (obj->IsAllocationSite()) { | |
738 DCHECK(obj->IsAllocationSite()); | |
739 // Allocation sites are present in the snapshot, and must be linked into | |
740 // a list at deserialization time. | |
741 AllocationSite* site = AllocationSite::cast(obj); | |
742 // TODO(mvstanton): consider treating the heap()->allocation_sites_list() | |
743 // as a (weak) root. If this root is relocated correctly, this becomes | |
744 // unnecessary. | |
745 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) { | |
746 site->set_weak_next(isolate_->heap()->undefined_value()); | |
747 } else { | |
748 site->set_weak_next(isolate_->heap()->allocation_sites_list()); | |
749 } | |
750 isolate_->heap()->set_allocation_sites_list(site); | |
751 } else if (obj->IsCode()) { | |
752 // We flush all code pages after deserializing the startup snapshot. In that | |
753 // case, we only need to remember code objects in the large object space. | |
754 // When deserializing user code, remember each individual code object. | |
755 if (deserializing_user_code() || space == LO_SPACE) { | |
756 new_code_objects_.Add(Code::cast(obj)); | |
757 } | |
758 } | |
759 // Check alignment. | |
760 DCHECK_EQ(0, Heap::GetFillToAlign(obj->address(), obj->RequiredAlignment())); | |
761 return obj; | |
762 } | |
763 | |
764 | |
765 void Deserializer::CommitPostProcessedObjects(Isolate* isolate) { | |
766 StringTable::EnsureCapacityForDeserialization( | |
767 isolate, new_internalized_strings_.length()); | |
768 for (Handle<String> string : new_internalized_strings_) { | |
769 StringTableInsertionKey key(*string); | |
770 DCHECK_NULL(StringTable::LookupKeyIfExists(isolate, &key)); | |
771 StringTable::LookupKey(isolate, &key); | |
772 } | |
773 | |
774 Heap* heap = isolate->heap(); | |
775 Factory* factory = isolate->factory(); | |
776 for (Handle<Script> script : new_scripts_) { | |
777 // Assign a new script id to avoid collision. | |
778 script->set_id(isolate_->heap()->NextScriptId()); | |
779 // Add script to list. | |
780 Handle<Object> list = WeakFixedArray::Add(factory->script_list(), script); | |
781 heap->SetRootScriptList(*list); | |
782 } | |
783 } | |
784 | |
785 | |
786 HeapObject* Deserializer::GetBackReferencedObject(int space) { | |
787 HeapObject* obj; | |
788 BackReference back_reference(source_.GetInt()); | |
789 if (space == LO_SPACE) { | |
790 CHECK(back_reference.chunk_index() == 0); | |
791 uint32_t index = back_reference.large_object_index(); | |
792 obj = deserialized_large_objects_[index]; | |
793 } else { | |
794 DCHECK(space < kNumberOfPreallocatedSpaces); | |
795 uint32_t chunk_index = back_reference.chunk_index(); | |
796 DCHECK_LE(chunk_index, current_chunk_[space]); | |
797 uint32_t chunk_offset = back_reference.chunk_offset(); | |
798 Address address = reservations_[space][chunk_index].start + chunk_offset; | |
799 if (next_alignment_ != kWordAligned) { | |
800 int padding = Heap::GetFillToAlign(address, next_alignment_); | |
801 next_alignment_ = kWordAligned; | |
802 DCHECK(padding == 0 || HeapObject::FromAddress(address)->IsFiller()); | |
803 address += padding; | |
804 } | |
805 obj = HeapObject::FromAddress(address); | |
806 } | |
807 if (deserializing_user_code() && obj->IsInternalizedString()) { | |
808 obj = String::cast(obj)->GetForwardedInternalizedString(); | |
809 } | |
810 hot_objects_.Add(obj); | |
811 return obj; | |
812 } | |
813 | |
814 | |
815 // This routine writes the new object into the pointer provided and then | |
816 // returns true if the new object was in young space and false otherwise. | |
817 // The reason for this strange interface is that otherwise the object is | |
818 // written very late, which means the FreeSpace map is not set up by the | |
819 // time we need to use it to mark the space at the end of a page free. | |
820 void Deserializer::ReadObject(int space_number, Object** write_back) { | |
821 Address address; | |
822 HeapObject* obj; | |
823 int size = source_.GetInt() << kObjectAlignmentBits; | |
824 | |
825 if (next_alignment_ != kWordAligned) { | |
826 int reserved = size + Heap::GetMaximumFillToAlign(next_alignment_); | |
827 address = Allocate(space_number, reserved); | |
828 obj = HeapObject::FromAddress(address); | |
829 // If one of the following assertions fails, then we are deserializing an | |
830 // aligned object when the filler maps have not been deserialized yet. | |
831 // We require filler maps as padding to align the object. | |
832 Heap* heap = isolate_->heap(); | |
833 DCHECK(heap->free_space_map()->IsMap()); | |
834 DCHECK(heap->one_pointer_filler_map()->IsMap()); | |
835 DCHECK(heap->two_pointer_filler_map()->IsMap()); | |
836 obj = heap->AlignWithFiller(obj, size, reserved, next_alignment_); | |
837 address = obj->address(); | |
838 next_alignment_ = kWordAligned; | |
839 } else { | |
840 address = Allocate(space_number, size); | |
841 obj = HeapObject::FromAddress(address); | |
842 } | |
843 | |
844 isolate_->heap()->OnAllocationEvent(obj, size); | |
845 Object** current = reinterpret_cast<Object**>(address); | |
846 Object** limit = current + (size >> kPointerSizeLog2); | |
847 if (FLAG_log_snapshot_positions) { | |
848 LOG(isolate_, SnapshotPositionEvent(address, source_.position())); | |
849 } | |
850 | |
851 if (ReadData(current, limit, space_number, address)) { | |
852 // Only post process if object content has not been deferred. | |
853 obj = PostProcessNewObject(obj, space_number); | |
854 } | |
855 | |
856 Object* write_back_obj = obj; | |
857 UnalignedCopy(write_back, &write_back_obj); | |
858 #ifdef DEBUG | |
859 if (obj->IsCode()) { | |
860 DCHECK(space_number == CODE_SPACE || space_number == LO_SPACE); | |
861 } else { | |
862 DCHECK(space_number != CODE_SPACE); | |
863 } | |
864 #endif // DEBUG | |
865 } | |
866 | |
867 | |
868 // We know the space requirements before deserialization and can | |
869 // pre-allocate that reserved space. During deserialization, all we need | |
870 // to do is to bump up the pointer for each space in the reserved | |
871 // space. This is also used for fixing back references. | |
872 // We may have to split up the pre-allocation into several chunks | |
873 // because it would not fit onto a single page. We do not have to keep | |
874 // track of when to move to the next chunk. An opcode will signal this. | |
875 // Since multiple large objects cannot be folded into one large object | |
876 // space allocation, we have to do an actual allocation when deserializing | |
877 // each large object. Instead of tracking offset for back references, we | |
878 // reference large objects by index. | |
879 Address Deserializer::Allocate(int space_index, int size) { | |
880 if (space_index == LO_SPACE) { | |
881 AlwaysAllocateScope scope(isolate_); | |
882 LargeObjectSpace* lo_space = isolate_->heap()->lo_space(); | |
883 Executability exec = static_cast<Executability>(source_.Get()); | |
884 AllocationResult result = lo_space->AllocateRaw(size, exec); | |
885 HeapObject* obj = HeapObject::cast(result.ToObjectChecked()); | |
886 deserialized_large_objects_.Add(obj); | |
887 return obj->address(); | |
888 } else { | |
889 DCHECK(space_index < kNumberOfPreallocatedSpaces); | |
890 Address address = high_water_[space_index]; | |
891 DCHECK_NOT_NULL(address); | |
892 high_water_[space_index] += size; | |
893 #ifdef DEBUG | |
894 // Assert that the current reserved chunk is still big enough. | |
895 const Heap::Reservation& reservation = reservations_[space_index]; | |
896 int chunk_index = current_chunk_[space_index]; | |
897 CHECK_LE(high_water_[space_index], reservation[chunk_index].end); | |
898 #endif | |
899 return address; | |
900 } | |
901 } | |
902 | |
903 | |
904 Object** Deserializer::CopyInNativesSource(Vector<const char> source_vector, | |
905 Object** current) { | |
906 DCHECK(!isolate_->heap()->deserialization_complete()); | |
907 NativesExternalStringResource* resource = new NativesExternalStringResource( | |
908 source_vector.start(), source_vector.length()); | |
909 Object* resource_obj = reinterpret_cast<Object*>(resource); | |
910 UnalignedCopy(current++, &resource_obj); | |
911 return current; | |
912 } | |
913 | |
914 | |
915 bool Deserializer::ReadData(Object** current, Object** limit, int source_space, | |
916 Address current_object_address) { | |
917 Isolate* const isolate = isolate_; | |
918 // Write barrier support costs around 1% in startup time. In fact there | |
919 // are no new space objects in current boot snapshots, so it's not needed, | |
920 // but that may change. | |
921 bool write_barrier_needed = | |
922 (current_object_address != NULL && source_space != NEW_SPACE && | |
923 source_space != CODE_SPACE); | |
924 while (current < limit) { | |
925 byte data = source_.Get(); | |
926 switch (data) { | |
927 #define CASE_STATEMENT(where, how, within, space_number) \ | |
928 case where + how + within + space_number: \ | |
929 STATIC_ASSERT((where & ~kWhereMask) == 0); \ | |
930 STATIC_ASSERT((how & ~kHowToCodeMask) == 0); \ | |
931 STATIC_ASSERT((within & ~kWhereToPointMask) == 0); \ | |
932 STATIC_ASSERT((space_number & ~kSpaceMask) == 0); | |
933 | |
934 #define CASE_BODY(where, how, within, space_number_if_any) \ | |
935 { \ | |
936 bool emit_write_barrier = false; \ | |
937 bool current_was_incremented = false; \ | |
938 int space_number = space_number_if_any == kAnyOldSpace \ | |
939 ? (data & kSpaceMask) \ | |
940 : space_number_if_any; \ | |
941 if (where == kNewObject && how == kPlain && within == kStartOfObject) { \ | |
942 ReadObject(space_number, current); \ | |
943 emit_write_barrier = (space_number == NEW_SPACE); \ | |
944 } else { \ | |
945 Object* new_object = NULL; /* May not be a real Object pointer. */ \ | |
946 if (where == kNewObject) { \ | |
947 ReadObject(space_number, &new_object); \ | |
948 } else if (where == kBackref) { \ | |
949 emit_write_barrier = (space_number == NEW_SPACE); \ | |
950 new_object = GetBackReferencedObject(data & kSpaceMask); \ | |
951 } else if (where == kBackrefWithSkip) { \ | |
952 int skip = source_.GetInt(); \ | |
953 current = reinterpret_cast<Object**>( \ | |
954 reinterpret_cast<Address>(current) + skip); \ | |
955 emit_write_barrier = (space_number == NEW_SPACE); \ | |
956 new_object = GetBackReferencedObject(data & kSpaceMask); \ | |
957 } else if (where == kRootArray) { \ | |
958 int id = source_.GetInt(); \ | |
959 Heap::RootListIndex root_index = static_cast<Heap::RootListIndex>(id); \ | |
960 new_object = isolate->heap()->root(root_index); \ | |
961 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \ | |
962 } else if (where == kPartialSnapshotCache) { \ | |
963 int cache_index = source_.GetInt(); \ | |
964 new_object = isolate->partial_snapshot_cache()->at(cache_index); \ | |
965 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \ | |
966 } else if (where == kExternalReference) { \ | |
967 int skip = source_.GetInt(); \ | |
968 current = reinterpret_cast<Object**>( \ | |
969 reinterpret_cast<Address>(current) + skip); \ | |
970 int reference_id = source_.GetInt(); \ | |
971 Address address = external_reference_table_->address(reference_id); \ | |
972 new_object = reinterpret_cast<Object*>(address); \ | |
973 } else if (where == kAttachedReference) { \ | |
974 int index = source_.GetInt(); \ | |
975 DCHECK(deserializing_user_code() || index == kGlobalProxyReference); \ | |
976 new_object = *attached_objects_[index]; \ | |
977 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \ | |
978 } else { \ | |
979 DCHECK(where == kBuiltin); \ | |
980 DCHECK(deserializing_user_code()); \ | |
981 int builtin_id = source_.GetInt(); \ | |
982 DCHECK_LE(0, builtin_id); \ | |
983 DCHECK_LT(builtin_id, Builtins::builtin_count); \ | |
984 Builtins::Name name = static_cast<Builtins::Name>(builtin_id); \ | |
985 new_object = isolate->builtins()->builtin(name); \ | |
986 emit_write_barrier = false; \ | |
987 } \ | |
988 if (within == kInnerPointer) { \ | |
989 if (space_number != CODE_SPACE || new_object->IsCode()) { \ | |
990 Code* new_code_object = reinterpret_cast<Code*>(new_object); \ | |
991 new_object = \ | |
992 reinterpret_cast<Object*>(new_code_object->instruction_start()); \ | |
993 } else { \ | |
994 DCHECK(space_number == CODE_SPACE); \ | |
995 Cell* cell = Cell::cast(new_object); \ | |
996 new_object = reinterpret_cast<Object*>(cell->ValueAddress()); \ | |
997 } \ | |
998 } \ | |
999 if (how == kFromCode) { \ | |
1000 Address location_of_branch_data = reinterpret_cast<Address>(current); \ | |
1001 Assembler::deserialization_set_special_target_at( \ | |
1002 isolate, location_of_branch_data, \ | |
1003 Code::cast(HeapObject::FromAddress(current_object_address)), \ | |
1004 reinterpret_cast<Address>(new_object)); \ | |
1005 location_of_branch_data += Assembler::kSpecialTargetSize; \ | |
1006 current = reinterpret_cast<Object**>(location_of_branch_data); \ | |
1007 current_was_incremented = true; \ | |
1008 } else { \ | |
1009 UnalignedCopy(current, &new_object); \ | |
1010 } \ | |
1011 } \ | |
1012 if (emit_write_barrier && write_barrier_needed) { \ | |
1013 Address current_address = reinterpret_cast<Address>(current); \ | |
1014 SLOW_DCHECK(isolate->heap()->ContainsSlow(current_object_address)); \ | |
1015 isolate->heap()->RecordWrite( \ | |
1016 HeapObject::FromAddress(current_object_address), \ | |
1017 static_cast<int>(current_address - current_object_address), \ | |
1018 *reinterpret_cast<Object**>(current_address)); \ | |
1019 } \ | |
1020 if (!current_was_incremented) { \ | |
1021 current++; \ | |
1022 } \ | |
1023 break; \ | |
1024 } | |
1025 | |
1026 // This generates a case and a body for the new space (which has to do extra | |
1027 // write barrier handling) and handles the other spaces with fall-through cases | |
1028 // and one body. | |
1029 #define ALL_SPACES(where, how, within) \ | |
1030 CASE_STATEMENT(where, how, within, NEW_SPACE) \ | |
1031 CASE_BODY(where, how, within, NEW_SPACE) \ | |
1032 CASE_STATEMENT(where, how, within, OLD_SPACE) \ | |
1033 CASE_STATEMENT(where, how, within, CODE_SPACE) \ | |
1034 CASE_STATEMENT(where, how, within, MAP_SPACE) \ | |
1035 CASE_STATEMENT(where, how, within, LO_SPACE) \ | |
1036 CASE_BODY(where, how, within, kAnyOldSpace) | |
1037 | |
1038 #define FOUR_CASES(byte_code) \ | |
1039 case byte_code: \ | |
1040 case byte_code + 1: \ | |
1041 case byte_code + 2: \ | |
1042 case byte_code + 3: | |
1043 | |
1044 #define SIXTEEN_CASES(byte_code) \ | |
1045 FOUR_CASES(byte_code) \ | |
1046 FOUR_CASES(byte_code + 4) \ | |
1047 FOUR_CASES(byte_code + 8) \ | |
1048 FOUR_CASES(byte_code + 12) | |
1049 | |
1050 #define SINGLE_CASE(where, how, within, space) \ | |
1051 CASE_STATEMENT(where, how, within, space) \ | |
1052 CASE_BODY(where, how, within, space) | |
1053 | |
1054 // Deserialize a new object and write a pointer to it to the current | |
1055 // object. | |
1056 ALL_SPACES(kNewObject, kPlain, kStartOfObject) | |
1057 // Support for direct instruction pointers in functions. It's an inner | |
1058 // pointer because it points at the entry point, not at the start of the | |
1059 // code object. | |
1060 SINGLE_CASE(kNewObject, kPlain, kInnerPointer, CODE_SPACE) | |
1061 // Deserialize a new code object and write a pointer to its first | |
1062 // instruction to the current code object. | |
1063 ALL_SPACES(kNewObject, kFromCode, kInnerPointer) | |
1064 // Find a recently deserialized object using its offset from the current | |
1065 // allocation point and write a pointer to it to the current object. | |
1066 ALL_SPACES(kBackref, kPlain, kStartOfObject) | |
1067 ALL_SPACES(kBackrefWithSkip, kPlain, kStartOfObject) | |
1068 #if defined(V8_TARGET_ARCH_MIPS) || defined(V8_TARGET_ARCH_MIPS64) || \ | |
1069 defined(V8_TARGET_ARCH_PPC) || V8_EMBEDDED_CONSTANT_POOL | |
1070 // Deserialize a new object from pointer found in code and write | |
1071 // a pointer to it to the current object. Required only for MIPS, PPC or | |
1072 // ARM with embedded constant pool, and omitted on the other architectures | |
1073 // because it is fully unrolled and would cause bloat. | |
1074 ALL_SPACES(kNewObject, kFromCode, kStartOfObject) | |
1075 // Find a recently deserialized code object using its offset from the | |
1076 // current allocation point and write a pointer to it to the current | |
1077 // object. Required only for MIPS, PPC or ARM with embedded constant pool. | |
1078 ALL_SPACES(kBackref, kFromCode, kStartOfObject) | |
1079 ALL_SPACES(kBackrefWithSkip, kFromCode, kStartOfObject) | |
1080 #endif | |
1081 // Find a recently deserialized code object using its offset from the | |
1082 // current allocation point and write a pointer to its first instruction | |
1083 // to the current code object or the instruction pointer in a function | |
1084 // object. | |
1085 ALL_SPACES(kBackref, kFromCode, kInnerPointer) | |
1086 ALL_SPACES(kBackrefWithSkip, kFromCode, kInnerPointer) | |
1087 ALL_SPACES(kBackref, kPlain, kInnerPointer) | |
1088 ALL_SPACES(kBackrefWithSkip, kPlain, kInnerPointer) | |
1089 // Find an object in the roots array and write a pointer to it to the | |
1090 // current object. | |
1091 SINGLE_CASE(kRootArray, kPlain, kStartOfObject, 0) | |
1092 #if defined(V8_TARGET_ARCH_MIPS) || defined(V8_TARGET_ARCH_MIPS64) || \ | |
1093 defined(V8_TARGET_ARCH_PPC) || V8_EMBEDDED_CONSTANT_POOL | |
1094 // Find an object in the roots array and write a pointer to it to in code. | |
1095 SINGLE_CASE(kRootArray, kFromCode, kStartOfObject, 0) | |
1096 #endif | |
1097 // Find an object in the partial snapshots cache and write a pointer to it | |
1098 // to the current object. | |
1099 SINGLE_CASE(kPartialSnapshotCache, kPlain, kStartOfObject, 0) | |
1100 // Find an code entry in the partial snapshots cache and | |
1101 // write a pointer to it to the current object. | |
1102 SINGLE_CASE(kPartialSnapshotCache, kPlain, kInnerPointer, 0) | |
1103 // Find an external reference and write a pointer to it to the current | |
1104 // object. | |
1105 SINGLE_CASE(kExternalReference, kPlain, kStartOfObject, 0) | |
1106 // Find an external reference and write a pointer to it in the current | |
1107 // code object. | |
1108 SINGLE_CASE(kExternalReference, kFromCode, kStartOfObject, 0) | |
1109 // Find an object in the attached references and write a pointer to it to | |
1110 // the current object. | |
1111 SINGLE_CASE(kAttachedReference, kPlain, kStartOfObject, 0) | |
1112 SINGLE_CASE(kAttachedReference, kPlain, kInnerPointer, 0) | |
1113 SINGLE_CASE(kAttachedReference, kFromCode, kInnerPointer, 0) | |
1114 // Find a builtin and write a pointer to it to the current object. | |
1115 SINGLE_CASE(kBuiltin, kPlain, kStartOfObject, 0) | |
1116 SINGLE_CASE(kBuiltin, kPlain, kInnerPointer, 0) | |
1117 SINGLE_CASE(kBuiltin, kFromCode, kInnerPointer, 0) | |
1118 | |
1119 #undef CASE_STATEMENT | |
1120 #undef CASE_BODY | |
1121 #undef ALL_SPACES | |
1122 | |
1123 case kSkip: { | |
1124 int size = source_.GetInt(); | |
1125 current = reinterpret_cast<Object**>( | |
1126 reinterpret_cast<intptr_t>(current) + size); | |
1127 break; | |
1128 } | |
1129 | |
1130 case kInternalReferenceEncoded: | |
1131 case kInternalReference: { | |
1132 // Internal reference address is not encoded via skip, but by offset | |
1133 // from code entry. | |
1134 int pc_offset = source_.GetInt(); | |
1135 int target_offset = source_.GetInt(); | |
1136 Code* code = | |
1137 Code::cast(HeapObject::FromAddress(current_object_address)); | |
1138 DCHECK(0 <= pc_offset && pc_offset <= code->instruction_size()); | |
1139 DCHECK(0 <= target_offset && target_offset <= code->instruction_size()); | |
1140 Address pc = code->entry() + pc_offset; | |
1141 Address target = code->entry() + target_offset; | |
1142 Assembler::deserialization_set_target_internal_reference_at( | |
1143 isolate, pc, target, data == kInternalReference | |
1144 ? RelocInfo::INTERNAL_REFERENCE | |
1145 : RelocInfo::INTERNAL_REFERENCE_ENCODED); | |
1146 break; | |
1147 } | |
1148 | |
1149 case kNop: | |
1150 break; | |
1151 | |
1152 case kNextChunk: { | |
1153 int space = source_.Get(); | |
1154 DCHECK(space < kNumberOfPreallocatedSpaces); | |
1155 int chunk_index = current_chunk_[space]; | |
1156 const Heap::Reservation& reservation = reservations_[space]; | |
1157 // Make sure the current chunk is indeed exhausted. | |
1158 CHECK_EQ(reservation[chunk_index].end, high_water_[space]); | |
1159 // Move to next reserved chunk. | |
1160 chunk_index = ++current_chunk_[space]; | |
1161 CHECK_LT(chunk_index, reservation.length()); | |
1162 high_water_[space] = reservation[chunk_index].start; | |
1163 break; | |
1164 } | |
1165 | |
1166 case kDeferred: { | |
1167 // Deferred can only occur right after the heap object header. | |
1168 DCHECK(current == reinterpret_cast<Object**>(current_object_address + | |
1169 kPointerSize)); | |
1170 HeapObject* obj = HeapObject::FromAddress(current_object_address); | |
1171 // If the deferred object is a map, its instance type may be used | |
1172 // during deserialization. Initialize it with a temporary value. | |
1173 if (obj->IsMap()) Map::cast(obj)->set_instance_type(FILLER_TYPE); | |
1174 current = limit; | |
1175 return false; | |
1176 } | |
1177 | |
1178 case kSynchronize: | |
1179 // If we get here then that indicates that you have a mismatch between | |
1180 // the number of GC roots when serializing and deserializing. | |
1181 CHECK(false); | |
1182 break; | |
1183 | |
1184 case kNativesStringResource: | |
1185 current = CopyInNativesSource(Natives::GetScriptSource(source_.Get()), | |
1186 current); | |
1187 break; | |
1188 | |
1189 case kExtraNativesStringResource: | |
1190 current = CopyInNativesSource( | |
1191 ExtraNatives::GetScriptSource(source_.Get()), current); | |
1192 break; | |
1193 | |
1194 // Deserialize raw data of variable length. | |
1195 case kVariableRawData: { | |
1196 int size_in_bytes = source_.GetInt(); | |
1197 byte* raw_data_out = reinterpret_cast<byte*>(current); | |
1198 source_.CopyRaw(raw_data_out, size_in_bytes); | |
1199 break; | |
1200 } | |
1201 | |
1202 case kVariableRepeat: { | |
1203 int repeats = source_.GetInt(); | |
1204 Object* object = current[-1]; | |
1205 DCHECK(!isolate->heap()->InNewSpace(object)); | |
1206 for (int i = 0; i < repeats; i++) UnalignedCopy(current++, &object); | |
1207 break; | |
1208 } | |
1209 | |
1210 case kAlignmentPrefix: | |
1211 case kAlignmentPrefix + 1: | |
1212 case kAlignmentPrefix + 2: | |
1213 SetAlignment(data); | |
1214 break; | |
1215 | |
1216 STATIC_ASSERT(kNumberOfRootArrayConstants == Heap::kOldSpaceRoots); | |
1217 STATIC_ASSERT(kNumberOfRootArrayConstants == 32); | |
1218 SIXTEEN_CASES(kRootArrayConstantsWithSkip) | |
1219 SIXTEEN_CASES(kRootArrayConstantsWithSkip + 16) { | |
1220 int skip = source_.GetInt(); | |
1221 current = reinterpret_cast<Object**>( | |
1222 reinterpret_cast<intptr_t>(current) + skip); | |
1223 // Fall through. | |
1224 } | |
1225 | |
1226 SIXTEEN_CASES(kRootArrayConstants) | |
1227 SIXTEEN_CASES(kRootArrayConstants + 16) { | |
1228 int id = data & kRootArrayConstantsMask; | |
1229 Heap::RootListIndex root_index = static_cast<Heap::RootListIndex>(id); | |
1230 Object* object = isolate->heap()->root(root_index); | |
1231 DCHECK(!isolate->heap()->InNewSpace(object)); | |
1232 UnalignedCopy(current++, &object); | |
1233 break; | |
1234 } | |
1235 | |
1236 STATIC_ASSERT(kNumberOfHotObjects == 8); | |
1237 FOUR_CASES(kHotObjectWithSkip) | |
1238 FOUR_CASES(kHotObjectWithSkip + 4) { | |
1239 int skip = source_.GetInt(); | |
1240 current = reinterpret_cast<Object**>( | |
1241 reinterpret_cast<Address>(current) + skip); | |
1242 // Fall through. | |
1243 } | |
1244 | |
1245 FOUR_CASES(kHotObject) | |
1246 FOUR_CASES(kHotObject + 4) { | |
1247 int index = data & kHotObjectMask; | |
1248 Object* hot_object = hot_objects_.Get(index); | |
1249 UnalignedCopy(current, &hot_object); | |
1250 if (write_barrier_needed) { | |
1251 Address current_address = reinterpret_cast<Address>(current); | |
1252 SLOW_DCHECK(isolate->heap()->ContainsSlow(current_object_address)); | |
1253 isolate->heap()->RecordWrite( | |
1254 HeapObject::FromAddress(current_object_address), | |
1255 static_cast<int>(current_address - current_object_address), | |
1256 hot_object); | |
1257 } | |
1258 current++; | |
1259 break; | |
1260 } | |
1261 | |
1262 // Deserialize raw data of fixed length from 1 to 32 words. | |
1263 STATIC_ASSERT(kNumberOfFixedRawData == 32); | |
1264 SIXTEEN_CASES(kFixedRawData) | |
1265 SIXTEEN_CASES(kFixedRawData + 16) { | |
1266 byte* raw_data_out = reinterpret_cast<byte*>(current); | |
1267 int size_in_bytes = (data - kFixedRawDataStart) << kPointerSizeLog2; | |
1268 source_.CopyRaw(raw_data_out, size_in_bytes); | |
1269 current = reinterpret_cast<Object**>(raw_data_out + size_in_bytes); | |
1270 break; | |
1271 } | |
1272 | |
1273 STATIC_ASSERT(kNumberOfFixedRepeat == 16); | |
1274 SIXTEEN_CASES(kFixedRepeat) { | |
1275 int repeats = data - kFixedRepeatStart; | |
1276 Object* object; | |
1277 UnalignedCopy(&object, current - 1); | |
1278 DCHECK(!isolate->heap()->InNewSpace(object)); | |
1279 for (int i = 0; i < repeats; i++) UnalignedCopy(current++, &object); | |
1280 break; | |
1281 } | |
1282 | |
1283 #undef SIXTEEN_CASES | |
1284 #undef FOUR_CASES | |
1285 #undef SINGLE_CASE | |
1286 | |
1287 default: | |
1288 CHECK(false); | |
1289 } | |
1290 } | |
1291 CHECK_EQ(limit, current); | |
1292 return true; | |
1293 } | |
1294 | |
1295 | |
1296 Serializer::Serializer(Isolate* isolate, SnapshotByteSink* sink) | |
1297 : isolate_(isolate), | |
1298 sink_(sink), | |
1299 external_reference_encoder_(isolate), | |
1300 root_index_map_(isolate), | |
1301 recursion_depth_(0), | |
1302 code_address_map_(NULL), | |
1303 large_objects_total_size_(0), | |
1304 seen_large_objects_index_(0) { | |
1305 // The serializer is meant to be used only to generate initial heap images | |
1306 // from a context in which there is only one isolate. | |
1307 for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) { | |
1308 pending_chunk_[i] = 0; | |
1309 max_chunk_size_[i] = static_cast<uint32_t>( | |
1310 MemoryAllocator::PageAreaSize(static_cast<AllocationSpace>(i))); | |
1311 } | |
1312 | |
1313 #ifdef OBJECT_PRINT | |
1314 if (FLAG_serialization_statistics) { | |
1315 instance_type_count_ = NewArray<int>(kInstanceTypes); | |
1316 instance_type_size_ = NewArray<size_t>(kInstanceTypes); | |
1317 for (int i = 0; i < kInstanceTypes; i++) { | |
1318 instance_type_count_[i] = 0; | |
1319 instance_type_size_[i] = 0; | |
1320 } | |
1321 } else { | |
1322 instance_type_count_ = NULL; | |
1323 instance_type_size_ = NULL; | |
1324 } | |
1325 #endif // OBJECT_PRINT | |
1326 } | |
1327 | |
1328 | |
1329 Serializer::~Serializer() { | |
1330 if (code_address_map_ != NULL) delete code_address_map_; | |
1331 #ifdef OBJECT_PRINT | |
1332 if (instance_type_count_ != NULL) { | |
1333 DeleteArray(instance_type_count_); | |
1334 DeleteArray(instance_type_size_); | |
1335 } | |
1336 #endif // OBJECT_PRINT | |
1337 } | |
1338 | |
1339 | |
1340 #ifdef OBJECT_PRINT | |
1341 void Serializer::CountInstanceType(Map* map, int size) { | |
1342 int instance_type = map->instance_type(); | |
1343 instance_type_count_[instance_type]++; | |
1344 instance_type_size_[instance_type] += size; | |
1345 } | |
1346 #endif // OBJECT_PRINT | |
1347 | |
1348 | |
1349 void Serializer::OutputStatistics(const char* name) { | |
1350 if (!FLAG_serialization_statistics) return; | |
1351 PrintF("%s:\n", name); | |
1352 PrintF(" Spaces (bytes):\n"); | |
1353 for (int space = 0; space < kNumberOfSpaces; space++) { | |
1354 PrintF("%16s", AllocationSpaceName(static_cast<AllocationSpace>(space))); | |
1355 } | |
1356 PrintF("\n"); | |
1357 for (int space = 0; space < kNumberOfPreallocatedSpaces; space++) { | |
1358 size_t s = pending_chunk_[space]; | |
1359 for (uint32_t chunk_size : completed_chunks_[space]) s += chunk_size; | |
1360 PrintF("%16" V8_PTR_PREFIX "d", s); | |
1361 } | |
1362 PrintF("%16d\n", large_objects_total_size_); | |
1363 #ifdef OBJECT_PRINT | |
1364 PrintF(" Instance types (count and bytes):\n"); | |
1365 #define PRINT_INSTANCE_TYPE(Name) \ | |
1366 if (instance_type_count_[Name]) { \ | |
1367 PrintF("%10d %10" V8_PTR_PREFIX "d %s\n", instance_type_count_[Name], \ | |
1368 instance_type_size_[Name], #Name); \ | |
1369 } | |
1370 INSTANCE_TYPE_LIST(PRINT_INSTANCE_TYPE) | |
1371 #undef PRINT_INSTANCE_TYPE | |
1372 PrintF("\n"); | |
1373 #endif // OBJECT_PRINT | |
1374 } | |
1375 | |
1376 | |
1377 class Serializer::ObjectSerializer : public ObjectVisitor { | |
1378 public: | |
1379 ObjectSerializer(Serializer* serializer, Object* o, SnapshotByteSink* sink, | |
1380 HowToCode how_to_code, WhereToPoint where_to_point) | |
1381 : serializer_(serializer), | |
1382 object_(HeapObject::cast(o)), | |
1383 sink_(sink), | |
1384 reference_representation_(how_to_code + where_to_point), | |
1385 bytes_processed_so_far_(0), | |
1386 is_code_object_(o->IsCode()), | |
1387 code_has_been_output_(false) {} | |
1388 void Serialize(); | |
1389 void SerializeDeferred(); | |
1390 void VisitPointers(Object** start, Object** end) override; | |
1391 void VisitEmbeddedPointer(RelocInfo* target) override; | |
1392 void VisitExternalReference(Address* p) override; | |
1393 void VisitExternalReference(RelocInfo* rinfo) override; | |
1394 void VisitInternalReference(RelocInfo* rinfo) override; | |
1395 void VisitCodeTarget(RelocInfo* target) override; | |
1396 void VisitCodeEntry(Address entry_address) override; | |
1397 void VisitCell(RelocInfo* rinfo) override; | |
1398 void VisitRuntimeEntry(RelocInfo* reloc) override; | |
1399 // Used for seralizing the external strings that hold the natives source. | |
1400 void VisitExternalOneByteString( | |
1401 v8::String::ExternalOneByteStringResource** resource) override; | |
1402 // We can't serialize a heap with external two byte strings. | |
1403 void VisitExternalTwoByteString( | |
1404 v8::String::ExternalStringResource** resource) override { | |
1405 UNREACHABLE(); | |
1406 } | |
1407 | |
1408 private: | |
1409 void SerializePrologue(AllocationSpace space, int size, Map* map); | |
1410 | |
1411 bool SerializeExternalNativeSourceString( | |
1412 int builtin_count, | |
1413 v8::String::ExternalOneByteStringResource** resource_pointer, | |
1414 FixedArray* source_cache, int resource_index); | |
1415 | |
1416 enum ReturnSkip { kCanReturnSkipInsteadOfSkipping, kIgnoringReturn }; | |
1417 // This function outputs or skips the raw data between the last pointer and | |
1418 // up to the current position. It optionally can just return the number of | |
1419 // bytes to skip instead of performing a skip instruction, in case the skip | |
1420 // can be merged into the next instruction. | |
1421 int OutputRawData(Address up_to, ReturnSkip return_skip = kIgnoringReturn); | |
1422 // External strings are serialized in a way to resemble sequential strings. | |
1423 void SerializeExternalString(); | |
1424 | |
1425 Address PrepareCode(); | |
1426 | |
1427 Serializer* serializer_; | |
1428 HeapObject* object_; | |
1429 SnapshotByteSink* sink_; | |
1430 int reference_representation_; | |
1431 int bytes_processed_so_far_; | |
1432 bool is_code_object_; | |
1433 bool code_has_been_output_; | |
1434 }; | |
1435 | |
1436 | |
1437 void Serializer::SerializeDeferredObjects() { | |
1438 while (deferred_objects_.length() > 0) { | |
1439 HeapObject* obj = deferred_objects_.RemoveLast(); | |
1440 ObjectSerializer obj_serializer(this, obj, sink_, kPlain, kStartOfObject); | |
1441 obj_serializer.SerializeDeferred(); | |
1442 } | |
1443 sink_->Put(kSynchronize, "Finished with deferred objects"); | |
1444 } | |
1445 | |
1446 | |
1447 void StartupSerializer::SerializeStrongReferences() { | |
1448 Isolate* isolate = this->isolate(); | |
1449 // No active threads. | |
1450 CHECK_NULL(isolate->thread_manager()->FirstThreadStateInUse()); | |
1451 // No active or weak handles. | |
1452 CHECK(isolate->handle_scope_implementer()->blocks()->is_empty()); | |
1453 CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles()); | |
1454 CHECK_EQ(0, isolate->eternal_handles()->NumberOfHandles()); | |
1455 // We don't support serializing installed extensions. | |
1456 CHECK(!isolate->has_installed_extensions()); | |
1457 isolate->heap()->IterateSmiRoots(this); | |
1458 isolate->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG); | |
1459 } | |
1460 | |
1461 | |
1462 void StartupSerializer::VisitPointers(Object** start, Object** end) { | |
1463 for (Object** current = start; current < end; current++) { | |
1464 if (start == isolate()->heap()->roots_array_start()) { | |
1465 root_index_wave_front_ = | |
1466 Max(root_index_wave_front_, static_cast<intptr_t>(current - start)); | |
1467 } | |
1468 if (ShouldBeSkipped(current)) { | |
1469 sink_->Put(kSkip, "Skip"); | |
1470 sink_->PutInt(kPointerSize, "SkipOneWord"); | |
1471 } else if ((*current)->IsSmi()) { | |
1472 sink_->Put(kOnePointerRawData, "Smi"); | |
1473 for (int i = 0; i < kPointerSize; i++) { | |
1474 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte"); | |
1475 } | |
1476 } else { | |
1477 SerializeObject(HeapObject::cast(*current), kPlain, kStartOfObject, 0); | |
1478 } | |
1479 } | |
1480 } | |
1481 | |
1482 | |
1483 void PartialSerializer::Serialize(Object** o) { | |
1484 if ((*o)->IsContext()) { | |
1485 Context* context = Context::cast(*o); | |
1486 global_object_ = context->global_object(); | |
1487 back_reference_map()->AddGlobalProxy(context->global_proxy()); | |
1488 // The bootstrap snapshot has a code-stub context. When serializing the | |
1489 // partial snapshot, it is chained into the weak context list on the isolate | |
1490 // and it's next context pointer may point to the code-stub context. Clear | |
1491 // it before serializing, it will get re-added to the context list | |
1492 // explicitly when it's loaded. | |
1493 if (context->IsNativeContext()) { | |
1494 context->set(Context::NEXT_CONTEXT_LINK, | |
1495 isolate_->heap()->undefined_value()); | |
1496 DCHECK(!context->global_object()->IsUndefined()); | |
1497 } | |
1498 } | |
1499 VisitPointer(o); | |
1500 SerializeDeferredObjects(); | |
1501 Pad(); | |
1502 } | |
1503 | |
1504 | |
1505 bool Serializer::ShouldBeSkipped(Object** current) { | |
1506 Object** roots = isolate()->heap()->roots_array_start(); | |
1507 return current == &roots[Heap::kStoreBufferTopRootIndex] | |
1508 || current == &roots[Heap::kStackLimitRootIndex] | |
1509 || current == &roots[Heap::kRealStackLimitRootIndex]; | |
1510 } | |
1511 | |
1512 | |
1513 void Serializer::VisitPointers(Object** start, Object** end) { | |
1514 for (Object** current = start; current < end; current++) { | |
1515 if ((*current)->IsSmi()) { | |
1516 sink_->Put(kOnePointerRawData, "Smi"); | |
1517 for (int i = 0; i < kPointerSize; i++) { | |
1518 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte"); | |
1519 } | |
1520 } else { | |
1521 SerializeObject(HeapObject::cast(*current), kPlain, kStartOfObject, 0); | |
1522 } | |
1523 } | |
1524 } | |
1525 | |
1526 | |
1527 void Serializer::EncodeReservations( | |
1528 List<SerializedData::Reservation>* out) const { | |
1529 for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) { | |
1530 for (int j = 0; j < completed_chunks_[i].length(); j++) { | |
1531 out->Add(SerializedData::Reservation(completed_chunks_[i][j])); | |
1532 } | |
1533 | |
1534 if (pending_chunk_[i] > 0 || completed_chunks_[i].length() == 0) { | |
1535 out->Add(SerializedData::Reservation(pending_chunk_[i])); | |
1536 } | |
1537 out->last().mark_as_last(); | |
1538 } | |
1539 | |
1540 out->Add(SerializedData::Reservation(large_objects_total_size_)); | |
1541 out->last().mark_as_last(); | |
1542 } | |
1543 | |
1544 | |
1545 // This ensures that the partial snapshot cache keeps things alive during GC and | |
1546 // tracks their movement. When it is called during serialization of the startup | |
1547 // snapshot nothing happens. When the partial (context) snapshot is created, | |
1548 // this array is populated with the pointers that the partial snapshot will | |
1549 // need. As that happens we emit serialized objects to the startup snapshot | |
1550 // that correspond to the elements of this cache array. On deserialization we | |
1551 // therefore need to visit the cache array. This fills it up with pointers to | |
1552 // deserialized objects. | |
1553 void SerializerDeserializer::Iterate(Isolate* isolate, | |
1554 ObjectVisitor* visitor) { | |
1555 if (isolate->serializer_enabled()) return; | |
1556 List<Object*>* cache = isolate->partial_snapshot_cache(); | |
1557 for (int i = 0;; ++i) { | |
1558 // Extend the array ready to get a value when deserializing. | |
1559 if (cache->length() <= i) cache->Add(Smi::FromInt(0)); | |
1560 visitor->VisitPointer(&cache->at(i)); | |
1561 // Sentinel is the undefined object, which is a root so it will not normally | |
1562 // be found in the cache. | |
1563 if (cache->at(i)->IsUndefined()) break; | |
1564 } | |
1565 } | |
1566 | |
1567 | |
1568 bool SerializerDeserializer::CanBeDeferred(HeapObject* o) { | |
1569 return !o->IsString() && !o->IsScript(); | |
1570 } | |
1571 | |
1572 | |
1573 int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) { | |
1574 Isolate* isolate = this->isolate(); | |
1575 List<Object*>* cache = isolate->partial_snapshot_cache(); | |
1576 int new_index = cache->length(); | |
1577 | |
1578 int index = partial_cache_index_map_.LookupOrInsert(heap_object, new_index); | |
1579 if (index == PartialCacheIndexMap::kInvalidIndex) { | |
1580 // We didn't find the object in the cache. So we add it to the cache and | |
1581 // then visit the pointer so that it becomes part of the startup snapshot | |
1582 // and we can refer to it from the partial snapshot. | |
1583 cache->Add(heap_object); | |
1584 startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object)); | |
1585 // We don't recurse from the startup snapshot generator into the partial | |
1586 // snapshot generator. | |
1587 return new_index; | |
1588 } | |
1589 return index; | |
1590 } | |
1591 | |
1592 | |
1593 bool PartialSerializer::ShouldBeInThePartialSnapshotCache(HeapObject* o) { | |
1594 // Scripts should be referred only through shared function infos. We can't | |
1595 // allow them to be part of the partial snapshot because they contain a | |
1596 // unique ID, and deserializing several partial snapshots containing script | |
1597 // would cause dupes. | |
1598 DCHECK(!o->IsScript()); | |
1599 return o->IsName() || o->IsSharedFunctionInfo() || o->IsHeapNumber() || | |
1600 o->IsCode() || o->IsScopeInfo() || o->IsAccessorInfo() || | |
1601 o->map() == | |
1602 startup_serializer_->isolate()->heap()->fixed_cow_array_map(); | |
1603 } | |
1604 | |
1605 | |
1606 #ifdef DEBUG | |
1607 bool Serializer::BackReferenceIsAlreadyAllocated(BackReference reference) { | |
1608 DCHECK(reference.is_valid()); | |
1609 DCHECK(!reference.is_source()); | |
1610 DCHECK(!reference.is_global_proxy()); | |
1611 AllocationSpace space = reference.space(); | |
1612 int chunk_index = reference.chunk_index(); | |
1613 if (space == LO_SPACE) { | |
1614 return chunk_index == 0 && | |
1615 reference.large_object_index() < seen_large_objects_index_; | |
1616 } else if (chunk_index == completed_chunks_[space].length()) { | |
1617 return reference.chunk_offset() < pending_chunk_[space]; | |
1618 } else { | |
1619 return chunk_index < completed_chunks_[space].length() && | |
1620 reference.chunk_offset() < completed_chunks_[space][chunk_index]; | |
1621 } | |
1622 } | |
1623 #endif // DEBUG | |
1624 | |
1625 | |
1626 bool Serializer::SerializeKnownObject(HeapObject* obj, HowToCode how_to_code, | |
1627 WhereToPoint where_to_point, int skip) { | |
1628 if (how_to_code == kPlain && where_to_point == kStartOfObject) { | |
1629 // Encode a reference to a hot object by its index in the working set. | |
1630 int index = hot_objects_.Find(obj); | |
1631 if (index != HotObjectsList::kNotFound) { | |
1632 DCHECK(index >= 0 && index < kNumberOfHotObjects); | |
1633 if (FLAG_trace_serializer) { | |
1634 PrintF(" Encoding hot object %d:", index); | |
1635 obj->ShortPrint(); | |
1636 PrintF("\n"); | |
1637 } | |
1638 if (skip != 0) { | |
1639 sink_->Put(kHotObjectWithSkip + index, "HotObjectWithSkip"); | |
1640 sink_->PutInt(skip, "HotObjectSkipDistance"); | |
1641 } else { | |
1642 sink_->Put(kHotObject + index, "HotObject"); | |
1643 } | |
1644 return true; | |
1645 } | |
1646 } | |
1647 BackReference back_reference = back_reference_map_.Lookup(obj); | |
1648 if (back_reference.is_valid()) { | |
1649 // Encode the location of an already deserialized object in order to write | |
1650 // its location into a later object. We can encode the location as an | |
1651 // offset fromthe start of the deserialized objects or as an offset | |
1652 // backwards from thecurrent allocation pointer. | |
1653 if (back_reference.is_source()) { | |
1654 FlushSkip(skip); | |
1655 if (FLAG_trace_serializer) PrintF(" Encoding source object\n"); | |
1656 DCHECK(how_to_code == kPlain && where_to_point == kStartOfObject); | |
1657 sink_->Put(kAttachedReference + kPlain + kStartOfObject, "Source"); | |
1658 sink_->PutInt(kSourceObjectReference, "kSourceObjectReference"); | |
1659 } else if (back_reference.is_global_proxy()) { | |
1660 FlushSkip(skip); | |
1661 if (FLAG_trace_serializer) PrintF(" Encoding global proxy\n"); | |
1662 DCHECK(how_to_code == kPlain && where_to_point == kStartOfObject); | |
1663 sink_->Put(kAttachedReference + kPlain + kStartOfObject, "Global Proxy"); | |
1664 sink_->PutInt(kGlobalProxyReference, "kGlobalProxyReference"); | |
1665 } else { | |
1666 if (FLAG_trace_serializer) { | |
1667 PrintF(" Encoding back reference to: "); | |
1668 obj->ShortPrint(); | |
1669 PrintF("\n"); | |
1670 } | |
1671 | |
1672 PutAlignmentPrefix(obj); | |
1673 AllocationSpace space = back_reference.space(); | |
1674 if (skip == 0) { | |
1675 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRef"); | |
1676 } else { | |
1677 sink_->Put(kBackrefWithSkip + how_to_code + where_to_point + space, | |
1678 "BackRefWithSkip"); | |
1679 sink_->PutInt(skip, "BackRefSkipDistance"); | |
1680 } | |
1681 PutBackReference(obj, back_reference); | |
1682 } | |
1683 return true; | |
1684 } | |
1685 return false; | |
1686 } | |
1687 | |
1688 StartupSerializer::StartupSerializer(Isolate* isolate, SnapshotByteSink* sink) | |
1689 : Serializer(isolate, sink), | |
1690 root_index_wave_front_(0), | |
1691 serializing_builtins_(false) { | |
1692 // Clear the cache of objects used by the partial snapshot. After the | |
1693 // strong roots have been serialized we can create a partial snapshot | |
1694 // which will repopulate the cache with objects needed by that partial | |
1695 // snapshot. | |
1696 isolate->partial_snapshot_cache()->Clear(); | |
1697 InitializeCodeAddressMap(); | |
1698 } | |
1699 | |
1700 | |
1701 void StartupSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code, | |
1702 WhereToPoint where_to_point, int skip) { | |
1703 DCHECK(!obj->IsJSFunction()); | |
1704 | |
1705 if (obj->IsCode()) { | |
1706 Code* code = Code::cast(obj); | |
1707 // If the function code is compiled (either as native code or bytecode), | |
1708 // replace it with lazy-compile builtin. Only exception is when we are | |
1709 // serializing the canonical interpreter-entry-trampoline builtin. | |
1710 if (code->kind() == Code::FUNCTION || | |
1711 (!serializing_builtins_ && code->is_interpreter_entry_trampoline())) { | |
1712 obj = isolate()->builtins()->builtin(Builtins::kCompileLazy); | |
1713 } | |
1714 } else if (obj->IsBytecodeArray()) { | |
1715 obj = isolate()->heap()->undefined_value(); | |
1716 } | |
1717 | |
1718 int root_index = root_index_map_.Lookup(obj); | |
1719 bool is_immortal_immovable_root = false; | |
1720 // We can only encode roots as such if it has already been serialized. | |
1721 // That applies to root indices below the wave front. | |
1722 if (root_index != RootIndexMap::kInvalidRootIndex) { | |
1723 if (root_index < root_index_wave_front_) { | |
1724 PutRoot(root_index, obj, how_to_code, where_to_point, skip); | |
1725 return; | |
1726 } else { | |
1727 is_immortal_immovable_root = Heap::RootIsImmortalImmovable(root_index); | |
1728 } | |
1729 } | |
1730 | |
1731 if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return; | |
1732 | |
1733 FlushSkip(skip); | |
1734 | |
1735 // Object has not yet been serialized. Serialize it here. | |
1736 ObjectSerializer object_serializer(this, obj, sink_, how_to_code, | |
1737 where_to_point); | |
1738 object_serializer.Serialize(); | |
1739 | |
1740 if (is_immortal_immovable_root) { | |
1741 // Make sure that the immortal immovable root has been included in the first | |
1742 // chunk of its reserved space , so that it is deserialized onto the first | |
1743 // page of its space and stays immortal immovable. | |
1744 BackReference ref = back_reference_map_.Lookup(obj); | |
1745 CHECK(ref.is_valid() && ref.chunk_index() == 0); | |
1746 } | |
1747 } | |
1748 | |
1749 | |
1750 void StartupSerializer::SerializeWeakReferencesAndDeferred() { | |
1751 // This phase comes right after the serialization (of the snapshot). | |
1752 // After we have done the partial serialization the partial snapshot cache | |
1753 // will contain some references needed to decode the partial snapshot. We | |
1754 // add one entry with 'undefined' which is the sentinel that the deserializer | |
1755 // uses to know it is done deserializing the array. | |
1756 Object* undefined = isolate()->heap()->undefined_value(); | |
1757 VisitPointer(&undefined); | |
1758 isolate()->heap()->IterateWeakRoots(this, VISIT_ALL); | |
1759 SerializeDeferredObjects(); | |
1760 Pad(); | |
1761 } | |
1762 | |
1763 void StartupSerializer::Synchronize(VisitorSynchronization::SyncTag tag) { | |
1764 // We expect the builtins tag after builtins have been serialized. | |
1765 DCHECK(!serializing_builtins_ || tag == VisitorSynchronization::kBuiltins); | |
1766 serializing_builtins_ = (tag == VisitorSynchronization::kHandleScope); | |
1767 sink_->Put(kSynchronize, "Synchronize"); | |
1768 } | |
1769 | |
1770 void Serializer::PutRoot(int root_index, | |
1771 HeapObject* object, | |
1772 SerializerDeserializer::HowToCode how_to_code, | |
1773 SerializerDeserializer::WhereToPoint where_to_point, | |
1774 int skip) { | |
1775 if (FLAG_trace_serializer) { | |
1776 PrintF(" Encoding root %d:", root_index); | |
1777 object->ShortPrint(); | |
1778 PrintF("\n"); | |
1779 } | |
1780 | |
1781 if (how_to_code == kPlain && where_to_point == kStartOfObject && | |
1782 root_index < kNumberOfRootArrayConstants && | |
1783 !isolate()->heap()->InNewSpace(object)) { | |
1784 if (skip == 0) { | |
1785 sink_->Put(kRootArrayConstants + root_index, "RootConstant"); | |
1786 } else { | |
1787 sink_->Put(kRootArrayConstantsWithSkip + root_index, "RootConstant"); | |
1788 sink_->PutInt(skip, "SkipInPutRoot"); | |
1789 } | |
1790 } else { | |
1791 FlushSkip(skip); | |
1792 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization"); | |
1793 sink_->PutInt(root_index, "root_index"); | |
1794 } | |
1795 } | |
1796 | |
1797 | |
1798 void Serializer::PutBackReference(HeapObject* object, BackReference reference) { | |
1799 DCHECK(BackReferenceIsAlreadyAllocated(reference)); | |
1800 sink_->PutInt(reference.reference(), "BackRefValue"); | |
1801 hot_objects_.Add(object); | |
1802 } | |
1803 | |
1804 | |
1805 int Serializer::PutAlignmentPrefix(HeapObject* object) { | |
1806 AllocationAlignment alignment = object->RequiredAlignment(); | |
1807 if (alignment != kWordAligned) { | |
1808 DCHECK(1 <= alignment && alignment <= 3); | |
1809 byte prefix = (kAlignmentPrefix - 1) + alignment; | |
1810 sink_->Put(prefix, "Alignment"); | |
1811 return Heap::GetMaximumFillToAlign(alignment); | |
1812 } | |
1813 return 0; | |
1814 } | |
1815 | |
1816 | |
1817 void PartialSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code, | |
1818 WhereToPoint where_to_point, int skip) { | |
1819 if (obj->IsMap()) { | |
1820 // The code-caches link to context-specific code objects, which | |
1821 // the startup and context serializes cannot currently handle. | |
1822 DCHECK(Map::cast(obj)->code_cache() == obj->GetHeap()->empty_fixed_array()); | |
1823 } | |
1824 | |
1825 // Replace typed arrays by undefined. | |
1826 if (obj->IsJSTypedArray()) obj = isolate_->heap()->undefined_value(); | |
1827 | |
1828 int root_index = root_index_map_.Lookup(obj); | |
1829 if (root_index != RootIndexMap::kInvalidRootIndex) { | |
1830 PutRoot(root_index, obj, how_to_code, where_to_point, skip); | |
1831 return; | |
1832 } | |
1833 | |
1834 if (ShouldBeInThePartialSnapshotCache(obj)) { | |
1835 FlushSkip(skip); | |
1836 | |
1837 int cache_index = PartialSnapshotCacheIndex(obj); | |
1838 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point, | |
1839 "PartialSnapshotCache"); | |
1840 sink_->PutInt(cache_index, "partial_snapshot_cache_index"); | |
1841 return; | |
1842 } | |
1843 | |
1844 // Pointers from the partial snapshot to the objects in the startup snapshot | |
1845 // should go through the root array or through the partial snapshot cache. | |
1846 // If this is not the case you may have to add something to the root array. | |
1847 DCHECK(!startup_serializer_->back_reference_map()->Lookup(obj).is_valid()); | |
1848 // All the internalized strings that the partial snapshot needs should be | |
1849 // either in the root table or in the partial snapshot cache. | |
1850 DCHECK(!obj->IsInternalizedString()); | |
1851 | |
1852 if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return; | |
1853 | |
1854 FlushSkip(skip); | |
1855 | |
1856 // Clear literal boilerplates. | |
1857 if (obj->IsJSFunction()) { | |
1858 FixedArray* literals = JSFunction::cast(obj)->literals(); | |
1859 for (int i = 0; i < literals->length(); i++) literals->set_undefined(i); | |
1860 } | |
1861 | |
1862 // Object has not yet been serialized. Serialize it here. | |
1863 ObjectSerializer serializer(this, obj, sink_, how_to_code, where_to_point); | |
1864 serializer.Serialize(); | |
1865 } | |
1866 | |
1867 | |
1868 void Serializer::ObjectSerializer::SerializePrologue(AllocationSpace space, | |
1869 int size, Map* map) { | |
1870 if (serializer_->code_address_map_) { | |
1871 const char* code_name = | |
1872 serializer_->code_address_map_->Lookup(object_->address()); | |
1873 LOG(serializer_->isolate_, | |
1874 CodeNameEvent(object_->address(), sink_->Position(), code_name)); | |
1875 LOG(serializer_->isolate_, | |
1876 SnapshotPositionEvent(object_->address(), sink_->Position())); | |
1877 } | |
1878 | |
1879 BackReference back_reference; | |
1880 if (space == LO_SPACE) { | |
1881 sink_->Put(kNewObject + reference_representation_ + space, | |
1882 "NewLargeObject"); | |
1883 sink_->PutInt(size >> kObjectAlignmentBits, "ObjectSizeInWords"); | |
1884 if (object_->IsCode()) { | |
1885 sink_->Put(EXECUTABLE, "executable large object"); | |
1886 } else { | |
1887 sink_->Put(NOT_EXECUTABLE, "not executable large object"); | |
1888 } | |
1889 back_reference = serializer_->AllocateLargeObject(size); | |
1890 } else { | |
1891 int fill = serializer_->PutAlignmentPrefix(object_); | |
1892 back_reference = serializer_->Allocate(space, size + fill); | |
1893 sink_->Put(kNewObject + reference_representation_ + space, "NewObject"); | |
1894 sink_->PutInt(size >> kObjectAlignmentBits, "ObjectSizeInWords"); | |
1895 } | |
1896 | |
1897 #ifdef OBJECT_PRINT | |
1898 if (FLAG_serialization_statistics) { | |
1899 serializer_->CountInstanceType(map, size); | |
1900 } | |
1901 #endif // OBJECT_PRINT | |
1902 | |
1903 // Mark this object as already serialized. | |
1904 serializer_->back_reference_map()->Add(object_, back_reference); | |
1905 | |
1906 // Serialize the map (first word of the object). | |
1907 serializer_->SerializeObject(map, kPlain, kStartOfObject, 0); | |
1908 } | |
1909 | |
1910 | |
1911 void Serializer::ObjectSerializer::SerializeExternalString() { | |
1912 // Instead of serializing this as an external string, we serialize | |
1913 // an imaginary sequential string with the same content. | |
1914 Isolate* isolate = serializer_->isolate(); | |
1915 DCHECK(object_->IsExternalString()); | |
1916 DCHECK(object_->map() != isolate->heap()->native_source_string_map()); | |
1917 ExternalString* string = ExternalString::cast(object_); | |
1918 int length = string->length(); | |
1919 Map* map; | |
1920 int content_size; | |
1921 int allocation_size; | |
1922 const byte* resource; | |
1923 // Find the map and size for the imaginary sequential string. | |
1924 bool internalized = object_->IsInternalizedString(); | |
1925 if (object_->IsExternalOneByteString()) { | |
1926 map = internalized ? isolate->heap()->one_byte_internalized_string_map() | |
1927 : isolate->heap()->one_byte_string_map(); | |
1928 allocation_size = SeqOneByteString::SizeFor(length); | |
1929 content_size = length * kCharSize; | |
1930 resource = reinterpret_cast<const byte*>( | |
1931 ExternalOneByteString::cast(string)->resource()->data()); | |
1932 } else { | |
1933 map = internalized ? isolate->heap()->internalized_string_map() | |
1934 : isolate->heap()->string_map(); | |
1935 allocation_size = SeqTwoByteString::SizeFor(length); | |
1936 content_size = length * kShortSize; | |
1937 resource = reinterpret_cast<const byte*>( | |
1938 ExternalTwoByteString::cast(string)->resource()->data()); | |
1939 } | |
1940 | |
1941 AllocationSpace space = (allocation_size > Page::kMaxRegularHeapObjectSize) | |
1942 ? LO_SPACE | |
1943 : OLD_SPACE; | |
1944 SerializePrologue(space, allocation_size, map); | |
1945 | |
1946 // Output the rest of the imaginary string. | |
1947 int bytes_to_output = allocation_size - HeapObject::kHeaderSize; | |
1948 | |
1949 // Output raw data header. Do not bother with common raw length cases here. | |
1950 sink_->Put(kVariableRawData, "RawDataForString"); | |
1951 sink_->PutInt(bytes_to_output, "length"); | |
1952 | |
1953 // Serialize string header (except for map). | |
1954 Address string_start = string->address(); | |
1955 for (int i = HeapObject::kHeaderSize; i < SeqString::kHeaderSize; i++) { | |
1956 sink_->PutSection(string_start[i], "StringHeader"); | |
1957 } | |
1958 | |
1959 // Serialize string content. | |
1960 sink_->PutRaw(resource, content_size, "StringContent"); | |
1961 | |
1962 // Since the allocation size is rounded up to object alignment, there | |
1963 // maybe left-over bytes that need to be padded. | |
1964 int padding_size = allocation_size - SeqString::kHeaderSize - content_size; | |
1965 DCHECK(0 <= padding_size && padding_size < kObjectAlignment); | |
1966 for (int i = 0; i < padding_size; i++) sink_->PutSection(0, "StringPadding"); | |
1967 | |
1968 sink_->Put(kSkip, "SkipAfterString"); | |
1969 sink_->PutInt(bytes_to_output, "SkipDistance"); | |
1970 } | |
1971 | |
1972 // Clear and later restore the next link in the weak cell or allocation site. | |
1973 // TODO(all): replace this with proper iteration of weak slots in serializer. | |
1974 class UnlinkWeakNextScope { | |
1975 public: | |
1976 explicit UnlinkWeakNextScope(HeapObject* object) : object_(nullptr) { | |
1977 if (object->IsWeakCell()) { | |
1978 object_ = object; | |
1979 next_ = WeakCell::cast(object)->next(); | |
1980 WeakCell::cast(object)->clear_next(object->GetHeap()->the_hole_value()); | |
1981 } else if (object->IsAllocationSite()) { | |
1982 object_ = object; | |
1983 next_ = AllocationSite::cast(object)->weak_next(); | |
1984 AllocationSite::cast(object) | |
1985 ->set_weak_next(object->GetHeap()->undefined_value()); | |
1986 } | |
1987 } | |
1988 | |
1989 ~UnlinkWeakNextScope() { | |
1990 if (object_ != nullptr) { | |
1991 if (object_->IsWeakCell()) { | |
1992 WeakCell::cast(object_)->set_next(next_, UPDATE_WEAK_WRITE_BARRIER); | |
1993 } else { | |
1994 AllocationSite::cast(object_) | |
1995 ->set_weak_next(next_, UPDATE_WEAK_WRITE_BARRIER); | |
1996 } | |
1997 } | |
1998 } | |
1999 | |
2000 private: | |
2001 HeapObject* object_; | |
2002 Object* next_; | |
2003 DisallowHeapAllocation no_gc_; | |
2004 }; | |
2005 | |
2006 | |
2007 void Serializer::ObjectSerializer::Serialize() { | |
2008 if (FLAG_trace_serializer) { | |
2009 PrintF(" Encoding heap object: "); | |
2010 object_->ShortPrint(); | |
2011 PrintF("\n"); | |
2012 } | |
2013 | |
2014 // We cannot serialize typed array objects correctly. | |
2015 DCHECK(!object_->IsJSTypedArray()); | |
2016 | |
2017 // We don't expect fillers. | |
2018 DCHECK(!object_->IsFiller()); | |
2019 | |
2020 if (object_->IsScript()) { | |
2021 // Clear cached line ends. | |
2022 Object* undefined = serializer_->isolate()->heap()->undefined_value(); | |
2023 Script::cast(object_)->set_line_ends(undefined); | |
2024 } | |
2025 | |
2026 if (object_->IsExternalString()) { | |
2027 Heap* heap = serializer_->isolate()->heap(); | |
2028 if (object_->map() != heap->native_source_string_map()) { | |
2029 // Usually we cannot recreate resources for external strings. To work | |
2030 // around this, external strings are serialized to look like ordinary | |
2031 // sequential strings. | |
2032 // The exception are native source code strings, since we can recreate | |
2033 // their resources. In that case we fall through and leave it to | |
2034 // VisitExternalOneByteString further down. | |
2035 SerializeExternalString(); | |
2036 return; | |
2037 } | |
2038 } | |
2039 | |
2040 int size = object_->Size(); | |
2041 Map* map = object_->map(); | |
2042 AllocationSpace space = | |
2043 MemoryChunk::FromAddress(object_->address())->owner()->identity(); | |
2044 SerializePrologue(space, size, map); | |
2045 | |
2046 // Serialize the rest of the object. | |
2047 CHECK_EQ(0, bytes_processed_so_far_); | |
2048 bytes_processed_so_far_ = kPointerSize; | |
2049 | |
2050 RecursionScope recursion(serializer_); | |
2051 // Objects that are immediately post processed during deserialization | |
2052 // cannot be deferred, since post processing requires the object content. | |
2053 if (recursion.ExceedsMaximum() && CanBeDeferred(object_)) { | |
2054 serializer_->QueueDeferredObject(object_); | |
2055 sink_->Put(kDeferred, "Deferring object content"); | |
2056 return; | |
2057 } | |
2058 | |
2059 UnlinkWeakNextScope unlink_weak_next(object_); | |
2060 | |
2061 object_->IterateBody(map->instance_type(), size, this); | |
2062 OutputRawData(object_->address() + size); | |
2063 } | |
2064 | |
2065 | |
2066 void Serializer::ObjectSerializer::SerializeDeferred() { | |
2067 if (FLAG_trace_serializer) { | |
2068 PrintF(" Encoding deferred heap object: "); | |
2069 object_->ShortPrint(); | |
2070 PrintF("\n"); | |
2071 } | |
2072 | |
2073 int size = object_->Size(); | |
2074 Map* map = object_->map(); | |
2075 BackReference reference = serializer_->back_reference_map()->Lookup(object_); | |
2076 | |
2077 // Serialize the rest of the object. | |
2078 CHECK_EQ(0, bytes_processed_so_far_); | |
2079 bytes_processed_so_far_ = kPointerSize; | |
2080 | |
2081 serializer_->PutAlignmentPrefix(object_); | |
2082 sink_->Put(kNewObject + reference.space(), "deferred object"); | |
2083 serializer_->PutBackReference(object_, reference); | |
2084 sink_->PutInt(size >> kPointerSizeLog2, "deferred object size"); | |
2085 | |
2086 UnlinkWeakNextScope unlink_weak_next(object_); | |
2087 | |
2088 object_->IterateBody(map->instance_type(), size, this); | |
2089 OutputRawData(object_->address() + size); | |
2090 } | |
2091 | |
2092 | |
2093 void Serializer::ObjectSerializer::VisitPointers(Object** start, | |
2094 Object** end) { | |
2095 Object** current = start; | |
2096 while (current < end) { | |
2097 while (current < end && (*current)->IsSmi()) current++; | |
2098 if (current < end) OutputRawData(reinterpret_cast<Address>(current)); | |
2099 | |
2100 while (current < end && !(*current)->IsSmi()) { | |
2101 HeapObject* current_contents = HeapObject::cast(*current); | |
2102 int root_index = serializer_->root_index_map()->Lookup(current_contents); | |
2103 // Repeats are not subject to the write barrier so we can only use | |
2104 // immortal immovable root members. They are never in new space. | |
2105 if (current != start && root_index != RootIndexMap::kInvalidRootIndex && | |
2106 Heap::RootIsImmortalImmovable(root_index) && | |
2107 current_contents == current[-1]) { | |
2108 DCHECK(!serializer_->isolate()->heap()->InNewSpace(current_contents)); | |
2109 int repeat_count = 1; | |
2110 while (¤t[repeat_count] < end - 1 && | |
2111 current[repeat_count] == current_contents) { | |
2112 repeat_count++; | |
2113 } | |
2114 current += repeat_count; | |
2115 bytes_processed_so_far_ += repeat_count * kPointerSize; | |
2116 if (repeat_count > kNumberOfFixedRepeat) { | |
2117 sink_->Put(kVariableRepeat, "VariableRepeat"); | |
2118 sink_->PutInt(repeat_count, "repeat count"); | |
2119 } else { | |
2120 sink_->Put(kFixedRepeatStart + repeat_count, "FixedRepeat"); | |
2121 } | |
2122 } else { | |
2123 serializer_->SerializeObject( | |
2124 current_contents, kPlain, kStartOfObject, 0); | |
2125 bytes_processed_so_far_ += kPointerSize; | |
2126 current++; | |
2127 } | |
2128 } | |
2129 } | |
2130 } | |
2131 | |
2132 | |
2133 void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) { | |
2134 int skip = OutputRawData(rinfo->target_address_address(), | |
2135 kCanReturnSkipInsteadOfSkipping); | |
2136 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain; | |
2137 Object* object = rinfo->target_object(); | |
2138 serializer_->SerializeObject(HeapObject::cast(object), how_to_code, | |
2139 kStartOfObject, skip); | |
2140 bytes_processed_so_far_ += rinfo->target_address_size(); | |
2141 } | |
2142 | |
2143 | |
2144 void Serializer::ObjectSerializer::VisitExternalReference(Address* p) { | |
2145 int skip = OutputRawData(reinterpret_cast<Address>(p), | |
2146 kCanReturnSkipInsteadOfSkipping); | |
2147 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef"); | |
2148 sink_->PutInt(skip, "SkipB4ExternalRef"); | |
2149 Address target = *p; | |
2150 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id"); | |
2151 bytes_processed_so_far_ += kPointerSize; | |
2152 } | |
2153 | |
2154 | |
2155 void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) { | |
2156 int skip = OutputRawData(rinfo->target_address_address(), | |
2157 kCanReturnSkipInsteadOfSkipping); | |
2158 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain; | |
2159 sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef"); | |
2160 sink_->PutInt(skip, "SkipB4ExternalRef"); | |
2161 Address target = rinfo->target_external_reference(); | |
2162 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id"); | |
2163 bytes_processed_so_far_ += rinfo->target_address_size(); | |
2164 } | |
2165 | |
2166 | |
2167 void Serializer::ObjectSerializer::VisitInternalReference(RelocInfo* rinfo) { | |
2168 // We can only reference to internal references of code that has been output. | |
2169 DCHECK(is_code_object_ && code_has_been_output_); | |
2170 // We do not use skip from last patched pc to find the pc to patch, since | |
2171 // target_address_address may not return addresses in ascending order when | |
2172 // used for internal references. External references may be stored at the | |
2173 // end of the code in the constant pool, whereas internal references are | |
2174 // inline. That would cause the skip to be negative. Instead, we store the | |
2175 // offset from code entry. | |
2176 Address entry = Code::cast(object_)->entry(); | |
2177 intptr_t pc_offset = rinfo->target_internal_reference_address() - entry; | |
2178 intptr_t target_offset = rinfo->target_internal_reference() - entry; | |
2179 DCHECK(0 <= pc_offset && | |
2180 pc_offset <= Code::cast(object_)->instruction_size()); | |
2181 DCHECK(0 <= target_offset && | |
2182 target_offset <= Code::cast(object_)->instruction_size()); | |
2183 sink_->Put(rinfo->rmode() == RelocInfo::INTERNAL_REFERENCE | |
2184 ? kInternalReference | |
2185 : kInternalReferenceEncoded, | |
2186 "InternalRef"); | |
2187 sink_->PutInt(static_cast<uintptr_t>(pc_offset), "internal ref address"); | |
2188 sink_->PutInt(static_cast<uintptr_t>(target_offset), "internal ref value"); | |
2189 } | |
2190 | |
2191 | |
2192 void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) { | |
2193 int skip = OutputRawData(rinfo->target_address_address(), | |
2194 kCanReturnSkipInsteadOfSkipping); | |
2195 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain; | |
2196 sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef"); | |
2197 sink_->PutInt(skip, "SkipB4ExternalRef"); | |
2198 Address target = rinfo->target_address(); | |
2199 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id"); | |
2200 bytes_processed_so_far_ += rinfo->target_address_size(); | |
2201 } | |
2202 | |
2203 | |
2204 void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) { | |
2205 int skip = OutputRawData(rinfo->target_address_address(), | |
2206 kCanReturnSkipInsteadOfSkipping); | |
2207 Code* object = Code::GetCodeFromTargetAddress(rinfo->target_address()); | |
2208 serializer_->SerializeObject(object, kFromCode, kInnerPointer, skip); | |
2209 bytes_processed_so_far_ += rinfo->target_address_size(); | |
2210 } | |
2211 | |
2212 | |
2213 void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) { | |
2214 int skip = OutputRawData(entry_address, kCanReturnSkipInsteadOfSkipping); | |
2215 Code* object = Code::cast(Code::GetObjectFromEntryAddress(entry_address)); | |
2216 serializer_->SerializeObject(object, kPlain, kInnerPointer, skip); | |
2217 bytes_processed_so_far_ += kPointerSize; | |
2218 } | |
2219 | |
2220 | |
2221 void Serializer::ObjectSerializer::VisitCell(RelocInfo* rinfo) { | |
2222 int skip = OutputRawData(rinfo->pc(), kCanReturnSkipInsteadOfSkipping); | |
2223 Cell* object = Cell::cast(rinfo->target_cell()); | |
2224 serializer_->SerializeObject(object, kPlain, kInnerPointer, skip); | |
2225 bytes_processed_so_far_ += kPointerSize; | |
2226 } | |
2227 | |
2228 | |
2229 bool Serializer::ObjectSerializer::SerializeExternalNativeSourceString( | |
2230 int builtin_count, | |
2231 v8::String::ExternalOneByteStringResource** resource_pointer, | |
2232 FixedArray* source_cache, int resource_index) { | |
2233 for (int i = 0; i < builtin_count; i++) { | |
2234 Object* source = source_cache->get(i); | |
2235 if (!source->IsUndefined()) { | |
2236 ExternalOneByteString* string = ExternalOneByteString::cast(source); | |
2237 typedef v8::String::ExternalOneByteStringResource Resource; | |
2238 const Resource* resource = string->resource(); | |
2239 if (resource == *resource_pointer) { | |
2240 sink_->Put(resource_index, "NativesStringResource"); | |
2241 sink_->PutSection(i, "NativesStringResourceEnd"); | |
2242 bytes_processed_so_far_ += sizeof(resource); | |
2243 return true; | |
2244 } | |
2245 } | |
2246 } | |
2247 return false; | |
2248 } | |
2249 | |
2250 | |
2251 void Serializer::ObjectSerializer::VisitExternalOneByteString( | |
2252 v8::String::ExternalOneByteStringResource** resource_pointer) { | |
2253 Address references_start = reinterpret_cast<Address>(resource_pointer); | |
2254 OutputRawData(references_start); | |
2255 if (SerializeExternalNativeSourceString( | |
2256 Natives::GetBuiltinsCount(), resource_pointer, | |
2257 Natives::GetSourceCache(serializer_->isolate()->heap()), | |
2258 kNativesStringResource)) { | |
2259 return; | |
2260 } | |
2261 if (SerializeExternalNativeSourceString( | |
2262 ExtraNatives::GetBuiltinsCount(), resource_pointer, | |
2263 ExtraNatives::GetSourceCache(serializer_->isolate()->heap()), | |
2264 kExtraNativesStringResource)) { | |
2265 return; | |
2266 } | |
2267 // One of the strings in the natives cache should match the resource. We | |
2268 // don't expect any other kinds of external strings here. | |
2269 UNREACHABLE(); | |
2270 } | |
2271 | |
2272 | |
2273 Address Serializer::ObjectSerializer::PrepareCode() { | |
2274 // To make snapshots reproducible, we make a copy of the code object | |
2275 // and wipe all pointers in the copy, which we then serialize. | |
2276 Code* original = Code::cast(object_); | |
2277 Code* code = serializer_->CopyCode(original); | |
2278 // Code age headers are not serializable. | |
2279 code->MakeYoung(serializer_->isolate()); | |
2280 int mode_mask = RelocInfo::kCodeTargetMask | | |
2281 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) | | |
2282 RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE) | | |
2283 RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY) | | |
2284 RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE) | | |
2285 RelocInfo::ModeMask(RelocInfo::INTERNAL_REFERENCE_ENCODED); | |
2286 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) { | |
2287 RelocInfo* rinfo = it.rinfo(); | |
2288 rinfo->WipeOut(); | |
2289 } | |
2290 // We need to wipe out the header fields *after* wiping out the | |
2291 // relocations, because some of these fields are needed for the latter. | |
2292 code->WipeOutHeader(); | |
2293 return code->address(); | |
2294 } | |
2295 | |
2296 | |
2297 int Serializer::ObjectSerializer::OutputRawData( | |
2298 Address up_to, Serializer::ObjectSerializer::ReturnSkip return_skip) { | |
2299 Address object_start = object_->address(); | |
2300 int base = bytes_processed_so_far_; | |
2301 int up_to_offset = static_cast<int>(up_to - object_start); | |
2302 int to_skip = up_to_offset - bytes_processed_so_far_; | |
2303 int bytes_to_output = to_skip; | |
2304 bytes_processed_so_far_ += to_skip; | |
2305 // This assert will fail if the reloc info gives us the target_address_address | |
2306 // locations in a non-ascending order. Luckily that doesn't happen. | |
2307 DCHECK(to_skip >= 0); | |
2308 bool outputting_code = false; | |
2309 if (to_skip != 0 && is_code_object_ && !code_has_been_output_) { | |
2310 // Output the code all at once and fix later. | |
2311 bytes_to_output = object_->Size() + to_skip - bytes_processed_so_far_; | |
2312 outputting_code = true; | |
2313 code_has_been_output_ = true; | |
2314 } | |
2315 if (bytes_to_output != 0 && (!is_code_object_ || outputting_code)) { | |
2316 if (!outputting_code && bytes_to_output == to_skip && | |
2317 IsAligned(bytes_to_output, kPointerAlignment) && | |
2318 bytes_to_output <= kNumberOfFixedRawData * kPointerSize) { | |
2319 int size_in_words = bytes_to_output >> kPointerSizeLog2; | |
2320 sink_->PutSection(kFixedRawDataStart + size_in_words, "FixedRawData"); | |
2321 to_skip = 0; // This instruction includes skip. | |
2322 } else { | |
2323 // We always end up here if we are outputting the code of a code object. | |
2324 sink_->Put(kVariableRawData, "VariableRawData"); | |
2325 sink_->PutInt(bytes_to_output, "length"); | |
2326 } | |
2327 | |
2328 if (is_code_object_) object_start = PrepareCode(); | |
2329 | |
2330 const char* description = is_code_object_ ? "Code" : "Byte"; | |
2331 sink_->PutRaw(object_start + base, bytes_to_output, description); | |
2332 } | |
2333 if (to_skip != 0 && return_skip == kIgnoringReturn) { | |
2334 sink_->Put(kSkip, "Skip"); | |
2335 sink_->PutInt(to_skip, "SkipDistance"); | |
2336 to_skip = 0; | |
2337 } | |
2338 return to_skip; | |
2339 } | |
2340 | |
2341 | |
2342 BackReference Serializer::AllocateLargeObject(int size) { | |
2343 // Large objects are allocated one-by-one when deserializing. We do not | |
2344 // have to keep track of multiple chunks. | |
2345 large_objects_total_size_ += size; | |
2346 return BackReference::LargeObjectReference(seen_large_objects_index_++); | |
2347 } | |
2348 | |
2349 | |
2350 BackReference Serializer::Allocate(AllocationSpace space, int size) { | |
2351 DCHECK(space >= 0 && space < kNumberOfPreallocatedSpaces); | |
2352 DCHECK(size > 0 && size <= static_cast<int>(max_chunk_size(space))); | |
2353 uint32_t new_chunk_size = pending_chunk_[space] + size; | |
2354 if (new_chunk_size > max_chunk_size(space)) { | |
2355 // The new chunk size would not fit onto a single page. Complete the | |
2356 // current chunk and start a new one. | |
2357 sink_->Put(kNextChunk, "NextChunk"); | |
2358 sink_->Put(space, "NextChunkSpace"); | |
2359 completed_chunks_[space].Add(pending_chunk_[space]); | |
2360 DCHECK_LE(completed_chunks_[space].length(), BackReference::kMaxChunkIndex); | |
2361 pending_chunk_[space] = 0; | |
2362 new_chunk_size = size; | |
2363 } | |
2364 uint32_t offset = pending_chunk_[space]; | |
2365 pending_chunk_[space] = new_chunk_size; | |
2366 return BackReference::Reference(space, completed_chunks_[space].length(), | |
2367 offset); | |
2368 } | |
2369 | |
2370 | |
2371 void Serializer::Pad() { | |
2372 // The non-branching GetInt will read up to 3 bytes too far, so we need | |
2373 // to pad the snapshot to make sure we don't read over the end. | |
2374 for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) { | |
2375 sink_->Put(kNop, "Padding"); | |
2376 } | |
2377 // Pad up to pointer size for checksum. | |
2378 while (!IsAligned(sink_->Position(), kPointerAlignment)) { | |
2379 sink_->Put(kNop, "Padding"); | |
2380 } | |
2381 } | |
2382 | |
2383 | |
2384 void Serializer::InitializeCodeAddressMap() { | |
2385 isolate_->InitializeLoggingAndCounters(); | |
2386 code_address_map_ = new CodeAddressMap(isolate_); | |
2387 } | |
2388 | |
2389 | |
2390 Code* Serializer::CopyCode(Code* code) { | |
2391 code_buffer_.Rewind(0); // Clear buffer without deleting backing store. | |
2392 int size = code->CodeSize(); | |
2393 code_buffer_.AddAll(Vector<byte>(code->address(), size)); | |
2394 return Code::cast(HeapObject::FromAddress(&code_buffer_.first())); | |
2395 } | |
2396 | |
2397 | |
2398 ScriptData* CodeSerializer::Serialize(Isolate* isolate, | |
2399 Handle<SharedFunctionInfo> info, | |
2400 Handle<String> source) { | |
2401 base::ElapsedTimer timer; | |
2402 if (FLAG_profile_deserialization) timer.Start(); | |
2403 if (FLAG_trace_serializer) { | |
2404 PrintF("[Serializing from"); | |
2405 Object* script = info->script(); | |
2406 if (script->IsScript()) Script::cast(script)->name()->ShortPrint(); | |
2407 PrintF("]\n"); | |
2408 } | |
2409 | |
2410 // Serialize code object. | |
2411 SnapshotByteSink sink(info->code()->CodeSize() * 2); | |
2412 CodeSerializer cs(isolate, &sink, *source); | |
2413 DisallowHeapAllocation no_gc; | |
2414 Object** location = Handle<Object>::cast(info).location(); | |
2415 cs.VisitPointer(location); | |
2416 cs.SerializeDeferredObjects(); | |
2417 cs.Pad(); | |
2418 | |
2419 SerializedCodeData data(sink.data(), cs); | |
2420 ScriptData* script_data = data.GetScriptData(); | |
2421 | |
2422 if (FLAG_profile_deserialization) { | |
2423 double ms = timer.Elapsed().InMillisecondsF(); | |
2424 int length = script_data->length(); | |
2425 PrintF("[Serializing to %d bytes took %0.3f ms]\n", length, ms); | |
2426 } | |
2427 | |
2428 return script_data; | |
2429 } | |
2430 | |
2431 | |
2432 void CodeSerializer::SerializeObject(HeapObject* obj, HowToCode how_to_code, | |
2433 WhereToPoint where_to_point, int skip) { | |
2434 int root_index = root_index_map_.Lookup(obj); | |
2435 if (root_index != RootIndexMap::kInvalidRootIndex) { | |
2436 PutRoot(root_index, obj, how_to_code, where_to_point, skip); | |
2437 return; | |
2438 } | |
2439 | |
2440 if (SerializeKnownObject(obj, how_to_code, where_to_point, skip)) return; | |
2441 | |
2442 FlushSkip(skip); | |
2443 | |
2444 if (obj->IsCode()) { | |
2445 Code* code_object = Code::cast(obj); | |
2446 switch (code_object->kind()) { | |
2447 case Code::OPTIMIZED_FUNCTION: // No optimized code compiled yet. | |
2448 case Code::HANDLER: // No handlers patched in yet. | |
2449 case Code::REGEXP: // No regexp literals initialized yet. | |
2450 case Code::NUMBER_OF_KINDS: // Pseudo enum value. | |
2451 CHECK(false); | |
2452 case Code::BUILTIN: | |
2453 SerializeBuiltin(code_object->builtin_index(), how_to_code, | |
2454 where_to_point); | |
2455 return; | |
2456 case Code::STUB: | |
2457 SerializeCodeStub(code_object->stub_key(), how_to_code, where_to_point); | |
2458 return; | |
2459 #define IC_KIND_CASE(KIND) case Code::KIND: | |
2460 IC_KIND_LIST(IC_KIND_CASE) | |
2461 #undef IC_KIND_CASE | |
2462 SerializeIC(code_object, how_to_code, where_to_point); | |
2463 return; | |
2464 case Code::FUNCTION: | |
2465 DCHECK(code_object->has_reloc_info_for_serialization()); | |
2466 SerializeGeneric(code_object, how_to_code, where_to_point); | |
2467 return; | |
2468 case Code::WASM_FUNCTION: | |
2469 UNREACHABLE(); | |
2470 } | |
2471 UNREACHABLE(); | |
2472 } | |
2473 | |
2474 // Past this point we should not see any (context-specific) maps anymore. | |
2475 CHECK(!obj->IsMap()); | |
2476 // There should be no references to the global object embedded. | |
2477 CHECK(!obj->IsJSGlobalProxy() && !obj->IsJSGlobalObject()); | |
2478 // There should be no hash table embedded. They would require rehashing. | |
2479 CHECK(!obj->IsHashTable()); | |
2480 // We expect no instantiated function objects or contexts. | |
2481 CHECK(!obj->IsJSFunction() && !obj->IsContext()); | |
2482 | |
2483 SerializeGeneric(obj, how_to_code, where_to_point); | |
2484 } | |
2485 | |
2486 | |
2487 void CodeSerializer::SerializeGeneric(HeapObject* heap_object, | |
2488 HowToCode how_to_code, | |
2489 WhereToPoint where_to_point) { | |
2490 // Object has not yet been serialized. Serialize it here. | |
2491 ObjectSerializer serializer(this, heap_object, sink_, how_to_code, | |
2492 where_to_point); | |
2493 serializer.Serialize(); | |
2494 } | |
2495 | |
2496 | |
2497 void CodeSerializer::SerializeBuiltin(int builtin_index, HowToCode how_to_code, | |
2498 WhereToPoint where_to_point) { | |
2499 DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) || | |
2500 (how_to_code == kPlain && where_to_point == kInnerPointer) || | |
2501 (how_to_code == kFromCode && where_to_point == kInnerPointer)); | |
2502 DCHECK_LT(builtin_index, Builtins::builtin_count); | |
2503 DCHECK_LE(0, builtin_index); | |
2504 | |
2505 if (FLAG_trace_serializer) { | |
2506 PrintF(" Encoding builtin: %s\n", | |
2507 isolate()->builtins()->name(builtin_index)); | |
2508 } | |
2509 | |
2510 sink_->Put(kBuiltin + how_to_code + where_to_point, "Builtin"); | |
2511 sink_->PutInt(builtin_index, "builtin_index"); | |
2512 } | |
2513 | |
2514 | |
2515 void CodeSerializer::SerializeCodeStub(uint32_t stub_key, HowToCode how_to_code, | |
2516 WhereToPoint where_to_point) { | |
2517 DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) || | |
2518 (how_to_code == kPlain && where_to_point == kInnerPointer) || | |
2519 (how_to_code == kFromCode && where_to_point == kInnerPointer)); | |
2520 DCHECK(CodeStub::MajorKeyFromKey(stub_key) != CodeStub::NoCache); | |
2521 DCHECK(!CodeStub::GetCode(isolate(), stub_key).is_null()); | |
2522 | |
2523 int index = AddCodeStubKey(stub_key) + kCodeStubsBaseIndex; | |
2524 | |
2525 if (FLAG_trace_serializer) { | |
2526 PrintF(" Encoding code stub %s as %d\n", | |
2527 CodeStub::MajorName(CodeStub::MajorKeyFromKey(stub_key)), index); | |
2528 } | |
2529 | |
2530 sink_->Put(kAttachedReference + how_to_code + where_to_point, "CodeStub"); | |
2531 sink_->PutInt(index, "CodeStub key"); | |
2532 } | |
2533 | |
2534 | |
2535 void CodeSerializer::SerializeIC(Code* ic, HowToCode how_to_code, | |
2536 WhereToPoint where_to_point) { | |
2537 // The IC may be implemented as a stub. | |
2538 uint32_t stub_key = ic->stub_key(); | |
2539 if (stub_key != CodeStub::NoCacheKey()) { | |
2540 if (FLAG_trace_serializer) { | |
2541 PrintF(" %s is a code stub\n", Code::Kind2String(ic->kind())); | |
2542 } | |
2543 SerializeCodeStub(stub_key, how_to_code, where_to_point); | |
2544 return; | |
2545 } | |
2546 // The IC may be implemented as builtin. Only real builtins have an | |
2547 // actual builtin_index value attached (otherwise it's just garbage). | |
2548 // Compare to make sure we are really dealing with a builtin. | |
2549 int builtin_index = ic->builtin_index(); | |
2550 if (builtin_index < Builtins::builtin_count) { | |
2551 Builtins::Name name = static_cast<Builtins::Name>(builtin_index); | |
2552 Code* builtin = isolate()->builtins()->builtin(name); | |
2553 if (builtin == ic) { | |
2554 if (FLAG_trace_serializer) { | |
2555 PrintF(" %s is a builtin\n", Code::Kind2String(ic->kind())); | |
2556 } | |
2557 DCHECK(ic->kind() == Code::KEYED_LOAD_IC || | |
2558 ic->kind() == Code::KEYED_STORE_IC); | |
2559 SerializeBuiltin(builtin_index, how_to_code, where_to_point); | |
2560 return; | |
2561 } | |
2562 } | |
2563 // The IC may also just be a piece of code kept in the non_monomorphic_cache. | |
2564 // In that case, just serialize as a normal code object. | |
2565 if (FLAG_trace_serializer) { | |
2566 PrintF(" %s has no special handling\n", Code::Kind2String(ic->kind())); | |
2567 } | |
2568 DCHECK(ic->kind() == Code::LOAD_IC || ic->kind() == Code::STORE_IC); | |
2569 SerializeGeneric(ic, how_to_code, where_to_point); | |
2570 } | |
2571 | |
2572 | |
2573 int CodeSerializer::AddCodeStubKey(uint32_t stub_key) { | |
2574 // TODO(yangguo) Maybe we need a hash table for a faster lookup than O(n^2). | |
2575 int index = 0; | |
2576 while (index < stub_keys_.length()) { | |
2577 if (stub_keys_[index] == stub_key) return index; | |
2578 index++; | |
2579 } | |
2580 stub_keys_.Add(stub_key); | |
2581 return index; | |
2582 } | |
2583 | |
2584 | |
2585 MaybeHandle<SharedFunctionInfo> CodeSerializer::Deserialize( | |
2586 Isolate* isolate, ScriptData* cached_data, Handle<String> source) { | |
2587 base::ElapsedTimer timer; | |
2588 if (FLAG_profile_deserialization) timer.Start(); | |
2589 | |
2590 HandleScope scope(isolate); | |
2591 | |
2592 base::SmartPointer<SerializedCodeData> scd( | |
2593 SerializedCodeData::FromCachedData(isolate, cached_data, *source)); | |
2594 if (scd.is_empty()) { | |
2595 if (FLAG_profile_deserialization) PrintF("[Cached code failed check]\n"); | |
2596 DCHECK(cached_data->rejected()); | |
2597 return MaybeHandle<SharedFunctionInfo>(); | |
2598 } | |
2599 | |
2600 // Prepare and register list of attached objects. | |
2601 Vector<const uint32_t> code_stub_keys = scd->CodeStubKeys(); | |
2602 Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::New( | |
2603 code_stub_keys.length() + kCodeStubsBaseIndex); | |
2604 attached_objects[kSourceObjectIndex] = source; | |
2605 for (int i = 0; i < code_stub_keys.length(); i++) { | |
2606 attached_objects[i + kCodeStubsBaseIndex] = | |
2607 CodeStub::GetCode(isolate, code_stub_keys[i]).ToHandleChecked(); | |
2608 } | |
2609 | |
2610 Deserializer deserializer(scd.get()); | |
2611 deserializer.SetAttachedObjects(attached_objects); | |
2612 | |
2613 // Deserialize. | |
2614 Handle<SharedFunctionInfo> result; | |
2615 if (!deserializer.DeserializeCode(isolate).ToHandle(&result)) { | |
2616 // Deserializing may fail if the reservations cannot be fulfilled. | |
2617 if (FLAG_profile_deserialization) PrintF("[Deserializing failed]\n"); | |
2618 return MaybeHandle<SharedFunctionInfo>(); | |
2619 } | |
2620 | |
2621 if (FLAG_profile_deserialization) { | |
2622 double ms = timer.Elapsed().InMillisecondsF(); | |
2623 int length = cached_data->length(); | |
2624 PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms); | |
2625 } | |
2626 result->set_deserialized(true); | |
2627 | |
2628 if (isolate->logger()->is_logging_code_events() || | |
2629 isolate->cpu_profiler()->is_profiling()) { | |
2630 String* name = isolate->heap()->empty_string(); | |
2631 if (result->script()->IsScript()) { | |
2632 Script* script = Script::cast(result->script()); | |
2633 if (script->name()->IsString()) name = String::cast(script->name()); | |
2634 } | |
2635 isolate->logger()->CodeCreateEvent(Logger::SCRIPT_TAG, | |
2636 AbstractCode::cast(result->code()), | |
2637 *result, NULL, name); | |
2638 } | |
2639 return scope.CloseAndEscape(result); | |
2640 } | |
2641 | |
2642 | |
2643 void SerializedData::AllocateData(int size) { | |
2644 DCHECK(!owns_data_); | |
2645 data_ = NewArray<byte>(size); | |
2646 size_ = size; | |
2647 owns_data_ = true; | |
2648 DCHECK(IsAligned(reinterpret_cast<intptr_t>(data_), kPointerAlignment)); | |
2649 } | |
2650 | |
2651 | |
2652 SnapshotData::SnapshotData(const Serializer& ser) { | |
2653 DisallowHeapAllocation no_gc; | |
2654 List<Reservation> reservations; | |
2655 ser.EncodeReservations(&reservations); | |
2656 const List<byte>& payload = ser.sink()->data(); | |
2657 | |
2658 // Calculate sizes. | |
2659 int reservation_size = reservations.length() * kInt32Size; | |
2660 int size = kHeaderSize + reservation_size + payload.length(); | |
2661 | |
2662 // Allocate backing store and create result data. | |
2663 AllocateData(size); | |
2664 | |
2665 // Set header values. | |
2666 SetMagicNumber(ser.isolate()); | |
2667 SetHeaderValue(kCheckSumOffset, Version::Hash()); | |
2668 SetHeaderValue(kNumReservationsOffset, reservations.length()); | |
2669 SetHeaderValue(kPayloadLengthOffset, payload.length()); | |
2670 | |
2671 // Copy reservation chunk sizes. | |
2672 CopyBytes(data_ + kHeaderSize, reinterpret_cast<byte*>(reservations.begin()), | |
2673 reservation_size); | |
2674 | |
2675 // Copy serialized data. | |
2676 CopyBytes(data_ + kHeaderSize + reservation_size, payload.begin(), | |
2677 static_cast<size_t>(payload.length())); | |
2678 } | |
2679 | |
2680 | |
2681 bool SnapshotData::IsSane() { | |
2682 return GetHeaderValue(kCheckSumOffset) == Version::Hash(); | |
2683 } | |
2684 | |
2685 | |
2686 Vector<const SerializedData::Reservation> SnapshotData::Reservations() const { | |
2687 return Vector<const Reservation>( | |
2688 reinterpret_cast<const Reservation*>(data_ + kHeaderSize), | |
2689 GetHeaderValue(kNumReservationsOffset)); | |
2690 } | |
2691 | |
2692 | |
2693 Vector<const byte> SnapshotData::Payload() const { | |
2694 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; | |
2695 const byte* payload = data_ + kHeaderSize + reservations_size; | |
2696 int length = GetHeaderValue(kPayloadLengthOffset); | |
2697 DCHECK_EQ(data_ + size_, payload + length); | |
2698 return Vector<const byte>(payload, length); | |
2699 } | |
2700 | |
2701 | |
2702 class Checksum { | |
2703 public: | |
2704 explicit Checksum(Vector<const byte> payload) { | |
2705 #ifdef MEMORY_SANITIZER | |
2706 // Computing the checksum includes padding bytes for objects like strings. | |
2707 // Mark every object as initialized in the code serializer. | |
2708 MSAN_MEMORY_IS_INITIALIZED(payload.start(), payload.length()); | |
2709 #endif // MEMORY_SANITIZER | |
2710 // Fletcher's checksum. Modified to reduce 64-bit sums to 32-bit. | |
2711 uintptr_t a = 1; | |
2712 uintptr_t b = 0; | |
2713 const uintptr_t* cur = reinterpret_cast<const uintptr_t*>(payload.start()); | |
2714 DCHECK(IsAligned(payload.length(), kIntptrSize)); | |
2715 const uintptr_t* end = cur + payload.length() / kIntptrSize; | |
2716 while (cur < end) { | |
2717 // Unsigned overflow expected and intended. | |
2718 a += *cur++; | |
2719 b += a; | |
2720 } | |
2721 #if V8_HOST_ARCH_64_BIT | |
2722 a ^= a >> 32; | |
2723 b ^= b >> 32; | |
2724 #endif // V8_HOST_ARCH_64_BIT | |
2725 a_ = static_cast<uint32_t>(a); | |
2726 b_ = static_cast<uint32_t>(b); | |
2727 } | |
2728 | |
2729 bool Check(uint32_t a, uint32_t b) const { return a == a_ && b == b_; } | |
2730 | |
2731 uint32_t a() const { return a_; } | |
2732 uint32_t b() const { return b_; } | |
2733 | |
2734 private: | |
2735 uint32_t a_; | |
2736 uint32_t b_; | |
2737 | |
2738 DISALLOW_COPY_AND_ASSIGN(Checksum); | |
2739 }; | |
2740 | |
2741 | |
2742 SerializedCodeData::SerializedCodeData(const List<byte>& payload, | |
2743 const CodeSerializer& cs) { | |
2744 DisallowHeapAllocation no_gc; | |
2745 const List<uint32_t>* stub_keys = cs.stub_keys(); | |
2746 | |
2747 List<Reservation> reservations; | |
2748 cs.EncodeReservations(&reservations); | |
2749 | |
2750 // Calculate sizes. | |
2751 int reservation_size = reservations.length() * kInt32Size; | |
2752 int num_stub_keys = stub_keys->length(); | |
2753 int stub_keys_size = stub_keys->length() * kInt32Size; | |
2754 int payload_offset = kHeaderSize + reservation_size + stub_keys_size; | |
2755 int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); | |
2756 int size = padded_payload_offset + payload.length(); | |
2757 | |
2758 // Allocate backing store and create result data. | |
2759 AllocateData(size); | |
2760 | |
2761 // Set header values. | |
2762 SetMagicNumber(cs.isolate()); | |
2763 SetHeaderValue(kVersionHashOffset, Version::Hash()); | |
2764 SetHeaderValue(kSourceHashOffset, SourceHash(cs.source())); | |
2765 SetHeaderValue(kCpuFeaturesOffset, | |
2766 static_cast<uint32_t>(CpuFeatures::SupportedFeatures())); | |
2767 SetHeaderValue(kFlagHashOffset, FlagList::Hash()); | |
2768 SetHeaderValue(kNumReservationsOffset, reservations.length()); | |
2769 SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys); | |
2770 SetHeaderValue(kPayloadLengthOffset, payload.length()); | |
2771 | |
2772 Checksum checksum(payload.ToConstVector()); | |
2773 SetHeaderValue(kChecksum1Offset, checksum.a()); | |
2774 SetHeaderValue(kChecksum2Offset, checksum.b()); | |
2775 | |
2776 // Copy reservation chunk sizes. | |
2777 CopyBytes(data_ + kHeaderSize, reinterpret_cast<byte*>(reservations.begin()), | |
2778 reservation_size); | |
2779 | |
2780 // Copy code stub keys. | |
2781 CopyBytes(data_ + kHeaderSize + reservation_size, | |
2782 reinterpret_cast<byte*>(stub_keys->begin()), stub_keys_size); | |
2783 | |
2784 memset(data_ + payload_offset, 0, padded_payload_offset - payload_offset); | |
2785 | |
2786 // Copy serialized data. | |
2787 CopyBytes(data_ + padded_payload_offset, payload.begin(), | |
2788 static_cast<size_t>(payload.length())); | |
2789 } | |
2790 | |
2791 | |
2792 SerializedCodeData::SanityCheckResult SerializedCodeData::SanityCheck( | |
2793 Isolate* isolate, String* source) const { | |
2794 uint32_t magic_number = GetMagicNumber(); | |
2795 if (magic_number != ComputeMagicNumber(isolate)) return MAGIC_NUMBER_MISMATCH; | |
2796 uint32_t version_hash = GetHeaderValue(kVersionHashOffset); | |
2797 uint32_t source_hash = GetHeaderValue(kSourceHashOffset); | |
2798 uint32_t cpu_features = GetHeaderValue(kCpuFeaturesOffset); | |
2799 uint32_t flags_hash = GetHeaderValue(kFlagHashOffset); | |
2800 uint32_t c1 = GetHeaderValue(kChecksum1Offset); | |
2801 uint32_t c2 = GetHeaderValue(kChecksum2Offset); | |
2802 if (version_hash != Version::Hash()) return VERSION_MISMATCH; | |
2803 if (source_hash != SourceHash(source)) return SOURCE_MISMATCH; | |
2804 if (cpu_features != static_cast<uint32_t>(CpuFeatures::SupportedFeatures())) { | |
2805 return CPU_FEATURES_MISMATCH; | |
2806 } | |
2807 if (flags_hash != FlagList::Hash()) return FLAGS_MISMATCH; | |
2808 if (!Checksum(Payload()).Check(c1, c2)) return CHECKSUM_MISMATCH; | |
2809 return CHECK_SUCCESS; | |
2810 } | |
2811 | |
2812 | |
2813 uint32_t SerializedCodeData::SourceHash(String* source) const { | |
2814 return source->length(); | |
2815 } | |
2816 | |
2817 | |
2818 // Return ScriptData object and relinquish ownership over it to the caller. | |
2819 ScriptData* SerializedCodeData::GetScriptData() { | |
2820 DCHECK(owns_data_); | |
2821 ScriptData* result = new ScriptData(data_, size_); | |
2822 result->AcquireDataOwnership(); | |
2823 owns_data_ = false; | |
2824 data_ = NULL; | |
2825 return result; | |
2826 } | |
2827 | |
2828 | |
2829 Vector<const SerializedData::Reservation> SerializedCodeData::Reservations() | |
2830 const { | |
2831 return Vector<const Reservation>( | |
2832 reinterpret_cast<const Reservation*>(data_ + kHeaderSize), | |
2833 GetHeaderValue(kNumReservationsOffset)); | |
2834 } | |
2835 | |
2836 | |
2837 Vector<const byte> SerializedCodeData::Payload() const { | |
2838 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; | |
2839 int code_stubs_size = GetHeaderValue(kNumCodeStubKeysOffset) * kInt32Size; | |
2840 int payload_offset = kHeaderSize + reservations_size + code_stubs_size; | |
2841 int padded_payload_offset = POINTER_SIZE_ALIGN(payload_offset); | |
2842 const byte* payload = data_ + padded_payload_offset; | |
2843 DCHECK(IsAligned(reinterpret_cast<intptr_t>(payload), kPointerAlignment)); | |
2844 int length = GetHeaderValue(kPayloadLengthOffset); | |
2845 DCHECK_EQ(data_ + size_, payload + length); | |
2846 return Vector<const byte>(payload, length); | |
2847 } | |
2848 | |
2849 | |
2850 Vector<const uint32_t> SerializedCodeData::CodeStubKeys() const { | |
2851 int reservations_size = GetHeaderValue(kNumReservationsOffset) * kInt32Size; | |
2852 const byte* start = data_ + kHeaderSize + reservations_size; | |
2853 return Vector<const uint32_t>(reinterpret_cast<const uint32_t*>(start), | |
2854 GetHeaderValue(kNumCodeStubKeysOffset)); | |
2855 } | |
2856 | |
2857 | |
2858 SerializedCodeData::SerializedCodeData(ScriptData* data) | |
2859 : SerializedData(const_cast<byte*>(data->data()), data->length()) {} | |
2860 | |
2861 | |
2862 SerializedCodeData* SerializedCodeData::FromCachedData(Isolate* isolate, | |
2863 ScriptData* cached_data, | |
2864 String* source) { | |
2865 DisallowHeapAllocation no_gc; | |
2866 SerializedCodeData* scd = new SerializedCodeData(cached_data); | |
2867 SanityCheckResult r = scd->SanityCheck(isolate, source); | |
2868 if (r == CHECK_SUCCESS) return scd; | |
2869 cached_data->Reject(); | |
2870 source->GetIsolate()->counters()->code_cache_reject_reason()->AddSample(r); | |
2871 delete scd; | |
2872 return NULL; | |
2873 } | |
2874 } // namespace internal | |
2875 } // namespace v8 | |
OLD | NEW |