53 #include "logging/logStream.hpp"
54 #include "memory/allocation.inline.hpp"
55 #include "memory/iterator.inline.hpp"
56 #include "memory/metadataFactory.hpp"
57 #include "memory/metaspaceClosure.hpp"
58 #include "memory/oopFactory.hpp"
59 #include "memory/resourceArea.hpp"
60 #include "memory/universe.hpp"
61 #include "oops/fieldStreams.inline.hpp"
62 #include "oops/constantPool.hpp"
63 #include "oops/instanceClassLoaderKlass.hpp"
64 #include "oops/instanceKlass.inline.hpp"
65 #include "oops/instanceMirrorKlass.hpp"
66 #include "oops/instanceOop.hpp"
67 #include "oops/instanceStackChunkKlass.hpp"
68 #include "oops/klass.inline.hpp"
69 #include "oops/method.hpp"
70 #include "oops/oop.inline.hpp"
71 #include "oops/recordComponent.hpp"
72 #include "oops/symbol.hpp"
73 #include "prims/jvmtiExport.hpp"
74 #include "prims/jvmtiRedefineClasses.hpp"
75 #include "prims/jvmtiThreadState.hpp"
76 #include "prims/methodComparator.hpp"
77 #include "runtime/arguments.hpp"
78 #include "runtime/deoptimization.hpp"
79 #include "runtime/atomic.hpp"
80 #include "runtime/fieldDescriptor.inline.hpp"
81 #include "runtime/handles.inline.hpp"
82 #include "runtime/javaCalls.hpp"
83 #include "runtime/javaThread.inline.hpp"
84 #include "runtime/mutexLocker.hpp"
85 #include "runtime/orderAccess.hpp"
86 #include "runtime/os.inline.hpp"
87 #include "runtime/reflectionUtils.hpp"
88 #include "runtime/threads.hpp"
89 #include "services/classLoadingService.hpp"
90 #include "services/finalizerService.hpp"
91 #include "services/threadService.hpp"
92 #include "utilities/dtrace.hpp"
149
150 static inline bool is_class_loader(const Symbol* class_name,
151 const ClassFileParser& parser) {
152 assert(class_name != nullptr, "invariant");
153
154 if (class_name == vmSymbols::java_lang_ClassLoader()) {
155 return true;
156 }
157
158 if (vmClasses::ClassLoader_klass_loaded()) {
159 const Klass* const super_klass = parser.super_klass();
160 if (super_klass != nullptr) {
161 if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
162 return true;
163 }
164 }
165 }
166 return false;
167 }
168
169 static inline bool is_stack_chunk_class(const Symbol* class_name,
170 const ClassLoaderData* loader_data) {
171 return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
172 loader_data->is_the_null_class_loader_data());
173 }
174
175 // private: called to verify that k is a static member of this nest.
176 // We know that k is an instance class in the same package and hence the
177 // same classloader.
178 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
179 assert(!is_hidden(), "unexpected hidden class");
180 if (_nest_members == nullptr || _nest_members == Universe::the_empty_short_array()) {
181 if (log_is_enabled(Trace, class, nestmates)) {
182 ResourceMark rm(current);
183 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
184 k->external_name(), this->external_name());
185 }
186 return false;
187 }
188
421 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
422 this->external_name(),
423 access ? "" : "NOT ",
424 k->external_name());
425 return access;
426 }
427
428 const char* InstanceKlass::nest_host_error() {
429 if (_nest_host_index == 0) {
430 return nullptr;
431 } else {
432 constantPoolHandle cph(Thread::current(), constants());
433 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
434 }
435 }
436
437 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
438 const int size = InstanceKlass::size(parser.vtable_size(),
439 parser.itable_size(),
440 nonstatic_oop_map_size(parser.total_oop_map_count()),
441 parser.is_interface());
442
443 const Symbol* const class_name = parser.class_name();
444 assert(class_name != nullptr, "invariant");
445 ClassLoaderData* loader_data = parser.loader_data();
446 assert(loader_data != nullptr, "invariant");
447
448 InstanceKlass* ik;
449
450 // Allocation
451 if (parser.is_instance_ref_klass()) {
452 // java.lang.ref.Reference
453 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
454 } else if (class_name == vmSymbols::java_lang_Class()) {
455 // mirror - java.lang.Class
456 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
457 } else if (is_stack_chunk_class(class_name, loader_data)) {
458 // stack chunk
459 ik = new (loader_data, size, THREAD) InstanceStackChunkKlass(parser);
460 } else if (is_class_loader(class_name, parser)) {
461 // class loader - java.lang.ClassLoader
462 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
463 } else {
464 // normal
465 ik = new (loader_data, size, THREAD) InstanceKlass(parser);
466 }
467
468 // Check for pending exception before adding to the loader data and incrementing
469 // class count. Can get OOM here.
470 if (HAS_PENDING_EXCEPTION) {
471 return nullptr;
472 }
473
474 return ik;
475 }
476
477
478 // copy method ordering from resource area to Metaspace
479 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
480 if (m != nullptr) {
481 // allocate a new array and copy contents (memcpy?)
482 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
483 for (int i = 0; i < m->length(); i++) {
484 _method_ordering->at_put(i, m->at(i));
485 }
486 } else {
487 _method_ordering = Universe::the_empty_int_array();
488 }
489 }
490
491 // create a new array of vtable_indices for default methods
492 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
493 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
494 assert(default_vtable_indices() == nullptr, "only create once");
495 set_default_vtable_indices(vtable_indices);
496 return vtable_indices;
497 }
498
499 static Monitor* create_init_monitor(const char* name) {
500 return new Monitor(Mutex::safepoint, name);
501 }
502
503 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
504 Klass(kind),
505 _nest_members(nullptr),
506 _nest_host(nullptr),
507 _permitted_subclasses(nullptr),
508 _record_components(nullptr),
509 _static_field_size(parser.static_field_size()),
510 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
511 _itable_len(parser.itable_size()),
512 _nest_host_index(0),
513 _init_state(allocated),
514 _reference_type(reference_type),
515 _init_monitor(create_init_monitor("InstanceKlassInitMonitor_lock")),
516 _init_thread(nullptr)
517 {
518 set_vtable_length(parser.vtable_size());
519 set_access_flags(parser.access_flags());
520 if (parser.is_hidden()) set_is_hidden();
521 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
522 false));
523
524 assert(nullptr == _methods, "underlying memory not zeroed?");
525 assert(is_instance_klass(), "is layout incorrect?");
526 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
527 }
528
529 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
530 Array<Method*>* methods) {
531 if (methods != nullptr && methods != Universe::the_empty_method_array() &&
532 !methods->is_shared()) {
533 for (int i = 0; i < methods->length(); i++) {
534 Method* method = methods->at(i);
535 if (method == nullptr) continue; // maybe null if error processing
536 // Only want to delete methods that are not executing for RedefineClasses.
537 // The previous version will point to them so they're not totally dangling
538 assert (!method->on_stack(), "shouldn't be called with methods on stack");
539 MetadataFactory::free_metadata(loader_data, method);
540 }
541 MetadataFactory::free_array<Method*>(loader_data, methods);
542 }
543 }
544
545 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
546 const Klass* super_klass,
676 inner_classes() != Universe::the_empty_short_array() &&
677 !inner_classes()->is_shared()) {
678 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
679 }
680 set_inner_classes(nullptr);
681
682 if (nest_members() != nullptr &&
683 nest_members() != Universe::the_empty_short_array() &&
684 !nest_members()->is_shared()) {
685 MetadataFactory::free_array<jushort>(loader_data, nest_members());
686 }
687 set_nest_members(nullptr);
688
689 if (permitted_subclasses() != nullptr &&
690 permitted_subclasses() != Universe::the_empty_short_array() &&
691 !permitted_subclasses()->is_shared()) {
692 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
693 }
694 set_permitted_subclasses(nullptr);
695
696 // We should deallocate the Annotations instance if it's not in shared spaces.
697 if (annotations() != nullptr && !annotations()->is_shared()) {
698 MetadataFactory::free_metadata(loader_data, annotations());
699 }
700 set_annotations(nullptr);
701
702 SystemDictionaryShared::handle_class_unloading(this);
703
704 #if INCLUDE_CDS_JAVA_HEAP
705 if (DumpSharedSpaces) {
706 HeapShared::remove_scratch_objects(this);
707 }
708 #endif
709 }
710
711 bool InstanceKlass::is_record() const {
712 return _record_components != nullptr &&
713 is_final() &&
714 java_super() == vmClasses::Record_klass();
715 }
832 vmSymbols::java_lang_IncompatibleClassChangeError(),
833 "class %s has interface %s as super class",
834 external_name(),
835 super_klass->external_name()
836 );
837 return false;
838 }
839
840 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
841 ik_super->link_class_impl(CHECK_false);
842 }
843
844 // link all interfaces implemented by this class before linking this class
845 Array<InstanceKlass*>* interfaces = local_interfaces();
846 int num_interfaces = interfaces->length();
847 for (int index = 0; index < num_interfaces; index++) {
848 InstanceKlass* interk = interfaces->at(index);
849 interk->link_class_impl(CHECK_false);
850 }
851
852 // in case the class is linked in the process of linking its superclasses
853 if (is_linked()) {
854 return true;
855 }
856
857 // trace only the link time for this klass that includes
858 // the verification time
859 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
860 ClassLoader::perf_class_link_selftime(),
861 ClassLoader::perf_classes_linked(),
862 jt->get_thread_stat()->perf_recursion_counts_addr(),
863 jt->get_thread_stat()->perf_timers_addr(),
864 PerfClassTraceTime::CLASS_LINK);
865
866 // verification & rewriting
867 {
868 LockLinkState init_lock(this, jt);
869
870 // rewritten will have been set if loader constraint error found
871 // on an earlier link attempt
1093 set_init_thread(jt);
1094 }
1095 }
1096
1097 // Throw error outside lock
1098 if (throw_error) {
1099 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1100 ResourceMark rm(THREAD);
1101 Handle cause(THREAD, get_initialization_error(THREAD));
1102
1103 stringStream ss;
1104 ss.print("Could not initialize class %s", external_name());
1105 if (cause.is_null()) {
1106 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1107 } else {
1108 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1109 ss.as_string(), cause);
1110 }
1111 }
1112
1113 // Step 7
1114 // Next, if C is a class rather than an interface, initialize it's super class and super
1115 // interfaces.
1116 if (!is_interface()) {
1117 Klass* super_klass = super();
1118 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1119 super_klass->initialize(THREAD);
1120 }
1121 // If C implements any interface that declares a non-static, concrete method,
1122 // the initialization of C triggers initialization of its super interfaces.
1123 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1124 // having a superinterface that declares, non-static, concrete methods
1125 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1126 initialize_super_interfaces(THREAD);
1127 }
1128
1129 // If any exceptions, complete abruptly, throwing the same exception as above.
1130 if (HAS_PENDING_EXCEPTION) {
1131 Handle e(THREAD, PENDING_EXCEPTION);
1132 CLEAR_PENDING_EXCEPTION;
1133 {
1134 EXCEPTION_MARK;
1135 add_initialization_error(THREAD, e);
1136 // Locks object, set state, and notify all waiting threads
1137 set_initialization_state_and_notify(initialization_error, THREAD);
1138 CLEAR_PENDING_EXCEPTION;
1139 }
1140 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1141 THROW_OOP(e());
1142 }
1143 }
1144
1145
1146 // Step 8
1147 {
1148 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1149 if (class_initializer() != nullptr) {
1150 // Timer includes any side effects of class initialization (resolution,
1151 // etc), but not recursive entry into call_class_initializer().
1152 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1153 ClassLoader::perf_class_init_selftime(),
1154 ClassLoader::perf_classes_inited(),
1155 jt->get_thread_stat()->perf_recursion_counts_addr(),
1156 jt->get_thread_stat()->perf_timers_addr(),
1157 PerfClassTraceTime::CLASS_CLINIT);
1158 call_class_initializer(THREAD);
1159 } else {
1160 // The elapsed time is so small it's not worth counting.
1161 if (UsePerfData) {
1162 ClassLoader::perf_classes_inited()->inc();
1163 }
1164 call_class_initializer(THREAD);
1165 }
1166 }
1167
1168 // Step 9
1169 if (!HAS_PENDING_EXCEPTION) {
1170 set_initialization_state_and_notify(fully_initialized, THREAD);
1171 debug_only(vtable().verify(tty, true);)
1172 }
1173 else {
1174 // Step 10 and 11
1175 Handle e(THREAD, PENDING_EXCEPTION);
1176 CLEAR_PENDING_EXCEPTION;
1177 // JVMTI has already reported the pending exception
1178 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1179 JvmtiExport::clear_detected_exception(jt);
1180 {
1181 EXCEPTION_MARK;
1182 add_initialization_error(THREAD, e);
1183 set_initialization_state_and_notify(initialization_error, THREAD);
1184 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1185 // JVMTI has already reported the pending exception
1186 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1187 JvmtiExport::clear_detected_exception(jt);
1188 }
1189 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1190 if (e->is_a(vmClasses::Error_klass())) {
1191 THROW_OOP(e());
1192 } else {
1193 JavaCallArguments args(e);
1194 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1480 : vmSymbols::java_lang_InstantiationException(), external_name());
1481 }
1482 if (this == vmClasses::Class_klass()) {
1483 ResourceMark rm(THREAD);
1484 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1485 : vmSymbols::java_lang_IllegalAccessException(), external_name());
1486 }
1487 }
1488
1489 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1490 // Need load-acquire for lock-free read
1491 if (array_klasses_acquire() == nullptr) {
1492 ResourceMark rm(THREAD);
1493 JavaThread *jt = THREAD;
1494 {
1495 // Atomic creation of array_klasses
1496 MutexLocker ma(THREAD, MultiArray_lock);
1497
1498 // Check if update has already taken place
1499 if (array_klasses() == nullptr) {
1500 ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
1501 // use 'release' to pair with lock-free load
1502 release_set_array_klasses(k);
1503 }
1504 }
1505 }
1506 // array_klasses() will always be set at this point
1507 ObjArrayKlass* oak = array_klasses();
1508 return oak->array_klass(n, THREAD);
1509 }
1510
1511 Klass* InstanceKlass::array_klass_or_null(int n) {
1512 // Need load-acquire for lock-free read
1513 ObjArrayKlass* oak = array_klasses_acquire();
1514 if (oak == nullptr) {
1515 return nullptr;
1516 } else {
1517 return oak->array_klass_or_null(n);
1518 }
1519 }
1520
1521 Klass* InstanceKlass::array_klass(TRAPS) {
1522 return array_klass(1, THREAD);
1523 }
1524
1525 Klass* InstanceKlass::array_klass_or_null() {
1526 return array_klass_or_null(1);
1527 }
1528
1529 static int call_class_initializer_counter = 0; // for debugging
1530
1531 Method* InstanceKlass::class_initializer() const {
1532 Method* clinit = find_method(
1533 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1534 if (clinit != nullptr && clinit->has_valid_initializer_flags()) {
1535 return clinit;
1536 }
1537 return nullptr;
1538 }
1539
1540 void InstanceKlass::call_class_initializer(TRAPS) {
1541 if (ReplayCompiles &&
1542 (ReplaySuppressInitializers == 1 ||
1543 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1544 // Hide the existence of the initializer for the purpose of replaying the compile
1545 return;
1546 }
1547
1548 #if INCLUDE_CDS
1549 // This is needed to ensure the consistency of the archived heap objects.
1550 if (has_archived_enum_objs()) {
1551 assert(is_shared(), "must be");
1552 bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
1553 if (initialized) {
1554 return;
1563 ResourceMark rm(THREAD);
1564 LogStream ls(lt);
1565 ls.print("%d Initializing ", call_class_initializer_counter++);
1566 name()->print_value_on(&ls);
1567 ls.print_cr("%s (" PTR_FORMAT ")", h_method() == nullptr ? "(no method)" : "", p2i(this));
1568 }
1569 if (h_method() != nullptr) {
1570 JavaCallArguments args; // No arguments
1571 JavaValue result(T_VOID);
1572 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1573 }
1574 }
1575
1576
1577 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1578 InterpreterOopMap* entry_for) {
1579 // Lazily create the _oop_map_cache at first request
1580 // Lock-free access requires load_acquire.
1581 OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1582 if (oop_map_cache == nullptr) {
1583 MutexLocker x(OopMapCacheAlloc_lock);
1584 // Check if _oop_map_cache was allocated while we were waiting for this lock
1585 if ((oop_map_cache = _oop_map_cache) == nullptr) {
1586 oop_map_cache = new OopMapCache();
1587 // Ensure _oop_map_cache is stable, since it is examined without a lock
1588 Atomic::release_store(&_oop_map_cache, oop_map_cache);
1589 }
1590 }
1591 // _oop_map_cache is constant after init; lookup below does its own locking.
1592 oop_map_cache->lookup(method, bci, entry_for);
1593 }
1594
1595 bool InstanceKlass::contains_field_offset(int offset) {
1596 fieldDescriptor fd;
1597 return find_field_from_offset(offset, false, &fd);
1598 }
1599
1600 FieldInfo InstanceKlass::field(int index) const {
1601 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1602 if (fs.index() == index) {
1603 return fs.to_FieldInfo();
1604 }
1605 }
1606 fatal("Field not found");
1607 return FieldInfo();
1608 }
1609
1610 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1611 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1612 Symbol* f_name = fs.name();
1613 Symbol* f_sig = fs.signature();
1614 if (f_name == name && f_sig == sig) {
1615 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1616 return true;
1617 }
1618 }
1660
1661 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1662 // search order according to newest JVM spec (5.4.3.2, p.167).
1663 // 1) search for field in current klass
1664 if (find_local_field(name, sig, fd)) {
1665 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1666 }
1667 // 2) search for field recursively in direct superinterfaces
1668 if (is_static) {
1669 Klass* intf = find_interface_field(name, sig, fd);
1670 if (intf != nullptr) return intf;
1671 }
1672 // 3) apply field lookup recursively if superclass exists
1673 { Klass* supr = super();
1674 if (supr != nullptr) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1675 }
1676 // 4) otherwise field lookup fails
1677 return nullptr;
1678 }
1679
1680
1681 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1682 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1683 if (fs.offset() == offset) {
1684 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1685 if (fd->is_static() == is_static) return true;
1686 }
1687 }
1688 return false;
1689 }
1690
1691
1692 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1693 Klass* klass = const_cast<InstanceKlass*>(this);
1694 while (klass != nullptr) {
1695 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1696 return true;
1697 }
1698 klass = klass->super();
1699 }
2052 }
2053
2054 // uncached_lookup_method searches both the local class methods array and all
2055 // superclasses methods arrays, skipping any overpass methods in superclasses,
2056 // and possibly skipping private methods.
2057 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2058 const Symbol* signature,
2059 OverpassLookupMode overpass_mode,
2060 PrivateLookupMode private_mode) const {
2061 OverpassLookupMode overpass_local_mode = overpass_mode;
2062 const Klass* klass = this;
2063 while (klass != nullptr) {
2064 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2065 signature,
2066 overpass_local_mode,
2067 StaticLookupMode::find,
2068 private_mode);
2069 if (method != nullptr) {
2070 return method;
2071 }
2072 klass = klass->super();
2073 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2074 }
2075 return nullptr;
2076 }
2077
2078 #ifdef ASSERT
2079 // search through class hierarchy and return true if this class or
2080 // one of the superclasses was redefined
2081 bool InstanceKlass::has_redefined_this_or_super() const {
2082 const Klass* klass = this;
2083 while (klass != nullptr) {
2084 if (InstanceKlass::cast(klass)->has_been_redefined()) {
2085 return true;
2086 }
2087 klass = klass->super();
2088 }
2089 return false;
2090 }
2091 #endif
2532 int method_table_offset_in_words = ioe->offset()/wordSize;
2533 int itable_offset_in_words = (int)(start_of_itable() - (intptr_t*)this);
2534
2535 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words)
2536 / itableOffsetEntry::size();
2537
2538 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2539 if (ioe->interface_klass() != nullptr) {
2540 it->push(ioe->interface_klass_addr());
2541 itableMethodEntry* ime = ioe->first_method_entry(this);
2542 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2543 for (int index = 0; index < n; index ++) {
2544 it->push(ime[index].method_addr());
2545 }
2546 }
2547 }
2548 }
2549
2550 it->push(&_nest_members);
2551 it->push(&_permitted_subclasses);
2552 it->push(&_record_components);
2553 }
2554
2555 #if INCLUDE_CDS
2556 void InstanceKlass::remove_unshareable_info() {
2557
2558 if (is_linked()) {
2559 assert(can_be_verified_at_dumptime(), "must be");
2560 // Remember this so we can avoid walking the hierarchy at runtime.
2561 set_verified_at_dump_time();
2562 }
2563
2564 Klass::remove_unshareable_info();
2565
2566 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2567 // Classes are attempted to link during dumping and may fail,
2568 // but these classes are still in the dictionary and class list in CLD.
2569 // If the class has failed verification, there is nothing else to remove.
2570 return;
2571 }
2572
2576 // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2577 _init_state = allocated;
2578
2579 { // Otherwise this needs to take out the Compile_lock.
2580 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2581 init_implementor();
2582 }
2583
2584 constants()->remove_unshareable_info();
2585
2586 for (int i = 0; i < methods()->length(); i++) {
2587 Method* m = methods()->at(i);
2588 m->remove_unshareable_info();
2589 }
2590
2591 // do array classes also.
2592 if (array_klasses() != nullptr) {
2593 array_klasses()->remove_unshareable_info();
2594 }
2595
2596 // These are not allocated from metaspace. They are safe to set to null.
2597 _source_debug_extension = nullptr;
2598 _dep_context = nullptr;
2599 _osr_nmethods_head = nullptr;
2600 #if INCLUDE_JVMTI
2601 _breakpoints = nullptr;
2602 _previous_versions = nullptr;
2603 _cached_class_file = nullptr;
2604 _jvmti_cached_class_field_map = nullptr;
2605 #endif
2606
2607 _init_thread = nullptr;
2608 _methods_jmethod_ids = nullptr;
2609 _jni_ids = nullptr;
2610 _oop_map_cache = nullptr;
2611 // clear _nest_host to ensure re-load at runtime
2612 _nest_host = nullptr;
2613 init_shared_package_entry();
2614 _dep_context_last_cleaned = 0;
2615 _init_monitor = nullptr;
2616
2660 void InstanceKlass::compute_has_loops_flag_for_methods() {
2661 Array<Method*>* methods = this->methods();
2662 for (int index = 0; index < methods->length(); ++index) {
2663 Method* m = methods->at(index);
2664 if (!m->is_overpass()) { // work around JDK-8305771
2665 m->compute_has_loops_flag();
2666 }
2667 }
2668 }
2669
2670 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2671 PackageEntry* pkg_entry, TRAPS) {
2672 // InstanceKlass::add_to_hierarchy() sets the init_state to loaded
2673 // before the InstanceKlass is added to the SystemDictionary. Make
2674 // sure the current state is <loaded.
2675 assert(!is_loaded(), "invalid init state");
2676 assert(!shared_loading_failed(), "Must not try to load failed class again");
2677 set_package(loader_data, pkg_entry, CHECK);
2678 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2679
2680 Array<Method*>* methods = this->methods();
2681 int num_methods = methods->length();
2682 for (int index = 0; index < num_methods; ++index) {
2683 methods->at(index)->restore_unshareable_info(CHECK);
2684 }
2685 #if INCLUDE_JVMTI
2686 if (JvmtiExport::has_redefined_a_class()) {
2687 // Reinitialize vtable because RedefineClasses may have changed some
2688 // entries in this vtable for super classes so the CDS vtable might
2689 // point to old or obsolete entries. RedefineClasses doesn't fix up
2690 // vtables in the shared system dictionary, only the main one.
2691 // It also redefines the itable too so fix that too.
2692 // First fix any default methods that point to a super class that may
2693 // have been redefined.
2694 bool trace_name_printed = false;
2695 adjust_default_methods(&trace_name_printed);
2696 vtable().initialize_vtable();
2697 itable().initialize_itable();
2698 }
2699 #endif
2886 } else {
2887 // Adding one to the attribute length in order to store a null terminator
2888 // character could cause an overflow because the attribute length is
2889 // already coded with an u4 in the classfile, but in practice, it's
2890 // unlikely to happen.
2891 assert((length+1) > length, "Overflow checking");
2892 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2893 for (int i = 0; i < length; i++) {
2894 sde[i] = array[i];
2895 }
2896 sde[length] = '\0';
2897 _source_debug_extension = sde;
2898 }
2899 }
2900
2901 Symbol* InstanceKlass::generic_signature() const { return _constants->generic_signature(); }
2902 u2 InstanceKlass::generic_signature_index() const { return _constants->generic_signature_index(); }
2903 void InstanceKlass::set_generic_signature_index(u2 sig_index) { _constants->set_generic_signature_index(sig_index); }
2904
2905 const char* InstanceKlass::signature_name() const {
2906
2907 // Get the internal name as a c string
2908 const char* src = (const char*) (name()->as_C_string());
2909 const int src_length = (int)strlen(src);
2910
2911 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
2912
2913 // Add L as type indicator
2914 int dest_index = 0;
2915 dest[dest_index++] = JVM_SIGNATURE_CLASS;
2916
2917 // Add the actual class name
2918 for (int src_index = 0; src_index < src_length; ) {
2919 dest[dest_index++] = src[src_index++];
2920 }
2921
2922 if (is_hidden()) { // Replace the last '+' with a '.'.
2923 for (int index = (int)src_length; index > 0; index--) {
2924 if (dest[index] == '+') {
2925 dest[index] = JVM_SIGNATURE_DOT;
2926 break;
2927 }
2928 }
2929 }
2930
2931 // Add the semicolon and the null
2932 dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
2933 dest[dest_index] = '\0';
2934 return dest;
2935 }
3237 jint InstanceKlass::compute_modifier_flags() const {
3238 jint access = access_flags().as_int();
3239
3240 // But check if it happens to be member class.
3241 InnerClassesIterator iter(this);
3242 for (; !iter.done(); iter.next()) {
3243 int ioff = iter.inner_class_info_index();
3244 // Inner class attribute can be zero, skip it.
3245 // Strange but true: JVM spec. allows null inner class refs.
3246 if (ioff == 0) continue;
3247
3248 // only look at classes that are already loaded
3249 // since we are looking for the flags for our self.
3250 Symbol* inner_name = constants()->klass_name_at(ioff);
3251 if (name() == inner_name) {
3252 // This is really a member class.
3253 access = iter.inner_access_flags();
3254 break;
3255 }
3256 }
3257 // Remember to strip ACC_SUPER bit
3258 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
3259 }
3260
3261 jint InstanceKlass::jvmti_class_status() const {
3262 jint result = 0;
3263
3264 if (is_linked()) {
3265 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3266 }
3267
3268 if (is_initialized()) {
3269 assert(is_linked(), "Class status is not consistent");
3270 result |= JVMTI_CLASS_STATUS_INITIALIZED;
3271 }
3272 if (is_in_error_state()) {
3273 result |= JVMTI_CLASS_STATUS_ERROR;
3274 }
3275 return result;
3276 }
3277
3278 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {
3495 }
3496 osr = osr->osr_link();
3497 }
3498
3499 assert(match_level == false || best == nullptr, "shouldn't pick up anything if match_level is set");
3500 if (best != nullptr && best->comp_level() >= comp_level) {
3501 return best;
3502 }
3503 return nullptr;
3504 }
3505
3506 // -----------------------------------------------------------------------------------------------------
3507 // Printing
3508
3509 #define BULLET " - "
3510
3511 static const char* state_names[] = {
3512 "allocated", "loaded", "being_linked", "linked", "being_initialized", "fully_initialized", "initialization_error"
3513 };
3514
3515 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3516 for (int i = 0; i < len; i++) {
3517 intptr_t e = start[i];
3518 st->print("%d : " INTPTR_FORMAT, i, e);
3519 if (MetaspaceObj::is_valid((Metadata*)e)) {
3520 st->print(" ");
3521 ((Metadata*)e)->print_value_on(st);
3522 }
3523 st->cr();
3524 }
3525 }
3526
3527 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3528 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3529 }
3530
3531 const char* InstanceKlass::init_state_name() const {
3532 return state_names[init_state()];
3533 }
3534
3535 void InstanceKlass::print_on(outputStream* st) const {
3536 assert(is_klass(), "must be klass");
3537 Klass::print_on(st);
3538
3539 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3540 st->print(BULLET"klass size: %d", size()); st->cr();
3541 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3542 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3543 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3544 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3545 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3546 st->print(BULLET"sub: ");
3547 Klass* sub = subklass();
3548 int n;
3549 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3550 if (n < MaxSubklassPrintSize) {
3551 sub->print_value_on(st);
3552 st->print(" ");
3553 }
3554 }
3555 if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3556 st->cr();
3557
3558 if (is_interface()) {
3559 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3560 if (nof_implementors() == 1) {
3561 st->print_cr(BULLET"implementor: ");
3562 st->print(" ");
3563 implementor()->print_value_on(st);
3564 st->cr();
3565 }
3566 }
3567
3568 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3569 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
3570 if (Verbose || WizardMode) {
3571 Array<Method*>* method_array = methods();
3572 for (int i = 0; i < method_array->length(); i++) {
3573 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3574 }
3575 }
3576 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
3577 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
3578 if (Verbose && default_methods() != nullptr) {
3579 Array<Method*>* method_array = default_methods();
3580 for (int i = 0; i < method_array->length(); i++) {
3581 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3582 }
3583 }
3584 if (default_vtable_indices() != nullptr) {
3585 st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr();
3586 }
3587 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
3588 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3589 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
3590 if (class_loader_data() != nullptr) {
3591 st->print(BULLET"class loader data: ");
3592 class_loader_data()->print_value_on(st);
3593 st->cr();
3594 }
3595 if (source_file_name() != nullptr) {
3596 st->print(BULLET"source file: ");
3597 source_file_name()->print_value_on(st);
3598 st->cr();
3599 }
3600 if (source_debug_extension() != nullptr) {
3601 st->print(BULLET"source debug extension: ");
3602 st->print("%s", source_debug_extension());
3603 st->cr();
3604 }
3605 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
3606 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
3607 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
3608 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
3614 pv_node = pv_node->previous_versions()) {
3615 if (!have_pv)
3616 st->print(BULLET"previous version: ");
3617 have_pv = true;
3618 pv_node->constants()->print_value_on(st);
3619 }
3620 if (have_pv) st->cr();
3621 }
3622
3623 if (generic_signature() != nullptr) {
3624 st->print(BULLET"generic signature: ");
3625 generic_signature()->print_value_on(st);
3626 st->cr();
3627 }
3628 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
3629 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
3630 if (record_components() != nullptr) {
3631 st->print(BULLET"record components: "); record_components()->print_value_on(st); st->cr();
3632 }
3633 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
3634 if (java_mirror() != nullptr) {
3635 st->print(BULLET"java mirror: ");
3636 java_mirror()->print_value_on(st);
3637 st->cr();
3638 } else {
3639 st->print_cr(BULLET"java mirror: null");
3640 }
3641 st->print(BULLET"vtable length %d (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3642 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
3643 st->print(BULLET"itable length %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3644 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
3645 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3646 FieldPrinter print_static_field(st);
3647 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3648 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3649 FieldPrinter print_nonstatic_field(st);
3650 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3651 ik->print_nonstatic_fields(&print_nonstatic_field);
3652
3653 st->print(BULLET"non-static oop maps: ");
3654 OopMapBlock* map = start_of_nonstatic_oop_maps();
3655 OopMapBlock* end_map = map + nonstatic_oop_map_count();
3656 while (map < end_map) {
3657 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3658 map++;
3659 }
3660 st->cr();
3661 }
3662
3663 void InstanceKlass::print_value_on(outputStream* st) const {
3664 assert(is_klass(), "must be klass");
|
53 #include "logging/logStream.hpp"
54 #include "memory/allocation.inline.hpp"
55 #include "memory/iterator.inline.hpp"
56 #include "memory/metadataFactory.hpp"
57 #include "memory/metaspaceClosure.hpp"
58 #include "memory/oopFactory.hpp"
59 #include "memory/resourceArea.hpp"
60 #include "memory/universe.hpp"
61 #include "oops/fieldStreams.inline.hpp"
62 #include "oops/constantPool.hpp"
63 #include "oops/instanceClassLoaderKlass.hpp"
64 #include "oops/instanceKlass.inline.hpp"
65 #include "oops/instanceMirrorKlass.hpp"
66 #include "oops/instanceOop.hpp"
67 #include "oops/instanceStackChunkKlass.hpp"
68 #include "oops/klass.inline.hpp"
69 #include "oops/method.hpp"
70 #include "oops/oop.inline.hpp"
71 #include "oops/recordComponent.hpp"
72 #include "oops/symbol.hpp"
73 #include "oops/inlineKlass.hpp"
74 #include "prims/jvmtiExport.hpp"
75 #include "prims/jvmtiRedefineClasses.hpp"
76 #include "prims/jvmtiThreadState.hpp"
77 #include "prims/methodComparator.hpp"
78 #include "runtime/arguments.hpp"
79 #include "runtime/deoptimization.hpp"
80 #include "runtime/atomic.hpp"
81 #include "runtime/fieldDescriptor.inline.hpp"
82 #include "runtime/handles.inline.hpp"
83 #include "runtime/javaCalls.hpp"
84 #include "runtime/javaThread.inline.hpp"
85 #include "runtime/mutexLocker.hpp"
86 #include "runtime/orderAccess.hpp"
87 #include "runtime/os.inline.hpp"
88 #include "runtime/reflectionUtils.hpp"
89 #include "runtime/threads.hpp"
90 #include "services/classLoadingService.hpp"
91 #include "services/finalizerService.hpp"
92 #include "services/threadService.hpp"
93 #include "utilities/dtrace.hpp"
150
151 static inline bool is_class_loader(const Symbol* class_name,
152 const ClassFileParser& parser) {
153 assert(class_name != nullptr, "invariant");
154
155 if (class_name == vmSymbols::java_lang_ClassLoader()) {
156 return true;
157 }
158
159 if (vmClasses::ClassLoader_klass_loaded()) {
160 const Klass* const super_klass = parser.super_klass();
161 if (super_klass != nullptr) {
162 if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
163 return true;
164 }
165 }
166 }
167 return false;
168 }
169
170 bool InstanceKlass::field_is_null_free_inline_type(int index) const {
171 return field(index).field_flags().is_null_free_inline_type();
172 }
173
174 static inline bool is_stack_chunk_class(const Symbol* class_name,
175 const ClassLoaderData* loader_data) {
176 return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
177 loader_data->is_the_null_class_loader_data());
178 }
179
180 // private: called to verify that k is a static member of this nest.
181 // We know that k is an instance class in the same package and hence the
182 // same classloader.
183 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
184 assert(!is_hidden(), "unexpected hidden class");
185 if (_nest_members == nullptr || _nest_members == Universe::the_empty_short_array()) {
186 if (log_is_enabled(Trace, class, nestmates)) {
187 ResourceMark rm(current);
188 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
189 k->external_name(), this->external_name());
190 }
191 return false;
192 }
193
426 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
427 this->external_name(),
428 access ? "" : "NOT ",
429 k->external_name());
430 return access;
431 }
432
433 const char* InstanceKlass::nest_host_error() {
434 if (_nest_host_index == 0) {
435 return nullptr;
436 } else {
437 constantPoolHandle cph(Thread::current(), constants());
438 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
439 }
440 }
441
442 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
443 const int size = InstanceKlass::size(parser.vtable_size(),
444 parser.itable_size(),
445 nonstatic_oop_map_size(parser.total_oop_map_count()),
446 parser.is_interface(),
447 parser.has_inline_fields() ? parser.java_fields_count() : 0,
448 parser.is_inline_type());
449
450 const Symbol* const class_name = parser.class_name();
451 assert(class_name != nullptr, "invariant");
452 ClassLoaderData* loader_data = parser.loader_data();
453 assert(loader_data != nullptr, "invariant");
454
455 InstanceKlass* ik;
456
457 // Allocation
458 if (parser.is_instance_ref_klass()) {
459 // java.lang.ref.Reference
460 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
461 } else if (class_name == vmSymbols::java_lang_Class()) {
462 // mirror - java.lang.Class
463 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
464 } else if (is_stack_chunk_class(class_name, loader_data)) {
465 // stack chunk
466 ik = new (loader_data, size, THREAD) InstanceStackChunkKlass(parser);
467 } else if (is_class_loader(class_name, parser)) {
468 // class loader - java.lang.ClassLoader
469 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
470 } else if (parser.is_inline_type()) {
471 // inline type
472 ik = new (loader_data, size, THREAD) InlineKlass(parser);
473 } else {
474 // normal
475 ik = new (loader_data, size, THREAD) InstanceKlass(parser);
476 }
477
478 // Check for pending exception before adding to the loader data and incrementing
479 // class count. Can get OOM here.
480 if (HAS_PENDING_EXCEPTION) {
481 return nullptr;
482 }
483
484 #ifdef ASSERT
485 ik->bounds_check((address) ik->start_of_vtable(), false, size);
486 ik->bounds_check((address) ik->start_of_itable(), false, size);
487 ik->bounds_check((address) ik->end_of_itable(), true, size);
488 ik->bounds_check((address) ik->end_of_nonstatic_oop_maps(), true, size);
489 #endif //ASSERT
490 return ik;
491 }
492
493 #ifndef PRODUCT
494 bool InstanceKlass::bounds_check(address addr, bool edge_ok, intptr_t size_in_bytes) const {
495 const char* bad = nullptr;
496 address end = nullptr;
497 if (addr < (address)this) {
498 bad = "before";
499 } else if (addr == (address)this) {
500 if (edge_ok) return true;
501 bad = "just before";
502 } else if (addr == (end = (address)this + sizeof(intptr_t) * (size_in_bytes < 0 ? size() : size_in_bytes))) {
503 if (edge_ok) return true;
504 bad = "just after";
505 } else if (addr > end) {
506 bad = "after";
507 } else {
508 return true;
509 }
510 tty->print_cr("%s object bounds: " INTPTR_FORMAT " [" INTPTR_FORMAT ".." INTPTR_FORMAT "]",
511 bad, (intptr_t)addr, (intptr_t)this, (intptr_t)end);
512 Verbose = WizardMode = true; this->print(); //@@
513 return false;
514 }
515 #endif //PRODUCT
516
517 // copy method ordering from resource area to Metaspace
518 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
519 if (m != nullptr) {
520 // allocate a new array and copy contents (memcpy?)
521 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
522 for (int i = 0; i < m->length(); i++) {
523 _method_ordering->at_put(i, m->at(i));
524 }
525 } else {
526 _method_ordering = Universe::the_empty_int_array();
527 }
528 }
529
530 // create a new array of vtable_indices for default methods
531 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
532 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
533 assert(default_vtable_indices() == nullptr, "only create once");
534 set_default_vtable_indices(vtable_indices);
535 return vtable_indices;
536 }
537
538 static Monitor* create_init_monitor(const char* name) {
539 return new Monitor(Mutex::safepoint, name);
540 }
541
542 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
543 Klass(kind),
544 _nest_members(nullptr),
545 _nest_host(nullptr),
546 _permitted_subclasses(nullptr),
547 _record_components(nullptr),
548 _static_field_size(parser.static_field_size()),
549 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
550 _itable_len(parser.itable_size()),
551 _nest_host_index(0),
552 _init_state(allocated),
553 _reference_type(reference_type),
554 _init_monitor(create_init_monitor("InstanceKlassInitMonitor_lock")),
555 _init_thread(nullptr),
556 _inline_type_field_klasses(nullptr),
557 _preload_classes(nullptr),
558 _adr_inlineklass_fixed_block(nullptr)
559 {
560 set_vtable_length(parser.vtable_size());
561 set_access_flags(parser.access_flags());
562 if (parser.is_hidden()) set_is_hidden();
563 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
564 false));
565 if (parser.has_inline_fields()) {
566 set_has_inline_type_fields();
567 }
568
569 assert(nullptr == _methods, "underlying memory not zeroed?");
570 assert(is_instance_klass(), "is layout incorrect?");
571 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
572
573 if (has_inline_type_fields()) {
574 _inline_type_field_klasses = (const Klass**) adr_inline_type_field_klasses();
575 }
576 }
577
578 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
579 Array<Method*>* methods) {
580 if (methods != nullptr && methods != Universe::the_empty_method_array() &&
581 !methods->is_shared()) {
582 for (int i = 0; i < methods->length(); i++) {
583 Method* method = methods->at(i);
584 if (method == nullptr) continue; // maybe null if error processing
585 // Only want to delete methods that are not executing for RedefineClasses.
586 // The previous version will point to them so they're not totally dangling
587 assert (!method->on_stack(), "shouldn't be called with methods on stack");
588 MetadataFactory::free_metadata(loader_data, method);
589 }
590 MetadataFactory::free_array<Method*>(loader_data, methods);
591 }
592 }
593
594 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
595 const Klass* super_klass,
725 inner_classes() != Universe::the_empty_short_array() &&
726 !inner_classes()->is_shared()) {
727 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
728 }
729 set_inner_classes(nullptr);
730
731 if (nest_members() != nullptr &&
732 nest_members() != Universe::the_empty_short_array() &&
733 !nest_members()->is_shared()) {
734 MetadataFactory::free_array<jushort>(loader_data, nest_members());
735 }
736 set_nest_members(nullptr);
737
738 if (permitted_subclasses() != nullptr &&
739 permitted_subclasses() != Universe::the_empty_short_array() &&
740 !permitted_subclasses()->is_shared()) {
741 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
742 }
743 set_permitted_subclasses(nullptr);
744
745 if (preload_classes() != nullptr &&
746 preload_classes() != Universe::the_empty_short_array() &&
747 !preload_classes()->is_shared()) {
748 MetadataFactory::free_array<jushort>(loader_data, preload_classes());
749 }
750
751 // We should deallocate the Annotations instance if it's not in shared spaces.
752 if (annotations() != nullptr && !annotations()->is_shared()) {
753 MetadataFactory::free_metadata(loader_data, annotations());
754 }
755 set_annotations(nullptr);
756
757 SystemDictionaryShared::handle_class_unloading(this);
758
759 #if INCLUDE_CDS_JAVA_HEAP
760 if (DumpSharedSpaces) {
761 HeapShared::remove_scratch_objects(this);
762 }
763 #endif
764 }
765
766 bool InstanceKlass::is_record() const {
767 return _record_components != nullptr &&
768 is_final() &&
769 java_super() == vmClasses::Record_klass();
770 }
887 vmSymbols::java_lang_IncompatibleClassChangeError(),
888 "class %s has interface %s as super class",
889 external_name(),
890 super_klass->external_name()
891 );
892 return false;
893 }
894
895 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
896 ik_super->link_class_impl(CHECK_false);
897 }
898
899 // link all interfaces implemented by this class before linking this class
900 Array<InstanceKlass*>* interfaces = local_interfaces();
901 int num_interfaces = interfaces->length();
902 for (int index = 0; index < num_interfaces; index++) {
903 InstanceKlass* interk = interfaces->at(index);
904 interk->link_class_impl(CHECK_false);
905 }
906
907
908 // If a class declares a method that uses an inline class as an argument
909 // type or return inline type, this inline class must be loaded during the
910 // linking of this class because size and properties of the inline class
911 // must be known in order to be able to perform inline type optimizations.
912 // The implementation below is an approximation of this rule, the code
913 // iterates over all methods of the current class (including overridden
914 // methods), not only the methods declared by this class. This
915 // approximation makes the code simpler, and doesn't change the semantic
916 // because classes declaring methods overridden by the current class are
917 // linked (and have performed their own pre-loading) before the linking
918 // of the current class.
919
920
921 // Note:
922 // Inline class types are loaded during
923 // the loading phase (see ClassFileParser::post_process_parsed_stream()).
924 // Inline class types used as element types for array creation
925 // are not pre-loaded. Their loading is triggered by either anewarray
926 // or multianewarray bytecodes.
927
928 // Could it be possible to do the following processing only if the
929 // class uses inline types?
930 if (EnableValhalla) {
931 ResourceMark rm(THREAD);
932 if (EnablePrimitiveClasses) {
933 for (int i = 0; i < methods()->length(); i++) {
934 Method* m = methods()->at(i);
935 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
936 if (ss.is_reference()) {
937 if (ss.is_array()) {
938 continue;
939 }
940 if (ss.type() == T_PRIMITIVE_OBJECT) {
941 Symbol* symb = ss.as_symbol();
942 if (symb == name()) continue;
943 oop loader = class_loader();
944 oop protection_domain = this->protection_domain();
945 Klass* klass = SystemDictionary::resolve_or_fail(symb,
946 Handle(THREAD, loader), Handle(THREAD, protection_domain), true,
947 CHECK_false);
948 if (klass == nullptr) {
949 THROW_(vmSymbols::java_lang_LinkageError(), false);
950 }
951 if (!klass->is_inline_klass()) {
952 Exceptions::fthrow(
953 THREAD_AND_LOCATION,
954 vmSymbols::java_lang_IncompatibleClassChangeError(),
955 "class %s is not an inline type",
956 klass->external_name());
957 }
958 }
959 }
960 }
961 }
962 }
963 // Aggressively preloading all classes from the Preload attribute
964 if (preload_classes() != nullptr) {
965 for (int i = 0; i < preload_classes()->length(); i++) {
966 if (constants()->tag_at(preload_classes()->at(i)).is_klass()) continue;
967 Symbol* class_name = constants()->klass_at_noresolve(preload_classes()->at(i));
968 if (class_name == name()) continue;
969 oop loader = class_loader();
970 oop protection_domain = this->protection_domain();
971 Klass* klass = SystemDictionary::resolve_or_null(class_name,
972 Handle(THREAD, loader), Handle(THREAD, protection_domain), THREAD);
973 if (HAS_PENDING_EXCEPTION) {
974 CLEAR_PENDING_EXCEPTION;
975 }
976 if (klass != nullptr) {
977 log_info(class, preload)("Preloading class %s during linking of class %s because of its Preload attribute", class_name->as_C_string(), name()->as_C_string());
978 } else {
979 log_warning(class, preload)("Preloading of class %s during linking of class %s (Preload attribute) failed", class_name->as_C_string(), name()->as_C_string());
980 }
981 }
982 }
983 }
984
985 // in case the class is linked in the process of linking its superclasses
986 if (is_linked()) {
987 return true;
988 }
989
990 // trace only the link time for this klass that includes
991 // the verification time
992 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
993 ClassLoader::perf_class_link_selftime(),
994 ClassLoader::perf_classes_linked(),
995 jt->get_thread_stat()->perf_recursion_counts_addr(),
996 jt->get_thread_stat()->perf_timers_addr(),
997 PerfClassTraceTime::CLASS_LINK);
998
999 // verification & rewriting
1000 {
1001 LockLinkState init_lock(this, jt);
1002
1003 // rewritten will have been set if loader constraint error found
1004 // on an earlier link attempt
1226 set_init_thread(jt);
1227 }
1228 }
1229
1230 // Throw error outside lock
1231 if (throw_error) {
1232 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1233 ResourceMark rm(THREAD);
1234 Handle cause(THREAD, get_initialization_error(THREAD));
1235
1236 stringStream ss;
1237 ss.print("Could not initialize class %s", external_name());
1238 if (cause.is_null()) {
1239 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1240 } else {
1241 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1242 ss.as_string(), cause);
1243 }
1244 }
1245
1246 // Pre-allocating an instance of the default value
1247 if (is_inline_klass()) {
1248 InlineKlass* vk = InlineKlass::cast(this);
1249 oop val = vk->allocate_instance(THREAD);
1250 if (HAS_PENDING_EXCEPTION) {
1251 Handle e(THREAD, PENDING_EXCEPTION);
1252 CLEAR_PENDING_EXCEPTION;
1253 {
1254 EXCEPTION_MARK;
1255 add_initialization_error(THREAD, e);
1256 // Locks object, set state, and notify all waiting threads
1257 set_initialization_state_and_notify(initialization_error, THREAD);
1258 CLEAR_PENDING_EXCEPTION;
1259 }
1260 THROW_OOP(e());
1261 }
1262 vk->set_default_value(val);
1263 }
1264
1265 // Step 7
1266 // Next, if C is a class rather than an interface, initialize it's super class and super
1267 // interfaces.
1268 if (!is_interface()) {
1269 Klass* super_klass = super();
1270 if (super_klass != nullptr && super_klass->should_be_initialized()) {
1271 super_klass->initialize(THREAD);
1272 }
1273 // If C implements any interface that declares a non-static, concrete method,
1274 // the initialization of C triggers initialization of its super interfaces.
1275 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1276 // having a superinterface that declares, non-static, concrete methods
1277 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1278 initialize_super_interfaces(THREAD);
1279 }
1280
1281 // If any exceptions, complete abruptly, throwing the same exception as above.
1282 if (HAS_PENDING_EXCEPTION) {
1283 Handle e(THREAD, PENDING_EXCEPTION);
1284 CLEAR_PENDING_EXCEPTION;
1285 {
1286 EXCEPTION_MARK;
1287 add_initialization_error(THREAD, e);
1288 // Locks object, set state, and notify all waiting threads
1289 set_initialization_state_and_notify(initialization_error, THREAD);
1290 CLEAR_PENDING_EXCEPTION;
1291 }
1292 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1293 THROW_OOP(e());
1294 }
1295 }
1296
1297 // Step 8
1298 // Initialize classes of inline fields
1299 if (EnablePrimitiveClasses) {
1300 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1301 if (fs.is_null_free_inline_type()) {
1302 Klass* klass = get_inline_type_field_klass_or_null(fs.index());
1303 if (fs.access_flags().is_static() && klass == nullptr) {
1304 klass = SystemDictionary::resolve_or_fail(field_signature(fs.index())->fundamental_name(THREAD),
1305 Handle(THREAD, class_loader()),
1306 Handle(THREAD, protection_domain()),
1307 true, THREAD);
1308 set_inline_type_field_klass(fs.index(), klass);
1309 }
1310
1311 if (!HAS_PENDING_EXCEPTION) {
1312 assert(klass != nullptr, "Must be");
1313 InstanceKlass::cast(klass)->initialize(THREAD);
1314 if (fs.access_flags().is_static()) {
1315 if (java_mirror()->obj_field(fs.offset()) == nullptr) {
1316 java_mirror()->obj_field_put(fs.offset(), InlineKlass::cast(klass)->default_value());
1317 }
1318 }
1319 }
1320
1321 if (HAS_PENDING_EXCEPTION) {
1322 Handle e(THREAD, PENDING_EXCEPTION);
1323 CLEAR_PENDING_EXCEPTION;
1324 {
1325 EXCEPTION_MARK;
1326 add_initialization_error(THREAD, e);
1327 // Locks object, set state, and notify all waiting threads
1328 set_initialization_state_and_notify(initialization_error, THREAD);
1329 CLEAR_PENDING_EXCEPTION;
1330 }
1331 THROW_OOP(e());
1332 }
1333 }
1334 }
1335 }
1336
1337
1338 // Step 9
1339 {
1340 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1341 if (class_initializer() != nullptr) {
1342 // Timer includes any side effects of class initialization (resolution,
1343 // etc), but not recursive entry into call_class_initializer().
1344 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1345 ClassLoader::perf_class_init_selftime(),
1346 ClassLoader::perf_classes_inited(),
1347 jt->get_thread_stat()->perf_recursion_counts_addr(),
1348 jt->get_thread_stat()->perf_timers_addr(),
1349 PerfClassTraceTime::CLASS_CLINIT);
1350 call_class_initializer(THREAD);
1351 } else {
1352 // The elapsed time is so small it's not worth counting.
1353 if (UsePerfData) {
1354 ClassLoader::perf_classes_inited()->inc();
1355 }
1356 call_class_initializer(THREAD);
1357 }
1358 }
1359
1360 // Step 10
1361 if (!HAS_PENDING_EXCEPTION) {
1362 set_initialization_state_and_notify(fully_initialized, THREAD);
1363 debug_only(vtable().verify(tty, true);)
1364 }
1365 else {
1366 // Step 11 and 12
1367 Handle e(THREAD, PENDING_EXCEPTION);
1368 CLEAR_PENDING_EXCEPTION;
1369 // JVMTI has already reported the pending exception
1370 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1371 JvmtiExport::clear_detected_exception(jt);
1372 {
1373 EXCEPTION_MARK;
1374 add_initialization_error(THREAD, e);
1375 set_initialization_state_and_notify(initialization_error, THREAD);
1376 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1377 // JVMTI has already reported the pending exception
1378 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1379 JvmtiExport::clear_detected_exception(jt);
1380 }
1381 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1382 if (e->is_a(vmClasses::Error_klass())) {
1383 THROW_OOP(e());
1384 } else {
1385 JavaCallArguments args(e);
1386 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1672 : vmSymbols::java_lang_InstantiationException(), external_name());
1673 }
1674 if (this == vmClasses::Class_klass()) {
1675 ResourceMark rm(THREAD);
1676 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1677 : vmSymbols::java_lang_IllegalAccessException(), external_name());
1678 }
1679 }
1680
1681 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1682 // Need load-acquire for lock-free read
1683 if (array_klasses_acquire() == nullptr) {
1684 ResourceMark rm(THREAD);
1685 JavaThread *jt = THREAD;
1686 {
1687 // Atomic creation of array_klasses
1688 MutexLocker ma(THREAD, MultiArray_lock);
1689
1690 // Check if update has already taken place
1691 if (array_klasses() == nullptr) {
1692 ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this,
1693 false, false, CHECK_NULL);
1694 // use 'release' to pair with lock-free load
1695 release_set_array_klasses(k);
1696 }
1697 }
1698 }
1699 // array_klasses() will always be set at this point
1700 ArrayKlass* ak = array_klasses();
1701 return ak->array_klass(n, THREAD);
1702 }
1703
1704 Klass* InstanceKlass::array_klass_or_null(int n) {
1705 // Need load-acquire for lock-free read
1706 ArrayKlass* ak = array_klasses_acquire();
1707 if (ak == nullptr) {
1708 return nullptr;
1709 } else {
1710 return ak->array_klass_or_null(n);
1711 }
1712 }
1713
1714 Klass* InstanceKlass::array_klass(TRAPS) {
1715 return array_klass(1, THREAD);
1716 }
1717
1718 Klass* InstanceKlass::array_klass_or_null() {
1719 return array_klass_or_null(1);
1720 }
1721
1722 static int call_class_initializer_counter = 0; // for debugging
1723
1724 Method* InstanceKlass::class_initializer() const {
1725 Method* clinit = find_method(
1726 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1727 if (clinit != nullptr && clinit->is_class_initializer()) {
1728 return clinit;
1729 }
1730 return nullptr;
1731 }
1732
1733 void InstanceKlass::call_class_initializer(TRAPS) {
1734 if (ReplayCompiles &&
1735 (ReplaySuppressInitializers == 1 ||
1736 (ReplaySuppressInitializers >= 2 && class_loader() != nullptr))) {
1737 // Hide the existence of the initializer for the purpose of replaying the compile
1738 return;
1739 }
1740
1741 #if INCLUDE_CDS
1742 // This is needed to ensure the consistency of the archived heap objects.
1743 if (has_archived_enum_objs()) {
1744 assert(is_shared(), "must be");
1745 bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
1746 if (initialized) {
1747 return;
1756 ResourceMark rm(THREAD);
1757 LogStream ls(lt);
1758 ls.print("%d Initializing ", call_class_initializer_counter++);
1759 name()->print_value_on(&ls);
1760 ls.print_cr("%s (" PTR_FORMAT ")", h_method() == nullptr ? "(no method)" : "", p2i(this));
1761 }
1762 if (h_method() != nullptr) {
1763 JavaCallArguments args; // No arguments
1764 JavaValue result(T_VOID);
1765 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1766 }
1767 }
1768
1769
1770 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1771 InterpreterOopMap* entry_for) {
1772 // Lazily create the _oop_map_cache at first request
1773 // Lock-free access requires load_acquire.
1774 OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1775 if (oop_map_cache == nullptr) {
1776 MutexLocker x(OopMapCacheAlloc_lock, Mutex::_no_safepoint_check_flag);
1777 // Check if _oop_map_cache was allocated while we were waiting for this lock
1778 if ((oop_map_cache = _oop_map_cache) == nullptr) {
1779 oop_map_cache = new OopMapCache();
1780 // Ensure _oop_map_cache is stable, since it is examined without a lock
1781 Atomic::release_store(&_oop_map_cache, oop_map_cache);
1782 }
1783 }
1784 // _oop_map_cache is constant after init; lookup below does its own locking.
1785 oop_map_cache->lookup(method, bci, entry_for);
1786 }
1787
1788
1789 FieldInfo InstanceKlass::field(int index) const {
1790 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1791 if (fs.index() == index) {
1792 return fs.to_FieldInfo();
1793 }
1794 }
1795 fatal("Field not found");
1796 return FieldInfo();
1797 }
1798
1799 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1800 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1801 Symbol* f_name = fs.name();
1802 Symbol* f_sig = fs.signature();
1803 if (f_name == name && f_sig == sig) {
1804 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1805 return true;
1806 }
1807 }
1849
1850 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1851 // search order according to newest JVM spec (5.4.3.2, p.167).
1852 // 1) search for field in current klass
1853 if (find_local_field(name, sig, fd)) {
1854 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1855 }
1856 // 2) search for field recursively in direct superinterfaces
1857 if (is_static) {
1858 Klass* intf = find_interface_field(name, sig, fd);
1859 if (intf != nullptr) return intf;
1860 }
1861 // 3) apply field lookup recursively if superclass exists
1862 { Klass* supr = super();
1863 if (supr != nullptr) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1864 }
1865 // 4) otherwise field lookup fails
1866 return nullptr;
1867 }
1868
1869 bool InstanceKlass::contains_field_offset(int offset) {
1870 if (this->is_inline_klass()) {
1871 InlineKlass* vk = InlineKlass::cast(this);
1872 return offset >= vk->first_field_offset() && offset < (vk->first_field_offset() + vk->get_exact_size_in_bytes());
1873 } else {
1874 fieldDescriptor fd;
1875 return find_field_from_offset(offset, false, &fd);
1876 }
1877 }
1878
1879 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1880 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1881 if (fs.offset() == offset) {
1882 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1883 if (fd->is_static() == is_static) return true;
1884 }
1885 }
1886 return false;
1887 }
1888
1889
1890 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1891 Klass* klass = const_cast<InstanceKlass*>(this);
1892 while (klass != nullptr) {
1893 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1894 return true;
1895 }
1896 klass = klass->super();
1897 }
2250 }
2251
2252 // uncached_lookup_method searches both the local class methods array and all
2253 // superclasses methods arrays, skipping any overpass methods in superclasses,
2254 // and possibly skipping private methods.
2255 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2256 const Symbol* signature,
2257 OverpassLookupMode overpass_mode,
2258 PrivateLookupMode private_mode) const {
2259 OverpassLookupMode overpass_local_mode = overpass_mode;
2260 const Klass* klass = this;
2261 while (klass != nullptr) {
2262 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2263 signature,
2264 overpass_local_mode,
2265 StaticLookupMode::find,
2266 private_mode);
2267 if (method != nullptr) {
2268 return method;
2269 }
2270 if (name == vmSymbols::object_initializer_name() ||
2271 name == vmSymbols::inline_factory_name()) {
2272 break; // <init> and <vnew> is never inherited
2273 }
2274 klass = klass->super();
2275 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2276 }
2277 return nullptr;
2278 }
2279
2280 #ifdef ASSERT
2281 // search through class hierarchy and return true if this class or
2282 // one of the superclasses was redefined
2283 bool InstanceKlass::has_redefined_this_or_super() const {
2284 const Klass* klass = this;
2285 while (klass != nullptr) {
2286 if (InstanceKlass::cast(klass)->has_been_redefined()) {
2287 return true;
2288 }
2289 klass = klass->super();
2290 }
2291 return false;
2292 }
2293 #endif
2734 int method_table_offset_in_words = ioe->offset()/wordSize;
2735 int itable_offset_in_words = (int)(start_of_itable() - (intptr_t*)this);
2736
2737 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words)
2738 / itableOffsetEntry::size();
2739
2740 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2741 if (ioe->interface_klass() != nullptr) {
2742 it->push(ioe->interface_klass_addr());
2743 itableMethodEntry* ime = ioe->first_method_entry(this);
2744 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2745 for (int index = 0; index < n; index ++) {
2746 it->push(ime[index].method_addr());
2747 }
2748 }
2749 }
2750 }
2751
2752 it->push(&_nest_members);
2753 it->push(&_permitted_subclasses);
2754 it->push(&_preload_classes);
2755 it->push(&_record_components);
2756
2757 if (has_inline_type_fields()) {
2758 for (int i = 0; i < java_fields_count(); i++) {
2759 it->push(&((Klass**)adr_inline_type_field_klasses())[i]);
2760 }
2761 }
2762 }
2763
2764 #if INCLUDE_CDS
2765 void InstanceKlass::remove_unshareable_info() {
2766
2767 if (is_linked()) {
2768 assert(can_be_verified_at_dumptime(), "must be");
2769 // Remember this so we can avoid walking the hierarchy at runtime.
2770 set_verified_at_dump_time();
2771 }
2772
2773 Klass::remove_unshareable_info();
2774
2775 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2776 // Classes are attempted to link during dumping and may fail,
2777 // but these classes are still in the dictionary and class list in CLD.
2778 // If the class has failed verification, there is nothing else to remove.
2779 return;
2780 }
2781
2785 // being added to class hierarchy (see InstanceKlass:::add_to_hierarchy()).
2786 _init_state = allocated;
2787
2788 { // Otherwise this needs to take out the Compile_lock.
2789 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2790 init_implementor();
2791 }
2792
2793 constants()->remove_unshareable_info();
2794
2795 for (int i = 0; i < methods()->length(); i++) {
2796 Method* m = methods()->at(i);
2797 m->remove_unshareable_info();
2798 }
2799
2800 // do array classes also.
2801 if (array_klasses() != nullptr) {
2802 array_klasses()->remove_unshareable_info();
2803 }
2804
2805 if (has_inline_type_fields()) {
2806 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
2807 if (fs.is_null_free_inline_type()) {
2808 reset_inline_type_field_klass(fs.index());
2809 }
2810 }
2811 }
2812
2813 // These are not allocated from metaspace. They are safe to set to nullptr.
2814 _source_debug_extension = nullptr;
2815 _dep_context = nullptr;
2816 _osr_nmethods_head = nullptr;
2817 #if INCLUDE_JVMTI
2818 _breakpoints = nullptr;
2819 _previous_versions = nullptr;
2820 _cached_class_file = nullptr;
2821 _jvmti_cached_class_field_map = nullptr;
2822 #endif
2823
2824 _init_thread = nullptr;
2825 _methods_jmethod_ids = nullptr;
2826 _jni_ids = nullptr;
2827 _oop_map_cache = nullptr;
2828 // clear _nest_host to ensure re-load at runtime
2829 _nest_host = nullptr;
2830 init_shared_package_entry();
2831 _dep_context_last_cleaned = 0;
2832 _init_monitor = nullptr;
2833
2877 void InstanceKlass::compute_has_loops_flag_for_methods() {
2878 Array<Method*>* methods = this->methods();
2879 for (int index = 0; index < methods->length(); ++index) {
2880 Method* m = methods->at(index);
2881 if (!m->is_overpass()) { // work around JDK-8305771
2882 m->compute_has_loops_flag();
2883 }
2884 }
2885 }
2886
2887 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2888 PackageEntry* pkg_entry, TRAPS) {
2889 // InstanceKlass::add_to_hierarchy() sets the init_state to loaded
2890 // before the InstanceKlass is added to the SystemDictionary. Make
2891 // sure the current state is <loaded.
2892 assert(!is_loaded(), "invalid init state");
2893 assert(!shared_loading_failed(), "Must not try to load failed class again");
2894 set_package(loader_data, pkg_entry, CHECK);
2895 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2896
2897 if (is_inline_klass()) {
2898 InlineKlass::cast(this)->initialize_calling_convention(CHECK);
2899 }
2900
2901 Array<Method*>* methods = this->methods();
2902 int num_methods = methods->length();
2903 for (int index = 0; index < num_methods; ++index) {
2904 methods->at(index)->restore_unshareable_info(CHECK);
2905 }
2906 #if INCLUDE_JVMTI
2907 if (JvmtiExport::has_redefined_a_class()) {
2908 // Reinitialize vtable because RedefineClasses may have changed some
2909 // entries in this vtable for super classes so the CDS vtable might
2910 // point to old or obsolete entries. RedefineClasses doesn't fix up
2911 // vtables in the shared system dictionary, only the main one.
2912 // It also redefines the itable too so fix that too.
2913 // First fix any default methods that point to a super class that may
2914 // have been redefined.
2915 bool trace_name_printed = false;
2916 adjust_default_methods(&trace_name_printed);
2917 vtable().initialize_vtable();
2918 itable().initialize_itable();
2919 }
2920 #endif
3107 } else {
3108 // Adding one to the attribute length in order to store a null terminator
3109 // character could cause an overflow because the attribute length is
3110 // already coded with an u4 in the classfile, but in practice, it's
3111 // unlikely to happen.
3112 assert((length+1) > length, "Overflow checking");
3113 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
3114 for (int i = 0; i < length; i++) {
3115 sde[i] = array[i];
3116 }
3117 sde[length] = '\0';
3118 _source_debug_extension = sde;
3119 }
3120 }
3121
3122 Symbol* InstanceKlass::generic_signature() const { return _constants->generic_signature(); }
3123 u2 InstanceKlass::generic_signature_index() const { return _constants->generic_signature_index(); }
3124 void InstanceKlass::set_generic_signature_index(u2 sig_index) { _constants->set_generic_signature_index(sig_index); }
3125
3126 const char* InstanceKlass::signature_name() const {
3127 return signature_name_of_carrier(JVM_SIGNATURE_CLASS);
3128 }
3129
3130 const char* InstanceKlass::signature_name_of_carrier(char c) const {
3131 // Get the internal name as a c string
3132 const char* src = (const char*) (name()->as_C_string());
3133 const int src_length = (int)strlen(src);
3134
3135 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
3136
3137 // Add L or Q as type indicator
3138 int dest_index = 0;
3139 dest[dest_index++] = c;
3140
3141 // Add the actual class name
3142 for (int src_index = 0; src_index < src_length; ) {
3143 dest[dest_index++] = src[src_index++];
3144 }
3145
3146 if (is_hidden()) { // Replace the last '+' with a '.'.
3147 for (int index = (int)src_length; index > 0; index--) {
3148 if (dest[index] == '+') {
3149 dest[index] = JVM_SIGNATURE_DOT;
3150 break;
3151 }
3152 }
3153 }
3154
3155 // Add the semicolon and the null
3156 dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
3157 dest[dest_index] = '\0';
3158 return dest;
3159 }
3461 jint InstanceKlass::compute_modifier_flags() const {
3462 jint access = access_flags().as_int();
3463
3464 // But check if it happens to be member class.
3465 InnerClassesIterator iter(this);
3466 for (; !iter.done(); iter.next()) {
3467 int ioff = iter.inner_class_info_index();
3468 // Inner class attribute can be zero, skip it.
3469 // Strange but true: JVM spec. allows null inner class refs.
3470 if (ioff == 0) continue;
3471
3472 // only look at classes that are already loaded
3473 // since we are looking for the flags for our self.
3474 Symbol* inner_name = constants()->klass_name_at(ioff);
3475 if (name() == inner_name) {
3476 // This is really a member class.
3477 access = iter.inner_access_flags();
3478 break;
3479 }
3480 }
3481 return (access & JVM_ACC_WRITTEN_FLAGS);
3482 }
3483
3484 jint InstanceKlass::jvmti_class_status() const {
3485 jint result = 0;
3486
3487 if (is_linked()) {
3488 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3489 }
3490
3491 if (is_initialized()) {
3492 assert(is_linked(), "Class status is not consistent");
3493 result |= JVMTI_CLASS_STATUS_INITIALIZED;
3494 }
3495 if (is_in_error_state()) {
3496 result |= JVMTI_CLASS_STATUS_ERROR;
3497 }
3498 return result;
3499 }
3500
3501 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {
3718 }
3719 osr = osr->osr_link();
3720 }
3721
3722 assert(match_level == false || best == nullptr, "shouldn't pick up anything if match_level is set");
3723 if (best != nullptr && best->comp_level() >= comp_level) {
3724 return best;
3725 }
3726 return nullptr;
3727 }
3728
3729 // -----------------------------------------------------------------------------------------------------
3730 // Printing
3731
3732 #define BULLET " - "
3733
3734 static const char* state_names[] = {
3735 "allocated", "loaded", "being_linked", "linked", "being_initialized", "fully_initialized", "initialization_error"
3736 };
3737
3738 static void print_vtable(address self, intptr_t* start, int len, outputStream* st) {
3739 ResourceMark rm;
3740 int* forward_refs = NEW_RESOURCE_ARRAY(int, len);
3741 for (int i = 0; i < len; i++) forward_refs[i] = 0;
3742 for (int i = 0; i < len; i++) {
3743 intptr_t e = start[i];
3744 st->print("%d : " INTPTR_FORMAT, i, e);
3745 if (forward_refs[i] != 0) {
3746 int from = forward_refs[i];
3747 int off = (int) start[from];
3748 st->print(" (offset %d <= [%d])", off, from);
3749 }
3750 if (MetaspaceObj::is_valid((Metadata*)e)) {
3751 st->print(" ");
3752 ((Metadata*)e)->print_value_on(st);
3753 } else if (self != nullptr && e > 0 && e < 0x10000) {
3754 address location = self + e;
3755 int index = (int)((intptr_t*)location - start);
3756 st->print(" (offset %d => [%d])", (int)e, index);
3757 if (index >= 0 && index < len)
3758 forward_refs[index] = i;
3759 }
3760 st->cr();
3761 }
3762 }
3763
3764 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3765 return print_vtable(nullptr, reinterpret_cast<intptr_t*>(start), len, st);
3766 }
3767
3768 template<typename T>
3769 static void print_array_on(outputStream* st, Array<T>* array) {
3770 if (array == nullptr) { st->print_cr("nullptr"); return; }
3771 array->print_value_on(st); st->cr();
3772 if (Verbose || WizardMode) {
3773 for (int i = 0; i < array->length(); i++) {
3774 st->print("%d : ", i); array->at(i)->print_value_on(st); st->cr();
3775 }
3776 }
3777 }
3778
3779 static void print_array_on(outputStream* st, Array<int>* array) {
3780 if (array == nullptr) { st->print_cr("nullptr"); return; }
3781 array->print_value_on(st); st->cr();
3782 if (Verbose || WizardMode) {
3783 for (int i = 0; i < array->length(); i++) {
3784 st->print("%d : %d", i, array->at(i)); st->cr();
3785 }
3786 }
3787 }
3788
3789 const char* InstanceKlass::init_state_name() const {
3790 return state_names[init_state()];
3791 }
3792
3793 void InstanceKlass::print_on(outputStream* st) const {
3794 assert(is_klass(), "must be klass");
3795 Klass::print_on(st);
3796
3797 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3798 st->print(BULLET"klass size: %d", size()); st->cr();
3799 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3800 st->print(BULLET"flags: "); _misc_flags.print_on(st); st->cr();
3801 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3802 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3803 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3804 st->print(BULLET"sub: ");
3805 Klass* sub = subklass();
3806 int n;
3807 for (n = 0; sub != nullptr; n++, sub = sub->next_sibling()) {
3808 if (n < MaxSubklassPrintSize) {
3809 sub->print_value_on(st);
3810 st->print(" ");
3811 }
3812 }
3813 if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3814 st->cr();
3815
3816 if (is_interface()) {
3817 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3818 if (nof_implementors() == 1) {
3819 st->print_cr(BULLET"implementor: ");
3820 st->print(" ");
3821 implementor()->print_value_on(st);
3822 st->cr();
3823 }
3824 }
3825
3826 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3827 st->print(BULLET"methods: "); print_array_on(st, methods());
3828 st->print(BULLET"method ordering: "); print_array_on(st, method_ordering());
3829 st->print(BULLET"default_methods: "); print_array_on(st, default_methods());
3830 if (default_vtable_indices() != nullptr) {
3831 st->print(BULLET"default vtable indices: "); print_array_on(st, default_vtable_indices());
3832 }
3833 st->print(BULLET"local interfaces: "); print_array_on(st, local_interfaces());
3834 st->print(BULLET"trans. interfaces: "); print_array_on(st, transitive_interfaces());
3835 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
3836 if (class_loader_data() != nullptr) {
3837 st->print(BULLET"class loader data: ");
3838 class_loader_data()->print_value_on(st);
3839 st->cr();
3840 }
3841 if (source_file_name() != nullptr) {
3842 st->print(BULLET"source file: ");
3843 source_file_name()->print_value_on(st);
3844 st->cr();
3845 }
3846 if (source_debug_extension() != nullptr) {
3847 st->print(BULLET"source debug extension: ");
3848 st->print("%s", source_debug_extension());
3849 st->cr();
3850 }
3851 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
3852 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
3853 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
3854 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
3860 pv_node = pv_node->previous_versions()) {
3861 if (!have_pv)
3862 st->print(BULLET"previous version: ");
3863 have_pv = true;
3864 pv_node->constants()->print_value_on(st);
3865 }
3866 if (have_pv) st->cr();
3867 }
3868
3869 if (generic_signature() != nullptr) {
3870 st->print(BULLET"generic signature: ");
3871 generic_signature()->print_value_on(st);
3872 st->cr();
3873 }
3874 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
3875 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
3876 if (record_components() != nullptr) {
3877 st->print(BULLET"record components: "); record_components()->print_value_on(st); st->cr();
3878 }
3879 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
3880 st->print(BULLET"preload classes: "); preload_classes()->print_value_on(st); st->cr();
3881 if (java_mirror() != nullptr) {
3882 st->print(BULLET"java mirror: ");
3883 java_mirror()->print_value_on(st);
3884 st->cr();
3885 } else {
3886 st->print_cr(BULLET"java mirror: null");
3887 }
3888 st->print(BULLET"vtable length %d (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3889 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
3890 st->print(BULLET"itable length %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3891 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(nullptr, start_of_itable(), itable_length(), st);
3892 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3893 FieldPrinter print_static_field(st);
3894 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3895 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3896 FieldPrinter print_nonstatic_field(st);
3897 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3898 ik->print_nonstatic_fields(&print_nonstatic_field);
3899
3900 st->print(BULLET"non-static oop maps: ");
3901 OopMapBlock* map = start_of_nonstatic_oop_maps();
3902 OopMapBlock* end_map = map + nonstatic_oop_map_count();
3903 while (map < end_map) {
3904 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3905 map++;
3906 }
3907 st->cr();
3908 }
3909
3910 void InstanceKlass::print_value_on(outputStream* st) const {
3911 assert(is_klass(), "must be klass");
|