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.
9 #include "bootstrapper.h"
10 #include "deoptimizer.h"
11 #include "execution.h"
12 #include "global-handles.h"
17 #include "serialize.h"
19 #include "stub-cache.h"
20 #include "v8threads.h"
26 // -----------------------------------------------------------------------------
27 // Coding of external references.
29 // The encoding of an external reference. The type is in the high word.
30 // The id is in the low word.
31 static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
32 return static_cast<uint32_t>(type) << 16 | id;
36 static int* GetInternalPointer(StatsCounter* counter) {
37 // All counters refer to dummy_counter, if deserializing happens without
38 // setting up counters.
39 static int dummy_counter = 0;
40 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
44 ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
45 ExternalReferenceTable* external_reference_table =
46 isolate->external_reference_table();
47 if (external_reference_table == NULL) {
48 external_reference_table = new ExternalReferenceTable(isolate);
49 isolate->set_external_reference_table(external_reference_table);
51 return external_reference_table;
55 void ExternalReferenceTable::AddFromId(TypeCode type,
62 ExternalReference ref(static_cast<Builtins::CFunctionId>(id), isolate);
63 address = ref.address();
67 ExternalReference ref(static_cast<Builtins::Name>(id), isolate);
68 address = ref.address();
71 case RUNTIME_FUNCTION: {
72 ExternalReference ref(static_cast<Runtime::FunctionId>(id), isolate);
73 address = ref.address();
77 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)),
79 address = ref.address();
86 Add(address, type, id, name);
90 void ExternalReferenceTable::Add(Address address,
94 ASSERT_NE(NULL, address);
95 ExternalReferenceEntry entry;
96 entry.address = address;
97 entry.code = EncodeExternal(type, id);
99 ASSERT_NE(0, entry.code);
101 if (id > max_id_[type]) max_id_[type] = id;
105 void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
106 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
107 max_id_[type_code] = 0;
110 // The following populates all of the different type of external references
111 // into the ExternalReferenceTable.
113 // NOTE: This function was originally 100k of code. It has since been
114 // rewritten to be mostly table driven, as the callback macro style tends to
115 // very easily cause code bloat. Please be careful in the future when adding
118 struct RefTableEntry {
124 static const RefTableEntry ref_table[] = {
126 #define DEF_ENTRY_C(name, ignored) \
128 Builtins::c_##name, \
129 "Builtins::" #name },
131 BUILTIN_LIST_C(DEF_ENTRY_C)
134 #define DEF_ENTRY_C(name, ignored) \
137 "Builtins::" #name },
138 #define DEF_ENTRY_A(name, kind, state, extra) DEF_ENTRY_C(name, ignored)
140 BUILTIN_LIST_C(DEF_ENTRY_C)
141 BUILTIN_LIST_A(DEF_ENTRY_A)
142 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
147 #define RUNTIME_ENTRY(name, nargs, ressize) \
148 { RUNTIME_FUNCTION, \
152 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
155 #define RUNTIME_HIDDEN_ENTRY(name, nargs, ressize) \
156 { RUNTIME_FUNCTION, \
157 Runtime::kHidden##name, \
158 "Runtime::Hidden" #name },
160 RUNTIME_HIDDEN_FUNCTION_LIST(RUNTIME_HIDDEN_ENTRY)
161 #undef RUNTIME_HIDDEN_ENTRY
163 #define INLINE_OPTIMIZED_ENTRY(name, nargs, ressize) \
164 { RUNTIME_FUNCTION, \
165 Runtime::kInlineOptimized##name, \
168 INLINE_OPTIMIZED_FUNCTION_LIST(INLINE_OPTIMIZED_ENTRY)
169 #undef INLINE_OPTIMIZED_ENTRY
172 #define IC_ENTRY(name) \
177 IC_UTIL_LIST(IC_ENTRY)
179 }; // end of ref_table[].
181 for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
182 AddFromId(ref_table[i].type,
189 Add(Debug_Address(Debug::k_after_break_target_address).address(isolate),
191 Debug::k_after_break_target_address << kDebugIdShift,
192 "Debug::after_break_target_address()");
193 Add(Debug_Address(Debug::k_restarter_frame_function_pointer).address(isolate),
195 Debug::k_restarter_frame_function_pointer << kDebugIdShift,
196 "Debug::restarter_frame_function_pointer_address()");
199 struct StatsRefTableEntry {
200 StatsCounter* (Counters::*counter)();
205 const StatsRefTableEntry stats_ref_table[] = {
206 #define COUNTER_ENTRY(name, caption) \
208 Counters::k_##name, \
209 "Counters::" #name },
211 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
212 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
214 }; // end of stats_ref_table[].
216 Counters* counters = isolate->counters();
217 for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
218 Add(reinterpret_cast<Address>(GetInternalPointer(
219 (counters->*(stats_ref_table[i].counter))())),
221 stats_ref_table[i].id,
222 stats_ref_table[i].name);
227 const char* AddressNames[] = {
228 #define BUILD_NAME_LITERAL(CamelName, hacker_name) \
229 "Isolate::" #hacker_name "_address",
230 FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL)
232 #undef BUILD_NAME_LITERAL
235 for (uint16_t i = 0; i < Isolate::kIsolateAddressCount; ++i) {
236 Add(isolate->get_address_from_id((Isolate::AddressId)i),
237 TOP_ADDRESS, i, AddressNames[i]);
241 #define ACCESSOR_INFO_DECLARATION(name) \
242 Add(FUNCTION_ADDR(&Accessors::name##Getter), \
244 Accessors::k##name##Getter, \
245 "Accessors::" #name "Getter"); \
246 Add(FUNCTION_ADDR(&Accessors::name##Setter), \
248 Accessors::k##name##Setter, \
249 "Accessors::" #name "Setter");
250 ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
251 #undef ACCESSOR_INFO_DECLARATION
253 StubCache* stub_cache = isolate->stub_cache();
256 Add(stub_cache->key_reference(StubCache::kPrimary).address(),
259 "StubCache::primary_->key");
260 Add(stub_cache->value_reference(StubCache::kPrimary).address(),
263 "StubCache::primary_->value");
264 Add(stub_cache->map_reference(StubCache::kPrimary).address(),
267 "StubCache::primary_->map");
268 Add(stub_cache->key_reference(StubCache::kSecondary).address(),
271 "StubCache::secondary_->key");
272 Add(stub_cache->value_reference(StubCache::kSecondary).address(),
275 "StubCache::secondary_->value");
276 Add(stub_cache->map_reference(StubCache::kSecondary).address(),
279 "StubCache::secondary_->map");
282 Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
285 "HandleScope::DeleteExtensions");
286 Add(ExternalReference::
287 incremental_marking_record_write_function(isolate).address(),
290 "IncrementalMarking::RecordWrite");
291 Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
294 "StoreBuffer::StoreBufferOverflow");
297 Add(ExternalReference::roots_array_start(isolate).address(),
300 "Heap::roots_array_start()");
301 Add(ExternalReference::address_of_stack_limit(isolate).address(),
304 "StackGuard::address_of_jslimit()");
305 Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
308 "StackGuard::address_of_real_jslimit()");
309 #ifndef V8_INTERPRETED_REGEXP
310 Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
313 "RegExpStack::limit_address()");
314 Add(ExternalReference::address_of_regexp_stack_memory_address(
318 "RegExpStack::memory_address()");
319 Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
322 "RegExpStack::memory_size()");
323 Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
326 "OffsetsVector::static_offsets_vector");
327 #endif // V8_INTERPRETED_REGEXP
328 Add(ExternalReference::new_space_start(isolate).address(),
331 "Heap::NewSpaceStart()");
332 Add(ExternalReference::new_space_mask(isolate).address(),
335 "Heap::NewSpaceMask()");
336 Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
339 "Heap::NewSpaceAllocationLimitAddress()");
340 Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
343 "Heap::NewSpaceAllocationTopAddress()");
344 Add(ExternalReference::debug_break(isolate).address(),
348 Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
351 "Debug::step_in_fp_addr()");
352 Add(ExternalReference::mod_two_doubles_operation(isolate).address(),
356 #ifndef V8_INTERPRETED_REGEXP
357 Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
360 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
361 Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
364 "RegExpMacroAssembler*::CheckStackGuardState()");
365 Add(ExternalReference::re_grow_stack(isolate).address(),
368 "NativeRegExpMacroAssembler::GrowStack()");
369 Add(ExternalReference::re_word_character_map().address(),
372 "NativeRegExpMacroAssembler::word_character_map");
373 #endif // V8_INTERPRETED_REGEXP
374 // Keyed lookup cache.
375 Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
378 "KeyedLookupCache::keys()");
379 Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
382 "KeyedLookupCache::field_offsets()");
383 Add(ExternalReference::handle_scope_next_address(isolate).address(),
386 "HandleScope::next");
387 Add(ExternalReference::handle_scope_limit_address(isolate).address(),
390 "HandleScope::limit");
391 Add(ExternalReference::handle_scope_level_address(isolate).address(),
394 "HandleScope::level");
395 Add(ExternalReference::new_deoptimizer_function(isolate).address(),
398 "Deoptimizer::New()");
399 Add(ExternalReference::compute_output_frames_function(isolate).address(),
402 "Deoptimizer::ComputeOutputFrames()");
403 Add(ExternalReference::address_of_min_int().address(),
406 "LDoubleConstant::min_int");
407 Add(ExternalReference::address_of_one_half().address(),
410 "LDoubleConstant::one_half");
411 Add(ExternalReference::isolate_address(isolate).address(),
415 Add(ExternalReference::address_of_minus_zero().address(),
418 "LDoubleConstant::minus_zero");
419 Add(ExternalReference::address_of_negative_infinity().address(),
422 "LDoubleConstant::negative_infinity");
423 Add(ExternalReference::power_double_double_function(isolate).address(),
426 "power_double_double_function");
427 Add(ExternalReference::power_double_int_function(isolate).address(),
430 "power_double_int_function");
431 Add(ExternalReference::store_buffer_top(isolate).address(),
435 Add(ExternalReference::address_of_canonical_non_hole_nan().address(),
439 Add(ExternalReference::address_of_the_hole_nan().address(),
443 Add(ExternalReference::get_date_field_function(isolate).address(),
447 Add(ExternalReference::date_cache_stamp(isolate).address(),
451 Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
454 "address_of_pending_message_obj");
455 Add(ExternalReference::address_of_has_pending_message(isolate).address(),
458 "address_of_has_pending_message");
459 Add(ExternalReference::address_of_pending_message_script(isolate).address(),
462 "pending_message_script");
463 Add(ExternalReference::get_make_code_young_function(isolate).address(),
466 "Code::MakeCodeYoung");
467 Add(ExternalReference::cpu_features().address(),
471 Add(ExternalReference(Runtime::kHiddenAllocateInNewSpace, isolate).address(),
474 "Runtime::AllocateInNewSpace");
475 Add(ExternalReference(
476 Runtime::kHiddenAllocateInTargetSpace, isolate).address(),
479 "Runtime::AllocateInTargetSpace");
480 Add(ExternalReference::old_pointer_space_allocation_top_address(
484 "Heap::OldPointerSpaceAllocationTopAddress");
485 Add(ExternalReference::old_pointer_space_allocation_limit_address(
489 "Heap::OldPointerSpaceAllocationLimitAddress");
490 Add(ExternalReference::old_data_space_allocation_top_address(
494 "Heap::OldDataSpaceAllocationTopAddress");
495 Add(ExternalReference::old_data_space_allocation_limit_address(
499 "Heap::OldDataSpaceAllocationLimitAddress");
500 Add(ExternalReference::new_space_high_promotion_mode_active_address(isolate).
504 "Heap::NewSpaceAllocationLimitAddress");
505 Add(ExternalReference::allocation_sites_list_address(isolate).address(),
508 "Heap::allocation_sites_list_address()");
509 Add(ExternalReference::address_of_uint32_bias().address(),
513 Add(ExternalReference::get_mark_code_as_executed_function(isolate).address(),
516 "Code::MarkCodeAsExecuted");
518 Add(ExternalReference::is_profiling_address(isolate).address(),
521 "CpuProfiler::is_profiling");
523 Add(ExternalReference::scheduled_exception_address(isolate).address(),
526 "Isolate::scheduled_exception");
528 Add(ExternalReference::invoke_function_callback(isolate).address(),
531 "InvokeFunctionCallback");
533 Add(ExternalReference::invoke_accessor_getter_callback(isolate).address(),
536 "InvokeAccessorGetterCallback");
538 // Add a small set of deopt entry addresses to encoder without generating the
539 // deopt table code, which isn't possible at deserialization time.
540 HandleScope scope(isolate);
541 for (int entry = 0; entry < kDeoptTableSerializeEntryCount; ++entry) {
542 Address address = Deoptimizer::GetDeoptimizationEntry(
546 Deoptimizer::CALCULATE_ENTRY_ADDRESS);
547 Add(address, LAZY_DEOPTIMIZATION, entry, "lazy_deopt");
552 ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate)
553 : encodings_(HashMap::PointersMatch),
555 ExternalReferenceTable* external_references =
556 ExternalReferenceTable::instance(isolate_);
557 for (int i = 0; i < external_references->size(); ++i) {
558 Put(external_references->address(i), i);
563 uint32_t ExternalReferenceEncoder::Encode(Address key) const {
564 int index = IndexOf(key);
565 ASSERT(key == NULL || index >= 0);
567 ExternalReferenceTable::instance(isolate_)->code(index) : 0;
571 const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
572 int index = IndexOf(key);
574 ExternalReferenceTable::instance(isolate_)->name(index) : NULL;
578 int ExternalReferenceEncoder::IndexOf(Address key) const {
579 if (key == NULL) return -1;
580 HashMap::Entry* entry =
581 const_cast<HashMap&>(encodings_).Lookup(key, Hash(key), false);
584 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
588 void ExternalReferenceEncoder::Put(Address key, int index) {
589 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
590 entry->value = reinterpret_cast<void*>(index);
594 ExternalReferenceDecoder::ExternalReferenceDecoder(Isolate* isolate)
595 : encodings_(NewArray<Address*>(kTypeCodeCount)),
597 ExternalReferenceTable* external_references =
598 ExternalReferenceTable::instance(isolate_);
599 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
600 int max = external_references->max_id(type) + 1;
601 encodings_[type] = NewArray<Address>(max + 1);
603 for (int i = 0; i < external_references->size(); ++i) {
604 Put(external_references->code(i), external_references->address(i));
609 ExternalReferenceDecoder::~ExternalReferenceDecoder() {
610 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
611 DeleteArray(encodings_[type]);
613 DeleteArray(encodings_);
616 AtomicWord Serializer::serialization_state_ = SERIALIZER_STATE_UNINITIALIZED;
618 class CodeAddressMap: public CodeEventLogger {
620 explicit CodeAddressMap(Isolate* isolate)
621 : isolate_(isolate) {
622 isolate->logger()->addCodeEventListener(this);
625 virtual ~CodeAddressMap() {
626 isolate_->logger()->removeCodeEventListener(this);
629 virtual void CodeMoveEvent(Address from, Address to) {
630 address_to_name_map_.Move(from, to);
633 virtual void CodeDeleteEvent(Address from) {
634 address_to_name_map_.Remove(from);
637 const char* Lookup(Address address) {
638 return address_to_name_map_.Lookup(address);
644 NameMap() : impl_(HashMap::PointersMatch) {}
647 for (HashMap::Entry* p = impl_.Start(); p != NULL; p = impl_.Next(p)) {
648 DeleteArray(static_cast<const char*>(p->value));
652 void Insert(Address code_address, const char* name, int name_size) {
653 HashMap::Entry* entry = FindOrCreateEntry(code_address);
654 if (entry->value == NULL) {
655 entry->value = CopyName(name, name_size);
659 const char* Lookup(Address code_address) {
660 HashMap::Entry* entry = FindEntry(code_address);
661 return (entry != NULL) ? static_cast<const char*>(entry->value) : NULL;
664 void Remove(Address code_address) {
665 HashMap::Entry* entry = FindEntry(code_address);
667 DeleteArray(static_cast<char*>(entry->value));
672 void Move(Address from, Address to) {
673 if (from == to) return;
674 HashMap::Entry* from_entry = FindEntry(from);
675 ASSERT(from_entry != NULL);
676 void* value = from_entry->value;
677 RemoveEntry(from_entry);
678 HashMap::Entry* to_entry = FindOrCreateEntry(to);
679 ASSERT(to_entry->value == NULL);
680 to_entry->value = value;
684 static char* CopyName(const char* name, int name_size) {
685 char* result = NewArray<char>(name_size + 1);
686 for (int i = 0; i < name_size; ++i) {
688 if (c == '\0') c = ' ';
691 result[name_size] = '\0';
695 HashMap::Entry* FindOrCreateEntry(Address code_address) {
696 return impl_.Lookup(code_address, ComputePointerHash(code_address), true);
699 HashMap::Entry* FindEntry(Address code_address) {
700 return impl_.Lookup(code_address,
701 ComputePointerHash(code_address),
705 void RemoveEntry(HashMap::Entry* entry) {
706 impl_.Remove(entry->key, entry->hash);
711 DISALLOW_COPY_AND_ASSIGN(NameMap);
714 virtual void LogRecordedBuffer(Code* code,
718 address_to_name_map_.Insert(code->address(), name, length);
721 NameMap address_to_name_map_;
726 CodeAddressMap* Serializer::code_address_map_ = NULL;
729 void Serializer::RequestEnable(Isolate* isolate) {
730 isolate->InitializeLoggingAndCounters();
731 code_address_map_ = new CodeAddressMap(isolate);
735 void Serializer::InitializeOncePerProcess() {
736 // InitializeOncePerProcess is called by V8::InitializeOncePerProcess, a
737 // method guaranteed to be called only once in a process lifetime.
738 // serialization_state_ is read by many threads, hence the use of
739 // Atomic primitives. Here, we don't need a barrier or mutex to
740 // write it because V8 initialization is done by one thread, and gates
741 // all reads of serialization_state_.
742 ASSERT(NoBarrier_Load(&serialization_state_) ==
743 SERIALIZER_STATE_UNINITIALIZED);
744 SerializationState state = code_address_map_
745 ? SERIALIZER_STATE_ENABLED
746 : SERIALIZER_STATE_DISABLED;
747 NoBarrier_Store(&serialization_state_, state);
751 void Serializer::TearDown() {
752 // TearDown is called by V8::TearDown() for the default isolate. It's safe
753 // to shut down the serializer by that point. Just to be safe, we restore
754 // serialization_state_ to uninitialized.
755 ASSERT(NoBarrier_Load(&serialization_state_) !=
756 SERIALIZER_STATE_UNINITIALIZED);
757 if (code_address_map_) {
758 ASSERT(NoBarrier_Load(&serialization_state_) ==
759 SERIALIZER_STATE_ENABLED);
760 delete code_address_map_;
761 code_address_map_ = NULL;
764 NoBarrier_Store(&serialization_state_, SERIALIZER_STATE_UNINITIALIZED);
768 Deserializer::Deserializer(SnapshotByteSource* source)
771 external_reference_decoder_(NULL) {
772 for (int i = 0; i < LAST_SPACE + 1; i++) {
773 reservations_[i] = kUninitializedReservation;
778 void Deserializer::FlushICacheForNewCodeObjects() {
779 PageIterator it(isolate_->heap()->code_space());
780 while (it.has_next()) {
782 CPU::FlushICache(p->area_start(), p->area_end() - p->area_start());
787 void Deserializer::Deserialize(Isolate* isolate) {
789 ASSERT(isolate_ != NULL);
790 isolate_->heap()->ReserveSpace(reservations_, &high_water_[0]);
791 // No active threads.
792 ASSERT_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse());
793 // No active handles.
794 ASSERT(isolate_->handle_scope_implementer()->blocks()->is_empty());
795 ASSERT_EQ(NULL, external_reference_decoder_);
796 external_reference_decoder_ = new ExternalReferenceDecoder(isolate);
797 isolate_->heap()->IterateSmiRoots(this);
798 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
799 isolate_->heap()->RepairFreeListsAfterBoot();
800 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
802 isolate_->heap()->set_native_contexts_list(
803 isolate_->heap()->undefined_value());
804 isolate_->heap()->set_array_buffers_list(
805 isolate_->heap()->undefined_value());
807 // The allocation site list is build during root iteration, but if no sites
808 // were encountered then it needs to be initialized to undefined.
809 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
810 isolate_->heap()->set_allocation_sites_list(
811 isolate_->heap()->undefined_value());
814 isolate_->heap()->InitializeWeakObjectToCodeTable();
816 // Update data pointers to the external strings containing natives sources.
817 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
818 Object* source = isolate_->heap()->natives_source_cache()->get(i);
819 if (!source->IsUndefined()) {
820 ExternalAsciiString::cast(source)->update_data_cache();
824 FlushICacheForNewCodeObjects();
826 // Issue code events for newly deserialized code objects.
827 LOG_CODE_EVENT(isolate_, LogCodeObjects());
828 LOG_CODE_EVENT(isolate_, LogCompiledFunctions());
832 void Deserializer::DeserializePartial(Isolate* isolate, Object** root) {
834 for (int i = NEW_SPACE; i < kNumberOfSpaces; i++) {
835 ASSERT(reservations_[i] != kUninitializedReservation);
837 isolate_->heap()->ReserveSpace(reservations_, &high_water_[0]);
838 if (external_reference_decoder_ == NULL) {
839 external_reference_decoder_ = new ExternalReferenceDecoder(isolate);
842 // Keep track of the code space start and end pointers in case new
843 // code objects were unserialized
844 OldSpace* code_space = isolate_->heap()->code_space();
845 Address start_address = code_space->top();
848 // There's no code deserialized here. If this assert fires
849 // then that's changed and logging should be added to notify
850 // the profiler et al of the new code.
851 CHECK_EQ(start_address, code_space->top());
855 Deserializer::~Deserializer() {
856 // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed.
857 // ASSERT(source_->AtEOF());
858 if (external_reference_decoder_) {
859 delete external_reference_decoder_;
860 external_reference_decoder_ = NULL;
865 // This is called on the roots. It is the driver of the deserialization
866 // process. It is also called on the body of each function.
867 void Deserializer::VisitPointers(Object** start, Object** end) {
868 // The space must be new space. Any other space would cause ReadChunk to try
869 // to update the remembered using NULL as the address.
870 ReadChunk(start, end, NEW_SPACE, NULL);
874 void Deserializer::RelinkAllocationSite(AllocationSite* site) {
875 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
876 site->set_weak_next(isolate_->heap()->undefined_value());
878 site->set_weak_next(isolate_->heap()->allocation_sites_list());
880 isolate_->heap()->set_allocation_sites_list(site);
884 // This routine writes the new object into the pointer provided and then
885 // returns true if the new object was in young space and false otherwise.
886 // The reason for this strange interface is that otherwise the object is
887 // written very late, which means the FreeSpace map is not set up by the
888 // time we need to use it to mark the space at the end of a page free.
889 void Deserializer::ReadObject(int space_number,
890 Object** write_back) {
891 int size = source_->GetInt() << kObjectAlignmentBits;
892 Address address = Allocate(space_number, size);
893 HeapObject* obj = HeapObject::FromAddress(address);
895 Object** current = reinterpret_cast<Object**>(address);
896 Object** limit = current + (size >> kPointerSizeLog2);
897 if (FLAG_log_snapshot_positions) {
898 LOG(isolate_, SnapshotPositionEvent(address, source_->position()));
900 ReadChunk(current, limit, space_number, address);
902 // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
903 // as a (weak) root. If this root is relocated correctly,
904 // RelinkAllocationSite() isn't necessary.
905 if (obj->IsAllocationSite()) {
906 RelinkAllocationSite(AllocationSite::cast(obj));
910 bool is_codespace = (space_number == CODE_SPACE);
911 ASSERT(obj->IsCode() == is_codespace);
915 void Deserializer::ReadChunk(Object** current,
918 Address current_object_address) {
919 Isolate* const isolate = isolate_;
920 // Write barrier support costs around 1% in startup time. In fact there
921 // are no new space objects in current boot snapshots, so it's not needed,
922 // but that may change.
923 bool write_barrier_needed = (current_object_address != NULL &&
924 source_space != NEW_SPACE &&
925 source_space != CELL_SPACE &&
926 source_space != PROPERTY_CELL_SPACE &&
927 source_space != CODE_SPACE &&
928 source_space != OLD_DATA_SPACE);
929 while (current < limit) {
930 int data = source_->Get();
932 #define CASE_STATEMENT(where, how, within, space_number) \
933 case where + how + within + space_number: \
934 ASSERT((where & ~kPointedToMask) == 0); \
935 ASSERT((how & ~kHowToCodeMask) == 0); \
936 ASSERT((within & ~kWhereToPointMask) == 0); \
937 ASSERT((space_number & ~kSpaceMask) == 0);
939 #define CASE_BODY(where, how, within, space_number_if_any) \
941 bool emit_write_barrier = false; \
942 bool current_was_incremented = false; \
943 int space_number = space_number_if_any == kAnyOldSpace ? \
944 (data & kSpaceMask) : space_number_if_any; \
945 if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
946 ReadObject(space_number, current); \
947 emit_write_barrier = (space_number == NEW_SPACE); \
949 Object* new_object = NULL; /* May not be a real Object pointer. */ \
950 if (where == kNewObject) { \
951 ReadObject(space_number, &new_object); \
952 } else if (where == kRootArray) { \
953 int root_id = source_->GetInt(); \
954 new_object = isolate->heap()->roots_array_start()[root_id]; \
955 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
956 } else if (where == kPartialSnapshotCache) { \
957 int cache_index = source_->GetInt(); \
958 new_object = isolate->serialize_partial_snapshot_cache() \
960 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
961 } else if (where == kExternalReference) { \
962 int skip = source_->GetInt(); \
963 current = reinterpret_cast<Object**>(reinterpret_cast<Address>( \
965 int reference_id = source_->GetInt(); \
966 Address address = external_reference_decoder_-> \
967 Decode(reference_id); \
968 new_object = reinterpret_cast<Object*>(address); \
969 } else if (where == kBackref) { \
970 emit_write_barrier = (space_number == NEW_SPACE); \
971 new_object = GetAddressFromEnd(data & kSpaceMask); \
973 ASSERT(where == kBackrefWithSkip); \
974 int skip = source_->GetInt(); \
975 current = reinterpret_cast<Object**>( \
976 reinterpret_cast<Address>(current) + skip); \
977 emit_write_barrier = (space_number == NEW_SPACE); \
978 new_object = GetAddressFromEnd(data & kSpaceMask); \
980 if (within == kInnerPointer) { \
981 if (space_number != CODE_SPACE || new_object->IsCode()) { \
982 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
983 new_object = reinterpret_cast<Object*>( \
984 new_code_object->instruction_start()); \
986 ASSERT(space_number == CODE_SPACE); \
987 Cell* cell = Cell::cast(new_object); \
988 new_object = reinterpret_cast<Object*>( \
989 cell->ValueAddress()); \
992 if (how == kFromCode) { \
993 Address location_of_branch_data = \
994 reinterpret_cast<Address>(current); \
995 Assembler::deserialization_set_special_target_at( \
996 location_of_branch_data, \
997 Code::cast(HeapObject::FromAddress(current_object_address)), \
998 reinterpret_cast<Address>(new_object)); \
999 location_of_branch_data += Assembler::kSpecialTargetSize; \
1000 current = reinterpret_cast<Object**>(location_of_branch_data); \
1001 current_was_incremented = true; \
1003 *current = new_object; \
1006 if (emit_write_barrier && write_barrier_needed) { \
1007 Address current_address = reinterpret_cast<Address>(current); \
1008 isolate->heap()->RecordWrite( \
1009 current_object_address, \
1010 static_cast<int>(current_address - current_object_address)); \
1012 if (!current_was_incremented) { \
1018 // This generates a case and a body for the new space (which has to do extra
1019 // write barrier handling) and handles the other spaces with 8 fall-through
1020 // cases and one body.
1021 #define ALL_SPACES(where, how, within) \
1022 CASE_STATEMENT(where, how, within, NEW_SPACE) \
1023 CASE_BODY(where, how, within, NEW_SPACE) \
1024 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
1025 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
1026 CASE_STATEMENT(where, how, within, CODE_SPACE) \
1027 CASE_STATEMENT(where, how, within, CELL_SPACE) \
1028 CASE_STATEMENT(where, how, within, PROPERTY_CELL_SPACE) \
1029 CASE_STATEMENT(where, how, within, MAP_SPACE) \
1030 CASE_BODY(where, how, within, kAnyOldSpace)
1032 #define FOUR_CASES(byte_code) \
1034 case byte_code + 1: \
1035 case byte_code + 2: \
1038 #define SIXTEEN_CASES(byte_code) \
1039 FOUR_CASES(byte_code) \
1040 FOUR_CASES(byte_code + 4) \
1041 FOUR_CASES(byte_code + 8) \
1042 FOUR_CASES(byte_code + 12)
1044 #define COMMON_RAW_LENGTHS(f) \
1077 // We generate 15 cases and bodies that process special tags that combine
1078 // the raw data tag and the length into one byte.
1079 #define RAW_CASE(index) \
1080 case kRawData + index: { \
1081 byte* raw_data_out = reinterpret_cast<byte*>(current); \
1082 source_->CopyRaw(raw_data_out, index * kPointerSize); \
1084 reinterpret_cast<Object**>(raw_data_out + index * kPointerSize); \
1087 COMMON_RAW_LENGTHS(RAW_CASE)
1090 // Deserialize a chunk of raw data that doesn't have one of the popular
1093 int size = source_->GetInt();
1094 byte* raw_data_out = reinterpret_cast<byte*>(current);
1095 source_->CopyRaw(raw_data_out, size);
1099 SIXTEEN_CASES(kRootArrayConstants + kNoSkipDistance)
1100 SIXTEEN_CASES(kRootArrayConstants + kNoSkipDistance + 16) {
1101 int root_id = RootArrayConstantFromByteCode(data);
1102 Object* object = isolate->heap()->roots_array_start()[root_id];
1103 ASSERT(!isolate->heap()->InNewSpace(object));
1104 *current++ = object;
1108 SIXTEEN_CASES(kRootArrayConstants + kHasSkipDistance)
1109 SIXTEEN_CASES(kRootArrayConstants + kHasSkipDistance + 16) {
1110 int root_id = RootArrayConstantFromByteCode(data);
1111 int skip = source_->GetInt();
1112 current = reinterpret_cast<Object**>(
1113 reinterpret_cast<intptr_t>(current) + skip);
1114 Object* object = isolate->heap()->roots_array_start()[root_id];
1115 ASSERT(!isolate->heap()->InNewSpace(object));
1116 *current++ = object;
1121 int repeats = source_->GetInt();
1122 Object* object = current[-1];
1123 ASSERT(!isolate->heap()->InNewSpace(object));
1124 for (int i = 0; i < repeats; i++) current[i] = object;
1129 STATIC_ASSERT(kRootArrayNumberOfConstantEncodings ==
1130 Heap::kOldSpaceRoots);
1131 STATIC_ASSERT(kMaxRepeats == 13);
1132 case kConstantRepeat:
1133 FOUR_CASES(kConstantRepeat + 1)
1134 FOUR_CASES(kConstantRepeat + 5)
1135 FOUR_CASES(kConstantRepeat + 9) {
1136 int repeats = RepeatsForCode(data);
1137 Object* object = current[-1];
1138 ASSERT(!isolate->heap()->InNewSpace(object));
1139 for (int i = 0; i < repeats; i++) current[i] = object;
1144 // Deserialize a new object and write a pointer to it to the current
1146 ALL_SPACES(kNewObject, kPlain, kStartOfObject)
1147 // Support for direct instruction pointers in functions. It's an inner
1148 // pointer because it points at the entry point, not at the start of the
1150 CASE_STATEMENT(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
1151 CASE_BODY(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
1152 // Deserialize a new code object and write a pointer to its first
1153 // instruction to the current code object.
1154 ALL_SPACES(kNewObject, kFromCode, kInnerPointer)
1155 // Find a recently deserialized object using its offset from the current
1156 // allocation point and write a pointer to it to the current object.
1157 ALL_SPACES(kBackref, kPlain, kStartOfObject)
1158 ALL_SPACES(kBackrefWithSkip, kPlain, kStartOfObject)
1159 #if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL
1160 // Deserialize a new object from pointer found in code and write
1161 // a pointer to it to the current object. Required only for MIPS or ARM
1162 // with ool constant pool, and omitted on the other architectures because
1163 // it is fully unrolled and would cause bloat.
1164 ALL_SPACES(kNewObject, kFromCode, kStartOfObject)
1165 // Find a recently deserialized code object using its offset from the
1166 // current allocation point and write a pointer to it to the current
1167 // object. Required only for MIPS or ARM with ool constant pool.
1168 ALL_SPACES(kBackref, kFromCode, kStartOfObject)
1169 ALL_SPACES(kBackrefWithSkip, kFromCode, kStartOfObject)
1171 // Find a recently deserialized code object using its offset from the
1172 // current allocation point and write a pointer to its first instruction
1173 // to the current code object or the instruction pointer in a function
1175 ALL_SPACES(kBackref, kFromCode, kInnerPointer)
1176 ALL_SPACES(kBackrefWithSkip, kFromCode, kInnerPointer)
1177 ALL_SPACES(kBackref, kPlain, kInnerPointer)
1178 ALL_SPACES(kBackrefWithSkip, kPlain, kInnerPointer)
1179 // Find an object in the roots array and write a pointer to it to the
1181 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
1182 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0)
1183 // Find an object in the partial snapshots cache and write a pointer to it
1184 // to the current object.
1185 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
1186 CASE_BODY(kPartialSnapshotCache,
1190 // Find an code entry in the partial snapshots cache and
1191 // write a pointer to it to the current object.
1192 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
1193 CASE_BODY(kPartialSnapshotCache,
1197 // Find an external reference and write a pointer to it to the current
1199 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
1200 CASE_BODY(kExternalReference,
1204 // Find an external reference and write a pointer to it in the current
1206 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
1207 CASE_BODY(kExternalReference,
1212 #undef CASE_STATEMENT
1217 int size = source_->GetInt();
1218 current = reinterpret_cast<Object**>(
1219 reinterpret_cast<intptr_t>(current) + size);
1223 case kNativesStringResource: {
1224 int index = source_->Get();
1225 Vector<const char> source_vector = Natives::GetRawScriptSource(index);
1226 NativesExternalStringResource* resource =
1227 new NativesExternalStringResource(isolate->bootstrapper(),
1228 source_vector.start(),
1229 source_vector.length());
1230 *current++ = reinterpret_cast<Object*>(resource);
1234 case kSynchronize: {
1235 // If we get here then that indicates that you have a mismatch between
1236 // the number of GC roots when serializing and deserializing.
1244 ASSERT_EQ(limit, current);
1248 void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
1249 ASSERT(integer < 1 << 22);
1252 if (integer > 0xff) bytes = 2;
1253 if (integer > 0xffff) bytes = 3;
1255 Put(static_cast<int>(integer & 0xff), "IntPart1");
1256 if (bytes > 1) Put(static_cast<int>((integer >> 8) & 0xff), "IntPart2");
1257 if (bytes > 2) Put(static_cast<int>((integer >> 16) & 0xff), "IntPart3");
1261 Serializer::Serializer(Isolate* isolate, SnapshotByteSink* sink)
1262 : isolate_(isolate),
1264 external_reference_encoder_(new ExternalReferenceEncoder(isolate)),
1265 root_index_wave_front_(0) {
1266 // The serializer is meant to be used only to generate initial heap images
1267 // from a context in which there is only one isolate.
1268 for (int i = 0; i <= LAST_SPACE; i++) {
1274 Serializer::~Serializer() {
1275 delete external_reference_encoder_;
1279 void StartupSerializer::SerializeStrongReferences() {
1280 Isolate* isolate = this->isolate();
1281 // No active threads.
1282 CHECK_EQ(NULL, isolate->thread_manager()->FirstThreadStateInUse());
1283 // No active or weak handles.
1284 CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
1285 CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
1286 CHECK_EQ(0, isolate->eternal_handles()->NumberOfHandles());
1287 // We don't support serializing installed extensions.
1288 CHECK(!isolate->has_installed_extensions());
1289 isolate->heap()->IterateSmiRoots(this);
1290 isolate->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
1294 void PartialSerializer::Serialize(Object** object) {
1295 this->VisitPointer(object);
1300 bool Serializer::ShouldBeSkipped(Object** current) {
1301 Object** roots = isolate()->heap()->roots_array_start();
1302 return current == &roots[Heap::kStoreBufferTopRootIndex]
1303 || current == &roots[Heap::kStackLimitRootIndex]
1304 || current == &roots[Heap::kRealStackLimitRootIndex];
1308 void Serializer::VisitPointers(Object** start, Object** end) {
1309 Isolate* isolate = this->isolate();;
1311 for (Object** current = start; current < end; current++) {
1312 if (start == isolate->heap()->roots_array_start()) {
1313 root_index_wave_front_ =
1314 Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
1316 if (ShouldBeSkipped(current)) {
1317 sink_->Put(kSkip, "Skip");
1318 sink_->PutInt(kPointerSize, "SkipOneWord");
1319 } else if ((*current)->IsSmi()) {
1320 sink_->Put(kRawData + 1, "Smi");
1321 for (int i = 0; i < kPointerSize; i++) {
1322 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1325 SerializeObject(*current, kPlain, kStartOfObject, 0);
1331 // This ensures that the partial snapshot cache keeps things alive during GC and
1332 // tracks their movement. When it is called during serialization of the startup
1333 // snapshot nothing happens. When the partial (context) snapshot is created,
1334 // this array is populated with the pointers that the partial snapshot will
1335 // need. As that happens we emit serialized objects to the startup snapshot
1336 // that correspond to the elements of this cache array. On deserialization we
1337 // therefore need to visit the cache array. This fills it up with pointers to
1338 // deserialized objects.
1339 void SerializerDeserializer::Iterate(Isolate* isolate,
1340 ObjectVisitor* visitor) {
1341 if (Serializer::enabled(isolate)) return;
1342 for (int i = 0; ; i++) {
1343 if (isolate->serialize_partial_snapshot_cache_length() <= i) {
1344 // Extend the array ready to get a value from the visitor when
1346 isolate->PushToPartialSnapshotCache(Smi::FromInt(0));
1348 Object** cache = isolate->serialize_partial_snapshot_cache();
1349 visitor->VisitPointers(&cache[i], &cache[i + 1]);
1350 // Sentinel is the undefined object, which is a root so it will not normally
1351 // be found in the cache.
1352 if (cache[i] == isolate->heap()->undefined_value()) {
1359 int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1360 Isolate* isolate = this->isolate();
1363 i < isolate->serialize_partial_snapshot_cache_length();
1365 Object* entry = isolate->serialize_partial_snapshot_cache()[i];
1366 if (entry == heap_object) return i;
1369 // We didn't find the object in the cache. So we add it to the cache and
1370 // then visit the pointer so that it becomes part of the startup snapshot
1371 // and we can refer to it from the partial snapshot.
1372 int length = isolate->serialize_partial_snapshot_cache_length();
1373 isolate->PushToPartialSnapshotCache(heap_object);
1374 startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object));
1375 // We don't recurse from the startup snapshot generator into the partial
1376 // snapshot generator.
1377 ASSERT(length == isolate->serialize_partial_snapshot_cache_length() - 1);
1382 int Serializer::RootIndex(HeapObject* heap_object, HowToCode from) {
1383 Heap* heap = isolate()->heap();
1384 if (heap->InNewSpace(heap_object)) return kInvalidRootIndex;
1385 for (int i = 0; i < root_index_wave_front_; i++) {
1386 Object* root = heap->roots_array_start()[i];
1387 if (!root->IsSmi() && root == heap_object) {
1388 #if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL
1389 if (from == kFromCode) {
1390 // In order to avoid code bloat in the deserializer we don't have
1391 // support for the encoding that specifies a particular root should
1392 // be written from within code.
1393 return kInvalidRootIndex;
1399 return kInvalidRootIndex;
1403 // Encode the location of an already deserialized object in order to write its
1404 // location into a later object. We can encode the location as an offset from
1405 // the start of the deserialized objects or as an offset backwards from the
1406 // current allocation pointer.
1407 void Serializer::SerializeReferenceToPreviousObject(
1410 HowToCode how_to_code,
1411 WhereToPoint where_to_point,
1413 int offset = CurrentAllocationAddress(space) - address;
1414 // Shift out the bits that are always 0.
1415 offset >>= kObjectAlignmentBits;
1417 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1419 sink_->Put(kBackrefWithSkip + how_to_code + where_to_point + space,
1420 "BackRefSerWithSkip");
1421 sink_->PutInt(skip, "BackRefSkipDistance");
1423 sink_->PutInt(offset, "offset");
1427 void StartupSerializer::SerializeObject(
1429 HowToCode how_to_code,
1430 WhereToPoint where_to_point,
1432 CHECK(o->IsHeapObject());
1433 HeapObject* heap_object = HeapObject::cast(o);
1436 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1437 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1441 if (address_mapper_.IsMapped(heap_object)) {
1442 int space = SpaceOfObject(heap_object);
1443 int address = address_mapper_.MappedTo(heap_object);
1444 SerializeReferenceToPreviousObject(space,
1451 sink_->Put(kSkip, "FlushPendingSkip");
1452 sink_->PutInt(skip, "SkipDistance");
1455 // Object has not yet been serialized. Serialize it here.
1456 ObjectSerializer object_serializer(this,
1461 object_serializer.Serialize();
1466 void StartupSerializer::SerializeWeakReferences() {
1467 // This phase comes right after the partial serialization (of the snapshot).
1468 // After we have done the partial serialization the partial snapshot cache
1469 // will contain some references needed to decode the partial snapshot. We
1470 // add one entry with 'undefined' which is the sentinel that the deserializer
1471 // uses to know it is done deserializing the array.
1472 Object* undefined = isolate()->heap()->undefined_value();
1473 VisitPointer(&undefined);
1474 isolate()->heap()->IterateWeakRoots(this, VISIT_ALL);
1479 void Serializer::PutRoot(int root_index,
1481 SerializerDeserializer::HowToCode how_to_code,
1482 SerializerDeserializer::WhereToPoint where_to_point,
1484 if (how_to_code == kPlain &&
1485 where_to_point == kStartOfObject &&
1486 root_index < kRootArrayNumberOfConstantEncodings &&
1487 !isolate()->heap()->InNewSpace(object)) {
1489 sink_->Put(kRootArrayConstants + kNoSkipDistance + root_index,
1492 sink_->Put(kRootArrayConstants + kHasSkipDistance + root_index,
1494 sink_->PutInt(skip, "SkipInPutRoot");
1498 sink_->Put(kSkip, "SkipFromPutRoot");
1499 sink_->PutInt(skip, "SkipFromPutRootDistance");
1501 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
1502 sink_->PutInt(root_index, "root_index");
1507 void PartialSerializer::SerializeObject(
1509 HowToCode how_to_code,
1510 WhereToPoint where_to_point,
1512 CHECK(o->IsHeapObject());
1513 HeapObject* heap_object = HeapObject::cast(o);
1515 if (heap_object->IsMap()) {
1516 // The code-caches link to context-specific code objects, which
1517 // the startup and context serializes cannot currently handle.
1518 ASSERT(Map::cast(heap_object)->code_cache() ==
1519 heap_object->GetHeap()->empty_fixed_array());
1523 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1524 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1528 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1530 sink_->Put(kSkip, "SkipFromSerializeObject");
1531 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1534 int cache_index = PartialSnapshotCacheIndex(heap_object);
1535 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1536 "PartialSnapshotCache");
1537 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1541 // Pointers from the partial snapshot to the objects in the startup snapshot
1542 // should go through the root array or through the partial snapshot cache.
1543 // If this is not the case you may have to add something to the root array.
1544 ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1545 // All the internalized strings that the partial snapshot needs should be
1546 // either in the root table or in the partial snapshot cache.
1547 ASSERT(!heap_object->IsInternalizedString());
1549 if (address_mapper_.IsMapped(heap_object)) {
1550 int space = SpaceOfObject(heap_object);
1551 int address = address_mapper_.MappedTo(heap_object);
1552 SerializeReferenceToPreviousObject(space,
1559 sink_->Put(kSkip, "SkipFromSerializeObject");
1560 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1562 // Object has not yet been serialized. Serialize it here.
1563 ObjectSerializer serializer(this,
1568 serializer.Serialize();
1573 void Serializer::ObjectSerializer::Serialize() {
1574 int space = Serializer::SpaceOfObject(object_);
1575 int size = object_->Size();
1577 sink_->Put(kNewObject + reference_representation_ + space,
1578 "ObjectSerialization");
1579 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1581 ASSERT(code_address_map_);
1582 const char* code_name = code_address_map_->Lookup(object_->address());
1583 LOG(serializer_->isolate_,
1584 CodeNameEvent(object_->address(), sink_->Position(), code_name));
1585 LOG(serializer_->isolate_,
1586 SnapshotPositionEvent(object_->address(), sink_->Position()));
1588 // Mark this object as already serialized.
1589 int offset = serializer_->Allocate(space, size);
1590 serializer_->address_mapper()->AddMapping(object_, offset);
1592 // Serialize the map (first word of the object).
1593 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject, 0);
1595 // Serialize the rest of the object.
1596 CHECK_EQ(0, bytes_processed_so_far_);
1597 bytes_processed_so_far_ = kPointerSize;
1598 object_->IterateBody(object_->map()->instance_type(), size, this);
1599 OutputRawData(object_->address() + size);
1603 void Serializer::ObjectSerializer::VisitPointers(Object** start,
1605 Object** current = start;
1606 while (current < end) {
1607 while (current < end && (*current)->IsSmi()) current++;
1608 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1610 while (current < end && !(*current)->IsSmi()) {
1611 HeapObject* current_contents = HeapObject::cast(*current);
1612 int root_index = serializer_->RootIndex(current_contents, kPlain);
1613 // Repeats are not subject to the write barrier so there are only some
1614 // objects that can be used in a repeat encoding. These are the early
1615 // ones in the root array that are never in new space.
1616 if (current != start &&
1617 root_index != kInvalidRootIndex &&
1618 root_index < kRootArrayNumberOfConstantEncodings &&
1619 current_contents == current[-1]) {
1620 ASSERT(!serializer_->isolate()->heap()->InNewSpace(current_contents));
1621 int repeat_count = 1;
1622 while (current < end - 1 && current[repeat_count] == current_contents) {
1625 current += repeat_count;
1626 bytes_processed_so_far_ += repeat_count * kPointerSize;
1627 if (repeat_count > kMaxRepeats) {
1628 sink_->Put(kRepeat, "SerializeRepeats");
1629 sink_->PutInt(repeat_count, "SerializeRepeats");
1631 sink_->Put(CodeForRepeats(repeat_count), "SerializeRepeats");
1634 serializer_->SerializeObject(
1635 current_contents, kPlain, kStartOfObject, 0);
1636 bytes_processed_so_far_ += kPointerSize;
1644 void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
1645 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1646 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1648 int skip = OutputRawData(rinfo->target_address_address(),
1649 kCanReturnSkipInsteadOfSkipping);
1650 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1651 Object* object = rinfo->target_object();
1652 serializer_->SerializeObject(object, how_to_code, kStartOfObject, skip);
1653 bytes_processed_so_far_ += rinfo->target_address_size();
1657 void Serializer::ObjectSerializer::VisitExternalReference(Address* p) {
1658 int skip = OutputRawData(reinterpret_cast<Address>(p),
1659 kCanReturnSkipInsteadOfSkipping);
1660 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
1661 sink_->PutInt(skip, "SkipB4ExternalRef");
1662 Address target = *p;
1663 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1664 bytes_processed_so_far_ += kPointerSize;
1668 void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
1669 int skip = OutputRawData(rinfo->target_address_address(),
1670 kCanReturnSkipInsteadOfSkipping);
1671 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1672 sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
1673 sink_->PutInt(skip, "SkipB4ExternalRef");
1674 Address target = rinfo->target_reference();
1675 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1676 bytes_processed_so_far_ += rinfo->target_address_size();
1680 void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1681 int skip = OutputRawData(rinfo->target_address_address(),
1682 kCanReturnSkipInsteadOfSkipping);
1683 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1684 sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
1685 sink_->PutInt(skip, "SkipB4ExternalRef");
1686 Address target = rinfo->target_address();
1687 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1688 bytes_processed_so_far_ += rinfo->target_address_size();
1692 void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1693 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1694 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1696 int skip = OutputRawData(rinfo->target_address_address(),
1697 kCanReturnSkipInsteadOfSkipping);
1698 Code* object = Code::GetCodeFromTargetAddress(rinfo->target_address());
1699 serializer_->SerializeObject(object, kFromCode, kInnerPointer, skip);
1700 bytes_processed_so_far_ += rinfo->target_address_size();
1704 void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1705 int skip = OutputRawData(entry_address, kCanReturnSkipInsteadOfSkipping);
1706 Code* object = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1707 serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
1708 bytes_processed_so_far_ += kPointerSize;
1712 void Serializer::ObjectSerializer::VisitCell(RelocInfo* rinfo) {
1713 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1714 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1716 int skip = OutputRawData(rinfo->pc(), kCanReturnSkipInsteadOfSkipping);
1717 Cell* object = Cell::cast(rinfo->target_cell());
1718 serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
1722 void Serializer::ObjectSerializer::VisitExternalAsciiString(
1723 v8::String::ExternalAsciiStringResource** resource_pointer) {
1724 Address references_start = reinterpret_cast<Address>(resource_pointer);
1725 OutputRawData(references_start);
1726 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1728 serializer_->isolate()->heap()->natives_source_cache()->get(i);
1729 if (!source->IsUndefined()) {
1730 ExternalAsciiString* string = ExternalAsciiString::cast(source);
1731 typedef v8::String::ExternalAsciiStringResource Resource;
1732 const Resource* resource = string->resource();
1733 if (resource == *resource_pointer) {
1734 sink_->Put(kNativesStringResource, "NativesStringResource");
1735 sink_->PutSection(i, "NativesStringResourceEnd");
1736 bytes_processed_so_far_ += sizeof(resource);
1741 // One of the strings in the natives cache should match the resource. We
1742 // can't serialize any other kinds of external strings.
1747 static Code* CloneCodeObject(HeapObject* code) {
1748 Address copy = new byte[code->Size()];
1749 OS::MemCopy(copy, code->address(), code->Size());
1750 return Code::cast(HeapObject::FromAddress(copy));
1754 static void WipeOutRelocations(Code* code) {
1756 RelocInfo::kCodeTargetMask |
1757 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
1758 RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE) |
1759 RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY);
1760 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
1761 if (!(FLAG_enable_ool_constant_pool && it.rinfo()->IsInConstantPool())) {
1762 it.rinfo()->WipeOut();
1768 int Serializer::ObjectSerializer::OutputRawData(
1769 Address up_to, Serializer::ObjectSerializer::ReturnSkip return_skip) {
1770 Address object_start = object_->address();
1771 int base = bytes_processed_so_far_;
1772 int up_to_offset = static_cast<int>(up_to - object_start);
1773 int to_skip = up_to_offset - bytes_processed_so_far_;
1774 int bytes_to_output = to_skip;
1775 bytes_processed_so_far_ += to_skip;
1776 // This assert will fail if the reloc info gives us the target_address_address
1777 // locations in a non-ascending order. Luckily that doesn't happen.
1778 ASSERT(to_skip >= 0);
1779 bool outputting_code = false;
1780 if (to_skip != 0 && code_object_ && !code_has_been_output_) {
1781 // Output the code all at once and fix later.
1782 bytes_to_output = object_->Size() + to_skip - bytes_processed_so_far_;
1783 outputting_code = true;
1784 code_has_been_output_ = true;
1786 if (bytes_to_output != 0 &&
1787 (!code_object_ || outputting_code)) {
1788 #define RAW_CASE(index) \
1789 if (!outputting_code && bytes_to_output == index * kPointerSize && \
1790 index * kPointerSize == to_skip) { \
1791 sink_->PutSection(kRawData + index, "RawDataFixed"); \
1792 to_skip = 0; /* This insn already skips. */ \
1794 COMMON_RAW_LENGTHS(RAW_CASE)
1797 // We always end up here if we are outputting the code of a code object.
1798 sink_->Put(kRawData, "RawData");
1799 sink_->PutInt(bytes_to_output, "length");
1802 // To make snapshots reproducible, we need to wipe out all pointers in code.
1804 Code* code = CloneCodeObject(object_);
1805 WipeOutRelocations(code);
1806 // We need to wipe out the header fields *after* wiping out the
1807 // relocations, because some of these fields are needed for the latter.
1808 code->WipeOutHeader();
1809 object_start = code->address();
1812 const char* description = code_object_ ? "Code" : "Byte";
1813 for (int i = 0; i < bytes_to_output; i++) {
1814 sink_->PutSection(object_start[base + i], description);
1816 if (code_object_) delete[] object_start;
1818 if (to_skip != 0 && return_skip == kIgnoringReturn) {
1819 sink_->Put(kSkip, "Skip");
1820 sink_->PutInt(to_skip, "SkipDistance");
1827 int Serializer::SpaceOfObject(HeapObject* object) {
1828 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1829 AllocationSpace s = static_cast<AllocationSpace>(i);
1830 if (object->GetHeap()->InSpace(object, s)) {
1831 ASSERT(i < kNumberOfSpaces);
1840 int Serializer::Allocate(int space, int size) {
1841 CHECK(space >= 0 && space < kNumberOfSpaces);
1842 int allocation_address = fullness_[space];
1843 fullness_[space] = allocation_address + size;
1844 return allocation_address;
1848 int Serializer::SpaceAreaSize(int space) {
1849 if (space == CODE_SPACE) {
1850 return isolate_->memory_allocator()->CodePageAreaSize();
1852 return Page::kPageSize - Page::kObjectStartOffset;
1857 void Serializer::Pad() {
1858 // The non-branching GetInt will read up to 3 bytes too far, so we need
1859 // to pad the snapshot to make sure we don't read over the end.
1860 for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) {
1861 sink_->Put(kNop, "Padding");
1866 bool SnapshotByteSource::AtEOF() {
1867 if (0u + length_ - position_ > 2 * sizeof(uint32_t)) return false;
1868 for (int x = position_; x < length_; x++) {
1869 if (data_[x] != SerializerDeserializer::nop()) return false;
1874 } } // namespace v8::internal