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