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/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 "classfile/classFileStream.hpp"
40 #include "classfile/classLoader.hpp"
41 #include "classfile/classLoaderData.inline.hpp"
42 #include "classfile/classLoaderDataGraph.hpp"
43 #include "classfile/classLoaderExt.hpp"
44 #include "classfile/dictionary.hpp"
45 #include "classfile/javaClasses.hpp"
46 #include "classfile/javaClasses.inline.hpp"
47 #include "classfile/symbolTable.hpp"
48 #include "classfile/systemDictionary.hpp"
49 #include "classfile/systemDictionaryShared.hpp"
50 #include "classfile/verificationType.hpp"
51 #include "classfile/vmClasses.hpp"
52 #include "classfile/vmSymbols.hpp"
53 #include "interpreter/bootstrapInfo.hpp"
54 #include "jfr/jfrEvents.hpp"
55 #include "logging/log.hpp"
56 #include "logging/logStream.hpp"
57 #include "memory/allocation.hpp"
58 #include "memory/metadataFactory.hpp"
59 #include "memory/metaspaceClosure.hpp"
60 #include "memory/oopFactory.hpp"
61 #include "memory/resourceArea.hpp"
62 #include "memory/universe.hpp"
63 #include "oops/compressedKlass.inline.hpp"
64 #include "oops/instanceKlass.hpp"
65 #include "oops/klass.inline.hpp"
66 #include "oops/objArrayKlass.hpp"
67 #include "oops/objArrayOop.inline.hpp"
68 #include "oops/oop.inline.hpp"
69 #include "oops/oopHandle.inline.hpp"
70 #include "oops/typeArrayOop.inline.hpp"
71 #include "runtime/arguments.hpp"
72 #include "runtime/handles.inline.hpp"
73 #include "runtime/java.hpp"
74 #include "runtime/javaCalls.hpp"
75 #include "runtime/mutexLocker.hpp"
76 #include "utilities/resourceHash.hpp"
77 #include "utilities/stringUtils.hpp"
78
79 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive;
80 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive;
81
82 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr;
83 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr;
84
85 // Used by NoClassLoadingMark
86 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;)
87
88 #ifdef ASSERT
89 static void check_klass_after_loading(const Klass* k) {
90 #ifdef _LP64
91 if (k != nullptr && UseCompressedClassPointers && k->needs_narrow_id()) {
92 CompressedKlassPointers::check_encodable(k);
93 }
94 #endif
95 }
96 #endif
97
98 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
99 Symbol* class_name, Handle class_loader, TRAPS) {
100 assert(CDSConfig::is_using_archive(), "must be");
101 InstanceKlass* ik = find_builtin_class(class_name);
102
103 if (ik != nullptr && !ik->shared_loading_failed()) {
104 if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class()) ||
105 (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) {
106 SharedClassLoadingMark slm(THREAD, ik);
107 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
108 Handle protection_domain =
109 CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL);
110 return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD);
111 }
112 }
113 return nullptr;
114 }
115
116 // This function is called for loading only UNREGISTERED classes
117 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name,
118 Handle class_loader,
119 Handle protection_domain,
120 const ClassFileStream* cfs,
121 TRAPS) {
122 if (!CDSConfig::is_using_archive()) {
123 return nullptr;
124 }
125 if (class_name == nullptr) { // don't do this for hidden classes
126 return nullptr;
127 }
128 if (class_loader.is_null() ||
129 SystemDictionary::is_system_class_loader(class_loader()) ||
176 loader_data->add_class(ik);
177
178 // Get the package entry.
179 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
180
181 // Load and check super/interfaces, restore unshareable info
182 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain,
183 cfs, pkg_entry, THREAD);
184 if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) {
185 // TODO: clean up <ik> so it can be used again
186 return nullptr;
187 }
188
189 return shared_klass;
190 }
191
192 // Guaranteed to return non-null value for non-shared classes.
193 // k must not be a shared class.
194 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) {
195 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
196 assert(!k->is_shared(), "sanity");
197 return get_info_locked(k);
198 }
199
200 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) {
201 assert_lock_strong(DumpTimeTable_lock);
202 assert(!k->is_shared(), "sanity");
203 DumpTimeClassInfo* info = _dumptime_table->get_info(k);
204 assert(info != nullptr, "must be");
205 return info;
206 }
207
208 void SystemDictionaryShared::mark_required_hidden_class(InstanceKlass* k) {
209 assert(k->is_hidden(), "sanity");
210 DumpTimeClassInfo* info = _dumptime_table->get(k);
211 ResourceMark rm;
212 if (info != nullptr) {
213 info->set_is_required_hidden_class();
214 }
215 }
216
217 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) {
218 if (MetaspaceShared::is_in_shared_metaspace(k)) {
219 // We have reached a super type that's already in the base archive. Treat it
220 // as "not excluded".
221 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
222 return false;
223 }
224
225 if (info == nullptr) {
226 info = _dumptime_table->get(k);
227 assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table");
228 }
229
230 if (!info->has_checked_exclusion()) {
231 if (check_for_exclusion_impl(k)) {
232 info->set_excluded();
233 }
234 info->set_has_checked_exclusion();
235 }
236
237 return info->is_excluded();
238 }
264 if (info != nullptr) {
265 info->_is_archived_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 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) {
285 if (k->is_in_error_state()) {
286 return warn_excluded(k, "In error state");
287 }
288 if (k->is_scratch_class()) {
289 return warn_excluded(k, "A scratch class");
290 }
291 if (!k->is_loaded()) {
292 return warn_excluded(k, "Not in loaded state");
293 }
294 if (has_been_redefined(k)) {
295 return warn_excluded(k, "Has been redefined");
296 }
297 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
298 if (k->name()->starts_with("java/lang/invoke/BoundMethodHandle$Species_")) {
299 // This class is dynamically generated by the JDK
300 if (CDSConfig::is_dumping_aot_linked_classes()) {
301 k->set_shared_classpath_index(0);
302 } else {
303 ResourceMark rm;
304 log_info(cds)("Skipping %s because it is dynamically generated", k->name()->as_C_string());
305 return true; // exclude without warning
306 }
307 } else {
308 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
309 // agent during dump time).
310 return warn_excluded(k, "Unsupported location");
311 }
312 }
313 if (k->signers() != nullptr) {
314 // We cannot include signed classes in the archive because the certificates
315 // used during dump time may be different than those used during
316 // runtime (due to expiration, etc).
317 return warn_excluded(k, "Signed JAR");
318 }
319 if (is_jfr_event_class(k)) {
320 // We cannot include JFR event classes because they need runtime-specific
321 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false.
322 // There are only a small number of these classes, so it's not worthwhile to
323 // support them and make CDS more complicated.
324 return warn_excluded(k, "JFR event class");
325 }
326
327 if (!k->is_linked()) {
328 if (has_class_failed_verification(k)) {
329 return warn_excluded(k, "Failed verification");
330 }
331 } else {
332 if (!k->can_be_verified_at_dumptime()) {
333 // We have an old class that has been linked (e.g., it's been executed during
334 // dump time). This class has been verified using the old verifier, which
335 // doesn't save the verification constraints, so check_verification_constraints()
336 // won't work at runtime.
337 // As a result, we cannot store this class. It must be loaded and fully verified
338 // at runtime.
339 return warn_excluded(k, "Old class has been linked");
340 }
341 }
342
343 if (k->is_hidden() && !should_hidden_class_be_archived(k)) {
344 log_info(cds)("Skipping %s: Hidden class", k->name()->as_C_string());
345 return true;
346 }
347
348 InstanceKlass* super = k->java_super();
349 if (super != nullptr && check_for_exclusion(super, nullptr)) {
350 ResourceMark rm;
351 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
352 return true;
353 }
354
355 Array<InstanceKlass*>* interfaces = k->local_interfaces();
356 int len = interfaces->length();
357 for (int i = 0; i < len; i++) {
358 InstanceKlass* intf = interfaces->at(i);
359 if (check_for_exclusion(intf, nullptr)) {
360 ResourceMark rm;
361 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
362 return true;
363 }
364 }
365
366 return false; // false == k should NOT be excluded
367 }
368
369 bool SystemDictionaryShared::is_builtin_loader(ClassLoaderData* loader_data) {
370 oop class_loader = loader_data->class_loader();
371 return (class_loader == nullptr ||
372 SystemDictionary::is_system_class_loader(class_loader) ||
373 SystemDictionary::is_platform_class_loader(class_loader));
374 }
375
376 bool SystemDictionaryShared::has_platform_or_app_classes() {
377 if (FileMapInfo::current_info()->has_platform_or_app_classes()) {
378 return true;
379 }
380 if (DynamicArchive::is_mapped() &&
381 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) {
382 return true;
383 }
384 return false;
385 }
522
523 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) {
524 assert(CDSConfig::is_dumping_archive(), "sanity");
525 assert(!is_builtin(k), "must be unregistered class");
526 DumpTimeClassInfo* info = get_info(k);
527 info->_clsfile_size = cfs->length();
528 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
529 }
530
531 void SystemDictionaryShared::initialize() {
532 if (CDSConfig::is_dumping_archive()) {
533 _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
534 _dumptime_lambda_proxy_class_dictionary =
535 new (mtClass) DumpTimeLambdaProxyClassDictionary;
536 }
537 }
538
539 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
540 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
541 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
542 _dumptime_table->allocate_info(k);
543 }
544
545 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
546 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
547 _dumptime_table->remove(k);
548 }
549
550 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
551 if (CDSConfig::is_dumping_archive()) {
552 remove_dumptime_info(klass);
553 }
554
555 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {
556 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag);
557 if (_unregistered_classes_table != nullptr) {
558 // Remove the class from _unregistered_classes_table: keep the entry but
559 // set it to null. This ensure no classes with the same name can be
560 // added again.
561 InstanceKlass** v = _unregistered_classes_table->get(klass->name());
562 if (v != nullptr) {
792 _dumptime_table->iterate_all_live_classes(check_hidden);
793 } while (made_progress);
794
795 // Now, all hidden classes that have not yet been scanned must be marked as excluded
796 auto exclude_remaining_hidden = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
797 if (k->is_hidden()) {
798 SystemDictionaryShared::check_for_exclusion(k, &info);
799 if (CDSConfig::is_dumping_invokedynamic()) {
800 if (should_hidden_class_be_archived(k)) {
801 guarantee(!info.is_excluded(), "Must be");
802 } else {
803 guarantee(info.is_excluded(), "Must be");
804 }
805 }
806 }
807 };
808 _dumptime_table->iterate_all_live_classes(exclude_remaining_hidden);
809 _dumptime_table->update_counts();
810
811 cleanup_lambda_proxy_class_dictionary();
812 }
813
814 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) {
815 assert(!class_loading_may_happen(), "class loading must be disabled");
816 assert_lock_strong(DumpTimeTable_lock);
817 assert(CDSConfig::is_dumping_archive(), "sanity");
818 DumpTimeClassInfo* p = get_info_locked(k);
819 return p->is_excluded();
820 }
821
822 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) {
823 assert_lock_strong(DumpTimeTable_lock);
824 assert(CDSConfig::is_dumping_archive(), "sanity");
825 DumpTimeClassInfo* info = get_info_locked(k);
826 info->set_excluded();
827 }
828
829 void SystemDictionaryShared::set_excluded(InstanceKlass* k) {
830 assert(CDSConfig::is_dumping_archive(), "sanity");
831 DumpTimeClassInfo* info = get_info(k);
832 info->set_excluded();
833 }
834
835 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) {
836 assert(CDSConfig::is_dumping_archive(), "sanity");
837 DumpTimeClassInfo* p = get_info(ik);
838 p->set_failed_verification();
839 }
840
841 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) {
842 assert(CDSConfig::is_dumping_archive(), "sanity");
843 DumpTimeClassInfo* p = _dumptime_table->get(ik);
844 return (p == nullptr) ? false : p->failed_verification();
845 }
846
847 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) {
848 assert_lock_strong(DumpTimeTable_lock);
849
850 auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
851 if (k->is_loader_alive() && !info.is_excluded()) {
852 info.metaspace_pointers_do(it);
853 }
854 };
855 _dumptime_table->iterate_all_live_classes(do_klass);
856
857 auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
858 if (key.caller_ik()->is_loader_alive()) {
859 info.metaspace_pointers_do(it);
860 key.metaspace_pointers_do(it);
861 }
862 };
863 _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);
864 }
865
866 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
867 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
868 assert(CDSConfig::is_dumping_archive(), "sanity");
869 DumpTimeClassInfo* info = get_info(k);
870 info->add_verification_constraint(k, name, from_name, from_field_is_protected,
871 from_is_array, from_is_object);
872
873 if (CDSConfig::is_dumping_dynamic_archive()) {
874 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
875 // the initial run prior to creating the archive before vm exit. We will also perform verification
876 // check when running with the archive.
877 return false;
878 } else {
879 if (is_builtin(k)) {
880 // For builtin class loaders, we can try to complete the verification check at dump time,
881 // because we can resolve all the constraint classes. We will also perform verification check
882 // when running with the archive.
883 return false;
884 } else {
885 // For non-builtin class loaders, we cannot complete the verification check at dump time,
886 // because at dump time we don't know how to resolve classes for such loaders.
887 return true;
888 }
903 DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
904 info->add_proxy_klass(proxy_klass);
905 if (created) {
906 ++_dumptime_lambda_proxy_class_dictionary->_count;
907 }
908 }
909
910 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
911 InstanceKlass* lambda_ik,
912 Symbol* invoked_name,
913 Symbol* invoked_type,
914 Symbol* method_type,
915 Method* member_method,
916 Symbol* instantiated_method_type,
917 TRAPS) {
918 if (CDSConfig::is_dumping_invokedynamic()) {
919 // The lambda proxy classes will be stored as part of aot-resolved constant pool entries.
920 // There's no need to remember them in a separate table.
921 return;
922 }
923
924 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
925 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
926 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
927
928 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
929
930 lambda_ik->assign_class_loader_type();
931 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
932 InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
933 assert(nest_host != nullptr, "unexpected nullptr nest_host");
934
935 DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
936 if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
937 // Don't include the lambda proxy if its nest host is not in the "linked" state.
938 && nest_host->is_linked()) {
939 // Set _is_archived_lambda_proxy in DumpTimeClassInfo so that the lambda_ik
940 // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
941 info->_is_archived_lambda_proxy = true;
942 info->set_nest_host(nest_host);
943
944 LambdaProxyClassKey key(caller_ik,
945 invoked_name,
946 invoked_type,
947 method_type,
948 member_method,
949 instantiated_method_type);
950 add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
951 }
952 }
953
954 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
955 Symbol* invoked_name,
956 Symbol* invoked_type,
957 Symbol* method_type,
958 Method* member_method,
959 Symbol* instantiated_method_type) {
960 if (!caller_ik->is_shared() ||
961 !invoked_name->is_shared() ||
962 !invoked_type->is_shared() ||
963 !method_type->is_shared() ||
964 !member_method->is_shared() ||
965 !instantiated_method_type->is_shared()) {
966 // These can't be represented as u4 offset, but we wouldn't have archived a lambda proxy in this case anyway.
967 return nullptr;
968 }
969
970 MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
971 RunTimeLambdaProxyClassKey key =
972 RunTimeLambdaProxyClassKey::init_for_runtime(caller_ik, invoked_name, invoked_type,
973 method_type, member_method, instantiated_method_type);
974
975 // Try to retrieve the lambda proxy class from static archive.
976 const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key);
977 InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info);
978 if (proxy_klass == nullptr) {
979 if (info != nullptr && log_is_enabled(Debug, cds)) {
999 }
1000
1001 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
1002 InstanceKlass* proxy_klass = nullptr;
1003 if (info != nullptr) {
1004 InstanceKlass* curr_klass = info->proxy_klass_head();
1005 InstanceKlass* prev_klass = curr_klass;
1006 if (curr_klass->lambda_proxy_is_available()) {
1007 while (curr_klass->next_link() != nullptr) {
1008 prev_klass = curr_klass;
1009 curr_klass = InstanceKlass::cast(curr_klass->next_link());
1010 }
1011 assert(curr_klass->is_hidden(), "must be");
1012 assert(curr_klass->lambda_proxy_is_available(), "must be");
1013
1014 prev_klass->set_next_link(nullptr);
1015 proxy_klass = curr_klass;
1016 proxy_klass->clear_lambda_proxy_is_available();
1017 if (log_is_enabled(Debug, cds)) {
1018 ResourceMark rm;
1019 log_debug(cds)("Loaded lambda proxy: %s ", proxy_klass->external_name());
1020 }
1021 }
1022 }
1023 return proxy_klass;
1024 }
1025
1026 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
1027 assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1028 RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
1029 return record->nest_host();
1030 }
1031
1032 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
1033 InstanceKlass* caller_ik, TRAPS) {
1034 Handle class_loader(THREAD, caller_ik->class_loader());
1035 Handle protection_domain;
1036 PackageEntry* pkg_entry = caller_ik->package();
1037 if (caller_ik->class_loader() != nullptr) {
1038 protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
1039 }
1058 // Add to class hierarchy, and do possible deoptimizations.
1059 loaded_lambda->add_to_hierarchy(THREAD);
1060 // But, do not add to dictionary.
1061
1062 loaded_lambda->link_class(CHECK_NULL);
1063 // notify jvmti
1064 if (JvmtiExport::should_post_class_load()) {
1065 JvmtiExport::post_class_load(THREAD, loaded_lambda);
1066 }
1067 if (class_load_start_event.should_commit()) {
1068 SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
1069 }
1070
1071 loaded_lambda->initialize(CHECK_NULL);
1072
1073 return loaded_lambda;
1074 }
1075
1076 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
1077 TRAPS) {
1078 assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1079 RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass);
1080
1081 int length = record->num_verifier_constraints();
1082 if (length > 0) {
1083 for (int i = 0; i < length; i++) {
1084 RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i);
1085 Symbol* name = vc->name();
1086 Symbol* from_name = vc->from_name();
1087 char c = record->verifier_constraint_flag(i);
1088
1089 if (log_is_enabled(Trace, cds, verification)) {
1090 ResourceMark rm(THREAD);
1091 log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]",
1092 klass->external_name(), from_name->as_klass_external_name(),
1093 name->as_klass_external_name(), c);
1094 }
1095
1096 bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false;
1097 bool from_is_array = (c & SystemDictionaryShared::FROM_IS_ARRAY) ? true : false;
1098 bool from_is_object = (c & SystemDictionaryShared::FROM_IS_OBJECT) ? true : false;
1168 assert(is_builtin(klass), "must be");
1169 assert(klass_loader != nullptr, "should not be called for boot loader");
1170 assert(loader1 != loader2, "must be");
1171
1172 if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) {
1173 // We are re-laying out the vtable/itables of the *copy* of
1174 // a class during the final stage of dynamic dumping. The
1175 // linking constraints for this class has already been recorded.
1176 return;
1177 }
1178 assert(!Thread::current()->is_VM_thread(), "must be");
1179
1180 assert(CDSConfig::is_dumping_archive(), "sanity");
1181 DumpTimeClassInfo* info = get_info(klass);
1182 info->record_linking_constraint(name, loader1, loader2);
1183 }
1184
1185 // returns true IFF there's no need to re-initialize the i/v-tables for klass for
1186 // the purpose of checking class loader constraints.
1187 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) {
1188 assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1189 LogTarget(Info, class, loader, constraints) log;
1190 if (klass->is_shared_boot_class()) {
1191 // No class loader constraint check performed for boot classes.
1192 return true;
1193 }
1194 if (klass->is_shared_platform_class() || klass->is_shared_app_class()) {
1195 RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass);
1196 assert(info != nullptr, "Sanity");
1197 if (info->num_loader_constraints() > 0) {
1198 HandleMark hm(current);
1199 for (int i = 0; i < info->num_loader_constraints(); i++) {
1200 RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i);
1201 Symbol* name = lc->constraint_name();
1202 Handle loader1(current, get_class_loader_by(lc->_loader_type1));
1203 Handle loader2(current, get_class_loader_by(lc->_loader_type2));
1204 if (log.is_enabled()) {
1205 ResourceMark rm(current);
1206 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s",
1207 klass->external_name(), name->as_C_string(),
1208 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),
1301 return _shared_class_info_size;
1302 }
1303 };
1304
1305 size_t SystemDictionaryShared::estimate_size_for_archive() {
1306 EstimateSizeForArchive est;
1307 _dumptime_table->iterate_all_live_classes(&est);
1308 size_t total_size = est.total() +
1309 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1310 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1311
1312 size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1313 total_size +=
1314 (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1315 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1316
1317 return total_size;
1318 }
1319
1320 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1321 if (ArchiveBuilder::is_active()) {
1322 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1323 unsigned int hash = primitive_hash<uintx>(offset);
1324 DEBUG_ONLY({
1325 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1326 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1327 }
1328 });
1329 return hash;
1330 } else {
1331 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1332 }
1333 }
1334
1335 class CopyLambdaProxyClassInfoToArchive : StackObj {
1336 CompactHashtableWriter* _writer;
1337 ArchiveBuilder* _builder;
1338 public:
1339 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1340 : _writer(writer), _builder(ArchiveBuilder::current()) {}
1341 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1466 }
1467
1468 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1469 for (auto id : EnumRange<vmClassID>{}) {
1470 soc->do_ptr(vmClasses::klass_addr_at(id));
1471 }
1472 }
1473
1474 const RunTimeClassInfo*
1475 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1476 if (!CDSConfig::is_using_archive() || !name->is_shared()) {
1477 // The names of all shared classes must also be a shared Symbol.
1478 return nullptr;
1479 }
1480
1481 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1482 const RunTimeClassInfo* record = nullptr;
1483 if (DynamicArchive::is_mapped()) {
1484 // Use the regenerated holder classes in the dynamic archive as they
1485 // have more methods than those in the base archive.
1486 if (name == vmSymbols::java_lang_invoke_Invokers_Holder() ||
1487 name == vmSymbols::java_lang_invoke_DirectMethodHandle_Holder() ||
1488 name == vmSymbols::java_lang_invoke_LambdaForm_Holder() ||
1489 name == vmSymbols::java_lang_invoke_DelegatingMethodHandle_Holder()) {
1490 record = dynamic_dict->lookup(name, hash, 0);
1491 if (record != nullptr) {
1492 return record;
1493 }
1494 }
1495 }
1496
1497 if (!MetaspaceShared::is_shared_dynamic(name)) {
1498 // The names of all shared classes in the static dict must also be in the
1499 // static archive
1500 record = static_dict->lookup(name, hash, 0);
1501 }
1502
1503 if (record == nullptr && DynamicArchive::is_mapped()) {
1504 record = dynamic_dict->lookup(name, hash, 0);
1505 }
1506
1507 return record;
1508 }
1509
1514 if (record != nullptr) {
1515 assert(!record->klass()->is_hidden(), "hidden class cannot be looked up by name");
1516 DEBUG_ONLY(check_klass_after_loading(record->klass());)
1517 // We did not save the classfile data of the generated LambdaForm invoker classes,
1518 // so we cannot support CLFH for such classes.
1519 if (record->klass()->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1520 return nullptr;
1521 }
1522 return record->klass();
1523 } else {
1524 return nullptr;
1525 }
1526 }
1527
1528 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1529 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1530 DumpTimeClassInfo* info = get_info(k);
1531 info->_id = id;
1532 }
1533
1534 static const char* class_loader_name_for_shared(Klass* k) {
1535 assert(k != nullptr, "Sanity");
1536 assert(k->is_shared(), "Must be");
1537 assert(k->is_instance_klass(), "Must be");
1538 InstanceKlass* ik = InstanceKlass::cast(k);
1539 if (ik->is_shared_boot_class()) {
1540 return "boot_loader";
1541 } else if (ik->is_shared_platform_class()) {
1542 return "platform_loader";
1543 } else if (ik->is_shared_app_class()) {
1544 return "app_loader";
1545 } else if (ik->is_shared_unregistered_class()) {
1546 return "unregistered_loader";
1547 } else {
1548 return "unknown loader";
1549 }
1550 }
1551
1552 class SharedDictionaryPrinter : StackObj {
1553 outputStream* _st;
1554 int _index;
1555 public:
1556 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1557
1558 void do_value(const RunTimeClassInfo* record) {
1559 ResourceMark rm;
1560 _st->print_cr("%4d: %s %s", _index++, record->klass()->external_name(),
1561 class_loader_name_for_shared(record->klass()));
1562 if (record->klass()->array_klasses() != nullptr) {
1563 record->klass()->array_klasses()->cds_print_value_on(_st);
1564 _st->cr();
1565 }
1566 }
1567 int index() const { return _index; }
1568 };
1569
1570 class SharedLambdaDictionaryPrinter : StackObj {
1571 outputStream* _st;
1572 int _index;
1573 public:
1574 SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1575
1576 void do_value(const RunTimeLambdaProxyClassInfo* record) {
1577 if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1578 ResourceMark rm;
1579 Klass* k = record->proxy_klass_head();
1580 while (k != nullptr) {
1581 _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1582 class_loader_name_for_shared(k));
1583 k = k->next_link();
1584 }
1585 }
1586 }
1587 };
1588
1589 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1590 outputStream* st) {
1591 st->print_cr("%sShared Dictionary", prefix);
1592 SharedDictionaryPrinter p(st);
1593 st->print_cr("%sShared Builtin Dictionary", prefix);
1594 _builtin_dictionary.iterate(&p);
1595 st->print_cr("%sShared Unregistered Dictionary", prefix);
1596 _unregistered_dictionary.iterate(&p);
1597 if (!_lambda_proxy_class_dictionary.empty()) {
1598 st->print_cr("%sShared Lambda Dictionary", prefix);
1599 SharedLambdaDictionaryPrinter ldp(st, p.index());
1600 _lambda_proxy_class_dictionary.iterate(&ldp);
1601 }
1602 }
1655 // must also be excluded.
1656 bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1657 SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1658
1659 for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1660 InstanceKlass* ik = info._proxy_klasses->at(i);
1661 if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1662 SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1663 info._proxy_klasses->remove_at(i);
1664 }
1665 }
1666 return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1667 }
1668 };
1669
1670 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1671 assert_lock_strong(DumpTimeTable_lock);
1672 CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1673 _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1674 }
|
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/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 "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/methodData.hpp"
68 #include "oops/trainingData.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()) ||
184 loader_data->add_class(ik);
185
186 // Get the package entry.
187 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
188
189 // Load and check super/interfaces, restore unshareable info
190 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain,
191 cfs, pkg_entry, THREAD);
192 if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) {
193 // TODO: clean up <ik> so it can be used again
194 return nullptr;
195 }
196
197 return shared_klass;
198 }
199
200 // Guaranteed to return non-null value for non-shared classes.
201 // k must not be a shared class.
202 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) {
203 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
204 //assert(!k->is_shared(), "sanity"); // FIXME new workflow
205 return get_info_locked(k);
206 }
207
208 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) {
209 assert_lock_strong(DumpTimeTable_lock);
210 //assert(!k->is_shared(), "sanity"); // FIXME new workflow
211 DumpTimeClassInfo* info = _dumptime_table->get_info(k);
212 assert(info != nullptr, "must be");
213 return info;
214 }
215
216 void SystemDictionaryShared::mark_required_hidden_class(InstanceKlass* k) {
217 assert(k->is_hidden(), "sanity");
218 DumpTimeClassInfo* info = _dumptime_table->get(k);
219 ResourceMark rm;
220 if (info != nullptr) {
221 info->set_is_required_hidden_class();
222 }
223 }
224
225 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) {
226 if (!CDSConfig::is_dumping_final_static_archive() && MetaspaceShared::is_in_shared_metaspace(k)) {
227 // We have reached a super type that's already in the base archive. Treat it
228 // as "not excluded".
229 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
230 return false;
231 }
232
233 if (info == nullptr) {
234 info = _dumptime_table->get(k);
235 assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table");
236 }
237
238 if (!info->has_checked_exclusion()) {
239 if (check_for_exclusion_impl(k)) {
240 info->set_excluded();
241 }
242 info->set_has_checked_exclusion();
243 }
244
245 return info->is_excluded();
246 }
272 if (info != nullptr) {
273 info->_is_archived_lambda_proxy = false;
274 info->set_excluded();
275 }
276 }
277
278 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) {
279 DumpTimeClassInfo* info = _dumptime_table->get(ik);
280 return (info != nullptr) ? info->is_early_klass() : false;
281 }
282
283 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
284 assert(ik->is_shared(), "applicable to only a shared class");
285 if (ik->is_hidden()) {
286 return true;
287 } else {
288 return false;
289 }
290 }
291
292 void SystemDictionaryShared::ignore_new_classes() {
293 _ignore_new_classes = true;
294 }
295
296
297 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) {
298 if (CDSConfig::is_dumping_final_static_archive() && k->is_shared_unregistered_class()
299 && k->is_shared()) {
300 return false; // Do not exclude: unregistered classes are passed from preimage to final image.
301 }
302
303 if (k->is_in_error_state()) {
304 return warn_excluded(k, "In error state");
305 }
306 if (k->is_scratch_class()) {
307 return warn_excluded(k, "A scratch class");
308 }
309 if (!k->is_loaded()) {
310 return warn_excluded(k, "Not in loaded state");
311 }
312 if (has_been_redefined(k)) {
313 return warn_excluded(k, "Has been redefined");
314 }
315 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
316 if (k->name()->starts_with("java/lang/invoke/BoundMethodHandle$Species_")) {
317 // This class is dynamically generated by the JDK
318 if (CDSConfig::is_dumping_aot_linked_classes()) {
319 k->set_shared_classpath_index(0);
320 } else {
321 ResourceMark rm;
322 log_info(cds)("Skipping %s because it is dynamically generated", k->name()->as_C_string());
323 return true; // exclude without warning
324 }
325 } else {
326 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
327 // agent during dump time).
328 return warn_excluded(k, "Unsupported location");
329 }
330 }
331 if (k->signers() != nullptr) {
332 // We cannot include signed classes in the archive because the certificates
333 // used during dump time may be different than those used during
334 // runtime (due to expiration, etc).
335 return warn_excluded(k, "Signed JAR");
336 }
337 if (is_jfr_event_class(k)) {
338 // We cannot include JFR event classes because they need runtime-specific
339 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false.
340 // There are only a small number of these classes, so it's not worthwhile to
341 // support them and make CDS more complicated.
342 if (!CDSConfig::is_dumping_reflection_data()) { // FIXME: !!! HACK !!!
343 return warn_excluded(k, "JFR event class");
344 }
345 }
346
347 if (!CDSConfig::preserve_all_dumptime_verification_states(k)) {
348 if (!k->is_linked()) {
349 if (has_class_failed_verification(k)) {
350 return warn_excluded(k, "Failed verification");
351 }
352 } else {
353 if (!k->can_be_verified_at_dumptime()) {
354 // We have an old class that has been linked (e.g., it's been executed during
355 // dump time). This class has been verified using the old verifier, which
356 // doesn't save the verification constraints, so check_verification_constraints()
357 // won't work at runtime.
358 // As a result, we cannot store this class. It must be loaded and fully verified
359 // at runtime.
360 return warn_excluded(k, "Old class has been linked");
361 }
362 }
363 }
364
365 if (k->is_hidden() && !should_hidden_class_be_archived(k)) {
366 log_info(cds)("Skipping %s: Hidden class", k->name()->as_C_string());
367 return true;
368 }
369
370 InstanceKlass* super = k->java_super();
371 if (super != nullptr && check_for_exclusion(super, nullptr)) {
372 ResourceMark rm;
373 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
374 return true;
375 }
376
377 Array<InstanceKlass*>* interfaces = k->local_interfaces();
378 int len = interfaces->length();
379 for (int i = 0; i < len; i++) {
380 InstanceKlass* intf = interfaces->at(i);
381 if (check_for_exclusion(intf, nullptr)) {
382 ResourceMark rm;
383 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
384 return true;
385 }
386 }
387
388 if (ClassLoaderExt::should_be_excluded(k)) {
389 ResourceMark rm;
390 log_info(cds)("Skipping %s: excluded via -XX:CacheOnlyClassesIn", k->name()->as_C_string());
391 return true;
392 }
393
394 return false; // false == k should NOT be excluded
395 }
396
397 bool SystemDictionaryShared::is_builtin_loader(ClassLoaderData* loader_data) {
398 oop class_loader = loader_data->class_loader();
399 return (class_loader == nullptr ||
400 SystemDictionary::is_system_class_loader(class_loader) ||
401 SystemDictionary::is_platform_class_loader(class_loader));
402 }
403
404 bool SystemDictionaryShared::has_platform_or_app_classes() {
405 if (FileMapInfo::current_info()->has_platform_or_app_classes()) {
406 return true;
407 }
408 if (DynamicArchive::is_mapped() &&
409 FileMapInfo::dynamic_info()->has_platform_or_app_classes()) {
410 return true;
411 }
412 return false;
413 }
550
551 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) {
552 assert(CDSConfig::is_dumping_archive(), "sanity");
553 assert(!is_builtin(k), "must be unregistered class");
554 DumpTimeClassInfo* info = get_info(k);
555 info->_clsfile_size = cfs->length();
556 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
557 }
558
559 void SystemDictionaryShared::initialize() {
560 if (CDSConfig::is_dumping_archive()) {
561 _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
562 _dumptime_lambda_proxy_class_dictionary =
563 new (mtClass) DumpTimeLambdaProxyClassDictionary;
564 }
565 }
566
567 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
568 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
569 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
570 DumpTimeClassInfo* info = _dumptime_table->allocate_info(k);
571 if (_ignore_new_classes) {
572 if (!LambdaFormInvokers::may_be_regenerated_class(k->name())) {
573 ResourceMark rm;
574 log_debug(cds)("Skipping %s: Class loaded for lambda form invoker regeneration", k->name()->as_C_string());
575 info->set_excluded();
576 }
577 }
578 }
579
580 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
581 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
582 _dumptime_table->remove(k);
583 }
584
585 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
586 if (CDSConfig::is_dumping_archive()) {
587 remove_dumptime_info(klass);
588 }
589
590 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {
591 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag);
592 if (_unregistered_classes_table != nullptr) {
593 // Remove the class from _unregistered_classes_table: keep the entry but
594 // set it to null. This ensure no classes with the same name can be
595 // added again.
596 InstanceKlass** v = _unregistered_classes_table->get(klass->name());
597 if (v != nullptr) {
827 _dumptime_table->iterate_all_live_classes(check_hidden);
828 } while (made_progress);
829
830 // Now, all hidden classes that have not yet been scanned must be marked as excluded
831 auto exclude_remaining_hidden = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
832 if (k->is_hidden()) {
833 SystemDictionaryShared::check_for_exclusion(k, &info);
834 if (CDSConfig::is_dumping_invokedynamic()) {
835 if (should_hidden_class_be_archived(k)) {
836 guarantee(!info.is_excluded(), "Must be");
837 } else {
838 guarantee(info.is_excluded(), "Must be");
839 }
840 }
841 }
842 };
843 _dumptime_table->iterate_all_live_classes(exclude_remaining_hidden);
844 _dumptime_table->update_counts();
845
846 cleanup_lambda_proxy_class_dictionary();
847
848 TrainingData::cleanup_training_data();
849 }
850
851 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) {
852 assert(!class_loading_may_happen(), "class loading must be disabled");
853 assert_lock_strong(DumpTimeTable_lock);
854 assert(CDSConfig::is_dumping_archive(), "sanity");
855 DumpTimeClassInfo* p = get_info_locked(k);
856 return p->is_excluded();
857 }
858
859 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) {
860 assert_lock_strong(DumpTimeTable_lock);
861 assert(CDSConfig::is_dumping_archive(), "sanity");
862 DumpTimeClassInfo* info = get_info_locked(k);
863 info->set_excluded();
864 }
865
866 void SystemDictionaryShared::set_excluded(InstanceKlass* k) {
867 assert(CDSConfig::is_dumping_archive(), "sanity");
868 DumpTimeClassInfo* info = get_info(k);
869 info->set_excluded();
870 }
871
872 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) {
873 assert(CDSConfig::is_dumping_archive(), "sanity");
874 DumpTimeClassInfo* p = get_info(ik);
875 p->set_failed_verification();
876 }
877
878 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) {
879 assert(CDSConfig::is_dumping_archive(), "sanity");
880 DumpTimeClassInfo* p = _dumptime_table->get(ik);
881 return (p == nullptr) ? false : p->failed_verification();
882 }
883
884 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) {
885 assert_lock_strong(DumpTimeTable_lock);
886
887 auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
888 if (CDSConfig::is_dumping_final_static_archive() && !k->is_loaded()) {
889 assert(k->is_shared_unregistered_class(), "must be");
890 info.metaspace_pointers_do(it);
891 } else if (k->is_loader_alive() && !info.is_excluded()) {
892 info.metaspace_pointers_do(it);
893 }
894 };
895 _dumptime_table->iterate_all_live_classes(do_klass);
896
897 auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
898 if (key.caller_ik()->is_loader_alive()) {
899 info.metaspace_pointers_do(it);
900 key.metaspace_pointers_do(it);
901 }
902 };
903 _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);
904 }
905
906 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
907 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
908 assert(CDSConfig::is_dumping_archive(), "sanity");
909 if (CDSConfig::is_dumping_dynamic_archive() && k->is_shared()) {
910 // k is a new class in the static archive, but one of its supertypes is an old class, so k wasn't
911 // verified during dump time. No need to record constraints as k won't be included in the dynamic archive.
912 return false;
913 }
914 if (CDSConfig::is_dumping_aot_linked_classes() && is_builtin(k)) {
915 // There's no need to save verification constraints
916 // TODO -- double check the logic before integrating into mainline!!
917 return false;
918 }
919
920 DumpTimeClassInfo* info = get_info(k);
921 info->add_verification_constraint(k, name, from_name, from_field_is_protected,
922 from_is_array, from_is_object);
923
924 if (CDSConfig::is_dumping_dynamic_archive()) {
925 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
926 // the initial run prior to creating the archive before vm exit. We will also perform verification
927 // check when running with the archive.
928 return false;
929 } else {
930 if (is_builtin(k)) {
931 // For builtin class loaders, we can try to complete the verification check at dump time,
932 // because we can resolve all the constraint classes. We will also perform verification check
933 // when running with the archive.
934 return false;
935 } else {
936 // For non-builtin class loaders, we cannot complete the verification check at dump time,
937 // because at dump time we don't know how to resolve classes for such loaders.
938 return true;
939 }
954 DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
955 info->add_proxy_klass(proxy_klass);
956 if (created) {
957 ++_dumptime_lambda_proxy_class_dictionary->_count;
958 }
959 }
960
961 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
962 InstanceKlass* lambda_ik,
963 Symbol* invoked_name,
964 Symbol* invoked_type,
965 Symbol* method_type,
966 Method* member_method,
967 Symbol* instantiated_method_type,
968 TRAPS) {
969 if (CDSConfig::is_dumping_invokedynamic()) {
970 // The lambda proxy classes will be stored as part of aot-resolved constant pool entries.
971 // There's no need to remember them in a separate table.
972 return;
973 }
974 if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
975 // TODO: not supported in new workflow
976 return;
977 }
978
979 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
980 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
981 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
982
983 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
984
985 lambda_ik->assign_class_loader_type();
986 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
987 InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
988 assert(nest_host != nullptr, "unexpected nullptr nest_host");
989
990 DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
991 if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
992 // Don't include the lambda proxy if its nest host is not in the "linked" state.
993 && nest_host->is_linked()) {
994 // Set _is_archived_lambda_proxy in DumpTimeClassInfo so that the lambda_ik
995 // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
996 info->_is_archived_lambda_proxy = true;
997 info->set_nest_host(nest_host);
998
999 LambdaProxyClassKey key(caller_ik,
1000 invoked_name,
1001 invoked_type,
1002 method_type,
1003 member_method,
1004 instantiated_method_type);
1005 add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
1006 }
1007 }
1008
1009 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
1010 Symbol* invoked_name,
1011 Symbol* invoked_type,
1012 Symbol* method_type,
1013 Method* member_method,
1014 Symbol* instantiated_method_type) {
1015 if (CDSConfig::is_dumping_final_static_archive()) {
1016 return nullptr;
1017 }
1018
1019 if (!caller_ik->is_shared() ||
1020 !invoked_name->is_shared() ||
1021 !invoked_type->is_shared() ||
1022 !method_type->is_shared() ||
1023 !member_method->is_shared() ||
1024 !instantiated_method_type->is_shared()) {
1025 // These can't be represented as u4 offset, but we wouldn't have archived a lambda proxy in this case anyway.
1026 return nullptr;
1027 }
1028
1029 MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
1030 RunTimeLambdaProxyClassKey key =
1031 RunTimeLambdaProxyClassKey::init_for_runtime(caller_ik, invoked_name, invoked_type,
1032 method_type, member_method, instantiated_method_type);
1033
1034 // Try to retrieve the lambda proxy class from static archive.
1035 const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key);
1036 InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info);
1037 if (proxy_klass == nullptr) {
1038 if (info != nullptr && log_is_enabled(Debug, cds)) {
1058 }
1059
1060 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
1061 InstanceKlass* proxy_klass = nullptr;
1062 if (info != nullptr) {
1063 InstanceKlass* curr_klass = info->proxy_klass_head();
1064 InstanceKlass* prev_klass = curr_klass;
1065 if (curr_klass->lambda_proxy_is_available()) {
1066 while (curr_klass->next_link() != nullptr) {
1067 prev_klass = curr_klass;
1068 curr_klass = InstanceKlass::cast(curr_klass->next_link());
1069 }
1070 assert(curr_klass->is_hidden(), "must be");
1071 assert(curr_klass->lambda_proxy_is_available(), "must be");
1072
1073 prev_klass->set_next_link(nullptr);
1074 proxy_klass = curr_klass;
1075 proxy_klass->clear_lambda_proxy_is_available();
1076 if (log_is_enabled(Debug, cds)) {
1077 ResourceMark rm;
1078 log_debug(cds)("Loaded lambda proxy: " PTR_FORMAT " %s ", p2i(proxy_klass), proxy_klass->external_name());
1079 }
1080 }
1081 }
1082 return proxy_klass;
1083 }
1084
1085 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
1086 assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1087 RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
1088 return record->nest_host();
1089 }
1090
1091 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
1092 InstanceKlass* caller_ik, TRAPS) {
1093 Handle class_loader(THREAD, caller_ik->class_loader());
1094 Handle protection_domain;
1095 PackageEntry* pkg_entry = caller_ik->package();
1096 if (caller_ik->class_loader() != nullptr) {
1097 protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
1098 }
1117 // Add to class hierarchy, and do possible deoptimizations.
1118 loaded_lambda->add_to_hierarchy(THREAD);
1119 // But, do not add to dictionary.
1120
1121 loaded_lambda->link_class(CHECK_NULL);
1122 // notify jvmti
1123 if (JvmtiExport::should_post_class_load()) {
1124 JvmtiExport::post_class_load(THREAD, loaded_lambda);
1125 }
1126 if (class_load_start_event.should_commit()) {
1127 SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
1128 }
1129
1130 loaded_lambda->initialize(CHECK_NULL);
1131
1132 return loaded_lambda;
1133 }
1134
1135 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
1136 TRAPS) {
1137 //assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1138 RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass);
1139
1140 int length = record->num_verifier_constraints();
1141 if (length > 0) {
1142 for (int i = 0; i < length; i++) {
1143 RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i);
1144 Symbol* name = vc->name();
1145 Symbol* from_name = vc->from_name();
1146 char c = record->verifier_constraint_flag(i);
1147
1148 if (log_is_enabled(Trace, cds, verification)) {
1149 ResourceMark rm(THREAD);
1150 log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]",
1151 klass->external_name(), from_name->as_klass_external_name(),
1152 name->as_klass_external_name(), c);
1153 }
1154
1155 bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false;
1156 bool from_is_array = (c & SystemDictionaryShared::FROM_IS_ARRAY) ? true : false;
1157 bool from_is_object = (c & SystemDictionaryShared::FROM_IS_OBJECT) ? true : false;
1227 assert(is_builtin(klass), "must be");
1228 assert(klass_loader != nullptr, "should not be called for boot loader");
1229 assert(loader1 != loader2, "must be");
1230
1231 if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) {
1232 // We are re-laying out the vtable/itables of the *copy* of
1233 // a class during the final stage of dynamic dumping. The
1234 // linking constraints for this class has already been recorded.
1235 return;
1236 }
1237 assert(!Thread::current()->is_VM_thread(), "must be");
1238
1239 assert(CDSConfig::is_dumping_archive(), "sanity");
1240 DumpTimeClassInfo* info = get_info(klass);
1241 info->record_linking_constraint(name, loader1, loader2);
1242 }
1243
1244 // returns true IFF there's no need to re-initialize the i/v-tables for klass for
1245 // the purpose of checking class loader constraints.
1246 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) {
1247 //assert(!CDSConfig::is_dumping_static_archive() && CDSConfig::is_using_archive(), "called at run time with CDS enabled only");
1248 LogTarget(Info, class, loader, constraints) log;
1249 if (klass->is_shared_boot_class()) {
1250 // No class loader constraint check performed for boot classes.
1251 return true;
1252 }
1253 if (klass->is_shared_platform_class() || klass->is_shared_app_class()) {
1254 RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass);
1255 assert(info != nullptr, "Sanity");
1256 if (info->num_loader_constraints() > 0) {
1257 HandleMark hm(current);
1258 for (int i = 0; i < info->num_loader_constraints(); i++) {
1259 RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i);
1260 Symbol* name = lc->constraint_name();
1261 Handle loader1(current, get_class_loader_by(lc->_loader_type1));
1262 Handle loader2(current, get_class_loader_by(lc->_loader_type2));
1263 if (log.is_enabled()) {
1264 ResourceMark rm(current);
1265 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s",
1266 klass->external_name(), name->as_C_string(),
1267 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),
1360 return _shared_class_info_size;
1361 }
1362 };
1363
1364 size_t SystemDictionaryShared::estimate_size_for_archive() {
1365 EstimateSizeForArchive est;
1366 _dumptime_table->iterate_all_live_classes(&est);
1367 size_t total_size = est.total() +
1368 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1369 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1370
1371 size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1372 total_size +=
1373 (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1374 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1375
1376 return total_size;
1377 }
1378
1379 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1380 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
1381 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1382 unsigned int hash = primitive_hash<uintx>(offset);
1383 DEBUG_ONLY({
1384 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1385 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1386 }
1387 });
1388 return hash;
1389 } else {
1390 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1391 }
1392 }
1393
1394 class CopyLambdaProxyClassInfoToArchive : StackObj {
1395 CompactHashtableWriter* _writer;
1396 ArchiveBuilder* _builder;
1397 public:
1398 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1399 : _writer(writer), _builder(ArchiveBuilder::current()) {}
1400 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1525 }
1526
1527 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1528 for (auto id : EnumRange<vmClassID>{}) {
1529 soc->do_ptr(vmClasses::klass_addr_at(id));
1530 }
1531 }
1532
1533 const RunTimeClassInfo*
1534 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1535 if (!CDSConfig::is_using_archive() || !name->is_shared()) {
1536 // The names of all shared classes must also be a shared Symbol.
1537 return nullptr;
1538 }
1539
1540 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1541 const RunTimeClassInfo* record = nullptr;
1542 if (DynamicArchive::is_mapped()) {
1543 // Use the regenerated holder classes in the dynamic archive as they
1544 // have more methods than those in the base archive.
1545 if (LambdaFormInvokers::may_be_regenerated_class(name)) {
1546 record = dynamic_dict->lookup(name, hash, 0);
1547 if (record != nullptr) {
1548 return record;
1549 }
1550 }
1551 }
1552
1553 if (!MetaspaceShared::is_shared_dynamic(name)) {
1554 // The names of all shared classes in the static dict must also be in the
1555 // static archive
1556 record = static_dict->lookup(name, hash, 0);
1557 }
1558
1559 if (record == nullptr && DynamicArchive::is_mapped()) {
1560 record = dynamic_dict->lookup(name, hash, 0);
1561 }
1562
1563 return record;
1564 }
1565
1570 if (record != nullptr) {
1571 assert(!record->klass()->is_hidden(), "hidden class cannot be looked up by name");
1572 DEBUG_ONLY(check_klass_after_loading(record->klass());)
1573 // We did not save the classfile data of the generated LambdaForm invoker classes,
1574 // so we cannot support CLFH for such classes.
1575 if (record->klass()->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1576 return nullptr;
1577 }
1578 return record->klass();
1579 } else {
1580 return nullptr;
1581 }
1582 }
1583
1584 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1585 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1586 DumpTimeClassInfo* info = get_info(k);
1587 info->_id = id;
1588 }
1589
1590 const char* SystemDictionaryShared::class_loader_name_for_shared(Klass* k) {
1591 assert(k != nullptr, "Sanity");
1592 assert(k->is_shared(), "Must be");
1593 assert(k->is_instance_klass(), "Must be");
1594 InstanceKlass* ik = InstanceKlass::cast(k);
1595 if (ik->is_shared_boot_class()) {
1596 return "boot_loader";
1597 } else if (ik->is_shared_platform_class()) {
1598 return "platform_loader";
1599 } else if (ik->is_shared_app_class()) {
1600 return "app_loader";
1601 } else if (ik->is_shared_unregistered_class()) {
1602 return "unregistered_loader";
1603 } else {
1604 return "unknown loader";
1605 }
1606 }
1607
1608 class SharedDictionaryPrinter : StackObj {
1609 outputStream* _st;
1610 int _index;
1611 public:
1612 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1613
1614 void do_value(const RunTimeClassInfo* record) {
1615 ResourceMark rm;
1616 _st->print_cr("%4d: %s %s", _index++, record->klass()->external_name(),
1617 SystemDictionaryShared::class_loader_name_for_shared(record->klass()));
1618 if (record->klass()->array_klasses() != nullptr) {
1619 record->klass()->array_klasses()->cds_print_value_on(_st);
1620 _st->cr();
1621 }
1622 }
1623 int index() const { return _index; }
1624 };
1625
1626 class SharedLambdaDictionaryPrinter : StackObj {
1627 outputStream* _st;
1628 int _index;
1629 public:
1630 SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1631
1632 void do_value(const RunTimeLambdaProxyClassInfo* record) {
1633 if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1634 ResourceMark rm;
1635 Klass* k = record->proxy_klass_head();
1636 while (k != nullptr) {
1637 _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1638 SystemDictionaryShared::class_loader_name_for_shared(k));
1639 k = k->next_link();
1640 }
1641 }
1642 }
1643 };
1644
1645 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1646 outputStream* st) {
1647 st->print_cr("%sShared Dictionary", prefix);
1648 SharedDictionaryPrinter p(st);
1649 st->print_cr("%sShared Builtin Dictionary", prefix);
1650 _builtin_dictionary.iterate(&p);
1651 st->print_cr("%sShared Unregistered Dictionary", prefix);
1652 _unregistered_dictionary.iterate(&p);
1653 if (!_lambda_proxy_class_dictionary.empty()) {
1654 st->print_cr("%sShared Lambda Dictionary", prefix);
1655 SharedLambdaDictionaryPrinter ldp(st, p.index());
1656 _lambda_proxy_class_dictionary.iterate(&ldp);
1657 }
1658 }
1711 // must also be excluded.
1712 bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1713 SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1714
1715 for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1716 InstanceKlass* ik = info._proxy_klasses->at(i);
1717 if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1718 SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1719 info._proxy_klasses->remove_at(i);
1720 }
1721 }
1722 return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1723 }
1724 };
1725
1726 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1727 assert_lock_strong(DumpTimeTable_lock);
1728 CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1729 _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1730 }
1731
1732 void SystemDictionaryShared::create_loader_positive_lookup_cache(TRAPS) {
1733 GrowableArray<InstanceKlass*> shared_classes_list;
1734 {
1735 // With static dumping, we have only a single Java thread (see JVM_StartThread) so
1736 // no no other threads should be loading classes. Otherwise, the code below may miss some
1737 // classes that are loaded concurrently.
1738 assert(CDSConfig::is_dumping_static_archive(), "no other threads should be loading classes");
1739
1740 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1741 _dumptime_table->iterate_all_classes_in_builtin_loaders([&](InstanceKlass* k, DumpTimeClassInfo& info) {
1742 if (!k->is_hidden() && !check_for_exclusion(k, &info)) {
1743 shared_classes_list.append(k);
1744 }
1745 }
1746 );
1747 }
1748
1749 InstanceKlass* ik = vmClasses::Class_klass();
1750 objArrayOop r = oopFactory::new_objArray(ik, shared_classes_list.length(), CHECK);
1751 objArrayHandle array_h(THREAD, r);
1752
1753 for (int i = 0; i < shared_classes_list.length(); i++) {
1754 oop mirror = shared_classes_list.at(i)->java_mirror();
1755 Handle mirror_h(THREAD, mirror);
1756 array_h->obj_at_put(i, mirror_h());
1757 }
1758
1759 TempNewSymbol method = SymbolTable::new_symbol("generatePositiveLookupCache");
1760 TempNewSymbol signature = SymbolTable::new_symbol("([Ljava/lang/Class;)V");
1761
1762 JavaCallArguments args(Handle(THREAD, SystemDictionary::java_system_loader()));
1763 args.push_oop(array_h);
1764 JavaValue result(T_VOID);
1765 JavaCalls::call_virtual(&result,
1766 vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(),
1767 method,
1768 signature,
1769 &args,
1770 CHECK);
1771
1772 if (HAS_PENDING_EXCEPTION) {
1773 Handle exc_handle(THREAD, PENDING_EXCEPTION);
1774 CLEAR_PENDING_EXCEPTION;
1775 ResourceMark rm(THREAD);
1776
1777 log_warning(cds)("Exception during AppClassLoader::generatePositiveLookupCache() call");
1778 LogStreamHandle(Debug, cds) log;
1779 if (log.is_enabled()) {
1780 java_lang_Throwable::print_stack_trace(exc_handle, &log);
1781 }
1782 return;
1783 }
1784 }
|