blob: 0a21feffa1484d7714e398ea15955778c52f7075 [file] [log] [blame]
Ben Murdochda12d292016-06-02 14:46:10 +01001// Copyright 2016 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/snapshot/deserializer.h"
6
7#include "src/bootstrapper.h"
8#include "src/external-reference-table.h"
9#include "src/heap/heap.h"
10#include "src/isolate.h"
11#include "src/macro-assembler.h"
12#include "src/snapshot/natives.h"
13#include "src/v8.h"
14
15namespace v8 {
16namespace internal {
17
18void Deserializer::DecodeReservation(
19 Vector<const SerializedData::Reservation> res) {
20 DCHECK_EQ(0, reservations_[NEW_SPACE].length());
21 STATIC_ASSERT(NEW_SPACE == 0);
22 int current_space = NEW_SPACE;
23 for (auto& r : res) {
24 reservations_[current_space].Add({r.chunk_size(), NULL, NULL});
25 if (r.is_last()) current_space++;
26 }
27 DCHECK_EQ(kNumberOfSpaces, current_space);
28 for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) current_chunk_[i] = 0;
29}
30
31void Deserializer::FlushICacheForNewIsolate() {
32 DCHECK(!deserializing_user_code_);
33 // The entire isolate is newly deserialized. Simply flush all code pages.
34 PageIterator it(isolate_->heap()->code_space());
35 while (it.has_next()) {
36 Page* p = it.next();
37 Assembler::FlushICache(isolate_, p->area_start(),
38 p->area_end() - p->area_start());
39 }
40}
41
42void Deserializer::FlushICacheForNewCodeObjects() {
43 DCHECK(deserializing_user_code_);
44 for (Code* code : new_code_objects_) {
45 if (FLAG_serialize_age_code) code->PreAge(isolate_);
46 Assembler::FlushICache(isolate_, code->instruction_start(),
47 code->instruction_size());
48 }
49}
50
51bool Deserializer::ReserveSpace() {
52#ifdef DEBUG
53 for (int i = NEW_SPACE; i < kNumberOfSpaces; ++i) {
54 CHECK(reservations_[i].length() > 0);
55 }
56#endif // DEBUG
57 if (!isolate_->heap()->ReserveSpace(reservations_)) return false;
58 for (int i = 0; i < kNumberOfPreallocatedSpaces; i++) {
59 high_water_[i] = reservations_[i][0].start;
60 }
61 return true;
62}
63
64void Deserializer::Initialize(Isolate* isolate) {
65 DCHECK_NULL(isolate_);
66 DCHECK_NOT_NULL(isolate);
67 isolate_ = isolate;
68 DCHECK_NULL(external_reference_table_);
69 external_reference_table_ = ExternalReferenceTable::instance(isolate);
70 CHECK_EQ(magic_number_,
71 SerializedData::ComputeMagicNumber(external_reference_table_));
72}
73
74void Deserializer::Deserialize(Isolate* isolate) {
75 Initialize(isolate);
76 if (!ReserveSpace()) V8::FatalProcessOutOfMemory("deserializing context");
77 // No active threads.
78 DCHECK_NULL(isolate_->thread_manager()->FirstThreadStateInUse());
79 // No active handles.
80 DCHECK(isolate_->handle_scope_implementer()->blocks()->is_empty());
81 // Partial snapshot cache is not yet populated.
82 DCHECK(isolate_->partial_snapshot_cache()->is_empty());
83
84 {
85 DisallowHeapAllocation no_gc;
86 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG_ROOT_LIST);
87 isolate_->heap()->IterateSmiRoots(this);
88 isolate_->heap()->IterateStrongRoots(this, VISIT_ONLY_STRONG);
89 isolate_->heap()->RepairFreeListsAfterDeserialization();
90 isolate_->heap()->IterateWeakRoots(this, VISIT_ALL);
91 DeserializeDeferredObjects();
92 FlushICacheForNewIsolate();
93 }
94
95 isolate_->heap()->set_native_contexts_list(
96 isolate_->heap()->undefined_value());
97 // The allocation site list is build during root iteration, but if no sites
98 // were encountered then it needs to be initialized to undefined.
99 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
100 isolate_->heap()->set_allocation_sites_list(
101 isolate_->heap()->undefined_value());
102 }
103
104 // Update data pointers to the external strings containing natives sources.
105 Natives::UpdateSourceCache(isolate_->heap());
106 ExtraNatives::UpdateSourceCache(isolate_->heap());
107
108 // Issue code events for newly deserialized code objects.
109 LOG_CODE_EVENT(isolate_, LogCodeObjects());
110 LOG_CODE_EVENT(isolate_, LogBytecodeHandlers());
111 LOG_CODE_EVENT(isolate_, LogCompiledFunctions());
112}
113
114MaybeHandle<Object> Deserializer::DeserializePartial(
115 Isolate* isolate, Handle<JSGlobalProxy> global_proxy) {
116 Initialize(isolate);
117 if (!ReserveSpace()) {
118 V8::FatalProcessOutOfMemory("deserialize context");
119 return MaybeHandle<Object>();
120 }
121
122 Vector<Handle<Object> > attached_objects = Vector<Handle<Object> >::New(1);
123 attached_objects[kGlobalProxyReference] = global_proxy;
124 SetAttachedObjects(attached_objects);
125
126 DisallowHeapAllocation no_gc;
127 // Keep track of the code space start and end pointers in case new
128 // code objects were unserialized
129 OldSpace* code_space = isolate_->heap()->code_space();
130 Address start_address = code_space->top();
131 Object* root;
132 VisitPointer(&root);
133 DeserializeDeferredObjects();
134
135 isolate->heap()->RegisterReservationsForBlackAllocation(reservations_);
136
137 // There's no code deserialized here. If this assert fires then that's
138 // changed and logging should be added to notify the profiler et al of the
139 // new code, which also has to be flushed from instruction cache.
140 CHECK_EQ(start_address, code_space->top());
141 return Handle<Object>(root, isolate);
142}
143
144MaybeHandle<SharedFunctionInfo> Deserializer::DeserializeCode(
145 Isolate* isolate) {
146 Initialize(isolate);
147 if (!ReserveSpace()) {
148 return Handle<SharedFunctionInfo>();
149 } else {
150 deserializing_user_code_ = true;
151 HandleScope scope(isolate);
152 Handle<SharedFunctionInfo> result;
153 {
154 DisallowHeapAllocation no_gc;
155 Object* root;
156 VisitPointer(&root);
157 DeserializeDeferredObjects();
158 FlushICacheForNewCodeObjects();
159 result = Handle<SharedFunctionInfo>(SharedFunctionInfo::cast(root));
160 isolate->heap()->RegisterReservationsForBlackAllocation(reservations_);
161 }
162 CommitPostProcessedObjects(isolate);
163 return scope.CloseAndEscape(result);
164 }
165}
166
167Deserializer::~Deserializer() {
168 // TODO(svenpanne) Re-enable this assertion when v8 initialization is fixed.
169 // DCHECK(source_.AtEOF());
170 attached_objects_.Dispose();
171}
172
173// This is called on the roots. It is the driver of the deserialization
174// process. It is also called on the body of each function.
175void Deserializer::VisitPointers(Object** start, Object** end) {
176 // The space must be new space. Any other space would cause ReadChunk to try
177 // to update the remembered using NULL as the address.
178 ReadData(start, end, NEW_SPACE, NULL);
179}
180
181void Deserializer::Synchronize(VisitorSynchronization::SyncTag tag) {
182 static const byte expected = kSynchronize;
183 CHECK_EQ(expected, source_.Get());
184}
185
186void Deserializer::DeserializeDeferredObjects() {
187 for (int code = source_.Get(); code != kSynchronize; code = source_.Get()) {
188 switch (code) {
189 case kAlignmentPrefix:
190 case kAlignmentPrefix + 1:
191 case kAlignmentPrefix + 2:
192 SetAlignment(code);
193 break;
194 default: {
195 int space = code & kSpaceMask;
196 DCHECK(space <= kNumberOfSpaces);
197 DCHECK(code - space == kNewObject);
198 HeapObject* object = GetBackReferencedObject(space);
199 int size = source_.GetInt() << kPointerSizeLog2;
200 Address obj_address = object->address();
201 Object** start = reinterpret_cast<Object**>(obj_address + kPointerSize);
202 Object** end = reinterpret_cast<Object**>(obj_address + size);
203 bool filled = ReadData(start, end, space, obj_address);
204 CHECK(filled);
205 DCHECK(CanBeDeferred(object));
206 PostProcessNewObject(object, space);
207 }
208 }
209 }
210}
211
212// Used to insert a deserialized internalized string into the string table.
213class StringTableInsertionKey : public HashTableKey {
214 public:
215 explicit StringTableInsertionKey(String* string)
216 : string_(string), hash_(HashForObject(string)) {
217 DCHECK(string->IsInternalizedString());
218 }
219
220 bool IsMatch(Object* string) override {
221 // We know that all entries in a hash table had their hash keys created.
222 // Use that knowledge to have fast failure.
223 if (hash_ != HashForObject(string)) return false;
224 // We want to compare the content of two internalized strings here.
225 return string_->SlowEquals(String::cast(string));
226 }
227
228 uint32_t Hash() override { return hash_; }
229
230 uint32_t HashForObject(Object* key) override {
231 return String::cast(key)->Hash();
232 }
233
234 MUST_USE_RESULT Handle<Object> AsHandle(Isolate* isolate) override {
235 return handle(string_, isolate);
236 }
237
238 private:
239 String* string_;
240 uint32_t hash_;
241 DisallowHeapAllocation no_gc;
242};
243
244HeapObject* Deserializer::PostProcessNewObject(HeapObject* obj, int space) {
245 if (deserializing_user_code()) {
246 if (obj->IsString()) {
247 String* string = String::cast(obj);
248 // Uninitialize hash field as the hash seed may have changed.
249 string->set_hash_field(String::kEmptyHashField);
250 if (string->IsInternalizedString()) {
251 // Canonicalize the internalized string. If it already exists in the
252 // string table, set it to forward to the existing one.
253 StringTableInsertionKey key(string);
254 String* canonical = StringTable::LookupKeyIfExists(isolate_, &key);
255 if (canonical == NULL) {
256 new_internalized_strings_.Add(handle(string));
257 return string;
258 } else {
259 string->SetForwardedInternalizedString(canonical);
260 return canonical;
261 }
262 }
263 } else if (obj->IsScript()) {
264 new_scripts_.Add(handle(Script::cast(obj)));
265 } else {
266 DCHECK(CanBeDeferred(obj));
267 }
268 }
269 if (obj->IsAllocationSite()) {
270 DCHECK(obj->IsAllocationSite());
271 // Allocation sites are present in the snapshot, and must be linked into
272 // a list at deserialization time.
273 AllocationSite* site = AllocationSite::cast(obj);
274 // TODO(mvstanton): consider treating the heap()->allocation_sites_list()
275 // as a (weak) root. If this root is relocated correctly, this becomes
276 // unnecessary.
277 if (isolate_->heap()->allocation_sites_list() == Smi::FromInt(0)) {
278 site->set_weak_next(isolate_->heap()->undefined_value());
279 } else {
280 site->set_weak_next(isolate_->heap()->allocation_sites_list());
281 }
282 isolate_->heap()->set_allocation_sites_list(site);
283 } else if (obj->IsCode()) {
284 // We flush all code pages after deserializing the startup snapshot. In that
285 // case, we only need to remember code objects in the large object space.
286 // When deserializing user code, remember each individual code object.
287 if (deserializing_user_code() || space == LO_SPACE) {
288 new_code_objects_.Add(Code::cast(obj));
289 }
290 }
291 // Check alignment.
292 DCHECK_EQ(0, Heap::GetFillToAlign(obj->address(), obj->RequiredAlignment()));
293 return obj;
294}
295
296void Deserializer::CommitPostProcessedObjects(Isolate* isolate) {
297 StringTable::EnsureCapacityForDeserialization(
298 isolate, new_internalized_strings_.length());
299 for (Handle<String> string : new_internalized_strings_) {
300 StringTableInsertionKey key(*string);
301 DCHECK_NULL(StringTable::LookupKeyIfExists(isolate, &key));
302 StringTable::LookupKey(isolate, &key);
303 }
304
305 Heap* heap = isolate->heap();
306 Factory* factory = isolate->factory();
307 for (Handle<Script> script : new_scripts_) {
308 // Assign a new script id to avoid collision.
309 script->set_id(isolate_->heap()->NextScriptId());
310 // Add script to list.
311 Handle<Object> list = WeakFixedArray::Add(factory->script_list(), script);
312 heap->SetRootScriptList(*list);
313 }
314}
315
316HeapObject* Deserializer::GetBackReferencedObject(int space) {
317 HeapObject* obj;
318 BackReference back_reference(source_.GetInt());
319 if (space == LO_SPACE) {
320 CHECK(back_reference.chunk_index() == 0);
321 uint32_t index = back_reference.large_object_index();
322 obj = deserialized_large_objects_[index];
323 } else {
324 DCHECK(space < kNumberOfPreallocatedSpaces);
325 uint32_t chunk_index = back_reference.chunk_index();
326 DCHECK_LE(chunk_index, current_chunk_[space]);
327 uint32_t chunk_offset = back_reference.chunk_offset();
328 Address address = reservations_[space][chunk_index].start + chunk_offset;
329 if (next_alignment_ != kWordAligned) {
330 int padding = Heap::GetFillToAlign(address, next_alignment_);
331 next_alignment_ = kWordAligned;
332 DCHECK(padding == 0 || HeapObject::FromAddress(address)->IsFiller());
333 address += padding;
334 }
335 obj = HeapObject::FromAddress(address);
336 }
337 if (deserializing_user_code() && obj->IsInternalizedString()) {
338 obj = String::cast(obj)->GetForwardedInternalizedString();
339 }
340 hot_objects_.Add(obj);
341 return obj;
342}
343
344// This routine writes the new object into the pointer provided and then
345// returns true if the new object was in young space and false otherwise.
346// The reason for this strange interface is that otherwise the object is
347// written very late, which means the FreeSpace map is not set up by the
348// time we need to use it to mark the space at the end of a page free.
349void Deserializer::ReadObject(int space_number, Object** write_back) {
350 Address address;
351 HeapObject* obj;
352 int size = source_.GetInt() << kObjectAlignmentBits;
353
354 if (next_alignment_ != kWordAligned) {
355 int reserved = size + Heap::GetMaximumFillToAlign(next_alignment_);
356 address = Allocate(space_number, reserved);
357 obj = HeapObject::FromAddress(address);
358 // If one of the following assertions fails, then we are deserializing an
359 // aligned object when the filler maps have not been deserialized yet.
360 // We require filler maps as padding to align the object.
361 Heap* heap = isolate_->heap();
362 DCHECK(heap->free_space_map()->IsMap());
363 DCHECK(heap->one_pointer_filler_map()->IsMap());
364 DCHECK(heap->two_pointer_filler_map()->IsMap());
365 obj = heap->AlignWithFiller(obj, size, reserved, next_alignment_);
366 address = obj->address();
367 next_alignment_ = kWordAligned;
368 } else {
369 address = Allocate(space_number, size);
370 obj = HeapObject::FromAddress(address);
371 }
372
373 isolate_->heap()->OnAllocationEvent(obj, size);
374 Object** current = reinterpret_cast<Object**>(address);
375 Object** limit = current + (size >> kPointerSizeLog2);
376
377 if (ReadData(current, limit, space_number, address)) {
378 // Only post process if object content has not been deferred.
379 obj = PostProcessNewObject(obj, space_number);
380 }
381
382 Object* write_back_obj = obj;
383 UnalignedCopy(write_back, &write_back_obj);
384#ifdef DEBUG
385 if (obj->IsCode()) {
386 DCHECK(space_number == CODE_SPACE || space_number == LO_SPACE);
387 } else {
388 DCHECK(space_number != CODE_SPACE);
389 }
390#endif // DEBUG
391}
392
393// We know the space requirements before deserialization and can
394// pre-allocate that reserved space. During deserialization, all we need
395// to do is to bump up the pointer for each space in the reserved
396// space. This is also used for fixing back references.
397// We may have to split up the pre-allocation into several chunks
398// because it would not fit onto a single page. We do not have to keep
399// track of when to move to the next chunk. An opcode will signal this.
400// Since multiple large objects cannot be folded into one large object
401// space allocation, we have to do an actual allocation when deserializing
402// each large object. Instead of tracking offset for back references, we
403// reference large objects by index.
404Address Deserializer::Allocate(int space_index, int size) {
405 if (space_index == LO_SPACE) {
406 AlwaysAllocateScope scope(isolate_);
407 LargeObjectSpace* lo_space = isolate_->heap()->lo_space();
408 Executability exec = static_cast<Executability>(source_.Get());
409 AllocationResult result = lo_space->AllocateRaw(size, exec);
410 HeapObject* obj = HeapObject::cast(result.ToObjectChecked());
411 deserialized_large_objects_.Add(obj);
412 return obj->address();
413 } else {
414 DCHECK(space_index < kNumberOfPreallocatedSpaces);
415 Address address = high_water_[space_index];
416 DCHECK_NOT_NULL(address);
417 high_water_[space_index] += size;
418#ifdef DEBUG
419 // Assert that the current reserved chunk is still big enough.
420 const Heap::Reservation& reservation = reservations_[space_index];
421 int chunk_index = current_chunk_[space_index];
422 CHECK_LE(high_water_[space_index], reservation[chunk_index].end);
423#endif
424 if (space_index == CODE_SPACE) SkipList::Update(address, size);
425 return address;
426 }
427}
428
429Object** Deserializer::CopyInNativesSource(Vector<const char> source_vector,
430 Object** current) {
431 DCHECK(!isolate_->heap()->deserialization_complete());
432 NativesExternalStringResource* resource = new NativesExternalStringResource(
433 source_vector.start(), source_vector.length());
434 Object* resource_obj = reinterpret_cast<Object*>(resource);
435 UnalignedCopy(current++, &resource_obj);
436 return current;
437}
438
439bool Deserializer::ReadData(Object** current, Object** limit, int source_space,
440 Address current_object_address) {
441 Isolate* const isolate = isolate_;
442 // Write barrier support costs around 1% in startup time. In fact there
443 // are no new space objects in current boot snapshots, so it's not needed,
444 // but that may change.
445 bool write_barrier_needed =
446 (current_object_address != NULL && source_space != NEW_SPACE &&
447 source_space != CODE_SPACE);
448 while (current < limit) {
449 byte data = source_.Get();
450 switch (data) {
451#define CASE_STATEMENT(where, how, within, space_number) \
452 case where + how + within + space_number: \
453 STATIC_ASSERT((where & ~kWhereMask) == 0); \
454 STATIC_ASSERT((how & ~kHowToCodeMask) == 0); \
455 STATIC_ASSERT((within & ~kWhereToPointMask) == 0); \
456 STATIC_ASSERT((space_number & ~kSpaceMask) == 0);
457
458#define CASE_BODY(where, how, within, space_number_if_any) \
459 { \
460 bool emit_write_barrier = false; \
461 bool current_was_incremented = false; \
462 int space_number = space_number_if_any == kAnyOldSpace \
463 ? (data & kSpaceMask) \
464 : space_number_if_any; \
465 if (where == kNewObject && how == kPlain && within == kStartOfObject) { \
466 ReadObject(space_number, current); \
467 emit_write_barrier = (space_number == NEW_SPACE); \
468 } else { \
469 Object* new_object = NULL; /* May not be a real Object pointer. */ \
470 if (where == kNewObject) { \
471 ReadObject(space_number, &new_object); \
472 } else if (where == kBackref) { \
473 emit_write_barrier = (space_number == NEW_SPACE); \
474 new_object = GetBackReferencedObject(data & kSpaceMask); \
475 } else if (where == kBackrefWithSkip) { \
476 int skip = source_.GetInt(); \
477 current = reinterpret_cast<Object**>( \
478 reinterpret_cast<Address>(current) + skip); \
479 emit_write_barrier = (space_number == NEW_SPACE); \
480 new_object = GetBackReferencedObject(data & kSpaceMask); \
481 } else if (where == kRootArray) { \
482 int id = source_.GetInt(); \
483 Heap::RootListIndex root_index = static_cast<Heap::RootListIndex>(id); \
484 new_object = isolate->heap()->root(root_index); \
485 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
486 } else if (where == kPartialSnapshotCache) { \
487 int cache_index = source_.GetInt(); \
488 new_object = isolate->partial_snapshot_cache()->at(cache_index); \
489 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
490 } else if (where == kExternalReference) { \
491 int skip = source_.GetInt(); \
492 current = reinterpret_cast<Object**>( \
493 reinterpret_cast<Address>(current) + skip); \
494 int reference_id = source_.GetInt(); \
495 Address address = external_reference_table_->address(reference_id); \
496 new_object = reinterpret_cast<Object*>(address); \
497 } else if (where == kAttachedReference) { \
498 int index = source_.GetInt(); \
499 DCHECK(deserializing_user_code() || index == kGlobalProxyReference); \
500 new_object = *attached_objects_[index]; \
501 emit_write_barrier = isolate->heap()->InNewSpace(new_object); \
502 } else { \
503 DCHECK(where == kBuiltin); \
504 DCHECK(deserializing_user_code()); \
505 int builtin_id = source_.GetInt(); \
506 DCHECK_LE(0, builtin_id); \
507 DCHECK_LT(builtin_id, Builtins::builtin_count); \
508 Builtins::Name name = static_cast<Builtins::Name>(builtin_id); \
509 new_object = isolate->builtins()->builtin(name); \
510 emit_write_barrier = false; \
511 } \
512 if (within == kInnerPointer) { \
513 if (space_number != CODE_SPACE || new_object->IsCode()) { \
514 Code* new_code_object = reinterpret_cast<Code*>(new_object); \
515 new_object = \
516 reinterpret_cast<Object*>(new_code_object->instruction_start()); \
517 } else { \
518 DCHECK(space_number == CODE_SPACE); \
519 Cell* cell = Cell::cast(new_object); \
520 new_object = reinterpret_cast<Object*>(cell->ValueAddress()); \
521 } \
522 } \
523 if (how == kFromCode) { \
524 Address location_of_branch_data = reinterpret_cast<Address>(current); \
525 Assembler::deserialization_set_special_target_at( \
526 isolate, location_of_branch_data, \
527 Code::cast(HeapObject::FromAddress(current_object_address)), \
528 reinterpret_cast<Address>(new_object)); \
529 location_of_branch_data += Assembler::kSpecialTargetSize; \
530 current = reinterpret_cast<Object**>(location_of_branch_data); \
531 current_was_incremented = true; \
532 } else { \
533 UnalignedCopy(current, &new_object); \
534 } \
535 } \
536 if (emit_write_barrier && write_barrier_needed) { \
537 Address current_address = reinterpret_cast<Address>(current); \
538 SLOW_DCHECK(isolate->heap()->ContainsSlow(current_object_address)); \
539 isolate->heap()->RecordWrite( \
540 HeapObject::FromAddress(current_object_address), \
541 static_cast<int>(current_address - current_object_address), \
542 *reinterpret_cast<Object**>(current_address)); \
543 } \
544 if (!current_was_incremented) { \
545 current++; \
546 } \
547 break; \
548 }
549
550// This generates a case and a body for the new space (which has to do extra
551// write barrier handling) and handles the other spaces with fall-through cases
552// and one body.
553#define ALL_SPACES(where, how, within) \
554 CASE_STATEMENT(where, how, within, NEW_SPACE) \
555 CASE_BODY(where, how, within, NEW_SPACE) \
556 CASE_STATEMENT(where, how, within, OLD_SPACE) \
557 CASE_STATEMENT(where, how, within, CODE_SPACE) \
558 CASE_STATEMENT(where, how, within, MAP_SPACE) \
559 CASE_STATEMENT(where, how, within, LO_SPACE) \
560 CASE_BODY(where, how, within, kAnyOldSpace)
561
562#define FOUR_CASES(byte_code) \
563 case byte_code: \
564 case byte_code + 1: \
565 case byte_code + 2: \
566 case byte_code + 3:
567
568#define SIXTEEN_CASES(byte_code) \
569 FOUR_CASES(byte_code) \
570 FOUR_CASES(byte_code + 4) \
571 FOUR_CASES(byte_code + 8) \
572 FOUR_CASES(byte_code + 12)
573
574#define SINGLE_CASE(where, how, within, space) \
575 CASE_STATEMENT(where, how, within, space) \
576 CASE_BODY(where, how, within, space)
577
578 // Deserialize a new object and write a pointer to it to the current
579 // object.
580 ALL_SPACES(kNewObject, kPlain, kStartOfObject)
581 // Support for direct instruction pointers in functions. It's an inner
582 // pointer because it points at the entry point, not at the start of the
583 // code object.
584 SINGLE_CASE(kNewObject, kPlain, kInnerPointer, CODE_SPACE)
585 // Deserialize a new code object and write a pointer to its first
586 // instruction to the current code object.
587 ALL_SPACES(kNewObject, kFromCode, kInnerPointer)
588 // Find a recently deserialized object using its offset from the current
589 // allocation point and write a pointer to it to the current object.
590 ALL_SPACES(kBackref, kPlain, kStartOfObject)
591 ALL_SPACES(kBackrefWithSkip, kPlain, kStartOfObject)
592#if V8_CODE_EMBEDS_OBJECT_POINTER
593 // Deserialize a new object from pointer found in code and write
594 // a pointer to it to the current object. Required only for MIPS, PPC, ARM
595 // or S390 with embedded constant pool, and omitted on the other
596 // architectures because it is fully unrolled and would cause bloat.
597 ALL_SPACES(kNewObject, kFromCode, kStartOfObject)
598 // Find a recently deserialized code object using its offset from the
599 // current allocation point and write a pointer to it to the current
600 // object. Required only for MIPS, PPC, ARM or S390 with embedded
601 // constant pool.
602 ALL_SPACES(kBackref, kFromCode, kStartOfObject)
603 ALL_SPACES(kBackrefWithSkip, kFromCode, kStartOfObject)
604#endif
605 // Find a recently deserialized code object using its offset from the
606 // current allocation point and write a pointer to its first instruction
607 // to the current code object or the instruction pointer in a function
608 // object.
609 ALL_SPACES(kBackref, kFromCode, kInnerPointer)
610 ALL_SPACES(kBackrefWithSkip, kFromCode, kInnerPointer)
611 ALL_SPACES(kBackref, kPlain, kInnerPointer)
612 ALL_SPACES(kBackrefWithSkip, kPlain, kInnerPointer)
613 // Find an object in the roots array and write a pointer to it to the
614 // current object.
615 SINGLE_CASE(kRootArray, kPlain, kStartOfObject, 0)
616#if V8_CODE_EMBEDS_OBJECT_POINTER
617 // Find an object in the roots array and write a pointer to it to in code.
618 SINGLE_CASE(kRootArray, kFromCode, kStartOfObject, 0)
619#endif
620 // Find an object in the partial snapshots cache and write a pointer to it
621 // to the current object.
622 SINGLE_CASE(kPartialSnapshotCache, kPlain, kStartOfObject, 0)
623 // Find an code entry in the partial snapshots cache and
624 // write a pointer to it to the current object.
625 SINGLE_CASE(kPartialSnapshotCache, kPlain, kInnerPointer, 0)
626 // Find an external reference and write a pointer to it to the current
627 // object.
628 SINGLE_CASE(kExternalReference, kPlain, kStartOfObject, 0)
629 // Find an external reference and write a pointer to it in the current
630 // code object.
631 SINGLE_CASE(kExternalReference, kFromCode, kStartOfObject, 0)
632 // Find an object in the attached references and write a pointer to it to
633 // the current object.
634 SINGLE_CASE(kAttachedReference, kPlain, kStartOfObject, 0)
635 SINGLE_CASE(kAttachedReference, kPlain, kInnerPointer, 0)
636 SINGLE_CASE(kAttachedReference, kFromCode, kInnerPointer, 0)
637 // Find a builtin and write a pointer to it to the current object.
638 SINGLE_CASE(kBuiltin, kPlain, kStartOfObject, 0)
639 SINGLE_CASE(kBuiltin, kPlain, kInnerPointer, 0)
640 SINGLE_CASE(kBuiltin, kFromCode, kInnerPointer, 0)
641
642#undef CASE_STATEMENT
643#undef CASE_BODY
644#undef ALL_SPACES
645
646 case kSkip: {
647 int size = source_.GetInt();
648 current = reinterpret_cast<Object**>(
649 reinterpret_cast<intptr_t>(current) + size);
650 break;
651 }
652
653 case kInternalReferenceEncoded:
654 case kInternalReference: {
655 // Internal reference address is not encoded via skip, but by offset
656 // from code entry.
657 int pc_offset = source_.GetInt();
658 int target_offset = source_.GetInt();
659 Code* code =
660 Code::cast(HeapObject::FromAddress(current_object_address));
661 DCHECK(0 <= pc_offset && pc_offset <= code->instruction_size());
662 DCHECK(0 <= target_offset && target_offset <= code->instruction_size());
663 Address pc = code->entry() + pc_offset;
664 Address target = code->entry() + target_offset;
665 Assembler::deserialization_set_target_internal_reference_at(
666 isolate, pc, target, data == kInternalReference
667 ? RelocInfo::INTERNAL_REFERENCE
668 : RelocInfo::INTERNAL_REFERENCE_ENCODED);
669 break;
670 }
671
672 case kNop:
673 break;
674
675 case kNextChunk: {
676 int space = source_.Get();
677 DCHECK(space < kNumberOfPreallocatedSpaces);
678 int chunk_index = current_chunk_[space];
679 const Heap::Reservation& reservation = reservations_[space];
680 // Make sure the current chunk is indeed exhausted.
681 CHECK_EQ(reservation[chunk_index].end, high_water_[space]);
682 // Move to next reserved chunk.
683 chunk_index = ++current_chunk_[space];
684 CHECK_LT(chunk_index, reservation.length());
685 high_water_[space] = reservation[chunk_index].start;
686 break;
687 }
688
689 case kDeferred: {
690 // Deferred can only occur right after the heap object header.
691 DCHECK(current == reinterpret_cast<Object**>(current_object_address +
692 kPointerSize));
693 HeapObject* obj = HeapObject::FromAddress(current_object_address);
694 // If the deferred object is a map, its instance type may be used
695 // during deserialization. Initialize it with a temporary value.
696 if (obj->IsMap()) Map::cast(obj)->set_instance_type(FILLER_TYPE);
697 current = limit;
698 return false;
699 }
700
701 case kSynchronize:
702 // If we get here then that indicates that you have a mismatch between
703 // the number of GC roots when serializing and deserializing.
704 CHECK(false);
705 break;
706
707 case kNativesStringResource:
708 current = CopyInNativesSource(Natives::GetScriptSource(source_.Get()),
709 current);
710 break;
711
712 case kExtraNativesStringResource:
713 current = CopyInNativesSource(
714 ExtraNatives::GetScriptSource(source_.Get()), current);
715 break;
716
717 // Deserialize raw data of variable length.
718 case kVariableRawData: {
719 int size_in_bytes = source_.GetInt();
720 byte* raw_data_out = reinterpret_cast<byte*>(current);
721 source_.CopyRaw(raw_data_out, size_in_bytes);
722 break;
723 }
724
725 case kVariableRepeat: {
726 int repeats = source_.GetInt();
727 Object* object = current[-1];
728 DCHECK(!isolate->heap()->InNewSpace(object));
729 for (int i = 0; i < repeats; i++) UnalignedCopy(current++, &object);
730 break;
731 }
732
733 case kAlignmentPrefix:
734 case kAlignmentPrefix + 1:
735 case kAlignmentPrefix + 2:
736 SetAlignment(data);
737 break;
738
739 STATIC_ASSERT(kNumberOfRootArrayConstants == Heap::kOldSpaceRoots);
740 STATIC_ASSERT(kNumberOfRootArrayConstants == 32);
741 SIXTEEN_CASES(kRootArrayConstantsWithSkip)
742 SIXTEEN_CASES(kRootArrayConstantsWithSkip + 16) {
743 int skip = source_.GetInt();
744 current = reinterpret_cast<Object**>(
745 reinterpret_cast<intptr_t>(current) + skip);
746 // Fall through.
747 }
748
749 SIXTEEN_CASES(kRootArrayConstants)
750 SIXTEEN_CASES(kRootArrayConstants + 16) {
751 int id = data & kRootArrayConstantsMask;
752 Heap::RootListIndex root_index = static_cast<Heap::RootListIndex>(id);
753 Object* object = isolate->heap()->root(root_index);
754 DCHECK(!isolate->heap()->InNewSpace(object));
755 UnalignedCopy(current++, &object);
756 break;
757 }
758
759 STATIC_ASSERT(kNumberOfHotObjects == 8);
760 FOUR_CASES(kHotObjectWithSkip)
761 FOUR_CASES(kHotObjectWithSkip + 4) {
762 int skip = source_.GetInt();
763 current = reinterpret_cast<Object**>(
764 reinterpret_cast<Address>(current) + skip);
765 // Fall through.
766 }
767
768 FOUR_CASES(kHotObject)
769 FOUR_CASES(kHotObject + 4) {
770 int index = data & kHotObjectMask;
771 Object* hot_object = hot_objects_.Get(index);
772 UnalignedCopy(current, &hot_object);
773 if (write_barrier_needed) {
774 Address current_address = reinterpret_cast<Address>(current);
775 SLOW_DCHECK(isolate->heap()->ContainsSlow(current_object_address));
776 isolate->heap()->RecordWrite(
777 HeapObject::FromAddress(current_object_address),
778 static_cast<int>(current_address - current_object_address),
779 hot_object);
780 }
781 current++;
782 break;
783 }
784
785 // Deserialize raw data of fixed length from 1 to 32 words.
786 STATIC_ASSERT(kNumberOfFixedRawData == 32);
787 SIXTEEN_CASES(kFixedRawData)
788 SIXTEEN_CASES(kFixedRawData + 16) {
789 byte* raw_data_out = reinterpret_cast<byte*>(current);
790 int size_in_bytes = (data - kFixedRawDataStart) << kPointerSizeLog2;
791 source_.CopyRaw(raw_data_out, size_in_bytes);
792 current = reinterpret_cast<Object**>(raw_data_out + size_in_bytes);
793 break;
794 }
795
796 STATIC_ASSERT(kNumberOfFixedRepeat == 16);
797 SIXTEEN_CASES(kFixedRepeat) {
798 int repeats = data - kFixedRepeatStart;
799 Object* object;
800 UnalignedCopy(&object, current - 1);
801 DCHECK(!isolate->heap()->InNewSpace(object));
802 for (int i = 0; i < repeats; i++) UnalignedCopy(current++, &object);
803 break;
804 }
805
806#undef SIXTEEN_CASES
807#undef FOUR_CASES
808#undef SINGLE_CASE
809
810 default:
811 CHECK(false);
812 }
813 }
814 CHECK_EQ(limit, current);
815 return true;
816}
817} // namespace internal
818} // namespace v8