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.
7 #include "src/accessors.h"
9 #include "src/base/platform/platform.h"
10 #include "src/bootstrapper.h"
11 #include "src/deoptimizer.h"
12 #include "src/execution.h"
13 #include "src/global-handles.h"
14 #include "src/ic/ic.h"
15 #include "src/ic/stub-cache.h"
16 #include "src/natives.h"
17 #include "src/objects.h"
18 #include "src/runtime.h"
19 #include "src/serialize.h"
20 #include "src/snapshot.h"
21 #include "src/snapshot-source-sink.h"
22 #include "src/v8threads.h"
23 #include "src/version.h"
29 // -----------------------------------------------------------------------------
30 // Coding of external references.
32 // The encoding of an external reference. The type is in the high word.
33 // The id is in the low word.
34 static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
35 return static_cast<uint32_t>(type) << 16 | id;
39 static int* GetInternalPointer(StatsCounter* counter) {
40 // All counters refer to dummy_counter, if deserializing happens without
41 // setting up counters.
42 static int dummy_counter = 0;
43 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
47 ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
48 ExternalReferenceTable* external_reference_table =
49 isolate->external_reference_table();
50 if (external_reference_table == NULL) {
51 external_reference_table = new ExternalReferenceTable(isolate);
52 isolate->set_external_reference_table(external_reference_table);
54 return external_reference_table;
58 void ExternalReferenceTable::AddFromId(TypeCode type,
65 ExternalReference ref(static_cast<Builtins::CFunctionId>(id), isolate);
66 address = ref.address();
70 ExternalReference ref(static_cast<Builtins::Name>(id), isolate);
71 address = ref.address();
74 case RUNTIME_FUNCTION: {
75 ExternalReference ref(static_cast<Runtime::FunctionId>(id), isolate);
76 address = ref.address();
80 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)),
82 address = ref.address();
89 Add(address, type, id, name);
93 void ExternalReferenceTable::Add(Address address,
97 DCHECK_NE(NULL, address);
98 ExternalReferenceEntry entry;
99 entry.address = address;
100 entry.code = EncodeExternal(type, id);
102 DCHECK_NE(0, entry.code);
103 // Assert that the code is added in ascending order to rule out duplicates.
104 DCHECK((size() == 0) || (code(size() - 1) < entry.code));
106 if (id > max_id_[type]) max_id_[type] = id;
110 void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
111 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
112 max_id_[type_code] = 0;
116 Add(ExternalReference::roots_array_start(isolate).address(),
117 "Heap::roots_array_start()");
118 Add(ExternalReference::address_of_stack_limit(isolate).address(),
119 "StackGuard::address_of_jslimit()");
120 Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
121 "StackGuard::address_of_real_jslimit()");
122 Add(ExternalReference::new_space_start(isolate).address(),
123 "Heap::NewSpaceStart()");
124 Add(ExternalReference::new_space_mask(isolate).address(),
125 "Heap::NewSpaceMask()");
126 Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
127 "Heap::NewSpaceAllocationLimitAddress()");
128 Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
129 "Heap::NewSpaceAllocationTopAddress()");
130 Add(ExternalReference::debug_break(isolate).address(), "Debug::Break()");
131 Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
132 "Debug::step_in_fp_addr()");
133 Add(ExternalReference::mod_two_doubles_operation(isolate).address(),
135 // Keyed lookup cache.
136 Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
137 "KeyedLookupCache::keys()");
138 Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
139 "KeyedLookupCache::field_offsets()");
140 Add(ExternalReference::handle_scope_next_address(isolate).address(),
141 "HandleScope::next");
142 Add(ExternalReference::handle_scope_limit_address(isolate).address(),
143 "HandleScope::limit");
144 Add(ExternalReference::handle_scope_level_address(isolate).address(),
145 "HandleScope::level");
146 Add(ExternalReference::new_deoptimizer_function(isolate).address(),
147 "Deoptimizer::New()");
148 Add(ExternalReference::compute_output_frames_function(isolate).address(),
149 "Deoptimizer::ComputeOutputFrames()");
150 Add(ExternalReference::address_of_min_int().address(),
151 "LDoubleConstant::min_int");
152 Add(ExternalReference::address_of_one_half().address(),
153 "LDoubleConstant::one_half");
154 Add(ExternalReference::isolate_address(isolate).address(), "isolate");
155 Add(ExternalReference::address_of_negative_infinity().address(),
156 "LDoubleConstant::negative_infinity");
157 Add(ExternalReference::power_double_double_function(isolate).address(),
158 "power_double_double_function");
159 Add(ExternalReference::power_double_int_function(isolate).address(),
160 "power_double_int_function");
161 Add(ExternalReference::math_log_double_function(isolate).address(),
163 Add(ExternalReference::store_buffer_top(isolate).address(),
165 Add(ExternalReference::address_of_canonical_non_hole_nan().address(),
167 Add(ExternalReference::address_of_the_hole_nan().address(), "the_hole_nan");
168 Add(ExternalReference::get_date_field_function(isolate).address(),
170 Add(ExternalReference::date_cache_stamp(isolate).address(),
172 Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
173 "address_of_pending_message_obj");
174 Add(ExternalReference::address_of_has_pending_message(isolate).address(),
175 "address_of_has_pending_message");
176 Add(ExternalReference::address_of_pending_message_script(isolate).address(),
177 "pending_message_script");
178 Add(ExternalReference::get_make_code_young_function(isolate).address(),
179 "Code::MakeCodeYoung");
180 Add(ExternalReference::cpu_features().address(), "cpu_features");
181 Add(ExternalReference(Runtime::kAllocateInNewSpace, isolate).address(),
182 "Runtime::AllocateInNewSpace");
183 Add(ExternalReference(Runtime::kAllocateInTargetSpace, isolate).address(),
184 "Runtime::AllocateInTargetSpace");
185 Add(ExternalReference::old_pointer_space_allocation_top_address(isolate)
187 "Heap::OldPointerSpaceAllocationTopAddress");
188 Add(ExternalReference::old_pointer_space_allocation_limit_address(isolate)
190 "Heap::OldPointerSpaceAllocationLimitAddress");
191 Add(ExternalReference::old_data_space_allocation_top_address(isolate)
193 "Heap::OldDataSpaceAllocationTopAddress");
194 Add(ExternalReference::old_data_space_allocation_limit_address(isolate)
196 "Heap::OldDataSpaceAllocationLimitAddress");
197 Add(ExternalReference::allocation_sites_list_address(isolate).address(),
198 "Heap::allocation_sites_list_address()");
199 Add(ExternalReference::address_of_uint32_bias().address(), "uint32_bias");
200 Add(ExternalReference::get_mark_code_as_executed_function(isolate).address(),
201 "Code::MarkCodeAsExecuted");
202 Add(ExternalReference::is_profiling_address(isolate).address(),
203 "CpuProfiler::is_profiling");
204 Add(ExternalReference::scheduled_exception_address(isolate).address(),
205 "Isolate::scheduled_exception");
206 Add(ExternalReference::invoke_function_callback(isolate).address(),
207 "InvokeFunctionCallback");
208 Add(ExternalReference::invoke_accessor_getter_callback(isolate).address(),
209 "InvokeAccessorGetterCallback");
210 Add(ExternalReference::flush_icache_function(isolate).address(),
211 "CpuFeatures::FlushICache");
212 Add(ExternalReference::log_enter_external_function(isolate).address(),
213 "Logger::EnterExternal");
214 Add(ExternalReference::log_leave_external_function(isolate).address(),
215 "Logger::LeaveExternal");
216 Add(ExternalReference::address_of_minus_one_half().address(),
217 "double_constants.minus_one_half");
218 Add(ExternalReference::stress_deopt_count(isolate).address(),
219 "Isolate::stress_deopt_count_address()");
220 Add(ExternalReference::incremental_marking_record_write_function(isolate)
222 "IncrementalMarking::RecordWriteFromCode");
225 Add(ExternalReference::debug_after_break_target_address(isolate).address(),
226 "Debug::after_break_target_address()");
227 Add(ExternalReference::debug_restarter_frame_function_pointer_address(isolate)
229 "Debug::restarter_frame_function_pointer_address()");
230 Add(ExternalReference::debug_is_active_address(isolate).address(),
231 "Debug::is_active_address()");
233 #ifndef V8_INTERPRETED_REGEXP
234 Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
235 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
236 Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
237 "RegExpMacroAssembler*::CheckStackGuardState()");
238 Add(ExternalReference::re_grow_stack(isolate).address(),
239 "NativeRegExpMacroAssembler::GrowStack()");
240 Add(ExternalReference::re_word_character_map().address(),
241 "NativeRegExpMacroAssembler::word_character_map");
242 Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
243 "RegExpStack::limit_address()");
244 Add(ExternalReference::address_of_regexp_stack_memory_address(isolate)
246 "RegExpStack::memory_address()");
247 Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
248 "RegExpStack::memory_size()");
249 Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
250 "OffsetsVector::static_offsets_vector");
251 #endif // V8_INTERPRETED_REGEXP
253 // The following populates all of the different type of external references
254 // into the ExternalReferenceTable.
256 // NOTE: This function was originally 100k of code. It has since been
257 // rewritten to be mostly table driven, as the callback macro style tends to
258 // very easily cause code bloat. Please be careful in the future when adding
261 struct RefTableEntry {
267 static const RefTableEntry ref_table[] = {
269 #define DEF_ENTRY_C(name, ignored) \
271 Builtins::c_##name, \
272 "Builtins::" #name },
274 BUILTIN_LIST_C(DEF_ENTRY_C)
277 #define DEF_ENTRY_C(name, ignored) \
280 "Builtins::" #name },
281 #define DEF_ENTRY_A(name, kind, state, extra) DEF_ENTRY_C(name, ignored)
283 BUILTIN_LIST_C(DEF_ENTRY_C)
284 BUILTIN_LIST_A(DEF_ENTRY_A)
285 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
290 #define RUNTIME_ENTRY(name, nargs, ressize) \
291 { RUNTIME_FUNCTION, \
295 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
296 INLINE_OPTIMIZED_FUNCTION_LIST(RUNTIME_ENTRY)
299 #define INLINE_OPTIMIZED_ENTRY(name, nargs, ressize) \
300 { RUNTIME_FUNCTION, \
301 Runtime::kInlineOptimized##name, \
304 INLINE_OPTIMIZED_FUNCTION_LIST(INLINE_OPTIMIZED_ENTRY)
305 #undef INLINE_OPTIMIZED_ENTRY
308 #define IC_ENTRY(name) \
313 IC_UTIL_LIST(IC_ENTRY)
315 }; // end of ref_table[].
317 for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
318 AddFromId(ref_table[i].type,
325 struct StatsRefTableEntry {
326 StatsCounter* (Counters::*counter)();
331 const StatsRefTableEntry stats_ref_table[] = {
332 #define COUNTER_ENTRY(name, caption) \
334 Counters::k_##name, \
335 "Counters::" #name },
337 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
338 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
340 }; // end of stats_ref_table[].
342 Counters* counters = isolate->counters();
343 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
344 Add(reinterpret_cast<Address>(GetInternalPointer(
345 (counters->*(stats_ref_table[i].counter))())),
347 stats_ref_table[i].id,
348 stats_ref_table[i].name);
353 const char* AddressNames[] = {
354 #define BUILD_NAME_LITERAL(CamelName, hacker_name) \
355 "Isolate::" #hacker_name "_address",
356 FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL)
358 #undef BUILD_NAME_LITERAL
361 for (uint16_t i = 0; i < Isolate::kIsolateAddressCount; ++i) {
362 Add(isolate->get_address_from_id((Isolate::AddressId)i),
363 TOP_ADDRESS, i, AddressNames[i]);
367 #define ACCESSOR_INFO_DECLARATION(name) \
368 Add(FUNCTION_ADDR(&Accessors::name##Getter), \
370 Accessors::k##name##Getter, \
371 "Accessors::" #name "Getter"); \
372 Add(FUNCTION_ADDR(&Accessors::name##Setter), \
374 Accessors::k##name##Setter, \
375 "Accessors::" #name "Setter");
376 ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
377 #undef ACCESSOR_INFO_DECLARATION
379 StubCache* stub_cache = isolate->stub_cache();
382 Add(stub_cache->key_reference(StubCache::kPrimary).address(),
383 STUB_CACHE_TABLE, 1, "StubCache::primary_->key");
384 Add(stub_cache->value_reference(StubCache::kPrimary).address(),
385 STUB_CACHE_TABLE, 2, "StubCache::primary_->value");
386 Add(stub_cache->map_reference(StubCache::kPrimary).address(),
387 STUB_CACHE_TABLE, 3, "StubCache::primary_->map");
388 Add(stub_cache->key_reference(StubCache::kSecondary).address(),
389 STUB_CACHE_TABLE, 4, "StubCache::secondary_->key");
390 Add(stub_cache->value_reference(StubCache::kSecondary).address(),
391 STUB_CACHE_TABLE, 5, "StubCache::secondary_->value");
392 Add(stub_cache->map_reference(StubCache::kSecondary).address(),
393 STUB_CACHE_TABLE, 6, "StubCache::secondary_->map");
396 Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
397 RUNTIME_ENTRY, 1, "HandleScope::DeleteExtensions");
398 Add(ExternalReference::incremental_marking_record_write_function(isolate)
400 RUNTIME_ENTRY, 2, "IncrementalMarking::RecordWrite");
401 Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
402 RUNTIME_ENTRY, 3, "StoreBuffer::StoreBufferOverflow");
404 // Add a small set of deopt entry addresses to encoder without generating the
405 // deopt table code, which isn't possible at deserialization time.
406 HandleScope scope(isolate);
407 for (int entry = 0; entry < kDeoptTableSerializeEntryCount; ++entry) {
408 Address address = Deoptimizer::GetDeoptimizationEntry(
412 Deoptimizer::CALCULATE_ENTRY_ADDRESS);
413 Add(address, LAZY_DEOPTIMIZATION, entry, "lazy_deopt");
418 ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate)
419 : encodings_(HashMap::PointersMatch),
421 ExternalReferenceTable* external_references =
422 ExternalReferenceTable::instance(isolate_);
423 for (int i = 0; i < external_references->size(); ++i) {
424 Put(external_references->address(i), i);
429 uint32_t ExternalReferenceEncoder::Encode(Address key) const {
430 int index = IndexOf(key);
431 DCHECK(key == NULL || index >= 0);
433 ExternalReferenceTable::instance(isolate_)->code(index) : 0;
437 const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
438 int index = IndexOf(key);
439 return index >= 0 ? ExternalReferenceTable::instance(isolate_)->name(index)
444 int ExternalReferenceEncoder::IndexOf(Address key) const {
445 if (key == NULL) return -1;
446 HashMap::Entry* entry =
447 const_cast<HashMap&>(encodings_).Lookup(key, Hash(key), false);
450 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
454 void ExternalReferenceEncoder::Put(Address key, int index) {
455 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
456 entry->value = reinterpret_cast<void*>(index);
460 ExternalReferenceDecoder::ExternalReferenceDecoder(Isolate* isolate)
461 : encodings_(NewArray<Address*>(kTypeCodeCount)),
463 ExternalReferenceTable* external_references =
464 ExternalReferenceTable::instance(isolate_);
465 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
466 int max = external_references->max_id(type) + 1;
467 encodings_[type] = NewArray<Address>(max + 1);
469 for (int i = 0; i < external_references->size(); ++i) {
470 Put(external_references->code(i), external_references->address(i));
475 ExternalReferenceDecoder::~ExternalReferenceDecoder() {
476 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
477 DeleteArray(encodings_[type]);
479 DeleteArray(encodings_);
483 class CodeAddressMap: public CodeEventLogger {
485 explicit CodeAddressMap(Isolate* isolate)
486 : isolate_(isolate) {
487 isolate->logger()->addCodeEventListener(this);
490 virtual ~CodeAddressMap() {
491 isolate_->logger()->removeCodeEventListener(this);
494 virtual void CodeMoveEvent(Address from, Address to) {
495 address_to_name_map_.Move(from, to);
498 virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) {
501 virtual void CodeDeleteEvent(Address from) {
502 address_to_name_map_.Remove(from);
505 const char* Lookup(Address address) {
506 return address_to_name_map_.Lookup(address);
512 NameMap() : impl_(HashMap::PointersMatch) {}
515 for (HashMap::Entry* p = impl_.Start(); p != NULL; p = impl_.Next(p)) {
516 DeleteArray(static_cast<const char*>(p->value));
520 void Insert(Address code_address, const char* name, int name_size) {
521 HashMap::Entry* entry = FindOrCreateEntry(code_address);
522 if (entry->value == NULL) {
523 entry->value = CopyName(name, name_size);
527 const char* Lookup(Address code_address) {
528 HashMap::Entry* entry = FindEntry(code_address);
529 return (entry != NULL) ? static_cast<const char*>(entry->value) : NULL;
532 void Remove(Address code_address) {
533 HashMap::Entry* entry = FindEntry(code_address);
535 DeleteArray(static_cast<char*>(entry->value));
540 void Move(Address from, Address to) {
541 if (from == to) return;
542 HashMap::Entry* from_entry = FindEntry(from);
543 DCHECK(from_entry != NULL);
544 void* value = from_entry->value;
545 RemoveEntry(from_entry);
546 HashMap::Entry* to_entry = FindOrCreateEntry(to);
547 DCHECK(to_entry->value == NULL);
548 to_entry->value = value;
552 static char* CopyName(const char* name, int name_size) {
553 char* result = NewArray<char>(name_size + 1);
554 for (int i = 0; i < name_size; ++i) {
556 if (c == '\0') c = ' ';
559 result[name_size] = '\0';
563 HashMap::Entry* FindOrCreateEntry(Address code_address) {
564 return impl_.Lookup(code_address, ComputePointerHash(code_address), true);
567 HashMap::Entry* FindEntry(Address code_address) {
568 return impl_.Lookup(code_address,
569 ComputePointerHash(code_address),
573 void RemoveEntry(HashMap::Entry* entry) {
574 impl_.Remove(entry->key, entry->hash);
579 DISALLOW_COPY_AND_ASSIGN(NameMap);
582 virtual void LogRecordedBuffer(Code* code,
586 address_to_name_map_.Insert(code->address(), name, length);
589 NameMap address_to_name_map_;
594 Deserializer::Deserializer(SnapshotByteSource* source)
596 attached_objects_(NULL),
598 external_reference_decoder_(NULL) {
599 for (int i = 0; i < LAST_SPACE + 1; i++) {
600 reservations_[i] = kUninitializedReservation;
605 void Deserializer::FlushICacheForNewCodeObjects() {
606 PageIterator it(isolate_->heap()->code_space());
607 while (it.has_next()) {
609 CpuFeatures::FlushICache(p->area_start(), p->area_end() - p->area_start());
614 void Deserializer::Deserialize(Isolate* isolate) {
616 DCHECK(isolate_ != NULL);
617 isolate_->heap()->ReserveSpace(reservations_, &high_water_[0]);
618 // No active threads.
619 DCHECK_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse());
620 // No active handles.
621 DCHECK(isolate_->handle_scope_implementer()->blocks()->is_empty());
622 DCHECK_EQ(NULL, external_reference_decoder_);
623 external_reference_decoder_ = new ExternalReferenceDecoder(isolate);
624 isolate_->heap()->IterateSmiRoots(this);
625 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
626 isolate_->heap()->RepairFreeListsAfterBoot();
627 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
629 isolate_->heap()->set_native_contexts_list(
630 isolate_->heap()->undefined_value());
631 isolate_->heap()->set_array_buffers_list(
632 isolate_->heap()->undefined_value());
634 // The allocation site list is build during root iteration, but if no sites
635 // were encountered then it needs to be initialized to undefined.
636 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
637 isolate_->heap()->set_allocation_sites_list(
638 isolate_->heap()->undefined_value());
641 isolate_->heap()->InitializeWeakObjectToCodeTable();
643 // Update data pointers to the external strings containing natives sources.
644 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
645 Object* source = isolate_->heap()->natives_source_cache()->get(i);
646 if (!source->IsUndefined()) {
647 ExternalAsciiString::cast(source)->update_data_cache();
651 FlushICacheForNewCodeObjects();
653 // Issue code events for newly deserialized code objects.
654 LOG_CODE_EVENT(isolate_, LogCodeObjects());
655 LOG_CODE_EVENT(isolate_, LogCompiledFunctions());
659 void Deserializer::DeserializePartial(Isolate* isolate, Object** root) {
661 for (int i = NEW_SPACE; i < kNumberOfSpaces; i++) {
662 DCHECK(reservations_[i] != kUninitializedReservation);
664 isolate_->heap()->ReserveSpace(reservations_, &high_water_[0]);
665 if (external_reference_decoder_ == NULL) {
666 external_reference_decoder_ = new ExternalReferenceDecoder(isolate);
669 DisallowHeapAllocation no_gc;
671 // Keep track of the code space start and end pointers in case new
672 // code objects were unserialized
673 OldSpace* code_space = isolate_->heap()->code_space();
674 Address start_address = code_space->top();
677 // There's no code deserialized here. If this assert fires
678 // then that's changed and logging should be added to notify
679 // the profiler et al of the new code.
680 CHECK_EQ(start_address, code_space->top());
684 Deserializer::~Deserializer() {
685 // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed.
686 // DCHECK(source_->AtEOF());
687 if (external_reference_decoder_) {
688 delete external_reference_decoder_;
689 external_reference_decoder_ = NULL;
691 if (attached_objects_) attached_objects_->Dispose();
695 // This is called on the roots. It is the driver of the deserialization
696 // process. It is also called on the body of each function.
697 void Deserializer::VisitPointers(Object** start, Object** end) {
698 // The space must be new space. Any other space would cause ReadChunk to try
699 // to update the remembered using NULL as the address.
700 ReadChunk(start, end, NEW_SPACE, NULL);
704 void Deserializer::RelinkAllocationSite(AllocationSite* site) {
705 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
706 site->set_weak_next(isolate_->heap()->undefined_value());
708 site->set_weak_next(isolate_->heap()->allocation_sites_list());
710 isolate_->heap()->set_allocation_sites_list(site);
714 // Used to insert a deserialized internalized string into the string table.
715 class StringTableInsertionKey : public HashTableKey {
717 explicit StringTableInsertionKey(String* string)
718 : string_(string), hash_(HashForObject(string)) {
719 DCHECK(string->IsInternalizedString());
722 virtual bool IsMatch(Object* string) {
723 // We know that all entries in a hash table had their hash keys created.
724 // Use that knowledge to have fast failure.
725 if (hash_ != HashForObject(string)) return false;
726 // We want to compare the content of two internalized strings here.
727 return string_->SlowEquals(String::cast(string));
730 virtual uint32_t Hash() V8_OVERRIDE { return hash_; }
732 virtual uint32_t HashForObject(Object* key) V8_OVERRIDE {
733 return String::cast(key)->Hash();
736 MUST_USE_RESULT virtual Handle<Object> AsHandle(Isolate* isolate)
738 return handle(string_, isolate);
746 HeapObject* Deserializer::ProcessNewObjectFromSerializedCode(HeapObject* obj) {
747 if (obj->IsString()) {
748 String* string = String::cast(obj);
749 // Uninitialize hash field as the hash seed may have changed.
750 string->set_hash_field(String::kEmptyHashField);
751 if (string->IsInternalizedString()) {
752 DisallowHeapAllocation no_gc;
753 HandleScope scope(isolate_);
754 StringTableInsertionKey key(string);
755 String* canonical = *StringTable::LookupKey(isolate_, &key);
756 string->SetForwardedInternalizedString(canonical);
764 Object* Deserializer::ProcessBackRefInSerializedCode(Object* obj) {
765 if (obj->IsInternalizedString()) {
766 return String::cast(obj)->GetForwardedInternalizedString();
772 // This routine writes the new object into the pointer provided and then
773 // returns true if the new object was in young space and false otherwise.
774 // The reason for this strange interface is that otherwise the object is
775 // written very late, which means the FreeSpace map is not set up by the
776 // time we need to use it to mark the space at the end of a page free.
777 void Deserializer::ReadObject(int space_number,
778 Object** write_back) {
779 int size = source_->GetInt() << kObjectAlignmentBits;
780 Address address = Allocate(space_number, size);
781 HeapObject* obj = HeapObject::FromAddress(address);
782 isolate_->heap()->OnAllocationEvent(obj, size);
783 Object** current = reinterpret_cast<Object**>(address);
784 Object** limit = current + (size >> kPointerSizeLog2);
785 if (FLAG_log_snapshot_positions) {
786 LOG(isolate_, SnapshotPositionEvent(address, source_->position()));
788 ReadChunk(current, limit, space_number, address);
790 // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
791 // as a (weak) root. If this root is relocated correctly,
792 // RelinkAllocationSite() isn't necessary.
793 if (obj->IsAllocationSite()) RelinkAllocationSite(AllocationSite::cast(obj));
795 // Fix up strings from serialized user code.
796 if (deserializing_user_code()) obj = ProcessNewObjectFromSerializedCode(obj);
800 bool is_codespace = (space_number == CODE_SPACE);
801 DCHECK(obj->IsCode() == is_codespace);
805 void Deserializer::ReadChunk(Object** current,
808 Address current_object_address) {
809 Isolate* const isolate = isolate_;
810 // Write barrier support costs around 1% in startup time. In fact there
811 // are no new space objects in current boot snapshots, so it's not needed,
812 // but that may change.
813 bool write_barrier_needed = (current_object_address != NULL &&
814 source_space != NEW_SPACE &&
815 source_space != CELL_SPACE &&
816 source_space != PROPERTY_CELL_SPACE &&
817 source_space != CODE_SPACE &&
818 source_space != OLD_DATA_SPACE);
819 while (current < limit) {
820 int data = source_->Get();
822 #define CASE_STATEMENT(where, how, within, space_number) \
823 case where + how + within + space_number: \
824 STATIC_ASSERT((where & ~kPointedToMask) == 0); \
825 STATIC_ASSERT((how & ~kHowToCodeMask) == 0); \
826 STATIC_ASSERT((within & ~kWhereToPointMask) == 0); \
827 STATIC_ASSERT((space_number & ~kSpaceMask) == 0);
829 #define CASE_BODY(where, how, within, space_number_if_any) \
831 bool emit_write_barrier = false; \
832 bool current_was_incremented = false; \
833 int space_number = space_number_if_any == kAnyOldSpace \
834 ? (data & kSpaceMask) \
835 : space_number_if_any; \
836 if (where == kNewObject && how == kPlain && within == kStartOfObject) { \
837 ReadObject(space_number, current); \
838 emit_write_barrier = (space_number == NEW_SPACE); \
840 Object* new_object = NULL; /* May not be a real Object pointer. */ \
841 if (where == kNewObject) { \
842 ReadObject(space_number, &new_object); \
843 } else if (where == kRootArray) { \
844 int root_id = source_->GetInt(); \
845 new_object = isolate->heap()->roots_array_start()[root_id]; \
846 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
847 } else if (where == kPartialSnapshotCache) { \
848 int cache_index = source_->GetInt(); \
849 new_object = isolate->serialize_partial_snapshot_cache()[cache_index]; \
850 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
851 } else if (where == kExternalReference) { \
852 int skip = source_->GetInt(); \
853 current = reinterpret_cast<Object**>( \
854 reinterpret_cast<Address>(current) + skip); \
855 int reference_id = source_->GetInt(); \
856 Address address = external_reference_decoder_->Decode(reference_id); \
857 new_object = reinterpret_cast<Object*>(address); \
858 } else if (where == kBackref) { \
859 emit_write_barrier = (space_number == NEW_SPACE); \
860 new_object = GetAddressFromEnd(data & kSpaceMask); \
861 if (deserializing_user_code()) { \
862 new_object = ProcessBackRefInSerializedCode(new_object); \
864 } else if (where == kBuiltin) { \
865 DCHECK(deserializing_user_code()); \
866 int builtin_id = source_->GetInt(); \
867 DCHECK_LE(0, builtin_id); \
868 DCHECK_LT(builtin_id, Builtins::builtin_count); \
869 Builtins::Name name = static_cast<Builtins::Name>(builtin_id); \
870 new_object = isolate->builtins()->builtin(name); \
871 emit_write_barrier = false; \
872 } else if (where == kAttachedReference) { \
873 DCHECK(deserializing_user_code()); \
874 int index = source_->GetInt(); \
875 new_object = attached_objects_->at(index); \
876 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
878 DCHECK(where == kBackrefWithSkip); \
879 int skip = source_->GetInt(); \
880 current = reinterpret_cast<Object**>( \
881 reinterpret_cast<Address>(current) + skip); \
882 emit_write_barrier = (space_number == NEW_SPACE); \
883 new_object = GetAddressFromEnd(data & kSpaceMask); \
884 if (deserializing_user_code()) { \
885 new_object = ProcessBackRefInSerializedCode(new_object); \
888 if (within == kInnerPointer) { \
889 if (space_number != CODE_SPACE || new_object->IsCode()) { \
890 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
892 reinterpret_cast<Object*>(new_code_object->instruction_start()); \
894 DCHECK(space_number == CODE_SPACE); \
895 Cell* cell = Cell::cast(new_object); \
896 new_object = reinterpret_cast<Object*>(cell->ValueAddress()); \
899 if (how == kFromCode) { \
900 Address location_of_branch_data = reinterpret_cast<Address>(current); \
901 Assembler::deserialization_set_special_target_at( \
902 location_of_branch_data, \
903 Code::cast(HeapObject::FromAddress(current_object_address)), \
904 reinterpret_cast<Address>(new_object)); \
905 location_of_branch_data += Assembler::kSpecialTargetSize; \
906 current = reinterpret_cast<Object**>(location_of_branch_data); \
907 current_was_incremented = true; \
909 *current = new_object; \
912 if (emit_write_barrier && write_barrier_needed) { \
913 Address current_address = reinterpret_cast<Address>(current); \
914 isolate->heap()->RecordWrite( \
915 current_object_address, \
916 static_cast<int>(current_address - current_object_address)); \
918 if (!current_was_incremented) { \
924 // This generates a case and a body for the new space (which has to do extra
925 // write barrier handling) and handles the other spaces with 8 fall-through
926 // cases and one body.
927 #define ALL_SPACES(where, how, within) \
928 CASE_STATEMENT(where, how, within, NEW_SPACE) \
929 CASE_BODY(where, how, within, NEW_SPACE) \
930 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
931 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
932 CASE_STATEMENT(where, how, within, CODE_SPACE) \
933 CASE_STATEMENT(where, how, within, CELL_SPACE) \
934 CASE_STATEMENT(where, how, within, PROPERTY_CELL_SPACE) \
935 CASE_STATEMENT(where, how, within, MAP_SPACE) \
936 CASE_BODY(where, how, within, kAnyOldSpace)
938 #define FOUR_CASES(byte_code) \
940 case byte_code + 1: \
941 case byte_code + 2: \
944 #define SIXTEEN_CASES(byte_code) \
945 FOUR_CASES(byte_code) \
946 FOUR_CASES(byte_code + 4) \
947 FOUR_CASES(byte_code + 8) \
948 FOUR_CASES(byte_code + 12)
950 #define COMMON_RAW_LENGTHS(f) \
983 // We generate 15 cases and bodies that process special tags that combine
984 // the raw data tag and the length into one byte.
985 #define RAW_CASE(index) \
986 case kRawData + index: { \
987 byte* raw_data_out = reinterpret_cast<byte*>(current); \
988 source_->CopyRaw(raw_data_out, index * kPointerSize); \
990 reinterpret_cast<Object**>(raw_data_out + index * kPointerSize); \
993 COMMON_RAW_LENGTHS(RAW_CASE)
996 // Deserialize a chunk of raw data that doesn't have one of the popular
999 int size = source_->GetInt();
1000 byte* raw_data_out = reinterpret_cast<byte*>(current);
1001 source_->CopyRaw(raw_data_out, size);
1005 SIXTEEN_CASES(kRootArrayConstants + kNoSkipDistance)
1006 SIXTEEN_CASES(kRootArrayConstants + kNoSkipDistance + 16) {
1007 int root_id = RootArrayConstantFromByteCode(data);
1008 Object* object = isolate->heap()->roots_array_start()[root_id];
1009 DCHECK(!isolate->heap()->InNewSpace(object));
1010 *current++ = object;
1014 SIXTEEN_CASES(kRootArrayConstants + kHasSkipDistance)
1015 SIXTEEN_CASES(kRootArrayConstants + kHasSkipDistance + 16) {
1016 int root_id = RootArrayConstantFromByteCode(data);
1017 int skip = source_->GetInt();
1018 current = reinterpret_cast<Object**>(
1019 reinterpret_cast<intptr_t>(current) + skip);
1020 Object* object = isolate->heap()->roots_array_start()[root_id];
1021 DCHECK(!isolate->heap()->InNewSpace(object));
1022 *current++ = object;
1027 int repeats = source_->GetInt();
1028 Object* object = current[-1];
1029 DCHECK(!isolate->heap()->InNewSpace(object));
1030 for (int i = 0; i < repeats; i++) current[i] = object;
1035 STATIC_ASSERT(kRootArrayNumberOfConstantEncodings ==
1036 Heap::kOldSpaceRoots);
1037 STATIC_ASSERT(kMaxRepeats == 13);
1038 case kConstantRepeat:
1039 FOUR_CASES(kConstantRepeat + 1)
1040 FOUR_CASES(kConstantRepeat + 5)
1041 FOUR_CASES(kConstantRepeat + 9) {
1042 int repeats = RepeatsForCode(data);
1043 Object* object = current[-1];
1044 DCHECK(!isolate->heap()->InNewSpace(object));
1045 for (int i = 0; i < repeats; i++) current[i] = object;
1050 // Deserialize a new object and write a pointer to it to the current
1052 ALL_SPACES(kNewObject, kPlain, kStartOfObject)
1053 // Support for direct instruction pointers in functions. It's an inner
1054 // pointer because it points at the entry point, not at the start of the
1056 CASE_STATEMENT(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
1057 CASE_BODY(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
1058 // Deserialize a new code object and write a pointer to its first
1059 // instruction to the current code object.
1060 ALL_SPACES(kNewObject, kFromCode, kInnerPointer)
1061 // Find a recently deserialized object using its offset from the current
1062 // allocation point and write a pointer to it to the current object.
1063 ALL_SPACES(kBackref, kPlain, kStartOfObject)
1064 ALL_SPACES(kBackrefWithSkip, kPlain, kStartOfObject)
1065 #if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL || \
1066 defined(V8_TARGET_ARCH_MIPS64)
1067 // Deserialize a new object from pointer found in code and write
1068 // a pointer to it to the current object. Required only for MIPS or ARM
1069 // with ool constant pool, and omitted on the other architectures because
1070 // it is fully unrolled and would cause bloat.
1071 ALL_SPACES(kNewObject, kFromCode, kStartOfObject)
1072 // Find a recently deserialized code object using its offset from the
1073 // current allocation point and write a pointer to it to the current
1074 // object. Required only for MIPS or ARM with ool constant pool.
1075 ALL_SPACES(kBackref, kFromCode, kStartOfObject)
1076 ALL_SPACES(kBackrefWithSkip, kFromCode, kStartOfObject)
1078 // Find a recently deserialized code object using its offset from the
1079 // current allocation point and write a pointer to its first instruction
1080 // to the current code object or the instruction pointer in a function
1082 ALL_SPACES(kBackref, kFromCode, kInnerPointer)
1083 ALL_SPACES(kBackrefWithSkip, kFromCode, kInnerPointer)
1084 ALL_SPACES(kBackref, kPlain, kInnerPointer)
1085 ALL_SPACES(kBackrefWithSkip, kPlain, kInnerPointer)
1086 // Find an object in the roots array and write a pointer to it to the
1088 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
1089 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0)
1090 #if V8_OOL_CONSTANT_POOL
1091 // Find an object in the roots array and write a pointer to it to in code.
1092 CASE_STATEMENT(kRootArray, kFromCode, kStartOfObject, 0)
1093 CASE_BODY(kRootArray, kFromCode, kStartOfObject, 0)
1095 // Find an object in the partial snapshots cache and write a pointer to it
1096 // to the current object.
1097 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
1098 CASE_BODY(kPartialSnapshotCache,
1102 // Find an code entry in the partial snapshots cache and
1103 // write a pointer to it to the current object.
1104 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
1105 CASE_BODY(kPartialSnapshotCache,
1109 // Find an external reference and write a pointer to it to the current
1111 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
1112 CASE_BODY(kExternalReference,
1116 // Find an external reference and write a pointer to it in the current
1118 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
1119 CASE_BODY(kExternalReference,
1123 // Find a builtin and write a pointer to it to the current object.
1124 CASE_STATEMENT(kBuiltin, kPlain, kStartOfObject, 0)
1125 CASE_BODY(kBuiltin, kPlain, kStartOfObject, 0)
1126 #if V8_OOL_CONSTANT_POOL
1127 // Find a builtin code entry and write a pointer to it to the current
1129 CASE_STATEMENT(kBuiltin, kPlain, kInnerPointer, 0)
1130 CASE_BODY(kBuiltin, kPlain, kInnerPointer, 0)
1132 // Find a builtin and write a pointer to it in the current code object.
1133 CASE_STATEMENT(kBuiltin, kFromCode, kInnerPointer, 0)
1134 CASE_BODY(kBuiltin, kFromCode, kInnerPointer, 0)
1135 // Find an object in the attached references and write a pointer to it to
1136 // the current object.
1137 CASE_STATEMENT(kAttachedReference, kPlain, kStartOfObject, 0)
1138 CASE_BODY(kAttachedReference, kPlain, kStartOfObject, 0)
1140 #undef CASE_STATEMENT
1145 int size = source_->GetInt();
1146 current = reinterpret_cast<Object**>(
1147 reinterpret_cast<intptr_t>(current) + size);
1151 case kNativesStringResource: {
1152 int index = source_->Get();
1153 Vector<const char> source_vector = Natives::GetRawScriptSource(index);
1154 NativesExternalStringResource* resource =
1155 new NativesExternalStringResource(isolate->bootstrapper(),
1156 source_vector.start(),
1157 source_vector.length());
1158 *current++ = reinterpret_cast<Object*>(resource);
1162 case kSynchronize: {
1163 // If we get here then that indicates that you have a mismatch between
1164 // the number of GC roots when serializing and deserializing.
1172 DCHECK_EQ(limit, current);
1176 Serializer::Serializer(Isolate* isolate, SnapshotByteSink* sink)
1177 : isolate_(isolate),
1179 external_reference_encoder_(new ExternalReferenceEncoder(isolate)),
1180 root_index_wave_front_(0),
1181 code_address_map_(NULL) {
1182 // The serializer is meant to be used only to generate initial heap images
1183 // from a context in which there is only one isolate.
1184 for (int i = 0; i <= LAST_SPACE; i++) {
1190 Serializer::~Serializer() {
1191 delete external_reference_encoder_;
1192 if (code_address_map_ != NULL) delete code_address_map_;
1196 void StartupSerializer::SerializeStrongReferences() {
1197 Isolate* isolate = this->isolate();
1198 // No active threads.
1199 CHECK_EQ(NULL, isolate->thread_manager()->FirstThreadStateInUse());
1200 // No active or weak handles.
1201 CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
1202 CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
1203 CHECK_EQ(0, isolate->eternal_handles()->NumberOfHandles());
1204 // We don't support serializing installed extensions.
1205 CHECK(!isolate->has_installed_extensions());
1206 isolate->heap()->IterateSmiRoots(this);
1207 isolate->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
1211 void PartialSerializer::Serialize(Object** object) {
1212 this->VisitPointer(object);
1217 bool Serializer::ShouldBeSkipped(Object** current) {
1218 Object** roots = isolate()->heap()->roots_array_start();
1219 return current == &roots[Heap::kStoreBufferTopRootIndex]
1220 || current == &roots[Heap::kStackLimitRootIndex]
1221 || current == &roots[Heap::kRealStackLimitRootIndex];
1225 void Serializer::VisitPointers(Object** start, Object** end) {
1226 Isolate* isolate = this->isolate();;
1228 for (Object** current = start; current < end; current++) {
1229 if (start == isolate->heap()->roots_array_start()) {
1230 root_index_wave_front_ =
1231 Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
1233 if (ShouldBeSkipped(current)) {
1234 sink_->Put(kSkip, "Skip");
1235 sink_->PutInt(kPointerSize, "SkipOneWord");
1236 } else if ((*current)->IsSmi()) {
1237 sink_->Put(kRawData + 1, "Smi");
1238 for (int i = 0; i < kPointerSize; i++) {
1239 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1242 SerializeObject(*current, kPlain, kStartOfObject, 0);
1248 // This ensures that the partial snapshot cache keeps things alive during GC and
1249 // tracks their movement. When it is called during serialization of the startup
1250 // snapshot nothing happens. When the partial (context) snapshot is created,
1251 // this array is populated with the pointers that the partial snapshot will
1252 // need. As that happens we emit serialized objects to the startup snapshot
1253 // that correspond to the elements of this cache array. On deserialization we
1254 // therefore need to visit the cache array. This fills it up with pointers to
1255 // deserialized objects.
1256 void SerializerDeserializer::Iterate(Isolate* isolate,
1257 ObjectVisitor* visitor) {
1258 if (isolate->serializer_enabled()) return;
1259 for (int i = 0; ; i++) {
1260 if (isolate->serialize_partial_snapshot_cache_length() <= i) {
1261 // Extend the array ready to get a value from the visitor when
1263 isolate->PushToPartialSnapshotCache(Smi::FromInt(0));
1265 Object** cache = isolate->serialize_partial_snapshot_cache();
1266 visitor->VisitPointers(&cache[i], &cache[i + 1]);
1267 // Sentinel is the undefined object, which is a root so it will not normally
1268 // be found in the cache.
1269 if (cache[i] == isolate->heap()->undefined_value()) {
1276 int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1277 Isolate* isolate = this->isolate();
1280 i < isolate->serialize_partial_snapshot_cache_length();
1282 Object* entry = isolate->serialize_partial_snapshot_cache()[i];
1283 if (entry == heap_object) return i;
1286 // We didn't find the object in the cache. So we add it to the cache and
1287 // then visit the pointer so that it becomes part of the startup snapshot
1288 // and we can refer to it from the partial snapshot.
1289 int length = isolate->serialize_partial_snapshot_cache_length();
1290 isolate->PushToPartialSnapshotCache(heap_object);
1291 startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object));
1292 // We don't recurse from the startup snapshot generator into the partial
1293 // snapshot generator.
1294 DCHECK(length == isolate->serialize_partial_snapshot_cache_length() - 1);
1299 int Serializer::RootIndex(HeapObject* heap_object, HowToCode from) {
1300 Heap* heap = isolate()->heap();
1301 if (heap->InNewSpace(heap_object)) return kInvalidRootIndex;
1302 for (int i = 0; i < root_index_wave_front_; i++) {
1303 Object* root = heap->roots_array_start()[i];
1304 if (!root->IsSmi() && root == heap_object) {
1305 #if defined(V8_TARGET_ARCH_MIPS) || defined(V8_TARGET_ARCH_MIPS64)
1306 if (from == kFromCode) {
1307 // In order to avoid code bloat in the deserializer we don't have
1308 // support for the encoding that specifies a particular root should
1309 // be written from within code.
1310 return kInvalidRootIndex;
1316 return kInvalidRootIndex;
1320 // Encode the location of an already deserialized object in order to write its
1321 // location into a later object. We can encode the location as an offset from
1322 // the start of the deserialized objects or as an offset backwards from the
1323 // current allocation pointer.
1324 void Serializer::SerializeReferenceToPreviousObject(
1327 HowToCode how_to_code,
1328 WhereToPoint where_to_point,
1330 int offset = CurrentAllocationAddress(space) - address;
1331 // Shift out the bits that are always 0.
1332 offset >>= kObjectAlignmentBits;
1334 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1336 sink_->Put(kBackrefWithSkip + how_to_code + where_to_point + space,
1337 "BackRefSerWithSkip");
1338 sink_->PutInt(skip, "BackRefSkipDistance");
1340 sink_->PutInt(offset, "offset");
1344 void StartupSerializer::SerializeObject(
1346 HowToCode how_to_code,
1347 WhereToPoint where_to_point,
1349 CHECK(o->IsHeapObject());
1350 HeapObject* heap_object = HeapObject::cast(o);
1351 DCHECK(!heap_object->IsJSFunction());
1354 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1355 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1359 if (address_mapper_.IsMapped(heap_object)) {
1360 int space = SpaceOfObject(heap_object);
1361 int address = address_mapper_.MappedTo(heap_object);
1362 SerializeReferenceToPreviousObject(space,
1369 sink_->Put(kSkip, "FlushPendingSkip");
1370 sink_->PutInt(skip, "SkipDistance");
1373 // Object has not yet been serialized. Serialize it here.
1374 ObjectSerializer object_serializer(this,
1379 object_serializer.Serialize();
1384 void StartupSerializer::SerializeWeakReferences() {
1385 // This phase comes right after the partial serialization (of the snapshot).
1386 // After we have done the partial serialization the partial snapshot cache
1387 // will contain some references needed to decode the partial snapshot. We
1388 // add one entry with 'undefined' which is the sentinel that the deserializer
1389 // uses to know it is done deserializing the array.
1390 Object* undefined = isolate()->heap()->undefined_value();
1391 VisitPointer(&undefined);
1392 isolate()->heap()->IterateWeakRoots(this, VISIT_ALL);
1397 void Serializer::PutRoot(int root_index,
1399 SerializerDeserializer::HowToCode how_to_code,
1400 SerializerDeserializer::WhereToPoint where_to_point,
1402 if (how_to_code == kPlain &&
1403 where_to_point == kStartOfObject &&
1404 root_index < kRootArrayNumberOfConstantEncodings &&
1405 !isolate()->heap()->InNewSpace(object)) {
1407 sink_->Put(kRootArrayConstants + kNoSkipDistance + root_index,
1410 sink_->Put(kRootArrayConstants + kHasSkipDistance + root_index,
1412 sink_->PutInt(skip, "SkipInPutRoot");
1416 sink_->Put(kSkip, "SkipFromPutRoot");
1417 sink_->PutInt(skip, "SkipFromPutRootDistance");
1419 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
1420 sink_->PutInt(root_index, "root_index");
1425 void PartialSerializer::SerializeObject(
1427 HowToCode how_to_code,
1428 WhereToPoint where_to_point,
1430 CHECK(o->IsHeapObject());
1431 HeapObject* heap_object = HeapObject::cast(o);
1433 if (heap_object->IsMap()) {
1434 // The code-caches link to context-specific code objects, which
1435 // the startup and context serializes cannot currently handle.
1436 DCHECK(Map::cast(heap_object)->code_cache() ==
1437 heap_object->GetHeap()->empty_fixed_array());
1441 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1442 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1446 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1448 sink_->Put(kSkip, "SkipFromSerializeObject");
1449 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1452 int cache_index = PartialSnapshotCacheIndex(heap_object);
1453 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1454 "PartialSnapshotCache");
1455 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1459 // Pointers from the partial snapshot to the objects in the startup snapshot
1460 // should go through the root array or through the partial snapshot cache.
1461 // If this is not the case you may have to add something to the root array.
1462 DCHECK(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1463 // All the internalized strings that the partial snapshot needs should be
1464 // either in the root table or in the partial snapshot cache.
1465 DCHECK(!heap_object->IsInternalizedString());
1467 if (address_mapper_.IsMapped(heap_object)) {
1468 int space = SpaceOfObject(heap_object);
1469 int address = address_mapper_.MappedTo(heap_object);
1470 SerializeReferenceToPreviousObject(space,
1477 sink_->Put(kSkip, "SkipFromSerializeObject");
1478 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1480 // Object has not yet been serialized. Serialize it here.
1481 ObjectSerializer serializer(this,
1486 serializer.Serialize();
1491 void Serializer::ObjectSerializer::Serialize() {
1492 int space = Serializer::SpaceOfObject(object_);
1493 int size = object_->Size();
1495 sink_->Put(kNewObject + reference_representation_ + space,
1496 "ObjectSerialization");
1497 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1499 if (serializer_->code_address_map_) {
1500 const char* code_name =
1501 serializer_->code_address_map_->Lookup(object_->address());
1502 LOG(serializer_->isolate_,
1503 CodeNameEvent(object_->address(), sink_->Position(), code_name));
1504 LOG(serializer_->isolate_,
1505 SnapshotPositionEvent(object_->address(), sink_->Position()));
1508 // Mark this object as already serialized.
1509 int offset = serializer_->Allocate(space, size);
1510 serializer_->address_mapper()->AddMapping(object_, offset);
1512 // Serialize the map (first word of the object).
1513 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject, 0);
1515 // Serialize the rest of the object.
1516 CHECK_EQ(0, bytes_processed_so_far_);
1517 bytes_processed_so_far_ = kPointerSize;
1518 object_->IterateBody(object_->map()->instance_type(), size, this);
1519 OutputRawData(object_->address() + size);
1523 void Serializer::ObjectSerializer::VisitPointers(Object** start,
1525 Object** current = start;
1526 while (current < end) {
1527 while (current < end && (*current)->IsSmi()) current++;
1528 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1530 while (current < end && !(*current)->IsSmi()) {
1531 HeapObject* current_contents = HeapObject::cast(*current);
1532 int root_index = serializer_->RootIndex(current_contents, kPlain);
1533 // Repeats are not subject to the write barrier so there are only some
1534 // objects that can be used in a repeat encoding. These are the early
1535 // ones in the root array that are never in new space.
1536 if (current != start &&
1537 root_index != kInvalidRootIndex &&
1538 root_index < kRootArrayNumberOfConstantEncodings &&
1539 current_contents == current[-1]) {
1540 DCHECK(!serializer_->isolate()->heap()->InNewSpace(current_contents));
1541 int repeat_count = 1;
1542 while (current < end - 1 && current[repeat_count] == current_contents) {
1545 current += repeat_count;
1546 bytes_processed_so_far_ += repeat_count * kPointerSize;
1547 if (repeat_count > kMaxRepeats) {
1548 sink_->Put(kRepeat, "SerializeRepeats");
1549 sink_->PutInt(repeat_count, "SerializeRepeats");
1551 sink_->Put(CodeForRepeats(repeat_count), "SerializeRepeats");
1554 serializer_->SerializeObject(
1555 current_contents, kPlain, kStartOfObject, 0);
1556 bytes_processed_so_far_ += kPointerSize;
1564 void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
1565 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1566 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1568 int skip = OutputRawData(rinfo->target_address_address(),
1569 kCanReturnSkipInsteadOfSkipping);
1570 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1571 Object* object = rinfo->target_object();
1572 serializer_->SerializeObject(object, how_to_code, kStartOfObject, skip);
1573 bytes_processed_so_far_ += rinfo->target_address_size();
1577 void Serializer::ObjectSerializer::VisitExternalReference(Address* p) {
1578 int skip = OutputRawData(reinterpret_cast<Address>(p),
1579 kCanReturnSkipInsteadOfSkipping);
1580 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
1581 sink_->PutInt(skip, "SkipB4ExternalRef");
1582 Address target = *p;
1583 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1584 bytes_processed_so_far_ += kPointerSize;
1588 void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
1589 int skip = OutputRawData(rinfo->target_address_address(),
1590 kCanReturnSkipInsteadOfSkipping);
1591 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1592 sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
1593 sink_->PutInt(skip, "SkipB4ExternalRef");
1594 Address target = rinfo->target_reference();
1595 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1596 bytes_processed_so_far_ += rinfo->target_address_size();
1600 void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1601 int skip = OutputRawData(rinfo->target_address_address(),
1602 kCanReturnSkipInsteadOfSkipping);
1603 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1604 sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
1605 sink_->PutInt(skip, "SkipB4ExternalRef");
1606 Address target = rinfo->target_address();
1607 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1608 bytes_processed_so_far_ += rinfo->target_address_size();
1612 void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1613 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1614 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1616 int skip = OutputRawData(rinfo->target_address_address(),
1617 kCanReturnSkipInsteadOfSkipping);
1618 Code* object = Code::GetCodeFromTargetAddress(rinfo->target_address());
1619 serializer_->SerializeObject(object, kFromCode, kInnerPointer, skip);
1620 bytes_processed_so_far_ += rinfo->target_address_size();
1624 void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1625 int skip = OutputRawData(entry_address, kCanReturnSkipInsteadOfSkipping);
1626 Code* object = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1627 serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
1628 bytes_processed_so_far_ += kPointerSize;
1632 void Serializer::ObjectSerializer::VisitCell(RelocInfo* rinfo) {
1633 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1634 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1636 int skip = OutputRawData(rinfo->pc(), kCanReturnSkipInsteadOfSkipping);
1637 Cell* object = Cell::cast(rinfo->target_cell());
1638 serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
1642 void Serializer::ObjectSerializer::VisitExternalAsciiString(
1643 v8::String::ExternalAsciiStringResource** resource_pointer) {
1644 Address references_start = reinterpret_cast<Address>(resource_pointer);
1645 OutputRawData(references_start);
1646 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1648 serializer_->isolate()->heap()->natives_source_cache()->get(i);
1649 if (!source->IsUndefined()) {
1650 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1651 typedef v8::String::ExternalAsciiStringResource Resource;
1652 const Resource* resource = string->resource();
1653 if (resource == *resource_pointer) {
1654 sink_->Put(kNativesStringResource, "NativesStringResource");
1655 sink_->PutSection(i, "NativesStringResourceEnd");
1656 bytes_processed_so_far_ += sizeof(resource);
1661 // One of the strings in the natives cache should match the resource. We
1662 // can't serialize any other kinds of external strings.
1667 static Code* CloneCodeObject(HeapObject* code) {
1668 Address copy = new byte[code->Size()];
1669 MemCopy(copy, code->address(), code->Size());
1670 return Code::cast(HeapObject::FromAddress(copy));
1674 static void WipeOutRelocations(Code* code) {
1676 RelocInfo::kCodeTargetMask |
1677 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
1678 RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE) |
1679 RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY);
1680 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
1681 if (!(FLAG_enable_ool_constant_pool && it.rinfo()->IsInConstantPool())) {
1682 it.rinfo()->WipeOut();
1688 int Serializer::ObjectSerializer::OutputRawData(
1689 Address up_to, Serializer::ObjectSerializer::ReturnSkip return_skip) {
1690 Address object_start = object_->address();
1691 int base = bytes_processed_so_far_;
1692 int up_to_offset = static_cast<int>(up_to - object_start);
1693 int to_skip = up_to_offset - bytes_processed_so_far_;
1694 int bytes_to_output = to_skip;
1695 bytes_processed_so_far_ += to_skip;
1696 // This assert will fail if the reloc info gives us the target_address_address
1697 // locations in a non-ascending order. Luckily that doesn't happen.
1698 DCHECK(to_skip >= 0);
1699 bool outputting_code = false;
1700 if (to_skip != 0 && code_object_ && !code_has_been_output_) {
1701 // Output the code all at once and fix later.
1702 bytes_to_output = object_->Size() + to_skip - bytes_processed_so_far_;
1703 outputting_code = true;
1704 code_has_been_output_ = true;
1706 if (bytes_to_output != 0 &&
1707 (!code_object_ || outputting_code)) {
1708 #define RAW_CASE(index) \
1709 if (!outputting_code && bytes_to_output == index * kPointerSize && \
1710 index * kPointerSize == to_skip) { \
1711 sink_->PutSection(kRawData + index, "RawDataFixed"); \
1712 to_skip = 0; /* This insn already skips. */ \
1714 COMMON_RAW_LENGTHS(RAW_CASE)
1717 // We always end up here if we are outputting the code of a code object.
1718 sink_->Put(kRawData, "RawData");
1719 sink_->PutInt(bytes_to_output, "length");
1722 // To make snapshots reproducible, we need to wipe out all pointers in code.
1724 Code* code = CloneCodeObject(object_);
1725 WipeOutRelocations(code);
1726 // We need to wipe out the header fields *after* wiping out the
1727 // relocations, because some of these fields are needed for the latter.
1728 code->WipeOutHeader();
1729 object_start = code->address();
1732 const char* description = code_object_ ? "Code" : "Byte";
1733 for (int i = 0; i < bytes_to_output; i++) {
1734 sink_->PutSection(object_start[base + i], description);
1736 if (code_object_) delete[] object_start;
1738 if (to_skip != 0 && return_skip == kIgnoringReturn) {
1739 sink_->Put(kSkip, "Skip");
1740 sink_->PutInt(to_skip, "SkipDistance");
1747 int Serializer::SpaceOfObject(HeapObject* object) {
1748 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1749 AllocationSpace s = static_cast<AllocationSpace>(i);
1750 if (object->GetHeap()->InSpace(object, s)) {
1751 DCHECK(i < kNumberOfSpaces);
1760 int Serializer::Allocate(int space, int size) {
1761 CHECK(space >= 0 && space < kNumberOfSpaces);
1762 int allocation_address = fullness_[space];
1763 fullness_[space] = allocation_address + size;
1764 return allocation_address;
1768 int Serializer::SpaceAreaSize(int space) {
1769 if (space == CODE_SPACE) {
1770 return isolate_->memory_allocator()->CodePageAreaSize();
1772 return Page::kPageSize - Page::kObjectStartOffset;
1777 void Serializer::Pad() {
1778 // The non-branching GetInt will read up to 3 bytes too far, so we need
1779 // to pad the snapshot to make sure we don't read over the end.
1780 for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) {
1781 sink_->Put(kNop, "Padding");
1786 void Serializer::InitializeCodeAddressMap() {
1787 isolate_->InitializeLoggingAndCounters();
1788 code_address_map_ = new CodeAddressMap(isolate_);
1792 ScriptData* CodeSerializer::Serialize(Isolate* isolate,
1793 Handle<SharedFunctionInfo> info,
1794 Handle<String> source) {
1795 // Serialize code object.
1797 ListSnapshotSink list_sink(&payload);
1798 CodeSerializer cs(isolate, &list_sink, *source);
1799 DisallowHeapAllocation no_gc;
1800 Object** location = Handle<Object>::cast(info).location();
1801 cs.VisitPointer(location);
1804 SerializedCodeData data(&payload, &cs);
1805 return data.GetScriptData();
1809 void CodeSerializer::SerializeObject(Object* o, HowToCode how_to_code,
1810 WhereToPoint where_to_point, int skip) {
1811 CHECK(o->IsHeapObject());
1812 HeapObject* heap_object = HeapObject::cast(o);
1814 // The code-caches link to context-specific code objects, which
1815 // the startup and context serializes cannot currently handle.
1816 DCHECK(!heap_object->IsMap() ||
1817 Map::cast(heap_object)->code_cache() ==
1818 heap_object->GetHeap()->empty_fixed_array());
1821 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1822 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1826 // TODO(yangguo) wire up stubs from stub cache.
1827 // TODO(yangguo) wire up global object.
1828 // TODO(yangguo) We cannot deal with different hash seeds yet.
1829 DCHECK(!heap_object->IsHashTable());
1831 if (address_mapper_.IsMapped(heap_object)) {
1832 int space = SpaceOfObject(heap_object);
1833 int address = address_mapper_.MappedTo(heap_object);
1834 SerializeReferenceToPreviousObject(space, address, how_to_code,
1835 where_to_point, skip);
1839 if (heap_object->IsCode()) {
1840 Code* code_object = Code::cast(heap_object);
1841 if (code_object->kind() == Code::BUILTIN) {
1842 SerializeBuiltin(code_object, how_to_code, where_to_point, skip);
1845 // TODO(yangguo) figure out whether other code kinds can be handled smarter.
1848 if (heap_object == source_) {
1849 SerializeSourceObject(how_to_code, where_to_point, skip);
1853 if (heap_object->IsScript()) {
1854 // The wrapper cache uses a Foreign object to point to a global handle.
1855 // However, the object visitor expects foreign objects to point to external
1856 // references. Clear the cache to avoid this issue.
1857 Script::cast(heap_object)->ClearWrapperCache();
1861 sink_->Put(kSkip, "SkipFromSerializeObject");
1862 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1864 // Object has not yet been serialized. Serialize it here.
1865 ObjectSerializer serializer(this, heap_object, sink_, how_to_code,
1867 serializer.Serialize();
1871 void CodeSerializer::SerializeBuiltin(Code* builtin, HowToCode how_to_code,
1872 WhereToPoint where_to_point, int skip) {
1874 sink_->Put(kSkip, "SkipFromSerializeBuiltin");
1875 sink_->PutInt(skip, "SkipDistanceFromSerializeBuiltin");
1878 DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) ||
1879 (how_to_code == kPlain && where_to_point == kInnerPointer) ||
1880 (how_to_code == kFromCode && where_to_point == kInnerPointer));
1881 int builtin_index = builtin->builtin_index();
1882 DCHECK_LT(builtin_index, Builtins::builtin_count);
1883 DCHECK_LE(0, builtin_index);
1884 sink_->Put(kBuiltin + how_to_code + where_to_point, "Builtin");
1885 sink_->PutInt(builtin_index, "builtin_index");
1889 void CodeSerializer::SerializeSourceObject(HowToCode how_to_code,
1890 WhereToPoint where_to_point,
1893 sink_->Put(kSkip, "SkipFromSerializeSourceObject");
1894 sink_->PutInt(skip, "SkipDistanceFromSerializeSourceObject");
1897 DCHECK(how_to_code == kPlain && where_to_point == kStartOfObject);
1898 sink_->Put(kAttachedReference + how_to_code + where_to_point, "Source");
1899 sink_->PutInt(kSourceObjectIndex, "kSourceObjectIndex");
1903 Handle<SharedFunctionInfo> CodeSerializer::Deserialize(Isolate* isolate,
1905 Handle<String> source) {
1906 base::ElapsedTimer timer;
1907 if (FLAG_profile_deserialization) timer.Start();
1908 SerializedCodeData scd(data, *source);
1909 SnapshotByteSource payload(scd.Payload(), scd.PayloadLength());
1910 Deserializer deserializer(&payload);
1911 STATIC_ASSERT(NEW_SPACE == 0);
1912 for (int i = NEW_SPACE; i <= PROPERTY_CELL_SPACE; i++) {
1913 deserializer.set_reservation(i, scd.GetReservation(i));
1916 // Prepare and register list of attached objects.
1917 Vector<Object*> attached_objects = Vector<Object*>::New(1);
1918 attached_objects[kSourceObjectIndex] = *source;
1919 deserializer.SetAttachedObjects(&attached_objects);
1922 deserializer.DeserializePartial(isolate, &root);
1923 deserializer.FlushICacheForNewCodeObjects();
1924 if (FLAG_profile_deserialization) {
1925 double ms = timer.Elapsed().InMillisecondsF();
1926 int length = data->length();
1927 PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms);
1929 return Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(root), isolate);
1933 SerializedCodeData::SerializedCodeData(List<byte>* payload, CodeSerializer* cs)
1934 : owns_script_data_(true) {
1935 DisallowHeapAllocation no_gc;
1936 int data_length = payload->length() + kHeaderEntries * kIntSize;
1937 byte* data = NewArray<byte>(data_length);
1938 DCHECK(IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment));
1939 CopyBytes(data + kHeaderEntries * kIntSize, payload->begin(),
1940 static_cast<size_t>(payload->length()));
1941 script_data_ = new ScriptData(data, data_length);
1942 script_data_->AcquireDataOwnership();
1943 SetHeaderValue(kCheckSumOffset, CheckSum(cs->source()));
1944 STATIC_ASSERT(NEW_SPACE == 0);
1945 for (int i = NEW_SPACE; i <= PROPERTY_CELL_SPACE; i++) {
1946 SetHeaderValue(kReservationsOffset + i, cs->CurrentAllocationAddress(i));
1951 bool SerializedCodeData::IsSane(String* source) {
1952 return GetHeaderValue(kCheckSumOffset) == CheckSum(source) &&
1953 PayloadLength() >= SharedFunctionInfo::kSize;
1957 int SerializedCodeData::CheckSum(String* string) {
1958 int checksum = Version::Hash();
1960 uint32_t seed = static_cast<uint32_t>(checksum);
1961 checksum = static_cast<int>(IteratingStringHasher::Hash(string, seed));
1965 } } // namespace v8::internal