1 /* 2 * Copyright (c) 2019, 2024, 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/archiveBuilder.hpp" 27 #include "cds/archiveHeapWriter.hpp" 28 #include "cds/archiveUtils.inline.hpp" 29 #include "cds/cds_globals.hpp" 30 #include "cds/cdsConfig.hpp" 31 #include "cds/classPrelinker.hpp" 32 #include "cds/dynamicArchive.hpp" 33 #include "cds/regeneratedClasses.hpp" 34 #include "classfile/classLoader.hpp" 35 #include "classfile/classLoaderData.inline.hpp" 36 #include "classfile/symbolTable.hpp" 37 #include "classfile/systemDictionaryShared.hpp" 38 #include "classfile/vmSymbols.hpp" 39 #include "gc/shared/collectedHeap.hpp" 40 #include "gc/shared/gcVMOperations.hpp" 41 #include "gc/shared/gc_globals.hpp" 42 #include "jvm.h" 43 #include "logging/log.hpp" 44 #include "memory/metaspaceClosure.hpp" 45 #include "memory/resourceArea.hpp" 46 #include "oops/klass.inline.hpp" 47 #include "runtime/arguments.hpp" 48 #include "runtime/os.hpp" 49 #include "runtime/sharedRuntime.hpp" 50 #include "runtime/vmThread.hpp" 51 #include "runtime/vmOperations.hpp" 52 #include "utilities/align.hpp" 53 #include "utilities/bitMap.inline.hpp" 54 55 56 class DynamicArchiveBuilder : public ArchiveBuilder { 57 const char* _archive_name; 58 public: 59 DynamicArchiveBuilder(const char* archive_name) : _archive_name(archive_name) {} 60 void mark_pointer(address* ptr_loc) { 61 ArchivePtrMarker::mark_pointer(ptr_loc); 62 } 63 64 static int dynamic_dump_method_comparator(Method* a, Method* b) { 65 Symbol* a_name = a->name(); 66 Symbol* b_name = b->name(); 67 68 if (a_name == b_name) { 69 return 0; 70 } 71 72 u4 a_offset = ArchiveBuilder::current()->any_to_offset_u4(a_name); 73 u4 b_offset = ArchiveBuilder::current()->any_to_offset_u4(b_name); 74 75 if (a_offset < b_offset) { 76 return -1; 77 } else { 78 assert(a_offset > b_offset, "must be"); 79 return 1; 80 } 81 } 82 83 public: 84 DynamicArchiveHeader *_header; 85 86 void init_header(); 87 void release_header(); 88 void post_dump(); 89 void sort_methods(); 90 void sort_methods(InstanceKlass* ik) const; 91 void remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const; 92 void write_archive(char* serialized_data); 93 void gather_array_klasses(); 94 95 public: 96 DynamicArchiveBuilder() : ArchiveBuilder() { } 97 98 // Do this before and after the archive dump to see if any corruption 99 // is caused by dynamic dumping. 100 void verify_universe(const char* info) { 101 if (VerifyBeforeExit) { 102 log_info(cds)("Verify %s", info); 103 // Among other things, this ensures that Eden top is correct. 104 Universe::heap()->prepare_for_verify(); 105 Universe::verify(info); 106 } 107 } 108 109 void doit() { 110 verify_universe("Before CDS dynamic dump"); 111 DEBUG_ONLY(SystemDictionaryShared::NoClassLoadingMark nclm); 112 113 // Block concurrent class unloading from changing the _dumptime_table 114 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 115 SystemDictionaryShared::check_excluded_classes(); 116 117 if (SystemDictionaryShared::is_dumptime_table_empty()) { 118 log_warning(cds, dynamic)("There is no class to be included in the dynamic archive."); 119 return; 120 } 121 122 log_info(cds,dynamic)("CDS dynamic dump: clinit = " JLONG_FORMAT "ms)", 123 ClassLoader::class_init_time_ms()); 124 125 init_header(); 126 gather_source_objs(); 127 gather_array_klasses(); 128 reserve_buffer(); 129 130 log_info(cds, dynamic)("Copying %d klasses and %d symbols", 131 klasses()->length(), symbols()->length()); 132 dump_rw_metadata(); 133 dump_ro_metadata(); 134 relocate_metaspaceobj_embedded_pointers(); 135 136 verify_estimate_size(_estimated_metaspaceobj_bytes, "MetaspaceObjs"); 137 138 char* serialized_data; 139 { 140 // Write the symbol table and system dictionaries to the RO space. 141 // Note that these tables still point to the *original* objects, so 142 // they would need to call DynamicArchive::original_to_target() to 143 // get the correct addresses. 144 assert(current_dump_region() == ro_region(), "Must be RO space"); 145 SymbolTable::write_to_archive(symbols()); 146 147 ArchiveBuilder::OtherROAllocMark mark; 148 SystemDictionaryShared::write_to_archive(false); 149 DynamicArchive::dump_array_klasses(); 150 151 serialized_data = ro_region()->top(); 152 WriteClosure wc(ro_region()); 153 ArchiveBuilder::serialize_dynamic_archivable_items(&wc); 154 } 155 156 verify_estimate_size(_estimated_hashtable_bytes, "Hashtables"); 157 158 sort_methods(); 159 160 log_info(cds)("Make classes shareable"); 161 make_klasses_shareable(); 162 163 log_info(cds)("Adjust lambda proxy class dictionary"); 164 SystemDictionaryShared::adjust_lambda_proxy_class_dictionary(); 165 166 relocate_to_requested(); 167 168 write_archive(serialized_data); 169 release_header(); 170 DynamicArchive::post_dump(); 171 172 post_dump(); 173 174 assert(_num_dump_regions_used == _total_dump_regions, "must be"); 175 verify_universe("After CDS dynamic dump"); 176 } 177 178 virtual void iterate_roots(MetaspaceClosure* it) { 179 FileMapInfo::metaspace_pointers_do(it); 180 SystemDictionaryShared::dumptime_classes_do(it); 181 iterate_primitive_array_klasses(it); 182 } 183 184 void iterate_primitive_array_klasses(MetaspaceClosure* it) { 185 for (int i = T_BOOLEAN; i <= T_LONG; i++) { 186 assert(is_java_primitive((BasicType)i), "sanity"); 187 Klass* k = Universe::typeArrayKlass((BasicType)i); // this give you "[I", etc 188 assert(MetaspaceShared::is_shared_static((void*)k), 189 "one-dimensional primitive array should be in static archive"); 190 ArrayKlass* ak = ArrayKlass::cast(k); 191 while (ak != nullptr && ak->is_shared()) { 192 Klass* next_k = ak->array_klass_or_null(); 193 if (next_k != nullptr) { 194 ak = ArrayKlass::cast(next_k); 195 } else { 196 ak = nullptr; 197 } 198 } 199 if (ak != nullptr) { 200 assert(ak->dimension() > 1, "sanity"); 201 // this is the lowest dimension that's not in the static archive 202 it->push(&ak); 203 } 204 } 205 } 206 }; 207 208 void DynamicArchiveBuilder::init_header() { 209 FileMapInfo* mapinfo = new FileMapInfo(_archive_name, false); 210 assert(FileMapInfo::dynamic_info() == mapinfo, "must be"); 211 FileMapInfo* base_info = FileMapInfo::current_info(); 212 // header only be available after populate_header 213 mapinfo->populate_header(base_info->core_region_alignment()); 214 _header = mapinfo->dynamic_header(); 215 216 _header->set_base_header_crc(base_info->crc()); 217 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 218 _header->set_base_region_crc(i, base_info->region_crc(i)); 219 } 220 } 221 222 void DynamicArchiveBuilder::release_header() { 223 // We temporarily allocated a dynamic FileMapInfo for dumping, which makes it appear we 224 // have mapped a dynamic archive, but we actually have not. We are in a safepoint now. 225 // Let's free it so that if class loading happens after we leave the safepoint, nothing 226 // bad will happen. 227 assert(SafepointSynchronize::is_at_safepoint(), "must be"); 228 FileMapInfo *mapinfo = FileMapInfo::dynamic_info(); 229 assert(mapinfo != nullptr && _header == mapinfo->dynamic_header(), "must be"); 230 delete mapinfo; 231 assert(!DynamicArchive::is_mapped(), "must be"); 232 _header = nullptr; 233 } 234 235 void DynamicArchiveBuilder::post_dump() { 236 ArchivePtrMarker::reset_map_and_vs(); 237 ClassPrelinker::dispose(); 238 } 239 240 void DynamicArchiveBuilder::sort_methods() { 241 InstanceKlass::disable_method_binary_search(); 242 for (int i = 0; i < klasses()->length(); i++) { 243 Klass* k = get_buffered_addr(klasses()->at(i)); 244 if (k->is_instance_klass()) { 245 sort_methods(InstanceKlass::cast(k)); 246 } 247 } 248 } 249 250 // The address order of the copied Symbols may be different than when the original 251 // klasses were created. Re-sort all the tables. See Method::sort_methods(). 252 void DynamicArchiveBuilder::sort_methods(InstanceKlass* ik) const { 253 assert(ik != nullptr, "DynamicArchiveBuilder currently doesn't support dumping the base archive"); 254 if (MetaspaceShared::is_in_shared_metaspace(ik)) { 255 // We have reached a supertype that's already in the base archive 256 return; 257 } 258 assert(is_in_buffer_space(ik), "method sorting must be done on buffered class, not original class"); 259 if (ik->java_mirror() == nullptr) { 260 // null mirror means this class has already been visited and methods are already sorted 261 return; 262 } 263 ik->remove_java_mirror(); 264 265 if (log_is_enabled(Debug, cds, dynamic)) { 266 ResourceMark rm; 267 log_debug(cds, dynamic)("sorting methods for " PTR_FORMAT " (" PTR_FORMAT ") %s", 268 p2i(ik), p2i(to_requested(ik)), ik->external_name()); 269 } 270 271 // Method sorting may re-layout the [iv]tables, which would change the offset(s) 272 // of the locations in an InstanceKlass that would contain pointers. Let's clear 273 // all the existing pointer marking bits, and re-mark the pointers after sorting. 274 remark_pointers_for_instance_klass(ik, false); 275 276 // Make sure all supertypes have been sorted 277 sort_methods(ik->java_super()); 278 Array<InstanceKlass*>* interfaces = ik->local_interfaces(); 279 int len = interfaces->length(); 280 for (int i = 0; i < len; i++) { 281 sort_methods(interfaces->at(i)); 282 } 283 284 #ifdef ASSERT 285 if (ik->methods() != nullptr) { 286 for (int m = 0; m < ik->methods()->length(); m++) { 287 Symbol* name = ik->methods()->at(m)->name(); 288 assert(MetaspaceShared::is_in_shared_metaspace(name) || is_in_buffer_space(name), "must be"); 289 } 290 } 291 if (ik->default_methods() != nullptr) { 292 for (int m = 0; m < ik->default_methods()->length(); m++) { 293 Symbol* name = ik->default_methods()->at(m)->name(); 294 assert(MetaspaceShared::is_in_shared_metaspace(name) || is_in_buffer_space(name), "must be"); 295 } 296 } 297 #endif 298 299 Method::sort_methods(ik->methods(), /*set_idnums=*/true, dynamic_dump_method_comparator); 300 if (ik->default_methods() != nullptr) { 301 Method::sort_methods(ik->default_methods(), /*set_idnums=*/false, dynamic_dump_method_comparator); 302 } 303 if (ik->is_linked()) { 304 // If the class has already been linked, we must relayout the i/v tables, whose order depends 305 // on the method sorting order. 306 // If the class is unlinked, we cannot layout the i/v tables yet. This is OK, as the 307 // i/v tables will be initialized at runtime after bytecode verification. 308 ik->vtable().initialize_vtable(); 309 ik->itable().initialize_itable(); 310 } 311 312 // Set all the pointer marking bits after sorting. 313 remark_pointers_for_instance_klass(ik, true); 314 } 315 316 template<bool should_mark> 317 class PointerRemarker: public MetaspaceClosure { 318 public: 319 virtual bool do_ref(Ref* ref, bool read_only) { 320 if (should_mark) { 321 ArchivePtrMarker::mark_pointer(ref->addr()); 322 } else { 323 ArchivePtrMarker::clear_pointer(ref->addr()); 324 } 325 return false; // don't recurse 326 } 327 }; 328 329 void DynamicArchiveBuilder::remark_pointers_for_instance_klass(InstanceKlass* k, bool should_mark) const { 330 if (should_mark) { 331 PointerRemarker<true> marker; 332 k->metaspace_pointers_do(&marker); 333 marker.finish(); 334 } else { 335 PointerRemarker<false> marker; 336 k->metaspace_pointers_do(&marker); 337 marker.finish(); 338 } 339 } 340 341 void DynamicArchiveBuilder::write_archive(char* serialized_data) { 342 _header->set_shared_path_table(FileMapInfo::shared_path_table().table()); 343 _header->set_serialized_data(serialized_data); 344 345 FileMapInfo* dynamic_info = FileMapInfo::dynamic_info(); 346 assert(dynamic_info != nullptr, "Sanity"); 347 348 dynamic_info->open_for_write(); 349 ArchiveHeapInfo no_heap_for_dynamic_dump; 350 ArchiveBuilder::write_archive(dynamic_info, &no_heap_for_dynamic_dump); 351 352 address base = _requested_dynamic_archive_bottom; 353 address top = _requested_dynamic_archive_top; 354 size_t file_size = pointer_delta(top, base, sizeof(char)); 355 356 log_info(cds, dynamic)("Written dynamic archive " PTR_FORMAT " - " PTR_FORMAT 357 " [" UINT32_FORMAT " bytes header, " SIZE_FORMAT " bytes total]", 358 p2i(base), p2i(top), _header->header_size(), file_size); 359 360 log_info(cds, dynamic)("%d klasses; %d symbols", klasses()->length(), symbols()->length()); 361 } 362 363 void DynamicArchiveBuilder::gather_array_klasses() { 364 for (int i = 0; i < klasses()->length(); i++) { 365 if (klasses()->at(i)->is_objArray_klass()) { 366 ObjArrayKlass* oak = ObjArrayKlass::cast(klasses()->at(i)); 367 Klass* elem = oak->element_klass(); 368 if (MetaspaceShared::is_shared_static(elem)) { 369 // Only capture the array klass whose element_klass is in the static archive. 370 // During run time, setup (see DynamicArchive::setup_array_klasses()) is needed 371 // so that the element_klass can find its array klasses from the dynamic archive. 372 DynamicArchive::append_array_klass(oak); 373 } else { 374 // The element_klass and its array klasses are in the same archive. 375 assert(!MetaspaceShared::is_shared_static(oak), 376 "we should not gather klasses that are already in the static archive"); 377 } 378 } 379 } 380 log_debug(cds)("Total array klasses gathered for dynamic archive: %d", DynamicArchive::num_array_klasses()); 381 } 382 383 class VM_PopulateDynamicDumpSharedSpace: public VM_GC_Sync_Operation { 384 DynamicArchiveBuilder _builder; 385 public: 386 VM_PopulateDynamicDumpSharedSpace(const char* archive_name) 387 : VM_GC_Sync_Operation(), _builder(archive_name) {} 388 VMOp_Type type() const { return VMOp_PopulateDumpSharedSpace; } 389 void doit() { 390 ResourceMark rm; 391 if (AllowArchivingWithJavaAgent) { 392 log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used " 393 "for testing purposes only and should not be used in a production environment"); 394 } 395 FileMapInfo::check_nonempty_dir_in_shared_path_table(); 396 397 _builder.doit(); 398 } 399 ~VM_PopulateDynamicDumpSharedSpace() { 400 RegeneratedClasses::cleanup(); 401 } 402 }; 403 404 // _array_klasses and _dynamic_archive_array_klasses only hold the array klasses 405 // which have element klass in the static archive. 406 GrowableArray<ObjArrayKlass*>* DynamicArchive::_array_klasses = nullptr; 407 Array<ObjArrayKlass*>* DynamicArchive::_dynamic_archive_array_klasses = nullptr; 408 409 void DynamicArchive::append_array_klass(ObjArrayKlass* ak) { 410 if (_array_klasses == nullptr) { 411 _array_klasses = new (mtClassShared) GrowableArray<ObjArrayKlass*>(50, mtClassShared); 412 } 413 _array_klasses->append(ak); 414 } 415 416 void DynamicArchive::dump_array_klasses() { 417 assert(CDSConfig::is_dumping_dynamic_archive(), "sanity"); 418 if (_array_klasses != nullptr) { 419 ArchiveBuilder* builder = ArchiveBuilder::current(); 420 int num_array_klasses = _array_klasses->length(); 421 _dynamic_archive_array_klasses = 422 ArchiveBuilder::new_ro_array<ObjArrayKlass*>(num_array_klasses); 423 for (int i = 0; i < num_array_klasses; i++) { 424 builder->write_pointer_in_buffer(_dynamic_archive_array_klasses->adr_at(i), _array_klasses->at(i)); 425 } 426 } 427 } 428 429 void DynamicArchive::setup_array_klasses() { 430 if (_dynamic_archive_array_klasses != nullptr) { 431 for (int i = 0; i < _dynamic_archive_array_klasses->length(); i++) { 432 ObjArrayKlass* oak = _dynamic_archive_array_klasses->at(i); 433 assert(!oak->is_typeArray_klass(), "all type array classes must be in static archive"); 434 435 Klass* elm = oak->element_klass(); 436 assert(MetaspaceShared::is_shared_static((void*)elm), "must be"); 437 438 if (elm->is_instance_klass()) { 439 assert(InstanceKlass::cast(elm)->array_klasses() == nullptr, "must be"); 440 InstanceKlass::cast(elm)->set_array_klasses(oak); 441 } else { 442 assert(elm->is_array_klass(), "sanity"); 443 assert(ArrayKlass::cast(elm)->higher_dimension() == nullptr, "must be"); 444 ArrayKlass::cast(elm)->set_higher_dimension(oak); 445 } 446 } 447 log_debug(cds)("Total array klasses read from dynamic archive: %d", _dynamic_archive_array_klasses->length()); 448 } 449 } 450 451 void DynamicArchive::serialize_array_klasses(SerializeClosure* soc) { 452 soc->do_ptr(&_dynamic_archive_array_klasses); 453 } 454 455 void DynamicArchive::make_array_klasses_shareable() { 456 if (_array_klasses != nullptr) { 457 int num_array_klasses = _array_klasses->length(); 458 for (int i = 0; i < num_array_klasses; i++) { 459 ObjArrayKlass* k = ArchiveBuilder::current()->get_buffered_addr(_array_klasses->at(i)); 460 k->remove_unshareable_info(); 461 } 462 } 463 } 464 465 void DynamicArchive::post_dump() { 466 if (_array_klasses != nullptr) { 467 delete _array_klasses; 468 _array_klasses = nullptr; 469 } 470 } 471 472 int DynamicArchive::num_array_klasses() { 473 return _array_klasses != nullptr ? _array_klasses->length() : 0; 474 } 475 476 void DynamicArchive::check_for_dynamic_dump() { 477 if (CDSConfig::is_dumping_dynamic_archive() && !CDSConfig::is_using_archive()) { 478 // This could happen if SharedArchiveFile has failed to load: 479 // - -Xshare:off was specified 480 // - SharedArchiveFile points to an non-existent file. 481 // - SharedArchiveFile points to an archive that has failed CRC check 482 // - SharedArchiveFile is not specified and the VM doesn't have a compatible default archive 483 484 #define __THEMSG " is unsupported when base CDS archive is not loaded. Run with -Xlog:cds for more info." 485 if (RecordDynamicDumpInfo) { 486 log_error(cds)("-XX:+RecordDynamicDumpInfo%s", __THEMSG); 487 MetaspaceShared::unrecoverable_loading_error(); 488 } else { 489 assert(ArchiveClassesAtExit != nullptr, "sanity"); 490 log_warning(cds)("-XX:ArchiveClassesAtExit" __THEMSG); 491 } 492 #undef __THEMSG 493 CDSConfig::disable_dumping_dynamic_archive(); 494 } 495 } 496 497 void DynamicArchive::dump_at_exit(JavaThread* current, const char* archive_name) { 498 ExceptionMark em(current); 499 ResourceMark rm(current); 500 501 if (!CDSConfig::is_dumping_dynamic_archive() || archive_name == nullptr) { 502 return; 503 } 504 505 log_info(cds, dynamic)("Preparing for dynamic dump at exit in thread %s", current->name()); 506 507 JavaThread* THREAD = current; // For TRAPS processing related to link_shared_classes 508 MetaspaceShared::link_shared_classes(false/*not from jcmd*/, THREAD); 509 if (!HAS_PENDING_EXCEPTION) { 510 // copy shared path table to saved. 511 if (!HAS_PENDING_EXCEPTION) { 512 VM_PopulateDynamicDumpSharedSpace op(archive_name); 513 VMThread::execute(&op); 514 return; 515 } 516 } 517 518 // One of the prepatory steps failed 519 oop ex = current->pending_exception(); 520 log_error(cds)("Dynamic dump has failed"); 521 log_error(cds)("%s: %s", ex->klass()->external_name(), 522 java_lang_String::as_utf8_string(java_lang_Throwable::message(ex))); 523 CLEAR_PENDING_EXCEPTION; 524 CDSConfig::disable_dumping_dynamic_archive(); // Just for good measure 525 } 526 527 // This is called by "jcmd VM.cds dynamic_dump" 528 void DynamicArchive::dump_for_jcmd(const char* archive_name, TRAPS) { 529 assert(CDSConfig::is_using_archive() && RecordDynamicDumpInfo, "already checked in arguments.cpp"); 530 assert(ArchiveClassesAtExit == nullptr, "already checked in arguments.cpp"); 531 assert(CDSConfig::is_dumping_dynamic_archive(), "already checked by check_for_dynamic_dump() during VM startup"); 532 MetaspaceShared::link_shared_classes(true/*from jcmd*/, CHECK); 533 // copy shared path table to saved. 534 VM_PopulateDynamicDumpSharedSpace op(archive_name); 535 VMThread::execute(&op); 536 } 537 538 bool DynamicArchive::validate(FileMapInfo* dynamic_info) { 539 assert(!dynamic_info->is_static(), "must be"); 540 // Check if the recorded base archive matches with the current one 541 FileMapInfo* base_info = FileMapInfo::current_info(); 542 DynamicArchiveHeader* dynamic_header = dynamic_info->dynamic_header(); 543 544 // Check the header crc 545 if (dynamic_header->base_header_crc() != base_info->crc()) { 546 log_warning(cds)("Dynamic archive cannot be used: static archive header checksum verification failed."); 547 return false; 548 } 549 550 // Check each space's crc 551 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 552 if (dynamic_header->base_region_crc(i) != base_info->region_crc(i)) { 553 log_warning(cds)("Dynamic archive cannot be used: static archive region #%d checksum verification failed.", i); 554 return false; 555 } 556 } 557 558 return true; 559 } 560 561 void DynamicArchiveHeader::print(outputStream* st) { 562 ResourceMark rm; 563 564 st->print_cr("- base_header_crc: 0x%08x", base_header_crc()); 565 for (int i = 0; i < NUM_CDS_REGIONS; i++) { 566 st->print_cr("- base_region_crc[%d]: 0x%08x", i, base_region_crc(i)); 567 } 568 }