47 #include "compiler/compileBroker.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "interpreter/bytecodeStream.hpp"
50 #include "interpreter/oopMapCache.hpp"
51 #include "interpreter/rewriter.hpp"
52 #include "jvm.h"
53 #include "jvmtifiles/jvmti.h"
54 #include "klass.inline.hpp"
55 #include "logging/log.hpp"
56 #include "logging/logMessage.hpp"
57 #include "logging/logStream.hpp"
58 #include "memory/allocation.inline.hpp"
59 #include "memory/iterator.inline.hpp"
60 #include "memory/metadataFactory.hpp"
61 #include "memory/metaspaceClosure.hpp"
62 #include "memory/oopFactory.hpp"
63 #include "memory/resourceArea.hpp"
64 #include "memory/universe.hpp"
65 #include "oops/constantPool.hpp"
66 #include "oops/fieldStreams.inline.hpp"
67 #include "oops/instanceClassLoaderKlass.hpp"
68 #include "oops/instanceKlass.inline.hpp"
69 #include "oops/instanceMirrorKlass.hpp"
70 #include "oops/instanceOop.hpp"
71 #include "oops/instanceStackChunkKlass.hpp"
72 #include "oops/klass.inline.hpp"
73 #include "oops/method.hpp"
74 #include "oops/oop.inline.hpp"
75 #include "oops/recordComponent.hpp"
76 #include "oops/symbol.hpp"
77 #include "prims/jvmtiExport.hpp"
78 #include "prims/jvmtiRedefineClasses.hpp"
79 #include "prims/jvmtiThreadState.hpp"
80 #include "prims/methodComparator.hpp"
81 #include "runtime/arguments.hpp"
82 #include "runtime/atomicAccess.hpp"
83 #include "runtime/deoptimization.hpp"
84 #include "runtime/fieldDescriptor.inline.hpp"
85 #include "runtime/handles.inline.hpp"
86 #include "runtime/javaCalls.hpp"
87 #include "runtime/javaThread.inline.hpp"
88 #include "runtime/mutexLocker.hpp"
89 #include "runtime/orderAccess.hpp"
90 #include "runtime/os.inline.hpp"
91 #include "runtime/reflection.hpp"
92 #include "runtime/synchronizer.hpp"
93 #include "runtime/threads.hpp"
94 #include "services/classLoadingService.hpp"
95 #include "services/finalizerService.hpp"
96 #include "services/threadService.hpp"
97 #include "utilities/dtrace.hpp"
98 #include "utilities/events.hpp"
99 #include "utilities/macros.hpp"
100 #include "utilities/nativeStackPrinter.hpp"
101 #include "utilities/stringUtils.hpp"
102 #ifdef COMPILER1
103 #include "c1/c1_Compiler.hpp"
104 #endif
105 #if INCLUDE_JFR
106 #include "jfr/jfrEvents.hpp"
107 #endif
108
109 #ifdef DTRACE_ENABLED
110
111
112 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
113 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
114 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
115 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
116 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
117 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
118 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
119 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
120 #define DTRACE_CLASSINIT_PROBE(type, thread_type) \
133 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait) \
134 { \
135 char* data = nullptr; \
136 int len = 0; \
137 Symbol* clss_name = name(); \
138 if (clss_name != nullptr) { \
139 data = (char*)clss_name->bytes(); \
140 len = clss_name->utf8_length(); \
141 } \
142 HOTSPOT_CLASS_INITIALIZATION_##type( \
143 data, len, (void*)class_loader(), thread_type, wait); \
144 }
145
146 #else // ndef DTRACE_ENABLED
147
148 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
149 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
150
151 #endif // ndef DTRACE_ENABLED
152
153 bool InstanceKlass::_finalization_enabled = true;
154 static int call_class_initializer_counter = 0; // for debugging
155
156 static inline bool is_class_loader(const Symbol* class_name,
157 const ClassFileParser& parser) {
158 assert(class_name != nullptr, "invariant");
159
160 if (class_name == vmSymbols::java_lang_ClassLoader()) {
161 return true;
162 }
163
164 if (vmClasses::ClassLoader_klass_is_loaded()) {
165 const Klass* const super_klass = parser.super_klass();
166 if (super_klass != nullptr) {
167 if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
168 return true;
169 }
170 }
171 }
172 return false;
173 }
174
175 static inline bool is_stack_chunk_class(const Symbol* class_name,
176 const ClassLoaderData* loader_data) {
177 return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
178 loader_data->is_the_null_class_loader_data());
179 }
180
181 // private: called to verify that k is a static member of this nest.
182 // We know that k is an instance class in the same package and hence the
183 // same classloader.
184 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
185 assert(!is_hidden(), "unexpected hidden class");
186 if (_nest_members == nullptr || _nest_members == Universe::the_empty_short_array()) {
187 if (log_is_enabled(Trace, class, nestmates)) {
188 ResourceMark rm(current);
189 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
190 k->external_name(), this->external_name());
191 }
192 return false;
193 }
194
442 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
443 this->external_name(),
444 access ? "" : "NOT ",
445 k->external_name());
446 return access;
447 }
448
449 const char* InstanceKlass::nest_host_error() {
450 if (_nest_host_index == 0) {
451 return nullptr;
452 } else {
453 constantPoolHandle cph(Thread::current(), constants());
454 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
455 }
456 }
457
458 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
459 const int size = InstanceKlass::size(parser.vtable_size(),
460 parser.itable_size(),
461 nonstatic_oop_map_size(parser.total_oop_map_count()),
462 parser.is_interface());
463
464 const Symbol* const class_name = parser.class_name();
465 assert(class_name != nullptr, "invariant");
466 ClassLoaderData* loader_data = parser.loader_data();
467 assert(loader_data != nullptr, "invariant");
468
469 InstanceKlass* ik;
470
471 // Allocation
472 if (parser.is_instance_ref_klass()) {
473 // java.lang.ref.Reference
474 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
475 } else if (class_name == vmSymbols::java_lang_Class()) {
476 // mirror - java.lang.Class
477 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
478 } else if (is_stack_chunk_class(class_name, loader_data)) {
479 // stack chunk
480 ik = new (loader_data, size, THREAD) InstanceStackChunkKlass(parser);
481 } else if (is_class_loader(class_name, parser)) {
482 // class loader - java.lang.ClassLoader
483 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
484 } else {
485 // normal
486 ik = new (loader_data, size, THREAD) InstanceKlass(parser);
487 }
488
489 assert(ik == nullptr || CompressedKlassPointers::is_encodable(ik),
490 "Klass " PTR_FORMAT "needs a narrow Klass ID, but is not encodable", p2i(ik));
491
492 // Check for pending exception before adding to the loader data and incrementing
493 // class count. Can get OOM here.
494 if (HAS_PENDING_EXCEPTION) {
495 return nullptr;
496 }
497
498 return ik;
499 }
500
501
502 // copy method ordering from resource area to Metaspace
503 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
504 if (m != nullptr) {
505 // allocate a new array and copy contents (memcpy?)
506 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
507 for (int i = 0; i < m->length(); i++) {
508 _method_ordering->at_put(i, m->at(i));
509 }
510 } else {
511 _method_ordering = Universe::the_empty_int_array();
512 }
513 }
514
515 // create a new array of vtable_indices for default methods
516 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
517 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
518 assert(default_vtable_indices() == nullptr, "only create once");
519 set_default_vtable_indices(vtable_indices);
520 return vtable_indices;
521 }
522
523
524 InstanceKlass::InstanceKlass() {
525 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
526 }
527
528 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
529 Klass(kind),
530 _nest_members(nullptr),
531 _nest_host(nullptr),
532 _permitted_subclasses(nullptr),
533 _record_components(nullptr),
534 _static_field_size(parser.static_field_size()),
535 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
536 _itable_len(parser.itable_size()),
537 _nest_host_index(0),
538 _init_state(allocated),
539 _reference_type(reference_type),
540 _init_thread(nullptr)
541 {
542 set_vtable_length(parser.vtable_size());
543 set_access_flags(parser.access_flags());
544 if (parser.is_hidden()) set_is_hidden();
545 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
546 false));
547
548 assert(nullptr == _methods, "underlying memory not zeroed?");
549 assert(is_instance_klass(), "is layout incorrect?");
550 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
551 }
552
553 void InstanceKlass::set_is_cloneable() {
554 if (name() == vmSymbols::java_lang_invoke_MemberName()) {
555 assert(is_final(), "no subclasses allowed");
556 // MemberName cloning should not be intrinsified and always happen in JVM_Clone.
557 } else if (reference_type() != REF_NONE) {
558 // Reference cloning should not be intrinsified and always happen in JVM_Clone.
559 } else {
560 set_is_cloneable_fast();
561 }
562 }
563
564 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
565 Array<Method*>* methods) {
566 if (methods != nullptr && methods != Universe::the_empty_method_array() &&
682
683 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
684 set_transitive_interfaces(nullptr);
685 set_local_interfaces(nullptr);
686
687 if (fieldinfo_stream() != nullptr && !fieldinfo_stream()->in_aot_cache()) {
688 MetadataFactory::free_array<u1>(loader_data, fieldinfo_stream());
689 }
690 set_fieldinfo_stream(nullptr);
691
692 if (fieldinfo_search_table() != nullptr && !fieldinfo_search_table()->in_aot_cache()) {
693 MetadataFactory::free_array<u1>(loader_data, fieldinfo_search_table());
694 }
695 set_fieldinfo_search_table(nullptr);
696
697 if (fields_status() != nullptr && !fields_status()->in_aot_cache()) {
698 MetadataFactory::free_array<FieldStatus>(loader_data, fields_status());
699 }
700 set_fields_status(nullptr);
701
702 // If a method from a redefined class is using this constant pool, don't
703 // delete it, yet. The new class's previous version will point to this.
704 if (constants() != nullptr) {
705 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
706 if (!constants()->in_aot_cache()) {
707 HeapShared::remove_scratch_resolved_references(constants());
708 MetadataFactory::free_metadata(loader_data, constants());
709 }
710 // Delete any cached resolution errors for the constant pool
711 SystemDictionary::delete_resolution_error(constants());
712
713 set_constants(nullptr);
714 }
715
716 if (inner_classes() != nullptr &&
717 inner_classes() != Universe::the_empty_short_array() &&
718 !inner_classes()->in_aot_cache()) {
719 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
720 }
721 set_inner_classes(nullptr);
722
723 if (nest_members() != nullptr &&
724 nest_members() != Universe::the_empty_short_array() &&
725 !nest_members()->in_aot_cache()) {
726 MetadataFactory::free_array<jushort>(loader_data, nest_members());
727 }
728 set_nest_members(nullptr);
729
730 if (permitted_subclasses() != nullptr &&
731 permitted_subclasses() != Universe::the_empty_short_array() &&
732 !permitted_subclasses()->in_aot_cache()) {
733 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
734 }
735 set_permitted_subclasses(nullptr);
736
737 // We should deallocate the Annotations instance if it's not in shared spaces.
738 if (annotations() != nullptr && !annotations()->in_aot_cache()) {
739 MetadataFactory::free_metadata(loader_data, annotations());
740 }
741 set_annotations(nullptr);
742
743 SystemDictionaryShared::handle_class_unloading(this);
744
745 #if INCLUDE_CDS_JAVA_HEAP
746 if (CDSConfig::is_dumping_heap()) {
747 HeapShared::remove_scratch_objects(this);
748 }
749 #endif
750 }
751
752 bool InstanceKlass::is_record() const {
753 return _record_components != nullptr &&
754 is_final() &&
755 super() == vmClasses::Record_klass();
756 }
759 return _permitted_subclasses != nullptr &&
760 _permitted_subclasses != Universe::the_empty_short_array();
761 }
762
763 // JLS 8.9: An enum class is either implicitly final and derives
764 // from java.lang.Enum, or else is implicitly sealed to its
765 // anonymous subclasses. This query detects both kinds.
766 // It does not validate the finality or
767 // sealing conditions: it merely checks for a super of Enum.
768 // This is sufficient for recognizing well-formed enums.
769 bool InstanceKlass::is_enum_subclass() const {
770 InstanceKlass* s = super();
771 return (s == vmClasses::Enum_klass() ||
772 (s != nullptr && s->super() == vmClasses::Enum_klass()));
773 }
774
775 bool InstanceKlass::should_be_initialized() const {
776 return !is_initialized();
777 }
778
779 klassItable InstanceKlass::itable() const {
780 return klassItable(const_cast<InstanceKlass*>(this));
781 }
782
783 // JVMTI spec thinks there are signers and protection domain in the
784 // instanceKlass. These accessors pretend these fields are there.
785 // The hprof specification also thinks these fields are in InstanceKlass.
786 oop InstanceKlass::protection_domain() const {
787 // return the protection_domain from the mirror
788 return java_lang_Class::protection_domain(java_mirror());
789 }
790
791 objArrayOop InstanceKlass::signers() const {
792 // return the signers from the mirror
793 return java_lang_Class::signers(java_mirror());
794 }
795
796 oop InstanceKlass::init_lock() const {
797 // return the init lock from the mirror
798 oop lock = java_lang_Class::init_lock(java_mirror());
934 #ifdef ASSERT
935 {
936 Handle h_init_lock(THREAD, init_lock());
937 ObjectLocker ol(h_init_lock, THREAD);
938 assert(!is_initialized(), "sanity");
939 assert(!is_being_initialized(), "sanity");
940 assert(!is_in_error_state(), "sanity");
941 }
942 #endif
943
944 set_init_thread(THREAD);
945 set_initialization_state_and_notify(fully_initialized, CHECK);
946 }
947 #endif
948
949 bool InstanceKlass::verify_code(TRAPS) {
950 // 1) Verify the bytecodes
951 return Verifier::verify(this, should_verify_class(), THREAD);
952 }
953
954 void InstanceKlass::link_class(TRAPS) {
955 assert(is_loaded(), "must be loaded");
956 if (!is_linked()) {
957 link_class_impl(CHECK);
958 }
959 }
960
961 // Called to verify that a class can link during initialization, without
962 // throwing a VerifyError.
963 bool InstanceKlass::link_class_or_fail(TRAPS) {
964 assert(is_loaded(), "must be loaded");
965 if (!is_linked()) {
966 link_class_impl(CHECK_false);
967 }
968 return is_linked();
969 }
970
971 bool InstanceKlass::link_class_impl(TRAPS) {
972 if (CDSConfig::is_dumping_static_archive() && SystemDictionaryShared::has_class_failed_verification(this)) {
973 // This is for CDS static dump only -- we use the in_error_state to indicate that
1004 THREAD_AND_LOCATION,
1005 vmSymbols::java_lang_IncompatibleClassChangeError(),
1006 "class %s has interface %s as super class",
1007 external_name(),
1008 super_klass->external_name()
1009 );
1010 return false;
1011 }
1012
1013 super_klass->link_class_impl(CHECK_false);
1014 }
1015
1016 // link all interfaces implemented by this class before linking this class
1017 Array<InstanceKlass*>* interfaces = local_interfaces();
1018 int num_interfaces = interfaces->length();
1019 for (int index = 0; index < num_interfaces; index++) {
1020 InstanceKlass* interk = interfaces->at(index);
1021 interk->link_class_impl(CHECK_false);
1022 }
1023
1024 // in case the class is linked in the process of linking its superclasses
1025 if (is_linked()) {
1026 return true;
1027 }
1028
1029 // trace only the link time for this klass that includes
1030 // the verification time
1031 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
1032 ClassLoader::perf_class_link_selftime(),
1033 ClassLoader::perf_classes_linked(),
1034 jt->get_thread_stat()->perf_recursion_counts_addr(),
1035 jt->get_thread_stat()->perf_timers_addr(),
1036 PerfClassTraceTime::CLASS_LINK);
1037
1038 // verification & rewriting
1039 {
1040 HandleMark hm(THREAD);
1041 Handle h_init_lock(THREAD, init_lock());
1042 ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE_false);
1043 // Don't allow preemption if we link/initialize classes below,
1333 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1334 ss.as_string(), cause);
1335 }
1336 } else {
1337
1338 // Step 6
1339 set_init_state(being_initialized);
1340 set_init_thread(jt);
1341 if (debug_logging_enabled) {
1342 ResourceMark rm(jt);
1343 log_debug(class, init)("Thread \"%s\" is initializing %s",
1344 jt->name(), external_name());
1345 }
1346 }
1347 }
1348
1349 // Block preemption once we are the initializer thread. Unmounting now
1350 // would complicate the reentrant case (identity is platform thread).
1351 NoPreemptMark npm(THREAD);
1352
1353 // Step 7
1354 // Next, if C is a class rather than an interface, initialize it's super class and super
1355 // interfaces.
1356 if (!is_interface()) {
1357 Klass* super_klass = super();
1358 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1359 super_klass->initialize(THREAD);
1360 }
1361 // If C implements any interface that declares a non-static, concrete method,
1362 // the initialization of C triggers initialization of its super interfaces.
1363 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1364 // having a superinterface that declares, non-static, concrete methods
1365 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1366 initialize_super_interfaces(THREAD);
1367 }
1368
1369 // If any exceptions, complete abruptly, throwing the same exception as above.
1370 if (HAS_PENDING_EXCEPTION) {
1371 Handle e(THREAD, PENDING_EXCEPTION);
1372 CLEAR_PENDING_EXCEPTION;
1386 // Step 8
1387 {
1388 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1389 if (class_initializer() != nullptr) {
1390 // Timer includes any side effects of class initialization (resolution,
1391 // etc), but not recursive entry into call_class_initializer().
1392 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1393 ClassLoader::perf_class_init_selftime(),
1394 ClassLoader::perf_classes_inited(),
1395 jt->get_thread_stat()->perf_recursion_counts_addr(),
1396 jt->get_thread_stat()->perf_timers_addr(),
1397 PerfClassTraceTime::CLASS_CLINIT);
1398 call_class_initializer(THREAD);
1399 } else {
1400 // The elapsed time is so small it's not worth counting.
1401 if (UsePerfData) {
1402 ClassLoader::perf_classes_inited()->inc();
1403 }
1404 call_class_initializer(THREAD);
1405 }
1406 }
1407
1408 // Step 9
1409 if (!HAS_PENDING_EXCEPTION) {
1410 set_initialization_state_and_notify(fully_initialized, CHECK);
1411 DEBUG_ONLY(vtable().verify(tty, true);)
1412 CompilationPolicy::replay_training_at_init(this, THREAD);
1413 }
1414 else {
1415 // Step 10 and 11
1416 Handle e(THREAD, PENDING_EXCEPTION);
1417 CLEAR_PENDING_EXCEPTION;
1418 // JVMTI has already reported the pending exception
1419 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1420 JvmtiExport::clear_detected_exception(jt);
1421 {
1422 EXCEPTION_MARK;
1423 add_initialization_error(THREAD, e);
1424 set_initialization_state_and_notify(initialization_error, THREAD);
1425 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1439 }
1440 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1441 }
1442
1443
1444 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1445 Handle h_init_lock(THREAD, init_lock());
1446 if (h_init_lock() != nullptr) {
1447 ObjectLocker ol(h_init_lock, THREAD);
1448 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1449 set_init_state(state);
1450 fence_and_clear_init_lock();
1451 ol.notify_all(CHECK);
1452 } else {
1453 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1454 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1455 set_init_state(state);
1456 }
1457 }
1458
1459 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1460 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1461 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1462 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1463
1464 DeoptimizationScope deopt_scope;
1465 {
1466 MutexLocker ml(current, Compile_lock);
1467
1468 set_init_state(InstanceKlass::loaded);
1469 // make sure init_state store is already done.
1470 // The compiler reads the hierarchy outside of the Compile_lock.
1471 // Access ordering is used to add to hierarchy.
1472
1473 // Link into hierarchy.
1474 append_to_sibling_list(); // add to superklass/sibling list
1475 process_interfaces(); // handle all "implements" declarations
1476
1477 // Now mark all code that depended on old class hierarchy.
1478 // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
1715 // Need load-acquire for lock-free read
1716 ObjArrayKlass* oak = array_klasses_acquire();
1717 if (oak == nullptr) {
1718 return nullptr;
1719 } else {
1720 return oak->array_klass_or_null(n);
1721 }
1722 }
1723
1724 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1725 return array_klass(1, THREAD);
1726 }
1727
1728 ArrayKlass* InstanceKlass::array_klass_or_null() {
1729 return array_klass_or_null(1);
1730 }
1731
1732 Method* InstanceKlass::class_initializer() const {
1733 Method* clinit = find_method(
1734 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1735 if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1736 return clinit;
1737 }
1738 return nullptr;
1739 }
1740
1741 void InstanceKlass::call_class_initializer(TRAPS) {
1742 if (ReplayCompiles &&
1743 (ReplaySuppressInitializers == 1 ||
1744 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1745 // Hide the existence of the initializer for the purpose of replaying the compile
1746 return;
1747 }
1748
1749 #if INCLUDE_CDS
1750 // This is needed to ensure the consistency of the archived heap objects.
1751 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1752 AOTClassInitializer::call_runtime_setup(THREAD, this);
1753 return;
1754 } else if (has_archived_enum_objs()) {
1755 assert(in_aot_cache(), "must be");
1824
1825 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1826 InterpreterOopMap* entry_for) {
1827 // Lazily create the _oop_map_cache at first request.
1828 // Load_acquire is needed to safely get instance published with CAS by another thread.
1829 OopMapCache* oop_map_cache = AtomicAccess::load_acquire(&_oop_map_cache);
1830 if (oop_map_cache == nullptr) {
1831 // Try to install new instance atomically.
1832 oop_map_cache = new OopMapCache();
1833 OopMapCache* other = AtomicAccess::cmpxchg(&_oop_map_cache, (OopMapCache*)nullptr, oop_map_cache);
1834 if (other != nullptr) {
1835 // Someone else managed to install before us, ditch local copy and use the existing one.
1836 delete oop_map_cache;
1837 oop_map_cache = other;
1838 }
1839 }
1840 // _oop_map_cache is constant after init; lookup below does its own locking.
1841 oop_map_cache->lookup(method, bci, entry_for);
1842 }
1843
1844 bool InstanceKlass::contains_field_offset(int offset) {
1845 fieldDescriptor fd;
1846 return find_field_from_offset(offset, false, &fd);
1847 }
1848
1849 FieldInfo InstanceKlass::field(int index) const {
1850 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1851 if (fs.index() == index) {
1852 return fs.to_FieldInfo();
1853 }
1854 }
1855 fatal("Field not found");
1856 return FieldInfo();
1857 }
1858
1859 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1860 JavaFieldStream fs(this);
1861 if (fs.lookup(name, sig)) {
1862 assert(fs.name() == name, "name must match");
1863 assert(fs.signature() == sig, "signature must match");
1864 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
1865 return true;
1866 }
1867 return false;
1908
1909 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1910 // search order according to newest JVM spec (5.4.3.2, p.167).
1911 // 1) search for field in current klass
1912 if (find_local_field(name, sig, fd)) {
1913 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1914 }
1915 // 2) search for field recursively in direct superinterfaces
1916 if (is_static) {
1917 Klass* intf = find_interface_field(name, sig, fd);
1918 if (intf != nullptr) return intf;
1919 }
1920 // 3) apply field lookup recursively if superclass exists
1921 { InstanceKlass* supr = super();
1922 if (supr != nullptr) return supr->find_field(name, sig, is_static, fd);
1923 }
1924 // 4) otherwise field lookup fails
1925 return nullptr;
1926 }
1927
1928
1929 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1930 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1931 if (fs.offset() == offset) {
1932 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
1933 if (fd->is_static() == is_static) return true;
1934 }
1935 }
1936 return false;
1937 }
1938
1939
1940 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1941 const InstanceKlass* klass = this;
1942 while (klass != nullptr) {
1943 if (klass->find_local_field_from_offset(offset, is_static, fd)) {
1944 return true;
1945 }
1946 klass = klass->super();
1947 }
1948 return false;
1949 }
1950
1951
1952 void InstanceKlass::methods_do(void f(Method* method)) {
1953 // Methods aren't stable until they are loaded. This can be read outside
1954 // a lock through the ClassLoaderData for profiling
1955 // Redefined scratch classes are on the list and need to be cleaned
1956 if (!is_loaded() && !is_scratch_class()) {
1957 return;
1958 }
1959
1960 int len = methods()->length();
1961 for (int index = 0; index < len; index++) {
1962 Method* m = methods()->at(index);
1963 assert(m->is_method(), "must be method");
1964 f(m);
1965 }
1966 }
1967
1968
1969 void InstanceKlass::do_local_static_fields(FieldClosure* cl) {
1970 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1971 if (fs.access_flags().is_static()) {
1972 fieldDescriptor& fd = fs.field_descriptor();
1973 cl->do_field(&fd);
1974 }
1975 }
1976 }
1977
1978
1979 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) {
1980 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1981 if (fs.access_flags().is_static()) {
1982 fieldDescriptor& fd = fs.field_descriptor();
1983 f(&fd, mirror, CHECK);
1984 }
1985 }
1986 }
1987
1988 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) {
1989 InstanceKlass* super = this->super();
1990 if (super != nullptr) {
1991 super->do_nonstatic_fields(cl);
1992 }
1993 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1994 fieldDescriptor& fd = fs.field_descriptor();
1995 if (!fd.is_static()) {
1996 cl->do_field(&fd);
1997 }
1998 }
1999 }
2000
2291 }
2292
2293 // uncached_lookup_method searches both the local class methods array and all
2294 // superclasses methods arrays, skipping any overpass methods in superclasses,
2295 // and possibly skipping private methods.
2296 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2297 const Symbol* signature,
2298 OverpassLookupMode overpass_mode,
2299 PrivateLookupMode private_mode) const {
2300 OverpassLookupMode overpass_local_mode = overpass_mode;
2301 const InstanceKlass* klass = this;
2302 while (klass != nullptr) {
2303 Method* const method = klass->find_method_impl(name,
2304 signature,
2305 overpass_local_mode,
2306 StaticLookupMode::find,
2307 private_mode);
2308 if (method != nullptr) {
2309 return method;
2310 }
2311 klass = klass->super();
2312 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2313 }
2314 return nullptr;
2315 }
2316
2317 #ifdef ASSERT
2318 // search through class hierarchy and return true if this class or
2319 // one of the superclasses was redefined
2320 bool InstanceKlass::has_redefined_this_or_super() const {
2321 const InstanceKlass* klass = this;
2322 while (klass != nullptr) {
2323 if (klass->has_been_redefined()) {
2324 return true;
2325 }
2326 klass = klass->super();
2327 }
2328 return false;
2329 }
2330 #endif
2703 int itable_offset_in_words = (int)(start_of_itable() - (intptr_t*)this);
2704
2705 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words)
2706 / itableOffsetEntry::size();
2707
2708 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2709 if (ioe->interface_klass() != nullptr) {
2710 it->push(ioe->interface_klass_addr());
2711 itableMethodEntry* ime = ioe->first_method_entry(this);
2712 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2713 for (int index = 0; index < n; index ++) {
2714 it->push(ime[index].method_addr());
2715 }
2716 }
2717 }
2718 }
2719
2720 it->push(&_nest_host);
2721 it->push(&_nest_members);
2722 it->push(&_permitted_subclasses);
2723 it->push(&_record_components);
2724
2725 if (CDSConfig::is_dumping_full_module_graph() && !defined_by_other_loaders()) {
2726 it->push(&_package_entry);
2727 }
2728 }
2729
2730 #if INCLUDE_CDS
2731 void InstanceKlass::remove_unshareable_info() {
2732
2733 if (is_linked()) {
2734 assert(can_be_verified_at_dumptime(), "must be");
2735 // Remember this so we can avoid walking the hierarchy at runtime.
2736 set_verified_at_dump_time();
2737 }
2738
2739 _misc_flags.set_has_init_deps_processed(false);
2740
2741 Klass::remove_unshareable_info();
2742
2743 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2755
2756 { // Otherwise this needs to take out the Compile_lock.
2757 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2758 init_implementor();
2759 }
2760
2761 // Call remove_unshareable_info() on other objects that belong to this class, except
2762 // for constants()->remove_unshareable_info(), which is called in a separate pass in
2763 // ArchiveBuilder::make_klasses_shareable(),
2764
2765 for (int i = 0; i < methods()->length(); i++) {
2766 Method* m = methods()->at(i);
2767 m->remove_unshareable_info();
2768 }
2769
2770 // do array classes also.
2771 if (array_klasses() != nullptr) {
2772 array_klasses()->remove_unshareable_info();
2773 }
2774
2775 // These are not allocated from metaspace. They are safe to set to null.
2776 _source_debug_extension = nullptr;
2777 _dep_context = nullptr;
2778 _osr_nmethods_head = nullptr;
2779 #if INCLUDE_JVMTI
2780 _breakpoints = nullptr;
2781 _previous_versions = nullptr;
2782 _cached_class_file = nullptr;
2783 _jvmti_cached_class_field_map = nullptr;
2784 #endif
2785
2786 _init_thread = nullptr;
2787 _methods_jmethod_ids = nullptr;
2788 _jni_ids = nullptr;
2789 _oop_map_cache = nullptr;
2790 if (CDSConfig::is_dumping_method_handles() && HeapShared::is_lambda_proxy_klass(this)) {
2791 // keep _nest_host
2792 } else {
2793 // clear _nest_host to ensure re-load at runtime
2794 _nest_host = nullptr;
2795 }
2831 void InstanceKlass::compute_has_loops_flag_for_methods() {
2832 Array<Method*>* methods = this->methods();
2833 for (int index = 0; index < methods->length(); ++index) {
2834 Method* m = methods->at(index);
2835 if (!m->is_overpass()) { // work around JDK-8305771
2836 m->compute_has_loops_flag();
2837 }
2838 }
2839 }
2840
2841 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2842 PackageEntry* pkg_entry, TRAPS) {
2843 // InstanceKlass::add_to_hierarchy() sets the init_state to loaded
2844 // before the InstanceKlass is added to the SystemDictionary. Make
2845 // sure the current state is <loaded.
2846 assert(!is_loaded(), "invalid init state");
2847 assert(!shared_loading_failed(), "Must not try to load failed class again");
2848 set_package(loader_data, pkg_entry, CHECK);
2849 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2850
2851 Array<Method*>* methods = this->methods();
2852 int num_methods = methods->length();
2853 for (int index = 0; index < num_methods; ++index) {
2854 methods->at(index)->restore_unshareable_info(CHECK);
2855 }
2856 #if INCLUDE_JVMTI
2857 if (JvmtiExport::has_redefined_a_class()) {
2858 // Reinitialize vtable because RedefineClasses may have changed some
2859 // entries in this vtable for super classes so the CDS vtable might
2860 // point to old or obsolete entries. RedefineClasses doesn't fix up
2861 // vtables in the shared system dictionary, only the main one.
2862 // It also redefines the itable too so fix that too.
2863 // First fix any default methods that point to a super class that may
2864 // have been redefined.
2865 bool trace_name_printed = false;
2866 adjust_default_methods(&trace_name_printed);
2867 if (verified_at_dump_time()) {
2868 // Initialize vtable and itable for classes which can be verified at dump time.
2869 // Unlinked classes such as old classes with major version < 50 cannot be verified
2870 // at dump time.
2871 vtable().initialize_vtable();
2872 itable().initialize_itable();
2873 }
2874 }
2875 #endif // INCLUDE_JVMTI
2876
2877 // restore constant pool resolved references
2878 constants()->restore_unshareable_info(CHECK);
2879
2880 if (array_klasses() != nullptr) {
2881 // To get a consistent list of classes we need MultiArray_lock to ensure
2882 // array classes aren't observed while they are being restored.
2883 RecursiveLocker rl(MultiArray_lock, THREAD);
2884 assert(this == array_klasses()->bottom_klass(), "sanity");
2885 // Array classes have null protection domain.
2886 // --> see ArrayKlass::complete_create_array_klass()
2887 array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2888 }
2889
2890 // Initialize @ValueBased class annotation if not already set in the archived klass.
2891 if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2892 set_is_value_based();
2893 }
2894
2895 DEBUG_ONLY(FieldInfoStream::validate_search_table(_constants, _fieldinfo_stream, _fieldinfo_search_table));
2896 }
2897
2898 bool InstanceKlass::can_be_verified_at_dumptime() const {
2899 if (CDSConfig::is_dumping_dynamic_archive() && AOTMetaspace::in_aot_cache(this)) {
2900 // This is a class that was dumped into the base archive, so we know
2901 // it was verified at dump time.
2902 return true;
2903 }
2904
2905 if (CDSConfig::is_preserving_verification_constraints()) {
2906 return true;
3022 constants()->release_C_heap_structures();
3023 }
3024 }
3025
3026 // The constant pool is on stack if any of the methods are executing or
3027 // referenced by handles.
3028 bool InstanceKlass::on_stack() const {
3029 return _constants->on_stack();
3030 }
3031
3032 Symbol* InstanceKlass::source_file_name() const { return _constants->source_file_name(); }
3033 u2 InstanceKlass::source_file_name_index() const { return _constants->source_file_name_index(); }
3034 void InstanceKlass::set_source_file_name_index(u2 sourcefile_index) { _constants->set_source_file_name_index(sourcefile_index); }
3035
3036 // minor and major version numbers of class file
3037 u2 InstanceKlass::minor_version() const { return _constants->minor_version(); }
3038 void InstanceKlass::set_minor_version(u2 minor_version) { _constants->set_minor_version(minor_version); }
3039 u2 InstanceKlass::major_version() const { return _constants->major_version(); }
3040 void InstanceKlass::set_major_version(u2 major_version) { _constants->set_major_version(major_version); }
3041
3042 const InstanceKlass* InstanceKlass::get_klass_version(int version) const {
3043 for (const InstanceKlass* ik = this; ik != nullptr; ik = ik->previous_versions()) {
3044 if (ik->constants()->version() == version) {
3045 return ik;
3046 }
3047 }
3048 return nullptr;
3049 }
3050
3051 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
3052 if (array == nullptr) {
3053 _source_debug_extension = nullptr;
3054 } else {
3055 // Adding one to the attribute length in order to store a null terminator
3056 // character could cause an overflow because the attribute length is
3057 // already coded with an u4 in the classfile, but in practice, it's
3058 // unlikely to happen.
3059 assert((length+1) > length, "Overflow checking");
3060 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
3061 for (int i = 0; i < length; i++) {
3062 sde[i] = array[i];
3063 }
3064 sde[length] = '\0';
3065 _source_debug_extension = sde;
3066 }
3067 }
3068
3069 Symbol* InstanceKlass::generic_signature() const { return _constants->generic_signature(); }
3070 u2 InstanceKlass::generic_signature_index() const { return _constants->generic_signature_index(); }
3071 void InstanceKlass::set_generic_signature_index(u2 sig_index) { _constants->set_generic_signature_index(sig_index); }
3072
3073 const char* InstanceKlass::signature_name() const {
3074
3075 // Get the internal name as a c string
3076 const char* src = (const char*) (name()->as_C_string());
3077 const int src_length = (int)strlen(src);
3078
3079 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
3080
3081 // Add L as type indicator
3082 int dest_index = 0;
3083 dest[dest_index++] = JVM_SIGNATURE_CLASS;
3084
3085 // Add the actual class name
3086 for (int src_index = 0; src_index < src_length; ) {
3087 dest[dest_index++] = src[src_index++];
3088 }
3089
3090 if (is_hidden()) { // Replace the last '+' with a '.'.
3091 for (int index = (int)src_length; index > 0; index--) {
3092 if (dest[index] == '+') {
3093 dest[index] = JVM_SIGNATURE_DOT;
3094 break;
3344 bool InstanceKlass::find_inner_classes_attr(int* ooff, int* noff, TRAPS) const {
3345 constantPoolHandle i_cp(THREAD, constants());
3346 for (InnerClassesIterator iter(this); !iter.done(); iter.next()) {
3347 int ioff = iter.inner_class_info_index();
3348 if (ioff != 0) {
3349 // Check to see if the name matches the class we're looking for
3350 // before attempting to find the class.
3351 if (i_cp->klass_name_at_matches(this, ioff)) {
3352 Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
3353 if (this == inner_klass) {
3354 *ooff = iter.outer_class_info_index();
3355 *noff = iter.inner_name_index();
3356 return true;
3357 }
3358 }
3359 }
3360 }
3361 return false;
3362 }
3363
3364 InstanceKlass* InstanceKlass::compute_enclosing_class(bool* inner_is_member, TRAPS) const {
3365 InstanceKlass* outer_klass = nullptr;
3366 *inner_is_member = false;
3367 int ooff = 0, noff = 0;
3368 bool has_inner_classes_attr = find_inner_classes_attr(&ooff, &noff, THREAD);
3369 if (has_inner_classes_attr) {
3370 constantPoolHandle i_cp(THREAD, constants());
3371 if (ooff != 0) {
3372 Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
3373 if (!ok->is_instance_klass()) {
3374 // If the outer class is not an instance klass then it cannot have
3375 // declared any inner classes.
3376 ResourceMark rm(THREAD);
3377 // Names are all known to be < 64k so we know this formatted message is not excessively large.
3378 Exceptions::fthrow(
3379 THREAD_AND_LOCATION,
3380 vmSymbols::java_lang_IncompatibleClassChangeError(),
3381 "%s and %s disagree on InnerClasses attribute",
3382 ok->external_name(),
3383 external_name());
3410 u2 InstanceKlass::compute_modifier_flags() const {
3411 u2 access = access_flags().as_unsigned_short();
3412
3413 // But check if it happens to be member class.
3414 InnerClassesIterator iter(this);
3415 for (; !iter.done(); iter.next()) {
3416 int ioff = iter.inner_class_info_index();
3417 // Inner class attribute can be zero, skip it.
3418 // Strange but true: JVM spec. allows null inner class refs.
3419 if (ioff == 0) continue;
3420
3421 // only look at classes that are already loaded
3422 // since we are looking for the flags for our self.
3423 Symbol* inner_name = constants()->klass_name_at(ioff);
3424 if (name() == inner_name) {
3425 // This is really a member class.
3426 access = iter.inner_access_flags();
3427 break;
3428 }
3429 }
3430 // Remember to strip ACC_SUPER bit
3431 return (access & (~JVM_ACC_SUPER));
3432 }
3433
3434 jint InstanceKlass::jvmti_class_status() const {
3435 jint result = 0;
3436
3437 if (is_linked()) {
3438 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3439 }
3440
3441 if (is_initialized()) {
3442 assert(is_linked(), "Class status is not consistent");
3443 result |= JVMTI_CLASS_STATUS_INITIALIZED;
3444 }
3445 if (is_in_error_state()) {
3446 result |= JVMTI_CLASS_STATUS_ERROR;
3447 }
3448 return result;
3449 }
3450
3451 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {
3697 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3698 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3699 }
3700
3701 const char* InstanceKlass::init_state_name() const {
3702 return state_names[init_state()];
3703 }
3704
3705 void InstanceKlass::print_class_flags(outputStream* st) const {
3706 AccessFlags flags(compute_modifier_flags());
3707 if (flags.is_public ()) st->print("public ");
3708 if (flags.is_private ()) st->print("private ");
3709 if (flags.is_protected ()) st->print("protected ");
3710 if (flags.is_static ()) st->print("static ");
3711 if (flags.is_final ()) st->print("final ");
3712 if (flags.is_interface ()) st->print("interface ");
3713 if (flags.is_abstract ()) st->print("abstract ");
3714 if (flags.is_annotation()) st->print("annotation ");
3715 if (flags.is_enum ()) st->print("enum ");
3716 if (flags.is_synthetic ()) st->print("synthetic ");
3717 }
3718
3719 void InstanceKlass::print_on(outputStream* st) const {
3720 assert(is_klass(), "must be klass");
3721 Klass::print_on(st);
3722
3723 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3724 st->print(BULLET"klass size: %d", size()); st->cr();
3725 st->print(BULLET"access: "); print_class_flags(st); st->cr();
3726 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3727 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3728 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3729 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3730 st->print(BULLET"sub: ");
3731 Klass* sub = subklass();
3732 int n;
3733 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3734 if (n < MaxSubklassPrintSize) {
3735 sub->print_value_on(st);
3736 st->print(" ");
3737 }
3738 }
3739 if (n >= MaxSubklassPrintSize) st->print("(%zd more klasses...)", n - MaxSubklassPrintSize);
3740 st->cr();
3741
3742 if (is_interface()) {
3743 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3744 if (nof_implementors() == 1) {
3745 st->print_cr(BULLET"implementor: ");
3746 st->print(" ");
3747 implementor()->print_value_on(st);
3748 st->cr();
3749 }
3750 }
3751
3752 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3753 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
3754 if (Verbose || WizardMode) {
3755 Array<Method*>* method_array = methods();
3756 for (int i = 0; i < method_array->length(); i++) {
3757 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3758 }
3759 }
3760 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
3761 if (default_methods() != nullptr) {
3762 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
3763 if (Verbose) {
3764 Array<Method*>* method_array = default_methods();
3765 for (int i = 0; i < method_array->length(); i++) {
3766 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3767 }
3768 }
3769 }
3770 print_on_maybe_null(st, BULLET"default vtable indices: ", default_vtable_indices());
3771 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
3772 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3773
3774 st->print(BULLET"secondary supers: "); secondary_supers()->print_value_on(st); st->cr();
3775
3776 st->print(BULLET"hash_slot: %d", hash_slot()); st->cr();
3777 st->print(BULLET"secondary bitmap: " UINTX_FORMAT_X_0, _secondary_supers_bitmap); st->cr();
3778
3779 if (secondary_supers() != nullptr) {
3780 if (Verbose) {
3781 bool is_hashed = (_secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_FULL);
3782 st->print_cr(BULLET"---- secondary supers (%d words):", _secondary_supers->length());
3783 for (int i = 0; i < _secondary_supers->length(); i++) {
3784 ResourceMark rm; // for external_name()
3785 Klass* secondary_super = _secondary_supers->at(i);
3786 st->print(BULLET"%2d:", i);
3787 if (is_hashed) {
3788 int home_slot = compute_home_slot(secondary_super, _secondary_supers_bitmap);
3789 int distance = (i - home_slot) & SECONDARY_SUPERS_TABLE_MASK;
3790 st->print(" dist:%02d:", distance);
3791 }
3792 st->print_cr(" %p %s", secondary_super, secondary_super->external_name());
3793 }
3794 }
3795 }
3796 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
3797
3798 print_on_maybe_null(st, BULLET"class loader data: ", class_loader_data());
3799 print_on_maybe_null(st, BULLET"source file: ", source_file_name());
3800 if (source_debug_extension() != nullptr) {
3801 st->print(BULLET"source debug extension: ");
3802 st->print("%s", source_debug_extension());
3803 st->cr();
3804 }
3805 print_on_maybe_null(st, BULLET"class annotations: ", class_annotations());
3806 print_on_maybe_null(st, BULLET"class type annotations: ", class_type_annotations());
3807 print_on_maybe_null(st, BULLET"field annotations: ", fields_annotations());
3808 print_on_maybe_null(st, BULLET"field type annotations: ", fields_type_annotations());
3809 {
3810 bool have_pv = false;
3811 // previous versions are linked together through the InstanceKlass
3812 for (InstanceKlass* pv_node = previous_versions();
3813 pv_node != nullptr;
3814 pv_node = pv_node->previous_versions()) {
3815 if (!have_pv)
3816 st->print(BULLET"previous version: ");
3817 have_pv = true;
3818 pv_node->constants()->print_value_on(st);
3819 }
3820 if (have_pv) st->cr();
3821 }
3822
3823 print_on_maybe_null(st, BULLET"generic signature: ", generic_signature());
3824 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
3825 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
3826 print_on_maybe_null(st, BULLET"record components: ", record_components());
3827 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
3828 if (java_mirror() != nullptr) {
3829 st->print(BULLET"java mirror: ");
3830 java_mirror()->print_value_on(st);
3831 st->cr();
3832 } else {
3833 st->print_cr(BULLET"java mirror: null");
3834 }
3835 st->print(BULLET"vtable length %d (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3836 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
3837 st->print(BULLET"itable length %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3838 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
3839 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3840
3841 FieldPrinter print_static_field(st);
3842 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3843 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3844 FieldPrinter print_nonstatic_field(st);
3845 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3846 ik->print_nonstatic_fields(&print_nonstatic_field);
3847
3848 st->print(BULLET"non-static oop maps (%d entries): ", nonstatic_oop_map_count());
3849 OopMapBlock* map = start_of_nonstatic_oop_maps();
3850 OopMapBlock* end_map = map + nonstatic_oop_map_count();
3851 while (map < end_map) {
3852 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3853 map++;
3854 }
3855 st->cr();
3856
3857 if (fieldinfo_search_table() != nullptr) {
3858 st->print_cr(BULLET"---- field info search table:");
3859 FieldInfoStream::print_search_table(st, _constants, _fieldinfo_stream, _fieldinfo_search_table);
3860 }
3861 }
3862
3863 void InstanceKlass::print_value_on(outputStream* st) const {
3864 assert(is_klass(), "must be klass");
3865 if (Verbose || WizardMode) print_class_flags(st);
3866 name()->print_value_on(st);
3867 }
3868
3869 void FieldPrinter::do_field(fieldDescriptor* fd) {
3870 _st->print(BULLET);
3871 if (_obj == nullptr) {
3872 fd->print_on(_st);
3873 _st->cr();
3874 } else {
3875 fd->print_on_for(_st, _obj);
3876 _st->cr();
3877 }
3878 }
3879
3880
3881 void InstanceKlass::oop_print_on(oop obj, outputStream* st) {
3882 Klass::oop_print_on(obj, st);
3883
3884 if (this == vmClasses::String_klass()) {
3885 typeArrayOop value = java_lang_String::value(obj);
3886 juint length = java_lang_String::length(obj);
3887 if (value != nullptr &&
3888 value->is_typeArray() &&
3889 length <= (juint) value->length()) {
3890 st->print(BULLET"string: ");
3891 java_lang_String::print(obj, st);
3892 st->cr();
3893 }
3894 }
3895
3896 st->print_cr(BULLET"---- fields (total size %zu words):", oop_size(obj));
3897 FieldPrinter print_field(st, obj);
3898 print_nonstatic_fields(&print_field);
3899
3900 if (this == vmClasses::Class_klass()) {
3901 st->print(BULLET"signature: ");
3902 java_lang_Class::print_signature(obj, st);
3903 st->cr();
3904 Klass* real_klass = java_lang_Class::as_Klass(obj);
3905 if (real_klass != nullptr && real_klass->is_instance_klass()) {
3906 st->print_cr(BULLET"---- static fields (%d):", java_lang_Class::static_oop_field_count(obj));
3907 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
3908 }
3909 } else if (this == vmClasses::MethodType_klass()) {
3910 st->print(BULLET"signature: ");
3911 java_lang_invoke_MethodType::print_signature(obj, st);
3912 st->cr();
3913 }
3914 }
3915
3916 #ifndef PRODUCT
3917
|
47 #include "compiler/compileBroker.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "interpreter/bytecodeStream.hpp"
50 #include "interpreter/oopMapCache.hpp"
51 #include "interpreter/rewriter.hpp"
52 #include "jvm.h"
53 #include "jvmtifiles/jvmti.h"
54 #include "klass.inline.hpp"
55 #include "logging/log.hpp"
56 #include "logging/logMessage.hpp"
57 #include "logging/logStream.hpp"
58 #include "memory/allocation.inline.hpp"
59 #include "memory/iterator.inline.hpp"
60 #include "memory/metadataFactory.hpp"
61 #include "memory/metaspaceClosure.hpp"
62 #include "memory/oopFactory.hpp"
63 #include "memory/resourceArea.hpp"
64 #include "memory/universe.hpp"
65 #include "oops/constantPool.hpp"
66 #include "oops/fieldStreams.inline.hpp"
67 #include "oops/inlineKlass.hpp"
68 #include "oops/instanceClassLoaderKlass.hpp"
69 #include "oops/instanceKlass.inline.hpp"
70 #include "oops/instanceMirrorKlass.hpp"
71 #include "oops/instanceOop.hpp"
72 #include "oops/instanceStackChunkKlass.hpp"
73 #include "oops/klass.inline.hpp"
74 #include "oops/layoutKind.hpp"
75 #include "oops/markWord.hpp"
76 #include "oops/method.hpp"
77 #include "oops/oop.inline.hpp"
78 #include "oops/recordComponent.hpp"
79 #include "oops/refArrayKlass.hpp"
80 #include "oops/symbol.hpp"
81 #include "prims/jvmtiExport.hpp"
82 #include "prims/jvmtiRedefineClasses.hpp"
83 #include "prims/jvmtiThreadState.hpp"
84 #include "prims/methodComparator.hpp"
85 #include "runtime/arguments.hpp"
86 #include "runtime/atomicAccess.hpp"
87 #include "runtime/deoptimization.hpp"
88 #include "runtime/fieldDescriptor.inline.hpp"
89 #include "runtime/handles.inline.hpp"
90 #include "runtime/javaCalls.hpp"
91 #include "runtime/javaThread.inline.hpp"
92 #include "runtime/mutexLocker.hpp"
93 #include "runtime/orderAccess.hpp"
94 #include "runtime/os.inline.hpp"
95 #include "runtime/reflection.hpp"
96 #include "runtime/synchronizer.hpp"
97 #include "runtime/threads.hpp"
98 #include "services/classLoadingService.hpp"
99 #include "services/finalizerService.hpp"
100 #include "services/threadService.hpp"
101 #include "utilities/dtrace.hpp"
102 #include "utilities/events.hpp"
103 #include "utilities/macros.hpp"
104 #include "utilities/nativeStackPrinter.hpp"
105 #include "utilities/ostream.hpp"
106 #include "utilities/stringUtils.hpp"
107 #ifdef COMPILER1
108 #include "c1/c1_Compiler.hpp"
109 #endif
110 #if INCLUDE_JFR
111 #include "jfr/jfrEvents.hpp"
112 #endif
113
114 #ifdef DTRACE_ENABLED
115
116
117 #define HOTSPOT_CLASS_INITIALIZATION_required HOTSPOT_CLASS_INITIALIZATION_REQUIRED
118 #define HOTSPOT_CLASS_INITIALIZATION_recursive HOTSPOT_CLASS_INITIALIZATION_RECURSIVE
119 #define HOTSPOT_CLASS_INITIALIZATION_concurrent HOTSPOT_CLASS_INITIALIZATION_CONCURRENT
120 #define HOTSPOT_CLASS_INITIALIZATION_erroneous HOTSPOT_CLASS_INITIALIZATION_ERRONEOUS
121 #define HOTSPOT_CLASS_INITIALIZATION_super__failed HOTSPOT_CLASS_INITIALIZATION_SUPER_FAILED
122 #define HOTSPOT_CLASS_INITIALIZATION_clinit HOTSPOT_CLASS_INITIALIZATION_CLINIT
123 #define HOTSPOT_CLASS_INITIALIZATION_error HOTSPOT_CLASS_INITIALIZATION_ERROR
124 #define HOTSPOT_CLASS_INITIALIZATION_end HOTSPOT_CLASS_INITIALIZATION_END
125 #define DTRACE_CLASSINIT_PROBE(type, thread_type) \
138 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait) \
139 { \
140 char* data = nullptr; \
141 int len = 0; \
142 Symbol* clss_name = name(); \
143 if (clss_name != nullptr) { \
144 data = (char*)clss_name->bytes(); \
145 len = clss_name->utf8_length(); \
146 } \
147 HOTSPOT_CLASS_INITIALIZATION_##type( \
148 data, len, (void*)class_loader(), thread_type, wait); \
149 }
150
151 #else // ndef DTRACE_ENABLED
152
153 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
154 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
155
156 #endif // ndef DTRACE_ENABLED
157
158 void InlineLayoutInfo::metaspace_pointers_do(MetaspaceClosure* it) {
159 log_trace(cds)("Iter(InlineFieldInfo): %p", this);
160 it->push(&_klass);
161 }
162
163 void InlineLayoutInfo::print() const {
164 print_on(tty);
165 }
166
167 void InlineLayoutInfo::print_on(outputStream* st) const {
168 st->print_cr("_klass: " PTR_FORMAT, p2i(_klass));
169 if (_klass != nullptr) {
170 StreamIndentor si(st);
171 _klass->print_on(st);
172 st->cr();
173 }
174
175 st->print("_layout: ");
176 LayoutKindHelper::print_on(_kind, st);
177 st->cr();
178
179 st->print("_null_marker_offset: %d", _null_marker_offset);
180 }
181
182 // A value class is considered naturally atomic if its layout,
183 // once all fields flattening have been applied, contains a single primitive
184 // or oop field. Because primitive types and oops are already handled
185 // atomically by the JVM, it means that there's no need to take
186 // special precautions when reading or writing this value to guarantee
187 // cross-fields invariants. Nullability has to be taken into consideration,
188 // as the null-marker has to be considered as a pseudo-field which must
189 // be kept consistent with the payload. The only kind of value class
190 // that can be considered naturally atomic when nullable is the empty
191 // value classes because the dummy field is re-used as a null-marker.
192 bool InstanceKlass::is_naturally_atomic(bool null_free) const {
193 assert(!is_identity_class(), "Doesn't make sense for an identity class");
194 if (null_free) {
195 // No extra null-marker, just check the layout of the fields
196 return _misc_flags.is_naturally_atomic();
197 } else {
198 // Requires a null-marker, can't have any other fields
199 return InlineKlass::cast(this)->is_empty_inline_type();
200 }
201 }
202
203 bool InstanceKlass::_finalization_enabled = true;
204 static int call_class_initializer_counter = 0; // for debugging
205
206 static inline bool is_class_loader(const Symbol* class_name,
207 const ClassFileParser& parser) {
208 assert(class_name != nullptr, "invariant");
209
210 if (class_name == vmSymbols::java_lang_ClassLoader()) {
211 return true;
212 }
213
214 if (vmClasses::ClassLoader_klass_is_loaded()) {
215 const Klass* const super_klass = parser.super_klass();
216 if (super_klass != nullptr) {
217 if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
218 return true;
219 }
220 }
221 }
222 return false;
223 }
224
225 bool InstanceKlass::field_is_null_free_inline_type(int index) const {
226 return field(index).field_flags().is_null_free_inline_type();
227 }
228
229 bool InstanceKlass::is_class_in_loadable_descriptors_attribute(Symbol* name) const {
230 if (_loadable_descriptors == nullptr) return false;
231 for (int i = 0; i < _loadable_descriptors->length(); i++) {
232 Symbol* class_name = _constants->symbol_at(_loadable_descriptors->at(i));
233 if (class_name == name) return true;
234 }
235 return false;
236 }
237
238 static inline bool is_stack_chunk_class(const Symbol* class_name,
239 const ClassLoaderData* loader_data) {
240 return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
241 loader_data->is_the_null_class_loader_data());
242 }
243
244 // private: called to verify that k is a static member of this nest.
245 // We know that k is an instance class in the same package and hence the
246 // same classloader.
247 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
248 assert(!is_hidden(), "unexpected hidden class");
249 if (_nest_members == nullptr || _nest_members == Universe::the_empty_short_array()) {
250 if (log_is_enabled(Trace, class, nestmates)) {
251 ResourceMark rm(current);
252 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
253 k->external_name(), this->external_name());
254 }
255 return false;
256 }
257
505 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
506 this->external_name(),
507 access ? "" : "NOT ",
508 k->external_name());
509 return access;
510 }
511
512 const char* InstanceKlass::nest_host_error() {
513 if (_nest_host_index == 0) {
514 return nullptr;
515 } else {
516 constantPoolHandle cph(Thread::current(), constants());
517 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
518 }
519 }
520
521 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
522 const int size = InstanceKlass::size(parser.vtable_size(),
523 parser.itable_size(),
524 nonstatic_oop_map_size(parser.total_oop_map_count()),
525 parser.is_interface(),
526 parser.is_inline_type());
527
528 const Symbol* const class_name = parser.class_name();
529 assert(class_name != nullptr, "invariant");
530 ClassLoaderData* loader_data = parser.loader_data();
531 assert(loader_data != nullptr, "invariant");
532
533 InstanceKlass* ik;
534
535 // Allocation
536 if (parser.is_instance_ref_klass()) {
537 // java.lang.ref.Reference
538 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
539 } else if (class_name == vmSymbols::java_lang_Class()) {
540 // mirror - java.lang.Class
541 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
542 } else if (is_stack_chunk_class(class_name, loader_data)) {
543 // stack chunk
544 ik = new (loader_data, size, THREAD) InstanceStackChunkKlass(parser);
545 } else if (is_class_loader(class_name, parser)) {
546 // class loader - java.lang.ClassLoader
547 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
548 } else if (parser.is_inline_type()) {
549 // inline type
550 ik = new (loader_data, size, THREAD) InlineKlass(parser);
551 } else {
552 // normal
553 ik = new (loader_data, size, THREAD) InstanceKlass(parser);
554 }
555
556 assert(ik == nullptr || CompressedKlassPointers::is_encodable(ik),
557 "Klass " PTR_FORMAT "needs a narrow Klass ID, but is not encodable", p2i(ik));
558
559 // Check for pending exception before adding to the loader data and incrementing
560 // class count. Can get OOM here.
561 if (HAS_PENDING_EXCEPTION) {
562 return nullptr;
563 }
564
565 return ik;
566 }
567
568 // copy method ordering from resource area to Metaspace
569 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
570 if (m != nullptr) {
571 // allocate a new array and copy contents (memcpy?)
572 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
573 for (int i = 0; i < m->length(); i++) {
574 _method_ordering->at_put(i, m->at(i));
575 }
576 } else {
577 _method_ordering = Universe::the_empty_int_array();
578 }
579 }
580
581 // create a new array of vtable_indices for default methods
582 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
583 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
584 assert(default_vtable_indices() == nullptr, "only create once");
585 set_default_vtable_indices(vtable_indices);
586 return vtable_indices;
587 }
588
589
590 InstanceKlass::InstanceKlass() {
591 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
592 }
593
594 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, markWord prototype_header, ReferenceType reference_type) :
595 Klass(kind, prototype_header),
596 _nest_members(nullptr),
597 _nest_host(nullptr),
598 _permitted_subclasses(nullptr),
599 _record_components(nullptr),
600 _static_field_size(parser.static_field_size()),
601 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
602 _itable_len(parser.itable_size()),
603 _nest_host_index(0),
604 _init_state(allocated),
605 _reference_type(reference_type),
606 _acmp_maps_offset(0),
607 _init_thread(nullptr),
608 _inline_layout_info_array(nullptr),
609 _loadable_descriptors(nullptr),
610 _acmp_maps_array(nullptr),
611 _adr_inline_klass_members(nullptr)
612 {
613 set_vtable_length(parser.vtable_size());
614 set_access_flags(parser.access_flags());
615 if (parser.is_hidden()) set_is_hidden();
616 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
617 false));
618 if (parser.has_inlined_fields()) {
619 set_has_inlined_fields();
620 }
621
622 assert(nullptr == _methods, "underlying memory not zeroed?");
623 assert(is_instance_klass(), "is layout incorrect?");
624 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
625 }
626
627 void InstanceKlass::set_is_cloneable() {
628 if (name() == vmSymbols::java_lang_invoke_MemberName()) {
629 assert(is_final(), "no subclasses allowed");
630 // MemberName cloning should not be intrinsified and always happen in JVM_Clone.
631 } else if (reference_type() != REF_NONE) {
632 // Reference cloning should not be intrinsified and always happen in JVM_Clone.
633 } else {
634 set_is_cloneable_fast();
635 }
636 }
637
638 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
639 Array<Method*>* methods) {
640 if (methods != nullptr && methods != Universe::the_empty_method_array() &&
756
757 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
758 set_transitive_interfaces(nullptr);
759 set_local_interfaces(nullptr);
760
761 if (fieldinfo_stream() != nullptr && !fieldinfo_stream()->in_aot_cache()) {
762 MetadataFactory::free_array<u1>(loader_data, fieldinfo_stream());
763 }
764 set_fieldinfo_stream(nullptr);
765
766 if (fieldinfo_search_table() != nullptr && !fieldinfo_search_table()->in_aot_cache()) {
767 MetadataFactory::free_array<u1>(loader_data, fieldinfo_search_table());
768 }
769 set_fieldinfo_search_table(nullptr);
770
771 if (fields_status() != nullptr && !fields_status()->in_aot_cache()) {
772 MetadataFactory::free_array<FieldStatus>(loader_data, fields_status());
773 }
774 set_fields_status(nullptr);
775
776 if (inline_layout_info_array() != nullptr) {
777 MetadataFactory::free_array<InlineLayoutInfo>(loader_data, inline_layout_info_array());
778 }
779 set_inline_layout_info_array(nullptr);
780
781 // If a method from a redefined class is using this constant pool, don't
782 // delete it, yet. The new class's previous version will point to this.
783 if (constants() != nullptr) {
784 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
785 if (!constants()->in_aot_cache()) {
786 HeapShared::remove_scratch_resolved_references(constants());
787 MetadataFactory::free_metadata(loader_data, constants());
788 }
789 // Delete any cached resolution errors for the constant pool
790 SystemDictionary::delete_resolution_error(constants());
791
792 set_constants(nullptr);
793 }
794
795 if (inner_classes() != nullptr &&
796 inner_classes() != Universe::the_empty_short_array() &&
797 !inner_classes()->in_aot_cache()) {
798 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
799 }
800 set_inner_classes(nullptr);
801
802 if (nest_members() != nullptr &&
803 nest_members() != Universe::the_empty_short_array() &&
804 !nest_members()->in_aot_cache()) {
805 MetadataFactory::free_array<jushort>(loader_data, nest_members());
806 }
807 set_nest_members(nullptr);
808
809 if (permitted_subclasses() != nullptr &&
810 permitted_subclasses() != Universe::the_empty_short_array() &&
811 !permitted_subclasses()->in_aot_cache()) {
812 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
813 }
814 set_permitted_subclasses(nullptr);
815
816 if (loadable_descriptors() != nullptr &&
817 loadable_descriptors() != Universe::the_empty_short_array() &&
818 !loadable_descriptors()->in_aot_cache()) {
819 MetadataFactory::free_array<jushort>(loader_data, loadable_descriptors());
820 }
821 set_loadable_descriptors(nullptr);
822
823 if (acmp_maps_array() != nullptr) {
824 MetadataFactory::free_array<int>(loader_data, acmp_maps_array());
825 }
826 set_acmp_maps_array(nullptr);
827
828 // We should deallocate the Annotations instance if it's not in shared spaces.
829 if (annotations() != nullptr && !annotations()->in_aot_cache()) {
830 MetadataFactory::free_metadata(loader_data, annotations());
831 }
832 set_annotations(nullptr);
833
834 SystemDictionaryShared::handle_class_unloading(this);
835
836 #if INCLUDE_CDS_JAVA_HEAP
837 if (CDSConfig::is_dumping_heap()) {
838 HeapShared::remove_scratch_objects(this);
839 }
840 #endif
841 }
842
843 bool InstanceKlass::is_record() const {
844 return _record_components != nullptr &&
845 is_final() &&
846 super() == vmClasses::Record_klass();
847 }
850 return _permitted_subclasses != nullptr &&
851 _permitted_subclasses != Universe::the_empty_short_array();
852 }
853
854 // JLS 8.9: An enum class is either implicitly final and derives
855 // from java.lang.Enum, or else is implicitly sealed to its
856 // anonymous subclasses. This query detects both kinds.
857 // It does not validate the finality or
858 // sealing conditions: it merely checks for a super of Enum.
859 // This is sufficient for recognizing well-formed enums.
860 bool InstanceKlass::is_enum_subclass() const {
861 InstanceKlass* s = super();
862 return (s == vmClasses::Enum_klass() ||
863 (s != nullptr && s->super() == vmClasses::Enum_klass()));
864 }
865
866 bool InstanceKlass::should_be_initialized() const {
867 return !is_initialized();
868 }
869
870 // Static size helper
871 int InstanceKlass::size(int vtable_length,
872 int itable_length,
873 int nonstatic_oop_map_size,
874 bool is_interface,
875 bool is_inline_type) {
876 return align_metadata_size(header_size() +
877 vtable_length +
878 itable_length +
879 nonstatic_oop_map_size +
880 (is_interface ? (int)sizeof(Klass*) / wordSize : 0) +
881 (is_inline_type ? (int)sizeof(InlineKlass::Members) / wordSize : 0));
882 }
883
884 int InstanceKlass::size() const {
885 return size(vtable_length(),
886 itable_length(),
887 nonstatic_oop_map_size(),
888 is_interface(),
889 is_inline_klass());
890 }
891
892 klassItable InstanceKlass::itable() const {
893 return klassItable(const_cast<InstanceKlass*>(this));
894 }
895
896 // JVMTI spec thinks there are signers and protection domain in the
897 // instanceKlass. These accessors pretend these fields are there.
898 // The hprof specification also thinks these fields are in InstanceKlass.
899 oop InstanceKlass::protection_domain() const {
900 // return the protection_domain from the mirror
901 return java_lang_Class::protection_domain(java_mirror());
902 }
903
904 objArrayOop InstanceKlass::signers() const {
905 // return the signers from the mirror
906 return java_lang_Class::signers(java_mirror());
907 }
908
909 oop InstanceKlass::init_lock() const {
910 // return the init lock from the mirror
911 oop lock = java_lang_Class::init_lock(java_mirror());
1047 #ifdef ASSERT
1048 {
1049 Handle h_init_lock(THREAD, init_lock());
1050 ObjectLocker ol(h_init_lock, THREAD);
1051 assert(!is_initialized(), "sanity");
1052 assert(!is_being_initialized(), "sanity");
1053 assert(!is_in_error_state(), "sanity");
1054 }
1055 #endif
1056
1057 set_init_thread(THREAD);
1058 set_initialization_state_and_notify(fully_initialized, CHECK);
1059 }
1060 #endif
1061
1062 bool InstanceKlass::verify_code(TRAPS) {
1063 // 1) Verify the bytecodes
1064 return Verifier::verify(this, should_verify_class(), THREAD);
1065 }
1066
1067 static void load_classes_from_loadable_descriptors_attribute(InstanceKlass *ik, TRAPS) {
1068 if (ik->loadable_descriptors() != Universe::the_empty_short_array() && PreloadClasses) {
1069 ResourceMark rm(THREAD);
1070 HandleMark hm(THREAD);
1071 for (int i = 0; i < ik->loadable_descriptors()->length(); i++) {
1072 Symbol* sig = ik->constants()->symbol_at(ik->loadable_descriptors()->at(i));
1073 if (!Signature::has_envelope(sig)) continue;
1074 TempNewSymbol class_name = Signature::strip_envelope(sig);
1075 if (class_name == ik->name()) continue;
1076 log_info(class, preload)("Preloading of class %s during linking of class %s "
1077 "because of the class is listed in the LoadableDescriptors attribute",
1078 sig->as_C_string(), ik->name()->as_C_string());
1079 oop loader = ik->class_loader();
1080 Klass* klass = SystemDictionary::resolve_or_null(class_name,
1081 Handle(THREAD, loader), THREAD);
1082 if (HAS_PENDING_EXCEPTION) {
1083 CLEAR_PENDING_EXCEPTION;
1084 }
1085 if (klass != nullptr) {
1086 log_info(class, preload)("Preloading of class %s during linking of class %s "
1087 "(cause: LoadableDescriptors attribute) succeeded",
1088 class_name->as_C_string(), ik->name()->as_C_string());
1089 if (!klass->is_inline_klass()) {
1090 // Non value classes are allowed by the current spec, but it could be an indication
1091 // of an issue so let's log a warning
1092 log_info(class, preload)("Preloading of class %s during linking of class %s "
1093 "(cause: LoadableDescriptors attribute) but loaded class is not a value class",
1094 class_name->as_C_string(), ik->name()->as_C_string());
1095 }
1096 } else {
1097 log_info(class, preload)("Preloading of class %s during linking of class %s "
1098 "(cause: LoadableDescriptors attribute) failed",
1099 class_name->as_C_string(), ik->name()->as_C_string());
1100 }
1101 }
1102 }
1103 }
1104
1105 void InstanceKlass::link_class(TRAPS) {
1106 assert(is_loaded(), "must be loaded");
1107 if (!is_linked()) {
1108 link_class_impl(CHECK);
1109 }
1110 }
1111
1112 // Called to verify that a class can link during initialization, without
1113 // throwing a VerifyError.
1114 bool InstanceKlass::link_class_or_fail(TRAPS) {
1115 assert(is_loaded(), "must be loaded");
1116 if (!is_linked()) {
1117 link_class_impl(CHECK_false);
1118 }
1119 return is_linked();
1120 }
1121
1122 bool InstanceKlass::link_class_impl(TRAPS) {
1123 if (CDSConfig::is_dumping_static_archive() && SystemDictionaryShared::has_class_failed_verification(this)) {
1124 // This is for CDS static dump only -- we use the in_error_state to indicate that
1155 THREAD_AND_LOCATION,
1156 vmSymbols::java_lang_IncompatibleClassChangeError(),
1157 "class %s has interface %s as super class",
1158 external_name(),
1159 super_klass->external_name()
1160 );
1161 return false;
1162 }
1163
1164 super_klass->link_class_impl(CHECK_false);
1165 }
1166
1167 // link all interfaces implemented by this class before linking this class
1168 Array<InstanceKlass*>* interfaces = local_interfaces();
1169 int num_interfaces = interfaces->length();
1170 for (int index = 0; index < num_interfaces; index++) {
1171 InstanceKlass* interk = interfaces->at(index);
1172 interk->link_class_impl(CHECK_false);
1173 }
1174
1175 if (Arguments::is_valhalla_enabled()) {
1176 // Aggressively preloading all classes from the LoadableDescriptors attribute
1177 // so inline classes can be scalarized in the calling conventions computed below
1178 load_classes_from_loadable_descriptors_attribute(this, THREAD);
1179 assert(!HAS_PENDING_EXCEPTION, "Shouldn't have pending exceptions from call above");
1180 }
1181
1182 // in case the class is linked in the process of linking its superclasses
1183 if (is_linked()) {
1184 return true;
1185 }
1186
1187 // trace only the link time for this klass that includes
1188 // the verification time
1189 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
1190 ClassLoader::perf_class_link_selftime(),
1191 ClassLoader::perf_classes_linked(),
1192 jt->get_thread_stat()->perf_recursion_counts_addr(),
1193 jt->get_thread_stat()->perf_timers_addr(),
1194 PerfClassTraceTime::CLASS_LINK);
1195
1196 // verification & rewriting
1197 {
1198 HandleMark hm(THREAD);
1199 Handle h_init_lock(THREAD, init_lock());
1200 ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE_false);
1201 // Don't allow preemption if we link/initialize classes below,
1491 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1492 ss.as_string(), cause);
1493 }
1494 } else {
1495
1496 // Step 6
1497 set_init_state(being_initialized);
1498 set_init_thread(jt);
1499 if (debug_logging_enabled) {
1500 ResourceMark rm(jt);
1501 log_debug(class, init)("Thread \"%s\" is initializing %s",
1502 jt->name(), external_name());
1503 }
1504 }
1505 }
1506
1507 // Block preemption once we are the initializer thread. Unmounting now
1508 // would complicate the reentrant case (identity is platform thread).
1509 NoPreemptMark npm(THREAD);
1510
1511 // Pre-allocating an all-zero value to be used to reset nullable flat storages
1512 if (is_inline_klass()) {
1513 InlineKlass* vk = InlineKlass::cast(this);
1514 if (vk->supports_nullable_layouts()) {
1515 oop val = vk->allocate_instance(THREAD);
1516 if (HAS_PENDING_EXCEPTION) {
1517 Handle e(THREAD, PENDING_EXCEPTION);
1518 CLEAR_PENDING_EXCEPTION;
1519 {
1520 EXCEPTION_MARK;
1521 add_initialization_error(THREAD, e);
1522 // Locks object, set state, and notify all waiting threads
1523 set_initialization_state_and_notify(initialization_error, THREAD);
1524 CLEAR_PENDING_EXCEPTION;
1525 }
1526 THROW_OOP(e());
1527 }
1528 vk->set_null_reset_value(val);
1529 }
1530 }
1531
1532 // Step 7
1533 // Next, if C is a class rather than an interface, initialize it's super class and super
1534 // interfaces.
1535 if (!is_interface()) {
1536 Klass* super_klass = super();
1537 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1538 super_klass->initialize(THREAD);
1539 }
1540 // If C implements any interface that declares a non-static, concrete method,
1541 // the initialization of C triggers initialization of its super interfaces.
1542 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1543 // having a superinterface that declares, non-static, concrete methods
1544 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1545 initialize_super_interfaces(THREAD);
1546 }
1547
1548 // If any exceptions, complete abruptly, throwing the same exception as above.
1549 if (HAS_PENDING_EXCEPTION) {
1550 Handle e(THREAD, PENDING_EXCEPTION);
1551 CLEAR_PENDING_EXCEPTION;
1565 // Step 8
1566 {
1567 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1568 if (class_initializer() != nullptr) {
1569 // Timer includes any side effects of class initialization (resolution,
1570 // etc), but not recursive entry into call_class_initializer().
1571 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1572 ClassLoader::perf_class_init_selftime(),
1573 ClassLoader::perf_classes_inited(),
1574 jt->get_thread_stat()->perf_recursion_counts_addr(),
1575 jt->get_thread_stat()->perf_timers_addr(),
1576 PerfClassTraceTime::CLASS_CLINIT);
1577 call_class_initializer(THREAD);
1578 } else {
1579 // The elapsed time is so small it's not worth counting.
1580 if (UsePerfData) {
1581 ClassLoader::perf_classes_inited()->inc();
1582 }
1583 call_class_initializer(THREAD);
1584 }
1585
1586 if (has_strict_static_fields() && !HAS_PENDING_EXCEPTION) {
1587 // Step 9 also verifies that strict static fields have been initialized.
1588 // Status bits were set in ClassFileParser::post_process_parsed_stream.
1589 // After <clinit>, bits must all be clear, or else we must throw an error.
1590 // This is an extremely fast check, so we won't bother with a timer.
1591 assert(fields_status() != nullptr, "");
1592 Symbol* bad_strict_static = nullptr;
1593 for (int index = 0; index < fields_status()->length(); index++) {
1594 // Very fast loop over single byte array looking for a set bit.
1595 if (fields_status()->adr_at(index)->is_strict_static_unset()) {
1596 // This strict static field has not been set by the class initializer.
1597 // Note that in the common no-error case, we read no field metadata.
1598 // We only unpack it when we need to report an error.
1599 FieldInfo fi = field(index);
1600 bad_strict_static = fi.name(constants());
1601 if (debug_logging_enabled) {
1602 ResourceMark rm(jt);
1603 const char* msg = format_strict_static_message(bad_strict_static);
1604 log_debug(class, init)("%s", msg);
1605 } else {
1606 // If we are not logging, do not bother to look for a second offense.
1607 break;
1608 }
1609 }
1610 }
1611 if (bad_strict_static != nullptr) {
1612 throw_strict_static_exception(bad_strict_static, "is unset after initialization of", THREAD);
1613 }
1614 }
1615 }
1616
1617 // Step 9
1618 if (!HAS_PENDING_EXCEPTION) {
1619 set_initialization_state_and_notify(fully_initialized, CHECK);
1620 DEBUG_ONLY(vtable().verify(tty, true);)
1621 CompilationPolicy::replay_training_at_init(this, THREAD);
1622 }
1623 else {
1624 // Step 10 and 11
1625 Handle e(THREAD, PENDING_EXCEPTION);
1626 CLEAR_PENDING_EXCEPTION;
1627 // JVMTI has already reported the pending exception
1628 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1629 JvmtiExport::clear_detected_exception(jt);
1630 {
1631 EXCEPTION_MARK;
1632 add_initialization_error(THREAD, e);
1633 set_initialization_state_and_notify(initialization_error, THREAD);
1634 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1648 }
1649 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1650 }
1651
1652
1653 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1654 Handle h_init_lock(THREAD, init_lock());
1655 if (h_init_lock() != nullptr) {
1656 ObjectLocker ol(h_init_lock, THREAD);
1657 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1658 set_init_state(state);
1659 fence_and_clear_init_lock();
1660 ol.notify_all(CHECK);
1661 } else {
1662 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1663 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1664 set_init_state(state);
1665 }
1666 }
1667
1668 void InstanceKlass::notify_strict_static_access(int field_index, bool is_writing, TRAPS) {
1669 guarantee(field_index >= 0 && field_index < fields_status()->length(), "valid field index");
1670 DEBUG_ONLY(FieldInfo debugfi = field(field_index));
1671 assert(debugfi.access_flags().is_strict(), "");
1672 assert(debugfi.access_flags().is_static(), "");
1673 FieldStatus& fs = *fields_status()->adr_at(field_index);
1674 LogTarget(Trace, class, init) lt;
1675 if (lt.is_enabled()) {
1676 ResourceMark rm(THREAD);
1677 LogStream ls(lt);
1678 FieldInfo fi = field(field_index);
1679 ls.print("notify %s %s %s%s ",
1680 external_name(), is_writing? "Write" : "Read",
1681 fs.is_strict_static_unset() ? "Unset" : "(set)",
1682 fs.is_strict_static_unread() ? "+Unread" : "");
1683 fi.print(&ls, constants());
1684 }
1685 if (fs.is_strict_static_unset()) {
1686 assert(fs.is_strict_static_unread(), "ClassFileParser resp.");
1687 // If it is not set, there are only two reasonable things we can do here:
1688 // - mark it set if this is putstatic
1689 // - throw an error (Read-Before-Write) if this is getstatic
1690
1691 // The unset state is (or should be) transient, and observable only in one
1692 // thread during the execution of <clinit>. Something is wrong here as this
1693 // should not be possible
1694 guarantee(is_reentrant_initialization(THREAD), "unscoped access to strict static");
1695 if (is_writing) {
1696 // clear the "unset" bit, since the field is actually going to be written
1697 fs.update_strict_static_unset(false);
1698 } else {
1699 // throw an IllegalStateException, since we are reading before writing
1700 // see also InstanceKlass::initialize_impl, Step 8 (at end)
1701 Symbol* bad_strict_static = field(field_index).name(constants());
1702 throw_strict_static_exception(bad_strict_static, "is unset before first read in", CHECK);
1703 }
1704 } else {
1705 // Ensure no write after read for final strict statics
1706 FieldInfo fi = field(field_index);
1707 bool is_final = fi.access_flags().is_final();
1708 if (is_final) {
1709 // no final write after read, so observing a constant freezes it, as if <clinit> ended early
1710 // (maybe we could trust the constant a little earlier, before <clinit> ends)
1711 if (is_writing && !fs.is_strict_static_unread()) {
1712 Symbol* bad_strict_static = fi.name(constants());
1713 throw_strict_static_exception(bad_strict_static, "is set after read (as final) in", CHECK);
1714 } else if (!is_writing && fs.is_strict_static_unread()) {
1715 fs.update_strict_static_unread(false);
1716 }
1717 }
1718 }
1719 }
1720
1721 void InstanceKlass::throw_strict_static_exception(Symbol* field_name, const char* when, TRAPS) {
1722 ResourceMark rm(THREAD);
1723 const char* msg = format_strict_static_message(field_name, when);
1724 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), msg);
1725 }
1726
1727 const char* InstanceKlass::format_strict_static_message(Symbol* field_name, const char* when) {
1728 stringStream ss;
1729 ss.print("Strict static \"%s\" %s %s",
1730 field_name->as_C_string(),
1731 when == nullptr ? "is unset in" : when,
1732 external_name());
1733 return ss.as_string();
1734 }
1735
1736 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1737 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1738 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1739 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1740
1741 DeoptimizationScope deopt_scope;
1742 {
1743 MutexLocker ml(current, Compile_lock);
1744
1745 set_init_state(InstanceKlass::loaded);
1746 // make sure init_state store is already done.
1747 // The compiler reads the hierarchy outside of the Compile_lock.
1748 // Access ordering is used to add to hierarchy.
1749
1750 // Link into hierarchy.
1751 append_to_sibling_list(); // add to superklass/sibling list
1752 process_interfaces(); // handle all "implements" declarations
1753
1754 // Now mark all code that depended on old class hierarchy.
1755 // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
1992 // Need load-acquire for lock-free read
1993 ObjArrayKlass* oak = array_klasses_acquire();
1994 if (oak == nullptr) {
1995 return nullptr;
1996 } else {
1997 return oak->array_klass_or_null(n);
1998 }
1999 }
2000
2001 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
2002 return array_klass(1, THREAD);
2003 }
2004
2005 ArrayKlass* InstanceKlass::array_klass_or_null() {
2006 return array_klass_or_null(1);
2007 }
2008
2009 Method* InstanceKlass::class_initializer() const {
2010 Method* clinit = find_method(
2011 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
2012 if (clinit != nullptr && clinit->is_class_initializer()) {
2013 return clinit;
2014 }
2015 return nullptr;
2016 }
2017
2018 void InstanceKlass::call_class_initializer(TRAPS) {
2019 if (ReplayCompiles &&
2020 (ReplaySuppressInitializers == 1 ||
2021 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
2022 // Hide the existence of the initializer for the purpose of replaying the compile
2023 return;
2024 }
2025
2026 #if INCLUDE_CDS
2027 // This is needed to ensure the consistency of the archived heap objects.
2028 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
2029 AOTClassInitializer::call_runtime_setup(THREAD, this);
2030 return;
2031 } else if (has_archived_enum_objs()) {
2032 assert(in_aot_cache(), "must be");
2101
2102 void InstanceKlass::mask_for(const methodHandle& method, int bci,
2103 InterpreterOopMap* entry_for) {
2104 // Lazily create the _oop_map_cache at first request.
2105 // Load_acquire is needed to safely get instance published with CAS by another thread.
2106 OopMapCache* oop_map_cache = AtomicAccess::load_acquire(&_oop_map_cache);
2107 if (oop_map_cache == nullptr) {
2108 // Try to install new instance atomically.
2109 oop_map_cache = new OopMapCache();
2110 OopMapCache* other = AtomicAccess::cmpxchg(&_oop_map_cache, (OopMapCache*)nullptr, oop_map_cache);
2111 if (other != nullptr) {
2112 // Someone else managed to install before us, ditch local copy and use the existing one.
2113 delete oop_map_cache;
2114 oop_map_cache = other;
2115 }
2116 }
2117 // _oop_map_cache is constant after init; lookup below does its own locking.
2118 oop_map_cache->lookup(method, bci, entry_for);
2119 }
2120
2121
2122 FieldInfo InstanceKlass::field(int index) const {
2123 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
2124 if (fs.index() == index) {
2125 return fs.to_FieldInfo();
2126 }
2127 }
2128 fatal("Field not found");
2129 return FieldInfo();
2130 }
2131
2132 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
2133 JavaFieldStream fs(this);
2134 if (fs.lookup(name, sig)) {
2135 assert(fs.name() == name, "name must match");
2136 assert(fs.signature() == sig, "signature must match");
2137 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
2138 return true;
2139 }
2140 return false;
2181
2182 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
2183 // search order according to newest JVM spec (5.4.3.2, p.167).
2184 // 1) search for field in current klass
2185 if (find_local_field(name, sig, fd)) {
2186 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
2187 }
2188 // 2) search for field recursively in direct superinterfaces
2189 if (is_static) {
2190 Klass* intf = find_interface_field(name, sig, fd);
2191 if (intf != nullptr) return intf;
2192 }
2193 // 3) apply field lookup recursively if superclass exists
2194 { InstanceKlass* supr = super();
2195 if (supr != nullptr) return supr->find_field(name, sig, is_static, fd);
2196 }
2197 // 4) otherwise field lookup fails
2198 return nullptr;
2199 }
2200
2201 bool InstanceKlass::contains_field_offset(int offset) {
2202 if (this->is_inline_klass()) {
2203 InlineKlass* vk = InlineKlass::cast(this);
2204 return offset >= vk->payload_offset() && offset < (vk->payload_offset() + vk->payload_size_in_bytes());
2205 } else {
2206 fieldDescriptor fd;
2207 return find_field_from_offset(offset, false, &fd);
2208 }
2209 }
2210
2211 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
2212 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
2213 if (fs.offset() == offset) {
2214 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
2215 if (fd->is_static() == is_static) return true;
2216 }
2217 }
2218 return false;
2219 }
2220
2221
2222 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
2223 const InstanceKlass* klass = this;
2224 while (klass != nullptr) {
2225 if (klass->find_local_field_from_offset(offset, is_static, fd)) {
2226 return true;
2227 }
2228 klass = klass->super();
2229 }
2230 return false;
2231 }
2232
2233 bool InstanceKlass::find_local_flat_field_containing_offset(int offset, fieldDescriptor* fd) const {
2234 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
2235 if (!fs.is_flat()) {
2236 continue;
2237 }
2238
2239 if (fs.offset() > offset) {
2240 continue;
2241 }
2242
2243 const int offset_in_flat_field = offset - fs.offset();
2244 const InlineLayoutInfo layout_info = inline_layout_info(fs.index());
2245 const int field_size = layout_info.klass()->layout_size_in_bytes(layout_info.kind());
2246
2247 assert(LayoutKindHelper::is_flat(layout_info.kind()), "Must be flat");
2248
2249 if (offset_in_flat_field < field_size) {
2250 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
2251 assert(!fd->is_static(), "Static fields are not flattened");
2252
2253 return true;
2254 }
2255 }
2256
2257 return false;
2258 }
2259
2260 bool InstanceKlass::find_flat_field_containing_offset(int offset, fieldDescriptor* fd) const {
2261 const InstanceKlass* klass = this;
2262 while (klass != nullptr) {
2263 if (klass->find_local_flat_field_containing_offset(offset, fd)) {
2264 return true;
2265 }
2266
2267 klass = klass->super();
2268 }
2269
2270 return false;
2271 }
2272
2273 void InstanceKlass::methods_do(void f(Method* method)) {
2274 // Methods aren't stable until they are loaded. This can be read outside
2275 // a lock through the ClassLoaderData for profiling
2276 // Redefined scratch classes are on the list and need to be cleaned
2277 if (!is_loaded() && !is_scratch_class()) {
2278 return;
2279 }
2280
2281 int len = methods()->length();
2282 for (int index = 0; index < len; index++) {
2283 Method* m = methods()->at(index);
2284 assert(m->is_method(), "must be method");
2285 f(m);
2286 }
2287 }
2288
2289
2290 void InstanceKlass::do_local_static_fields(FieldClosure* cl) {
2291 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
2292 if (fs.access_flags().is_static()) {
2293 fieldDescriptor& fd = fs.field_descriptor();
2294 cl->do_field(&fd);
2295 }
2296 }
2297 }
2298
2299
2300 void InstanceKlass::do_local_static_fields(void f(fieldDescriptor*, Handle, TRAPS), Handle mirror, TRAPS) {
2301 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
2302 if (fs.access_flags().is_static()) {
2303 fieldDescriptor& fd = fs.field_descriptor();
2304 f(&fd, mirror, CHECK);
2305 }
2306 }
2307 }
2308
2309 void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) {
2310 InstanceKlass* super = this->super();
2311 if (super != nullptr) {
2312 super->do_nonstatic_fields(cl);
2313 }
2314 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
2315 fieldDescriptor& fd = fs.field_descriptor();
2316 if (!fd.is_static()) {
2317 cl->do_field(&fd);
2318 }
2319 }
2320 }
2321
2612 }
2613
2614 // uncached_lookup_method searches both the local class methods array and all
2615 // superclasses methods arrays, skipping any overpass methods in superclasses,
2616 // and possibly skipping private methods.
2617 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2618 const Symbol* signature,
2619 OverpassLookupMode overpass_mode,
2620 PrivateLookupMode private_mode) const {
2621 OverpassLookupMode overpass_local_mode = overpass_mode;
2622 const InstanceKlass* klass = this;
2623 while (klass != nullptr) {
2624 Method* const method = klass->find_method_impl(name,
2625 signature,
2626 overpass_local_mode,
2627 StaticLookupMode::find,
2628 private_mode);
2629 if (method != nullptr) {
2630 return method;
2631 }
2632 if (name == vmSymbols::object_initializer_name()) {
2633 break; // <init> is never inherited
2634 }
2635 klass = klass->super();
2636 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2637 }
2638 return nullptr;
2639 }
2640
2641 #ifdef ASSERT
2642 // search through class hierarchy and return true if this class or
2643 // one of the superclasses was redefined
2644 bool InstanceKlass::has_redefined_this_or_super() const {
2645 const InstanceKlass* klass = this;
2646 while (klass != nullptr) {
2647 if (klass->has_been_redefined()) {
2648 return true;
2649 }
2650 klass = klass->super();
2651 }
2652 return false;
2653 }
2654 #endif
3027 int itable_offset_in_words = (int)(start_of_itable() - (intptr_t*)this);
3028
3029 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words)
3030 / itableOffsetEntry::size();
3031
3032 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
3033 if (ioe->interface_klass() != nullptr) {
3034 it->push(ioe->interface_klass_addr());
3035 itableMethodEntry* ime = ioe->first_method_entry(this);
3036 int n = klassItable::method_count_for_interface(ioe->interface_klass());
3037 for (int index = 0; index < n; index ++) {
3038 it->push(ime[index].method_addr());
3039 }
3040 }
3041 }
3042 }
3043
3044 it->push(&_nest_host);
3045 it->push(&_nest_members);
3046 it->push(&_permitted_subclasses);
3047 it->push(&_loadable_descriptors);
3048 it->push(&_acmp_maps_array, MetaspaceClosure::_writable);
3049 it->push(&_record_components);
3050 it->push(&_inline_layout_info_array, MetaspaceClosure::_writable);
3051
3052 if (CDSConfig::is_dumping_full_module_graph() && !defined_by_other_loaders()) {
3053 it->push(&_package_entry);
3054 }
3055 }
3056
3057 #if INCLUDE_CDS
3058 void InstanceKlass::remove_unshareable_info() {
3059
3060 if (is_linked()) {
3061 assert(can_be_verified_at_dumptime(), "must be");
3062 // Remember this so we can avoid walking the hierarchy at runtime.
3063 set_verified_at_dump_time();
3064 }
3065
3066 _misc_flags.set_has_init_deps_processed(false);
3067
3068 Klass::remove_unshareable_info();
3069
3070 if (SystemDictionaryShared::has_class_failed_verification(this)) {
3082
3083 { // Otherwise this needs to take out the Compile_lock.
3084 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
3085 init_implementor();
3086 }
3087
3088 // Call remove_unshareable_info() on other objects that belong to this class, except
3089 // for constants()->remove_unshareable_info(), which is called in a separate pass in
3090 // ArchiveBuilder::make_klasses_shareable(),
3091
3092 for (int i = 0; i < methods()->length(); i++) {
3093 Method* m = methods()->at(i);
3094 m->remove_unshareable_info();
3095 }
3096
3097 // do array classes also.
3098 if (array_klasses() != nullptr) {
3099 array_klasses()->remove_unshareable_info();
3100 }
3101
3102 // These are not allocated from metaspace. They are safe to set to nullptr.
3103 _source_debug_extension = nullptr;
3104 _dep_context = nullptr;
3105 _osr_nmethods_head = nullptr;
3106 #if INCLUDE_JVMTI
3107 _breakpoints = nullptr;
3108 _previous_versions = nullptr;
3109 _cached_class_file = nullptr;
3110 _jvmti_cached_class_field_map = nullptr;
3111 #endif
3112
3113 _init_thread = nullptr;
3114 _methods_jmethod_ids = nullptr;
3115 _jni_ids = nullptr;
3116 _oop_map_cache = nullptr;
3117 if (CDSConfig::is_dumping_method_handles() && HeapShared::is_lambda_proxy_klass(this)) {
3118 // keep _nest_host
3119 } else {
3120 // clear _nest_host to ensure re-load at runtime
3121 _nest_host = nullptr;
3122 }
3158 void InstanceKlass::compute_has_loops_flag_for_methods() {
3159 Array<Method*>* methods = this->methods();
3160 for (int index = 0; index < methods->length(); ++index) {
3161 Method* m = methods->at(index);
3162 if (!m->is_overpass()) { // work around JDK-8305771
3163 m->compute_has_loops_flag();
3164 }
3165 }
3166 }
3167
3168 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
3169 PackageEntry* pkg_entry, TRAPS) {
3170 // InstanceKlass::add_to_hierarchy() sets the init_state to loaded
3171 // before the InstanceKlass is added to the SystemDictionary. Make
3172 // sure the current state is <loaded.
3173 assert(!is_loaded(), "invalid init state");
3174 assert(!shared_loading_failed(), "Must not try to load failed class again");
3175 set_package(loader_data, pkg_entry, CHECK);
3176 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
3177
3178 if (is_inline_klass()) {
3179 InlineKlass::cast(this)->initialize_calling_convention(CHECK);
3180 }
3181
3182 Array<Method*>* methods = this->methods();
3183 int num_methods = methods->length();
3184 for (int index = 0; index < num_methods; ++index) {
3185 methods->at(index)->restore_unshareable_info(CHECK);
3186 }
3187 #if INCLUDE_JVMTI
3188 if (JvmtiExport::has_redefined_a_class()) {
3189 // Reinitialize vtable because RedefineClasses may have changed some
3190 // entries in this vtable for super classes so the CDS vtable might
3191 // point to old or obsolete entries. RedefineClasses doesn't fix up
3192 // vtables in the shared system dictionary, only the main one.
3193 // It also redefines the itable too so fix that too.
3194 // First fix any default methods that point to a super class that may
3195 // have been redefined.
3196 bool trace_name_printed = false;
3197 adjust_default_methods(&trace_name_printed);
3198 if (verified_at_dump_time()) {
3199 // Initialize vtable and itable for classes which can be verified at dump time.
3200 // Unlinked classes such as old classes with major version < 50 cannot be verified
3201 // at dump time.
3202 vtable().initialize_vtable();
3203 itable().initialize_itable();
3204 }
3205 }
3206 #endif // INCLUDE_JVMTI
3207
3208 // restore constant pool resolved references
3209 constants()->restore_unshareable_info(CHECK);
3210
3211 // Restore acmp_maps java array from the version stored in metadata.
3212 // if it cannot be found in the archive
3213 if (Arguments::is_valhalla_enabled() && has_acmp_maps_offset() && java_mirror()->obj_field(_acmp_maps_offset) == nullptr) {
3214 int acmp_maps_size = _acmp_maps_array->length();
3215 typeArrayOop map = oopFactory::new_intArray(acmp_maps_size, CHECK);
3216 typeArrayHandle map_h(THREAD, map);
3217 for (int i = 0; i < acmp_maps_size; i++) {
3218 map_h->int_at_put(i, _acmp_maps_array->at(i));
3219 }
3220 java_mirror()->obj_field_put(_acmp_maps_offset, map_h());
3221 }
3222
3223 if (array_klasses() != nullptr) {
3224 // To get a consistent list of classes we need MultiArray_lock to ensure
3225 // array classes aren't observed while they are being restored.
3226 RecursiveLocker rl(MultiArray_lock, THREAD);
3227 assert(this == ObjArrayKlass::cast(array_klasses())->bottom_klass(), "sanity");
3228 // Array classes have null protection domain.
3229 // --> see ArrayKlass::complete_create_array_klass()
3230 if (class_loader_data() == nullptr) {
3231 ResourceMark rm(THREAD);
3232 log_debug(cds)(" loader_data %s ", loader_data == nullptr ? "nullptr" : "non null");
3233 log_debug(cds)(" this %s array_klasses %s ", this->name()->as_C_string(), array_klasses()->name()->as_C_string());
3234 }
3235 assert(!array_klasses()->is_refined_objArray_klass(), "must be non-refined objarrayklass");
3236 array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
3237 }
3238
3239 // Initialize @ValueBased class annotation if not already set in the archived klass.
3240 if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
3241 set_is_value_based();
3242 }
3243
3244 DEBUG_ONLY(FieldInfoStream::validate_search_table(_constants, _fieldinfo_stream, _fieldinfo_search_table));
3245 }
3246
3247 bool InstanceKlass::can_be_verified_at_dumptime() const {
3248 if (CDSConfig::is_dumping_dynamic_archive() && AOTMetaspace::in_aot_cache(this)) {
3249 // This is a class that was dumped into the base archive, so we know
3250 // it was verified at dump time.
3251 return true;
3252 }
3253
3254 if (CDSConfig::is_preserving_verification_constraints()) {
3255 return true;
3371 constants()->release_C_heap_structures();
3372 }
3373 }
3374
3375 // The constant pool is on stack if any of the methods are executing or
3376 // referenced by handles.
3377 bool InstanceKlass::on_stack() const {
3378 return _constants->on_stack();
3379 }
3380
3381 Symbol* InstanceKlass::source_file_name() const { return _constants->source_file_name(); }
3382 u2 InstanceKlass::source_file_name_index() const { return _constants->source_file_name_index(); }
3383 void InstanceKlass::set_source_file_name_index(u2 sourcefile_index) { _constants->set_source_file_name_index(sourcefile_index); }
3384
3385 // minor and major version numbers of class file
3386 u2 InstanceKlass::minor_version() const { return _constants->minor_version(); }
3387 void InstanceKlass::set_minor_version(u2 minor_version) { _constants->set_minor_version(minor_version); }
3388 u2 InstanceKlass::major_version() const { return _constants->major_version(); }
3389 void InstanceKlass::set_major_version(u2 major_version) { _constants->set_major_version(major_version); }
3390
3391 bool InstanceKlass::supports_inline_types() const {
3392 return major_version() >= Verifier::VALUE_TYPES_MAJOR_VERSION && minor_version() == Verifier::JAVA_PREVIEW_MINOR_VERSION;
3393 }
3394
3395 const InstanceKlass* InstanceKlass::get_klass_version(int version) const {
3396 for (const InstanceKlass* ik = this; ik != nullptr; ik = ik->previous_versions()) {
3397 if (ik->constants()->version() == version) {
3398 return ik;
3399 }
3400 }
3401 return nullptr;
3402 }
3403
3404 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
3405 if (array == nullptr) {
3406 _source_debug_extension = nullptr;
3407 } else {
3408 // Adding one to the attribute length in order to store a null terminator
3409 // character could cause an overflow because the attribute length is
3410 // already coded with an u4 in the classfile, but in practice, it's
3411 // unlikely to happen.
3412 assert((length+1) > length, "Overflow checking");
3413 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
3414 for (int i = 0; i < length; i++) {
3415 sde[i] = array[i];
3416 }
3417 sde[length] = '\0';
3418 _source_debug_extension = sde;
3419 }
3420 }
3421
3422 Symbol* InstanceKlass::generic_signature() const { return _constants->generic_signature(); }
3423 u2 InstanceKlass::generic_signature_index() const { return _constants->generic_signature_index(); }
3424 void InstanceKlass::set_generic_signature_index(u2 sig_index) { _constants->set_generic_signature_index(sig_index); }
3425
3426 const char* InstanceKlass::signature_name() const {
3427 // Get the internal name as a c string
3428 const char* src = (const char*) (name()->as_C_string());
3429 const int src_length = (int)strlen(src);
3430
3431 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
3432
3433 // Add L as type indicator
3434 int dest_index = 0;
3435 dest[dest_index++] = JVM_SIGNATURE_CLASS;
3436
3437 // Add the actual class name
3438 for (int src_index = 0; src_index < src_length; ) {
3439 dest[dest_index++] = src[src_index++];
3440 }
3441
3442 if (is_hidden()) { // Replace the last '+' with a '.'.
3443 for (int index = (int)src_length; index > 0; index--) {
3444 if (dest[index] == '+') {
3445 dest[index] = JVM_SIGNATURE_DOT;
3446 break;
3696 bool InstanceKlass::find_inner_classes_attr(int* ooff, int* noff, TRAPS) const {
3697 constantPoolHandle i_cp(THREAD, constants());
3698 for (InnerClassesIterator iter(this); !iter.done(); iter.next()) {
3699 int ioff = iter.inner_class_info_index();
3700 if (ioff != 0) {
3701 // Check to see if the name matches the class we're looking for
3702 // before attempting to find the class.
3703 if (i_cp->klass_name_at_matches(this, ioff)) {
3704 Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
3705 if (this == inner_klass) {
3706 *ooff = iter.outer_class_info_index();
3707 *noff = iter.inner_name_index();
3708 return true;
3709 }
3710 }
3711 }
3712 }
3713 return false;
3714 }
3715
3716 void InstanceKlass::check_can_be_annotated_with_NullRestricted(InstanceKlass* type, Symbol* container_klass_name, TRAPS) {
3717 assert(type->is_instance_klass(), "Sanity check");
3718 if (type->is_identity_class()) {
3719 ResourceMark rm(THREAD);
3720 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
3721 err_msg("Class %s expects class %s to be a value class, but it is an identity class",
3722 container_klass_name->as_C_string(),
3723 type->external_name()));
3724 }
3725
3726 if (type->is_abstract()) {
3727 ResourceMark rm(THREAD);
3728 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
3729 err_msg("Class %s expects class %s to be concrete value type, but it is an abstract class",
3730 container_klass_name->as_C_string(),
3731 type->external_name()));
3732 }
3733 }
3734
3735 InstanceKlass* InstanceKlass::compute_enclosing_class(bool* inner_is_member, TRAPS) const {
3736 InstanceKlass* outer_klass = nullptr;
3737 *inner_is_member = false;
3738 int ooff = 0, noff = 0;
3739 bool has_inner_classes_attr = find_inner_classes_attr(&ooff, &noff, THREAD);
3740 if (has_inner_classes_attr) {
3741 constantPoolHandle i_cp(THREAD, constants());
3742 if (ooff != 0) {
3743 Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
3744 if (!ok->is_instance_klass()) {
3745 // If the outer class is not an instance klass then it cannot have
3746 // declared any inner classes.
3747 ResourceMark rm(THREAD);
3748 // Names are all known to be < 64k so we know this formatted message is not excessively large.
3749 Exceptions::fthrow(
3750 THREAD_AND_LOCATION,
3751 vmSymbols::java_lang_IncompatibleClassChangeError(),
3752 "%s and %s disagree on InnerClasses attribute",
3753 ok->external_name(),
3754 external_name());
3781 u2 InstanceKlass::compute_modifier_flags() const {
3782 u2 access = access_flags().as_unsigned_short();
3783
3784 // But check if it happens to be member class.
3785 InnerClassesIterator iter(this);
3786 for (; !iter.done(); iter.next()) {
3787 int ioff = iter.inner_class_info_index();
3788 // Inner class attribute can be zero, skip it.
3789 // Strange but true: JVM spec. allows null inner class refs.
3790 if (ioff == 0) continue;
3791
3792 // only look at classes that are already loaded
3793 // since we are looking for the flags for our self.
3794 Symbol* inner_name = constants()->klass_name_at(ioff);
3795 if (name() == inner_name) {
3796 // This is really a member class.
3797 access = iter.inner_access_flags();
3798 break;
3799 }
3800 }
3801 if (!Arguments::is_valhalla_enabled()) {
3802 // Remember to strip ACC_SUPER bit without Valhalla
3803 access &= (~JVM_ACC_SUPER);
3804 }
3805 return access;
3806 }
3807
3808 jint InstanceKlass::jvmti_class_status() const {
3809 jint result = 0;
3810
3811 if (is_linked()) {
3812 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3813 }
3814
3815 if (is_initialized()) {
3816 assert(is_linked(), "Class status is not consistent");
3817 result |= JVMTI_CLASS_STATUS_INITIALIZED;
3818 }
3819 if (is_in_error_state()) {
3820 result |= JVMTI_CLASS_STATUS_ERROR;
3821 }
3822 return result;
3823 }
3824
3825 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {
4071 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
4072 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
4073 }
4074
4075 const char* InstanceKlass::init_state_name() const {
4076 return state_names[init_state()];
4077 }
4078
4079 void InstanceKlass::print_class_flags(outputStream* st) const {
4080 AccessFlags flags(compute_modifier_flags());
4081 if (flags.is_public ()) st->print("public ");
4082 if (flags.is_private ()) st->print("private ");
4083 if (flags.is_protected ()) st->print("protected ");
4084 if (flags.is_static ()) st->print("static ");
4085 if (flags.is_final ()) st->print("final ");
4086 if (flags.is_interface ()) st->print("interface ");
4087 if (flags.is_abstract ()) st->print("abstract ");
4088 if (flags.is_annotation()) st->print("annotation ");
4089 if (flags.is_enum ()) st->print("enum ");
4090 if (flags.is_synthetic ()) st->print("synthetic ");
4091 if (Arguments::is_valhalla_enabled()) {
4092 if (flags.is_identity_class()) st->print("identity ");
4093 if (!flags.is_identity_class()) st->print("value " );
4094 }
4095 }
4096
4097 void InstanceKlass::print_on(outputStream* st) const {
4098 assert(is_klass(), "must be klass");
4099 Klass::print_on(st);
4100
4101 st->print(BULLET"instance size: %d", size_helper()); st->cr();
4102 st->print(BULLET"klass size: %d", size()); st->cr();
4103 st->print(BULLET"access: "); print_class_flags(st); st->cr();
4104 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
4105 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
4106 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
4107 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
4108 st->print(BULLET"sub: ");
4109 Klass* sub = subklass();
4110 int n;
4111 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
4112 if (n < MaxSubklassPrintSize) {
4113 sub->print_value_on(st);
4114 st->print(" ");
4115 }
4116 }
4117 if (n >= MaxSubklassPrintSize) st->print("(%zd more klasses...)", n - MaxSubklassPrintSize);
4118 st->cr();
4119
4120 if (is_interface()) {
4121 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
4122 if (nof_implementors() == 1) {
4123 st->print_cr(BULLET"implementor: ");
4124 st->print(" ");
4125 implementor()->print_value_on(st);
4126 st->cr();
4127 }
4128 }
4129
4130 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
4131 st->print(BULLET"methods: ");
4132 print_array_on(st, methods(), [](outputStream* ost, Method* method) {
4133 method->print_value_on(ost);
4134 });
4135 st->print(BULLET"method ordering: ");
4136 print_array_on(st, method_ordering(), [](outputStream* ost, int i) {
4137 ost->print("%d", i);
4138 });
4139 if (default_methods() != nullptr) {
4140 st->print(BULLET"default_methods: ");
4141 print_array_on(st, default_methods(), [](outputStream* ost, Method* method) {
4142 method->print_value_on(ost);
4143 });
4144 }
4145 print_on_maybe_null(st, BULLET"default vtable indices: ", default_vtable_indices());
4146 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
4147 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
4148
4149 st->print(BULLET"secondary supers: "); secondary_supers()->print_value_on(st); st->cr();
4150
4151 st->print(BULLET"hash_slot: %d", hash_slot()); st->cr();
4152 st->print(BULLET"secondary bitmap: " UINTX_FORMAT_X_0, _secondary_supers_bitmap); st->cr();
4153
4154 if (secondary_supers() != nullptr) {
4155 if (Verbose) {
4156 bool is_hashed = (_secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_FULL);
4157 st->print_cr(BULLET"---- secondary supers (%d words):", _secondary_supers->length());
4158 for (int i = 0; i < _secondary_supers->length(); i++) {
4159 ResourceMark rm; // for external_name()
4160 Klass* secondary_super = _secondary_supers->at(i);
4161 st->print(BULLET"%2d:", i);
4162 if (is_hashed) {
4163 int home_slot = compute_home_slot(secondary_super, _secondary_supers_bitmap);
4164 int distance = (i - home_slot) & SECONDARY_SUPERS_TABLE_MASK;
4165 st->print(" dist:%02d:", distance);
4166 }
4167 st->print_cr(" %p %s", secondary_super, secondary_super->external_name());
4168 }
4169 }
4170 }
4171 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
4172
4173 print_on_maybe_null(st, BULLET"class loader data: ", class_loader_data());
4174 print_on_maybe_null(st, BULLET"source file: ", source_file_name());
4175 if (source_debug_extension() != nullptr) {
4176 st->print(BULLET"source debug extension: ");
4177 st->print("%s", source_debug_extension());
4178 st->cr();
4179 }
4180 print_on_maybe_null(st, BULLET"class annotations: ", class_annotations());
4181 print_on_maybe_null(st, BULLET"class type annotations: ", class_type_annotations());
4182 print_on_maybe_null(st, BULLET"field annotations: ", fields_annotations());
4183 print_on_maybe_null(st, BULLET"field type annotations: ", fields_type_annotations());
4184 {
4185 bool have_pv = false;
4186 // previous versions are linked together through the InstanceKlass
4187 for (InstanceKlass* pv_node = previous_versions();
4188 pv_node != nullptr;
4189 pv_node = pv_node->previous_versions()) {
4190 if (!have_pv)
4191 st->print(BULLET"previous version: ");
4192 have_pv = true;
4193 pv_node->constants()->print_value_on(st);
4194 }
4195 if (have_pv) st->cr();
4196 }
4197
4198 print_on_maybe_null(st, BULLET"generic signature: ", generic_signature());
4199 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
4200 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
4201 print_on_maybe_null(st, BULLET"record components: ", record_components());
4202 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
4203 st->print(BULLET"loadable descriptors: "); loadable_descriptors()->print_value_on(st); st->cr();
4204 if (java_mirror() != nullptr) {
4205 st->print(BULLET"java mirror: ");
4206 java_mirror()->print_value_on(st);
4207 st->cr();
4208 } else {
4209 st->print_cr(BULLET"java mirror: null");
4210 }
4211 st->print(BULLET"vtable length %d (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
4212 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
4213 st->print(BULLET"itable length %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
4214 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
4215
4216 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
4217 // There is no oop so static and nonstatic printing can use the same printer.
4218 FieldPrinter field_printer(st);
4219 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
4220 ik->do_local_static_fields(&field_printer);
4221 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
4222 ik->print_nonstatic_fields(&field_printer);
4223
4224 st->print(BULLET"non-static oop maps (%d entries): ", nonstatic_oop_map_count());
4225 OopMapBlock* map = start_of_nonstatic_oop_maps();
4226 OopMapBlock* end_map = map + nonstatic_oop_map_count();
4227 while (map < end_map) {
4228 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
4229 map++;
4230 }
4231 st->cr();
4232
4233 if (fieldinfo_search_table() != nullptr) {
4234 st->print_cr(BULLET"---- field info search table:");
4235 FieldInfoStream::print_search_table(st, _constants, _fieldinfo_stream, _fieldinfo_search_table);
4236 }
4237 }
4238
4239 void InstanceKlass::print_value_on(outputStream* st) const {
4240 assert(is_klass(), "must be klass");
4241 if (Verbose || WizardMode) print_class_flags(st);
4242 name()->print_value_on(st);
4243 }
4244
4245 void FieldPrinter::do_field(fieldDescriptor* fd) {
4246 for (int i = 0; i < _indent; i++) _st->print(" ");
4247 _st->print(BULLET);
4248 // Handles the cases of static fields or instance fields but no oop is given.
4249 if (_obj == nullptr) {
4250 fd->print_on(_st, _base_offset);
4251 _st->cr();
4252 } else {
4253 fd->print_on_for(_st, _obj, _indent, _base_offset);
4254 if (!fd->field_flags().is_flat()) _st->cr();
4255 }
4256 }
4257
4258
4259 void InstanceKlass::oop_print_on(oop obj, outputStream* st, int indent, int base_offset) {
4260 Klass::oop_print_on(obj, st);
4261
4262 if (this == vmClasses::String_klass()) {
4263 typeArrayOop value = java_lang_String::value(obj);
4264 juint length = java_lang_String::length(obj);
4265 if (value != nullptr &&
4266 value->is_typeArray() &&
4267 length <= (juint) value->length()) {
4268 st->print(BULLET"string: ");
4269 java_lang_String::print(obj, st);
4270 st->cr();
4271 }
4272 }
4273
4274 st->print_cr(BULLET"---- fields (total size %zu words):", oop_size(obj));
4275 FieldPrinter print_field(st, obj, indent, base_offset);
4276 print_nonstatic_fields(&print_field);
4277
4278 if (this == vmClasses::Class_klass()) {
4279 st->print(BULLET"signature: ");
4280 java_lang_Class::print_signature(obj, st);
4281 st->cr();
4282 Klass* real_klass = java_lang_Class::as_Klass(obj);
4283 if (real_klass != nullptr && real_klass->is_instance_klass()) {
4284 st->print_cr(BULLET"---- static fields (%d):", java_lang_Class::static_oop_field_count(obj));
4285 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
4286 }
4287 } else if (this == vmClasses::MethodType_klass()) {
4288 st->print(BULLET"signature: ");
4289 java_lang_invoke_MethodType::print_signature(obj, st);
4290 st->cr();
4291 }
4292 }
4293
4294 #ifndef PRODUCT
4295
|