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