52 #include "logging/logStream.hpp"
53 #include "memory/allocation.inline.hpp"
54 #include "memory/iterator.inline.hpp"
55 #include "memory/metadataFactory.hpp"
56 #include "memory/metaspaceClosure.hpp"
57 #include "memory/oopFactory.hpp"
58 #include "memory/resourceArea.hpp"
59 #include "memory/universe.hpp"
60 #include "oops/fieldStreams.inline.hpp"
61 #include "oops/constantPool.hpp"
62 #include "oops/instanceClassLoaderKlass.hpp"
63 #include "oops/instanceKlass.inline.hpp"
64 #include "oops/instanceMirrorKlass.hpp"
65 #include "oops/instanceOop.hpp"
66 #include "oops/instanceStackChunkKlass.hpp"
67 #include "oops/klass.inline.hpp"
68 #include "oops/method.hpp"
69 #include "oops/oop.inline.hpp"
70 #include "oops/recordComponent.hpp"
71 #include "oops/symbol.hpp"
72 #include "prims/jvmtiExport.hpp"
73 #include "prims/jvmtiRedefineClasses.hpp"
74 #include "prims/jvmtiThreadState.hpp"
75 #include "prims/methodComparator.hpp"
76 #include "runtime/arguments.hpp"
77 #include "runtime/atomic.hpp"
78 #include "runtime/fieldDescriptor.inline.hpp"
79 #include "runtime/handles.inline.hpp"
80 #include "runtime/javaCalls.hpp"
81 #include "runtime/javaThread.inline.hpp"
82 #include "runtime/mutexLocker.hpp"
83 #include "runtime/orderAccess.hpp"
84 #include "runtime/reflectionUtils.hpp"
85 #include "runtime/threads.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"
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 static inline bool is_stack_chunk_class(const Symbol* class_name,
167 const ClassLoaderData* loader_data) {
168 return (class_name == vmSymbols::jdk_internal_vm_StackChunk() &&
169 loader_data->is_the_null_class_loader_data());
170 }
171
172 // private: called to verify that k is a static member of this nest.
173 // We know that k is an instance class in the same package and hence the
174 // same classloader.
175 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
176 assert(!is_hidden(), "unexpected hidden class");
177 if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
178 if (log_is_enabled(Trace, class, nestmates)) {
179 ResourceMark rm(current);
180 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
181 k->external_name(), this->external_name());
182 }
183 return false;
184 }
185
418 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
419 this->external_name(),
420 access ? "" : "NOT ",
421 k->external_name());
422 return access;
423 }
424
425 const char* InstanceKlass::nest_host_error() {
426 if (_nest_host_index == 0) {
427 return NULL;
428 } else {
429 constantPoolHandle cph(Thread::current(), constants());
430 return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
431 }
432 }
433
434 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
435 const int size = InstanceKlass::size(parser.vtable_size(),
436 parser.itable_size(),
437 nonstatic_oop_map_size(parser.total_oop_map_count()),
438 parser.is_interface());
439
440 const Symbol* const class_name = parser.class_name();
441 assert(class_name != NULL, "invariant");
442 ClassLoaderData* loader_data = parser.loader_data();
443 assert(loader_data != NULL, "invariant");
444
445 InstanceKlass* ik;
446
447 // Allocation
448 if (parser.is_instance_ref_klass()) {
449 // java.lang.ref.Reference
450 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
451 } else if (class_name == vmSymbols::java_lang_Class()) {
452 // mirror - java.lang.Class
453 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
454 } else if (is_stack_chunk_class(class_name, loader_data)) {
455 // stack chunk
456 ik = new (loader_data, size, THREAD) InstanceStackChunkKlass(parser);
457 } else if (is_class_loader(class_name, parser)) {
458 // class loader - java.lang.ClassLoader
459 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
460 } else {
461 // normal
462 ik = new (loader_data, size, THREAD) InstanceKlass(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 return ik;
472 }
473
474
475 // copy method ordering from resource area to Metaspace
476 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
477 if (m != NULL) {
478 // allocate a new array and copy contents (memcpy?)
479 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
480 for (int i = 0; i < m->length(); i++) {
481 _method_ordering->at_put(i, m->at(i));
482 }
483 } else {
484 _method_ordering = Universe::the_empty_int_array();
485 }
486 }
487
488 // create a new array of vtable_indices for default methods
489 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
490 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
491 assert(default_vtable_indices() == NULL, "only create once");
492 set_default_vtable_indices(vtable_indices);
493 return vtable_indices;
494 }
495
496 static Monitor* create_init_monitor(const char* name) {
497 return new Monitor(Mutex::safepoint, name);
498 }
499
500 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
501 Klass(kind),
502 _nest_members(NULL),
503 _nest_host(NULL),
504 _permitted_subclasses(NULL),
505 _record_components(NULL),
506 _static_field_size(parser.static_field_size()),
507 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
508 _itable_len(parser.itable_size()),
509 _nest_host_index(0),
510 _init_state(allocated),
511 _reference_type(reference_type),
512 _init_monitor(create_init_monitor("InstanceKlassInitMonitor_lock")),
513 _init_thread(NULL)
514 {
515 set_vtable_length(parser.vtable_size());
516 set_access_flags(parser.access_flags());
517 if (parser.is_hidden()) set_is_hidden();
518 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
519 false));
520
521 assert(NULL == _methods, "underlying memory not zeroed?");
522 assert(is_instance_klass(), "is layout incorrect?");
523 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
524 }
525
526 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
527 Array<Method*>* methods) {
528 if (methods != NULL && methods != Universe::the_empty_method_array() &&
529 !methods->is_shared()) {
530 for (int i = 0; i < methods->length(); i++) {
531 Method* method = methods->at(i);
532 if (method == NULL) continue; // maybe null if error processing
533 // Only want to delete methods that are not executing for RedefineClasses.
534 // The previous version will point to them so they're not totally dangling
535 assert (!method->on_stack(), "shouldn't be called with methods on stack");
536 MetadataFactory::free_metadata(loader_data, method);
537 }
538 MetadataFactory::free_array<Method*>(loader_data, methods);
539 }
540 }
541
542 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
543 const Klass* super_klass,
668 inner_classes() != Universe::the_empty_short_array() &&
669 !inner_classes()->is_shared()) {
670 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
671 }
672 set_inner_classes(NULL);
673
674 if (nest_members() != NULL &&
675 nest_members() != Universe::the_empty_short_array() &&
676 !nest_members()->is_shared()) {
677 MetadataFactory::free_array<jushort>(loader_data, nest_members());
678 }
679 set_nest_members(NULL);
680
681 if (permitted_subclasses() != NULL &&
682 permitted_subclasses() != Universe::the_empty_short_array() &&
683 !permitted_subclasses()->is_shared()) {
684 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
685 }
686 set_permitted_subclasses(NULL);
687
688 // We should deallocate the Annotations instance if it's not in shared spaces.
689 if (annotations() != NULL && !annotations()->is_shared()) {
690 MetadataFactory::free_metadata(loader_data, annotations());
691 }
692 set_annotations(NULL);
693
694 SystemDictionaryShared::handle_class_unloading(this);
695 }
696
697 bool InstanceKlass::is_record() const {
698 return _record_components != NULL &&
699 is_final() &&
700 java_super() == vmClasses::Record_klass();
701 }
702
703 bool InstanceKlass::is_sealed() const {
704 return _permitted_subclasses != NULL &&
705 _permitted_subclasses != Universe::the_empty_short_array();
706 }
707
818 vmSymbols::java_lang_IncompatibleClassChangeError(),
819 "class %s has interface %s as super class",
820 external_name(),
821 super_klass->external_name()
822 );
823 return false;
824 }
825
826 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
827 ik_super->link_class_impl(CHECK_false);
828 }
829
830 // link all interfaces implemented by this class before linking this class
831 Array<InstanceKlass*>* interfaces = local_interfaces();
832 int num_interfaces = interfaces->length();
833 for (int index = 0; index < num_interfaces; index++) {
834 InstanceKlass* interk = interfaces->at(index);
835 interk->link_class_impl(CHECK_false);
836 }
837
838 // in case the class is linked in the process of linking its superclasses
839 if (is_linked()) {
840 return true;
841 }
842
843 // trace only the link time for this klass that includes
844 // the verification time
845 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
846 ClassLoader::perf_class_link_selftime(),
847 ClassLoader::perf_classes_linked(),
848 jt->get_thread_stat()->perf_recursion_counts_addr(),
849 jt->get_thread_stat()->perf_timers_addr(),
850 PerfClassTraceTime::CLASS_LINK);
851
852 // verification & rewriting
853 {
854 LockLinkState init_lock(this, jt);
855
856 // rewritten will have been set if loader constraint error found
857 // on an earlier link attempt
1062 set_init_thread(jt);
1063 }
1064 }
1065
1066 // Throw error outside lock
1067 if (throw_error) {
1068 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1069 ResourceMark rm(THREAD);
1070 Handle cause(THREAD, get_initialization_error(THREAD));
1071
1072 stringStream ss;
1073 ss.print("Could not initialize class %s", external_name());
1074 if (cause.is_null()) {
1075 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1076 } else {
1077 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1078 ss.as_string(), cause);
1079 }
1080 }
1081
1082 // Step 7
1083 // Next, if C is a class rather than an interface, initialize it's super class and super
1084 // interfaces.
1085 if (!is_interface()) {
1086 Klass* super_klass = super();
1087 if (super_klass != NULL && super_klass->should_be_initialized()) {
1088 super_klass->initialize(THREAD);
1089 }
1090 // If C implements any interface that declares a non-static, concrete method,
1091 // the initialization of C triggers initialization of its super interfaces.
1092 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1093 // having a superinterface that declares, non-static, concrete methods
1094 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1095 initialize_super_interfaces(THREAD);
1096 }
1097
1098 // If any exceptions, complete abruptly, throwing the same exception as above.
1099 if (HAS_PENDING_EXCEPTION) {
1100 Handle e(THREAD, PENDING_EXCEPTION);
1101 CLEAR_PENDING_EXCEPTION;
1102 {
1103 EXCEPTION_MARK;
1104 add_initialization_error(THREAD, e);
1105 // Locks object, set state, and notify all waiting threads
1106 set_initialization_state_and_notify(initialization_error, THREAD);
1107 CLEAR_PENDING_EXCEPTION;
1108 }
1109 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1110 THROW_OOP(e());
1111 }
1112 }
1113
1114
1115 // Step 8
1116 {
1117 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1118 if (class_initializer() != NULL) {
1119 // Timer includes any side effects of class initialization (resolution,
1120 // etc), but not recursive entry into call_class_initializer().
1121 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1122 ClassLoader::perf_class_init_selftime(),
1123 ClassLoader::perf_classes_inited(),
1124 jt->get_thread_stat()->perf_recursion_counts_addr(),
1125 jt->get_thread_stat()->perf_timers_addr(),
1126 PerfClassTraceTime::CLASS_CLINIT);
1127 call_class_initializer(THREAD);
1128 } else {
1129 // The elapsed time is so small it's not worth counting.
1130 if (UsePerfData) {
1131 ClassLoader::perf_classes_inited()->inc();
1132 }
1133 call_class_initializer(THREAD);
1134 }
1135 }
1136
1137 // Step 9
1138 if (!HAS_PENDING_EXCEPTION) {
1139 set_initialization_state_and_notify(fully_initialized, THREAD);
1140 debug_only(vtable().verify(tty, true);)
1141 }
1142 else {
1143 // Step 10 and 11
1144 Handle e(THREAD, PENDING_EXCEPTION);
1145 CLEAR_PENDING_EXCEPTION;
1146 // JVMTI has already reported the pending exception
1147 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1148 JvmtiExport::clear_detected_exception(jt);
1149 {
1150 EXCEPTION_MARK;
1151 add_initialization_error(THREAD, e);
1152 set_initialization_state_and_notify(initialization_error, THREAD);
1153 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1154 // JVMTI has already reported the pending exception
1155 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1156 JvmtiExport::clear_detected_exception(jt);
1157 }
1158 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1159 if (e->is_a(vmClasses::Error_klass())) {
1160 THROW_OOP(e());
1161 } else {
1162 JavaCallArguments args(e);
1163 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1392 : vmSymbols::java_lang_InstantiationException(), external_name());
1393 }
1394 if (this == vmClasses::Class_klass()) {
1395 ResourceMark rm(THREAD);
1396 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1397 : vmSymbols::java_lang_IllegalAccessException(), external_name());
1398 }
1399 }
1400
1401 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1402 // Need load-acquire for lock-free read
1403 if (array_klasses_acquire() == NULL) {
1404 ResourceMark rm(THREAD);
1405 JavaThread *jt = THREAD;
1406 {
1407 // Atomic creation of array_klasses
1408 MutexLocker ma(THREAD, MultiArray_lock);
1409
1410 // Check if update has already taken place
1411 if (array_klasses() == NULL) {
1412 ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);
1413 // use 'release' to pair with lock-free load
1414 release_set_array_klasses(k);
1415 }
1416 }
1417 }
1418 // array_klasses() will always be set at this point
1419 ObjArrayKlass* oak = array_klasses();
1420 return oak->array_klass(n, THREAD);
1421 }
1422
1423 Klass* InstanceKlass::array_klass_or_null(int n) {
1424 // Need load-acquire for lock-free read
1425 ObjArrayKlass* oak = array_klasses_acquire();
1426 if (oak == NULL) {
1427 return NULL;
1428 } else {
1429 return oak->array_klass_or_null(n);
1430 }
1431 }
1432
1433 Klass* InstanceKlass::array_klass(TRAPS) {
1434 return array_klass(1, THREAD);
1435 }
1436
1437 Klass* InstanceKlass::array_klass_or_null() {
1438 return array_klass_or_null(1);
1439 }
1440
1441 static int call_class_initializer_counter = 0; // for debugging
1442
1443 Method* InstanceKlass::class_initializer() const {
1444 Method* clinit = find_method(
1445 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1446 if (clinit != NULL && clinit->has_valid_initializer_flags()) {
1447 return clinit;
1448 }
1449 return NULL;
1450 }
1451
1452 void InstanceKlass::call_class_initializer(TRAPS) {
1453 if (ReplayCompiles &&
1454 (ReplaySuppressInitializers == 1 ||
1455 (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
1456 // Hide the existence of the initializer for the purpose of replaying the compile
1457 return;
1458 }
1459
1460 #if INCLUDE_CDS
1461 // This is needed to ensure the consistency of the archived heap objects.
1462 if (has_archived_enum_objs()) {
1463 assert(is_shared(), "must be");
1464 bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
1465 if (initialized) {
1466 return;
1475 ResourceMark rm(THREAD);
1476 LogStream ls(lt);
1477 ls.print("%d Initializing ", call_class_initializer_counter++);
1478 name()->print_value_on(&ls);
1479 ls.print_cr("%s (" PTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
1480 }
1481 if (h_method() != NULL) {
1482 JavaCallArguments args; // No arguments
1483 JavaValue result(T_VOID);
1484 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1485 }
1486 }
1487
1488
1489 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1490 InterpreterOopMap* entry_for) {
1491 // Lazily create the _oop_map_cache at first request
1492 // Lock-free access requires load_acquire.
1493 OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1494 if (oop_map_cache == NULL) {
1495 MutexLocker x(OopMapCacheAlloc_lock);
1496 // Check if _oop_map_cache was allocated while we were waiting for this lock
1497 if ((oop_map_cache = _oop_map_cache) == NULL) {
1498 oop_map_cache = new OopMapCache();
1499 // Ensure _oop_map_cache is stable, since it is examined without a lock
1500 Atomic::release_store(&_oop_map_cache, oop_map_cache);
1501 }
1502 }
1503 // _oop_map_cache is constant after init; lookup below does its own locking.
1504 oop_map_cache->lookup(method, bci, entry_for);
1505 }
1506
1507 bool InstanceKlass::contains_field_offset(int offset) {
1508 fieldDescriptor fd;
1509 return find_field_from_offset(offset, false, &fd);
1510 }
1511
1512 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1513 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1514 Symbol* f_name = fs.name();
1515 Symbol* f_sig = fs.signature();
1516 if (f_name == name && f_sig == sig) {
1517 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1518 return true;
1519 }
1520 }
1521 return false;
1522 }
1523
1524
1525 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1526 const int n = local_interfaces()->length();
1527 for (int i = 0; i < n; i++) {
1528 Klass* intf1 = local_interfaces()->at(i);
1529 assert(intf1->is_interface(), "just checking type");
1530 // search for field in current interface
1531 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
1562
1563 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1564 // search order according to newest JVM spec (5.4.3.2, p.167).
1565 // 1) search for field in current klass
1566 if (find_local_field(name, sig, fd)) {
1567 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1568 }
1569 // 2) search for field recursively in direct superinterfaces
1570 if (is_static) {
1571 Klass* intf = find_interface_field(name, sig, fd);
1572 if (intf != NULL) return intf;
1573 }
1574 // 3) apply field lookup recursively if superclass exists
1575 { Klass* supr = super();
1576 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1577 }
1578 // 4) otherwise field lookup fails
1579 return NULL;
1580 }
1581
1582
1583 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1584 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1585 if (fs.offset() == offset) {
1586 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1587 if (fd->is_static() == is_static) return true;
1588 }
1589 }
1590 return false;
1591 }
1592
1593
1594 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1595 Klass* klass = const_cast<InstanceKlass*>(this);
1596 while (klass != NULL) {
1597 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1598 return true;
1599 }
1600 klass = klass->super();
1601 }
1954 }
1955
1956 // uncached_lookup_method searches both the local class methods array and all
1957 // superclasses methods arrays, skipping any overpass methods in superclasses,
1958 // and possibly skipping private methods.
1959 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
1960 const Symbol* signature,
1961 OverpassLookupMode overpass_mode,
1962 PrivateLookupMode private_mode) const {
1963 OverpassLookupMode overpass_local_mode = overpass_mode;
1964 const Klass* klass = this;
1965 while (klass != NULL) {
1966 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
1967 signature,
1968 overpass_local_mode,
1969 StaticLookupMode::find,
1970 private_mode);
1971 if (method != NULL) {
1972 return method;
1973 }
1974 klass = klass->super();
1975 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
1976 }
1977 return NULL;
1978 }
1979
1980 #ifdef ASSERT
1981 // search through class hierarchy and return true if this class or
1982 // one of the superclasses was redefined
1983 bool InstanceKlass::has_redefined_this_or_super() const {
1984 const Klass* klass = this;
1985 while (klass != NULL) {
1986 if (InstanceKlass::cast(klass)->has_been_redefined()) {
1987 return true;
1988 }
1989 klass = klass->super();
1990 }
1991 return false;
1992 }
1993 #endif
2417 if (itable_length() > 0) {
2418 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2419 int method_table_offset_in_words = ioe->offset()/wordSize;
2420 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2421 / itableOffsetEntry::size();
2422
2423 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2424 if (ioe->interface_klass() != NULL) {
2425 it->push(ioe->interface_klass_addr());
2426 itableMethodEntry* ime = ioe->first_method_entry(this);
2427 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2428 for (int index = 0; index < n; index ++) {
2429 it->push(ime[index].method_addr());
2430 }
2431 }
2432 }
2433 }
2434
2435 it->push(&_nest_members);
2436 it->push(&_permitted_subclasses);
2437 it->push(&_record_components);
2438 }
2439
2440 #if INCLUDE_CDS
2441 void InstanceKlass::remove_unshareable_info() {
2442
2443 if (is_linked()) {
2444 assert(can_be_verified_at_dumptime(), "must be");
2445 // Remember this so we can avoid walking the hierarchy at runtime.
2446 set_verified_at_dump_time();
2447 }
2448
2449 Klass::remove_unshareable_info();
2450
2451 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2452 // Classes are attempted to link during dumping and may fail,
2453 // but these classes are still in the dictionary and class list in CLD.
2454 // If the class has failed verification, there is nothing else to remove.
2455 return;
2456 }
2457
2461 // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2462 _init_state = allocated;
2463
2464 { // Otherwise this needs to take out the Compile_lock.
2465 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2466 init_implementor();
2467 }
2468
2469 constants()->remove_unshareable_info();
2470
2471 for (int i = 0; i < methods()->length(); i++) {
2472 Method* m = methods()->at(i);
2473 m->remove_unshareable_info();
2474 }
2475
2476 // do array classes also.
2477 if (array_klasses() != NULL) {
2478 array_klasses()->remove_unshareable_info();
2479 }
2480
2481 // These are not allocated from metaspace. They are safe to set to NULL.
2482 _source_debug_extension = NULL;
2483 _dep_context = NULL;
2484 _osr_nmethods_head = NULL;
2485 #if INCLUDE_JVMTI
2486 _breakpoints = NULL;
2487 _previous_versions = NULL;
2488 _cached_class_file = NULL;
2489 _jvmti_cached_class_field_map = NULL;
2490 #endif
2491
2492 _init_thread = NULL;
2493 _methods_jmethod_ids = NULL;
2494 _jni_ids = NULL;
2495 _oop_map_cache = NULL;
2496 // clear _nest_host to ensure re-load at runtime
2497 _nest_host = NULL;
2498 init_shared_package_entry();
2499 _dep_context_last_cleaned = 0;
2500 _init_monitor = NULL;
2523 if (is_shared_unregistered_class()) {
2524 _package_entry = NULL;
2525 } else {
2526 _package_entry = PackageEntry::get_archived_entry(_package_entry);
2527 }
2528 }
2529 ArchivePtrMarker::mark_pointer((address**)&_package_entry);
2530 #endif
2531 }
2532
2533 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2534 PackageEntry* pkg_entry, TRAPS) {
2535 // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2536 // before the InstanceKlass is added to the SystemDictionary. Make
2537 // sure the current state is <loaded.
2538 assert(!is_loaded(), "invalid init state");
2539 assert(!shared_loading_failed(), "Must not try to load failed class again");
2540 set_package(loader_data, pkg_entry, CHECK);
2541 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2542
2543 Array<Method*>* methods = this->methods();
2544 int num_methods = methods->length();
2545 for (int index = 0; index < num_methods; ++index) {
2546 methods->at(index)->restore_unshareable_info(CHECK);
2547 }
2548 #if INCLUDE_JVMTI
2549 if (JvmtiExport::has_redefined_a_class()) {
2550 // Reinitialize vtable because RedefineClasses may have changed some
2551 // entries in this vtable for super classes so the CDS vtable might
2552 // point to old or obsolete entries. RedefineClasses doesn't fix up
2553 // vtables in the shared system dictionary, only the main one.
2554 // It also redefines the itable too so fix that too.
2555 // First fix any default methods that point to a super class that may
2556 // have been redefined.
2557 bool trace_name_printed = false;
2558 adjust_default_methods(&trace_name_printed);
2559 vtable().initialize_vtable();
2560 itable().initialize_itable();
2561 }
2562 #endif
2705
2706 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2707 if (array == NULL) {
2708 _source_debug_extension = NULL;
2709 } else {
2710 // Adding one to the attribute length in order to store a null terminator
2711 // character could cause an overflow because the attribute length is
2712 // already coded with an u4 in the classfile, but in practice, it's
2713 // unlikely to happen.
2714 assert((length+1) > length, "Overflow checking");
2715 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2716 for (int i = 0; i < length; i++) {
2717 sde[i] = array[i];
2718 }
2719 sde[length] = '\0';
2720 _source_debug_extension = sde;
2721 }
2722 }
2723
2724 const char* InstanceKlass::signature_name() const {
2725
2726 // Get the internal name as a c string
2727 const char* src = (const char*) (name()->as_C_string());
2728 const int src_length = (int)strlen(src);
2729
2730 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
2731
2732 // Add L as type indicator
2733 int dest_index = 0;
2734 dest[dest_index++] = JVM_SIGNATURE_CLASS;
2735
2736 // Add the actual class name
2737 for (int src_index = 0; src_index < src_length; ) {
2738 dest[dest_index++] = src[src_index++];
2739 }
2740
2741 if (is_hidden()) { // Replace the last '+' with a '.'.
2742 for (int index = (int)src_length; index > 0; index--) {
2743 if (dest[index] == '+') {
2744 dest[index] = JVM_SIGNATURE_DOT;
2745 break;
2746 }
2747 }
2748 }
2749
2750 // Add the semicolon and the NULL
2751 dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
2752 dest[dest_index] = '\0';
2753 return dest;
2754 }
3056 jint InstanceKlass::compute_modifier_flags() const {
3057 jint access = access_flags().as_int();
3058
3059 // But check if it happens to be member class.
3060 InnerClassesIterator iter(this);
3061 for (; !iter.done(); iter.next()) {
3062 int ioff = iter.inner_class_info_index();
3063 // Inner class attribute can be zero, skip it.
3064 // Strange but true: JVM spec. allows null inner class refs.
3065 if (ioff == 0) continue;
3066
3067 // only look at classes that are already loaded
3068 // since we are looking for the flags for our self.
3069 Symbol* inner_name = constants()->klass_name_at(ioff);
3070 if (name() == inner_name) {
3071 // This is really a member class.
3072 access = iter.inner_access_flags();
3073 break;
3074 }
3075 }
3076 // Remember to strip ACC_SUPER bit
3077 return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
3078 }
3079
3080 jint InstanceKlass::jvmti_class_status() const {
3081 jint result = 0;
3082
3083 if (is_linked()) {
3084 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3085 }
3086
3087 if (is_initialized()) {
3088 assert(is_linked(), "Class status is not consistent");
3089 result |= JVMTI_CLASS_STATUS_INITIALIZED;
3090 }
3091 if (is_in_error_state()) {
3092 result |= JVMTI_CLASS_STATUS_ERROR;
3093 }
3094 return result;
3095 }
3096
3097 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {
3314 }
3315 osr = osr->osr_link();
3316 }
3317
3318 assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3319 if (best != NULL && best->comp_level() >= comp_level) {
3320 return best;
3321 }
3322 return NULL;
3323 }
3324
3325 // -----------------------------------------------------------------------------------------------------
3326 // Printing
3327
3328 #define BULLET " - "
3329
3330 static const char* state_names[] = {
3331 "allocated", "loaded", "being_linked", "linked", "being_initialized", "fully_initialized", "initialization_error"
3332 };
3333
3334 static void print_vtable(intptr_t* start, int len, outputStream* st) {
3335 for (int i = 0; i < len; i++) {
3336 intptr_t e = start[i];
3337 st->print("%d : " INTPTR_FORMAT, i, e);
3338 if (MetaspaceObj::is_valid((Metadata*)e)) {
3339 st->print(" ");
3340 ((Metadata*)e)->print_value_on(st);
3341 }
3342 st->cr();
3343 }
3344 }
3345
3346 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3347 return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);
3348 }
3349
3350 const char* InstanceKlass::init_state_name() const {
3351 return state_names[_init_state];
3352 }
3353
3354 void InstanceKlass::print_on(outputStream* st) const {
3355 assert(is_klass(), "must be klass");
3356 Klass::print_on(st);
3357
3358 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3359 st->print(BULLET"klass size: %d", size()); st->cr();
3360 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3361 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3362 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3363 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3364 st->print(BULLET"sub: ");
3365 Klass* sub = subklass();
3366 int n;
3367 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
3368 if (n < MaxSubklassPrintSize) {
3369 sub->print_value_on(st);
3370 st->print(" ");
3371 }
3372 }
3373 if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3374 st->cr();
3375
3376 if (is_interface()) {
3377 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3378 if (nof_implementors() == 1) {
3379 st->print_cr(BULLET"implementor: ");
3380 st->print(" ");
3381 implementor()->print_value_on(st);
3382 st->cr();
3383 }
3384 }
3385
3386 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3387 st->print(BULLET"methods: "); methods()->print_value_on(st); st->cr();
3388 if (Verbose || WizardMode) {
3389 Array<Method*>* method_array = methods();
3390 for (int i = 0; i < method_array->length(); i++) {
3391 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3392 }
3393 }
3394 st->print(BULLET"method ordering: "); method_ordering()->print_value_on(st); st->cr();
3395 st->print(BULLET"default_methods: "); default_methods()->print_value_on(st); st->cr();
3396 if (Verbose && default_methods() != NULL) {
3397 Array<Method*>* method_array = default_methods();
3398 for (int i = 0; i < method_array->length(); i++) {
3399 st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3400 }
3401 }
3402 if (default_vtable_indices() != NULL) {
3403 st->print(BULLET"default vtable indices: "); default_vtable_indices()->print_value_on(st); st->cr();
3404 }
3405 st->print(BULLET"local interfaces: "); local_interfaces()->print_value_on(st); st->cr();
3406 st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3407 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
3408 if (class_loader_data() != NULL) {
3409 st->print(BULLET"class loader data: ");
3410 class_loader_data()->print_value_on(st);
3411 st->cr();
3412 }
3413 if (source_file_name() != NULL) {
3414 st->print(BULLET"source file: ");
3415 source_file_name()->print_value_on(st);
3416 st->cr();
3417 }
3418 if (source_debug_extension() != NULL) {
3419 st->print(BULLET"source debug extension: ");
3420 st->print("%s", source_debug_extension());
3421 st->cr();
3422 }
3423 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
3424 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
3425 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
3426 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
3432 pv_node = pv_node->previous_versions()) {
3433 if (!have_pv)
3434 st->print(BULLET"previous version: ");
3435 have_pv = true;
3436 pv_node->constants()->print_value_on(st);
3437 }
3438 if (have_pv) st->cr();
3439 }
3440
3441 if (generic_signature() != NULL) {
3442 st->print(BULLET"generic signature: ");
3443 generic_signature()->print_value_on(st);
3444 st->cr();
3445 }
3446 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
3447 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
3448 if (record_components() != NULL) {
3449 st->print(BULLET"record components: "); record_components()->print_value_on(st); st->cr();
3450 }
3451 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
3452 if (java_mirror() != NULL) {
3453 st->print(BULLET"java mirror: ");
3454 java_mirror()->print_value_on(st);
3455 st->cr();
3456 } else {
3457 st->print_cr(BULLET"java mirror: NULL");
3458 }
3459 st->print(BULLET"vtable length %d (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3460 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
3461 st->print(BULLET"itable length %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3462 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_itable(), itable_length(), st);
3463 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3464 FieldPrinter print_static_field(st);
3465 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3466 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3467 FieldPrinter print_nonstatic_field(st);
3468 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3469 ik->print_nonstatic_fields(&print_nonstatic_field);
3470
3471 st->print(BULLET"non-static oop maps: ");
3472 OopMapBlock* map = start_of_nonstatic_oop_maps();
3473 OopMapBlock* end_map = map + nonstatic_oop_map_count();
3474 while (map < end_map) {
3475 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3476 map++;
3477 }
3478 st->cr();
3479 }
3480
3481 void InstanceKlass::print_value_on(outputStream* st) const {
3482 assert(is_klass(), "must be klass");
|
52 #include "logging/logStream.hpp"
53 #include "memory/allocation.inline.hpp"
54 #include "memory/iterator.inline.hpp"
55 #include "memory/metadataFactory.hpp"
56 #include "memory/metaspaceClosure.hpp"
57 #include "memory/oopFactory.hpp"
58 #include "memory/resourceArea.hpp"
59 #include "memory/universe.hpp"
60 #include "oops/fieldStreams.inline.hpp"
61 #include "oops/constantPool.hpp"
62 #include "oops/instanceClassLoaderKlass.hpp"
63 #include "oops/instanceKlass.inline.hpp"
64 #include "oops/instanceMirrorKlass.hpp"
65 #include "oops/instanceOop.hpp"
66 #include "oops/instanceStackChunkKlass.hpp"
67 #include "oops/klass.inline.hpp"
68 #include "oops/method.hpp"
69 #include "oops/oop.inline.hpp"
70 #include "oops/recordComponent.hpp"
71 #include "oops/symbol.hpp"
72 #include "oops/inlineKlass.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/javaThread.inline.hpp"
83 #include "runtime/mutexLocker.hpp"
84 #include "runtime/orderAccess.hpp"
85 #include "runtime/reflectionUtils.hpp"
86 #include "runtime/threads.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"
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 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 == NULL || _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 NULL;
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 parser.has_inline_fields() ? parser.java_fields_count() : 0,
443 parser.is_inline_type());
444
445 const Symbol* const class_name = parser.class_name();
446 assert(class_name != NULL, "invariant");
447 ClassLoaderData* loader_data = parser.loader_data();
448 assert(loader_data != NULL, "invariant");
449
450 InstanceKlass* ik;
451
452 // Allocation
453 if (parser.is_instance_ref_klass()) {
454 // java.lang.ref.Reference
455 ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
456 } else if (class_name == vmSymbols::java_lang_Class()) {
457 // mirror - java.lang.Class
458 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
459 } else if (is_stack_chunk_class(class_name, loader_data)) {
460 // stack chunk
461 ik = new (loader_data, size, THREAD) InstanceStackChunkKlass(parser);
462 } else if (is_class_loader(class_name, parser)) {
463 // class loader - java.lang.ClassLoader
464 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
465 } else if (parser.is_inline_type()) {
466 // inline type
467 ik = new (loader_data, size, THREAD) InlineKlass(parser);
468 } else {
469 // normal
470 ik = new (loader_data, size, THREAD) InstanceKlass(parser);
471 }
472
473 // Check for pending exception before adding to the loader data and incrementing
474 // class count. Can get OOM here.
475 if (HAS_PENDING_EXCEPTION) {
476 return NULL;
477 }
478
479 #ifdef ASSERT
480 assert(ik->size() == size, "");
481 ik->bounds_check((address) ik->start_of_vtable(), false, size);
482 ik->bounds_check((address) ik->start_of_itable(), false, size);
483 ik->bounds_check((address) ik->end_of_itable(), true, size);
484 ik->bounds_check((address) ik->end_of_nonstatic_oop_maps(), true, size);
485 #endif //ASSERT
486 return ik;
487 }
488
489 #ifndef PRODUCT
490 bool InstanceKlass::bounds_check(address addr, bool edge_ok, intptr_t size_in_bytes) const {
491 const char* bad = NULL;
492 address end = NULL;
493 if (addr < (address)this) {
494 bad = "before";
495 } else if (addr == (address)this) {
496 if (edge_ok) return true;
497 bad = "just before";
498 } else if (addr == (end = (address)this + sizeof(intptr_t) * (size_in_bytes < 0 ? size() : size_in_bytes))) {
499 if (edge_ok) return true;
500 bad = "just after";
501 } else if (addr > end) {
502 bad = "after";
503 } else {
504 return true;
505 }
506 tty->print_cr("%s object bounds: " INTPTR_FORMAT " [" INTPTR_FORMAT ".." INTPTR_FORMAT "]",
507 bad, (intptr_t)addr, (intptr_t)this, (intptr_t)end);
508 Verbose = WizardMode = true; this->print(); //@@
509 return false;
510 }
511 #endif //PRODUCT
512
513 // copy method ordering from resource area to Metaspace
514 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
515 if (m != NULL) {
516 // allocate a new array and copy contents (memcpy?)
517 _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
518 for (int i = 0; i < m->length(); i++) {
519 _method_ordering->at_put(i, m->at(i));
520 }
521 } else {
522 _method_ordering = Universe::the_empty_int_array();
523 }
524 }
525
526 // create a new array of vtable_indices for default methods
527 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
528 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
529 assert(default_vtable_indices() == NULL, "only create once");
530 set_default_vtable_indices(vtable_indices);
531 return vtable_indices;
532 }
533
534 static Monitor* create_init_monitor(const char* name) {
535 return new Monitor(Mutex::safepoint, name);
536 }
537
538 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
539 Klass(kind),
540 _nest_members(NULL),
541 _nest_host(NULL),
542 _permitted_subclasses(NULL),
543 _record_components(NULL),
544 _static_field_size(parser.static_field_size()),
545 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
546 _itable_len(parser.itable_size()),
547 _nest_host_index(0),
548 _init_state(allocated),
549 _reference_type(reference_type),
550 _init_monitor(create_init_monitor("InstanceKlassInitMonitor_lock")),
551 _init_thread(NULL),
552 _inline_type_field_klasses(NULL),
553 _preload_classes(NULL),
554 _adr_inlineklass_fixed_block(NULL)
555 {
556 set_vtable_length(parser.vtable_size());
557 set_access_flags(parser.access_flags());
558 if (parser.is_hidden()) set_is_hidden();
559 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
560 false));
561 if (parser.has_inline_fields()) {
562 set_has_inline_type_fields();
563 }
564 _java_fields_count = parser.java_fields_count();
565
566 assert(NULL == _methods, "underlying memory not zeroed?");
567 assert(is_instance_klass(), "is layout incorrect?");
568 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
569
570 if (has_inline_type_fields()) {
571 _inline_type_field_klasses = (const Klass**) adr_inline_type_field_klasses();
572 }
573 }
574
575 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
576 Array<Method*>* methods) {
577 if (methods != NULL && methods != Universe::the_empty_method_array() &&
578 !methods->is_shared()) {
579 for (int i = 0; i < methods->length(); i++) {
580 Method* method = methods->at(i);
581 if (method == NULL) continue; // maybe null if error processing
582 // Only want to delete methods that are not executing for RedefineClasses.
583 // The previous version will point to them so they're not totally dangling
584 assert (!method->on_stack(), "shouldn't be called with methods on stack");
585 MetadataFactory::free_metadata(loader_data, method);
586 }
587 MetadataFactory::free_array<Method*>(loader_data, methods);
588 }
589 }
590
591 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
592 const Klass* super_klass,
717 inner_classes() != Universe::the_empty_short_array() &&
718 !inner_classes()->is_shared()) {
719 MetadataFactory::free_array<jushort>(loader_data, inner_classes());
720 }
721 set_inner_classes(NULL);
722
723 if (nest_members() != NULL &&
724 nest_members() != Universe::the_empty_short_array() &&
725 !nest_members()->is_shared()) {
726 MetadataFactory::free_array<jushort>(loader_data, nest_members());
727 }
728 set_nest_members(NULL);
729
730 if (permitted_subclasses() != NULL &&
731 permitted_subclasses() != Universe::the_empty_short_array() &&
732 !permitted_subclasses()->is_shared()) {
733 MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
734 }
735 set_permitted_subclasses(NULL);
736
737 if (preload_classes() != NULL &&
738 preload_classes() != Universe::the_empty_short_array() &&
739 !preload_classes()->is_shared()) {
740 MetadataFactory::free_array<jushort>(loader_data, preload_classes());
741 }
742
743 // We should deallocate the Annotations instance if it's not in shared spaces.
744 if (annotations() != NULL && !annotations()->is_shared()) {
745 MetadataFactory::free_metadata(loader_data, annotations());
746 }
747 set_annotations(NULL);
748
749 SystemDictionaryShared::handle_class_unloading(this);
750 }
751
752 bool InstanceKlass::is_record() const {
753 return _record_components != NULL &&
754 is_final() &&
755 java_super() == vmClasses::Record_klass();
756 }
757
758 bool InstanceKlass::is_sealed() const {
759 return _permitted_subclasses != NULL &&
760 _permitted_subclasses != Universe::the_empty_short_array();
761 }
762
873 vmSymbols::java_lang_IncompatibleClassChangeError(),
874 "class %s has interface %s as super class",
875 external_name(),
876 super_klass->external_name()
877 );
878 return false;
879 }
880
881 InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
882 ik_super->link_class_impl(CHECK_false);
883 }
884
885 // link all interfaces implemented by this class before linking this class
886 Array<InstanceKlass*>* interfaces = local_interfaces();
887 int num_interfaces = interfaces->length();
888 for (int index = 0; index < num_interfaces; index++) {
889 InstanceKlass* interk = interfaces->at(index);
890 interk->link_class_impl(CHECK_false);
891 }
892
893
894 // If a class declares a method that uses an inline class as an argument
895 // type or return inline type, this inline class must be loaded during the
896 // linking of this class because size and properties of the inline class
897 // must be known in order to be able to perform inline type optimizations.
898 // The implementation below is an approximation of this rule, the code
899 // iterates over all methods of the current class (including overridden
900 // methods), not only the methods declared by this class. This
901 // approximation makes the code simpler, and doesn't change the semantic
902 // because classes declaring methods overridden by the current class are
903 // linked (and have performed their own pre-loading) before the linking
904 // of the current class.
905
906
907 // Note:
908 // Inline class types are loaded during
909 // the loading phase (see ClassFileParser::post_process_parsed_stream()).
910 // Inline class types used as element types for array creation
911 // are not pre-loaded. Their loading is triggered by either anewarray
912 // or multianewarray bytecodes.
913
914 // Could it be possible to do the following processing only if the
915 // class uses inline types?
916 if (EnableValhalla) {
917 ResourceMark rm(THREAD);
918 if (EnablePrimitiveClasses) {
919 for (int i = 0; i < methods()->length(); i++) {
920 Method* m = methods()->at(i);
921 for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
922 if (ss.is_reference()) {
923 if (ss.is_array()) {
924 continue;
925 }
926 if (ss.type() == T_PRIMITIVE_OBJECT) {
927 Symbol* symb = ss.as_symbol();
928 if (symb == name()) continue;
929 oop loader = class_loader();
930 oop protection_domain = this->protection_domain();
931 Klass* klass = SystemDictionary::resolve_or_fail(symb,
932 Handle(THREAD, loader), Handle(THREAD, protection_domain), true,
933 CHECK_false);
934 if (klass == NULL) {
935 THROW_(vmSymbols::java_lang_LinkageError(), false);
936 }
937 if (!klass->is_inline_klass()) {
938 Exceptions::fthrow(
939 THREAD_AND_LOCATION,
940 vmSymbols::java_lang_IncompatibleClassChangeError(),
941 "class %s is not an inline type",
942 klass->external_name());
943 }
944 }
945 }
946 }
947 }
948 }
949 // Aggressively preloading all classes from the Preload attribute
950 if (preload_classes() != NULL) {
951 for (int i = 0; i < preload_classes()->length(); i++) {
952 if (constants()->tag_at(preload_classes()->at(i)).is_klass()) continue;
953 Symbol* class_name = constants()->klass_at_noresolve(preload_classes()->at(i));
954 if (class_name == name()) continue;
955 oop loader = class_loader();
956 oop protection_domain = this->protection_domain();
957 Klass* klass = SystemDictionary::resolve_or_null(class_name,
958 Handle(THREAD, loader), Handle(THREAD, protection_domain), THREAD);
959 if (HAS_PENDING_EXCEPTION) {
960 CLEAR_PENDING_EXCEPTION;
961 }
962 if (klass != NULL) {
963 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());
964 } else {
965 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());
966 }
967 }
968 }
969 }
970
971 // in case the class is linked in the process of linking its superclasses
972 if (is_linked()) {
973 return true;
974 }
975
976 // trace only the link time for this klass that includes
977 // the verification time
978 PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
979 ClassLoader::perf_class_link_selftime(),
980 ClassLoader::perf_classes_linked(),
981 jt->get_thread_stat()->perf_recursion_counts_addr(),
982 jt->get_thread_stat()->perf_timers_addr(),
983 PerfClassTraceTime::CLASS_LINK);
984
985 // verification & rewriting
986 {
987 LockLinkState init_lock(this, jt);
988
989 // rewritten will have been set if loader constraint error found
990 // on an earlier link attempt
1195 set_init_thread(jt);
1196 }
1197 }
1198
1199 // Throw error outside lock
1200 if (throw_error) {
1201 DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1202 ResourceMark rm(THREAD);
1203 Handle cause(THREAD, get_initialization_error(THREAD));
1204
1205 stringStream ss;
1206 ss.print("Could not initialize class %s", external_name());
1207 if (cause.is_null()) {
1208 THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1209 } else {
1210 THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1211 ss.as_string(), cause);
1212 }
1213 }
1214
1215 // Pre-allocating an instance of the default value
1216 if (is_inline_klass()) {
1217 InlineKlass* vk = InlineKlass::cast(this);
1218 oop val = vk->allocate_instance(THREAD);
1219 if (HAS_PENDING_EXCEPTION) {
1220 Handle e(THREAD, PENDING_EXCEPTION);
1221 CLEAR_PENDING_EXCEPTION;
1222 {
1223 EXCEPTION_MARK;
1224 add_initialization_error(THREAD, e);
1225 // Locks object, set state, and notify all waiting threads
1226 set_initialization_state_and_notify(initialization_error, THREAD);
1227 CLEAR_PENDING_EXCEPTION;
1228 }
1229 THROW_OOP(e());
1230 }
1231 vk->set_default_value(val);
1232 }
1233
1234 // Step 7
1235 // Next, if C is a class rather than an interface, initialize it's super class and super
1236 // interfaces.
1237 if (!is_interface()) {
1238 Klass* super_klass = super();
1239 if (super_klass != NULL && super_klass->should_be_initialized()) {
1240 super_klass->initialize(THREAD);
1241 }
1242 // If C implements any interface that declares a non-static, concrete method,
1243 // the initialization of C triggers initialization of its super interfaces.
1244 // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1245 // having a superinterface that declares, non-static, concrete methods
1246 if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1247 initialize_super_interfaces(THREAD);
1248 }
1249
1250 // If any exceptions, complete abruptly, throwing the same exception as above.
1251 if (HAS_PENDING_EXCEPTION) {
1252 Handle e(THREAD, PENDING_EXCEPTION);
1253 CLEAR_PENDING_EXCEPTION;
1254 {
1255 EXCEPTION_MARK;
1256 add_initialization_error(THREAD, e);
1257 // Locks object, set state, and notify all waiting threads
1258 set_initialization_state_and_notify(initialization_error, THREAD);
1259 CLEAR_PENDING_EXCEPTION;
1260 }
1261 DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1262 THROW_OOP(e());
1263 }
1264 }
1265
1266 // Step 8
1267 // Initialize classes of inline fields
1268 if (EnablePrimitiveClasses) {
1269 for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1270 if (Signature::basic_type(fs.signature()) == T_PRIMITIVE_OBJECT) {
1271 Klass* klass = get_inline_type_field_klass_or_null(fs.index());
1272 if (fs.access_flags().is_static() && klass == NULL) {
1273 klass = SystemDictionary::resolve_or_fail(field_signature(fs.index())->fundamental_name(THREAD),
1274 Handle(THREAD, class_loader()),
1275 Handle(THREAD, protection_domain()),
1276 true, THREAD);
1277 set_inline_type_field_klass(fs.index(), klass);
1278 }
1279
1280 if (!HAS_PENDING_EXCEPTION) {
1281 assert(klass != NULL, "Must be");
1282 InstanceKlass::cast(klass)->initialize(THREAD);
1283 if (fs.access_flags().is_static()) {
1284 if (java_mirror()->obj_field(fs.offset()) == NULL) {
1285 java_mirror()->obj_field_put(fs.offset(), InlineKlass::cast(klass)->default_value());
1286 }
1287 }
1288 }
1289
1290 if (HAS_PENDING_EXCEPTION) {
1291 Handle e(THREAD, PENDING_EXCEPTION);
1292 CLEAR_PENDING_EXCEPTION;
1293 {
1294 EXCEPTION_MARK;
1295 add_initialization_error(THREAD, e);
1296 // Locks object, set state, and notify all waiting threads
1297 set_initialization_state_and_notify(initialization_error, THREAD);
1298 CLEAR_PENDING_EXCEPTION;
1299 }
1300 THROW_OOP(e());
1301 }
1302 }
1303 }
1304 }
1305
1306
1307 // Step 9
1308 {
1309 DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1310 if (class_initializer() != NULL) {
1311 // Timer includes any side effects of class initialization (resolution,
1312 // etc), but not recursive entry into call_class_initializer().
1313 PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1314 ClassLoader::perf_class_init_selftime(),
1315 ClassLoader::perf_classes_inited(),
1316 jt->get_thread_stat()->perf_recursion_counts_addr(),
1317 jt->get_thread_stat()->perf_timers_addr(),
1318 PerfClassTraceTime::CLASS_CLINIT);
1319 call_class_initializer(THREAD);
1320 } else {
1321 // The elapsed time is so small it's not worth counting.
1322 if (UsePerfData) {
1323 ClassLoader::perf_classes_inited()->inc();
1324 }
1325 call_class_initializer(THREAD);
1326 }
1327 }
1328
1329 // Step 10
1330 if (!HAS_PENDING_EXCEPTION) {
1331 set_initialization_state_and_notify(fully_initialized, THREAD);
1332 debug_only(vtable().verify(tty, true);)
1333 }
1334 else {
1335 // Step 11 and 12
1336 Handle e(THREAD, PENDING_EXCEPTION);
1337 CLEAR_PENDING_EXCEPTION;
1338 // JVMTI has already reported the pending exception
1339 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1340 JvmtiExport::clear_detected_exception(jt);
1341 {
1342 EXCEPTION_MARK;
1343 add_initialization_error(THREAD, e);
1344 set_initialization_state_and_notify(initialization_error, THREAD);
1345 CLEAR_PENDING_EXCEPTION; // ignore any exception thrown, class initialization error is thrown below
1346 // JVMTI has already reported the pending exception
1347 // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1348 JvmtiExport::clear_detected_exception(jt);
1349 }
1350 DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1351 if (e->is_a(vmClasses::Error_klass())) {
1352 THROW_OOP(e());
1353 } else {
1354 JavaCallArguments args(e);
1355 THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),
1584 : vmSymbols::java_lang_InstantiationException(), external_name());
1585 }
1586 if (this == vmClasses::Class_klass()) {
1587 ResourceMark rm(THREAD);
1588 THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1589 : vmSymbols::java_lang_IllegalAccessException(), external_name());
1590 }
1591 }
1592
1593 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1594 // Need load-acquire for lock-free read
1595 if (array_klasses_acquire() == NULL) {
1596 ResourceMark rm(THREAD);
1597 JavaThread *jt = THREAD;
1598 {
1599 // Atomic creation of array_klasses
1600 MutexLocker ma(THREAD, MultiArray_lock);
1601
1602 // Check if update has already taken place
1603 if (array_klasses() == NULL) {
1604 ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this,
1605 false, false, CHECK_NULL);
1606 // use 'release' to pair with lock-free load
1607 release_set_array_klasses(k);
1608 }
1609 }
1610 }
1611 // array_klasses() will always be set at this point
1612 ArrayKlass* ak = array_klasses();
1613 return ak->array_klass(n, THREAD);
1614 }
1615
1616 Klass* InstanceKlass::array_klass_or_null(int n) {
1617 // Need load-acquire for lock-free read
1618 ArrayKlass* ak = array_klasses_acquire();
1619 if (ak == NULL) {
1620 return NULL;
1621 } else {
1622 return ak->array_klass_or_null(n);
1623 }
1624 }
1625
1626 Klass* InstanceKlass::array_klass(TRAPS) {
1627 return array_klass(1, THREAD);
1628 }
1629
1630 Klass* InstanceKlass::array_klass_or_null() {
1631 return array_klass_or_null(1);
1632 }
1633
1634 static int call_class_initializer_counter = 0; // for debugging
1635
1636 Method* InstanceKlass::class_initializer() const {
1637 Method* clinit = find_method(
1638 vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1639 if (clinit != NULL && clinit->is_class_initializer()) {
1640 return clinit;
1641 }
1642 return NULL;
1643 }
1644
1645 void InstanceKlass::call_class_initializer(TRAPS) {
1646 if (ReplayCompiles &&
1647 (ReplaySuppressInitializers == 1 ||
1648 (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
1649 // Hide the existence of the initializer for the purpose of replaying the compile
1650 return;
1651 }
1652
1653 #if INCLUDE_CDS
1654 // This is needed to ensure the consistency of the archived heap objects.
1655 if (has_archived_enum_objs()) {
1656 assert(is_shared(), "must be");
1657 bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
1658 if (initialized) {
1659 return;
1668 ResourceMark rm(THREAD);
1669 LogStream ls(lt);
1670 ls.print("%d Initializing ", call_class_initializer_counter++);
1671 name()->print_value_on(&ls);
1672 ls.print_cr("%s (" PTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
1673 }
1674 if (h_method() != NULL) {
1675 JavaCallArguments args; // No arguments
1676 JavaValue result(T_VOID);
1677 JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1678 }
1679 }
1680
1681
1682 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1683 InterpreterOopMap* entry_for) {
1684 // Lazily create the _oop_map_cache at first request
1685 // Lock-free access requires load_acquire.
1686 OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1687 if (oop_map_cache == NULL) {
1688 MutexLocker x(OopMapCacheAlloc_lock, Mutex::_no_safepoint_check_flag);
1689 // Check if _oop_map_cache was allocated while we were waiting for this lock
1690 if ((oop_map_cache = _oop_map_cache) == NULL) {
1691 oop_map_cache = new OopMapCache();
1692 // Ensure _oop_map_cache is stable, since it is examined without a lock
1693 Atomic::release_store(&_oop_map_cache, oop_map_cache);
1694 }
1695 }
1696 // _oop_map_cache is constant after init; lookup below does its own locking.
1697 oop_map_cache->lookup(method, bci, entry_for);
1698 }
1699
1700 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1701 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1702 Symbol* f_name = fs.name();
1703 Symbol* f_sig = fs.signature();
1704 if (f_name == name && f_sig == sig) {
1705 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1706 return true;
1707 }
1708 }
1709 return false;
1710 }
1711
1712
1713 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1714 const int n = local_interfaces()->length();
1715 for (int i = 0; i < n; i++) {
1716 Klass* intf1 = local_interfaces()->at(i);
1717 assert(intf1->is_interface(), "just checking type");
1718 // search for field in current interface
1719 if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {
1750
1751 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1752 // search order according to newest JVM spec (5.4.3.2, p.167).
1753 // 1) search for field in current klass
1754 if (find_local_field(name, sig, fd)) {
1755 if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1756 }
1757 // 2) search for field recursively in direct superinterfaces
1758 if (is_static) {
1759 Klass* intf = find_interface_field(name, sig, fd);
1760 if (intf != NULL) return intf;
1761 }
1762 // 3) apply field lookup recursively if superclass exists
1763 { Klass* supr = super();
1764 if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1765 }
1766 // 4) otherwise field lookup fails
1767 return NULL;
1768 }
1769
1770 bool InstanceKlass::contains_field_offset(int offset) {
1771 if (this->is_inline_klass()) {
1772 InlineKlass* vk = InlineKlass::cast(this);
1773 return offset >= vk->first_field_offset() && offset < (vk->first_field_offset() + vk->get_exact_size_in_bytes());
1774 } else {
1775 fieldDescriptor fd;
1776 return find_field_from_offset(offset, false, &fd);
1777 }
1778 }
1779
1780 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1781 for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1782 if (fs.offset() == offset) {
1783 fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1784 if (fd->is_static() == is_static) return true;
1785 }
1786 }
1787 return false;
1788 }
1789
1790
1791 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1792 Klass* klass = const_cast<InstanceKlass*>(this);
1793 while (klass != NULL) {
1794 if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1795 return true;
1796 }
1797 klass = klass->super();
1798 }
2151 }
2152
2153 // uncached_lookup_method searches both the local class methods array and all
2154 // superclasses methods arrays, skipping any overpass methods in superclasses,
2155 // and possibly skipping private methods.
2156 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2157 const Symbol* signature,
2158 OverpassLookupMode overpass_mode,
2159 PrivateLookupMode private_mode) const {
2160 OverpassLookupMode overpass_local_mode = overpass_mode;
2161 const Klass* klass = this;
2162 while (klass != NULL) {
2163 Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2164 signature,
2165 overpass_local_mode,
2166 StaticLookupMode::find,
2167 private_mode);
2168 if (method != NULL) {
2169 return method;
2170 }
2171 if (name == vmSymbols::object_initializer_name() ||
2172 name == vmSymbols::inline_factory_name()) {
2173 break; // <init> and <vnew> is never inherited
2174 }
2175 klass = klass->super();
2176 overpass_local_mode = OverpassLookupMode::skip; // Always ignore overpass methods in superclasses
2177 }
2178 return NULL;
2179 }
2180
2181 #ifdef ASSERT
2182 // search through class hierarchy and return true if this class or
2183 // one of the superclasses was redefined
2184 bool InstanceKlass::has_redefined_this_or_super() const {
2185 const Klass* klass = this;
2186 while (klass != NULL) {
2187 if (InstanceKlass::cast(klass)->has_been_redefined()) {
2188 return true;
2189 }
2190 klass = klass->super();
2191 }
2192 return false;
2193 }
2194 #endif
2618 if (itable_length() > 0) {
2619 itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2620 int method_table_offset_in_words = ioe->offset()/wordSize;
2621 int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2622 / itableOffsetEntry::size();
2623
2624 for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2625 if (ioe->interface_klass() != NULL) {
2626 it->push(ioe->interface_klass_addr());
2627 itableMethodEntry* ime = ioe->first_method_entry(this);
2628 int n = klassItable::method_count_for_interface(ioe->interface_klass());
2629 for (int index = 0; index < n; index ++) {
2630 it->push(ime[index].method_addr());
2631 }
2632 }
2633 }
2634 }
2635
2636 it->push(&_nest_members);
2637 it->push(&_permitted_subclasses);
2638 it->push(&_preload_classes);
2639 it->push(&_record_components);
2640
2641 if (has_inline_type_fields()) {
2642 for (int i = 0; i < java_fields_count(); i++) {
2643 it->push(&((Klass**)adr_inline_type_field_klasses())[i]);
2644 }
2645 }
2646 }
2647
2648 #if INCLUDE_CDS
2649 void InstanceKlass::remove_unshareable_info() {
2650
2651 if (is_linked()) {
2652 assert(can_be_verified_at_dumptime(), "must be");
2653 // Remember this so we can avoid walking the hierarchy at runtime.
2654 set_verified_at_dump_time();
2655 }
2656
2657 Klass::remove_unshareable_info();
2658
2659 if (SystemDictionaryShared::has_class_failed_verification(this)) {
2660 // Classes are attempted to link during dumping and may fail,
2661 // but these classes are still in the dictionary and class list in CLD.
2662 // If the class has failed verification, there is nothing else to remove.
2663 return;
2664 }
2665
2669 // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2670 _init_state = allocated;
2671
2672 { // Otherwise this needs to take out the Compile_lock.
2673 assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2674 init_implementor();
2675 }
2676
2677 constants()->remove_unshareable_info();
2678
2679 for (int i = 0; i < methods()->length(); i++) {
2680 Method* m = methods()->at(i);
2681 m->remove_unshareable_info();
2682 }
2683
2684 // do array classes also.
2685 if (array_klasses() != NULL) {
2686 array_klasses()->remove_unshareable_info();
2687 }
2688
2689 if (has_inline_type_fields()) {
2690 for (AllFieldStream fs(fields(), constants()); !fs.done(); fs.next()) {
2691 if (Signature::basic_type(fs.signature()) == T_PRIMITIVE_OBJECT) {
2692 reset_inline_type_field_klass(fs.index());
2693 }
2694 }
2695 }
2696
2697 // These are not allocated from metaspace. They are safe to set to NULL.
2698 _source_debug_extension = NULL;
2699 _dep_context = NULL;
2700 _osr_nmethods_head = NULL;
2701 #if INCLUDE_JVMTI
2702 _breakpoints = NULL;
2703 _previous_versions = NULL;
2704 _cached_class_file = NULL;
2705 _jvmti_cached_class_field_map = NULL;
2706 #endif
2707
2708 _init_thread = NULL;
2709 _methods_jmethod_ids = NULL;
2710 _jni_ids = NULL;
2711 _oop_map_cache = NULL;
2712 // clear _nest_host to ensure re-load at runtime
2713 _nest_host = NULL;
2714 init_shared_package_entry();
2715 _dep_context_last_cleaned = 0;
2716 _init_monitor = NULL;
2739 if (is_shared_unregistered_class()) {
2740 _package_entry = NULL;
2741 } else {
2742 _package_entry = PackageEntry::get_archived_entry(_package_entry);
2743 }
2744 }
2745 ArchivePtrMarker::mark_pointer((address**)&_package_entry);
2746 #endif
2747 }
2748
2749 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2750 PackageEntry* pkg_entry, TRAPS) {
2751 // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2752 // before the InstanceKlass is added to the SystemDictionary. Make
2753 // sure the current state is <loaded.
2754 assert(!is_loaded(), "invalid init state");
2755 assert(!shared_loading_failed(), "Must not try to load failed class again");
2756 set_package(loader_data, pkg_entry, CHECK);
2757 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2758
2759 if (is_inline_klass()) {
2760 InlineKlass::cast(this)->initialize_calling_convention(CHECK);
2761 }
2762
2763 Array<Method*>* methods = this->methods();
2764 int num_methods = methods->length();
2765 for (int index = 0; index < num_methods; ++index) {
2766 methods->at(index)->restore_unshareable_info(CHECK);
2767 }
2768 #if INCLUDE_JVMTI
2769 if (JvmtiExport::has_redefined_a_class()) {
2770 // Reinitialize vtable because RedefineClasses may have changed some
2771 // entries in this vtable for super classes so the CDS vtable might
2772 // point to old or obsolete entries. RedefineClasses doesn't fix up
2773 // vtables in the shared system dictionary, only the main one.
2774 // It also redefines the itable too so fix that too.
2775 // First fix any default methods that point to a super class that may
2776 // have been redefined.
2777 bool trace_name_printed = false;
2778 adjust_default_methods(&trace_name_printed);
2779 vtable().initialize_vtable();
2780 itable().initialize_itable();
2781 }
2782 #endif
2925
2926 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2927 if (array == NULL) {
2928 _source_debug_extension = NULL;
2929 } else {
2930 // Adding one to the attribute length in order to store a null terminator
2931 // character could cause an overflow because the attribute length is
2932 // already coded with an u4 in the classfile, but in practice, it's
2933 // unlikely to happen.
2934 assert((length+1) > length, "Overflow checking");
2935 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2936 for (int i = 0; i < length; i++) {
2937 sde[i] = array[i];
2938 }
2939 sde[length] = '\0';
2940 _source_debug_extension = sde;
2941 }
2942 }
2943
2944 const char* InstanceKlass::signature_name() const {
2945 return signature_name_of_carrier(JVM_SIGNATURE_CLASS);
2946 }
2947
2948 const char* InstanceKlass::signature_name_of_carrier(char c) const {
2949 // Get the internal name as a c string
2950 const char* src = (const char*) (name()->as_C_string());
2951 const int src_length = (int)strlen(src);
2952
2953 char* dest = NEW_RESOURCE_ARRAY(char, src_length + 3);
2954
2955 // Add L or Q as type indicator
2956 int dest_index = 0;
2957 dest[dest_index++] = c;
2958
2959 // Add the actual class name
2960 for (int src_index = 0; src_index < src_length; ) {
2961 dest[dest_index++] = src[src_index++];
2962 }
2963
2964 if (is_hidden()) { // Replace the last '+' with a '.'.
2965 for (int index = (int)src_length; index > 0; index--) {
2966 if (dest[index] == '+') {
2967 dest[index] = JVM_SIGNATURE_DOT;
2968 break;
2969 }
2970 }
2971 }
2972
2973 // Add the semicolon and the NULL
2974 dest[dest_index++] = JVM_SIGNATURE_ENDCLASS;
2975 dest[dest_index] = '\0';
2976 return dest;
2977 }
3279 jint InstanceKlass::compute_modifier_flags() const {
3280 jint access = access_flags().as_int();
3281
3282 // But check if it happens to be member class.
3283 InnerClassesIterator iter(this);
3284 for (; !iter.done(); iter.next()) {
3285 int ioff = iter.inner_class_info_index();
3286 // Inner class attribute can be zero, skip it.
3287 // Strange but true: JVM spec. allows null inner class refs.
3288 if (ioff == 0) continue;
3289
3290 // only look at classes that are already loaded
3291 // since we are looking for the flags for our self.
3292 Symbol* inner_name = constants()->klass_name_at(ioff);
3293 if (name() == inner_name) {
3294 // This is really a member class.
3295 access = iter.inner_access_flags();
3296 break;
3297 }
3298 }
3299 return (access & JVM_ACC_WRITTEN_FLAGS);
3300 }
3301
3302 jint InstanceKlass::jvmti_class_status() const {
3303 jint result = 0;
3304
3305 if (is_linked()) {
3306 result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3307 }
3308
3309 if (is_initialized()) {
3310 assert(is_linked(), "Class status is not consistent");
3311 result |= JVMTI_CLASS_STATUS_INITIALIZED;
3312 }
3313 if (is_in_error_state()) {
3314 result |= JVMTI_CLASS_STATUS_ERROR;
3315 }
3316 return result;
3317 }
3318
3319 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {
3536 }
3537 osr = osr->osr_link();
3538 }
3539
3540 assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3541 if (best != NULL && best->comp_level() >= comp_level) {
3542 return best;
3543 }
3544 return NULL;
3545 }
3546
3547 // -----------------------------------------------------------------------------------------------------
3548 // Printing
3549
3550 #define BULLET " - "
3551
3552 static const char* state_names[] = {
3553 "allocated", "loaded", "being_linked", "linked", "being_initialized", "fully_initialized", "initialization_error"
3554 };
3555
3556 static void print_vtable(address self, intptr_t* start, int len, outputStream* st) {
3557 ResourceMark rm;
3558 int* forward_refs = NEW_RESOURCE_ARRAY(int, len);
3559 for (int i = 0; i < len; i++) forward_refs[i] = 0;
3560 for (int i = 0; i < len; i++) {
3561 intptr_t e = start[i];
3562 st->print("%d : " INTPTR_FORMAT, i, e);
3563 if (forward_refs[i] != 0) {
3564 int from = forward_refs[i];
3565 int off = (int) start[from];
3566 st->print(" (offset %d <= [%d])", off, from);
3567 }
3568 if (MetaspaceObj::is_valid((Metadata*)e)) {
3569 st->print(" ");
3570 ((Metadata*)e)->print_value_on(st);
3571 } else if (self != NULL && e > 0 && e < 0x10000) {
3572 address location = self + e;
3573 int index = (int)((intptr_t*)location - start);
3574 st->print(" (offset %d => [%d])", (int)e, index);
3575 if (index >= 0 && index < len)
3576 forward_refs[index] = i;
3577 }
3578 st->cr();
3579 }
3580 }
3581
3582 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3583 return print_vtable(NULL, reinterpret_cast<intptr_t*>(start), len, st);
3584 }
3585
3586 template<typename T>
3587 static void print_array_on(outputStream* st, Array<T>* array) {
3588 if (array == NULL) { st->print_cr("NULL"); return; }
3589 array->print_value_on(st); st->cr();
3590 if (Verbose || WizardMode) {
3591 for (int i = 0; i < array->length(); i++) {
3592 st->print("%d : ", i); array->at(i)->print_value_on(st); st->cr();
3593 }
3594 }
3595 }
3596
3597 static void print_array_on(outputStream* st, Array<int>* array) {
3598 if (array == NULL) { st->print_cr("NULL"); return; }
3599 array->print_value_on(st); st->cr();
3600 if (Verbose || WizardMode) {
3601 for (int i = 0; i < array->length(); i++) {
3602 st->print("%d : %d", i, array->at(i)); st->cr();
3603 }
3604 }
3605 }
3606
3607 const char* InstanceKlass::init_state_name() const {
3608 return state_names[_init_state];
3609 }
3610
3611 void InstanceKlass::print_on(outputStream* st) const {
3612 assert(is_klass(), "must be klass");
3613 Klass::print_on(st);
3614
3615 st->print(BULLET"instance size: %d", size_helper()); st->cr();
3616 st->print(BULLET"klass size: %d", size()); st->cr();
3617 st->print(BULLET"access: "); access_flags().print_on(st); st->cr();
3618 st->print(BULLET"misc flags: 0x%x", _misc_status.flags()); st->cr();
3619 st->print(BULLET"state: "); st->print_cr("%s", init_state_name());
3620 st->print(BULLET"name: "); name()->print_value_on(st); st->cr();
3621 st->print(BULLET"super: "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3622 st->print(BULLET"sub: ");
3623 Klass* sub = subklass();
3624 int n;
3625 for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
3626 if (n < MaxSubklassPrintSize) {
3627 sub->print_value_on(st);
3628 st->print(" ");
3629 }
3630 }
3631 if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3632 st->cr();
3633
3634 if (is_interface()) {
3635 st->print_cr(BULLET"nof implementors: %d", nof_implementors());
3636 if (nof_implementors() == 1) {
3637 st->print_cr(BULLET"implementor: ");
3638 st->print(" ");
3639 implementor()->print_value_on(st);
3640 st->cr();
3641 }
3642 }
3643
3644 st->print(BULLET"arrays: "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3645 st->print(BULLET"methods: "); print_array_on(st, methods());
3646 st->print(BULLET"method ordering: "); print_array_on(st, method_ordering());
3647 st->print(BULLET"default_methods: "); print_array_on(st, default_methods());
3648 if (default_vtable_indices() != NULL) {
3649 st->print(BULLET"default vtable indices: "); print_array_on(st, default_vtable_indices());
3650 }
3651 st->print(BULLET"local interfaces: "); print_array_on(st, local_interfaces());
3652 st->print(BULLET"trans. interfaces: "); print_array_on(st, transitive_interfaces());
3653 st->print(BULLET"constants: "); constants()->print_value_on(st); st->cr();
3654 if (class_loader_data() != NULL) {
3655 st->print(BULLET"class loader data: ");
3656 class_loader_data()->print_value_on(st);
3657 st->cr();
3658 }
3659 if (source_file_name() != NULL) {
3660 st->print(BULLET"source file: ");
3661 source_file_name()->print_value_on(st);
3662 st->cr();
3663 }
3664 if (source_debug_extension() != NULL) {
3665 st->print(BULLET"source debug extension: ");
3666 st->print("%s", source_debug_extension());
3667 st->cr();
3668 }
3669 st->print(BULLET"class annotations: "); class_annotations()->print_value_on(st); st->cr();
3670 st->print(BULLET"class type annotations: "); class_type_annotations()->print_value_on(st); st->cr();
3671 st->print(BULLET"field annotations: "); fields_annotations()->print_value_on(st); st->cr();
3672 st->print(BULLET"field type annotations: "); fields_type_annotations()->print_value_on(st); st->cr();
3678 pv_node = pv_node->previous_versions()) {
3679 if (!have_pv)
3680 st->print(BULLET"previous version: ");
3681 have_pv = true;
3682 pv_node->constants()->print_value_on(st);
3683 }
3684 if (have_pv) st->cr();
3685 }
3686
3687 if (generic_signature() != NULL) {
3688 st->print(BULLET"generic signature: ");
3689 generic_signature()->print_value_on(st);
3690 st->cr();
3691 }
3692 st->print(BULLET"inner classes: "); inner_classes()->print_value_on(st); st->cr();
3693 st->print(BULLET"nest members: "); nest_members()->print_value_on(st); st->cr();
3694 if (record_components() != NULL) {
3695 st->print(BULLET"record components: "); record_components()->print_value_on(st); st->cr();
3696 }
3697 st->print(BULLET"permitted subclasses: "); permitted_subclasses()->print_value_on(st); st->cr();
3698 st->print(BULLET"preload classes: "); preload_classes()->print_value_on(st); st->cr();
3699 if (java_mirror() != NULL) {
3700 st->print(BULLET"java mirror: ");
3701 java_mirror()->print_value_on(st);
3702 st->cr();
3703 } else {
3704 st->print_cr(BULLET"java mirror: NULL");
3705 }
3706 st->print(BULLET"vtable length %d (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3707 if (vtable_length() > 0 && (Verbose || WizardMode)) print_vtable(start_of_vtable(), vtable_length(), st);
3708 st->print(BULLET"itable length %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3709 if (itable_length() > 0 && (Verbose || WizardMode)) print_vtable(NULL, start_of_itable(), itable_length(), st);
3710 st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3711 FieldPrinter print_static_field(st);
3712 ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3713 st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3714 FieldPrinter print_nonstatic_field(st);
3715 InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3716 ik->print_nonstatic_fields(&print_nonstatic_field);
3717
3718 st->print(BULLET"non-static oop maps: ");
3719 OopMapBlock* map = start_of_nonstatic_oop_maps();
3720 OopMapBlock* end_map = map + nonstatic_oop_map_count();
3721 while (map < end_map) {
3722 st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3723 map++;
3724 }
3725 st->cr();
3726 }
3727
3728 void InstanceKlass::print_value_on(outputStream* st) const {
3729 assert(is_klass(), "must be klass");
|