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