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/cdsConfig.hpp" 31 #include "cds/heapShared.hpp" 32 #include "classfile/classLoaderData.hpp" 33 #include "classfile/systemDictionary.hpp" 34 #include "classfile/systemDictionaryShared.hpp" 35 #include "classfile/vmClasses.hpp" 36 #include "gc/shared/gcVMOperations.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/instanceKlass.hpp" 39 #include "oops/klass.inline.hpp" 40 #include "runtime/handles.inline.hpp" 41 #include "runtime/java.hpp" 42 43 bool AOTLinkedClassBulkLoader::_boot2_completed = false; 44 bool AOTLinkedClassBulkLoader::_platform_completed = false; 45 bool AOTLinkedClassBulkLoader::_app_completed = false; 46 bool AOTLinkedClassBulkLoader::_all_completed = false; 47 48 void AOTLinkedClassBulkLoader::serialize(SerializeClosure* soc, bool is_static_archive) { 49 AOTLinkedClassTable::get(is_static_archive)->serialize(soc); 50 } 51 52 void AOTLinkedClassBulkLoader::load_javabase_classes(JavaThread* current) { 53 assert(CDSConfig::is_using_aot_linked_classes(), "sanity"); 54 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT1, nullptr); // only java.base classes 55 } 56 57 void AOTLinkedClassBulkLoader::load_non_javabase_classes(JavaThread* current) { 58 assert(CDSConfig::is_using_aot_linked_classes(), "sanity"); 59 60 // is_using_aot_linked_classes() requires is_using_full_module_graph(). As a result, 61 // the platform/system class loader should already have been initialized as part 62 // of the FMG support. 63 assert(CDSConfig::is_using_full_module_graph(), "must be"); 64 assert(SystemDictionary::java_platform_loader() != nullptr, "must be"); 65 assert(SystemDictionary::java_system_loader() != nullptr, "must be"); 66 67 load_classes_in_loader(current, AOTLinkedClassCategory::BOOT2, nullptr); // all boot classes outside of java.base 68 _boot2_completed = true; 69 70 load_classes_in_loader(current, AOTLinkedClassCategory::PLATFORM, SystemDictionary::java_platform_loader()); 71 _platform_completed = true; 72 73 load_classes_in_loader(current, AOTLinkedClassCategory::APP, SystemDictionary::java_system_loader()); 74 _app_completed = true; 75 _all_completed = true; 76 } 77 78 void AOTLinkedClassBulkLoader::load_classes_in_loader(JavaThread* current, AOTLinkedClassCategory class_category, oop class_loader_oop) { 79 load_classes_in_loader_impl(class_category, class_loader_oop, current); 80 if (current->has_pending_exception()) { 81 // We cannot continue, as we might have loaded some of the aot-linked classes, which 82 // may have dangling C++ pointers to other aot-linked classes that we have failed to load. 83 exit_on_exception(current); 84 } 85 } 86 87 void AOTLinkedClassBulkLoader::exit_on_exception(JavaThread* current) { 88 assert(current->has_pending_exception(), "precondition"); 89 ResourceMark rm(current); 90 if (current->pending_exception()->is_a(vmClasses::OutOfMemoryError_klass())) { 91 log_error(cds)("Out of memory. Please run with a larger Java heap, current MaxHeapSize = " 92 SIZE_FORMAT "M", MaxHeapSize/M); 93 } else { 94 log_error(cds)("%s: %s", current->pending_exception()->klass()->external_name(), 95 java_lang_String::as_utf8_string(java_lang_Throwable::message(current->pending_exception()))); 96 } 97 vm_exit_during_initialization("Unexpected exception when loading aot-linked classes."); 98 } 99 100 void AOTLinkedClassBulkLoader::load_classes_in_loader_impl(AOTLinkedClassCategory class_category, oop class_loader_oop, TRAPS) { 101 Handle h_loader(THREAD, class_loader_oop); 102 load_table(AOTLinkedClassTable::for_static_archive(), class_category, h_loader, CHECK); 103 load_table(AOTLinkedClassTable::for_dynamic_archive(), class_category, h_loader, CHECK); 104 105 // Initialize the InstanceKlasses of all archived heap objects that are reachable from the 106 // archived java class mirrors. 107 // 108 // Only the classes in the static archive can have archived mirrors. 109 AOTLinkedClassTable* static_table = AOTLinkedClassTable::for_static_archive(); 110 switch (class_category) { 111 case AOTLinkedClassCategory::BOOT1: 112 // Delayed until finish_loading_javabase_classes(), as the VM is not ready to 113 // execute some of the <clinit> methods. 114 break; 115 case AOTLinkedClassCategory::BOOT2: 116 init_required_classes_for_loader(h_loader, static_table->boot2(), CHECK); 117 break; 118 case AOTLinkedClassCategory::PLATFORM: 119 init_required_classes_for_loader(h_loader, static_table->platform(), CHECK); 120 break; 121 case AOTLinkedClassCategory::APP: 122 init_required_classes_for_loader(h_loader, static_table->app(), CHECK); 123 break; 124 case AOTLinkedClassCategory::UNREGISTERED: 125 ShouldNotReachHere(); 126 break; 127 } 128 129 if (Universe::is_fully_initialized() && VerifyDuringStartup) { 130 // Make sure we're still in a clean state. 131 VM_Verify verify_op; 132 VMThread::execute(&verify_op); 133 } 134 } 135 136 void AOTLinkedClassBulkLoader::load_table(AOTLinkedClassTable* table, AOTLinkedClassCategory class_category, Handle loader, TRAPS) { 137 if (class_category != AOTLinkedClassCategory::BOOT1) { 138 assert(Universe::is_module_initialized(), "sanity"); 139 } 140 141 const char* category_name = AOTClassLinker::class_category_name(class_category); 142 switch (class_category) { 143 case AOTLinkedClassCategory::BOOT1: 144 load_classes_impl(class_category, table->boot(), category_name, loader, CHECK); 145 break; 146 147 case AOTLinkedClassCategory::BOOT2: 148 load_classes_impl(class_category, table->boot2(), category_name, loader, CHECK); 149 break; 150 151 case AOTLinkedClassCategory::PLATFORM: 152 { 153 initiate_loading(THREAD, category_name, loader, table->boot()); 154 initiate_loading(THREAD, category_name, loader, table->boot2()); 155 load_classes_impl(class_category, table->platform(), category_name, loader, CHECK); 156 } 157 break; 158 case AOTLinkedClassCategory::APP: 159 { 160 initiate_loading(THREAD, category_name, loader, table->boot()); 161 initiate_loading(THREAD, category_name, loader, table->boot2()); 162 initiate_loading(THREAD, category_name, loader, table->platform()); 163 load_classes_impl(class_category, table->app(), category_name, loader, CHECK); 164 } 165 break; 166 case AOTLinkedClassCategory::UNREGISTERED: 167 default: 168 ShouldNotReachHere(); // Currently aot-linked classes are not supported for this category. 169 break; 170 } 171 } 172 173 void AOTLinkedClassBulkLoader::load_classes_impl(AOTLinkedClassCategory class_category, Array<InstanceKlass*>* classes, 174 const char* category_name, Handle loader, TRAPS) { 175 if (classes == nullptr) { 176 return; 177 } 178 179 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader()); 180 181 for (int i = 0; i < classes->length(); i++) { 182 InstanceKlass* ik = classes->at(i); 183 if (log_is_enabled(Info, cds, aot, load)) { 184 ResourceMark rm(THREAD); 185 log_info(cds, aot, load)("%-5s %s%s%s", category_name, ik->external_name(), 186 ik->is_loaded() ? " (already loaded)" : "", 187 ik->is_hidden() ? " (hidden)" : ""); 188 } 189 190 if (!ik->is_loaded()) { 191 if (ik->is_hidden()) { 192 load_hidden_class(loader_data, ik, CHECK); 193 } else { 194 InstanceKlass* actual; 195 if (loader_data == ClassLoaderData::the_null_class_loader_data()) { 196 actual = SystemDictionary::load_instance_class(ik->name(), loader, CHECK); 197 } else { 198 actual = SystemDictionaryShared::find_or_load_shared_class(ik->name(), loader, CHECK); 199 } 200 201 if (actual != ik) { 202 ResourceMark rm(THREAD); 203 log_error(cds)("Unable to resolve %s class from CDS archive: %s", category_name, ik->external_name()); 204 log_error(cds)("Expected: " INTPTR_FORMAT ", actual: " INTPTR_FORMAT, p2i(ik), p2i(actual)); 205 log_error(cds)("JVMTI class retransformation is not supported when archive was generated with -XX:+AOTClassLinking."); 206 MetaspaceShared::unrecoverable_loading_error(); 207 } 208 assert(actual->is_loaded(), "must be"); 209 } 210 } 211 } 212 } 213 214 // Initiate loading of the <classes> in the <initiating_loader>. The <classes> should have already been loaded 215 // by a parent loader of the <initiating_loader>. This is necessary for handling pre-resolved CP entries. 216 // 217 // For example, we initiate the loading of java/lang/String in the AppClassLoader. This will allow 218 // any App classes to have a pre-resolved ConstantPool entry that references java/lang/String. 219 // 220 // TODO: we can limit the number of initiated classes to only those that are actually referenced by 221 // AOT-linked classes loaded by <initiating_loader>. 222 void AOTLinkedClassBulkLoader::initiate_loading(JavaThread* current, const char* category_name, 223 Handle initiating_loader, Array<InstanceKlass*>* classes) { 224 if (classes == nullptr) { 225 return; 226 } 227 228 assert(initiating_loader() == SystemDictionary::java_platform_loader() || 229 initiating_loader() == SystemDictionary::java_system_loader(), "must be"); 230 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(initiating_loader()); 231 MonitorLocker mu1(SystemDictionary_lock); 232 233 for (int i = 0; i < classes->length(); i++) { 234 InstanceKlass* ik = classes->at(i); 235 assert(ik->is_loaded(), "must have already been loaded by a parent loader"); 236 assert(ik->class_loader() != initiating_loader(), "must be a parent loader"); 237 assert(ik->class_loader() == nullptr || 238 ik->class_loader() == SystemDictionary::java_platform_loader(), "must be"); 239 if (ik->is_public() && !ik->is_hidden()) { 240 if (log_is_enabled(Info, cds, aot, load)) { 241 ResourceMark rm(current); 242 const char* defining_loader = (ik->class_loader() == nullptr ? "boot" : "plat"); 243 log_info(cds, aot, load)("%s %s (initiated, defined by %s)", category_name, ik->external_name(), 244 defining_loader); 245 } 246 SystemDictionary::add_to_initiating_loader(current, ik, loader_data); 247 } 248 } 249 } 250 251 // Currently, we archive only three types of hidden classes: 252 // - LambdaForms 253 // - lambda proxy classes 254 // - StringConcat classes 255 // See HeapShared::is_archivable_hidden_klass(). 256 // 257 // LambdaForm classes (with names like java/lang/invoke/LambdaForm$MH+0x800000015) logically 258 // belong to the boot loader, but they are usually stored in their own special ClassLoaderData to 259 // facilitate class unloading, as a LambdaForm may refer to a class loaded by a custom loader 260 // that may be unloaded. 261 // 262 // We only support AOT-resolution of indys in the boot/platform/app loader, so there's no need 263 // to support class unloading. For simplicity, we put all archived LambdaForm classes in the 264 // "main" ClassLoaderData of the boot loader. 265 // 266 // (Even if we were to support other loaders, we would still feel free to ignore any requirement 267 // of class unloading, for any class asset in the AOT cache. Anything that makes it into the AOT 268 // cache has a lifetime dispensation from unloading. After all, the AOT cache never grows, and 269 // we can assume that the user is content with its size, and doesn't need its footprint to shrink.) 270 // 271 // Lambda proxy classes are normally stored in the same ClassLoaderData as their nest hosts, and 272 // StringConcat are normally stored in the main ClassLoaderData of the boot class loader. We 273 // do the same for the archived copies of such classes. 274 void AOTLinkedClassBulkLoader::load_hidden_class(ClassLoaderData* loader_data, InstanceKlass* ik, TRAPS) { 275 assert(HeapShared::is_lambda_form_klass(ik) || 276 HeapShared::is_lambda_proxy_klass(ik) || 277 HeapShared::is_string_concat_klass(ik), "sanity"); 278 DEBUG_ONLY({ 279 assert(ik->java_super()->is_loaded(), "must be"); 280 for (int i = 0; i < ik->local_interfaces()->length(); i++) { 281 assert(ik->local_interfaces()->at(i)->is_loaded(), "must be"); 282 } 283 }); 284 285 Handle pd; 286 PackageEntry* pkg_entry = nullptr; 287 288 // Since a hidden class does not have a name, it cannot be reloaded 289 // normally via the system dictionary. Instead, we have to finish the 290 // loading job here. 291 292 if (HeapShared::is_lambda_proxy_klass(ik)) { 293 InstanceKlass* nest_host = ik->nest_host_not_null(); 294 assert(nest_host->is_loaded(), "must be"); 295 pd = Handle(THREAD, nest_host->protection_domain()); 296 pkg_entry = nest_host->package(); 297 } 298 299 ik->restore_unshareable_info(loader_data, pd, pkg_entry, CHECK); 300 SystemDictionary::load_shared_class_misc(ik, loader_data); 301 ik->add_to_hierarchy(THREAD); 302 assert(ik->is_loaded(), "Must be in at least loaded state"); 303 304 DEBUG_ONLY({ 305 // Make sure we don't make this hidden class available by name, even if we don't 306 // use any special ClassLoaderData. 307 Handle loader(THREAD, loader_data->class_loader()); 308 ResourceMark rm(THREAD); 309 assert(SystemDictionary::resolve_or_null(ik->name(), loader, pd, THREAD) == nullptr, 310 "hidden classes cannot be accessible by name: %s", ik->external_name()); 311 if (HAS_PENDING_EXCEPTION) { 312 CLEAR_PENDING_EXCEPTION; 313 } 314 }); 315 } 316 317 void AOTLinkedClassBulkLoader::finish_loading_javabase_classes(TRAPS) { 318 init_required_classes_for_loader(Handle(), AOTLinkedClassTable::for_static_archive()->boot(), CHECK); 319 } 320 321 // Some AOT-linked classes for <class_loader> must be initialized early. This includes 322 // - classes that were AOT-initialized by AOTClassInitializer 323 // - the classes of all objects that are reachable from the archived mirrors of 324 // the AOT-linked classes for <class_loader>. 325 void AOTLinkedClassBulkLoader::init_required_classes_for_loader(Handle class_loader, Array<InstanceKlass*>* classes, TRAPS) { 326 if (classes != nullptr) { 327 for (int i = 0; i < classes->length(); i++) { 328 InstanceKlass* ik = classes->at(i); 329 if (ik->class_loader_data() == nullptr) { 330 // This class is not yet loaded. We will initialize it in a later phase. 331 // For example, we have loaded only AOTLinkedClassCategory::BOOT1 classes 332 // but k is part of AOTLinkedClassCategory::BOOT2. 333 continue; 334 } 335 if (ik->has_aot_initialized_mirror()) { 336 ik->initialize_with_aot_initialized_mirror(CHECK); 337 } else { 338 // Some cached heap objects may hold references to methods in aot-linked 339 // classes (via MemberName). We need to make sure all classes are 340 // linked to allow such MemberNames to be invoked. 341 ik->link_class(CHECK); 342 } 343 } 344 } 345 346 HeapShared::init_classes_for_special_subgraph(class_loader, CHECK); 347 } 348 349 bool AOTLinkedClassBulkLoader::is_pending_aot_linked_class(Klass* k) { 350 if (!CDSConfig::is_using_aot_linked_classes()) { 351 return false; 352 } 353 354 if (_all_completed) { // no more pending aot-linked classes 355 return false; 356 } 357 358 if (k->is_objArray_klass()) { 359 k = ObjArrayKlass::cast(k)->bottom_klass(); 360 } 361 if (!k->is_instance_klass()) { 362 // type array klasses (and their higher dimensions), 363 // must have been loaded before a GC can ever happen. 364 return false; 365 } 366 367 // There's a small window during VM start-up where a not-yet loaded aot-linked 368 // class k may be discovered by the GC during VM initialization. This can happen 369 // when the heap contains an aot-cached instance of k, but k is not ready to be 370 // loaded yet. (TODO: JDK-8342429 eliminates this possibility) 371 // 372 // The following checks try to limit this window as much as possible for each of 373 // the four AOTLinkedClassCategory of classes that can be aot-linked. 374 375 InstanceKlass* ik = InstanceKlass::cast(k); 376 if (ik->is_shared_boot_class()) { 377 if (ik->module() != nullptr && ik->in_javabase_module()) { 378 // AOTLinkedClassCategory::BOOT1 -- all aot-linked classes in 379 // java.base must have been loaded before a GC can ever happen. 380 return false; 381 } else { 382 // AOTLinkedClassCategory::BOOT2 classes cannot be loaded until 383 // module system is ready. 384 return !_boot2_completed; 385 } 386 } else if (ik->is_shared_platform_class()) { 387 // AOTLinkedClassCategory::PLATFORM classes cannot be loaded until 388 // the platform class loader is initialized. 389 return !_platform_completed; 390 } else if (ik->is_shared_app_class()) { 391 // AOTLinkedClassCategory::APP cannot be loaded until the app class loader 392 // is initialized. 393 return !_app_completed; 394 } else { 395 return false; 396 } 397 }