1 /*
  2  * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "cds/archiveHeapWriter.hpp"
 27 #include "cds/filemap.hpp"
 28 #include "cds/heapShared.hpp"
 29 #include "gc/shared/collectedHeap.hpp"
 30 #include "memory/iterator.inline.hpp"
 31 #include "memory/oopFactory.hpp"
 32 #include "memory/universe.hpp"
 33 #include "oops/compressedOops.hpp"
 34 #include "oops/oop.inline.hpp"
 35 #include "oops/objArrayOop.inline.hpp"
 36 #include "oops/oopHandle.inline.hpp"
 37 #include "oops/typeArrayKlass.hpp"
 38 #include "oops/typeArrayOop.hpp"
 39 #include "runtime/java.hpp"
 40 #include "runtime/mutexLocker.hpp"
 41 #include "utilities/bitMap.inline.hpp"
 42 
 43 #if INCLUDE_G1GC
 44 #include "gc/g1/g1CollectedHeap.hpp"
 45 #include "gc/g1/heapRegion.hpp"
 46 #endif
 47 
 48 #if INCLUDE_CDS_JAVA_HEAP
 49 
 50 GrowableArrayCHeap<u1, mtClassShared>* ArchiveHeapWriter::_buffer;
 51 
 52 // The following are offsets from buffer_bottom()
 53 size_t ArchiveHeapWriter::_buffer_used;
 54 size_t ArchiveHeapWriter::_heap_roots_bottom_offset;
 55 
 56 size_t ArchiveHeapWriter::_heap_roots_word_size;
 57 
 58 address ArchiveHeapWriter::_requested_bottom;
 59 address ArchiveHeapWriter::_requested_top;
 60 
 61 GrowableArrayCHeap<ArchiveHeapWriter::NativePointerInfo, mtClassShared>* ArchiveHeapWriter::_native_pointers;
 62 GrowableArrayCHeap<oop, mtClassShared>* ArchiveHeapWriter::_source_objs;
 63 
 64 ArchiveHeapWriter::BufferOffsetToSourceObjectTable*
 65   ArchiveHeapWriter::_buffer_offset_to_source_obj_table = nullptr;
 66 
 67 void ArchiveHeapWriter::init() {
 68   if (HeapShared::can_write()) {
 69     Universe::heap()->collect(GCCause::_java_lang_system_gc);
 70 
 71     _buffer_offset_to_source_obj_table = new BufferOffsetToSourceObjectTable();
 72 
 73     _requested_bottom = nullptr;
 74     _requested_top = nullptr;
 75 
 76     _native_pointers = new GrowableArrayCHeap<NativePointerInfo, mtClassShared>(2048);
 77     _source_objs = new GrowableArrayCHeap<oop, mtClassShared>(10000);
 78 
 79     guarantee(UseG1GC, "implementation limitation");
 80     guarantee(MIN_GC_REGION_ALIGNMENT <= /*G1*/HeapRegion::min_region_size_in_words() * HeapWordSize, "must be");
 81   }
 82 }
 83 
 84 void ArchiveHeapWriter::add_source_obj(oop src_obj) {
 85   _source_objs->append(src_obj);
 86 }
 87 
 88 void ArchiveHeapWriter::write(GrowableArrayCHeap<oop, mtClassShared>* roots,
 89                               ArchiveHeapInfo* heap_info) {
 90   assert(HeapShared::can_write(), "sanity");
 91   allocate_buffer();
 92   copy_source_objs_to_buffer(roots);
 93   set_requested_address(heap_info);
 94   relocate_embedded_oops(roots, heap_info);
 95 }
 96 
 97 bool ArchiveHeapWriter::is_too_large_to_archive(oop o) {
 98   return is_too_large_to_archive(o->size());
 99 }
100 
101 bool ArchiveHeapWriter::is_string_too_large_to_archive(oop string) {
102   typeArrayOop value = java_lang_String::value_no_keepalive(string);
103   return is_too_large_to_archive(value);
104 }
105 
106 bool ArchiveHeapWriter::is_too_large_to_archive(size_t size) {
107   assert(size > 0, "no zero-size object");
108   assert(size * HeapWordSize > size, "no overflow");
109   static_assert(MIN_GC_REGION_ALIGNMENT > 0, "must be positive");
110 
111   size_t byte_size = size * HeapWordSize;
112   if (byte_size > size_t(MIN_GC_REGION_ALIGNMENT)) {
113     return true;
114   } else {
115     return false;
116   }
117 }
118 
119 // Various lookup functions between source_obj, buffered_obj and requested_obj
120 bool ArchiveHeapWriter::is_in_requested_range(oop o) {
121   assert(_requested_bottom != nullptr, "do not call before _requested_bottom is initialized");
122   address a = cast_from_oop<address>(o);
123   return (_requested_bottom <= a && a < _requested_top);
124 }
125 
126 oop ArchiveHeapWriter::requested_obj_from_buffer_offset(size_t offset) {
127   oop req_obj = cast_to_oop(_requested_bottom + offset);
128   assert(is_in_requested_range(req_obj), "must be");
129   return req_obj;
130 }
131 
132 oop ArchiveHeapWriter::source_obj_to_requested_obj(oop src_obj) {
133   assert(DumpSharedSpaces, "dump-time only");
134   HeapShared::CachedOopInfo* p = HeapShared::archived_object_cache()->get(src_obj);
135   if (p != nullptr) {
136     return requested_obj_from_buffer_offset(p->buffer_offset());
137   } else {
138     return nullptr;
139   }
140 }
141 
142 oop ArchiveHeapWriter::buffered_addr_to_source_obj(address buffered_addr) {
143   oop* p = _buffer_offset_to_source_obj_table->get(buffered_address_to_offset(buffered_addr));
144   if (p != nullptr) {
145     return *p;
146   } else {
147     return nullptr;
148   }
149 }
150 
151 address ArchiveHeapWriter::buffered_addr_to_requested_addr(address buffered_addr) {
152   return _requested_bottom + buffered_address_to_offset(buffered_addr);
153 }
154 
155 oop ArchiveHeapWriter::heap_roots_requested_address() {
156   return cast_to_oop(_requested_bottom + _heap_roots_bottom_offset);
157 }
158 
159 address ArchiveHeapWriter::requested_address() {
160   assert(_buffer != nullptr, "must be initialized");
161   return _requested_bottom;
162 }
163 
164 void ArchiveHeapWriter::allocate_buffer() {
165   int initial_buffer_size = 100000;
166   _buffer = new GrowableArrayCHeap<u1, mtClassShared>(initial_buffer_size);
167   _buffer_used = 0;
168   ensure_buffer_space(1); // so that buffer_bottom() works
169 }
170 
171 void ArchiveHeapWriter::ensure_buffer_space(size_t min_bytes) {
172   // We usually have very small heaps. If we get a huge one it's probably caused by a bug.
173   guarantee(min_bytes <= max_jint, "we dont support archiving more than 2G of objects");
174   _buffer->at_grow(to_array_index(min_bytes));
175 }
176 
177 void ArchiveHeapWriter::copy_roots_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) {
178   Klass* k = Universe::objectArrayKlassObj(); // already relocated to point to archived klass
179   int length = roots->length();
180   _heap_roots_word_size = objArrayOopDesc::object_size(length);
181   size_t byte_size = _heap_roots_word_size * HeapWordSize;
182   if (byte_size >= MIN_GC_REGION_ALIGNMENT) {
183     log_error(cds, heap)("roots array is too large. Please reduce the number of classes");
184     vm_exit(1);
185   }
186 
187   maybe_fill_gc_region_gap(byte_size);
188 
189   size_t new_used = _buffer_used + byte_size;
190   ensure_buffer_space(new_used);
191 
192   HeapWord* mem = offset_to_buffered_address<HeapWord*>(_buffer_used);
193   memset(mem, 0, byte_size);
194   {
195     // This is copied from MemAllocator::finish
196     if (UseCompactObjectHeaders) {
197       narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(k);
198       oopDesc::release_set_mark(mem, markWord::prototype().set_narrow_klass(nk));
199     } else {
200       oopDesc::set_mark(mem, markWord::prototype());
201       oopDesc::release_set_klass(mem, k);
202     }
203   }
204   {
205     // This is copied from ObjArrayAllocator::initialize
206     arrayOopDesc::set_length(mem, length);
207   }
208 
209   objArrayOop arrayOop = objArrayOop(cast_to_oop(mem));
210   for (int i = 0; i < length; i++) {
211     // Do not use arrayOop->obj_at_put(i, o) as arrayOop is outside of the real heap!
212     oop o = roots->at(i);
213     if (UseCompressedOops) {
214       * arrayOop->obj_at_addr<narrowOop>(i) = CompressedOops::encode(o);
215     } else {
216       * arrayOop->obj_at_addr<oop>(i) = o;
217     }
218   }
219   log_info(cds, heap)("archived obj roots[%d] = " SIZE_FORMAT " bytes, klass = %p, obj = %p", length, byte_size, k, mem);
220 
221   _heap_roots_bottom_offset = _buffer_used;
222   _buffer_used = new_used;
223 }
224 
225 void ArchiveHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) {
226   for (int i = 0; i < _source_objs->length(); i++) {
227     oop src_obj = _source_objs->at(i);
228     HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(src_obj);
229     assert(info != nullptr, "must be");
230     size_t buffer_offset = copy_one_source_obj_to_buffer(src_obj);
231     info->set_buffer_offset(buffer_offset);
232 
233     _buffer_offset_to_source_obj_table->put(buffer_offset, src_obj);
234   }
235 
236   copy_roots_to_buffer(roots);
237 
238   log_info(cds)("Size of heap region = " SIZE_FORMAT " bytes, %d objects, %d roots",
239                 _buffer_used, _source_objs->length() + 1, roots->length());
240 }
241 
242 size_t ArchiveHeapWriter::filler_array_byte_size(int length) {
243   size_t byte_size = objArrayOopDesc::object_size(length) * HeapWordSize;
244   return byte_size;
245 }
246 
247 int ArchiveHeapWriter::filler_array_length(size_t fill_bytes) {
248   assert(is_object_aligned(fill_bytes), "must be");
249   size_t elemSize = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop));
250 
251   int initial_length = to_array_length(fill_bytes / elemSize);
252   for (int length = initial_length; length >= 0; length --) {
253     size_t array_byte_size = filler_array_byte_size(length);
254     if (array_byte_size == fill_bytes) {
255       return length;
256     }
257   }
258 
259   ShouldNotReachHere();
260   return -1;
261 }
262 
263 void ArchiveHeapWriter::init_filler_array_at_buffer_top(int array_length, size_t fill_bytes) {
264   assert(UseCompressedClassPointers, "Archived heap only supported for compressed klasses");
265   Klass* oak = Universe::objectArrayKlassObj(); // already relocated to point to archived klass
266   HeapWord* mem = offset_to_buffered_address<HeapWord*>(_buffer_used);
267   memset(mem, 0, fill_bytes);
268   narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(oak);
269   if (UseCompactObjectHeaders) {
270     oopDesc::release_set_mark(mem, markWord::prototype().set_narrow_klass(nk));
271   } else {
272     oopDesc::set_mark(mem, markWord::prototype());
273     cast_to_oop(mem)->set_narrow_klass(nk);
274   }
275   arrayOopDesc::set_length(mem, array_length);
276 }
277 
278 void ArchiveHeapWriter::maybe_fill_gc_region_gap(size_t required_byte_size) {
279   // We fill only with arrays (so we don't need to use a single HeapWord filler if the
280   // leftover space is smaller than a zero-sized array object). Therefore, we need to
281   // make sure there's enough space of min_filler_byte_size in the current region after
282   // required_byte_size has been allocated. If not, fill the remainder of the current
283   // region.
284   size_t min_filler_byte_size = filler_array_byte_size(0);
285   size_t new_used = _buffer_used + required_byte_size + min_filler_byte_size;
286 
287   const size_t cur_min_region_bottom = align_down(_buffer_used, MIN_GC_REGION_ALIGNMENT);
288   const size_t next_min_region_bottom = align_down(new_used, MIN_GC_REGION_ALIGNMENT);
289 
290   if (cur_min_region_bottom != next_min_region_bottom) {
291     // Make sure that no objects span across MIN_GC_REGION_ALIGNMENT. This way
292     // we can map the region in any region-based collector.
293     assert(next_min_region_bottom > cur_min_region_bottom, "must be");
294     assert(next_min_region_bottom - cur_min_region_bottom == MIN_GC_REGION_ALIGNMENT,
295            "no buffered object can be larger than %d bytes",  MIN_GC_REGION_ALIGNMENT);
296 
297     const size_t filler_end = next_min_region_bottom;
298     const size_t fill_bytes = filler_end - _buffer_used;
299     assert(fill_bytes > 0, "must be");
300     ensure_buffer_space(filler_end);
301 
302     int array_length = filler_array_length(fill_bytes);
303     log_info(cds, heap)("Inserting filler obj array of %d elements (" SIZE_FORMAT " bytes total) @ buffer offset " SIZE_FORMAT,
304                         array_length, fill_bytes, _buffer_used);
305     init_filler_array_at_buffer_top(array_length, fill_bytes);
306 
307     _buffer_used = filler_end;
308   }
309 }
310 
311 size_t ArchiveHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) {
312   assert(!is_too_large_to_archive(src_obj), "already checked");
313   size_t byte_size = src_obj->size() * HeapWordSize;
314   assert(byte_size > 0, "no zero-size objects");
315 
316   // For region-based collectors such as G1, the archive heap may be mapped into
317   // multiple regions. We need to make sure that we don't have an object that can possible
318   // span across two regions.
319   maybe_fill_gc_region_gap(byte_size);
320 
321   size_t new_used = _buffer_used + byte_size;
322   assert(new_used > _buffer_used, "no wrap around");
323 
324   size_t cur_min_region_bottom = align_down(_buffer_used, MIN_GC_REGION_ALIGNMENT);
325   size_t next_min_region_bottom = align_down(new_used, MIN_GC_REGION_ALIGNMENT);
326   assert(cur_min_region_bottom == next_min_region_bottom, "no object should cross minimal GC region boundaries");
327 
328   ensure_buffer_space(new_used);
329 
330   address from = cast_from_oop<address>(src_obj);
331   address to = offset_to_buffered_address<address>(_buffer_used);
332   assert(is_object_aligned(_buffer_used), "sanity");
333   assert(is_object_aligned(byte_size), "sanity");
334   memcpy(to, from, byte_size);
335 
336   size_t buffered_obj_offset = _buffer_used;
337   _buffer_used = new_used;
338 
339   return buffered_obj_offset;
340 }
341 
342 void ArchiveHeapWriter::set_requested_address(ArchiveHeapInfo* info) {
343   assert(!info->is_used(), "only set once");
344   assert(UseG1GC, "must be");
345   address heap_end = (address)G1CollectedHeap::heap()->reserved().end();
346   log_info(cds, heap)("Heap end = %p", heap_end);
347 
348   size_t heap_region_byte_size = _buffer_used;
349   assert(heap_region_byte_size > 0, "must archived at least one object!");
350 
351   _requested_bottom = align_down(heap_end - heap_region_byte_size, HeapRegion::GrainBytes);
352   assert(is_aligned(_requested_bottom, HeapRegion::GrainBytes), "sanity");
353 
354   _requested_top = _requested_bottom + _buffer_used;
355 
356   info->set_memregion(MemRegion(offset_to_buffered_address<HeapWord*>(0),
357                                 offset_to_buffered_address<HeapWord*>(_buffer_used)));
358 }
359 
360 // Oop relocation
361 
362 template <typename T> T* ArchiveHeapWriter::requested_addr_to_buffered_addr(T* p) {
363   assert(is_in_requested_range(cast_to_oop(p)), "must be");
364 
365   address addr = address(p);
366   assert(addr >= _requested_bottom, "must be");
367   size_t offset = addr - _requested_bottom;
368   return offset_to_buffered_address<T*>(offset);
369 }
370 
371 template <typename T> oop ArchiveHeapWriter::load_source_oop_from_buffer(T* buffered_addr) {
372   oop o = load_oop_from_buffer(buffered_addr);
373   assert(!in_buffer(cast_from_oop<address>(o)), "must point to source oop");
374   return o;
375 }
376 
377 template <typename T> void ArchiveHeapWriter::store_requested_oop_in_buffer(T* buffered_addr,
378                                                                             oop request_oop) {
379   assert(is_in_requested_range(request_oop), "must be");
380   store_oop_in_buffer(buffered_addr, request_oop);
381 }
382 
383 void ArchiveHeapWriter::store_oop_in_buffer(oop* buffered_addr, oop requested_obj) {
384   // Make heap content deterministic. See comments inside HeapShared::to_requested_address.
385   *buffered_addr = HeapShared::to_requested_address(requested_obj);
386 }
387 
388 void ArchiveHeapWriter::store_oop_in_buffer(narrowOop* buffered_addr, oop requested_obj) {
389   // Note: HeapShared::to_requested_address() is not necessary because
390   // the heap always starts at a deterministic address with UseCompressedOops==true.
391   narrowOop val = CompressedOops::encode_not_null(requested_obj);
392   *buffered_addr = val;
393 }
394 
395 oop ArchiveHeapWriter::load_oop_from_buffer(oop* buffered_addr) {
396   return *buffered_addr;
397 }
398 
399 oop ArchiveHeapWriter::load_oop_from_buffer(narrowOop* buffered_addr) {
400   return CompressedOops::decode(*buffered_addr);
401 }
402 
403 template <typename T> void ArchiveHeapWriter::relocate_field_in_buffer(T* field_addr_in_buffer, CHeapBitMap* oopmap) {
404   oop source_referent = load_source_oop_from_buffer<T>(field_addr_in_buffer);
405   if (!CompressedOops::is_null(source_referent)) {
406     oop request_referent = source_obj_to_requested_obj(source_referent);
407     store_requested_oop_in_buffer<T>(field_addr_in_buffer, request_referent);
408     mark_oop_pointer<T>(field_addr_in_buffer, oopmap);
409   }
410 }
411 
412 template <typename T> void ArchiveHeapWriter::mark_oop_pointer(T* buffered_addr, CHeapBitMap* oopmap) {
413   T* request_p = (T*)(buffered_addr_to_requested_addr((address)buffered_addr));
414   address requested_region_bottom;
415 
416   assert(request_p >= (T*)_requested_bottom, "sanity");
417   assert(request_p <  (T*)_requested_top, "sanity");
418   requested_region_bottom = _requested_bottom;
419 
420   // Mark the pointer in the oopmap
421   T* region_bottom = (T*)requested_region_bottom;
422   assert(request_p >= region_bottom, "must be");
423   BitMap::idx_t idx = request_p - region_bottom;
424   assert(idx < oopmap->size(), "overflow");
425   oopmap->set_bit(idx);
426 }
427 
428 void ArchiveHeapWriter::update_header_for_requested_obj(oop requested_obj, oop src_obj,  Klass* src_klass) {
429   assert(UseCompressedClassPointers, "Archived heap only supported for compressed klasses");
430   narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(src_klass);
431   address buffered_addr = requested_addr_to_buffered_addr(cast_from_oop<address>(requested_obj));
432 
433   oop fake_oop = cast_to_oop(buffered_addr);
434   if (!UseCompactObjectHeaders) {
435     fake_oop->set_narrow_klass(nk);
436   }
437 
438   // We need to retain the identity_hash, because it may have been used by some hashtables
439   // in the shared heap. This also has the side effect of pre-initializing the
440   // identity_hash for all shared objects, so they are less likely to be written
441   // into during run time, increasing the potential of memory sharing.
442   if (src_obj != nullptr) {
443     int src_hash = src_obj->identity_hash();
444     if (UseCompactObjectHeaders) {
445       fake_oop->set_mark(markWord::prototype().set_narrow_klass(nk).copy_set_hash(src_hash));
446     } else {
447       fake_oop->set_mark(markWord::prototype().copy_set_hash(src_hash));
448     }
449     assert(fake_oop->mark().is_unlocked(), "sanity");
450 
451     DEBUG_ONLY(int archived_hash = fake_oop->identity_hash());
452     assert(src_hash == archived_hash, "Different hash codes: original %x, archived %x", src_hash, archived_hash);
453   }
454 }
455 
456 // Relocate an element in the buffered copy of HeapShared::roots()
457 template <typename T> void ArchiveHeapWriter::relocate_root_at(oop requested_roots, int index, CHeapBitMap* oopmap) {
458   size_t offset = (size_t)((objArrayOop)requested_roots)->obj_at_offset<T>(index);
459   relocate_field_in_buffer<T>((T*)(buffered_heap_roots_addr() + offset), oopmap);
460 }
461 
462 class ArchiveHeapWriter::EmbeddedOopRelocator: public BasicOopIterateClosure {
463   oop _src_obj;
464   address _buffered_obj;
465   CHeapBitMap* _oopmap;
466 
467 public:
468   EmbeddedOopRelocator(oop src_obj, address buffered_obj, CHeapBitMap* oopmap) :
469     _src_obj(src_obj), _buffered_obj(buffered_obj), _oopmap(oopmap) {}
470 
471   void do_oop(narrowOop *p) { EmbeddedOopRelocator::do_oop_work(p); }
472   void do_oop(      oop *p) { EmbeddedOopRelocator::do_oop_work(p); }
473 
474 private:
475   template <class T> void do_oop_work(T *p) {
476     size_t field_offset = pointer_delta(p, _src_obj, sizeof(char));
477     ArchiveHeapWriter::relocate_field_in_buffer<T>((T*)(_buffered_obj + field_offset), _oopmap);
478   }
479 };
480 
481 // Update all oop fields embedded in the buffered objects
482 void ArchiveHeapWriter::relocate_embedded_oops(GrowableArrayCHeap<oop, mtClassShared>* roots,
483                                                ArchiveHeapInfo* heap_info) {
484   size_t oopmap_unit = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop));
485   size_t heap_region_byte_size = _buffer_used;
486   heap_info->oopmap()->resize(heap_region_byte_size   / oopmap_unit);
487 
488   auto iterator = [&] (oop src_obj, HeapShared::CachedOopInfo& info) {
489     oop requested_obj = requested_obj_from_buffer_offset(info.buffer_offset());
490     update_header_for_requested_obj(requested_obj, src_obj, src_obj->klass());
491     address buffered_obj = offset_to_buffered_address<address>(info.buffer_offset());
492     EmbeddedOopRelocator relocator(src_obj, buffered_obj, heap_info->oopmap());
493     src_obj->oop_iterate(&relocator);
494   };
495   HeapShared::archived_object_cache()->iterate_all(iterator);
496 
497   // Relocate HeapShared::roots(), which is created in copy_roots_to_buffer() and
498   // doesn't have a corresponding src_obj, so we can't use EmbeddedOopRelocator on it.
499   oop requested_roots = requested_obj_from_buffer_offset(_heap_roots_bottom_offset);
500   update_header_for_requested_obj(requested_roots, nullptr, Universe::objectArrayKlassObj());
501   int length = roots != nullptr ? roots->length() : 0;
502   for (int i = 0; i < length; i++) {
503     if (UseCompressedOops) {
504       relocate_root_at<narrowOop>(requested_roots, i, heap_info->oopmap());
505     } else {
506       relocate_root_at<oop>(requested_roots, i, heap_info->oopmap());
507     }
508   }
509 
510   compute_ptrmap(heap_info);
511 }
512 
513 void ArchiveHeapWriter::mark_native_pointer(oop src_obj, int field_offset) {
514   Metadata* ptr = src_obj->metadata_field_acquire(field_offset);
515   if (ptr != nullptr) {
516     NativePointerInfo info;
517     info._src_obj = src_obj;
518     info._field_offset = field_offset;
519     _native_pointers->append(info);
520   }
521 }
522 
523 void ArchiveHeapWriter::compute_ptrmap(ArchiveHeapInfo* heap_info) {
524   int num_non_null_ptrs = 0;
525   Metadata** bottom = (Metadata**) _requested_bottom;
526   Metadata** top = (Metadata**) _requested_top; // exclusive
527   heap_info->ptrmap()->resize(top - bottom);
528 
529   BitMap::idx_t max_idx = 32; // paranoid - don't make it too small
530   for (int i = 0; i < _native_pointers->length(); i++) {
531     NativePointerInfo info = _native_pointers->at(i);
532     oop src_obj = info._src_obj;
533     int field_offset = info._field_offset;
534     HeapShared::CachedOopInfo* p = HeapShared::archived_object_cache()->get(src_obj);
535     // requested_field_addr = the address of this field in the requested space
536     oop requested_obj = requested_obj_from_buffer_offset(p->buffer_offset());
537     Metadata** requested_field_addr = (Metadata**)(cast_from_oop<address>(requested_obj) + field_offset);
538     assert(bottom <= requested_field_addr && requested_field_addr < top, "range check");
539 
540     // Mark this field in the bitmap
541     BitMap::idx_t idx = requested_field_addr - bottom;
542     heap_info->ptrmap()->set_bit(idx);
543     num_non_null_ptrs ++;
544     max_idx = MAX2(max_idx, idx);
545 
546     // Set the native pointer to the requested address of the metadata (at runtime, the metadata will have
547     // this address if the RO/RW regions are mapped at the default location).
548 
549     Metadata** buffered_field_addr = requested_addr_to_buffered_addr(requested_field_addr);
550     Metadata* native_ptr = *buffered_field_addr;
551     assert(native_ptr != nullptr, "sanity");
552 
553     address buffered_native_ptr = ArchiveBuilder::current()->get_buffered_addr((address)native_ptr);
554     address requested_native_ptr = ArchiveBuilder::current()->to_requested(buffered_native_ptr);
555     *buffered_field_addr = (Metadata*)requested_native_ptr;
556   }
557 
558   heap_info->ptrmap()->resize(max_idx + 1);
559   log_info(cds, heap)("calculate_ptrmap: marked %d non-null native pointers for heap region (" SIZE_FORMAT " bits)",
560                       num_non_null_ptrs, size_t(heap_info->ptrmap()->size()));
561 }
562 
563 #endif // INCLUDE_CDS_JAVA_HEAP