< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page

  52 #include "logging/logMessage.hpp"
  53 #include "logging/logStream.hpp"
  54 #include "memory/allocation.inline.hpp"
  55 #include "memory/iterator.inline.hpp"
  56 #include "memory/metadataFactory.hpp"
  57 #include "memory/metaspaceClosure.hpp"
  58 #include "memory/oopFactory.hpp"
  59 #include "memory/resourceArea.hpp"
  60 #include "memory/universe.hpp"
  61 #include "oops/fieldStreams.inline.hpp"
  62 #include "oops/constantPool.hpp"
  63 #include "oops/instanceClassLoaderKlass.hpp"
  64 #include "oops/instanceKlass.inline.hpp"
  65 #include "oops/instanceMirrorKlass.hpp"
  66 #include "oops/instanceOop.hpp"
  67 #include "oops/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/mutexLocker.hpp"
  82 #include "runtime/orderAccess.hpp"
  83 #include "runtime/reflectionUtils.hpp"
  84 #include "runtime/thread.inline.hpp"
  85 #include "services/classLoadingService.hpp"
  86 #include "services/finalizerService.hpp"
  87 #include "services/threadService.hpp"
  88 #include "utilities/dtrace.hpp"
  89 #include "utilities/events.hpp"
  90 #include "utilities/macros.hpp"
  91 #include "utilities/stringUtils.hpp"

 144 
 145 static inline bool is_class_loader(const Symbol* class_name,
 146                                    const ClassFileParser& parser) {
 147   assert(class_name != NULL, "invariant");
 148 
 149   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 150     return true;
 151   }
 152 
 153   if (vmClasses::ClassLoader_klass_loaded()) {
 154     const Klass* const super_klass = parser.super_klass();
 155     if (super_klass != NULL) {
 156       if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
 157         return true;
 158       }
 159     }
 160   }
 161   return false;
 162 }
 163 


 164 // private: called to verify that k is a static member of this nest.
 165 // We know that k is an instance class in the same package and hence the
 166 // same classloader.
 167 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
 168   assert(!is_hidden(), "unexpected hidden class");
 169   if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
 170     if (log_is_enabled(Trace, class, nestmates)) {
 171       ResourceMark rm(current);
 172       log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
 173                                   k->external_name(), this->external_name());
 174     }
 175     return false;
 176   }
 177 
 178   if (log_is_enabled(Trace, class, nestmates)) {
 179     ResourceMark rm(current);
 180     log_trace(class, nestmates)("Checking nest membership of %s in %s",
 181                                 k->external_name(), this->external_name());
 182   }
 183 

 409   log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 410                               this->external_name(),
 411                               access ? "" : "NOT ",
 412                               k->external_name());
 413   return access;
 414 }
 415 
 416 const char* InstanceKlass::nest_host_error() {
 417   if (_nest_host_index == 0) {
 418     return NULL;
 419   } else {
 420     constantPoolHandle cph(Thread::current(), constants());
 421     return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
 422   }
 423 }
 424 
 425 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 426   const int size = InstanceKlass::size(parser.vtable_size(),
 427                                        parser.itable_size(),
 428                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 429                                        parser.is_interface());


 430 
 431   const Symbol* const class_name = parser.class_name();
 432   assert(class_name != NULL, "invariant");
 433   ClassLoaderData* loader_data = parser.loader_data();
 434   assert(loader_data != NULL, "invariant");
 435 
 436   InstanceKlass* ik;
 437 
 438   // Allocation
 439   if (REF_NONE == parser.reference_type()) {
 440     if (class_name == vmSymbols::java_lang_Class()) {
 441       // mirror
 442       ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
 443     }
 444     else if (is_class_loader(class_name, parser)) {
 445       // class loader
 446       ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);



 447     } else {
 448       // normal
 449       ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_kind_other);
 450     }
 451   } else {
 452     // reference
 453     ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
 454   }
 455 
 456   // Check for pending exception before adding to the loader data and incrementing
 457   // class count.  Can get OOM here.
 458   if (HAS_PENDING_EXCEPTION) {
 459     return NULL;
 460   }
 461 







 462   return ik;
 463 }
 464 























 465 
 466 // copy method ordering from resource area to Metaspace
 467 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
 468   if (m != NULL) {
 469     // allocate a new array and copy contents (memcpy?)
 470     _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
 471     for (int i = 0; i < m->length(); i++) {
 472       _method_ordering->at_put(i, m->at(i));
 473     }
 474   } else {
 475     _method_ordering = Universe::the_empty_int_array();
 476   }
 477 }
 478 
 479 // create a new array of vtable_indices for default methods
 480 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 481   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 482   assert(default_vtable_indices() == NULL, "only create once");
 483   set_default_vtable_indices(vtable_indices);
 484   return vtable_indices;
 485 }
 486 
 487 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
 488   Klass(id),
 489   _nest_members(NULL),
 490   _nest_host(NULL),
 491   _permitted_subclasses(NULL),
 492   _record_components(NULL),
 493   _static_field_size(parser.static_field_size()),
 494   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 495   _itable_len(parser.itable_size()),
 496   _nest_host_index(0),
 497   _init_state(allocated),
 498   _reference_type(parser.reference_type()),
 499   _init_thread(NULL)


 500 {
 501   set_vtable_length(parser.vtable_size());
 502   set_kind(kind);
 503   set_access_flags(parser.access_flags());
 504   if (parser.is_hidden()) set_is_hidden();
 505   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 506                                                     false));




 507 
 508   assert(NULL == _methods, "underlying memory not zeroed?");
 509   assert(is_instance_klass(), "is layout incorrect?");
 510   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");




 511 }
 512 
 513 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 514                                        Array<Method*>* methods) {
 515   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 516       !methods->is_shared()) {
 517     for (int i = 0; i < methods->length(); i++) {
 518       Method* method = methods->at(i);
 519       if (method == NULL) continue;  // maybe null if error processing
 520       // Only want to delete methods that are not executing for RedefineClasses.
 521       // The previous version will point to them so they're not totally dangling
 522       assert (!method->on_stack(), "shouldn't be called with methods on stack");
 523       MetadataFactory::free_metadata(loader_data, method);
 524     }
 525     MetadataFactory::free_array<Method*>(loader_data, methods);
 526   }
 527 }
 528 
 529 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
 530                                           const Klass* super_klass,
 531                                           Array<InstanceKlass*>* local_interfaces,
 532                                           Array<InstanceKlass*>* transitive_interfaces) {
 533   // Only deallocate transitive interfaces if not empty, same as super class
 534   // or same as local interfaces.  See code in parseClassFile.
 535   Array<InstanceKlass*>* ti = transitive_interfaces;
 536   if (ti != Universe::the_empty_instance_klass_array() && ti != local_interfaces) {
 537     // check that the interfaces don't come from super class
 538     Array<InstanceKlass*>* sti = (super_klass == NULL) ? NULL :
 539                     InstanceKlass::cast(super_klass)->transitive_interfaces();
 540     if (ti != sti && ti != NULL && !ti->is_shared()) {


 541       MetadataFactory::free_array<InstanceKlass*>(loader_data, ti);
 542     }
 543   }
 544 
 545   // local interfaces can be empty
 546   if (local_interfaces != Universe::the_empty_instance_klass_array() &&
 547       local_interfaces != NULL && !local_interfaces->is_shared()) {


 548     MetadataFactory::free_array<InstanceKlass*>(loader_data, local_interfaces);
 549   }
 550 }
 551 
 552 void InstanceKlass::deallocate_record_components(ClassLoaderData* loader_data,
 553                                                  Array<RecordComponent*>* record_components) {
 554   if (record_components != NULL && !record_components->is_shared()) {
 555     for (int i = 0; i < record_components->length(); i++) {
 556       RecordComponent* record_component = record_components->at(i);
 557       MetadataFactory::free_metadata(loader_data, record_component);
 558     }
 559     MetadataFactory::free_array<RecordComponent*>(loader_data, record_components);
 560   }
 561 }
 562 
 563 // This function deallocates the metadata and C heap pointers that the
 564 // InstanceKlass points to.
 565 void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
 566 
 567   // Orphan the mirror first, CMS thinks it's still live.

 859         vmSymbols::java_lang_IncompatibleClassChangeError(),
 860         "class %s has interface %s as super class",
 861         external_name(),
 862         super_klass->external_name()
 863       );
 864       return false;
 865     }
 866 
 867     InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
 868     ik_super->link_class_impl(CHECK_false);
 869   }
 870 
 871   // link all interfaces implemented by this class before linking this class
 872   Array<InstanceKlass*>* interfaces = local_interfaces();
 873   int num_interfaces = interfaces->length();
 874   for (int index = 0; index < num_interfaces; index++) {
 875     InstanceKlass* interk = interfaces->at(index);
 876     interk->link_class_impl(CHECK_false);
 877   }
 878 
























































 879   // in case the class is linked in the process of linking its superclasses
 880   if (is_linked()) {
 881     return true;
 882   }
 883 
 884   // trace only the link time for this klass that includes
 885   // the verification time
 886   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 887                              ClassLoader::perf_class_link_selftime(),
 888                              ClassLoader::perf_classes_linked(),
 889                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 890                              jt->get_thread_stat()->perf_timers_addr(),
 891                              PerfClassTraceTime::CLASS_LINK);
 892 
 893   // verification & rewriting
 894   {
 895     HandleMark hm(THREAD);
 896     Handle h_init_lock(THREAD, init_lock());
 897     ObjectLocker ol(h_init_lock, jt);
 898     // rewritten will have been set if loader constraint error found

1111     if (is_in_error_state()) {
1112       DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1113       ResourceMark rm(THREAD);
1114       Handle cause(THREAD, get_initialization_error(THREAD));
1115 
1116       stringStream ss;
1117       ss.print("Could not initialize class %s", external_name());
1118       if (cause.is_null()) {
1119         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1120       } else {
1121         THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1122                         ss.as_string(), cause);
1123       }
1124     }
1125 
1126     // Step 6
1127     set_init_state(being_initialized);
1128     set_init_thread(jt);
1129   }
1130 



















1131   // Step 7
1132   // Next, if C is a class rather than an interface, initialize it's super class and super
1133   // interfaces.
1134   if (!is_interface()) {
1135     Klass* super_klass = super();
1136     if (super_klass != NULL && super_klass->should_be_initialized()) {
1137       super_klass->initialize(THREAD);
1138     }
1139     // If C implements any interface that declares a non-static, concrete method,
1140     // the initialization of C triggers initialization of its super interfaces.
1141     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1142     // having a superinterface that declares, non-static, concrete methods
1143     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1144       initialize_super_interfaces(THREAD);
1145     }
1146 
1147     // If any exceptions, complete abruptly, throwing the same exception as above.
1148     if (HAS_PENDING_EXCEPTION) {
1149       Handle e(THREAD, PENDING_EXCEPTION);
1150       CLEAR_PENDING_EXCEPTION;
1151       {
1152         EXCEPTION_MARK;
1153         add_initialization_error(THREAD, e);
1154         // Locks object, set state, and notify all waiting threads
1155         set_initialization_state_and_notify(initialization_error, THREAD);
1156         CLEAR_PENDING_EXCEPTION;
1157       }
1158       DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1159       THROW_OOP(e());
1160     }
1161   }
1162 
1163 
1164   // Step 8









































1165   {
1166     DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1167     if (class_initializer() != NULL) {
1168       // Timer includes any side effects of class initialization (resolution,
1169       // etc), but not recursive entry into call_class_initializer().
1170       PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1171                                ClassLoader::perf_class_init_selftime(),
1172                                ClassLoader::perf_classes_inited(),
1173                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1174                                jt->get_thread_stat()->perf_timers_addr(),
1175                                PerfClassTraceTime::CLASS_CLINIT);
1176       call_class_initializer(THREAD);
1177     } else {
1178       // The elapsed time is so small it's not worth counting.
1179       if (UsePerfData) {
1180         ClassLoader::perf_classes_inited()->inc();
1181       }
1182       call_class_initializer(THREAD);
1183     }
1184   }
1185 
1186   // Step 9
1187   if (!HAS_PENDING_EXCEPTION) {
1188     set_initialization_state_and_notify(fully_initialized, CHECK);
1189     debug_only(vtable().verify(tty, true);)
1190   }
1191   else {
1192     // Step 10 and 11
1193     Handle e(THREAD, PENDING_EXCEPTION);
1194     CLEAR_PENDING_EXCEPTION;
1195     // JVMTI has already reported the pending exception
1196     // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1197     JvmtiExport::clear_detected_exception(jt);
1198     {
1199       EXCEPTION_MARK;
1200       add_initialization_error(THREAD, e);
1201       set_initialization_state_and_notify(initialization_error, THREAD);
1202       CLEAR_PENDING_EXCEPTION;   // ignore any exception thrown, class initialization error is thrown below
1203       // JVMTI has already reported the pending exception
1204       // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1205       JvmtiExport::clear_detected_exception(jt);
1206     }
1207     DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1208     if (e->is_a(vmClasses::Error_klass())) {
1209       THROW_OOP(e());
1210     } else {
1211       JavaCallArguments args(e);
1212       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),

1437               : vmSymbols::java_lang_InstantiationException(), external_name());
1438   }
1439   if (this == vmClasses::Class_klass()) {
1440     ResourceMark rm(THREAD);
1441     THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1442               : vmSymbols::java_lang_IllegalAccessException(), external_name());
1443   }
1444 }
1445 
1446 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1447   // Need load-acquire for lock-free read
1448   if (array_klasses_acquire() == NULL) {
1449     ResourceMark rm(THREAD);
1450     JavaThread *jt = THREAD;
1451     {
1452       // Atomic creation of array_klasses
1453       MutexLocker ma(THREAD, MultiArray_lock);
1454 
1455       // Check if update has already taken place
1456       if (array_klasses() == NULL) {
1457         ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);

1458         // use 'release' to pair with lock-free load
1459         release_set_array_klasses(k);
1460       }
1461     }
1462   }
1463   // array_klasses() will always be set at this point
1464   ObjArrayKlass* oak = array_klasses();
1465   return oak->array_klass(n, THREAD);
1466 }
1467 
1468 Klass* InstanceKlass::array_klass_or_null(int n) {
1469   // Need load-acquire for lock-free read
1470   ObjArrayKlass* oak = array_klasses_acquire();
1471   if (oak == NULL) {
1472     return NULL;
1473   } else {
1474     return oak->array_klass_or_null(n);
1475   }
1476 }
1477 
1478 Klass* InstanceKlass::array_klass(TRAPS) {
1479   return array_klass(1, THREAD);
1480 }
1481 
1482 Klass* InstanceKlass::array_klass_or_null() {
1483   return array_klass_or_null(1);
1484 }
1485 
1486 static int call_class_initializer_counter = 0;   // for debugging
1487 
1488 Method* InstanceKlass::class_initializer() const {
1489   Method* clinit = find_method(
1490       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1491   if (clinit != NULL && clinit->has_valid_initializer_flags()) {
1492     return clinit;
1493   }
1494   return NULL;
1495 }
1496 
1497 void InstanceKlass::call_class_initializer(TRAPS) {
1498   if (ReplayCompiles &&
1499       (ReplaySuppressInitializers == 1 ||
1500        (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
1501     // Hide the existence of the initializer for the purpose of replaying the compile
1502     return;
1503   }
1504 
1505   methodHandle h_method(THREAD, class_initializer());
1506   assert(!is_initialized(), "we cannot initialize twice");
1507   LogTarget(Info, class, init) lt;
1508   if (lt.is_enabled()) {
1509     ResourceMark rm(THREAD);
1510     LogStream ls(lt);
1511     ls.print("%d Initializing ", call_class_initializer_counter++);
1512     name()->print_value_on(&ls);
1513     ls.print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
1514   }
1515   if (h_method() != NULL) {
1516     JavaCallArguments args; // No arguments
1517     JavaValue result(T_VOID);
1518     JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1519   }
1520 }
1521 
1522 
1523 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1524   InterpreterOopMap* entry_for) {
1525   // Lazily create the _oop_map_cache at first request
1526   // Lock-free access requires load_acquire.
1527   OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1528   if (oop_map_cache == NULL) {
1529     MutexLocker x(OopMapCacheAlloc_lock);
1530     // Check if _oop_map_cache was allocated while we were waiting for this lock
1531     if ((oop_map_cache = _oop_map_cache) == NULL) {
1532       oop_map_cache = new OopMapCache();
1533       // Ensure _oop_map_cache is stable, since it is examined without a lock
1534       Atomic::release_store(&_oop_map_cache, oop_map_cache);
1535     }
1536   }
1537   // _oop_map_cache is constant after init; lookup below does its own locking.
1538   oop_map_cache->lookup(method, bci, entry_for);
1539 }
1540 
1541 bool InstanceKlass::contains_field_offset(int offset) {
1542   fieldDescriptor fd;
1543   return find_field_from_offset(offset, false, &fd);
1544 }
1545 
1546 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1547   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1548     Symbol* f_name = fs.name();
1549     Symbol* f_sig  = fs.signature();
1550     if (f_name == name && f_sig == sig) {
1551       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1552       return true;
1553     }
1554   }
1555   return false;
1556 }
1557 
1558 
1559 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1560   const int n = local_interfaces()->length();
1561   for (int i = 0; i < n; i++) {
1562     Klass* intf1 = local_interfaces()->at(i);
1563     assert(intf1->is_interface(), "just checking type");
1564     // search for field in current interface
1565     if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {

1596 
1597 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1598   // search order according to newest JVM spec (5.4.3.2, p.167).
1599   // 1) search for field in current klass
1600   if (find_local_field(name, sig, fd)) {
1601     if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1602   }
1603   // 2) search for field recursively in direct superinterfaces
1604   if (is_static) {
1605     Klass* intf = find_interface_field(name, sig, fd);
1606     if (intf != NULL) return intf;
1607   }
1608   // 3) apply field lookup recursively if superclass exists
1609   { Klass* supr = super();
1610     if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1611   }
1612   // 4) otherwise field lookup fails
1613   return NULL;
1614 }
1615 









1616 
1617 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1618   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1619     if (fs.offset() == offset) {
1620       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1621       if (fd->is_static() == is_static) return true;
1622     }
1623   }
1624   return false;
1625 }
1626 
1627 
1628 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1629   Klass* klass = const_cast<InstanceKlass*>(this);
1630   while (klass != NULL) {
1631     if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1632       return true;
1633     }
1634     klass = klass->super();
1635   }

1988 }
1989 
1990 // uncached_lookup_method searches both the local class methods array and all
1991 // superclasses methods arrays, skipping any overpass methods in superclasses,
1992 // and possibly skipping private methods.
1993 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
1994                                               const Symbol* signature,
1995                                               OverpassLookupMode overpass_mode,
1996                                               PrivateLookupMode private_mode) const {
1997   OverpassLookupMode overpass_local_mode = overpass_mode;
1998   const Klass* klass = this;
1999   while (klass != NULL) {
2000     Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2001                                                                         signature,
2002                                                                         overpass_local_mode,
2003                                                                         StaticLookupMode::find,
2004                                                                         private_mode);
2005     if (method != NULL) {
2006       return method;
2007     }



2008     klass = klass->super();
2009     overpass_local_mode = OverpassLookupMode::skip;   // Always ignore overpass methods in superclasses
2010   }
2011   return NULL;
2012 }
2013 
2014 #ifdef ASSERT
2015 // search through class hierarchy and return true if this class or
2016 // one of the superclasses was redefined
2017 bool InstanceKlass::has_redefined_this_or_super() const {
2018   const Klass* klass = this;
2019   while (klass != NULL) {
2020     if (InstanceKlass::cast(klass)->has_been_redefined()) {
2021       return true;
2022     }
2023     klass = klass->super();
2024   }
2025   return false;
2026 }
2027 #endif

2422     itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2423     int method_table_offset_in_words = ioe->offset()/wordSize;
2424     int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2425                          / itableOffsetEntry::size();
2426 
2427     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2428       if (ioe->interface_klass() != NULL) {
2429         it->push(ioe->interface_klass_addr());
2430         itableMethodEntry* ime = ioe->first_method_entry(this);
2431         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2432         for (int index = 0; index < n; index ++) {
2433           it->push(ime[index].method_addr());
2434         }
2435       }
2436     }
2437   }
2438 
2439   it->push(&_nest_members);
2440   it->push(&_permitted_subclasses);
2441   it->push(&_record_components);






2442 }
2443 
2444 void InstanceKlass::remove_unshareable_info() {
2445 
2446   if (is_linked()) {
2447     assert(can_be_verified_at_dumptime(), "must be");
2448     // Remember this so we can avoid walking the hierarchy at runtime.
2449     set_verified_at_dump_time();
2450   }
2451 
2452   Klass::remove_unshareable_info();
2453 
2454   if (SystemDictionaryShared::has_class_failed_verification(this)) {
2455     // Classes are attempted to link during dumping and may fail,
2456     // but these classes are still in the dictionary and class list in CLD.
2457     // If the class has failed verification, there is nothing else to remove.
2458     return;
2459   }
2460 
2461   // Reset to the 'allocated' state to prevent any premature accessing to

2464   // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2465   _init_state = allocated;
2466 
2467   { // Otherwise this needs to take out the Compile_lock.
2468     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2469     init_implementor();
2470   }
2471 
2472   constants()->remove_unshareable_info();
2473 
2474   for (int i = 0; i < methods()->length(); i++) {
2475     Method* m = methods()->at(i);
2476     m->remove_unshareable_info();
2477   }
2478 
2479   // do array classes also.
2480   if (array_klasses() != NULL) {
2481     array_klasses()->remove_unshareable_info();
2482   }
2483 








2484   // These are not allocated from metaspace. They are safe to set to NULL.
2485   _source_debug_extension = NULL;
2486   _dep_context = NULL;
2487   _osr_nmethods_head = NULL;
2488 #if INCLUDE_JVMTI
2489   _breakpoints = NULL;
2490   _previous_versions = NULL;
2491   _cached_class_file = NULL;
2492   _jvmti_cached_class_field_map = NULL;
2493 #endif
2494 
2495   _init_thread = NULL;
2496   _methods_jmethod_ids = NULL;
2497   _jni_ids = NULL;
2498   _oop_map_cache = NULL;
2499   // clear _nest_host to ensure re-load at runtime
2500   _nest_host = NULL;
2501   init_shared_package_entry();
2502   _dep_context_last_cleaned = 0;
2503 }

2525     if (is_shared_unregistered_class()) {
2526       _package_entry = NULL;
2527     } else {
2528       _package_entry = PackageEntry::get_archived_entry(_package_entry);
2529     }
2530   }
2531   ArchivePtrMarker::mark_pointer((address**)&_package_entry);
2532 #endif
2533 }
2534 
2535 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2536                                              PackageEntry* pkg_entry, TRAPS) {
2537   // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2538   // before the InstanceKlass is added to the SystemDictionary. Make
2539   // sure the current state is <loaded.
2540   assert(!is_loaded(), "invalid init state");
2541   assert(!shared_loading_failed(), "Must not try to load failed class again");
2542   set_package(loader_data, pkg_entry, CHECK);
2543   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2544 




2545   Array<Method*>* methods = this->methods();
2546   int num_methods = methods->length();
2547   for (int index = 0; index < num_methods; ++index) {
2548     methods->at(index)->restore_unshareable_info(CHECK);
2549   }
2550 #if INCLUDE_JVMTI
2551   if (JvmtiExport::has_redefined_a_class()) {
2552     // Reinitialize vtable because RedefineClasses may have changed some
2553     // entries in this vtable for super classes so the CDS vtable might
2554     // point to old or obsolete entries.  RedefineClasses doesn't fix up
2555     // vtables in the shared system dictionary, only the main one.
2556     // It also redefines the itable too so fix that too.
2557     // First fix any default methods that point to a super class that may
2558     // have been redefined.
2559     bool trace_name_printed = false;
2560     adjust_default_methods(&trace_name_printed);
2561     vtable().initialize_vtable();
2562     itable().initialize_itable();
2563   }
2564 #endif

2728 
2729 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2730   if (array == NULL) {
2731     _source_debug_extension = NULL;
2732   } else {
2733     // Adding one to the attribute length in order to store a null terminator
2734     // character could cause an overflow because the attribute length is
2735     // already coded with an u4 in the classfile, but in practice, it's
2736     // unlikely to happen.
2737     assert((length+1) > length, "Overflow checking");
2738     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2739     for (int i = 0; i < length; i++) {
2740       sde[i] = array[i];
2741     }
2742     sde[length] = '\0';
2743     _source_debug_extension = sde;
2744   }
2745 }
2746 
2747 const char* InstanceKlass::signature_name() const {




2748   int hash_len = 0;
2749   char hash_buf[40];
2750 
2751   // Get the internal name as a c string
2752   const char* src = (const char*) (name()->as_C_string());
2753   const int src_length = (int)strlen(src);
2754 
2755   char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
2756 
2757   // Add L as type indicator
2758   int dest_index = 0;
2759   dest[dest_index++] = JVM_SIGNATURE_CLASS;
2760 
2761   // Add the actual class name
2762   for (int src_index = 0; src_index < src_length; ) {
2763     dest[dest_index++] = src[src_index++];
2764   }
2765 
2766   if (is_hidden()) { // Replace the last '+' with a '.'.
2767     for (int index = (int)src_length; index > 0; index--) {
2768       if (dest[index] == '+') {
2769         dest[index] = JVM_SIGNATURE_DOT;
2770         break;
2771       }
2772     }
2773   }
2774 
2775   // If we have a hash, append it
2776   for (int hash_index = 0; hash_index < hash_len; ) {
2777     dest[dest_index++] = hash_buf[hash_index++];
2778   }
2779 

3345     }
3346     osr = osr->osr_link();
3347   }
3348 
3349   assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3350   if (best != NULL && best->comp_level() >= comp_level) {
3351     return best;
3352   }
3353   return NULL;
3354 }
3355 
3356 // -----------------------------------------------------------------------------------------------------
3357 // Printing
3358 
3359 #define BULLET  " - "
3360 
3361 static const char* state_names[] = {
3362   "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3363 };
3364 
3365 static void print_vtable(intptr_t* start, int len, outputStream* st) {



3366   for (int i = 0; i < len; i++) {
3367     intptr_t e = start[i];
3368     st->print("%d : " INTPTR_FORMAT, i, e);





3369     if (MetaspaceObj::is_valid((Metadata*)e)) {
3370       st->print(" ");
3371       ((Metadata*)e)->print_value_on(st);






3372     }
3373     st->cr();
3374   }
3375 }
3376 
3377 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3378   return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);





















3379 }
3380 
3381 void InstanceKlass::print_on(outputStream* st) const {
3382   assert(is_klass(), "must be klass");
3383   Klass::print_on(st);
3384 
3385   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3386   st->print(BULLET"klass size:        %d", size());                               st->cr();
3387   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();

3388   st->print(BULLET"state:             "); st->print_cr("%s", state_names[_init_state]);
3389   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
3390   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3391   st->print(BULLET"sub:               ");
3392   Klass* sub = subklass();
3393   int n;
3394   for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
3395     if (n < MaxSubklassPrintSize) {
3396       sub->print_value_on(st);
3397       st->print("   ");
3398     }
3399   }
3400   if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3401   st->cr();
3402 
3403   if (is_interface()) {
3404     st->print_cr(BULLET"nof implementors:  %d", nof_implementors());
3405     if (nof_implementors() == 1) {
3406       st->print_cr(BULLET"implementor:    ");
3407       st->print("   ");
3408       implementor()->print_value_on(st);
3409       st->cr();
3410     }
3411   }
3412 
3413   st->print(BULLET"arrays:            "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3414   st->print(BULLET"methods:           "); methods()->print_value_on(st);                  st->cr();
3415   if (Verbose || WizardMode) {
3416     Array<Method*>* method_array = methods();
3417     for (int i = 0; i < method_array->length(); i++) {
3418       st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3419     }
3420   }
3421   st->print(BULLET"method ordering:   "); method_ordering()->print_value_on(st);      st->cr();
3422   st->print(BULLET"default_methods:   "); default_methods()->print_value_on(st);      st->cr();
3423   if (Verbose && default_methods() != NULL) {
3424     Array<Method*>* method_array = default_methods();
3425     for (int i = 0; i < method_array->length(); i++) {
3426       st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3427     }
3428   }
3429   if (default_vtable_indices() != NULL) {
3430     st->print(BULLET"default vtable indices:   "); default_vtable_indices()->print_value_on(st);       st->cr();
3431   }
3432   st->print(BULLET"local interfaces:  "); local_interfaces()->print_value_on(st);      st->cr();
3433   st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3434   st->print(BULLET"constants:         "); constants()->print_value_on(st);         st->cr();
3435   if (class_loader_data() != NULL) {
3436     st->print(BULLET"class loader data:  ");
3437     class_loader_data()->print_value_on(st);
3438     st->cr();
3439   }
3440   if (source_file_name() != NULL) {
3441     st->print(BULLET"source file:       ");
3442     source_file_name()->print_value_on(st);
3443     st->cr();
3444   }
3445   if (source_debug_extension() != NULL) {
3446     st->print(BULLET"source debug extension:       ");
3447     st->print("%s", source_debug_extension());
3448     st->cr();
3449   }
3450   st->print(BULLET"class annotations:       "); class_annotations()->print_value_on(st); st->cr();
3451   st->print(BULLET"class type annotations:  "); class_type_annotations()->print_value_on(st); st->cr();
3452   st->print(BULLET"field annotations:       "); fields_annotations()->print_value_on(st); st->cr();
3453   st->print(BULLET"field type annotations:  "); fields_type_annotations()->print_value_on(st); st->cr();

3469     st->print(BULLET"generic signature: ");
3470     generic_signature()->print_value_on(st);
3471     st->cr();
3472   }
3473   st->print(BULLET"inner classes:     "); inner_classes()->print_value_on(st);     st->cr();
3474   st->print(BULLET"nest members:     "); nest_members()->print_value_on(st);     st->cr();
3475   if (record_components() != NULL) {
3476     st->print(BULLET"record components:     "); record_components()->print_value_on(st);     st->cr();
3477   }
3478   st->print(BULLET"permitted subclasses:     "); permitted_subclasses()->print_value_on(st);     st->cr();
3479   if (java_mirror() != NULL) {
3480     st->print(BULLET"java mirror:       ");
3481     java_mirror()->print_value_on(st);
3482     st->cr();
3483   } else {
3484     st->print_cr(BULLET"java mirror:       NULL");
3485   }
3486   st->print(BULLET"vtable length      %d  (start addr: " INTPTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3487   if (vtable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_vtable(), vtable_length(), st);
3488   st->print(BULLET"itable length      %d (start addr: " INTPTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3489   if (itable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_itable(), itable_length(), st);
3490   st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3491   FieldPrinter print_static_field(st);
3492   ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3493   st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3494   FieldPrinter print_nonstatic_field(st);
3495   InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3496   ik->print_nonstatic_fields(&print_nonstatic_field);
3497 
3498   st->print(BULLET"non-static oop maps: ");
3499   OopMapBlock* map     = start_of_nonstatic_oop_maps();
3500   OopMapBlock* end_map = map + nonstatic_oop_map_count();
3501   while (map < end_map) {
3502     st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3503     map++;
3504   }
3505   st->cr();
3506 }
3507 
3508 void InstanceKlass::print_value_on(outputStream* st) const {
3509   assert(is_klass(), "must be klass");

  52 #include "logging/logMessage.hpp"
  53 #include "logging/logStream.hpp"
  54 #include "memory/allocation.inline.hpp"
  55 #include "memory/iterator.inline.hpp"
  56 #include "memory/metadataFactory.hpp"
  57 #include "memory/metaspaceClosure.hpp"
  58 #include "memory/oopFactory.hpp"
  59 #include "memory/resourceArea.hpp"
  60 #include "memory/universe.hpp"
  61 #include "oops/fieldStreams.inline.hpp"
  62 #include "oops/constantPool.hpp"
  63 #include "oops/instanceClassLoaderKlass.hpp"
  64 #include "oops/instanceKlass.inline.hpp"
  65 #include "oops/instanceMirrorKlass.hpp"
  66 #include "oops/instanceOop.hpp"
  67 #include "oops/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/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"

 145 
 146 static inline bool is_class_loader(const Symbol* class_name,
 147                                    const ClassFileParser& parser) {
 148   assert(class_name != NULL, "invariant");
 149 
 150   if (class_name == vmSymbols::java_lang_ClassLoader()) {
 151     return true;
 152   }
 153 
 154   if (vmClasses::ClassLoader_klass_loaded()) {
 155     const Klass* const super_klass = parser.super_klass();
 156     if (super_klass != NULL) {
 157       if (super_klass->is_subtype_of(vmClasses::ClassLoader_klass())) {
 158         return true;
 159       }
 160     }
 161   }
 162   return false;
 163 }
 164 
 165 bool InstanceKlass::field_is_null_free_inline_type(int index) const { return Signature::basic_type(field(index)->signature(constants())) == T_INLINE_TYPE; }
 166 
 167 // private: called to verify that k is a static member of this nest.
 168 // We know that k is an instance class in the same package and hence the
 169 // same classloader.
 170 bool InstanceKlass::has_nest_member(JavaThread* current, InstanceKlass* k) const {
 171   assert(!is_hidden(), "unexpected hidden class");
 172   if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
 173     if (log_is_enabled(Trace, class, nestmates)) {
 174       ResourceMark rm(current);
 175       log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
 176                                   k->external_name(), this->external_name());
 177     }
 178     return false;
 179   }
 180 
 181   if (log_is_enabled(Trace, class, nestmates)) {
 182     ResourceMark rm(current);
 183     log_trace(class, nestmates)("Checking nest membership of %s in %s",
 184                                 k->external_name(), this->external_name());
 185   }
 186 

 412   log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 413                               this->external_name(),
 414                               access ? "" : "NOT ",
 415                               k->external_name());
 416   return access;
 417 }
 418 
 419 const char* InstanceKlass::nest_host_error() {
 420   if (_nest_host_index == 0) {
 421     return NULL;
 422   } else {
 423     constantPoolHandle cph(Thread::current(), constants());
 424     return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
 425   }
 426 }
 427 
 428 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 429   const int size = InstanceKlass::size(parser.vtable_size(),
 430                                        parser.itable_size(),
 431                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 432                                        parser.is_interface(),
 433                                        parser.has_inline_fields() ? parser.java_fields_count() : 0,
 434                                        parser.is_inline_type());
 435 
 436   const Symbol* const class_name = parser.class_name();
 437   assert(class_name != NULL, "invariant");
 438   ClassLoaderData* loader_data = parser.loader_data();
 439   assert(loader_data != NULL, "invariant");
 440 
 441   InstanceKlass* ik;
 442 
 443   // Allocation
 444   if (REF_NONE == parser.reference_type()) {
 445     if (class_name == vmSymbols::java_lang_Class()) {
 446       // mirror
 447       ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
 448     } else if (is_class_loader(class_name, parser)) {

 449       // class loader
 450       ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
 451     } else if (parser.is_inline_type()) {
 452       // inline type
 453       ik = new (loader_data, size, THREAD) InlineKlass(parser);
 454     } else {
 455       // normal
 456       ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_kind_other);
 457     }
 458   } else {
 459     // reference
 460     ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
 461   }
 462 
 463   // Check for pending exception before adding to the loader data and incrementing
 464   // class count.  Can get OOM here.
 465   if (HAS_PENDING_EXCEPTION) {
 466     return NULL;
 467   }
 468 
 469 #ifdef ASSERT
 470   assert(ik->size() == size, "");
 471   ik->bounds_check((address) ik->start_of_vtable(), false, size);
 472   ik->bounds_check((address) ik->start_of_itable(), false, size);
 473   ik->bounds_check((address) ik->end_of_itable(), true, size);
 474   ik->bounds_check((address) ik->end_of_nonstatic_oop_maps(), true, size);
 475 #endif //ASSERT
 476   return ik;
 477 }
 478 
 479 #ifndef PRODUCT
 480 bool InstanceKlass::bounds_check(address addr, bool edge_ok, intptr_t size_in_bytes) const {
 481   const char* bad = NULL;
 482   address end = NULL;
 483   if (addr < (address)this) {
 484     bad = "before";
 485   } else if (addr == (address)this) {
 486     if (edge_ok)  return true;
 487     bad = "just before";
 488   } else if (addr == (end = (address)this + sizeof(intptr_t) * (size_in_bytes < 0 ? size() : size_in_bytes))) {
 489     if (edge_ok)  return true;
 490     bad = "just after";
 491   } else if (addr > end) {
 492     bad = "after";
 493   } else {
 494     return true;
 495   }
 496   tty->print_cr("%s object bounds: " INTPTR_FORMAT " [" INTPTR_FORMAT ".." INTPTR_FORMAT "]",
 497       bad, (intptr_t)addr, (intptr_t)this, (intptr_t)end);
 498   Verbose = WizardMode = true; this->print(); //@@
 499   return false;
 500 }
 501 #endif //PRODUCT
 502 
 503 // copy method ordering from resource area to Metaspace
 504 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
 505   if (m != NULL) {
 506     // allocate a new array and copy contents (memcpy?)
 507     _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
 508     for (int i = 0; i < m->length(); i++) {
 509       _method_ordering->at_put(i, m->at(i));
 510     }
 511   } else {
 512     _method_ordering = Universe::the_empty_int_array();
 513   }
 514 }
 515 
 516 // create a new array of vtable_indices for default methods
 517 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 518   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 519   assert(default_vtable_indices() == NULL, "only create once");
 520   set_default_vtable_indices(vtable_indices);
 521   return vtable_indices;
 522 }
 523 
 524 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
 525   Klass(id),
 526   _nest_members(NULL),
 527   _nest_host(NULL),
 528   _permitted_subclasses(NULL),
 529   _record_components(NULL),
 530   _static_field_size(parser.static_field_size()),
 531   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 532   _itable_len(parser.itable_size()),
 533   _nest_host_index(0),
 534   _init_state(allocated),
 535   _reference_type(parser.reference_type()),
 536   _init_thread(NULL),
 537   _inline_type_field_klasses(NULL),
 538   _adr_inlineklass_fixed_block(NULL)
 539 {
 540   set_vtable_length(parser.vtable_size());
 541   set_kind(kind);
 542   set_access_flags(parser.access_flags());
 543   if (parser.is_hidden()) set_is_hidden();
 544   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 545                                                     false));
 546     if (parser.has_inline_fields()) {
 547       set_has_inline_type_fields();
 548     }
 549     _java_fields_count = parser.java_fields_count();
 550 
 551   assert(NULL == _methods, "underlying memory not zeroed?");
 552   assert(is_instance_klass(), "is layout incorrect?");
 553   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 554 
 555   if (has_inline_type_fields()) {
 556     _inline_type_field_klasses = (const Klass**) adr_inline_type_field_klasses();
 557   }
 558 }
 559 
 560 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 561                                        Array<Method*>* methods) {
 562   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 563       !methods->is_shared()) {
 564     for (int i = 0; i < methods->length(); i++) {
 565       Method* method = methods->at(i);
 566       if (method == NULL) continue;  // maybe null if error processing
 567       // Only want to delete methods that are not executing for RedefineClasses.
 568       // The previous version will point to them so they're not totally dangling
 569       assert (!method->on_stack(), "shouldn't be called with methods on stack");
 570       MetadataFactory::free_metadata(loader_data, method);
 571     }
 572     MetadataFactory::free_array<Method*>(loader_data, methods);
 573   }
 574 }
 575 
 576 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
 577                                           const Klass* super_klass,
 578                                           Array<InstanceKlass*>* local_interfaces,
 579                                           Array<InstanceKlass*>* transitive_interfaces) {
 580   // Only deallocate transitive interfaces if not empty, same as super class
 581   // or same as local interfaces.  See code in parseClassFile.
 582   Array<InstanceKlass*>* ti = transitive_interfaces;
 583   if (ti != Universe::the_empty_instance_klass_array() && ti != local_interfaces) {
 584     // check that the interfaces don't come from super class
 585     Array<InstanceKlass*>* sti = (super_klass == NULL) ? NULL :
 586                     InstanceKlass::cast(super_klass)->transitive_interfaces();
 587     if (ti != sti && ti != NULL && !ti->is_shared() &&
 588         ti != Universe::the_single_IdentityObject_klass_array() &&
 589         ti != Universe::the_single_ValueObject_klass_array()) {
 590       MetadataFactory::free_array<InstanceKlass*>(loader_data, ti);
 591     }
 592   }
 593 
 594   // local interfaces can be empty
 595   if (local_interfaces != Universe::the_empty_instance_klass_array() &&
 596       local_interfaces != NULL && !local_interfaces->is_shared() &&
 597       local_interfaces != Universe::the_single_IdentityObject_klass_array() &&
 598       local_interfaces != Universe::the_single_ValueObject_klass_array()) {
 599     MetadataFactory::free_array<InstanceKlass*>(loader_data, local_interfaces);
 600   }
 601 }
 602 
 603 void InstanceKlass::deallocate_record_components(ClassLoaderData* loader_data,
 604                                                  Array<RecordComponent*>* record_components) {
 605   if (record_components != NULL && !record_components->is_shared()) {
 606     for (int i = 0; i < record_components->length(); i++) {
 607       RecordComponent* record_component = record_components->at(i);
 608       MetadataFactory::free_metadata(loader_data, record_component);
 609     }
 610     MetadataFactory::free_array<RecordComponent*>(loader_data, record_components);
 611   }
 612 }
 613 
 614 // This function deallocates the metadata and C heap pointers that the
 615 // InstanceKlass points to.
 616 void InstanceKlass::deallocate_contents(ClassLoaderData* loader_data) {
 617 
 618   // Orphan the mirror first, CMS thinks it's still live.

 910         vmSymbols::java_lang_IncompatibleClassChangeError(),
 911         "class %s has interface %s as super class",
 912         external_name(),
 913         super_klass->external_name()
 914       );
 915       return false;
 916     }
 917 
 918     InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
 919     ik_super->link_class_impl(CHECK_false);
 920   }
 921 
 922   // link all interfaces implemented by this class before linking this class
 923   Array<InstanceKlass*>* interfaces = local_interfaces();
 924   int num_interfaces = interfaces->length();
 925   for (int index = 0; index < num_interfaces; index++) {
 926     InstanceKlass* interk = interfaces->at(index);
 927     interk->link_class_impl(CHECK_false);
 928   }
 929 
 930 
 931   // If a class declares a method that uses an inline class as an argument
 932   // type or return inline type, this inline class must be loaded during the
 933   // linking of this class because size and properties of the inline class
 934   // must be known in order to be able to perform inline type optimizations.
 935   // The implementation below is an approximation of this rule, the code
 936   // iterates over all methods of the current class (including overridden
 937   // methods), not only the methods declared by this class. This
 938   // approximation makes the code simpler, and doesn't change the semantic
 939   // because classes declaring methods overridden by the current class are
 940   // linked (and have performed their own pre-loading) before the linking
 941   // of the current class.
 942 
 943 
 944   // Note:
 945   // Inline class types are loaded during
 946   // the loading phase (see ClassFileParser::post_process_parsed_stream()).
 947   // Inline class types used as element types for array creation
 948   // are not pre-loaded. Their loading is triggered by either anewarray
 949   // or multianewarray bytecodes.
 950 
 951   // Could it be possible to do the following processing only if the
 952   // class uses inline types?
 953   if (EnableValhalla) {
 954     ResourceMark rm(THREAD);
 955     for (int i = 0; i < methods()->length(); i++) {
 956       Method* m = methods()->at(i);
 957       for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
 958         if (ss.is_reference()) {
 959           if (ss.is_array()) {
 960             continue;
 961           }
 962           if (ss.type() == T_INLINE_TYPE) {
 963             Symbol* symb = ss.as_symbol();
 964 
 965             oop loader = class_loader();
 966             oop protection_domain = this->protection_domain();
 967             Klass* klass = SystemDictionary::resolve_or_fail(symb,
 968                                                              Handle(THREAD, loader), Handle(THREAD, protection_domain), true,
 969                                                              CHECK_false);
 970             if (klass == NULL) {
 971               THROW_(vmSymbols::java_lang_LinkageError(), false);
 972             }
 973             if (!klass->is_inline_klass()) {
 974               Exceptions::fthrow(
 975                 THREAD_AND_LOCATION,
 976                 vmSymbols::java_lang_IncompatibleClassChangeError(),
 977                 "class %s is not an inline type",
 978                 klass->external_name());
 979             }
 980           }
 981         }
 982       }
 983     }
 984   }
 985 
 986   // in case the class is linked in the process of linking its superclasses
 987   if (is_linked()) {
 988     return true;
 989   }
 990 
 991   // trace only the link time for this klass that includes
 992   // the verification time
 993   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 994                              ClassLoader::perf_class_link_selftime(),
 995                              ClassLoader::perf_classes_linked(),
 996                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 997                              jt->get_thread_stat()->perf_timers_addr(),
 998                              PerfClassTraceTime::CLASS_LINK);
 999 
1000   // verification & rewriting
1001   {
1002     HandleMark hm(THREAD);
1003     Handle h_init_lock(THREAD, init_lock());
1004     ObjectLocker ol(h_init_lock, jt);
1005     // rewritten will have been set if loader constraint error found

1218     if (is_in_error_state()) {
1219       DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1220       ResourceMark rm(THREAD);
1221       Handle cause(THREAD, get_initialization_error(THREAD));
1222 
1223       stringStream ss;
1224       ss.print("Could not initialize class %s", external_name());
1225       if (cause.is_null()) {
1226         THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1227       } else {
1228         THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1229                         ss.as_string(), cause);
1230       }
1231     }
1232 
1233     // Step 6
1234     set_init_state(being_initialized);
1235     set_init_thread(jt);
1236   }
1237 
1238   // Pre-allocating an instance of the default value
1239   if (is_inline_klass()) {
1240       InlineKlass* vk = InlineKlass::cast(this);
1241       oop val = vk->allocate_instance(THREAD);
1242       if (HAS_PENDING_EXCEPTION) {
1243           Handle e(THREAD, PENDING_EXCEPTION);
1244           CLEAR_PENDING_EXCEPTION;
1245           {
1246               EXCEPTION_MARK;
1247               add_initialization_error(THREAD, e);
1248               // Locks object, set state, and notify all waiting threads
1249               set_initialization_state_and_notify(initialization_error, THREAD);
1250               CLEAR_PENDING_EXCEPTION;
1251           }
1252           THROW_OOP(e());
1253       }
1254       vk->set_default_value(val);
1255   }
1256 
1257   // Step 7
1258   // Next, if C is a class rather than an interface, initialize it's super class and super
1259   // interfaces.
1260   if (!is_interface()) {
1261     Klass* super_klass = super();
1262     if (super_klass != NULL && super_klass->should_be_initialized()) {
1263       super_klass->initialize(THREAD);
1264     }
1265     // If C implements any interface that declares a non-static, concrete method,
1266     // the initialization of C triggers initialization of its super interfaces.
1267     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1268     // having a superinterface that declares, non-static, concrete methods
1269     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1270       initialize_super_interfaces(THREAD);
1271     }
1272 
1273     // If any exceptions, complete abruptly, throwing the same exception as above.
1274     if (HAS_PENDING_EXCEPTION) {
1275       Handle e(THREAD, PENDING_EXCEPTION);
1276       CLEAR_PENDING_EXCEPTION;
1277       {
1278         EXCEPTION_MARK;
1279         add_initialization_error(THREAD, e);
1280         // Locks object, set state, and notify all waiting threads
1281         set_initialization_state_and_notify(initialization_error, THREAD);
1282         CLEAR_PENDING_EXCEPTION;
1283       }
1284       DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1285       THROW_OOP(e());
1286     }
1287   }
1288 

1289   // Step 8
1290   // Initialize classes of inline fields
1291   if (EnableValhalla) {
1292     for (AllFieldStream fs(this); !fs.done(); fs.next()) {
1293       if (Signature::basic_type(fs.signature()) == T_INLINE_TYPE) {
1294         Klass* klass = get_inline_type_field_klass_or_null(fs.index());
1295         if (fs.access_flags().is_static() && klass == NULL) {
1296           klass = SystemDictionary::resolve_or_fail(field_signature(fs.index())->fundamental_name(THREAD),
1297               Handle(THREAD, class_loader()),
1298               Handle(THREAD, protection_domain()),
1299               true, THREAD);
1300           set_inline_type_field_klass(fs.index(), klass);
1301         }
1302 
1303         if (!HAS_PENDING_EXCEPTION) {
1304           assert(klass != NULL, "Must  be");
1305           InstanceKlass::cast(klass)->initialize(THREAD);
1306           if (fs.access_flags().is_static()) {
1307             if (java_mirror()->obj_field(fs.offset()) == NULL) {
1308               java_mirror()->obj_field_put(fs.offset(), InlineKlass::cast(klass)->default_value());
1309             }
1310           }
1311         }
1312 
1313         if (HAS_PENDING_EXCEPTION) {
1314           Handle e(THREAD, PENDING_EXCEPTION);
1315           CLEAR_PENDING_EXCEPTION;
1316           {
1317             EXCEPTION_MARK;
1318             add_initialization_error(THREAD, e);
1319             // Locks object, set state, and notify all waiting threads
1320             set_initialization_state_and_notify(initialization_error, THREAD);
1321             CLEAR_PENDING_EXCEPTION;
1322           }
1323           THROW_OOP(e());
1324         }
1325       }
1326     }
1327   }
1328 
1329 
1330   // Step 9
1331   {
1332     DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1333     if (class_initializer() != NULL) {
1334       // Timer includes any side effects of class initialization (resolution,
1335       // etc), but not recursive entry into call_class_initializer().
1336       PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1337                                ClassLoader::perf_class_init_selftime(),
1338                                ClassLoader::perf_classes_inited(),
1339                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1340                                jt->get_thread_stat()->perf_timers_addr(),
1341                                PerfClassTraceTime::CLASS_CLINIT);
1342       call_class_initializer(THREAD);
1343     } else {
1344       // The elapsed time is so small it's not worth counting.
1345       if (UsePerfData) {
1346         ClassLoader::perf_classes_inited()->inc();
1347       }
1348       call_class_initializer(THREAD);
1349     }
1350   }
1351 
1352   // Step 10
1353   if (!HAS_PENDING_EXCEPTION) {
1354     set_initialization_state_and_notify(fully_initialized, CHECK);
1355     debug_only(vtable().verify(tty, true);)
1356   }
1357   else {
1358     // Step 11 and 12
1359     Handle e(THREAD, PENDING_EXCEPTION);
1360     CLEAR_PENDING_EXCEPTION;
1361     // JVMTI has already reported the pending exception
1362     // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1363     JvmtiExport::clear_detected_exception(jt);
1364     {
1365       EXCEPTION_MARK;
1366       add_initialization_error(THREAD, e);
1367       set_initialization_state_and_notify(initialization_error, THREAD);
1368       CLEAR_PENDING_EXCEPTION;   // ignore any exception thrown, class initialization error is thrown below
1369       // JVMTI has already reported the pending exception
1370       // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1371       JvmtiExport::clear_detected_exception(jt);
1372     }
1373     DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1374     if (e->is_a(vmClasses::Error_klass())) {
1375       THROW_OOP(e());
1376     } else {
1377       JavaCallArguments args(e);
1378       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),

1603               : vmSymbols::java_lang_InstantiationException(), external_name());
1604   }
1605   if (this == vmClasses::Class_klass()) {
1606     ResourceMark rm(THREAD);
1607     THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1608               : vmSymbols::java_lang_IllegalAccessException(), external_name());
1609   }
1610 }
1611 
1612 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1613   // Need load-acquire for lock-free read
1614   if (array_klasses_acquire() == NULL) {
1615     ResourceMark rm(THREAD);
1616     JavaThread *jt = THREAD;
1617     {
1618       // Atomic creation of array_klasses
1619       MutexLocker ma(THREAD, MultiArray_lock);
1620 
1621       // Check if update has already taken place
1622       if (array_klasses() == NULL) {
1623         ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this,
1624                                                                   false, false, CHECK_NULL);
1625         // use 'release' to pair with lock-free load
1626         release_set_array_klasses(k);
1627       }
1628     }
1629   }
1630   // array_klasses() will always be set at this point
1631   ArrayKlass* ak = array_klasses();
1632   return ak->array_klass(n, THREAD);
1633 }
1634 
1635 Klass* InstanceKlass::array_klass_or_null(int n) {
1636   // Need load-acquire for lock-free read
1637   ArrayKlass* ak = array_klasses_acquire();
1638   if (ak == NULL) {
1639     return NULL;
1640   } else {
1641     return ak->array_klass_or_null(n);
1642   }
1643 }
1644 
1645 Klass* InstanceKlass::array_klass(TRAPS) {
1646   return array_klass(1, THREAD);
1647 }
1648 
1649 Klass* InstanceKlass::array_klass_or_null() {
1650   return array_klass_or_null(1);
1651 }
1652 
1653 static int call_class_initializer_counter = 0;   // for debugging
1654 
1655 Method* InstanceKlass::class_initializer() const {
1656   Method* clinit = find_method(
1657       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1658   if (clinit != NULL && clinit->is_class_initializer()) {
1659     return clinit;
1660   }
1661   return NULL;
1662 }
1663 
1664 void InstanceKlass::call_class_initializer(TRAPS) {
1665   if (ReplayCompiles &&
1666       (ReplaySuppressInitializers == 1 ||
1667        (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
1668     // Hide the existence of the initializer for the purpose of replaying the compile
1669     return;
1670   }
1671 
1672   methodHandle h_method(THREAD, class_initializer());
1673   assert(!is_initialized(), "we cannot initialize twice");
1674   LogTarget(Info, class, init) lt;
1675   if (lt.is_enabled()) {
1676     ResourceMark rm(THREAD);
1677     LogStream ls(lt);
1678     ls.print("%d Initializing ", call_class_initializer_counter++);
1679     name()->print_value_on(&ls);
1680     ls.print_cr("%s (" INTPTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
1681   }
1682   if (h_method() != NULL) {
1683     JavaCallArguments args; // No arguments
1684     JavaValue result(T_VOID);
1685     JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1686   }
1687 }
1688 
1689 
1690 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1691   InterpreterOopMap* entry_for) {
1692   // Lazily create the _oop_map_cache at first request
1693   // Lock-free access requires load_acquire.
1694   OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1695   if (oop_map_cache == NULL) {
1696     MutexLocker x(OopMapCacheAlloc_lock,  Mutex::_no_safepoint_check_flag);
1697     // Check if _oop_map_cache was allocated while we were waiting for this lock
1698     if ((oop_map_cache = _oop_map_cache) == NULL) {
1699       oop_map_cache = new OopMapCache();
1700       // Ensure _oop_map_cache is stable, since it is examined without a lock
1701       Atomic::release_store(&_oop_map_cache, oop_map_cache);
1702     }
1703   }
1704   // _oop_map_cache is constant after init; lookup below does its own locking.
1705   oop_map_cache->lookup(method, bci, entry_for);
1706 }
1707 





1708 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1709   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1710     Symbol* f_name = fs.name();
1711     Symbol* f_sig  = fs.signature();
1712     if (f_name == name && f_sig == sig) {
1713       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1714       return true;
1715     }
1716   }
1717   return false;
1718 }
1719 
1720 
1721 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1722   const int n = local_interfaces()->length();
1723   for (int i = 0; i < n; i++) {
1724     Klass* intf1 = local_interfaces()->at(i);
1725     assert(intf1->is_interface(), "just checking type");
1726     // search for field in current interface
1727     if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {

1758 
1759 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1760   // search order according to newest JVM spec (5.4.3.2, p.167).
1761   // 1) search for field in current klass
1762   if (find_local_field(name, sig, fd)) {
1763     if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1764   }
1765   // 2) search for field recursively in direct superinterfaces
1766   if (is_static) {
1767     Klass* intf = find_interface_field(name, sig, fd);
1768     if (intf != NULL) return intf;
1769   }
1770   // 3) apply field lookup recursively if superclass exists
1771   { Klass* supr = super();
1772     if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1773   }
1774   // 4) otherwise field lookup fails
1775   return NULL;
1776 }
1777 
1778 bool InstanceKlass::contains_field_offset(int offset) {
1779   if (this->is_inline_klass()) {
1780     InlineKlass* vk = InlineKlass::cast(this);
1781     return offset >= vk->first_field_offset() && offset < (vk->first_field_offset() + vk->get_exact_size_in_bytes());
1782   } else {
1783     fieldDescriptor fd;
1784     return find_field_from_offset(offset, false, &fd);
1785   }
1786 }
1787 
1788 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1789   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1790     if (fs.offset() == offset) {
1791       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1792       if (fd->is_static() == is_static) return true;
1793     }
1794   }
1795   return false;
1796 }
1797 
1798 
1799 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1800   Klass* klass = const_cast<InstanceKlass*>(this);
1801   while (klass != NULL) {
1802     if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1803       return true;
1804     }
1805     klass = klass->super();
1806   }

2159 }
2160 
2161 // uncached_lookup_method searches both the local class methods array and all
2162 // superclasses methods arrays, skipping any overpass methods in superclasses,
2163 // and possibly skipping private methods.
2164 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
2165                                               const Symbol* signature,
2166                                               OverpassLookupMode overpass_mode,
2167                                               PrivateLookupMode private_mode) const {
2168   OverpassLookupMode overpass_local_mode = overpass_mode;
2169   const Klass* klass = this;
2170   while (klass != NULL) {
2171     Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
2172                                                                         signature,
2173                                                                         overpass_local_mode,
2174                                                                         StaticLookupMode::find,
2175                                                                         private_mode);
2176     if (method != NULL) {
2177       return method;
2178     }
2179     if (name == vmSymbols::object_initializer_name()) {
2180       break;  // <init> is never inherited, not even as a static factory
2181     }
2182     klass = klass->super();
2183     overpass_local_mode = OverpassLookupMode::skip;   // Always ignore overpass methods in superclasses
2184   }
2185   return NULL;
2186 }
2187 
2188 #ifdef ASSERT
2189 // search through class hierarchy and return true if this class or
2190 // one of the superclasses was redefined
2191 bool InstanceKlass::has_redefined_this_or_super() const {
2192   const Klass* klass = this;
2193   while (klass != NULL) {
2194     if (InstanceKlass::cast(klass)->has_been_redefined()) {
2195       return true;
2196     }
2197     klass = klass->super();
2198   }
2199   return false;
2200 }
2201 #endif

2596     itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2597     int method_table_offset_in_words = ioe->offset()/wordSize;
2598     int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2599                          / itableOffsetEntry::size();
2600 
2601     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2602       if (ioe->interface_klass() != NULL) {
2603         it->push(ioe->interface_klass_addr());
2604         itableMethodEntry* ime = ioe->first_method_entry(this);
2605         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2606         for (int index = 0; index < n; index ++) {
2607           it->push(ime[index].method_addr());
2608         }
2609       }
2610     }
2611   }
2612 
2613   it->push(&_nest_members);
2614   it->push(&_permitted_subclasses);
2615   it->push(&_record_components);
2616 
2617   if (has_inline_type_fields()) {
2618     for (int i = 0; i < java_fields_count(); i++) {
2619       it->push(&((Klass**)adr_inline_type_field_klasses())[i]);
2620     }
2621   }
2622 }
2623 
2624 void InstanceKlass::remove_unshareable_info() {
2625 
2626   if (is_linked()) {
2627     assert(can_be_verified_at_dumptime(), "must be");
2628     // Remember this so we can avoid walking the hierarchy at runtime.
2629     set_verified_at_dump_time();
2630   }
2631 
2632   Klass::remove_unshareable_info();
2633 
2634   if (SystemDictionaryShared::has_class_failed_verification(this)) {
2635     // Classes are attempted to link during dumping and may fail,
2636     // but these classes are still in the dictionary and class list in CLD.
2637     // If the class has failed verification, there is nothing else to remove.
2638     return;
2639   }
2640 
2641   // Reset to the 'allocated' state to prevent any premature accessing to

2644   // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2645   _init_state = allocated;
2646 
2647   { // Otherwise this needs to take out the Compile_lock.
2648     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2649     init_implementor();
2650   }
2651 
2652   constants()->remove_unshareable_info();
2653 
2654   for (int i = 0; i < methods()->length(); i++) {
2655     Method* m = methods()->at(i);
2656     m->remove_unshareable_info();
2657   }
2658 
2659   // do array classes also.
2660   if (array_klasses() != NULL) {
2661     array_klasses()->remove_unshareable_info();
2662   }
2663 
2664   if (has_inline_type_fields()) {
2665     for (AllFieldStream fs(fields(), constants()); !fs.done(); fs.next()) {
2666       if (Signature::basic_type(fs.signature()) == T_INLINE_TYPE) {
2667         reset_inline_type_field_klass(fs.index());
2668       }
2669     }
2670   }
2671 
2672   // These are not allocated from metaspace. They are safe to set to NULL.
2673   _source_debug_extension = NULL;
2674   _dep_context = NULL;
2675   _osr_nmethods_head = NULL;
2676 #if INCLUDE_JVMTI
2677   _breakpoints = NULL;
2678   _previous_versions = NULL;
2679   _cached_class_file = NULL;
2680   _jvmti_cached_class_field_map = NULL;
2681 #endif
2682 
2683   _init_thread = NULL;
2684   _methods_jmethod_ids = NULL;
2685   _jni_ids = NULL;
2686   _oop_map_cache = NULL;
2687   // clear _nest_host to ensure re-load at runtime
2688   _nest_host = NULL;
2689   init_shared_package_entry();
2690   _dep_context_last_cleaned = 0;
2691 }

2713     if (is_shared_unregistered_class()) {
2714       _package_entry = NULL;
2715     } else {
2716       _package_entry = PackageEntry::get_archived_entry(_package_entry);
2717     }
2718   }
2719   ArchivePtrMarker::mark_pointer((address**)&_package_entry);
2720 #endif
2721 }
2722 
2723 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2724                                              PackageEntry* pkg_entry, TRAPS) {
2725   // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2726   // before the InstanceKlass is added to the SystemDictionary. Make
2727   // sure the current state is <loaded.
2728   assert(!is_loaded(), "invalid init state");
2729   assert(!shared_loading_failed(), "Must not try to load failed class again");
2730   set_package(loader_data, pkg_entry, CHECK);
2731   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2732 
2733   if (is_inline_klass()) {
2734     InlineKlass::cast(this)->initialize_calling_convention(CHECK);
2735   }
2736 
2737   Array<Method*>* methods = this->methods();
2738   int num_methods = methods->length();
2739   for (int index = 0; index < num_methods; ++index) {
2740     methods->at(index)->restore_unshareable_info(CHECK);
2741   }
2742 #if INCLUDE_JVMTI
2743   if (JvmtiExport::has_redefined_a_class()) {
2744     // Reinitialize vtable because RedefineClasses may have changed some
2745     // entries in this vtable for super classes so the CDS vtable might
2746     // point to old or obsolete entries.  RedefineClasses doesn't fix up
2747     // vtables in the shared system dictionary, only the main one.
2748     // It also redefines the itable too so fix that too.
2749     // First fix any default methods that point to a super class that may
2750     // have been redefined.
2751     bool trace_name_printed = false;
2752     adjust_default_methods(&trace_name_printed);
2753     vtable().initialize_vtable();
2754     itable().initialize_itable();
2755   }
2756 #endif

2920 
2921 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2922   if (array == NULL) {
2923     _source_debug_extension = NULL;
2924   } else {
2925     // Adding one to the attribute length in order to store a null terminator
2926     // character could cause an overflow because the attribute length is
2927     // already coded with an u4 in the classfile, but in practice, it's
2928     // unlikely to happen.
2929     assert((length+1) > length, "Overflow checking");
2930     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2931     for (int i = 0; i < length; i++) {
2932       sde[i] = array[i];
2933     }
2934     sde[length] = '\0';
2935     _source_debug_extension = sde;
2936   }
2937 }
2938 
2939 const char* InstanceKlass::signature_name() const {
2940   return signature_name_of_carrier(JVM_SIGNATURE_CLASS);
2941 }
2942 
2943 const char* InstanceKlass::signature_name_of_carrier(char c) const {
2944   int hash_len = 0;
2945   char hash_buf[40];
2946 
2947   // Get the internal name as a c string
2948   const char* src = (const char*) (name()->as_C_string());
2949   const int src_length = (int)strlen(src);
2950 
2951   char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
2952 
2953   // Add L or Q as type indicator
2954   int dest_index = 0;
2955   dest[dest_index++] = c;
2956 
2957   // Add the actual class name
2958   for (int src_index = 0; src_index < src_length; ) {
2959     dest[dest_index++] = src[src_index++];
2960   }
2961 
2962   if (is_hidden()) { // Replace the last '+' with a '.'.
2963     for (int index = (int)src_length; index > 0; index--) {
2964       if (dest[index] == '+') {
2965         dest[index] = JVM_SIGNATURE_DOT;
2966         break;
2967       }
2968     }
2969   }
2970 
2971   // If we have a hash, append it
2972   for (int hash_index = 0; hash_index < hash_len; ) {
2973     dest[dest_index++] = hash_buf[hash_index++];
2974   }
2975 

3541     }
3542     osr = osr->osr_link();
3543   }
3544 
3545   assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3546   if (best != NULL && best->comp_level() >= comp_level) {
3547     return best;
3548   }
3549   return NULL;
3550 }
3551 
3552 // -----------------------------------------------------------------------------------------------------
3553 // Printing
3554 
3555 #define BULLET  " - "
3556 
3557 static const char* state_names[] = {
3558   "allocated", "loaded", "linked", "being_initialized", "fully_initialized", "initialization_error"
3559 };
3560 
3561 static void print_vtable(address self, intptr_t* start, int len, outputStream* st) {
3562   ResourceMark rm;
3563   int* forward_refs = NEW_RESOURCE_ARRAY(int, len);
3564   for (int i = 0; i < len; i++)  forward_refs[i] = 0;
3565   for (int i = 0; i < len; i++) {
3566     intptr_t e = start[i];
3567     st->print("%d : " INTPTR_FORMAT, i, e);
3568     if (forward_refs[i] != 0) {
3569       int from = forward_refs[i];
3570       int off = (int) start[from];
3571       st->print(" (offset %d <= [%d])", off, from);
3572     }
3573     if (MetaspaceObj::is_valid((Metadata*)e)) {
3574       st->print(" ");
3575       ((Metadata*)e)->print_value_on(st);
3576     } else if (self != NULL && e > 0 && e < 0x10000) {
3577       address location = self + e;
3578       int index = (int)((intptr_t*)location - start);
3579       st->print(" (offset %d => [%d])", (int)e, index);
3580       if (index >= 0 && index < len)
3581         forward_refs[index] = i;
3582     }
3583     st->cr();
3584   }
3585 }
3586 
3587 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3588   return print_vtable(NULL, reinterpret_cast<intptr_t*>(start), len, st);
3589 }
3590 
3591 template<typename T>
3592  static void print_array_on(outputStream* st, Array<T>* array) {
3593    if (array == NULL) { st->print_cr("NULL"); return; }
3594    array->print_value_on(st); st->cr();
3595    if (Verbose || WizardMode) {
3596      for (int i = 0; i < array->length(); i++) {
3597        st->print("%d : ", i); array->at(i)->print_value_on(st); st->cr();
3598      }
3599    }
3600  }
3601 
3602 static void print_array_on(outputStream* st, Array<int>* array) {
3603   if (array == NULL) { st->print_cr("NULL"); return; }
3604   array->print_value_on(st); st->cr();
3605   if (Verbose || WizardMode) {
3606     for (int i = 0; i < array->length(); i++) {
3607       st->print("%d : %d", i, array->at(i)); st->cr();
3608     }
3609   }
3610 }
3611 
3612 void InstanceKlass::print_on(outputStream* st) const {
3613   assert(is_klass(), "must be klass");
3614   Klass::print_on(st);
3615 
3616   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3617   st->print(BULLET"klass size:        %d", size());                               st->cr();
3618   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();
3619   st->print(BULLET"misc flags:        0x%x", _misc_flags);                        st->cr();
3620   st->print(BULLET"state:             "); st->print_cr("%s", state_names[_init_state]);
3621   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
3622   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3623   st->print(BULLET"sub:               ");
3624   Klass* sub = subklass();
3625   int n;
3626   for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
3627     if (n < MaxSubklassPrintSize) {
3628       sub->print_value_on(st);
3629       st->print("   ");
3630     }
3631   }
3632   if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3633   st->cr();
3634 
3635   if (is_interface()) {
3636     st->print_cr(BULLET"nof implementors:  %d", nof_implementors());
3637     if (nof_implementors() == 1) {
3638       st->print_cr(BULLET"implementor:    ");
3639       st->print("   ");
3640       implementor()->print_value_on(st);
3641       st->cr();
3642     }
3643   }
3644 
3645   st->print(BULLET"arrays:            "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3646   st->print(BULLET"methods:           "); print_array_on(st, methods());
3647   st->print(BULLET"method ordering:   "); print_array_on(st, method_ordering());
3648   st->print(BULLET"default_methods:   "); print_array_on(st, default_methods());












3649   if (default_vtable_indices() != NULL) {
3650     st->print(BULLET"default vtable indices:   "); print_array_on(st, default_vtable_indices());
3651   }
3652   st->print(BULLET"local interfaces:  "); print_array_on(st, local_interfaces());
3653   st->print(BULLET"trans. interfaces: "); print_array_on(st, transitive_interfaces());
3654   st->print(BULLET"constants:         "); constants()->print_value_on(st);         st->cr();
3655   if (class_loader_data() != NULL) {
3656     st->print(BULLET"class loader data:  ");
3657     class_loader_data()->print_value_on(st);
3658     st->cr();
3659   }
3660   if (source_file_name() != NULL) {
3661     st->print(BULLET"source file:       ");
3662     source_file_name()->print_value_on(st);
3663     st->cr();
3664   }
3665   if (source_debug_extension() != NULL) {
3666     st->print(BULLET"source debug extension:       ");
3667     st->print("%s", source_debug_extension());
3668     st->cr();
3669   }
3670   st->print(BULLET"class annotations:       "); class_annotations()->print_value_on(st); st->cr();
3671   st->print(BULLET"class type annotations:  "); class_type_annotations()->print_value_on(st); st->cr();
3672   st->print(BULLET"field annotations:       "); fields_annotations()->print_value_on(st); st->cr();
3673   st->print(BULLET"field type annotations:  "); fields_type_annotations()->print_value_on(st); st->cr();

3689     st->print(BULLET"generic signature: ");
3690     generic_signature()->print_value_on(st);
3691     st->cr();
3692   }
3693   st->print(BULLET"inner classes:     "); inner_classes()->print_value_on(st);     st->cr();
3694   st->print(BULLET"nest members:     "); nest_members()->print_value_on(st);     st->cr();
3695   if (record_components() != NULL) {
3696     st->print(BULLET"record components:     "); record_components()->print_value_on(st);     st->cr();
3697   }
3698   st->print(BULLET"permitted subclasses:     "); permitted_subclasses()->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: " INTPTR_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: " INTPTR_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");
< prev index next >