1
// Copyright 2012 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
6
// * Redistributions of source code must retain the above copyright
7
// notice, this list of conditions and the following disclaimer.
8
// * Redistributions in binary form must reproduce the above
9
// copyright notice, this list of conditions and the following
10
// disclaimer in the documentation and/or other materials provided
11
// with the distribution.
12
// * Neither the name of Google Inc. nor the names of its
13
// contributors may be used to endorse or promote products derived
14
// from this software without specific prior written permission.
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
#include "accessors.h"
32
#include "bootstrapper.h"
33
#include "execution.h"
34
#include "global-handles.h"
39
#include "serialize.h"
41
#include "stub-cache.h"
42
#include "v8threads.h"
48
// -----------------------------------------------------------------------------
49
// Coding of external references.
51
// The encoding of an external reference. The type is in the high word.
52
// The id is in the low word.
53
static uint32_t EncodeExternal(TypeCode type, uint16_t id) {
54
return static_cast<uint32_t>(type) << 16 | id;
58
static int* GetInternalPointer(StatsCounter* counter) {
59
// All counters refer to dummy_counter, if deserializing happens without
60
// setting up counters.
61
static int dummy_counter = 0;
62
return counter->Enabled() ? counter->GetInternalPointer() : &dummy_counter;
66
ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
67
ExternalReferenceTable* external_reference_table =
68
isolate->external_reference_table();
69
if (external_reference_table == NULL) {
70
external_reference_table = new ExternalReferenceTable(isolate);
71
isolate->set_external_reference_table(external_reference_table);
73
return external_reference_table;
77
void ExternalReferenceTable::AddFromId(TypeCode type,
84
ExternalReference ref(static_cast<Builtins::CFunctionId>(id), isolate);
85
address = ref.address();
89
ExternalReference ref(static_cast<Builtins::Name>(id), isolate);
90
address = ref.address();
93
case RUNTIME_FUNCTION: {
94
ExternalReference ref(static_cast<Runtime::FunctionId>(id), isolate);
95
address = ref.address();
99
ExternalReference ref(IC_Utility(static_cast<IC::UtilityId>(id)),
101
address = ref.address();
108
Add(address, type, id, name);
112
void ExternalReferenceTable::Add(Address address,
116
ASSERT_NE(NULL, address);
117
ExternalReferenceEntry entry;
118
entry.address = address;
119
entry.code = EncodeExternal(type, id);
121
ASSERT_NE(0, entry.code);
123
if (id > max_id_[type]) max_id_[type] = id;
127
void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
128
for (int type_code = 0; type_code < kTypeCodeCount; type_code++) {
129
max_id_[type_code] = 0;
132
// The following populates all of the different type of external references
133
// into the ExternalReferenceTable.
135
// NOTE: This function was originally 100k of code. It has since been
136
// rewritten to be mostly table driven, as the callback macro style tends to
137
// very easily cause code bloat. Please be careful in the future when adding
140
struct RefTableEntry {
146
static const RefTableEntry ref_table[] = {
148
#define DEF_ENTRY_C(name, ignored) \
150
Builtins::c_##name, \
151
"Builtins::" #name },
153
BUILTIN_LIST_C(DEF_ENTRY_C)
156
#define DEF_ENTRY_C(name, ignored) \
159
"Builtins::" #name },
160
#define DEF_ENTRY_A(name, kind, state, extra) DEF_ENTRY_C(name, ignored)
162
BUILTIN_LIST_C(DEF_ENTRY_C)
163
BUILTIN_LIST_A(DEF_ENTRY_A)
164
BUILTIN_LIST_DEBUG_A(DEF_ENTRY_A)
169
#define RUNTIME_ENTRY(name, nargs, ressize) \
170
{ RUNTIME_FUNCTION, \
174
RUNTIME_FUNCTION_LIST(RUNTIME_ENTRY)
178
#define IC_ENTRY(name) \
183
IC_UTIL_LIST(IC_ENTRY)
185
}; // end of ref_table[].
187
for (size_t i = 0; i < ARRAY_SIZE(ref_table); ++i) {
188
AddFromId(ref_table[i].type,
194
#ifdef ENABLE_DEBUGGER_SUPPORT
196
Add(Debug_Address(Debug::k_after_break_target_address).address(isolate),
198
Debug::k_after_break_target_address << kDebugIdShift,
199
"Debug::after_break_target_address()");
200
Add(Debug_Address(Debug::k_debug_break_slot_address).address(isolate),
202
Debug::k_debug_break_slot_address << kDebugIdShift,
203
"Debug::debug_break_slot_address()");
204
Add(Debug_Address(Debug::k_debug_break_return_address).address(isolate),
206
Debug::k_debug_break_return_address << kDebugIdShift,
207
"Debug::debug_break_return_address()");
208
Add(Debug_Address(Debug::k_restarter_frame_function_pointer).address(isolate),
210
Debug::k_restarter_frame_function_pointer << kDebugIdShift,
211
"Debug::restarter_frame_function_pointer_address()");
215
struct StatsRefTableEntry {
216
StatsCounter* (Counters::*counter)();
221
const StatsRefTableEntry stats_ref_table[] = {
222
#define COUNTER_ENTRY(name, caption) \
224
Counters::k_##name, \
225
"Counters::" #name },
227
STATS_COUNTER_LIST_1(COUNTER_ENTRY)
228
STATS_COUNTER_LIST_2(COUNTER_ENTRY)
230
}; // end of stats_ref_table[].
232
Counters* counters = isolate->counters();
233
for (size_t i = 0; i < ARRAY_SIZE(stats_ref_table); ++i) {
234
Add(reinterpret_cast<Address>(GetInternalPointer(
235
(counters->*(stats_ref_table[i].counter))())),
237
stats_ref_table[i].id,
238
stats_ref_table[i].name);
243
const char* AddressNames[] = {
244
#define BUILD_NAME_LITERAL(CamelName, hacker_name) \
245
"Isolate::" #hacker_name "_address",
246
FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL)
248
#undef BUILD_NAME_LITERAL
251
for (uint16_t i = 0; i < Isolate::kIsolateAddressCount; ++i) {
252
Add(isolate->get_address_from_id((Isolate::AddressId)i),
253
TOP_ADDRESS, i, AddressNames[i]);
257
#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
258
Add((Address)&Accessors::name, \
260
Accessors::k##name, \
261
"Accessors::" #name);
263
ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
264
#undef ACCESSOR_DESCRIPTOR_DECLARATION
266
StubCache* stub_cache = isolate->stub_cache();
269
Add(stub_cache->key_reference(StubCache::kPrimary).address(),
272
"StubCache::primary_->key");
273
Add(stub_cache->value_reference(StubCache::kPrimary).address(),
276
"StubCache::primary_->value");
277
Add(stub_cache->map_reference(StubCache::kPrimary).address(),
280
"StubCache::primary_->map");
281
Add(stub_cache->key_reference(StubCache::kSecondary).address(),
284
"StubCache::secondary_->key");
285
Add(stub_cache->value_reference(StubCache::kSecondary).address(),
288
"StubCache::secondary_->value");
289
Add(stub_cache->map_reference(StubCache::kSecondary).address(),
292
"StubCache::secondary_->map");
295
Add(ExternalReference::perform_gc_function(isolate).address(),
298
"Runtime::PerformGC");
299
Add(ExternalReference::fill_heap_number_with_random_function(
303
"V8::FillHeapNumberWithRandom");
304
Add(ExternalReference::random_uint32_function(isolate).address(),
308
Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
311
"HandleScope::DeleteExtensions");
312
Add(ExternalReference::
313
incremental_marking_record_write_function(isolate).address(),
316
"IncrementalMarking::RecordWrite");
317
Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
320
"StoreBuffer::StoreBufferOverflow");
321
Add(ExternalReference::
322
incremental_evacuation_record_write_function(isolate).address(),
325
"IncrementalMarking::RecordWrite");
330
Add(ExternalReference::roots_array_start(isolate).address(),
333
"Heap::roots_array_start()");
334
Add(ExternalReference::address_of_stack_limit(isolate).address(),
337
"StackGuard::address_of_jslimit()");
338
Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
341
"StackGuard::address_of_real_jslimit()");
342
#ifndef V8_INTERPRETED_REGEXP
343
Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
346
"RegExpStack::limit_address()");
347
Add(ExternalReference::address_of_regexp_stack_memory_address(
351
"RegExpStack::memory_address()");
352
Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
355
"RegExpStack::memory_size()");
356
Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
359
"OffsetsVector::static_offsets_vector");
360
#endif // V8_INTERPRETED_REGEXP
361
Add(ExternalReference::new_space_start(isolate).address(),
364
"Heap::NewSpaceStart()");
365
Add(ExternalReference::new_space_mask(isolate).address(),
368
"Heap::NewSpaceMask()");
369
Add(ExternalReference::heap_always_allocate_scope_depth(isolate).address(),
372
"Heap::always_allocate_scope_depth()");
373
Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
376
"Heap::NewSpaceAllocationLimitAddress()");
377
Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
380
"Heap::NewSpaceAllocationTopAddress()");
381
#ifdef ENABLE_DEBUGGER_SUPPORT
382
Add(ExternalReference::debug_break(isolate).address(),
386
Add(ExternalReference::debug_step_in_fp_address(isolate).address(),
389
"Debug::step_in_fp_addr()");
391
Add(ExternalReference::double_fp_operation(Token::ADD, isolate).address(),
395
Add(ExternalReference::double_fp_operation(Token::SUB, isolate).address(),
399
Add(ExternalReference::double_fp_operation(Token::MUL, isolate).address(),
403
Add(ExternalReference::double_fp_operation(Token::DIV, isolate).address(),
407
Add(ExternalReference::double_fp_operation(Token::MOD, isolate).address(),
411
Add(ExternalReference::compare_doubles(isolate).address(),
415
#ifndef V8_INTERPRETED_REGEXP
416
Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
419
"NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
420
Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
423
"RegExpMacroAssembler*::CheckStackGuardState()");
424
Add(ExternalReference::re_grow_stack(isolate).address(),
427
"NativeRegExpMacroAssembler::GrowStack()");
428
Add(ExternalReference::re_word_character_map().address(),
431
"NativeRegExpMacroAssembler::word_character_map");
432
#endif // V8_INTERPRETED_REGEXP
433
// Keyed lookup cache.
434
Add(ExternalReference::keyed_lookup_cache_keys(isolate).address(),
437
"KeyedLookupCache::keys()");
438
Add(ExternalReference::keyed_lookup_cache_field_offsets(isolate).address(),
441
"KeyedLookupCache::field_offsets()");
442
Add(ExternalReference::transcendental_cache_array_address(isolate).address(),
445
"TranscendentalCache::caches()");
446
Add(ExternalReference::handle_scope_next_address().address(),
449
"HandleScope::next");
450
Add(ExternalReference::handle_scope_limit_address().address(),
453
"HandleScope::limit");
454
Add(ExternalReference::handle_scope_level_address().address(),
457
"HandleScope::level");
458
Add(ExternalReference::new_deoptimizer_function(isolate).address(),
461
"Deoptimizer::New()");
462
Add(ExternalReference::compute_output_frames_function(isolate).address(),
465
"Deoptimizer::ComputeOutputFrames()");
466
Add(ExternalReference::address_of_min_int().address(),
469
"LDoubleConstant::min_int");
470
Add(ExternalReference::address_of_one_half().address(),
473
"LDoubleConstant::one_half");
474
Add(ExternalReference::isolate_address().address(),
478
Add(ExternalReference::address_of_minus_zero().address(),
481
"LDoubleConstant::minus_zero");
482
Add(ExternalReference::address_of_negative_infinity().address(),
485
"LDoubleConstant::negative_infinity");
486
Add(ExternalReference::power_double_double_function(isolate).address(),
489
"power_double_double_function");
490
Add(ExternalReference::power_double_int_function(isolate).address(),
493
"power_double_int_function");
494
Add(ExternalReference::store_buffer_top(isolate).address(),
498
Add(ExternalReference::address_of_canonical_non_hole_nan().address(),
502
Add(ExternalReference::address_of_the_hole_nan().address(),
506
Add(ExternalReference::get_date_field_function(isolate).address(),
510
Add(ExternalReference::date_cache_stamp(isolate).address(),
514
Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
517
"address_of_pending_message_obj");
518
Add(ExternalReference::address_of_has_pending_message(isolate).address(),
521
"address_of_has_pending_message");
522
Add(ExternalReference::address_of_pending_message_script(isolate).address(),
525
"pending_message_script");
529
ExternalReferenceEncoder::ExternalReferenceEncoder()
531
isolate_(Isolate::Current()) {
532
ExternalReferenceTable* external_references =
533
ExternalReferenceTable::instance(isolate_);
534
for (int i = 0; i < external_references->size(); ++i) {
535
Put(external_references->address(i), i);
540
uint32_t ExternalReferenceEncoder::Encode(Address key) const {
541
int index = IndexOf(key);
542
ASSERT(key == NULL || index >= 0);
544
ExternalReferenceTable::instance(isolate_)->code(index) : 0;
548
const char* ExternalReferenceEncoder::NameOfAddress(Address key) const {
549
int index = IndexOf(key);
551
ExternalReferenceTable::instance(isolate_)->name(index) : NULL;
555
int ExternalReferenceEncoder::IndexOf(Address key) const {
556
if (key == NULL) return -1;
557
HashMap::Entry* entry =
558
const_cast<HashMap&>(encodings_).Lookup(key, Hash(key), false);
561
: static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
565
void ExternalReferenceEncoder::Put(Address key, int index) {
566
HashMap::Entry* entry = encodings_.Lookup(key, Hash(key), true);
567
entry->value = reinterpret_cast<void*>(index);
571
ExternalReferenceDecoder::ExternalReferenceDecoder()
572
: encodings_(NewArray<Address*>(kTypeCodeCount)),
573
isolate_(Isolate::Current()) {
574
ExternalReferenceTable* external_references =
575
ExternalReferenceTable::instance(isolate_);
576
for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
577
int max = external_references->max_id(type) + 1;
578
encodings_[type] = NewArray<Address>(max + 1);
580
for (int i = 0; i < external_references->size(); ++i) {
581
Put(external_references->code(i), external_references->address(i));
586
ExternalReferenceDecoder::~ExternalReferenceDecoder() {
587
for (int type = kFirstTypeCode; type < kTypeCodeCount; ++type) {
588
DeleteArray(encodings_[type]);
590
DeleteArray(encodings_);
594
bool Serializer::serialization_enabled_ = false;
595
bool Serializer::too_late_to_enable_now_ = false;
598
Deserializer::Deserializer(SnapshotByteSource* source)
601
external_reference_decoder_(NULL) {
605
// This routine both allocates a new object, and also keeps
606
// track of where objects have been allocated so that we can
607
// fix back references when deserializing.
608
Address Deserializer::Allocate(int space_index, Space* space, int size) {
610
if (!SpaceIsLarge(space_index)) {
611
ASSERT(!SpaceIsPaged(space_index) ||
612
size <= Page::kPageSize - Page::kObjectStartOffset);
613
MaybeObject* maybe_new_allocation;
614
if (space_index == NEW_SPACE) {
615
maybe_new_allocation =
616
reinterpret_cast<NewSpace*>(space)->AllocateRaw(size);
618
maybe_new_allocation =
619
reinterpret_cast<PagedSpace*>(space)->AllocateRaw(size);
621
ASSERT(!maybe_new_allocation->IsFailure());
622
Object* new_allocation = maybe_new_allocation->ToObjectUnchecked();
623
HeapObject* new_object = HeapObject::cast(new_allocation);
624
address = new_object->address();
625
high_water_[space_index] = address + size;
627
ASSERT(SpaceIsLarge(space_index));
628
LargeObjectSpace* lo_space = reinterpret_cast<LargeObjectSpace*>(space);
629
Object* new_allocation;
630
if (space_index == kLargeData || space_index == kLargeFixedArray) {
632
lo_space->AllocateRaw(size, NOT_EXECUTABLE)->ToObjectUnchecked();
634
ASSERT_EQ(kLargeCode, space_index);
636
lo_space->AllocateRaw(size, EXECUTABLE)->ToObjectUnchecked();
638
HeapObject* new_object = HeapObject::cast(new_allocation);
639
// Record all large objects in the same space.
640
address = new_object->address();
641
pages_[LO_SPACE].Add(address);
643
last_object_address_ = address;
648
// This returns the address of an object that has been described in the
649
// snapshot as being offset bytes back in a particular space.
650
HeapObject* Deserializer::GetAddressFromEnd(int space) {
651
int offset = source_->GetInt();
652
ASSERT(!SpaceIsLarge(space));
653
offset <<= kObjectAlignmentBits;
654
return HeapObject::FromAddress(high_water_[space] - offset);
658
// This returns the address of an object that has been described in the
659
// snapshot as being offset bytes into a particular space.
660
HeapObject* Deserializer::GetAddressFromStart(int space) {
661
int offset = source_->GetInt();
662
if (SpaceIsLarge(space)) {
663
// Large spaces have one object per 'page'.
664
return HeapObject::FromAddress(pages_[LO_SPACE][offset]);
666
offset <<= kObjectAlignmentBits;
667
if (space == NEW_SPACE) {
668
// New space has only one space - numbered 0.
669
return HeapObject::FromAddress(pages_[space][0] + offset);
671
ASSERT(SpaceIsPaged(space));
672
int page_of_pointee = offset >> kPageSizeBits;
673
Address object_address = pages_[space][page_of_pointee] +
674
(offset & Page::kPageAlignmentMask);
675
return HeapObject::FromAddress(object_address);
679
void Deserializer::Deserialize() {
680
isolate_ = Isolate::Current();
681
ASSERT(isolate_ != NULL);
682
// Don't GC while deserializing - just expand the heap.
683
AlwaysAllocateScope always_allocate;
684
// Don't use the free lists while deserializing.
685
LinearAllocationScope allocate_linearly;
686
// No active threads.
687
ASSERT_EQ(NULL, isolate_->thread_manager()->FirstThreadStateInUse());
688
// No active handles.
689
ASSERT(isolate_->handle_scope_implementer()->blocks()->is_empty());
690
ASSERT_EQ(NULL, external_reference_decoder_);
691
external_reference_decoder_ = new ExternalReferenceDecoder();
692
isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
693
isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
695
isolate_->heap()->set_global_contexts_list(
696
isolate_->heap()->undefined_value());
698
// Update data pointers to the external strings containing natives sources.
699
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
700
Object* source = isolate_->heap()->natives_source_cache()->get(i);
701
if (!source->IsUndefined()) {
702
ExternalAsciiString::cast(source)->update_data_cache();
708
void Deserializer::DeserializePartial(Object** root) {
709
isolate_ = Isolate::Current();
710
// Don't GC while deserializing - just expand the heap.
711
AlwaysAllocateScope always_allocate;
712
// Don't use the free lists while deserializing.
713
LinearAllocationScope allocate_linearly;
714
if (external_reference_decoder_ == NULL) {
715
external_reference_decoder_ = new ExternalReferenceDecoder();
721
Deserializer::~Deserializer() {
722
ASSERT(source_->AtEOF());
723
if (external_reference_decoder_) {
724
delete external_reference_decoder_;
725
external_reference_decoder_ = NULL;
730
// This is called on the roots. It is the driver of the deserialization
731
// process. It is also called on the body of each function.
732
void Deserializer::VisitPointers(Object** start, Object** end) {
733
// The space must be new space. Any other space would cause ReadChunk to try
734
// to update the remembered using NULL as the address.
735
ReadChunk(start, end, NEW_SPACE, NULL);
739
// This routine writes the new object into the pointer provided and then
740
// returns true if the new object was in young space and false otherwise.
741
// The reason for this strange interface is that otherwise the object is
742
// written very late, which means the FreeSpace map is not set up by the
743
// time we need to use it to mark the space at the end of a page free.
744
void Deserializer::ReadObject(int space_number,
746
Object** write_back) {
747
int size = source_->GetInt() << kObjectAlignmentBits;
748
Address address = Allocate(space_number, space, size);
749
*write_back = HeapObject::FromAddress(address);
750
Object** current = reinterpret_cast<Object**>(address);
751
Object** limit = current + (size >> kPointerSizeLog2);
752
if (FLAG_log_snapshot_positions) {
753
LOG(isolate_, SnapshotPositionEvent(address, source_->position()));
755
ReadChunk(current, limit, space_number, address);
757
bool is_codespace = (space == HEAP->code_space()) ||
758
((space == HEAP->lo_space()) && (space_number == kLargeCode));
759
ASSERT(HeapObject::FromAddress(address)->IsCode() == is_codespace);
764
// This macro is always used with a constant argument so it should all fold
765
// away to almost nothing in the generated code. It might be nicer to do this
766
// with the ternary operator but there are type issues with that.
767
#define ASSIGN_DEST_SPACE(space_number) \
769
if (space_number == NEW_SPACE) { \
770
dest_space = isolate->heap()->new_space(); \
771
} else if (space_number == OLD_POINTER_SPACE) { \
772
dest_space = isolate->heap()->old_pointer_space(); \
773
} else if (space_number == OLD_DATA_SPACE) { \
774
dest_space = isolate->heap()->old_data_space(); \
775
} else if (space_number == CODE_SPACE) { \
776
dest_space = isolate->heap()->code_space(); \
777
} else if (space_number == MAP_SPACE) { \
778
dest_space = isolate->heap()->map_space(); \
779
} else if (space_number == CELL_SPACE) { \
780
dest_space = isolate->heap()->cell_space(); \
782
ASSERT(space_number >= LO_SPACE); \
783
dest_space = isolate->heap()->lo_space(); \
787
static const int kUnknownOffsetFromStart = -1;
790
void Deserializer::ReadChunk(Object** current,
793
Address current_object_address) {
794
Isolate* const isolate = isolate_;
795
bool write_barrier_needed = (current_object_address != NULL &&
796
source_space != NEW_SPACE &&
797
source_space != CELL_SPACE &&
798
source_space != CODE_SPACE &&
799
source_space != OLD_DATA_SPACE);
800
while (current < limit) {
801
int data = source_->Get();
803
#define CASE_STATEMENT(where, how, within, space_number) \
804
case where + how + within + space_number: \
805
ASSERT((where & ~kPointedToMask) == 0); \
806
ASSERT((how & ~kHowToCodeMask) == 0); \
807
ASSERT((within & ~kWhereToPointMask) == 0); \
808
ASSERT((space_number & ~kSpaceMask) == 0);
810
#define CASE_BODY(where, how, within, space_number_if_any, offset_from_start) \
812
bool emit_write_barrier = false; \
813
bool current_was_incremented = false; \
814
int space_number = space_number_if_any == kAnyOldSpace ? \
815
(data & kSpaceMask) : space_number_if_any; \
816
if (where == kNewObject && how == kPlain && within == kStartOfObject) {\
817
ASSIGN_DEST_SPACE(space_number) \
818
ReadObject(space_number, dest_space, current); \
819
emit_write_barrier = (space_number == NEW_SPACE); \
821
Object* new_object = NULL; /* May not be a real Object pointer. */ \
822
if (where == kNewObject) { \
823
ASSIGN_DEST_SPACE(space_number) \
824
ReadObject(space_number, dest_space, &new_object); \
825
} else if (where == kRootArray) { \
826
int root_id = source_->GetInt(); \
827
new_object = isolate->heap()->roots_array_start()[root_id]; \
828
emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
829
} else if (where == kPartialSnapshotCache) { \
830
int cache_index = source_->GetInt(); \
831
new_object = isolate->serialize_partial_snapshot_cache() \
833
emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
834
} else if (where == kExternalReference) { \
835
int reference_id = source_->GetInt(); \
836
Address address = external_reference_decoder_-> \
837
Decode(reference_id); \
838
new_object = reinterpret_cast<Object*>(address); \
839
} else if (where == kBackref) { \
840
emit_write_barrier = (space_number == NEW_SPACE); \
841
new_object = GetAddressFromEnd(data & kSpaceMask); \
843
ASSERT(where == kFromStart); \
844
if (offset_from_start == kUnknownOffsetFromStart) { \
845
emit_write_barrier = (space_number == NEW_SPACE); \
846
new_object = GetAddressFromStart(data & kSpaceMask); \
848
Address object_address = pages_[space_number][0] + \
849
(offset_from_start << kObjectAlignmentBits); \
850
new_object = HeapObject::FromAddress(object_address); \
853
if (within == kInnerPointer) { \
854
if (space_number != CODE_SPACE || new_object->IsCode()) { \
855
Code* new_code_object = reinterpret_cast<Code*>(new_object); \
856
new_object = reinterpret_cast<Object*>( \
857
new_code_object->instruction_start()); \
859
ASSERT(space_number == CODE_SPACE || space_number == kLargeCode);\
860
JSGlobalPropertyCell* cell = \
861
JSGlobalPropertyCell::cast(new_object); \
862
new_object = reinterpret_cast<Object*>( \
863
cell->ValueAddress()); \
866
if (how == kFromCode) { \
867
Address location_of_branch_data = \
868
reinterpret_cast<Address>(current); \
869
Assembler::deserialization_set_special_target_at( \
870
location_of_branch_data, \
871
reinterpret_cast<Address>(new_object)); \
872
location_of_branch_data += Assembler::kSpecialTargetSize; \
873
current = reinterpret_cast<Object**>(location_of_branch_data); \
874
current_was_incremented = true; \
876
*current = new_object; \
879
if (emit_write_barrier && write_barrier_needed) { \
880
Address current_address = reinterpret_cast<Address>(current); \
881
isolate->heap()->RecordWrite( \
882
current_object_address, \
883
static_cast<int>(current_address - current_object_address)); \
885
if (!current_was_incremented) { \
891
// This generates a case and a body for each space. The large object spaces are
892
// very rare in snapshots so they are grouped in one body.
893
#define ONE_PER_SPACE(where, how, within) \
894
CASE_STATEMENT(where, how, within, NEW_SPACE) \
895
CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
896
CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
897
CASE_BODY(where, how, within, OLD_DATA_SPACE, kUnknownOffsetFromStart) \
898
CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
899
CASE_BODY(where, how, within, OLD_POINTER_SPACE, kUnknownOffsetFromStart) \
900
CASE_STATEMENT(where, how, within, CODE_SPACE) \
901
CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
902
CASE_STATEMENT(where, how, within, CELL_SPACE) \
903
CASE_BODY(where, how, within, CELL_SPACE, kUnknownOffsetFromStart) \
904
CASE_STATEMENT(where, how, within, MAP_SPACE) \
905
CASE_BODY(where, how, within, MAP_SPACE, kUnknownOffsetFromStart) \
906
CASE_STATEMENT(where, how, within, kLargeData) \
907
CASE_STATEMENT(where, how, within, kLargeCode) \
908
CASE_STATEMENT(where, how, within, kLargeFixedArray) \
909
CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
911
// This generates a case and a body for the new space (which has to do extra
912
// write barrier handling) and handles the other spaces with 8 fall-through
913
// cases and one body.
914
#define ALL_SPACES(where, how, within) \
915
CASE_STATEMENT(where, how, within, NEW_SPACE) \
916
CASE_BODY(where, how, within, NEW_SPACE, kUnknownOffsetFromStart) \
917
CASE_STATEMENT(where, how, within, OLD_DATA_SPACE) \
918
CASE_STATEMENT(where, how, within, OLD_POINTER_SPACE) \
919
CASE_STATEMENT(where, how, within, CODE_SPACE) \
920
CASE_STATEMENT(where, how, within, CELL_SPACE) \
921
CASE_STATEMENT(where, how, within, MAP_SPACE) \
922
CASE_STATEMENT(where, how, within, kLargeData) \
923
CASE_STATEMENT(where, how, within, kLargeCode) \
924
CASE_STATEMENT(where, how, within, kLargeFixedArray) \
925
CASE_BODY(where, how, within, kAnyOldSpace, kUnknownOffsetFromStart)
927
#define ONE_PER_CODE_SPACE(where, how, within) \
928
CASE_STATEMENT(where, how, within, CODE_SPACE) \
929
CASE_BODY(where, how, within, CODE_SPACE, kUnknownOffsetFromStart) \
930
CASE_STATEMENT(where, how, within, kLargeCode) \
931
CASE_BODY(where, how, within, kLargeCode, kUnknownOffsetFromStart)
933
#define FOUR_CASES(byte_code) \
935
case byte_code + 1: \
936
case byte_code + 2: \
939
#define SIXTEEN_CASES(byte_code) \
940
FOUR_CASES(byte_code) \
941
FOUR_CASES(byte_code + 4) \
942
FOUR_CASES(byte_code + 8) \
943
FOUR_CASES(byte_code + 12)
945
// We generate 15 cases and bodies that process special tags that combine
946
// the raw data tag and the length into one byte.
947
#define RAW_CASE(index, size) \
948
case kRawData + index: { \
949
byte* raw_data_out = reinterpret_cast<byte*>(current); \
950
source_->CopyRaw(raw_data_out, size); \
951
current = reinterpret_cast<Object**>(raw_data_out + size); \
954
COMMON_RAW_LENGTHS(RAW_CASE)
957
// Deserialize a chunk of raw data that doesn't have one of the popular
960
int size = source_->GetInt();
961
byte* raw_data_out = reinterpret_cast<byte*>(current);
962
source_->CopyRaw(raw_data_out, size);
963
current = reinterpret_cast<Object**>(raw_data_out + size);
967
SIXTEEN_CASES(kRootArrayLowConstants)
968
SIXTEEN_CASES(kRootArrayHighConstants) {
969
int root_id = RootArrayConstantFromByteCode(data);
970
Object* object = isolate->heap()->roots_array_start()[root_id];
971
ASSERT(!isolate->heap()->InNewSpace(object));
977
int repeats = source_->GetInt();
978
Object* object = current[-1];
979
ASSERT(!isolate->heap()->InNewSpace(object));
980
for (int i = 0; i < repeats; i++) current[i] = object;
985
STATIC_ASSERT(kRootArrayNumberOfConstantEncodings ==
986
Heap::kOldSpaceRoots);
987
STATIC_ASSERT(kMaxRepeats == 12);
988
FOUR_CASES(kConstantRepeat)
989
FOUR_CASES(kConstantRepeat + 4)
990
FOUR_CASES(kConstantRepeat + 8) {
991
int repeats = RepeatsForCode(data);
992
Object* object = current[-1];
993
ASSERT(!isolate->heap()->InNewSpace(object));
994
for (int i = 0; i < repeats; i++) current[i] = object;
999
// Deserialize a new object and write a pointer to it to the current
1001
ONE_PER_SPACE(kNewObject, kPlain, kStartOfObject)
1002
// Support for direct instruction pointers in functions. It's an inner
1003
// pointer because it points at the entry point, not at the start of the
1005
ONE_PER_CODE_SPACE(kNewObject, kPlain, kInnerPointer)
1006
// Deserialize a new code object and write a pointer to its first
1007
// instruction to the current code object.
1008
ONE_PER_SPACE(kNewObject, kFromCode, kInnerPointer)
1009
// Find a recently deserialized object using its offset from the current
1010
// allocation point and write a pointer to it to the current object.
1011
ALL_SPACES(kBackref, kPlain, kStartOfObject)
1012
#if V8_TARGET_ARCH_MIPS
1013
// Deserialize a new object from pointer found in code and write
1014
// a pointer to it to the current object. Required only for MIPS, and
1015
// omitted on the other architectures because it is fully unrolled and
1016
// would cause bloat.
1017
ONE_PER_SPACE(kNewObject, kFromCode, kStartOfObject)
1018
// Find a recently deserialized code object using its offset from the
1019
// current allocation point and write a pointer to it to the current
1020
// object. Required only for MIPS.
1021
ALL_SPACES(kBackref, kFromCode, kStartOfObject)
1022
// Find an already deserialized code object using its offset from
1023
// the start and write a pointer to it to the current object.
1024
// Required only for MIPS.
1025
ALL_SPACES(kFromStart, kFromCode, kStartOfObject)
1027
// Find a recently deserialized code object using its offset from the
1028
// current allocation point and write a pointer to its first instruction
1029
// to the current code object or the instruction pointer in a function
1031
ALL_SPACES(kBackref, kFromCode, kInnerPointer)
1032
ALL_SPACES(kBackref, kPlain, kInnerPointer)
1033
// Find an already deserialized object using its offset from the start
1034
// and write a pointer to it to the current object.
1035
ALL_SPACES(kFromStart, kPlain, kStartOfObject)
1036
ALL_SPACES(kFromStart, kPlain, kInnerPointer)
1037
// Find an already deserialized code object using its offset from the
1038
// start and write a pointer to its first instruction to the current code
1040
ALL_SPACES(kFromStart, kFromCode, kInnerPointer)
1041
// Find an object in the roots array and write a pointer to it to the
1043
CASE_STATEMENT(kRootArray, kPlain, kStartOfObject, 0)
1044
CASE_BODY(kRootArray, kPlain, kStartOfObject, 0, kUnknownOffsetFromStart)
1045
// Find an object in the partial snapshots cache and write a pointer to it
1046
// to the current object.
1047
CASE_STATEMENT(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
1048
CASE_BODY(kPartialSnapshotCache,
1052
kUnknownOffsetFromStart)
1053
// Find an code entry in the partial snapshots cache and
1054
// write a pointer to it to the current object.
1055
CASE_STATEMENT(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
1056
CASE_BODY(kPartialSnapshotCache,
1060
kUnknownOffsetFromStart)
1061
// Find an external reference and write a pointer to it to the current
1063
CASE_STATEMENT(kExternalReference, kPlain, kStartOfObject, 0)
1064
CASE_BODY(kExternalReference,
1068
kUnknownOffsetFromStart)
1069
// Find an external reference and write a pointer to it in the current
1071
CASE_STATEMENT(kExternalReference, kFromCode, kStartOfObject, 0)
1072
CASE_BODY(kExternalReference,
1076
kUnknownOffsetFromStart)
1078
#undef CASE_STATEMENT
1080
#undef ONE_PER_SPACE
1082
#undef ASSIGN_DEST_SPACE
1085
int space = source_->Get();
1086
pages_[space].Add(last_object_address_);
1087
if (space == CODE_SPACE) {
1088
CPU::FlushICache(last_object_address_, Page::kPageSize);
1098
case kNativesStringResource: {
1099
int index = source_->Get();
1100
Vector<const char> source_vector = Natives::GetRawScriptSource(index);
1101
NativesExternalStringResource* resource =
1102
new NativesExternalStringResource(isolate->bootstrapper(),
1103
source_vector.start(),
1104
source_vector.length());
1105
*current++ = reinterpret_cast<Object*>(resource);
1109
case kSynchronize: {
1110
// If we get here then that indicates that you have a mismatch between
1111
// the number of GC roots when serializing and deserializing.
1119
ASSERT_EQ(current, limit);
1123
void SnapshotByteSink::PutInt(uintptr_t integer, const char* description) {
1124
const int max_shift = ((kPointerSize * kBitsPerByte) / 7) * 7;
1125
for (int shift = max_shift; shift > 0; shift -= 7) {
1126
if (integer >= static_cast<uintptr_t>(1u) << shift) {
1127
Put((static_cast<int>((integer >> shift)) & 0x7f) | 0x80, "IntPart");
1130
PutSection(static_cast<int>(integer & 0x7f), "IntLastPart");
1134
Serializer::Serializer(SnapshotByteSink* sink)
1136
current_root_index_(0),
1137
external_reference_encoder_(new ExternalReferenceEncoder),
1138
large_object_total_(0),
1139
root_index_wave_front_(0) {
1140
isolate_ = Isolate::Current();
1141
// The serializer is meant to be used only to generate initial heap images
1142
// from a context in which there is only one isolate.
1143
ASSERT(isolate_->IsDefaultIsolate());
1144
for (int i = 0; i <= LAST_SPACE; i++) {
1150
Serializer::~Serializer() {
1151
delete external_reference_encoder_;
1155
void StartupSerializer::SerializeStrongReferences() {
1156
Isolate* isolate = Isolate::Current();
1157
// No active threads.
1158
CHECK_EQ(NULL, Isolate::Current()->thread_manager()->FirstThreadStateInUse());
1159
// No active or weak handles.
1160
CHECK(isolate->handle_scope_implementer()->blocks()->is_empty());
1161
CHECK_EQ(0, isolate->global_handles()->NumberOfWeakHandles());
1162
// We don't support serializing installed extensions.
1163
CHECK(!isolate->has_installed_extensions());
1165
HEAP->IterateStrongRoots(this, VISIT_ONLY_STRONG);
1169
void PartialSerializer::Serialize(Object** object) {
1170
this->VisitPointer(object);
1174
void Serializer::VisitPointers(Object** start, Object** end) {
1175
Isolate* isolate = Isolate::Current();
1177
for (Object** current = start; current < end; current++) {
1178
if (start == isolate->heap()->roots_array_start()) {
1179
root_index_wave_front_ =
1180
Max(root_index_wave_front_, static_cast<intptr_t>(current - start));
1182
if (reinterpret_cast<Address>(current) ==
1183
isolate->heap()->store_buffer()->TopAddress()) {
1184
sink_->Put(kSkip, "Skip");
1185
} else if ((*current)->IsSmi()) {
1186
sink_->Put(kRawData, "RawData");
1187
sink_->PutInt(kPointerSize, "length");
1188
for (int i = 0; i < kPointerSize; i++) {
1189
sink_->Put(reinterpret_cast<byte*>(current)[i], "Byte");
1192
SerializeObject(*current, kPlain, kStartOfObject);
1198
// This ensures that the partial snapshot cache keeps things alive during GC and
1199
// tracks their movement. When it is called during serialization of the startup
1200
// snapshot nothing happens. When the partial (context) snapshot is created,
1201
// this array is populated with the pointers that the partial snapshot will
1202
// need. As that happens we emit serialized objects to the startup snapshot
1203
// that correspond to the elements of this cache array. On deserialization we
1204
// therefore need to visit the cache array. This fills it up with pointers to
1205
// deserialized objects.
1206
void SerializerDeserializer::Iterate(ObjectVisitor* visitor) {
1207
if (Serializer::enabled()) return;
1208
Isolate* isolate = Isolate::Current();
1209
for (int i = 0; ; i++) {
1210
if (isolate->serialize_partial_snapshot_cache_length() <= i) {
1211
// Extend the array ready to get a value from the visitor when
1213
isolate->PushToPartialSnapshotCache(Smi::FromInt(0));
1215
Object** cache = isolate->serialize_partial_snapshot_cache();
1216
visitor->VisitPointers(&cache[i], &cache[i + 1]);
1217
// Sentinel is the undefined object, which is a root so it will not normally
1218
// be found in the cache.
1219
if (cache[i] == isolate->heap()->undefined_value()) {
1226
int PartialSerializer::PartialSnapshotCacheIndex(HeapObject* heap_object) {
1227
Isolate* isolate = Isolate::Current();
1230
i < isolate->serialize_partial_snapshot_cache_length();
1232
Object* entry = isolate->serialize_partial_snapshot_cache()[i];
1233
if (entry == heap_object) return i;
1236
// We didn't find the object in the cache. So we add it to the cache and
1237
// then visit the pointer so that it becomes part of the startup snapshot
1238
// and we can refer to it from the partial snapshot.
1239
int length = isolate->serialize_partial_snapshot_cache_length();
1240
isolate->PushToPartialSnapshotCache(heap_object);
1241
startup_serializer_->VisitPointer(reinterpret_cast<Object**>(&heap_object));
1242
// We don't recurse from the startup snapshot generator into the partial
1243
// snapshot generator.
1244
ASSERT(length == isolate->serialize_partial_snapshot_cache_length() - 1);
1249
int Serializer::RootIndex(HeapObject* heap_object, HowToCode from) {
1251
if (heap->InNewSpace(heap_object)) return kInvalidRootIndex;
1252
for (int i = 0; i < root_index_wave_front_; i++) {
1253
Object* root = heap->roots_array_start()[i];
1254
if (!root->IsSmi() && root == heap_object) {
1255
#if V8_TARGET_ARCH_MIPS
1256
if (from == kFromCode) {
1257
// In order to avoid code bloat in the deserializer we don't have
1258
// support for the encoding that specifies a particular root should
1259
// be written into the lui/ori instructions on MIPS. Therefore we
1260
// should not generate such serialization data for MIPS.
1261
return kInvalidRootIndex;
1267
return kInvalidRootIndex;
1271
// Encode the location of an already deserialized object in order to write its
1272
// location into a later object. We can encode the location as an offset from
1273
// the start of the deserialized objects or as an offset backwards from the
1274
// current allocation pointer.
1275
void Serializer::SerializeReferenceToPreviousObject(
1278
HowToCode how_to_code,
1279
WhereToPoint where_to_point) {
1280
int offset = CurrentAllocationAddress(space) - address;
1281
bool from_start = true;
1282
if (SpaceIsPaged(space)) {
1283
// For paged space it is simple to encode back from current allocation if
1284
// the object is on the same page as the current allocation pointer.
1285
if ((CurrentAllocationAddress(space) >> kPageSizeBits) ==
1286
(address >> kPageSizeBits)) {
1290
} else if (space == NEW_SPACE) {
1291
// For new space it is always simple to encode back from current allocation.
1292
if (offset < address) {
1297
// If we are actually dealing with real offsets (and not a numbering of
1298
// all objects) then we should shift out the bits that are always 0.
1299
if (!SpaceIsLarge(space)) address >>= kObjectAlignmentBits;
1301
sink_->Put(kFromStart + how_to_code + where_to_point + space, "RefSer");
1302
sink_->PutInt(address, "address");
1304
sink_->Put(kBackref + how_to_code + where_to_point + space, "BackRefSer");
1305
sink_->PutInt(address, "address");
1310
void StartupSerializer::SerializeObject(
1312
HowToCode how_to_code,
1313
WhereToPoint where_to_point) {
1314
CHECK(o->IsHeapObject());
1315
HeapObject* heap_object = HeapObject::cast(o);
1318
if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1319
PutRoot(root_index, heap_object, how_to_code, where_to_point);
1323
if (address_mapper_.IsMapped(heap_object)) {
1324
int space = SpaceOfAlreadySerializedObject(heap_object);
1325
int address = address_mapper_.MappedTo(heap_object);
1326
SerializeReferenceToPreviousObject(space,
1331
// Object has not yet been serialized. Serialize it here.
1332
ObjectSerializer object_serializer(this,
1337
object_serializer.Serialize();
1342
void StartupSerializer::SerializeWeakReferences() {
1343
// This phase comes right after the partial serialization (of the snapshot).
1344
// After we have done the partial serialization the partial snapshot cache
1345
// will contain some references needed to decode the partial snapshot. We
1346
// add one entry with 'undefined' which is the sentinel that the deserializer
1347
// uses to know it is done deserializing the array.
1348
Isolate* isolate = Isolate::Current();
1349
Object* undefined = isolate->heap()->undefined_value();
1350
VisitPointer(&undefined);
1351
HEAP->IterateWeakRoots(this, VISIT_ALL);
1355
void Serializer::PutRoot(int root_index,
1357
SerializerDeserializer::HowToCode how_to_code,
1358
SerializerDeserializer::WhereToPoint where_to_point) {
1359
if (how_to_code == kPlain &&
1360
where_to_point == kStartOfObject &&
1361
root_index < kRootArrayNumberOfConstantEncodings &&
1362
!HEAP->InNewSpace(object)) {
1363
if (root_index < kRootArrayNumberOfLowConstantEncodings) {
1364
sink_->Put(kRootArrayLowConstants + root_index, "RootLoConstant");
1366
sink_->Put(kRootArrayHighConstants + root_index -
1367
kRootArrayNumberOfLowConstantEncodings,
1371
sink_->Put(kRootArray + how_to_code + where_to_point, "RootSerialization");
1372
sink_->PutInt(root_index, "root_index");
1377
void PartialSerializer::SerializeObject(
1379
HowToCode how_to_code,
1380
WhereToPoint where_to_point) {
1381
CHECK(o->IsHeapObject());
1382
HeapObject* heap_object = HeapObject::cast(o);
1384
if (heap_object->IsMap()) {
1385
// The code-caches link to context-specific code objects, which
1386
// the startup and context serializes cannot currently handle.
1387
ASSERT(Map::cast(heap_object)->code_cache() ==
1388
heap_object->GetHeap()->raw_unchecked_empty_fixed_array());
1392
if ((root_index = RootIndex(heap_object, how_to_code)) != kInvalidRootIndex) {
1393
PutRoot(root_index, heap_object, how_to_code, where_to_point);
1397
if (ShouldBeInThePartialSnapshotCache(heap_object)) {
1398
int cache_index = PartialSnapshotCacheIndex(heap_object);
1399
sink_->Put(kPartialSnapshotCache + how_to_code + where_to_point,
1400
"PartialSnapshotCache");
1401
sink_->PutInt(cache_index, "partial_snapshot_cache_index");
1405
// Pointers from the partial snapshot to the objects in the startup snapshot
1406
// should go through the root array or through the partial snapshot cache.
1407
// If this is not the case you may have to add something to the root array.
1408
ASSERT(!startup_serializer_->address_mapper()->IsMapped(heap_object));
1409
// All the symbols that the partial snapshot needs should be either in the
1410
// root table or in the partial snapshot cache.
1411
ASSERT(!heap_object->IsSymbol());
1413
if (address_mapper_.IsMapped(heap_object)) {
1414
int space = SpaceOfAlreadySerializedObject(heap_object);
1415
int address = address_mapper_.MappedTo(heap_object);
1416
SerializeReferenceToPreviousObject(space,
1421
// Object has not yet been serialized. Serialize it here.
1422
ObjectSerializer serializer(this,
1427
serializer.Serialize();
1432
void Serializer::ObjectSerializer::Serialize() {
1433
int space = Serializer::SpaceOfObject(object_);
1434
int size = object_->Size();
1436
sink_->Put(kNewObject + reference_representation_ + space,
1437
"ObjectSerialization");
1438
sink_->PutInt(size >> kObjectAlignmentBits, "Size in words");
1440
LOG(i::Isolate::Current(),
1441
SnapshotPositionEvent(object_->address(), sink_->Position()));
1443
// Mark this object as already serialized.
1444
bool start_new_page;
1445
int offset = serializer_->Allocate(space, size, &start_new_page);
1446
serializer_->address_mapper()->AddMapping(object_, offset);
1447
if (start_new_page) {
1448
sink_->Put(kNewPage, "NewPage");
1449
sink_->PutSection(space, "NewPageSpace");
1452
// Serialize the map (first word of the object).
1453
serializer_->SerializeObject(object_->map(), kPlain, kStartOfObject);
1455
// Serialize the rest of the object.
1456
CHECK_EQ(0, bytes_processed_so_far_);
1457
bytes_processed_so_far_ = kPointerSize;
1458
object_->IterateBody(object_->map()->instance_type(), size, this);
1459
OutputRawData(object_->address() + size);
1463
void Serializer::ObjectSerializer::VisitPointers(Object** start,
1465
Object** current = start;
1466
while (current < end) {
1467
while (current < end && (*current)->IsSmi()) current++;
1468
if (current < end) OutputRawData(reinterpret_cast<Address>(current));
1470
while (current < end && !(*current)->IsSmi()) {
1471
HeapObject* current_contents = HeapObject::cast(*current);
1472
int root_index = serializer_->RootIndex(current_contents, kPlain);
1473
// Repeats are not subject to the write barrier so there are only some
1474
// objects that can be used in a repeat encoding. These are the early
1475
// ones in the root array that are never in new space.
1476
if (current != start &&
1477
root_index != kInvalidRootIndex &&
1478
root_index < kRootArrayNumberOfConstantEncodings &&
1479
current_contents == current[-1]) {
1480
ASSERT(!HEAP->InNewSpace(current_contents));
1481
int repeat_count = 1;
1482
while (current < end - 1 && current[repeat_count] == current_contents) {
1485
current += repeat_count;
1486
bytes_processed_so_far_ += repeat_count * kPointerSize;
1487
if (repeat_count > kMaxRepeats) {
1488
sink_->Put(kRepeat, "SerializeRepeats");
1489
sink_->PutInt(repeat_count, "SerializeRepeats");
1491
sink_->Put(CodeForRepeats(repeat_count), "SerializeRepeats");
1494
serializer_->SerializeObject(current_contents, kPlain, kStartOfObject);
1495
bytes_processed_so_far_ += kPointerSize;
1503
void Serializer::ObjectSerializer::VisitEmbeddedPointer(RelocInfo* rinfo) {
1504
Object** current = rinfo->target_object_address();
1506
OutputRawData(rinfo->target_address_address());
1507
HowToCode representation = rinfo->IsCodedSpecially() ? kFromCode : kPlain;
1508
serializer_->SerializeObject(*current, representation, kStartOfObject);
1509
bytes_processed_so_far_ += rinfo->target_address_size();
1513
void Serializer::ObjectSerializer::VisitExternalReferences(Address* start,
1515
Address references_start = reinterpret_cast<Address>(start);
1516
OutputRawData(references_start);
1518
for (Address* current = start; current < end; current++) {
1519
sink_->Put(kExternalReference + kPlain + kStartOfObject, "ExternalRef");
1520
int reference_id = serializer_->EncodeExternalReference(*current);
1521
sink_->PutInt(reference_id, "reference id");
1523
bytes_processed_so_far_ += static_cast<int>((end - start) * kPointerSize);
1527
void Serializer::ObjectSerializer::VisitExternalReference(RelocInfo* rinfo) {
1528
Address references_start = rinfo->target_address_address();
1529
OutputRawData(references_start);
1531
Address* current = rinfo->target_reference_address();
1532
int representation = rinfo->IsCodedSpecially() ?
1533
kFromCode + kStartOfObject : kPlain + kStartOfObject;
1534
sink_->Put(kExternalReference + representation, "ExternalRef");
1535
int reference_id = serializer_->EncodeExternalReference(*current);
1536
sink_->PutInt(reference_id, "reference id");
1537
bytes_processed_so_far_ += rinfo->target_address_size();
1541
void Serializer::ObjectSerializer::VisitRuntimeEntry(RelocInfo* rinfo) {
1542
Address target_start = rinfo->target_address_address();
1543
OutputRawData(target_start);
1544
Address target = rinfo->target_address();
1545
uint32_t encoding = serializer_->EncodeExternalReference(target);
1546
CHECK(target == NULL ? encoding == 0 : encoding != 0);
1548
// Can't use a ternary operator because of gcc.
1549
if (rinfo->IsCodedSpecially()) {
1550
representation = kStartOfObject + kFromCode;
1552
representation = kStartOfObject + kPlain;
1554
sink_->Put(kExternalReference + representation, "ExternalReference");
1555
sink_->PutInt(encoding, "reference id");
1556
bytes_processed_so_far_ += rinfo->target_address_size();
1560
void Serializer::ObjectSerializer::VisitCodeTarget(RelocInfo* rinfo) {
1561
CHECK(RelocInfo::IsCodeTarget(rinfo->rmode()));
1562
Address target_start = rinfo->target_address_address();
1563
OutputRawData(target_start);
1564
Code* target = Code::GetCodeFromTargetAddress(rinfo->target_address());
1565
serializer_->SerializeObject(target, kFromCode, kInnerPointer);
1566
bytes_processed_so_far_ += rinfo->target_address_size();
1570
void Serializer::ObjectSerializer::VisitCodeEntry(Address entry_address) {
1571
Code* target = Code::cast(Code::GetObjectFromEntryAddress(entry_address));
1572
OutputRawData(entry_address);
1573
serializer_->SerializeObject(target, kPlain, kInnerPointer);
1574
bytes_processed_so_far_ += kPointerSize;
1578
void Serializer::ObjectSerializer::VisitGlobalPropertyCell(RelocInfo* rinfo) {
1579
ASSERT(rinfo->rmode() == RelocInfo::GLOBAL_PROPERTY_CELL);
1580
JSGlobalPropertyCell* cell =
1581
JSGlobalPropertyCell::cast(rinfo->target_cell());
1582
OutputRawData(rinfo->pc());
1583
serializer_->SerializeObject(cell, kPlain, kInnerPointer);
1587
void Serializer::ObjectSerializer::VisitExternalAsciiString(
1588
v8::String::ExternalAsciiStringResource** resource_pointer) {
1589
Address references_start = reinterpret_cast<Address>(resource_pointer);
1590
OutputRawData(references_start);
1591
for (int i = 0; i < Natives::GetBuiltinsCount(); i++) {
1592
Object* source = HEAP->natives_source_cache()->get(i);
1593
if (!source->IsUndefined()) {
1594
ExternalAsciiString* string = ExternalAsciiString::cast(source);
1595
typedef v8::String::ExternalAsciiStringResource Resource;
1596
const Resource* resource = string->resource();
1597
if (resource == *resource_pointer) {
1598
sink_->Put(kNativesStringResource, "NativesStringResource");
1599
sink_->PutSection(i, "NativesStringResourceEnd");
1600
bytes_processed_so_far_ += sizeof(resource);
1605
// One of the strings in the natives cache should match the resource. We
1606
// can't serialize any other kinds of external strings.
1611
void Serializer::ObjectSerializer::OutputRawData(Address up_to) {
1612
Address object_start = object_->address();
1613
int up_to_offset = static_cast<int>(up_to - object_start);
1614
int skipped = up_to_offset - bytes_processed_so_far_;
1615
// This assert will fail if the reloc info gives us the target_address_address
1616
// locations in a non-ascending order. Luckily that doesn't happen.
1617
ASSERT(skipped >= 0);
1619
Address base = object_start + bytes_processed_so_far_;
1620
#define RAW_CASE(index, length) \
1621
if (skipped == length) { \
1622
sink_->PutSection(kRawData + index, "RawDataFixed"); \
1624
COMMON_RAW_LENGTHS(RAW_CASE)
1627
sink_->Put(kRawData, "RawData");
1628
sink_->PutInt(skipped, "length");
1630
for (int i = 0; i < skipped; i++) {
1631
unsigned int data = base[i];
1632
sink_->PutSection(data, "Byte");
1634
bytes_processed_so_far_ += skipped;
1639
int Serializer::SpaceOfObject(HeapObject* object) {
1640
for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1641
AllocationSpace s = static_cast<AllocationSpace>(i);
1642
if (HEAP->InSpace(object, s)) {
1643
if (i == LO_SPACE) {
1644
if (object->IsCode()) {
1646
} else if (object->IsFixedArray()) {
1647
return kLargeFixedArray;
1660
int Serializer::SpaceOfAlreadySerializedObject(HeapObject* object) {
1661
for (int i = FIRST_SPACE; i <= LAST_SPACE; i++) {
1662
AllocationSpace s = static_cast<AllocationSpace>(i);
1663
if (HEAP->InSpace(object, s)) {
1672
int Serializer::Allocate(int space, int size, bool* new_page) {
1673
CHECK(space >= 0 && space < kNumberOfSpaces);
1674
if (SpaceIsLarge(space)) {
1675
// In large object space we merely number the objects instead of trying to
1676
// determine some sort of address.
1678
large_object_total_ += size;
1679
return fullness_[LO_SPACE]++;
1682
if (fullness_[space] == 0) {
1685
if (SpaceIsPaged(space)) {
1686
// Paged spaces are a little special. We encode their addresses as if the
1687
// pages were all contiguous and each page were filled up in the range
1688
// 0 - Page::kObjectAreaSize. In practice the pages may not be contiguous
1689
// and allocation does not start at offset 0 in the page, but this scheme
1690
// means the deserializer can get the page number quickly by shifting the
1691
// serialized address.
1692
CHECK(IsPowerOf2(Page::kPageSize));
1693
int used_in_this_page = (fullness_[space] & (Page::kPageSize - 1));
1694
CHECK(size <= SpaceAreaSize(space));
1695
if (used_in_this_page + size > SpaceAreaSize(space)) {
1697
fullness_[space] = RoundUp(fullness_[space], Page::kPageSize);
1700
int allocation_address = fullness_[space];
1701
fullness_[space] = allocation_address + size;
1702
return allocation_address;
1706
int Serializer::SpaceAreaSize(int space) {
1707
if (space == CODE_SPACE) {
1708
return isolate_->memory_allocator()->CodePageAreaSize();
1710
return Page::kPageSize - Page::kObjectStartOffset;
1715
} } // namespace v8::internal