1 /* 2 * Copyright (c) 2023, 2025, 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 "cds/aotReferenceObjSupport.hpp" 26 #include "cds/archiveHeapWriter.hpp" 27 #include "cds/cdsConfig.hpp" 28 #include "cds/filemap.hpp" 29 #include "cds/heapShared.hpp" 30 #include "classfile/javaClasses.hpp" 31 #include "classfile/modules.hpp" 32 #include "classfile/systemDictionary.hpp" 33 #include "gc/shared/collectedHeap.hpp" 34 #include "memory/iterator.inline.hpp" 35 #include "memory/oopFactory.hpp" 36 #include "memory/universe.hpp" 37 #include "oops/compressedOops.hpp" 38 #include "oops/objArrayOop.inline.hpp" 39 #include "oops/oop.inline.hpp" 40 #include "oops/oopHandle.inline.hpp" 41 #include "oops/typeArrayKlass.hpp" 42 #include "oops/typeArrayOop.hpp" 43 #include "runtime/java.hpp" 44 #include "runtime/mutexLocker.hpp" 45 #include "utilities/bitMap.inline.hpp" 46 #if INCLUDE_G1GC 47 #include "gc/g1/g1CollectedHeap.hpp" 48 #include "gc/g1/g1HeapRegion.hpp" 49 #endif 50 51 #if INCLUDE_CDS_JAVA_HEAP 52 53 GrowableArrayCHeap<u1, mtClassShared>* ArchiveHeapWriter::_buffer = nullptr; 54 55 // The following are offsets from buffer_bottom() 56 size_t ArchiveHeapWriter::_buffer_used; 57 58 // Heap root segments 59 HeapRootSegments ArchiveHeapWriter::_heap_root_segments; 60 61 address ArchiveHeapWriter::_requested_bottom; 62 address ArchiveHeapWriter::_requested_top; 63 64 GrowableArrayCHeap<ArchiveHeapWriter::NativePointerInfo, mtClassShared>* ArchiveHeapWriter::_native_pointers; 65 GrowableArrayCHeap<oop, mtClassShared>* ArchiveHeapWriter::_source_objs; 66 GrowableArrayCHeap<ArchiveHeapWriter::HeapObjOrder, mtClassShared>* ArchiveHeapWriter::_source_objs_order; 67 68 ArchiveHeapWriter::BufferOffsetToSourceObjectTable* 69 ArchiveHeapWriter::_buffer_offset_to_source_obj_table = nullptr; 70 71 72 typedef ResourceHashtable< 73 size_t, // offset of a filler from ArchiveHeapWriter::buffer_bottom() 74 size_t, // size of this filler (in bytes) 75 127, // prime number 76 AnyObj::C_HEAP, 77 mtClassShared> FillersTable; 78 static FillersTable* _fillers; 79 static int _num_native_ptrs = 0; 80 81 void ArchiveHeapWriter::init() { 82 if (CDSConfig::is_dumping_heap()) { 83 Universe::heap()->collect(GCCause::_java_lang_system_gc); 84 85 _buffer_offset_to_source_obj_table = new BufferOffsetToSourceObjectTable(/*size (prime)*/36137, /*max size*/1 * M); 86 _fillers = new FillersTable(); 87 _requested_bottom = nullptr; 88 _requested_top = nullptr; 89 90 _native_pointers = new GrowableArrayCHeap<NativePointerInfo, mtClassShared>(2048); 91 _source_objs = new GrowableArrayCHeap<oop, mtClassShared>(10000); 92 93 guarantee(MIN_GC_REGION_ALIGNMENT <= G1HeapRegion::min_region_size_in_words() * HeapWordSize, "must be"); 94 } 95 } 96 97 void ArchiveHeapWriter::add_source_obj(oop src_obj) { 98 _source_objs->append(src_obj); 99 } 100 101 void ArchiveHeapWriter::write(GrowableArrayCHeap<oop, mtClassShared>* roots, 102 ArchiveHeapInfo* heap_info) { 103 assert(CDSConfig::is_dumping_heap(), "sanity"); 104 allocate_buffer(); 105 copy_source_objs_to_buffer(roots); 106 set_requested_address(heap_info); 107 relocate_embedded_oops(roots, heap_info); 108 } 109 110 bool ArchiveHeapWriter::is_too_large_to_archive(oop o) { 111 return is_too_large_to_archive(o->size()); 112 } 113 114 bool ArchiveHeapWriter::is_string_too_large_to_archive(oop string) { 115 typeArrayOop value = java_lang_String::value_no_keepalive(string); 116 return is_too_large_to_archive(value); 117 } 118 119 bool ArchiveHeapWriter::is_too_large_to_archive(size_t size) { 120 assert(size > 0, "no zero-size object"); 121 assert(size * HeapWordSize > size, "no overflow"); 122 static_assert(MIN_GC_REGION_ALIGNMENT > 0, "must be positive"); 123 124 size_t byte_size = size * HeapWordSize; 125 if (byte_size > size_t(MIN_GC_REGION_ALIGNMENT)) { 126 return true; 127 } else { 128 return false; 129 } 130 } 131 132 // Various lookup functions between source_obj, buffered_obj and requested_obj 133 bool ArchiveHeapWriter::is_in_requested_range(oop o) { 134 assert(_requested_bottom != nullptr, "do not call before _requested_bottom is initialized"); 135 address a = cast_from_oop<address>(o); 136 return (_requested_bottom <= a && a < _requested_top); 137 } 138 139 oop ArchiveHeapWriter::requested_obj_from_buffer_offset(size_t offset) { 140 oop req_obj = cast_to_oop(_requested_bottom + offset); 141 assert(is_in_requested_range(req_obj), "must be"); 142 return req_obj; 143 } 144 145 oop ArchiveHeapWriter::source_obj_to_requested_obj(oop src_obj) { 146 assert(CDSConfig::is_dumping_heap(), "dump-time only"); 147 HeapShared::CachedOopInfo* p = HeapShared::archived_object_cache()->get(src_obj); 148 if (p != nullptr) { 149 return requested_obj_from_buffer_offset(p->buffer_offset()); 150 } else { 151 return nullptr; 152 } 153 } 154 155 oop ArchiveHeapWriter::buffered_addr_to_source_obj(address buffered_addr) { 156 oop* p = _buffer_offset_to_source_obj_table->get(buffered_address_to_offset(buffered_addr)); 157 if (p != nullptr) { 158 return *p; 159 } else { 160 return nullptr; 161 } 162 } 163 164 address ArchiveHeapWriter::buffered_addr_to_requested_addr(address buffered_addr) { 165 return _requested_bottom + buffered_address_to_offset(buffered_addr); 166 } 167 168 address ArchiveHeapWriter::requested_address() { 169 assert(_buffer != nullptr, "must be initialized"); 170 return _requested_bottom; 171 } 172 173 void ArchiveHeapWriter::allocate_buffer() { 174 int initial_buffer_size = 100000; 175 _buffer = new GrowableArrayCHeap<u1, mtClassShared>(initial_buffer_size); 176 _buffer_used = 0; 177 ensure_buffer_space(1); // so that buffer_bottom() works 178 } 179 180 void ArchiveHeapWriter::ensure_buffer_space(size_t min_bytes) { 181 // We usually have very small heaps. If we get a huge one it's probably caused by a bug. 182 guarantee(min_bytes <= max_jint, "we dont support archiving more than 2G of objects"); 183 _buffer->at_grow(to_array_index(min_bytes)); 184 } 185 186 objArrayOop ArchiveHeapWriter::allocate_root_segment(size_t offset, int element_count) { 187 HeapWord* mem = offset_to_buffered_address<HeapWord *>(offset); 188 memset(mem, 0, objArrayOopDesc::object_size(element_count)); 189 190 // The initialization code is copied from MemAllocator::finish and ObjArrayAllocator::initialize. 191 if (UseCompactObjectHeaders) { 192 oopDesc::release_set_mark(mem, Universe::objectArrayKlass()->prototype_header()); 193 } else { 194 oopDesc::set_mark(mem, markWord::prototype()); 195 oopDesc::release_set_klass(mem, Universe::objectArrayKlass()); 196 } 197 arrayOopDesc::set_length(mem, element_count); 198 return objArrayOop(cast_to_oop(mem)); 199 } 200 201 void ArchiveHeapWriter::root_segment_at_put(objArrayOop segment, int index, oop root) { 202 // Do not use arrayOop->obj_at_put(i, o) as arrayOop is outside the real heap! 203 if (UseCompressedOops) { 204 *segment->obj_at_addr<narrowOop>(index) = CompressedOops::encode(root); 205 } else { 206 *segment->obj_at_addr<oop>(index) = root; 207 } 208 } 209 210 void ArchiveHeapWriter::copy_roots_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) { 211 // Depending on the number of classes we are archiving, a single roots array may be 212 // larger than MIN_GC_REGION_ALIGNMENT. Roots are allocated first in the buffer, which 213 // allows us to chop the large array into a series of "segments". Current layout 214 // starts with zero or more segments exactly fitting MIN_GC_REGION_ALIGNMENT, and end 215 // with a single segment that may be smaller than MIN_GC_REGION_ALIGNMENT. 216 // This is simple and efficient. We do not need filler objects anywhere between the segments, 217 // or immediately after the last segment. This allows starting the object dump immediately 218 // after the roots. 219 220 assert((_buffer_used % MIN_GC_REGION_ALIGNMENT) == 0, 221 "Pre-condition: Roots start at aligned boundary: %zu", _buffer_used); 222 223 int max_elem_count = ((MIN_GC_REGION_ALIGNMENT - arrayOopDesc::header_size_in_bytes()) / heapOopSize); 224 assert(objArrayOopDesc::object_size(max_elem_count)*HeapWordSize == MIN_GC_REGION_ALIGNMENT, 225 "Should match exactly"); 226 227 HeapRootSegments segments(_buffer_used, 228 roots->length(), 229 MIN_GC_REGION_ALIGNMENT, 230 max_elem_count); 231 232 int root_index = 0; 233 for (size_t seg_idx = 0; seg_idx < segments.count(); seg_idx++) { 234 int size_elems = segments.size_in_elems(seg_idx); 235 size_t size_bytes = segments.size_in_bytes(seg_idx); 236 237 size_t oop_offset = _buffer_used; 238 _buffer_used = oop_offset + size_bytes; 239 ensure_buffer_space(_buffer_used); 240 241 assert((oop_offset % MIN_GC_REGION_ALIGNMENT) == 0, 242 "Roots segment %zu start is not aligned: %zu", 243 segments.count(), oop_offset); 244 245 objArrayOop seg_oop = allocate_root_segment(oop_offset, size_elems); 246 for (int i = 0; i < size_elems; i++) { 247 root_segment_at_put(seg_oop, i, roots->at(root_index++)); 248 } 249 250 log_info(cds, heap)("archived obj root segment [%d] = %zu bytes, obj = " PTR_FORMAT, 251 size_elems, size_bytes, p2i(seg_oop)); 252 } 253 254 assert(root_index == roots->length(), "Post-condition: All roots are handled"); 255 256 _heap_root_segments = segments; 257 } 258 259 // The goal is to sort the objects in increasing order of: 260 // - objects that have only oop pointers 261 // - objects that have both native and oop pointers 262 // - objects that have only native pointers 263 // - objects that have no pointers 264 static int oop_sorting_rank(oop o) { 265 bool has_oop_ptr, has_native_ptr; 266 HeapShared::get_pointer_info(o, has_oop_ptr, has_native_ptr); 267 268 if (has_oop_ptr) { 269 if (!has_native_ptr) { 270 return 0; 271 } else { 272 return 1; 273 } 274 } else { 275 if (has_native_ptr) { 276 return 2; 277 } else { 278 return 3; 279 } 280 } 281 } 282 283 int ArchiveHeapWriter::compare_objs_by_oop_fields(HeapObjOrder* a, HeapObjOrder* b) { 284 int rank_a = a->_rank; 285 int rank_b = b->_rank; 286 287 if (rank_a != rank_b) { 288 return rank_a - rank_b; 289 } else { 290 // If they are the same rank, sort them by their position in the _source_objs array 291 return a->_index - b->_index; 292 } 293 } 294 295 void ArchiveHeapWriter::sort_source_objs() { 296 log_info(cds)("sorting heap objects"); 297 int len = _source_objs->length(); 298 _source_objs_order = new GrowableArrayCHeap<HeapObjOrder, mtClassShared>(len); 299 300 for (int i = 0; i < len; i++) { 301 oop o = _source_objs->at(i); 302 int rank = oop_sorting_rank(o); 303 HeapObjOrder os = {i, rank}; 304 _source_objs_order->append(os); 305 } 306 log_info(cds)("computed ranks"); 307 _source_objs_order->sort(compare_objs_by_oop_fields); 308 log_info(cds)("sorting heap objects done"); 309 } 310 311 void ArchiveHeapWriter::copy_source_objs_to_buffer(GrowableArrayCHeap<oop, mtClassShared>* roots) { 312 // There could be multiple root segments, which we want to be aligned by region. 313 // Putting them ahead of objects makes sure we waste no space. 314 copy_roots_to_buffer(roots); 315 316 sort_source_objs(); 317 for (int i = 0; i < _source_objs_order->length(); i++) { 318 int src_obj_index = _source_objs_order->at(i)._index; 319 oop src_obj = _source_objs->at(src_obj_index); 320 HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(src_obj); 321 assert(info != nullptr, "must be"); 322 size_t buffer_offset = copy_one_source_obj_to_buffer(src_obj); 323 info->set_buffer_offset(buffer_offset); 324 325 _buffer_offset_to_source_obj_table->put_when_absent(buffer_offset, src_obj); 326 _buffer_offset_to_source_obj_table->maybe_grow(); 327 328 if (java_lang_Module::is_instance(src_obj)) { 329 Modules::check_archived_module_oop(src_obj); 330 } 331 } 332 333 log_info(cds)("Size of heap region = %zu bytes, %d objects, %d roots, %d native ptrs", 334 _buffer_used, _source_objs->length() + 1, roots->length(), _num_native_ptrs); 335 } 336 337 size_t ArchiveHeapWriter::filler_array_byte_size(int length) { 338 size_t byte_size = objArrayOopDesc::object_size(length) * HeapWordSize; 339 return byte_size; 340 } 341 342 int ArchiveHeapWriter::filler_array_length(size_t fill_bytes) { 343 assert(is_object_aligned(fill_bytes), "must be"); 344 size_t elemSize = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop)); 345 346 int initial_length = to_array_length(fill_bytes / elemSize); 347 for (int length = initial_length; length >= 0; length --) { 348 size_t array_byte_size = filler_array_byte_size(length); 349 if (array_byte_size == fill_bytes) { 350 return length; 351 } 352 } 353 354 ShouldNotReachHere(); 355 return -1; 356 } 357 358 HeapWord* ArchiveHeapWriter::init_filler_array_at_buffer_top(int array_length, size_t fill_bytes) { 359 assert(UseCompressedClassPointers, "Archived heap only supported for compressed klasses"); 360 Klass* oak = Universe::objectArrayKlass(); // already relocated to point to archived klass 361 HeapWord* mem = offset_to_buffered_address<HeapWord*>(_buffer_used); 362 memset(mem, 0, fill_bytes); 363 narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(oak); 364 if (UseCompactObjectHeaders) { 365 oopDesc::release_set_mark(mem, markWord::prototype().set_narrow_klass(nk)); 366 } else { 367 oopDesc::set_mark(mem, markWord::prototype()); 368 cast_to_oop(mem)->set_narrow_klass(nk); 369 } 370 arrayOopDesc::set_length(mem, array_length); 371 return mem; 372 } 373 374 void ArchiveHeapWriter::maybe_fill_gc_region_gap(size_t required_byte_size) { 375 // We fill only with arrays (so we don't need to use a single HeapWord filler if the 376 // leftover space is smaller than a zero-sized array object). Therefore, we need to 377 // make sure there's enough space of min_filler_byte_size in the current region after 378 // required_byte_size has been allocated. If not, fill the remainder of the current 379 // region. 380 size_t min_filler_byte_size = filler_array_byte_size(0); 381 size_t new_used = _buffer_used + required_byte_size + min_filler_byte_size; 382 383 const size_t cur_min_region_bottom = align_down(_buffer_used, MIN_GC_REGION_ALIGNMENT); 384 const size_t next_min_region_bottom = align_down(new_used, MIN_GC_REGION_ALIGNMENT); 385 386 if (cur_min_region_bottom != next_min_region_bottom) { 387 // Make sure that no objects span across MIN_GC_REGION_ALIGNMENT. This way 388 // we can map the region in any region-based collector. 389 assert(next_min_region_bottom > cur_min_region_bottom, "must be"); 390 assert(next_min_region_bottom - cur_min_region_bottom == MIN_GC_REGION_ALIGNMENT, 391 "no buffered object can be larger than %d bytes", MIN_GC_REGION_ALIGNMENT); 392 393 const size_t filler_end = next_min_region_bottom; 394 const size_t fill_bytes = filler_end - _buffer_used; 395 assert(fill_bytes > 0, "must be"); 396 ensure_buffer_space(filler_end); 397 398 int array_length = filler_array_length(fill_bytes); 399 log_info(cds, heap)("Inserting filler obj array of %d elements (%zu bytes total) @ buffer offset %zu", 400 array_length, fill_bytes, _buffer_used); 401 HeapWord* filler = init_filler_array_at_buffer_top(array_length, fill_bytes); 402 _buffer_used = filler_end; 403 _fillers->put(buffered_address_to_offset((address)filler), fill_bytes); 404 } 405 } 406 407 size_t ArchiveHeapWriter::get_filler_size_at(address buffered_addr) { 408 size_t* p = _fillers->get(buffered_address_to_offset(buffered_addr)); 409 if (p != nullptr) { 410 assert(*p > 0, "filler must be larger than zero bytes"); 411 return *p; 412 } else { 413 return 0; // buffered_addr is not a filler 414 } 415 } 416 417 template <typename T> 418 void update_buffered_object_field(address buffered_obj, int field_offset, T value) { 419 T* field_addr = cast_to_oop(buffered_obj)->field_addr<T>(field_offset); 420 *field_addr = value; 421 } 422 423 size_t ArchiveHeapWriter::copy_one_source_obj_to_buffer(oop src_obj) { 424 assert(!is_too_large_to_archive(src_obj), "already checked"); 425 size_t byte_size = src_obj->size() * HeapWordSize; 426 assert(byte_size > 0, "no zero-size objects"); 427 428 // For region-based collectors such as G1, the archive heap may be mapped into 429 // multiple regions. We need to make sure that we don't have an object that can possible 430 // span across two regions. 431 maybe_fill_gc_region_gap(byte_size); 432 433 size_t new_used = _buffer_used + byte_size; 434 assert(new_used > _buffer_used, "no wrap around"); 435 436 size_t cur_min_region_bottom = align_down(_buffer_used, MIN_GC_REGION_ALIGNMENT); 437 size_t next_min_region_bottom = align_down(new_used, MIN_GC_REGION_ALIGNMENT); 438 assert(cur_min_region_bottom == next_min_region_bottom, "no object should cross minimal GC region boundaries"); 439 440 ensure_buffer_space(new_used); 441 442 address from = cast_from_oop<address>(src_obj); 443 address to = offset_to_buffered_address<address>(_buffer_used); 444 assert(is_object_aligned(_buffer_used), "sanity"); 445 assert(is_object_aligned(byte_size), "sanity"); 446 memcpy(to, from, byte_size); 447 448 // These native pointers will be restored explicitly at run time. 449 if (java_lang_Module::is_instance(src_obj)) { 450 update_buffered_object_field<ModuleEntry*>(to, java_lang_Module::module_entry_offset(), nullptr); 451 } else if (java_lang_ClassLoader::is_instance(src_obj)) { 452 #ifdef ASSERT 453 // We only archive these loaders 454 if (src_obj != SystemDictionary::java_platform_loader() && 455 src_obj != SystemDictionary::java_system_loader()) { 456 assert(src_obj->klass()->name()->equals("jdk/internal/loader/ClassLoaders$BootClassLoader"), "must be"); 457 } 458 #endif 459 update_buffered_object_field<ClassLoaderData*>(to, java_lang_ClassLoader::loader_data_offset(), nullptr); 460 } 461 462 size_t buffered_obj_offset = _buffer_used; 463 _buffer_used = new_used; 464 465 return buffered_obj_offset; 466 } 467 468 void ArchiveHeapWriter::set_requested_address(ArchiveHeapInfo* info) { 469 assert(!info->is_used(), "only set once"); 470 471 size_t heap_region_byte_size = _buffer_used; 472 assert(heap_region_byte_size > 0, "must archived at least one object!"); 473 474 if (UseCompressedOops) { 475 if (UseG1GC) { 476 address heap_end = (address)G1CollectedHeap::heap()->reserved().end(); 477 log_info(cds, heap)("Heap end = %p", heap_end); 478 _requested_bottom = align_down(heap_end - heap_region_byte_size, G1HeapRegion::GrainBytes); 479 _requested_bottom = align_down(_requested_bottom, MIN_GC_REGION_ALIGNMENT); 480 assert(is_aligned(_requested_bottom, G1HeapRegion::GrainBytes), "sanity"); 481 } else { 482 _requested_bottom = align_up(CompressedOops::begin(), MIN_GC_REGION_ALIGNMENT); 483 } 484 } else { 485 // We always write the objects as if the heap started at this address. This 486 // makes the contents of the archive heap deterministic. 487 // 488 // Note that at runtime, the heap address is selected by the OS, so the archive 489 // heap will not be mapped at 0x10000000, and the contents need to be patched. 490 _requested_bottom = align_up((address)NOCOOPS_REQUESTED_BASE, MIN_GC_REGION_ALIGNMENT); 491 } 492 493 assert(is_aligned(_requested_bottom, MIN_GC_REGION_ALIGNMENT), "sanity"); 494 495 _requested_top = _requested_bottom + _buffer_used; 496 497 info->set_buffer_region(MemRegion(offset_to_buffered_address<HeapWord*>(0), 498 offset_to_buffered_address<HeapWord*>(_buffer_used))); 499 info->set_heap_root_segments(_heap_root_segments); 500 } 501 502 // Oop relocation 503 504 template <typename T> T* ArchiveHeapWriter::requested_addr_to_buffered_addr(T* p) { 505 assert(is_in_requested_range(cast_to_oop(p)), "must be"); 506 507 address addr = address(p); 508 assert(addr >= _requested_bottom, "must be"); 509 size_t offset = addr - _requested_bottom; 510 return offset_to_buffered_address<T*>(offset); 511 } 512 513 template <typename T> oop ArchiveHeapWriter::load_source_oop_from_buffer(T* buffered_addr) { 514 oop o = load_oop_from_buffer(buffered_addr); 515 assert(!in_buffer(cast_from_oop<address>(o)), "must point to source oop"); 516 return o; 517 } 518 519 template <typename T> void ArchiveHeapWriter::store_requested_oop_in_buffer(T* buffered_addr, 520 oop request_oop) { 521 assert(is_in_requested_range(request_oop), "must be"); 522 store_oop_in_buffer(buffered_addr, request_oop); 523 } 524 525 inline void ArchiveHeapWriter::store_oop_in_buffer(oop* buffered_addr, oop requested_obj) { 526 *buffered_addr = requested_obj; 527 } 528 529 inline void ArchiveHeapWriter::store_oop_in_buffer(narrowOop* buffered_addr, oop requested_obj) { 530 narrowOop val = CompressedOops::encode_not_null(requested_obj); 531 *buffered_addr = val; 532 } 533 534 oop ArchiveHeapWriter::load_oop_from_buffer(oop* buffered_addr) { 535 return *buffered_addr; 536 } 537 538 oop ArchiveHeapWriter::load_oop_from_buffer(narrowOop* buffered_addr) { 539 return CompressedOops::decode(*buffered_addr); 540 } 541 542 template <typename T> void ArchiveHeapWriter::relocate_field_in_buffer(T* field_addr_in_buffer, CHeapBitMap* oopmap) { 543 oop source_referent = load_source_oop_from_buffer<T>(field_addr_in_buffer); 544 if (source_referent != nullptr) { 545 if (java_lang_Class::is_instance(source_referent)) { 546 // When the source object points to a "real" mirror, the buffered object should point 547 // to the "scratch" mirror, which has all unarchivable fields scrubbed (to be reinstated 548 // at run time). 549 source_referent = HeapShared::scratch_java_mirror(source_referent); 550 assert(source_referent != nullptr, "must be"); 551 } 552 oop request_referent = source_obj_to_requested_obj(source_referent); 553 store_requested_oop_in_buffer<T>(field_addr_in_buffer, request_referent); 554 mark_oop_pointer<T>(field_addr_in_buffer, oopmap); 555 } 556 } 557 558 template <typename T> void ArchiveHeapWriter::mark_oop_pointer(T* buffered_addr, CHeapBitMap* oopmap) { 559 T* request_p = (T*)(buffered_addr_to_requested_addr((address)buffered_addr)); 560 address requested_region_bottom; 561 562 assert(request_p >= (T*)_requested_bottom, "sanity"); 563 assert(request_p < (T*)_requested_top, "sanity"); 564 requested_region_bottom = _requested_bottom; 565 566 // Mark the pointer in the oopmap 567 T* region_bottom = (T*)requested_region_bottom; 568 assert(request_p >= region_bottom, "must be"); 569 BitMap::idx_t idx = request_p - region_bottom; 570 assert(idx < oopmap->size(), "overflow"); 571 oopmap->set_bit(idx); 572 } 573 574 void ArchiveHeapWriter::update_header_for_requested_obj(oop requested_obj, oop src_obj, Klass* src_klass) { 575 assert(UseCompressedClassPointers, "Archived heap only supported for compressed klasses"); 576 narrowKlass nk = ArchiveBuilder::current()->get_requested_narrow_klass(src_klass); 577 address buffered_addr = requested_addr_to_buffered_addr(cast_from_oop<address>(requested_obj)); 578 579 oop fake_oop = cast_to_oop(buffered_addr); 580 if (UseCompactObjectHeaders) { 581 fake_oop->set_mark(markWord::prototype().set_narrow_klass(nk)); 582 } else { 583 fake_oop->set_narrow_klass(nk); 584 } 585 586 if (src_obj == nullptr) { 587 return; 588 } 589 // We need to retain the identity_hash, because it may have been used by some hashtables 590 // in the shared heap. 591 if (!src_obj->fast_no_hash_check()) { 592 intptr_t src_hash = src_obj->identity_hash(); 593 if (UseCompactObjectHeaders) { 594 fake_oop->set_mark(markWord::prototype().set_narrow_klass(nk).copy_set_hash(src_hash)); 595 } else { 596 fake_oop->set_mark(markWord::prototype().copy_set_hash(src_hash)); 597 } 598 assert(fake_oop->mark().is_unlocked(), "sanity"); 599 600 DEBUG_ONLY(intptr_t archived_hash = fake_oop->identity_hash()); 601 assert(src_hash == archived_hash, "Different hash codes: original " INTPTR_FORMAT ", archived " INTPTR_FORMAT, src_hash, archived_hash); 602 } 603 // Strip age bits. 604 fake_oop->set_mark(fake_oop->mark().set_age(0)); 605 } 606 607 class ArchiveHeapWriter::EmbeddedOopRelocator: public BasicOopIterateClosure { 608 oop _src_obj; 609 address _buffered_obj; 610 CHeapBitMap* _oopmap; 611 bool _is_java_lang_ref; 612 public: 613 EmbeddedOopRelocator(oop src_obj, address buffered_obj, CHeapBitMap* oopmap) : 614 _src_obj(src_obj), _buffered_obj(buffered_obj), _oopmap(oopmap) 615 { 616 _is_java_lang_ref = AOTReferenceObjSupport::check_if_ref_obj(src_obj); 617 } 618 619 void do_oop(narrowOop *p) { EmbeddedOopRelocator::do_oop_work(p); } 620 void do_oop( oop *p) { EmbeddedOopRelocator::do_oop_work(p); } 621 622 private: 623 template <class T> void do_oop_work(T *p) { 624 int field_offset = pointer_delta_as_int((char*)p, cast_from_oop<char*>(_src_obj)); 625 T* field_addr = (T*)(_buffered_obj + field_offset); 626 if (_is_java_lang_ref && AOTReferenceObjSupport::skip_field(field_offset)) { 627 // Do not copy these fields. Set them to null 628 *field_addr = (T)0x0; 629 } else { 630 ArchiveHeapWriter::relocate_field_in_buffer<T>(field_addr, _oopmap); 631 } 632 } 633 }; 634 635 static void log_bitmap_usage(const char* which, BitMap* bitmap, size_t total_bits) { 636 // The whole heap is covered by total_bits, but there are only non-zero bits within [start ... end). 637 size_t start = bitmap->find_first_set_bit(0); 638 size_t end = bitmap->size(); 639 log_info(cds)("%s = %7zu ... %7zu (%3zu%% ... %3zu%% = %3zu%%)", which, 640 start, end, 641 start * 100 / total_bits, 642 end * 100 / total_bits, 643 (end - start) * 100 / total_bits); 644 } 645 646 // Update all oop fields embedded in the buffered objects 647 void ArchiveHeapWriter::relocate_embedded_oops(GrowableArrayCHeap<oop, mtClassShared>* roots, 648 ArchiveHeapInfo* heap_info) { 649 size_t oopmap_unit = (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop)); 650 size_t heap_region_byte_size = _buffer_used; 651 heap_info->oopmap()->resize(heap_region_byte_size / oopmap_unit); 652 653 for (int i = 0; i < _source_objs_order->length(); i++) { 654 int src_obj_index = _source_objs_order->at(i)._index; 655 oop src_obj = _source_objs->at(src_obj_index); 656 HeapShared::CachedOopInfo* info = HeapShared::archived_object_cache()->get(src_obj); 657 assert(info != nullptr, "must be"); 658 oop requested_obj = requested_obj_from_buffer_offset(info->buffer_offset()); 659 update_header_for_requested_obj(requested_obj, src_obj, src_obj->klass()); 660 address buffered_obj = offset_to_buffered_address<address>(info->buffer_offset()); 661 EmbeddedOopRelocator relocator(src_obj, buffered_obj, heap_info->oopmap()); 662 src_obj->oop_iterate(&relocator); 663 }; 664 665 // Relocate HeapShared::roots(), which is created in copy_roots_to_buffer() and 666 // doesn't have a corresponding src_obj, so we can't use EmbeddedOopRelocator on it. 667 for (size_t seg_idx = 0; seg_idx < _heap_root_segments.count(); seg_idx++) { 668 size_t seg_offset = _heap_root_segments.segment_offset(seg_idx); 669 670 objArrayOop requested_obj = (objArrayOop)requested_obj_from_buffer_offset(seg_offset); 671 update_header_for_requested_obj(requested_obj, nullptr, Universe::objectArrayKlass()); 672 address buffered_obj = offset_to_buffered_address<address>(seg_offset); 673 int length = _heap_root_segments.size_in_elems(seg_idx); 674 675 if (UseCompressedOops) { 676 for (int i = 0; i < length; i++) { 677 narrowOop* addr = (narrowOop*)(buffered_obj + objArrayOopDesc::obj_at_offset<narrowOop>(i)); 678 relocate_field_in_buffer<narrowOop>(addr, heap_info->oopmap()); 679 } 680 } else { 681 for (int i = 0; i < length; i++) { 682 oop* addr = (oop*)(buffered_obj + objArrayOopDesc::obj_at_offset<oop>(i)); 683 relocate_field_in_buffer<oop>(addr, heap_info->oopmap()); 684 } 685 } 686 } 687 688 compute_ptrmap(heap_info); 689 690 size_t total_bytes = (size_t)_buffer->length(); 691 log_bitmap_usage("oopmap", heap_info->oopmap(), total_bytes / (UseCompressedOops ? sizeof(narrowOop) : sizeof(oop))); 692 log_bitmap_usage("ptrmap", heap_info->ptrmap(), total_bytes / sizeof(address)); 693 } 694 695 void ArchiveHeapWriter::mark_native_pointer(oop src_obj, int field_offset) { 696 Metadata* ptr = src_obj->metadata_field_acquire(field_offset); 697 if (ptr != nullptr) { 698 NativePointerInfo info; 699 info._src_obj = src_obj; 700 info._field_offset = field_offset; 701 _native_pointers->append(info); 702 HeapShared::set_has_native_pointers(src_obj); 703 _num_native_ptrs ++; 704 } 705 } 706 707 // Do we have a jlong/jint field that's actually a pointer to a MetaspaceObj? 708 bool ArchiveHeapWriter::is_marked_as_native_pointer(ArchiveHeapInfo* heap_info, oop src_obj, int field_offset) { 709 HeapShared::CachedOopInfo* p = HeapShared::archived_object_cache()->get(src_obj); 710 assert(p != nullptr, "must be"); 711 712 // requested_field_addr = the address of this field in the requested space 713 oop requested_obj = requested_obj_from_buffer_offset(p->buffer_offset()); 714 Metadata** requested_field_addr = (Metadata**)(cast_from_oop<address>(requested_obj) + field_offset); 715 assert((Metadata**)_requested_bottom <= requested_field_addr && requested_field_addr < (Metadata**) _requested_top, "range check"); 716 717 BitMap::idx_t idx = requested_field_addr - (Metadata**) _requested_bottom; 718 // Leading zeros have been removed so some addresses may not be in the ptrmap 719 size_t start_pos = FileMapInfo::current_info()->heap_ptrmap_start_pos(); 720 if (idx < start_pos) { 721 return false; 722 } else { 723 idx -= start_pos; 724 } 725 return (idx < heap_info->ptrmap()->size()) && (heap_info->ptrmap()->at(idx) == true); 726 } 727 728 void ArchiveHeapWriter::compute_ptrmap(ArchiveHeapInfo* heap_info) { 729 int num_non_null_ptrs = 0; 730 Metadata** bottom = (Metadata**) _requested_bottom; 731 Metadata** top = (Metadata**) _requested_top; // exclusive 732 heap_info->ptrmap()->resize(top - bottom); 733 734 BitMap::idx_t max_idx = 32; // paranoid - don't make it too small 735 for (int i = 0; i < _native_pointers->length(); i++) { 736 NativePointerInfo info = _native_pointers->at(i); 737 oop src_obj = info._src_obj; 738 int field_offset = info._field_offset; 739 HeapShared::CachedOopInfo* p = HeapShared::archived_object_cache()->get(src_obj); 740 // requested_field_addr = the address of this field in the requested space 741 oop requested_obj = requested_obj_from_buffer_offset(p->buffer_offset()); 742 Metadata** requested_field_addr = (Metadata**)(cast_from_oop<address>(requested_obj) + field_offset); 743 assert(bottom <= requested_field_addr && requested_field_addr < top, "range check"); 744 745 // Mark this field in the bitmap 746 BitMap::idx_t idx = requested_field_addr - bottom; 747 heap_info->ptrmap()->set_bit(idx); 748 num_non_null_ptrs ++; 749 max_idx = MAX2(max_idx, idx); 750 751 // Set the native pointer to the requested address of the metadata (at runtime, the metadata will have 752 // this address if the RO/RW regions are mapped at the default location). 753 754 Metadata** buffered_field_addr = requested_addr_to_buffered_addr(requested_field_addr); 755 Metadata* native_ptr = *buffered_field_addr; 756 guarantee(native_ptr != nullptr, "sanity"); 757 guarantee(ArchiveBuilder::current()->has_been_buffered((address)native_ptr), 758 "Metadata %p should have been archived", native_ptr); 759 760 address buffered_native_ptr = ArchiveBuilder::current()->get_buffered_addr((address)native_ptr); 761 address requested_native_ptr = ArchiveBuilder::current()->to_requested(buffered_native_ptr); 762 *buffered_field_addr = (Metadata*)requested_native_ptr; 763 } 764 765 heap_info->ptrmap()->resize(max_idx + 1); 766 log_info(cds, heap)("calculate_ptrmap: marked %d non-null native pointers for heap region (%zu bits)", 767 num_non_null_ptrs, size_t(heap_info->ptrmap()->size())); 768 } 769 770 #endif // INCLUDE_CDS_JAVA_HEAP