12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/archiveUtils.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/classListParser.hpp"
31 #include "cds/classListWriter.hpp"
32 #include "cds/dynamicArchive.hpp"
33 #include "cds/filemap.hpp"
34 #include "cds/cdsProtectionDomain.hpp"
35 #include "cds/dumpTimeClassInfo.inline.hpp"
36 #include "cds/metaspaceShared.hpp"
37 #include "cds/runTimeClassInfo.hpp"
38 #include "classfile/classFileStream.hpp"
39 #include "classfile/classLoader.hpp"
40 #include "classfile/classLoaderData.inline.hpp"
41 #include "classfile/classLoaderDataGraph.hpp"
42 #include "classfile/classLoaderExt.hpp"
43 #include "classfile/dictionary.hpp"
44 #include "classfile/javaClasses.hpp"
45 #include "classfile/javaClasses.inline.hpp"
46 #include "classfile/symbolTable.hpp"
47 #include "classfile/systemDictionary.hpp"
48 #include "classfile/systemDictionaryShared.hpp"
49 #include "classfile/verificationType.hpp"
50 #include "classfile/vmClasses.hpp"
51 #include "classfile/vmSymbols.hpp"
52 #include "interpreter/bootstrapInfo.hpp"
53 #include "jfr/jfrEvents.hpp"
54 #include "logging/log.hpp"
55 #include "logging/logStream.hpp"
56 #include "memory/allocation.hpp"
57 #include "memory/metadataFactory.hpp"
58 #include "memory/metaspaceClosure.hpp"
59 #include "memory/oopFactory.hpp"
60 #include "memory/resourceArea.hpp"
61 #include "memory/universe.hpp"
62 #include "oops/instanceKlass.hpp"
63 #include "oops/klass.inline.hpp"
64 #include "oops/objArrayKlass.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "oops/oopHandle.inline.hpp"
68 #include "oops/typeArrayOop.inline.hpp"
69 #include "runtime/arguments.hpp"
70 #include "runtime/handles.inline.hpp"
71 #include "runtime/java.hpp"
72 #include "runtime/javaCalls.hpp"
73 #include "runtime/mutexLocker.hpp"
74 #include "utilities/resourceHash.hpp"
75 #include "utilities/stringUtils.hpp"
76
77 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive;
78 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive;
79
80 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr;
81 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr;
82
83 // Used by NoClassLoadingMark
84 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;)
85
86 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
87 Symbol* class_name, Handle class_loader, TRAPS) {
88 assert(UseSharedSpaces, "must be");
89 InstanceKlass* ik = find_builtin_class(class_name);
90
91 if (ik != nullptr && !ik->shared_loading_failed()) {
92 if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class()) ||
93 (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) {
94 SharedClassLoadingMark slm(THREAD, ik);
95 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
96 Handle protection_domain =
97 CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL);
98 return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD);
99 }
100 }
101 return nullptr;
102 }
103
104 // This function is called for loading only UNREGISTERED classes
105 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name,
106 Handle class_loader,
107 Handle protection_domain,
108 const ClassFileStream* cfs,
109 TRAPS) {
110 if (!UseSharedSpaces) {
111 return nullptr;
112 }
113 if (class_name == nullptr) { // don't do this for hidden classes
114 return nullptr;
115 }
116 if (class_loader.is_null() ||
117 SystemDictionary::is_system_class_loader(class_loader()) ||
164 loader_data->add_class(ik);
165
166 // Get the package entry.
167 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
168
169 // Load and check super/interfaces, restore unshareable info
170 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain,
171 cfs, pkg_entry, THREAD);
172 if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) {
173 // TODO: clean up <ik> so it can be used again
174 return nullptr;
175 }
176
177 return shared_klass;
178 }
179
180 // Guaranteed to return non-null value for non-shared classes.
181 // k must not be a shared class.
182 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) {
183 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
184 assert(!k->is_shared(), "sanity");
185 return get_info_locked(k);
186 }
187
188 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) {
189 assert_lock_strong(DumpTimeTable_lock);
190 assert(!k->is_shared(), "sanity");
191 DumpTimeClassInfo* info = _dumptime_table->get_info(k);
192 assert(info != nullptr, "must be");
193 return info;
194 }
195
196 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) {
197 if (MetaspaceShared::is_in_shared_metaspace(k)) {
198 // We have reached a super type that's already in the base archive. Treat it
199 // as "not excluded".
200 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
201 return false;
202 }
203
204 if (info == nullptr) {
205 info = _dumptime_table->get(k);
206 assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table");
207 }
208
209 if (!info->has_checked_exclusion()) {
210 if (check_for_exclusion_impl(k)) {
211 info->set_excluded();
212 }
213 info->set_has_checked_exclusion();
214 }
215
216 return info->is_excluded();
217 }
243 if (info != nullptr) {
244 info->_is_archived_lambda_proxy = false;
245 info->set_excluded();
246 }
247 }
248
249 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) {
250 DumpTimeClassInfo* info = _dumptime_table->get(ik);
251 return (info != nullptr) ? info->is_early_klass() : false;
252 }
253
254 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
255 assert(ik->is_shared(), "applicable to only a shared class");
256 if (ik->is_hidden()) {
257 return true;
258 } else {
259 return false;
260 }
261 }
262
263 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) {
264 if (k->is_in_error_state()) {
265 return warn_excluded(k, "In error state");
266 }
267 if (k->is_scratch_class()) {
268 return warn_excluded(k, "A scratch class");
269 }
270 if (!k->is_loaded()) {
271 return warn_excluded(k, "Not in loaded state");
272 }
273 if (has_been_redefined(k)) {
274 return warn_excluded(k, "Has been redefined");
275 }
276 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
277 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
278 // agent during dump time).
279 return warn_excluded(k, "Unsupported location");
280 }
281 if (k->signers() != nullptr) {
282 // We cannot include signed classes in the archive because the certificates
283 // used during dump time may be different than those used during
284 // runtime (due to expiration, etc).
285 return warn_excluded(k, "Signed JAR");
286 }
287 if (is_jfr_event_class(k)) {
288 // We cannot include JFR event classes because they need runtime-specific
289 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false.
290 // There are only a small number of these classes, so it's not worthwhile to
291 // support them and make CDS more complicated.
292 return warn_excluded(k, "JFR event class");
293 }
294
295 if (!k->is_linked()) {
296 if (has_class_failed_verification(k)) {
297 return warn_excluded(k, "Failed verification");
298 }
299 } else {
300 if (!k->can_be_verified_at_dumptime()) {
301 // We have an old class that has been linked (e.g., it's been executed during
302 // dump time). This class has been verified using the old verifier, which
303 // doesn't save the verification constraints, so check_verification_constraints()
304 // won't work at runtime.
305 // As a result, we cannot store this class. It must be loaded and fully verified
306 // at runtime.
307 return warn_excluded(k, "Old class has been linked");
308 }
309 }
310
311 if (k->is_hidden() && !is_registered_lambda_proxy_class(k)) {
312 ResourceMark rm;
313 log_debug(cds)("Skipping %s: Hidden class", k->name()->as_C_string());
314 return true;
315 }
316
317 InstanceKlass* super = k->java_super();
318 if (super != nullptr && check_for_exclusion(super, nullptr)) {
319 ResourceMark rm;
320 log_warning(cds)("Skipping %s: super class %s is excluded", k->name()->as_C_string(), super->name()->as_C_string());
321 return true;
322 }
323
324 Array<InstanceKlass*>* interfaces = k->local_interfaces();
325 int len = interfaces->length();
326 for (int i = 0; i < len; i++) {
327 InstanceKlass* intf = interfaces->at(i);
328 if (check_for_exclusion(intf, nullptr)) {
329 ResourceMark rm;
330 log_warning(cds)("Skipping %s: interface %s is excluded", k->name()->as_C_string(), intf->name()->as_C_string());
331 return true;
332 }
333 }
334
485 // The VM is not trying to resolve a super type of parser->current_class_name().
486 // Instead, it's resolving an error class (because parser->current_class_name() has
487 // failed parsing or verification). Don't do anything here.
488 return nullptr;
489 }
490 }
491
492 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) {
493 assert(CDSConfig::is_dumping_archive(), "sanity");
494 assert(!is_builtin(k), "must be unregistered class");
495 DumpTimeClassInfo* info = get_info(k);
496 info->_clsfile_size = cfs->length();
497 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
498 }
499
500 void SystemDictionaryShared::initialize() {
501 if (CDSConfig::is_dumping_archive()) {
502 _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
503 _dumptime_lambda_proxy_class_dictionary =
504 new (mtClass) DumpTimeLambdaProxyClassDictionary;
505 }
506 }
507
508 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
509 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
510 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
511 _dumptime_table->allocate_info(k);
512 }
513
514 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
515 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
516 _dumptime_table->remove(k);
517 }
518
519 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
520 if (CDSConfig::is_dumping_archive()) {
521 remove_dumptime_info(klass);
522 }
523
524 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {
525 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag);
526 if (_unregistered_classes_table != nullptr) {
527 // Remove the class from _unregistered_classes_table: keep the entry but
528 // set it to null. This ensure no classes with the same name can be
529 // added again.
530 InstanceKlass** v = _unregistered_classes_table->get(klass->name());
531 if (v != nullptr) {
553 Array<InstanceKlass*>* interfaces = k->local_interfaces();
554 int len = interfaces->length();
555 for (int i = 0; i < len; i++) {
556 if (has_been_redefined(interfaces->at(i))) {
557 return true;
558 }
559 }
560 return false;
561 }
562
563 // k is a class before relocating by ArchiveBuilder
564 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) {
565 ResourceMark rm;
566 const char* name = k->name()->as_C_string();
567 DumpTimeClassInfo* info = _dumptime_table->get(k);
568 assert(!class_loading_may_happen(), "class loading must be disabled");
569 guarantee(info != nullptr, "Class %s must be entered into _dumptime_table", name);
570 guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name);
571 if (is_builtin(k)) {
572 if (k->is_hidden()) {
573 assert(is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name);
574 }
575 guarantee(!k->is_shared_unregistered_class(),
576 "Class loader type must be set for BUILTIN class %s", name);
577
578 } else {
579 guarantee(k->is_shared_unregistered_class(),
580 "Class loader type must not be set for UNREGISTERED class %s", name);
581 }
582 }
583
584 class UnregisteredClassesDuplicationChecker : StackObj {
585 GrowableArray<InstanceKlass*> _list;
586 Thread* _thread;
587 public:
588 UnregisteredClassesDuplicationChecker() : _thread(Thread::current()) {}
589
590 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) {
591 if (!SystemDictionaryShared::is_builtin(k)) {
592 _list.append(k);
625 void SystemDictionaryShared::check_excluded_classes() {
626 assert(!class_loading_may_happen(), "class loading must be disabled");
627 assert_lock_strong(DumpTimeTable_lock);
628
629 if (CDSConfig::is_dumping_dynamic_archive()) {
630 // Do this first -- if a base class is excluded due to duplication,
631 // all of its subclasses will also be excluded.
632 ResourceMark rm;
633 UnregisteredClassesDuplicationChecker dup_checker;
634 _dumptime_table->iterate_all_live_classes(&dup_checker);
635 dup_checker.mark_duplicated_classes();
636 }
637
638 auto check_for_exclusion = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
639 SystemDictionaryShared::check_for_exclusion(k, &info);
640 };
641 _dumptime_table->iterate_all_live_classes(check_for_exclusion);
642 _dumptime_table->update_counts();
643
644 cleanup_lambda_proxy_class_dictionary();
645 }
646
647 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) {
648 assert(!class_loading_may_happen(), "class loading must be disabled");
649 assert_lock_strong(DumpTimeTable_lock);
650 assert(CDSConfig::is_dumping_archive(), "sanity");
651 DumpTimeClassInfo* p = get_info_locked(k);
652 return p->is_excluded();
653 }
654
655 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) {
656 assert_lock_strong(DumpTimeTable_lock);
657 assert(CDSConfig::is_dumping_archive(), "sanity");
658 DumpTimeClassInfo* info = get_info_locked(k);
659 info->set_excluded();
660 }
661
662 void SystemDictionaryShared::set_excluded(InstanceKlass* k) {
663 assert(CDSConfig::is_dumping_archive(), "sanity");
664 DumpTimeClassInfo* info = get_info(k);
665 info->set_excluded();
666 }
667
668 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) {
669 assert(CDSConfig::is_dumping_archive(), "sanity");
670 DumpTimeClassInfo* p = get_info(ik);
671 p->set_failed_verification();
672 }
673
674 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) {
675 assert(CDSConfig::is_dumping_archive(), "sanity");
676 DumpTimeClassInfo* p = _dumptime_table->get(ik);
677 return (p == nullptr) ? false : p->failed_verification();
678 }
679
680 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) {
681 assert_lock_strong(DumpTimeTable_lock);
682
683 auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
684 if (k->is_loader_alive() && !info.is_excluded()) {
685 info.metaspace_pointers_do(it);
686 }
687 };
688 _dumptime_table->iterate_all_live_classes(do_klass);
689
690 auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
691 if (key.caller_ik()->is_loader_alive()) {
692 info.metaspace_pointers_do(it);
693 key.metaspace_pointers_do(it);
694 }
695 };
696 _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);
697 }
698
699 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
700 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
701 assert(CDSConfig::is_dumping_archive(), "sanity");
702 DumpTimeClassInfo* info = get_info(k);
703 info->add_verification_constraint(k, name, from_name, from_field_is_protected,
704 from_is_array, from_is_object);
705
706 if (CDSConfig::is_dumping_dynamic_archive()) {
707 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
708 // the initial run prior to creating the archive before vm exit. We will also perform verification
709 // check when running with the archive.
710 return false;
711 } else {
712 if (is_builtin(k)) {
713 // For builtin class loaders, we can try to complete the verification check at dump time,
714 // because we can resolve all the constraint classes. We will also perform verification check
715 // when running with the archive.
716 return false;
717 } else {
718 // For non-builtin class loaders, we cannot complete the verification check at dump time,
719 // because at dump time we don't know how to resolve classes for such loaders.
720 return true;
721 }
731 void SystemDictionaryShared::add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey& key,
732 InstanceKlass* proxy_klass) {
733 assert_lock_strong(DumpTimeTable_lock);
734
735 bool created;
736 DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
737 info->add_proxy_klass(proxy_klass);
738 if (created) {
739 ++_dumptime_lambda_proxy_class_dictionary->_count;
740 }
741 }
742
743 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
744 InstanceKlass* lambda_ik,
745 Symbol* invoked_name,
746 Symbol* invoked_type,
747 Symbol* method_type,
748 Method* member_method,
749 Symbol* instantiated_method_type,
750 TRAPS) {
751
752 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
753 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
754 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
755
756 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
757
758 lambda_ik->assign_class_loader_type();
759 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
760 InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
761 assert(nest_host != nullptr, "unexpected nullptr nest_host");
762
763 DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
764 if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
765 // Don't include the lambda proxy if its nest host is not in the "linked" state.
766 && nest_host->is_linked()) {
767 // Set _is_archived_lambda_proxy in DumpTimeClassInfo so that the lambda_ik
768 // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
769 info->_is_archived_lambda_proxy = true;
770 info->set_nest_host(nest_host);
771
772 LambdaProxyClassKey key(caller_ik,
773 invoked_name,
774 invoked_type,
775 method_type,
776 member_method,
777 instantiated_method_type);
778 add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
779 }
780 }
781
782 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
783 Symbol* invoked_name,
784 Symbol* invoked_type,
785 Symbol* method_type,
786 Method* member_method,
787 Symbol* instantiated_method_type) {
788 MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
789 LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type,
790 method_type, member_method, instantiated_method_type);
791
792 // Try to retrieve the lambda proxy class from static archive.
793 const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key);
794 InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info);
795 if (proxy_klass == nullptr) {
796 if (info != nullptr && log_is_enabled(Debug, cds)) {
797 ResourceMark rm;
798 log_debug(cds)("Used all static archived lambda proxy classes for: %s %s%s",
799 caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string());
800 }
801 } else {
802 return proxy_klass;
803 }
804
805 // Retrieving from static archive is unsuccessful, try dynamic archive.
806 info = _dynamic_archive.lookup_lambda_proxy_class(&key);
807 proxy_klass = retrieve_lambda_proxy_class(info);
816 }
817
818 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
819 InstanceKlass* proxy_klass = nullptr;
820 if (info != nullptr) {
821 InstanceKlass* curr_klass = info->proxy_klass_head();
822 InstanceKlass* prev_klass = curr_klass;
823 if (curr_klass->lambda_proxy_is_available()) {
824 while (curr_klass->next_link() != nullptr) {
825 prev_klass = curr_klass;
826 curr_klass = InstanceKlass::cast(curr_klass->next_link());
827 }
828 assert(curr_klass->is_hidden(), "must be");
829 assert(curr_klass->lambda_proxy_is_available(), "must be");
830
831 prev_klass->set_next_link(nullptr);
832 proxy_klass = curr_klass;
833 proxy_klass->clear_lambda_proxy_is_available();
834 if (log_is_enabled(Debug, cds)) {
835 ResourceMark rm;
836 log_debug(cds)("Loaded lambda proxy: %s ", proxy_klass->external_name());
837 }
838 }
839 }
840 return proxy_klass;
841 }
842
843 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
844 assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
845 RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
846 return record->nest_host();
847 }
848
849 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
850 InstanceKlass* caller_ik, TRAPS) {
851 Handle class_loader(THREAD, caller_ik->class_loader());
852 Handle protection_domain;
853 PackageEntry* pkg_entry = caller_ik->package();
854 if (caller_ik->class_loader() != nullptr) {
855 protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
856 }
875 // Add to class hierarchy, and do possible deoptimizations.
876 loaded_lambda->add_to_hierarchy(THREAD);
877 // But, do not add to dictionary.
878
879 loaded_lambda->link_class(CHECK_NULL);
880 // notify jvmti
881 if (JvmtiExport::should_post_class_load()) {
882 JvmtiExport::post_class_load(THREAD, loaded_lambda);
883 }
884 if (class_load_start_event.should_commit()) {
885 SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
886 }
887
888 loaded_lambda->initialize(CHECK_NULL);
889
890 return loaded_lambda;
891 }
892
893 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
894 TRAPS) {
895 assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
896 RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass);
897
898 int length = record->_num_verifier_constraints;
899 if (length > 0) {
900 for (int i = 0; i < length; i++) {
901 RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i);
902 Symbol* name = vc->name();
903 Symbol* from_name = vc->from_name();
904 char c = record->verifier_constraint_flag(i);
905
906 if (log_is_enabled(Trace, cds, verification)) {
907 ResourceMark rm(THREAD);
908 log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]",
909 klass->external_name(), from_name->as_klass_external_name(),
910 name->as_klass_external_name(), c);
911 }
912
913 bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false;
914 bool from_is_array = (c & SystemDictionaryShared::FROM_IS_ARRAY) ? true : false;
915 bool from_is_object = (c & SystemDictionaryShared::FROM_IS_OBJECT) ? true : false;
985 assert(is_builtin(klass), "must be");
986 assert(klass_loader != nullptr, "should not be called for boot loader");
987 assert(loader1 != loader2, "must be");
988
989 if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) {
990 // We are re-laying out the vtable/itables of the *copy* of
991 // a class during the final stage of dynamic dumping. The
992 // linking constraints for this class has already been recorded.
993 return;
994 }
995 assert(!Thread::current()->is_VM_thread(), "must be");
996
997 assert(CDSConfig::is_dumping_archive(), "sanity");
998 DumpTimeClassInfo* info = get_info(klass);
999 info->record_linking_constraint(name, loader1, loader2);
1000 }
1001
1002 // returns true IFF there's no need to re-initialize the i/v-tables for klass for
1003 // the purpose of checking class loader constraints.
1004 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) {
1005 assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
1006 LogTarget(Info, class, loader, constraints) log;
1007 if (klass->is_shared_boot_class()) {
1008 // No class loader constraint check performed for boot classes.
1009 return true;
1010 }
1011 if (klass->is_shared_platform_class() || klass->is_shared_app_class()) {
1012 RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass);
1013 assert(info != nullptr, "Sanity");
1014 if (info->_num_loader_constraints > 0) {
1015 HandleMark hm(current);
1016 for (int i = 0; i < info->_num_loader_constraints; i++) {
1017 RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i);
1018 Symbol* name = lc->constraint_name();
1019 Handle loader1(current, get_class_loader_by(lc->_loader_type1));
1020 Handle loader2(current, get_class_loader_by(lc->_loader_type2));
1021 if (log.is_enabled()) {
1022 ResourceMark rm(current);
1023 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s",
1024 klass->external_name(), name->as_C_string(),
1025 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),
1114 }
1115 }
1116
1117 size_t total() {
1118 return _shared_class_info_size;
1119 }
1120 };
1121
1122 size_t SystemDictionaryShared::estimate_size_for_archive() {
1123 EstimateSizeForArchive est;
1124 _dumptime_table->iterate_all_live_classes(&est);
1125 size_t total_size = est.total() +
1126 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1127 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1128
1129 size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1130 total_size +=
1131 (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1132 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1133
1134 return total_size;
1135 }
1136
1137 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1138 if (ArchiveBuilder::is_active()) {
1139 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1140 unsigned int hash = primitive_hash<uintx>(offset);
1141 DEBUG_ONLY({
1142 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1143 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1144 }
1145 });
1146 return hash;
1147 } else {
1148 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1149 }
1150 }
1151
1152 class CopyLambdaProxyClassInfoToArchive : StackObj {
1153 CompactHashtableWriter* _writer;
1154 ArchiveBuilder* _builder;
1155 public:
1156 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1157 : _writer(writer), _builder(ArchiveBuilder::current()) {}
1158 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1231 ResourceMark rm;
1232 log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name());
1233 }
1234
1235 // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo*
1236 InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass);
1237 RunTimeClassInfo::set_for(buffered_klass, record);
1238 }
1239 }
1240 };
1241
1242 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) {
1243 CompactHashtableStats stats;
1244 dictionary->reset();
1245 CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats);
1246 CopyLambdaProxyClassInfoToArchive copy(&writer);
1247 _dumptime_lambda_proxy_class_dictionary->iterate(©);
1248 writer.dump(dictionary, "lambda proxy class dictionary");
1249 }
1250
1251 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary,
1252 bool is_builtin) {
1253 CompactHashtableStats stats;
1254 dictionary->reset();
1255 CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats);
1256 CopySharedClassInfoToArchive copy(&writer, is_builtin);
1257 assert_lock_strong(DumpTimeTable_lock);
1258 _dumptime_table->iterate_all_live_classes(©);
1259 writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary");
1260 }
1261
1262 void SystemDictionaryShared::write_to_archive(bool is_static_archive) {
1263 ArchiveInfo* archive = get_archive(is_static_archive);
1264
1265 write_dictionary(&archive->_builtin_dictionary, true);
1266 write_dictionary(&archive->_unregistered_dictionary, false);
1267
1268 write_lambda_proxy_class_dictionary(&archive->_lambda_proxy_class_dictionary);
1269 }
1270
1271 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() {
1272 AdjustLambdaProxyClassInfo adjuster;
1273 _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster);
1274 }
1275
1276 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
1277 bool is_static_archive) {
1278 ArchiveInfo* archive = get_archive(is_static_archive);
1279
1280 archive->_builtin_dictionary.serialize_header(soc);
1281 archive->_unregistered_dictionary.serialize_header(soc);
1282 archive->_lambda_proxy_class_dictionary.serialize_header(soc);
1283 }
1284
1285 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1286 for (auto id : EnumRange<vmClassID>{}) {
1287 soc->do_ptr(vmClasses::klass_addr_at(id));
1288 }
1289 }
1290
1291 const RunTimeClassInfo*
1292 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1293 if (!UseSharedSpaces || !name->is_shared()) {
1294 // The names of all shared classes must also be a shared Symbol.
1295 return nullptr;
1296 }
1297
1298 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1299 const RunTimeClassInfo* record = nullptr;
1300 if (DynamicArchive::is_mapped()) {
1301 // Use the regenerated holder classes in the dynamic archive as they
1302 // have more methods than those in the base archive.
1303 if (name == vmSymbols::java_lang_invoke_Invokers_Holder() ||
1304 name == vmSymbols::java_lang_invoke_DirectMethodHandle_Holder() ||
1305 name == vmSymbols::java_lang_invoke_LambdaForm_Holder() ||
1306 name == vmSymbols::java_lang_invoke_DelegatingMethodHandle_Holder()) {
1307 record = dynamic_dict->lookup(name, hash, 0);
1308 if (record != nullptr) {
1309 return record;
1310 }
1311 }
1312 }
1313
1314 if (!MetaspaceShared::is_shared_dynamic(name)) {
1315 // The names of all shared classes in the static dict must also be in the
1316 // static archive
1317 record = static_dict->lookup(name, hash, 0);
1318 }
1319
1320 if (record == nullptr && DynamicArchive::is_mapped()) {
1321 record = dynamic_dict->lookup(name, hash, 0);
1322 }
1323
1324 return record;
1325 }
1326
1331 if (record != nullptr) {
1332 assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name");
1333 assert(check_alignment(record->_klass), "Address not aligned");
1334 // We did not save the classfile data of the generated LambdaForm invoker classes,
1335 // so we cannot support CLFH for such classes.
1336 if (record->_klass->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1337 return nullptr;
1338 }
1339 return record->_klass;
1340 } else {
1341 return nullptr;
1342 }
1343 }
1344
1345 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1346 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1347 DumpTimeClassInfo* info = get_info(k);
1348 info->_id = id;
1349 }
1350
1351 static const char* class_loader_name_for_shared(Klass* k) {
1352 assert(k != nullptr, "Sanity");
1353 assert(k->is_shared(), "Must be");
1354 assert(k->is_instance_klass(), "Must be");
1355 InstanceKlass* ik = InstanceKlass::cast(k);
1356 if (ik->is_shared_boot_class()) {
1357 return "boot_loader";
1358 } else if (ik->is_shared_platform_class()) {
1359 return "platform_loader";
1360 } else if (ik->is_shared_app_class()) {
1361 return "app_loader";
1362 } else if (ik->is_shared_unregistered_class()) {
1363 return "unregistered_loader";
1364 } else {
1365 return "unknown loader";
1366 }
1367 }
1368
1369 class SharedDictionaryPrinter : StackObj {
1370 outputStream* _st;
1371 int _index;
1372 public:
1373 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1374
1375 void do_value(const RunTimeClassInfo* record) {
1376 ResourceMark rm;
1377 _st->print_cr("%4d: %s %s", _index++, record->_klass->external_name(),
1378 class_loader_name_for_shared(record->_klass));
1379 if (record->_klass->array_klasses() != nullptr) {
1380 record->_klass->array_klasses()->cds_print_value_on(_st);
1381 _st->cr();
1382 }
1383 }
1384 int index() const { return _index; }
1385 };
1386
1387 class SharedLambdaDictionaryPrinter : StackObj {
1388 outputStream* _st;
1389 int _index;
1390 public:
1391 SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1392
1393 void do_value(const RunTimeLambdaProxyClassInfo* record) {
1394 if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1395 ResourceMark rm;
1396 Klass* k = record->proxy_klass_head();
1397 while (k != nullptr) {
1398 _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1399 class_loader_name_for_shared(k));
1400 k = k->next_link();
1401 }
1402 }
1403 }
1404 };
1405
1406 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1407 outputStream* st) {
1408 st->print_cr("%sShared Dictionary", prefix);
1409 SharedDictionaryPrinter p(st);
1410 st->print_cr("%sShared Builtin Dictionary", prefix);
1411 _builtin_dictionary.iterate(&p);
1412 st->print_cr("%sShared Unregistered Dictionary", prefix);
1413 _unregistered_dictionary.iterate(&p);
1414 if (!_lambda_proxy_class_dictionary.empty()) {
1415 st->print_cr("%sShared Lambda Dictionary", prefix);
1416 SharedLambdaDictionaryPrinter ldp(st, p.index());
1417 _lambda_proxy_class_dictionary.iterate(&ldp);
1418 }
1419 }
1420
1421 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix,
1422 outputStream* st) {
1423 st->print_cr("%sArchve Statistics", prefix);
1424 _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary");
1425 _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
1426 _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary");
1427 }
1428
1429 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) {
1430 if (UseSharedSpaces) {
1431 if (is_static) {
1432 _static_archive.print_on("", st);
1433 } else {
1434 if (DynamicArchive::is_mapped()) {
1435 _dynamic_archive.print_on("Dynamic ", st);
1436 }
1437 }
1438 }
1439 }
1440
1441 void SystemDictionaryShared::print_on(outputStream* st) {
1442 print_shared_archive(st, true);
1443 print_shared_archive(st, false);
1444 }
1445
1446 void SystemDictionaryShared::print_table_statistics(outputStream* st) {
1472 // must also be excluded.
1473 bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1474 SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1475
1476 for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1477 InstanceKlass* ik = info._proxy_klasses->at(i);
1478 if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1479 SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1480 info._proxy_klasses->remove_at(i);
1481 }
1482 }
1483 return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1484 }
1485 };
1486
1487 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1488 assert_lock_strong(DumpTimeTable_lock);
1489 CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1490 _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1491 }
|
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "cds/archiveBuilder.hpp"
27 #include "cds/archiveHeapLoader.hpp"
28 #include "cds/archiveUtils.hpp"
29 #include "cds/cdsConfig.hpp"
30 #include "cds/classListParser.hpp"
31 #include "cds/classListWriter.hpp"
32 #include "cds/classPreinitializer.hpp"
33 #include "cds/dynamicArchive.hpp"
34 #include "cds/filemap.hpp"
35 #include "cds/heapShared.hpp"
36 #include "cds/cdsProtectionDomain.hpp"
37 #include "cds/dumpTimeClassInfo.inline.hpp"
38 #include "cds/lambdaFormInvokers.inline.hpp"
39 #include "cds/metaspaceShared.hpp"
40 #include "cds/methodDataDictionary.hpp"
41 #include "cds/runTimeClassInfo.hpp"
42 #include "classfile/classFileStream.hpp"
43 #include "classfile/classLoader.hpp"
44 #include "classfile/classLoaderData.inline.hpp"
45 #include "classfile/classLoaderDataGraph.hpp"
46 #include "classfile/classLoaderExt.hpp"
47 #include "classfile/dictionary.hpp"
48 #include "classfile/javaClasses.hpp"
49 #include "classfile/javaClasses.inline.hpp"
50 #include "classfile/symbolTable.hpp"
51 #include "classfile/systemDictionary.hpp"
52 #include "classfile/systemDictionaryShared.hpp"
53 #include "classfile/verificationType.hpp"
54 #include "classfile/vmClasses.hpp"
55 #include "classfile/vmSymbols.hpp"
56 #include "interpreter/bootstrapInfo.hpp"
57 #include "jfr/jfrEvents.hpp"
58 #include "logging/log.hpp"
59 #include "logging/logStream.hpp"
60 #include "memory/allocation.hpp"
61 #include "memory/metadataFactory.hpp"
62 #include "memory/metaspaceClosure.hpp"
63 #include "memory/oopFactory.hpp"
64 #include "memory/resourceArea.hpp"
65 #include "memory/universe.hpp"
66 #include "oops/instanceKlass.hpp"
67 #include "oops/klass.inline.hpp"
68 #include "oops/methodData.hpp"
69 #include "oops/trainingData.hpp"
70 #include "oops/objArrayKlass.hpp"
71 #include "oops/objArrayOop.inline.hpp"
72 #include "oops/oop.inline.hpp"
73 #include "oops/oopHandle.inline.hpp"
74 #include "oops/typeArrayOop.inline.hpp"
75 #include "runtime/arguments.hpp"
76 #include "runtime/handles.inline.hpp"
77 #include "runtime/java.hpp"
78 #include "runtime/javaCalls.hpp"
79 #include "runtime/mutexLocker.hpp"
80 #include "utilities/resourceHash.hpp"
81 #include "utilities/stringUtils.hpp"
82
83 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_static_archive;
84 SystemDictionaryShared::ArchiveInfo SystemDictionaryShared::_dynamic_archive;
85
86 DumpTimeSharedClassTable* SystemDictionaryShared::_dumptime_table = nullptr;
87 DumpTimeLambdaProxyClassDictionary* SystemDictionaryShared::_dumptime_lambda_proxy_class_dictionary = nullptr;
88
89 DumpTimeMethodInfoDictionary* SystemDictionaryShared::_dumptime_method_info_dictionary = nullptr;
90 DumpTimeMethodInfoDictionary* SystemDictionaryShared::_cloned_dumptime_method_info_dictionary = nullptr;
91 static Array<InstanceKlass*>* _archived_lambda_form_classes = nullptr;
92 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_boot = nullptr;
93 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_boot2 = nullptr;
94 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_platform = nullptr;
95 static Array<InstanceKlass*>* _archived_lambda_proxy_classes_app = nullptr;
96 static bool _ignore_new_classes = false;
97
98 // Used by NoClassLoadingMark
99 DEBUG_ONLY(bool SystemDictionaryShared::_class_loading_may_happen = true;)
100
101 InstanceKlass* SystemDictionaryShared::load_shared_class_for_builtin_loader(
102 Symbol* class_name, Handle class_loader, TRAPS) {
103 assert(UseSharedSpaces, "must be");
104 InstanceKlass* ik = find_builtin_class(class_name);
105
106 if (ik != nullptr && !ik->shared_loading_failed()) {
107 if ((SystemDictionary::is_system_class_loader(class_loader()) && ik->is_shared_app_class()) ||
108 (SystemDictionary::is_platform_class_loader(class_loader()) && ik->is_shared_platform_class())) {
109 SharedClassLoadingMark slm(THREAD, ik);
110 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
111 Handle protection_domain;
112 if (!class_name->starts_with("jdk/proxy")) // java/lang/reflect/Proxy$ProxyBuilder defines the proxy classes with a null protection domain.
113 {
114 protection_domain = CDSProtectionDomain::init_security_info(class_loader, ik, pkg_entry, CHECK_NULL);
115 }
116 return load_shared_class(ik, class_loader, protection_domain, nullptr, pkg_entry, THREAD);
117 }
118 }
119 return nullptr;
120 }
121
122 // This function is called for loading only UNREGISTERED classes
123 InstanceKlass* SystemDictionaryShared::lookup_from_stream(Symbol* class_name,
124 Handle class_loader,
125 Handle protection_domain,
126 const ClassFileStream* cfs,
127 TRAPS) {
128 if (!UseSharedSpaces) {
129 return nullptr;
130 }
131 if (class_name == nullptr) { // don't do this for hidden classes
132 return nullptr;
133 }
134 if (class_loader.is_null() ||
135 SystemDictionary::is_system_class_loader(class_loader()) ||
182 loader_data->add_class(ik);
183
184 // Get the package entry.
185 PackageEntry* pkg_entry = CDSProtectionDomain::get_package_entry_from_class(ik, class_loader);
186
187 // Load and check super/interfaces, restore unshareable info
188 InstanceKlass* shared_klass = load_shared_class(ik, class_loader, protection_domain,
189 cfs, pkg_entry, THREAD);
190 if (shared_klass == nullptr || HAS_PENDING_EXCEPTION) {
191 // TODO: clean up <ik> so it can be used again
192 return nullptr;
193 }
194
195 return shared_klass;
196 }
197
198 // Guaranteed to return non-null value for non-shared classes.
199 // k must not be a shared class.
200 DumpTimeClassInfo* SystemDictionaryShared::get_info(InstanceKlass* k) {
201 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
202 //assert(!k->is_shared(), "sanity"); // FIXME new workflow
203 return get_info_locked(k);
204 }
205
206 DumpTimeClassInfo* SystemDictionaryShared::get_info_locked(InstanceKlass* k) {
207 assert_lock_strong(DumpTimeTable_lock);
208 //assert(!k->is_shared(), "sanity"); // FIXME new workflow
209 DumpTimeClassInfo* info = _dumptime_table->get_info(k);
210 assert(info != nullptr, "must be");
211 return info;
212 }
213
214 bool SystemDictionaryShared::check_for_exclusion(InstanceKlass* k, DumpTimeClassInfo* info) {
215 if (!CDSConfig::is_dumping_final_static_archive() && MetaspaceShared::is_in_shared_metaspace(k)) {
216 // We have reached a super type that's already in the base archive. Treat it
217 // as "not excluded".
218 assert(CDSConfig::is_dumping_dynamic_archive(), "must be");
219 return false;
220 }
221
222 if (info == nullptr) {
223 info = _dumptime_table->get(k);
224 assert(info != nullptr, "supertypes of any classes in _dumptime_table must either be shared, or must also be in _dumptime_table");
225 }
226
227 if (!info->has_checked_exclusion()) {
228 if (check_for_exclusion_impl(k)) {
229 info->set_excluded();
230 }
231 info->set_has_checked_exclusion();
232 }
233
234 return info->is_excluded();
235 }
261 if (info != nullptr) {
262 info->_is_archived_lambda_proxy = false;
263 info->set_excluded();
264 }
265 }
266
267 bool SystemDictionaryShared::is_early_klass(InstanceKlass* ik) {
268 DumpTimeClassInfo* info = _dumptime_table->get(ik);
269 return (info != nullptr) ? info->is_early_klass() : false;
270 }
271
272 bool SystemDictionaryShared::is_hidden_lambda_proxy(InstanceKlass* ik) {
273 assert(ik->is_shared(), "applicable to only a shared class");
274 if (ik->is_hidden()) {
275 return true;
276 } else {
277 return false;
278 }
279 }
280
281 void SystemDictionaryShared::ignore_new_classes() {
282 _ignore_new_classes = true;
283 }
284
285
286 bool SystemDictionaryShared::check_for_exclusion_impl(InstanceKlass* k) {
287 if (CDSConfig::is_dumping_final_static_archive() && k->is_shared_unregistered_class()
288 && k->is_shared()) {
289 return false; // Do not exclude: unregistered classes are passed from preimage to final image.
290 }
291
292 if (k->is_in_error_state()) {
293 return warn_excluded(k, "In error state");
294 }
295 if (k->is_scratch_class()) {
296 return warn_excluded(k, "A scratch class");
297 }
298 if (!k->is_loaded()) {
299 return warn_excluded(k, "Not in loaded state");
300 }
301 if (has_been_redefined(k)) {
302 return warn_excluded(k, "Has been redefined");
303 }
304 if (!k->is_hidden() && k->shared_classpath_index() < 0 && is_builtin(k)) {
305 // These are classes loaded from unsupported locations (such as those loaded by JVMTI native
306 // agent during dump time).
307 return warn_excluded(k, "Unsupported location");
308 }
309 if (k->signers() != nullptr) {
310 // We cannot include signed classes in the archive because the certificates
311 // used during dump time may be different than those used during
312 // runtime (due to expiration, etc).
313 return warn_excluded(k, "Signed JAR");
314 }
315 if (is_jfr_event_class(k)) {
316 // We cannot include JFR event classes because they need runtime-specific
317 // instrumentation in order to work with -XX:FlightRecorderOptions:retransform=false.
318 // There are only a small number of these classes, so it's not worthwhile to
319 // support them and make CDS more complicated.
320 if (!ArchiveReflectionData) { // FIXME: !!! HACK !!!
321 return warn_excluded(k, "JFR event class");
322 }
323 }
324
325 if (!CDSConfig::preserve_all_dumptime_verification_states(k)) {
326 if (!k->is_linked()) {
327 if (has_class_failed_verification(k)) {
328 return warn_excluded(k, "Failed verification");
329 }
330 } else {
331 if (!k->can_be_verified_at_dumptime()) {
332 // We have an old class that has been linked (e.g., it's been executed during
333 // dump time). This class has been verified using the old verifier, which
334 // doesn't save the verification constraints, so check_verification_constraints()
335 // won't work at runtime.
336 // As a result, we cannot store this class. It must be loaded and fully verified
337 // at runtime.
338 return warn_excluded(k, "Old class has been linked");
339 }
340 }
341 }
342
343 if (k->is_hidden() && !is_registered_lambda_proxy_class(k)) {
344 if (ArchiveInvokeDynamic && HeapShared::is_archivable_hidden_klass(k)) {
345 // Allow Lambda Proxy and LambdaForm classes, for ArchiveInvokeDynamic only
346 } else {
347 log_info(cds)("Skipping %s: Hidden class", k->name()->as_C_string());
348 return true;
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
520 // The VM is not trying to resolve a super type of parser->current_class_name().
521 // Instead, it's resolving an error class (because parser->current_class_name() has
522 // failed parsing or verification). Don't do anything here.
523 return nullptr;
524 }
525 }
526
527 void SystemDictionaryShared::set_shared_class_misc_info(InstanceKlass* k, ClassFileStream* cfs) {
528 assert(CDSConfig::is_dumping_archive(), "sanity");
529 assert(!is_builtin(k), "must be unregistered class");
530 DumpTimeClassInfo* info = get_info(k);
531 info->_clsfile_size = cfs->length();
532 info->_clsfile_crc32 = ClassLoader::crc32(0, (const char*)cfs->buffer(), cfs->length());
533 }
534
535 void SystemDictionaryShared::initialize() {
536 if (CDSConfig::is_dumping_archive()) {
537 _dumptime_table = new (mtClass) DumpTimeSharedClassTable;
538 _dumptime_lambda_proxy_class_dictionary =
539 new (mtClass) DumpTimeLambdaProxyClassDictionary;
540 _dumptime_method_info_dictionary = new (mtClass) DumpTimeMethodInfoDictionary;
541 }
542 }
543
544 void SystemDictionaryShared::init_dumptime_info(InstanceKlass* k) {
545 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
546 assert(SystemDictionaryShared::class_loading_may_happen(), "sanity");
547 DumpTimeClassInfo* info = _dumptime_table->allocate_info(k);
548 if (_ignore_new_classes) {
549 if (!LambdaFormInvokers::may_be_regenerated_class(k->name())) {
550 ResourceMark rm;
551 log_debug(cds)("Skipping %s: Class loaded for lambda form invoker regeneration", k->name()->as_C_string());
552 info->set_excluded();
553 }
554 }
555 }
556
557 void SystemDictionaryShared::remove_dumptime_info(InstanceKlass* k) {
558 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
559 _dumptime_table->remove(k);
560 }
561
562 void SystemDictionaryShared::handle_class_unloading(InstanceKlass* klass) {
563 if (CDSConfig::is_dumping_archive()) {
564 remove_dumptime_info(klass);
565 }
566
567 if (CDSConfig::is_dumping_archive() || ClassListWriter::is_enabled()) {
568 MutexLocker ml(Thread::current(), UnregisteredClassesTable_lock, Mutex::_no_safepoint_check_flag);
569 if (_unregistered_classes_table != nullptr) {
570 // Remove the class from _unregistered_classes_table: keep the entry but
571 // set it to null. This ensure no classes with the same name can be
572 // added again.
573 InstanceKlass** v = _unregistered_classes_table->get(klass->name());
574 if (v != nullptr) {
596 Array<InstanceKlass*>* interfaces = k->local_interfaces();
597 int len = interfaces->length();
598 for (int i = 0; i < len; i++) {
599 if (has_been_redefined(interfaces->at(i))) {
600 return true;
601 }
602 }
603 return false;
604 }
605
606 // k is a class before relocating by ArchiveBuilder
607 void SystemDictionaryShared::validate_before_archiving(InstanceKlass* k) {
608 ResourceMark rm;
609 const char* name = k->name()->as_C_string();
610 DumpTimeClassInfo* info = _dumptime_table->get(k);
611 assert(!class_loading_may_happen(), "class loading must be disabled");
612 guarantee(info != nullptr, "Class %s must be entered into _dumptime_table", name);
613 guarantee(!info->is_excluded(), "Should not attempt to archive excluded class %s", name);
614 if (is_builtin(k)) {
615 if (k->is_hidden()) {
616 if (ArchiveInvokeDynamic) { // FIXME -- clean up
617 return;
618 }
619 assert(is_registered_lambda_proxy_class(k), "unexpected hidden class %s", name);
620 }
621 guarantee(!k->is_shared_unregistered_class(),
622 "Class loader type must be set for BUILTIN class %s", name);
623
624 } else {
625 guarantee(k->is_shared_unregistered_class(),
626 "Class loader type must not be set for UNREGISTERED class %s", name);
627 }
628 }
629
630 class UnregisteredClassesDuplicationChecker : StackObj {
631 GrowableArray<InstanceKlass*> _list;
632 Thread* _thread;
633 public:
634 UnregisteredClassesDuplicationChecker() : _thread(Thread::current()) {}
635
636 void do_entry(InstanceKlass* k, DumpTimeClassInfo& info) {
637 if (!SystemDictionaryShared::is_builtin(k)) {
638 _list.append(k);
671 void SystemDictionaryShared::check_excluded_classes() {
672 assert(!class_loading_may_happen(), "class loading must be disabled");
673 assert_lock_strong(DumpTimeTable_lock);
674
675 if (CDSConfig::is_dumping_dynamic_archive()) {
676 // Do this first -- if a base class is excluded due to duplication,
677 // all of its subclasses will also be excluded.
678 ResourceMark rm;
679 UnregisteredClassesDuplicationChecker dup_checker;
680 _dumptime_table->iterate_all_live_classes(&dup_checker);
681 dup_checker.mark_duplicated_classes();
682 }
683
684 auto check_for_exclusion = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
685 SystemDictionaryShared::check_for_exclusion(k, &info);
686 };
687 _dumptime_table->iterate_all_live_classes(check_for_exclusion);
688 _dumptime_table->update_counts();
689
690 cleanup_lambda_proxy_class_dictionary();
691
692 cleanup_method_info_dictionary();
693
694 TrainingData::cleanup_training_data();
695 }
696
697 bool SystemDictionaryShared::is_excluded_class(InstanceKlass* k) {
698 assert(!class_loading_may_happen(), "class loading must be disabled");
699 assert_lock_strong(DumpTimeTable_lock);
700 assert(CDSConfig::is_dumping_archive(), "sanity");
701 DumpTimeClassInfo* p = get_info_locked(k);
702 return p->is_excluded();
703 }
704
705 void SystemDictionaryShared::set_excluded_locked(InstanceKlass* k) {
706 assert_lock_strong(DumpTimeTable_lock);
707 assert(CDSConfig::is_dumping_archive(), "sanity");
708 DumpTimeClassInfo* info = get_info_locked(k);
709 info->set_excluded();
710 }
711
712 void SystemDictionaryShared::set_excluded(InstanceKlass* k) {
713 assert(CDSConfig::is_dumping_archive(), "sanity");
714 DumpTimeClassInfo* info = get_info(k);
715 info->set_excluded();
716 }
717
718 void SystemDictionaryShared::set_class_has_failed_verification(InstanceKlass* ik) {
719 assert(CDSConfig::is_dumping_archive(), "sanity");
720 DumpTimeClassInfo* p = get_info(ik);
721 p->set_failed_verification();
722 }
723
724 bool SystemDictionaryShared::has_class_failed_verification(InstanceKlass* ik) {
725 assert(CDSConfig::is_dumping_archive(), "sanity");
726 DumpTimeClassInfo* p = _dumptime_table->get(ik);
727 return (p == nullptr) ? false : p->failed_verification();
728 }
729
730 void SystemDictionaryShared::dumptime_classes_do(class MetaspaceClosure* it) {
731 assert_lock_strong(DumpTimeTable_lock);
732
733 auto do_klass = [&] (InstanceKlass* k, DumpTimeClassInfo& info) {
734 if (CDSConfig::is_dumping_final_static_archive() && !k->is_loaded()) {
735 assert(k->is_shared_unregistered_class(), "must be");
736 info.metaspace_pointers_do(it);
737 } else if (k->is_loader_alive() && !info.is_excluded()) {
738 info.metaspace_pointers_do(it);
739 }
740 };
741 _dumptime_table->iterate_all_live_classes(do_klass);
742
743 auto do_lambda = [&] (LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
744 if (key.caller_ik()->is_loader_alive()) {
745 info.metaspace_pointers_do(it);
746 key.metaspace_pointers_do(it);
747 }
748 };
749 _dumptime_lambda_proxy_class_dictionary->iterate_all(do_lambda);
750
751 auto do_method_info = [&] (MethodDataKey& key, DumpTimeMethodDataInfo& info) {
752 info.metaspace_pointers_do(it);
753 key.metaspace_pointers_do(it);
754 };
755 _dumptime_method_info_dictionary->iterate_all(do_method_info);
756 }
757
758 bool SystemDictionaryShared::add_verification_constraint(InstanceKlass* k, Symbol* name,
759 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object) {
760 assert(CDSConfig::is_dumping_archive(), "sanity");
761 if (CDSConfig::is_dumping_dynamic_archive() && k->is_shared()) {
762 // k is a new class in the static archive, but one of its supertypes is an old class, so k wasn't
763 // verified during dump time. No need to record constraints as k won't be included in the dynamic archive.
764 return false;
765 }
766 if (PreloadSharedClasses && is_builtin(k)) {
767 // There's no need to save verification constraints
768 return false;
769 }
770
771 DumpTimeClassInfo* info = get_info(k);
772 info->add_verification_constraint(k, name, from_name, from_field_is_protected,
773 from_is_array, from_is_object);
774
775 if (CDSConfig::is_dumping_dynamic_archive()) {
776 // For dynamic dumping, we can resolve all the constraint classes for all class loaders during
777 // the initial run prior to creating the archive before vm exit. We will also perform verification
778 // check when running with the archive.
779 return false;
780 } else {
781 if (is_builtin(k)) {
782 // For builtin class loaders, we can try to complete the verification check at dump time,
783 // because we can resolve all the constraint classes. We will also perform verification check
784 // when running with the archive.
785 return false;
786 } else {
787 // For non-builtin class loaders, we cannot complete the verification check at dump time,
788 // because at dump time we don't know how to resolve classes for such loaders.
789 return true;
790 }
800 void SystemDictionaryShared::add_to_dump_time_lambda_proxy_class_dictionary(LambdaProxyClassKey& key,
801 InstanceKlass* proxy_klass) {
802 assert_lock_strong(DumpTimeTable_lock);
803
804 bool created;
805 DumpTimeLambdaProxyClassInfo* info = _dumptime_lambda_proxy_class_dictionary->put_if_absent(key, &created);
806 info->add_proxy_klass(proxy_klass);
807 if (created) {
808 ++_dumptime_lambda_proxy_class_dictionary->_count;
809 }
810 }
811
812 void SystemDictionaryShared::add_lambda_proxy_class(InstanceKlass* caller_ik,
813 InstanceKlass* lambda_ik,
814 Symbol* invoked_name,
815 Symbol* invoked_type,
816 Symbol* method_type,
817 Method* member_method,
818 Symbol* instantiated_method_type,
819 TRAPS) {
820 if (CDSConfig::is_dumping_static_archive() && ArchiveInvokeDynamic) {
821 // The proxy classes will be accessible through the archived CP entries.
822 return;
823 }
824 if (CDSConfig::is_dumping_preimage_static_archive() || CDSConfig::is_dumping_final_static_archive()) {
825 // TODO: not supported in new workflow
826 return;
827 }
828
829 assert(caller_ik->class_loader() == lambda_ik->class_loader(), "mismatched class loader");
830 assert(caller_ik->class_loader_data() == lambda_ik->class_loader_data(), "mismatched class loader data");
831 assert(java_lang_Class::class_data(lambda_ik->java_mirror()) == nullptr, "must not have class data");
832
833 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
834
835 lambda_ik->assign_class_loader_type();
836 lambda_ik->set_shared_classpath_index(caller_ik->shared_classpath_index());
837 InstanceKlass* nest_host = caller_ik->nest_host(CHECK);
838 assert(nest_host != nullptr, "unexpected nullptr nest_host");
839
840 DumpTimeClassInfo* info = _dumptime_table->get(lambda_ik);
841 if (info != nullptr && !lambda_ik->is_non_strong_hidden() && is_builtin(lambda_ik) && is_builtin(caller_ik)
842 // Don't include the lambda proxy if its nest host is not in the "linked" state.
843 && nest_host->is_linked()) {
844 // Set _is_archived_lambda_proxy in DumpTimeClassInfo so that the lambda_ik
845 // won't be excluded during dumping of shared archive. See ExcludeDumpTimeSharedClasses.
846 info->_is_archived_lambda_proxy = true;
847 info->set_nest_host(nest_host);
848
849 LambdaProxyClassKey key(caller_ik,
850 invoked_name,
851 invoked_type,
852 method_type,
853 member_method,
854 instantiated_method_type);
855 add_to_dump_time_lambda_proxy_class_dictionary(key, lambda_ik);
856 }
857 }
858
859 InstanceKlass* SystemDictionaryShared::get_shared_lambda_proxy_class(InstanceKlass* caller_ik,
860 Symbol* invoked_name,
861 Symbol* invoked_type,
862 Symbol* method_type,
863 Method* member_method,
864 Symbol* instantiated_method_type) {
865 if (CDSConfig::is_dumping_final_static_archive()) {
866 return nullptr;
867 }
868 MutexLocker ml(CDSLambda_lock, Mutex::_no_safepoint_check_flag);
869 LambdaProxyClassKey key(caller_ik, invoked_name, invoked_type,
870 method_type, member_method, instantiated_method_type);
871
872 // Try to retrieve the lambda proxy class from static archive.
873 const RunTimeLambdaProxyClassInfo* info = _static_archive.lookup_lambda_proxy_class(&key);
874 InstanceKlass* proxy_klass = retrieve_lambda_proxy_class(info);
875 if (proxy_klass == nullptr) {
876 if (info != nullptr && log_is_enabled(Debug, cds)) {
877 ResourceMark rm;
878 log_debug(cds)("Used all static archived lambda proxy classes for: %s %s%s",
879 caller_ik->external_name(), invoked_name->as_C_string(), invoked_type->as_C_string());
880 }
881 } else {
882 return proxy_klass;
883 }
884
885 // Retrieving from static archive is unsuccessful, try dynamic archive.
886 info = _dynamic_archive.lookup_lambda_proxy_class(&key);
887 proxy_klass = retrieve_lambda_proxy_class(info);
896 }
897
898 InstanceKlass* SystemDictionaryShared::retrieve_lambda_proxy_class(const RunTimeLambdaProxyClassInfo* info) {
899 InstanceKlass* proxy_klass = nullptr;
900 if (info != nullptr) {
901 InstanceKlass* curr_klass = info->proxy_klass_head();
902 InstanceKlass* prev_klass = curr_klass;
903 if (curr_klass->lambda_proxy_is_available()) {
904 while (curr_klass->next_link() != nullptr) {
905 prev_klass = curr_klass;
906 curr_klass = InstanceKlass::cast(curr_klass->next_link());
907 }
908 assert(curr_klass->is_hidden(), "must be");
909 assert(curr_klass->lambda_proxy_is_available(), "must be");
910
911 prev_klass->set_next_link(nullptr);
912 proxy_klass = curr_klass;
913 proxy_klass->clear_lambda_proxy_is_available();
914 if (log_is_enabled(Debug, cds)) {
915 ResourceMark rm;
916 log_debug(cds)("Loaded lambda proxy: " PTR_FORMAT " %s ", p2i(proxy_klass), proxy_klass->external_name());
917 }
918 }
919 }
920 return proxy_klass;
921 }
922
923 InstanceKlass* SystemDictionaryShared::get_shared_nest_host(InstanceKlass* lambda_ik) {
924 assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
925 RunTimeClassInfo* record = RunTimeClassInfo::get_for(lambda_ik);
926 return record->nest_host();
927 }
928
929 InstanceKlass* SystemDictionaryShared::prepare_shared_lambda_proxy_class(InstanceKlass* lambda_ik,
930 InstanceKlass* caller_ik, TRAPS) {
931 Handle class_loader(THREAD, caller_ik->class_loader());
932 Handle protection_domain;
933 PackageEntry* pkg_entry = caller_ik->package();
934 if (caller_ik->class_loader() != nullptr) {
935 protection_domain = CDSProtectionDomain::init_security_info(class_loader, caller_ik, pkg_entry, CHECK_NULL);
936 }
955 // Add to class hierarchy, and do possible deoptimizations.
956 loaded_lambda->add_to_hierarchy(THREAD);
957 // But, do not add to dictionary.
958
959 loaded_lambda->link_class(CHECK_NULL);
960 // notify jvmti
961 if (JvmtiExport::should_post_class_load()) {
962 JvmtiExport::post_class_load(THREAD, loaded_lambda);
963 }
964 if (class_load_start_event.should_commit()) {
965 SystemDictionary::post_class_load_event(&class_load_start_event, loaded_lambda, ClassLoaderData::class_loader_data(class_loader()));
966 }
967
968 loaded_lambda->initialize(CHECK_NULL);
969
970 return loaded_lambda;
971 }
972
973 void SystemDictionaryShared::check_verification_constraints(InstanceKlass* klass,
974 TRAPS) {
975 //assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
976 RunTimeClassInfo* record = RunTimeClassInfo::get_for(klass);
977
978 int length = record->_num_verifier_constraints;
979 if (length > 0) {
980 for (int i = 0; i < length; i++) {
981 RunTimeClassInfo::RTVerifierConstraint* vc = record->verifier_constraint_at(i);
982 Symbol* name = vc->name();
983 Symbol* from_name = vc->from_name();
984 char c = record->verifier_constraint_flag(i);
985
986 if (log_is_enabled(Trace, cds, verification)) {
987 ResourceMark rm(THREAD);
988 log_trace(cds, verification)("check_verification_constraint: %s: %s must be subclass of %s [0x%x]",
989 klass->external_name(), from_name->as_klass_external_name(),
990 name->as_klass_external_name(), c);
991 }
992
993 bool from_field_is_protected = (c & SystemDictionaryShared::FROM_FIELD_IS_PROTECTED) ? true : false;
994 bool from_is_array = (c & SystemDictionaryShared::FROM_IS_ARRAY) ? true : false;
995 bool from_is_object = (c & SystemDictionaryShared::FROM_IS_OBJECT) ? true : false;
1065 assert(is_builtin(klass), "must be");
1066 assert(klass_loader != nullptr, "should not be called for boot loader");
1067 assert(loader1 != loader2, "must be");
1068
1069 if (CDSConfig::is_dumping_dynamic_archive() && Thread::current()->is_VM_thread()) {
1070 // We are re-laying out the vtable/itables of the *copy* of
1071 // a class during the final stage of dynamic dumping. The
1072 // linking constraints for this class has already been recorded.
1073 return;
1074 }
1075 assert(!Thread::current()->is_VM_thread(), "must be");
1076
1077 assert(CDSConfig::is_dumping_archive(), "sanity");
1078 DumpTimeClassInfo* info = get_info(klass);
1079 info->record_linking_constraint(name, loader1, loader2);
1080 }
1081
1082 // returns true IFF there's no need to re-initialize the i/v-tables for klass for
1083 // the purpose of checking class loader constraints.
1084 bool SystemDictionaryShared::check_linking_constraints(Thread* current, InstanceKlass* klass) {
1085 //assert(!CDSConfig::is_dumping_static_archive() && UseSharedSpaces, "called at run time with CDS enabled only");
1086 LogTarget(Info, class, loader, constraints) log;
1087 if (klass->is_shared_boot_class()) {
1088 // No class loader constraint check performed for boot classes.
1089 return true;
1090 }
1091 if (klass->is_shared_platform_class() || klass->is_shared_app_class()) {
1092 RunTimeClassInfo* info = RunTimeClassInfo::get_for(klass);
1093 assert(info != nullptr, "Sanity");
1094 if (info->_num_loader_constraints > 0) {
1095 HandleMark hm(current);
1096 for (int i = 0; i < info->_num_loader_constraints; i++) {
1097 RunTimeClassInfo::RTLoaderConstraint* lc = info->loader_constraint_at(i);
1098 Symbol* name = lc->constraint_name();
1099 Handle loader1(current, get_class_loader_by(lc->_loader_type1));
1100 Handle loader2(current, get_class_loader_by(lc->_loader_type2));
1101 if (log.is_enabled()) {
1102 ResourceMark rm(current);
1103 log.print("[CDS add loader constraint for class %s symbol %s loader[0] %s loader[1] %s",
1104 klass->external_name(), name->as_C_string(),
1105 ClassLoaderData::class_loader_data(loader1())->loader_name_and_id(),
1194 }
1195 }
1196
1197 size_t total() {
1198 return _shared_class_info_size;
1199 }
1200 };
1201
1202 size_t SystemDictionaryShared::estimate_size_for_archive() {
1203 EstimateSizeForArchive est;
1204 _dumptime_table->iterate_all_live_classes(&est);
1205 size_t total_size = est.total() +
1206 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(true)) +
1207 CompactHashtableWriter::estimate_size(_dumptime_table->count_of(false));
1208
1209 size_t bytesize = align_up(sizeof(RunTimeLambdaProxyClassInfo), SharedSpaceObjectAlignment);
1210 total_size +=
1211 (bytesize * _dumptime_lambda_proxy_class_dictionary->_count) +
1212 CompactHashtableWriter::estimate_size(_dumptime_lambda_proxy_class_dictionary->_count);
1213
1214 size_t method_info_byte_size = align_up(sizeof(RunTimeMethodDataInfo), SharedSpaceObjectAlignment);
1215 total_size +=
1216 (method_info_byte_size * _dumptime_method_info_dictionary->_count) +
1217 CompactHashtableWriter::estimate_size(_dumptime_method_info_dictionary->_count);
1218
1219 return total_size;
1220 }
1221
1222 unsigned int SystemDictionaryShared::hash_for_shared_dictionary(address ptr) {
1223 if (ArchiveBuilder::is_active() && ArchiveBuilder::current()->is_in_buffer_space(ptr)) {
1224 uintx offset = ArchiveBuilder::current()->any_to_offset(ptr);
1225 unsigned int hash = primitive_hash<uintx>(offset);
1226 DEBUG_ONLY({
1227 if (MetaspaceObj::is_shared((const MetaspaceObj*)ptr)) {
1228 assert(hash == SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr), "must be");
1229 }
1230 });
1231 return hash;
1232 } else {
1233 return SystemDictionaryShared::hash_for_shared_dictionary_quick(ptr);
1234 }
1235 }
1236
1237 class CopyLambdaProxyClassInfoToArchive : StackObj {
1238 CompactHashtableWriter* _writer;
1239 ArchiveBuilder* _builder;
1240 public:
1241 CopyLambdaProxyClassInfoToArchive(CompactHashtableWriter* writer)
1242 : _writer(writer), _builder(ArchiveBuilder::current()) {}
1243 bool do_entry(LambdaProxyClassKey& key, DumpTimeLambdaProxyClassInfo& info) {
1316 ResourceMark rm;
1317 log_trace(cds,hashtables)("%s dictionary: %s", (_is_builtin ? "builtin" : "unregistered"), info._klass->external_name());
1318 }
1319
1320 // Save this for quick runtime lookup of InstanceKlass* -> RunTimeClassInfo*
1321 InstanceKlass* buffered_klass = ArchiveBuilder::current()->get_buffered_addr(info._klass);
1322 RunTimeClassInfo::set_for(buffered_klass, record);
1323 }
1324 }
1325 };
1326
1327 void SystemDictionaryShared::write_lambda_proxy_class_dictionary(LambdaProxyClassDictionary *dictionary) {
1328 CompactHashtableStats stats;
1329 dictionary->reset();
1330 CompactHashtableWriter writer(_dumptime_lambda_proxy_class_dictionary->_count, &stats);
1331 CopyLambdaProxyClassInfoToArchive copy(&writer);
1332 _dumptime_lambda_proxy_class_dictionary->iterate(©);
1333 writer.dump(dictionary, "lambda proxy class dictionary");
1334 }
1335
1336 class CopyMethodDataInfoToArchive : StackObj {
1337 CompactHashtableWriter* _writer;
1338 ArchiveBuilder* _builder;
1339 public:
1340 CopyMethodDataInfoToArchive(CompactHashtableWriter* writer)
1341 : _writer(writer), _builder(ArchiveBuilder::current()) {}
1342
1343 bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1344 Method* holder = key.method();
1345 log_info(cds,dynamic)("Archiving method info for %s", holder->external_name());
1346
1347 size_t byte_size = sizeof(RunTimeMethodDataInfo);
1348 RunTimeMethodDataInfo* record = (RunTimeMethodDataInfo*)ArchiveBuilder::ro_region_alloc(byte_size);
1349
1350 DumpTimeMethodDataInfo data(info.method_data(), info.method_counters());
1351 record->init(key, data);
1352
1353 uint hash = SystemDictionaryShared::hash_for_shared_dictionary((address)holder);
1354 u4 delta = _builder->buffer_to_offset_u4((address)record);
1355 _writer->add(hash, delta);
1356
1357 return true;
1358 }
1359 };
1360
1361 void SystemDictionaryShared::write_method_info_dictionary(MethodDataInfoDictionary* dictionary) {
1362 CompactHashtableStats stats;
1363 dictionary->reset();
1364 CompactHashtableWriter writer(_dumptime_method_info_dictionary->_count, &stats);
1365 CopyMethodDataInfoToArchive copy(&writer);
1366 _dumptime_method_info_dictionary->iterate(©);
1367 writer.dump(dictionary, "method info dictionary");
1368 }
1369
1370 void SystemDictionaryShared::write_dictionary(RunTimeSharedDictionary* dictionary,
1371 bool is_builtin) {
1372 CompactHashtableStats stats;
1373 dictionary->reset();
1374 CompactHashtableWriter writer(_dumptime_table->count_of(is_builtin), &stats);
1375 CopySharedClassInfoToArchive copy(&writer, is_builtin);
1376 assert_lock_strong(DumpTimeTable_lock);
1377 _dumptime_table->iterate_all_live_classes(©);
1378 writer.dump(dictionary, is_builtin ? "builtin dictionary" : "unregistered dictionary");
1379 }
1380
1381 void SystemDictionaryShared::write_to_archive(bool is_static_archive) {
1382 ArchiveInfo* archive = get_archive(is_static_archive);
1383
1384 write_dictionary(&archive->_builtin_dictionary, true);
1385 write_dictionary(&archive->_unregistered_dictionary, false);
1386
1387 write_lambda_proxy_class_dictionary(&archive->_lambda_proxy_class_dictionary);
1388
1389 write_method_info_dictionary(&archive->_method_info_dictionary);
1390 }
1391
1392 void SystemDictionaryShared::adjust_lambda_proxy_class_dictionary() {
1393 AdjustLambdaProxyClassInfo adjuster;
1394 _dumptime_lambda_proxy_class_dictionary->iterate(&adjuster);
1395 }
1396
1397 class AdjustMethodInfo : StackObj {
1398 public:
1399 AdjustMethodInfo() {}
1400 bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1401 // TODO: is it possible for the data to become stale/invalid?
1402 MethodData* md = info.method_data();
1403 MethodCounters* mc = info.method_counters();
1404 if (md != nullptr) {
1405 md = ArchiveBuilder::current()->get_buffered_addr(md);
1406 }
1407 if (mc != nullptr) {
1408 mc = ArchiveBuilder::current()->get_buffered_addr(mc);
1409 }
1410 assert(ArchiveBuilder::current()->is_in_buffer_space(md) || md == nullptr, "must be");
1411 assert(ArchiveBuilder::current()->is_in_buffer_space(mc) || mc == nullptr, "must be");
1412 if (md != nullptr) {
1413 md->remove_unshareable_info();
1414 }
1415 if (mc != nullptr) {
1416 mc->remove_unshareable_info();
1417 }
1418 return true;
1419 }
1420 };
1421
1422 void SystemDictionaryShared::adjust_method_info_dictionary() {
1423 AdjustMethodInfo adjuster;
1424 _dumptime_method_info_dictionary->iterate(&adjuster);
1425 }
1426
1427 void SystemDictionaryShared::serialize_dictionary_headers(SerializeClosure* soc,
1428 bool is_static_archive) {
1429 ArchiveInfo* archive = get_archive(is_static_archive);
1430
1431 archive->_builtin_dictionary.serialize_header(soc);
1432 archive->_unregistered_dictionary.serialize_header(soc);
1433 archive->_lambda_proxy_class_dictionary.serialize_header(soc);
1434 archive->_method_info_dictionary.serialize_header(soc);
1435 }
1436
1437 void SystemDictionaryShared::serialize_vm_classes(SerializeClosure* soc) {
1438 for (auto id : EnumRange<vmClassID>{}) {
1439 soc->do_ptr(vmClasses::klass_addr_at(id));
1440 }
1441 soc->do_ptr((void**)&_archived_lambda_form_classes);
1442 soc->do_ptr((void**)&_archived_lambda_proxy_classes_boot);
1443 soc->do_ptr((void**)&_archived_lambda_proxy_classes_boot2);
1444 soc->do_ptr((void**)&_archived_lambda_proxy_classes_platform);
1445 soc->do_ptr((void**)&_archived_lambda_proxy_classes_app);
1446 }
1447
1448 const RunTimeClassInfo*
1449 SystemDictionaryShared::find_record(RunTimeSharedDictionary* static_dict, RunTimeSharedDictionary* dynamic_dict, Symbol* name) {
1450 if (!UseSharedSpaces || !name->is_shared()) {
1451 // The names of all shared classes must also be a shared Symbol.
1452 return nullptr;
1453 }
1454
1455 unsigned int hash = SystemDictionaryShared::hash_for_shared_dictionary_quick(name);
1456 const RunTimeClassInfo* record = nullptr;
1457 if (DynamicArchive::is_mapped()) {
1458 // Use the regenerated holder classes in the dynamic archive as they
1459 // have more methods than those in the base archive.
1460 if (LambdaFormInvokers::may_be_regenerated_class(name)) {
1461 record = dynamic_dict->lookup(name, hash, 0);
1462 if (record != nullptr) {
1463 return record;
1464 }
1465 }
1466 }
1467
1468 if (!MetaspaceShared::is_shared_dynamic(name)) {
1469 // The names of all shared classes in the static dict must also be in the
1470 // static archive
1471 record = static_dict->lookup(name, hash, 0);
1472 }
1473
1474 if (record == nullptr && DynamicArchive::is_mapped()) {
1475 record = dynamic_dict->lookup(name, hash, 0);
1476 }
1477
1478 return record;
1479 }
1480
1485 if (record != nullptr) {
1486 assert(!record->_klass->is_hidden(), "hidden class cannot be looked up by name");
1487 assert(check_alignment(record->_klass), "Address not aligned");
1488 // We did not save the classfile data of the generated LambdaForm invoker classes,
1489 // so we cannot support CLFH for such classes.
1490 if (record->_klass->is_generated_shared_class() && JvmtiExport::should_post_class_file_load_hook()) {
1491 return nullptr;
1492 }
1493 return record->_klass;
1494 } else {
1495 return nullptr;
1496 }
1497 }
1498
1499 void SystemDictionaryShared::update_shared_entry(InstanceKlass* k, int id) {
1500 assert(CDSConfig::is_dumping_static_archive(), "class ID is used only for static dump (from classlist)");
1501 DumpTimeClassInfo* info = get_info(k);
1502 info->_id = id;
1503 }
1504
1505 const char* SystemDictionaryShared::class_loader_name_for_shared(Klass* k) {
1506 assert(k != nullptr, "Sanity");
1507 assert(k->is_shared(), "Must be");
1508 assert(k->is_instance_klass(), "Must be");
1509 InstanceKlass* ik = InstanceKlass::cast(k);
1510 if (ik->is_shared_boot_class()) {
1511 return "boot_loader";
1512 } else if (ik->is_shared_platform_class()) {
1513 return "platform_loader";
1514 } else if (ik->is_shared_app_class()) {
1515 return "app_loader";
1516 } else if (ik->is_shared_unregistered_class()) {
1517 return "unregistered_loader";
1518 } else {
1519 return "unknown loader";
1520 }
1521 }
1522
1523 class SharedDictionaryPrinter : StackObj {
1524 outputStream* _st;
1525 int _index;
1526 public:
1527 SharedDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1528
1529 void do_value(const RunTimeClassInfo* record) {
1530 ResourceMark rm;
1531 _st->print_cr("%4d: %s %s", _index++, record->_klass->external_name(),
1532 SystemDictionaryShared::class_loader_name_for_shared(record->_klass));
1533 if (record->_klass->array_klasses() != nullptr) {
1534 record->_klass->array_klasses()->cds_print_value_on(_st);
1535 _st->cr();
1536 }
1537 }
1538 int index() const { return _index; }
1539 };
1540
1541 class SharedLambdaDictionaryPrinter : StackObj {
1542 outputStream* _st;
1543 int _index;
1544 public:
1545 SharedLambdaDictionaryPrinter(outputStream* st, int idx) : _st(st), _index(idx) {}
1546
1547 void do_value(const RunTimeLambdaProxyClassInfo* record) {
1548 if (record->proxy_klass_head()->lambda_proxy_is_available()) {
1549 ResourceMark rm;
1550 Klass* k = record->proxy_klass_head();
1551 while (k != nullptr) {
1552 _st->print_cr("%4d: %s %s", _index++, k->external_name(),
1553 SystemDictionaryShared::class_loader_name_for_shared(k));
1554 k = k->next_link();
1555 }
1556 }
1557 }
1558 };
1559
1560 class SharedMethodInfoDictionaryPrinter : StackObj {
1561 outputStream* _st;
1562 int _index;
1563
1564 private:
1565 static const char* tag(void* p) {
1566 if (p == nullptr) {
1567 return " ";
1568 } else if (MetaspaceShared::is_shared_dynamic(p)) {
1569 return "<D>";
1570 } else if (MetaspaceShared::is_in_shared_metaspace(p)) {
1571 return "<S>";
1572 } else {
1573 return "???";
1574 }
1575 }
1576 public:
1577 SharedMethodInfoDictionaryPrinter(outputStream* st) : _st(st), _index(0) {}
1578
1579 void do_value(const RunTimeMethodDataInfo* record) {
1580 ResourceMark rm;
1581 Method* m = record->method();
1582 MethodCounters* mc = record->method_counters();
1583 MethodData* md = record->method_data();
1584
1585 _st->print_cr("%4d: %s" PTR_FORMAT " %s" PTR_FORMAT " %s" PTR_FORMAT " %s", _index++,
1586 tag(m), p2i(m),
1587 tag(mc), p2i(mc),
1588 tag(md), p2i(md),
1589 m->external_name());
1590 if (Verbose) {
1591 if (mc != nullptr) {
1592 mc->print_on(_st);
1593 }
1594 if (md != nullptr) {
1595 md->print_on(_st);
1596 }
1597 _st->cr();
1598 }
1599 }
1600 };
1601
1602 void SystemDictionaryShared::ArchiveInfo::print_on(const char* prefix,
1603 outputStream* st) {
1604 st->print_cr("%sShared Dictionary", prefix);
1605 SharedDictionaryPrinter p(st);
1606 st->print_cr("%sShared Builtin Dictionary", prefix);
1607 _builtin_dictionary.iterate(&p);
1608 st->print_cr("%sShared Unregistered Dictionary", prefix);
1609 _unregistered_dictionary.iterate(&p);
1610 if (!_lambda_proxy_class_dictionary.empty()) {
1611 st->print_cr("%sShared Lambda Dictionary", prefix);
1612 SharedLambdaDictionaryPrinter ldp(st, p.index());
1613 _lambda_proxy_class_dictionary.iterate(&ldp);
1614 }
1615 if (!_method_info_dictionary.empty()) {
1616 st->print_cr("%sShared MethodData Dictionary", prefix);
1617 SharedMethodInfoDictionaryPrinter mdp(st);
1618 _method_info_dictionary.iterate(&mdp);
1619 }
1620 }
1621
1622 void SystemDictionaryShared::ArchiveInfo::print_table_statistics(const char* prefix,
1623 outputStream* st) {
1624 st->print_cr("%sArchve Statistics", prefix);
1625 _builtin_dictionary.print_table_statistics(st, "Builtin Shared Dictionary");
1626 _unregistered_dictionary.print_table_statistics(st, "Unregistered Shared Dictionary");
1627 _lambda_proxy_class_dictionary.print_table_statistics(st, "Lambda Shared Dictionary");
1628 _method_info_dictionary.print_table_statistics(st, "MethodData Dictionary");
1629 }
1630
1631 void SystemDictionaryShared::print_shared_archive(outputStream* st, bool is_static) {
1632 if (UseSharedSpaces) {
1633 if (is_static) {
1634 _static_archive.print_on("", st);
1635 } else {
1636 if (DynamicArchive::is_mapped()) {
1637 _dynamic_archive.print_on("Dynamic ", st);
1638 }
1639 }
1640 }
1641 }
1642
1643 void SystemDictionaryShared::print_on(outputStream* st) {
1644 print_shared_archive(st, true);
1645 print_shared_archive(st, false);
1646 }
1647
1648 void SystemDictionaryShared::print_table_statistics(outputStream* st) {
1674 // must also be excluded.
1675 bool always_exclude = SystemDictionaryShared::check_for_exclusion(caller_ik, nullptr) ||
1676 SystemDictionaryShared::check_for_exclusion(nest_host, nullptr);
1677
1678 for (int i = info._proxy_klasses->length() - 1; i >= 0; i--) {
1679 InstanceKlass* ik = info._proxy_klasses->at(i);
1680 if (always_exclude || SystemDictionaryShared::check_for_exclusion(ik, nullptr)) {
1681 SystemDictionaryShared::reset_registered_lambda_proxy_class(ik);
1682 info._proxy_klasses->remove_at(i);
1683 }
1684 }
1685 return info._proxy_klasses->length() == 0 ? true /* delete the node*/ : false;
1686 }
1687 };
1688
1689 void SystemDictionaryShared::cleanup_lambda_proxy_class_dictionary() {
1690 assert_lock_strong(DumpTimeTable_lock);
1691 CleanupDumpTimeLambdaProxyClassTable cleanup_proxy_classes;
1692 _dumptime_lambda_proxy_class_dictionary->unlink(&cleanup_proxy_classes);
1693 }
1694
1695 class CleanupDumpTimeMethodInfoTable : StackObj {
1696 public:
1697 bool do_entry(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
1698 assert_lock_strong(DumpTimeTable_lock);
1699 assert(MetaspaceShared::is_in_shared_metaspace(key.method()), "");
1700 InstanceKlass* holder = key.method()->method_holder();
1701 bool is_excluded = SystemDictionaryShared::check_for_exclusion(holder, nullptr);
1702 return is_excluded;
1703 }
1704 };
1705
1706 void SystemDictionaryShared::cleanup_method_info_dictionary() {
1707 assert_lock_strong(DumpTimeTable_lock);
1708
1709 CleanupDumpTimeMethodInfoTable cleanup_method_info;
1710 _dumptime_method_info_dictionary->unlink(&cleanup_method_info);
1711 }
1712
1713 void SystemDictionaryShared::create_loader_positive_lookup_cache(TRAPS) {
1714 GrowableArray<InstanceKlass*> shared_classes_list;
1715 {
1716 MutexLocker ml(DumpTimeTable_lock, Mutex::_no_safepoint_check_flag);
1717 _dumptime_table->iterate_all_classes_in_builtin_loaders([&](InstanceKlass* k, DumpTimeClassInfo& info) {
1718 if (!check_for_exclusion(k, &info)) {
1719 shared_classes_list.append(k);
1720 }
1721 }
1722 );
1723 }
1724
1725 InstanceKlass* ik = vmClasses::Class_klass();
1726 objArrayOop r = oopFactory::new_objArray(ik, shared_classes_list.length(), CHECK);
1727 objArrayHandle array_h(THREAD, r);
1728
1729 for (int i = 0; i < shared_classes_list.length(); i++) {
1730 oop mirror = shared_classes_list.at(i)->java_mirror();
1731 Handle mirror_h(THREAD, mirror);
1732 array_h->obj_at_put(i, mirror_h());
1733 }
1734
1735 TempNewSymbol method = SymbolTable::new_symbol("generatePositiveLookupCache");
1736 TempNewSymbol signature = SymbolTable::new_symbol("([Ljava/lang/Class;)V");
1737
1738 JavaCallArguments args(Handle(THREAD, SystemDictionary::java_system_loader()));
1739 args.push_oop(array_h);
1740 JavaValue result(T_VOID);
1741 JavaCalls::call_virtual(&result,
1742 vmClasses::jdk_internal_loader_ClassLoaders_AppClassLoader_klass(),
1743 method,
1744 signature,
1745 &args,
1746 CHECK);
1747
1748 if (HAS_PENDING_EXCEPTION) {
1749 Handle exc_handle(THREAD, PENDING_EXCEPTION);
1750 CLEAR_PENDING_EXCEPTION;
1751 ResourceMark rm(THREAD);
1752
1753 log_warning(cds)("Exception during AppClassLoader::generatePositiveLookupCache() call");
1754 LogStreamHandle(Debug, cds) log;
1755 if (log.is_enabled()) {
1756 java_lang_Throwable::print_stack_trace(exc_handle, &log);
1757 }
1758 return;
1759 }
1760 }
|