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"
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"
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/deoptimization.hpp"
83 #include "runtime/atomic.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/runtimeUpcalls.hpp"
93 #include "runtime/synchronizer.hpp"
94 #include "runtime/threads.hpp"
95 #include "services/classLoadingService.hpp"
96 #include "services/finalizerService.hpp"
97 #include "services/threadService.hpp"
98 #include "utilities/dtrace.hpp"
99 #include "utilities/events.hpp"
100 #include "utilities/macros.hpp"
101 #include "utilities/nativeStackPrinter.hpp"
102 #include "utilities/pair.hpp"
103 #include "utilities/stringUtils.hpp"
104 #ifdef COMPILER1
105 #include "c1/c1_Compiler.hpp"
106 #endif
107 #if INCLUDE_JFR
108 #include "jfr/jfrEvents.hpp"
109 #endif
110
111 #ifdef DTRACE_ENABLED
112
864 }
865
866 if (log_is_enabled(Info, cds, init)) {
867 ResourceMark rm;
868 log_info(cds, init)("%s (aot-inited)", external_name());
869 }
870
871 link_class(CHECK);
872
873 #ifdef ASSERT
874 {
875 Handle h_init_lock(THREAD, init_lock());
876 ObjectLocker ol(h_init_lock, THREAD);
877 assert(!is_initialized(), "sanity");
878 assert(!is_being_initialized(), "sanity");
879 assert(!is_in_error_state(), "sanity");
880 }
881 #endif
882
883 set_init_thread(THREAD);
884 set_initialization_state_and_notify(fully_initialized, CHECK);
885 AOTClassInitializer::call_runtime_setup(THREAD, this);
886 }
887 #endif
888
889 bool InstanceKlass::verify_code(TRAPS) {
890 // 1) Verify the bytecodes
891 return Verifier::verify(this, should_verify_class(), THREAD);
892 }
893
894 void InstanceKlass::link_class(TRAPS) {
895 assert(is_loaded(), "must be loaded");
896 if (!is_linked()) {
897 link_class_impl(CHECK);
898 }
899 }
900
901 // Called to verify that a class can link during initialization, without
902 // throwing a VerifyError.
903 bool InstanceKlass::link_class_or_fail(TRAPS) {
904 assert(is_loaded(), "must be loaded");
905 if (!is_linked()) {
1061 return true;
1062 }
1063
1064 // Rewrite the byte codes of all of the methods of a class.
1065 // The rewriter must be called exactly once. Rewriting must happen after
1066 // verification but before the first method of the class is executed.
1067 void InstanceKlass::rewrite_class(TRAPS) {
1068 assert(is_loaded(), "must be loaded");
1069 if (is_rewritten()) {
1070 assert(is_shared(), "rewriting an unshared class?");
1071 return;
1072 }
1073 Rewriter::rewrite(this, CHECK);
1074 set_rewritten();
1075 }
1076
1077 // Now relocate and link method entry points after class is rewritten.
1078 // This is outside is_rewritten flag. In case of an exception, it can be
1079 // executed more than once.
1080 void InstanceKlass::link_methods(TRAPS) {
1081 PerfTraceElapsedTime timer(ClassLoader::perf_ik_link_methods_time());
1082
1083 int len = methods()->length();
1084 for (int i = len-1; i >= 0; i--) {
1085 methodHandle m(THREAD, methods()->at(i));
1086 RuntimeUpcalls::install_upcalls(m);
1087
1088 // Set up method entry points for compiler and interpreter .
1089 m->link_method(m, CHECK);
1090 }
1091 }
1092
1093 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1094 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1095 assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1096 for (int i = 0; i < local_interfaces()->length(); ++i) {
1097 InstanceKlass* ik = local_interfaces()->at(i);
1098
1099 // Initialization is depth first search ie. we start with top of the inheritance tree
1100 // has_nonstatic_concrete_methods drives searching superinterfaces since it
1101 // means has_nonstatic_concrete_methods in its superinterface hierarchy
1102 if (ik->has_nonstatic_concrete_methods()) {
1103 ik->initialize_super_interfaces(CHECK);
1104 }
1105
1106 // Only initialize() interfaces that "declare" concrete methods.
1171 assert_locked_or_safepoint(ClassInitError_lock);
1172 InitErrorTableCleaner cleaner;
1173 if (_initialization_error_table != nullptr) {
1174 _initialization_error_table->unlink(&cleaner);
1175 }
1176 }
1177
1178 void InstanceKlass::initialize_impl(TRAPS) {
1179 HandleMark hm(THREAD);
1180
1181 // Make sure klass is linked (verified) before initialization
1182 // A class could already be verified, since it has been reflected upon.
1183 link_class(CHECK);
1184
1185 DTRACE_CLASSINIT_PROBE(required, -1);
1186
1187 bool wait = false;
1188
1189 JavaThread* jt = THREAD;
1190
1191 if (ForceProfiling) {
1192 // Preallocate MDOs.
1193 for (int i = 0; i < methods()->length(); i++) {
1194 assert(!HAS_PENDING_EXCEPTION, "");
1195 methodHandle m(THREAD, methods()->at(i));
1196 Method::build_profiling_method_data(m, THREAD);
1197 if (HAS_PENDING_EXCEPTION) {
1198 ResourceMark rm;
1199 log_warning(cds)("MDO preallocation failed for %s", external_name());
1200 CLEAR_PENDING_EXCEPTION;
1201 break;
1202 }
1203 }
1204 }
1205
1206 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1207
1208 // refer to the JVM book page 47 for description of steps
1209 // Step 1
1210 {
1211 Handle h_init_lock(THREAD, init_lock());
1212 ObjectLocker ol(h_init_lock, jt);
1213
1214 // Step 2
1215 // If we were to use wait() instead of waitInterruptibly() then
1216 // we might end up throwing IE from link/symbol resolution sites
1217 // that aren't expected to throw. This would wreak havoc. See 6320309.
1218 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1219 if (debug_logging_enabled) {
1220 ResourceMark rm(jt);
1221 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1222 jt->name(), external_name(), init_thread_name());
1223 }
1224 wait = true;
1225 jt->set_class_to_be_initialized(this);
1324 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1325 ClassLoader::perf_class_init_selftime(),
1326 ClassLoader::perf_classes_inited(),
1327 jt->get_thread_stat()->perf_recursion_counts_addr(),
1328 jt->get_thread_stat()->perf_timers_addr(),
1329 PerfClassTraceTime::CLASS_CLINIT);
1330 call_class_initializer(THREAD);
1331 } else {
1332 // The elapsed time is so small it's not worth counting.
1333 if (UsePerfData) {
1334 ClassLoader::perf_classes_inited()->inc();
1335 }
1336 call_class_initializer(THREAD);
1337 }
1338 }
1339
1340 // Step 9
1341 if (!HAS_PENDING_EXCEPTION) {
1342 set_initialization_state_and_notify(fully_initialized, CHECK);
1343 debug_only(vtable().verify(tty, true);)
1344 CompilationPolicy::replay_training_at_init(this, THREAD);
1345 }
1346 else {
1347 // Step 10 and 11
1348 Handle e(THREAD, PENDING_EXCEPTION);
1349 CLEAR_PENDING_EXCEPTION;
1350 // JVMTI has already reported the pending exception
1351 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1352 JvmtiExport::clear_detected_exception(jt);
1353 {
1354 EXCEPTION_MARK;
1355 add_initialization_error(THREAD, e);
1356 set_initialization_state_and_notify(initialization_error, THREAD);
1357 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1358 // JVMTI has already reported the pending exception
1359 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1360 JvmtiExport::clear_detected_exception(jt);
1361 }
1362 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1363 if (e->is_a(vmClasses::Error_klass())) {
1364 THROW_OOP(e());
1365 } else {
1366 JavaCallArguments args(e);
1367 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1368 vmSymbols::throwable_void_signature(),
1369 &args);
1370 }
1371 }
1372 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1373 }
1374
1375 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1376 Handle h_init_lock(THREAD, init_lock());
1377 if (h_init_lock() != nullptr) {
1378 ObjectLocker ol(h_init_lock, THREAD);
1379 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1380 set_init_state(state);
1381 fence_and_clear_init_lock();
1382 ol.notify_all(CHECK);
1383 } else {
1384 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1385 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1386 set_init_state(state);
1387 }
1388 }
1389
1390 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1391 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1392 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1393 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1394
1584 tty->print("Registered ");
1585 i->print_value_on(tty);
1586 tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1587 }
1588 instanceHandle h_i(THREAD, i);
1589 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1590 JavaValue result(T_VOID);
1591 JavaCallArguments args(h_i);
1592 methodHandle mh(THREAD, Universe::finalizer_register_method());
1593 JavaCalls::call(&result, mh, &args, CHECK_NULL);
1594 MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1595 return h_i();
1596 }
1597
1598 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1599 assert(!is_abstract() && !is_interface(), "Should not create this object");
1600 size_t size = size_helper(); // Query before forming handle.
1601 return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1602 }
1603
1604 instanceOop InstanceKlass::allocate_instance(oop java_class,
1605 const char* who,
1606 TRAPS) {
1607 Klass* k = java_lang_Class::as_Klass(java_class);
1608 if (k == nullptr) {
1609 ResourceMark rm(THREAD);
1610 THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1611 }
1612 InstanceKlass* ik = cast(k);
1613 ik->check_valid_for_instantiation(false, CHECK_NULL);
1614 ik->initialize(CHECK_NULL);
1615 return ik->allocate_instance(THREAD);
1616 }
1617
1618 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1619 return instanceHandle(THREAD, allocate_instance(THREAD));
1620 }
1621
1622 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1623 if (is_interface() || is_abstract()) {
1624 ResourceMark rm(THREAD);
1625 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1626 : vmSymbols::java_lang_InstantiationException(), external_name());
1654 }
1655
1656 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1657 // Need load-acquire for lock-free read
1658 ObjArrayKlass* oak = array_klasses_acquire();
1659 if (oak == nullptr) {
1660 return nullptr;
1661 } else {
1662 return oak->array_klass_or_null(n);
1663 }
1664 }
1665
1666 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1667 return array_klass(1, THREAD);
1668 }
1669
1670 ArrayKlass* InstanceKlass::array_klass_or_null() {
1671 return array_klass_or_null(1);
1672 }
1673
1674 Method* InstanceKlass::class_initializer() const {
1675 Method* clinit = find_method(
1676 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1677 if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1678 return clinit;
1679 }
1680 return nullptr;
1681 }
1682
1683 void InstanceKlass::call_class_initializer(TRAPS) {
1684 if (ReplayCompiles &&
1685 (ReplaySuppressInitializers == 1 ||
1686 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1687 // Hide the existence of the initializer for the purpose of replaying the compile
1688 return;
1689 }
1690
1691 #if INCLUDE_CDS
1692 // This is needed to ensure the consistency of the archived heap objects.
1693 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1694 AOTClassInitializer::call_runtime_setup(THREAD, this);
1695 return;
1696 } else if (has_archived_enum_objs()) {
1697 assert(is_shared(), "must be");
1698 bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
1699 if (initialized) {
1700 return;
1701 }
1702 }
1703 #endif
1704
1705 methodHandle h_method(THREAD, class_initializer());
1706 assert(!is_initialized(), "we cannot initialize twice");
1707
1708 #if 0
1709 // FIXME -- revive this code added to leyden/premain for <clinit> profiling
1710 int init_id = log_class_init(THREAD, this);
1711 if (h_method() != nullptr) {
1712 JavaCallArguments args; // No arguments
1713 JavaValue result(T_VOID);
1714 InstanceKlass* outer = THREAD->set_class_being_initialized(this);
1715 jlong bc_start = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1716
1717 elapsedTimer timer;
1718 {
1719 PerfPauseTimer pt(THREAD->current_rt_call_timer(), THREAD->profile_rt_calls());
1720 PauseRuntimeCallProfiling prcp(THREAD, THREAD->profile_rt_calls());
1721
1722 timer.start();
1723 JavaCalls::call(&result, h_method, &args, THREAD); // Static call (no args)
1724 timer.stop();
1725 }
1726
1727 jlong bc_end = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1728
1729 jlong bc_executed = (bc_end - bc_start);
1730 if (UsePerfData && outer == nullptr) { // outermost clinit
1731 THREAD->inc_clinit_bc_counter_value(bc_executed);
1732 ClassLoader::perf_class_init_bytecodes_count()->inc(bc_executed);
1733 }
1734
1735 THREAD->set_class_being_initialized(outer);
1736
1737 LogStreamHandle(Debug, init) log;
1738 if (log.is_enabled()) {
1739 ResourceMark rm(THREAD);
1740 log.print("%d Initialized in %.3fms (total: " JLONG_FORMAT "ms); ",
1741 init_id, timer.seconds() * 1000.0, ClassLoader::class_init_time_ms());
1742 if (CountBytecodes || CountBytecodesPerThread) {
1743 log.print("executed " JLONG_FORMAT " bytecodes; ", bc_executed);
1744 }
1745 name()->print_value_on(&log);
1746 log.print_cr(" by thread " PTR_FORMAT " \"%s\" (" PTR_FORMAT ")",
1747 p2i(THREAD), THREAD->name(), p2i(this));
1748 }
1749 }
1750 LogTarget(Info, class, init) lt;
1751 if (lt.is_enabled()) {
1752 ResourceMark rm(THREAD);
1753 LogStream ls(lt);
1754 ls.print("%d Initialized ", init_id);
1755 name()->print_value_on(&ls);
1756 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1757 h_method() == nullptr ? "(no method)" : "", p2i(this),
1758 THREAD->name());
1759 }
1760 #else
1761 static int call_class_initializer_counter = 0; // for debugging
1762 LogTarget(Info, class, init) lt;
1763 if (lt.is_enabled()) {
1764 ResourceMark rm(THREAD);
1765 LogStream ls(lt);
1766 ls.print("%d Initializing ", call_class_initializer_counter++);
1767 name()->print_value_on(&ls);
1768 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1769 h_method() == nullptr ? "(no method)" : "", p2i(this),
1770 THREAD->name());
1771 }
1772 if (h_method() != nullptr) {
1773 ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1774 JavaCallArguments args; // No arguments
1775 JavaValue result(T_VOID);
1776 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1777 }
1778 #endif
1779 }
1780
1781 // If a class that implements this interface is initialized, is the JVM required
1782 // to first execute a <clinit> method declared in this interface,
1783 // or (if also_check_supers==true) any of the super types of this interface?
1784 //
1785 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1786 // an interface, then let SC be its superclass and let SI1, ..., SIn
1787 // be all superinterfaces of C (whether direct or indirect) that
1788 // declare at least one non-abstract, non-static method.
1789 //
1790 // So when an interface is initialized, it does not look at its
1791 // supers. But a proper class will ensure that all of its supers have
1792 // run their <clinit> methods, except that it disregards interfaces
1793 // that lack a non-static concrete method (i.e., a default method).
1794 // Therefore, you should probably call this method only when the
1795 // current class is a super of some proper class, not an interface.
1796 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1797 assert(is_interface(), "must be");
1798
2563 }
2564 }
2565 if (new_jmeths != 0) {
2566 Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
2567 }
2568 }
2569
2570 // Lookup a jmethodID, null if not found. Do no blocking, no allocations, no handles
2571 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2572 int idnum = method->method_idnum();
2573 jmethodID* jmeths = methods_jmethod_ids_acquire();
2574 return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2575 }
2576
2577 inline DependencyContext InstanceKlass::dependencies() {
2578 DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2579 return dep_context;
2580 }
2581
2582 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2583 dependencies().mark_dependent_nmethods(deopt_scope, changes, this);
2584 }
2585
2586 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2587 dependencies().add_dependent_nmethod(nm);
2588 }
2589
2590 void InstanceKlass::clean_dependency_context() {
2591 dependencies().clean_unloading_dependents();
2592 }
2593
2594 #ifndef PRODUCT
2595 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2596 dependencies().print_dependent_nmethods(verbose);
2597 }
2598
2599 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2600 return dependencies().is_dependent_nmethod(nm);
2601 }
2602 #endif //PRODUCT
2603
2700 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2701 if (ioe->interface_klass() != nullptr) {
2702 it->push(ioe->interface_klass_addr());
2703 itableMethodEntry* ime = ioe->first_method_entry(this);
2704 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2705 for (int index = 0; index < n; index ++) {
2706 it->push(ime[index].method_addr());
2707 }
2708 }
2709 }
2710 }
2711
2712 it->push(&_nest_host);
2713 it->push(&_nest_members);
2714 it->push(&_permitted_subclasses);
2715 it->push(&_record_components);
2716 }
2717
2718 #if INCLUDE_CDS
2719 void InstanceKlass::remove_unshareable_info() {
2720 if (is_linked()) {
2721 assert(can_be_verified_at_dumptime(), "must be");
2722 // Remember this so we can avoid walking the hierarchy at runtime.
2723 set_verified_at_dump_time();
2724 }
2725 _misc_flags.set_has_init_deps_processed(false);
2726
2727 Klass::remove_unshareable_info();
2728
2729 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2730 // Classes are attempted to link during dumping and may fail,
2731 // but these classes are still in the dictionary and class list in CLD.
2732 // If the class has failed verification, there is nothing else to remove.
2733 return;
2734 }
2735
2736 // Reset to the 'allocated' state to prevent any premature accessing to
2737 // a shared class at runtime while the class is still being loaded and
2738 // restored. A class' init_state is set to 'loaded' at runtime when it's
2739 // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2740 _init_state = allocated;
2741
2742 { // Otherwise this needs to take out the Compile_lock.
2743 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2744 init_implementor();
2745 }
2875
2876 // restore constant pool resolved references
2877 constants()->restore_unshareable_info(CHECK);
2878
2879 if (array_klasses() != nullptr) {
2880 // To get a consistent list of classes we need MultiArray_lock to ensure
2881 // array classes aren't observed while they are being restored.
2882 RecursiveLocker rl(MultiArray_lock, THREAD);
2883 assert(this == array_klasses()->bottom_klass(), "sanity");
2884 // Array classes have null protection domain.
2885 // --> see ArrayKlass::complete_create_array_klass()
2886 array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2887 }
2888
2889 // Initialize @ValueBased class annotation if not already set in the archived klass.
2890 if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2891 set_is_value_based();
2892 }
2893 }
2894
2895 bool InstanceKlass::can_be_verified_at_dumptime() const {
2896 if (CDSConfig::preserve_all_dumptime_verification_states(this)) {
2897 return true;
2898 }
2899
2900 if (MetaspaceShared::is_in_shared_metaspace(this)) {
2901 // This is a class that was dumped into the base archive, so we know
2902 // it was verified at dump time.
2903 return true;
2904 }
2905
2906 // Check if a class or any of its supertypes has a version older than 50.
2907 // CDS will not perform verification of old classes during dump time because
2908 // without changing the old verifier, the verification constraint cannot be
2909 // retrieved during dump time.
2910 // Verification of archived old classes will be performed during run time.
2911 if (major_version() < 50 /*JAVA_6_VERSION*/) {
2912 return false;
2913 }
2914 if (java_super() != nullptr && !java_super()->can_be_verified_at_dumptime()) {
2915 return false;
2916 }
2917 Array<InstanceKlass*>* interfaces = local_interfaces();
2918 int len = interfaces->length();
2919 for (int i = 0; i < len; i++) {
2920 if (!interfaces->at(i)->can_be_verified_at_dumptime()) {
2921 return false;
2922 }
2923 }
2924 return true;
2925 }
2926
2927 int InstanceKlass::shared_class_loader_type() const {
2928 if (is_shared_boot_class()) {
2929 return ClassLoader::BOOT_LOADER;
2930 } else if (is_shared_platform_class()) {
2931 return ClassLoader::PLATFORM_LOADER;
2932 } else if (is_shared_app_class()) {
2933 return ClassLoader::APP_LOADER;
2934 } else {
2935 return ClassLoader::OTHER;
2936 }
2937 }
2938
2939 #endif // INCLUDE_CDS
2940
2941 #if INCLUDE_JVMTI
2942 static void clear_all_breakpoints(Method* m) {
2943 m->clear_all_breakpoints();
2944 }
2945 #endif
2946
2947 void InstanceKlass::unload_class(InstanceKlass* ik) {
2948
2949 if (ik->is_scratch_class()) {
2950 assert(ik->dependencies().is_empty(), "dependencies should be empty for scratch classes");
2951 return;
2952 }
2953 assert(ik->is_loaded(), "class should be loaded " PTR_FORMAT, p2i(ik));
2954
2955 // Release dependencies.
2956 ik->dependencies().remove_all_dependents();
2957
2958 // notify the debugger
3697 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3698 for (int i = 0; i < len; i++) {
3699 intptr_t e = start[i];
3700 st->print("%d : " INTPTR_FORMAT, i, e);
3701 if (MetaspaceObj::is_valid((Metadata*)e)) {
3702 st->print(" ");
3703 ((Metadata*)e)->print_value_on(st);
3704 }
3705 st->cr();
3706 }
3707 }
3708
3709 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3710 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3711 }
3712
3713 const char* InstanceKlass::init_state_name() const {
3714 return state_names[init_state()];
3715 }
3716
3717 const char* InstanceKlass::state2name(ClassState s) {
3718 return state_names[s];
3719 }
3720
3721 void InstanceKlass::print_on(outputStream* st) const {
3722 assert(is_klass(), "must be klass");
3723 Klass::print_on(st);
3724
3725 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3726 st->print(BULLET"klass size: %d", size()); st->cr();
3727 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3728 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3729 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3730 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3731 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3732 st->print(BULLET"sub: ");
3733 Klass* sub = subklass();
3734 int n;
3735 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3736 if (n < MaxSubklassPrintSize) {
3737 sub->print_value_on(st);
3738 st->print(" ");
3739 }
3740 }
4030 nullptr;
4031 // caller can be null, for example, during a JVMTI VM_Init hook
4032 if (caller != nullptr) {
4033 info_stream.print(" source: instance of %s", caller->external_name());
4034 } else {
4035 // source is unknown
4036 }
4037 } else {
4038 oop class_loader = loader_data->class_loader();
4039 info_stream.print(" source: %s", class_loader->klass()->external_name());
4040 }
4041 } else {
4042 assert(this->is_shared(), "must be");
4043 if (MetaspaceShared::is_shared_dynamic((void*)this)) {
4044 info_stream.print(" source: shared objects file (top)");
4045 } else {
4046 info_stream.print(" source: shared objects file");
4047 }
4048 }
4049
4050 info_stream.print(" loader:");
4051 if (is_shared()) {
4052 info_stream.print(" %s", SystemDictionaryShared::loader_type_for_shared_class((Klass*)this));
4053 } else if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
4054 info_stream.print(" boot_loader");
4055 } else {
4056 oop class_loader = loader_data->class_loader();
4057 if (class_loader != nullptr) {
4058 info_stream.print(" %s", class_loader->klass()->external_name());
4059 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader);
4060 if (cl_name_and_id != nullptr) {
4061 info_stream.print(" %s", java_lang_String::as_utf8_string(cl_name_and_id));
4062 }
4063 } else {
4064 info_stream.print(" null");
4065 }
4066 }
4067 msg.info("%s", info_stream.as_string());
4068
4069 if (log_is_enabled(Debug, class, load)) {
4070 stringStream debug_stream;
4071
4072 // Class hierarchy info
4073 debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
4074 p2i(this), p2i(superklass()));
4075
4076 // Interfaces
4077 if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
4078 debug_stream.print(" interfaces:");
4079 int length = local_interfaces()->length();
4080 for (int i = 0; i < length; i++) {
4081 debug_stream.print(" " PTR_FORMAT,
4082 p2i(InstanceKlass::cast(local_interfaces()->at(i))));
4083 }
4084 }
4085
4086 // Class loader
|