53 #include "logging/logMessage.hpp"
54 #include "logging/logStream.hpp"
55 #include "memory/allocation.inline.hpp"
56 #include "memory/iterator.inline.hpp"
57 #include "memory/metadataFactory.hpp"
58 #include "memory/metaspaceClosure.hpp"
59 #include "memory/oopFactory.hpp"
60 #include "memory/resourceArea.hpp"
61 #include "memory/universe.hpp"
62 #include "oops/fieldStreams.inline.hpp"
63 #include "oops/constantPool.hpp"
64 #include "oops/instanceClassLoaderKlass.hpp"
65 #include "oops/instanceKlass.inline.hpp"
66 #include "oops/instanceMirrorKlass.hpp"
67 #include "oops/instanceOop.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/atomic.hpp"
79 #include "runtime/fieldDescriptor.inline.hpp"
80 #include "runtime/handles.inline.hpp"
81 #include "runtime/javaCalls.hpp"
82 #include "runtime/mutexLocker.hpp"
83 #include "runtime/orderAccess.hpp"
84 #include "runtime/reflectionUtils.hpp"
85 #include "runtime/thread.inline.hpp"
86 #include "services/classLoadingService.hpp"
87 #include "services/finalizerService.hpp"
88 #include "services/threadService.hpp"
89 #include "utilities/dtrace.hpp"
90 #include "utilities/events.hpp"
91 #include "utilities/macros.hpp"
92 #include "utilities/stringUtils.hpp"
146
147 static inline bool is_class_loader(const Symbol* class_name,
148 const ClassFileParser& parser) {
149 assert(class_name != NULL, "invariant");
150
151 if (class_name == vmSymbols::java_lang_ClassLoader()) {
152 return true;
153 }
154
155 if (vmClasses::ClassLoader_klass_loaded()) {
156 const Klass* const super_klass = parser.super_klass();
157 if (super_klass != NULL) {
158 if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
159 return true;
160 }
161 }
162 }
163 return false;
164 }
165
166 // private: called to verify that k is a static member of this nest.
167 // We know that k is an instance class in the same package and hence the
168 // same classloader.
169 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
170 assert(!is_hidden(), "unexpected hidden class");
171 if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
172 if (log_is_enabled(Trace, class, nestmates)) {
173 ResourceMark rm(current);
174 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
175 k->external_name(), this->external_name());
176 }
177 return false;
178 }
179
180 if (log_is_enabled(Trace, class, nestmates)) {
181 ResourceMark rm(current);
182 log_trace(class, nestmates)("Checking nest membership of %s in %s",
183 k->external_name(), this->external_name());
184 }
185
411 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
412 this->external_name(),
413 access ? "" : "NOT ",
414 k->external_name());
415 return access;
416 }
417
418 const char* InstanceKlass::nest_host_error() {
419 if (_nest_host_index == 0) {
420 return NULL;
421 } else {
422 constantPoolHandle cph(Thread::current(), constants());
423 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
424 }
425 }
426
427 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
428 const int size = InstanceKlass::size(parser.vtable_size(),
429 parser.itable_size(),
430 nonstatic_oop_map_size(parser.total_oop_map_count()),
431 parser.is_interface());
432
433 const Symbol* const class_name = parser.class_name();
434 assert(class_name != NULL, "invariant");
435 ClassLoaderData* loader_data = parser.loader_data();
436 assert(loader_data != NULL, "invariant");
437
438 InstanceKlass* ik;
439
440 // Allocation
441 if (REF_NONE == parser.reference_type()) {
442 if (class_name == vmSymbols::java_lang_Class()) {
443 // mirror
444 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
445 }
446 else if (is_class_loader(class_name, parser)) {
447 // class loader
448 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
449 } else {
450 // normal
451 ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_kind_other);
452 }
453 } else {
454 // reference
455 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
456 }
457
458 // Check for pending exception before adding to the loader data and incrementing
459 // class count. Can get OOM here.
460 if (HAS_PENDING_EXCEPTION) {
461 return NULL;
462 }
463
464 return ik;
465 }
466
467
468 // copy method ordering from resource area to Metaspace
469 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
470 if (m != NULL) {
471 // allocate a new array and copy contents (memcpy?)
472 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
473 for (int i = 0; i < m->length(); i++) {
474 _method_ordering->at_put(i, m->at(i));
475 }
476 } else {
477 _method_ordering = Universe::the_empty_int_array();
478 }
479 }
480
481 // create a new array of vtable_indices for default methods
482 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
483 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
484 assert(default_vtable_indices() == NULL, "only create once");
485 set_default_vtable_indices(vtable_indices);
486 return vtable_indices;
487 }
488
489 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
490 Klass(id),
491 _nest_members(NULL),
492 _nest_host(NULL),
493 _permitted_subclasses(NULL),
494 _record_components(NULL),
495 _static_field_size(parser.static_field_size()),
496 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
497 _itable_len(parser.itable_size()),
498 _nest_host_index(0),
499 _init_state(allocated),
500 _reference_type(parser.reference_type()),
501 _init_thread(NULL)
502 {
503 set_vtable_length(parser.vtable_size());
504 set_kind(kind);
505 set_access_flags(parser.access_flags());
506 if (parser.is_hidden()) set_is_hidden();
507 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
508 false));
509
510 assert(NULL == _methods, "underlying memory not zeroed?");
511 assert(is_instance_klass(), "is layout incorrect?");
512 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
513 }
514
515 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
516 Array<Method*>* methods) {
517 if (methods != NULL && methods != Universe::the_empty_method_array() &&
518 !methods->is_shared()) {
519 for (int i = 0; i < methods->length(); i++) {
520 Method* method = methods->at(i);
521 if (method == NULL) continue; // maybe null if error processing
522 // Only want to delete methods that are not executing for RedefineClasses.
523 // The previous version will point to them so they're not totally dangling
524 assert (!method->on_stack(), "shouldn't be called with methods on stack");
525 MetadataFactory::free_metadata(loader_data, method);
526 }
527 MetadataFactory::free_array<Method*>(loader_data, methods);
528 }
529 }
530
531 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
532 const Klass* super_klass,
658 inner_classes() != Universe::the_empty_short_array() &&
659 !inner_classes()->is_shared()) {
660 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
661 }
662 set_inner_classes(NULL);
663
664 if (nest_members() != NULL &&
665 nest_members() != Universe::the_empty_short_array() &&
666 !nest_members()->is_shared()) {
667 MetadataFactory::free_array<jushort>(loader_data, nest_members());
668 }
669 set_nest_members(NULL);
670
671 if (permitted_subclasses() != NULL &&
672 permitted_subclasses() != Universe::the_empty_short_array() &&
673 !permitted_subclasses()->is_shared()) {
674 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
675 }
676 set_permitted_subclasses(NULL);
677
678 // We should deallocate the Annotations instance if it's not in shared spaces.
679 if (annotations() != NULL && !annotations()->is_shared()) {
680 MetadataFactory::free_metadata(loader_data, annotations());
681 }
682 set_annotations(NULL);
683
684 SystemDictionaryShared::handle_class_unloading(this);
685 }
686
687 bool InstanceKlass::is_record() const {
688 return _record_components != NULL &&
689 is_final() &&
690 java_super() == vmClasses::Record_klass();
691 }
692
693 bool InstanceKlass::is_sealed() const {
694 return _permitted_subclasses != NULL &&
695 _permitted_subclasses != Universe::the_empty_short_array();
696 }
697
861 vmSymbols::java_lang_IncompatibleClassChangeError(),
862 "class %s has interface %s as super class",
863 external_name(),
864 super_klass->external_name()
865 );
866 return false;
867 }
868
869 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
870 ik_super->link_class_impl(CHECK_false);
871 }
872
873 // link all interfaces implemented by this class before linking this class
874 Array<InstanceKlass*>* interfaces = local_interfaces();
875 int num_interfaces = interfaces->length();
876 for (int index = 0; index < num_interfaces; index++) {
877 InstanceKlass* interk = interfaces->at(index);
878 interk->link_class_impl(CHECK_false);
879 }
880
881 // in case the class is linked in the process of linking its superclasses
882 if (is_linked()) {
883 return true;
884 }
885
886 // trace only the link time for this klass that includes
887 // the verification time
888 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
889 ClassLoader::perf_class_link_selftime(),
890 ClassLoader::perf_classes_linked(),
891 jt->get_thread_stat()->perf_recursion_counts_addr(),
892 jt->get_thread_stat()->perf_timers_addr(),
893 PerfClassTraceTime::CLASS_LINK);
894
895 // verification & rewriting
896 {
897 HandleMark hm(THREAD);
898 Handle h_init_lock(THREAD, init_lock());
899 ObjectLocker ol(h_init_lock, jt);
900 // rewritten will have been set if loader constraint error found
1113 if (is_in_error_state()) {
1114 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1115 ResourceMark rm(THREAD);
1116 Handle cause(THREAD, get_initialization_error(THREAD));
1117
1118 stringStream ss;
1119 ss.print("Could not initialize class %s", external_name());
1120 if (cause.is_null()) {
1121 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1122 } else {
1123 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1124 ss.as_string(), cause);
1125 }
1126 }
1127
1128 // Step 6
1129 set_init_state(being_initialized);
1130 set_init_thread(jt);
1131 }
1132
1133 // Step 7
1134 // Next, if C is a class rather than an interface, initialize it's super class and super
1135 // interfaces.
1136 if (!is_interface()) {
1137 Klass* super_klass = super();
1138 if (super_klass != NULL && super_klass->should_be_initialized()) {
1139 super_klass->initialize(THREAD);
1140 }
1141 // If C implements any interface that declares a non-static, concrete method,
1142 // the initialization of C triggers initialization of its super interfaces.
1143 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1144 // having a superinterface that declares, non-static, concrete methods
1145 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1146 initialize_super_interfaces(THREAD);
1147 }
1148
1149 // If any exceptions, complete abruptly, throwing the same exception as above.
1150 if (HAS_PENDING_EXCEPTION) {
1151 Handle e(THREAD, PENDING_EXCEPTION);
1152 CLEAR_PENDING_EXCEPTION;
1153 {
1154 EXCEPTION_MARK;
1155 add_initialization_error(THREAD, e);
1156 // Locks object, set state, and notify all waiting threads
1157 set_initialization_state_and_notify(initialization_error, THREAD);
1158 CLEAR_PENDING_EXCEPTION;
1159 }
1160 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1161 THROW_OOP(e());
1162 }
1163 }
1164
1165
1166 // Step 8
1167 {
1168 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1169 if (class_initializer() != NULL) {
1170 // Timer includes any side effects of class initialization (resolution,
1171 // etc), but not recursive entry into call_class_initializer().
1172 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1173 ClassLoader::perf_class_init_selftime(),
1174 ClassLoader::perf_classes_inited(),
1175 jt->get_thread_stat()->perf_recursion_counts_addr(),
1176 jt->get_thread_stat()->perf_timers_addr(),
1177 PerfClassTraceTime::CLASS_CLINIT);
1178 call_class_initializer(THREAD);
1179 } else {
1180 // The elapsed time is so small it's not worth counting.
1181 if (UsePerfData) {
1182 ClassLoader::perf_classes_inited()->inc();
1183 }
1184 call_class_initializer(THREAD);
1185 }
1186 }
1187
1188 // Step 9
1189 if (!HAS_PENDING_EXCEPTION) {
1190 set_initialization_state_and_notify(fully_initialized, CHECK);
1191 debug_only(vtable().verify(tty, true);)
1192 }
1193 else {
1194 // Step 10 and 11
1195 Handle e(THREAD, PENDING_EXCEPTION);
1196 CLEAR_PENDING_EXCEPTION;
1197 // JVMTI has already reported the pending exception
1198 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1199 JvmtiExport::clear_detected_exception(jt);
1200 {
1201 EXCEPTION_MARK;
1202 add_initialization_error(THREAD, e);
1203 set_initialization_state_and_notify(initialization_error, THREAD);
1204 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1205 // JVMTI has already reported the pending exception
1206 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1207 JvmtiExport::clear_detected_exception(jt);
1208 }
1209 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1210 if (e->is_a(vmClasses::Error_klass())) {
1211 THROW_OOP(e());
1212 } else {
1213 JavaCallArguments args(e);
1214 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1439 : vmSymbols::java_lang_InstantiationException(), external_name());
1440 }
1441 if (this == vmClasses::Class_klass()) {
1442 ResourceMark rm(THREAD);
1443 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1444 : vmSymbols::java_lang_IllegalAccessException(), external_name());
1445 }
1446 }
1447
1448 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1449 // Need load-acquire for lock-free read
1450 if (array_klasses_acquire() == NULL) {
1451 ResourceMark rm(THREAD);
1452 JavaThread *jt = THREAD;
1453 {
1454 // Atomic creation of array_klasses
1455 MutexLocker ma(THREAD, MultiArray_lock);
1456
1457 // Check if update has already taken place
1458 if (array_klasses() == NULL) {
1459 ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
1460 // use 'release' to pair with lock-free load
1461 release_set_array_klasses(k);
1462 }
1463 }
1464 }
1465 // array_klasses() will always be set at this point
1466 ObjArrayKlass* oak = array_klasses();
1467 return oak->array_klass(n, THREAD);
1468 }
1469
1470 Klass* InstanceKlass::array_klass_or_null(int n) {
1471 // Need load-acquire for lock-free read
1472 ObjArrayKlass* oak = array_klasses_acquire();
1473 if (oak == NULL) {
1474 return NULL;
1475 } else {
1476 return oak->array_klass_or_null(n);
1477 }
1478 }
1479
1480 Klass* InstanceKlass::array_klass(TRAPS) {
1481 return array_klass(1, THREAD);
1482 }
1483
1484 Klass* InstanceKlass::array_klass_or_null() {
1485 return array_klass_or_null(1);
1486 }
1487
1488 static int call_class_initializer_counter = 0; // for debugging
1489
1490 Method* InstanceKlass::class_initializer() const {
1491 Method* clinit = find_method(
1492 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1493 if (clinit != NULL && clinit->has_valid_initializer_flags()) {
1494 return clinit;
1495 }
1496 return NULL;
1497 }
1498
1499 void InstanceKlass::call_class_initializer(TRAPS) {
1500 if (ReplayCompiles &&
1501 (ReplaySuppressInitializers == 1 ||
1502 (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
1503 // Hide the existence of the initializer for the purpose of replaying the compile
1504 return;
1505 }
1506
1507 #if INCLUDE_CDS
1508 // This is needed to ensure the consistency of the archived heap objects.
1509 if (has_archived_enum_objs()) {
1510 assert(is_shared(), "must be");
1511 bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
1512 if (initialized) {
1513 return;
1522 ResourceMark rm(THREAD);
1523 LogStream ls(lt);
1524 ls.print("%d Initializing ", call_class_initializer_counter++);
1525 name()->print_value_on(&ls);
1526 ls.print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
1527 }
1528 if (h_method() != NULL) {
1529 JavaCallArguments args; // No arguments
1530 JavaValue result(T_VOID);
1531 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1532 }
1533 }
1534
1535
1536 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1537 InterpreterOopMap* entry_for) {
1538 // Lazily create the _oop_map_cache at first request
1539 // Lock-free access requires load_acquire.
1540 OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1541 if (oop_map_cache == NULL) {
1542 MutexLocker x(OopMapCacheAlloc_lock);
1543 // Check if _oop_map_cache was allocated while we were waiting for this lock
1544 if ((oop_map_cache = _oop_map_cache) == NULL) {
1545 oop_map_cache = new OopMapCache();
1546 // Ensure _oop_map_cache is stable, since it is examined without a lock
1547 Atomic::release_store(&_oop_map_cache, oop_map_cache);
1548 }
1549 }
1550 // _oop_map_cache is constant after init; lookup below does its own locking.
1551 oop_map_cache->lookup(method, bci, entry_for);
1552 }
1553
1554 bool InstanceKlass::contains_field_offset(int offset) {
1555 fieldDescriptor fd;
1556 return find_field_from_offset(offset, false, &fd);
1557 }
1558
1559 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1560 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1561 Symbol* f_name = fs.name();
1562 Symbol* f_sig = fs.signature();
1563 if (f_name == name && f_sig == sig) {
1564 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1565 return true;
1566 }
1567 }
1568 return false;
1569 }
1570
1571
1572 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1573 const int n = local_interfaces()->length();
1574 for (int i = 0; i < n; i++) {
1575 Klass* intf1 = local_interfaces()->at(i);
1576 assert(intf1->is_interface(), "just checking type");
1577 // search for field in current interface
1578 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
1609
1610 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1611 // search order according to newest JVM spec (5.4.3.2, p.167).
1612 // 1) search for field in current klass
1613 if (find_local_field(name, sig, fd)) {
1614 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1615 }
1616 // 2) search for field recursively in direct superinterfaces
1617 if (is_static) {
1618 Klass* intf = find_interface_field(name, sig, fd);
1619 if (intf != NULL) return intf;
1620 }
1621 // 3) apply field lookup recursively if superclass exists
1622 { Klass* supr = super();
1623 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1624 }
1625 // 4) otherwise field lookup fails
1626 return NULL;
1627 }
1628
1629
1630 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1631 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1632 if (fs.offset() == offset) {
1633 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1634 if (fd->is_static() == is_static) return true;
1635 }
1636 }
1637 return false;
1638 }
1639
1640
1641 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1642 Klass* klass = const_cast<InstanceKlass*>(this);
1643 while (klass != NULL) {
1644 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1645 return true;
1646 }
1647 klass = klass->super();
1648 }
2001 }
2002
2003 // uncached_lookup_method searches both the local class methods array and all
2004 // superclasses methods arrays, skipping any overpass methods in superclasses,
2005 // and possibly skipping private methods.
2006 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2007 const Symbol* signature,
2008 OverpassLookupMode overpass_mode,
2009 PrivateLookupMode private_mode) const {
2010 OverpassLookupMode overpass_local_mode = overpass_mode;
2011 const Klass* klass = this;
2012 while (klass != NULL) {
2013 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2014 signature,
2015 overpass_local_mode,
2016 StaticLookupMode::find,
2017 private_mode);
2018 if (method != NULL) {
2019 return method;
2020 }
2021 klass = klass->super();
2022 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2023 }
2024 return NULL;
2025 }
2026
2027 #ifdef ASSERT
2028 // search through class hierarchy and return true if this class or
2029 // one of the superclasses was redefined
2030 bool InstanceKlass::has_redefined_this_or_super() const {
2031 const Klass* klass = this;
2032 while (klass != NULL) {
2033 if (InstanceKlass::cast(klass)->has_been_redefined()) {
2034 return true;
2035 }
2036 klass = klass->super();
2037 }
2038 return false;
2039 }
2040 #endif
2482 if (itable_length() > 0) {
2483 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2484 int method_table_offset_in_words = ioe->offset()/wordSize;
2485 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2486 / itableOffsetEntry::size();
2487
2488 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2489 if (ioe->interface_klass() != NULL) {
2490 it->push(ioe->interface_klass_addr());
2491 itableMethodEntry* ime = ioe->first_method_entry(this);
2492 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2493 for (int index = 0; index < n; index ++) {
2494 it->push(ime[index].method_addr());
2495 }
2496 }
2497 }
2498 }
2499
2500 it->push(&_nest_members);
2501 it->push(&_permitted_subclasses);
2502 it->push(&_record_components);
2503 }
2504
2505 void InstanceKlass::remove_unshareable_info() {
2506
2507 if (is_linked()) {
2508 assert(can_be_verified_at_dumptime(), "must be");
2509 // Remember this so we can avoid walking the hierarchy at runtime.
2510 set_verified_at_dump_time();
2511 }
2512
2513 Klass::remove_unshareable_info();
2514
2515 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2516 // Classes are attempted to link during dumping and may fail,
2517 // but these classes are still in the dictionary and class list in CLD.
2518 // If the class has failed verification, there is nothing else to remove.
2519 return;
2520 }
2521
2522 // Reset to the 'allocated' state to prevent any premature accessing to
2525 // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2526 _init_state = allocated;
2527
2528 { // Otherwise this needs to take out the Compile_lock.
2529 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2530 init_implementor();
2531 }
2532
2533 constants()->remove_unshareable_info();
2534
2535 for (int i = 0; i < methods()->length(); i++) {
2536 Method* m = methods()->at(i);
2537 m->remove_unshareable_info();
2538 }
2539
2540 // do array classes also.
2541 if (array_klasses() != NULL) {
2542 array_klasses()->remove_unshareable_info();
2543 }
2544
2545 // These are not allocated from metaspace. They are safe to set to NULL.
2546 _source_debug_extension = NULL;
2547 _dep_context = NULL;
2548 _osr_nmethods_head = NULL;
2549 #if INCLUDE_JVMTI
2550 _breakpoints = NULL;
2551 _previous_versions = NULL;
2552 _cached_class_file = NULL;
2553 _jvmti_cached_class_field_map = NULL;
2554 #endif
2555
2556 _init_thread = NULL;
2557 _methods_jmethod_ids = NULL;
2558 _jni_ids = NULL;
2559 _oop_map_cache = NULL;
2560 // clear _nest_host to ensure re-load at runtime
2561 _nest_host = NULL;
2562 init_shared_package_entry();
2563 _dep_context_last_cleaned = 0;
2564 }
2586 if (is_shared_unregistered_class()) {
2587 _package_entry = NULL;
2588 } else {
2589 _package_entry = PackageEntry::get_archived_entry(_package_entry);
2590 }
2591 }
2592 ArchivePtrMarker::mark_pointer((address**)&_package_entry);
2593 #endif
2594 }
2595
2596 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2597 PackageEntry* pkg_entry, TRAPS) {
2598 // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2599 // before the InstanceKlass is added to the SystemDictionary. Make
2600 // sure the current state is <loaded.
2601 assert(!is_loaded(), "invalid init state");
2602 assert(!shared_loading_failed(), "Must not try to load failed class again");
2603 set_package(loader_data, pkg_entry, CHECK);
2604 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2605
2606 Array<Method*>* methods = this->methods();
2607 int num_methods = methods->length();
2608 for (int index = 0; index < num_methods; ++index) {
2609 methods->at(index)->restore_unshareable_info(CHECK);
2610 }
2611 #if INCLUDE_JVMTI
2612 if (JvmtiExport::has_redefined_a_class()) {
2613 // Reinitialize vtable because RedefineClasses may have changed some
2614 // entries in this vtable for super classes so the CDS vtable might
2615 // point to old or obsolete entries. RedefineClasses doesn't fix up
2616 // vtables in the shared system dictionary, only the main one.
2617 // It also redefines the itable too so fix that too.
2618 // First fix any default methods that point to a super class that may
2619 // have been redefined.
2620 bool trace_name_printed = false;
2621 adjust_default_methods(&trace_name_printed);
2622 vtable().initialize_vtable();
2623 itable().initialize_itable();
2624 }
2625 #endif
2789
2790 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2791 if (array == NULL) {
2792 _source_debug_extension = NULL;
2793 } else {
2794 // Adding one to the attribute length in order to store a null terminator
2795 // character could cause an overflow because the attribute length is
2796 // already coded with an u4 in the classfile, but in practice, it's
2797 // unlikely to happen.
2798 assert((length+1) > length, "Overflow checking");
2799 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2800 for (int i = 0; i < length; i++) {
2801 sde[i] = array[i];
2802 }
2803 sde[length] = '\0';
2804 _source_debug_extension = sde;
2805 }
2806 }
2807
2808 const char* InstanceKlass::signature_name() const {
2809 int hash_len = 0;
2810 char hash_buf[40];
2811
2812 // Get the internal name as a c string
2813 const char* src = (const char*) (name()->as_C_string());
2814 const int src_length = (int)strlen(src);
2815
2816 char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
2817
2818 // Add L as type indicator
2819 int dest_index = 0;
2820 dest[dest_index++] = JVM_SIGNATURE_CLASS;
2821
2822 // Add the actual class name
2823 for (int src_index = 0; src_index < src_length; ) {
2824 dest[dest_index++] = src[src_index++];
2825 }
2826
2827 if (is_hidden()) { // Replace the last '+' with a '.'.
2828 for (int index = (int)src_length; index > 0; index--) {
2829 if (dest[index] == '+') {
2830 dest[index] = JVM_SIGNATURE_DOT;
2831 break;
2832 }
2833 }
2834 }
2835
2836 // If we have a hash, append it
2837 for (int hash_index = 0; hash_index < hash_len; ) {
2838 dest[dest_index++] = hash_buf[hash_index++];
2839 }
2840
3406 }
3407 osr = osr->osr_link();
3408 }
3409
3410 assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3411 if (best != NULL && best->comp_level() >= comp_level) {
3412 return best;
3413 }
3414 return NULL;
3415 }
3416
3417 // -----------------------------------------------------------------------------------------------------
3418 // Printing
3419
3420 #define BULLET " - "
3421
3422 static const char* state_names[] = {
3423 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3424 };
3425
3426 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3427 for (int i = 0; i < len; i++) {
3428 intptr_t e = start[i];
3429 st->print("%d : " INTPTR_FORMAT, i, e);
3430 if (MetaspaceObj::is_valid((Metadata*)e)) {
3431 st->print(" ");
3432 ((Metadata*)e)->print_value_on(st);
3433 }
3434 st->cr();
3435 }
3436 }
3437
3438 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3439 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3440 }
3441
3442 const char* InstanceKlass::init_state_name() const {
3443 return state_names[_init_state];
3444 }
3445
3446 void InstanceKlass::print_on(outputStream* st) const {
3447 assert(is_klass(), "must be klass");
3448 Klass::print_on(st);
3449
3450 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3451 st->print(BULLET"klass size: %d", size()); st->cr();
3452 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3453 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3454 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3455 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3456 st->print(BULLET"sub: ");
3457 Klass* sub = subklass();
3458 int n;
3459 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
3460 if (n < MaxSubklassPrintSize) {
3461 sub->print_value_on(st);
3462 st->print(" ");
3463 }
3464 }
3465 if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3466 st->cr();
3467
3468 if (is_interface()) {
3469 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3470 if (nof_implementors() == 1) {
3471 st->print_cr(BULLET"implementor: ");
3472 st->print(" ");
3473 implementor()->print_value_on(st);
3474 st->cr();
3475 }
3476 }
3477
3478 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3479 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
3480 if (Verbose || WizardMode) {
3481 Array<Method*>* method_array = methods();
3482 for (int i = 0; i < method_array->length(); i++) {
3483 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3484 }
3485 }
3486 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
3487 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
3488 if (Verbose && default_methods() != NULL) {
3489 Array<Method*>* method_array = default_methods();
3490 for (int i = 0; i < method_array->length(); i++) {
3491 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3492 }
3493 }
3494 if (default_vtable_indices() != NULL) {
3495 st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr();
3496 }
3497 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
3498 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3499 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
3500 if (class_loader_data() != NULL) {
3501 st->print(BULLET"class loader data: ");
3502 class_loader_data()->print_value_on(st);
3503 st->cr();
3504 }
3505 if (source_file_name() != NULL) {
3506 st->print(BULLET"source file: ");
3507 source_file_name()->print_value_on(st);
3508 st->cr();
3509 }
3510 if (source_debug_extension() != NULL) {
3511 st->print(BULLET"source debug extension: ");
3512 st->print("%s", source_debug_extension());
3513 st->cr();
3514 }
3515 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
3516 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
3517 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
3518 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
3524 pv_node = pv_node->previous_versions()) {
3525 if (!have_pv)
3526 st->print(BULLET"previous version: ");
3527 have_pv = true;
3528 pv_node->constants()->print_value_on(st);
3529 }
3530 if (have_pv) st->cr();
3531 }
3532
3533 if (generic_signature() != NULL) {
3534 st->print(BULLET"generic signature: ");
3535 generic_signature()->print_value_on(st);
3536 st->cr();
3537 }
3538 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
3539 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
3540 if (record_components() != NULL) {
3541 st->print(BULLET"record components: "); record_components()->print_value_on(st); st->cr();
3542 }
3543 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
3544 if (java_mirror() != NULL) {
3545 st->print(BULLET"java mirror: ");
3546 java_mirror()->print_value_on(st);
3547 st->cr();
3548 } else {
3549 st->print_cr(BULLET"java mirror: NULL");
3550 }
3551 st->print(BULLET"vtable length %d (start addr: " INTPTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3552 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
3553 st->print(BULLET"itable length %d (start addr: " INTPTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3554 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
3555 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3556 FieldPrinter print_static_field(st);
3557 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3558 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3559 FieldPrinter print_nonstatic_field(st);
3560 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3561 ik->print_nonstatic_fields(&print_nonstatic_field);
3562
3563 st->print(BULLET"non-static oop maps: ");
3564 OopMapBlock* map = start_of_nonstatic_oop_maps();
3565 OopMapBlock* end_map = map + nonstatic_oop_map_count();
3566 while (map < end_map) {
3567 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3568 map++;
3569 }
3570 st->cr();
3571 }
3572
3573 void InstanceKlass::print_value_on(outputStream* st) const {
3574 assert(is_klass(), "must be klass");
|
53 #include "logging/logMessage.hpp"
54 #include "logging/logStream.hpp"
55 #include "memory/allocation.inline.hpp"
56 #include "memory/iterator.inline.hpp"
57 #include "memory/metadataFactory.hpp"
58 #include "memory/metaspaceClosure.hpp"
59 #include "memory/oopFactory.hpp"
60 #include "memory/resourceArea.hpp"
61 #include "memory/universe.hpp"
62 #include "oops/fieldStreams.inline.hpp"
63 #include "oops/constantPool.hpp"
64 #include "oops/instanceClassLoaderKlass.hpp"
65 #include "oops/instanceKlass.inline.hpp"
66 #include "oops/instanceMirrorKlass.hpp"
67 #include "oops/instanceOop.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/atomic.hpp"
80 #include "runtime/fieldDescriptor.inline.hpp"
81 #include "runtime/handles.inline.hpp"
82 #include "runtime/javaCalls.hpp"
83 #include "runtime/mutexLocker.hpp"
84 #include "runtime/orderAccess.hpp"
85 #include "runtime/reflectionUtils.hpp"
86 #include "runtime/thread.inline.hpp"
87 #include "services/classLoadingService.hpp"
88 #include "services/finalizerService.hpp"
89 #include "services/threadService.hpp"
90 #include "utilities/dtrace.hpp"
91 #include "utilities/events.hpp"
92 #include "utilities/macros.hpp"
93 #include "utilities/stringUtils.hpp"
147
148 static inline bool is_class_loader(const Symbol* class_name,
149 const ClassFileParser& parser) {
150 assert(class_name != NULL, "invariant");
151
152 if (class_name == vmSymbols::java_lang_ClassLoader()) {
153 return true;
154 }
155
156 if (vmClasses::ClassLoader_klass_loaded()) {
157 const Klass* const super_klass = parser.super_klass();
158 if (super_klass != NULL) {
159 if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
160 return true;
161 }
162 }
163 }
164 return false;
165 }
166
167 bool InstanceKlass::field_is_null_free_inline_type(int index) const { return Signature::basic_type(field(index)->signature(constants())) == T_PRIMITIVE_OBJECT; }
168
169 // private: called to verify that k is a static member of this nest.
170 // We know that k is an instance class in the same package and hence the
171 // same classloader.
172 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
173 assert(!is_hidden(), "unexpected hidden class");
174 if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
175 if (log_is_enabled(Trace, class, nestmates)) {
176 ResourceMark rm(current);
177 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
178 k->external_name(), this->external_name());
179 }
180 return false;
181 }
182
183 if (log_is_enabled(Trace, class, nestmates)) {
184 ResourceMark rm(current);
185 log_trace(class, nestmates)("Checking nest membership of %s in %s",
186 k->external_name(), this->external_name());
187 }
188
414 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
415 this->external_name(),
416 access ? "" : "NOT ",
417 k->external_name());
418 return access;
419 }
420
421 const char* InstanceKlass::nest_host_error() {
422 if (_nest_host_index == 0) {
423 return NULL;
424 } else {
425 constantPoolHandle cph(Thread::current(), constants());
426 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
427 }
428 }
429
430 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
431 const int size = InstanceKlass::size(parser.vtable_size(),
432 parser.itable_size(),
433 nonstatic_oop_map_size(parser.total_oop_map_count()),
434 parser.is_interface(),
435 parser.has_inline_fields() ? parser.java_fields_count() : 0,
436 parser.is_inline_type());
437
438 const Symbol* const class_name = parser.class_name();
439 assert(class_name != NULL, "invariant");
440 ClassLoaderData* loader_data = parser.loader_data();
441 assert(loader_data != NULL, "invariant");
442
443 InstanceKlass* ik;
444
445 // Allocation
446 if (REF_NONE == parser.reference_type()) {
447 if (class_name == vmSymbols::java_lang_Class()) {
448 // mirror
449 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
450 } else if (is_class_loader(class_name, parser)) {
451 // class loader
452 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
453 } else if (parser.is_inline_type()) {
454 // inline type
455 ik = new (loader_data, size, THREAD) InlineKlass(parser);
456 } else {
457 // normal
458 ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_kind_other);
459 }
460 } else {
461 // reference
462 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
463 }
464
465 // Check for pending exception before adding to the loader data and incrementing
466 // class count. Can get OOM here.
467 if (HAS_PENDING_EXCEPTION) {
468 return NULL;
469 }
470
471 #ifdef ASSERT
472 assert(ik->size() == size, "");
473 ik->bounds_check((address) ik->start_of_vtable(), false, size);
474 ik->bounds_check((address) ik->start_of_itable(), false, size);
475 ik->bounds_check((address) ik->end_of_itable(), true, size);
476 ik->bounds_check((address) ik->end_of_nonstatic_oop_maps(), true, size);
477 #endif //ASSERT
478 return ik;
479 }
480
481 #ifndef PRODUCT
482 bool InstanceKlass::bounds_check(address addr, bool edge_ok, intptr_t size_in_bytes) const {
483 const char* bad = NULL;
484 address end = NULL;
485 if (addr < (address)this) {
486 bad = "before";
487 } else if (addr == (address)this) {
488 if (edge_ok) return true;
489 bad = "just before";
490 } else if (addr == (end = (address)this + sizeof(intptr_t) * (size_in_bytes < 0 ? size() : size_in_bytes))) {
491 if (edge_ok) return true;
492 bad = "just after";
493 } else if (addr > end) {
494 bad = "after";
495 } else {
496 return true;
497 }
498 tty->print_cr("%s object bounds: " INTPTR_FORMAT " [" INTPTR_FORMAT ".." INTPTR_FORMAT "]",
499 bad, (intptr_t)addr, (intptr_t)this, (intptr_t)end);
500 Verbose = WizardMode = true; this->print(); //@@
501 return false;
502 }
503 #endif //PRODUCT
504
505 // copy method ordering from resource area to Metaspace
506 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
507 if (m != NULL) {
508 // allocate a new array and copy contents (memcpy?)
509 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
510 for (int i = 0; i < m->length(); i++) {
511 _method_ordering->at_put(i, m->at(i));
512 }
513 } else {
514 _method_ordering = Universe::the_empty_int_array();
515 }
516 }
517
518 // create a new array of vtable_indices for default methods
519 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
520 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
521 assert(default_vtable_indices() == NULL, "only create once");
522 set_default_vtable_indices(vtable_indices);
523 return vtable_indices;
524 }
525
526 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
527 Klass(id),
528 _nest_members(NULL),
529 _nest_host(NULL),
530 _permitted_subclasses(NULL),
531 _record_components(NULL),
532 _static_field_size(parser.static_field_size()),
533 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
534 _itable_len(parser.itable_size()),
535 _nest_host_index(0),
536 _init_state(allocated),
537 _reference_type(parser.reference_type()),
538 _init_thread(NULL),
539 _inline_type_field_klasses(NULL),
540 _preload_classes(NULL),
541 _adr_inlineklass_fixed_block(NULL)
542 {
543 set_vtable_length(parser.vtable_size());
544 set_kind(kind);
545 set_access_flags(parser.access_flags());
546 if (parser.is_hidden()) set_is_hidden();
547 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
548 false));
549 if (parser.has_inline_fields()) {
550 set_has_inline_type_fields();
551 }
552 _java_fields_count = parser.java_fields_count();
553
554 assert(NULL == _methods, "underlying memory not zeroed?");
555 assert(is_instance_klass(), "is layout incorrect?");
556 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
557
558 if (has_inline_type_fields()) {
559 _inline_type_field_klasses = (const Klass**) adr_inline_type_field_klasses();
560 }
561 }
562
563 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
564 Array<Method*>* methods) {
565 if (methods != NULL && methods != Universe::the_empty_method_array() &&
566 !methods->is_shared()) {
567 for (int i = 0; i < methods->length(); i++) {
568 Method* method = methods->at(i);
569 if (method == NULL) continue; // maybe null if error processing
570 // Only want to delete methods that are not executing for RedefineClasses.
571 // The previous version will point to them so they're not totally dangling
572 assert (!method->on_stack(), "shouldn't be called with methods on stack");
573 MetadataFactory::free_metadata(loader_data, method);
574 }
575 MetadataFactory::free_array<Method*>(loader_data, methods);
576 }
577 }
578
579 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
580 const Klass* super_klass,
706 inner_classes() != Universe::the_empty_short_array() &&
707 !inner_classes()->is_shared()) {
708 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
709 }
710 set_inner_classes(NULL);
711
712 if (nest_members() != NULL &&
713 nest_members() != Universe::the_empty_short_array() &&
714 !nest_members()->is_shared()) {
715 MetadataFactory::free_array<jushort>(loader_data, nest_members());
716 }
717 set_nest_members(NULL);
718
719 if (permitted_subclasses() != NULL &&
720 permitted_subclasses() != Universe::the_empty_short_array() &&
721 !permitted_subclasses()->is_shared()) {
722 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
723 }
724 set_permitted_subclasses(NULL);
725
726 if (preload_classes() != NULL &&
727 preload_classes() != Universe::the_empty_short_array() &&
728 !preload_classes()->is_shared()) {
729 MetadataFactory::free_array<jushort>(loader_data, preload_classes());
730 }
731
732 // We should deallocate the Annotations instance if it's not in shared spaces.
733 if (annotations() != NULL && !annotations()->is_shared()) {
734 MetadataFactory::free_metadata(loader_data, annotations());
735 }
736 set_annotations(NULL);
737
738 SystemDictionaryShared::handle_class_unloading(this);
739 }
740
741 bool InstanceKlass::is_record() const {
742 return _record_components != NULL &&
743 is_final() &&
744 java_super() == vmClasses::Record_klass();
745 }
746
747 bool InstanceKlass::is_sealed() const {
748 return _permitted_subclasses != NULL &&
749 _permitted_subclasses != Universe::the_empty_short_array();
750 }
751
915 vmSymbols::java_lang_IncompatibleClassChangeError(),
916 "class %s has interface %s as super class",
917 external_name(),
918 super_klass->external_name()
919 );
920 return false;
921 }
922
923 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
924 ik_super->link_class_impl(CHECK_false);
925 }
926
927 // link all interfaces implemented by this class before linking this class
928 Array<InstanceKlass*>* interfaces = local_interfaces();
929 int num_interfaces = interfaces->length();
930 for (int index = 0; index < num_interfaces; index++) {
931 InstanceKlass* interk = interfaces->at(index);
932 interk->link_class_impl(CHECK_false);
933 }
934
935
936 // If a class declares a method that uses an inline class as an argument
937 // type or return inline type, this inline class must be loaded during the
938 // linking of this class because size and properties of the inline class
939 // must be known in order to be able to perform inline type optimizations.
940 // The implementation below is an approximation of this rule, the code
941 // iterates over all methods of the current class (including overridden
942 // methods), not only the methods declared by this class. This
943 // approximation makes the code simpler, and doesn't change the semantic
944 // because classes declaring methods overridden by the current class are
945 // linked (and have performed their own pre-loading) before the linking
946 // of the current class.
947
948
949 // Note:
950 // Inline class types are loaded during
951 // the loading phase (see ClassFileParser::post_process_parsed_stream()).
952 // Inline class types used as element types for array creation
953 // are not pre-loaded. Their loading is triggered by either anewarray
954 // or multianewarray bytecodes.
955
956 // Could it be possible to do the following processing only if the
957 // class uses inline types?
958 if (EnableValhalla) {
959 ResourceMark rm(THREAD);
960 for (int i = 0; i < methods()->length(); i++) {
961 Method* m = methods()->at(i);
962 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
963 if (ss.is_reference()) {
964 if (ss.is_array()) {
965 continue;
966 }
967 if (ss.type() == T_PRIMITIVE_OBJECT) {
968 Symbol* symb = ss.as_symbol();
969 if (symb == name()) continue;
970 oop loader = class_loader();
971 oop protection_domain = this->protection_domain();
972 Klass* klass = SystemDictionary::resolve_or_fail(symb,
973 Handle(THREAD, loader), Handle(THREAD, protection_domain), true,
974 CHECK_false);
975 if (klass == NULL) {
976 THROW_(vmSymbols::java_lang_LinkageError(), false);
977 }
978 if (!klass->is_inline_klass()) {
979 Exceptions::fthrow(
980 THREAD_AND_LOCATION,
981 vmSymbols::java_lang_IncompatibleClassChangeError(),
982 "class %s is not an inline type",
983 klass->external_name());
984 }
985 }
986 }
987 }
988 }
989 // Aggressively preloading all classes from the Preload attribute
990 if (preload_classes() != NULL) {
991 for (int i = 0; i < preload_classes()->length(); i++) {
992 if (constants()->tag_at(preload_classes()->at(i)).is_klass()) continue;
993 Symbol* class_name = constants()->klass_at_noresolve(preload_classes()->at(i));
994 if (class_name == name()) continue;
995 oop loader = class_loader();
996 oop protection_domain = this->protection_domain();
997 Klass* klass = SystemDictionary::resolve_or_null(class_name,
998 Handle(THREAD, loader), Handle(THREAD, protection_domain), THREAD);
999 if (HAS_PENDING_EXCEPTION) {
1000 CLEAR_PENDING_EXCEPTION;
1001 }
1002 if (klass != NULL) {
1003 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());
1004 } else {
1005 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());
1006 }
1007 }
1008 }
1009 }
1010
1011 // in case the class is linked in the process of linking its superclasses
1012 if (is_linked()) {
1013 return true;
1014 }
1015
1016 // trace only the link time for this klass that includes
1017 // the verification time
1018 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
1019 ClassLoader::perf_class_link_selftime(),
1020 ClassLoader::perf_classes_linked(),
1021 jt->get_thread_stat()->perf_recursion_counts_addr(),
1022 jt->get_thread_stat()->perf_timers_addr(),
1023 PerfClassTraceTime::CLASS_LINK);
1024
1025 // verification & rewriting
1026 {
1027 HandleMark hm(THREAD);
1028 Handle h_init_lock(THREAD, init_lock());
1029 ObjectLocker ol(h_init_lock, jt);
1030 // rewritten will have been set if loader constraint error found
1243 if (is_in_error_state()) {
1244 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1245 ResourceMark rm(THREAD);
1246 Handle cause(THREAD, get_initialization_error(THREAD));
1247
1248 stringStream ss;
1249 ss.print("Could not initialize class %s", external_name());
1250 if (cause.is_null()) {
1251 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1252 } else {
1253 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1254 ss.as_string(), cause);
1255 }
1256 }
1257
1258 // Step 6
1259 set_init_state(being_initialized);
1260 set_init_thread(jt);
1261 }
1262
1263 // Pre-allocating an instance of the default value
1264 if (is_inline_klass()) {
1265 InlineKlass* vk = InlineKlass::cast(this);
1266 oop val = vk->allocate_instance(THREAD);
1267 if (HAS_PENDING_EXCEPTION) {
1268 Handle e(THREAD, PENDING_EXCEPTION);
1269 CLEAR_PENDING_EXCEPTION;
1270 {
1271 EXCEPTION_MARK;
1272 add_initialization_error(THREAD, e);
1273 // Locks object, set state, and notify all waiting threads
1274 set_initialization_state_and_notify(initialization_error, THREAD);
1275 CLEAR_PENDING_EXCEPTION;
1276 }
1277 THROW_OOP(e());
1278 }
1279 vk->set_default_value(val);
1280 }
1281
1282 // Step 7
1283 // Next, if C is a class rather than an interface, initialize it's super class and super
1284 // interfaces.
1285 if (!is_interface()) {
1286 Klass* super_klass = super();
1287 if (super_klass != NULL && super_klass->should_be_initialized()) {
1288 super_klass->initialize(THREAD);
1289 }
1290 // If C implements any interface that declares a non-static, concrete method,
1291 // the initialization of C triggers initialization of its super interfaces.
1292 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1293 // having a superinterface that declares, non-static, concrete methods
1294 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1295 initialize_super_interfaces(THREAD);
1296 }
1297
1298 // If any exceptions, complete abruptly, throwing the same exception as above.
1299 if (HAS_PENDING_EXCEPTION) {
1300 Handle e(THREAD, PENDING_EXCEPTION);
1301 CLEAR_PENDING_EXCEPTION;
1302 {
1303 EXCEPTION_MARK;
1304 add_initialization_error(THREAD, e);
1305 // Locks object, set state, and notify all waiting threads
1306 set_initialization_state_and_notify(initialization_error, THREAD);
1307 CLEAR_PENDING_EXCEPTION;
1308 }
1309 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1310 THROW_OOP(e());
1311 }
1312 }
1313
1314 // Step 8
1315 // Initialize classes of inline fields
1316 if (EnableValhalla) {
1317 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1318 if (Signature::basic_type(fs.signature()) == T_PRIMITIVE_OBJECT) {
1319 Klass* klass = get_inline_type_field_klass_or_null(fs.index());
1320 if (fs.access_flags().is_static() && klass == NULL) {
1321 klass = SystemDictionary::resolve_or_fail(field_signature(fs.index())->fundamental_name(THREAD),
1322 Handle(THREAD, class_loader()),
1323 Handle(THREAD, protection_domain()),
1324 true, THREAD);
1325 set_inline_type_field_klass(fs.index(), klass);
1326 }
1327
1328 if (!HAS_PENDING_EXCEPTION) {
1329 assert(klass != NULL, "Must be");
1330 InstanceKlass::cast(klass)->initialize(THREAD);
1331 if (fs.access_flags().is_static()) {
1332 if (java_mirror()->obj_field(fs.offset()) == NULL) {
1333 java_mirror()->obj_field_put(fs.offset(), InlineKlass::cast(klass)->default_value());
1334 }
1335 }
1336 }
1337
1338 if (HAS_PENDING_EXCEPTION) {
1339 Handle e(THREAD, PENDING_EXCEPTION);
1340 CLEAR_PENDING_EXCEPTION;
1341 {
1342 EXCEPTION_MARK;
1343 add_initialization_error(THREAD, e);
1344 // Locks object, set state, and notify all waiting threads
1345 set_initialization_state_and_notify(initialization_error, THREAD);
1346 CLEAR_PENDING_EXCEPTION;
1347 }
1348 THROW_OOP(e());
1349 }
1350 }
1351 }
1352 }
1353
1354
1355 // Step 9
1356 {
1357 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1358 if (class_initializer() != NULL) {
1359 // Timer includes any side effects of class initialization (resolution,
1360 // etc), but not recursive entry into call_class_initializer().
1361 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1362 ClassLoader::perf_class_init_selftime(),
1363 ClassLoader::perf_classes_inited(),
1364 jt->get_thread_stat()->perf_recursion_counts_addr(),
1365 jt->get_thread_stat()->perf_timers_addr(),
1366 PerfClassTraceTime::CLASS_CLINIT);
1367 call_class_initializer(THREAD);
1368 } else {
1369 // The elapsed time is so small it's not worth counting.
1370 if (UsePerfData) {
1371 ClassLoader::perf_classes_inited()->inc();
1372 }
1373 call_class_initializer(THREAD);
1374 }
1375 }
1376
1377 // Step 10
1378 if (!HAS_PENDING_EXCEPTION) {
1379 set_initialization_state_and_notify(fully_initialized, CHECK);
1380 debug_only(vtable().verify(tty, true);)
1381 }
1382 else {
1383 // Step 11 and 12
1384 Handle e(THREAD, PENDING_EXCEPTION);
1385 CLEAR_PENDING_EXCEPTION;
1386 // JVMTI has already reported the pending exception
1387 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1388 JvmtiExport::clear_detected_exception(jt);
1389 {
1390 EXCEPTION_MARK;
1391 add_initialization_error(THREAD, e);
1392 set_initialization_state_and_notify(initialization_error, THREAD);
1393 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1394 // JVMTI has already reported the pending exception
1395 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1396 JvmtiExport::clear_detected_exception(jt);
1397 }
1398 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1399 if (e->is_a(vmClasses::Error_klass())) {
1400 THROW_OOP(e());
1401 } else {
1402 JavaCallArguments args(e);
1403 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1628 : vmSymbols::java_lang_InstantiationException(), external_name());
1629 }
1630 if (this == vmClasses::Class_klass()) {
1631 ResourceMark rm(THREAD);
1632 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1633 : vmSymbols::java_lang_IllegalAccessException(), external_name());
1634 }
1635 }
1636
1637 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1638 // Need load-acquire for lock-free read
1639 if (array_klasses_acquire() == NULL) {
1640 ResourceMark rm(THREAD);
1641 JavaThread *jt = THREAD;
1642 {
1643 // Atomic creation of array_klasses
1644 MutexLocker ma(THREAD, MultiArray_lock);
1645
1646 // Check if update has already taken place
1647 if (array_klasses() == NULL) {
1648 ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this,
1649 false, false, CHECK_NULL);
1650 // use 'release' to pair with lock-free load
1651 release_set_array_klasses(k);
1652 }
1653 }
1654 }
1655 // array_klasses() will always be set at this point
1656 ArrayKlass* ak = array_klasses();
1657 return ak->array_klass(n, THREAD);
1658 }
1659
1660 Klass* InstanceKlass::array_klass_or_null(int n) {
1661 // Need load-acquire for lock-free read
1662 ArrayKlass* ak = array_klasses_acquire();
1663 if (ak == NULL) {
1664 return NULL;
1665 } else {
1666 return ak->array_klass_or_null(n);
1667 }
1668 }
1669
1670 Klass* InstanceKlass::array_klass(TRAPS) {
1671 return array_klass(1, THREAD);
1672 }
1673
1674 Klass* InstanceKlass::array_klass_or_null() {
1675 return array_klass_or_null(1);
1676 }
1677
1678 static int call_class_initializer_counter = 0; // for debugging
1679
1680 Method* InstanceKlass::class_initializer() const {
1681 Method* clinit = find_method(
1682 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1683 if (clinit != NULL && clinit->is_class_initializer()) {
1684 return clinit;
1685 }
1686 return NULL;
1687 }
1688
1689 void InstanceKlass::call_class_initializer(TRAPS) {
1690 if (ReplayCompiles &&
1691 (ReplaySuppressInitializers == 1 ||
1692 (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
1693 // Hide the existence of the initializer for the purpose of replaying the compile
1694 return;
1695 }
1696
1697 #if INCLUDE_CDS
1698 // This is needed to ensure the consistency of the archived heap objects.
1699 if (has_archived_enum_objs()) {
1700 assert(is_shared(), "must be");
1701 bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
1702 if (initialized) {
1703 return;
1712 ResourceMark rm(THREAD);
1713 LogStream ls(lt);
1714 ls.print("%d Initializing ", call_class_initializer_counter++);
1715 name()->print_value_on(&ls);
1716 ls.print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
1717 }
1718 if (h_method() != NULL) {
1719 JavaCallArguments args; // No arguments
1720 JavaValue result(T_VOID);
1721 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1722 }
1723 }
1724
1725
1726 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1727 InterpreterOopMap* entry_for) {
1728 // Lazily create the _oop_map_cache at first request
1729 // Lock-free access requires load_acquire.
1730 OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1731 if (oop_map_cache == NULL) {
1732 MutexLocker x(OopMapCacheAlloc_lock, Mutex::_no_safepoint_check_flag);
1733 // Check if _oop_map_cache was allocated while we were waiting for this lock
1734 if ((oop_map_cache = _oop_map_cache) == NULL) {
1735 oop_map_cache = new OopMapCache();
1736 // Ensure _oop_map_cache is stable, since it is examined without a lock
1737 Atomic::release_store(&_oop_map_cache, oop_map_cache);
1738 }
1739 }
1740 // _oop_map_cache is constant after init; lookup below does its own locking.
1741 oop_map_cache->lookup(method, bci, entry_for);
1742 }
1743
1744 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1745 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1746 Symbol* f_name = fs.name();
1747 Symbol* f_sig = fs.signature();
1748 if (f_name == name && f_sig == sig) {
1749 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1750 return true;
1751 }
1752 }
1753 return false;
1754 }
1755
1756
1757 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1758 const int n = local_interfaces()->length();
1759 for (int i = 0; i < n; i++) {
1760 Klass* intf1 = local_interfaces()->at(i);
1761 assert(intf1->is_interface(), "just checking type");
1762 // search for field in current interface
1763 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
1794
1795 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1796 // search order according to newest JVM spec (5.4.3.2, p.167).
1797 // 1) search for field in current klass
1798 if (find_local_field(name, sig, fd)) {
1799 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1800 }
1801 // 2) search for field recursively in direct superinterfaces
1802 if (is_static) {
1803 Klass* intf = find_interface_field(name, sig, fd);
1804 if (intf != NULL) return intf;
1805 }
1806 // 3) apply field lookup recursively if superclass exists
1807 { Klass* supr = super();
1808 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1809 }
1810 // 4) otherwise field lookup fails
1811 return NULL;
1812 }
1813
1814 bool InstanceKlass::contains_field_offset(int offset) {
1815 if (this->is_inline_klass()) {
1816 InlineKlass* vk = InlineKlass::cast(this);
1817 return offset >= vk->first_field_offset() && offset < (vk->first_field_offset() + vk->get_exact_size_in_bytes());
1818 } else {
1819 fieldDescriptor fd;
1820 return find_field_from_offset(offset, false, &fd);
1821 }
1822 }
1823
1824 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1825 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1826 if (fs.offset() == offset) {
1827 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1828 if (fd->is_static() == is_static) return true;
1829 }
1830 }
1831 return false;
1832 }
1833
1834
1835 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1836 Klass* klass = const_cast<InstanceKlass*>(this);
1837 while (klass != NULL) {
1838 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1839 return true;
1840 }
1841 klass = klass->super();
1842 }
2195 }
2196
2197 // uncached_lookup_method searches both the local class methods array and all
2198 // superclasses methods arrays, skipping any overpass methods in superclasses,
2199 // and possibly skipping private methods.
2200 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2201 const Symbol* signature,
2202 OverpassLookupMode overpass_mode,
2203 PrivateLookupMode private_mode) const {
2204 OverpassLookupMode overpass_local_mode = overpass_mode;
2205 const Klass* klass = this;
2206 while (klass != NULL) {
2207 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2208 signature,
2209 overpass_local_mode,
2210 StaticLookupMode::find,
2211 private_mode);
2212 if (method != NULL) {
2213 return method;
2214 }
2215 if (name == vmSymbols::object_initializer_name()) {
2216 break; // <init> is never inherited, not even as a static factory
2217 }
2218 klass = klass->super();
2219 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2220 }
2221 return NULL;
2222 }
2223
2224 #ifdef ASSERT
2225 // search through class hierarchy and return true if this class or
2226 // one of the superclasses was redefined
2227 bool InstanceKlass::has_redefined_this_or_super() const {
2228 const Klass* klass = this;
2229 while (klass != NULL) {
2230 if (InstanceKlass::cast(klass)->has_been_redefined()) {
2231 return true;
2232 }
2233 klass = klass->super();
2234 }
2235 return false;
2236 }
2237 #endif
2679 if (itable_length() > 0) {
2680 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2681 int method_table_offset_in_words = ioe->offset()/wordSize;
2682 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2683 / itableOffsetEntry::size();
2684
2685 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2686 if (ioe->interface_klass() != NULL) {
2687 it->push(ioe->interface_klass_addr());
2688 itableMethodEntry* ime = ioe->first_method_entry(this);
2689 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2690 for (int index = 0; index < n; index ++) {
2691 it->push(ime[index].method_addr());
2692 }
2693 }
2694 }
2695 }
2696
2697 it->push(&_nest_members);
2698 it->push(&_permitted_subclasses);
2699 it->push(&_preload_classes);
2700 it->push(&_record_components);
2701
2702 if (has_inline_type_fields()) {
2703 for (int i = 0; i < java_fields_count(); i++) {
2704 it->push(&((Klass**)adr_inline_type_field_klasses())[i]);
2705 }
2706 }
2707 }
2708
2709 void InstanceKlass::remove_unshareable_info() {
2710
2711 if (is_linked()) {
2712 assert(can_be_verified_at_dumptime(), "must be");
2713 // Remember this so we can avoid walking the hierarchy at runtime.
2714 set_verified_at_dump_time();
2715 }
2716
2717 Klass::remove_unshareable_info();
2718
2719 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2720 // Classes are attempted to link during dumping and may fail,
2721 // but these classes are still in the dictionary and class list in CLD.
2722 // If the class has failed verification, there is nothing else to remove.
2723 return;
2724 }
2725
2726 // Reset to the 'allocated' state to prevent any premature accessing to
2729 // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2730 _init_state = allocated;
2731
2732 { // Otherwise this needs to take out the Compile_lock.
2733 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2734 init_implementor();
2735 }
2736
2737 constants()->remove_unshareable_info();
2738
2739 for (int i = 0; i < methods()->length(); i++) {
2740 Method* m = methods()->at(i);
2741 m->remove_unshareable_info();
2742 }
2743
2744 // do array classes also.
2745 if (array_klasses() != NULL) {
2746 array_klasses()->remove_unshareable_info();
2747 }
2748
2749 if (has_inline_type_fields()) {
2750 for (AllFieldStream fs(fields(), constants()); !fs.done(); fs.next()) {
2751 if (Signature::basic_type(fs.signature()) == T_PRIMITIVE_OBJECT) {
2752 reset_inline_type_field_klass(fs.index());
2753 }
2754 }
2755 }
2756
2757 // These are not allocated from metaspace. They are safe to set to NULL.
2758 _source_debug_extension = NULL;
2759 _dep_context = NULL;
2760 _osr_nmethods_head = NULL;
2761 #if INCLUDE_JVMTI
2762 _breakpoints = NULL;
2763 _previous_versions = NULL;
2764 _cached_class_file = NULL;
2765 _jvmti_cached_class_field_map = NULL;
2766 #endif
2767
2768 _init_thread = NULL;
2769 _methods_jmethod_ids = NULL;
2770 _jni_ids = NULL;
2771 _oop_map_cache = NULL;
2772 // clear _nest_host to ensure re-load at runtime
2773 _nest_host = NULL;
2774 init_shared_package_entry();
2775 _dep_context_last_cleaned = 0;
2776 }
2798 if (is_shared_unregistered_class()) {
2799 _package_entry = NULL;
2800 } else {
2801 _package_entry = PackageEntry::get_archived_entry(_package_entry);
2802 }
2803 }
2804 ArchivePtrMarker::mark_pointer((address**)&_package_entry);
2805 #endif
2806 }
2807
2808 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2809 PackageEntry* pkg_entry, TRAPS) {
2810 // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2811 // before the InstanceKlass is added to the SystemDictionary. Make
2812 // sure the current state is <loaded.
2813 assert(!is_loaded(), "invalid init state");
2814 assert(!shared_loading_failed(), "Must not try to load failed class again");
2815 set_package(loader_data, pkg_entry, CHECK);
2816 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2817
2818 if (is_inline_klass()) {
2819 InlineKlass::cast(this)->initialize_calling_convention(CHECK);
2820 }
2821
2822 Array<Method*>* methods = this->methods();
2823 int num_methods = methods->length();
2824 for (int index = 0; index < num_methods; ++index) {
2825 methods->at(index)->restore_unshareable_info(CHECK);
2826 }
2827 #if INCLUDE_JVMTI
2828 if (JvmtiExport::has_redefined_a_class()) {
2829 // Reinitialize vtable because RedefineClasses may have changed some
2830 // entries in this vtable for super classes so the CDS vtable might
2831 // point to old or obsolete entries. RedefineClasses doesn't fix up
2832 // vtables in the shared system dictionary, only the main one.
2833 // It also redefines the itable too so fix that too.
2834 // First fix any default methods that point to a super class that may
2835 // have been redefined.
2836 bool trace_name_printed = false;
2837 adjust_default_methods(&trace_name_printed);
2838 vtable().initialize_vtable();
2839 itable().initialize_itable();
2840 }
2841 #endif
3005
3006 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
3007 if (array == NULL) {
3008 _source_debug_extension = NULL;
3009 } else {
3010 // Adding one to the attribute length in order to store a null terminator
3011 // character could cause an overflow because the attribute length is
3012 // already coded with an u4 in the classfile, but in practice, it's
3013 // unlikely to happen.
3014 assert((length+1) > length, "Overflow checking");
3015 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
3016 for (int i = 0; i < length; i++) {
3017 sde[i] = array[i];
3018 }
3019 sde[length] = '\0';
3020 _source_debug_extension = sde;
3021 }
3022 }
3023
3024 const char* InstanceKlass::signature_name() const {
3025 return signature_name_of_carrier(JVM_SIGNATURE_CLASS);
3026 }
3027
3028 const char* InstanceKlass::signature_name_of_carrier(char c) const {
3029 int hash_len = 0;
3030 char hash_buf[40];
3031
3032 // Get the internal name as a c string
3033 const char* src = (const char*) (name()->as_C_string());
3034 const int src_length = (int)strlen(src);
3035
3036 char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
3037
3038 // Add L or Q as type indicator
3039 int dest_index = 0;
3040 dest[dest_index++] = c;
3041
3042 // Add the actual class name
3043 for (int src_index = 0; src_index < src_length; ) {
3044 dest[dest_index++] = src[src_index++];
3045 }
3046
3047 if (is_hidden()) { // Replace the last '+' with a '.'.
3048 for (int index = (int)src_length; index > 0; index--) {
3049 if (dest[index] == '+') {
3050 dest[index] = JVM_SIGNATURE_DOT;
3051 break;
3052 }
3053 }
3054 }
3055
3056 // If we have a hash, append it
3057 for (int hash_index = 0; hash_index < hash_len; ) {
3058 dest[dest_index++] = hash_buf[hash_index++];
3059 }
3060
3626 }
3627 osr = osr->osr_link();
3628 }
3629
3630 assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3631 if (best != NULL && best->comp_level() >= comp_level) {
3632 return best;
3633 }
3634 return NULL;
3635 }
3636
3637 // -----------------------------------------------------------------------------------------------------
3638 // Printing
3639
3640 #define BULLET " - "
3641
3642 static const char* state_names[] = {
3643 "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3644 };
3645
3646 static void print_vtable(address self, intptr_t* start, int len, outputStream* st) {
3647 ResourceMark rm;
3648 int* forward_refs = NEW_RESOURCE_ARRAY(int, len);
3649 for (int i = 0; i < len; i++) forward_refs[i] = 0;
3650 for (int i = 0; i < len; i++) {
3651 intptr_t e = start[i];
3652 st->print("%d : " INTPTR_FORMAT, i, e);
3653 if (forward_refs[i] != 0) {
3654 int from = forward_refs[i];
3655 int off = (int) start[from];
3656 st->print(" (offset %d <= [%d])", off, from);
3657 }
3658 if (MetaspaceObj::is_valid((Metadata*)e)) {
3659 st->print(" ");
3660 ((Metadata*)e)->print_value_on(st);
3661 } else if (self != NULL && e > 0 && e < 0x10000) {
3662 address location = self + e;
3663 int index = (int)((intptr_t*)location - start);
3664 st->print(" (offset %d => [%d])", (int)e, index);
3665 if (index >= 0 && index < len)
3666 forward_refs[index] = i;
3667 }
3668 st->cr();
3669 }
3670 }
3671
3672 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3673 return print_vtable(NULL, reinterpret_cast<intptr_t*>(start), len, st);
3674 }
3675
3676 template<typename T>
3677 static void print_array_on(outputStream* st, Array<T>* array) {
3678 if (array == NULL) { st->print_cr("NULL"); return; }
3679 array->print_value_on(st); st->cr();
3680 if (Verbose || WizardMode) {
3681 for (int i = 0; i < array->length(); i++) {
3682 st->print("%d : ", i); array->at(i)->print_value_on(st); st->cr();
3683 }
3684 }
3685 }
3686
3687 static void print_array_on(outputStream* st, Array<int>* array) {
3688 if (array == NULL) { st->print_cr("NULL"); return; }
3689 array->print_value_on(st); st->cr();
3690 if (Verbose || WizardMode) {
3691 for (int i = 0; i < array->length(); i++) {
3692 st->print("%d : %d", i, array->at(i)); st->cr();
3693 }
3694 }
3695 }
3696
3697 const char* InstanceKlass::init_state_name() const {
3698 return state_names[_init_state];
3699 }
3700
3701 void InstanceKlass::print_on(outputStream* st) const {
3702 assert(is_klass(), "must be klass");
3703 Klass::print_on(st);
3704
3705 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3706 st->print(BULLET"klass size: %d", size()); st->cr();
3707 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3708 st->print(BULLET"misc flags: 0x%x", _misc_flags); st->cr();
3709 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3710 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3711 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3712 st->print(BULLET"sub: ");
3713 Klass* sub = subklass();
3714 int n;
3715 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
3716 if (n < MaxSubklassPrintSize) {
3717 sub->print_value_on(st);
3718 st->print(" ");
3719 }
3720 }
3721 if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3722 st->cr();
3723
3724 if (is_interface()) {
3725 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3726 if (nof_implementors() == 1) {
3727 st->print_cr(BULLET"implementor: ");
3728 st->print(" ");
3729 implementor()->print_value_on(st);
3730 st->cr();
3731 }
3732 }
3733
3734 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3735 st->print(BULLET"methods: "); print_array_on(st, methods());
3736 st->print(BULLET"method ordering: "); print_array_on(st, method_ordering());
3737 st->print(BULLET"default_methods: "); print_array_on(st, default_methods());
3738 if (default_vtable_indices() != NULL) {
3739 st->print(BULLET"default vtable indices: "); print_array_on(st, default_vtable_indices());
3740 }
3741 st->print(BULLET"local interfaces: "); print_array_on(st, local_interfaces());
3742 st->print(BULLET"trans. interfaces: "); print_array_on(st, transitive_interfaces());
3743 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
3744 if (class_loader_data() != NULL) {
3745 st->print(BULLET"class loader data: ");
3746 class_loader_data()->print_value_on(st);
3747 st->cr();
3748 }
3749 if (source_file_name() != NULL) {
3750 st->print(BULLET"source file: ");
3751 source_file_name()->print_value_on(st);
3752 st->cr();
3753 }
3754 if (source_debug_extension() != NULL) {
3755 st->print(BULLET"source debug extension: ");
3756 st->print("%s", source_debug_extension());
3757 st->cr();
3758 }
3759 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
3760 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
3761 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
3762 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
3768 pv_node = pv_node->previous_versions()) {
3769 if (!have_pv)
3770 st->print(BULLET"previous version: ");
3771 have_pv = true;
3772 pv_node->constants()->print_value_on(st);
3773 }
3774 if (have_pv) st->cr();
3775 }
3776
3777 if (generic_signature() != NULL) {
3778 st->print(BULLET"generic signature: ");
3779 generic_signature()->print_value_on(st);
3780 st->cr();
3781 }
3782 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
3783 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
3784 if (record_components() != NULL) {
3785 st->print(BULLET"record components: "); record_components()->print_value_on(st); st->cr();
3786 }
3787 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
3788 st->print(BULLET"preload classes: "); preload_classes()->print_value_on(st); st->cr();
3789 if (java_mirror() != NULL) {
3790 st->print(BULLET"java mirror: ");
3791 java_mirror()->print_value_on(st);
3792 st->cr();
3793 } else {
3794 st->print_cr(BULLET"java mirror: NULL");
3795 }
3796 st->print(BULLET"vtable length %d (start addr: " INTPTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3797 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
3798 st->print(BULLET"itable length %d (start addr: " INTPTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3799 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(NULL, start_of_itable(), itable_length(), st);
3800 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3801 FieldPrinter print_static_field(st);
3802 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3803 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3804 FieldPrinter print_nonstatic_field(st);
3805 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3806 ik->print_nonstatic_fields(&print_nonstatic_field);
3807
3808 st->print(BULLET"non-static oop maps: ");
3809 OopMapBlock* map = start_of_nonstatic_oop_maps();
3810 OopMapBlock* end_map = map + nonstatic_oop_map_count();
3811 while (map < end_map) {
3812 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3813 map++;
3814 }
3815 st->cr();
3816 }
3817
3818 void InstanceKlass::print_value_on(outputStream* st) const {
3819 assert(is_klass(), "must be klass");
|