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/code-stubs.h"
12 #include "src/deoptimizer.h"
13 #include "src/execution.h"
14 #include "src/global-handles.h"
15 #include "src/ic/ic.h"
16 #include "src/ic/stub-cache.h"
17 #include "src/natives.h"
18 #include "src/objects.h"
19 #include "src/runtime.h"
20 #include "src/serialize.h"
21 #include "src/snapshot.h"
22 #include "src/snapshot-source-sink.h"
23 #include "src/v8threads.h"
24 #include "src/version.h"
30 // -----------------------------------------------------------------------------
31 // Coding of external references.
33 // The encoding of an external reference. The type is in the high word.
34 // The id is in the low word.
35 static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
36 return static_cast<uint32_t>(type) << 16 | id;
40 static int* GetInternalPointer(StatsCounter* counter) {
41 // All counters refer to dummy_counter, if deserializing happens without
42 // setting up counters.
43 static int dummy_counter = 0;
44 return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
48 ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
49 ExternalReferenceTable* external_reference_table =
50 isolate->external_reference_table();
51 if (external_reference_table == NULL) {
52 external_reference_table = new ExternalReferenceTable(isolate);
53 isolate->set_external_reference_table(external_reference_table);
55 return external_reference_table;
59 void ExternalReferenceTable::AddFromId(TypeCode type,
66 ExternalReference ref(static_cast<Builtins::CFunctionId>(id), isolate);
67 address = ref.address();
71 ExternalReference ref(static_cast<Builtins::Name>(id), isolate);
72 address = ref.address();
75 case RUNTIME_FUNCTION: {
76 ExternalReference ref(static_cast<Runtime::FunctionId>(id), isolate);
77 address = ref.address();
81 ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)),
83 address = ref.address();
90 Add(address, type, id, name);
94 void ExternalReferenceTable::Add(Address address,
98 DCHECK_NE(NULL, address);
99 ExternalReferenceEntry entry;
100 entry.address = address;
101 entry.code = EncodeExternal(type, id);
103 DCHECK_NE(0, entry.code);
104 // Assert that the code is added in ascending order to rule out duplicates.
105 DCHECK((size() == 0) || (code(size() - 1) < entry.code));
107 if (id > max_id_[type]) max_id_[type] = id;
111 void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
112 for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
113 max_id_[type_code] = 0;
117 Add(ExternalReference::roots_array_start(isolate).address(),
118 "Heap::roots_array_start()");
119 Add(ExternalReference::address_of_stack_limit(isolate).address(),
120 "StackGuard::address_of_jslimit()");
121 Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
122 "StackGuard::address_of_real_jslimit()");
123 Add(ExternalReference::new_space_start(isolate).address(),
124 "Heap::NewSpaceStart()");
125 Add(ExternalReference::new_space_mask(isolate).address(),
126 "Heap::NewSpaceMask()");
127 Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
128 "Heap::NewSpaceAllocationLimitAddress()");
129 Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
130 "Heap::NewSpaceAllocationTopAddress()");
131 Add(ExternalReference::debug_break(isolate).address(), "Debug::Break()");
132 Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
133 "Debug::step_in_fp_addr()");
134 Add(ExternalReference::mod_two_doubles_operation(isolate).address(),
136 // Keyed lookup cache.
137 Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
138 "KeyedLookupCache::keys()");
139 Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
140 "KeyedLookupCache::field_offsets()");
141 Add(ExternalReference::handle_scope_next_address(isolate).address(),
142 "HandleScope::next");
143 Add(ExternalReference::handle_scope_limit_address(isolate).address(),
144 "HandleScope::limit");
145 Add(ExternalReference::handle_scope_level_address(isolate).address(),
146 "HandleScope::level");
147 Add(ExternalReference::new_deoptimizer_function(isolate).address(),
148 "Deoptimizer::New()");
149 Add(ExternalReference::compute_output_frames_function(isolate).address(),
150 "Deoptimizer::ComputeOutputFrames()");
151 Add(ExternalReference::address_of_min_int().address(),
152 "LDoubleConstant::min_int");
153 Add(ExternalReference::address_of_one_half().address(),
154 "LDoubleConstant::one_half");
155 Add(ExternalReference::isolate_address(isolate).address(), "isolate");
156 Add(ExternalReference::address_of_negative_infinity().address(),
157 "LDoubleConstant::negative_infinity");
158 Add(ExternalReference::power_double_double_function(isolate).address(),
159 "power_double_double_function");
160 Add(ExternalReference::power_double_int_function(isolate).address(),
161 "power_double_int_function");
162 Add(ExternalReference::math_log_double_function(isolate).address(),
164 Add(ExternalReference::store_buffer_top(isolate).address(),
166 Add(ExternalReference::address_of_canonical_non_hole_nan().address(),
168 Add(ExternalReference::address_of_the_hole_nan().address(), "the_hole_nan");
169 Add(ExternalReference::get_date_field_function(isolate).address(),
171 Add(ExternalReference::date_cache_stamp(isolate).address(),
173 Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
174 "address_of_pending_message_obj");
175 Add(ExternalReference::address_of_has_pending_message(isolate).address(),
176 "address_of_has_pending_message");
177 Add(ExternalReference::address_of_pending_message_script(isolate).address(),
178 "pending_message_script");
179 Add(ExternalReference::get_make_code_young_function(isolate).address(),
180 "Code::MakeCodeYoung");
181 Add(ExternalReference::cpu_features().address(), "cpu_features");
182 Add(ExternalReference(Runtime::kAllocateInNewSpace, isolate).address(),
183 "Runtime::AllocateInNewSpace");
184 Add(ExternalReference(Runtime::kAllocateInTargetSpace, isolate).address(),
185 "Runtime::AllocateInTargetSpace");
186 Add(ExternalReference::old_pointer_space_allocation_top_address(isolate)
188 "Heap::OldPointerSpaceAllocationTopAddress");
189 Add(ExternalReference::old_pointer_space_allocation_limit_address(isolate)
191 "Heap::OldPointerSpaceAllocationLimitAddress");
192 Add(ExternalReference::old_data_space_allocation_top_address(isolate)
194 "Heap::OldDataSpaceAllocationTopAddress");
195 Add(ExternalReference::old_data_space_allocation_limit_address(isolate)
197 "Heap::OldDataSpaceAllocationLimitAddress");
198 Add(ExternalReference::allocation_sites_list_address(isolate).address(),
199 "Heap::allocation_sites_list_address()");
200 Add(ExternalReference::address_of_uint32_bias().address(), "uint32_bias");
201 Add(ExternalReference::get_mark_code_as_executed_function(isolate).address(),
202 "Code::MarkCodeAsExecuted");
203 Add(ExternalReference::is_profiling_address(isolate).address(),
204 "CpuProfiler::is_profiling");
205 Add(ExternalReference::scheduled_exception_address(isolate).address(),
206 "Isolate::scheduled_exception");
207 Add(ExternalReference::invoke_function_callback(isolate).address(),
208 "InvokeFunctionCallback");
209 Add(ExternalReference::invoke_accessor_getter_callback(isolate).address(),
210 "InvokeAccessorGetterCallback");
211 Add(ExternalReference::flush_icache_function(isolate).address(),
212 "CpuFeatures::FlushICache");
213 Add(ExternalReference::log_enter_external_function(isolate).address(),
214 "Logger::EnterExternal");
215 Add(ExternalReference::log_leave_external_function(isolate).address(),
216 "Logger::LeaveExternal");
217 Add(ExternalReference::address_of_minus_one_half().address(),
218 "double_constants.minus_one_half");
219 Add(ExternalReference::stress_deopt_count(isolate).address(),
220 "Isolate::stress_deopt_count_address()");
221 Add(ExternalReference::incremental_marking_record_write_function(isolate)
223 "IncrementalMarking::RecordWriteFromCode");
226 Add(ExternalReference::debug_after_break_target_address(isolate).address(),
227 "Debug::after_break_target_address()");
228 Add(ExternalReference::debug_restarter_frame_function_pointer_address(isolate)
230 "Debug::restarter_frame_function_pointer_address()");
231 Add(ExternalReference::debug_is_active_address(isolate).address(),
232 "Debug::is_active_address()");
234 #ifndef V8_INTERPRETED_REGEXP
235 Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
236 "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
237 Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
238 "RegExpMacroAssembler*::CheckStackGuardState()");
239 Add(ExternalReference::re_grow_stack(isolate).address(),
240 "NativeRegExpMacroAssembler::GrowStack()");
241 Add(ExternalReference::re_word_character_map().address(),
242 "NativeRegExpMacroAssembler::word_character_map");
243 Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
244 "RegExpStack::limit_address()");
245 Add(ExternalReference::address_of_regexp_stack_memory_address(isolate)
247 "RegExpStack::memory_address()");
248 Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
249 "RegExpStack::memory_size()");
250 Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
251 "OffsetsVector::static_offsets_vector");
252 #endif // V8_INTERPRETED_REGEXP
254 // The following populates all of the different type of external references
255 // into the ExternalReferenceTable.
257 // NOTE: This function was originally 100k of code. It has since been
258 // rewritten to be mostly table driven, as the callback macro style tends to
259 // very easily cause code bloat. Please be careful in the future when adding
262 struct RefTableEntry {
268 static const RefTableEntry ref_table[] = {
270 #define DEF_ENTRY_C(name, ignored) \
272 Builtins::c_##name, \
273 "Builtins::" #name },
275 BUILTIN_LIST_C(DEF_ENTRY_C)
278 #define DEF_ENTRY_C(name, ignored) \
281 "Builtins::" #name },
282 #define DEF_ENTRY_A(name, kind, state, extra) DEF_ENTRY_C(name, ignored)
284 BUILTIN_LIST_C(DEF_ENTRY_C)
285 BUILTIN_LIST_A(DEF_ENTRY_A)
286 BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
291 #define RUNTIME_ENTRY(name, nargs, ressize) \
292 { RUNTIME_FUNCTION, \
296 RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
297 INLINE_OPTIMIZED_FUNCTION_LIST(RUNTIME_ENTRY)
300 #define INLINE_OPTIMIZED_ENTRY(name, nargs, ressize) \
301 { RUNTIME_FUNCTION, \
302 Runtime::kInlineOptimized##name, \
305 INLINE_OPTIMIZED_FUNCTION_LIST(INLINE_OPTIMIZED_ENTRY)
306 #undef INLINE_OPTIMIZED_ENTRY
309 #define IC_ENTRY(name) \
314 IC_UTIL_LIST(IC_ENTRY)
316 }; // end of ref_table[].
318 for (size_t i = 0; i < arraysize(ref_table); ++i) {
319 AddFromId(ref_table[i].type,
326 struct StatsRefTableEntry {
327 StatsCounter* (Counters::*counter)();
332 const StatsRefTableEntry stats_ref_table[] = {
333 #define COUNTER_ENTRY(name, caption) \
335 Counters::k_##name, \
336 "Counters::" #name },
338 STATS_COUNTER_LIST_1(COUNTER_ENTRY)
339 STATS_COUNTER_LIST_2(COUNTER_ENTRY)
341 }; // end of stats_ref_table[].
343 Counters* counters = isolate->counters();
344 for (size_t i = 0; i < arraysize(stats_ref_table); ++i) {
345 Add(reinterpret_cast<Address>(GetInternalPointer(
346 (counters->*(stats_ref_table[i].counter))())),
348 stats_ref_table[i].id,
349 stats_ref_table[i].name);
354 const char* AddressNames[] = {
355 #define BUILD_NAME_LITERAL(CamelName, hacker_name) \
356 "Isolate::" #hacker_name "_address",
357 FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL)
359 #undef BUILD_NAME_LITERAL
362 for (uint16_t i = 0; i < Isolate::kIsolateAddressCount; ++i) {
363 Add(isolate->get_address_from_id((Isolate::AddressId)i),
364 TOP_ADDRESS, i, AddressNames[i]);
368 #define ACCESSOR_INFO_DECLARATION(name) \
369 Add(FUNCTION_ADDR(&Accessors::name##Getter), \
371 Accessors::k##name##Getter, \
372 "Accessors::" #name "Getter"); \
373 Add(FUNCTION_ADDR(&Accessors::name##Setter), \
375 Accessors::k##name##Setter, \
376 "Accessors::" #name "Setter");
377 ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
378 #undef ACCESSOR_INFO_DECLARATION
380 StubCache* stub_cache = isolate->stub_cache();
383 Add(stub_cache->key_reference(StubCache::kPrimary).address(),
384 STUB_CACHE_TABLE, 1, "StubCache::primary_->key");
385 Add(stub_cache->value_reference(StubCache::kPrimary).address(),
386 STUB_CACHE_TABLE, 2, "StubCache::primary_->value");
387 Add(stub_cache->map_reference(StubCache::kPrimary).address(),
388 STUB_CACHE_TABLE, 3, "StubCache::primary_->map");
389 Add(stub_cache->key_reference(StubCache::kSecondary).address(),
390 STUB_CACHE_TABLE, 4, "StubCache::secondary_->key");
391 Add(stub_cache->value_reference(StubCache::kSecondary).address(),
392 STUB_CACHE_TABLE, 5, "StubCache::secondary_->value");
393 Add(stub_cache->map_reference(StubCache::kSecondary).address(),
394 STUB_CACHE_TABLE, 6, "StubCache::secondary_->map");
397 Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
398 RUNTIME_ENTRY, 1, "HandleScope::DeleteExtensions");
399 Add(ExternalReference::incremental_marking_record_write_function(isolate)
401 RUNTIME_ENTRY, 2, "IncrementalMarking::RecordWrite");
402 Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
403 RUNTIME_ENTRY, 3, "StoreBuffer::StoreBufferOverflow");
405 // Add a small set of deopt entry addresses to encoder without generating the
406 // deopt table code, which isn't possible at deserialization time.
407 HandleScope scope(isolate);
408 for (int entry = 0; entry < kDeoptTableSerializeEntryCount; ++entry) {
409 Address address = Deoptimizer::GetDeoptimizationEntry(
413 Deoptimizer::CALCULATE_ENTRY_ADDRESS);
414 Add(address, LAZY_DEOPTIMIZATION, entry, "lazy_deopt");
419 ExternalReferenceEncoder::ExternalReferenceEncoder(Isolate* isolate)
420 : encodings_(HashMap::PointersMatch),
422 ExternalReferenceTable* external_references =
423 ExternalReferenceTable::instance(isolate_);
424 for (int i = 0; i < external_references->size(); ++i) {
425 Put(external_references->address(i), i);
430 uint32_t ExternalReferenceEncoder::Encode(Address key) const {
431 int index = IndexOf(key);
432 DCHECK(key == NULL || index >= 0);
434 ExternalReferenceTable::instance(isolate_)->code(index) : 0;
438 const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
439 int index = IndexOf(key);
440 return index >= 0 ? ExternalReferenceTable::instance(isolate_)->name(index)
445 int ExternalReferenceEncoder::IndexOf(Address key) const {
446 if (key == NULL) return -1;
447 HashMap::Entry* entry =
448 const_cast<HashMap&>(encodings_).Lookup(key, Hash(key), false);
451 : static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
455 void ExternalReferenceEncoder::Put(Address key, int index) {
456 HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
457 entry->value = reinterpret_cast<void*>(index);
461 ExternalReferenceDecoder::ExternalReferenceDecoder(Isolate* isolate)
462 : encodings_(NewArray<Address*>(kTypeCodeCount)),
464 ExternalReferenceTable* external_references =
465 ExternalReferenceTable::instance(isolate_);
466 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
467 int max = external_references->max_id(type) + 1;
468 encodings_[type] = NewArray<Address>(max + 1);
470 for (int i = 0; i < external_references->size(); ++i) {
471 Put(external_references->code(i), external_references->address(i));
476 ExternalReferenceDecoder::~ExternalReferenceDecoder() {
477 for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
478 DeleteArray(encodings_[type]);
480 DeleteArray(encodings_);
484 class CodeAddressMap: public CodeEventLogger {
486 explicit CodeAddressMap(Isolate* isolate)
487 : isolate_(isolate) {
488 isolate->logger()->addCodeEventListener(this);
491 virtual ~CodeAddressMap() {
492 isolate_->logger()->removeCodeEventListener(this);
495 virtual void CodeMoveEvent(Address from, Address to) {
496 address_to_name_map_.Move(from, to);
499 virtual void CodeDisableOptEvent(Code* code, SharedFunctionInfo* shared) {
502 virtual void CodeDeleteEvent(Address from) {
503 address_to_name_map_.Remove(from);
506 const char* Lookup(Address address) {
507 return address_to_name_map_.Lookup(address);
513 NameMap() : impl_(HashMap::PointersMatch) {}
516 for (HashMap::Entry* p = impl_.Start(); p != NULL; p = impl_.Next(p)) {
517 DeleteArray(static_cast<const char*>(p->value));
521 void Insert(Address code_address, const char* name, int name_size) {
522 HashMap::Entry* entry = FindOrCreateEntry(code_address);
523 if (entry->value == NULL) {
524 entry->value = CopyName(name, name_size);
528 const char* Lookup(Address code_address) {
529 HashMap::Entry* entry = FindEntry(code_address);
530 return (entry != NULL) ? static_cast<const char*>(entry->value) : NULL;
533 void Remove(Address code_address) {
534 HashMap::Entry* entry = FindEntry(code_address);
536 DeleteArray(static_cast<char*>(entry->value));
541 void Move(Address from, Address to) {
542 if (from == to) return;
543 HashMap::Entry* from_entry = FindEntry(from);
544 DCHECK(from_entry != NULL);
545 void* value = from_entry->value;
546 RemoveEntry(from_entry);
547 HashMap::Entry* to_entry = FindOrCreateEntry(to);
548 DCHECK(to_entry->value == NULL);
549 to_entry->value = value;
553 static char* CopyName(const char* name, int name_size) {
554 char* result = NewArray<char>(name_size + 1);
555 for (int i = 0; i < name_size; ++i) {
557 if (c == '\0') c = ' ';
560 result[name_size] = '\0';
564 HashMap::Entry* FindOrCreateEntry(Address code_address) {
565 return impl_.Lookup(code_address, ComputePointerHash(code_address), true);
568 HashMap::Entry* FindEntry(Address code_address) {
569 return impl_.Lookup(code_address,
570 ComputePointerHash(code_address),
574 void RemoveEntry(HashMap::Entry* entry) {
575 impl_.Remove(entry->key, entry->hash);
580 DISALLOW_COPY_AND_ASSIGN(NameMap);
583 virtual void LogRecordedBuffer(Code* code,
587 address_to_name_map_.Insert(code->address(), name, length);
590 NameMap address_to_name_map_;
595 Deserializer::Deserializer(SnapshotByteSource* source)
597 attached_objects_(NULL),
599 external_reference_decoder_(NULL) {
600 for (int i = 0; i < LAST_SPACE + 1; i++) {
601 reservations_[i] = kUninitializedReservation;
606 void Deserializer::FlushICacheForNewCodeObjects() {
607 PageIterator it(isolate_->heap()->code_space());
608 while (it.has_next()) {
610 CpuFeatures::FlushICache(p->area_start(), p->area_end() - p->area_start());
615 void Deserializer::Deserialize(Isolate* isolate) {
617 DCHECK(isolate_ != NULL);
618 isolate_->heap()->ReserveSpace(reservations_, &high_water_[0]);
619 // No active threads.
620 DCHECK_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse());
621 // No active handles.
622 DCHECK(isolate_->handle_scope_implementer()->blocks()->is_empty());
623 DCHECK_EQ(NULL, external_reference_decoder_);
624 external_reference_decoder_ = new ExternalReferenceDecoder(isolate);
625 isolate_->heap()->IterateSmiRoots(this);
626 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
627 isolate_->heap()->RepairFreeListsAfterBoot();
628 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
630 isolate_->heap()->set_native_contexts_list(
631 isolate_->heap()->undefined_value());
632 isolate_->heap()->set_array_buffers_list(
633 isolate_->heap()->undefined_value());
635 // The allocation site list is build during root iteration, but if no sites
636 // were encountered then it needs to be initialized to undefined.
637 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
638 isolate_->heap()->set_allocation_sites_list(
639 isolate_->heap()->undefined_value());
642 isolate_->heap()->InitializeWeakObjectToCodeTable();
644 // Update data pointers to the external strings containing natives sources.
645 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
646 Object* source = isolate_->heap()->natives_source_cache()->get(i);
647 if (!source->IsUndefined()) {
648 ExternalOneByteString::cast(source)->update_data_cache();
652 FlushICacheForNewCodeObjects();
654 // Issue code events for newly deserialized code objects.
655 LOG_CODE_EVENT(isolate_, LogCodeObjects());
656 LOG_CODE_EVENT(isolate_, LogCompiledFunctions());
660 void Deserializer::DeserializePartial(Isolate* isolate, Object** root) {
662 for (int i = NEW_SPACE; i < kNumberOfSpaces; i++) {
663 DCHECK(reservations_[i] != kUninitializedReservation);
665 isolate_->heap()->ReserveSpace(reservations_, &high_water_[0]);
666 if (external_reference_decoder_ == NULL) {
667 external_reference_decoder_ = new ExternalReferenceDecoder(isolate);
670 DisallowHeapAllocation no_gc;
672 // Keep track of the code space start and end pointers in case new
673 // code objects were unserialized
674 OldSpace* code_space = isolate_->heap()->code_space();
675 Address start_address = code_space->top();
678 // There's no code deserialized here. If this assert fires
679 // then that's changed and logging should be added to notify
680 // the profiler et al of the new code.
681 CHECK_EQ(start_address, code_space->top());
685 Deserializer::~Deserializer() {
686 // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed.
687 // DCHECK(source_->AtEOF());
688 if (external_reference_decoder_) {
689 delete external_reference_decoder_;
690 external_reference_decoder_ = NULL;
692 if (attached_objects_) attached_objects_->Dispose();
696 // This is called on the roots. It is the driver of the deserialization
697 // process. It is also called on the body of each function.
698 void Deserializer::VisitPointers(Object** start, Object** end) {
699 // The space must be new space. Any other space would cause ReadChunk to try
700 // to update the remembered using NULL as the address.
701 ReadChunk(start, end, NEW_SPACE, NULL);
705 void Deserializer::RelinkAllocationSite(AllocationSite* site) {
706 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
707 site->set_weak_next(isolate_->heap()->undefined_value());
709 site->set_weak_next(isolate_->heap()->allocation_sites_list());
711 isolate_->heap()->set_allocation_sites_list(site);
715 // Used to insert a deserialized internalized string into the string table.
716 class StringTableInsertionKey : public HashTableKey {
718 explicit StringTableInsertionKey(String* string)
719 : string_(string), hash_(HashForObject(string)) {
720 DCHECK(string->IsInternalizedString());
723 virtual bool IsMatch(Object* string) {
724 // We know that all entries in a hash table had their hash keys created.
725 // Use that knowledge to have fast failure.
726 if (hash_ != HashForObject(string)) return false;
727 // We want to compare the content of two internalized strings here.
728 return string_->SlowEquals(String::cast(string));
731 virtual uint32_t Hash() OVERRIDE { return hash_; }
733 virtual uint32_t HashForObject(Object* key) OVERRIDE {
734 return String::cast(key)->Hash();
737 MUST_USE_RESULT virtual Handle<Object> AsHandle(Isolate* isolate)
739 return handle(string_, isolate);
747 HeapObject* Deserializer::ProcessNewObjectFromSerializedCode(HeapObject* obj) {
748 if (obj->IsString()) {
749 String* string = String::cast(obj);
750 // Uninitialize hash field as the hash seed may have changed.
751 string->set_hash_field(String::kEmptyHashField);
752 if (string->IsInternalizedString()) {
753 DisallowHeapAllocation no_gc;
754 HandleScope scope(isolate_);
755 StringTableInsertionKey key(string);
756 String* canonical = *StringTable::LookupKey(isolate_, &key);
757 string->SetForwardedInternalizedString(canonical);
765 Object* Deserializer::ProcessBackRefInSerializedCode(Object* obj) {
766 if (obj->IsInternalizedString()) {
767 return String::cast(obj)->GetForwardedInternalizedString();
773 // This routine writes the new object into the pointer provided and then
774 // returns true if the new object was in young space and false otherwise.
775 // The reason for this strange interface is that otherwise the object is
776 // written very late, which means the FreeSpace map is not set up by the
777 // time we need to use it to mark the space at the end of a page free.
778 void Deserializer::ReadObject(int space_number,
779 Object** write_back) {
780 int size = source_->GetInt() << kObjectAlignmentBits;
781 Address address = Allocate(space_number, size);
782 HeapObject* obj = HeapObject::FromAddress(address);
783 isolate_->heap()->OnAllocationEvent(obj, size);
784 Object** current = reinterpret_cast<Object**>(address);
785 Object** limit = current + (size >> kPointerSizeLog2);
786 if (FLAG_log_snapshot_positions) {
787 LOG(isolate_, SnapshotPositionEvent(address, source_->position()));
789 ReadChunk(current, limit, space_number, address);
791 // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
792 // as a (weak) root. If this root is relocated correctly,
793 // RelinkAllocationSite() isn't necessary.
794 if (obj->IsAllocationSite()) RelinkAllocationSite(AllocationSite::cast(obj));
796 // Fix up strings from serialized user code.
797 if (deserializing_user_code()) obj = ProcessNewObjectFromSerializedCode(obj);
801 bool is_codespace = (space_number == CODE_SPACE);
802 DCHECK(obj->IsCode() == is_codespace);
806 void Deserializer::ReadChunk(Object** current,
809 Address current_object_address) {
810 Isolate* const isolate = isolate_;
811 // Write barrier support costs around 1% in startup time. In fact there
812 // are no new space objects in current boot snapshots, so it's not needed,
813 // but that may change.
814 bool write_barrier_needed = (current_object_address != NULL &&
815 source_space != NEW_SPACE &&
816 source_space != CELL_SPACE &&
817 source_space != PROPERTY_CELL_SPACE &&
818 source_space != CODE_SPACE &&
819 source_space != OLD_DATA_SPACE);
820 while (current < limit) {
821 int data = source_->Get();
823 #define CASE_STATEMENT(where, how, within, space_number) \
824 case where + how + within + space_number: \
825 STATIC_ASSERT((where & ~kPointedToMask) == 0); \
826 STATIC_ASSERT((how & ~kHowToCodeMask) == 0); \
827 STATIC_ASSERT((within & ~kWhereToPointMask) == 0); \
828 STATIC_ASSERT((space_number & ~kSpaceMask) == 0);
830 #define CASE_BODY(where, how, within, space_number_if_any) \
832 bool emit_write_barrier = false; \
833 bool current_was_incremented = false; \
834 int space_number = space_number_if_any == kAnyOldSpace \
835 ? (data & kSpaceMask) \
836 : space_number_if_any; \
837 if (where == kNewObject && how == kPlain && within == kStartOfObject) { \
838 ReadObject(space_number, current); \
839 emit_write_barrier = (space_number == NEW_SPACE); \
841 Object* new_object = NULL; /* May not be a real Object pointer. */ \
842 if (where == kNewObject) { \
843 ReadObject(space_number, &new_object); \
844 } else if (where == kRootArray) { \
845 int root_id = source_->GetInt(); \
846 new_object = isolate->heap()->roots_array_start()[root_id]; \
847 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
848 } else if (where == kPartialSnapshotCache) { \
849 int cache_index = source_->GetInt(); \
850 new_object = isolate->serialize_partial_snapshot_cache()[cache_index]; \
851 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
852 } else if (where == kExternalReference) { \
853 int skip = source_->GetInt(); \
854 current = reinterpret_cast<Object**>( \
855 reinterpret_cast<Address>(current) + skip); \
856 int reference_id = source_->GetInt(); \
857 Address address = external_reference_decoder_->Decode(reference_id); \
858 new_object = reinterpret_cast<Object*>(address); \
859 } else if (where == kBackref) { \
860 emit_write_barrier = (space_number == NEW_SPACE); \
861 new_object = GetAddressFromEnd(data & kSpaceMask); \
862 if (deserializing_user_code()) { \
863 new_object = ProcessBackRefInSerializedCode(new_object); \
865 } else if (where == kBuiltin) { \
866 DCHECK(deserializing_user_code()); \
867 int builtin_id = source_->GetInt(); \
868 DCHECK_LE(0, builtin_id); \
869 DCHECK_LT(builtin_id, Builtins::builtin_count); \
870 Builtins::Name name = static_cast<Builtins::Name>(builtin_id); \
871 new_object = isolate->builtins()->builtin(name); \
872 emit_write_barrier = false; \
873 } else if (where == kAttachedReference) { \
874 DCHECK(deserializing_user_code()); \
875 int index = source_->GetInt(); \
876 new_object = *attached_objects_->at(index); \
877 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
879 DCHECK(where == kBackrefWithSkip); \
880 int skip = source_->GetInt(); \
881 current = reinterpret_cast<Object**>( \
882 reinterpret_cast<Address>(current) + skip); \
883 emit_write_barrier = (space_number == NEW_SPACE); \
884 new_object = GetAddressFromEnd(data & kSpaceMask); \
885 if (deserializing_user_code()) { \
886 new_object = ProcessBackRefInSerializedCode(new_object); \
889 if (within == kInnerPointer) { \
890 if (space_number != CODE_SPACE || new_object->IsCode()) { \
891 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
893 reinterpret_cast<Object*>(new_code_object->instruction_start()); \
895 DCHECK(space_number == CODE_SPACE); \
896 Cell* cell = Cell::cast(new_object); \
897 new_object = reinterpret_cast<Object*>(cell->ValueAddress()); \
900 if (how == kFromCode) { \
901 Address location_of_branch_data = reinterpret_cast<Address>(current); \
902 Assembler::deserialization_set_special_target_at( \
903 location_of_branch_data, \
904 Code::cast(HeapObject::FromAddress(current_object_address)), \
905 reinterpret_cast<Address>(new_object)); \
906 location_of_branch_data += Assembler::kSpecialTargetSize; \
907 current = reinterpret_cast<Object**>(location_of_branch_data); \
908 current_was_incremented = true; \
910 *current = new_object; \
913 if (emit_write_barrier && write_barrier_needed) { \
914 Address current_address = reinterpret_cast<Address>(current); \
915 isolate->heap()->RecordWrite( \
916 current_object_address, \
917 static_cast<int>(current_address - current_object_address)); \
919 if (!current_was_incremented) { \
925 // This generates a case and a body for the new space (which has to do extra
926 // write barrier handling) and handles the other spaces with 8 fall-through
927 // cases and one body.
928 #define ALL_SPACES(where, how, within) \
929 CASE_STATEMENT(where, how, within, NEW_SPACE) \
930 CASE_BODY(where, how, within, NEW_SPACE) \
931 CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
932 CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
933 CASE_STATEMENT(where, how, within, CODE_SPACE) \
934 CASE_STATEMENT(where, how, within, CELL_SPACE) \
935 CASE_STATEMENT(where, how, within, PROPERTY_CELL_SPACE) \
936 CASE_STATEMENT(where, how, within, MAP_SPACE) \
937 CASE_BODY(where, how, within, kAnyOldSpace)
939 #define FOUR_CASES(byte_code) \
941 case byte_code + 1: \
942 case byte_code + 2: \
945 #define SIXTEEN_CASES(byte_code) \
946 FOUR_CASES(byte_code) \
947 FOUR_CASES(byte_code + 4) \
948 FOUR_CASES(byte_code + 8) \
949 FOUR_CASES(byte_code + 12)
951 #define COMMON_RAW_LENGTHS(f) \
984 // We generate 15 cases and bodies that process special tags that combine
985 // the raw data tag and the length into one byte.
986 #define RAW_CASE(index) \
987 case kRawData + index: { \
988 byte* raw_data_out = reinterpret_cast<byte*>(current); \
989 source_->CopyRaw(raw_data_out, index * kPointerSize); \
991 reinterpret_cast<Object**>(raw_data_out + index * kPointerSize); \
994 COMMON_RAW_LENGTHS(RAW_CASE)
997 // Deserialize a chunk of raw data that doesn't have one of the popular
1000 int size = source_->GetInt();
1001 byte* raw_data_out = reinterpret_cast<byte*>(current);
1002 source_->CopyRaw(raw_data_out, size);
1006 SIXTEEN_CASES(kRootArrayConstants + kNoSkipDistance)
1007 SIXTEEN_CASES(kRootArrayConstants + kNoSkipDistance + 16) {
1008 int root_id = RootArrayConstantFromByteCode(data);
1009 Object* object = isolate->heap()->roots_array_start()[root_id];
1010 DCHECK(!isolate->heap()->InNewSpace(object));
1011 *current++ = object;
1015 SIXTEEN_CASES(kRootArrayConstants + kHasSkipDistance)
1016 SIXTEEN_CASES(kRootArrayConstants + kHasSkipDistance + 16) {
1017 int root_id = RootArrayConstantFromByteCode(data);
1018 int skip = source_->GetInt();
1019 current = reinterpret_cast<Object**>(
1020 reinterpret_cast<intptr_t>(current) + skip);
1021 Object* object = isolate->heap()->roots_array_start()[root_id];
1022 DCHECK(!isolate->heap()->InNewSpace(object));
1023 *current++ = object;
1028 int repeats = source_->GetInt();
1029 Object* object = current[-1];
1030 DCHECK(!isolate->heap()->InNewSpace(object));
1031 for (int i = 0; i < repeats; i++) current[i] = object;
1036 STATIC_ASSERT(kRootArrayNumberOfConstantEncodings ==
1037 Heap::kOldSpaceRoots);
1038 STATIC_ASSERT(kMaxRepeats == 13);
1039 case kConstantRepeat:
1040 FOUR_CASES(kConstantRepeat + 1)
1041 FOUR_CASES(kConstantRepeat + 5)
1042 FOUR_CASES(kConstantRepeat + 9) {
1043 int repeats = RepeatsForCode(data);
1044 Object* object = current[-1];
1045 DCHECK(!isolate->heap()->InNewSpace(object));
1046 for (int i = 0; i < repeats; i++) current[i] = object;
1051 // Deserialize a new object and write a pointer to it to the current
1053 ALL_SPACES(kNewObject, kPlain, kStartOfObject)
1054 // Support for direct instruction pointers in functions. It's an inner
1055 // pointer because it points at the entry point, not at the start of the
1057 CASE_STATEMENT(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
1058 CASE_BODY(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
1059 // Deserialize a new code object and write a pointer to its first
1060 // instruction to the current code object.
1061 ALL_SPACES(kNewObject, kFromCode, kInnerPointer)
1062 // Find a recently deserialized object using its offset from the current
1063 // allocation point and write a pointer to it to the current object.
1064 ALL_SPACES(kBackref, kPlain, kStartOfObject)
1065 ALL_SPACES(kBackrefWithSkip, kPlain, kStartOfObject)
1066 #if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL || \
1067 defined(V8_TARGET_ARCH_MIPS64)
1068 // Deserialize a new object from pointer found in code and write
1069 // a pointer to it to the current object. Required only for MIPS or ARM
1070 // with ool constant pool, and omitted on the other architectures because
1071 // it is fully unrolled and would cause bloat.
1072 ALL_SPACES(kNewObject, kFromCode, kStartOfObject)
1073 // Find a recently deserialized code object using its offset from the
1074 // current allocation point and write a pointer to it to the current
1075 // object. Required only for MIPS or ARM with ool constant pool.
1076 ALL_SPACES(kBackref, kFromCode, kStartOfObject)
1077 ALL_SPACES(kBackrefWithSkip, kFromCode, kStartOfObject)
1079 // Find a recently deserialized code object using its offset from the
1080 // current allocation point and write a pointer to its first instruction
1081 // to the current code object or the instruction pointer in a function
1083 ALL_SPACES(kBackref, kFromCode, kInnerPointer)
1084 ALL_SPACES(kBackrefWithSkip, kFromCode, kInnerPointer)
1085 ALL_SPACES(kBackref, kPlain, kInnerPointer)
1086 ALL_SPACES(kBackrefWithSkip, kPlain, kInnerPointer)
1087 // Find an object in the roots array and write a pointer to it to the
1089 CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
1090 CASE_BODY(kRootArray, kPlain, kStartOfObject, 0)
1091 #if defined(V8_TARGET_ARCH_MIPS) || V8_OOL_CONSTANT_POOL || \
1092 defined(V8_TARGET_ARCH_MIPS64)
1093 // Find an object in the roots array and write a pointer to it to in code.
1094 CASE_STATEMENT(kRootArray, kFromCode, kStartOfObject, 0)
1095 CASE_BODY(kRootArray, kFromCode, kStartOfObject, 0)
1097 // Find an object in the partial snapshots cache and write a pointer to it
1098 // to the current object.
1099 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
1100 CASE_BODY(kPartialSnapshotCache,
1104 // Find an code entry in the partial snapshots cache and
1105 // write a pointer to it to the current object.
1106 CASE_STATEMENT(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
1107 CASE_BODY(kPartialSnapshotCache,
1111 // Find an external reference and write a pointer to it to the current
1113 CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
1114 CASE_BODY(kExternalReference,
1118 // Find an external reference and write a pointer to it in the current
1120 CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
1121 CASE_BODY(kExternalReference,
1125 // Find a builtin and write a pointer to it to the current object.
1126 CASE_STATEMENT(kBuiltin, kPlain, kStartOfObject, 0)
1127 CASE_BODY(kBuiltin, kPlain, kStartOfObject, 0)
1128 #if V8_OOL_CONSTANT_POOL
1129 // Find a builtin code entry and write a pointer to it to the current
1131 CASE_STATEMENT(kBuiltin, kPlain, kInnerPointer, 0)
1132 CASE_BODY(kBuiltin, kPlain, kInnerPointer, 0)
1134 // Find a builtin and write a pointer to it in the current code object.
1135 CASE_STATEMENT(kBuiltin, kFromCode, kInnerPointer, 0)
1136 CASE_BODY(kBuiltin, kFromCode, kInnerPointer, 0)
1137 // Find an object in the attached references and write a pointer to it to
1138 // the current object.
1139 CASE_STATEMENT(kAttachedReference, kPlain, kStartOfObject, 0)
1140 CASE_BODY(kAttachedReference, kPlain, kStartOfObject, 0)
1141 CASE_STATEMENT(kAttachedReference, kPlain, kInnerPointer, 0)
1142 CASE_BODY(kAttachedReference, kPlain, kInnerPointer, 0)
1143 CASE_STATEMENT(kAttachedReference, kFromCode, kInnerPointer, 0)
1144 CASE_BODY(kAttachedReference, kFromCode, kInnerPointer, 0)
1146 #undef CASE_STATEMENT
1151 int size = source_->GetInt();
1152 current = reinterpret_cast<Object**>(
1153 reinterpret_cast<intptr_t>(current) + size);
1157 case kNativesStringResource: {
1158 int index = source_->Get();
1159 Vector<const char> source_vector = Natives::GetRawScriptSource(index);
1160 NativesExternalStringResource* resource =
1161 new NativesExternalStringResource(isolate->bootstrapper(),
1162 source_vector.start(),
1163 source_vector.length());
1164 *current++ = reinterpret_cast<Object*>(resource);
1168 case kSynchronize: {
1169 // If we get here then that indicates that you have a mismatch between
1170 // the number of GC roots when serializing and deserializing.
1178 DCHECK_EQ(limit, current);
1182 Serializer::Serializer(Isolate* isolate, SnapshotByteSink* sink)
1183 : isolate_(isolate),
1185 external_reference_encoder_(new ExternalReferenceEncoder(isolate)),
1186 root_index_wave_front_(0),
1187 code_address_map_(NULL) {
1188 // The serializer is meant to be used only to generate initial heap images
1189 // from a context in which there is only one isolate.
1190 for (int i = 0; i <= LAST_SPACE; i++) {
1196 Serializer::~Serializer() {
1197 delete external_reference_encoder_;
1198 if (code_address_map_ != NULL) delete code_address_map_;
1202 void StartupSerializer::SerializeStrongReferences() {
1203 Isolate* isolate = this->isolate();
1204 // No active threads.
1205 CHECK_EQ(NULL, isolate->thread_manager()->FirstThreadStateInUse());
1206 // No active or weak handles.
1207 CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
1208 CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
1209 CHECK_EQ(0, isolate->eternal_handles()->NumberOfHandles());
1210 // We don't support serializing installed extensions.
1211 CHECK(!isolate->has_installed_extensions());
1212 isolate->heap()->IterateSmiRoots(this);
1213 isolate->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
1217 void PartialSerializer::Serialize(Object** object) {
1218 this->VisitPointer(object);
1223 bool Serializer::ShouldBeSkipped(Object** current) {
1224 Object** roots = isolate()->heap()->roots_array_start();
1225 return current == &roots[Heap::kStoreBufferTopRootIndex]
1226 || current == &roots[Heap::kStackLimitRootIndex]
1227 || current == &roots[Heap::kRealStackLimitRootIndex];
1231 void Serializer::VisitPointers(Object** start, Object** end) {
1232 Isolate* isolate = this->isolate();;
1234 for (Object** current = start; current < end; current++) {
1235 if (start == isolate->heap()->roots_array_start()) {
1236 root_index_wave_front_ =
1237 Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
1239 if (ShouldBeSkipped(current)) {
1240 sink_->Put(kSkip, "Skip");
1241 sink_->PutInt(kPointerSize, "SkipOneWord");
1242 } else if ((*current)->IsSmi()) {
1243 sink_->Put(kRawData + 1, "Smi");
1244 for (int i = 0; i < kPointerSize; i++) {
1245 sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1248 SerializeObject(*current, kPlain, kStartOfObject, 0);
1254 // This ensures that the partial snapshot cache keeps things alive during GC and
1255 // tracks their movement. When it is called during serialization of the startup
1256 // snapshot nothing happens. When the partial (context) snapshot is created,
1257 // this array is populated with the pointers that the partial snapshot will
1258 // need. As that happens we emit serialized objects to the startup snapshot
1259 // that correspond to the elements of this cache array. On deserialization we
1260 // therefore need to visit the cache array. This fills it up with pointers to
1261 // deserialized objects.
1262 void SerializerDeserializer::Iterate(Isolate* isolate,
1263 ObjectVisitor* visitor) {
1264 if (isolate->serializer_enabled()) return;
1265 for (int i = 0; ; i++) {
1266 if (isolate->serialize_partial_snapshot_cache_length() <= i) {
1267 // Extend the array ready to get a value from the visitor when
1269 isolate->PushToPartialSnapshotCache(Smi::FromInt(0));
1271 Object** cache = isolate->serialize_partial_snapshot_cache();
1272 visitor->VisitPointers(&cache[i], &cache[i + 1]);
1273 // Sentinel is the undefined object, which is a root so it will not normally
1274 // be found in the cache.
1275 if (cache[i] == isolate->heap()->undefined_value()) {
1282 int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1283 Isolate* isolate = this->isolate();
1286 i < isolate->serialize_partial_snapshot_cache_length();
1288 Object* entry = isolate->serialize_partial_snapshot_cache()[i];
1289 if (entry == heap_object) return i;
1292 // We didn't find the object in the cache. So we add it to the cache and
1293 // then visit the pointer so that it becomes part of the startup snapshot
1294 // and we can refer to it from the partial snapshot.
1295 int length = isolate->serialize_partial_snapshot_cache_length();
1296 isolate->PushToPartialSnapshotCache(heap_object);
1297 startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object));
1298 // We don't recurse from the startup snapshot generator into the partial
1299 // snapshot generator.
1300 DCHECK(length == isolate->serialize_partial_snapshot_cache_length() - 1);
1305 int Serializer::RootIndex(HeapObject* heap_object, HowToCode from) {
1306 Heap* heap = isolate()->heap();
1307 if (heap->InNewSpace(heap_object)) return kInvalidRootIndex;
1308 for (int i = 0; i < root_index_wave_front_; i++) {
1309 Object* root = heap->roots_array_start()[i];
1310 if (!root->IsSmi() && root == heap_object) {
1314 return kInvalidRootIndex;
1318 // Encode the location of an already deserialized object in order to write its
1319 // location into a later object. We can encode the location as an offset from
1320 // the start of the deserialized objects or as an offset backwards from the
1321 // current allocation pointer.
1322 void Serializer::SerializeReferenceToPreviousObject(HeapObject* heap_object,
1323 HowToCode how_to_code,
1324 WhereToPoint where_to_point,
1326 int space = SpaceOfObject(heap_object);
1327 int address = address_mapper_.MappedTo(heap_object);
1328 int offset = CurrentAllocationAddress(space) - address;
1329 // Shift out the bits that are always 0.
1330 offset >>= kObjectAlignmentBits;
1332 sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1334 sink_->Put(kBackrefWithSkip + how_to_code + where_to_point + space,
1335 "BackRefSerWithSkip");
1336 sink_->PutInt(skip, "BackRefSkipDistance");
1338 sink_->PutInt(offset, "offset");
1342 void StartupSerializer::SerializeObject(
1344 HowToCode how_to_code,
1345 WhereToPoint where_to_point,
1347 CHECK(o->IsHeapObject());
1348 HeapObject* heap_object = HeapObject::cast(o);
1349 DCHECK(!heap_object->IsJSFunction());
1352 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1353 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1357 if (address_mapper_.IsMapped(heap_object)) {
1358 SerializeReferenceToPreviousObject(heap_object, how_to_code, where_to_point,
1362 sink_->Put(kSkip, "FlushPendingSkip");
1363 sink_->PutInt(skip, "SkipDistance");
1366 // Object has not yet been serialized. Serialize it here.
1367 ObjectSerializer object_serializer(this,
1372 object_serializer.Serialize();
1377 void StartupSerializer::SerializeWeakReferences() {
1378 // This phase comes right after the partial serialization (of the snapshot).
1379 // After we have done the partial serialization the partial snapshot cache
1380 // will contain some references needed to decode the partial snapshot. We
1381 // add one entry with 'undefined' which is the sentinel that the deserializer
1382 // uses to know it is done deserializing the array.
1383 Object* undefined = isolate()->heap()->undefined_value();
1384 VisitPointer(&undefined);
1385 isolate()->heap()->IterateWeakRoots(this, VISIT_ALL);
1390 void Serializer::PutRoot(int root_index,
1392 SerializerDeserializer::HowToCode how_to_code,
1393 SerializerDeserializer::WhereToPoint where_to_point,
1395 if (how_to_code == kPlain &&
1396 where_to_point == kStartOfObject &&
1397 root_index < kRootArrayNumberOfConstantEncodings &&
1398 !isolate()->heap()->InNewSpace(object)) {
1400 sink_->Put(kRootArrayConstants + kNoSkipDistance + root_index,
1403 sink_->Put(kRootArrayConstants + kHasSkipDistance + root_index,
1405 sink_->PutInt(skip, "SkipInPutRoot");
1409 sink_->Put(kSkip, "SkipFromPutRoot");
1410 sink_->PutInt(skip, "SkipFromPutRootDistance");
1412 sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
1413 sink_->PutInt(root_index, "root_index");
1418 void PartialSerializer::SerializeObject(
1420 HowToCode how_to_code,
1421 WhereToPoint where_to_point,
1423 CHECK(o->IsHeapObject());
1424 HeapObject* heap_object = HeapObject::cast(o);
1426 if (heap_object->IsMap()) {
1427 // The code-caches link to context-specific code objects, which
1428 // the startup and context serializes cannot currently handle.
1429 DCHECK(Map::cast(heap_object)->code_cache() ==
1430 heap_object->GetHeap()->empty_fixed_array());
1434 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1435 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1439 if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1441 sink_->Put(kSkip, "SkipFromSerializeObject");
1442 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1445 int cache_index = PartialSnapshotCacheIndex(heap_object);
1446 sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1447 "PartialSnapshotCache");
1448 sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1452 // Pointers from the partial snapshot to the objects in the startup snapshot
1453 // should go through the root array or through the partial snapshot cache.
1454 // If this is not the case you may have to add something to the root array.
1455 DCHECK(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1456 // All the internalized strings that the partial snapshot needs should be
1457 // either in the root table or in the partial snapshot cache.
1458 DCHECK(!heap_object->IsInternalizedString());
1460 if (address_mapper_.IsMapped(heap_object)) {
1461 SerializeReferenceToPreviousObject(heap_object, how_to_code, where_to_point,
1465 sink_->Put(kSkip, "SkipFromSerializeObject");
1466 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1468 // Object has not yet been serialized. Serialize it here.
1469 ObjectSerializer serializer(this,
1474 serializer.Serialize();
1479 void Serializer::ObjectSerializer::Serialize() {
1480 int space = Serializer::SpaceOfObject(object_);
1481 int size = object_->Size();
1483 sink_->Put(kNewObject + reference_representation_ + space,
1484 "ObjectSerialization");
1485 sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1487 if (serializer_->code_address_map_) {
1488 const char* code_name =
1489 serializer_->code_address_map_->Lookup(object_->address());
1490 LOG(serializer_->isolate_,
1491 CodeNameEvent(object_->address(), sink_->Position(), code_name));
1492 LOG(serializer_->isolate_,
1493 SnapshotPositionEvent(object_->address(), sink_->Position()));
1496 // Mark this object as already serialized.
1497 int offset = serializer_->Allocate(space, size);
1498 serializer_->address_mapper()->AddMapping(object_, offset);
1500 // Serialize the map (first word of the object).
1501 serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject, 0);
1503 // Serialize the rest of the object.
1504 CHECK_EQ(0, bytes_processed_so_far_);
1505 bytes_processed_so_far_ = kPointerSize;
1506 object_->IterateBody(object_->map()->instance_type(), size, this);
1507 OutputRawData(object_->address() + size);
1511 void Serializer::ObjectSerializer::VisitPointers(Object** start,
1513 Object** current = start;
1514 while (current < end) {
1515 while (current < end && (*current)->IsSmi()) current++;
1516 if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1518 while (current < end && !(*current)->IsSmi()) {
1519 HeapObject* current_contents = HeapObject::cast(*current);
1520 int root_index = serializer_->RootIndex(current_contents, kPlain);
1521 // Repeats are not subject to the write barrier so there are only some
1522 // objects that can be used in a repeat encoding. These are the early
1523 // ones in the root array that are never in new space.
1524 if (current != start &&
1525 root_index != kInvalidRootIndex &&
1526 root_index < kRootArrayNumberOfConstantEncodings &&
1527 current_contents == current[-1]) {
1528 DCHECK(!serializer_->isolate()->heap()->InNewSpace(current_contents));
1529 int repeat_count = 1;
1530 while (¤t[repeat_count] < end - 1 &&
1531 current[repeat_count] == current_contents) {
1534 current += repeat_count;
1535 bytes_processed_so_far_ += repeat_count * kPointerSize;
1536 if (repeat_count > kMaxRepeats) {
1537 sink_->Put(kRepeat, "SerializeRepeats");
1538 sink_->PutInt(repeat_count, "SerializeRepeats");
1540 sink_->Put(CodeForRepeats(repeat_count), "SerializeRepeats");
1543 serializer_->SerializeObject(
1544 current_contents, kPlain, kStartOfObject, 0);
1545 bytes_processed_so_far_ += kPointerSize;
1553 void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
1554 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1555 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1557 int skip = OutputRawData(rinfo->target_address_address(),
1558 kCanReturnSkipInsteadOfSkipping);
1559 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1560 Object* object = rinfo->target_object();
1561 serializer_->SerializeObject(object, how_to_code, kStartOfObject, skip);
1562 bytes_processed_so_far_ += rinfo->target_address_size();
1566 void Serializer::ObjectSerializer::VisitExternalReference(Address* p) {
1567 int skip = OutputRawData(reinterpret_cast<Address>(p),
1568 kCanReturnSkipInsteadOfSkipping);
1569 sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
1570 sink_->PutInt(skip, "SkipB4ExternalRef");
1571 Address target = *p;
1572 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1573 bytes_processed_so_far_ += kPointerSize;
1577 void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
1578 int skip = OutputRawData(rinfo->target_address_address(),
1579 kCanReturnSkipInsteadOfSkipping);
1580 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1581 sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
1582 sink_->PutInt(skip, "SkipB4ExternalRef");
1583 Address target = rinfo->target_reference();
1584 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1585 bytes_processed_so_far_ += rinfo->target_address_size();
1589 void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1590 int skip = OutputRawData(rinfo->target_address_address(),
1591 kCanReturnSkipInsteadOfSkipping);
1592 HowToCode how_to_code = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1593 sink_->Put(kExternalReference + how_to_code + kStartOfObject, "ExternalRef");
1594 sink_->PutInt(skip, "SkipB4ExternalRef");
1595 Address target = rinfo->target_address();
1596 sink_->PutInt(serializer_->EncodeExternalReference(target), "reference id");
1597 bytes_processed_so_far_ += rinfo->target_address_size();
1601 void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1602 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1603 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1605 int skip = OutputRawData(rinfo->target_address_address(),
1606 kCanReturnSkipInsteadOfSkipping);
1607 Code* object = Code::GetCodeFromTargetAddress(rinfo->target_address());
1608 serializer_->SerializeObject(object, kFromCode, kInnerPointer, skip);
1609 bytes_processed_so_far_ += rinfo->target_address_size();
1613 void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1614 int skip = OutputRawData(entry_address, kCanReturnSkipInsteadOfSkipping);
1615 Code* object = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1616 serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
1617 bytes_processed_so_far_ += kPointerSize;
1621 void Serializer::ObjectSerializer::VisitCell(RelocInfo* rinfo) {
1622 // Out-of-line constant pool entries will be visited by the ConstantPoolArray.
1623 if (FLAG_enable_ool_constant_pool && rinfo->IsInConstantPool()) return;
1625 int skip = OutputRawData(rinfo->pc(), kCanReturnSkipInsteadOfSkipping);
1626 Cell* object = Cell::cast(rinfo->target_cell());
1627 serializer_->SerializeObject(object, kPlain, kInnerPointer, skip);
1628 bytes_processed_so_far_ += kPointerSize;
1632 void Serializer::ObjectSerializer::VisitExternalOneByteString(
1633 v8::String::ExternalOneByteStringResource** resource_pointer) {
1634 Address references_start = reinterpret_cast<Address>(resource_pointer);
1635 OutputRawData(references_start);
1636 for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1638 serializer_->isolate()->heap()->natives_source_cache()->get(i);
1639 if (!source->IsUndefined()) {
1640 ExternalOneByteString* string = ExternalOneByteString::cast(source);
1641 typedef v8::String::ExternalOneByteStringResource Resource;
1642 const Resource* resource = string->resource();
1643 if (resource == *resource_pointer) {
1644 sink_->Put(kNativesStringResource, "NativesStringResource");
1645 sink_->PutSection(i, "NativesStringResourceEnd");
1646 bytes_processed_so_far_ += sizeof(resource);
1651 // One of the strings in the natives cache should match the resource. We
1652 // can't serialize any other kinds of external strings.
1657 static Code* CloneCodeObject(HeapObject* code) {
1658 Address copy = new byte[code->Size()];
1659 MemCopy(copy, code->address(), code->Size());
1660 return Code::cast(HeapObject::FromAddress(copy));
1664 static void WipeOutRelocations(Code* code) {
1666 RelocInfo::kCodeTargetMask |
1667 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
1668 RelocInfo::ModeMask(RelocInfo::EXTERNAL_REFERENCE) |
1669 RelocInfo::ModeMask(RelocInfo::RUNTIME_ENTRY);
1670 for (RelocIterator it(code, mode_mask); !it.done(); it.next()) {
1671 if (!(FLAG_enable_ool_constant_pool && it.rinfo()->IsInConstantPool())) {
1672 it.rinfo()->WipeOut();
1678 int Serializer::ObjectSerializer::OutputRawData(
1679 Address up_to, Serializer::ObjectSerializer::ReturnSkip return_skip) {
1680 Address object_start = object_->address();
1681 int base = bytes_processed_so_far_;
1682 int up_to_offset = static_cast<int>(up_to - object_start);
1683 int to_skip = up_to_offset - bytes_processed_so_far_;
1684 int bytes_to_output = to_skip;
1685 bytes_processed_so_far_ += to_skip;
1686 // This assert will fail if the reloc info gives us the target_address_address
1687 // locations in a non-ascending order. Luckily that doesn't happen.
1688 DCHECK(to_skip >= 0);
1689 bool outputting_code = false;
1690 if (to_skip != 0 && code_object_ && !code_has_been_output_) {
1691 // Output the code all at once and fix later.
1692 bytes_to_output = object_->Size() + to_skip - bytes_processed_so_far_;
1693 outputting_code = true;
1694 code_has_been_output_ = true;
1696 if (bytes_to_output != 0 &&
1697 (!code_object_ || outputting_code)) {
1698 #define RAW_CASE(index) \
1699 if (!outputting_code && bytes_to_output == index * kPointerSize && \
1700 index * kPointerSize == to_skip) { \
1701 sink_->PutSection(kRawData + index, "RawDataFixed"); \
1702 to_skip = 0; /* This insn already skips. */ \
1704 COMMON_RAW_LENGTHS(RAW_CASE)
1707 // We always end up here if we are outputting the code of a code object.
1708 sink_->Put(kRawData, "RawData");
1709 sink_->PutInt(bytes_to_output, "length");
1712 // To make snapshots reproducible, we need to wipe out all pointers in code.
1714 Code* code = CloneCodeObject(object_);
1715 WipeOutRelocations(code);
1716 // We need to wipe out the header fields *after* wiping out the
1717 // relocations, because some of these fields are needed for the latter.
1718 code->WipeOutHeader();
1719 object_start = code->address();
1722 const char* description = code_object_ ? "Code" : "Byte";
1723 for (int i = 0; i < bytes_to_output; i++) {
1724 sink_->PutSection(object_start[base + i], description);
1726 if (code_object_) delete[] object_start;
1728 if (to_skip != 0 && return_skip == kIgnoringReturn) {
1729 sink_->Put(kSkip, "Skip");
1730 sink_->PutInt(to_skip, "SkipDistance");
1737 int Serializer::SpaceOfObject(HeapObject* object) {
1738 for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1739 AllocationSpace s = static_cast<AllocationSpace>(i);
1740 if (object->GetHeap()->InSpace(object, s)) {
1741 DCHECK(i < kNumberOfSpaces);
1750 int Serializer::Allocate(int space, int size) {
1751 CHECK(space >= 0 && space < kNumberOfSpaces);
1752 int allocation_address = fullness_[space];
1753 fullness_[space] = allocation_address + size;
1754 return allocation_address;
1758 int Serializer::SpaceAreaSize(int space) {
1759 if (space == CODE_SPACE) {
1760 return isolate_->memory_allocator()->CodePageAreaSize();
1762 return Page::kPageSize - Page::kObjectStartOffset;
1767 void Serializer::Pad() {
1768 // The non-branching GetInt will read up to 3 bytes too far, so we need
1769 // to pad the snapshot to make sure we don't read over the end.
1770 for (unsigned i = 0; i < sizeof(int32_t) - 1; i++) {
1771 sink_->Put(kNop, "Padding");
1776 void Serializer::InitializeCodeAddressMap() {
1777 isolate_->InitializeLoggingAndCounters();
1778 code_address_map_ = new CodeAddressMap(isolate_);
1782 ScriptData* CodeSerializer::Serialize(Isolate* isolate,
1783 Handle<SharedFunctionInfo> info,
1784 Handle<String> source) {
1785 base::ElapsedTimer timer;
1786 if (FLAG_profile_deserialization) timer.Start();
1788 // Serialize code object.
1790 ListSnapshotSink list_sink(&payload);
1791 DebugSnapshotSink debug_sink(&list_sink);
1792 SnapshotByteSink* sink = FLAG_trace_code_serializer
1793 ? static_cast<SnapshotByteSink*>(&debug_sink)
1794 : static_cast<SnapshotByteSink*>(&list_sink);
1795 CodeSerializer cs(isolate, sink, *source);
1796 DisallowHeapAllocation no_gc;
1797 Object** location = Handle<Object>::cast(info).location();
1798 cs.VisitPointer(location);
1801 SerializedCodeData data(&payload, &cs);
1802 ScriptData* script_data = data.GetScriptData();
1804 if (FLAG_profile_deserialization) {
1805 double ms = timer.Elapsed().InMillisecondsF();
1806 int length = script_data->length();
1807 PrintF("[Serializing to %d bytes took %0.3f ms]\n", length, ms);
1814 void CodeSerializer::SerializeObject(Object* o, HowToCode how_to_code,
1815 WhereToPoint where_to_point, int skip) {
1816 CHECK(o->IsHeapObject());
1817 HeapObject* heap_object = HeapObject::cast(o);
1819 // The code-caches link to context-specific code objects, which
1820 // the startup and context serializes cannot currently handle.
1821 DCHECK(!heap_object->IsMap() ||
1822 Map::cast(heap_object)->code_cache() ==
1823 heap_object->GetHeap()->empty_fixed_array());
1826 if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1827 PutRoot(root_index, heap_object, how_to_code, where_to_point, skip);
1831 // TODO(yangguo) wire up global object.
1832 // TODO(yangguo) We cannot deal with different hash seeds yet.
1833 DCHECK(!heap_object->IsHashTable());
1835 if (address_mapper_.IsMapped(heap_object)) {
1836 SerializeReferenceToPreviousObject(heap_object, how_to_code, where_to_point,
1841 if (heap_object->IsCode()) {
1842 Code* code_object = Code::cast(heap_object);
1843 if (code_object->kind() == Code::BUILTIN) {
1844 SerializeBuiltin(code_object, how_to_code, where_to_point, skip);
1847 if (code_object->IsCodeStubOrIC()) {
1848 SerializeCodeStub(code_object, how_to_code, where_to_point, skip);
1851 code_object->ClearInlineCaches();
1854 if (heap_object == source_) {
1855 SerializeSourceObject(how_to_code, where_to_point, skip);
1859 SerializeHeapObject(heap_object, how_to_code, where_to_point, skip);
1863 void CodeSerializer::SerializeHeapObject(HeapObject* heap_object,
1864 HowToCode how_to_code,
1865 WhereToPoint where_to_point,
1867 if (heap_object->IsScript()) {
1868 // The wrapper cache uses a Foreign object to point to a global handle.
1869 // However, the object visitor expects foreign objects to point to external
1870 // references. Clear the cache to avoid this issue.
1871 Script::cast(heap_object)->ClearWrapperCache();
1875 sink_->Put(kSkip, "SkipFromSerializeObject");
1876 sink_->PutInt(skip, "SkipDistanceFromSerializeObject");
1879 if (FLAG_trace_code_serializer) {
1880 PrintF("Encoding heap object: ");
1881 heap_object->ShortPrint();
1885 // Object has not yet been serialized. Serialize it here.
1886 ObjectSerializer serializer(this, heap_object, sink_, how_to_code,
1888 serializer.Serialize();
1892 void CodeSerializer::SerializeBuiltin(Code* builtin, HowToCode how_to_code,
1893 WhereToPoint where_to_point, int skip) {
1895 sink_->Put(kSkip, "SkipFromSerializeBuiltin");
1896 sink_->PutInt(skip, "SkipDistanceFromSerializeBuiltin");
1899 DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) ||
1900 (how_to_code == kPlain && where_to_point == kInnerPointer) ||
1901 (how_to_code == kFromCode && where_to_point == kInnerPointer));
1902 int builtin_index = builtin->builtin_index();
1903 DCHECK_LT(builtin_index, Builtins::builtin_count);
1904 DCHECK_LE(0, builtin_index);
1906 if (FLAG_trace_code_serializer) {
1907 PrintF("Encoding builtin: %s\n",
1908 isolate()->builtins()->name(builtin_index));
1911 sink_->Put(kBuiltin + how_to_code + where_to_point, "Builtin");
1912 sink_->PutInt(builtin_index, "builtin_index");
1916 void CodeSerializer::SerializeCodeStub(Code* code, HowToCode how_to_code,
1917 WhereToPoint where_to_point, int skip) {
1918 DCHECK((how_to_code == kPlain && where_to_point == kStartOfObject) ||
1919 (how_to_code == kPlain && where_to_point == kInnerPointer) ||
1920 (how_to_code == kFromCode && where_to_point == kInnerPointer));
1921 uint32_t stub_key = code->stub_key();
1923 if (stub_key == CodeStub::NoCacheKey()) {
1924 if (FLAG_trace_code_serializer) {
1925 PrintF("Encoding uncacheable code stub as heap object\n");
1927 SerializeHeapObject(code, how_to_code, where_to_point, skip);
1932 sink_->Put(kSkip, "SkipFromSerializeCodeStub");
1933 sink_->PutInt(skip, "SkipDistanceFromSerializeCodeStub");
1936 int index = AddCodeStubKey(stub_key) + kCodeStubsBaseIndex;
1938 if (FLAG_trace_code_serializer) {
1939 PrintF("Encoding code stub %s as %d\n",
1940 CodeStub::MajorName(CodeStub::MajorKeyFromKey(stub_key), false),
1944 sink_->Put(kAttachedReference + how_to_code + where_to_point, "CodeStub");
1945 sink_->PutInt(index, "CodeStub key");
1949 int CodeSerializer::AddCodeStubKey(uint32_t stub_key) {
1950 // TODO(yangguo) Maybe we need a hash table for a faster lookup than O(n^2).
1952 while (index < stub_keys_.length()) {
1953 if (stub_keys_[index] == stub_key) return index;
1956 stub_keys_.Add(stub_key);
1961 void CodeSerializer::SerializeSourceObject(HowToCode how_to_code,
1962 WhereToPoint where_to_point,
1965 sink_->Put(kSkip, "SkipFromSerializeSourceObject");
1966 sink_->PutInt(skip, "SkipDistanceFromSerializeSourceObject");
1969 if (FLAG_trace_code_serializer) {
1970 PrintF("Encoding source object\n");
1973 DCHECK(how_to_code == kPlain && where_to_point == kStartOfObject);
1974 sink_->Put(kAttachedReference + how_to_code + where_to_point, "Source");
1975 sink_->PutInt(kSourceObjectIndex, "kSourceObjectIndex");
1979 Handle<SharedFunctionInfo> CodeSerializer::Deserialize(Isolate* isolate,
1981 Handle<String> source) {
1982 base::ElapsedTimer timer;
1983 if (FLAG_profile_deserialization) timer.Start();
1988 HandleScope scope(isolate);
1990 SerializedCodeData scd(data, *source);
1991 SnapshotByteSource payload(scd.Payload(), scd.PayloadLength());
1992 Deserializer deserializer(&payload);
1993 STATIC_ASSERT(NEW_SPACE == 0);
1994 for (int i = NEW_SPACE; i <= PROPERTY_CELL_SPACE; i++) {
1995 deserializer.set_reservation(i, scd.GetReservation(i));
1998 // Prepare and register list of attached objects.
1999 Vector<const uint32_t> code_stub_keys = scd.CodeStubKeys();
2000 Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::New(
2001 code_stub_keys.length() + kCodeStubsBaseIndex);
2002 attached_objects[kSourceObjectIndex] = source;
2003 for (int i = 0; i < code_stub_keys.length(); i++) {
2004 attached_objects[i + kCodeStubsBaseIndex] =
2005 CodeStub::GetCode(isolate, code_stub_keys[i]).ToHandleChecked();
2007 deserializer.SetAttachedObjects(&attached_objects);
2010 deserializer.DeserializePartial(isolate, &root);
2011 deserializer.FlushICacheForNewCodeObjects();
2014 if (FLAG_profile_deserialization) {
2015 double ms = timer.Elapsed().InMillisecondsF();
2016 int length = data->length();
2017 PrintF("[Deserializing from %d bytes took %0.3f ms]\n", length, ms);
2019 return Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(root), isolate);
2023 SerializedCodeData::SerializedCodeData(List<byte>* payload, CodeSerializer* cs)
2024 : owns_script_data_(true) {
2025 DisallowHeapAllocation no_gc;
2026 List<uint32_t>* stub_keys = cs->stub_keys();
2029 int num_stub_keys = stub_keys->length();
2030 int stub_keys_size = stub_keys->length() * kInt32Size;
2031 int data_length = kHeaderSize + stub_keys_size + payload->length();
2033 // Allocate backing store and create result data.
2034 byte* data = NewArray<byte>(data_length);
2035 DCHECK(IsAligned(reinterpret_cast<intptr_t>(data), kPointerAlignment));
2036 script_data_ = new ScriptData(data, data_length);
2037 script_data_->AcquireDataOwnership();
2039 // Set header values.
2040 SetHeaderValue(kCheckSumOffset, CheckSum(cs->source()));
2041 SetHeaderValue(kNumCodeStubKeysOffset, num_stub_keys);
2042 SetHeaderValue(kPayloadLengthOffset, payload->length());
2043 STATIC_ASSERT(NEW_SPACE == 0);
2044 for (int i = NEW_SPACE; i <= PROPERTY_CELL_SPACE; i++) {
2045 SetHeaderValue(kReservationsOffset + i, cs->CurrentAllocationAddress(i));
2048 // Copy code stub keys.
2049 CopyBytes(data + kHeaderSize, reinterpret_cast<byte*>(stub_keys->begin()),
2052 // Copy serialized data.
2053 CopyBytes(data + kHeaderSize + stub_keys_size, payload->begin(),
2054 static_cast<size_t>(payload->length()));
2058 bool SerializedCodeData::IsSane(String* source) {
2059 return GetHeaderValue(kCheckSumOffset) == CheckSum(source) &&
2060 PayloadLength() >= SharedFunctionInfo::kSize;
2064 int SerializedCodeData::CheckSum(String* string) {
2065 int checksum = Version::Hash();
2067 uint32_t seed = static_cast<uint32_t>(checksum);
2068 checksum = static_cast<int>(IteratingStringHasher::Hash(string, seed));
2072 } } // namespace v8::internal