28 #include "cds/cdsEnumKlass.hpp"
29 #include "cds/classListWriter.hpp"
30 #include "cds/heapShared.hpp"
31 #include "cds/metaspaceShared.hpp"
32 #include "classfile/classFileParser.hpp"
33 #include "classfile/classFileStream.hpp"
34 #include "classfile/classLoader.hpp"
35 #include "classfile/classLoaderData.inline.hpp"
36 #include "classfile/javaClasses.hpp"
37 #include "classfile/moduleEntry.hpp"
38 #include "classfile/systemDictionary.hpp"
39 #include "classfile/systemDictionaryShared.hpp"
40 #include "classfile/verifier.hpp"
41 #include "classfile/vmClasses.hpp"
42 #include "classfile/vmSymbols.hpp"
43 #include "code/codeCache.hpp"
44 #include "code/dependencyContext.hpp"
45 #include "compiler/compilationPolicy.hpp"
46 #include "compiler/compileBroker.hpp"
47 #include "gc/shared/collectedHeap.inline.hpp"
48 #include "interpreter/bytecodeStream.hpp"
49 #include "interpreter/oopMapCache.hpp"
50 #include "interpreter/rewriter.hpp"
51 #include "jvm.h"
52 #include "jvmtifiles/jvmti.h"
53 #include "logging/log.hpp"
54 #include "klass.inline.hpp"
55 #include "logging/logMessage.hpp"
56 #include "logging/logStream.hpp"
57 #include "memory/allocation.inline.hpp"
58 #include "memory/iterator.inline.hpp"
59 #include "memory/metadataFactory.hpp"
60 #include "memory/metaspaceClosure.hpp"
61 #include "memory/oopFactory.hpp"
62 #include "memory/resourceArea.hpp"
63 #include "memory/universe.hpp"
64 #include "oops/fieldStreams.inline.hpp"
65 #include "oops/constantPool.hpp"
66 #include "oops/instanceClassLoaderKlass.hpp"
67 #include "oops/instanceKlass.inline.hpp"
68 #include "oops/instanceMirrorKlass.hpp"
69 #include "oops/instanceOop.hpp"
70 #include "oops/instanceStackChunkKlass.hpp"
71 #include "oops/klass.inline.hpp"
72 #include "oops/method.hpp"
73 #include "oops/oop.inline.hpp"
74 #include "oops/recordComponent.hpp"
75 #include "oops/symbol.hpp"
76 #include "prims/jvmtiExport.hpp"
77 #include "prims/jvmtiRedefineClasses.hpp"
78 #include "prims/jvmtiThreadState.hpp"
79 #include "prims/methodComparator.hpp"
80 #include "runtime/arguments.hpp"
81 #include "runtime/deoptimization.hpp"
82 #include "runtime/atomic.hpp"
83 #include "runtime/fieldDescriptor.inline.hpp"
84 #include "runtime/handles.inline.hpp"
85 #include "runtime/javaCalls.hpp"
86 #include "runtime/javaThread.inline.hpp"
87 #include "runtime/mutexLocker.hpp"
88 #include "runtime/orderAccess.hpp"
89 #include "runtime/os.inline.hpp"
90 #include "runtime/reflection.hpp"
91 #include "runtime/synchronizer.hpp"
92 #include "runtime/threads.hpp"
93 #include "services/classLoadingService.hpp"
94 #include "services/finalizerService.hpp"
95 #include "services/threadService.hpp"
96 #include "utilities/dtrace.hpp"
97 #include "utilities/events.hpp"
98 #include "utilities/macros.hpp"
99 #include "utilities/nativeStackPrinter.hpp"
100 #include "utilities/pair.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
862 }
863
864 if (log_is_enabled(Info, cds, init)) {
865 ResourceMark rm;
866 log_info(cds, init)("%s (aot-inited)", external_name());
867 }
868
869 link_class(CHECK);
870
871 #ifdef ASSERT
872 {
873 Handle h_init_lock(THREAD, init_lock());
874 ObjectLocker ol(h_init_lock, THREAD);
875 assert(!is_initialized(), "sanity");
876 assert(!is_being_initialized(), "sanity");
877 assert(!is_in_error_state(), "sanity");
878 }
879 #endif
880
881 set_init_thread(THREAD);
882 AOTClassInitializer::call_runtime_setup(THREAD, this);
883 set_initialization_state_and_notify(fully_initialized, CHECK);
884 }
885 #endif
886
887 bool InstanceKlass::verify_code(TRAPS) {
888 // 1) Verify the bytecodes
889 return Verifier::verify(this, should_verify_class(), THREAD);
890 }
891
892 void InstanceKlass::link_class(TRAPS) {
893 assert(is_loaded(), "must be loaded");
894 if (!is_linked()) {
895 link_class_impl(CHECK);
896 }
897 }
898
899 // Called to verify that a class can link during initialization, without
900 // throwing a VerifyError.
901 bool InstanceKlass::link_class_or_fail(TRAPS) {
902 assert(is_loaded(), "must be loaded");
903 if (!is_linked()) {
1059 return true;
1060 }
1061
1062 // Rewrite the byte codes of all of the methods of a class.
1063 // The rewriter must be called exactly once. Rewriting must happen after
1064 // verification but before the first method of the class is executed.
1065 void InstanceKlass::rewrite_class(TRAPS) {
1066 assert(is_loaded(), "must be loaded");
1067 if (is_rewritten()) {
1068 assert(is_shared(), "rewriting an unshared class?");
1069 return;
1070 }
1071 Rewriter::rewrite(this, CHECK);
1072 set_rewritten();
1073 }
1074
1075 // Now relocate and link method entry points after class is rewritten.
1076 // This is outside is_rewritten flag. In case of an exception, it can be
1077 // executed more than once.
1078 void InstanceKlass::link_methods(TRAPS) {
1079 PerfTraceTime timer(ClassLoader::perf_ik_link_methods_time());
1080
1081 int len = methods()->length();
1082 for (int i = len-1; i >= 0; i--) {
1083 methodHandle m(THREAD, methods()->at(i));
1084
1085 // Set up method entry points for compiler and interpreter .
1086 m->link_method(m, CHECK);
1087 }
1088 }
1089
1090 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1091 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1092 assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1093 for (int i = 0; i < local_interfaces()->length(); ++i) {
1094 InstanceKlass* ik = local_interfaces()->at(i);
1095
1096 // Initialization is depth first search ie. we start with top of the inheritance tree
1097 // has_nonstatic_concrete_methods drives searching superinterfaces since it
1098 // means has_nonstatic_concrete_methods in its superinterface hierarchy
1099 if (ik->has_nonstatic_concrete_methods()) {
1100 ik->initialize_super_interfaces(CHECK);
1101 }
1102
1103 // Only initialize() interfaces that "declare" concrete methods.
1168 assert_locked_or_safepoint(ClassInitError_lock);
1169 InitErrorTableCleaner cleaner;
1170 if (_initialization_error_table != nullptr) {
1171 _initialization_error_table->unlink(&cleaner);
1172 }
1173 }
1174
1175 void InstanceKlass::initialize_impl(TRAPS) {
1176 HandleMark hm(THREAD);
1177
1178 // Make sure klass is linked (verified) before initialization
1179 // A class could already be verified, since it has been reflected upon.
1180 link_class(CHECK);
1181
1182 DTRACE_CLASSINIT_PROBE(required, -1);
1183
1184 bool wait = false;
1185
1186 JavaThread* jt = THREAD;
1187
1188 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1189
1190 // refer to the JVM book page 47 for description of steps
1191 // Step 1
1192 {
1193 Handle h_init_lock(THREAD, init_lock());
1194 ObjectLocker ol(h_init_lock, jt);
1195
1196 // Step 2
1197 // If we were to use wait() instead of waitInterruptibly() then
1198 // we might end up throwing IE from link/symbol resolution sites
1199 // that aren't expected to throw. This would wreak havoc. See 6320309.
1200 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1201 if (debug_logging_enabled) {
1202 ResourceMark rm(jt);
1203 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1204 jt->name(), external_name(), init_thread_name());
1205 }
1206 wait = true;
1207 jt->set_class_to_be_initialized(this);
1306 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1307 ClassLoader::perf_class_init_selftime(),
1308 ClassLoader::perf_classes_inited(),
1309 jt->get_thread_stat()->perf_recursion_counts_addr(),
1310 jt->get_thread_stat()->perf_timers_addr(),
1311 PerfClassTraceTime::CLASS_CLINIT);
1312 call_class_initializer(THREAD);
1313 } else {
1314 // The elapsed time is so small it's not worth counting.
1315 if (UsePerfData) {
1316 ClassLoader::perf_classes_inited()->inc();
1317 }
1318 call_class_initializer(THREAD);
1319 }
1320 }
1321
1322 // Step 9
1323 if (!HAS_PENDING_EXCEPTION) {
1324 set_initialization_state_and_notify(fully_initialized, CHECK);
1325 debug_only(vtable().verify(tty, true);)
1326 }
1327 else {
1328 // Step 10 and 11
1329 Handle e(THREAD, PENDING_EXCEPTION);
1330 CLEAR_PENDING_EXCEPTION;
1331 // JVMTI has already reported the pending exception
1332 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1333 JvmtiExport::clear_detected_exception(jt);
1334 {
1335 EXCEPTION_MARK;
1336 add_initialization_error(THREAD, e);
1337 set_initialization_state_and_notify(initialization_error, THREAD);
1338 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1339 // JVMTI has already reported the pending exception
1340 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1341 JvmtiExport::clear_detected_exception(jt);
1342 }
1343 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1344 if (e->is_a(vmClasses::Error_klass())) {
1345 THROW_OOP(e());
1346 } else {
1347 JavaCallArguments args(e);
1348 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1349 vmSymbols::throwable_void_signature(),
1350 &args);
1351 }
1352 }
1353 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1354 }
1355
1356
1357 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1358 Handle h_init_lock(THREAD, init_lock());
1359 if (h_init_lock() != nullptr) {
1360 ObjectLocker ol(h_init_lock, THREAD);
1361 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1362 set_init_state(state);
1363 fence_and_clear_init_lock();
1364 ol.notify_all(CHECK);
1365 } else {
1366 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1367 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1368 set_init_state(state);
1369 }
1370 }
1371
1372 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1373 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1374 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1375 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1376
1566 tty->print("Registered ");
1567 i->print_value_on(tty);
1568 tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1569 }
1570 instanceHandle h_i(THREAD, i);
1571 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1572 JavaValue result(T_VOID);
1573 JavaCallArguments args(h_i);
1574 methodHandle mh(THREAD, Universe::finalizer_register_method());
1575 JavaCalls::call(&result, mh, &args, CHECK_NULL);
1576 MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1577 return h_i();
1578 }
1579
1580 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1581 assert(!is_abstract() && !is_interface(), "Should not create this object");
1582 size_t size = size_helper(); // Query before forming handle.
1583 return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1584 }
1585
1586 instanceOop InstanceKlass::allocate_instance(oop java_class, TRAPS) {
1587 Klass* k = java_lang_Class::as_Klass(java_class);
1588 if (k == nullptr) {
1589 ResourceMark rm(THREAD);
1590 THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1591 }
1592 InstanceKlass* ik = cast(k);
1593 ik->check_valid_for_instantiation(false, CHECK_NULL);
1594 ik->initialize(CHECK_NULL);
1595 return ik->allocate_instance(THREAD);
1596 }
1597
1598 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1599 return instanceHandle(THREAD, allocate_instance(THREAD));
1600 }
1601
1602 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1603 if (is_interface() || is_abstract()) {
1604 ResourceMark rm(THREAD);
1605 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1606 : vmSymbols::java_lang_InstantiationException(), external_name());
1634 }
1635
1636 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1637 // Need load-acquire for lock-free read
1638 ObjArrayKlass* oak = array_klasses_acquire();
1639 if (oak == nullptr) {
1640 return nullptr;
1641 } else {
1642 return oak->array_klass_or_null(n);
1643 }
1644 }
1645
1646 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1647 return array_klass(1, THREAD);
1648 }
1649
1650 ArrayKlass* InstanceKlass::array_klass_or_null() {
1651 return array_klass_or_null(1);
1652 }
1653
1654 static int call_class_initializer_counter = 0; // for debugging
1655
1656 Method* InstanceKlass::class_initializer() const {
1657 Method* clinit = find_method(
1658 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1659 if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1660 return clinit;
1661 }
1662 return nullptr;
1663 }
1664
1665 void InstanceKlass::call_class_initializer(TRAPS) {
1666 if (ReplayCompiles &&
1667 (ReplaySuppressInitializers == 1 ||
1668 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1669 // Hide the existence of the initializer for the purpose of replaying the compile
1670 return;
1671 }
1672
1673 #if INCLUDE_CDS
1674 // This is needed to ensure the consistency of the archived heap objects.
1675 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1676 AOTClassInitializer::call_runtime_setup(THREAD, this);
1677 return;
1678 } else if (has_archived_enum_objs()) {
1679 assert(is_shared(), "must be");
1680 bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
1681 if (initialized) {
1682 return;
1683 }
1684 }
1685 #endif
1686
1687 methodHandle h_method(THREAD, class_initializer());
1688 assert(!is_initialized(), "we cannot initialize twice");
1689 LogTarget(Info, class, init) lt;
1690 if (lt.is_enabled()) {
1691 ResourceMark rm(THREAD);
1692 LogStream ls(lt);
1693 ls.print("%d Initializing ", call_class_initializer_counter++);
1694 name()->print_value_on(&ls);
1695 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1696 h_method() == nullptr ? "(no method)" : "", p2i(this),
1697 THREAD->name());
1698 }
1699 if (h_method() != nullptr) {
1700 ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1701 JavaCallArguments args; // No arguments
1702 JavaValue result(T_VOID);
1703 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1704 }
1705 }
1706
1707 // If a class that implements this interface is initialized, is the JVM required
1708 // to first execute a <clinit> method declared in this interface,
1709 // or (if also_check_supers==true) any of the super types of this interface?
1710 //
1711 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1712 // an interface, then let SC be its superclass and let SI1, ..., SIn
1713 // be all superinterfaces of C (whether direct or indirect) that
1714 // declare at least one non-abstract, non-static method.
1715 //
1716 // So when an interface is initialized, it does not look at its
1717 // supers. But a proper class will ensure that all of its supers have
1718 // run their <clinit> methods, except that it disregards interfaces
1719 // that lack a non-static concrete method (i.e., a default method).
1720 // Therefore, you should probably call this method only when the
1721 // current class is a super of some proper class, not an interface.
1722 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1723 assert(is_interface(), "must be");
1724
2489 }
2490 }
2491 if (new_jmeths != 0) {
2492 Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
2493 }
2494 }
2495
2496 // Lookup a jmethodID, null if not found. Do no blocking, no allocations, no handles
2497 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2498 int idnum = method->method_idnum();
2499 jmethodID* jmeths = methods_jmethod_ids_acquire();
2500 return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2501 }
2502
2503 inline DependencyContext InstanceKlass::dependencies() {
2504 DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2505 return dep_context;
2506 }
2507
2508 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2509 dependencies().mark_dependent_nmethods(deopt_scope, changes);
2510 }
2511
2512 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2513 dependencies().add_dependent_nmethod(nm);
2514 }
2515
2516 void InstanceKlass::clean_dependency_context() {
2517 dependencies().clean_unloading_dependents();
2518 }
2519
2520 #ifndef PRODUCT
2521 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2522 dependencies().print_dependent_nmethods(verbose);
2523 }
2524
2525 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2526 return dependencies().is_dependent_nmethod(nm);
2527 }
2528 #endif //PRODUCT
2529
2626 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2627 if (ioe->interface_klass() != nullptr) {
2628 it->push(ioe->interface_klass_addr());
2629 itableMethodEntry* ime = ioe->first_method_entry(this);
2630 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2631 for (int index = 0; index < n; index ++) {
2632 it->push(ime[index].method_addr());
2633 }
2634 }
2635 }
2636 }
2637
2638 it->push(&_nest_host);
2639 it->push(&_nest_members);
2640 it->push(&_permitted_subclasses);
2641 it->push(&_record_components);
2642 }
2643
2644 #if INCLUDE_CDS
2645 void InstanceKlass::remove_unshareable_info() {
2646
2647 if (is_linked()) {
2648 assert(can_be_verified_at_dumptime(), "must be");
2649 // Remember this so we can avoid walking the hierarchy at runtime.
2650 set_verified_at_dump_time();
2651 }
2652
2653 Klass::remove_unshareable_info();
2654
2655 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2656 // Classes are attempted to link during dumping and may fail,
2657 // but these classes are still in the dictionary and class list in CLD.
2658 // If the class has failed verification, there is nothing else to remove.
2659 return;
2660 }
2661
2662 // Reset to the 'allocated' state to prevent any premature accessing to
2663 // a shared class at runtime while the class is still being loaded and
2664 // restored. A class' init_state is set to 'loaded' at runtime when it's
2665 // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2666 _init_state = allocated;
2667
2668 { // Otherwise this needs to take out the Compile_lock.
2669 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2670 init_implementor();
2671 }
2801
2802 // restore constant pool resolved references
2803 constants()->restore_unshareable_info(CHECK);
2804
2805 if (array_klasses() != nullptr) {
2806 // To get a consistent list of classes we need MultiArray_lock to ensure
2807 // array classes aren't observed while they are being restored.
2808 RecursiveLocker rl(MultiArray_lock, THREAD);
2809 assert(this == array_klasses()->bottom_klass(), "sanity");
2810 // Array classes have null protection domain.
2811 // --> see ArrayKlass::complete_create_array_klass()
2812 array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2813 }
2814
2815 // Initialize @ValueBased class annotation if not already set in the archived klass.
2816 if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2817 set_is_value_based();
2818 }
2819 }
2820
2821 // Check if a class or any of its supertypes has a version older than 50.
2822 // CDS will not perform verification of old classes during dump time because
2823 // without changing the old verifier, the verification constraint cannot be
2824 // retrieved during dump time.
2825 // Verification of archived old classes will be performed during run time.
2826 bool InstanceKlass::can_be_verified_at_dumptime() const {
2827 if (MetaspaceShared::is_in_shared_metaspace(this)) {
2828 // This is a class that was dumped into the base archive, so we know
2829 // it was verified at dump time.
2830 return true;
2831 }
2832 if (major_version() < 50 /*JAVA_6_VERSION*/) {
2833 return false;
2834 }
2835 if (java_super() != nullptr && !java_super()->can_be_verified_at_dumptime()) {
2836 return false;
2837 }
2838 Array<InstanceKlass*>* interfaces = local_interfaces();
2839 int len = interfaces->length();
2840 for (int i = 0; i < len; i++) {
2841 if (!interfaces->at(i)->can_be_verified_at_dumptime()) {
2842 return false;
2843 }
2844 }
2845 return true;
2846 }
2847
2848 int InstanceKlass::shared_class_loader_type() const {
2849 if (is_shared_boot_class()) {
2850 return ClassLoader::BOOT_LOADER;
2851 } else if (is_shared_platform_class()) {
2852 return ClassLoader::PLATFORM_LOADER;
2853 } else if (is_shared_app_class()) {
2854 return ClassLoader::APP_LOADER;
2855 } else {
2856 return ClassLoader::OTHER;
2857 }
2858 }
2859 #endif // INCLUDE_CDS
2860
2861 #if INCLUDE_JVMTI
2862 static void clear_all_breakpoints(Method* m) {
2863 m->clear_all_breakpoints();
2864 }
2865 #endif
2866
2867 void InstanceKlass::unload_class(InstanceKlass* ik) {
2868
2869 if (ik->is_scratch_class()) {
2870 assert(ik->dependencies().is_empty(), "dependencies should be empty for scratch classes");
2871 return;
2872 }
2873 assert(ik->is_loaded(), "class should be loaded " PTR_FORMAT, p2i(ik));
2874
2875 // Release dependencies.
2876 ik->dependencies().remove_all_dependents();
2877
2878 // notify the debugger
3617 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3618 for (int i = 0; i < len; i++) {
3619 intptr_t e = start[i];
3620 st->print("%d : " INTPTR_FORMAT, i, e);
3621 if (MetaspaceObj::is_valid((Metadata*)e)) {
3622 st->print(" ");
3623 ((Metadata*)e)->print_value_on(st);
3624 }
3625 st->cr();
3626 }
3627 }
3628
3629 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3630 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3631 }
3632
3633 const char* InstanceKlass::init_state_name() const {
3634 return state_names[init_state()];
3635 }
3636
3637 void InstanceKlass::print_on(outputStream* st) const {
3638 assert(is_klass(), "must be klass");
3639 Klass::print_on(st);
3640
3641 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3642 st->print(BULLET"klass size: %d", size()); st->cr();
3643 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3644 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3645 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3646 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3647 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3648 st->print(BULLET"sub: ");
3649 Klass* sub = subklass();
3650 int n;
3651 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3652 if (n < MaxSubklassPrintSize) {
3653 sub->print_value_on(st);
3654 st->print(" ");
3655 }
3656 }
3946 nullptr;
3947 // caller can be null, for example, during a JVMTI VM_Init hook
3948 if (caller != nullptr) {
3949 info_stream.print(" source: instance of %s", caller->external_name());
3950 } else {
3951 // source is unknown
3952 }
3953 } else {
3954 oop class_loader = loader_data->class_loader();
3955 info_stream.print(" source: %s", class_loader->klass()->external_name());
3956 }
3957 } else {
3958 assert(this->is_shared(), "must be");
3959 if (MetaspaceShared::is_shared_dynamic((void*)this)) {
3960 info_stream.print(" source: shared objects file (top)");
3961 } else {
3962 info_stream.print(" source: shared objects file");
3963 }
3964 }
3965
3966 msg.info("%s", info_stream.as_string());
3967
3968 if (log_is_enabled(Debug, class, load)) {
3969 stringStream debug_stream;
3970
3971 // Class hierarchy info
3972 debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
3973 p2i(this), p2i(superklass()));
3974
3975 // Interfaces
3976 if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
3977 debug_stream.print(" interfaces:");
3978 int length = local_interfaces()->length();
3979 for (int i = 0; i < length; i++) {
3980 debug_stream.print(" " PTR_FORMAT,
3981 p2i(InstanceKlass::cast(local_interfaces()->at(i))));
3982 }
3983 }
3984
3985 // Class loader
|
28 #include "cds/cdsEnumKlass.hpp"
29 #include "cds/classListWriter.hpp"
30 #include "cds/heapShared.hpp"
31 #include "cds/metaspaceShared.hpp"
32 #include "classfile/classFileParser.hpp"
33 #include "classfile/classFileStream.hpp"
34 #include "classfile/classLoader.hpp"
35 #include "classfile/classLoaderData.inline.hpp"
36 #include "classfile/javaClasses.hpp"
37 #include "classfile/moduleEntry.hpp"
38 #include "classfile/systemDictionary.hpp"
39 #include "classfile/systemDictionaryShared.hpp"
40 #include "classfile/verifier.hpp"
41 #include "classfile/vmClasses.hpp"
42 #include "classfile/vmSymbols.hpp"
43 #include "code/codeCache.hpp"
44 #include "code/dependencyContext.hpp"
45 #include "compiler/compilationPolicy.hpp"
46 #include "compiler/compileBroker.hpp"
47 #include "gc/shared/collectedHeap.inline.hpp"
48 #include "interpreter/bytecodeHistogram.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 "logging/log.hpp"
55 #include "klass.inline.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/fieldStreams.inline.hpp"
66 #include "oops/constantPool.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 "oops/trainingData.hpp"
78 #include "prims/jvmtiExport.hpp"
79 #include "prims/jvmtiRedefineClasses.hpp"
80 #include "prims/jvmtiThreadState.hpp"
81 #include "prims/methodComparator.hpp"
82 #include "runtime/arguments.hpp"
83 #include "runtime/deoptimization.hpp"
84 #include "runtime/atomic.hpp"
85 #include "runtime/fieldDescriptor.inline.hpp"
86 #include "runtime/handles.inline.hpp"
87 #include "runtime/javaCalls.hpp"
88 #include "runtime/javaThread.inline.hpp"
89 #include "runtime/mutexLocker.hpp"
90 #include "runtime/orderAccess.hpp"
91 #include "runtime/os.inline.hpp"
92 #include "runtime/reflection.hpp"
93 #include "runtime/runtimeUpcalls.hpp"
94 #include "runtime/synchronizer.hpp"
95 #include "runtime/threads.hpp"
96 #include "services/classLoadingService.hpp"
97 #include "services/finalizerService.hpp"
98 #include "services/threadService.hpp"
99 #include "utilities/dtrace.hpp"
100 #include "utilities/events.hpp"
101 #include "utilities/macros.hpp"
102 #include "utilities/nativeStackPrinter.hpp"
103 #include "utilities/pair.hpp"
104 #include "utilities/stringUtils.hpp"
105 #ifdef COMPILER1
106 #include "c1/c1_Compiler.hpp"
107 #endif
108 #if INCLUDE_JFR
109 #include "jfr/jfrEvents.hpp"
110 #endif
111
112 #ifdef DTRACE_ENABLED
113
865 }
866
867 if (log_is_enabled(Info, cds, init)) {
868 ResourceMark rm;
869 log_info(cds, init)("%s (aot-inited)", external_name());
870 }
871
872 link_class(CHECK);
873
874 #ifdef ASSERT
875 {
876 Handle h_init_lock(THREAD, init_lock());
877 ObjectLocker ol(h_init_lock, THREAD);
878 assert(!is_initialized(), "sanity");
879 assert(!is_being_initialized(), "sanity");
880 assert(!is_in_error_state(), "sanity");
881 }
882 #endif
883
884 set_init_thread(THREAD);
885 set_initialization_state_and_notify(fully_initialized, CHECK);
886 AOTClassInitializer::call_runtime_setup(THREAD, this);
887 }
888 #endif
889
890 bool InstanceKlass::verify_code(TRAPS) {
891 // 1) Verify the bytecodes
892 return Verifier::verify(this, should_verify_class(), THREAD);
893 }
894
895 void InstanceKlass::link_class(TRAPS) {
896 assert(is_loaded(), "must be loaded");
897 if (!is_linked()) {
898 link_class_impl(CHECK);
899 }
900 }
901
902 // Called to verify that a class can link during initialization, without
903 // throwing a VerifyError.
904 bool InstanceKlass::link_class_or_fail(TRAPS) {
905 assert(is_loaded(), "must be loaded");
906 if (!is_linked()) {
1062 return true;
1063 }
1064
1065 // Rewrite the byte codes of all of the methods of a class.
1066 // The rewriter must be called exactly once. Rewriting must happen after
1067 // verification but before the first method of the class is executed.
1068 void InstanceKlass::rewrite_class(TRAPS) {
1069 assert(is_loaded(), "must be loaded");
1070 if (is_rewritten()) {
1071 assert(is_shared(), "rewriting an unshared class?");
1072 return;
1073 }
1074 Rewriter::rewrite(this, CHECK);
1075 set_rewritten();
1076 }
1077
1078 // Now relocate and link method entry points after class is rewritten.
1079 // This is outside is_rewritten flag. In case of an exception, it can be
1080 // executed more than once.
1081 void InstanceKlass::link_methods(TRAPS) {
1082 PerfTraceElapsedTime timer(ClassLoader::perf_ik_link_methods_time());
1083
1084 int len = methods()->length();
1085 for (int i = len-1; i >= 0; i--) {
1086 methodHandle m(THREAD, methods()->at(i));
1087 RuntimeUpcalls::install_upcalls(m);
1088
1089 // Set up method entry points for compiler and interpreter .
1090 m->link_method(m, CHECK);
1091 }
1092 }
1093
1094 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1095 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1096 assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1097 for (int i = 0; i < local_interfaces()->length(); ++i) {
1098 InstanceKlass* ik = local_interfaces()->at(i);
1099
1100 // Initialization is depth first search ie. we start with top of the inheritance tree
1101 // has_nonstatic_concrete_methods drives searching superinterfaces since it
1102 // means has_nonstatic_concrete_methods in its superinterface hierarchy
1103 if (ik->has_nonstatic_concrete_methods()) {
1104 ik->initialize_super_interfaces(CHECK);
1105 }
1106
1107 // Only initialize() interfaces that "declare" concrete methods.
1172 assert_locked_or_safepoint(ClassInitError_lock);
1173 InitErrorTableCleaner cleaner;
1174 if (_initialization_error_table != nullptr) {
1175 _initialization_error_table->unlink(&cleaner);
1176 }
1177 }
1178
1179 void InstanceKlass::initialize_impl(TRAPS) {
1180 HandleMark hm(THREAD);
1181
1182 // Make sure klass is linked (verified) before initialization
1183 // A class could already be verified, since it has been reflected upon.
1184 link_class(CHECK);
1185
1186 DTRACE_CLASSINIT_PROBE(required, -1);
1187
1188 bool wait = false;
1189
1190 JavaThread* jt = THREAD;
1191
1192 if (ForceProfiling) {
1193 // Preallocate MDOs.
1194 for (int i = 0; i < methods()->length(); i++) {
1195 assert(!HAS_PENDING_EXCEPTION, "");
1196 methodHandle m(THREAD, methods()->at(i));
1197 Method::build_profiling_method_data(m, THREAD);
1198 if (HAS_PENDING_EXCEPTION) {
1199 ResourceMark rm;
1200 log_warning(cds)("MDO preallocation failed for %s", external_name());
1201 CLEAR_PENDING_EXCEPTION;
1202 break;
1203 }
1204 }
1205 }
1206
1207 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1208
1209 // refer to the JVM book page 47 for description of steps
1210 // Step 1
1211 {
1212 Handle h_init_lock(THREAD, init_lock());
1213 ObjectLocker ol(h_init_lock, jt);
1214
1215 // Step 2
1216 // If we were to use wait() instead of waitInterruptibly() then
1217 // we might end up throwing IE from link/symbol resolution sites
1218 // that aren't expected to throw. This would wreak havoc. See 6320309.
1219 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1220 if (debug_logging_enabled) {
1221 ResourceMark rm(jt);
1222 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1223 jt->name(), external_name(), init_thread_name());
1224 }
1225 wait = true;
1226 jt->set_class_to_be_initialized(this);
1325 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1326 ClassLoader::perf_class_init_selftime(),
1327 ClassLoader::perf_classes_inited(),
1328 jt->get_thread_stat()->perf_recursion_counts_addr(),
1329 jt->get_thread_stat()->perf_timers_addr(),
1330 PerfClassTraceTime::CLASS_CLINIT);
1331 call_class_initializer(THREAD);
1332 } else {
1333 // The elapsed time is so small it's not worth counting.
1334 if (UsePerfData) {
1335 ClassLoader::perf_classes_inited()->inc();
1336 }
1337 call_class_initializer(THREAD);
1338 }
1339 }
1340
1341 // Step 9
1342 if (!HAS_PENDING_EXCEPTION) {
1343 set_initialization_state_and_notify(fully_initialized, CHECK);
1344 debug_only(vtable().verify(tty, true);)
1345 CompilationPolicy::replay_training_at_init(this, THREAD);
1346 }
1347 else {
1348 // Step 10 and 11
1349 Handle e(THREAD, PENDING_EXCEPTION);
1350 CLEAR_PENDING_EXCEPTION;
1351 // JVMTI has already reported the pending exception
1352 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1353 JvmtiExport::clear_detected_exception(jt);
1354 {
1355 EXCEPTION_MARK;
1356 add_initialization_error(THREAD, e);
1357 set_initialization_state_and_notify(initialization_error, THREAD);
1358 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1359 // JVMTI has already reported the pending exception
1360 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1361 JvmtiExport::clear_detected_exception(jt);
1362 }
1363 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1364 if (e->is_a(vmClasses::Error_klass())) {
1365 THROW_OOP(e());
1366 } else {
1367 JavaCallArguments args(e);
1368 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1369 vmSymbols::throwable_void_signature(),
1370 &args);
1371 }
1372 }
1373 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1374 }
1375
1376 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1377 Handle h_init_lock(THREAD, init_lock());
1378 if (h_init_lock() != nullptr) {
1379 ObjectLocker ol(h_init_lock, THREAD);
1380 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1381 set_init_state(state);
1382 fence_and_clear_init_lock();
1383 ol.notify_all(CHECK);
1384 } else {
1385 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1386 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1387 set_init_state(state);
1388 }
1389 }
1390
1391 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1392 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1393 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1394 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1395
1585 tty->print("Registered ");
1586 i->print_value_on(tty);
1587 tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1588 }
1589 instanceHandle h_i(THREAD, i);
1590 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1591 JavaValue result(T_VOID);
1592 JavaCallArguments args(h_i);
1593 methodHandle mh(THREAD, Universe::finalizer_register_method());
1594 JavaCalls::call(&result, mh, &args, CHECK_NULL);
1595 MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1596 return h_i();
1597 }
1598
1599 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1600 assert(!is_abstract() && !is_interface(), "Should not create this object");
1601 size_t size = size_helper(); // Query before forming handle.
1602 return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1603 }
1604
1605 instanceOop InstanceKlass::allocate_instance(oop java_class,
1606 const char* who,
1607 TRAPS) {
1608 Klass* k = java_lang_Class::as_Klass(java_class);
1609 if (k == nullptr) {
1610 ResourceMark rm(THREAD);
1611 THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1612 }
1613 InstanceKlass* ik = cast(k);
1614 ik->check_valid_for_instantiation(false, CHECK_NULL);
1615 ik->initialize(CHECK_NULL);
1616 return ik->allocate_instance(THREAD);
1617 }
1618
1619 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1620 return instanceHandle(THREAD, allocate_instance(THREAD));
1621 }
1622
1623 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1624 if (is_interface() || is_abstract()) {
1625 ResourceMark rm(THREAD);
1626 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1627 : vmSymbols::java_lang_InstantiationException(), external_name());
1655 }
1656
1657 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1658 // Need load-acquire for lock-free read
1659 ObjArrayKlass* oak = array_klasses_acquire();
1660 if (oak == nullptr) {
1661 return nullptr;
1662 } else {
1663 return oak->array_klass_or_null(n);
1664 }
1665 }
1666
1667 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1668 return array_klass(1, THREAD);
1669 }
1670
1671 ArrayKlass* InstanceKlass::array_klass_or_null() {
1672 return array_klass_or_null(1);
1673 }
1674
1675 Method* InstanceKlass::class_initializer() const {
1676 Method* clinit = find_method(
1677 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1678 if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1679 return clinit;
1680 }
1681 return nullptr;
1682 }
1683
1684 void InstanceKlass::call_class_initializer(TRAPS) {
1685 if (ReplayCompiles &&
1686 (ReplaySuppressInitializers == 1 ||
1687 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1688 // Hide the existence of the initializer for the purpose of replaying the compile
1689 return;
1690 }
1691
1692 #if INCLUDE_CDS
1693 // This is needed to ensure the consistency of the archived heap objects.
1694 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1695 AOTClassInitializer::call_runtime_setup(THREAD, this);
1696 return;
1697 } else if (has_archived_enum_objs()) {
1698 assert(is_shared(), "must be");
1699 bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
1700 if (initialized) {
1701 return;
1702 }
1703 }
1704 #endif
1705
1706 methodHandle h_method(THREAD, class_initializer());
1707 assert(!is_initialized(), "we cannot initialize twice");
1708
1709 #if 0
1710 // FIXME -- revive this code added to leyden/premain for <clinit> profiling
1711 int init_id = log_class_init(THREAD, this);
1712 if (h_method() != nullptr) {
1713 JavaCallArguments args; // No arguments
1714 JavaValue result(T_VOID);
1715 InstanceKlass* outer = THREAD->set_class_being_initialized(this);
1716 jlong bc_start = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1717
1718 elapsedTimer timer;
1719 {
1720 PerfPauseTimer pt(THREAD->current_rt_call_timer(), THREAD->profile_rt_calls());
1721 PauseRuntimeCallProfiling prcp(THREAD, THREAD->profile_rt_calls());
1722
1723 timer.start();
1724 JavaCalls::call(&result, h_method, &args, THREAD); // Static call (no args)
1725 timer.stop();
1726 }
1727
1728 jlong bc_end = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1729
1730 jlong bc_executed = (bc_end - bc_start);
1731 if (UsePerfData && outer == nullptr) { // outermost clinit
1732 THREAD->inc_clinit_bc_counter_value(bc_executed);
1733 ClassLoader::perf_class_init_bytecodes_count()->inc(bc_executed);
1734 }
1735
1736 THREAD->set_class_being_initialized(outer);
1737
1738 LogStreamHandle(Debug, init) log;
1739 if (log.is_enabled()) {
1740 ResourceMark rm(THREAD);
1741 log.print("%d Initialized in %.3fms (total: " JLONG_FORMAT "ms); ",
1742 init_id, timer.seconds() * 1000.0, ClassLoader::class_init_time_ms());
1743 if (CountBytecodes || CountBytecodesPerThread) {
1744 log.print("executed " JLONG_FORMAT " bytecodes; ", bc_executed);
1745 }
1746 name()->print_value_on(&log);
1747 log.print_cr(" by thread " PTR_FORMAT " \"%s\" (" PTR_FORMAT ")",
1748 p2i(THREAD), THREAD->name(), p2i(this));
1749 }
1750 }
1751 LogTarget(Info, class, init) lt;
1752 if (lt.is_enabled()) {
1753 ResourceMark rm(THREAD);
1754 LogStream ls(lt);
1755 ls.print("%d Initialized ", init_id);
1756 name()->print_value_on(&ls);
1757 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1758 h_method() == nullptr ? "(no method)" : "", p2i(this),
1759 THREAD->name());
1760 }
1761 #else
1762 static int call_class_initializer_counter = 0; // for debugging
1763 LogTarget(Info, class, init) lt;
1764 if (lt.is_enabled()) {
1765 ResourceMark rm(THREAD);
1766 LogStream ls(lt);
1767 ls.print("%d Initializing ", call_class_initializer_counter++);
1768 name()->print_value_on(&ls);
1769 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1770 h_method() == nullptr ? "(no method)" : "", p2i(this),
1771 THREAD->name());
1772 }
1773 if (h_method() != nullptr) {
1774 ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1775 JavaCallArguments args; // No arguments
1776 JavaValue result(T_VOID);
1777 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1778 }
1779 #endif
1780 }
1781
1782 // If a class that implements this interface is initialized, is the JVM required
1783 // to first execute a <clinit> method declared in this interface,
1784 // or (if also_check_supers==true) any of the super types of this interface?
1785 //
1786 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1787 // an interface, then let SC be its superclass and let SI1, ..., SIn
1788 // be all superinterfaces of C (whether direct or indirect) that
1789 // declare at least one non-abstract, non-static method.
1790 //
1791 // So when an interface is initialized, it does not look at its
1792 // supers. But a proper class will ensure that all of its supers have
1793 // run their <clinit> methods, except that it disregards interfaces
1794 // that lack a non-static concrete method (i.e., a default method).
1795 // Therefore, you should probably call this method only when the
1796 // current class is a super of some proper class, not an interface.
1797 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1798 assert(is_interface(), "must be");
1799
2564 }
2565 }
2566 if (new_jmeths != 0) {
2567 Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
2568 }
2569 }
2570
2571 // Lookup a jmethodID, null if not found. Do no blocking, no allocations, no handles
2572 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2573 int idnum = method->method_idnum();
2574 jmethodID* jmeths = methods_jmethod_ids_acquire();
2575 return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2576 }
2577
2578 inline DependencyContext InstanceKlass::dependencies() {
2579 DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2580 return dep_context;
2581 }
2582
2583 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2584 dependencies().mark_dependent_nmethods(deopt_scope, changes, this);
2585 }
2586
2587 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2588 dependencies().add_dependent_nmethod(nm);
2589 }
2590
2591 void InstanceKlass::clean_dependency_context() {
2592 dependencies().clean_unloading_dependents();
2593 }
2594
2595 #ifndef PRODUCT
2596 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2597 dependencies().print_dependent_nmethods(verbose);
2598 }
2599
2600 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2601 return dependencies().is_dependent_nmethod(nm);
2602 }
2603 #endif //PRODUCT
2604
2701 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2702 if (ioe->interface_klass() != nullptr) {
2703 it->push(ioe->interface_klass_addr());
2704 itableMethodEntry* ime = ioe->first_method_entry(this);
2705 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2706 for (int index = 0; index < n; index ++) {
2707 it->push(ime[index].method_addr());
2708 }
2709 }
2710 }
2711 }
2712
2713 it->push(&_nest_host);
2714 it->push(&_nest_members);
2715 it->push(&_permitted_subclasses);
2716 it->push(&_record_components);
2717 }
2718
2719 #if INCLUDE_CDS
2720 void InstanceKlass::remove_unshareable_info() {
2721 if (is_linked()) {
2722 assert(can_be_verified_at_dumptime(), "must be");
2723 // Remember this so we can avoid walking the hierarchy at runtime.
2724 set_verified_at_dump_time();
2725 }
2726 _misc_flags.set_has_init_deps_processed(false);
2727
2728 Klass::remove_unshareable_info();
2729
2730 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2731 // Classes are attempted to link during dumping and may fail,
2732 // but these classes are still in the dictionary and class list in CLD.
2733 // If the class has failed verification, there is nothing else to remove.
2734 return;
2735 }
2736
2737 // Reset to the 'allocated' state to prevent any premature accessing to
2738 // a shared class at runtime while the class is still being loaded and
2739 // restored. A class' init_state is set to 'loaded' at runtime when it's
2740 // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2741 _init_state = allocated;
2742
2743 { // Otherwise this needs to take out the Compile_lock.
2744 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2745 init_implementor();
2746 }
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
2896 bool InstanceKlass::can_be_verified_at_dumptime() const {
2897 if (CDSConfig::preserve_all_dumptime_verification_states(this)) {
2898 return true;
2899 }
2900
2901 if (MetaspaceShared::is_in_shared_metaspace(this)) {
2902 // This is a class that was dumped into the base archive, so we know
2903 // it was verified at dump time.
2904 return true;
2905 }
2906
2907 // Check if a class or any of its supertypes has a version older than 50.
2908 // CDS will not perform verification of old classes during dump time because
2909 // without changing the old verifier, the verification constraint cannot be
2910 // retrieved during dump time.
2911 // Verification of archived old classes will be performed during run time.
2912 if (major_version() < 50 /*JAVA_6_VERSION*/) {
2913 return false;
2914 }
2915 if (java_super() != nullptr && !java_super()->can_be_verified_at_dumptime()) {
2916 return false;
2917 }
2918 Array<InstanceKlass*>* interfaces = local_interfaces();
2919 int len = interfaces->length();
2920 for (int i = 0; i < len; i++) {
2921 if (!interfaces->at(i)->can_be_verified_at_dumptime()) {
2922 return false;
2923 }
2924 }
2925 return true;
2926 }
2927
2928 int InstanceKlass::shared_class_loader_type() const {
2929 if (is_shared_boot_class()) {
2930 return ClassLoader::BOOT_LOADER;
2931 } else if (is_shared_platform_class()) {
2932 return ClassLoader::PLATFORM_LOADER;
2933 } else if (is_shared_app_class()) {
2934 return ClassLoader::APP_LOADER;
2935 } else {
2936 return ClassLoader::OTHER;
2937 }
2938 }
2939
2940 #endif // INCLUDE_CDS
2941
2942 #if INCLUDE_JVMTI
2943 static void clear_all_breakpoints(Method* m) {
2944 m->clear_all_breakpoints();
2945 }
2946 #endif
2947
2948 void InstanceKlass::unload_class(InstanceKlass* ik) {
2949
2950 if (ik->is_scratch_class()) {
2951 assert(ik->dependencies().is_empty(), "dependencies should be empty for scratch classes");
2952 return;
2953 }
2954 assert(ik->is_loaded(), "class should be loaded " PTR_FORMAT, p2i(ik));
2955
2956 // Release dependencies.
2957 ik->dependencies().remove_all_dependents();
2958
2959 // notify the debugger
3698 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3699 for (int i = 0; i < len; i++) {
3700 intptr_t e = start[i];
3701 st->print("%d : " INTPTR_FORMAT, i, e);
3702 if (MetaspaceObj::is_valid((Metadata*)e)) {
3703 st->print(" ");
3704 ((Metadata*)e)->print_value_on(st);
3705 }
3706 st->cr();
3707 }
3708 }
3709
3710 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3711 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3712 }
3713
3714 const char* InstanceKlass::init_state_name() const {
3715 return state_names[init_state()];
3716 }
3717
3718 const char* InstanceKlass::state2name(ClassState s) {
3719 return state_names[s];
3720 }
3721
3722 void InstanceKlass::print_on(outputStream* st) const {
3723 assert(is_klass(), "must be klass");
3724 Klass::print_on(st);
3725
3726 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3727 st->print(BULLET"klass size: %d", size()); st->cr();
3728 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3729 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3730 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3731 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3732 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3733 st->print(BULLET"sub: ");
3734 Klass* sub = subklass();
3735 int n;
3736 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3737 if (n < MaxSubklassPrintSize) {
3738 sub->print_value_on(st);
3739 st->print(" ");
3740 }
3741 }
4031 nullptr;
4032 // caller can be null, for example, during a JVMTI VM_Init hook
4033 if (caller != nullptr) {
4034 info_stream.print(" source: instance of %s", caller->external_name());
4035 } else {
4036 // source is unknown
4037 }
4038 } else {
4039 oop class_loader = loader_data->class_loader();
4040 info_stream.print(" source: %s", class_loader->klass()->external_name());
4041 }
4042 } else {
4043 assert(this->is_shared(), "must be");
4044 if (MetaspaceShared::is_shared_dynamic((void*)this)) {
4045 info_stream.print(" source: shared objects file (top)");
4046 } else {
4047 info_stream.print(" source: shared objects file");
4048 }
4049 }
4050
4051 info_stream.print(" loader:");
4052 if (is_shared()) {
4053 info_stream.print(" %s", SystemDictionaryShared::class_loader_name_for_shared((Klass*)this));
4054 } else if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
4055 info_stream.print(" boot_loader");
4056 } else {
4057 oop class_loader = loader_data->class_loader();
4058 if (class_loader != nullptr) {
4059 info_stream.print(" %s", class_loader->klass()->external_name());
4060 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader);
4061 if (cl_name_and_id != nullptr) {
4062 info_stream.print(" %s", java_lang_String::as_utf8_string(cl_name_and_id));
4063 }
4064 } else {
4065 info_stream.print(" null");
4066 }
4067 }
4068 msg.info("%s", info_stream.as_string());
4069
4070 if (log_is_enabled(Debug, class, load)) {
4071 stringStream debug_stream;
4072
4073 // Class hierarchy info
4074 debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
4075 p2i(this), p2i(superklass()));
4076
4077 // Interfaces
4078 if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
4079 debug_stream.print(" interfaces:");
4080 int length = local_interfaces()->length();
4081 for (int i = 0; i < length; i++) {
4082 debug_stream.print(" " PTR_FORMAT,
4083 p2i(InstanceKlass::cast(local_interfaces()->at(i))));
4084 }
4085 }
4086
4087 // Class loader
|