1 /* 2 * Copyright (c) 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/aotClassInitializer.hpp" 27 #include "cds/aotClassLinker.hpp" 28 #include "cds/aotLinkedClassBulkLoader.hpp" 29 #include "cds/aotLinkedClassTable.hpp" 30 #include "cds/archiveBuilder.hpp" 31 #include "cds/archiveUtils.inline.hpp" 32 #include "cds/cdsAccess.hpp" 33 #include "cds/cdsConfig.hpp" 34 #include "cds/cdsProtectionDomain.hpp" 35 #include "cds/heapShared.hpp" 36 #include "cds/lambdaFormInvokers.inline.hpp" 37 #include "classfile/classLoaderDataGraph.hpp" 38 #include "classfile/classLoaderData.hpp" 39 #include "classfile/classLoaderExt.hpp" 40 #include "classfile/classLoader.hpp" 41 #include "classfile/dictionary.hpp" 42 #include "classfile/javaClasses.hpp" 43 #include "classfile/systemDictionary.hpp" 44 #include "classfile/systemDictionaryShared.hpp" 45 #include "classfile/vmClasses.hpp" 46 #include "compiler/compilationPolicy.hpp" 47 #include "gc/shared/gcVMOperations.hpp" 48 #include "memory/resourceArea.hpp" 49 #include "oops/constantPool.inline.hpp" 50 #include "oops/instanceKlass.hpp" 51 #include "oops/klass.inline.hpp" 52 #include "oops/trainingData.hpp" 53 #include "runtime/handles.inline.hpp" 54 #include "runtime/javaCalls.hpp" 55 #include "runtime/java.hpp" 56 #include "runtime/perfData.inline.hpp" 57 #include "runtime/timer.hpp" 58 #include "services/management.hpp" 59 60 bool AOTLinkedClassBulkLoader::_boot2_completed = false; 61 bool AOTLinkedClassBulkLoader::_platform_completed = false; 62 bool AOTLinkedClassBulkLoader::_app_completed = false; 63 bool AOTLinkedClassBulkLoader::_all_completed = false; 64 65 Array<InstanceKlass*>* AOTLinkedClassBulkLoader::_unregistered_classes_from_preimage = nullptr; 66 67 static PerfCounter* _perf_classes_preloaded = nullptr; 68 static PerfTickCounters* _perf_class_preload_counters = nullptr; 69 70 void AOTLinkedClassBulkLoader::record_unregistered_classes() { 71 if (CDSConfig::is_dumping_preimage_static_archive()) { 72 GrowableArray<InstanceKlass*> unreg_classes; 73 GrowableArray<Klass*>* klasses = ArchiveBuilder::current()->klasses(); 74 for (int i = 0; i < klasses->length(); i++) { 75 Klass* k = klasses->at(i); 76 if (k->is_instance_klass()) { 77 InstanceKlass* ik = InstanceKlass::cast(k); 78 if (ik->is_shared_unregistered_class()) { 79 unreg_classes.append((InstanceKlass*)ArchiveBuilder::get_buffered_klass(ik)); 80 } 81 } 82 } 83 _unregistered_classes_from_preimage = ArchiveUtils::archive_array(&unreg_classes); 84 } else { 85 _unregistered_classes_from_preimage = nullptr; 86 } 87 } 88 89 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc, bool is_static_archive) { 90 AOTLinkedClassTable::get(is_static_archive)->serialize(soc); 91 92 if (is_static_archive) { 93 soc->do_ptr((void**)&_unregistered_classes_from_preimage); 94 95 if (soc->reading() && UsePerfData) { 96 JavaThread* THREAD = JavaThread::current(); 97 NEWPERFEVENTCOUNTER(_perf_classes_preloaded, SUN_CLS, "preloadedClasses"); 98 NEWPERFTICKCOUNTERS(_perf_class_preload_counters, SUN_CLS, "classPreload"); 99 } 100 } 101 } 102 103 bool AOTLinkedClassBulkLoader::class_preloading_finished() { 104 if (!CDSConfig::is_using_aot_linked_classes()) { 105 return true; 106 } else { 107 // The ConstantPools of preloaded classes have references to other preloaded classes. We don't 108 // want any Java code (including JVMCI compiler) to use these classes until all of them 109 // are loaded. 110 return Atomic::load_acquire(&_all_completed); 111 } 112 } 113 114 void AOTLinkedClassBulkLoader::load_javabase_classes(JavaThread* current) { 115 assert(CDSConfig::is_using_aot_linked_classes(), "sanity"); 116 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT1, nullptr); // only java.base classes 117 } 118 119 void AOTLinkedClassBulkLoader::load_non_javabase_classes(JavaThread* current) { 120 assert(CDSConfig::is_using_aot_linked_classes(), "sanity"); 121 122 // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result, 123 // the platform/system class loader should already have been initialized as part 124 // of the FMG support. 125 if (!CDSConfig::is_dumping_final_static_archive()) { 126 assert(CDSConfig::is_using_full_module_graph(), "must be"); 127 } 128 assert(SystemDictionary::java_platform_loader() != nullptr, "must be"); 129 assert(SystemDictionary::java_system_loader() != nullptr, "must be"); 130 131 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT2, nullptr); // all boot classes outside of java.base 132 _boot2_completed = true; 133 134 load_classes_in_loader(current, AOTLinkedClassCategory::PLATFORM, SystemDictionary::java_platform_loader()); 135 _platform_completed = true; 136 137 load_classes_in_loader(current, AOTLinkedClassCategory::APP, SystemDictionary::java_system_loader()); 138 139 if (PrintTrainingInfo) { 140 tty->print_cr("==================== archived_training_data ** after all classes preloaded ===================="); 141 TrainingData::print_archived_training_data_on(tty); 142 } 143 144 if (log_is_enabled(Info, cds, jit)) { 145 CDSAccess::test_heap_access_api(); 146 } 147 148 if (CDSConfig::is_dumping_final_static_archive()) { 149 assert(_unregistered_classes_from_preimage != nullptr, "must be"); 150 for (int i = 0; i < _unregistered_classes_from_preimage->length(); i++) { 151 InstanceKlass* ik = _unregistered_classes_from_preimage->at(i); 152 SystemDictionaryShared::init_dumptime_info(ik); 153 SystemDictionaryShared::add_unregistered_class(current, ik); 154 } 155 } 156 157 _app_completed = true; 158 Atomic::release_store(&_all_completed, true); 159 } 160 161 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) { 162 load_classes_in_loader_impl(class_category, class_loader_oop, current); 163 if (current->has_pending_exception()) { 164 // We cannot continue, as we might have loaded some of the aot-linked classes, which 165 // may have dangling C++ pointers to other aot-linked classes that we have failed to load. 166 exit_on_exception(current); 167 } 168 } 169 170 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) { 171 assert(current->has_pending_exception(), "precondition"); 172 ResourceMark rm(current); 173 if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) { 174 log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = " 175 SIZE_FORMAT "M", MaxHeapSize/M); 176 } else { 177 log_error(cds)("%s: %s", current->pending_exception()->klass()->external_name(), 178 java_lang_String::as_utf8_string(java_lang_Throwable::message(current->pending_exception()))); 179 } 180 vm_exit_during_initialization("Unexpected exception when loading aot-linked classes."); 181 } 182 183 void AOTLinkedClassBulkLoader::load_classes_in_loader_impl(AOTLinkedClassCategory class_category, oop class_loader_oop, TRAPS) { 184 Handle h_loader(THREAD, class_loader_oop); 185 load_table(AOTLinkedClassTable::for_static_archive(), class_category, h_loader, CHECK); 186 load_table(AOTLinkedClassTable::for_dynamic_archive(), class_category, h_loader, CHECK); 187 188 // Initialize the InstanceKlasses of all archived heap objects that are reachable from the 189 // archived java class mirrors. 190 // 191 // Only the classes in the static archive can have archived mirrors. 192 AOTLinkedClassTable* static_table = AOTLinkedClassTable::for_static_archive(); 193 switch (class_category) { 194 case AOTLinkedClassCategory::BOOT1: 195 // Delayed until finish_loading_javabase_classes(), as the VM is not ready to 196 // execute some of the <clinit> methods. 197 break; 198 case AOTLinkedClassCategory::BOOT2: 199 init_required_classes_for_loader(h_loader, static_table->boot2(), CHECK); 200 break; 201 case AOTLinkedClassCategory::PLATFORM: 202 init_required_classes_for_loader(h_loader, static_table->platform(), CHECK); 203 break; 204 case AOTLinkedClassCategory::APP: 205 init_required_classes_for_loader(h_loader, static_table->app(), CHECK); 206 break; 207 case AOTLinkedClassCategory::UNREGISTERED: 208 ShouldNotReachHere(); 209 break; 210 } 211 212 if (Universe::is_fully_initialized() && VerifyDuringStartup) { 213 // Make sure we're still in a clean state. 214 VM_Verify verify_op; 215 VMThread::execute(&verify_op); 216 } 217 } 218 219 void AOTLinkedClassBulkLoader::load_table(AOTLinkedClassTable* table, AOTLinkedClassCategory class_category, Handle loader, TRAPS) { 220 if (class_category != AOTLinkedClassCategory::BOOT1) { 221 assert(Universe::is_module_initialized(), "sanity"); 222 } 223 224 const char* category_name = AOTClassLinker::class_category_name(class_category); 225 switch (class_category) { 226 case AOTLinkedClassCategory::BOOT1: 227 load_classes_impl(class_category, table->boot(), category_name, loader, CHECK); 228 break; 229 230 case AOTLinkedClassCategory::BOOT2: 231 load_classes_impl(class_category, table->boot2(), category_name, loader, CHECK); 232 break; 233 234 case AOTLinkedClassCategory::PLATFORM: 235 { 236 initiate_loading(THREAD, category_name, loader, table->boot()); 237 initiate_loading(THREAD, category_name, loader, table->boot2()); 238 load_classes_impl(class_category, table->platform(), category_name, loader, CHECK); 239 } 240 break; 241 case AOTLinkedClassCategory::APP: 242 { 243 initiate_loading(THREAD, category_name, loader, table->boot()); 244 initiate_loading(THREAD, category_name, loader, table->boot2()); 245 initiate_loading(THREAD, category_name, loader, table->platform()); 246 load_classes_impl(class_category, table->app(), category_name, loader, CHECK); 247 } 248 break; 249 case AOTLinkedClassCategory::UNREGISTERED: 250 default: 251 ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category. 252 break; 253 } 254 } 255 256 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes, 257 const char* category_name, Handle loader, TRAPS) { 258 if (classes == nullptr) { 259 return; 260 } 261 262 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader()); 263 264 for (int i = 0; i < classes->length(); i++) { 265 if (UsePerfData) { 266 _perf_classes_preloaded->inc(); 267 } 268 InstanceKlass* ik = classes->at(i); 269 if (log_is_enabled(Info, cds, aot, load)) { 270 ResourceMark rm(THREAD); 271 log_info(cds, aot, load)("%-5s %s%s%s", category_name, ik->external_name(), 272 ik->is_loaded() ? " (already loaded)" : "", 273 ik->is_hidden() ? " (hidden)" : ""); 274 } 275 276 if (!ik->is_loaded()) { 277 if (ik->is_hidden()) { 278 load_hidden_class(loader_data, ik, CHECK); 279 } else { 280 InstanceKlass* actual; 281 if (loader_data == ClassLoaderData::the_null_class_loader_data()) { 282 actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK); 283 } else { 284 actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK); 285 } 286 287 if (actual != ik) { 288 ResourceMark rm(THREAD); 289 log_error(cds)("Unable to resolve %s class from CDS archive: %s", category_name, ik->external_name()); 290 log_error(cds)("Expected: " INTPTR_FORMAT ", actual: " INTPTR_FORMAT, p2i(ik), p2i(actual)); 291 log_error(cds)("JVMTI class retransformation is not supported when archive was generated with -XX:+AOTClassLinking."); 292 MetaspaceShared::unrecoverable_loading_error(); 293 } 294 assert(actual->is_loaded(), "must be"); 295 } 296 } 297 } 298 } 299 300 // Initiate loading of the <classes> in the <initiating_loader>. The <classes> should have already been loaded 301 // by a parent loader of the <initiating_loader>. This is necessary for handling pre-resolved CP entries. 302 // 303 // For example, we initiate the loading of java/lang/String in the AppClassLoader. This will allow 304 // any App classes to have a pre-resolved ConstantPool entry that references java/lang/String. 305 // 306 // TODO: we can limit the number of initiated classes to only those that are actually referenced by 307 // AOT-linked classes loaded by <initiating_loader>. 308 void AOTLinkedClassBulkLoader::initiate_loading(JavaThread* current, const char* category_name, 309 Handle initiating_loader, Array<InstanceKlass*>* classes) { 310 if (classes == nullptr) { 311 return; 312 } 313 314 assert(initiating_loader() == SystemDictionary::java_platform_loader() || 315 initiating_loader() == SystemDictionary::java_system_loader(), "must be"); 316 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(initiating_loader()); 317 MonitorLocker mu1(SystemDictionary_lock); 318 319 for (int i = 0; i < classes->length(); i++) { 320 InstanceKlass* ik = classes->at(i); 321 assert(ik->is_loaded(), "must have already been loaded by a parent loader"); 322 assert(ik->class_loader() != initiating_loader(), "must be a parent loader"); 323 assert(ik->class_loader() == nullptr || 324 ik->class_loader() == SystemDictionary::java_platform_loader(), "must be"); 325 if (ik->is_public() && !ik->is_hidden()) { 326 if (log_is_enabled(Info, cds, aot, load)) { 327 ResourceMark rm(current); 328 const char* defining_loader = (ik->class_loader() == nullptr ? "boot" : "plat"); 329 log_info(cds, aot, load)("%s %s (initiated, defined by %s)", category_name, ik->external_name(), 330 defining_loader); 331 } 332 SystemDictionary::add_to_initiating_loader(current, ik, loader_data); 333 } 334 } 335 } 336 337 // Currently, we archive only three types of hidden classes: 338 // - LambdaForms 339 // - lambda proxy classes 340 // - StringConcat classes 341 // See HeapShared::is_archivable_hidden_klass(). 342 // 343 // LambdaForm classes (with names like java/lang/invoke/LambdaForm$MH+0x800000015) logically 344 // belong to the boot loader, but they are usually stored in their own special ClassLoaderData to 345 // facilitate class unloading, as a LambdaForm may refer to a class loaded by a custom loader 346 // that may be unloaded. 347 // 348 // We only support AOT-resolution of indys in the boot/platform/app loader, so there's no need 349 // to support class unloading. For simplicity, we put all archived LambdaForm classes in the 350 // "main" ClassLoaderData of the boot loader. 351 // 352 // (Even if we were to support other loaders, we would still feel free to ignore any requirement 353 // of class unloading, for any class asset in the AOT cache. Anything that makes it into the AOT 354 // cache has a lifetime dispensation from unloading. After all, the AOT cache never grows, and 355 // we can assume that the user is content with its size, and doesn't need its footprint to shrink.) 356 // 357 // Lambda proxy classes are normally stored in the same ClassLoaderData as their nest hosts, and 358 // StringConcat are normally stored in the main ClassLoaderData of the boot class loader. We 359 // do the same for the archived copies of such classes. 360 void AOTLinkedClassBulkLoader::load_hidden_class(ClassLoaderData* loader_data, InstanceKlass* ik, TRAPS) { 361 assert(HeapShared::is_lambda_form_klass(ik) || 362 HeapShared::is_lambda_proxy_klass(ik) || 363 HeapShared::is_string_concat_klass(ik), "sanity"); 364 DEBUG_ONLY({ 365 assert(ik->java_super()->is_loaded(), "must be"); 366 for (int i = 0; i < ik->local_interfaces()->length(); i++) { 367 assert(ik->local_interfaces()->at(i)->is_loaded(), "must be"); 368 } 369 }); 370 371 Handle pd; 372 PackageEntry* pkg_entry = nullptr; 373 374 // Since a hidden class does not have a name, it cannot be reloaded 375 // normally via the system dictionary. Instead, we have to finish the 376 // loading job here. 377 378 if (HeapShared::is_lambda_proxy_klass(ik)) { 379 InstanceKlass* nest_host = ik->nest_host_not_null(); 380 assert(nest_host->is_loaded(), "must be"); 381 pd = Handle(THREAD, nest_host->protection_domain()); 382 pkg_entry = nest_host->package(); 383 } 384 385 ik->restore_unshareable_info(loader_data, pd, pkg_entry, CHECK); 386 SystemDictionary::load_shared_class_misc(ik, loader_data); 387 ik->add_to_hierarchy(THREAD); 388 assert(ik->is_loaded(), "Must be in at least loaded state"); 389 390 DEBUG_ONLY({ 391 // Make sure we don't make this hidden class available by name, even if we don't 392 // use any special ClassLoaderData. 393 Handle loader(THREAD, loader_data->class_loader()); 394 ResourceMark rm(THREAD); 395 assert(SystemDictionary::resolve_or_null(ik->name(), loader, pd, THREAD) == nullptr, 396 "hidden classes cannot be accessible by name: %s", ik->external_name()); 397 if (HAS_PENDING_EXCEPTION) { 398 CLEAR_PENDING_EXCEPTION; 399 } 400 }); 401 } 402 403 void AOTLinkedClassBulkLoader::finish_loading_javabase_classes(TRAPS) { 404 init_required_classes_for_loader(Handle(), AOTLinkedClassTable::for_static_archive()->boot(), CHECK); 405 } 406 407 // Some AOT-linked classes for <class_loader> must be initialized early. This includes 408 // - classes that were AOT-initialized by AOTClassInitializer 409 // - the classes of all objects that are reachable from the archived mirrors of 410 // the AOT-linked classes for <class_loader>. 411 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) { 412 if (classes != nullptr) { 413 for (int i = 0; i < classes->length(); i++) { 414 InstanceKlass* ik = classes->at(i); 415 if (ik->class_loader_data() == nullptr) { 416 // This class is not yet loaded. We will initialize it in a later phase. 417 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes 418 // but k is part of AOTLinkedClassCategory::BOOT2. 419 continue; 420 } 421 if (ik->has_aot_initialized_mirror()) { 422 ik->initialize_with_aot_initialized_mirror(CHECK); 423 } else { 424 // Some cached heap objects may hold references to methods in aot-linked 425 // classes (via MemberName). We need to make sure all classes are 426 // linked to allow such MemberNames to be invoked. 427 ik->link_class(CHECK); 428 } 429 } 430 } 431 432 HeapShared::init_classes_for_special_subgraph(class_loader, CHECK); 433 } 434 435 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) { 436 if (!CDSConfig::is_using_aot_linked_classes()) { 437 return false; 438 } 439 440 if (_all_completed) { // no more pending aot-linked classes 441 return false; 442 } 443 444 if (k->is_objArray_klass()) { 445 k = ObjArrayKlass::cast(k)->bottom_klass(); 446 } 447 if (!k->is_instance_klass()) { 448 // type array klasses (and their higher dimensions), 449 // must have been loaded before a GC can ever happen. 450 return false; 451 } 452 453 // There's a small window during VM start-up where a not-yet loaded aot-linked 454 // class k may be discovered by the GC during VM initialization. This can happen 455 // when the heap contains an aot-cached instance of k, but k is not ready to be 456 // loaded yet. (TODO: JDK-8342429 eliminates this possibility) 457 // 458 // The following checks try to limit this window as much as possible for each of 459 // the four AOTLinkedClassCategory of classes that can be aot-linked. 460 461 InstanceKlass* ik = InstanceKlass::cast(k); 462 if (ik->is_shared_boot_class()) { 463 if (ik->module() != nullptr && ik->in_javabase_module()) { 464 // AOTLinkedClassCategory::BOOT1 -- all aot-linked classes in 465 // java.base must have been loaded before a GC can ever happen. 466 return false; 467 } else { 468 // AOTLinkedClassCategory::BOOT2 classes cannot be loaded until 469 // module system is ready. 470 return !_boot2_completed; 471 } 472 } else if (ik->is_shared_platform_class()) { 473 // AOTLinkedClassCategory::PLATFORM classes cannot be loaded until 474 // the platform class loader is initialized. 475 return !_platform_completed; 476 } else if (ik->is_shared_app_class()) { 477 // AOTLinkedClassCategory::APP cannot be loaded until the app class loader 478 // is initialized. 479 return !_app_completed; 480 } else { 481 return false; 482 } 483 } 484 485 void AOTLinkedClassBulkLoader::replay_training_at_init(Array<InstanceKlass*>* classes, TRAPS) { 486 if (classes != nullptr) { 487 for (int i = 0; i < classes->length(); i++) { 488 InstanceKlass* ik = classes->at(i); 489 if (ik->has_aot_initialized_mirror() && ik->is_initialized() && !ik->has_init_deps_processed()) { 490 CompilationPolicy::replay_training_at_init(ik, CHECK); 491 } 492 } 493 } 494 } 495 496 void AOTLinkedClassBulkLoader::replay_training_at_init_for_preloaded_classes(TRAPS) { 497 if (CDSConfig::is_using_aot_linked_classes() && TrainingData::have_data()) { 498 AOTLinkedClassTable* table = AOTLinkedClassTable::for_static_archive(); // not applicable for dynamic archive (?? why??) 499 replay_training_at_init(table->boot(), CHECK); 500 replay_training_at_init(table->boot2(), CHECK); 501 replay_training_at_init(table->platform(), CHECK); 502 replay_training_at_init(table->app(), CHECK); 503 504 CompilationPolicy::replay_training_at_init(false, CHECK); 505 } 506 } 507 508 void AOTLinkedClassBulkLoader::print_counters_on(outputStream* st) { 509 if (UsePerfData && _perf_class_preload_counters != nullptr) { 510 st->print_cr("AOTLinkedClassBulkLoader:"); 511 st->print_cr(" preload: " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) " (thread) / " JLONG_FORMAT_W(5) " events", 512 _perf_class_preload_counters->elapsed_counter_value_us(), 513 _perf_class_preload_counters->thread_counter_value_us(), 514 _perf_classes_preloaded->get_value()); 515 } 516 }