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 "cds/aotClassLocation.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/archiveUtils.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/cdsProtectionDomain.hpp"
31 #include "cds/classListParser.hpp"
32 #include "cds/classListWriter.hpp"
33 #include "cds/dumpTimeClassInfo.inline.hpp"
34 #include "cds/dynamicArchive.hpp"
35 #include "cds/filemap.hpp"
36 #include "cds/heapShared.hpp"
37 #include "cds/metaspaceShared.hpp"
38 #include "cds/runTimeClassInfo.hpp"
39 #include "cds/unregisteredClasses.hpp"
40 #include "classfile/classFileStream.hpp"
41 #include "classfile/classLoader.hpp"
42 #include "classfile/classLoaderData.inline.hpp"
43 #include "classfile/classLoaderDataGraph.hpp"
44 #include "classfile/classLoaderExt.hpp"
45 #include "classfile/dictionary.hpp"
46 #include "classfile/javaClasses.hpp"
47 #include "classfile/javaClasses.inline.hpp"
48 #include "classfile/symbolTable.hpp"
49 #include "classfile/systemDictionary.hpp"
50 #include "classfile/systemDictionaryShared.hpp"
51 #include "classfile/verificationType.hpp"
52 #include "classfile/vmClasses.hpp"
53 #include "classfile/vmSymbols.hpp"
54 #include "interpreter/bootstrapInfo.hpp"
55 #include "jfr/jfrEvents.hpp"
56 #include "logging/log.hpp"
57 #include "logging/logStream.hpp"
58 #include "memory/allocation.hpp"
59 #include "memory/metadataFactory.hpp"
60 #include "memory/metaspaceClosure.hpp"
61 #include "memory/oopFactory.hpp"
62 #include "memory/resourceArea.hpp"
63 #include "memory/universe.hpp"
64 #include "oops/compressedKlass.inline.hpp"
65 #include "oops/instanceKlass.hpp"
66 #include "oops/klass.inline.hpp"
67 #include "oops/objArrayKlass.hpp"
68 #include "oops/objArrayOop.inline.hpp"
69 #include "oops/oop.inline.hpp"
70 #include "oops/oopHandle.inline.hpp"
71 #include "oops/typeArrayOop.inline.hpp"
72 #include "runtime/arguments.hpp"
73 #include "runtime/handles.inline.hpp"
74 #include "runtime/java.hpp"
75 #include "runtime/javaCalls.hpp"
76 #include "runtime/mutexLocker.hpp"
77 #include "utilities/resourceHash.hpp"
78 #include "utilities/stringUtils.hpp"
79
80 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive;
81 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive;
82
83 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr;
84 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr;
85
86 // Used by NoClassLoadingMark
87 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;)
88
89 #ifdef ASSERT
90 static void check_klass_after_loading(const Klass* k) {
91 #ifdef _LP64
92 if (k != nullptr && UseCompressedClassPointers && k->needs_narrow_id()) {
93 CompressedKlassPointers::check_encodable(k);
94 }
95 #endif
96 }
97 #endif
98
99 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
100 Symbol* class_name, Handle class_loader, TRAPS) {
101 assert(CDSConfig::is_using_archive(), "must be");
102 InstanceKlass* ik = find_builtin_class(class_name);
103
104 if (ik != nullptr && !ik->shared_loading_failed()) {
105 if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class()) ||
106 (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) {
107 SharedClassLoadingMark slm(THREAD, ik);
108 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
109 Handle protection_domain =
110 CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL);
111 return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD);
112 }
113 }
114 return nullptr;
115 }
116
117 // This function is called for loading only UNREGISTERED classes
118 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name,
119 Handle class_loader,
120 Handle protection_domain,
121 const ClassFileStream* cfs,
122 TRAPS) {
123 if (!CDSConfig::is_using_archive()) {
124 return nullptr;
125 }
126 if (class_name == nullptr) { // don't do this for hidden classes
127 return nullptr;
128 }
129 if (class_loader.is_null() ||
130 SystemDictionary::is_system_class_loader(class_loader()) ||
257 if (info != nullptr) {
258 info->_is_registered_lambda_proxy = false;
259 info->set_excluded();
260 }
261 }
262
263 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) {
264 DumpTimeClassInfo* info = _dumptime_table->get(ik);
265 return (info != nullptr) ? info->is_early_klass() : false;
266 }
267
268 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
269 assert(ik->is_shared(), "applicable to only a shared class");
270 if (ik->is_hidden()) {
271 return true;
272 } else {
273 return false;
274 }
275 }
276
277 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) {
278 if (CDSConfig::is_dumping_final_static_archive() && k->is_shared_unregistered_class()
279 && k->is_shared()) {
280 return false; // Do not exclude: unregistered classes are passed from preimage to final image.
281 }
282
283 if (k->is_in_error_state()) {
284 return warn_excluded(k, "In error state");
285 }
286 if (k->is_scratch_class()) {
287 return warn_excluded(k, "A scratch class");
288 }
289 if (!k->is_loaded()) {
290 return warn_excluded(k, "Not in loaded state");
291 }
292 if (has_been_redefined(k)) {
293 return warn_excluded(k, "Has been redefined");
294 }
295 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
296 if (k->name()->starts_with("java/lang/invoke/BoundMethodHandle$Species_")) {
302 log_info(cds)("Skipping %s because it is dynamically generated", k->name()->as_C_string());
303 return true; // exclude without warning
304 }
305 } else {
306 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
307 // agent during dump time).
308 return warn_excluded(k, "Unsupported location");
309 }
310 }
311 if (k->signers() != nullptr) {
312 // We cannot include signed classes in the archive because the certificates
313 // used during dump time may be different than those used during
314 // runtime (due to expiration, etc).
315 return warn_excluded(k, "Signed JAR");
316 }
317 if (is_jfr_event_class(k)) {
318 // We cannot include JFR event classes because they need runtime-specific
319 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false.
320 // There are only a small number of these classes, so it's not worthwhile to
321 // support them and make CDS more complicated.
322 return warn_excluded(k, "JFR event class");
323 }
324
325 if (!k->is_linked()) {
326 if (has_class_failed_verification(k)) {
327 return warn_excluded(k, "Failed verification");
328 } else if (CDSConfig::is_dumping_aot_linked_classes()) {
329 // Most loaded classes should have been speculatively linked by MetaspaceShared::link_class_for_cds().
330 // However, we do not speculatively link old classes, as they are not recorded by
331 // SystemDictionaryShared::record_linking_constraint(). As a result, such an unlinked
332 // class may fail to verify in AOTLinkedClassBulkLoader::init_required_classes_for_loader(),
333 // causing the JVM to fail at bootstrap.
334 return warn_excluded(k, "Unlinked class not supported by AOTClassLinking");
335 } else if (CDSConfig::is_dumping_preimage_static_archive()) {
336 // When dumping the final static archive, we will unconditionally load and link all
337 // classes from tje preimage. We don't want to get a VerifyError when linking this class.
338 return warn_excluded(k, "Unlinked class not supported by AOTConfiguration");
339 }
340 } else {
341 if (!k->can_be_verified_at_dumptime()) {
342 // We have an old class that has been linked (e.g., it's been executed during
343 // dump time). This class has been verified using the old verifier, which
344 // doesn't save the verification constraints, so check_verification_constraints()
345 // won't work at runtime.
346 // As a result, we cannot store this class. It must be loaded and fully verified
347 // at runtime.
348 return warn_excluded(k, "Old class has been linked");
349 }
350 }
351
352 InstanceKlass* super = k->java_super();
353 if (super != nullptr && check_for_exclusion(super, nullptr)) {
354 ResourceMark rm;
355 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
356 return true;
357 }
358
359 Array<InstanceKlass*>* interfaces = k->local_interfaces();
360 int len = interfaces->length();
361 for (int i = 0; i < len; i++) {
362 InstanceKlass* intf = interfaces->at(i);
363 if (check_for_exclusion(intf, nullptr)) {
364 ResourceMark rm;
365 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
366 return true;
367 }
368 }
369
370 if (k == UnregisteredClasses::UnregisteredClassLoader_klass()) {
371 ResourceMark rm;
372 log_info(cds)("Skipping %s: used only when dumping CDS archive", k->name()->as_C_string());
373 return true;
374 }
375
376 return false; // false == k should NOT be excluded
377 }
378
379 bool SystemDictionaryShared::is_builtin_loader(ClassLoaderData* loader_data) {
380 oop class_loader = loader_data->class_loader();
381 return (class_loader == nullptr ||
382 SystemDictionary::is_system_class_loader(class_loader) ||
383 SystemDictionary::is_platform_class_loader(class_loader));
384 }
385
386 bool SystemDictionaryShared::has_platform_or_app_classes() {
387 if (FileMapInfo::current_info()->has_platform_or_app_classes()) {
388 return true;
389 }
390 if (DynamicArchive::is_mapped() &&
391 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) {
392 return true;
393 }
394 return false;
395 }
496 assert(!is_builtin(k), "must be unregistered class");
497 DumpTimeClassInfo* info = get_info(k);
498 info->_clsfile_size = cfs->length();
499 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
500 }
501
502 void SystemDictionaryShared::initialize() {
503 if (CDSConfig::is_dumping_archive()) {
504 _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
505 _dumptime_lambda_proxy_class_dictionary =
506 new (mtClass) DumpTimeLambdaProxyClassDictionary;
507 if (CDSConfig::is_dumping_heap()) {
508 HeapShared::init_dumping();
509 }
510 }
511 }
512
513 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
514 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
515 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
516 _dumptime_table->allocate_info(k);
517 }
518
519 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
520 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
521 _dumptime_table->remove(k);
522 }
523
524 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
525 if (CDSConfig::is_dumping_archive()) {
526 remove_dumptime_info(klass);
527 }
528
529 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {
530 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag);
531 if (_unregistered_classes_table != nullptr) {
532 // Remove the class from _unregistered_classes_table: keep the entry but
533 // set it to null. This ensure no classes with the same name can be
534 // added again.
535 InstanceKlass** v = _unregistered_classes_table->get(klass->name());
536 if (v != nullptr) {
749 assert(k->is_shared_unregistered_class(), "must be");
750 info.metaspace_pointers_do(it);
751 } else if (k->is_loader_alive() && !info.is_excluded()) {
752 info.metaspace_pointers_do(it);
753 }
754 };
755 _dumptime_table->iterate_all_live_classes(do_klass);
756
757 auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
758 if (key.caller_ik()->is_loader_alive()) {
759 info.metaspace_pointers_do(it);
760 key.metaspace_pointers_do(it);
761 }
762 };
763 _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);
764 }
765
766 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
767 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
768 assert(CDSConfig::is_dumping_archive(), "sanity");
769 DumpTimeClassInfo* info = get_info(k);
770 info->add_verification_constraint(k, name, from_name, from_field_is_protected,
771 from_is_array, from_is_object);
772
773 if (CDSConfig::is_dumping_dynamic_archive()) {
774 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
775 // the initial run prior to creating the archive before vm exit. We will also perform verification
776 // check when running with the archive.
777 return false;
778 } else {
779 if (is_builtin(k)) {
780 // For builtin class loaders, we can try to complete the verification check at dump time,
781 // because we can resolve all the constraint classes. We will also perform verification check
782 // when running with the archive.
783 return false;
784 } else {
785 // For non-builtin class loaders, we cannot complete the verification check at dump time,
786 // because at dump time we don't know how to resolve classes for such loaders.
787 return true;
788 }
803 DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
804 info->add_proxy_klass(proxy_klass);
805 if (created) {
806 ++_dumptime_lambda_proxy_class_dictionary->_count;
807 }
808 }
809
810 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
811 InstanceKlass* lambda_ik,
812 Symbol* invoked_name,
813 Symbol* invoked_type,
814 Symbol* method_type,
815 Method* member_method,
816 Symbol* instantiated_method_type,
817 TRAPS) {
818 if (CDSConfig::is_dumping_invokedynamic()) {
819 // The lambda proxy classes will be stored as part of aot-resolved constant pool entries.
820 // There's no need to remember them in a separate table.
821 return;
822 }
823 if (CDSConfig::is_dumping_preimage_static_archive()) {
824 // Information about lambda proxies are recorded in FinalImageRecipes.
825 return;
826 }
827
828 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
829 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
830 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
831
832 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
833
834 lambda_ik->assign_class_loader_type();
835 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
836 InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
837 assert(nest_host != nullptr, "unexpected nullptr nest_host");
838
839 DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
840 if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
841 // Don't include the lambda proxy if its nest host is not in the "linked" state.
842 && nest_host->is_linked()) {
913 }
914
915 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
916 InstanceKlass* proxy_klass = nullptr;
917 if (info != nullptr) {
918 InstanceKlass* curr_klass = info->proxy_klass_head();
919 InstanceKlass* prev_klass = curr_klass;
920 if (curr_klass->lambda_proxy_is_available()) {
921 while (curr_klass->next_link() != nullptr) {
922 prev_klass = curr_klass;
923 curr_klass = InstanceKlass::cast(curr_klass->next_link());
924 }
925 assert(curr_klass->is_hidden(), "must be");
926 assert(curr_klass->lambda_proxy_is_available(), "must be");
927
928 prev_klass->set_next_link(nullptr);
929 proxy_klass = curr_klass;
930 proxy_klass->clear_lambda_proxy_is_available();
931 if (log_is_enabled(Debug, cds)) {
932 ResourceMark rm;
933 log_debug(cds)("Loaded lambda proxy: %s ", proxy_klass->external_name());
934 }
935 }
936 }
937 return proxy_klass;
938 }
939
940 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
941 assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
942 RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
943 return record->nest_host();
944 }
945
946 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
947 InstanceKlass* caller_ik, TRAPS) {
948 Handle class_loader(THREAD, caller_ik->class_loader());
949 Handle protection_domain;
950 PackageEntry* pkg_entry = caller_ik->package();
951 if (caller_ik->class_loader() != nullptr) {
952 protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
953 }
1246 return _shared_class_info_size;
1247 }
1248 };
1249
1250 size_t SystemDictionaryShared::estimate_size_for_archive() {
1251 EstimateSizeForArchive est;
1252 _dumptime_table->iterate_all_live_classes(&est);
1253 size_t total_size = est.total() +
1254 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1255 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1256
1257 size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1258 total_size +=
1259 (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1260 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1261
1262 return total_size;
1263 }
1264
1265 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1266 if (ArchiveBuilder::is_active()) {
1267 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1268 unsigned int hash = primitive_hash<uintx>(offset);
1269 DEBUG_ONLY({
1270 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1271 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1272 }
1273 });
1274 return hash;
1275 } else {
1276 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1277 }
1278 }
1279
1280 class CopyLambdaProxyClassInfoToArchive : StackObj {
1281 CompactHashtableWriter* _writer;
1282 ArchiveBuilder* _builder;
1283 public:
1284 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1285 : _writer(writer), _builder(ArchiveBuilder::current()) {}
1286 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1404 }
1405
1406 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1407 for (auto id : EnumRange<vmClassID>{}) {
1408 soc->do_ptr(vmClasses::klass_addr_at(id));
1409 }
1410 }
1411
1412 const RunTimeClassInfo*
1413 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1414 if (!CDSConfig::is_using_archive() || !name->is_shared()) {
1415 // The names of all shared classes must also be a shared Symbol.
1416 return nullptr;
1417 }
1418
1419 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1420 const RunTimeClassInfo* record = nullptr;
1421 if (DynamicArchive::is_mapped()) {
1422 // Use the regenerated holder classes in the dynamic archive as they
1423 // have more methods than those in the base archive.
1424 if (name == vmSymbols::java_lang_invoke_Invokers_Holder() ||
1425 name == vmSymbols::java_lang_invoke_DirectMethodHandle_Holder() ||
1426 name == vmSymbols::java_lang_invoke_LambdaForm_Holder() ||
1427 name == vmSymbols::java_lang_invoke_DelegatingMethodHandle_Holder()) {
1428 record = dynamic_dict->lookup(name, hash, 0);
1429 if (record != nullptr) {
1430 return record;
1431 }
1432 }
1433 }
1434
1435 if (!MetaspaceShared::is_shared_dynamic(name)) {
1436 // The names of all shared classes in the static dict must also be in the
1437 // static archive
1438 record = static_dict->lookup(name, hash, 0);
1439 }
1440
1441 if (record == nullptr && DynamicArchive::is_mapped()) {
1442 record = dynamic_dict->lookup(name, hash, 0);
1443 }
1444
1445 return record;
1446 }
1447
1452 if (record != nullptr) {
1453 assert(!record->klass()->is_hidden(), "hidden class cannot be looked up by name");
1454 DEBUG_ONLY(check_klass_after_loading(record->klass());)
1455 // We did not save the classfile data of the generated LambdaForm invoker classes,
1456 // so we cannot support CLFH for such classes.
1457 if (record->klass()->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1458 return nullptr;
1459 }
1460 return record->klass();
1461 } else {
1462 return nullptr;
1463 }
1464 }
1465
1466 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1467 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1468 DumpTimeClassInfo* info = get_info(k);
1469 info->_id = id;
1470 }
1471
1472 static const char* class_loader_name_for_shared(Klass* k) {
1473 assert(k != nullptr, "Sanity");
1474 assert(k->is_shared(), "Must be");
1475 assert(k->is_instance_klass(), "Must be");
1476 InstanceKlass* ik = InstanceKlass::cast(k);
1477 if (ik->is_shared_boot_class()) {
1478 return "boot_loader";
1479 } else if (ik->is_shared_platform_class()) {
1480 return "platform_loader";
1481 } else if (ik->is_shared_app_class()) {
1482 return "app_loader";
1483 } else if (ik->is_shared_unregistered_class()) {
1484 return "unregistered_loader";
1485 } else {
1486 return "unknown loader";
1487 }
1488 }
1489
1490 class SharedDictionaryPrinter : StackObj {
1491 outputStream* _st;
1492 int _index;
1493 public:
1494 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1495
1496 void do_value(const RunTimeClassInfo* record) {
1497 ResourceMark rm;
1498 _st->print_cr("%4d: %s %s", _index++, record->klass()->external_name(),
1499 class_loader_name_for_shared(record->klass()));
1500 if (record->klass()->array_klasses() != nullptr) {
1501 record->klass()->array_klasses()->cds_print_value_on(_st);
1502 _st->cr();
1503 }
1504 }
1505 int index() const { return _index; }
1506 };
1507
1508 class SharedLambdaDictionaryPrinter : StackObj {
1509 outputStream* _st;
1510 int _index;
1511 public:
1512 SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1513
1514 void do_value(const RunTimeLambdaProxyClassInfo* record) {
1515 if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1516 ResourceMark rm;
1517 Klass* k = record->proxy_klass_head();
1518 while (k != nullptr) {
1519 _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1520 class_loader_name_for_shared(k));
1521 k = k->next_link();
1522 }
1523 }
1524 }
1525 };
1526
1527 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1528 outputStream* st) {
1529 st->print_cr("%sShared Dictionary", prefix);
1530 SharedDictionaryPrinter p(st);
1531 st->print_cr("%sShared Builtin Dictionary", prefix);
1532 _builtin_dictionary.iterate(&p);
1533 st->print_cr("%sShared Unregistered Dictionary", prefix);
1534 _unregistered_dictionary.iterate(&p);
1535 if (!_lambda_proxy_class_dictionary.empty()) {
1536 st->print_cr("%sShared Lambda Dictionary", prefix);
1537 SharedLambdaDictionaryPrinter ldp(st, p.index());
1538 _lambda_proxy_class_dictionary.iterate(&ldp);
1539 }
1540 }
1593 // must also be excluded.
1594 bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1595 SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1596
1597 for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1598 InstanceKlass* ik = info._proxy_klasses->at(i);
1599 if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1600 SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1601 info._proxy_klasses->remove_at(i);
1602 }
1603 }
1604 return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1605 }
1606 };
1607
1608 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1609 assert_lock_strong(DumpTimeTable_lock);
1610 CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1611 _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1612 }
|
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 "cds/aotClassLocation.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/archiveUtils.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/cdsProtectionDomain.hpp"
31 #include "cds/classListParser.hpp"
32 #include "cds/classListWriter.hpp"
33 #include "cds/dumpTimeClassInfo.inline.hpp"
34 #include "cds/dynamicArchive.hpp"
35 #include "cds/filemap.hpp"
36 #include "cds/heapShared.hpp"
37 #include "cds/lambdaFormInvokers.inline.hpp"
38 #include "cds/metaspaceShared.hpp"
39 #include "cds/runTimeClassInfo.hpp"
40 #include "cds/unregisteredClasses.hpp"
41 #include "classfile/classFileStream.hpp"
42 #include "classfile/classLoader.hpp"
43 #include "classfile/classLoaderData.inline.hpp"
44 #include "classfile/classLoaderDataGraph.hpp"
45 #include "classfile/classLoaderExt.hpp"
46 #include "classfile/dictionary.hpp"
47 #include "classfile/javaClasses.hpp"
48 #include "classfile/javaClasses.inline.hpp"
49 #include "classfile/symbolTable.hpp"
50 #include "classfile/systemDictionary.hpp"
51 #include "classfile/systemDictionaryShared.hpp"
52 #include "classfile/verificationType.hpp"
53 #include "classfile/vmClasses.hpp"
54 #include "classfile/vmSymbols.hpp"
55 #include "interpreter/bootstrapInfo.hpp"
56 #include "jfr/jfrEvents.hpp"
57 #include "logging/log.hpp"
58 #include "logging/logStream.hpp"
59 #include "memory/allocation.hpp"
60 #include "memory/metadataFactory.hpp"
61 #include "memory/metaspaceClosure.hpp"
62 #include "memory/oopFactory.hpp"
63 #include "memory/resourceArea.hpp"
64 #include "memory/universe.hpp"
65 #include "oops/compressedKlass.inline.hpp"
66 #include "oops/instanceKlass.hpp"
67 #include "oops/klass.inline.hpp"
68 #include "oops/methodData.hpp"
69 #include "oops/objArrayKlass.hpp"
70 #include "oops/objArrayOop.inline.hpp"
71 #include "oops/oop.inline.hpp"
72 #include "oops/oopHandle.inline.hpp"
73 #include "oops/typeArrayOop.inline.hpp"
74 #include "runtime/arguments.hpp"
75 #include "runtime/handles.inline.hpp"
76 #include "runtime/java.hpp"
77 #include "runtime/javaCalls.hpp"
78 #include "runtime/mutexLocker.hpp"
79 #include "utilities/resourceHash.hpp"
80 #include "utilities/stringUtils.hpp"
81
82 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive;
83 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive;
84
85 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr;
86 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr;
87
88 static bool _ignore_new_classes = false;
89
90 // Used by NoClassLoadingMark
91 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;)
92
93 #ifdef ASSERT
94 static void check_klass_after_loading(const Klass* k) {
95 #ifdef _LP64
96 if (k != nullptr && UseCompressedClassPointers && k->needs_narrow_id()) {
97 CompressedKlassPointers::check_encodable(k);
98 }
99 #endif
100 }
101 #endif
102
103 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
104 Symbol* class_name, Handle class_loader, TRAPS) {
105 assert(CDSConfig::is_using_archive(), "must be");
106 InstanceKlass* ik = find_builtin_class(class_name);
107
108 if (ik != nullptr && !ik->shared_loading_failed()) {
109 if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class()) ||
110 (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) {
111 SharedClassLoadingMark slm(THREAD, ik);
112 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
113 Handle protection_domain;
114 if (!class_name->starts_with("jdk/proxy")) // java/lang/reflect/Proxy$ProxyBuilder defines the proxy classes with a null protection domain.
115 {
116 protection_domain = CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL);
117 }
118 return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD);
119 }
120 }
121 return nullptr;
122 }
123
124 // This function is called for loading only UNREGISTERED classes
125 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name,
126 Handle class_loader,
127 Handle protection_domain,
128 const ClassFileStream* cfs,
129 TRAPS) {
130 if (!CDSConfig::is_using_archive()) {
131 return nullptr;
132 }
133 if (class_name == nullptr) { // don't do this for hidden classes
134 return nullptr;
135 }
136 if (class_loader.is_null() ||
137 SystemDictionary::is_system_class_loader(class_loader()) ||
264 if (info != nullptr) {
265 info->_is_registered_lambda_proxy = false;
266 info->set_excluded();
267 }
268 }
269
270 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) {
271 DumpTimeClassInfo* info = _dumptime_table->get(ik);
272 return (info != nullptr) ? info->is_early_klass() : false;
273 }
274
275 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
276 assert(ik->is_shared(), "applicable to only a shared class");
277 if (ik->is_hidden()) {
278 return true;
279 } else {
280 return false;
281 }
282 }
283
284 void SystemDictionaryShared::ignore_new_classes() {
285 _ignore_new_classes = true;
286 }
287
288
289 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) {
290 if (CDSConfig::is_dumping_final_static_archive() && k->is_shared_unregistered_class()
291 && k->is_shared()) {
292 return false; // Do not exclude: unregistered classes are passed from preimage to final image.
293 }
294
295 if (k->is_in_error_state()) {
296 return warn_excluded(k, "In error state");
297 }
298 if (k->is_scratch_class()) {
299 return warn_excluded(k, "A scratch class");
300 }
301 if (!k->is_loaded()) {
302 return warn_excluded(k, "Not in loaded state");
303 }
304 if (has_been_redefined(k)) {
305 return warn_excluded(k, "Has been redefined");
306 }
307 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
308 if (k->name()->starts_with("java/lang/invoke/BoundMethodHandle$Species_")) {
314 log_info(cds)("Skipping %s because it is dynamically generated", k->name()->as_C_string());
315 return true; // exclude without warning
316 }
317 } else {
318 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
319 // agent during dump time).
320 return warn_excluded(k, "Unsupported location");
321 }
322 }
323 if (k->signers() != nullptr) {
324 // We cannot include signed classes in the archive because the certificates
325 // used during dump time may be different than those used during
326 // runtime (due to expiration, etc).
327 return warn_excluded(k, "Signed JAR");
328 }
329 if (is_jfr_event_class(k)) {
330 // We cannot include JFR event classes because they need runtime-specific
331 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false.
332 // There are only a small number of these classes, so it's not worthwhile to
333 // support them and make CDS more complicated.
334 if (!CDSConfig::is_dumping_reflection_data()) { // FIXME: !!! HACK !!!
335 return warn_excluded(k, "JFR event class");
336 }
337 }
338
339 if (!k->is_linked()) {
340 if (has_class_failed_verification(k)) {
341 return warn_excluded(k, "Failed verification");
342 } else if (CDSConfig::is_dumping_aot_linked_classes()) {
343 // Most loaded classes should have been speculatively linked by MetaspaceShared::link_class_for_cds().
344 // However, we do not speculatively link old classes, as they are not recorded by
345 // SystemDictionaryShared::record_linking_constraint(). As a result, such an unlinked
346 // class may fail to verify in AOTLinkedClassBulkLoader::init_required_classes_for_loader(),
347 // causing the JVM to fail at bootstrap.
348 return warn_excluded(k, "Unlinked class not supported by AOTClassLinking");
349 } else if (CDSConfig::is_dumping_preimage_static_archive()) {
350 // When dumping the final static archive, we will unconditionally load and link all
351 // classes from tje preimage. We don't want to get a VerifyError when linking this class.
352 return warn_excluded(k, "Unlinked class not supported by AOTConfiguration");
353 }
354 } else {
355 if (!k->can_be_verified_at_dumptime() && !CDSConfig::preserve_all_dumptime_verification_states(k)) {
356 // We have an old class that has been linked (e.g., it's been executed during
357 // dump time). This class has been verified using the old verifier, which
358 // doesn't save the verification constraints, so check_verification_constraints()
359 // won't work at runtime.
360 // As a result, we cannot store this class. It must be loaded and fully verified
361 // at runtime.
362 return warn_excluded(k, "Old class has been linked");
363 }
364 }
365
366 InstanceKlass* super = k->java_super();
367 if (super != nullptr && check_for_exclusion(super, nullptr)) {
368 ResourceMark rm;
369 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
370 return true;
371 }
372
373 Array<InstanceKlass*>* interfaces = k->local_interfaces();
374 int len = interfaces->length();
375 for (int i = 0; i < len; i++) {
376 InstanceKlass* intf = interfaces->at(i);
377 if (check_for_exclusion(intf, nullptr)) {
378 ResourceMark rm;
379 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
380 return true;
381 }
382 }
383
384 if (k == UnregisteredClasses::UnregisteredClassLoader_klass()) {
385 ResourceMark rm;
386 log_debug(cds)("Skipping %s: used only when dumping CDS archive", k->name()->as_C_string());
387 return true;
388 }
389
390 if (k->name()->equals("jdk/internal/misc/CDS$DummyForDynamicArchive") && !CDSConfig::is_dumping_dynamic_archive()) {
391 ResourceMark rm;
392 log_debug(cds)("Skipping %s: used only when dumping dynamic CDS archive", k->name()->as_C_string());
393 return true;
394 }
395
396
397 return false; // false == k should NOT be excluded
398 }
399
400 bool SystemDictionaryShared::is_builtin_loader(ClassLoaderData* loader_data) {
401 oop class_loader = loader_data->class_loader();
402 return (class_loader == nullptr ||
403 SystemDictionary::is_system_class_loader(class_loader) ||
404 SystemDictionary::is_platform_class_loader(class_loader));
405 }
406
407 bool SystemDictionaryShared::has_platform_or_app_classes() {
408 if (FileMapInfo::current_info()->has_platform_or_app_classes()) {
409 return true;
410 }
411 if (DynamicArchive::is_mapped() &&
412 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) {
413 return true;
414 }
415 return false;
416 }
517 assert(!is_builtin(k), "must be unregistered class");
518 DumpTimeClassInfo* info = get_info(k);
519 info->_clsfile_size = cfs->length();
520 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
521 }
522
523 void SystemDictionaryShared::initialize() {
524 if (CDSConfig::is_dumping_archive()) {
525 _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
526 _dumptime_lambda_proxy_class_dictionary =
527 new (mtClass) DumpTimeLambdaProxyClassDictionary;
528 if (CDSConfig::is_dumping_heap()) {
529 HeapShared::init_dumping();
530 }
531 }
532 }
533
534 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
535 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
536 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
537 DumpTimeClassInfo* info = _dumptime_table->allocate_info(k);
538 if (_ignore_new_classes) {
539 if (!LambdaFormInvokers::may_be_regenerated_class(k->name())) {
540 ResourceMark rm;
541 log_debug(cds)("Skipping %s: Class loaded for lambda form invoker regeneration", k->name()->as_C_string());
542 info->set_has_checked_exclusion();
543 info->set_excluded();
544 }
545 }
546 }
547
548 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
549 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
550 _dumptime_table->remove(k);
551 }
552
553 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
554 if (CDSConfig::is_dumping_archive()) {
555 remove_dumptime_info(klass);
556 }
557
558 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {
559 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag);
560 if (_unregistered_classes_table != nullptr) {
561 // Remove the class from _unregistered_classes_table: keep the entry but
562 // set it to null. This ensure no classes with the same name can be
563 // added again.
564 InstanceKlass** v = _unregistered_classes_table->get(klass->name());
565 if (v != nullptr) {
778 assert(k->is_shared_unregistered_class(), "must be");
779 info.metaspace_pointers_do(it);
780 } else if (k->is_loader_alive() && !info.is_excluded()) {
781 info.metaspace_pointers_do(it);
782 }
783 };
784 _dumptime_table->iterate_all_live_classes(do_klass);
785
786 auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
787 if (key.caller_ik()->is_loader_alive()) {
788 info.metaspace_pointers_do(it);
789 key.metaspace_pointers_do(it);
790 }
791 };
792 _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);
793 }
794
795 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
796 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
797 assert(CDSConfig::is_dumping_archive(), "sanity");
798 if (CDSConfig::is_dumping_dynamic_archive() && k->is_shared()) {
799 // k is a new class in the static archive, but one of its supertypes is an old class, so k wasn't
800 // verified during dump time. No need to record constraints as k won't be included in the dynamic archive.
801 return false;
802 }
803 if (CDSConfig::is_dumping_aot_linked_classes() && is_builtin(k)) {
804 // There's no need to save verification constraints
805 // TODO -- double check the logic before integrating into mainline!!
806 return false;
807 }
808
809 DumpTimeClassInfo* info = get_info(k);
810 info->add_verification_constraint(k, name, from_name, from_field_is_protected,
811 from_is_array, from_is_object);
812
813 if (CDSConfig::is_dumping_dynamic_archive()) {
814 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
815 // the initial run prior to creating the archive before vm exit. We will also perform verification
816 // check when running with the archive.
817 return false;
818 } else {
819 if (is_builtin(k)) {
820 // For builtin class loaders, we can try to complete the verification check at dump time,
821 // because we can resolve all the constraint classes. We will also perform verification check
822 // when running with the archive.
823 return false;
824 } else {
825 // For non-builtin class loaders, we cannot complete the verification check at dump time,
826 // because at dump time we don't know how to resolve classes for such loaders.
827 return true;
828 }
843 DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
844 info->add_proxy_klass(proxy_klass);
845 if (created) {
846 ++_dumptime_lambda_proxy_class_dictionary->_count;
847 }
848 }
849
850 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
851 InstanceKlass* lambda_ik,
852 Symbol* invoked_name,
853 Symbol* invoked_type,
854 Symbol* method_type,
855 Method* member_method,
856 Symbol* instantiated_method_type,
857 TRAPS) {
858 if (CDSConfig::is_dumping_invokedynamic()) {
859 // The lambda proxy classes will be stored as part of aot-resolved constant pool entries.
860 // There's no need to remember them in a separate table.
861 return;
862 }
863
864 if (CDSConfig::is_dumping_preimage_static_archive()) {
865 // Information about lambda proxies are recorded in FinalImageRecipes.
866 return;
867 }
868
869 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
870 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
871 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
872
873 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
874
875 lambda_ik->assign_class_loader_type();
876 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
877 InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
878 assert(nest_host != nullptr, "unexpected nullptr nest_host");
879
880 DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
881 if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
882 // Don't include the lambda proxy if its nest host is not in the "linked" state.
883 && nest_host->is_linked()) {
954 }
955
956 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
957 InstanceKlass* proxy_klass = nullptr;
958 if (info != nullptr) {
959 InstanceKlass* curr_klass = info->proxy_klass_head();
960 InstanceKlass* prev_klass = curr_klass;
961 if (curr_klass->lambda_proxy_is_available()) {
962 while (curr_klass->next_link() != nullptr) {
963 prev_klass = curr_klass;
964 curr_klass = InstanceKlass::cast(curr_klass->next_link());
965 }
966 assert(curr_klass->is_hidden(), "must be");
967 assert(curr_klass->lambda_proxy_is_available(), "must be");
968
969 prev_klass->set_next_link(nullptr);
970 proxy_klass = curr_klass;
971 proxy_klass->clear_lambda_proxy_is_available();
972 if (log_is_enabled(Debug, cds)) {
973 ResourceMark rm;
974 log_debug(cds)("Loaded lambda proxy: " PTR_FORMAT " %s ", p2i(proxy_klass), proxy_klass->external_name());
975 }
976 }
977 }
978 return proxy_klass;
979 }
980
981 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
982 assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
983 RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
984 return record->nest_host();
985 }
986
987 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
988 InstanceKlass* caller_ik, TRAPS) {
989 Handle class_loader(THREAD, caller_ik->class_loader());
990 Handle protection_domain;
991 PackageEntry* pkg_entry = caller_ik->package();
992 if (caller_ik->class_loader() != nullptr) {
993 protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
994 }
1287 return _shared_class_info_size;
1288 }
1289 };
1290
1291 size_t SystemDictionaryShared::estimate_size_for_archive() {
1292 EstimateSizeForArchive est;
1293 _dumptime_table->iterate_all_live_classes(&est);
1294 size_t total_size = est.total() +
1295 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1296 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1297
1298 size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1299 total_size +=
1300 (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1301 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1302
1303 return total_size;
1304 }
1305
1306 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1307 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
1308 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1309 unsigned int hash = primitive_hash<uintx>(offset);
1310 DEBUG_ONLY({
1311 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1312 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1313 }
1314 });
1315 return hash;
1316 } else {
1317 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1318 }
1319 }
1320
1321 class CopyLambdaProxyClassInfoToArchive : StackObj {
1322 CompactHashtableWriter* _writer;
1323 ArchiveBuilder* _builder;
1324 public:
1325 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1326 : _writer(writer), _builder(ArchiveBuilder::current()) {}
1327 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1445 }
1446
1447 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1448 for (auto id : EnumRange<vmClassID>{}) {
1449 soc->do_ptr(vmClasses::klass_addr_at(id));
1450 }
1451 }
1452
1453 const RunTimeClassInfo*
1454 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1455 if (!CDSConfig::is_using_archive() || !name->is_shared()) {
1456 // The names of all shared classes must also be a shared Symbol.
1457 return nullptr;
1458 }
1459
1460 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1461 const RunTimeClassInfo* record = nullptr;
1462 if (DynamicArchive::is_mapped()) {
1463 // Use the regenerated holder classes in the dynamic archive as they
1464 // have more methods than those in the base archive.
1465 if (LambdaFormInvokers::may_be_regenerated_class(name)) {
1466 record = dynamic_dict->lookup(name, hash, 0);
1467 if (record != nullptr) {
1468 return record;
1469 }
1470 }
1471 }
1472
1473 if (!MetaspaceShared::is_shared_dynamic(name)) {
1474 // The names of all shared classes in the static dict must also be in the
1475 // static archive
1476 record = static_dict->lookup(name, hash, 0);
1477 }
1478
1479 if (record == nullptr && DynamicArchive::is_mapped()) {
1480 record = dynamic_dict->lookup(name, hash, 0);
1481 }
1482
1483 return record;
1484 }
1485
1490 if (record != nullptr) {
1491 assert(!record->klass()->is_hidden(), "hidden class cannot be looked up by name");
1492 DEBUG_ONLY(check_klass_after_loading(record->klass());)
1493 // We did not save the classfile data of the generated LambdaForm invoker classes,
1494 // so we cannot support CLFH for such classes.
1495 if (record->klass()->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1496 return nullptr;
1497 }
1498 return record->klass();
1499 } else {
1500 return nullptr;
1501 }
1502 }
1503
1504 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1505 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1506 DumpTimeClassInfo* info = get_info(k);
1507 info->_id = id;
1508 }
1509
1510 const char* SystemDictionaryShared::class_loader_name_for_shared(Klass* k) {
1511 assert(k != nullptr, "Sanity");
1512 assert(k->is_shared(), "Must be");
1513 assert(k->is_instance_klass(), "Must be");
1514 InstanceKlass* ik = InstanceKlass::cast(k);
1515 if (ik->is_shared_boot_class()) {
1516 return "boot_loader";
1517 } else if (ik->is_shared_platform_class()) {
1518 return "platform_loader";
1519 } else if (ik->is_shared_app_class()) {
1520 return "app_loader";
1521 } else if (ik->is_shared_unregistered_class()) {
1522 return "unregistered_loader";
1523 } else {
1524 return "unknown loader";
1525 }
1526 }
1527
1528 class SharedDictionaryPrinter : StackObj {
1529 outputStream* _st;
1530 int _index;
1531 public:
1532 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1533
1534 void do_value(const RunTimeClassInfo* record) {
1535 ResourceMark rm;
1536 _st->print_cr("%4d: %s %s", _index++, record->klass()->external_name(),
1537 SystemDictionaryShared::class_loader_name_for_shared(record->klass()));
1538 if (record->klass()->array_klasses() != nullptr) {
1539 record->klass()->array_klasses()->cds_print_value_on(_st);
1540 _st->cr();
1541 }
1542 }
1543 int index() const { return _index; }
1544 };
1545
1546 class SharedLambdaDictionaryPrinter : StackObj {
1547 outputStream* _st;
1548 int _index;
1549 public:
1550 SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1551
1552 void do_value(const RunTimeLambdaProxyClassInfo* record) {
1553 if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1554 ResourceMark rm;
1555 Klass* k = record->proxy_klass_head();
1556 while (k != nullptr) {
1557 _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1558 SystemDictionaryShared::class_loader_name_for_shared(k));
1559 k = k->next_link();
1560 }
1561 }
1562 }
1563 };
1564
1565 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1566 outputStream* st) {
1567 st->print_cr("%sShared Dictionary", prefix);
1568 SharedDictionaryPrinter p(st);
1569 st->print_cr("%sShared Builtin Dictionary", prefix);
1570 _builtin_dictionary.iterate(&p);
1571 st->print_cr("%sShared Unregistered Dictionary", prefix);
1572 _unregistered_dictionary.iterate(&p);
1573 if (!_lambda_proxy_class_dictionary.empty()) {
1574 st->print_cr("%sShared Lambda Dictionary", prefix);
1575 SharedLambdaDictionaryPrinter ldp(st, p.index());
1576 _lambda_proxy_class_dictionary.iterate(&ldp);
1577 }
1578 }
1631 // must also be excluded.
1632 bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1633 SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1634
1635 for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1636 InstanceKlass* ik = info._proxy_klasses->at(i);
1637 if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1638 SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1639 info._proxy_klasses->remove_at(i);
1640 }
1641 }
1642 return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1643 }
1644 };
1645
1646 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1647 assert_lock_strong(DumpTimeTable_lock);
1648 CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1649 _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1650 }
1651
1652 void SystemDictionaryShared::create_loader_positive_lookup_cache(TRAPS) {
1653 GrowableArray<InstanceKlass*> shared_classes_list;
1654 {
1655 // With static dumping, we have only a single Java thread (see JVM_StartThread) so
1656 // no no other threads should be loading classes. Otherwise, the code below may miss some
1657 // classes that are loaded concurrently.
1658 assert(CDSConfig::is_dumping_static_archive(), "no other threads should be loading classes");
1659
1660 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1661 _dumptime_table->iterate_all_classes_in_builtin_loaders([&](InstanceKlass* k, DumpTimeClassInfo& info) {
1662 if (!k->is_hidden() && !check_for_exclusion(k, &info)) {
1663 shared_classes_list.append(k);
1664 }
1665 }
1666 );
1667 }
1668
1669 InstanceKlass* ik = vmClasses::Class_klass();
1670 objArrayOop r = oopFactory::new_objArray(ik, shared_classes_list.length(), CHECK);
1671 objArrayHandle array_h(THREAD, r);
1672
1673 for (int i = 0; i < shared_classes_list.length(); i++) {
1674 oop mirror = shared_classes_list.at(i)->java_mirror();
1675 Handle mirror_h(THREAD, mirror);
1676 array_h->obj_at_put(i, mirror_h());
1677 }
1678
1679 TempNewSymbol method = SymbolTable::new_symbol("generatePositiveLookupCache");
1680 TempNewSymbol signature = SymbolTable::new_symbol("([Ljava/lang/Class;)V");
1681
1682 JavaCallArguments args(Handle(THREAD, SystemDictionary::java_system_loader()));
1683 args.push_oop(array_h);
1684 JavaValue result(T_VOID);
1685 JavaCalls::call_virtual(&result,
1686 vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(),
1687 method,
1688 signature,
1689 &args,
1690 CHECK);
1691
1692 if (HAS_PENDING_EXCEPTION) {
1693 Handle exc_handle(THREAD, PENDING_EXCEPTION);
1694 CLEAR_PENDING_EXCEPTION;
1695 ResourceMark rm(THREAD);
1696
1697 log_warning(cds)("Exception during AppClassLoader::generatePositiveLookupCache() call");
1698 LogStreamHandle(Debug, cds) log;
1699 if (log.is_enabled()) {
1700 java_lang_Throwable::print_stack_trace(exc_handle, &log);
1701 }
1702 return;
1703 }
1704 }
|