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"
68 #include "oops/instanceMirrorKlass.hpp"
69 #include "oops/instanceOop.hpp"
70 #include "oops/instanceStackChunkKlass.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/atomic.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"
132 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait) \
133 { \
134 char* data = nullptr; \
135 int len = 0; \
136 Symbol* clss_name = name(); \
137 if (clss_name != nullptr) { \
138 data = (char*)clss_name->bytes(); \
139 len = clss_name->utf8_length(); \
140 } \
141 HOTSPOT_CLASS_INITIALIZATION_##type( \
142 data, len, (void*)class_loader(), thread_type, wait); \
143 }
144
145 #else // ndef DTRACE_ENABLED
146
147 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
148 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
149
150 #endif // ndef DTRACE_ENABLED
151
152 bool InstanceKlass::_finalization_enabled = true;
153
154 static inline bool is_class_loader(const Symbol* class_name,
155 const ClassFileParser& parser) {
156 assert(class_name != nullptr, "invariant");
157
158 if (class_name == vmSymbols::java_lang_ClassLoader()) {
159 return true;
160 }
161
162 if (vmClasses::ClassLoader_klass_loaded()) {
163 const Klass* const super_klass = parser.super_klass();
164 if (super_klass != nullptr) {
165 if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
166 return true;
167 }
168 }
169 }
170 return false;
171 }
172
173 static inline bool is_stack_chunk_class(const Symbol* class_name,
174 const ClassLoaderData* loader_data) {
175 return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
176 loader_data->is_the_null_class_loader_data());
177 }
178
179 // private: called to verify that k is a static member of this nest.
180 // We know that k is an instance class in the same package and hence the
181 // same classloader.
182 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
183 assert(!is_hidden(), "unexpected hidden class");
184 if (_nest_members == nullptr || _nest_members == Universe::the_empty_short_array()) {
185 if (log_is_enabled(Trace, class, nestmates)) {
186 ResourceMark rm(current);
187 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
188 k->external_name(), this->external_name());
189 }
190 return false;
191 }
192
447 }
448
449 const char* InstanceKlass::nest_host_error() {
450 if (_nest_host_index == 0) {
451 return nullptr;
452 } else {
453 constantPoolHandle cph(Thread::current(), constants());
454 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
455 }
456 }
457
458 void* InstanceKlass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size,
459 bool use_class_space, TRAPS) throw() {
460 return Metaspace::allocate(loader_data, word_size, ClassType, use_class_space, THREAD);
461 }
462
463 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
464 const int size = InstanceKlass::size(parser.vtable_size(),
465 parser.itable_size(),
466 nonstatic_oop_map_size(parser.total_oop_map_count()),
467 parser.is_interface());
468
469 const Symbol* const class_name = parser.class_name();
470 assert(class_name != nullptr, "invariant");
471 ClassLoaderData* loader_data = parser.loader_data();
472 assert(loader_data != nullptr, "invariant");
473
474 InstanceKlass* ik;
475 const bool use_class_space = UseClassMetaspaceForAllClasses || parser.klass_needs_narrow_id();
476
477 // Allocation
478 if (parser.is_instance_ref_klass()) {
479 // java.lang.ref.Reference
480 ik = new (loader_data, size, use_class_space, THREAD) InstanceRefKlass(parser);
481 } else if (class_name == vmSymbols::java_lang_Class()) {
482 // mirror - java.lang.Class
483 ik = new (loader_data, size, use_class_space, THREAD) InstanceMirrorKlass(parser);
484 } else if (is_stack_chunk_class(class_name, loader_data)) {
485 // stack chunk
486 ik = new (loader_data, size, use_class_space, THREAD) InstanceStackChunkKlass(parser);
487 } else if (is_class_loader(class_name, parser)) {
488 // class loader - java.lang.ClassLoader
489 ik = new (loader_data, size, use_class_space, THREAD) InstanceClassLoaderKlass(parser);
490 } else {
491 // normal
492 ik = new (loader_data, size, use_class_space, THREAD) InstanceKlass(parser);
493 }
494
495 if (ik != nullptr && UseCompressedClassPointers && use_class_space) {
496 assert(CompressedKlassPointers::is_encodable(ik),
497 "Klass " PTR_FORMAT "needs a narrow Klass ID, but is not encodable", p2i(ik));
498 }
499
500 // Check for pending exception before adding to the loader data and incrementing
501 // class count. Can get OOM here.
502 if (HAS_PENDING_EXCEPTION) {
503 return nullptr;
504 }
505
506 return ik;
507 }
508
509
510 // copy method ordering from resource area to Metaspace
511 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
512 if (m != nullptr) {
513 // allocate a new array and copy contents (memcpy?)
514 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
515 for (int i = 0; i < m->length(); i++) {
516 _method_ordering->at_put(i, m->at(i));
517 }
518 } else {
519 _method_ordering = Universe::the_empty_int_array();
520 }
521 }
522
523 // create a new array of vtable_indices for default methods
524 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
525 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
526 assert(default_vtable_indices() == nullptr, "only create once");
527 set_default_vtable_indices(vtable_indices);
528 return vtable_indices;
529 }
530
531
532 InstanceKlass::InstanceKlass() {
533 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
534 }
535
536 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
537 Klass(kind),
538 _nest_members(nullptr),
539 _nest_host(nullptr),
540 _permitted_subclasses(nullptr),
541 _record_components(nullptr),
542 _static_field_size(parser.static_field_size()),
543 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
544 _itable_len(parser.itable_size()),
545 _nest_host_index(0),
546 _init_state(allocated),
547 _reference_type(reference_type),
548 _init_thread(nullptr)
549 {
550 set_vtable_length(parser.vtable_size());
551 set_access_flags(parser.access_flags());
552 if (parser.is_hidden()) set_is_hidden();
553 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
554 false));
555
556 assert(nullptr == _methods, "underlying memory not zeroed?");
557 assert(is_instance_klass(), "is layout incorrect?");
558 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
559 }
560
561 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
562 Array<Method*>* methods) {
563 if (methods != nullptr && methods != Universe::the_empty_method_array() &&
564 !methods->is_shared()) {
565 for (int i = 0; i < methods->length(); i++) {
566 Method* method = methods->at(i);
567 if (method == nullptr) continue; // maybe null if error processing
568 // Only want to delete methods that are not executing for RedefineClasses.
569 // The previous version will point to them so they're not totally dangling
570 assert (!method->on_stack(), "shouldn't be called with methods on stack");
571 MetadataFactory::free_metadata(loader_data, method);
572 }
573 MetadataFactory::free_array<Method*>(loader_data, methods);
574 }
679
680 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
681 set_transitive_interfaces(nullptr);
682 set_local_interfaces(nullptr);
683
684 if (fieldinfo_stream() != nullptr && !fieldinfo_stream()->is_shared()) {
685 MetadataFactory::free_array<u1>(loader_data, fieldinfo_stream());
686 }
687 set_fieldinfo_stream(nullptr);
688
689 if (fieldinfo_search_table() != nullptr && !fieldinfo_search_table()->is_shared()) {
690 MetadataFactory::free_array<u1>(loader_data, fieldinfo_search_table());
691 }
692 set_fieldinfo_search_table(nullptr);
693
694 if (fields_status() != nullptr && !fields_status()->is_shared()) {
695 MetadataFactory::free_array<FieldStatus>(loader_data, fields_status());
696 }
697 set_fields_status(nullptr);
698
699 // If a method from a redefined class is using this constant pool, don't
700 // delete it, yet. The new class's previous version will point to this.
701 if (constants() != nullptr) {
702 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
703 if (!constants()->is_shared()) {
704 MetadataFactory::free_metadata(loader_data, constants());
705 }
706 // Delete any cached resolution errors for the constant pool
707 SystemDictionary::delete_resolution_error(constants());
708
709 set_constants(nullptr);
710 }
711
712 if (inner_classes() != nullptr &&
713 inner_classes() != Universe::the_empty_short_array() &&
714 !inner_classes()->is_shared()) {
715 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
716 }
717 set_inner_classes(nullptr);
718
719 if (nest_members() != nullptr &&
720 nest_members() != Universe::the_empty_short_array() &&
721 !nest_members()->is_shared()) {
722 MetadataFactory::free_array<jushort>(loader_data, nest_members());
723 }
724 set_nest_members(nullptr);
725
726 if (permitted_subclasses() != nullptr &&
727 permitted_subclasses() != Universe::the_empty_short_array() &&
728 !permitted_subclasses()->is_shared()) {
729 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
730 }
731 set_permitted_subclasses(nullptr);
732
733 // We should deallocate the Annotations instance if it's not in shared spaces.
734 if (annotations() != nullptr && !annotations()->is_shared()) {
735 MetadataFactory::free_metadata(loader_data, annotations());
736 }
737 set_annotations(nullptr);
738
739 SystemDictionaryShared::handle_class_unloading(this);
740
741 #if INCLUDE_CDS_JAVA_HEAP
742 if (CDSConfig::is_dumping_heap()) {
743 HeapShared::remove_scratch_objects(this);
744 }
745 #endif
746 }
747
748 bool InstanceKlass::is_record() const {
749 return _record_components != nullptr &&
750 is_final() &&
751 java_super() == vmClasses::Record_klass();
752 }
881 #ifdef ASSERT
882 {
883 Handle h_init_lock(THREAD, init_lock());
884 ObjectLocker ol(h_init_lock, THREAD);
885 assert(!is_initialized(), "sanity");
886 assert(!is_being_initialized(), "sanity");
887 assert(!is_in_error_state(), "sanity");
888 }
889 #endif
890
891 set_init_thread(THREAD);
892 set_initialization_state_and_notify(fully_initialized, CHECK);
893 }
894 #endif
895
896 bool InstanceKlass::verify_code(TRAPS) {
897 // 1) Verify the bytecodes
898 return Verifier::verify(this, should_verify_class(), THREAD);
899 }
900
901 void InstanceKlass::link_class(TRAPS) {
902 assert(is_loaded(), "must be loaded");
903 if (!is_linked()) {
904 link_class_impl(CHECK);
905 }
906 }
907
908 // Called to verify that a class can link during initialization, without
909 // throwing a VerifyError.
910 bool InstanceKlass::link_class_or_fail(TRAPS) {
911 assert(is_loaded(), "must be loaded");
912 if (!is_linked()) {
913 link_class_impl(CHECK_false);
914 }
915 return is_linked();
916 }
917
918 bool InstanceKlass::link_class_impl(TRAPS) {
919 if (CDSConfig::is_dumping_static_archive() && SystemDictionaryShared::has_class_failed_verification(this)) {
920 // This is for CDS static dump only -- we use the in_error_state to indicate that
952 vmSymbols::java_lang_IncompatibleClassChangeError(),
953 "class %s has interface %s as super class",
954 external_name(),
955 super_klass->external_name()
956 );
957 return false;
958 }
959
960 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
961 ik_super->link_class_impl(CHECK_false);
962 }
963
964 // link all interfaces implemented by this class before linking this class
965 Array<InstanceKlass*>* interfaces = local_interfaces();
966 int num_interfaces = interfaces->length();
967 for (int index = 0; index < num_interfaces; index++) {
968 InstanceKlass* interk = interfaces->at(index);
969 interk->link_class_impl(CHECK_false);
970 }
971
972 // in case the class is linked in the process of linking its superclasses
973 if (is_linked()) {
974 return true;
975 }
976
977 // trace only the link time for this klass that includes
978 // the verification time
979 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
980 ClassLoader::perf_class_link_selftime(),
981 ClassLoader::perf_classes_linked(),
982 jt->get_thread_stat()->perf_recursion_counts_addr(),
983 jt->get_thread_stat()->perf_timers_addr(),
984 PerfClassTraceTime::CLASS_LINK);
985
986 // verification & rewriting
987 {
988 HandleMark hm(THREAD);
989 Handle h_init_lock(THREAD, init_lock());
990 ObjectLocker ol(h_init_lock, jt);
991 // rewritten will have been set if loader constraint error found
1256 ss.print("Could not initialize class %s", external_name());
1257 if (cause.is_null()) {
1258 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1259 } else {
1260 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1261 ss.as_string(), cause);
1262 }
1263 } else {
1264
1265 // Step 6
1266 set_init_state(being_initialized);
1267 set_init_thread(jt);
1268 if (debug_logging_enabled) {
1269 ResourceMark rm(jt);
1270 log_debug(class, init)("Thread \"%s\" is initializing %s",
1271 jt->name(), external_name());
1272 }
1273 }
1274 }
1275
1276 // Step 7
1277 // Next, if C is a class rather than an interface, initialize it's super class and super
1278 // interfaces.
1279 if (!is_interface()) {
1280 Klass* super_klass = super();
1281 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1282 super_klass->initialize(THREAD);
1283 }
1284 // If C implements any interface that declares a non-static, concrete method,
1285 // the initialization of C triggers initialization of its super interfaces.
1286 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1287 // having a superinterface that declares, non-static, concrete methods
1288 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1289 initialize_super_interfaces(THREAD);
1290 }
1291
1292 // If any exceptions, complete abruptly, throwing the same exception as above.
1293 if (HAS_PENDING_EXCEPTION) {
1294 Handle e(THREAD, PENDING_EXCEPTION);
1295 CLEAR_PENDING_EXCEPTION;
1296 {
1297 EXCEPTION_MARK;
1298 add_initialization_error(THREAD, e);
1299 // Locks object, set state, and notify all waiting threads
1300 set_initialization_state_and_notify(initialization_error, THREAD);
1301 CLEAR_PENDING_EXCEPTION;
1302 }
1303 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1304 THROW_OOP(e());
1305 }
1306 }
1307
1308
1309 // Step 8
1310 {
1311 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1312 if (class_initializer() != nullptr) {
1313 // Timer includes any side effects of class initialization (resolution,
1314 // etc), but not recursive entry into call_class_initializer().
1315 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1316 ClassLoader::perf_class_init_selftime(),
1317 ClassLoader::perf_classes_inited(),
1318 jt->get_thread_stat()->perf_recursion_counts_addr(),
1319 jt->get_thread_stat()->perf_timers_addr(),
1320 PerfClassTraceTime::CLASS_CLINIT);
1321 call_class_initializer(THREAD);
1322 } else {
1323 // The elapsed time is so small it's not worth counting.
1324 if (UsePerfData) {
1325 ClassLoader::perf_classes_inited()->inc();
1326 }
1327 call_class_initializer(THREAD);
1328 }
1329 }
1330
1331 // Step 9
1332 if (!HAS_PENDING_EXCEPTION) {
1333 set_initialization_state_and_notify(fully_initialized, CHECK);
1334 DEBUG_ONLY(vtable().verify(tty, true);)
1335 CompilationPolicy::replay_training_at_init(this, THREAD);
1336 }
1337 else {
1338 // Step 10 and 11
1339 Handle e(THREAD, PENDING_EXCEPTION);
1340 CLEAR_PENDING_EXCEPTION;
1341 // JVMTI has already reported the pending exception
1342 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1343 JvmtiExport::clear_detected_exception(jt);
1344 {
1345 EXCEPTION_MARK;
1346 add_initialization_error(THREAD, e);
1347 set_initialization_state_and_notify(initialization_error, THREAD);
1348 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1362 }
1363 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1364 }
1365
1366
1367 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1368 Handle h_init_lock(THREAD, init_lock());
1369 if (h_init_lock() != nullptr) {
1370 ObjectLocker ol(h_init_lock, THREAD);
1371 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1372 set_init_state(state);
1373 fence_and_clear_init_lock();
1374 ol.notify_all(CHECK);
1375 } else {
1376 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1377 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1378 set_init_state(state);
1379 }
1380 }
1381
1382 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1383 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1384 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1385 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1386
1387 DeoptimizationScope deopt_scope;
1388 {
1389 MutexLocker ml(current, Compile_lock);
1390
1391 set_init_state(InstanceKlass::loaded);
1392 // make sure init_state store is already done.
1393 // The compiler reads the hierarchy outside of the Compile_lock.
1394 // Access ordering is used to add to hierarchy.
1395
1396 // Link into hierarchy.
1397 append_to_sibling_list(); // add to superklass/sibling list
1398 process_interfaces(); // handle all "implements" declarations
1399
1400 // Now mark all code that depended on old class hierarchy.
1401 // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
1545 for (int i = 0; i < transitive_interfaces()->length(); i++) {
1546 if (transitive_interfaces()->at(i) == k) {
1547 return true;
1548 }
1549 }
1550 return false;
1551 }
1552
1553 bool InstanceKlass::is_same_or_direct_interface(Klass *k) const {
1554 // Verify direct super interface
1555 if (this == k) return true;
1556 assert(k->is_interface(), "should be an interface class");
1557 for (int i = 0; i < local_interfaces()->length(); i++) {
1558 if (local_interfaces()->at(i) == k) {
1559 return true;
1560 }
1561 }
1562 return false;
1563 }
1564
1565 objArrayOop InstanceKlass::allocate_objArray(int n, int length, TRAPS) {
1566 check_array_allocation_length(length, arrayOopDesc::max_array_length(T_OBJECT), CHECK_NULL);
1567 size_t size = objArrayOopDesc::object_size(length);
1568 ArrayKlass* ak = array_klass(n, CHECK_NULL);
1569 objArrayOop o = (objArrayOop)Universe::heap()->array_allocate(ak, size, length,
1570 /* do_zero */ true, CHECK_NULL);
1571 return o;
1572 }
1573
1574 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
1575 if (TraceFinalizerRegistration) {
1576 tty->print("Registered ");
1577 i->print_value_on(tty);
1578 tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1579 }
1580 instanceHandle h_i(THREAD, i);
1581 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1582 JavaValue result(T_VOID);
1583 JavaCallArguments args(h_i);
1584 methodHandle mh(THREAD, Universe::finalizer_register_method());
1585 JavaCalls::call(&result, mh, &args, CHECK_NULL);
1586 MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1587 return h_i();
1588 }
1589
1590 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1591 assert(!is_abstract() && !is_interface(), "Should not create this object");
1592 size_t size = size_helper(); // Query before forming handle.
1593 return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1621 : vmSymbols::java_lang_IllegalAccessException(), external_name());
1622 }
1623 }
1624
1625 ArrayKlass* InstanceKlass::array_klass(int n, TRAPS) {
1626 // Need load-acquire for lock-free read
1627 if (array_klasses_acquire() == nullptr) {
1628
1629 // Recursively lock array allocation
1630 RecursiveLocker rl(MultiArray_lock, THREAD);
1631
1632 // Check if another thread created the array klass while we were waiting for the lock.
1633 if (array_klasses() == nullptr) {
1634 ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
1635 // use 'release' to pair with lock-free load
1636 release_set_array_klasses(k);
1637 }
1638 }
1639
1640 // array_klasses() will always be set at this point
1641 ObjArrayKlass* ak = array_klasses();
1642 assert(ak != nullptr, "should be set");
1643 return ak->array_klass(n, THREAD);
1644 }
1645
1646 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1647 // Need load-acquire for lock-free read
1648 ObjArrayKlass* oak = array_klasses_acquire();
1649 if (oak == nullptr) {
1650 return nullptr;
1651 } else {
1652 return oak->array_klass_or_null(n);
1653 }
1654 }
1655
1656 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1657 return array_klass(1, THREAD);
1658 }
1659
1660 ArrayKlass* InstanceKlass::array_klass_or_null() {
1661 return array_klass_or_null(1);
1662 }
1663
1664 static int call_class_initializer_counter = 0; // for debugging
1665
1666 Method* InstanceKlass::class_initializer() const {
1667 Method* clinit = find_method(
1668 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1669 if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1670 return clinit;
1671 }
1672 return nullptr;
1673 }
1674
1675 void InstanceKlass::call_class_initializer(TRAPS) {
1676 if (ReplayCompiles &&
1677 (ReplaySuppressInitializers == 1 ||
1678 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1679 // Hide the existence of the initializer for the purpose of replaying the compile
1680 return;
1681 }
1682
1683 #if INCLUDE_CDS
1684 // This is needed to ensure the consistency of the archived heap objects.
1685 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1686 AOTClassInitializer::call_runtime_setup(THREAD, this);
1687 return;
1688 } else if (has_archived_enum_objs()) {
1689 assert(is_shared(), "must be");
1758
1759 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1760 InterpreterOopMap* entry_for) {
1761 // Lazily create the _oop_map_cache at first request.
1762 // Load_acquire is needed to safely get instance published with CAS by another thread.
1763 OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1764 if (oop_map_cache == nullptr) {
1765 // Try to install new instance atomically.
1766 oop_map_cache = new OopMapCache();
1767 OopMapCache* other = Atomic::cmpxchg(&_oop_map_cache, (OopMapCache*)nullptr, oop_map_cache);
1768 if (other != nullptr) {
1769 // Someone else managed to install before us, ditch local copy and use the existing one.
1770 delete oop_map_cache;
1771 oop_map_cache = other;
1772 }
1773 }
1774 // _oop_map_cache is constant after init; lookup below does its own locking.
1775 oop_map_cache->lookup(method, bci, entry_for);
1776 }
1777
1778 bool InstanceKlass::contains_field_offset(int offset) {
1779 fieldDescriptor fd;
1780 return find_field_from_offset(offset, false, &fd);
1781 }
1782
1783 FieldInfo InstanceKlass::field(int index) const {
1784 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1785 if (fs.index() == index) {
1786 return fs.to_FieldInfo();
1787 }
1788 }
1789 fatal("Field not found");
1790 return FieldInfo();
1791 }
1792
1793 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1794 JavaFieldStream fs(this);
1795 if (fs.lookup(name, sig)) {
1796 assert(fs.name() == name, "name must match");
1797 assert(fs.signature() == sig, "signature must match");
1798 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
1799 return true;
1800 }
1801 return false;
1842
1843 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1844 // search order according to newest JVM spec (5.4.3.2, p.167).
1845 // 1) search for field in current klass
1846 if (find_local_field(name, sig, fd)) {
1847 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1848 }
1849 // 2) search for field recursively in direct superinterfaces
1850 if (is_static) {
1851 Klass* intf = find_interface_field(name, sig, fd);
1852 if (intf != nullptr) return intf;
1853 }
1854 // 3) apply field lookup recursively if superclass exists
1855 { Klass* supr = super();
1856 if (supr != nullptr) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1857 }
1858 // 4) otherwise field lookup fails
1859 return nullptr;
1860 }
1861
1862
1863 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1864 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1865 if (fs.offset() == offset) {
1866 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
1867 if (fd->is_static() == is_static) return true;
1868 }
1869 }
1870 return false;
1871 }
1872
1873
1874 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1875 Klass* klass = const_cast<InstanceKlass*>(this);
1876 while (klass != nullptr) {
1877 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1878 return true;
1879 }
1880 klass = klass->super();
1881 }
2225 }
2226
2227 // uncached_lookup_method searches both the local class methods array and all
2228 // superclasses methods arrays, skipping any overpass methods in superclasses,
2229 // and possibly skipping private methods.
2230 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2231 const Symbol* signature,
2232 OverpassLookupMode overpass_mode,
2233 PrivateLookupMode private_mode) const {
2234 OverpassLookupMode overpass_local_mode = overpass_mode;
2235 const Klass* klass = this;
2236 while (klass != nullptr) {
2237 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2238 signature,
2239 overpass_local_mode,
2240 StaticLookupMode::find,
2241 private_mode);
2242 if (method != nullptr) {
2243 return method;
2244 }
2245 klass = klass->super();
2246 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2247 }
2248 return nullptr;
2249 }
2250
2251 #ifdef ASSERT
2252 // search through class hierarchy and return true if this class or
2253 // one of the superclasses was redefined
2254 bool InstanceKlass::has_redefined_this_or_super() const {
2255 const Klass* klass = this;
2256 while (klass != nullptr) {
2257 if (InstanceKlass::cast(klass)->has_been_redefined()) {
2258 return true;
2259 }
2260 klass = klass->super();
2261 }
2262 return false;
2263 }
2264 #endif
2637 int itable_offset_in_words = (int)(start_of_itable() - (intptr_t*)this);
2638
2639 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words)
2640 / itableOffsetEntry::size();
2641
2642 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2643 if (ioe->interface_klass() != nullptr) {
2644 it->push(ioe->interface_klass_addr());
2645 itableMethodEntry* ime = ioe->first_method_entry(this);
2646 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2647 for (int index = 0; index < n; index ++) {
2648 it->push(ime[index].method_addr());
2649 }
2650 }
2651 }
2652 }
2653
2654 it->push(&_nest_host);
2655 it->push(&_nest_members);
2656 it->push(&_permitted_subclasses);
2657 it->push(&_record_components);
2658 }
2659
2660 #if INCLUDE_CDS
2661 void InstanceKlass::remove_unshareable_info() {
2662
2663 if (is_linked()) {
2664 assert(can_be_verified_at_dumptime(), "must be");
2665 // Remember this so we can avoid walking the hierarchy at runtime.
2666 set_verified_at_dump_time();
2667 }
2668
2669 _misc_flags.set_has_init_deps_processed(false);
2670
2671 Klass::remove_unshareable_info();
2672
2673 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2674 // Classes are attempted to link during dumping and may fail,
2675 // but these classes are still in the dictionary and class list in CLD.
2676 // If the class has failed verification, there is nothing else to remove.
2677 return;
2685
2686 { // Otherwise this needs to take out the Compile_lock.
2687 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2688 init_implementor();
2689 }
2690
2691 // Call remove_unshareable_info() on other objects that belong to this class, except
2692 // for constants()->remove_unshareable_info(), which is called in a separate pass in
2693 // ArchiveBuilder::make_klasses_shareable(),
2694
2695 for (int i = 0; i < methods()->length(); i++) {
2696 Method* m = methods()->at(i);
2697 m->remove_unshareable_info();
2698 }
2699
2700 // do array classes also.
2701 if (array_klasses() != nullptr) {
2702 array_klasses()->remove_unshareable_info();
2703 }
2704
2705 // These are not allocated from metaspace. They are safe to set to null.
2706 _source_debug_extension = nullptr;
2707 _dep_context = nullptr;
2708 _osr_nmethods_head = nullptr;
2709 #if INCLUDE_JVMTI
2710 _breakpoints = nullptr;
2711 _previous_versions = nullptr;
2712 _cached_class_file = nullptr;
2713 _jvmti_cached_class_field_map = nullptr;
2714 #endif
2715
2716 _init_thread = nullptr;
2717 _methods_jmethod_ids = nullptr;
2718 _jni_ids = nullptr;
2719 _oop_map_cache = nullptr;
2720 if (CDSConfig::is_dumping_method_handles() && HeapShared::is_lambda_proxy_klass(this)) {
2721 // keep _nest_host
2722 } else {
2723 // clear _nest_host to ensure re-load at runtime
2724 _nest_host = nullptr;
2725 }
2776 void InstanceKlass::compute_has_loops_flag_for_methods() {
2777 Array<Method*>* methods = this->methods();
2778 for (int index = 0; index < methods->length(); ++index) {
2779 Method* m = methods->at(index);
2780 if (!m->is_overpass()) { // work around JDK-8305771
2781 m->compute_has_loops_flag();
2782 }
2783 }
2784 }
2785
2786 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2787 PackageEntry* pkg_entry, TRAPS) {
2788 // InstanceKlass::add_to_hierarchy() sets the init_state to loaded
2789 // before the InstanceKlass is added to the SystemDictionary. Make
2790 // sure the current state is <loaded.
2791 assert(!is_loaded(), "invalid init state");
2792 assert(!shared_loading_failed(), "Must not try to load failed class again");
2793 set_package(loader_data, pkg_entry, CHECK);
2794 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2795
2796 Array<Method*>* methods = this->methods();
2797 int num_methods = methods->length();
2798 for (int index = 0; index < num_methods; ++index) {
2799 methods->at(index)->restore_unshareable_info(CHECK);
2800 }
2801 #if INCLUDE_JVMTI
2802 if (JvmtiExport::has_redefined_a_class()) {
2803 // Reinitialize vtable because RedefineClasses may have changed some
2804 // entries in this vtable for super classes so the CDS vtable might
2805 // point to old or obsolete entries. RedefineClasses doesn't fix up
2806 // vtables in the shared system dictionary, only the main one.
2807 // It also redefines the itable too so fix that too.
2808 // First fix any default methods that point to a super class that may
2809 // have been redefined.
2810 bool trace_name_printed = false;
2811 adjust_default_methods(&trace_name_printed);
2812 if (verified_at_dump_time()) {
2813 // Initialize vtable and itable for classes which can be verified at dump time.
2814 // Unlinked classes such as old classes with major version < 50 cannot be verified
2815 // at dump time.
2816 vtable().initialize_vtable();
2817 itable().initialize_itable();
2818 }
2819 }
2820 #endif // INCLUDE_JVMTI
2821
2822 // restore constant pool resolved references
2823 constants()->restore_unshareable_info(CHECK);
2824
2825 if (array_klasses() != nullptr) {
2826 // To get a consistent list of classes we need MultiArray_lock to ensure
2827 // array classes aren't observed while they are being restored.
2828 RecursiveLocker rl(MultiArray_lock, THREAD);
2829 assert(this == array_klasses()->bottom_klass(), "sanity");
2830 // Array classes have null protection domain.
2831 // --> see ArrayKlass::complete_create_array_klass()
2832 array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
2833 }
2834
2835 // Initialize @ValueBased class annotation if not already set in the archived klass.
2836 if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
2837 set_is_value_based();
2838 }
2839
2840 DEBUG_ONLY(FieldInfoStream::validate_search_table(_constants, _fieldinfo_stream, _fieldinfo_search_table));
2841 }
2842
2843 // Check if a class or any of its supertypes has a version older than 50.
2844 // CDS will not perform verification of old classes during dump time because
2845 // without changing the old verifier, the verification constraint cannot be
2846 // retrieved during dump time.
2847 // Verification of archived old classes will be performed during run time.
2848 bool InstanceKlass::can_be_verified_at_dumptime() const {
2849 if (MetaspaceShared::is_in_shared_metaspace(this)) {
2850 // This is a class that was dumped into the base archive, so we know
2851 // it was verified at dump time.
2965 constants()->release_C_heap_structures();
2966 }
2967 }
2968
2969 // The constant pool is on stack if any of the methods are executing or
2970 // referenced by handles.
2971 bool InstanceKlass::on_stack() const {
2972 return _constants->on_stack();
2973 }
2974
2975 Symbol* InstanceKlass::source_file_name() const { return _constants->source_file_name(); }
2976 u2 InstanceKlass::source_file_name_index() const { return _constants->source_file_name_index(); }
2977 void InstanceKlass::set_source_file_name_index(u2 sourcefile_index) { _constants->set_source_file_name_index(sourcefile_index); }
2978
2979 // minor and major version numbers of class file
2980 u2 InstanceKlass::minor_version() const { return _constants->minor_version(); }
2981 void InstanceKlass::set_minor_version(u2 minor_version) { _constants->set_minor_version(minor_version); }
2982 u2 InstanceKlass::major_version() const { return _constants->major_version(); }
2983 void InstanceKlass::set_major_version(u2 major_version) { _constants->set_major_version(major_version); }
2984
2985 const InstanceKlass* InstanceKlass::get_klass_version(int version) const {
2986 for (const InstanceKlass* ik = this; ik != nullptr; ik = ik->previous_versions()) {
2987 if (ik->constants()->version() == version) {
2988 return ik;
2989 }
2990 }
2991 return nullptr;
2992 }
2993
2994 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2995 if (array == nullptr) {
2996 _source_debug_extension = nullptr;
2997 } else {
2998 // Adding one to the attribute length in order to store a null terminator
2999 // character could cause an overflow because the attribute length is
3000 // already coded with an u4 in the classfile, but in practice, it's
3001 // unlikely to happen.
3002 assert((length+1) > length, "Overflow checking");
3003 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
3004 for (int i = 0; i < length; i++) {
3005 sde[i] = array[i];
3006 }
3007 sde[length] = '\0';
3008 _source_debug_extension = sde;
3009 }
3010 }
3011
3012 Symbol* InstanceKlass::generic_signature() const { return _constants->generic_signature(); }
3013 u2 InstanceKlass::generic_signature_index() const { return _constants->generic_signature_index(); }
3014 void InstanceKlass::set_generic_signature_index(u2 sig_index) { _constants->set_generic_signature_index(sig_index); }
3015
3016 const char* InstanceKlass::signature_name() const {
3017
3018 // Get the internal name as a c string
3019 const char* src = (const char*) (name()->as_C_string());
3020 const int src_length = (int)strlen(src);
3021
3022 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
3023
3024 // Add L as type indicator
3025 int dest_index = 0;
3026 dest[dest_index++] = JVM_SIGNATURE_CLASS;
3027
3028 // Add the actual class name
3029 for (int src_index = 0; src_index < src_length; ) {
3030 dest[dest_index++] = src[src_index++];
3031 }
3032
3033 if (is_hidden()) { // Replace the last '+' with a '.'.
3034 for (int index = (int)src_length; index > 0; index--) {
3035 if (dest[index] == '+') {
3036 dest[index] = JVM_SIGNATURE_DOT;
3037 break;
3038 }
3039 }
3040 }
3041
3042 // Add the semicolon and the null
3043 dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
3044 dest[dest_index] = '\0';
3045 return dest;
3046 }
3287 bool InstanceKlass::find_inner_classes_attr(int* ooff, int* noff, TRAPS) const {
3288 constantPoolHandle i_cp(THREAD, constants());
3289 for (InnerClassesIterator iter(this); !iter.done(); iter.next()) {
3290 int ioff = iter.inner_class_info_index();
3291 if (ioff != 0) {
3292 // Check to see if the name matches the class we're looking for
3293 // before attempting to find the class.
3294 if (i_cp->klass_name_at_matches(this, ioff)) {
3295 Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
3296 if (this == inner_klass) {
3297 *ooff = iter.outer_class_info_index();
3298 *noff = iter.inner_name_index();
3299 return true;
3300 }
3301 }
3302 }
3303 }
3304 return false;
3305 }
3306
3307 InstanceKlass* InstanceKlass::compute_enclosing_class(bool* inner_is_member, TRAPS) const {
3308 InstanceKlass* outer_klass = nullptr;
3309 *inner_is_member = false;
3310 int ooff = 0, noff = 0;
3311 bool has_inner_classes_attr = find_inner_classes_attr(&ooff, &noff, THREAD);
3312 if (has_inner_classes_attr) {
3313 constantPoolHandle i_cp(THREAD, constants());
3314 if (ooff != 0) {
3315 Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
3316 if (!ok->is_instance_klass()) {
3317 // If the outer class is not an instance klass then it cannot have
3318 // declared any inner classes.
3319 ResourceMark rm(THREAD);
3320 // Names are all known to be < 64k so we know this formatted message is not excessively large.
3321 Exceptions::fthrow(
3322 THREAD_AND_LOCATION,
3323 vmSymbols::java_lang_IncompatibleClassChangeError(),
3324 "%s and %s disagree on InnerClasses attribute",
3325 ok->external_name(),
3326 external_name());
3353 u2 InstanceKlass::compute_modifier_flags() const {
3354 u2 access = access_flags().as_unsigned_short();
3355
3356 // But check if it happens to be member class.
3357 InnerClassesIterator iter(this);
3358 for (; !iter.done(); iter.next()) {
3359 int ioff = iter.inner_class_info_index();
3360 // Inner class attribute can be zero, skip it.
3361 // Strange but true: JVM spec. allows null inner class refs.
3362 if (ioff == 0) continue;
3363
3364 // only look at classes that are already loaded
3365 // since we are looking for the flags for our self.
3366 Symbol* inner_name = constants()->klass_name_at(ioff);
3367 if (name() == inner_name) {
3368 // This is really a member class.
3369 access = iter.inner_access_flags();
3370 break;
3371 }
3372 }
3373 // Remember to strip ACC_SUPER bit
3374 return (access & (~JVM_ACC_SUPER));
3375 }
3376
3377 jint InstanceKlass::jvmti_class_status() const {
3378 jint result = 0;
3379
3380 if (is_linked()) {
3381 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3382 }
3383
3384 if (is_initialized()) {
3385 assert(is_linked(), "Class status is not consistent");
3386 result |= JVMTI_CLASS_STATUS_INITIALIZED;
3387 }
3388 if (is_in_error_state()) {
3389 result |= JVMTI_CLASS_STATUS_ERROR;
3390 }
3391 return result;
3392 }
3393
3394 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {
3608 }
3609 osr = osr->osr_link();
3610 }
3611
3612 assert(match_level == false || best == nullptr, "shouldn't pick up anything if match_level is set");
3613 if (best != nullptr && best->comp_level() >= comp_level) {
3614 return best;
3615 }
3616 return nullptr;
3617 }
3618
3619 // -----------------------------------------------------------------------------------------------------
3620 // Printing
3621
3622 #define BULLET " - "
3623
3624 static const char* state_names[] = {
3625 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3626 };
3627
3628 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3629 for (int i = 0; i < len; i++) {
3630 intptr_t e = start[i];
3631 st->print("%d : " INTPTR_FORMAT, i, e);
3632 if (MetaspaceObj::is_valid((Metadata*)e)) {
3633 st->print(" ");
3634 ((Metadata*)e)->print_value_on(st);
3635 }
3636 st->cr();
3637 }
3638 }
3639
3640 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3641 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3642 }
3643
3644 const char* InstanceKlass::init_state_name() const {
3645 return state_names[init_state()];
3646 }
3647
3648 void InstanceKlass::print_on(outputStream* st) const {
3649 assert(is_klass(), "must be klass");
3650 Klass::print_on(st);
3651
3652 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3653 st->print(BULLET"klass size: %d", size()); st->cr();
3654 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3655 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3656 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3657 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3658 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3659 st->print(BULLET"sub: ");
3660 Klass* sub = subklass();
3661 int n;
3662 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3663 if (n < MaxSubklassPrintSize) {
3664 sub->print_value_on(st);
3665 st->print(" ");
3666 }
3667 }
3668 if (n >= MaxSubklassPrintSize) st->print("(%zd more klasses...)", n - MaxSubklassPrintSize);
3669 st->cr();
3670
3671 if (is_interface()) {
3672 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3673 if (nof_implementors() == 1) {
3674 st->print_cr(BULLET"implementor: ");
3675 st->print(" ");
3676 implementor()->print_value_on(st);
3677 st->cr();
3678 }
3679 }
3680
3681 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3682 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
3683 if (Verbose || WizardMode) {
3684 Array<Method*>* method_array = methods();
3685 for (int i = 0; i < method_array->length(); i++) {
3686 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3687 }
3688 }
3689 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
3690 if (default_methods() != nullptr) {
3691 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
3692 if (Verbose) {
3693 Array<Method*>* method_array = default_methods();
3694 for (int i = 0; i < method_array->length(); i++) {
3695 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3696 }
3697 }
3698 }
3699 print_on_maybe_null(st, BULLET"default vtable indices: ", default_vtable_indices());
3700 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
3701 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3702
3703 st->print(BULLET"secondary supers: "); secondary_supers()->print_value_on(st); st->cr();
3704
3705 st->print(BULLET"hash_slot: %d", hash_slot()); st->cr();
3706 st->print(BULLET"secondary bitmap: " UINTX_FORMAT_X_0, _secondary_supers_bitmap); st->cr();
3707
3708 if (secondary_supers() != nullptr) {
3709 if (Verbose) {
3710 bool is_hashed = (_secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_FULL);
3711 st->print_cr(BULLET"---- secondary supers (%d words):", _secondary_supers->length());
3712 for (int i = 0; i < _secondary_supers->length(); i++) {
3713 ResourceMark rm; // for external_name()
3714 Klass* secondary_super = _secondary_supers->at(i);
3715 st->print(BULLET"%2d:", i);
3716 if (is_hashed) {
3717 int home_slot = compute_home_slot(secondary_super, _secondary_supers_bitmap);
3737 print_on_maybe_null(st, BULLET"field type annotations: ", fields_type_annotations());
3738 {
3739 bool have_pv = false;
3740 // previous versions are linked together through the InstanceKlass
3741 for (InstanceKlass* pv_node = previous_versions();
3742 pv_node != nullptr;
3743 pv_node = pv_node->previous_versions()) {
3744 if (!have_pv)
3745 st->print(BULLET"previous version: ");
3746 have_pv = true;
3747 pv_node->constants()->print_value_on(st);
3748 }
3749 if (have_pv) st->cr();
3750 }
3751
3752 print_on_maybe_null(st, BULLET"generic signature: ", generic_signature());
3753 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
3754 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
3755 print_on_maybe_null(st, BULLET"record components: ", record_components());
3756 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
3757 if (java_mirror() != nullptr) {
3758 st->print(BULLET"java mirror: ");
3759 java_mirror()->print_value_on(st);
3760 st->cr();
3761 } else {
3762 st->print_cr(BULLET"java mirror: null");
3763 }
3764 st->print(BULLET"vtable length %d (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3765 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
3766 st->print(BULLET"itable length %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3767 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
3768 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3769
3770 FieldPrinter print_static_field(st);
3771 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3772 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3773 FieldPrinter print_nonstatic_field(st);
3774 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3775 ik->print_nonstatic_fields(&print_nonstatic_field);
3776
3777 st->print(BULLET"non-static oop maps (%d entries): ", nonstatic_oop_map_count());
3778 OopMapBlock* map = start_of_nonstatic_oop_maps();
3779 OopMapBlock* end_map = map + nonstatic_oop_map_count();
3780 while (map < end_map) {
3781 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3782 map++;
3783 }
3784 st->cr();
3785
3786 if (fieldinfo_search_table() != nullptr) {
3787 st->print_cr(BULLET"---- field info search table:");
3788 FieldInfoStream::print_search_table(st, _constants, _fieldinfo_stream, _fieldinfo_search_table);
3789 }
3790 }
3791
3792 void InstanceKlass::print_value_on(outputStream* st) const {
3793 assert(is_klass(), "must be klass");
3794 if (Verbose || WizardMode) access_flags().print_on(st);
3795 name()->print_value_on(st);
3796 }
3797
3798 void FieldPrinter::do_field(fieldDescriptor* fd) {
3799 _st->print(BULLET);
3800 if (_obj == nullptr) {
3801 fd->print_on(_st);
3802 _st->cr();
3803 } else {
3804 fd->print_on_for(_st, _obj);
3805 _st->cr();
3806 }
3807 }
3808
3809
3810 void InstanceKlass::oop_print_on(oop obj, outputStream* st) {
3811 Klass::oop_print_on(obj, st);
3812
3813 if (this == vmClasses::String_klass()) {
3814 typeArrayOop value = java_lang_String::value(obj);
3815 juint length = java_lang_String::length(obj);
3816 if (value != nullptr &&
3817 value->is_typeArray() &&
3818 length <= (juint) value->length()) {
3819 st->print(BULLET"string: ");
3820 java_lang_String::print(obj, st);
3821 st->cr();
3822 }
3823 }
3824
3825 st->print_cr(BULLET"---- fields (total size %zu words):", oop_size(obj));
3826 FieldPrinter print_field(st, obj);
3827 print_nonstatic_fields(&print_field);
3828
3829 if (this == vmClasses::Class_klass()) {
3830 st->print(BULLET"signature: ");
3831 java_lang_Class::print_signature(obj, st);
3832 st->cr();
3833 Klass* real_klass = java_lang_Class::as_Klass(obj);
3834 if (real_klass != nullptr && real_klass->is_instance_klass()) {
3835 st->print_cr(BULLET"---- static fields (%d):", java_lang_Class::static_oop_field_count(obj));
3836 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
3837 }
3838 } else if (this == vmClasses::MethodType_klass()) {
3839 st->print(BULLET"signature: ");
3840 java_lang_invoke_MethodType::print_signature(obj, st);
3841 st->cr();
3842 }
3843 }
3844
3845 #ifndef PRODUCT
3846
|
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/inlineKlass.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/markWord.hpp"
74 #include "oops/method.hpp"
75 #include "oops/oop.inline.hpp"
76 #include "oops/recordComponent.hpp"
77 #include "oops/refArrayKlass.hpp"
78 #include "oops/symbol.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/atomic.hpp"
85 #include "runtime/deoptimization.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/synchronizer.hpp"
95 #include "runtime/threads.hpp"
96 #include "services/classLoadingService.hpp"
97 #include "services/finalizerService.hpp"
135 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait) \
136 { \
137 char* data = nullptr; \
138 int len = 0; \
139 Symbol* clss_name = name(); \
140 if (clss_name != nullptr) { \
141 data = (char*)clss_name->bytes(); \
142 len = clss_name->utf8_length(); \
143 } \
144 HOTSPOT_CLASS_INITIALIZATION_##type( \
145 data, len, (void*)class_loader(), thread_type, wait); \
146 }
147
148 #else // ndef DTRACE_ENABLED
149
150 #define DTRACE_CLASSINIT_PROBE(type, thread_type)
151 #define DTRACE_CLASSINIT_PROBE_WAIT(type, thread_type, wait)
152
153 #endif // ndef DTRACE_ENABLED
154
155 void InlineLayoutInfo::metaspace_pointers_do(MetaspaceClosure* it) {
156 log_trace(cds)("Iter(InlineFieldInfo): %p", this);
157 it->push(&_klass);
158 }
159
160 bool InstanceKlass::_finalization_enabled = true;
161
162 static inline bool is_class_loader(const Symbol* class_name,
163 const ClassFileParser& parser) {
164 assert(class_name != nullptr, "invariant");
165
166 if (class_name == vmSymbols::java_lang_ClassLoader()) {
167 return true;
168 }
169
170 if (vmClasses::ClassLoader_klass_loaded()) {
171 const Klass* const super_klass = parser.super_klass();
172 if (super_klass != nullptr) {
173 if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
174 return true;
175 }
176 }
177 }
178 return false;
179 }
180
181 bool InstanceKlass::field_is_null_free_inline_type(int index) const {
182 return field(index).field_flags().is_null_free_inline_type();
183 }
184
185 bool InstanceKlass::is_class_in_loadable_descriptors_attribute(Symbol* name) const {
186 if (_loadable_descriptors == nullptr) return false;
187 for (int i = 0; i < _loadable_descriptors->length(); i++) {
188 Symbol* class_name = _constants->symbol_at(_loadable_descriptors->at(i));
189 if (class_name == name) return true;
190 }
191 return false;
192 }
193
194 static inline bool is_stack_chunk_class(const Symbol* class_name,
195 const ClassLoaderData* loader_data) {
196 return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
197 loader_data->is_the_null_class_loader_data());
198 }
199
200 // private: called to verify that k is a static member of this nest.
201 // We know that k is an instance class in the same package and hence the
202 // same classloader.
203 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
204 assert(!is_hidden(), "unexpected hidden class");
205 if (_nest_members == nullptr || _nest_members == Universe::the_empty_short_array()) {
206 if (log_is_enabled(Trace, class, nestmates)) {
207 ResourceMark rm(current);
208 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
209 k->external_name(), this->external_name());
210 }
211 return false;
212 }
213
468 }
469
470 const char* InstanceKlass::nest_host_error() {
471 if (_nest_host_index == 0) {
472 return nullptr;
473 } else {
474 constantPoolHandle cph(Thread::current(), constants());
475 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
476 }
477 }
478
479 void* InstanceKlass::operator new(size_t size, ClassLoaderData* loader_data, size_t word_size,
480 bool use_class_space, TRAPS) throw() {
481 return Metaspace::allocate(loader_data, word_size, ClassType, use_class_space, THREAD);
482 }
483
484 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
485 const int size = InstanceKlass::size(parser.vtable_size(),
486 parser.itable_size(),
487 nonstatic_oop_map_size(parser.total_oop_map_count()),
488 parser.is_interface(),
489 parser.is_inline_type());
490
491 const Symbol* const class_name = parser.class_name();
492 assert(class_name != nullptr, "invariant");
493 ClassLoaderData* loader_data = parser.loader_data();
494 assert(loader_data != nullptr, "invariant");
495
496 InstanceKlass* ik;
497 const bool use_class_space = UseClassMetaspaceForAllClasses || parser.klass_needs_narrow_id();
498
499 // Allocation
500 if (parser.is_instance_ref_klass()) {
501 // java.lang.ref.Reference
502 ik = new (loader_data, size, use_class_space, THREAD) InstanceRefKlass(parser);
503 } else if (class_name == vmSymbols::java_lang_Class()) {
504 // mirror - java.lang.Class
505 ik = new (loader_data, size, use_class_space, THREAD) InstanceMirrorKlass(parser);
506 } else if (is_stack_chunk_class(class_name, loader_data)) {
507 // stack chunk
508 ik = new (loader_data, size, use_class_space, THREAD) InstanceStackChunkKlass(parser);
509 } else if (is_class_loader(class_name, parser)) {
510 // class loader - java.lang.ClassLoader
511 ik = new (loader_data, size, use_class_space, THREAD) InstanceClassLoaderKlass(parser);
512 } else if (parser.is_inline_type()) {
513 // inline type
514 ik = new (loader_data, size, use_class_space, THREAD) InlineKlass(parser);
515 } else {
516 // normal
517 ik = new (loader_data, size, use_class_space, THREAD) InstanceKlass(parser);
518 }
519
520 if (ik != nullptr && UseCompressedClassPointers && use_class_space) {
521 assert(CompressedKlassPointers::is_encodable(ik),
522 "Klass " PTR_FORMAT "needs a narrow Klass ID, but is not encodable", p2i(ik));
523 }
524
525 // Check for pending exception before adding to the loader data and incrementing
526 // class count. Can get OOM here.
527 if (HAS_PENDING_EXCEPTION) {
528 return nullptr;
529 }
530
531 #ifdef ASSERT
532 ik->bounds_check((address) ik->start_of_vtable(), false, size);
533 ik->bounds_check((address) ik->start_of_itable(), false, size);
534 ik->bounds_check((address) ik->end_of_itable(), true, size);
535 ik->bounds_check((address) ik->end_of_nonstatic_oop_maps(), true, size);
536 #endif //ASSERT
537 return ik;
538 }
539
540 #ifndef PRODUCT
541 bool InstanceKlass::bounds_check(address addr, bool edge_ok, intptr_t size_in_bytes) const {
542 const char* bad = nullptr;
543 address end = nullptr;
544 if (addr < (address)this) {
545 bad = "before";
546 } else if (addr == (address)this) {
547 if (edge_ok) return true;
548 bad = "just before";
549 } else if (addr == (end = (address)this + sizeof(intptr_t) * (size_in_bytes < 0 ? size() : size_in_bytes))) {
550 if (edge_ok) return true;
551 bad = "just after";
552 } else if (addr > end) {
553 bad = "after";
554 } else {
555 return true;
556 }
557 tty->print_cr("%s object bounds: " INTPTR_FORMAT " [" INTPTR_FORMAT ".." INTPTR_FORMAT "]",
558 bad, (intptr_t)addr, (intptr_t)this, (intptr_t)end);
559 Verbose = WizardMode = true; this->print(); //@@
560 return false;
561 }
562 #endif //PRODUCT
563
564 // copy method ordering from resource area to Metaspace
565 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
566 if (m != nullptr) {
567 // allocate a new array and copy contents (memcpy?)
568 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
569 for (int i = 0; i < m->length(); i++) {
570 _method_ordering->at_put(i, m->at(i));
571 }
572 } else {
573 _method_ordering = Universe::the_empty_int_array();
574 }
575 }
576
577 // create a new array of vtable_indices for default methods
578 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
579 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
580 assert(default_vtable_indices() == nullptr, "only create once");
581 set_default_vtable_indices(vtable_indices);
582 return vtable_indices;
583 }
584
585
586 InstanceKlass::InstanceKlass() {
587 assert(CDSConfig::is_dumping_static_archive() || CDSConfig::is_using_archive(), "only for CDS");
588 }
589
590 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, markWord prototype_header, ReferenceType reference_type) :
591 Klass(kind, prototype_header),
592 _nest_members(nullptr),
593 _nest_host(nullptr),
594 _permitted_subclasses(nullptr),
595 _record_components(nullptr),
596 _static_field_size(parser.static_field_size()),
597 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
598 _itable_len(parser.itable_size()),
599 _nest_host_index(0),
600 _init_state(allocated),
601 _reference_type(reference_type),
602 _init_thread(nullptr),
603 _inline_layout_info_array(nullptr),
604 _loadable_descriptors(nullptr),
605 _adr_inlineklass_fixed_block(nullptr)
606 {
607 set_vtable_length(parser.vtable_size());
608 set_access_flags(parser.access_flags());
609 if (parser.is_hidden()) set_is_hidden();
610 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
611 false));
612 if (parser.has_inline_fields()) {
613 set_has_inline_type_fields();
614 }
615
616 assert(nullptr == _methods, "underlying memory not zeroed?");
617 assert(is_instance_klass(), "is layout incorrect?");
618 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
619 }
620
621 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
622 Array<Method*>* methods) {
623 if (methods != nullptr && methods != Universe::the_empty_method_array() &&
624 !methods->is_shared()) {
625 for (int i = 0; i < methods->length(); i++) {
626 Method* method = methods->at(i);
627 if (method == nullptr) continue; // maybe null if error processing
628 // Only want to delete methods that are not executing for RedefineClasses.
629 // The previous version will point to them so they're not totally dangling
630 assert (!method->on_stack(), "shouldn't be called with methods on stack");
631 MetadataFactory::free_metadata(loader_data, method);
632 }
633 MetadataFactory::free_array<Method*>(loader_data, methods);
634 }
739
740 deallocate_interfaces(loader_data, super(), local_interfaces(), transitive_interfaces());
741 set_transitive_interfaces(nullptr);
742 set_local_interfaces(nullptr);
743
744 if (fieldinfo_stream() != nullptr && !fieldinfo_stream()->is_shared()) {
745 MetadataFactory::free_array<u1>(loader_data, fieldinfo_stream());
746 }
747 set_fieldinfo_stream(nullptr);
748
749 if (fieldinfo_search_table() != nullptr && !fieldinfo_search_table()->is_shared()) {
750 MetadataFactory::free_array<u1>(loader_data, fieldinfo_search_table());
751 }
752 set_fieldinfo_search_table(nullptr);
753
754 if (fields_status() != nullptr && !fields_status()->is_shared()) {
755 MetadataFactory::free_array<FieldStatus>(loader_data, fields_status());
756 }
757 set_fields_status(nullptr);
758
759 if (inline_layout_info_array() != nullptr) {
760 MetadataFactory::free_array<InlineLayoutInfo>(loader_data, inline_layout_info_array());
761 }
762 set_inline_layout_info_array(nullptr);
763
764 // If a method from a redefined class is using this constant pool, don't
765 // delete it, yet. The new class's previous version will point to this.
766 if (constants() != nullptr) {
767 assert (!constants()->on_stack(), "shouldn't be called if anything is onstack");
768 if (!constants()->is_shared()) {
769 MetadataFactory::free_metadata(loader_data, constants());
770 }
771 // Delete any cached resolution errors for the constant pool
772 SystemDictionary::delete_resolution_error(constants());
773
774 set_constants(nullptr);
775 }
776
777 if (inner_classes() != nullptr &&
778 inner_classes() != Universe::the_empty_short_array() &&
779 !inner_classes()->is_shared()) {
780 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
781 }
782 set_inner_classes(nullptr);
783
784 if (nest_members() != nullptr &&
785 nest_members() != Universe::the_empty_short_array() &&
786 !nest_members()->is_shared()) {
787 MetadataFactory::free_array<jushort>(loader_data, nest_members());
788 }
789 set_nest_members(nullptr);
790
791 if (permitted_subclasses() != nullptr &&
792 permitted_subclasses() != Universe::the_empty_short_array() &&
793 !permitted_subclasses()->is_shared()) {
794 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
795 }
796 set_permitted_subclasses(nullptr);
797
798 if (loadable_descriptors() != nullptr &&
799 loadable_descriptors() != Universe::the_empty_short_array() &&
800 !loadable_descriptors()->is_shared()) {
801 MetadataFactory::free_array<jushort>(loader_data, loadable_descriptors());
802 }
803 set_loadable_descriptors(nullptr);
804
805 // We should deallocate the Annotations instance if it's not in shared spaces.
806 if (annotations() != nullptr && !annotations()->is_shared()) {
807 MetadataFactory::free_metadata(loader_data, annotations());
808 }
809 set_annotations(nullptr);
810
811 SystemDictionaryShared::handle_class_unloading(this);
812
813 #if INCLUDE_CDS_JAVA_HEAP
814 if (CDSConfig::is_dumping_heap()) {
815 HeapShared::remove_scratch_objects(this);
816 }
817 #endif
818 }
819
820 bool InstanceKlass::is_record() const {
821 return _record_components != nullptr &&
822 is_final() &&
823 java_super() == vmClasses::Record_klass();
824 }
953 #ifdef ASSERT
954 {
955 Handle h_init_lock(THREAD, init_lock());
956 ObjectLocker ol(h_init_lock, THREAD);
957 assert(!is_initialized(), "sanity");
958 assert(!is_being_initialized(), "sanity");
959 assert(!is_in_error_state(), "sanity");
960 }
961 #endif
962
963 set_init_thread(THREAD);
964 set_initialization_state_and_notify(fully_initialized, CHECK);
965 }
966 #endif
967
968 bool InstanceKlass::verify_code(TRAPS) {
969 // 1) Verify the bytecodes
970 return Verifier::verify(this, should_verify_class(), THREAD);
971 }
972
973 static void load_classes_from_loadable_descriptors_attribute(InstanceKlass *ik, TRAPS) {
974 ResourceMark rm(THREAD);
975 if (ik->loadable_descriptors() != nullptr && PreloadClasses) {
976 HandleMark hm(THREAD);
977 for (int i = 0; i < ik->loadable_descriptors()->length(); i++) {
978 Symbol* sig = ik->constants()->symbol_at(ik->loadable_descriptors()->at(i));
979 if (!Signature::has_envelope(sig)) continue;
980 TempNewSymbol class_name = Signature::strip_envelope(sig);
981 if (class_name == ik->name()) continue;
982 log_info(class, preload)("Preloading of class %s during linking of class %s "
983 "because of the class is listed in the LoadableDescriptors attribute",
984 sig->as_C_string(), ik->name()->as_C_string());
985 oop loader = ik->class_loader();
986 Klass* klass = SystemDictionary::resolve_or_null(class_name,
987 Handle(THREAD, loader), THREAD);
988 if (HAS_PENDING_EXCEPTION) {
989 CLEAR_PENDING_EXCEPTION;
990 }
991 if (klass != nullptr) {
992 log_info(class, preload)("Preloading of class %s during linking of class %s "
993 "(cause: LoadableDescriptors attribute) succeeded",
994 class_name->as_C_string(), ik->name()->as_C_string());
995 if (!klass->is_inline_klass()) {
996 // Non value class are allowed by the current spec, but it could be an indication
997 // of an issue so let's log a warning
998 log_warning(class, preload)("Preloading of class %s during linking of class %s "
999 "(cause: LoadableDescriptors attribute) but loaded class is not a value class",
1000 class_name->as_C_string(), ik->name()->as_C_string());
1001 }
1002 } else {
1003 log_warning(class, preload)("Preloading of class %s during linking of class %s "
1004 "(cause: LoadableDescriptors attribute) failed",
1005 class_name->as_C_string(), ik->name()->as_C_string());
1006 }
1007 }
1008 }
1009 }
1010
1011 void InstanceKlass::link_class(TRAPS) {
1012 assert(is_loaded(), "must be loaded");
1013 if (!is_linked()) {
1014 link_class_impl(CHECK);
1015 }
1016 }
1017
1018 // Called to verify that a class can link during initialization, without
1019 // throwing a VerifyError.
1020 bool InstanceKlass::link_class_or_fail(TRAPS) {
1021 assert(is_loaded(), "must be loaded");
1022 if (!is_linked()) {
1023 link_class_impl(CHECK_false);
1024 }
1025 return is_linked();
1026 }
1027
1028 bool InstanceKlass::link_class_impl(TRAPS) {
1029 if (CDSConfig::is_dumping_static_archive() && SystemDictionaryShared::has_class_failed_verification(this)) {
1030 // This is for CDS static dump only -- we use the in_error_state to indicate that
1062 vmSymbols::java_lang_IncompatibleClassChangeError(),
1063 "class %s has interface %s as super class",
1064 external_name(),
1065 super_klass->external_name()
1066 );
1067 return false;
1068 }
1069
1070 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
1071 ik_super->link_class_impl(CHECK_false);
1072 }
1073
1074 // link all interfaces implemented by this class before linking this class
1075 Array<InstanceKlass*>* interfaces = local_interfaces();
1076 int num_interfaces = interfaces->length();
1077 for (int index = 0; index < num_interfaces; index++) {
1078 InstanceKlass* interk = interfaces->at(index);
1079 interk->link_class_impl(CHECK_false);
1080 }
1081
1082 if (EnableValhalla) {
1083 // Aggressively preloading all classes from the LoadableDescriptors attribute
1084 // so inline classes can be scalarized in the calling conventions computed below
1085 load_classes_from_loadable_descriptors_attribute(this, THREAD);
1086 assert(!HAS_PENDING_EXCEPTION, "Shouldn't have pending exceptions from call above");
1087 }
1088
1089 // in case the class is linked in the process of linking its superclasses
1090 if (is_linked()) {
1091 return true;
1092 }
1093
1094 // trace only the link time for this klass that includes
1095 // the verification time
1096 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
1097 ClassLoader::perf_class_link_selftime(),
1098 ClassLoader::perf_classes_linked(),
1099 jt->get_thread_stat()->perf_recursion_counts_addr(),
1100 jt->get_thread_stat()->perf_timers_addr(),
1101 PerfClassTraceTime::CLASS_LINK);
1102
1103 // verification & rewriting
1104 {
1105 HandleMark hm(THREAD);
1106 Handle h_init_lock(THREAD, init_lock());
1107 ObjectLocker ol(h_init_lock, jt);
1108 // rewritten will have been set if loader constraint error found
1373 ss.print("Could not initialize class %s", external_name());
1374 if (cause.is_null()) {
1375 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1376 } else {
1377 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1378 ss.as_string(), cause);
1379 }
1380 } else {
1381
1382 // Step 6
1383 set_init_state(being_initialized);
1384 set_init_thread(jt);
1385 if (debug_logging_enabled) {
1386 ResourceMark rm(jt);
1387 log_debug(class, init)("Thread \"%s\" is initializing %s",
1388 jt->name(), external_name());
1389 }
1390 }
1391 }
1392
1393 // Pre-allocating an all-zero value to be used to reset nullable flat storages
1394 if (is_inline_klass()) {
1395 InlineKlass* vk = InlineKlass::cast(this);
1396 if (vk->has_nullable_atomic_layout()) {
1397 oop val = vk->allocate_instance(THREAD);
1398 if (HAS_PENDING_EXCEPTION) {
1399 Handle e(THREAD, PENDING_EXCEPTION);
1400 CLEAR_PENDING_EXCEPTION;
1401 {
1402 EXCEPTION_MARK;
1403 add_initialization_error(THREAD, e);
1404 // Locks object, set state, and notify all waiting threads
1405 set_initialization_state_and_notify(initialization_error, THREAD);
1406 CLEAR_PENDING_EXCEPTION;
1407 }
1408 THROW_OOP(e());
1409 }
1410 vk->set_null_reset_value(val);
1411 }
1412 }
1413
1414 // Step 7
1415 // Next, if C is a class rather than an interface, initialize it's super class and super
1416 // interfaces.
1417 if (!is_interface()) {
1418 Klass* super_klass = super();
1419 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1420 super_klass->initialize(THREAD);
1421 }
1422 // If C implements any interface that declares a non-static, concrete method,
1423 // the initialization of C triggers initialization of its super interfaces.
1424 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1425 // having a superinterface that declares, non-static, concrete methods
1426 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1427 initialize_super_interfaces(THREAD);
1428 }
1429
1430 // If any exceptions, complete abruptly, throwing the same exception as above.
1431 if (HAS_PENDING_EXCEPTION) {
1432 Handle e(THREAD, PENDING_EXCEPTION);
1433 CLEAR_PENDING_EXCEPTION;
1434 {
1435 EXCEPTION_MARK;
1436 add_initialization_error(THREAD, e);
1437 // Locks object, set state, and notify all waiting threads
1438 set_initialization_state_and_notify(initialization_error, THREAD);
1439 CLEAR_PENDING_EXCEPTION;
1440 }
1441 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1442 THROW_OOP(e());
1443 }
1444 }
1445
1446 // Step 8
1447 {
1448 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1449 if (class_initializer() != nullptr) {
1450 // Timer includes any side effects of class initialization (resolution,
1451 // etc), but not recursive entry into call_class_initializer().
1452 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1453 ClassLoader::perf_class_init_selftime(),
1454 ClassLoader::perf_classes_inited(),
1455 jt->get_thread_stat()->perf_recursion_counts_addr(),
1456 jt->get_thread_stat()->perf_timers_addr(),
1457 PerfClassTraceTime::CLASS_CLINIT);
1458 call_class_initializer(THREAD);
1459 } else {
1460 // The elapsed time is so small it's not worth counting.
1461 if (UsePerfData) {
1462 ClassLoader::perf_classes_inited()->inc();
1463 }
1464 call_class_initializer(THREAD);
1465 }
1466
1467 if (has_strict_static_fields() && !HAS_PENDING_EXCEPTION) {
1468 // Step 9 also verifies that strict static fields have been initialized.
1469 // Status bits were set in ClassFileParser::post_process_parsed_stream.
1470 // After <clinit>, bits must all be clear, or else we must throw an error.
1471 // This is an extremely fast check, so we won't bother with a timer.
1472 assert(fields_status() != nullptr, "");
1473 Symbol* bad_strict_static = nullptr;
1474 for (int index = 0; index < fields_status()->length(); index++) {
1475 // Very fast loop over single byte array looking for a set bit.
1476 if (fields_status()->adr_at(index)->is_strict_static_unset()) {
1477 // This strict static field has not been set by the class initializer.
1478 // Note that in the common no-error case, we read no field metadata.
1479 // We only unpack it when we need to report an error.
1480 FieldInfo fi = field(index);
1481 bad_strict_static = fi.name(constants());
1482 if (debug_logging_enabled) {
1483 ResourceMark rm(jt);
1484 const char* msg = format_strict_static_message(bad_strict_static);
1485 log_debug(class, init)("%s", msg);
1486 } else {
1487 // If we are not logging, do not bother to look for a second offense.
1488 break;
1489 }
1490 }
1491 }
1492 if (bad_strict_static != nullptr) {
1493 throw_strict_static_exception(bad_strict_static, "is unset after initialization of", THREAD);
1494 }
1495 }
1496 }
1497
1498 // Step 9
1499 if (!HAS_PENDING_EXCEPTION) {
1500 set_initialization_state_and_notify(fully_initialized, CHECK);
1501 DEBUG_ONLY(vtable().verify(tty, true);)
1502 CompilationPolicy::replay_training_at_init(this, THREAD);
1503 }
1504 else {
1505 // Step 10 and 11
1506 Handle e(THREAD, PENDING_EXCEPTION);
1507 CLEAR_PENDING_EXCEPTION;
1508 // JVMTI has already reported the pending exception
1509 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1510 JvmtiExport::clear_detected_exception(jt);
1511 {
1512 EXCEPTION_MARK;
1513 add_initialization_error(THREAD, e);
1514 set_initialization_state_and_notify(initialization_error, THREAD);
1515 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1529 }
1530 DTRACE_CLASSINIT_PROBE_WAIT(end, -1, wait);
1531 }
1532
1533
1534 void InstanceKlass::set_initialization_state_and_notify(ClassState state, TRAPS) {
1535 Handle h_init_lock(THREAD, init_lock());
1536 if (h_init_lock() != nullptr) {
1537 ObjectLocker ol(h_init_lock, THREAD);
1538 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1539 set_init_state(state);
1540 fence_and_clear_init_lock();
1541 ol.notify_all(CHECK);
1542 } else {
1543 assert(h_init_lock() != nullptr, "The initialization state should never be set twice");
1544 set_init_thread(nullptr); // reset _init_thread before changing _init_state
1545 set_init_state(state);
1546 }
1547 }
1548
1549 void InstanceKlass::notify_strict_static_access(int field_index, bool is_writing, TRAPS) {
1550 guarantee(field_index >= 0 && field_index < fields_status()->length(), "valid field index");
1551 DEBUG_ONLY(FieldInfo debugfi = field(field_index));
1552 assert(debugfi.access_flags().is_strict(), "");
1553 assert(debugfi.access_flags().is_static(), "");
1554 FieldStatus& fs = *fields_status()->adr_at(field_index);
1555 LogTarget(Trace, class, init) lt;
1556 if (lt.is_enabled()) {
1557 ResourceMark rm(THREAD);
1558 LogStream ls(lt);
1559 FieldInfo fi = field(field_index);
1560 ls.print("notify %s %s %s%s ",
1561 external_name(), is_writing? "Write" : "Read",
1562 fs.is_strict_static_unset() ? "Unset" : "(set)",
1563 fs.is_strict_static_unread() ? "+Unread" : "");
1564 fi.print(&ls, constants());
1565 }
1566 if (fs.is_strict_static_unset()) {
1567 assert(fs.is_strict_static_unread(), "ClassFileParser resp.");
1568 // If it is not set, there are only two reasonable things we can do here:
1569 // - mark it set if this is putstatic
1570 // - throw an error (Read-Before-Write) if this is getstatic
1571
1572 // The unset state is (or should be) transient, and observable only in one
1573 // thread during the execution of <clinit>. Something is wrong here as this
1574 // should not be possible
1575 guarantee(is_reentrant_initialization(THREAD), "unscoped access to strict static");
1576 if (is_writing) {
1577 // clear the "unset" bit, since the field is actually going to be written
1578 fs.update_strict_static_unset(false);
1579 } else {
1580 // throw an IllegalStateException, since we are reading before writing
1581 // see also InstanceKlass::initialize_impl, Step 8 (at end)
1582 Symbol* bad_strict_static = field(field_index).name(constants());
1583 throw_strict_static_exception(bad_strict_static, "is unset before first read in", CHECK);
1584 }
1585 } else {
1586 // Ensure no write after read for final strict statics
1587 FieldInfo fi = field(field_index);
1588 bool is_final = fi.access_flags().is_final();
1589 if (is_final) {
1590 // no final write after read, so observing a constant freezes it, as if <clinit> ended early
1591 // (maybe we could trust the constant a little earlier, before <clinit> ends)
1592 if (is_writing && !fs.is_strict_static_unread()) {
1593 Symbol* bad_strict_static = fi.name(constants());
1594 throw_strict_static_exception(bad_strict_static, "is set after read (as final) in", CHECK);
1595 } else if (!is_writing && fs.is_strict_static_unread()) {
1596 fs.update_strict_static_unread(false);
1597 }
1598 }
1599 }
1600 }
1601
1602 void InstanceKlass::throw_strict_static_exception(Symbol* field_name, const char* when, TRAPS) {
1603 ResourceMark rm(THREAD);
1604 const char* msg = format_strict_static_message(field_name, when);
1605 THROW_MSG(vmSymbols::java_lang_IllegalStateException(), msg);
1606 }
1607
1608 const char* InstanceKlass::format_strict_static_message(Symbol* field_name, const char* when) {
1609 stringStream ss;
1610 ss.print("Strict static \"%s\" %s %s",
1611 field_name->as_C_string(),
1612 when == nullptr ? "is unset in" : when,
1613 external_name());
1614 return ss.as_string();
1615 }
1616
1617 // Update hierarchy. This is done before the new klass has been added to the SystemDictionary. The Compile_lock
1618 // is grabbed, to ensure that the compiler is not using the class hierarchy.
1619 void InstanceKlass::add_to_hierarchy(JavaThread* current) {
1620 assert(!SafepointSynchronize::is_at_safepoint(), "must NOT be at safepoint");
1621
1622 DeoptimizationScope deopt_scope;
1623 {
1624 MutexLocker ml(current, Compile_lock);
1625
1626 set_init_state(InstanceKlass::loaded);
1627 // make sure init_state store is already done.
1628 // The compiler reads the hierarchy outside of the Compile_lock.
1629 // Access ordering is used to add to hierarchy.
1630
1631 // Link into hierarchy.
1632 append_to_sibling_list(); // add to superklass/sibling list
1633 process_interfaces(); // handle all "implements" declarations
1634
1635 // Now mark all code that depended on old class hierarchy.
1636 // Note: must be done *after* linking k into the hierarchy (was bug 12/9/97)
1780 for (int i = 0; i < transitive_interfaces()->length(); i++) {
1781 if (transitive_interfaces()->at(i) == k) {
1782 return true;
1783 }
1784 }
1785 return false;
1786 }
1787
1788 bool InstanceKlass::is_same_or_direct_interface(Klass *k) const {
1789 // Verify direct super interface
1790 if (this == k) return true;
1791 assert(k->is_interface(), "should be an interface class");
1792 for (int i = 0; i < local_interfaces()->length(); i++) {
1793 if (local_interfaces()->at(i) == k) {
1794 return true;
1795 }
1796 }
1797 return false;
1798 }
1799
1800 instanceOop InstanceKlass::register_finalizer(instanceOop i, TRAPS) {
1801 if (TraceFinalizerRegistration) {
1802 tty->print("Registered ");
1803 i->print_value_on(tty);
1804 tty->print_cr(" (" PTR_FORMAT ") as finalizable", p2i(i));
1805 }
1806 instanceHandle h_i(THREAD, i);
1807 // Pass the handle as argument, JavaCalls::call expects oop as jobjects
1808 JavaValue result(T_VOID);
1809 JavaCallArguments args(h_i);
1810 methodHandle mh(THREAD, Universe::finalizer_register_method());
1811 JavaCalls::call(&result, mh, &args, CHECK_NULL);
1812 MANAGEMENT_ONLY(FinalizerService::on_register(h_i(), THREAD);)
1813 return h_i();
1814 }
1815
1816 instanceOop InstanceKlass::allocate_instance(TRAPS) {
1817 assert(!is_abstract() && !is_interface(), "Should not create this object");
1818 size_t size = size_helper(); // Query before forming handle.
1819 return (instanceOop)Universe::heap()->obj_allocate(this, size, CHECK_NULL);
1847 : vmSymbols::java_lang_IllegalAccessException(), external_name());
1848 }
1849 }
1850
1851 ArrayKlass* InstanceKlass::array_klass(int n, TRAPS) {
1852 // Need load-acquire for lock-free read
1853 if (array_klasses_acquire() == nullptr) {
1854
1855 // Recursively lock array allocation
1856 RecursiveLocker rl(MultiArray_lock, THREAD);
1857
1858 // Check if another thread created the array klass while we were waiting for the lock.
1859 if (array_klasses() == nullptr) {
1860 ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
1861 // use 'release' to pair with lock-free load
1862 release_set_array_klasses(k);
1863 }
1864 }
1865
1866 // array_klasses() will always be set at this point
1867 ArrayKlass* ak = array_klasses();
1868 assert(ak != nullptr, "should be set");
1869 return ak->array_klass(n, THREAD);
1870 }
1871
1872 ArrayKlass* InstanceKlass::array_klass_or_null(int n) {
1873 // Need load-acquire for lock-free read
1874 ArrayKlass* ak = array_klasses_acquire();
1875 if (ak == nullptr) {
1876 return nullptr;
1877 } else {
1878 return ak->array_klass_or_null(n);
1879 }
1880 }
1881
1882 ArrayKlass* InstanceKlass::array_klass(TRAPS) {
1883 return array_klass(1, THREAD);
1884 }
1885
1886 ArrayKlass* InstanceKlass::array_klass_or_null() {
1887 return array_klass_or_null(1);
1888 }
1889
1890 static int call_class_initializer_counter = 0; // for debugging
1891
1892 Method* InstanceKlass::class_initializer() const {
1893 Method* clinit = find_method(
1894 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1895 if (clinit != nullptr && clinit->is_class_initializer()) {
1896 return clinit;
1897 }
1898 return nullptr;
1899 }
1900
1901 void InstanceKlass::call_class_initializer(TRAPS) {
1902 if (ReplayCompiles &&
1903 (ReplaySuppressInitializers == 1 ||
1904 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1905 // Hide the existence of the initializer for the purpose of replaying the compile
1906 return;
1907 }
1908
1909 #if INCLUDE_CDS
1910 // This is needed to ensure the consistency of the archived heap objects.
1911 if (has_aot_initialized_mirror() && CDSConfig::is_loading_heap()) {
1912 AOTClassInitializer::call_runtime_setup(THREAD, this);
1913 return;
1914 } else if (has_archived_enum_objs()) {
1915 assert(is_shared(), "must be");
1984
1985 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1986 InterpreterOopMap* entry_for) {
1987 // Lazily create the _oop_map_cache at first request.
1988 // Load_acquire is needed to safely get instance published with CAS by another thread.
1989 OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1990 if (oop_map_cache == nullptr) {
1991 // Try to install new instance atomically.
1992 oop_map_cache = new OopMapCache();
1993 OopMapCache* other = Atomic::cmpxchg(&_oop_map_cache, (OopMapCache*)nullptr, oop_map_cache);
1994 if (other != nullptr) {
1995 // Someone else managed to install before us, ditch local copy and use the existing one.
1996 delete oop_map_cache;
1997 oop_map_cache = other;
1998 }
1999 }
2000 // _oop_map_cache is constant after init; lookup below does its own locking.
2001 oop_map_cache->lookup(method, bci, entry_for);
2002 }
2003
2004
2005 FieldInfo InstanceKlass::field(int index) const {
2006 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
2007 if (fs.index() == index) {
2008 return fs.to_FieldInfo();
2009 }
2010 }
2011 fatal("Field not found");
2012 return FieldInfo();
2013 }
2014
2015 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
2016 JavaFieldStream fs(this);
2017 if (fs.lookup(name, sig)) {
2018 assert(fs.name() == name, "name must match");
2019 assert(fs.signature() == sig, "signature must match");
2020 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
2021 return true;
2022 }
2023 return false;
2064
2065 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
2066 // search order according to newest JVM spec (5.4.3.2, p.167).
2067 // 1) search for field in current klass
2068 if (find_local_field(name, sig, fd)) {
2069 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
2070 }
2071 // 2) search for field recursively in direct superinterfaces
2072 if (is_static) {
2073 Klass* intf = find_interface_field(name, sig, fd);
2074 if (intf != nullptr) return intf;
2075 }
2076 // 3) apply field lookup recursively if superclass exists
2077 { Klass* supr = super();
2078 if (supr != nullptr) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
2079 }
2080 // 4) otherwise field lookup fails
2081 return nullptr;
2082 }
2083
2084 bool InstanceKlass::contains_field_offset(int offset) {
2085 if (this->is_inline_klass()) {
2086 InlineKlass* vk = InlineKlass::cast(this);
2087 return offset >= vk->payload_offset() && offset < (vk->payload_offset() + vk->payload_size_in_bytes());
2088 } else {
2089 fieldDescriptor fd;
2090 return find_field_from_offset(offset, false, &fd);
2091 }
2092 }
2093
2094 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
2095 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
2096 if (fs.offset() == offset) {
2097 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
2098 if (fd->is_static() == is_static) return true;
2099 }
2100 }
2101 return false;
2102 }
2103
2104
2105 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
2106 Klass* klass = const_cast<InstanceKlass*>(this);
2107 while (klass != nullptr) {
2108 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
2109 return true;
2110 }
2111 klass = klass->super();
2112 }
2456 }
2457
2458 // uncached_lookup_method searches both the local class methods array and all
2459 // superclasses methods arrays, skipping any overpass methods in superclasses,
2460 // and possibly skipping private methods.
2461 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2462 const Symbol* signature,
2463 OverpassLookupMode overpass_mode,
2464 PrivateLookupMode private_mode) const {
2465 OverpassLookupMode overpass_local_mode = overpass_mode;
2466 const Klass* klass = this;
2467 while (klass != nullptr) {
2468 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2469 signature,
2470 overpass_local_mode,
2471 StaticLookupMode::find,
2472 private_mode);
2473 if (method != nullptr) {
2474 return method;
2475 }
2476 if (name == vmSymbols::object_initializer_name()) {
2477 break; // <init> is never inherited
2478 }
2479 klass = klass->super();
2480 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2481 }
2482 return nullptr;
2483 }
2484
2485 #ifdef ASSERT
2486 // search through class hierarchy and return true if this class or
2487 // one of the superclasses was redefined
2488 bool InstanceKlass::has_redefined_this_or_super() const {
2489 const Klass* klass = this;
2490 while (klass != nullptr) {
2491 if (InstanceKlass::cast(klass)->has_been_redefined()) {
2492 return true;
2493 }
2494 klass = klass->super();
2495 }
2496 return false;
2497 }
2498 #endif
2871 int itable_offset_in_words = (int)(start_of_itable() - (intptr_t*)this);
2872
2873 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words)
2874 / itableOffsetEntry::size();
2875
2876 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2877 if (ioe->interface_klass() != nullptr) {
2878 it->push(ioe->interface_klass_addr());
2879 itableMethodEntry* ime = ioe->first_method_entry(this);
2880 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2881 for (int index = 0; index < n; index ++) {
2882 it->push(ime[index].method_addr());
2883 }
2884 }
2885 }
2886 }
2887
2888 it->push(&_nest_host);
2889 it->push(&_nest_members);
2890 it->push(&_permitted_subclasses);
2891 it->push(&_loadable_descriptors);
2892 it->push(&_record_components);
2893 it->push(&_inline_layout_info_array, MetaspaceClosure::_writable);
2894 }
2895
2896 #if INCLUDE_CDS
2897 void InstanceKlass::remove_unshareable_info() {
2898
2899 if (is_linked()) {
2900 assert(can_be_verified_at_dumptime(), "must be");
2901 // Remember this so we can avoid walking the hierarchy at runtime.
2902 set_verified_at_dump_time();
2903 }
2904
2905 _misc_flags.set_has_init_deps_processed(false);
2906
2907 Klass::remove_unshareable_info();
2908
2909 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2910 // Classes are attempted to link during dumping and may fail,
2911 // but these classes are still in the dictionary and class list in CLD.
2912 // If the class has failed verification, there is nothing else to remove.
2913 return;
2921
2922 { // Otherwise this needs to take out the Compile_lock.
2923 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2924 init_implementor();
2925 }
2926
2927 // Call remove_unshareable_info() on other objects that belong to this class, except
2928 // for constants()->remove_unshareable_info(), which is called in a separate pass in
2929 // ArchiveBuilder::make_klasses_shareable(),
2930
2931 for (int i = 0; i < methods()->length(); i++) {
2932 Method* m = methods()->at(i);
2933 m->remove_unshareable_info();
2934 }
2935
2936 // do array classes also.
2937 if (array_klasses() != nullptr) {
2938 array_klasses()->remove_unshareable_info();
2939 }
2940
2941 // These are not allocated from metaspace. They are safe to set to nullptr.
2942 _source_debug_extension = nullptr;
2943 _dep_context = nullptr;
2944 _osr_nmethods_head = nullptr;
2945 #if INCLUDE_JVMTI
2946 _breakpoints = nullptr;
2947 _previous_versions = nullptr;
2948 _cached_class_file = nullptr;
2949 _jvmti_cached_class_field_map = nullptr;
2950 #endif
2951
2952 _init_thread = nullptr;
2953 _methods_jmethod_ids = nullptr;
2954 _jni_ids = nullptr;
2955 _oop_map_cache = nullptr;
2956 if (CDSConfig::is_dumping_method_handles() && HeapShared::is_lambda_proxy_klass(this)) {
2957 // keep _nest_host
2958 } else {
2959 // clear _nest_host to ensure re-load at runtime
2960 _nest_host = nullptr;
2961 }
3012 void InstanceKlass::compute_has_loops_flag_for_methods() {
3013 Array<Method*>* methods = this->methods();
3014 for (int index = 0; index < methods->length(); ++index) {
3015 Method* m = methods->at(index);
3016 if (!m->is_overpass()) { // work around JDK-8305771
3017 m->compute_has_loops_flag();
3018 }
3019 }
3020 }
3021
3022 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
3023 PackageEntry* pkg_entry, TRAPS) {
3024 // InstanceKlass::add_to_hierarchy() sets the init_state to loaded
3025 // before the InstanceKlass is added to the SystemDictionary. Make
3026 // sure the current state is <loaded.
3027 assert(!is_loaded(), "invalid init state");
3028 assert(!shared_loading_failed(), "Must not try to load failed class again");
3029 set_package(loader_data, pkg_entry, CHECK);
3030 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
3031
3032 if (is_inline_klass()) {
3033 InlineKlass::cast(this)->initialize_calling_convention(CHECK);
3034 }
3035
3036 Array<Method*>* methods = this->methods();
3037 int num_methods = methods->length();
3038 for (int index = 0; index < num_methods; ++index) {
3039 methods->at(index)->restore_unshareable_info(CHECK);
3040 }
3041 #if INCLUDE_JVMTI
3042 if (JvmtiExport::has_redefined_a_class()) {
3043 // Reinitialize vtable because RedefineClasses may have changed some
3044 // entries in this vtable for super classes so the CDS vtable might
3045 // point to old or obsolete entries. RedefineClasses doesn't fix up
3046 // vtables in the shared system dictionary, only the main one.
3047 // It also redefines the itable too so fix that too.
3048 // First fix any default methods that point to a super class that may
3049 // have been redefined.
3050 bool trace_name_printed = false;
3051 adjust_default_methods(&trace_name_printed);
3052 if (verified_at_dump_time()) {
3053 // Initialize vtable and itable for classes which can be verified at dump time.
3054 // Unlinked classes such as old classes with major version < 50 cannot be verified
3055 // at dump time.
3056 vtable().initialize_vtable();
3057 itable().initialize_itable();
3058 }
3059 }
3060 #endif // INCLUDE_JVMTI
3061
3062 // restore constant pool resolved references
3063 constants()->restore_unshareable_info(CHECK);
3064
3065 if (array_klasses() != nullptr) {
3066 // To get a consistent list of classes we need MultiArray_lock to ensure
3067 // array classes aren't observed while they are being restored.
3068 RecursiveLocker rl(MultiArray_lock, THREAD);
3069 assert(this == ObjArrayKlass::cast(array_klasses())->bottom_klass(), "sanity");
3070 // Array classes have null protection domain.
3071 // --> see ArrayKlass::complete_create_array_klass()
3072 if (class_loader_data() == nullptr) {
3073 ResourceMark rm(THREAD);
3074 log_debug(cds)(" loader_data %s ", loader_data == nullptr ? "nullptr" : "non null");
3075 log_debug(cds)(" this %s array_klasses %s ", this->name()->as_C_string(), array_klasses()->name()->as_C_string());
3076 }
3077 assert(!array_klasses()->is_refined_objArray_klass(), "must be non-refined objarrayklass");
3078 array_klasses()->restore_unshareable_info(class_loader_data(), Handle(), CHECK);
3079 }
3080
3081 // Initialize @ValueBased class annotation if not already set in the archived klass.
3082 if (DiagnoseSyncOnValueBasedClasses && has_value_based_class_annotation() && !is_value_based()) {
3083 set_is_value_based();
3084 }
3085
3086 DEBUG_ONLY(FieldInfoStream::validate_search_table(_constants, _fieldinfo_stream, _fieldinfo_search_table));
3087 }
3088
3089 // Check if a class or any of its supertypes has a version older than 50.
3090 // CDS will not perform verification of old classes during dump time because
3091 // without changing the old verifier, the verification constraint cannot be
3092 // retrieved during dump time.
3093 // Verification of archived old classes will be performed during run time.
3094 bool InstanceKlass::can_be_verified_at_dumptime() const {
3095 if (MetaspaceShared::is_in_shared_metaspace(this)) {
3096 // This is a class that was dumped into the base archive, so we know
3097 // it was verified at dump time.
3211 constants()->release_C_heap_structures();
3212 }
3213 }
3214
3215 // The constant pool is on stack if any of the methods are executing or
3216 // referenced by handles.
3217 bool InstanceKlass::on_stack() const {
3218 return _constants->on_stack();
3219 }
3220
3221 Symbol* InstanceKlass::source_file_name() const { return _constants->source_file_name(); }
3222 u2 InstanceKlass::source_file_name_index() const { return _constants->source_file_name_index(); }
3223 void InstanceKlass::set_source_file_name_index(u2 sourcefile_index) { _constants->set_source_file_name_index(sourcefile_index); }
3224
3225 // minor and major version numbers of class file
3226 u2 InstanceKlass::minor_version() const { return _constants->minor_version(); }
3227 void InstanceKlass::set_minor_version(u2 minor_version) { _constants->set_minor_version(minor_version); }
3228 u2 InstanceKlass::major_version() const { return _constants->major_version(); }
3229 void InstanceKlass::set_major_version(u2 major_version) { _constants->set_major_version(major_version); }
3230
3231 bool InstanceKlass::supports_inline_types() const {
3232 return major_version() >= Verifier::VALUE_TYPES_MAJOR_VERSION && minor_version() == Verifier::JAVA_PREVIEW_MINOR_VERSION;
3233 }
3234
3235 const InstanceKlass* InstanceKlass::get_klass_version(int version) const {
3236 for (const InstanceKlass* ik = this; ik != nullptr; ik = ik->previous_versions()) {
3237 if (ik->constants()->version() == version) {
3238 return ik;
3239 }
3240 }
3241 return nullptr;
3242 }
3243
3244 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
3245 if (array == nullptr) {
3246 _source_debug_extension = nullptr;
3247 } else {
3248 // Adding one to the attribute length in order to store a null terminator
3249 // character could cause an overflow because the attribute length is
3250 // already coded with an u4 in the classfile, but in practice, it's
3251 // unlikely to happen.
3252 assert((length+1) > length, "Overflow checking");
3253 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
3254 for (int i = 0; i < length; i++) {
3255 sde[i] = array[i];
3256 }
3257 sde[length] = '\0';
3258 _source_debug_extension = sde;
3259 }
3260 }
3261
3262 Symbol* InstanceKlass::generic_signature() const { return _constants->generic_signature(); }
3263 u2 InstanceKlass::generic_signature_index() const { return _constants->generic_signature_index(); }
3264 void InstanceKlass::set_generic_signature_index(u2 sig_index) { _constants->set_generic_signature_index(sig_index); }
3265
3266 const char* InstanceKlass::signature_name() const {
3267 return signature_name_of_carrier(JVM_SIGNATURE_CLASS);
3268 }
3269
3270 const char* InstanceKlass::signature_name_of_carrier(char c) const {
3271 // Get the internal name as a c string
3272 const char* src = (const char*) (name()->as_C_string());
3273 const int src_length = (int)strlen(src);
3274
3275 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
3276
3277 // Add L or Q as type indicator
3278 int dest_index = 0;
3279 dest[dest_index++] = c;
3280
3281 // Add the actual class name
3282 for (int src_index = 0; src_index < src_length; ) {
3283 dest[dest_index++] = src[src_index++];
3284 }
3285
3286 if (is_hidden()) { // Replace the last '+' with a '.'.
3287 for (int index = (int)src_length; index > 0; index--) {
3288 if (dest[index] == '+') {
3289 dest[index] = JVM_SIGNATURE_DOT;
3290 break;
3291 }
3292 }
3293 }
3294
3295 // Add the semicolon and the null
3296 dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
3297 dest[dest_index] = '\0';
3298 return dest;
3299 }
3540 bool InstanceKlass::find_inner_classes_attr(int* ooff, int* noff, TRAPS) const {
3541 constantPoolHandle i_cp(THREAD, constants());
3542 for (InnerClassesIterator iter(this); !iter.done(); iter.next()) {
3543 int ioff = iter.inner_class_info_index();
3544 if (ioff != 0) {
3545 // Check to see if the name matches the class we're looking for
3546 // before attempting to find the class.
3547 if (i_cp->klass_name_at_matches(this, ioff)) {
3548 Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
3549 if (this == inner_klass) {
3550 *ooff = iter.outer_class_info_index();
3551 *noff = iter.inner_name_index();
3552 return true;
3553 }
3554 }
3555 }
3556 }
3557 return false;
3558 }
3559
3560 void InstanceKlass::check_can_be_annotated_with_NullRestricted(InstanceKlass* type, Symbol* container_klass_name, TRAPS) {
3561 assert(type->is_instance_klass(), "Sanity check");
3562 if (type->is_identity_class()) {
3563 ResourceMark rm(THREAD);
3564 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
3565 err_msg("Class %s expects class %s to be a value class, but it is an identity class",
3566 container_klass_name->as_C_string(),
3567 type->external_name()));
3568 }
3569
3570 if (type->is_abstract()) {
3571 ResourceMark rm(THREAD);
3572 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(),
3573 err_msg("Class %s expects class %s to be concrete value type, but it is an abstract class",
3574 container_klass_name->as_C_string(),
3575 type->external_name()));
3576 }
3577 }
3578
3579 InstanceKlass* InstanceKlass::compute_enclosing_class(bool* inner_is_member, TRAPS) const {
3580 InstanceKlass* outer_klass = nullptr;
3581 *inner_is_member = false;
3582 int ooff = 0, noff = 0;
3583 bool has_inner_classes_attr = find_inner_classes_attr(&ooff, &noff, THREAD);
3584 if (has_inner_classes_attr) {
3585 constantPoolHandle i_cp(THREAD, constants());
3586 if (ooff != 0) {
3587 Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
3588 if (!ok->is_instance_klass()) {
3589 // If the outer class is not an instance klass then it cannot have
3590 // declared any inner classes.
3591 ResourceMark rm(THREAD);
3592 // Names are all known to be < 64k so we know this formatted message is not excessively large.
3593 Exceptions::fthrow(
3594 THREAD_AND_LOCATION,
3595 vmSymbols::java_lang_IncompatibleClassChangeError(),
3596 "%s and %s disagree on InnerClasses attribute",
3597 ok->external_name(),
3598 external_name());
3625 u2 InstanceKlass::compute_modifier_flags() const {
3626 u2 access = access_flags().as_unsigned_short();
3627
3628 // But check if it happens to be member class.
3629 InnerClassesIterator iter(this);
3630 for (; !iter.done(); iter.next()) {
3631 int ioff = iter.inner_class_info_index();
3632 // Inner class attribute can be zero, skip it.
3633 // Strange but true: JVM spec. allows null inner class refs.
3634 if (ioff == 0) continue;
3635
3636 // only look at classes that are already loaded
3637 // since we are looking for the flags for our self.
3638 Symbol* inner_name = constants()->klass_name_at(ioff);
3639 if (name() == inner_name) {
3640 // This is really a member class.
3641 access = iter.inner_access_flags();
3642 break;
3643 }
3644 }
3645 return access;
3646 }
3647
3648 jint InstanceKlass::jvmti_class_status() const {
3649 jint result = 0;
3650
3651 if (is_linked()) {
3652 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3653 }
3654
3655 if (is_initialized()) {
3656 assert(is_linked(), "Class status is not consistent");
3657 result |= JVMTI_CLASS_STATUS_INITIALIZED;
3658 }
3659 if (is_in_error_state()) {
3660 result |= JVMTI_CLASS_STATUS_ERROR;
3661 }
3662 return result;
3663 }
3664
3665 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {
3879 }
3880 osr = osr->osr_link();
3881 }
3882
3883 assert(match_level == false || best == nullptr, "shouldn't pick up anything if match_level is set");
3884 if (best != nullptr && best->comp_level() >= comp_level) {
3885 return best;
3886 }
3887 return nullptr;
3888 }
3889
3890 // -----------------------------------------------------------------------------------------------------
3891 // Printing
3892
3893 #define BULLET " - "
3894
3895 static const char* state_names[] = {
3896 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3897 };
3898
3899 static void print_vtable(address self, intptr_t* start, int len, outputStream* st) {
3900 ResourceMark rm;
3901 int* forward_refs = NEW_RESOURCE_ARRAY(int, len);
3902 for (int i = 0; i < len; i++) forward_refs[i] = 0;
3903 for (int i = 0; i < len; i++) {
3904 intptr_t e = start[i];
3905 st->print("%d : " INTPTR_FORMAT, i, e);
3906 if (forward_refs[i] != 0) {
3907 int from = forward_refs[i];
3908 int off = (int) start[from];
3909 st->print(" (offset %d <= [%d])", off, from);
3910 }
3911 if (MetaspaceObj::is_valid((Metadata*)e)) {
3912 st->print(" ");
3913 ((Metadata*)e)->print_value_on(st);
3914 } else if (self != nullptr && e > 0 && e < 0x10000) {
3915 address location = self + e;
3916 int index = (int)((intptr_t*)location - start);
3917 st->print(" (offset %d => [%d])", (int)e, index);
3918 if (index >= 0 && index < len)
3919 forward_refs[index] = i;
3920 }
3921 st->cr();
3922 }
3923 }
3924
3925 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3926 return print_vtable(nullptr, reinterpret_cast<intptr_t*>(start), len, st);
3927 }
3928
3929 template<typename T>
3930 static void print_array_on(outputStream* st, Array<T>* array) {
3931 if (array == nullptr) { st->print_cr("nullptr"); return; }
3932 array->print_value_on(st); st->cr();
3933 if (Verbose || WizardMode) {
3934 for (int i = 0; i < array->length(); i++) {
3935 st->print("%d : ", i); array->at(i)->print_value_on(st); st->cr();
3936 }
3937 }
3938 }
3939
3940 static void print_array_on(outputStream* st, Array<int>* array) {
3941 if (array == nullptr) { st->print_cr("nullptr"); return; }
3942 array->print_value_on(st); st->cr();
3943 if (Verbose || WizardMode) {
3944 for (int i = 0; i < array->length(); i++) {
3945 st->print("%d : %d", i, array->at(i)); st->cr();
3946 }
3947 }
3948 }
3949
3950 const char* InstanceKlass::init_state_name() const {
3951 return state_names[init_state()];
3952 }
3953
3954 void InstanceKlass::print_on(outputStream* st) const {
3955 assert(is_klass(), "must be klass");
3956 Klass::print_on(st);
3957
3958 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3959 st->print(BULLET"klass size: %d", size()); st->cr();
3960 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3961 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3962 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3963 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3964 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3965 st->print(BULLET"sub: ");
3966 Klass* sub = subklass();
3967 int n;
3968 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3969 if (n < MaxSubklassPrintSize) {
3970 sub->print_value_on(st);
3971 st->print(" ");
3972 }
3973 }
3974 if (n >= MaxSubklassPrintSize) st->print("(%zd more klasses...)", n - MaxSubklassPrintSize);
3975 st->cr();
3976
3977 if (is_interface()) {
3978 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3979 if (nof_implementors() == 1) {
3980 st->print_cr(BULLET"implementor: ");
3981 st->print(" ");
3982 implementor()->print_value_on(st);
3983 st->cr();
3984 }
3985 }
3986
3987 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3988 st->print(BULLET"methods: "); print_array_on(st, methods());
3989 st->print(BULLET"method ordering: "); print_array_on(st, method_ordering());
3990 if (default_methods() != nullptr) {
3991 st->print(BULLET"default_methods: "); print_array_on(st, default_methods());
3992 }
3993 print_on_maybe_null(st, BULLET"default vtable indices: ", default_vtable_indices());
3994 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
3995 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3996
3997 st->print(BULLET"secondary supers: "); secondary_supers()->print_value_on(st); st->cr();
3998
3999 st->print(BULLET"hash_slot: %d", hash_slot()); st->cr();
4000 st->print(BULLET"secondary bitmap: " UINTX_FORMAT_X_0, _secondary_supers_bitmap); st->cr();
4001
4002 if (secondary_supers() != nullptr) {
4003 if (Verbose) {
4004 bool is_hashed = (_secondary_supers_bitmap != SECONDARY_SUPERS_BITMAP_FULL);
4005 st->print_cr(BULLET"---- secondary supers (%d words):", _secondary_supers->length());
4006 for (int i = 0; i < _secondary_supers->length(); i++) {
4007 ResourceMark rm; // for external_name()
4008 Klass* secondary_super = _secondary_supers->at(i);
4009 st->print(BULLET"%2d:", i);
4010 if (is_hashed) {
4011 int home_slot = compute_home_slot(secondary_super, _secondary_supers_bitmap);
4031 print_on_maybe_null(st, BULLET"field type annotations: ", fields_type_annotations());
4032 {
4033 bool have_pv = false;
4034 // previous versions are linked together through the InstanceKlass
4035 for (InstanceKlass* pv_node = previous_versions();
4036 pv_node != nullptr;
4037 pv_node = pv_node->previous_versions()) {
4038 if (!have_pv)
4039 st->print(BULLET"previous version: ");
4040 have_pv = true;
4041 pv_node->constants()->print_value_on(st);
4042 }
4043 if (have_pv) st->cr();
4044 }
4045
4046 print_on_maybe_null(st, BULLET"generic signature: ", generic_signature());
4047 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
4048 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
4049 print_on_maybe_null(st, BULLET"record components: ", record_components());
4050 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
4051 st->print(BULLET"loadable descriptors: "); loadable_descriptors()->print_value_on(st); st->cr();
4052 if (java_mirror() != nullptr) {
4053 st->print(BULLET"java mirror: ");
4054 java_mirror()->print_value_on(st);
4055 st->cr();
4056 } else {
4057 st->print_cr(BULLET"java mirror: null");
4058 }
4059 st->print(BULLET"vtable length %d (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
4060 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
4061 st->print(BULLET"itable length %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
4062 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(nullptr, start_of_itable(), itable_length(), st);
4063 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
4064
4065 FieldPrinter print_static_field(st);
4066 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
4067 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
4068 FieldPrinter print_nonstatic_field(st);
4069 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
4070 ik->print_nonstatic_fields(&print_nonstatic_field);
4071
4072 st->print(BULLET"non-static oop maps (%d entries): ", nonstatic_oop_map_count());
4073 OopMapBlock* map = start_of_nonstatic_oop_maps();
4074 OopMapBlock* end_map = map + nonstatic_oop_map_count();
4075 while (map < end_map) {
4076 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
4077 map++;
4078 }
4079 st->cr();
4080
4081 if (fieldinfo_search_table() != nullptr) {
4082 st->print_cr(BULLET"---- field info search table:");
4083 FieldInfoStream::print_search_table(st, _constants, _fieldinfo_stream, _fieldinfo_search_table);
4084 }
4085 }
4086
4087 void InstanceKlass::print_value_on(outputStream* st) const {
4088 assert(is_klass(), "must be klass");
4089 if (Verbose || WizardMode) access_flags().print_on(st);
4090 name()->print_value_on(st);
4091 }
4092
4093 void FieldPrinter::do_field(fieldDescriptor* fd) {
4094 for (int i = 0; i < _indent; i++) _st->print(" ");
4095 _st->print(BULLET);
4096 if (_obj == nullptr) {
4097 fd->print_on(_st, _base_offset);
4098 _st->cr();
4099 } else {
4100 fd->print_on_for(_st, _obj, _indent, _base_offset);
4101 if (!fd->field_flags().is_flat()) _st->cr();
4102 }
4103 }
4104
4105
4106 void InstanceKlass::oop_print_on(oop obj, outputStream* st, int indent, int base_offset) {
4107 Klass::oop_print_on(obj, st);
4108
4109 if (this == vmClasses::String_klass()) {
4110 typeArrayOop value = java_lang_String::value(obj);
4111 juint length = java_lang_String::length(obj);
4112 if (value != nullptr &&
4113 value->is_typeArray() &&
4114 length <= (juint) value->length()) {
4115 st->print(BULLET"string: ");
4116 java_lang_String::print(obj, st);
4117 st->cr();
4118 }
4119 }
4120
4121 st->print_cr(BULLET"---- fields (total size %zu words):", oop_size(obj));
4122 FieldPrinter print_field(st, obj, indent, base_offset);
4123 print_nonstatic_fields(&print_field);
4124
4125 if (this == vmClasses::Class_klass()) {
4126 st->print(BULLET"signature: ");
4127 java_lang_Class::print_signature(obj, st);
4128 st->cr();
4129 Klass* real_klass = java_lang_Class::as_Klass(obj);
4130 if (real_klass != nullptr && real_klass->is_instance_klass()) {
4131 st->print_cr(BULLET"---- static fields (%d):", java_lang_Class::static_oop_field_count(obj));
4132 InstanceKlass::cast(real_klass)->do_local_static_fields(&print_field);
4133 }
4134 } else if (this == vmClasses::MethodType_klass()) {
4135 st->print(BULLET"signature: ");
4136 java_lang_invoke_MethodType::print_signature(obj, st);
4137 st->cr();
4138 }
4139 }
4140
4141 #ifndef PRODUCT
4142
|