1 /* 2 * Copyright (c) 2014, 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/archiveHeapLoader.hpp" 28 #include "cds/archiveUtils.hpp" 29 #include "cds/cdsConfig.hpp" 30 #include "cds/classListParser.hpp" 31 #include "cds/classListWriter.hpp" 32 #include "cds/classPreinitializer.hpp" 33 #include "cds/dynamicArchive.hpp" 34 #include "cds/filemap.hpp" 35 #include "cds/heapShared.hpp" 36 #include "cds/cdsProtectionDomain.hpp" 37 #include "cds/dumpTimeClassInfo.inline.hpp" 38 #include "cds/lambdaFormInvokers.inline.hpp" 39 #include "cds/metaspaceShared.hpp" 40 #include "cds/methodDataDictionary.hpp" 41 #include "cds/runTimeClassInfo.hpp" 42 #include "classfile/classFileStream.hpp" 43 #include "classfile/classLoader.hpp" 44 #include "classfile/classLoaderData.inline.hpp" 45 #include "classfile/classLoaderDataGraph.hpp" 46 #include "classfile/classLoaderExt.hpp" 47 #include "classfile/dictionary.hpp" 48 #include "classfile/javaClasses.hpp" 49 #include "classfile/javaClasses.inline.hpp" 50 #include "classfile/symbolTable.hpp" 51 #include "classfile/systemDictionary.hpp" 52 #include "classfile/systemDictionaryShared.hpp" 53 #include "classfile/verificationType.hpp" 54 #include "classfile/vmClasses.hpp" 55 #include "classfile/vmSymbols.hpp" 56 #include "interpreter/bootstrapInfo.hpp" 57 #include "jfr/jfrEvents.hpp" 58 #include "logging/log.hpp" 59 #include "logging/logStream.hpp" 60 #include "memory/allocation.hpp" 61 #include "memory/metadataFactory.hpp" 62 #include "memory/metaspaceClosure.hpp" 63 #include "memory/oopFactory.hpp" 64 #include "memory/resourceArea.hpp" 65 #include "memory/universe.hpp" 66 #include "oops/instanceKlass.hpp" 67 #include "oops/klass.inline.hpp" 68 #include "oops/methodData.hpp" 69 #include "oops/trainingData.hpp" 70 #include "oops/objArrayKlass.hpp" 71 #include "oops/objArrayOop.inline.hpp" 72 #include "oops/oop.inline.hpp" 73 #include "oops/oopHandle.inline.hpp" 74 #include "oops/typeArrayOop.inline.hpp" 75 #include "runtime/arguments.hpp" 76 #include "runtime/handles.inline.hpp" 77 #include "runtime/java.hpp" 78 #include "runtime/javaCalls.hpp" 79 #include "runtime/mutexLocker.hpp" 80 #include "utilities/resourceHash.hpp" 81 #include "utilities/stringUtils.hpp" 82 83 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive; 84 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive; 85 86 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr; 87 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr; 88 89 DumpTimeMethodInfoDictionary* SystemDictionaryShared::_dumptime_method_info_dictionary = nullptr; 90 DumpTimeMethodInfoDictionary* SystemDictionaryShared::_cloned_dumptime_method_info_dictionary = nullptr; 91 static Array<InstanceKlass*>* _archived_lambda_form_classes = nullptr; 92 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_boot = nullptr; 93 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_boot2 = nullptr; 94 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_platform = nullptr; 95 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_app = nullptr; 96 static bool _ignore_new_classes = false; 97 98 // Used by NoClassLoadingMark 99 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;) 100 101 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader( 102 Symbol* class_name, Handle class_loader, TRAPS) { 103 assert(CDSConfig::is_using_archive(), "must be"); 104 InstanceKlass* ik = find_builtin_class(class_name); 105 106 if (ik != nullptr && !ik->shared_loading_failed()) { 107 if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class()) || 108 (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) { 109 SharedClassLoadingMark slm(THREAD, ik); 110 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader); 111 Handle protection_domain; 112 if (!class_name->starts_with("jdk/proxy")) // java/lang/reflect/Proxy$ProxyBuilder defines the proxy classes with a null protection domain. 113 { 114 protection_domain = CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL); 115 } 116 return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD); 117 } 118 } 119 return nullptr; 120 } 121 122 // This function is called for loading only UNREGISTERED classes 123 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name, 124 Handle class_loader, 125 Handle protection_domain, 126 const ClassFileStream* cfs, 127 TRAPS) { 128 if (!CDSConfig::is_using_archive()) { 129 return nullptr; 130 } 131 if (class_name == nullptr) { // don't do this for hidden classes 132 return nullptr; 133 } 134 if (class_loader.is_null() || 135 SystemDictionary::is_system_class_loader(class_loader()) || 136 SystemDictionary::is_platform_class_loader(class_loader())) { 137 // Do nothing for the BUILTIN loaders. 138 return nullptr; 139 } 140 141 const RunTimeClassInfo* record = find_record(&_static_archive._unregistered_dictionary, 142 &_dynamic_archive._unregistered_dictionary, 143 class_name); 144 if (record == nullptr) { 145 return nullptr; 146 } 147 148 int clsfile_size = cfs->length(); 149 int clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 150 151 if (!record->matches(clsfile_size, clsfile_crc32)) { 152 return nullptr; 153 } 154 155 return acquire_class_for_current_thread(record->_klass, class_loader, 156 protection_domain, cfs, 157 THREAD); 158 } 159 160 InstanceKlass* SystemDictionaryShared::acquire_class_for_current_thread( 161 InstanceKlass *ik, 162 Handle class_loader, 163 Handle protection_domain, 164 const ClassFileStream *cfs, 165 TRAPS) { 166 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader()); 167 168 { 169 MutexLocker mu(THREAD, SharedDictionary_lock); 170 if (ik->class_loader_data() != nullptr) { 171 // ik is already loaded (by this loader or by a different loader) 172 // or ik is being loaded by a different thread (by this loader or by a different loader) 173 return nullptr; 174 } 175 176 // No other thread has acquired this yet, so give it to *this thread* 177 ik->set_class_loader_data(loader_data); 178 } 179 180 // No longer holding SharedDictionary_lock 181 // No need to lock, as <ik> can be held only by a single thread. 182 loader_data->add_class(ik); 183 184 // Get the package entry. 185 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader); 186 187 // Load and check super/interfaces, restore unshareable info 188 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain, 189 cfs, pkg_entry, THREAD); 190 if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) { 191 // TODO: clean up <ik> so it can be used again 192 return nullptr; 193 } 194 195 return shared_klass; 196 } 197 198 // Guaranteed to return non-null value for non-shared classes. 199 // k must not be a shared class. 200 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) { 201 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 202 //assert(!k->is_shared(), "sanity"); // FIXME new workflow 203 return get_info_locked(k); 204 } 205 206 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) { 207 assert_lock_strong(DumpTimeTable_lock); 208 //assert(!k->is_shared(), "sanity"); // FIXME new workflow 209 DumpTimeClassInfo* info = _dumptime_table->get_info(k); 210 assert(info != nullptr, "must be"); 211 return info; 212 } 213 214 void SystemDictionaryShared::mark_required_class(InstanceKlass* k) { 215 DumpTimeClassInfo* info = _dumptime_table->get(k); 216 ResourceMark rm; 217 if (info != nullptr) { 218 info->set_is_required(); 219 } 220 } 221 222 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) { 223 if (!CDSConfig::is_dumping_final_static_archive() && MetaspaceShared::is_in_shared_metaspace(k)) { 224 // We have reached a super type that's already in the base archive. Treat it 225 // as "not excluded". 226 assert(CDSConfig::is_dumping_dynamic_archive(), "must be"); 227 return false; 228 } 229 230 if (info == nullptr) { 231 info = _dumptime_table->get(k); 232 assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table"); 233 } 234 235 if (!info->has_checked_exclusion()) { 236 if (check_for_exclusion_impl(k)) { 237 info->set_excluded(); 238 } 239 info->set_has_checked_exclusion(); 240 } 241 242 return info->is_excluded(); 243 } 244 245 // Returns true so the caller can do: return warn_excluded("....."); 246 bool SystemDictionaryShared::warn_excluded(InstanceKlass* k, const char* reason) { 247 ResourceMark rm; 248 log_warning(cds)("Skipping %s: %s", k->name()->as_C_string(), reason); 249 return true; 250 } 251 252 bool SystemDictionaryShared::is_jfr_event_class(InstanceKlass *k) { 253 while (k) { 254 if (k->name()->equals("jdk/internal/event/Event")) { 255 return true; 256 } 257 k = k->java_super(); 258 } 259 return false; 260 } 261 262 bool SystemDictionaryShared::is_registered_lambda_proxy_class(InstanceKlass* ik) { 263 DumpTimeClassInfo* info = _dumptime_table->get(ik); 264 return (info != nullptr) ? info->_is_archived_lambda_proxy : false; 265 } 266 267 void SystemDictionaryShared::reset_registered_lambda_proxy_class(InstanceKlass* ik) { 268 DumpTimeClassInfo* info = _dumptime_table->get(ik); 269 if (info != nullptr) { 270 info->_is_archived_lambda_proxy = false; 271 info->set_excluded(); 272 } 273 } 274 275 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) { 276 DumpTimeClassInfo* info = _dumptime_table->get(ik); 277 return (info != nullptr) ? info->is_early_klass() : false; 278 } 279 280 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) { 281 assert(ik->is_shared(), "applicable to only a shared class"); 282 if (ik->is_hidden()) { 283 return true; 284 } else { 285 return false; 286 } 287 } 288 289 void SystemDictionaryShared::ignore_new_classes() { 290 _ignore_new_classes = true; 291 } 292 293 294 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) { 295 if (CDSConfig::is_dumping_final_static_archive() && k->is_shared_unregistered_class() 296 && k->is_shared()) { 297 return false; // Do not exclude: unregistered classes are passed from preimage to final image. 298 } 299 300 if (k->is_in_error_state()) { 301 return warn_excluded(k, "In error state"); 302 } 303 if (k->is_scratch_class()) { 304 return warn_excluded(k, "A scratch class"); 305 } 306 if (!k->is_loaded()) { 307 return warn_excluded(k, "Not in loaded state"); 308 } 309 if (has_been_redefined(k)) { 310 return warn_excluded(k, "Has been redefined"); 311 } 312 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) { 313 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native 314 // agent during dump time). 315 return warn_excluded(k, "Unsupported location"); 316 } 317 if (k->signers() != nullptr) { 318 // We cannot include signed classes in the archive because the certificates 319 // used during dump time may be different than those used during 320 // runtime (due to expiration, etc). 321 return warn_excluded(k, "Signed JAR"); 322 } 323 if (is_jfr_event_class(k)) { 324 // We cannot include JFR event classes because they need runtime-specific 325 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false. 326 // There are only a small number of these classes, so it's not worthwhile to 327 // support them and make CDS more complicated. 328 if (!CDSConfig::is_dumping_reflection_data()) { // FIXME: !!! HACK !!! 329 return warn_excluded(k, "JFR event class"); 330 } 331 } 332 333 if (!CDSConfig::preserve_all_dumptime_verification_states(k)) { 334 if (!k->is_linked()) { 335 if (has_class_failed_verification(k)) { 336 return warn_excluded(k, "Failed verification"); 337 } 338 } else { 339 if (!k->can_be_verified_at_dumptime()) { 340 // We have an old class that has been linked (e.g., it's been executed during 341 // dump time). This class has been verified using the old verifier, which 342 // doesn't save the verification constraints, so check_verification_constraints() 343 // won't work at runtime. 344 // As a result, we cannot store this class. It must be loaded and fully verified 345 // at runtime. 346 return warn_excluded(k, "Old class has been linked"); 347 } 348 } 349 } 350 351 if (k->is_hidden() && !should_hidden_class_be_archived(k)) { 352 log_info(cds)("Skipping %s: Hidden class", k->name()->as_C_string()); 353 return true; 354 } 355 356 InstanceKlass* super = k->java_super(); 357 if (super != nullptr && check_for_exclusion(super, nullptr)) { 358 ResourceMark rm; 359 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string()); 360 return true; 361 } 362 363 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 364 int len = interfaces->length(); 365 for (int i = 0; i < len; i++) { 366 InstanceKlass* intf = interfaces->at(i); 367 if (check_for_exclusion(intf, nullptr)) { 368 ResourceMark rm; 369 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string()); 370 return true; 371 } 372 } 373 374 return false; // false == k should NOT be excluded 375 } 376 377 bool SystemDictionaryShared::is_builtin_loader(ClassLoaderData* loader_data) { 378 oop class_loader = loader_data->class_loader(); 379 return (class_loader == nullptr || 380 SystemDictionary::is_system_class_loader(class_loader) || 381 SystemDictionary::is_platform_class_loader(class_loader)); 382 } 383 384 bool SystemDictionaryShared::has_platform_or_app_classes() { 385 if (FileMapInfo::current_info()->has_platform_or_app_classes()) { 386 return true; 387 } 388 if (DynamicArchive::is_mapped() && 389 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) { 390 return true; 391 } 392 return false; 393 } 394 395 // The following stack shows how this code is reached: 396 // 397 // [0] SystemDictionaryShared::find_or_load_shared_class() 398 // [1] JVM_FindLoadedClass 399 // [2] java.lang.ClassLoader.findLoadedClass0() 400 // [3] java.lang.ClassLoader.findLoadedClass() 401 // [4] jdk.internal.loader.BuiltinClassLoader.loadClassOrNull() 402 // [5] jdk.internal.loader.BuiltinClassLoader.loadClass() 403 // [6] jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(), or 404 // jdk.internal.loader.ClassLoaders$PlatformClassLoader.loadClass() 405 // 406 // AppCDS supports fast class loading for these 2 built-in class loaders: 407 // jdk.internal.loader.ClassLoaders$PlatformClassLoader 408 // jdk.internal.loader.ClassLoaders$AppClassLoader 409 // with the following assumptions (based on the JDK core library source code): 410 // 411 // [a] these two loaders use the BuiltinClassLoader.loadClassOrNull() to 412 // load the named class. 413 // [b] BuiltinClassLoader.loadClassOrNull() first calls findLoadedClass(name). 414 // [c] At this point, if we can find the named class inside the 415 // shared_dictionary, we can perform further checks (see 416 // SystemDictionary::is_shared_class_visible) to ensure that this class 417 // was loaded by the same class loader during dump time. 418 // 419 // Given these assumptions, we intercept the findLoadedClass() call to invoke 420 // SystemDictionaryShared::find_or_load_shared_class() to load the shared class from 421 // the archive for the 2 built-in class loaders. This way, 422 // we can improve start-up because we avoid decoding the classfile, 423 // and avoid delegating to the parent loader. 424 // 425 // NOTE: there's a lot of assumption about the Java code. If any of that change, this 426 // needs to be redesigned. 427 428 InstanceKlass* SystemDictionaryShared::find_or_load_shared_class( 429 Symbol* name, Handle class_loader, TRAPS) { 430 InstanceKlass* k = nullptr; 431 if (CDSConfig::is_using_archive()) { 432 if (!has_platform_or_app_classes()) { 433 return nullptr; 434 } 435 436 if (SystemDictionary::is_system_class_loader(class_loader()) || 437 SystemDictionary::is_platform_class_loader(class_loader())) { 438 // Fix for 4474172; see evaluation for more details 439 class_loader = Handle( 440 THREAD, java_lang_ClassLoader::non_reflection_class_loader(class_loader())); 441 ClassLoaderData *loader_data = register_loader(class_loader); 442 Dictionary* dictionary = loader_data->dictionary(); 443 444 // Note: currently, find_or_load_shared_class is called only from 445 // JVM_FindLoadedClass and used for PlatformClassLoader and AppClassLoader, 446 // which are parallel-capable loaders, so a lock here is NOT taken. 447 assert(get_loader_lock_or_null(class_loader) == nullptr, "ObjectLocker not required"); 448 { 449 MutexLocker mu(THREAD, SystemDictionary_lock); 450 InstanceKlass* check = dictionary->find_class(THREAD, name); 451 if (check != nullptr) { 452 return check; 453 } 454 } 455 456 k = load_shared_class_for_builtin_loader(name, class_loader, THREAD); 457 if (k != nullptr) { 458 SharedClassLoadingMark slm(THREAD, k); 459 k = find_or_define_instance_class(name, class_loader, k, CHECK_NULL); 460 } 461 } 462 } 463 return k; 464 } 465 466 class UnregisteredClassesTable : public ResourceHashtable< 467 Symbol*, InstanceKlass*, 468 15889, // prime number 469 AnyObj::C_HEAP> {}; 470 471 static UnregisteredClassesTable* _unregistered_classes_table = nullptr; 472 473 // true == class was successfully added; false == a duplicated class (with the same name) already exists. 474 bool SystemDictionaryShared::add_unregistered_class(Thread* current, InstanceKlass* klass) { 475 // We don't allow duplicated unregistered classes with the same name. 476 // We only archive the first class with that name that succeeds putting 477 // itself into the table. 478 assert(CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled(), "sanity"); 479 MutexLocker ml(current, UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag); 480 Symbol* name = klass->name(); 481 if (_unregistered_classes_table == nullptr) { 482 _unregistered_classes_table = new (mtClass)UnregisteredClassesTable(); 483 } 484 bool created; 485 InstanceKlass** v = _unregistered_classes_table->put_if_absent(name, klass, &created); 486 if (created) { 487 name->increment_refcount(); 488 } 489 return (klass == *v); 490 } 491 492 // This function is called to lookup the super/interfaces of shared classes for 493 // unregistered loaders. E.g., SharedClass in the below example 494 // where "super:" (and optionally "interface:") have been specified. 495 // 496 // java/lang/Object id: 0 497 // Interface id: 2 super: 0 source: cust.jar 498 // SharedClass id: 4 super: 0 interfaces: 2 source: cust.jar 499 InstanceKlass* SystemDictionaryShared::lookup_super_for_unregistered_class( 500 Symbol* class_name, Symbol* super_name, bool is_superclass) { 501 502 assert(CDSConfig::is_dumping_static_archive(), "only when static dumping"); 503 504 if (!ClassListParser::is_parsing_thread()) { 505 // Unregistered classes can be created only by ClassListParser::_parsing_thread. 506 507 return nullptr; 508 } 509 510 ClassListParser* parser = ClassListParser::instance(); 511 if (parser == nullptr) { 512 // We're still loading the well-known classes, before the ClassListParser is created. 513 return nullptr; 514 } 515 if (class_name->equals(parser->current_class_name())) { 516 // When this function is called, all the numbered super and interface types 517 // must have already been loaded. Hence this function is never recursively called. 518 if (is_superclass) { 519 return parser->lookup_super_for_current_class(super_name); 520 } else { 521 return parser->lookup_interface_for_current_class(super_name); 522 } 523 } else { 524 // The VM is not trying to resolve a super type of parser->current_class_name(). 525 // Instead, it's resolving an error class (because parser->current_class_name() has 526 // failed parsing or verification). Don't do anything here. 527 return nullptr; 528 } 529 } 530 531 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) { 532 assert(CDSConfig::is_dumping_archive(), "sanity"); 533 assert(!is_builtin(k), "must be unregistered class"); 534 DumpTimeClassInfo* info = get_info(k); 535 info->_clsfile_size = cfs->length(); 536 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length()); 537 } 538 539 void SystemDictionaryShared::initialize() { 540 if (CDSConfig::is_dumping_archive()) { 541 _dumptime_table = new (mtClass) DumpTimeSharedClassTable; 542 _dumptime_lambda_proxy_class_dictionary = 543 new (mtClass) DumpTimeLambdaProxyClassDictionary; 544 _dumptime_method_info_dictionary = new (mtClass) DumpTimeMethodInfoDictionary; 545 } 546 } 547 548 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) { 549 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 550 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity"); 551 DumpTimeClassInfo* info = _dumptime_table->allocate_info(k); 552 if (_ignore_new_classes) { 553 if (!LambdaFormInvokers::may_be_regenerated_class(k->name())) { 554 ResourceMark rm; 555 log_debug(cds)("Skipping %s: Class loaded for lambda form invoker regeneration", k->name()->as_C_string()); 556 info->set_excluded(); 557 } 558 } 559 } 560 561 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) { 562 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 563 _dumptime_table->remove(k); 564 } 565 566 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) { 567 if (CDSConfig::is_dumping_archive()) { 568 remove_dumptime_info(klass); 569 } 570 571 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) { 572 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag); 573 if (_unregistered_classes_table != nullptr) { 574 // Remove the class from _unregistered_classes_table: keep the entry but 575 // set it to null. This ensure no classes with the same name can be 576 // added again. 577 InstanceKlass** v = _unregistered_classes_table->get(klass->name()); 578 if (v != nullptr) { 579 *v = nullptr; 580 } 581 } 582 } else { 583 assert(_unregistered_classes_table == nullptr, "must not be used"); 584 } 585 586 if (ClassListWriter::is_enabled()) { 587 ClassListWriter cw; 588 cw.handle_class_unloading((const InstanceKlass*)klass); 589 } 590 } 591 592 // Check if a class or any of its supertypes has been redefined. 593 bool SystemDictionaryShared::has_been_redefined(InstanceKlass* k) { 594 if (k->has_been_redefined()) { 595 return true; 596 } 597 if (k->java_super() != nullptr && has_been_redefined(k->java_super())) { 598 return true; 599 } 600 Array<InstanceKlass*>* interfaces = k->local_interfaces(); 601 int len = interfaces->length(); 602 for (int i = 0; i < len; i++) { 603 if (has_been_redefined(interfaces->at(i))) { 604 return true; 605 } 606 } 607 return false; 608 } 609 610 // k is a class before relocating by ArchiveBuilder 611 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) { 612 ResourceMark rm; 613 const char* name = k->name()->as_C_string(); 614 DumpTimeClassInfo* info = _dumptime_table->get(k); 615 assert(!class_loading_may_happen(), "class loading must be disabled"); 616 guarantee(info != nullptr, "Class %s must be entered into _dumptime_table", name); 617 guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name); 618 if (is_builtin(k)) { 619 if (k->is_hidden()) { 620 if (CDSConfig::is_dumping_invokedynamic()) { // FIXME -- clean up 621 return; 622 } 623 assert(should_hidden_class_be_archived(k), "unexpected hidden class %s", name); 624 } 625 guarantee(!k->is_shared_unregistered_class(), 626 "Class loader type must be set for BUILTIN class %s", name); 627 628 } else { 629 guarantee(k->is_shared_unregistered_class(), 630 "Class loader type must not be set for UNREGISTERED class %s", name); 631 } 632 } 633 634 class UnregisteredClassesDuplicationChecker : StackObj { 635 GrowableArray<InstanceKlass*> _list; 636 Thread* _thread; 637 public: 638 UnregisteredClassesDuplicationChecker() : _thread(Thread::current()) {} 639 640 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 641 if (!SystemDictionaryShared::is_builtin(k)) { 642 _list.append(k); 643 } 644 } 645 646 static int compare_by_loader(InstanceKlass** a, InstanceKlass** b) { 647 ClassLoaderData* loader_a = a[0]->class_loader_data(); 648 ClassLoaderData* loader_b = b[0]->class_loader_data(); 649 650 if (loader_a != loader_b) { 651 return primitive_compare(loader_a, loader_b); 652 } else { 653 return primitive_compare(a[0], b[0]); 654 } 655 } 656 657 void mark_duplicated_classes() { 658 // Two loaders may load two identical or similar hierarchies of classes. If we 659 // check for duplication in random order, we may end up excluding important base classes 660 // in both hierarchies, causing most of the classes to be excluded. 661 // We sort the classes by their loaders. This way we're likely to archive 662 // all classes in the one of the two hierarchies. 663 _list.sort(compare_by_loader); 664 for (int i = 0; i < _list.length(); i++) { 665 InstanceKlass* k = _list.at(i); 666 bool i_am_first = SystemDictionaryShared::add_unregistered_class(_thread, k); 667 if (!i_am_first) { 668 SystemDictionaryShared::warn_excluded(k, "Duplicated unregistered class"); 669 SystemDictionaryShared::set_excluded_locked(k); 670 } 671 } 672 } 673 }; 674 675 void SystemDictionaryShared::scan_constant_pool(InstanceKlass* k) { 676 k->constants()->find_archivable_hidden_classes(); 677 } 678 679 bool SystemDictionaryShared::should_hidden_class_be_archived(InstanceKlass* k) { 680 assert(k->is_hidden(), "sanity"); 681 if (is_registered_lambda_proxy_class(k)) { 682 return true; 683 } 684 685 if (CDSConfig::is_dumping_invokedynamic()) { 686 if (HeapShared::is_archivable_hidden_klass(k)) { 687 return true; 688 } 689 690 // TODO: merge the following with HeapShared::is_archivable_hidden_klass() 691 DumpTimeClassInfo* info = _dumptime_table->get(k); 692 if (info != nullptr && info->is_required()) { 693 return true; 694 } 695 } 696 697 return false; 698 } 699 700 void SystemDictionaryShared::check_excluded_classes() { 701 assert(!class_loading_may_happen(), "class loading must be disabled"); 702 assert_lock_strong(DumpTimeTable_lock); 703 704 if (CDSConfig::is_dumping_dynamic_archive()) { 705 // Do this first -- if a base class is excluded due to duplication, 706 // all of its subclasses will also be excluded. 707 ResourceMark rm; 708 UnregisteredClassesDuplicationChecker dup_checker; 709 _dumptime_table->iterate_all_live_classes(&dup_checker); 710 dup_checker.mark_duplicated_classes(); 711 } 712 713 ResourceMark rm; 714 715 // First, scan all non-hidden classes 716 auto check_non_hidden = [&] (InstanceKlass* k, DumpTimeClassInfo& info) { 717 if (!k->is_hidden()) { 718 SystemDictionaryShared::check_for_exclusion(k, &info); 719 if (!info.is_excluded() && !info.has_scanned_constant_pool()) { 720 scan_constant_pool(k); 721 info.set_has_scanned_constant_pool(); 722 } 723 } 724 }; 725 _dumptime_table->iterate_all_live_classes(check_non_hidden); 726 727 // Then, scan all that hidden classes that have been marked as required, until 728 // we reach a stable state. 729 bool made_progress; 730 do { 731 made_progress = false; 732 auto check_hidden = [&] (InstanceKlass* k, DumpTimeClassInfo& info) { 733 if (k->is_hidden() && should_hidden_class_be_archived(k)) { 734 SystemDictionaryShared::check_for_exclusion(k, &info); 735 if (info.is_excluded()) { 736 assert(!info.is_required(), "A required hidden class cannot be marked as excluded"); 737 } else if (!info.has_scanned_constant_pool()) { 738 scan_constant_pool(k); 739 info.set_has_scanned_constant_pool(); 740 // The CP entries in k *MAY* refer to other hidden classes, so scan 741 // every hidden class again. 742 made_progress = true; 743 } 744 } 745 }; 746 _dumptime_table->iterate_all_live_classes(check_hidden); 747 } while (made_progress); 748 749 // Now, all hidden classes that have not yet been scanned should be excluded 750 auto exclude_remaining_hidden = [&] (InstanceKlass* k, DumpTimeClassInfo& info) { 751 if (k->is_hidden() && !info.has_checked_exclusion()) { 752 SystemDictionaryShared::check_for_exclusion(k, &info); 753 assert(info.is_excluded(), "Must be"); 754 } 755 }; 756 _dumptime_table->iterate_all_live_classes(exclude_remaining_hidden); 757 _dumptime_table->update_counts(); 758 759 cleanup_lambda_proxy_class_dictionary(); 760 761 cleanup_method_info_dictionary(); 762 763 TrainingData::cleanup_training_data(); 764 } 765 766 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) { 767 assert(!class_loading_may_happen(), "class loading must be disabled"); 768 assert_lock_strong(DumpTimeTable_lock); 769 assert(CDSConfig::is_dumping_archive(), "sanity"); 770 DumpTimeClassInfo* p = get_info_locked(k); 771 return p->is_excluded(); 772 } 773 774 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) { 775 assert_lock_strong(DumpTimeTable_lock); 776 assert(CDSConfig::is_dumping_archive(), "sanity"); 777 DumpTimeClassInfo* info = get_info_locked(k); 778 info->set_excluded(); 779 } 780 781 void SystemDictionaryShared::set_excluded(InstanceKlass* k) { 782 assert(CDSConfig::is_dumping_archive(), "sanity"); 783 DumpTimeClassInfo* info = get_info(k); 784 info->set_excluded(); 785 } 786 787 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) { 788 assert(CDSConfig::is_dumping_archive(), "sanity"); 789 DumpTimeClassInfo* p = get_info(ik); 790 p->set_failed_verification(); 791 } 792 793 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) { 794 assert(CDSConfig::is_dumping_archive(), "sanity"); 795 DumpTimeClassInfo* p = _dumptime_table->get(ik); 796 return (p == nullptr) ? false : p->failed_verification(); 797 } 798 799 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) { 800 assert_lock_strong(DumpTimeTable_lock); 801 802 auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) { 803 if (CDSConfig::is_dumping_final_static_archive() && !k->is_loaded()) { 804 assert(k->is_shared_unregistered_class(), "must be"); 805 info.metaspace_pointers_do(it); 806 } else if (k->is_loader_alive() && !info.is_excluded()) { 807 info.metaspace_pointers_do(it); 808 } 809 }; 810 _dumptime_table->iterate_all_live_classes(do_klass); 811 812 auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 813 if (key.caller_ik()->is_loader_alive()) { 814 info.metaspace_pointers_do(it); 815 key.metaspace_pointers_do(it); 816 } 817 }; 818 _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda); 819 820 auto do_method_info = [&] (MethodDataKey& key, DumpTimeMethodDataInfo& info) { 821 info.metaspace_pointers_do(it); 822 key.metaspace_pointers_do(it); 823 }; 824 _dumptime_method_info_dictionary->iterate_all(do_method_info); 825 } 826 827 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name, 828 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) { 829 assert(CDSConfig::is_dumping_archive(), "sanity"); 830 if (CDSConfig::is_dumping_dynamic_archive() && k->is_shared()) { 831 // k is a new class in the static archive, but one of its supertypes is an old class, so k wasn't 832 // verified during dump time. No need to record constraints as k won't be included in the dynamic archive. 833 return false; 834 } 835 if (PreloadSharedClasses && is_builtin(k)) { 836 // There's no need to save verification constraints 837 return false; 838 } 839 840 DumpTimeClassInfo* info = get_info(k); 841 info->add_verification_constraint(k, name, from_name, from_field_is_protected, 842 from_is_array, from_is_object); 843 844 if (CDSConfig::is_dumping_dynamic_archive()) { 845 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during 846 // the initial run prior to creating the archive before vm exit. We will also perform verification 847 // check when running with the archive. 848 return false; 849 } else { 850 if (is_builtin(k)) { 851 // For builtin class loaders, we can try to complete the verification check at dump time, 852 // because we can resolve all the constraint classes. We will also perform verification check 853 // when running with the archive. 854 return false; 855 } else { 856 // For non-builtin class loaders, we cannot complete the verification check at dump time, 857 // because at dump time we don't know how to resolve classes for such loaders. 858 return true; 859 } 860 } 861 } 862 863 void SystemDictionaryShared::add_enum_klass_static_field(InstanceKlass* ik, int root_index) { 864 assert(CDSConfig::is_dumping_heap(), "sanity"); 865 DumpTimeClassInfo* info = get_info_locked(ik); 866 info->add_enum_klass_static_field(root_index); 867 } 868 869 void SystemDictionaryShared::add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey& key, 870 InstanceKlass* proxy_klass) { 871 assert_lock_strong(DumpTimeTable_lock); 872 873 bool created; 874 DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created); 875 info->add_proxy_klass(proxy_klass); 876 if (created) { 877 ++_dumptime_lambda_proxy_class_dictionary->_count; 878 } 879 } 880 881 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik, 882 InstanceKlass* lambda_ik, 883 Symbol* invoked_name, 884 Symbol* invoked_type, 885 Symbol* method_type, 886 Method* member_method, 887 Symbol* instantiated_method_type, 888 TRAPS) { 889 if (CDSConfig::is_dumping_invokedynamic()) { 890 // The proxy classes will be accessible through the archived CP entries. 891 return; 892 } 893 if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) { 894 // TODO: not supported in new workflow 895 return; 896 } 897 898 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader"); 899 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data"); 900 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data"); 901 902 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 903 904 lambda_ik->assign_class_loader_type(); 905 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index()); 906 InstanceKlass* nest_host = caller_ik->nest_host(CHECK); 907 assert(nest_host != nullptr, "unexpected nullptr nest_host"); 908 909 DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik); 910 if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik) 911 // Don't include the lambda proxy if its nest host is not in the "linked" state. 912 && nest_host->is_linked()) { 913 // Set _is_archived_lambda_proxy in DumpTimeClassInfo so that the lambda_ik 914 // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses. 915 info->_is_archived_lambda_proxy = true; 916 info->set_nest_host(nest_host); 917 918 LambdaProxyClassKey key(caller_ik, 919 invoked_name, 920 invoked_type, 921 method_type, 922 member_method, 923 instantiated_method_type); 924 add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik); 925 } 926 } 927 928 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik, 929 Symbol* invoked_name, 930 Symbol* invoked_type, 931 Symbol* method_type, 932 Method* member_method, 933 Symbol* instantiated_method_type) { 934 if (CDSConfig::is_dumping_final_static_archive()) { 935 return nullptr; 936 } 937 MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag); 938 LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type, 939 method_type, member_method, instantiated_method_type); 940 941 // Try to retrieve the lambda proxy class from static archive. 942 const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key); 943 InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info); 944 if (proxy_klass == nullptr) { 945 if (info != nullptr && log_is_enabled(Debug, cds)) { 946 ResourceMark rm; 947 log_debug(cds)("Used all static archived lambda proxy classes for: %s %s%s", 948 caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string()); 949 } 950 } else { 951 return proxy_klass; 952 } 953 954 // Retrieving from static archive is unsuccessful, try dynamic archive. 955 info = _dynamic_archive.lookup_lambda_proxy_class(&key); 956 proxy_klass = retrieve_lambda_proxy_class(info); 957 if (proxy_klass == nullptr) { 958 if (info != nullptr && log_is_enabled(Debug, cds)) { 959 ResourceMark rm; 960 log_debug(cds)("Used all dynamic archived lambda proxy classes for: %s %s%s", 961 caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string()); 962 } 963 } 964 return proxy_klass; 965 } 966 967 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) { 968 InstanceKlass* proxy_klass = nullptr; 969 if (info != nullptr) { 970 InstanceKlass* curr_klass = info->proxy_klass_head(); 971 InstanceKlass* prev_klass = curr_klass; 972 if (curr_klass->lambda_proxy_is_available()) { 973 while (curr_klass->next_link() != nullptr) { 974 prev_klass = curr_klass; 975 curr_klass = InstanceKlass::cast(curr_klass->next_link()); 976 } 977 assert(curr_klass->is_hidden(), "must be"); 978 assert(curr_klass->lambda_proxy_is_available(), "must be"); 979 980 prev_klass->set_next_link(nullptr); 981 proxy_klass = curr_klass; 982 proxy_klass->clear_lambda_proxy_is_available(); 983 if (log_is_enabled(Debug, cds)) { 984 ResourceMark rm; 985 log_debug(cds)("Loaded lambda proxy: " PTR_FORMAT " %s ", p2i(proxy_klass), proxy_klass->external_name()); 986 } 987 } 988 } 989 return proxy_klass; 990 } 991 992 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) { 993 assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 994 RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik); 995 return record->nest_host(); 996 } 997 998 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik, 999 InstanceKlass* caller_ik, TRAPS) { 1000 Handle class_loader(THREAD, caller_ik->class_loader()); 1001 Handle protection_domain; 1002 PackageEntry* pkg_entry = caller_ik->package(); 1003 if (caller_ik->class_loader() != nullptr) { 1004 protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL); 1005 } 1006 1007 InstanceKlass* shared_nest_host = get_shared_nest_host(lambda_ik); 1008 assert(shared_nest_host != nullptr, "unexpected nullptr _nest_host"); 1009 1010 InstanceKlass* loaded_lambda = 1011 SystemDictionary::load_shared_lambda_proxy_class(lambda_ik, class_loader, protection_domain, pkg_entry, CHECK_NULL); 1012 1013 if (loaded_lambda == nullptr) { 1014 return nullptr; 1015 } 1016 1017 // Ensures the nest host is the same as the lambda proxy's 1018 // nest host recorded at dump time. 1019 InstanceKlass* nest_host = caller_ik->nest_host(THREAD); 1020 assert(nest_host == shared_nest_host, "mismatched nest host"); 1021 1022 EventClassLoad class_load_start_event; 1023 1024 // Add to class hierarchy, and do possible deoptimizations. 1025 loaded_lambda->add_to_hierarchy(THREAD); 1026 // But, do not add to dictionary. 1027 1028 loaded_lambda->link_class(CHECK_NULL); 1029 // notify jvmti 1030 if (JvmtiExport::should_post_class_load()) { 1031 JvmtiExport::post_class_load(THREAD, loaded_lambda); 1032 } 1033 if (class_load_start_event.should_commit()) { 1034 SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader())); 1035 } 1036 1037 loaded_lambda->initialize(CHECK_NULL); 1038 1039 return loaded_lambda; 1040 } 1041 1042 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass, 1043 TRAPS) { 1044 //assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 1045 RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass); 1046 1047 int length = record->_num_verifier_constraints; 1048 if (length > 0) { 1049 for (int i = 0; i < length; i++) { 1050 RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i); 1051 Symbol* name = vc->name(); 1052 Symbol* from_name = vc->from_name(); 1053 char c = record->verifier_constraint_flag(i); 1054 1055 if (log_is_enabled(Trace, cds, verification)) { 1056 ResourceMark rm(THREAD); 1057 log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]", 1058 klass->external_name(), from_name->as_klass_external_name(), 1059 name->as_klass_external_name(), c); 1060 } 1061 1062 bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false; 1063 bool from_is_array = (c & SystemDictionaryShared::FROM_IS_ARRAY) ? true : false; 1064 bool from_is_object = (c & SystemDictionaryShared::FROM_IS_OBJECT) ? true : false; 1065 1066 bool ok = VerificationType::resolve_and_check_assignability(klass, name, 1067 from_name, from_field_is_protected, from_is_array, from_is_object, CHECK); 1068 if (!ok) { 1069 ResourceMark rm(THREAD); 1070 stringStream ss; 1071 1072 ss.print_cr("Bad type on operand stack"); 1073 ss.print_cr("Exception Details:"); 1074 ss.print_cr(" Location:\n %s", klass->name()->as_C_string()); 1075 ss.print_cr(" Reason:\n Type '%s' is not assignable to '%s'", 1076 from_name->as_quoted_ascii(), name->as_quoted_ascii()); 1077 THROW_MSG(vmSymbols::java_lang_VerifyError(), ss.as_string()); 1078 } 1079 } 1080 } 1081 } 1082 1083 static oop get_class_loader_by(char type) { 1084 if (type == (char)ClassLoader::BOOT_LOADER) { 1085 return (oop)nullptr; 1086 } else if (type == (char)ClassLoader::PLATFORM_LOADER) { 1087 return SystemDictionary::java_platform_loader(); 1088 } else { 1089 assert (type == (char)ClassLoader::APP_LOADER, "Sanity"); 1090 return SystemDictionary::java_system_loader(); 1091 } 1092 } 1093 1094 // Record class loader constraints that are checked inside 1095 // InstanceKlass::link_class(), so that these can be checked quickly 1096 // at runtime without laying out the vtable/itables. 1097 void SystemDictionaryShared::record_linking_constraint(Symbol* name, InstanceKlass* klass, 1098 Handle loader1, Handle loader2) { 1099 // A linking constraint check is executed when: 1100 // - klass extends or implements type S 1101 // - klass overrides method S.M(...) with X.M 1102 // - If klass defines the method M, X is 1103 // the same as klass. 1104 // - If klass does not define the method M, 1105 // X must be a supertype of klass and X.M is 1106 // a default method defined by X. 1107 // - loader1 = X->class_loader() 1108 // - loader2 = S->class_loader() 1109 // - loader1 != loader2 1110 // - M's parameter(s) include an object type T 1111 // We require that 1112 // - whenever loader1 and loader2 try to 1113 // resolve the type T, they must always resolve to 1114 // the same InstanceKlass. 1115 // NOTE: type T may or may not be currently resolved in 1116 // either of these two loaders. The check itself does not 1117 // try to resolve T. 1118 oop klass_loader = klass->class_loader(); 1119 1120 if (!is_system_class_loader(klass_loader) && 1121 !is_platform_class_loader(klass_loader)) { 1122 // If klass is loaded by system/platform loaders, we can 1123 // guarantee that klass and S must be loaded by the same 1124 // respective loader between dump time and run time, and 1125 // the exact same check on (name, loader1, loader2) will 1126 // be executed. Hence, we can cache this check and execute 1127 // it at runtime without walking the vtable/itables. 1128 // 1129 // This cannot be guaranteed for classes loaded by other 1130 // loaders, so we bail. 1131 return; 1132 } 1133 1134 assert(is_builtin(klass), "must be"); 1135 assert(klass_loader != nullptr, "should not be called for boot loader"); 1136 assert(loader1 != loader2, "must be"); 1137 1138 if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) { 1139 // We are re-laying out the vtable/itables of the *copy* of 1140 // a class during the final stage of dynamic dumping. The 1141 // linking constraints for this class has already been recorded. 1142 return; 1143 } 1144 assert(!Thread::current()->is_VM_thread(), "must be"); 1145 1146 assert(CDSConfig::is_dumping_archive(), "sanity"); 1147 DumpTimeClassInfo* info = get_info(klass); 1148 info->record_linking_constraint(name, loader1, loader2); 1149 } 1150 1151 // returns true IFF there's no need to re-initialize the i/v-tables for klass for 1152 // the purpose of checking class loader constraints. 1153 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) { 1154 //assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only"); 1155 LogTarget(Info, class, loader, constraints) log; 1156 if (klass->is_shared_boot_class()) { 1157 // No class loader constraint check performed for boot classes. 1158 return true; 1159 } 1160 if (klass->is_shared_platform_class() || klass->is_shared_app_class()) { 1161 RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass); 1162 assert(info != nullptr, "Sanity"); 1163 if (info->_num_loader_constraints > 0) { 1164 HandleMark hm(current); 1165 for (int i = 0; i < info->_num_loader_constraints; i++) { 1166 RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i); 1167 Symbol* name = lc->constraint_name(); 1168 Handle loader1(current, get_class_loader_by(lc->_loader_type1)); 1169 Handle loader2(current, get_class_loader_by(lc->_loader_type2)); 1170 if (log.is_enabled()) { 1171 ResourceMark rm(current); 1172 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s", 1173 klass->external_name(), name->as_C_string(), 1174 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(), 1175 ClassLoaderData::class_loader_data(loader2())->loader_name_and_id()); 1176 } 1177 if (!SystemDictionary::add_loader_constraint(name, klass, loader1, loader2)) { 1178 // Loader constraint violation has been found. The caller 1179 // will re-layout the vtable/itables to produce the correct 1180 // exception. 1181 if (log.is_enabled()) { 1182 log.print(" failed]"); 1183 } 1184 return false; 1185 } 1186 if (log.is_enabled()) { 1187 log.print(" succeeded]"); 1188 } 1189 } 1190 return true; // for all recorded constraints added successfully. 1191 } 1192 } 1193 if (log.is_enabled()) { 1194 ResourceMark rm(current); 1195 log.print("[CDS has not recorded loader constraint for class %s]", klass->external_name()); 1196 } 1197 return false; 1198 } 1199 1200 bool SystemDictionaryShared::is_supported_invokedynamic(BootstrapInfo* bsi) { 1201 LogTarget(Debug, cds, lambda) log; 1202 if (bsi->arg_values() == nullptr || !bsi->arg_values()->is_objArray()) { 1203 if (log.is_enabled()) { 1204 LogStream log_stream(log); 1205 log.print("bsi check failed"); 1206 log.print(" bsi->arg_values().not_null() %d", bsi->arg_values().not_null()); 1207 if (bsi->arg_values().not_null()) { 1208 log.print(" bsi->arg_values()->is_objArray() %d", bsi->arg_values()->is_objArray()); 1209 bsi->print_msg_on(&log_stream); 1210 } 1211 } 1212 return false; 1213 } 1214 1215 Handle bsm = bsi->bsm(); 1216 if (bsm.is_null() || !java_lang_invoke_DirectMethodHandle::is_instance(bsm())) { 1217 if (log.is_enabled()) { 1218 log.print("bsm check failed"); 1219 log.print(" bsm.is_null() %d", bsm.is_null()); 1220 log.print(" java_lang_invoke_DirectMethodHandle::is_instance(bsm()) %d", 1221 java_lang_invoke_DirectMethodHandle::is_instance(bsm())); 1222 } 1223 return false; 1224 } 1225 1226 oop mn = java_lang_invoke_DirectMethodHandle::member(bsm()); 1227 Method* method = java_lang_invoke_MemberName::vmtarget(mn); 1228 if (method->klass_name()->equals("java/lang/invoke/LambdaMetafactory") && 1229 method->name()->equals("metafactory") && 1230 method->signature()->equals("(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;" 1231 "Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;" 1232 "Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;")) { 1233 return true; 1234 } else { 1235 if (log.is_enabled()) { 1236 ResourceMark rm; 1237 log.print("method check failed"); 1238 log.print(" klass_name() %s", method->klass_name()->as_C_string()); 1239 log.print(" name() %s", method->name()->as_C_string()); 1240 log.print(" signature() %s", method->signature()->as_C_string()); 1241 } 1242 } 1243 1244 return false; 1245 } 1246 1247 class EstimateSizeForArchive : StackObj { 1248 size_t _shared_class_info_size; 1249 int _num_builtin_klasses; 1250 int _num_unregistered_klasses; 1251 1252 public: 1253 EstimateSizeForArchive() { 1254 _shared_class_info_size = 0; 1255 _num_builtin_klasses = 0; 1256 _num_unregistered_klasses = 0; 1257 } 1258 1259 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 1260 if (!info.is_excluded()) { 1261 size_t byte_size = info.runtime_info_bytesize(); 1262 _shared_class_info_size += align_up(byte_size, SharedSpaceObjectAlignment); 1263 } 1264 } 1265 1266 size_t total() { 1267 return _shared_class_info_size; 1268 } 1269 }; 1270 1271 size_t SystemDictionaryShared::estimate_size_for_archive() { 1272 EstimateSizeForArchive est; 1273 _dumptime_table->iterate_all_live_classes(&est); 1274 size_t total_size = est.total() + 1275 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) + 1276 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false)); 1277 1278 size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment); 1279 total_size += 1280 (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) + 1281 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count); 1282 1283 size_t method_info_byte_size = align_up(sizeof(RunTimeMethodDataInfo), SharedSpaceObjectAlignment); 1284 total_size += 1285 (method_info_byte_size * _dumptime_method_info_dictionary->_count) + 1286 CompactHashtableWriter::estimate_size(_dumptime_method_info_dictionary->_count); 1287 1288 return total_size; 1289 } 1290 1291 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) { 1292 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(ptr)) { 1293 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr); 1294 unsigned int hash = primitive_hash<uintx>(offset); 1295 DEBUG_ONLY({ 1296 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) { 1297 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be"); 1298 } 1299 }); 1300 return hash; 1301 } else { 1302 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr); 1303 } 1304 } 1305 1306 class CopyLambdaProxyClassInfoToArchive : StackObj { 1307 CompactHashtableWriter* _writer; 1308 ArchiveBuilder* _builder; 1309 public: 1310 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer) 1311 : _writer(writer), _builder(ArchiveBuilder::current()) {} 1312 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 1313 // In static dump, info._proxy_klasses->at(0) is already relocated to point to the archived class 1314 // (not the original class). 1315 // 1316 // The following check has been moved to SystemDictionaryShared::check_excluded_classes(), which 1317 // happens before the classes are copied. 1318 // 1319 // if (SystemDictionaryShared::is_excluded_class(info._proxy_klasses->at(0))) { 1320 // return true; 1321 //} 1322 ResourceMark rm; 1323 log_info(cds,dynamic)("Archiving hidden %s", info._proxy_klasses->at(0)->external_name()); 1324 size_t byte_size = sizeof(RunTimeLambdaProxyClassInfo); 1325 RunTimeLambdaProxyClassInfo* runtime_info = 1326 (RunTimeLambdaProxyClassInfo*)ArchiveBuilder::ro_region_alloc(byte_size); 1327 runtime_info->init(key, info); 1328 unsigned int hash = runtime_info->hash(); 1329 u4 delta = _builder->any_to_offset_u4((void*)runtime_info); 1330 _writer->add(hash, delta); 1331 return true; 1332 } 1333 }; 1334 1335 class AdjustLambdaProxyClassInfo : StackObj { 1336 public: 1337 AdjustLambdaProxyClassInfo() {} 1338 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 1339 int len = info._proxy_klasses->length(); 1340 InstanceKlass* last_buff_k = nullptr; 1341 1342 for (int i = len - 1; i >= 0; i--) { 1343 InstanceKlass* orig_k = info._proxy_klasses->at(i); 1344 InstanceKlass* buff_k = ArchiveBuilder::current()->get_buffered_addr(orig_k); 1345 assert(ArchiveBuilder::current()->is_in_buffer_space(buff_k), "must be"); 1346 buff_k->set_lambda_proxy_is_available(); 1347 buff_k->set_next_link(last_buff_k); 1348 if (last_buff_k != nullptr) { 1349 ArchivePtrMarker::mark_pointer(buff_k->next_link_addr()); 1350 } 1351 last_buff_k = buff_k; 1352 } 1353 1354 return true; 1355 } 1356 }; 1357 1358 class CopySharedClassInfoToArchive : StackObj { 1359 CompactHashtableWriter* _writer; 1360 bool _is_builtin; 1361 ArchiveBuilder *_builder; 1362 public: 1363 CopySharedClassInfoToArchive(CompactHashtableWriter* writer, 1364 bool is_builtin) 1365 : _writer(writer), _is_builtin(is_builtin), _builder(ArchiveBuilder::current()) {} 1366 1367 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) { 1368 if (!info.is_excluded() && info.is_builtin() == _is_builtin) { 1369 size_t byte_size = info.runtime_info_bytesize(); 1370 RunTimeClassInfo* record; 1371 record = (RunTimeClassInfo*)ArchiveBuilder::ro_region_alloc(byte_size); 1372 record->init(info); 1373 1374 unsigned int hash; 1375 Symbol* name = info._klass->name(); 1376 name = ArchiveBuilder::current()->get_buffered_addr(name); 1377 hash = SystemDictionaryShared::hash_for_shared_dictionary((address)name); 1378 u4 delta = _builder->buffer_to_offset_u4((address)record); 1379 if (_is_builtin && info._klass->is_hidden()) { 1380 // skip 1381 } else { 1382 _writer->add(hash, delta); 1383 } 1384 if (log_is_enabled(Trace, cds, hashtables)) { 1385 ResourceMark rm; 1386 log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name()); 1387 } 1388 1389 // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo* 1390 InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass); 1391 RunTimeClassInfo::set_for(buffered_klass, record); 1392 } 1393 } 1394 }; 1395 1396 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) { 1397 CompactHashtableStats stats; 1398 dictionary->reset(); 1399 CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats); 1400 CopyLambdaProxyClassInfoToArchive copy(&writer); 1401 _dumptime_lambda_proxy_class_dictionary->iterate(©); 1402 writer.dump(dictionary, "lambda proxy class dictionary"); 1403 } 1404 1405 class CopyMethodDataInfoToArchive : StackObj { 1406 CompactHashtableWriter* _writer; 1407 ArchiveBuilder* _builder; 1408 public: 1409 CopyMethodDataInfoToArchive(CompactHashtableWriter* writer) 1410 : _writer(writer), _builder(ArchiveBuilder::current()) {} 1411 1412 bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) { 1413 Method* holder = key.method(); 1414 log_info(cds,dynamic)("Archiving method info for %s", holder->external_name()); 1415 1416 size_t byte_size = sizeof(RunTimeMethodDataInfo); 1417 RunTimeMethodDataInfo* record = (RunTimeMethodDataInfo*)ArchiveBuilder::ro_region_alloc(byte_size); 1418 1419 DumpTimeMethodDataInfo data(info.method_data(), info.method_counters()); 1420 record->init(key, data); 1421 1422 uint hash = SystemDictionaryShared::hash_for_shared_dictionary((address)holder); 1423 u4 delta = _builder->buffer_to_offset_u4((address)record); 1424 _writer->add(hash, delta); 1425 1426 return true; 1427 } 1428 }; 1429 1430 void SystemDictionaryShared::write_method_info_dictionary(MethodDataInfoDictionary* dictionary) { 1431 CompactHashtableStats stats; 1432 dictionary->reset(); 1433 CompactHashtableWriter writer(_dumptime_method_info_dictionary->_count, &stats); 1434 CopyMethodDataInfoToArchive copy(&writer); 1435 _dumptime_method_info_dictionary->iterate(©); 1436 writer.dump(dictionary, "method info dictionary"); 1437 } 1438 1439 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary, 1440 bool is_builtin) { 1441 CompactHashtableStats stats; 1442 dictionary->reset(); 1443 CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats); 1444 CopySharedClassInfoToArchive copy(&writer, is_builtin); 1445 assert_lock_strong(DumpTimeTable_lock); 1446 _dumptime_table->iterate_all_live_classes(©); 1447 writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary"); 1448 } 1449 1450 void SystemDictionaryShared::write_to_archive(bool is_static_archive) { 1451 ArchiveInfo* archive = get_archive(is_static_archive); 1452 1453 write_dictionary(&archive->_builtin_dictionary, true); 1454 write_dictionary(&archive->_unregistered_dictionary, false); 1455 1456 write_lambda_proxy_class_dictionary(&archive->_lambda_proxy_class_dictionary); 1457 1458 write_method_info_dictionary(&archive->_method_info_dictionary); 1459 } 1460 1461 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() { 1462 AdjustLambdaProxyClassInfo adjuster; 1463 _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster); 1464 } 1465 1466 class AdjustMethodInfo : StackObj { 1467 public: 1468 AdjustMethodInfo() {} 1469 bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) { 1470 // TODO: is it possible for the data to become stale/invalid? 1471 MethodData* md = info.method_data(); 1472 MethodCounters* mc = info.method_counters(); 1473 if (md != nullptr) { 1474 md = ArchiveBuilder::current()->get_buffered_addr(md); 1475 } 1476 if (mc != nullptr) { 1477 mc = ArchiveBuilder::current()->get_buffered_addr(mc); 1478 } 1479 assert(ArchiveBuilder::current()->is_in_buffer_space(md) || md == nullptr, "must be"); 1480 assert(ArchiveBuilder::current()->is_in_buffer_space(mc) || mc == nullptr, "must be"); 1481 if (md != nullptr) { 1482 md->remove_unshareable_info(); 1483 } 1484 if (mc != nullptr) { 1485 mc->remove_unshareable_info(); 1486 } 1487 return true; 1488 } 1489 }; 1490 1491 void SystemDictionaryShared::adjust_method_info_dictionary() { 1492 AdjustMethodInfo adjuster; 1493 _dumptime_method_info_dictionary->iterate(&adjuster); 1494 } 1495 1496 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc, 1497 bool is_static_archive) { 1498 ArchiveInfo* archive = get_archive(is_static_archive); 1499 1500 archive->_builtin_dictionary.serialize_header(soc); 1501 archive->_unregistered_dictionary.serialize_header(soc); 1502 archive->_lambda_proxy_class_dictionary.serialize_header(soc); 1503 archive->_method_info_dictionary.serialize_header(soc); 1504 } 1505 1506 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) { 1507 for (auto id : EnumRange<vmClassID>{}) { 1508 soc->do_ptr(vmClasses::klass_addr_at(id)); 1509 } 1510 soc->do_ptr((void**)&_archived_lambda_form_classes); 1511 soc->do_ptr((void**)&_archived_lambda_proxy_classes_boot); 1512 soc->do_ptr((void**)&_archived_lambda_proxy_classes_boot2); 1513 soc->do_ptr((void**)&_archived_lambda_proxy_classes_platform); 1514 soc->do_ptr((void**)&_archived_lambda_proxy_classes_app); 1515 } 1516 1517 const RunTimeClassInfo* 1518 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) { 1519 if (!CDSConfig::is_using_archive() || !name->is_shared()) { 1520 // The names of all shared classes must also be a shared Symbol. 1521 return nullptr; 1522 } 1523 1524 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name); 1525 const RunTimeClassInfo* record = nullptr; 1526 if (DynamicArchive::is_mapped()) { 1527 // Use the regenerated holder classes in the dynamic archive as they 1528 // have more methods than those in the base archive. 1529 if (LambdaFormInvokers::may_be_regenerated_class(name)) { 1530 record = dynamic_dict->lookup(name, hash, 0); 1531 if (record != nullptr) { 1532 return record; 1533 } 1534 } 1535 } 1536 1537 if (!MetaspaceShared::is_shared_dynamic(name)) { 1538 // The names of all shared classes in the static dict must also be in the 1539 // static archive 1540 record = static_dict->lookup(name, hash, 0); 1541 } 1542 1543 if (record == nullptr && DynamicArchive::is_mapped()) { 1544 record = dynamic_dict->lookup(name, hash, 0); 1545 } 1546 1547 return record; 1548 } 1549 1550 InstanceKlass* SystemDictionaryShared::find_builtin_class(Symbol* name) { 1551 const RunTimeClassInfo* record = find_record(&_static_archive._builtin_dictionary, 1552 &_dynamic_archive._builtin_dictionary, 1553 name); 1554 if (record != nullptr) { 1555 assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name"); 1556 assert(check_alignment(record->_klass), "Address not aligned"); 1557 // We did not save the classfile data of the generated LambdaForm invoker classes, 1558 // so we cannot support CLFH for such classes. 1559 if (record->_klass->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) { 1560 return nullptr; 1561 } 1562 return record->_klass; 1563 } else { 1564 return nullptr; 1565 } 1566 } 1567 1568 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) { 1569 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)"); 1570 DumpTimeClassInfo* info = get_info(k); 1571 info->_id = id; 1572 } 1573 1574 const char* SystemDictionaryShared::class_loader_name_for_shared(Klass* k) { 1575 assert(k != nullptr, "Sanity"); 1576 assert(k->is_shared(), "Must be"); 1577 assert(k->is_instance_klass(), "Must be"); 1578 InstanceKlass* ik = InstanceKlass::cast(k); 1579 if (ik->is_shared_boot_class()) { 1580 return "boot_loader"; 1581 } else if (ik->is_shared_platform_class()) { 1582 return "platform_loader"; 1583 } else if (ik->is_shared_app_class()) { 1584 return "app_loader"; 1585 } else if (ik->is_shared_unregistered_class()) { 1586 return "unregistered_loader"; 1587 } else { 1588 return "unknown loader"; 1589 } 1590 } 1591 1592 class SharedDictionaryPrinter : StackObj { 1593 outputStream* _st; 1594 int _index; 1595 public: 1596 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {} 1597 1598 void do_value(const RunTimeClassInfo* record) { 1599 ResourceMark rm; 1600 _st->print_cr("%4d: %s %s", _index++, record->_klass->external_name(), 1601 SystemDictionaryShared::class_loader_name_for_shared(record->_klass)); 1602 if (record->_klass->array_klasses() != nullptr) { 1603 record->_klass->array_klasses()->cds_print_value_on(_st); 1604 _st->cr(); 1605 } 1606 } 1607 int index() const { return _index; } 1608 }; 1609 1610 class SharedLambdaDictionaryPrinter : StackObj { 1611 outputStream* _st; 1612 int _index; 1613 public: 1614 SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {} 1615 1616 void do_value(const RunTimeLambdaProxyClassInfo* record) { 1617 if (record->proxy_klass_head()->lambda_proxy_is_available()) { 1618 ResourceMark rm; 1619 Klass* k = record->proxy_klass_head(); 1620 while (k != nullptr) { 1621 _st->print_cr("%4d: %s %s", _index++, k->external_name(), 1622 SystemDictionaryShared::class_loader_name_for_shared(k)); 1623 k = k->next_link(); 1624 } 1625 } 1626 } 1627 }; 1628 1629 class SharedMethodInfoDictionaryPrinter : StackObj { 1630 outputStream* _st; 1631 int _index; 1632 1633 private: 1634 static const char* tag(void* p) { 1635 if (p == nullptr) { 1636 return " "; 1637 } else if (MetaspaceShared::is_shared_dynamic(p)) { 1638 return "<D>"; 1639 } else if (MetaspaceShared::is_in_shared_metaspace(p)) { 1640 return "<S>"; 1641 } else { 1642 return "???"; 1643 } 1644 } 1645 public: 1646 SharedMethodInfoDictionaryPrinter(outputStream* st) : _st(st), _index(0) {} 1647 1648 void do_value(const RunTimeMethodDataInfo* record) { 1649 ResourceMark rm; 1650 Method* m = record->method(); 1651 MethodCounters* mc = record->method_counters(); 1652 MethodData* md = record->method_data(); 1653 1654 _st->print_cr("%4d: %s" PTR_FORMAT " %s" PTR_FORMAT " %s" PTR_FORMAT " %s", _index++, 1655 tag(m), p2i(m), 1656 tag(mc), p2i(mc), 1657 tag(md), p2i(md), 1658 m->external_name()); 1659 if (Verbose) { 1660 if (mc != nullptr) { 1661 mc->print_on(_st); 1662 } 1663 if (md != nullptr) { 1664 md->print_on(_st); 1665 } 1666 _st->cr(); 1667 } 1668 } 1669 }; 1670 1671 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix, 1672 outputStream* st) { 1673 st->print_cr("%sShared Dictionary", prefix); 1674 SharedDictionaryPrinter p(st); 1675 st->print_cr("%sShared Builtin Dictionary", prefix); 1676 _builtin_dictionary.iterate(&p); 1677 st->print_cr("%sShared Unregistered Dictionary", prefix); 1678 _unregistered_dictionary.iterate(&p); 1679 if (!_lambda_proxy_class_dictionary.empty()) { 1680 st->print_cr("%sShared Lambda Dictionary", prefix); 1681 SharedLambdaDictionaryPrinter ldp(st, p.index()); 1682 _lambda_proxy_class_dictionary.iterate(&ldp); 1683 } 1684 if (!_method_info_dictionary.empty()) { 1685 st->print_cr("%sShared MethodData Dictionary", prefix); 1686 SharedMethodInfoDictionaryPrinter mdp(st); 1687 _method_info_dictionary.iterate(&mdp); 1688 } 1689 } 1690 1691 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix, 1692 outputStream* st) { 1693 st->print_cr("%sArchve Statistics", prefix); 1694 _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary"); 1695 _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary"); 1696 _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary"); 1697 _method_info_dictionary.print_table_statistics(st, "MethodData Dictionary"); 1698 } 1699 1700 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) { 1701 if (CDSConfig::is_using_archive()) { 1702 if (is_static) { 1703 _static_archive.print_on("", st); 1704 } else { 1705 if (DynamicArchive::is_mapped()) { 1706 _dynamic_archive.print_on("Dynamic ", st); 1707 } 1708 } 1709 } 1710 } 1711 1712 void SystemDictionaryShared::print_on(outputStream* st) { 1713 print_shared_archive(st, true); 1714 print_shared_archive(st, false); 1715 } 1716 1717 void SystemDictionaryShared::print_table_statistics(outputStream* st) { 1718 if (CDSConfig::is_using_archive()) { 1719 _static_archive.print_table_statistics("Static ", st); 1720 if (DynamicArchive::is_mapped()) { 1721 _dynamic_archive.print_table_statistics("Dynamic ", st); 1722 } 1723 } 1724 } 1725 1726 bool SystemDictionaryShared::is_dumptime_table_empty() { 1727 assert_lock_strong(DumpTimeTable_lock); 1728 _dumptime_table->update_counts(); 1729 if (_dumptime_table->count_of(true) == 0 && _dumptime_table->count_of(false) == 0){ 1730 return true; 1731 } 1732 return false; 1733 } 1734 1735 class CleanupDumpTimeLambdaProxyClassTable: StackObj { 1736 public: 1737 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) { 1738 assert_lock_strong(DumpTimeTable_lock); 1739 InstanceKlass* caller_ik = key.caller_ik(); 1740 InstanceKlass* nest_host = caller_ik->nest_host_not_null(); 1741 1742 // If the caller class and/or nest_host are excluded, the associated lambda proxy 1743 // must also be excluded. 1744 bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) || 1745 SystemDictionaryShared::check_for_exclusion(nest_host, nullptr); 1746 1747 for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) { 1748 InstanceKlass* ik = info._proxy_klasses->at(i); 1749 if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) { 1750 SystemDictionaryShared::reset_registered_lambda_proxy_class(ik); 1751 info._proxy_klasses->remove_at(i); 1752 } 1753 } 1754 return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false; 1755 } 1756 }; 1757 1758 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() { 1759 assert_lock_strong(DumpTimeTable_lock); 1760 CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes; 1761 _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes); 1762 } 1763 1764 class CleanupDumpTimeMethodInfoTable : StackObj { 1765 public: 1766 bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) { 1767 assert_lock_strong(DumpTimeTable_lock); 1768 assert(MetaspaceShared::is_in_shared_metaspace(key.method()), ""); 1769 InstanceKlass* holder = key.method()->method_holder(); 1770 bool is_excluded = SystemDictionaryShared::check_for_exclusion(holder, nullptr); 1771 return is_excluded; 1772 } 1773 }; 1774 1775 void SystemDictionaryShared::cleanup_method_info_dictionary() { 1776 assert_lock_strong(DumpTimeTable_lock); 1777 1778 CleanupDumpTimeMethodInfoTable cleanup_method_info; 1779 _dumptime_method_info_dictionary->unlink(&cleanup_method_info); 1780 } 1781 1782 void SystemDictionaryShared::create_loader_positive_lookup_cache(TRAPS) { 1783 GrowableArray<InstanceKlass*> shared_classes_list; 1784 { 1785 // With static dumping, we have only a single Java thread (see JVM_StartThread) so 1786 // no no other threads should be loading classes. Otherwise, the code below may miss some 1787 // classes that are loaded concurrently. 1788 assert(CDSConfig::is_dumping_static_archive(), "no other threads should be loading classes"); 1789 1790 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag); 1791 _dumptime_table->iterate_all_classes_in_builtin_loaders([&](InstanceKlass* k, DumpTimeClassInfo& info) { 1792 if (!k->is_hidden() && !check_for_exclusion(k, &info)) { 1793 shared_classes_list.append(k); 1794 } 1795 } 1796 ); 1797 } 1798 1799 InstanceKlass* ik = vmClasses::Class_klass(); 1800 objArrayOop r = oopFactory::new_objArray(ik, shared_classes_list.length(), CHECK); 1801 objArrayHandle array_h(THREAD, r); 1802 1803 for (int i = 0; i < shared_classes_list.length(); i++) { 1804 oop mirror = shared_classes_list.at(i)->java_mirror(); 1805 Handle mirror_h(THREAD, mirror); 1806 array_h->obj_at_put(i, mirror_h()); 1807 } 1808 1809 TempNewSymbol method = SymbolTable::new_symbol("generatePositiveLookupCache"); 1810 TempNewSymbol signature = SymbolTable::new_symbol("([Ljava/lang/Class;)V"); 1811 1812 JavaCallArguments args(Handle(THREAD, SystemDictionary::java_system_loader())); 1813 args.push_oop(array_h); 1814 JavaValue result(T_VOID); 1815 JavaCalls::call_virtual(&result, 1816 vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(), 1817 method, 1818 signature, 1819 &args, 1820 CHECK); 1821 1822 if (HAS_PENDING_EXCEPTION) { 1823 Handle exc_handle(THREAD, PENDING_EXCEPTION); 1824 CLEAR_PENDING_EXCEPTION; 1825 ResourceMark rm(THREAD); 1826 1827 log_warning(cds)("Exception during AppClassLoader::generatePositiveLookupCache() call"); 1828 LogStreamHandle(Debug, cds) log; 1829 if (log.is_enabled()) { 1830 java_lang_Throwable::print_stack_trace(exc_handle, &log); 1831 } 1832 return; 1833 } 1834 }