29 #include "cds/cdsEnumKlass.hpp"
30 #include "cds/classListWriter.hpp"
31 #include "cds/heapShared.hpp"
32 #include "cds/metaspaceShared.hpp"
33 #include "classfile/classFileParser.hpp"
34 #include "classfile/classFileStream.hpp"
35 #include "classfile/classLoader.hpp"
36 #include "classfile/classLoaderData.inline.hpp"
37 #include "classfile/javaClasses.hpp"
38 #include "classfile/moduleEntry.hpp"
39 #include "classfile/systemDictionary.hpp"
40 #include "classfile/systemDictionaryShared.hpp"
41 #include "classfile/verifier.hpp"
42 #include "classfile/vmClasses.hpp"
43 #include "classfile/vmSymbols.hpp"
44 #include "code/codeCache.hpp"
45 #include "code/dependencyContext.hpp"
46 #include "compiler/compilationPolicy.hpp"
47 #include "compiler/compileBroker.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "interpreter/bytecodeStream.hpp"
50 #include "interpreter/oopMapCache.hpp"
51 #include "interpreter/rewriter.hpp"
52 #include "jvm.h"
53 #include "jvmtifiles/jvmti.h"
54 #include "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 "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/synchronizer.hpp"
93 #include "runtime/threads.hpp"
94 #include "services/classLoadingService.hpp"
95 #include "services/finalizerService.hpp"
96 #include "services/threadService.hpp"
97 #include "utilities/dtrace.hpp"
98 #include "utilities/events.hpp"
99 #include "utilities/macros.hpp"
100 #include "utilities/stringUtils.hpp"
101 #include "utilities/pair.hpp"
102 #ifdef COMPILER1
103 #include "c1/c1_Compiler.hpp"
104 #endif
105 #if INCLUDE_JFR
106 #include "jfr/jfrEvents.hpp"
107 #endif
108
109 #ifdef DTRACE_ENABLED
110
111
1040 return true;
1041 }
1042
1043 // Rewrite the byte codes of all of the methods of a class.
1044 // The rewriter must be called exactly once. Rewriting must happen after
1045 // verification but before the first method of the class is executed.
1046 void InstanceKlass::rewrite_class(TRAPS) {
1047 assert(is_loaded(), "must be loaded");
1048 if (is_rewritten()) {
1049 assert(is_shared(), "rewriting an unshared class?");
1050 return;
1051 }
1052 Rewriter::rewrite(this, CHECK);
1053 set_rewritten();
1054 }
1055
1056 // Now relocate and link method entry points after class is rewritten.
1057 // This is outside is_rewritten flag. In case of an exception, it can be
1058 // executed more than once.
1059 void InstanceKlass::link_methods(TRAPS) {
1060 PerfTraceTime timer(ClassLoader::perf_ik_link_methods_time());
1061
1062 int len = methods()->length();
1063 for (int i = len-1; i >= 0; i--) {
1064 methodHandle m(THREAD, methods()->at(i));
1065
1066 // Set up method entry points for compiler and interpreter .
1067 m->link_method(m, CHECK);
1068 }
1069 }
1070
1071 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1072 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1073 assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1074 for (int i = 0; i < local_interfaces()->length(); ++i) {
1075 InstanceKlass* ik = local_interfaces()->at(i);
1076
1077 // Initialization is depth first search ie. we start with top of the inheritance tree
1078 // has_nonstatic_concrete_methods drives searching superinterfaces since it
1079 // means has_nonstatic_concrete_methods in its superinterface hierarchy
1080 if (ik->has_nonstatic_concrete_methods()) {
1081 ik->initialize_super_interfaces(CHECK);
1082 }
1083
1084 // Only initialize() interfaces that "declare" concrete methods.
1149 assert_locked_or_safepoint(ClassInitError_lock);
1150 InitErrorTableCleaner cleaner;
1151 if (_initialization_error_table != nullptr) {
1152 _initialization_error_table->unlink(&cleaner);
1153 }
1154 }
1155
1156 void InstanceKlass::initialize_impl(TRAPS) {
1157 HandleMark hm(THREAD);
1158
1159 // Make sure klass is linked (verified) before initialization
1160 // A class could already be verified, since it has been reflected upon.
1161 link_class(CHECK);
1162
1163 DTRACE_CLASSINIT_PROBE(required, -1);
1164
1165 bool wait = false;
1166
1167 JavaThread* jt = THREAD;
1168
1169 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1170
1171 // refer to the JVM book page 47 for description of steps
1172 // Step 1
1173 {
1174 Handle h_init_lock(THREAD, init_lock());
1175 ObjectLocker ol(h_init_lock, jt);
1176
1177 // Step 2
1178 // If we were to use wait() instead of waitInterruptibly() then
1179 // we might end up throwing IE from link/symbol resolution sites
1180 // that aren't expected to throw. This would wreak havoc. See 6320309.
1181 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1182 if (debug_logging_enabled) {
1183 ResourceMark rm(jt);
1184 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1185 jt->name(), external_name(), init_thread_name());
1186 }
1187 wait = true;
1188 jt->set_class_to_be_initialized(this);
1287 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1288 ClassLoader::perf_class_init_selftime(),
1289 ClassLoader::perf_classes_inited(),
1290 jt->get_thread_stat()->perf_recursion_counts_addr(),
1291 jt->get_thread_stat()->perf_timers_addr(),
1292 PerfClassTraceTime::CLASS_CLINIT);
1293 call_class_initializer(THREAD);
1294 } else {
1295 // The elapsed time is so small it's not worth counting.
1296 if (UsePerfData) {
1297 ClassLoader::perf_classes_inited()->inc();
1298 }
1299 call_class_initializer(THREAD);
1300 }
1301 }
1302
1303 // Step 9
1304 if (!HAS_PENDING_EXCEPTION) {
1305 set_initialization_state_and_notify(fully_initialized, CHECK);
1306 debug_only(vtable().verify(tty, true);)
1307 }
1308 else {
1309 // Step 10 and 11
1310 Handle e(THREAD, PENDING_EXCEPTION);
1311 CLEAR_PENDING_EXCEPTION;
1312 // JVMTI has already reported the pending exception
1313 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1314 JvmtiExport::clear_detected_exception(jt);
1315 {
1316 EXCEPTION_MARK;
1317 add_initialization_error(THREAD, e);
1318 set_initialization_state_and_notify(initialization_error, THREAD);
1319 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1320 // JVMTI has already reported the pending exception
1321 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1322 JvmtiExport::clear_detected_exception(jt);
1323 }
1324 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1325 if (e->is_a(vmClasses::Error_klass())) {
1326 THROW_OOP(e());
1327 } else {
1328 JavaCallArguments args(e);
1329 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1330 vmSymbols::throwable_void_signature(),
1331 &args);
1332 }
1333 }
1334 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1335 }
1336
1337
1338 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1339 Handle h_init_lock(THREAD, init_lock());
1340 if (h_init_lock() != nullptr) {
1341 ObjectLocker ol(h_init_lock, THREAD);
1342 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1343 set_init_state(state);
1344 fence_and_clear_init_lock();
1345 ol.notify_all(CHECK);
1346 } else {
1347 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1348 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1349 set_init_state(state);
1350 }
1351 }
1352
1353 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1354 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1355 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1356 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1357
1547 tty->print("Registered ");
1548 i->print_value_on(tty);
1549 tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1550 }
1551 instanceHandle h_i(THREAD, i);
1552 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1553 JavaValue result(T_VOID);
1554 JavaCallArguments args(h_i);
1555 methodHandle mh(THREAD, Universe::finalizer_register_method());
1556 JavaCalls::call(&result, mh, &args, CHECK_NULL);
1557 MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1558 return h_i();
1559 }
1560
1561 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1562 assert(!is_abstract() && !is_interface(), "Should not create this object");
1563 size_t size = size_helper(); // Query before forming handle.
1564 return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1565 }
1566
1567 instanceOop InstanceKlass::allocate_instance(oop java_class, TRAPS) {
1568 Klass* k = java_lang_Class::as_Klass(java_class);
1569 if (k == nullptr) {
1570 ResourceMark rm(THREAD);
1571 THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1572 }
1573 InstanceKlass* ik = cast(k);
1574 ik->check_valid_for_instantiation(false, CHECK_NULL);
1575 ik->initialize(CHECK_NULL);
1576 return ik->allocate_instance(THREAD);
1577 }
1578
1579 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1580 return instanceHandle(THREAD, allocate_instance(THREAD));
1581 }
1582
1583 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1584 if (is_interface() || is_abstract()) {
1585 ResourceMark rm(THREAD);
1586 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1587 : vmSymbols::java_lang_InstantiationException(), external_name());
1615 }
1616
1617 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1618 // Need load-acquire for lock-free read
1619 ObjArrayKlass* oak = array_klasses_acquire();
1620 if (oak == nullptr) {
1621 return nullptr;
1622 } else {
1623 return oak->array_klass_or_null(n);
1624 }
1625 }
1626
1627 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1628 return array_klass(1, THREAD);
1629 }
1630
1631 ArrayKlass* InstanceKlass::array_klass_or_null() {
1632 return array_klass_or_null(1);
1633 }
1634
1635 static int call_class_initializer_counter = 0; // for debugging
1636
1637 Method* InstanceKlass::class_initializer() const {
1638 Method* clinit = find_method(
1639 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1640 if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1641 return clinit;
1642 }
1643 return nullptr;
1644 }
1645
1646 void InstanceKlass::call_class_initializer(TRAPS) {
1647 if (ReplayCompiles &&
1648 (ReplaySuppressInitializers == 1 ||
1649 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1650 // Hide the existence of the initializer for the purpose of replaying the compile
1651 return;
1652 }
1653
1654 #if INCLUDE_CDS
1655 // This is needed to ensure the consistency of the archived heap objects.
1656 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1657 AOTClassInitializer::call_runtime_setup(THREAD, this);
1658 return;
1659 } else if (has_archived_enum_objs()) {
1660 assert(is_shared(), "must be");
1661 bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
1662 if (initialized) {
1663 return;
1664 }
1665 }
1666 #endif
1667
1668 methodHandle h_method(THREAD, class_initializer());
1669 assert(!is_initialized(), "we cannot initialize twice");
1670 LogTarget(Info, class, init) lt;
1671 if (lt.is_enabled()) {
1672 ResourceMark rm(THREAD);
1673 LogStream ls(lt);
1674 ls.print("%d Initializing ", call_class_initializer_counter++);
1675 name()->print_value_on(&ls);
1676 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1677 h_method() == nullptr ? "(no method)" : "", p2i(this),
1678 THREAD->name());
1679 }
1680 if (h_method() != nullptr) {
1681 ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1682 JavaCallArguments args; // No arguments
1683 JavaValue result(T_VOID);
1684 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1685 }
1686 }
1687
1688 // If a class that implements this interface is initialized, is the JVM required
1689 // to first execute a <clinit> method declared in this interface,
1690 // or (if also_check_supers==true) any of the super types of this interface?
1691 //
1692 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1693 // an interface, then let SC be its superclass and let SI1, ..., SIn
1694 // be all superinterfaces of C (whether direct or indirect) that
1695 // declare at least one non-abstract, non-static method.
1696 //
1697 // So when an interface is initialized, it does not look at its
1698 // supers. But a proper class will ensure that all of its supers have
1699 // run their <clinit> methods, except that it disregards interfaces
1700 // that lack a non-static concrete method (i.e., a default method).
1701 // Therefore, you should probably call this method only when the
1702 // current class is a super of some proper class, not an interface.
1703 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1704 assert(is_interface(), "must be");
1705
2470 }
2471 }
2472 if (new_jmeths != 0) {
2473 Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
2474 }
2475 }
2476
2477 // Lookup a jmethodID, null if not found. Do no blocking, no allocations, no handles
2478 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2479 int idnum = method->method_idnum();
2480 jmethodID* jmeths = methods_jmethod_ids_acquire();
2481 return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2482 }
2483
2484 inline DependencyContext InstanceKlass::dependencies() {
2485 DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2486 return dep_context;
2487 }
2488
2489 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2490 dependencies().mark_dependent_nmethods(deopt_scope, changes);
2491 }
2492
2493 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2494 dependencies().add_dependent_nmethod(nm);
2495 }
2496
2497 void InstanceKlass::clean_dependency_context() {
2498 dependencies().clean_unloading_dependents();
2499 }
2500
2501 #ifndef PRODUCT
2502 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2503 dependencies().print_dependent_nmethods(verbose);
2504 }
2505
2506 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2507 return dependencies().is_dependent_nmethod(nm);
2508 }
2509 #endif //PRODUCT
2510
2607 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2608 if (ioe->interface_klass() != nullptr) {
2609 it->push(ioe->interface_klass_addr());
2610 itableMethodEntry* ime = ioe->first_method_entry(this);
2611 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2612 for (int index = 0; index < n; index ++) {
2613 it->push(ime[index].method_addr());
2614 }
2615 }
2616 }
2617 }
2618
2619 it->push(&_nest_host);
2620 it->push(&_nest_members);
2621 it->push(&_permitted_subclasses);
2622 it->push(&_record_components);
2623 }
2624
2625 #if INCLUDE_CDS
2626 void InstanceKlass::remove_unshareable_info() {
2627
2628 if (is_linked()) {
2629 assert(can_be_verified_at_dumptime(), "must be");
2630 // Remember this so we can avoid walking the hierarchy at runtime.
2631 set_verified_at_dump_time();
2632 }
2633
2634 Klass::remove_unshareable_info();
2635
2636 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2637 // Classes are attempted to link during dumping and may fail,
2638 // but these classes are still in the dictionary and class list in CLD.
2639 // If the class has failed verification, there is nothing else to remove.
2640 return;
2641 }
2642
2643 // Reset to the 'allocated' state to prevent any premature accessing to
2644 // a shared class at runtime while the class is still being loaded and
2645 // restored. A class' init_state is set to 'loaded' at runtime when it's
2646 // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2647 _init_state = allocated;
2648
2649 { // Otherwise this needs to take out the Compile_lock.
2650 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2651 init_implementor();
2652 }
2671 _osr_nmethods_head = nullptr;
2672 #if INCLUDE_JVMTI
2673 _breakpoints = nullptr;
2674 _previous_versions = nullptr;
2675 _cached_class_file = nullptr;
2676 _jvmti_cached_class_field_map = nullptr;
2677 #endif
2678
2679 _init_thread = nullptr;
2680 _methods_jmethod_ids = nullptr;
2681 _jni_ids = nullptr;
2682 _oop_map_cache = nullptr;
2683 if (CDSConfig::is_dumping_invokedynamic() && HeapShared::is_lambda_proxy_klass(this)) {
2684 // keep _nest_host
2685 } else {
2686 // clear _nest_host to ensure re-load at runtime
2687 _nest_host = nullptr;
2688 }
2689 init_shared_package_entry();
2690 _dep_context_last_cleaned = 0;
2691
2692 remove_unshareable_flags();
2693 }
2694
2695 void InstanceKlass::remove_unshareable_flags() {
2696 // clear all the flags/stats that shouldn't be in the archived version
2697 assert(!is_scratch_class(), "must be");
2698 assert(!has_been_redefined(), "must be");
2699 #if INCLUDE_JVMTI
2700 set_is_being_redefined(false);
2701 #endif
2702 set_has_resolved_methods(false);
2703 }
2704
2705 void InstanceKlass::remove_java_mirror() {
2706 Klass::remove_java_mirror();
2707
2708 // do array classes also.
2709 if (array_klasses() != nullptr) {
2710 array_klasses()->remove_java_mirror();
2781
2782 // restore constant pool resolved references
2783 constants()->restore_unshareable_info(CHECK);
2784
2785 if (array_klasses() != nullptr) {
2786 // To get a consistent list of classes we need MultiArray_lock to ensure
2787 // array classes aren't observed while they are being restored.
2788 RecursiveLocker rl(MultiArray_lock, THREAD);
2789 assert(this == array_klasses()->bottom_klass(), "sanity");
2790 // Array classes have null protection domain.
2791 // --> see ArrayKlass::complete_create_array_klass()
2792 array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2793 }
2794
2795 // Initialize @ValueBased class annotation if not already set in the archived klass.
2796 if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2797 set_is_value_based();
2798 }
2799 }
2800
2801 // Check if a class or any of its supertypes has a version older than 50.
2802 // CDS will not perform verification of old classes during dump time because
2803 // without changing the old verifier, the verification constraint cannot be
2804 // retrieved during dump time.
2805 // Verification of archived old classes will be performed during run time.
2806 bool InstanceKlass::can_be_verified_at_dumptime() const {
2807 if (MetaspaceShared::is_in_shared_metaspace(this)) {
2808 // This is a class that was dumped into the base archive, so we know
2809 // it was verified at dump time.
2810 return true;
2811 }
2812 if (major_version() < 50 /*JAVA_6_VERSION*/) {
2813 return false;
2814 }
2815 if (java_super() != nullptr && !java_super()->can_be_verified_at_dumptime()) {
2816 return false;
2817 }
2818 Array<InstanceKlass*>* interfaces = local_interfaces();
2819 int len = interfaces->length();
2820 for (int i = 0; i < len; i++) {
2821 if (!interfaces->at(i)->can_be_verified_at_dumptime()) {
2822 return false;
2823 }
2824 }
2825 return true;
2826 }
2827
2828 int InstanceKlass::shared_class_loader_type() const {
2829 if (is_shared_boot_class()) {
2830 return ClassLoader::BOOT_LOADER;
2831 } else if (is_shared_platform_class()) {
2832 return ClassLoader::PLATFORM_LOADER;
2833 } else if (is_shared_app_class()) {
2834 return ClassLoader::APP_LOADER;
2835 } else {
2836 return ClassLoader::OTHER;
2837 }
2838 }
2839 #endif // INCLUDE_CDS
2840
2841 #if INCLUDE_JVMTI
2842 static void clear_all_breakpoints(Method* m) {
2843 m->clear_all_breakpoints();
2844 }
2845 #endif
2846
2847 void InstanceKlass::unload_class(InstanceKlass* ik) {
2848
2849 if (ik->is_scratch_class()) {
2850 assert(ik->dependencies().is_empty(), "dependencies should be empty for scratch classes");
2851 return;
2852 }
2853 assert(ik->is_loaded(), "class should be loaded " PTR_FORMAT, p2i(ik));
2854
2855 // Release dependencies.
2856 ik->dependencies().remove_all_dependents();
2857
2858 // notify the debugger
3596 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3597 for (int i = 0; i < len; i++) {
3598 intptr_t e = start[i];
3599 st->print("%d : " INTPTR_FORMAT, i, e);
3600 if (MetaspaceObj::is_valid((Metadata*)e)) {
3601 st->print(" ");
3602 ((Metadata*)e)->print_value_on(st);
3603 }
3604 st->cr();
3605 }
3606 }
3607
3608 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3609 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3610 }
3611
3612 const char* InstanceKlass::init_state_name() const {
3613 return state_names[init_state()];
3614 }
3615
3616 void InstanceKlass::print_on(outputStream* st) const {
3617 assert(is_klass(), "must be klass");
3618 Klass::print_on(st);
3619
3620 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3621 st->print(BULLET"klass size: %d", size()); st->cr();
3622 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3623 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3624 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3625 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3626 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3627 st->print(BULLET"sub: ");
3628 Klass* sub = subklass();
3629 int n;
3630 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3631 if (n < MaxSubklassPrintSize) {
3632 sub->print_value_on(st);
3633 st->print(" ");
3634 }
3635 }
3925 nullptr;
3926 // caller can be null, for example, during a JVMTI VM_Init hook
3927 if (caller != nullptr) {
3928 info_stream.print(" source: instance of %s", caller->external_name());
3929 } else {
3930 // source is unknown
3931 }
3932 } else {
3933 oop class_loader = loader_data->class_loader();
3934 info_stream.print(" source: %s", class_loader->klass()->external_name());
3935 }
3936 } else {
3937 assert(this->is_shared(), "must be");
3938 if (MetaspaceShared::is_shared_dynamic((void*)this)) {
3939 info_stream.print(" source: shared objects file (top)");
3940 } else {
3941 info_stream.print(" source: shared objects file");
3942 }
3943 }
3944
3945 msg.info("%s", info_stream.as_string());
3946
3947 if (log_is_enabled(Debug, class, load)) {
3948 stringStream debug_stream;
3949
3950 // Class hierarchy info
3951 debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
3952 p2i(this), p2i(superklass()));
3953
3954 // Interfaces
3955 if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
3956 debug_stream.print(" interfaces:");
3957 int length = local_interfaces()->length();
3958 for (int i = 0; i < length; i++) {
3959 debug_stream.print(" " PTR_FORMAT,
3960 p2i(InstanceKlass::cast(local_interfaces()->at(i))));
3961 }
3962 }
3963
3964 // Class loader
|
29 #include "cds/cdsEnumKlass.hpp"
30 #include "cds/classListWriter.hpp"
31 #include "cds/heapShared.hpp"
32 #include "cds/metaspaceShared.hpp"
33 #include "classfile/classFileParser.hpp"
34 #include "classfile/classFileStream.hpp"
35 #include "classfile/classLoader.hpp"
36 #include "classfile/classLoaderData.inline.hpp"
37 #include "classfile/javaClasses.hpp"
38 #include "classfile/moduleEntry.hpp"
39 #include "classfile/systemDictionary.hpp"
40 #include "classfile/systemDictionaryShared.hpp"
41 #include "classfile/verifier.hpp"
42 #include "classfile/vmClasses.hpp"
43 #include "classfile/vmSymbols.hpp"
44 #include "code/codeCache.hpp"
45 #include "code/dependencyContext.hpp"
46 #include "compiler/compilationPolicy.hpp"
47 #include "compiler/compileBroker.hpp"
48 #include "gc/shared/collectedHeap.inline.hpp"
49 #include "interpreter/bytecodeHistogram.hpp"
50 #include "interpreter/bytecodeStream.hpp"
51 #include "interpreter/oopMapCache.hpp"
52 #include "interpreter/rewriter.hpp"
53 #include "jvm.h"
54 #include "jvmtifiles/jvmti.h"
55 #include "logging/log.hpp"
56 #include "klass.inline.hpp"
57 #include "logging/logMessage.hpp"
58 #include "logging/logStream.hpp"
59 #include "memory/allocation.inline.hpp"
60 #include "memory/iterator.inline.hpp"
61 #include "memory/metadataFactory.hpp"
62 #include "memory/metaspaceClosure.hpp"
63 #include "memory/oopFactory.hpp"
64 #include "memory/resourceArea.hpp"
65 #include "memory/universe.hpp"
66 #include "oops/fieldStreams.inline.hpp"
67 #include "oops/constantPool.hpp"
68 #include "oops/instanceClassLoaderKlass.hpp"
69 #include "oops/instanceKlass.inline.hpp"
70 #include "oops/instanceMirrorKlass.hpp"
71 #include "oops/instanceOop.hpp"
72 #include "oops/instanceStackChunkKlass.hpp"
73 #include "oops/klass.inline.hpp"
74 #include "oops/method.hpp"
75 #include "oops/oop.inline.hpp"
76 #include "oops/recordComponent.hpp"
77 #include "oops/symbol.hpp"
78 #include "oops/trainingData.hpp"
79 #include "prims/jvmtiExport.hpp"
80 #include "prims/jvmtiRedefineClasses.hpp"
81 #include "prims/jvmtiThreadState.hpp"
82 #include "prims/methodComparator.hpp"
83 #include "runtime/arguments.hpp"
84 #include "runtime/deoptimization.hpp"
85 #include "runtime/atomic.hpp"
86 #include "runtime/fieldDescriptor.inline.hpp"
87 #include "runtime/handles.inline.hpp"
88 #include "runtime/javaCalls.hpp"
89 #include "runtime/javaThread.inline.hpp"
90 #include "runtime/mutexLocker.hpp"
91 #include "runtime/orderAccess.hpp"
92 #include "runtime/os.inline.hpp"
93 #include "runtime/reflection.hpp"
94 #include "runtime/runtimeUpcalls.hpp"
95 #include "runtime/synchronizer.hpp"
96 #include "runtime/threads.hpp"
97 #include "services/classLoadingService.hpp"
98 #include "services/finalizerService.hpp"
99 #include "services/threadService.hpp"
100 #include "utilities/dtrace.hpp"
101 #include "utilities/events.hpp"
102 #include "utilities/macros.hpp"
103 #include "utilities/stringUtils.hpp"
104 #include "utilities/pair.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
114
1043 return true;
1044 }
1045
1046 // Rewrite the byte codes of all of the methods of a class.
1047 // The rewriter must be called exactly once. Rewriting must happen after
1048 // verification but before the first method of the class is executed.
1049 void InstanceKlass::rewrite_class(TRAPS) {
1050 assert(is_loaded(), "must be loaded");
1051 if (is_rewritten()) {
1052 assert(is_shared(), "rewriting an unshared class?");
1053 return;
1054 }
1055 Rewriter::rewrite(this, CHECK);
1056 set_rewritten();
1057 }
1058
1059 // Now relocate and link method entry points after class is rewritten.
1060 // This is outside is_rewritten flag. In case of an exception, it can be
1061 // executed more than once.
1062 void InstanceKlass::link_methods(TRAPS) {
1063 PerfTraceElapsedTime timer(ClassLoader::perf_ik_link_methods_time());
1064
1065 int len = methods()->length();
1066 for (int i = len-1; i >= 0; i--) {
1067 methodHandle m(THREAD, methods()->at(i));
1068 RuntimeUpcalls::install_upcalls(m);
1069
1070 // Set up method entry points for compiler and interpreter .
1071 m->link_method(m, CHECK);
1072 }
1073 }
1074
1075 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1076 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1077 assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1078 for (int i = 0; i < local_interfaces()->length(); ++i) {
1079 InstanceKlass* ik = local_interfaces()->at(i);
1080
1081 // Initialization is depth first search ie. we start with top of the inheritance tree
1082 // has_nonstatic_concrete_methods drives searching superinterfaces since it
1083 // means has_nonstatic_concrete_methods in its superinterface hierarchy
1084 if (ik->has_nonstatic_concrete_methods()) {
1085 ik->initialize_super_interfaces(CHECK);
1086 }
1087
1088 // Only initialize() interfaces that "declare" concrete methods.
1153 assert_locked_or_safepoint(ClassInitError_lock);
1154 InitErrorTableCleaner cleaner;
1155 if (_initialization_error_table != nullptr) {
1156 _initialization_error_table->unlink(&cleaner);
1157 }
1158 }
1159
1160 void InstanceKlass::initialize_impl(TRAPS) {
1161 HandleMark hm(THREAD);
1162
1163 // Make sure klass is linked (verified) before initialization
1164 // A class could already be verified, since it has been reflected upon.
1165 link_class(CHECK);
1166
1167 DTRACE_CLASSINIT_PROBE(required, -1);
1168
1169 bool wait = false;
1170
1171 JavaThread* jt = THREAD;
1172
1173 if (ForceProfiling) {
1174 // Preallocate MDOs.
1175 for (int i = 0; i < methods()->length(); i++) {
1176 assert(!HAS_PENDING_EXCEPTION, "");
1177 methodHandle m(THREAD, methods()->at(i));
1178 Method::build_profiling_method_data(m, THREAD);
1179 if (HAS_PENDING_EXCEPTION) {
1180 ResourceMark rm;
1181 log_warning(cds)("MDO preallocation failed for %s", external_name());
1182 CLEAR_PENDING_EXCEPTION;
1183 break;
1184 }
1185 }
1186 }
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 CompilationPolicy::replay_training_at_init(this, THREAD);
1327 }
1328 else {
1329 // Step 10 and 11
1330 Handle e(THREAD, PENDING_EXCEPTION);
1331 CLEAR_PENDING_EXCEPTION;
1332 // JVMTI has already reported the pending exception
1333 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1334 JvmtiExport::clear_detected_exception(jt);
1335 {
1336 EXCEPTION_MARK;
1337 add_initialization_error(THREAD, e);
1338 set_initialization_state_and_notify(initialization_error, THREAD);
1339 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1340 // JVMTI has already reported the pending exception
1341 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1342 JvmtiExport::clear_detected_exception(jt);
1343 }
1344 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1345 if (e->is_a(vmClasses::Error_klass())) {
1346 THROW_OOP(e());
1347 } else {
1348 JavaCallArguments args(e);
1349 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1350 vmSymbols::throwable_void_signature(),
1351 &args);
1352 }
1353 }
1354 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
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,
1587 const char* who,
1588 TRAPS) {
1589 Klass* k = java_lang_Class::as_Klass(java_class);
1590 if (k == nullptr) {
1591 ResourceMark rm(THREAD);
1592 THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1593 }
1594 InstanceKlass* ik = cast(k);
1595 ik->check_valid_for_instantiation(false, CHECK_NULL);
1596 ik->initialize(CHECK_NULL);
1597 return ik->allocate_instance(THREAD);
1598 }
1599
1600 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1601 return instanceHandle(THREAD, allocate_instance(THREAD));
1602 }
1603
1604 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1605 if (is_interface() || is_abstract()) {
1606 ResourceMark rm(THREAD);
1607 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1608 : vmSymbols::java_lang_InstantiationException(), external_name());
1636 }
1637
1638 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1639 // Need load-acquire for lock-free read
1640 ObjArrayKlass* oak = array_klasses_acquire();
1641 if (oak == nullptr) {
1642 return nullptr;
1643 } else {
1644 return oak->array_klass_or_null(n);
1645 }
1646 }
1647
1648 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1649 return array_klass(1, THREAD);
1650 }
1651
1652 ArrayKlass* InstanceKlass::array_klass_or_null() {
1653 return array_klass_or_null(1);
1654 }
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
1690 #if 0
1691 // FIXME -- revive this code added to leyden/premain for <clinit> profiling
1692 int init_id = log_class_init(THREAD, this);
1693 if (h_method() != nullptr) {
1694 JavaCallArguments args; // No arguments
1695 JavaValue result(T_VOID);
1696 InstanceKlass* outer = THREAD->set_class_being_initialized(this);
1697 jlong bc_start = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1698
1699 elapsedTimer timer;
1700 {
1701 PerfPauseTimer pt(THREAD->current_rt_call_timer(), THREAD->profile_rt_calls());
1702 PauseRuntimeCallProfiling prcp(THREAD, THREAD->profile_rt_calls());
1703
1704 timer.start();
1705 JavaCalls::call(&result, h_method, &args, THREAD); // Static call (no args)
1706 timer.stop();
1707 }
1708
1709 jlong bc_end = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1710
1711 jlong bc_executed = (bc_end - bc_start);
1712 if (UsePerfData && outer == nullptr) { // outermost clinit
1713 THREAD->inc_clinit_bc_counter_value(bc_executed);
1714 ClassLoader::perf_class_init_bytecodes_count()->inc(bc_executed);
1715 }
1716
1717 THREAD->set_class_being_initialized(outer);
1718
1719 LogStreamHandle(Debug, init) log;
1720 if (log.is_enabled()) {
1721 ResourceMark rm(THREAD);
1722 log.print("%d Initialized in %.3fms (total: " JLONG_FORMAT "ms); ",
1723 init_id, timer.seconds() * 1000.0, ClassLoader::class_init_time_ms());
1724 if (CountBytecodes || CountBytecodesPerThread) {
1725 log.print("executed " JLONG_FORMAT " bytecodes; ", bc_executed);
1726 }
1727 name()->print_value_on(&log);
1728 log.print_cr(" by thread " PTR_FORMAT " \"%s\" (" PTR_FORMAT ")",
1729 p2i(THREAD), THREAD->name(), p2i(this));
1730 }
1731 }
1732 LogTarget(Info, class, init) lt;
1733 if (lt.is_enabled()) {
1734 ResourceMark rm(THREAD);
1735 LogStream ls(lt);
1736 ls.print("%d Initialized ", init_id);
1737 name()->print_value_on(&ls);
1738 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1739 h_method() == nullptr ? "(no method)" : "", p2i(this),
1740 THREAD->name());
1741 }
1742 #else
1743 static int call_class_initializer_counter = 0; // for debugging
1744 LogTarget(Info, class, init) lt;
1745 if (lt.is_enabled()) {
1746 ResourceMark rm(THREAD);
1747 LogStream ls(lt);
1748 ls.print("%d Initializing ", call_class_initializer_counter++);
1749 name()->print_value_on(&ls);
1750 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1751 h_method() == nullptr ? "(no method)" : "", p2i(this),
1752 THREAD->name());
1753 }
1754 if (h_method() != nullptr) {
1755 ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1756 JavaCallArguments args; // No arguments
1757 JavaValue result(T_VOID);
1758 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1759 }
1760 #endif
1761 }
1762
1763 // If a class that implements this interface is initialized, is the JVM required
1764 // to first execute a <clinit> method declared in this interface,
1765 // or (if also_check_supers==true) any of the super types of this interface?
1766 //
1767 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1768 // an interface, then let SC be its superclass and let SI1, ..., SIn
1769 // be all superinterfaces of C (whether direct or indirect) that
1770 // declare at least one non-abstract, non-static method.
1771 //
1772 // So when an interface is initialized, it does not look at its
1773 // supers. But a proper class will ensure that all of its supers have
1774 // run their <clinit> methods, except that it disregards interfaces
1775 // that lack a non-static concrete method (i.e., a default method).
1776 // Therefore, you should probably call this method only when the
1777 // current class is a super of some proper class, not an interface.
1778 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1779 assert(is_interface(), "must be");
1780
2545 }
2546 }
2547 if (new_jmeths != 0) {
2548 Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
2549 }
2550 }
2551
2552 // Lookup a jmethodID, null if not found. Do no blocking, no allocations, no handles
2553 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2554 int idnum = method->method_idnum();
2555 jmethodID* jmeths = methods_jmethod_ids_acquire();
2556 return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2557 }
2558
2559 inline DependencyContext InstanceKlass::dependencies() {
2560 DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2561 return dep_context;
2562 }
2563
2564 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2565 dependencies().mark_dependent_nmethods(deopt_scope, changes, this);
2566 }
2567
2568 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2569 dependencies().add_dependent_nmethod(nm);
2570 }
2571
2572 void InstanceKlass::clean_dependency_context() {
2573 dependencies().clean_unloading_dependents();
2574 }
2575
2576 #ifndef PRODUCT
2577 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2578 dependencies().print_dependent_nmethods(verbose);
2579 }
2580
2581 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2582 return dependencies().is_dependent_nmethod(nm);
2583 }
2584 #endif //PRODUCT
2585
2682 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2683 if (ioe->interface_klass() != nullptr) {
2684 it->push(ioe->interface_klass_addr());
2685 itableMethodEntry* ime = ioe->first_method_entry(this);
2686 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2687 for (int index = 0; index < n; index ++) {
2688 it->push(ime[index].method_addr());
2689 }
2690 }
2691 }
2692 }
2693
2694 it->push(&_nest_host);
2695 it->push(&_nest_members);
2696 it->push(&_permitted_subclasses);
2697 it->push(&_record_components);
2698 }
2699
2700 #if INCLUDE_CDS
2701 void InstanceKlass::remove_unshareable_info() {
2702 if (is_linked()) {
2703 assert(can_be_verified_at_dumptime(), "must be");
2704 // Remember this so we can avoid walking the hierarchy at runtime.
2705 set_verified_at_dump_time();
2706 }
2707 _misc_flags.set_has_init_deps_processed(false);
2708
2709 Klass::remove_unshareable_info();
2710
2711 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2712 // Classes are attempted to link during dumping and may fail,
2713 // but these classes are still in the dictionary and class list in CLD.
2714 // If the class has failed verification, there is nothing else to remove.
2715 return;
2716 }
2717
2718 // Reset to the 'allocated' state to prevent any premature accessing to
2719 // a shared class at runtime while the class is still being loaded and
2720 // restored. A class' init_state is set to 'loaded' at runtime when it's
2721 // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2722 _init_state = allocated;
2723
2724 { // Otherwise this needs to take out the Compile_lock.
2725 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2726 init_implementor();
2727 }
2746 _osr_nmethods_head = nullptr;
2747 #if INCLUDE_JVMTI
2748 _breakpoints = nullptr;
2749 _previous_versions = nullptr;
2750 _cached_class_file = nullptr;
2751 _jvmti_cached_class_field_map = nullptr;
2752 #endif
2753
2754 _init_thread = nullptr;
2755 _methods_jmethod_ids = nullptr;
2756 _jni_ids = nullptr;
2757 _oop_map_cache = nullptr;
2758 if (CDSConfig::is_dumping_invokedynamic() && HeapShared::is_lambda_proxy_klass(this)) {
2759 // keep _nest_host
2760 } else {
2761 // clear _nest_host to ensure re-load at runtime
2762 _nest_host = nullptr;
2763 }
2764 init_shared_package_entry();
2765 _dep_context_last_cleaned = 0;
2766 DEBUG_ONLY(_shared_class_load_count = 0);
2767
2768 remove_unshareable_flags();
2769 }
2770
2771 void InstanceKlass::remove_unshareable_flags() {
2772 // clear all the flags/stats that shouldn't be in the archived version
2773 assert(!is_scratch_class(), "must be");
2774 assert(!has_been_redefined(), "must be");
2775 #if INCLUDE_JVMTI
2776 set_is_being_redefined(false);
2777 #endif
2778 set_has_resolved_methods(false);
2779 }
2780
2781 void InstanceKlass::remove_java_mirror() {
2782 Klass::remove_java_mirror();
2783
2784 // do array classes also.
2785 if (array_klasses() != nullptr) {
2786 array_klasses()->remove_java_mirror();
2857
2858 // restore constant pool resolved references
2859 constants()->restore_unshareable_info(CHECK);
2860
2861 if (array_klasses() != nullptr) {
2862 // To get a consistent list of classes we need MultiArray_lock to ensure
2863 // array classes aren't observed while they are being restored.
2864 RecursiveLocker rl(MultiArray_lock, THREAD);
2865 assert(this == array_klasses()->bottom_klass(), "sanity");
2866 // Array classes have null protection domain.
2867 // --> see ArrayKlass::complete_create_array_klass()
2868 array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2869 }
2870
2871 // Initialize @ValueBased class annotation if not already set in the archived klass.
2872 if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2873 set_is_value_based();
2874 }
2875 }
2876
2877 bool InstanceKlass::can_be_verified_at_dumptime() const {
2878 if (CDSConfig::preserve_all_dumptime_verification_states(this)) {
2879 return true;
2880 }
2881
2882 if (MetaspaceShared::is_in_shared_metaspace(this)) {
2883 // This is a class that was dumped into the base archive, so we know
2884 // it was verified at dump time.
2885 return true;
2886 }
2887
2888 // Check if a class or any of its supertypes has a version older than 50.
2889 // CDS will not perform verification of old classes during dump time because
2890 // without changing the old verifier, the verification constraint cannot be
2891 // retrieved during dump time.
2892 // Verification of archived old classes will be performed during run time.
2893 if (major_version() < 50 /*JAVA_6_VERSION*/) {
2894 return false;
2895 }
2896 if (java_super() != nullptr && !java_super()->can_be_verified_at_dumptime()) {
2897 return false;
2898 }
2899 Array<InstanceKlass*>* interfaces = local_interfaces();
2900 int len = interfaces->length();
2901 for (int i = 0; i < len; i++) {
2902 if (!interfaces->at(i)->can_be_verified_at_dumptime()) {
2903 return false;
2904 }
2905 }
2906 return true;
2907 }
2908
2909 int InstanceKlass::shared_class_loader_type() const {
2910 if (is_shared_boot_class()) {
2911 return ClassLoader::BOOT_LOADER;
2912 } else if (is_shared_platform_class()) {
2913 return ClassLoader::PLATFORM_LOADER;
2914 } else if (is_shared_app_class()) {
2915 return ClassLoader::APP_LOADER;
2916 } else {
2917 return ClassLoader::OTHER;
2918 }
2919 }
2920
2921 #endif // INCLUDE_CDS
2922
2923 #if INCLUDE_JVMTI
2924 static void clear_all_breakpoints(Method* m) {
2925 m->clear_all_breakpoints();
2926 }
2927 #endif
2928
2929 void InstanceKlass::unload_class(InstanceKlass* ik) {
2930
2931 if (ik->is_scratch_class()) {
2932 assert(ik->dependencies().is_empty(), "dependencies should be empty for scratch classes");
2933 return;
2934 }
2935 assert(ik->is_loaded(), "class should be loaded " PTR_FORMAT, p2i(ik));
2936
2937 // Release dependencies.
2938 ik->dependencies().remove_all_dependents();
2939
2940 // notify the debugger
3678 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3679 for (int i = 0; i < len; i++) {
3680 intptr_t e = start[i];
3681 st->print("%d : " INTPTR_FORMAT, i, e);
3682 if (MetaspaceObj::is_valid((Metadata*)e)) {
3683 st->print(" ");
3684 ((Metadata*)e)->print_value_on(st);
3685 }
3686 st->cr();
3687 }
3688 }
3689
3690 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3691 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3692 }
3693
3694 const char* InstanceKlass::init_state_name() const {
3695 return state_names[init_state()];
3696 }
3697
3698 const char* InstanceKlass::state2name(ClassState s) {
3699 return state_names[s];
3700 }
3701
3702 void InstanceKlass::print_on(outputStream* st) const {
3703 assert(is_klass(), "must be klass");
3704 Klass::print_on(st);
3705
3706 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3707 st->print(BULLET"klass size: %d", size()); st->cr();
3708 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3709 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3710 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3711 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3712 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3713 st->print(BULLET"sub: ");
3714 Klass* sub = subklass();
3715 int n;
3716 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3717 if (n < MaxSubklassPrintSize) {
3718 sub->print_value_on(st);
3719 st->print(" ");
3720 }
3721 }
4011 nullptr;
4012 // caller can be null, for example, during a JVMTI VM_Init hook
4013 if (caller != nullptr) {
4014 info_stream.print(" source: instance of %s", caller->external_name());
4015 } else {
4016 // source is unknown
4017 }
4018 } else {
4019 oop class_loader = loader_data->class_loader();
4020 info_stream.print(" source: %s", class_loader->klass()->external_name());
4021 }
4022 } else {
4023 assert(this->is_shared(), "must be");
4024 if (MetaspaceShared::is_shared_dynamic((void*)this)) {
4025 info_stream.print(" source: shared objects file (top)");
4026 } else {
4027 info_stream.print(" source: shared objects file");
4028 }
4029 }
4030
4031 info_stream.print(" loader:");
4032 if (is_shared()) {
4033 info_stream.print(" %s", SystemDictionaryShared::class_loader_name_for_shared((Klass*)this));
4034 } else if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
4035 info_stream.print(" boot_loader");
4036 } else {
4037 oop class_loader = loader_data->class_loader();
4038 if (class_loader != nullptr) {
4039 info_stream.print(" %s", class_loader->klass()->external_name());
4040 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader);
4041 if (cl_name_and_id != nullptr) {
4042 info_stream.print(" %s", java_lang_String::as_utf8_string(cl_name_and_id));
4043 }
4044 } else {
4045 info_stream.print(" null");
4046 }
4047 }
4048 msg.info("%s", info_stream.as_string());
4049
4050 if (log_is_enabled(Debug, class, load)) {
4051 stringStream debug_stream;
4052
4053 // Class hierarchy info
4054 debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
4055 p2i(this), p2i(superklass()));
4056
4057 // Interfaces
4058 if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
4059 debug_stream.print(" interfaces:");
4060 int length = local_interfaces()->length();
4061 for (int i = 0; i < length; i++) {
4062 debug_stream.print(" " PTR_FORMAT,
4063 p2i(InstanceKlass::cast(local_interfaces()->at(i))));
4064 }
4065 }
4066
4067 // Class loader
|