< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page

  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/nativeStackPrinter.hpp"
 101 #include "utilities/pair.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 

 863   }
 864 
 865   if (log_is_enabled(Info, cds, init)) {
 866     ResourceMark rm;
 867     log_info(cds, init)("%s (aot-inited)", external_name());
 868   }
 869 
 870   link_class(CHECK);
 871 
 872 #ifdef ASSERT
 873   {
 874     Handle h_init_lock(THREAD, init_lock());
 875     ObjectLocker ol(h_init_lock, THREAD);
 876     assert(!is_initialized(), "sanity");
 877     assert(!is_being_initialized(), "sanity");
 878     assert(!is_in_error_state(), "sanity");
 879   }
 880 #endif
 881 
 882   set_init_thread(THREAD);
 883   AOTClassInitializer::call_runtime_setup(THREAD, this);
 884   set_initialization_state_and_notify(fully_initialized, CHECK);

 885 }
 886 #endif
 887 
 888 bool InstanceKlass::verify_code(TRAPS) {
 889   // 1) Verify the bytecodes
 890   return Verifier::verify(this, should_verify_class(), THREAD);
 891 }
 892 
 893 void InstanceKlass::link_class(TRAPS) {
 894   assert(is_loaded(), "must be loaded");
 895   if (!is_linked()) {
 896     link_class_impl(CHECK);
 897   }
 898 }
 899 
 900 // Called to verify that a class can link during initialization, without
 901 // throwing a VerifyError.
 902 bool InstanceKlass::link_class_or_fail(TRAPS) {
 903   assert(is_loaded(), "must be loaded");
 904   if (!is_linked()) {

1060   return true;
1061 }
1062 
1063 // Rewrite the byte codes of all of the methods of a class.
1064 // The rewriter must be called exactly once. Rewriting must happen after
1065 // verification but before the first method of the class is executed.
1066 void InstanceKlass::rewrite_class(TRAPS) {
1067   assert(is_loaded(), "must be loaded");
1068   if (is_rewritten()) {
1069     assert(is_shared(), "rewriting an unshared class?");
1070     return;
1071   }
1072   Rewriter::rewrite(this, CHECK);
1073   set_rewritten();
1074 }
1075 
1076 // Now relocate and link method entry points after class is rewritten.
1077 // This is outside is_rewritten flag. In case of an exception, it can be
1078 // executed more than once.
1079 void InstanceKlass::link_methods(TRAPS) {
1080   PerfTraceTime timer(ClassLoader::perf_ik_link_methods_time());
1081 
1082   int len = methods()->length();
1083   for (int i = len-1; i >= 0; i--) {
1084     methodHandle m(THREAD, methods()->at(i));

1085 
1086     // Set up method entry points for compiler and interpreter    .
1087     m->link_method(m, CHECK);
1088   }
1089 }
1090 
1091 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1092 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1093   assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1094   for (int i = 0; i < local_interfaces()->length(); ++i) {
1095     InstanceKlass* ik = local_interfaces()->at(i);
1096 
1097     // Initialization is depth first search ie. we start with top of the inheritance tree
1098     // has_nonstatic_concrete_methods drives searching superinterfaces since it
1099     // means has_nonstatic_concrete_methods in its superinterface hierarchy
1100     if (ik->has_nonstatic_concrete_methods()) {
1101       ik->initialize_super_interfaces(CHECK);
1102     }
1103 
1104     // Only initialize() interfaces that "declare" concrete methods.

1169   assert_locked_or_safepoint(ClassInitError_lock);
1170   InitErrorTableCleaner cleaner;
1171   if (_initialization_error_table != nullptr) {
1172     _initialization_error_table->unlink(&cleaner);
1173   }
1174 }
1175 
1176 void InstanceKlass::initialize_impl(TRAPS) {
1177   HandleMark hm(THREAD);
1178 
1179   // Make sure klass is linked (verified) before initialization
1180   // A class could already be verified, since it has been reflected upon.
1181   link_class(CHECK);
1182 
1183   DTRACE_CLASSINIT_PROBE(required, -1);
1184 
1185   bool wait = false;
1186 
1187   JavaThread* jt = THREAD;
1188 















1189   bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1190 
1191   // refer to the JVM book page 47 for description of steps
1192   // Step 1
1193   {
1194     Handle h_init_lock(THREAD, init_lock());
1195     ObjectLocker ol(h_init_lock, jt);
1196 
1197     // Step 2
1198     // If we were to use wait() instead of waitInterruptibly() then
1199     // we might end up throwing IE from link/symbol resolution sites
1200     // that aren't expected to throw.  This would wreak havoc.  See 6320309.
1201     while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1202       if (debug_logging_enabled) {
1203         ResourceMark rm(jt);
1204         log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1205                                jt->name(), external_name(), init_thread_name());
1206       }
1207       wait = true;
1208       jt->set_class_to_be_initialized(this);

1307       PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1308                                ClassLoader::perf_class_init_selftime(),
1309                                ClassLoader::perf_classes_inited(),
1310                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1311                                jt->get_thread_stat()->perf_timers_addr(),
1312                                PerfClassTraceTime::CLASS_CLINIT);
1313       call_class_initializer(THREAD);
1314     } else {
1315       // The elapsed time is so small it's not worth counting.
1316       if (UsePerfData) {
1317         ClassLoader::perf_classes_inited()->inc();
1318       }
1319       call_class_initializer(THREAD);
1320     }
1321   }
1322 
1323   // Step 9
1324   if (!HAS_PENDING_EXCEPTION) {
1325     set_initialization_state_and_notify(fully_initialized, CHECK);
1326     debug_only(vtable().verify(tty, true);)

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 
1358 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1359   Handle h_init_lock(THREAD, init_lock());
1360   if (h_init_lock() != nullptr) {
1361     ObjectLocker ol(h_init_lock, THREAD);
1362     set_init_thread(nullptr); // reset _init_thread before changing _init_state
1363     set_init_state(state);
1364     fence_and_clear_init_lock();
1365     ol.notify_all(CHECK);
1366   } else {
1367     assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1368     set_init_thread(nullptr); // reset _init_thread before changing _init_state
1369     set_init_state(state);
1370   }
1371 }
1372 
1373 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1374 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1375 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1376   assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1377 

1567     tty->print("Registered ");
1568     i->print_value_on(tty);
1569     tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1570   }
1571   instanceHandle h_i(THREAD, i);
1572   // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1573   JavaValue result(T_VOID);
1574   JavaCallArguments args(h_i);
1575   methodHandle mh(THREAD, Universe::finalizer_register_method());
1576   JavaCalls::call(&result, mh, &args, CHECK_NULL);
1577   MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1578   return h_i();
1579 }
1580 
1581 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1582   assert(!is_abstract() && !is_interface(), "Should not create this object");
1583   size_t size = size_helper();  // Query before forming handle.
1584   return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1585 }
1586 
1587 instanceOop InstanceKlass::allocate_instance(oop java_class, TRAPS) {


1588   Klass* k = java_lang_Class::as_Klass(java_class);
1589   if (k == nullptr) {
1590     ResourceMark rm(THREAD);
1591     THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1592   }
1593   InstanceKlass* ik = cast(k);
1594   ik->check_valid_for_instantiation(false, CHECK_NULL);
1595   ik->initialize(CHECK_NULL);
1596   return ik->allocate_instance(THREAD);
1597 }
1598 
1599 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1600   return instanceHandle(THREAD, allocate_instance(THREAD));
1601 }
1602 
1603 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1604   if (is_interface() || is_abstract()) {
1605     ResourceMark rm(THREAD);
1606     THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1607               : vmSymbols::java_lang_InstantiationException(), external_name());

1635 }
1636 
1637 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1638   // Need load-acquire for lock-free read
1639   ObjArrayKlass* oak = array_klasses_acquire();
1640   if (oak == nullptr) {
1641     return nullptr;
1642   } else {
1643     return oak->array_klass_or_null(n);
1644   }
1645 }
1646 
1647 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1648   return array_klass(1, THREAD);
1649 }
1650 
1651 ArrayKlass* InstanceKlass::array_klass_or_null() {
1652   return array_klass_or_null(1);
1653 }
1654 
1655 static int call_class_initializer_counter = 0;   // for debugging
1656 
1657 Method* InstanceKlass::class_initializer() const {
1658   Method* clinit = find_method(
1659       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1660   if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1661     return clinit;
1662   }
1663   return nullptr;
1664 }
1665 
1666 void InstanceKlass::call_class_initializer(TRAPS) {
1667   if (ReplayCompiles &&
1668       (ReplaySuppressInitializers == 1 ||
1669        (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1670     // Hide the existence of the initializer for the purpose of replaying the compile
1671     return;
1672   }
1673 
1674 #if INCLUDE_CDS
1675   // This is needed to ensure the consistency of the archived heap objects.
1676   if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1677     AOTClassInitializer::call_runtime_setup(THREAD, this);
1678     return;
1679   } else if (has_archived_enum_objs()) {
1680     assert(is_shared(), "must be");
1681     bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
1682     if (initialized) {
1683       return;
1684     }
1685   }
1686 #endif
1687 
1688   methodHandle h_method(THREAD, class_initializer());
1689   assert(!is_initialized(), "we cannot initialize twice");























































1690   LogTarget(Info, class, init) lt;
1691   if (lt.is_enabled()) {
1692     ResourceMark rm(THREAD);
1693     LogStream ls(lt);
1694     ls.print("%d Initializing ", call_class_initializer_counter++);
1695     name()->print_value_on(&ls);
1696     ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1697                 h_method() == nullptr ? "(no method)" : "", p2i(this),
1698                 THREAD->name());
1699   }
1700   if (h_method() != nullptr) {
1701     ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1702     JavaCallArguments args; // No arguments
1703     JavaValue result(T_VOID);
1704     JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1705   }

1706 }
1707 
1708 // If a class that implements this interface is initialized, is the JVM required
1709 // to first execute a <clinit> method declared in this interface,
1710 // or (if also_check_supers==true) any of the super types of this interface?
1711 //
1712 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1713 // an interface, then let SC be its superclass and let SI1, ..., SIn
1714 // be all superinterfaces of C (whether direct or indirect) that
1715 // declare at least one non-abstract, non-static method.
1716 //
1717 // So when an interface is initialized, it does not look at its
1718 // supers. But a proper class will ensure that all of its supers have
1719 // run their <clinit> methods, except that it disregards interfaces
1720 // that lack a non-static concrete method (i.e., a default method).
1721 // Therefore, you should probably call this method only when the
1722 // current class is a super of some proper class, not an interface.
1723 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1724   assert(is_interface(), "must be");
1725 

2490     }
2491   }
2492   if (new_jmeths != 0) {
2493     Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
2494   }
2495 }
2496 
2497 // Lookup a jmethodID, null if not found.  Do no blocking, no allocations, no handles
2498 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2499   int idnum = method->method_idnum();
2500   jmethodID* jmeths = methods_jmethod_ids_acquire();
2501   return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2502 }
2503 
2504 inline DependencyContext InstanceKlass::dependencies() {
2505   DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2506   return dep_context;
2507 }
2508 
2509 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2510   dependencies().mark_dependent_nmethods(deopt_scope, changes);
2511 }
2512 
2513 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2514   dependencies().add_dependent_nmethod(nm);
2515 }
2516 
2517 void InstanceKlass::clean_dependency_context() {
2518   dependencies().clean_unloading_dependents();
2519 }
2520 
2521 #ifndef PRODUCT
2522 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2523   dependencies().print_dependent_nmethods(verbose);
2524 }
2525 
2526 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2527   return dependencies().is_dependent_nmethod(nm);
2528 }
2529 #endif //PRODUCT
2530 

2627     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2628       if (ioe->interface_klass() != nullptr) {
2629         it->push(ioe->interface_klass_addr());
2630         itableMethodEntry* ime = ioe->first_method_entry(this);
2631         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2632         for (int index = 0; index < n; index ++) {
2633           it->push(ime[index].method_addr());
2634         }
2635       }
2636     }
2637   }
2638 
2639   it->push(&_nest_host);
2640   it->push(&_nest_members);
2641   it->push(&_permitted_subclasses);
2642   it->push(&_record_components);
2643 }
2644 
2645 #if INCLUDE_CDS
2646 void InstanceKlass::remove_unshareable_info() {
2647 
2648   if (is_linked()) {
2649     assert(can_be_verified_at_dumptime(), "must be");
2650     // Remember this so we can avoid walking the hierarchy at runtime.
2651     set_verified_at_dump_time();
2652   }

2653 
2654   Klass::remove_unshareable_info();
2655 
2656   if (SystemDictionaryShared::has_class_failed_verification(this)) {
2657     // Classes are attempted to link during dumping and may fail,
2658     // but these classes are still in the dictionary and class list in CLD.
2659     // If the class has failed verification, there is nothing else to remove.
2660     return;
2661   }
2662 
2663   // Reset to the 'allocated' state to prevent any premature accessing to
2664   // a shared class at runtime while the class is still being loaded and
2665   // restored. A class' init_state is set to 'loaded' at runtime when it's
2666   // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2667   _init_state = allocated;
2668 
2669   { // Otherwise this needs to take out the Compile_lock.
2670     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2671     init_implementor();
2672   }

2691   _osr_nmethods_head = nullptr;
2692 #if INCLUDE_JVMTI
2693   _breakpoints = nullptr;
2694   _previous_versions = nullptr;
2695   _cached_class_file = nullptr;
2696   _jvmti_cached_class_field_map = nullptr;
2697 #endif
2698 
2699   _init_thread = nullptr;
2700   _methods_jmethod_ids = nullptr;
2701   _jni_ids = nullptr;
2702   _oop_map_cache = nullptr;
2703   if (CDSConfig::is_dumping_invokedynamic() && HeapShared::is_lambda_proxy_klass(this)) {
2704     // keep _nest_host
2705   } else {
2706     // clear _nest_host to ensure re-load at runtime
2707     _nest_host = nullptr;
2708   }
2709   init_shared_package_entry();
2710   _dep_context_last_cleaned = 0;

2711 
2712   remove_unshareable_flags();
2713 }
2714 
2715 void InstanceKlass::remove_unshareable_flags() {
2716   // clear all the flags/stats that shouldn't be in the archived version
2717   assert(!is_scratch_class(), "must be");
2718   assert(!has_been_redefined(), "must be");
2719 #if INCLUDE_JVMTI
2720   set_is_being_redefined(false);
2721 #endif
2722   set_has_resolved_methods(false);
2723 }
2724 
2725 void InstanceKlass::remove_java_mirror() {
2726   Klass::remove_java_mirror();
2727 
2728   // do array classes also.
2729   if (array_klasses() != nullptr) {
2730     array_klasses()->remove_java_mirror();

2801 
2802   // restore constant pool resolved references
2803   constants()->restore_unshareable_info(CHECK);
2804 
2805   if (array_klasses() != nullptr) {
2806     // To get a consistent list of classes we need MultiArray_lock to ensure
2807     // array classes aren't observed while they are being restored.
2808     RecursiveLocker rl(MultiArray_lock, THREAD);
2809     assert(this == array_klasses()->bottom_klass(), "sanity");
2810     // Array classes have null protection domain.
2811     // --> see ArrayKlass::complete_create_array_klass()
2812     array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2813   }
2814 
2815   // Initialize @ValueBased class annotation if not already set in the archived klass.
2816   if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2817     set_is_value_based();
2818   }
2819 }
2820 
2821 // Check if a class or any of its supertypes has a version older than 50.
2822 // CDS will not perform verification of old classes during dump time because
2823 // without changing the old verifier, the verification constraint cannot be
2824 // retrieved during dump time.
2825 // Verification of archived old classes will be performed during run time.
2826 bool InstanceKlass::can_be_verified_at_dumptime() const {




2827   if (MetaspaceShared::is_in_shared_metaspace(this)) {
2828     // This is a class that was dumped into the base archive, so we know
2829     // it was verified at dump time.
2830     return true;
2831   }






2832   if (major_version() < 50 /*JAVA_6_VERSION*/) {
2833     return false;
2834   }
2835   if (java_super() != nullptr && !java_super()->can_be_verified_at_dumptime()) {
2836     return false;
2837   }
2838   Array<InstanceKlass*>* interfaces = local_interfaces();
2839   int len = interfaces->length();
2840   for (int i = 0; i < len; i++) {
2841     if (!interfaces->at(i)->can_be_verified_at_dumptime()) {
2842       return false;
2843     }
2844   }
2845   return true;
2846 }
2847 
2848 int InstanceKlass::shared_class_loader_type() const {
2849   if (is_shared_boot_class()) {
2850     return ClassLoader::BOOT_LOADER;
2851   } else if (is_shared_platform_class()) {
2852     return ClassLoader::PLATFORM_LOADER;
2853   } else if (is_shared_app_class()) {
2854     return ClassLoader::APP_LOADER;
2855   } else {
2856     return ClassLoader::OTHER;
2857   }
2858 }

2859 #endif // INCLUDE_CDS
2860 
2861 #if INCLUDE_JVMTI
2862 static void clear_all_breakpoints(Method* m) {
2863   m->clear_all_breakpoints();
2864 }
2865 #endif
2866 
2867 void InstanceKlass::unload_class(InstanceKlass* ik) {
2868 
2869   if (ik->is_scratch_class()) {
2870     assert(ik->dependencies().is_empty(), "dependencies should be empty for scratch classes");
2871     return;
2872   }
2873   assert(ik->is_loaded(), "class should be loaded " PTR_FORMAT, p2i(ik));
2874 
2875   // Release dependencies.
2876   ik->dependencies().remove_all_dependents();
2877 
2878   // notify the debugger

3617 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3618   for (int i = 0; i < len; i++) {
3619     intptr_t e = start[i];
3620     st->print("%d : " INTPTR_FORMAT, i, e);
3621     if (MetaspaceObj::is_valid((Metadata*)e)) {
3622       st->print(" ");
3623       ((Metadata*)e)->print_value_on(st);
3624     }
3625     st->cr();
3626   }
3627 }
3628 
3629 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3630   return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3631 }
3632 
3633 const char* InstanceKlass::init_state_name() const {
3634   return state_names[init_state()];
3635 }
3636 




3637 void InstanceKlass::print_on(outputStream* st) const {
3638   assert(is_klass(), "must be klass");
3639   Klass::print_on(st);
3640 
3641   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3642   st->print(BULLET"klass size:        %d", size());                               st->cr();
3643   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();
3644   st->print(BULLET"flags:             "); _misc_flags.print_on(st);               st->cr();
3645   st->print(BULLET"state:             "); st->print_cr("%s", init_state_name());
3646   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
3647   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3648   st->print(BULLET"sub:               ");
3649   Klass* sub = subklass();
3650   int n;
3651   for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3652     if (n < MaxSubklassPrintSize) {
3653       sub->print_value_on(st);
3654       st->print("   ");
3655     }
3656   }

3946         nullptr;
3947       // caller can be null, for example, during a JVMTI VM_Init hook
3948       if (caller != nullptr) {
3949         info_stream.print(" source: instance of %s", caller->external_name());
3950       } else {
3951         // source is unknown
3952       }
3953     } else {
3954       oop class_loader = loader_data->class_loader();
3955       info_stream.print(" source: %s", class_loader->klass()->external_name());
3956     }
3957   } else {
3958     assert(this->is_shared(), "must be");
3959     if (MetaspaceShared::is_shared_dynamic((void*)this)) {
3960       info_stream.print(" source: shared objects file (top)");
3961     } else {
3962       info_stream.print(" source: shared objects file");
3963     }
3964   }
3965 

















3966   msg.info("%s", info_stream.as_string());
3967 
3968   if (log_is_enabled(Debug, class, load)) {
3969     stringStream debug_stream;
3970 
3971     // Class hierarchy info
3972     debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
3973                        p2i(this),  p2i(superklass()));
3974 
3975     // Interfaces
3976     if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
3977       debug_stream.print(" interfaces:");
3978       int length = local_interfaces()->length();
3979       for (int i = 0; i < length; i++) {
3980         debug_stream.print(" " PTR_FORMAT,
3981                            p2i(InstanceKlass::cast(local_interfaces()->at(i))));
3982       }
3983     }
3984 
3985     // Class loader

  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/nativeStackPrinter.hpp"
 104 #include "utilities/pair.hpp"
 105 #include "utilities/stringUtils.hpp"
 106 #ifdef COMPILER1
 107 #include "c1/c1_Compiler.hpp"
 108 #endif
 109 #if INCLUDE_JFR
 110 #include "jfr/jfrEvents.hpp"
 111 #endif
 112 
 113 #ifdef DTRACE_ENABLED
 114 

 866   }
 867 
 868   if (log_is_enabled(Info, cds, init)) {
 869     ResourceMark rm;
 870     log_info(cds, init)("%s (aot-inited)", external_name());
 871   }
 872 
 873   link_class(CHECK);
 874 
 875 #ifdef ASSERT
 876   {
 877     Handle h_init_lock(THREAD, init_lock());
 878     ObjectLocker ol(h_init_lock, THREAD);
 879     assert(!is_initialized(), "sanity");
 880     assert(!is_being_initialized(), "sanity");
 881     assert(!is_in_error_state(), "sanity");
 882   }
 883 #endif
 884 
 885   set_init_thread(THREAD);

 886   set_initialization_state_and_notify(fully_initialized, CHECK);
 887   AOTClassInitializer::call_runtime_setup(THREAD, this);
 888 }
 889 #endif
 890 
 891 bool InstanceKlass::verify_code(TRAPS) {
 892   // 1) Verify the bytecodes
 893   return Verifier::verify(this, should_verify_class(), THREAD);
 894 }
 895 
 896 void InstanceKlass::link_class(TRAPS) {
 897   assert(is_loaded(), "must be loaded");
 898   if (!is_linked()) {
 899     link_class_impl(CHECK);
 900   }
 901 }
 902 
 903 // Called to verify that a class can link during initialization, without
 904 // throwing a VerifyError.
 905 bool InstanceKlass::link_class_or_fail(TRAPS) {
 906   assert(is_loaded(), "must be loaded");
 907   if (!is_linked()) {

1063   return true;
1064 }
1065 
1066 // Rewrite the byte codes of all of the methods of a class.
1067 // The rewriter must be called exactly once. Rewriting must happen after
1068 // verification but before the first method of the class is executed.
1069 void InstanceKlass::rewrite_class(TRAPS) {
1070   assert(is_loaded(), "must be loaded");
1071   if (is_rewritten()) {
1072     assert(is_shared(), "rewriting an unshared class?");
1073     return;
1074   }
1075   Rewriter::rewrite(this, CHECK);
1076   set_rewritten();
1077 }
1078 
1079 // Now relocate and link method entry points after class is rewritten.
1080 // This is outside is_rewritten flag. In case of an exception, it can be
1081 // executed more than once.
1082 void InstanceKlass::link_methods(TRAPS) {
1083   PerfTraceElapsedTime timer(ClassLoader::perf_ik_link_methods_time());
1084 
1085   int len = methods()->length();
1086   for (int i = len-1; i >= 0; i--) {
1087     methodHandle m(THREAD, methods()->at(i));
1088     RuntimeUpcalls::install_upcalls(m);
1089 
1090     // Set up method entry points for compiler and interpreter    .
1091     m->link_method(m, CHECK);
1092   }
1093 }
1094 
1095 // Eagerly initialize superinterfaces that declare default methods (concrete instance: any access)
1096 void InstanceKlass::initialize_super_interfaces(TRAPS) {
1097   assert (has_nonstatic_concrete_methods(), "caller should have checked this");
1098   for (int i = 0; i < local_interfaces()->length(); ++i) {
1099     InstanceKlass* ik = local_interfaces()->at(i);
1100 
1101     // Initialization is depth first search ie. we start with top of the inheritance tree
1102     // has_nonstatic_concrete_methods drives searching superinterfaces since it
1103     // means has_nonstatic_concrete_methods in its superinterface hierarchy
1104     if (ik->has_nonstatic_concrete_methods()) {
1105       ik->initialize_super_interfaces(CHECK);
1106     }
1107 
1108     // Only initialize() interfaces that "declare" concrete methods.

1173   assert_locked_or_safepoint(ClassInitError_lock);
1174   InitErrorTableCleaner cleaner;
1175   if (_initialization_error_table != nullptr) {
1176     _initialization_error_table->unlink(&cleaner);
1177   }
1178 }
1179 
1180 void InstanceKlass::initialize_impl(TRAPS) {
1181   HandleMark hm(THREAD);
1182 
1183   // Make sure klass is linked (verified) before initialization
1184   // A class could already be verified, since it has been reflected upon.
1185   link_class(CHECK);
1186 
1187   DTRACE_CLASSINIT_PROBE(required, -1);
1188 
1189   bool wait = false;
1190 
1191   JavaThread* jt = THREAD;
1192 
1193   if (ForceProfiling) {
1194     // Preallocate MDOs.
1195     for (int i = 0; i < methods()->length(); i++) {
1196       assert(!HAS_PENDING_EXCEPTION, "");
1197       methodHandle m(THREAD, methods()->at(i));
1198       Method::build_profiling_method_data(m, THREAD);
1199       if (HAS_PENDING_EXCEPTION) {
1200         ResourceMark rm;
1201         log_warning(cds)("MDO preallocation failed for %s", external_name());
1202         CLEAR_PENDING_EXCEPTION;
1203         break;
1204       }
1205     }
1206   }
1207 
1208   bool debug_logging_enabled = log_is_enabled(Debug, class, init);
1209 
1210   // refer to the JVM book page 47 for description of steps
1211   // Step 1
1212   {
1213     Handle h_init_lock(THREAD, init_lock());
1214     ObjectLocker ol(h_init_lock, jt);
1215 
1216     // Step 2
1217     // If we were to use wait() instead of waitInterruptibly() then
1218     // we might end up throwing IE from link/symbol resolution sites
1219     // that aren't expected to throw.  This would wreak havoc.  See 6320309.
1220     while (is_being_initialized() && !is_reentrant_initialization(jt)) {
1221       if (debug_logging_enabled) {
1222         ResourceMark rm(jt);
1223         log_debug(class, init)("Thread \"%s\" waiting for initialization of %s by thread \"%s\"",
1224                                jt->name(), external_name(), init_thread_name());
1225       }
1226       wait = true;
1227       jt->set_class_to_be_initialized(this);

1326       PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1327                                ClassLoader::perf_class_init_selftime(),
1328                                ClassLoader::perf_classes_inited(),
1329                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1330                                jt->get_thread_stat()->perf_timers_addr(),
1331                                PerfClassTraceTime::CLASS_CLINIT);
1332       call_class_initializer(THREAD);
1333     } else {
1334       // The elapsed time is so small it's not worth counting.
1335       if (UsePerfData) {
1336         ClassLoader::perf_classes_inited()->inc();
1337       }
1338       call_class_initializer(THREAD);
1339     }
1340   }
1341 
1342   // Step 9
1343   if (!HAS_PENDING_EXCEPTION) {
1344     set_initialization_state_and_notify(fully_initialized, CHECK);
1345     debug_only(vtable().verify(tty, true);)
1346     CompilationPolicy::replay_training_at_init(this, THREAD);
1347   }
1348   else {
1349     // Step 10 and 11
1350     Handle e(THREAD, PENDING_EXCEPTION);
1351     CLEAR_PENDING_EXCEPTION;
1352     // JVMTI has already reported the pending exception
1353     // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1354     JvmtiExport::clear_detected_exception(jt);
1355     {
1356       EXCEPTION_MARK;
1357       add_initialization_error(THREAD, e);
1358       set_initialization_state_and_notify(initialization_error, THREAD);
1359       CLEAR_PENDING_EXCEPTION;   // ignore any exception thrown, class initialization error is thrown below
1360       // JVMTI has already reported the pending exception
1361       // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1362       JvmtiExport::clear_detected_exception(jt);
1363     }
1364     DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1365     if (e->is_a(vmClasses::Error_klass())) {
1366       THROW_OOP(e());
1367     } else {
1368       JavaCallArguments args(e);
1369       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1370                 vmSymbols::throwable_void_signature(),
1371                 &args);
1372     }
1373   }
1374   DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1375 }
1376 

1377 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1378   Handle h_init_lock(THREAD, init_lock());
1379   if (h_init_lock() != nullptr) {
1380     ObjectLocker ol(h_init_lock, THREAD);
1381     set_init_thread(nullptr); // reset _init_thread before changing _init_state
1382     set_init_state(state);
1383     fence_and_clear_init_lock();
1384     ol.notify_all(CHECK);
1385   } else {
1386     assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1387     set_init_thread(nullptr); // reset _init_thread before changing _init_state
1388     set_init_state(state);
1389   }
1390 }
1391 
1392 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1393 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1394 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1395   assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1396 

1586     tty->print("Registered ");
1587     i->print_value_on(tty);
1588     tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1589   }
1590   instanceHandle h_i(THREAD, i);
1591   // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1592   JavaValue result(T_VOID);
1593   JavaCallArguments args(h_i);
1594   methodHandle mh(THREAD, Universe::finalizer_register_method());
1595   JavaCalls::call(&result, mh, &args, CHECK_NULL);
1596   MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1597   return h_i();
1598 }
1599 
1600 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1601   assert(!is_abstract() && !is_interface(), "Should not create this object");
1602   size_t size = size_helper();  // Query before forming handle.
1603   return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1604 }
1605 
1606 instanceOop InstanceKlass::allocate_instance(oop java_class,
1607                                              const char* who,
1608                                              TRAPS) {
1609   Klass* k = java_lang_Class::as_Klass(java_class);
1610   if (k == nullptr) {
1611     ResourceMark rm(THREAD);
1612     THROW_(vmSymbols::java_lang_InstantiationException(), nullptr);
1613   }
1614   InstanceKlass* ik = cast(k);
1615   ik->check_valid_for_instantiation(false, CHECK_NULL);
1616   ik->initialize(CHECK_NULL);
1617   return ik->allocate_instance(THREAD);
1618 }
1619 
1620 instanceHandle InstanceKlass::allocate_instance_handle(TRAPS) {
1621   return instanceHandle(THREAD, allocate_instance(THREAD));
1622 }
1623 
1624 void InstanceKlass::check_valid_for_instantiation(bool throwError, TRAPS) {
1625   if (is_interface() || is_abstract()) {
1626     ResourceMark rm(THREAD);
1627     THROW_MSG(throwError ? vmSymbols::java_lang_InstantiationError()
1628               : vmSymbols::java_lang_InstantiationException(), external_name());

1656 }
1657 
1658 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1659   // Need load-acquire for lock-free read
1660   ObjArrayKlass* oak = array_klasses_acquire();
1661   if (oak == nullptr) {
1662     return nullptr;
1663   } else {
1664     return oak->array_klass_or_null(n);
1665   }
1666 }
1667 
1668 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1669   return array_klass(1, THREAD);
1670 }
1671 
1672 ArrayKlass* InstanceKlass::array_klass_or_null() {
1673   return array_klass_or_null(1);
1674 }
1675 


1676 Method* InstanceKlass::class_initializer() const {
1677   Method* clinit = find_method(
1678       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1679   if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1680     return clinit;
1681   }
1682   return nullptr;
1683 }
1684 
1685 void InstanceKlass::call_class_initializer(TRAPS) {
1686   if (ReplayCompiles &&
1687       (ReplaySuppressInitializers == 1 ||
1688        (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1689     // Hide the existence of the initializer for the purpose of replaying the compile
1690     return;
1691   }
1692 
1693 #if INCLUDE_CDS
1694   // This is needed to ensure the consistency of the archived heap objects.
1695   if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1696     AOTClassInitializer::call_runtime_setup(THREAD, this);
1697     return;
1698   } else if (has_archived_enum_objs()) {
1699     assert(is_shared(), "must be");
1700     bool initialized = CDSEnumKlass::initialize_enum_klass(this, CHECK);
1701     if (initialized) {
1702       return;
1703     }
1704   }
1705 #endif
1706 
1707   methodHandle h_method(THREAD, class_initializer());
1708   assert(!is_initialized(), "we cannot initialize twice");
1709 
1710 #if 0
1711   // FIXME -- revive this code added to leyden/premain for <clinit> profiling
1712   int init_id = log_class_init(THREAD, this);
1713   if (h_method() != nullptr) {
1714     JavaCallArguments args; // No arguments
1715     JavaValue result(T_VOID);
1716     InstanceKlass* outer = THREAD->set_class_being_initialized(this);
1717     jlong bc_start = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1718 
1719     elapsedTimer timer;
1720     {
1721       PerfPauseTimer pt(THREAD->current_rt_call_timer(), THREAD->profile_rt_calls());
1722       PauseRuntimeCallProfiling prcp(THREAD, THREAD->profile_rt_calls());
1723 
1724       timer.start();
1725       JavaCalls::call(&result, h_method, &args, THREAD); // Static call (no args)
1726       timer.stop();
1727     }
1728 
1729     jlong bc_end = (CountBytecodesPerThread ? THREAD->bc_counter_value() : BytecodeCounter::counter_value());
1730 
1731     jlong bc_executed = (bc_end - bc_start);
1732     if (UsePerfData && outer == nullptr) { // outermost clinit
1733       THREAD->inc_clinit_bc_counter_value(bc_executed);
1734       ClassLoader::perf_class_init_bytecodes_count()->inc(bc_executed);
1735     }
1736 
1737     THREAD->set_class_being_initialized(outer);
1738 
1739     LogStreamHandle(Debug, init) log;
1740     if (log.is_enabled()) {
1741       ResourceMark rm(THREAD);
1742       log.print("%d Initialized in %.3fms (total: " JLONG_FORMAT "ms); ",
1743                 init_id, timer.seconds() * 1000.0, ClassLoader::class_init_time_ms());
1744       if (CountBytecodes || CountBytecodesPerThread) {
1745         log.print("executed " JLONG_FORMAT " bytecodes; ", bc_executed);
1746       }
1747       name()->print_value_on(&log);
1748       log.print_cr(" by thread " PTR_FORMAT " \"%s\" (" PTR_FORMAT ")",
1749                    p2i(THREAD), THREAD->name(), p2i(this));
1750     }
1751   }
1752   LogTarget(Info, class, init) lt;
1753   if (lt.is_enabled()) {
1754     ResourceMark rm(THREAD);
1755     LogStream ls(lt);
1756     ls.print("%d Initialized ", init_id);
1757     name()->print_value_on(&ls);
1758     ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1759                 h_method() == nullptr ? "(no method)" : "", p2i(this),
1760                 THREAD->name());
1761   }
1762 #else
1763   static int call_class_initializer_counter = 0;   // for debugging
1764   LogTarget(Info, class, init) lt;
1765   if (lt.is_enabled()) {
1766     ResourceMark rm(THREAD);
1767     LogStream ls(lt);
1768     ls.print("%d Initializing ", call_class_initializer_counter++);
1769     name()->print_value_on(&ls);
1770     ls.print_cr("%s (" PTR_FORMAT ") by thread \"%s\"",
1771                 h_method() == nullptr ? "(no method)" : "", p2i(this),
1772                 THREAD->name());
1773   }
1774   if (h_method() != nullptr) {
1775     ThreadInClassInitializer ticl(THREAD, this); // Track class being initialized
1776     JavaCallArguments args; // No arguments
1777     JavaValue result(T_VOID);
1778     JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1779   }
1780 #endif
1781 }
1782 
1783 // If a class that implements this interface is initialized, is the JVM required
1784 // to first execute a <clinit> method declared in this interface,
1785 // or (if also_check_supers==true) any of the super types of this interface?
1786 //
1787 // JVMS 5.5. Initialization, step 7: Next, if C is a class rather than
1788 // an interface, then let SC be its superclass and let SI1, ..., SIn
1789 // be all superinterfaces of C (whether direct or indirect) that
1790 // declare at least one non-abstract, non-static method.
1791 //
1792 // So when an interface is initialized, it does not look at its
1793 // supers. But a proper class will ensure that all of its supers have
1794 // run their <clinit> methods, except that it disregards interfaces
1795 // that lack a non-static concrete method (i.e., a default method).
1796 // Therefore, you should probably call this method only when the
1797 // current class is a super of some proper class, not an interface.
1798 bool InstanceKlass::interface_needs_clinit_execution_as_super(bool also_check_supers) const {
1799   assert(is_interface(), "must be");
1800 

2565     }
2566   }
2567   if (new_jmeths != 0) {
2568     Method::ensure_jmethod_ids(class_loader_data(), new_jmeths);
2569   }
2570 }
2571 
2572 // Lookup a jmethodID, null if not found.  Do no blocking, no allocations, no handles
2573 jmethodID InstanceKlass::jmethod_id_or_null(Method* method) {
2574   int idnum = method->method_idnum();
2575   jmethodID* jmeths = methods_jmethod_ids_acquire();
2576   return (jmeths != nullptr) ? jmeths[idnum + 1] : nullptr;
2577 }
2578 
2579 inline DependencyContext InstanceKlass::dependencies() {
2580   DependencyContext dep_context(&_dep_context, &_dep_context_last_cleaned);
2581   return dep_context;
2582 }
2583 
2584 void InstanceKlass::mark_dependent_nmethods(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
2585   dependencies().mark_dependent_nmethods(deopt_scope, changes, this);
2586 }
2587 
2588 void InstanceKlass::add_dependent_nmethod(nmethod* nm) {
2589   dependencies().add_dependent_nmethod(nm);
2590 }
2591 
2592 void InstanceKlass::clean_dependency_context() {
2593   dependencies().clean_unloading_dependents();
2594 }
2595 
2596 #ifndef PRODUCT
2597 void InstanceKlass::print_dependent_nmethods(bool verbose) {
2598   dependencies().print_dependent_nmethods(verbose);
2599 }
2600 
2601 bool InstanceKlass::is_dependent_nmethod(nmethod* nm) {
2602   return dependencies().is_dependent_nmethod(nm);
2603 }
2604 #endif //PRODUCT
2605 

2702     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2703       if (ioe->interface_klass() != nullptr) {
2704         it->push(ioe->interface_klass_addr());
2705         itableMethodEntry* ime = ioe->first_method_entry(this);
2706         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2707         for (int index = 0; index < n; index ++) {
2708           it->push(ime[index].method_addr());
2709         }
2710       }
2711     }
2712   }
2713 
2714   it->push(&_nest_host);
2715   it->push(&_nest_members);
2716   it->push(&_permitted_subclasses);
2717   it->push(&_record_components);
2718 }
2719 
2720 #if INCLUDE_CDS
2721 void InstanceKlass::remove_unshareable_info() {

2722   if (is_linked()) {
2723     assert(can_be_verified_at_dumptime(), "must be");
2724     // Remember this so we can avoid walking the hierarchy at runtime.
2725     set_verified_at_dump_time();
2726   }
2727   _misc_flags.set_has_init_deps_processed(false);
2728 
2729   Klass::remove_unshareable_info();
2730 
2731   if (SystemDictionaryShared::has_class_failed_verification(this)) {
2732     // Classes are attempted to link during dumping and may fail,
2733     // but these classes are still in the dictionary and class list in CLD.
2734     // If the class has failed verification, there is nothing else to remove.
2735     return;
2736   }
2737 
2738   // Reset to the 'allocated' state to prevent any premature accessing to
2739   // a shared class at runtime while the class is still being loaded and
2740   // restored. A class' init_state is set to 'loaded' at runtime when it's
2741   // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2742   _init_state = allocated;
2743 
2744   { // Otherwise this needs to take out the Compile_lock.
2745     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2746     init_implementor();
2747   }

2766   _osr_nmethods_head = nullptr;
2767 #if INCLUDE_JVMTI
2768   _breakpoints = nullptr;
2769   _previous_versions = nullptr;
2770   _cached_class_file = nullptr;
2771   _jvmti_cached_class_field_map = nullptr;
2772 #endif
2773 
2774   _init_thread = nullptr;
2775   _methods_jmethod_ids = nullptr;
2776   _jni_ids = nullptr;
2777   _oop_map_cache = nullptr;
2778   if (CDSConfig::is_dumping_invokedynamic() && HeapShared::is_lambda_proxy_klass(this)) {
2779     // keep _nest_host
2780   } else {
2781     // clear _nest_host to ensure re-load at runtime
2782     _nest_host = nullptr;
2783   }
2784   init_shared_package_entry();
2785   _dep_context_last_cleaned = 0;
2786   DEBUG_ONLY(_shared_class_load_count = 0);
2787 
2788   remove_unshareable_flags();
2789 }
2790 
2791 void InstanceKlass::remove_unshareable_flags() {
2792   // clear all the flags/stats that shouldn't be in the archived version
2793   assert(!is_scratch_class(), "must be");
2794   assert(!has_been_redefined(), "must be");
2795 #if INCLUDE_JVMTI
2796   set_is_being_redefined(false);
2797 #endif
2798   set_has_resolved_methods(false);
2799 }
2800 
2801 void InstanceKlass::remove_java_mirror() {
2802   Klass::remove_java_mirror();
2803 
2804   // do array classes also.
2805   if (array_klasses() != nullptr) {
2806     array_klasses()->remove_java_mirror();

2877 
2878   // restore constant pool resolved references
2879   constants()->restore_unshareable_info(CHECK);
2880 
2881   if (array_klasses() != nullptr) {
2882     // To get a consistent list of classes we need MultiArray_lock to ensure
2883     // array classes aren't observed while they are being restored.
2884     RecursiveLocker rl(MultiArray_lock, THREAD);
2885     assert(this == array_klasses()->bottom_klass(), "sanity");
2886     // Array classes have null protection domain.
2887     // --> see ArrayKlass::complete_create_array_klass()
2888     array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2889   }
2890 
2891   // Initialize @ValueBased class annotation if not already set in the archived klass.
2892   if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2893     set_is_value_based();
2894   }
2895 }
2896 





2897 bool InstanceKlass::can_be_verified_at_dumptime() const {
2898   if (CDSConfig::preserve_all_dumptime_verification_states(this)) {
2899     return true;
2900   }
2901 
2902   if (MetaspaceShared::is_in_shared_metaspace(this)) {
2903     // This is a class that was dumped into the base archive, so we know
2904     // it was verified at dump time.
2905     return true;
2906   }
2907 
2908   // Check if a class or any of its supertypes has a version older than 50.
2909   // CDS will not perform verification of old classes during dump time because
2910   // without changing the old verifier, the verification constraint cannot be
2911   // retrieved during dump time.
2912   // Verification of archived old classes will be performed during run time.
2913   if (major_version() < 50 /*JAVA_6_VERSION*/) {
2914     return false;
2915   }
2916   if (java_super() != nullptr && !java_super()->can_be_verified_at_dumptime()) {
2917     return false;
2918   }
2919   Array<InstanceKlass*>* interfaces = local_interfaces();
2920   int len = interfaces->length();
2921   for (int i = 0; i < len; i++) {
2922     if (!interfaces->at(i)->can_be_verified_at_dumptime()) {
2923       return false;
2924     }
2925   }
2926   return true;
2927 }
2928 
2929 int InstanceKlass::shared_class_loader_type() const {
2930   if (is_shared_boot_class()) {
2931     return ClassLoader::BOOT_LOADER;
2932   } else if (is_shared_platform_class()) {
2933     return ClassLoader::PLATFORM_LOADER;
2934   } else if (is_shared_app_class()) {
2935     return ClassLoader::APP_LOADER;
2936   } else {
2937     return ClassLoader::OTHER;
2938   }
2939 }
2940 
2941 #endif // INCLUDE_CDS
2942 
2943 #if INCLUDE_JVMTI
2944 static void clear_all_breakpoints(Method* m) {
2945   m->clear_all_breakpoints();
2946 }
2947 #endif
2948 
2949 void InstanceKlass::unload_class(InstanceKlass* ik) {
2950 
2951   if (ik->is_scratch_class()) {
2952     assert(ik->dependencies().is_empty(), "dependencies should be empty for scratch classes");
2953     return;
2954   }
2955   assert(ik->is_loaded(), "class should be loaded " PTR_FORMAT, p2i(ik));
2956 
2957   // Release dependencies.
2958   ik->dependencies().remove_all_dependents();
2959 
2960   // notify the debugger

3699 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3700   for (int i = 0; i < len; i++) {
3701     intptr_t e = start[i];
3702     st->print("%d : " INTPTR_FORMAT, i, e);
3703     if (MetaspaceObj::is_valid((Metadata*)e)) {
3704       st->print(" ");
3705       ((Metadata*)e)->print_value_on(st);
3706     }
3707     st->cr();
3708   }
3709 }
3710 
3711 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3712   return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3713 }
3714 
3715 const char* InstanceKlass::init_state_name() const {
3716   return state_names[init_state()];
3717 }
3718 
3719 const char* InstanceKlass::state2name(ClassState s) {
3720   return state_names[s];
3721 }
3722 
3723 void InstanceKlass::print_on(outputStream* st) const {
3724   assert(is_klass(), "must be klass");
3725   Klass::print_on(st);
3726 
3727   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3728   st->print(BULLET"klass size:        %d", size());                               st->cr();
3729   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();
3730   st->print(BULLET"flags:             "); _misc_flags.print_on(st);               st->cr();
3731   st->print(BULLET"state:             "); st->print_cr("%s", init_state_name());
3732   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
3733   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3734   st->print(BULLET"sub:               ");
3735   Klass* sub = subklass();
3736   int n;
3737   for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3738     if (n < MaxSubklassPrintSize) {
3739       sub->print_value_on(st);
3740       st->print("   ");
3741     }
3742   }

4032         nullptr;
4033       // caller can be null, for example, during a JVMTI VM_Init hook
4034       if (caller != nullptr) {
4035         info_stream.print(" source: instance of %s", caller->external_name());
4036       } else {
4037         // source is unknown
4038       }
4039     } else {
4040       oop class_loader = loader_data->class_loader();
4041       info_stream.print(" source: %s", class_loader->klass()->external_name());
4042     }
4043   } else {
4044     assert(this->is_shared(), "must be");
4045     if (MetaspaceShared::is_shared_dynamic((void*)this)) {
4046       info_stream.print(" source: shared objects file (top)");
4047     } else {
4048       info_stream.print(" source: shared objects file");
4049     }
4050   }
4051 
4052   info_stream.print(" loader:");
4053   if (is_shared()) {
4054     info_stream.print(" %s", SystemDictionaryShared::class_loader_name_for_shared((Klass*)this));
4055   } else if (loader_data == ClassLoaderData::the_null_class_loader_data()) {
4056     info_stream.print(" boot_loader");
4057   } else {
4058     oop class_loader = loader_data->class_loader();
4059     if (class_loader != nullptr) {
4060       info_stream.print(" %s", class_loader->klass()->external_name());
4061       oop cl_name_and_id = java_lang_ClassLoader::nameAndId(class_loader);
4062       if (cl_name_and_id != nullptr) {
4063         info_stream.print(" %s", java_lang_String::as_utf8_string(cl_name_and_id));
4064       }
4065     } else {
4066       info_stream.print(" null");
4067     }
4068   }
4069   msg.info("%s", info_stream.as_string());
4070 
4071   if (log_is_enabled(Debug, class, load)) {
4072     stringStream debug_stream;
4073 
4074     // Class hierarchy info
4075     debug_stream.print(" klass: " PTR_FORMAT " super: " PTR_FORMAT,
4076                        p2i(this),  p2i(superklass()));
4077 
4078     // Interfaces
4079     if (local_interfaces() != nullptr && local_interfaces()->length() > 0) {
4080       debug_stream.print(" interfaces:");
4081       int length = local_interfaces()->length();
4082       for (int i = 0; i < length; i++) {
4083         debug_stream.print(" " PTR_FORMAT,
4084                            p2i(InstanceKlass::cast(local_interfaces()->at(i))));
4085       }
4086     }
4087 
4088     // Class loader
< prev index next >