28 #include "cds/cdsConfig.hpp"
29 #include "cds/cdsEnumKlass.hpp"
30 #include "cds/classListWriter.hpp"
31 #include "cds/heapShared.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 "klass.inline.hpp"
54 #include "logging/log.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/constantPool.hpp"
65 #include "oops/fieldStreams.inline.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/atomicAccess.hpp"
82 #include "runtime/deoptimization.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/stringUtils.hpp"
101 #ifdef COMPILER1
102 #include "c1/c1_Compiler.hpp"
103 #endif
104 #if INCLUDE_JFR
105 #include "jfr/jfrEvents.hpp"
106 #endif
107
108 #ifdef DTRACE_ENABLED
109
110
1101 return true;
1102 }
1103
1104 // Rewrite the byte codes of all of the methods of a class.
1105 // The rewriter must be called exactly once. Rewriting must happen after
1106 // verification but before the first method of the class is executed.
1107 void InstanceKlass::rewrite_class(TRAPS) {
1108 assert(is_loaded(), "must be loaded");
1109 if (is_rewritten()) {
1110 assert(in_aot_cache(), "rewriting an unshared class?");
1111 return;
1112 }
1113 Rewriter::rewrite(this, CHECK);
1114 set_rewritten();
1115 }
1116
1117 // Now relocate and link method entry points after class is rewritten.
1118 // This is outside is_rewritten flag. In case of an exception, it can be
1119 // executed more than once.
1120 void InstanceKlass::link_methods(TRAPS) {
1121 PerfTraceTime timer(ClassLoader::perf_ik_link_methods_time());
1122
1123 int len = methods()->length();
1124 for (int i = len-1; i >= 0; i--) {
1125 methodHandle m(THREAD, methods()->at(i));
1126
1127 // Set up method entry points for compiler and interpreter .
1128 m->link_method(m, CHECK);
1129 }
1130 }
1131
1132 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1133 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1134 assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1135 for (int i = 0; i < local_interfaces()->length(); ++i) {
1136 InstanceKlass* ik = local_interfaces()->at(i);
1137
1138 // Initialization is depth first search ie. we start with top of the inheritance tree
1139 // has_nonstatic_concrete_methods drives searching superinterfaces since it
1140 // means has_nonstatic_concrete_methods in its superinterface hierarchy
1141 if (ik->has_nonstatic_concrete_methods()) {
1142 ik->initialize_super_interfaces(CHECK);
1143 }
1144
1145 // Only initialize() interfaces that "declare" concrete methods.
1221 _thread->set_class_to_be_initialized(ik);
1222 }
1223 ~ThreadWaitingForClassInit() {
1224 _thread->set_class_to_be_initialized(nullptr);
1225 }
1226 };
1227
1228 void InstanceKlass::initialize_impl(TRAPS) {
1229 HandleMark hm(THREAD);
1230
1231 // Make sure klass is linked (verified) before initialization
1232 // A class could already be verified, since it has been reflected upon.
1233 link_class(CHECK);
1234
1235 DTRACE_CLASSINIT_PROBE(required, -1);
1236
1237 bool wait = false;
1238
1239 JavaThread* jt = THREAD;
1240
1241 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1242
1243 // refer to the JVM book page 47 for description of steps
1244 // Step 1
1245 {
1246 Handle h_init_lock(THREAD, init_lock());
1247 ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE);
1248
1249 // Step 2
1250 // If we were to use wait() instead of waitInterruptibly() then
1251 // we might end up throwing IE from link/symbol resolution sites
1252 // that aren't expected to throw. This would wreak havoc. See 6320309.
1253 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1254 if (debug_logging_enabled) {
1255 ResourceMark rm(jt);
1256 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1257 jt->name(), external_name(), init_thread_name());
1258 }
1259 wait = true;
1260 ThreadWaitingForClassInit twcl(THREAD, this);
1393 add_initialization_error(THREAD, e);
1394 set_initialization_state_and_notify(initialization_error, THREAD);
1395 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1396 // JVMTI has already reported the pending exception
1397 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1398 JvmtiExport::clear_detected_exception(jt);
1399 }
1400 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1401 if (e->is_a(vmClasses::Error_klass())) {
1402 THROW_OOP(e());
1403 } else {
1404 JavaCallArguments args(e);
1405 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1406 vmSymbols::throwable_void_signature(),
1407 &args);
1408 }
1409 }
1410 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1411 }
1412
1413
1414 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1415 Handle h_init_lock(THREAD, init_lock());
1416 if (h_init_lock() != nullptr) {
1417 ObjectLocker ol(h_init_lock, THREAD);
1418 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1419 set_init_state(state);
1420 fence_and_clear_init_lock();
1421 ol.notify_all(CHECK);
1422 } else {
1423 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1424 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1425 set_init_state(state);
1426 }
1427 }
1428
1429 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1430 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1431 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1432 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1433
1614 tty->print("Registered ");
1615 i->print_value_on(tty);
1616 tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1617 }
1618 instanceHandle h_i(THREAD, i);
1619 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1620 JavaValue result(T_VOID);
1621 JavaCallArguments args(h_i);
1622 methodHandle mh(THREAD, Universe::finalizer_register_method());
1623 JavaCalls::call(&result, mh, &args, CHECK_NULL);
1624 MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1625 return h_i();
1626 }
1627
1628 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1629 assert(!is_abstract() && !is_interface(), "Should not create this object");
1630 size_t size = size_helper(); // Query before forming handle.
1631 return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1632 }
1633
1634 instanceOop InstanceKlass::allocate_instance(oop java_class, TRAPS) {
1635 Klass* k = java_lang_Class::as_Klass(java_class);
1636 if (k == nullptr) {
1637 ResourceMark rm(THREAD);
1638 THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1639 }
1640 InstanceKlass* ik = cast(k);
1641 ik->check_valid_for_instantiation(false, CHECK_NULL);
1642 ik->initialize(CHECK_NULL);
1643 return ik->allocate_instance(THREAD);
1644 }
1645
1646 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1647 return instanceHandle(THREAD, allocate_instance(THREAD));
1648 }
1649
1650 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1651 if (is_interface() || is_abstract()) {
1652 ResourceMark rm(THREAD);
1653 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1654 : vmSymbols::java_lang_InstantiationException(), external_name());
1717 // Hide the existence of the initializer for the purpose of replaying the compile
1718 return;
1719 }
1720
1721 #if INCLUDE_CDS
1722 // This is needed to ensure the consistency of the archived heap objects.
1723 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1724 AOTClassInitializer::call_runtime_setup(THREAD, this);
1725 return;
1726 } else if (has_archived_enum_objs()) {
1727 assert(in_aot_cache(), "must be");
1728 bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
1729 if (initialized) {
1730 return;
1731 }
1732 }
1733 #endif
1734
1735 methodHandle h_method(THREAD, class_initializer());
1736 assert(!is_initialized(), "we cannot initialize twice");
1737 LogTarget(Info, class, init) lt;
1738 if (lt.is_enabled()) {
1739 ResourceMark rm(THREAD);
1740 LogStream ls(lt);
1741 ls.print("%d Initializing ", call_class_initializer_counter++);
1742 name()->print_value_on(&ls);
1743 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1744 h_method() == nullptr ? "(no method)" : "", p2i(this),
1745 THREAD->name());
1746 }
1747 if (h_method() != nullptr) {
1748 ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1749 JavaCallArguments args; // No arguments
1750 JavaValue result(T_VOID);
1751 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1752 }
1753 }
1754
1755 // If a class that implements this interface is initialized, is the JVM required
1756 // to first execute a <clinit> method declared in this interface,
1757 // or (if also_check_supers==true) any of the super types of this interface?
1758 //
1759 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1760 // an interface, then let SC be its superclass and let SI1, ..., SIn
1761 // be all superinterfaces of C (whether direct or indirect) that
1762 // declare at least one non-abstract, non-static method.
1763 //
1764 // So when an interface is initialized, it does not look at its
1765 // supers. But a proper class will ensure that all of its supers have
1766 // run their <clinit> methods, except that it disregards interfaces
1767 // that lack a non-static concrete method (i.e., a default method).
1768 // Therefore, you should probably call this method only when the
1769 // current class is a super of some proper class, not an interface.
1770 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1771 assert(is_interface(), "must be");
1772
2541 id == nullptr) {
2542 id = Method::make_jmethod_id(class_loader_data(), m);
2543 AtomicAccess::release_store(&jmeths[idnum + 1], id);
2544 }
2545 }
2546 }
2547
2548 // Lookup a jmethodID, null if not found. Do no blocking, no allocations, no handles
2549 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2550 int idnum = method->method_idnum();
2551 jmethodID* jmeths = methods_jmethod_ids_acquire();
2552 return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2553 }
2554
2555 inline DependencyContext InstanceKlass::dependencies() {
2556 DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2557 return dep_context;
2558 }
2559
2560 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2561 dependencies().mark_dependent_nmethods(deopt_scope, changes);
2562 }
2563
2564 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2565 assert_lock_strong(CodeCache_lock);
2566 dependencies().add_dependent_nmethod(nm);
2567 }
2568
2569 void InstanceKlass::clean_dependency_context() {
2570 dependencies().clean_unloading_dependents();
2571 }
2572
2573 #ifndef PRODUCT
2574 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2575 dependencies().print_dependent_nmethods(verbose);
2576 }
2577
2578 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2579 return dependencies().is_dependent_nmethod(nm);
2580 }
2581 #endif //PRODUCT
2684 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2685 for (int index = 0; index < n; index ++) {
2686 it->push(ime[index].method_addr());
2687 }
2688 }
2689 }
2690 }
2691
2692 it->push(&_nest_host);
2693 it->push(&_nest_members);
2694 it->push(&_permitted_subclasses);
2695 it->push(&_record_components);
2696
2697 if (CDSConfig::is_dumping_full_module_graph() && !defined_by_other_loaders()) {
2698 it->push(&_package_entry);
2699 }
2700 }
2701
2702 #if INCLUDE_CDS
2703 void InstanceKlass::remove_unshareable_info() {
2704
2705 if (is_linked()) {
2706 assert(can_be_verified_at_dumptime(), "must be");
2707 // Remember this so we can avoid walking the hierarchy at runtime.
2708 set_verified_at_dump_time();
2709 }
2710
2711 _misc_flags.set_has_init_deps_processed(false);
2712
2713 Klass::remove_unshareable_info();
2714
2715 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2716 // Classes are attempted to link during dumping and may fail,
2717 // but these classes are still in the dictionary and class list in CLD.
2718 // If the class has failed verification, there is nothing else to remove.
2719 return;
2720 }
2721
2722 // Reset to the 'allocated' state to prevent any premature accessing to
2723 // a shared class at runtime while the class is still being loaded and
2724 // restored. A class' init_state is set to 'loaded' at runtime when it's
3657 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3658 for (int i = 0; i < len; i++) {
3659 intptr_t e = start[i];
3660 st->print("%d : " INTPTR_FORMAT, i, e);
3661 if (MetaspaceObj::is_valid((Metadata*)e)) {
3662 st->print(" ");
3663 ((Metadata*)e)->print_value_on(st);
3664 }
3665 st->cr();
3666 }
3667 }
3668
3669 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3670 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3671 }
3672
3673 const char* InstanceKlass::init_state_name() const {
3674 return state_names[init_state()];
3675 }
3676
3677 void InstanceKlass::print_on(outputStream* st) const {
3678 assert(is_klass(), "must be klass");
3679 Klass::print_on(st);
3680
3681 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3682 st->print(BULLET"klass size: %d", size()); st->cr();
3683 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3684 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3685 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3686 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3687 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3688 st->print(BULLET"sub: ");
3689 Klass* sub = subklass();
3690 int n;
3691 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3692 if (n < MaxSubklassPrintSize) {
3693 sub->print_value_on(st);
3694 st->print(" ");
3695 }
3696 }
3991 nullptr;
3992 // caller can be null, for example, during a JVMTI VM_Init hook
3993 if (caller != nullptr) {
3994 info_stream.print(" source: instance of %s", caller->external_name());
3995 } else {
3996 // source is unknown
3997 }
3998 } else {
3999 oop class_loader = loader_data->class_loader();
4000 info_stream.print(" source: %s", class_loader->klass()->external_name());
4001 }
4002 } else {
4003 assert(this->in_aot_cache(), "must be");
4004 if (AOTMetaspace::in_aot_cache_dynamic_region((void*)this)) {
4005 info_stream.print(" source: shared objects file (top)");
4006 } else {
4007 info_stream.print(" source: shared objects file");
4008 }
4009 }
4010
4011 msg.info("%s", info_stream.as_string());
4012
4013 if (log_is_enabled(Debug, class, load)) {
4014 stringStream debug_stream;
4015
4016 // Class hierarchy info
4017 debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
4018 p2i(this), p2i(super()));
4019
4020 // Interfaces
4021 if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
4022 debug_stream.print(" interfaces:");
4023 int length = local_interfaces()->length();
4024 for (int i = 0; i < length; i++) {
4025 debug_stream.print(" " PTR_FORMAT,
4026 p2i(local_interfaces()->at(i)));
4027 }
4028 }
4029
4030 // Class loader
|
28 #include "cds/cdsConfig.hpp"
29 #include "cds/cdsEnumKlass.hpp"
30 #include "cds/classListWriter.hpp"
31 #include "cds/heapShared.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 "klass.inline.hpp"
55 #include "logging/log.hpp"
56 #include "logging/logMessage.hpp"
57 #include "logging/logStream.hpp"
58 #include "memory/allocation.inline.hpp"
59 #include "memory/iterator.inline.hpp"
60 #include "memory/metadataFactory.hpp"
61 #include "memory/metaspaceClosure.hpp"
62 #include "memory/oopFactory.hpp"
63 #include "memory/resourceArea.hpp"
64 #include "memory/universe.hpp"
65 #include "oops/constantPool.hpp"
66 #include "oops/fieldStreams.inline.hpp"
67 #include "oops/instanceClassLoaderKlass.hpp"
68 #include "oops/instanceKlass.inline.hpp"
72 #include "oops/klass.inline.hpp"
73 #include "oops/method.hpp"
74 #include "oops/oop.inline.hpp"
75 #include "oops/recordComponent.hpp"
76 #include "oops/symbol.hpp"
77 #include "prims/jvmtiExport.hpp"
78 #include "prims/jvmtiRedefineClasses.hpp"
79 #include "prims/jvmtiThreadState.hpp"
80 #include "prims/methodComparator.hpp"
81 #include "runtime/arguments.hpp"
82 #include "runtime/atomicAccess.hpp"
83 #include "runtime/deoptimization.hpp"
84 #include "runtime/fieldDescriptor.inline.hpp"
85 #include "runtime/handles.inline.hpp"
86 #include "runtime/javaCalls.hpp"
87 #include "runtime/javaThread.inline.hpp"
88 #include "runtime/mutexLocker.hpp"
89 #include "runtime/orderAccess.hpp"
90 #include "runtime/os.inline.hpp"
91 #include "runtime/reflection.hpp"
92 #include "runtime/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/stringUtils.hpp"
103 #ifdef COMPILER1
104 #include "c1/c1_Compiler.hpp"
105 #endif
106 #if INCLUDE_JFR
107 #include "jfr/jfrEvents.hpp"
108 #endif
109
110 #ifdef DTRACE_ENABLED
111
112
1103 return true;
1104 }
1105
1106 // Rewrite the byte codes of all of the methods of a class.
1107 // The rewriter must be called exactly once. Rewriting must happen after
1108 // verification but before the first method of the class is executed.
1109 void InstanceKlass::rewrite_class(TRAPS) {
1110 assert(is_loaded(), "must be loaded");
1111 if (is_rewritten()) {
1112 assert(in_aot_cache(), "rewriting an unshared class?");
1113 return;
1114 }
1115 Rewriter::rewrite(this, CHECK);
1116 set_rewritten();
1117 }
1118
1119 // Now relocate and link method entry points after class is rewritten.
1120 // This is outside is_rewritten flag. In case of an exception, it can be
1121 // executed more than once.
1122 void InstanceKlass::link_methods(TRAPS) {
1123 PerfTraceElapsedTime timer(ClassLoader::perf_ik_link_methods_time());
1124
1125 int len = methods()->length();
1126 for (int i = len-1; i >= 0; i--) {
1127 methodHandle m(THREAD, methods()->at(i));
1128 RuntimeUpcalls::install_upcalls(m);
1129
1130 // Set up method entry points for compiler and interpreter .
1131 m->link_method(m, CHECK);
1132 }
1133 }
1134
1135 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1136 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1137 assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1138 for (int i = 0; i < local_interfaces()->length(); ++i) {
1139 InstanceKlass* ik = local_interfaces()->at(i);
1140
1141 // Initialization is depth first search ie. we start with top of the inheritance tree
1142 // has_nonstatic_concrete_methods drives searching superinterfaces since it
1143 // means has_nonstatic_concrete_methods in its superinterface hierarchy
1144 if (ik->has_nonstatic_concrete_methods()) {
1145 ik->initialize_super_interfaces(CHECK);
1146 }
1147
1148 // Only initialize() interfaces that "declare" concrete methods.
1224 _thread->set_class_to_be_initialized(ik);
1225 }
1226 ~ThreadWaitingForClassInit() {
1227 _thread->set_class_to_be_initialized(nullptr);
1228 }
1229 };
1230
1231 void InstanceKlass::initialize_impl(TRAPS) {
1232 HandleMark hm(THREAD);
1233
1234 // Make sure klass is linked (verified) before initialization
1235 // A class could already be verified, since it has been reflected upon.
1236 link_class(CHECK);
1237
1238 DTRACE_CLASSINIT_PROBE(required, -1);
1239
1240 bool wait = false;
1241
1242 JavaThread* jt = THREAD;
1243
1244 if (ForceProfiling) {
1245 // Preallocate MDOs.
1246 for (int i = 0; i < methods()->length(); i++) {
1247 assert(!HAS_PENDING_EXCEPTION, "");
1248 methodHandle m(THREAD, methods()->at(i));
1249 Method::build_profiling_method_data(m, THREAD);
1250 if (HAS_PENDING_EXCEPTION) {
1251 ResourceMark rm;
1252 log_warning(cds)("MDO preallocation failed for %s", external_name());
1253 CLEAR_PENDING_EXCEPTION;
1254 break;
1255 }
1256 }
1257 }
1258
1259 bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1260
1261 // refer to the JVM book page 47 for description of steps
1262 // Step 1
1263 {
1264 Handle h_init_lock(THREAD, init_lock());
1265 ObjectLocker ol(h_init_lock, CHECK_PREEMPTABLE);
1266
1267 // Step 2
1268 // If we were to use wait() instead of waitInterruptibly() then
1269 // we might end up throwing IE from link/symbol resolution sites
1270 // that aren't expected to throw. This would wreak havoc. See 6320309.
1271 while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1272 if (debug_logging_enabled) {
1273 ResourceMark rm(jt);
1274 log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1275 jt->name(), external_name(), init_thread_name());
1276 }
1277 wait = true;
1278 ThreadWaitingForClassInit twcl(THREAD, this);
1411 add_initialization_error(THREAD, e);
1412 set_initialization_state_and_notify(initialization_error, THREAD);
1413 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1414 // JVMTI has already reported the pending exception
1415 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1416 JvmtiExport::clear_detected_exception(jt);
1417 }
1418 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1419 if (e->is_a(vmClasses::Error_klass())) {
1420 THROW_OOP(e());
1421 } else {
1422 JavaCallArguments args(e);
1423 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1424 vmSymbols::throwable_void_signature(),
1425 &args);
1426 }
1427 }
1428 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1429 }
1430
1431 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1432 Handle h_init_lock(THREAD, init_lock());
1433 if (h_init_lock() != nullptr) {
1434 ObjectLocker ol(h_init_lock, THREAD);
1435 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1436 set_init_state(state);
1437 fence_and_clear_init_lock();
1438 ol.notify_all(CHECK);
1439 } else {
1440 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1441 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1442 set_init_state(state);
1443 }
1444 }
1445
1446 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1447 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1448 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1449 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1450
1631 tty->print("Registered ");
1632 i->print_value_on(tty);
1633 tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1634 }
1635 instanceHandle h_i(THREAD, i);
1636 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1637 JavaValue result(T_VOID);
1638 JavaCallArguments args(h_i);
1639 methodHandle mh(THREAD, Universe::finalizer_register_method());
1640 JavaCalls::call(&result, mh, &args, CHECK_NULL);
1641 MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1642 return h_i();
1643 }
1644
1645 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1646 assert(!is_abstract() && !is_interface(), "Should not create this object");
1647 size_t size = size_helper(); // Query before forming handle.
1648 return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1649 }
1650
1651 instanceOop InstanceKlass::allocate_instance(oop java_class,
1652 const char* who,
1653 TRAPS) {
1654 Klass* k = java_lang_Class::as_Klass(java_class);
1655 if (k == nullptr) {
1656 ResourceMark rm(THREAD);
1657 THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1658 }
1659 InstanceKlass* ik = cast(k);
1660 ik->check_valid_for_instantiation(false, CHECK_NULL);
1661 ik->initialize(CHECK_NULL);
1662 return ik->allocate_instance(THREAD);
1663 }
1664
1665 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1666 return instanceHandle(THREAD, allocate_instance(THREAD));
1667 }
1668
1669 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1670 if (is_interface() || is_abstract()) {
1671 ResourceMark rm(THREAD);
1672 THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1673 : vmSymbols::java_lang_InstantiationException(), external_name());
1736 // Hide the existence of the initializer for the purpose of replaying the compile
1737 return;
1738 }
1739
1740 #if INCLUDE_CDS
1741 // This is needed to ensure the consistency of the archived heap objects.
1742 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1743 AOTClassInitializer::call_runtime_setup(THREAD, this);
1744 return;
1745 } else if (has_archived_enum_objs()) {
1746 assert(in_aot_cache(), "must be");
1747 bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
1748 if (initialized) {
1749 return;
1750 }
1751 }
1752 #endif
1753
1754 methodHandle h_method(THREAD, class_initializer());
1755 assert(!is_initialized(), "we cannot initialize twice");
1756
1757 #if 0
1758 // FIXME -- revive this code added to leyden/premain for <clinit> profiling
1759 int init_id = log_class_init(THREAD, this);
1760 if (h_method() != nullptr) {
1761 JavaCallArguments args; // No arguments
1762 JavaValue result(T_VOID);
1763 InstanceKlass* outer = THREAD->set_class_being_initialized(this);
1764 jlong bc_start = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1765
1766 elapsedTimer timer;
1767 {
1768 PerfPauseTimer pt(THREAD->current_rt_call_timer(), THREAD->profile_rt_calls());
1769 PauseRuntimeCallProfiling prcp(THREAD, THREAD->profile_rt_calls());
1770
1771 timer.start();
1772 JavaCalls::call(&result, h_method, &args, THREAD); // Static call (no args)
1773 timer.stop();
1774 }
1775
1776 jlong bc_end = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1777
1778 jlong bc_executed = (bc_end - bc_start);
1779 if (UsePerfData && outer == nullptr) { // outermost clinit
1780 THREAD->inc_clinit_bc_counter_value(bc_executed);
1781 ClassLoader::perf_class_init_bytecodes_count()->inc(bc_executed);
1782 }
1783
1784 THREAD->set_class_being_initialized(outer);
1785
1786 LogStreamHandle(Debug, init) log;
1787 if (log.is_enabled()) {
1788 ResourceMark rm(THREAD);
1789 log.print("%d Initialized in %.3fms (total: " JLONG_FORMAT "ms); ",
1790 init_id, timer.seconds() * 1000.0, ClassLoader::class_init_time_ms());
1791 if (CountBytecodes || CountBytecodesPerThread) {
1792 log.print("executed " JLONG_FORMAT " bytecodes; ", bc_executed);
1793 }
1794 name()->print_value_on(&log);
1795 log.print_cr(" by thread " PTR_FORMAT " \"%s\" (" PTR_FORMAT ")",
1796 p2i(THREAD), THREAD->name(), p2i(this));
1797 }
1798 }
1799 LogTarget(Info, class, init) lt;
1800 if (lt.is_enabled()) {
1801 ResourceMark rm(THREAD);
1802 LogStream ls(lt);
1803 ls.print("%d Initialized ", init_id);
1804 name()->print_value_on(&ls);
1805 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1806 h_method() == nullptr ? "(no method)" : "", p2i(this),
1807 THREAD->name());
1808 }
1809 #else
1810 LogTarget(Info, class, init) lt;
1811 if (lt.is_enabled()) {
1812 ResourceMark rm(THREAD);
1813 LogStream ls(lt);
1814 ls.print("%d Initializing ", call_class_initializer_counter++);
1815 name()->print_value_on(&ls);
1816 ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1817 h_method() == nullptr ? "(no method)" : "", p2i(this),
1818 THREAD->name());
1819 }
1820 if (h_method() != nullptr) {
1821 ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1822 JavaCallArguments args; // No arguments
1823 JavaValue result(T_VOID);
1824 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1825 }
1826 #endif
1827 }
1828
1829 // If a class that implements this interface is initialized, is the JVM required
1830 // to first execute a <clinit> method declared in this interface,
1831 // or (if also_check_supers==true) any of the super types of this interface?
1832 //
1833 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1834 // an interface, then let SC be its superclass and let SI1, ..., SIn
1835 // be all superinterfaces of C (whether direct or indirect) that
1836 // declare at least one non-abstract, non-static method.
1837 //
1838 // So when an interface is initialized, it does not look at its
1839 // supers. But a proper class will ensure that all of its supers have
1840 // run their <clinit> methods, except that it disregards interfaces
1841 // that lack a non-static concrete method (i.e., a default method).
1842 // Therefore, you should probably call this method only when the
1843 // current class is a super of some proper class, not an interface.
1844 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1845 assert(is_interface(), "must be");
1846
2615 id == nullptr) {
2616 id = Method::make_jmethod_id(class_loader_data(), m);
2617 AtomicAccess::release_store(&jmeths[idnum + 1], id);
2618 }
2619 }
2620 }
2621
2622 // Lookup a jmethodID, null if not found. Do no blocking, no allocations, no handles
2623 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2624 int idnum = method->method_idnum();
2625 jmethodID* jmeths = methods_jmethod_ids_acquire();
2626 return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2627 }
2628
2629 inline DependencyContext InstanceKlass::dependencies() {
2630 DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2631 return dep_context;
2632 }
2633
2634 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2635 dependencies().mark_dependent_nmethods(deopt_scope, changes, this);
2636 }
2637
2638 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2639 assert_lock_strong(CodeCache_lock);
2640 dependencies().add_dependent_nmethod(nm);
2641 }
2642
2643 void InstanceKlass::clean_dependency_context() {
2644 dependencies().clean_unloading_dependents();
2645 }
2646
2647 #ifndef PRODUCT
2648 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2649 dependencies().print_dependent_nmethods(verbose);
2650 }
2651
2652 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2653 return dependencies().is_dependent_nmethod(nm);
2654 }
2655 #endif //PRODUCT
2758 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2759 for (int index = 0; index < n; index ++) {
2760 it->push(ime[index].method_addr());
2761 }
2762 }
2763 }
2764 }
2765
2766 it->push(&_nest_host);
2767 it->push(&_nest_members);
2768 it->push(&_permitted_subclasses);
2769 it->push(&_record_components);
2770
2771 if (CDSConfig::is_dumping_full_module_graph() && !defined_by_other_loaders()) {
2772 it->push(&_package_entry);
2773 }
2774 }
2775
2776 #if INCLUDE_CDS
2777 void InstanceKlass::remove_unshareable_info() {
2778 if (is_linked()) {
2779 assert(can_be_verified_at_dumptime(), "must be");
2780 // Remember this so we can avoid walking the hierarchy at runtime.
2781 set_verified_at_dump_time();
2782 }
2783
2784 _misc_flags.set_has_init_deps_processed(false);
2785
2786 Klass::remove_unshareable_info();
2787
2788 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2789 // Classes are attempted to link during dumping and may fail,
2790 // but these classes are still in the dictionary and class list in CLD.
2791 // If the class has failed verification, there is nothing else to remove.
2792 return;
2793 }
2794
2795 // Reset to the 'allocated' state to prevent any premature accessing to
2796 // a shared class at runtime while the class is still being loaded and
2797 // restored. A class' init_state is set to 'loaded' at runtime when it's
3730 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3731 for (int i = 0; i < len; i++) {
3732 intptr_t e = start[i];
3733 st->print("%d : " INTPTR_FORMAT, i, e);
3734 if (MetaspaceObj::is_valid((Metadata*)e)) {
3735 st->print(" ");
3736 ((Metadata*)e)->print_value_on(st);
3737 }
3738 st->cr();
3739 }
3740 }
3741
3742 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3743 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3744 }
3745
3746 const char* InstanceKlass::init_state_name() const {
3747 return state_names[init_state()];
3748 }
3749
3750 const char* InstanceKlass::state2name(ClassState s) {
3751 return state_names[s];
3752 }
3753
3754 void InstanceKlass::print_on(outputStream* st) const {
3755 assert(is_klass(), "must be klass");
3756 Klass::print_on(st);
3757
3758 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3759 st->print(BULLET"klass size: %d", size()); st->cr();
3760 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3761 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3762 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3763 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3764 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3765 st->print(BULLET"sub: ");
3766 Klass* sub = subklass();
3767 int n;
3768 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3769 if (n < MaxSubklassPrintSize) {
3770 sub->print_value_on(st);
3771 st->print(" ");
3772 }
3773 }
4068 nullptr;
4069 // caller can be null, for example, during a JVMTI VM_Init hook
4070 if (caller != nullptr) {
4071 info_stream.print(" source: instance of %s", caller->external_name());
4072 } else {
4073 // source is unknown
4074 }
4075 } else {
4076 oop class_loader = loader_data->class_loader();
4077 info_stream.print(" source: %s", class_loader->klass()->external_name());
4078 }
4079 } else {
4080 assert(this->in_aot_cache(), "must be");
4081 if (AOTMetaspace::in_aot_cache_dynamic_region((void*)this)) {
4082 info_stream.print(" source: shared objects file (top)");
4083 } else {
4084 info_stream.print(" source: shared objects file");
4085 }
4086 }
4087
4088 info_stream.print(" loader:");
4089 #if INCLUDE_CDS
4090 if (in_aot_cache()) {
4091 info_stream.print(" %s", SystemDictionaryShared::loader_type_for_shared_class((Klass*)this));
4092 } else
4093 #endif
4094 if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
4095 info_stream.print(" boot_loader");
4096 } else {
4097 oop class_loader = loader_data->class_loader();
4098 if (class_loader != nullptr) {
4099 info_stream.print(" %s", class_loader->klass()->external_name());
4100 oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader);
4101 if (cl_name_and_id != nullptr) {
4102 info_stream.print(" %s", java_lang_String::as_utf8_string(cl_name_and_id));
4103 }
4104 } else {
4105 info_stream.print(" null");
4106 }
4107 }
4108 msg.info("%s", info_stream.as_string());
4109
4110 if (log_is_enabled(Debug, class, load)) {
4111 stringStream debug_stream;
4112
4113 // Class hierarchy info
4114 debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
4115 p2i(this), p2i(super()));
4116
4117 // Interfaces
4118 if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
4119 debug_stream.print(" interfaces:");
4120 int length = local_interfaces()->length();
4121 for (int i = 0; i < length; i++) {
4122 debug_stream.print(" " PTR_FORMAT,
4123 p2i(local_interfaces()->at(i)));
4124 }
4125 }
4126
4127 // Class loader
|