< prev index next >

src/hotspot/share/oops/instanceKlass.cpp

Print this page

  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 

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


 438 
 439   const Symbol* const class_name = parser.class_name();
 440   assert(class_name != NULL, "invariant");
 441   ClassLoaderData* loader_data = parser.loader_data();
 442   assert(loader_data != NULL, "invariant");
 443 
 444   InstanceKlass* ik;
 445 
 446   // Allocation
 447   if (parser.is_instance_ref_klass()) {
 448     // java.lang.ref.Reference
 449     ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
 450   } else if (class_name == vmSymbols::java_lang_Class()) {
 451     // mirror - java.lang.Class
 452     ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
 453   } else if (is_stack_chunk_class(class_name, loader_data)) {
 454     // stack chunk
 455     ik = new (loader_data, size, THREAD) InstanceStackChunkKlass(parser);
 456   } else if (is_class_loader(class_name, parser)) {
 457     // class loader - java.lang.ClassLoader
 458     ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);



 459   } else {
 460     // normal
 461     ik = new (loader_data, size, THREAD) InstanceKlass(parser);
 462   }
 463 
 464   // Check for pending exception before adding to the loader data and incrementing
 465   // class count.  Can get OOM here.
 466   if (HAS_PENDING_EXCEPTION) {
 467     return NULL;
 468   }
 469 







 470   return ik;
 471 }
 472 























 473 
 474 // copy method ordering from resource area to Metaspace
 475 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
 476   if (m != NULL) {
 477     // allocate a new array and copy contents (memcpy?)
 478     _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
 479     for (int i = 0; i < m->length(); i++) {
 480       _method_ordering->at_put(i, m->at(i));
 481     }
 482   } else {
 483     _method_ordering = Universe::the_empty_int_array();
 484   }
 485 }
 486 
 487 // create a new array of vtable_indices for default methods
 488 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 489   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 490   assert(default_vtable_indices() == NULL, "only create once");
 491   set_default_vtable_indices(vtable_indices);
 492   return vtable_indices;
 493 }
 494 
 495 static Monitor* create_init_monitor(const char* name) {
 496   return new Monitor(Mutex::safepoint, name);
 497 }
 498 
 499 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
 500   Klass(kind),
 501   _nest_members(NULL),
 502   _nest_host(NULL),
 503   _permitted_subclasses(NULL),
 504   _record_components(NULL),
 505   _static_field_size(parser.static_field_size()),
 506   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 507   _itable_len(parser.itable_size()),
 508   _nest_host_index(0),
 509   _init_state(allocated),
 510   _reference_type(reference_type),
 511   _init_monitor(create_init_monitor("InstanceKlassInitMonitor_lock")),
 512   _init_thread(NULL)



 513 {
 514   set_vtable_length(parser.vtable_size());
 515   set_access_flags(parser.access_flags());
 516   if (parser.is_hidden()) set_is_hidden();
 517   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 518                                                     false));




 519 
 520   assert(NULL == _methods, "underlying memory not zeroed?");
 521   assert(is_instance_klass(), "is layout incorrect?");
 522   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");




 523 }
 524 
 525 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 526                                        Array<Method*>* methods) {
 527   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 528       !methods->is_shared()) {
 529     for (int i = 0; i < methods->length(); i++) {
 530       Method* method = methods->at(i);
 531       if (method == NULL) continue;  // maybe null if error processing
 532       // Only want to delete methods that are not executing for RedefineClasses.
 533       // The previous version will point to them so they're not totally dangling
 534       assert (!method->on_stack(), "shouldn't be called with methods on stack");
 535       MetadataFactory::free_metadata(loader_data, method);
 536     }
 537     MetadataFactory::free_array<Method*>(loader_data, methods);
 538   }
 539 }
 540 
 541 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
 542                                           const Klass* super_klass,

 667       inner_classes() != Universe::the_empty_short_array() &&
 668       !inner_classes()->is_shared()) {
 669     MetadataFactory::free_array<jushort>(loader_data, inner_classes());
 670   }
 671   set_inner_classes(NULL);
 672 
 673   if (nest_members() != NULL &&
 674       nest_members() != Universe::the_empty_short_array() &&
 675       !nest_members()->is_shared()) {
 676     MetadataFactory::free_array<jushort>(loader_data, nest_members());
 677   }
 678   set_nest_members(NULL);
 679 
 680   if (permitted_subclasses() != NULL &&
 681       permitted_subclasses() != Universe::the_empty_short_array() &&
 682       !permitted_subclasses()->is_shared()) {
 683     MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
 684   }
 685   set_permitted_subclasses(NULL);
 686 






 687   // We should deallocate the Annotations instance if it's not in shared spaces.
 688   if (annotations() != NULL && !annotations()->is_shared()) {
 689     MetadataFactory::free_metadata(loader_data, annotations());
 690   }
 691   set_annotations(NULL);
 692 
 693   SystemDictionaryShared::handle_class_unloading(this);
 694 }
 695 
 696 bool InstanceKlass::is_record() const {
 697   return _record_components != NULL &&
 698          is_final() &&
 699          java_super() == vmClasses::Record_klass();
 700 }
 701 
 702 bool InstanceKlass::is_sealed() const {
 703   return _permitted_subclasses != NULL &&
 704          _permitted_subclasses != Universe::the_empty_short_array();
 705 }
 706 

 817         vmSymbols::java_lang_IncompatibleClassChangeError(),
 818         "class %s has interface %s as super class",
 819         external_name(),
 820         super_klass->external_name()
 821       );
 822       return false;
 823     }
 824 
 825     InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
 826     ik_super->link_class_impl(CHECK_false);
 827   }
 828 
 829   // link all interfaces implemented by this class before linking this class
 830   Array<InstanceKlass*>* interfaces = local_interfaces();
 831   int num_interfaces = interfaces->length();
 832   for (int index = 0; index < num_interfaces; index++) {
 833     InstanceKlass* interk = interfaces->at(index);
 834     interk->link_class_impl(CHECK_false);
 835   }
 836 














































































 837   // in case the class is linked in the process of linking its superclasses
 838   if (is_linked()) {
 839     return true;
 840   }
 841 
 842   // trace only the link time for this klass that includes
 843   // the verification time
 844   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 845                              ClassLoader::perf_class_link_selftime(),
 846                              ClassLoader::perf_classes_linked(),
 847                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 848                              jt->get_thread_stat()->perf_timers_addr(),
 849                              PerfClassTraceTime::CLASS_LINK);
 850 
 851   // verification & rewriting
 852   {
 853     LockLinkState init_lock(this, jt);
 854 
 855     // rewritten will have been set if loader constraint error found
 856     // on an earlier link attempt

1061       set_init_thread(jt);
1062     }
1063   }
1064 
1065   // Throw error outside lock
1066   if (throw_error) {
1067     DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1068     ResourceMark rm(THREAD);
1069     Handle cause(THREAD, get_initialization_error(THREAD));
1070 
1071     stringStream ss;
1072     ss.print("Could not initialize class %s", external_name());
1073     if (cause.is_null()) {
1074       THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1075     } else {
1076       THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1077                       ss.as_string(), cause);
1078     }
1079   }
1080 



















1081   // Step 7
1082   // Next, if C is a class rather than an interface, initialize it's super class and super
1083   // interfaces.
1084   if (!is_interface()) {
1085     Klass* super_klass = super();
1086     if (super_klass != NULL && super_klass->should_be_initialized()) {
1087       super_klass->initialize(THREAD);
1088     }
1089     // If C implements any interface that declares a non-static, concrete method,
1090     // the initialization of C triggers initialization of its super interfaces.
1091     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1092     // having a superinterface that declares, non-static, concrete methods
1093     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1094       initialize_super_interfaces(THREAD);
1095     }
1096 
1097     // If any exceptions, complete abruptly, throwing the same exception as above.
1098     if (HAS_PENDING_EXCEPTION) {
1099       Handle e(THREAD, PENDING_EXCEPTION);
1100       CLEAR_PENDING_EXCEPTION;
1101       {
1102         EXCEPTION_MARK;
1103         add_initialization_error(THREAD, e);
1104         // Locks object, set state, and notify all waiting threads
1105         set_initialization_state_and_notify(initialization_error, THREAD);
1106         CLEAR_PENDING_EXCEPTION;
1107       }
1108       DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1109       THROW_OOP(e());
1110     }
1111   }
1112 
1113 
1114   // Step 8









































1115   {
1116     DTRACE_CLASSINIT_PROBE_WAIT(clinit, -1, wait);
1117     if (class_initializer() != NULL) {
1118       // Timer includes any side effects of class initialization (resolution,
1119       // etc), but not recursive entry into call_class_initializer().
1120       PerfClassTraceTime timer(ClassLoader::perf_class_init_time(),
1121                                ClassLoader::perf_class_init_selftime(),
1122                                ClassLoader::perf_classes_inited(),
1123                                jt->get_thread_stat()->perf_recursion_counts_addr(),
1124                                jt->get_thread_stat()->perf_timers_addr(),
1125                                PerfClassTraceTime::CLASS_CLINIT);
1126       call_class_initializer(THREAD);
1127     } else {
1128       // The elapsed time is so small it's not worth counting.
1129       if (UsePerfData) {
1130         ClassLoader::perf_classes_inited()->inc();
1131       }
1132       call_class_initializer(THREAD);
1133     }
1134   }
1135 
1136   // Step 9
1137   if (!HAS_PENDING_EXCEPTION) {
1138     set_initialization_state_and_notify(fully_initialized, THREAD);
1139     debug_only(vtable().verify(tty, true);)
1140   }
1141   else {
1142     // Step 10 and 11
1143     Handle e(THREAD, PENDING_EXCEPTION);
1144     CLEAR_PENDING_EXCEPTION;
1145     // JVMTI has already reported the pending exception
1146     // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1147     JvmtiExport::clear_detected_exception(jt);
1148     {
1149       EXCEPTION_MARK;
1150       add_initialization_error(THREAD, e);
1151       set_initialization_state_and_notify(initialization_error, THREAD);
1152       CLEAR_PENDING_EXCEPTION;   // ignore any exception thrown, class initialization error is thrown below
1153       // JVMTI has already reported the pending exception
1154       // JVMTI internal flag reset is needed in order to report ExceptionInInitializerError
1155       JvmtiExport::clear_detected_exception(jt);
1156     }
1157     DTRACE_CLASSINIT_PROBE_WAIT(error, -1, wait);
1158     if (e->is_a(vmClasses::Error_klass())) {
1159       THROW_OOP(e());
1160     } else {
1161       JavaCallArguments args(e);
1162       THROW_ARG(vmSymbols::java_lang_ExceptionInInitializerError(),

1391               : vmSymbols::java_lang_InstantiationException(), external_name());
1392   }
1393   if (this == vmClasses::Class_klass()) {
1394     ResourceMark rm(THREAD);
1395     THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1396               : vmSymbols::java_lang_IllegalAccessException(), external_name());
1397   }
1398 }
1399 
1400 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1401   // Need load-acquire for lock-free read
1402   if (array_klasses_acquire() == NULL) {
1403     ResourceMark rm(THREAD);
1404     JavaThread *jt = THREAD;
1405     {
1406       // Atomic creation of array_klasses
1407       MutexLocker ma(THREAD, MultiArray_lock);
1408 
1409       // Check if update has already taken place
1410       if (array_klasses() == NULL) {
1411         ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this, CHECK_NULL);

1412         // use 'release' to pair with lock-free load
1413         release_set_array_klasses(k);
1414       }
1415     }
1416   }
1417   // array_klasses() will always be set at this point
1418   ObjArrayKlass* oak = array_klasses();
1419   return oak->array_klass(n, THREAD);
1420 }
1421 
1422 Klass* InstanceKlass::array_klass_or_null(int n) {
1423   // Need load-acquire for lock-free read
1424   ObjArrayKlass* oak = array_klasses_acquire();
1425   if (oak == NULL) {
1426     return NULL;
1427   } else {
1428     return oak->array_klass_or_null(n);
1429   }
1430 }
1431 
1432 Klass* InstanceKlass::array_klass(TRAPS) {
1433   return array_klass(1, THREAD);
1434 }
1435 
1436 Klass* InstanceKlass::array_klass_or_null() {
1437   return array_klass_or_null(1);
1438 }
1439 
1440 static int call_class_initializer_counter = 0;   // for debugging
1441 
1442 Method* InstanceKlass::class_initializer() const {
1443   Method* clinit = find_method(
1444       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1445   if (clinit != NULL && clinit->has_valid_initializer_flags()) {
1446     return clinit;
1447   }
1448   return NULL;
1449 }
1450 
1451 void InstanceKlass::call_class_initializer(TRAPS) {
1452   if (ReplayCompiles &&
1453       (ReplaySuppressInitializers == 1 ||
1454        (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
1455     // Hide the existence of the initializer for the purpose of replaying the compile
1456     return;
1457   }
1458 
1459 #if INCLUDE_CDS
1460   // This is needed to ensure the consistency of the archived heap objects.
1461   if (has_archived_enum_objs()) {
1462     assert(is_shared(), "must be");
1463     bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
1464     if (initialized) {
1465       return;

1474     ResourceMark rm(THREAD);
1475     LogStream ls(lt);
1476     ls.print("%d Initializing ", call_class_initializer_counter++);
1477     name()->print_value_on(&ls);
1478     ls.print_cr("%s (" PTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
1479   }
1480   if (h_method() != NULL) {
1481     JavaCallArguments args; // No arguments
1482     JavaValue result(T_VOID);
1483     JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1484   }
1485 }
1486 
1487 
1488 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1489   InterpreterOopMap* entry_for) {
1490   // Lazily create the _oop_map_cache at first request
1491   // Lock-free access requires load_acquire.
1492   OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1493   if (oop_map_cache == NULL) {
1494     MutexLocker x(OopMapCacheAlloc_lock);
1495     // Check if _oop_map_cache was allocated while we were waiting for this lock
1496     if ((oop_map_cache = _oop_map_cache) == NULL) {
1497       oop_map_cache = new OopMapCache();
1498       // Ensure _oop_map_cache is stable, since it is examined without a lock
1499       Atomic::release_store(&_oop_map_cache, oop_map_cache);
1500     }
1501   }
1502   // _oop_map_cache is constant after init; lookup below does its own locking.
1503   oop_map_cache->lookup(method, bci, entry_for);
1504 }
1505 
1506 bool InstanceKlass::contains_field_offset(int offset) {
1507   fieldDescriptor fd;
1508   return find_field_from_offset(offset, false, &fd);
1509 }
1510 
1511 bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1512   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1513     Symbol* f_name = fs.name();
1514     Symbol* f_sig  = fs.signature();
1515     if (f_name == name && f_sig == sig) {
1516       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1517       return true;
1518     }
1519   }
1520   return false;
1521 }
1522 
1523 
1524 Klass* InstanceKlass::find_interface_field(Symbol* name, Symbol* sig, fieldDescriptor* fd) const {
1525   const int n = local_interfaces()->length();
1526   for (int i = 0; i < n; i++) {
1527     Klass* intf1 = local_interfaces()->at(i);
1528     assert(intf1->is_interface(), "just checking type");
1529     // search for field in current interface
1530     if (InstanceKlass::cast(intf1)->find_local_field(name, sig, fd)) {

1561 
1562 Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fieldDescriptor* fd) const {
1563   // search order according to newest JVM spec (5.4.3.2, p.167).
1564   // 1) search for field in current klass
1565   if (find_local_field(name, sig, fd)) {
1566     if (fd->is_static() == is_static) return const_cast<InstanceKlass*>(this);
1567   }
1568   // 2) search for field recursively in direct superinterfaces
1569   if (is_static) {
1570     Klass* intf = find_interface_field(name, sig, fd);
1571     if (intf != NULL) return intf;
1572   }
1573   // 3) apply field lookup recursively if superclass exists
1574   { Klass* supr = super();
1575     if (supr != NULL) return InstanceKlass::cast(supr)->find_field(name, sig, is_static, fd);
1576   }
1577   // 4) otherwise field lookup fails
1578   return NULL;
1579 }
1580 









1581 
1582 bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1583   for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1584     if (fs.offset() == offset) {
1585       fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1586       if (fd->is_static() == is_static) return true;
1587     }
1588   }
1589   return false;
1590 }
1591 
1592 
1593 bool InstanceKlass::find_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
1594   Klass* klass = const_cast<InstanceKlass*>(this);
1595   while (klass != NULL) {
1596     if (InstanceKlass::cast(klass)->find_local_field_from_offset(offset, is_static, fd)) {
1597       return true;
1598     }
1599     klass = klass->super();
1600   }

1953 }
1954 
1955 // uncached_lookup_method searches both the local class methods array and all
1956 // superclasses methods arrays, skipping any overpass methods in superclasses,
1957 // and possibly skipping private methods.
1958 Method* InstanceKlass::uncached_lookup_method(const Symbol* name,
1959                                               const Symbol* signature,
1960                                               OverpassLookupMode overpass_mode,
1961                                               PrivateLookupMode private_mode) const {
1962   OverpassLookupMode overpass_local_mode = overpass_mode;
1963   const Klass* klass = this;
1964   while (klass != NULL) {
1965     Method* const method = InstanceKlass::cast(klass)->find_method_impl(name,
1966                                                                         signature,
1967                                                                         overpass_local_mode,
1968                                                                         StaticLookupMode::find,
1969                                                                         private_mode);
1970     if (method != NULL) {
1971       return method;
1972     }




1973     klass = klass->super();
1974     overpass_local_mode = OverpassLookupMode::skip;   // Always ignore overpass methods in superclasses
1975   }
1976   return NULL;
1977 }
1978 
1979 #ifdef ASSERT
1980 // search through class hierarchy and return true if this class or
1981 // one of the superclasses was redefined
1982 bool InstanceKlass::has_redefined_this_or_super() const {
1983   const Klass* klass = this;
1984   while (klass != NULL) {
1985     if (InstanceKlass::cast(klass)->has_been_redefined()) {
1986       return true;
1987     }
1988     klass = klass->super();
1989   }
1990   return false;
1991 }
1992 #endif

2430   if (itable_length() > 0) {
2431     itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2432     int method_table_offset_in_words = ioe->offset()/wordSize;
2433     int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2434                          / itableOffsetEntry::size();
2435 
2436     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2437       if (ioe->interface_klass() != NULL) {
2438         it->push(ioe->interface_klass_addr());
2439         itableMethodEntry* ime = ioe->first_method_entry(this);
2440         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2441         for (int index = 0; index < n; index ++) {
2442           it->push(ime[index].method_addr());
2443         }
2444       }
2445     }
2446   }
2447 
2448   it->push(&_nest_members);
2449   it->push(&_permitted_subclasses);

2450   it->push(&_record_components);






2451 }
2452 
2453 #if INCLUDE_CDS
2454 void InstanceKlass::remove_unshareable_info() {
2455 
2456   if (is_linked()) {
2457     assert(can_be_verified_at_dumptime(), "must be");
2458     // Remember this so we can avoid walking the hierarchy at runtime.
2459     set_verified_at_dump_time();
2460   }
2461 
2462   Klass::remove_unshareable_info();
2463 
2464   if (SystemDictionaryShared::has_class_failed_verification(this)) {
2465     // Classes are attempted to link during dumping and may fail,
2466     // but these classes are still in the dictionary and class list in CLD.
2467     // If the class has failed verification, there is nothing else to remove.
2468     return;
2469   }
2470 

2474   // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2475   _init_state = allocated;
2476 
2477   { // Otherwise this needs to take out the Compile_lock.
2478     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2479     init_implementor();
2480   }
2481 
2482   constants()->remove_unshareable_info();
2483 
2484   for (int i = 0; i < methods()->length(); i++) {
2485     Method* m = methods()->at(i);
2486     m->remove_unshareable_info();
2487   }
2488 
2489   // do array classes also.
2490   if (array_klasses() != NULL) {
2491     array_klasses()->remove_unshareable_info();
2492   }
2493 








2494   // These are not allocated from metaspace. They are safe to set to NULL.
2495   _source_debug_extension = NULL;
2496   _dep_context = NULL;
2497   _osr_nmethods_head = NULL;
2498 #if INCLUDE_JVMTI
2499   _breakpoints = NULL;
2500   _previous_versions = NULL;
2501   _cached_class_file = NULL;
2502   _jvmti_cached_class_field_map = NULL;
2503 #endif
2504 
2505   _init_thread = NULL;
2506   _methods_jmethod_ids = NULL;
2507   _jni_ids = NULL;
2508   _oop_map_cache = NULL;
2509   // clear _nest_host to ensure re-load at runtime
2510   _nest_host = NULL;
2511   init_shared_package_entry();
2512   _dep_context_last_cleaned = 0;
2513   _init_monitor = NULL;

2536     if (is_shared_unregistered_class()) {
2537       _package_entry = NULL;
2538     } else {
2539       _package_entry = PackageEntry::get_archived_entry(_package_entry);
2540     }
2541   }
2542   ArchivePtrMarker::mark_pointer((address**)&_package_entry);
2543 #endif
2544 }
2545 
2546 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2547                                              PackageEntry* pkg_entry, TRAPS) {
2548   // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2549   // before the InstanceKlass is added to the SystemDictionary. Make
2550   // sure the current state is <loaded.
2551   assert(!is_loaded(), "invalid init state");
2552   assert(!shared_loading_failed(), "Must not try to load failed class again");
2553   set_package(loader_data, pkg_entry, CHECK);
2554   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2555 




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

2746 
2747 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2748   if (array == NULL) {
2749     _source_debug_extension = NULL;
2750   } else {
2751     // Adding one to the attribute length in order to store a null terminator
2752     // character could cause an overflow because the attribute length is
2753     // already coded with an u4 in the classfile, but in practice, it's
2754     // unlikely to happen.
2755     assert((length+1) > length, "Overflow checking");
2756     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2757     for (int i = 0; i < length; i++) {
2758       sde[i] = array[i];
2759     }
2760     sde[length] = '\0';
2761     _source_debug_extension = sde;
2762   }
2763 }
2764 
2765 const char* InstanceKlass::signature_name() const {




2766   int hash_len = 0;
2767   char hash_buf[40];
2768 
2769   // Get the internal name as a c string
2770   const char* src = (const char*) (name()->as_C_string());
2771   const int src_length = (int)strlen(src);
2772 
2773   char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
2774 
2775   // Add L as type indicator
2776   int dest_index = 0;
2777   dest[dest_index++] = JVM_SIGNATURE_CLASS;
2778 
2779   // Add the actual class name
2780   for (int src_index = 0; src_index < src_length; ) {
2781     dest[dest_index++] = src[src_index++];
2782   }
2783 
2784   if (is_hidden()) { // Replace the last '+' with a '.'.
2785     for (int index = (int)src_length; index > 0; index--) {
2786       if (dest[index] == '+') {
2787         dest[index] = JVM_SIGNATURE_DOT;
2788         break;
2789       }
2790     }
2791   }
2792 
2793   // If we have a hash, append it
2794   for (int hash_index = 0; hash_index < hash_len; ) {
2795     dest[dest_index++] = hash_buf[hash_index++];
2796   }
2797 

3104 jint InstanceKlass::compute_modifier_flags() const {
3105   jint access = access_flags().as_int();
3106 
3107   // But check if it happens to be member class.
3108   InnerClassesIterator iter(this);
3109   for (; !iter.done(); iter.next()) {
3110     int ioff = iter.inner_class_info_index();
3111     // Inner class attribute can be zero, skip it.
3112     // Strange but true:  JVM spec. allows null inner class refs.
3113     if (ioff == 0) continue;
3114 
3115     // only look at classes that are already loaded
3116     // since we are looking for the flags for our self.
3117     Symbol* inner_name = constants()->klass_name_at(ioff);
3118     if (name() == inner_name) {
3119       // This is really a member class.
3120       access = iter.inner_access_flags();
3121       break;
3122     }
3123   }
3124   // Remember to strip ACC_SUPER bit
3125   return (access & (~JVM_ACC_SUPER)) & JVM_ACC_WRITTEN_FLAGS;
3126 }
3127 
3128 jint InstanceKlass::jvmti_class_status() const {
3129   jint result = 0;
3130 
3131   if (is_linked()) {
3132     result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3133   }
3134 
3135   if (is_initialized()) {
3136     assert(is_linked(), "Class status is not consistent");
3137     result |= JVMTI_CLASS_STATUS_INITIALIZED;
3138   }
3139   if (is_in_error_state()) {
3140     result |= JVMTI_CLASS_STATUS_ERROR;
3141   }
3142   return result;
3143 }
3144 
3145 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {

3362     }
3363     osr = osr->osr_link();
3364   }
3365 
3366   assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3367   if (best != NULL && best->comp_level() >= comp_level) {
3368     return best;
3369   }
3370   return NULL;
3371 }
3372 
3373 // -----------------------------------------------------------------------------------------------------
3374 // Printing
3375 
3376 #define BULLET  " - "
3377 
3378 static const char* state_names[] = {
3379   "allocated", "loaded", "being_linked", "linked", "being_initialized", "fully_initialized", "initialization_error"
3380 };
3381 
3382 static void print_vtable(intptr_t* start, int len, outputStream* st) {



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





3386     if (MetaspaceObj::is_valid((Metadata*)e)) {
3387       st->print(" ");
3388       ((Metadata*)e)->print_value_on(st);






3389     }
3390     st->cr();
3391   }
3392 }
3393 
3394 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3395   return print_vtable(reinterpret_cast<intptr_t*>(start), len, st);





















3396 }
3397 
3398 const char* InstanceKlass::init_state_name() const {
3399   return state_names[_init_state];
3400 }
3401 
3402 void InstanceKlass::print_on(outputStream* st) const {
3403   assert(is_klass(), "must be klass");
3404   Klass::print_on(st);
3405 
3406   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3407   st->print(BULLET"klass size:        %d", size());                               st->cr();
3408   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();

3409   st->print(BULLET"state:             "); st->print_cr("%s", init_state_name());
3410   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
3411   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3412   st->print(BULLET"sub:               ");
3413   Klass* sub = subklass();
3414   int n;
3415   for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
3416     if (n < MaxSubklassPrintSize) {
3417       sub->print_value_on(st);
3418       st->print("   ");
3419     }
3420   }
3421   if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3422   st->cr();
3423 
3424   if (is_interface()) {
3425     st->print_cr(BULLET"nof implementors:  %d", nof_implementors());
3426     if (nof_implementors() == 1) {
3427       st->print_cr(BULLET"implementor:    ");
3428       st->print("   ");
3429       implementor()->print_value_on(st);
3430       st->cr();
3431     }
3432   }
3433 
3434   st->print(BULLET"arrays:            "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3435   st->print(BULLET"methods:           "); methods()->print_value_on(st);                  st->cr();
3436   if (Verbose || WizardMode) {
3437     Array<Method*>* method_array = methods();
3438     for (int i = 0; i < method_array->length(); i++) {
3439       st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3440     }
3441   }
3442   st->print(BULLET"method ordering:   "); method_ordering()->print_value_on(st);      st->cr();
3443   st->print(BULLET"default_methods:   "); default_methods()->print_value_on(st);      st->cr();
3444   if (Verbose && default_methods() != NULL) {
3445     Array<Method*>* method_array = default_methods();
3446     for (int i = 0; i < method_array->length(); i++) {
3447       st->print("%d : ", i); method_array->at(i)->print_value(); st->cr();
3448     }
3449   }
3450   if (default_vtable_indices() != NULL) {
3451     st->print(BULLET"default vtable indices:   "); default_vtable_indices()->print_value_on(st);       st->cr();
3452   }
3453   st->print(BULLET"local interfaces:  "); local_interfaces()->print_value_on(st);      st->cr();
3454   st->print(BULLET"trans. interfaces: "); transitive_interfaces()->print_value_on(st); st->cr();
3455   st->print(BULLET"constants:         "); constants()->print_value_on(st);         st->cr();
3456   if (class_loader_data() != NULL) {
3457     st->print(BULLET"class loader data:  ");
3458     class_loader_data()->print_value_on(st);
3459     st->cr();
3460   }
3461   if (source_file_name() != NULL) {
3462     st->print(BULLET"source file:       ");
3463     source_file_name()->print_value_on(st);
3464     st->cr();
3465   }
3466   if (source_debug_extension() != NULL) {
3467     st->print(BULLET"source debug extension:       ");
3468     st->print("%s", source_debug_extension());
3469     st->cr();
3470   }
3471   st->print(BULLET"class annotations:       "); class_annotations()->print_value_on(st); st->cr();
3472   st->print(BULLET"class type annotations:  "); class_type_annotations()->print_value_on(st); st->cr();
3473   st->print(BULLET"field annotations:       "); fields_annotations()->print_value_on(st); st->cr();
3474   st->print(BULLET"field type annotations:  "); fields_type_annotations()->print_value_on(st); st->cr();

3480          pv_node = pv_node->previous_versions()) {
3481       if (!have_pv)
3482         st->print(BULLET"previous version:  ");
3483       have_pv = true;
3484       pv_node->constants()->print_value_on(st);
3485     }
3486     if (have_pv) st->cr();
3487   }
3488 
3489   if (generic_signature() != NULL) {
3490     st->print(BULLET"generic signature: ");
3491     generic_signature()->print_value_on(st);
3492     st->cr();
3493   }
3494   st->print(BULLET"inner classes:     "); inner_classes()->print_value_on(st);     st->cr();
3495   st->print(BULLET"nest members:     "); nest_members()->print_value_on(st);     st->cr();
3496   if (record_components() != NULL) {
3497     st->print(BULLET"record components:     "); record_components()->print_value_on(st);     st->cr();
3498   }
3499   st->print(BULLET"permitted subclasses:     "); permitted_subclasses()->print_value_on(st);     st->cr();

3500   if (java_mirror() != NULL) {
3501     st->print(BULLET"java mirror:       ");
3502     java_mirror()->print_value_on(st);
3503     st->cr();
3504   } else {
3505     st->print_cr(BULLET"java mirror:       NULL");
3506   }
3507   st->print(BULLET"vtable length      %d  (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3508   if (vtable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_vtable(), vtable_length(), st);
3509   st->print(BULLET"itable length      %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3510   if (itable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_itable(), itable_length(), st);
3511   st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3512   FieldPrinter print_static_field(st);
3513   ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3514   st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3515   FieldPrinter print_nonstatic_field(st);
3516   InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3517   ik->print_nonstatic_fields(&print_nonstatic_field);
3518 
3519   st->print(BULLET"non-static oop maps: ");
3520   OopMapBlock* map     = start_of_nonstatic_oop_maps();
3521   OopMapBlock* end_map = map + nonstatic_oop_map_count();
3522   while (map < end_map) {
3523     st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3524     map++;
3525   }
3526   st->cr();
3527 }
3528 
3529 void InstanceKlass::print_value_on(outputStream* st) const {
3530   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 

 420   log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
 421                               this->external_name(),
 422                               access ? "" : "NOT ",
 423                               k->external_name());
 424   return access;
 425 }
 426 
 427 const char* InstanceKlass::nest_host_error() {
 428   if (_nest_host_index == 0) {
 429     return NULL;
 430   } else {
 431     constantPoolHandle cph(Thread::current(), constants());
 432     return SystemDictionary::find_nest_host_error(cph, (int)_nest_host_index);
 433   }
 434 }
 435 
 436 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
 437   const int size = InstanceKlass::size(parser.vtable_size(),
 438                                        parser.itable_size(),
 439                                        nonstatic_oop_map_size(parser.total_oop_map_count()),
 440                                        parser.is_interface(),
 441                                        parser.has_inline_fields() ? parser.java_fields_count() : 0,
 442                                        parser.is_inline_type());
 443 
 444   const Symbol* const class_name = parser.class_name();
 445   assert(class_name != NULL, "invariant");
 446   ClassLoaderData* loader_data = parser.loader_data();
 447   assert(loader_data != NULL, "invariant");
 448 
 449   InstanceKlass* ik;
 450 
 451   // Allocation
 452   if (parser.is_instance_ref_klass()) {
 453     // java.lang.ref.Reference
 454     ik = new (loader_data, size, THREAD) InstanceRefKlass(parser);
 455   } else if (class_name == vmSymbols::java_lang_Class()) {
 456     // mirror - java.lang.Class
 457     ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
 458   } else if (is_stack_chunk_class(class_name, loader_data)) {
 459     // stack chunk
 460     ik = new (loader_data, size, THREAD) InstanceStackChunkKlass(parser);
 461   } else if (is_class_loader(class_name, parser)) {
 462     // class loader - java.lang.ClassLoader
 463     ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
 464   } else if (parser.is_inline_type()) {
 465     // inline type
 466     ik = new (loader_data, size, THREAD) InlineKlass(parser);
 467   } else {
 468     // normal
 469     ik = new (loader_data, size, THREAD) InstanceKlass(parser);
 470   }
 471 
 472   // Check for pending exception before adding to the loader data and incrementing
 473   // class count.  Can get OOM here.
 474   if (HAS_PENDING_EXCEPTION) {
 475     return NULL;
 476   }
 477 
 478 #ifdef ASSERT
 479   assert(ik->size() == size, "");
 480   ik->bounds_check((address) ik->start_of_vtable(), false, size);
 481   ik->bounds_check((address) ik->start_of_itable(), false, size);
 482   ik->bounds_check((address) ik->end_of_itable(), true, size);
 483   ik->bounds_check((address) ik->end_of_nonstatic_oop_maps(), true, size);
 484 #endif //ASSERT
 485   return ik;
 486 }
 487 
 488 #ifndef PRODUCT
 489 bool InstanceKlass::bounds_check(address addr, bool edge_ok, intptr_t size_in_bytes) const {
 490   const char* bad = NULL;
 491   address end = NULL;
 492   if (addr < (address)this) {
 493     bad = "before";
 494   } else if (addr == (address)this) {
 495     if (edge_ok)  return true;
 496     bad = "just before";
 497   } else if (addr == (end = (address)this + sizeof(intptr_t) * (size_in_bytes < 0 ? size() : size_in_bytes))) {
 498     if (edge_ok)  return true;
 499     bad = "just after";
 500   } else if (addr > end) {
 501     bad = "after";
 502   } else {
 503     return true;
 504   }
 505   tty->print_cr("%s object bounds: " INTPTR_FORMAT " [" INTPTR_FORMAT ".." INTPTR_FORMAT "]",
 506       bad, (intptr_t)addr, (intptr_t)this, (intptr_t)end);
 507   Verbose = WizardMode = true; this->print(); //@@
 508   return false;
 509 }
 510 #endif //PRODUCT
 511 
 512 // copy method ordering from resource area to Metaspace
 513 void InstanceKlass::copy_method_ordering(const intArray* m, TRAPS) {
 514   if (m != NULL) {
 515     // allocate a new array and copy contents (memcpy?)
 516     _method_ordering = MetadataFactory::new_array<int>(class_loader_data(), m->length(), CHECK);
 517     for (int i = 0; i < m->length(); i++) {
 518       _method_ordering->at_put(i, m->at(i));
 519     }
 520   } else {
 521     _method_ordering = Universe::the_empty_int_array();
 522   }
 523 }
 524 
 525 // create a new array of vtable_indices for default methods
 526 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
 527   Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
 528   assert(default_vtable_indices() == NULL, "only create once");
 529   set_default_vtable_indices(vtable_indices);
 530   return vtable_indices;
 531 }
 532 
 533 static Monitor* create_init_monitor(const char* name) {
 534   return new Monitor(Mutex::safepoint, name);
 535 }
 536 
 537 InstanceKlass::InstanceKlass(const ClassFileParser& parser, KlassKind kind, ReferenceType reference_type) :
 538   Klass(kind),
 539   _nest_members(NULL),
 540   _nest_host(NULL),
 541   _permitted_subclasses(NULL),
 542   _record_components(NULL),
 543   _static_field_size(parser.static_field_size()),
 544   _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
 545   _itable_len(parser.itable_size()),
 546   _nest_host_index(0),
 547   _init_state(allocated),
 548   _reference_type(reference_type),
 549   _init_monitor(create_init_monitor("InstanceKlassInitMonitor_lock")),
 550   _init_thread(NULL),
 551   _inline_type_field_klasses(NULL),
 552   _preload_classes(NULL),
 553   _adr_inlineklass_fixed_block(NULL)
 554 {
 555   set_vtable_length(parser.vtable_size());
 556   set_access_flags(parser.access_flags());
 557   if (parser.is_hidden()) set_is_hidden();
 558   set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
 559                                                     false));
 560     if (parser.has_inline_fields()) {
 561       set_has_inline_type_fields();
 562     }
 563     _java_fields_count = parser.java_fields_count();
 564 
 565   assert(NULL == _methods, "underlying memory not zeroed?");
 566   assert(is_instance_klass(), "is layout incorrect?");
 567   assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
 568 
 569   if (has_inline_type_fields()) {
 570     _inline_type_field_klasses = (const Klass**) adr_inline_type_field_klasses();
 571   }
 572 }
 573 
 574 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
 575                                        Array<Method*>* methods) {
 576   if (methods != NULL && methods != Universe::the_empty_method_array() &&
 577       !methods->is_shared()) {
 578     for (int i = 0; i < methods->length(); i++) {
 579       Method* method = methods->at(i);
 580       if (method == NULL) continue;  // maybe null if error processing
 581       // Only want to delete methods that are not executing for RedefineClasses.
 582       // The previous version will point to them so they're not totally dangling
 583       assert (!method->on_stack(), "shouldn't be called with methods on stack");
 584       MetadataFactory::free_metadata(loader_data, method);
 585     }
 586     MetadataFactory::free_array<Method*>(loader_data, methods);
 587   }
 588 }
 589 
 590 void InstanceKlass::deallocate_interfaces(ClassLoaderData* loader_data,
 591                                           const Klass* super_klass,

 716       inner_classes() != Universe::the_empty_short_array() &&
 717       !inner_classes()->is_shared()) {
 718     MetadataFactory::free_array<jushort>(loader_data, inner_classes());
 719   }
 720   set_inner_classes(NULL);
 721 
 722   if (nest_members() != NULL &&
 723       nest_members() != Universe::the_empty_short_array() &&
 724       !nest_members()->is_shared()) {
 725     MetadataFactory::free_array<jushort>(loader_data, nest_members());
 726   }
 727   set_nest_members(NULL);
 728 
 729   if (permitted_subclasses() != NULL &&
 730       permitted_subclasses() != Universe::the_empty_short_array() &&
 731       !permitted_subclasses()->is_shared()) {
 732     MetadataFactory::free_array<jushort>(loader_data, permitted_subclasses());
 733   }
 734   set_permitted_subclasses(NULL);
 735 
 736   if (preload_classes() != NULL &&
 737       preload_classes() != Universe::the_empty_short_array() &&
 738       !preload_classes()->is_shared()) {
 739     MetadataFactory::free_array<jushort>(loader_data, preload_classes());
 740   }
 741 
 742   // We should deallocate the Annotations instance if it's not in shared spaces.
 743   if (annotations() != NULL && !annotations()->is_shared()) {
 744     MetadataFactory::free_metadata(loader_data, annotations());
 745   }
 746   set_annotations(NULL);
 747 
 748   SystemDictionaryShared::handle_class_unloading(this);
 749 }
 750 
 751 bool InstanceKlass::is_record() const {
 752   return _record_components != NULL &&
 753          is_final() &&
 754          java_super() == vmClasses::Record_klass();
 755 }
 756 
 757 bool InstanceKlass::is_sealed() const {
 758   return _permitted_subclasses != NULL &&
 759          _permitted_subclasses != Universe::the_empty_short_array();
 760 }
 761 

 872         vmSymbols::java_lang_IncompatibleClassChangeError(),
 873         "class %s has interface %s as super class",
 874         external_name(),
 875         super_klass->external_name()
 876       );
 877       return false;
 878     }
 879 
 880     InstanceKlass* ik_super = InstanceKlass::cast(super_klass);
 881     ik_super->link_class_impl(CHECK_false);
 882   }
 883 
 884   // link all interfaces implemented by this class before linking this class
 885   Array<InstanceKlass*>* interfaces = local_interfaces();
 886   int num_interfaces = interfaces->length();
 887   for (int index = 0; index < num_interfaces; index++) {
 888     InstanceKlass* interk = interfaces->at(index);
 889     interk->link_class_impl(CHECK_false);
 890   }
 891 
 892 
 893   // If a class declares a method that uses an inline class as an argument
 894   // type or return inline type, this inline class must be loaded during the
 895   // linking of this class because size and properties of the inline class
 896   // must be known in order to be able to perform inline type optimizations.
 897   // The implementation below is an approximation of this rule, the code
 898   // iterates over all methods of the current class (including overridden
 899   // methods), not only the methods declared by this class. This
 900   // approximation makes the code simpler, and doesn't change the semantic
 901   // because classes declaring methods overridden by the current class are
 902   // linked (and have performed their own pre-loading) before the linking
 903   // of the current class.
 904 
 905 
 906   // Note:
 907   // Inline class types are loaded during
 908   // the loading phase (see ClassFileParser::post_process_parsed_stream()).
 909   // Inline class types used as element types for array creation
 910   // are not pre-loaded. Their loading is triggered by either anewarray
 911   // or multianewarray bytecodes.
 912 
 913   // Could it be possible to do the following processing only if the
 914   // class uses inline types?
 915   if (EnableValhalla) {
 916     ResourceMark rm(THREAD);
 917     if (EnablePrimitiveClasses) {
 918       for (int i = 0; i < methods()->length(); i++) {
 919         Method* m = methods()->at(i);
 920         for (SignatureStream ss(m->signature()); !ss.is_done(); ss.next()) {
 921           if (ss.is_reference()) {
 922             if (ss.is_array()) {
 923               continue;
 924             }
 925             if (ss.type() == T_PRIMITIVE_OBJECT) {
 926               Symbol* symb = ss.as_symbol();
 927               if (symb == name()) continue;
 928               oop loader = class_loader();
 929               oop protection_domain = this->protection_domain();
 930               Klass* klass = SystemDictionary::resolve_or_fail(symb,
 931                                                               Handle(THREAD, loader), Handle(THREAD, protection_domain), true,
 932                                                               CHECK_false);
 933               if (klass == NULL) {
 934                 THROW_(vmSymbols::java_lang_LinkageError(), false);
 935               }
 936               if (!klass->is_inline_klass()) {
 937                 Exceptions::fthrow(
 938                   THREAD_AND_LOCATION,
 939                   vmSymbols::java_lang_IncompatibleClassChangeError(),
 940                   "class %s is not an inline type",
 941                   klass->external_name());
 942               }
 943             }
 944           }
 945         }
 946       }
 947     }
 948     // Aggressively preloading all classes from the Preload attribute
 949     if (preload_classes() != NULL) {
 950       for (int i = 0; i < preload_classes()->length(); i++) {
 951         if (constants()->tag_at(preload_classes()->at(i)).is_klass()) continue;
 952         Symbol* class_name = constants()->klass_at_noresolve(preload_classes()->at(i));
 953         if (class_name == name()) continue;
 954         oop loader = class_loader();
 955         oop protection_domain = this->protection_domain();
 956         Klass* klass = SystemDictionary::resolve_or_null(class_name,
 957                                                           Handle(THREAD, loader), Handle(THREAD, protection_domain), THREAD);
 958         if (HAS_PENDING_EXCEPTION) {
 959           CLEAR_PENDING_EXCEPTION;
 960         }
 961         if (klass != NULL) {
 962           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());
 963         } else {
 964           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());
 965         }
 966       }
 967     }
 968   }
 969 
 970   // in case the class is linked in the process of linking its superclasses
 971   if (is_linked()) {
 972     return true;
 973   }
 974 
 975   // trace only the link time for this klass that includes
 976   // the verification time
 977   PerfClassTraceTime vmtimer(ClassLoader::perf_class_link_time(),
 978                              ClassLoader::perf_class_link_selftime(),
 979                              ClassLoader::perf_classes_linked(),
 980                              jt->get_thread_stat()->perf_recursion_counts_addr(),
 981                              jt->get_thread_stat()->perf_timers_addr(),
 982                              PerfClassTraceTime::CLASS_LINK);
 983 
 984   // verification & rewriting
 985   {
 986     LockLinkState init_lock(this, jt);
 987 
 988     // rewritten will have been set if loader constraint error found
 989     // on an earlier link attempt

1194       set_init_thread(jt);
1195     }
1196   }
1197 
1198   // Throw error outside lock
1199   if (throw_error) {
1200     DTRACE_CLASSINIT_PROBE_WAIT(erroneous, -1, wait);
1201     ResourceMark rm(THREAD);
1202     Handle cause(THREAD, get_initialization_error(THREAD));
1203 
1204     stringStream ss;
1205     ss.print("Could not initialize class %s", external_name());
1206     if (cause.is_null()) {
1207       THROW_MSG(vmSymbols::java_lang_NoClassDefFoundError(), ss.as_string());
1208     } else {
1209       THROW_MSG_CAUSE(vmSymbols::java_lang_NoClassDefFoundError(),
1210                       ss.as_string(), cause);
1211     }
1212   }
1213 
1214   // Pre-allocating an instance of the default value
1215   if (is_inline_klass()) {
1216       InlineKlass* vk = InlineKlass::cast(this);
1217       oop val = vk->allocate_instance(THREAD);
1218       if (HAS_PENDING_EXCEPTION) {
1219           Handle e(THREAD, PENDING_EXCEPTION);
1220           CLEAR_PENDING_EXCEPTION;
1221           {
1222               EXCEPTION_MARK;
1223               add_initialization_error(THREAD, e);
1224               // Locks object, set state, and notify all waiting threads
1225               set_initialization_state_and_notify(initialization_error, THREAD);
1226               CLEAR_PENDING_EXCEPTION;
1227           }
1228           THROW_OOP(e());
1229       }
1230       vk->set_default_value(val);
1231   }
1232 
1233   // Step 7
1234   // Next, if C is a class rather than an interface, initialize it's super class and super
1235   // interfaces.
1236   if (!is_interface()) {
1237     Klass* super_klass = super();
1238     if (super_klass != NULL && super_klass->should_be_initialized()) {
1239       super_klass->initialize(THREAD);
1240     }
1241     // If C implements any interface that declares a non-static, concrete method,
1242     // the initialization of C triggers initialization of its super interfaces.
1243     // Only need to recurse if has_nonstatic_concrete_methods which includes declaring and
1244     // having a superinterface that declares, non-static, concrete methods
1245     if (!HAS_PENDING_EXCEPTION && has_nonstatic_concrete_methods()) {
1246       initialize_super_interfaces(THREAD);
1247     }
1248 
1249     // If any exceptions, complete abruptly, throwing the same exception as above.
1250     if (HAS_PENDING_EXCEPTION) {
1251       Handle e(THREAD, PENDING_EXCEPTION);
1252       CLEAR_PENDING_EXCEPTION;
1253       {
1254         EXCEPTION_MARK;
1255         add_initialization_error(THREAD, e);
1256         // Locks object, set state, and notify all waiting threads
1257         set_initialization_state_and_notify(initialization_error, THREAD);
1258         CLEAR_PENDING_EXCEPTION;
1259       }
1260       DTRACE_CLASSINIT_PROBE_WAIT(super__failed, -1, wait);
1261       THROW_OOP(e());
1262     }
1263   }
1264 

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

1583               : vmSymbols::java_lang_InstantiationException(), external_name());
1584   }
1585   if (this == vmClasses::Class_klass()) {
1586     ResourceMark rm(THREAD);
1587     THROW_MSG(throwError ? vmSymbols::java_lang_IllegalAccessError()
1588               : vmSymbols::java_lang_IllegalAccessException(), external_name());
1589   }
1590 }
1591 
1592 Klass* InstanceKlass::array_klass(int n, TRAPS) {
1593   // Need load-acquire for lock-free read
1594   if (array_klasses_acquire() == NULL) {
1595     ResourceMark rm(THREAD);
1596     JavaThread *jt = THREAD;
1597     {
1598       // Atomic creation of array_klasses
1599       MutexLocker ma(THREAD, MultiArray_lock);
1600 
1601       // Check if update has already taken place
1602       if (array_klasses() == NULL) {
1603         ObjArrayKlass* k = ObjArrayKlass::allocate_objArray_klass(class_loader_data(), 1, this,
1604                                                                   false, false, CHECK_NULL);
1605         // use 'release' to pair with lock-free load
1606         release_set_array_klasses(k);
1607       }
1608     }
1609   }
1610   // array_klasses() will always be set at this point
1611   ArrayKlass* ak = array_klasses();
1612   return ak->array_klass(n, THREAD);
1613 }
1614 
1615 Klass* InstanceKlass::array_klass_or_null(int n) {
1616   // Need load-acquire for lock-free read
1617   ArrayKlass* ak = array_klasses_acquire();
1618   if (ak == NULL) {
1619     return NULL;
1620   } else {
1621     return ak->array_klass_or_null(n);
1622   }
1623 }
1624 
1625 Klass* InstanceKlass::array_klass(TRAPS) {
1626   return array_klass(1, THREAD);
1627 }
1628 
1629 Klass* InstanceKlass::array_klass_or_null() {
1630   return array_klass_or_null(1);
1631 }
1632 
1633 static int call_class_initializer_counter = 0;   // for debugging
1634 
1635 Method* InstanceKlass::class_initializer() const {
1636   Method* clinit = find_method(
1637       vmSymbols::class_initializer_name(), vmSymbols::void_method_signature());
1638   if (clinit != NULL && clinit->is_class_initializer()) {
1639     return clinit;
1640   }
1641   return NULL;
1642 }
1643 
1644 void InstanceKlass::call_class_initializer(TRAPS) {
1645   if (ReplayCompiles &&
1646       (ReplaySuppressInitializers == 1 ||
1647        (ReplaySuppressInitializers >= 2 && class_loader() != NULL))) {
1648     // Hide the existence of the initializer for the purpose of replaying the compile
1649     return;
1650   }
1651 
1652 #if INCLUDE_CDS
1653   // This is needed to ensure the consistency of the archived heap objects.
1654   if (has_archived_enum_objs()) {
1655     assert(is_shared(), "must be");
1656     bool initialized = HeapShared::initialize_enum_klass(this, CHECK);
1657     if (initialized) {
1658       return;

1667     ResourceMark rm(THREAD);
1668     LogStream ls(lt);
1669     ls.print("%d Initializing ", call_class_initializer_counter++);
1670     name()->print_value_on(&ls);
1671     ls.print_cr("%s (" PTR_FORMAT ")", h_method() == NULL ? "(no method)" : "", p2i(this));
1672   }
1673   if (h_method() != NULL) {
1674     JavaCallArguments args; // No arguments
1675     JavaValue result(T_VOID);
1676     JavaCalls::call(&result, h_method, &args, CHECK); // Static call (no args)
1677   }
1678 }
1679 
1680 
1681 void InstanceKlass::mask_for(const methodHandle& method, int bci,
1682   InterpreterOopMap* entry_for) {
1683   // Lazily create the _oop_map_cache at first request
1684   // Lock-free access requires load_acquire.
1685   OopMapCache* oop_map_cache = Atomic::load_acquire(&_oop_map_cache);
1686   if (oop_map_cache == NULL) {
1687     MutexLocker x(OopMapCacheAlloc_lock,  Mutex::_no_safepoint_check_flag);
1688     // Check if _oop_map_cache was allocated while we were waiting for this lock
1689     if ((oop_map_cache = _oop_map_cache) == NULL) {
1690       oop_map_cache = new OopMapCache();
1691       // Ensure _oop_map_cache is stable, since it is examined without a lock
1692       Atomic::release_store(&_oop_map_cache, oop_map_cache);
1693     }
1694   }
1695   // _oop_map_cache is constant after init; lookup below does its own locking.
1696   oop_map_cache->lookup(method, bci, entry_for);
1697 }
1698 





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

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

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

2631   if (itable_length() > 0) {
2632     itableOffsetEntry* ioe = (itableOffsetEntry*)start_of_itable();
2633     int method_table_offset_in_words = ioe->offset()/wordSize;
2634     int nof_interfaces = (method_table_offset_in_words - itable_offset_in_words())
2635                          / itableOffsetEntry::size();
2636 
2637     for (int i = 0; i < nof_interfaces; i ++, ioe ++) {
2638       if (ioe->interface_klass() != NULL) {
2639         it->push(ioe->interface_klass_addr());
2640         itableMethodEntry* ime = ioe->first_method_entry(this);
2641         int n = klassItable::method_count_for_interface(ioe->interface_klass());
2642         for (int index = 0; index < n; index ++) {
2643           it->push(ime[index].method_addr());
2644         }
2645       }
2646     }
2647   }
2648 
2649   it->push(&_nest_members);
2650   it->push(&_permitted_subclasses);
2651   it->push(&_preload_classes);
2652   it->push(&_record_components);
2653 
2654   if (has_inline_type_fields()) {
2655     for (int i = 0; i < java_fields_count(); i++) {
2656       it->push(&((Klass**)adr_inline_type_field_klasses())[i]);
2657     }
2658   }
2659 }
2660 
2661 #if INCLUDE_CDS
2662 void InstanceKlass::remove_unshareable_info() {
2663 
2664   if (is_linked()) {
2665     assert(can_be_verified_at_dumptime(), "must be");
2666     // Remember this so we can avoid walking the hierarchy at runtime.
2667     set_verified_at_dump_time();
2668   }
2669 
2670   Klass::remove_unshareable_info();
2671 
2672   if (SystemDictionaryShared::has_class_failed_verification(this)) {
2673     // Classes are attempted to link during dumping and may fail,
2674     // but these classes are still in the dictionary and class list in CLD.
2675     // If the class has failed verification, there is nothing else to remove.
2676     return;
2677   }
2678 

2682   // being added to class hierarchy (see SystemDictionary:::add_to_hierarchy()).
2683   _init_state = allocated;
2684 
2685   { // Otherwise this needs to take out the Compile_lock.
2686     assert(SafepointSynchronize::is_at_safepoint(), "only called at safepoint");
2687     init_implementor();
2688   }
2689 
2690   constants()->remove_unshareable_info();
2691 
2692   for (int i = 0; i < methods()->length(); i++) {
2693     Method* m = methods()->at(i);
2694     m->remove_unshareable_info();
2695   }
2696 
2697   // do array classes also.
2698   if (array_klasses() != NULL) {
2699     array_klasses()->remove_unshareable_info();
2700   }
2701 
2702   if (has_inline_type_fields()) {
2703     for (AllFieldStream fs(fields(), constants()); !fs.done(); fs.next()) {
2704       if (Signature::basic_type(fs.signature()) == T_PRIMITIVE_OBJECT) {
2705         reset_inline_type_field_klass(fs.index());
2706       }
2707     }
2708   }
2709 
2710   // These are not allocated from metaspace. They are safe to set to NULL.
2711   _source_debug_extension = NULL;
2712   _dep_context = NULL;
2713   _osr_nmethods_head = NULL;
2714 #if INCLUDE_JVMTI
2715   _breakpoints = NULL;
2716   _previous_versions = NULL;
2717   _cached_class_file = NULL;
2718   _jvmti_cached_class_field_map = NULL;
2719 #endif
2720 
2721   _init_thread = NULL;
2722   _methods_jmethod_ids = NULL;
2723   _jni_ids = NULL;
2724   _oop_map_cache = NULL;
2725   // clear _nest_host to ensure re-load at runtime
2726   _nest_host = NULL;
2727   init_shared_package_entry();
2728   _dep_context_last_cleaned = 0;
2729   _init_monitor = NULL;

2752     if (is_shared_unregistered_class()) {
2753       _package_entry = NULL;
2754     } else {
2755       _package_entry = PackageEntry::get_archived_entry(_package_entry);
2756     }
2757   }
2758   ArchivePtrMarker::mark_pointer((address**)&_package_entry);
2759 #endif
2760 }
2761 
2762 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain,
2763                                              PackageEntry* pkg_entry, TRAPS) {
2764   // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2765   // before the InstanceKlass is added to the SystemDictionary. Make
2766   // sure the current state is <loaded.
2767   assert(!is_loaded(), "invalid init state");
2768   assert(!shared_loading_failed(), "Must not try to load failed class again");
2769   set_package(loader_data, pkg_entry, CHECK);
2770   Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2771 
2772   if (is_inline_klass()) {
2773     InlineKlass::cast(this)->initialize_calling_convention(CHECK);
2774   }
2775 
2776   Array<Method*>* methods = this->methods();
2777   int num_methods = methods->length();
2778   for (int index = 0; index < num_methods; ++index) {
2779     methods->at(index)->restore_unshareable_info(CHECK);
2780   }
2781 #if INCLUDE_JVMTI
2782   if (JvmtiExport::has_redefined_a_class()) {
2783     // Reinitialize vtable because RedefineClasses may have changed some
2784     // entries in this vtable for super classes so the CDS vtable might
2785     // point to old or obsolete entries.  RedefineClasses doesn't fix up
2786     // vtables in the shared system dictionary, only the main one.
2787     // It also redefines the itable too so fix that too.
2788     // First fix any default methods that point to a super class that may
2789     // have been redefined.
2790     bool trace_name_printed = false;
2791     adjust_default_methods(&trace_name_printed);
2792     vtable().initialize_vtable();
2793     itable().initialize_itable();
2794   }
2795 #endif

2966 
2967 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2968   if (array == NULL) {
2969     _source_debug_extension = NULL;
2970   } else {
2971     // Adding one to the attribute length in order to store a null terminator
2972     // character could cause an overflow because the attribute length is
2973     // already coded with an u4 in the classfile, but in practice, it's
2974     // unlikely to happen.
2975     assert((length+1) > length, "Overflow checking");
2976     char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2977     for (int i = 0; i < length; i++) {
2978       sde[i] = array[i];
2979     }
2980     sde[length] = '\0';
2981     _source_debug_extension = sde;
2982   }
2983 }
2984 
2985 const char* InstanceKlass::signature_name() const {
2986   return signature_name_of_carrier(JVM_SIGNATURE_CLASS);
2987 }
2988 
2989 const char* InstanceKlass::signature_name_of_carrier(char c) const {
2990   int hash_len = 0;
2991   char hash_buf[40];
2992 
2993   // Get the internal name as a c string
2994   const char* src = (const char*) (name()->as_C_string());
2995   const int src_length = (int)strlen(src);
2996 
2997   char* dest = NEW_RESOURCE_ARRAY(char, src_length + hash_len + 3);
2998 
2999   // Add L or Q as type indicator
3000   int dest_index = 0;
3001   dest[dest_index++] = c;
3002 
3003   // Add the actual class name
3004   for (int src_index = 0; src_index < src_length; ) {
3005     dest[dest_index++] = src[src_index++];
3006   }
3007 
3008   if (is_hidden()) { // Replace the last '+' with a '.'.
3009     for (int index = (int)src_length; index > 0; index--) {
3010       if (dest[index] == '+') {
3011         dest[index] = JVM_SIGNATURE_DOT;
3012         break;
3013       }
3014     }
3015   }
3016 
3017   // If we have a hash, append it
3018   for (int hash_index = 0; hash_index < hash_len; ) {
3019     dest[dest_index++] = hash_buf[hash_index++];
3020   }
3021 

3328 jint InstanceKlass::compute_modifier_flags() const {
3329   jint access = access_flags().as_int();
3330 
3331   // But check if it happens to be member class.
3332   InnerClassesIterator iter(this);
3333   for (; !iter.done(); iter.next()) {
3334     int ioff = iter.inner_class_info_index();
3335     // Inner class attribute can be zero, skip it.
3336     // Strange but true:  JVM spec. allows null inner class refs.
3337     if (ioff == 0) continue;
3338 
3339     // only look at classes that are already loaded
3340     // since we are looking for the flags for our self.
3341     Symbol* inner_name = constants()->klass_name_at(ioff);
3342     if (name() == inner_name) {
3343       // This is really a member class.
3344       access = iter.inner_access_flags();
3345       break;
3346     }
3347   }
3348   return (access & JVM_ACC_WRITTEN_FLAGS);

3349 }
3350 
3351 jint InstanceKlass::jvmti_class_status() const {
3352   jint result = 0;
3353 
3354   if (is_linked()) {
3355     result |= JVMTI_CLASS_STATUS_VERIFIED | JVMTI_CLASS_STATUS_PREPARED;
3356   }
3357 
3358   if (is_initialized()) {
3359     assert(is_linked(), "Class status is not consistent");
3360     result |= JVMTI_CLASS_STATUS_INITIALIZED;
3361   }
3362   if (is_in_error_state()) {
3363     result |= JVMTI_CLASS_STATUS_ERROR;
3364   }
3365   return result;
3366 }
3367 
3368 Method* InstanceKlass::method_at_itable(InstanceKlass* holder, int index, TRAPS) {

3585     }
3586     osr = osr->osr_link();
3587   }
3588 
3589   assert(match_level == false || best == NULL, "shouldn't pick up anything if match_level is set");
3590   if (best != NULL && best->comp_level() >= comp_level) {
3591     return best;
3592   }
3593   return NULL;
3594 }
3595 
3596 // -----------------------------------------------------------------------------------------------------
3597 // Printing
3598 
3599 #define BULLET  " - "
3600 
3601 static const char* state_names[] = {
3602   "allocated", "loaded", "being_linked", "linked", "being_initialized", "fully_initialized", "initialization_error"
3603 };
3604 
3605 static void print_vtable(address self, intptr_t* start, int len, outputStream* st) {
3606   ResourceMark rm;
3607   int* forward_refs = NEW_RESOURCE_ARRAY(int, len);
3608   for (int i = 0; i < len; i++)  forward_refs[i] = 0;
3609   for (int i = 0; i < len; i++) {
3610     intptr_t e = start[i];
3611     st->print("%d : " INTPTR_FORMAT, i, e);
3612     if (forward_refs[i] != 0) {
3613       int from = forward_refs[i];
3614       int off = (int) start[from];
3615       st->print(" (offset %d <= [%d])", off, from);
3616     }
3617     if (MetaspaceObj::is_valid((Metadata*)e)) {
3618       st->print(" ");
3619       ((Metadata*)e)->print_value_on(st);
3620     } else if (self != NULL && e > 0 && e < 0x10000) {
3621       address location = self + e;
3622       int index = (int)((intptr_t*)location - start);
3623       st->print(" (offset %d => [%d])", (int)e, index);
3624       if (index >= 0 && index < len)
3625         forward_refs[index] = i;
3626     }
3627     st->cr();
3628   }
3629 }
3630 
3631 static void print_vtable(vtableEntry* start, int len, outputStream* st) {
3632   return print_vtable(NULL, reinterpret_cast<intptr_t*>(start), len, st);
3633 }
3634 
3635 template<typename T>
3636  static void print_array_on(outputStream* st, Array<T>* array) {
3637    if (array == NULL) { st->print_cr("NULL"); return; }
3638    array->print_value_on(st); st->cr();
3639    if (Verbose || WizardMode) {
3640      for (int i = 0; i < array->length(); i++) {
3641        st->print("%d : ", i); array->at(i)->print_value_on(st); st->cr();
3642      }
3643    }
3644  }
3645 
3646 static void print_array_on(outputStream* st, Array<int>* array) {
3647   if (array == NULL) { st->print_cr("NULL"); return; }
3648   array->print_value_on(st); st->cr();
3649   if (Verbose || WizardMode) {
3650     for (int i = 0; i < array->length(); i++) {
3651       st->print("%d : %d", i, array->at(i)); st->cr();
3652     }
3653   }
3654 }
3655 
3656 const char* InstanceKlass::init_state_name() const {
3657   return state_names[_init_state];
3658 }
3659 
3660 void InstanceKlass::print_on(outputStream* st) const {
3661   assert(is_klass(), "must be klass");
3662   Klass::print_on(st);
3663 
3664   st->print(BULLET"instance size:     %d", size_helper());                        st->cr();
3665   st->print(BULLET"klass size:        %d", size());                               st->cr();
3666   st->print(BULLET"access:            "); access_flags().print_on(st);            st->cr();
3667   st->print(BULLET"misc flags:        0x%x", _misc_flags);                        st->cr();
3668   st->print(BULLET"state:             "); st->print_cr("%s", init_state_name());
3669   st->print(BULLET"name:              "); name()->print_value_on(st);             st->cr();
3670   st->print(BULLET"super:             "); Metadata::print_value_on_maybe_null(st, super()); st->cr();
3671   st->print(BULLET"sub:               ");
3672   Klass* sub = subklass();
3673   int n;
3674   for (n = 0; sub != NULL; n++, sub = sub->next_sibling()) {
3675     if (n < MaxSubklassPrintSize) {
3676       sub->print_value_on(st);
3677       st->print("   ");
3678     }
3679   }
3680   if (n >= MaxSubklassPrintSize) st->print("(" INTX_FORMAT " more klasses...)", n - MaxSubklassPrintSize);
3681   st->cr();
3682 
3683   if (is_interface()) {
3684     st->print_cr(BULLET"nof implementors:  %d", nof_implementors());
3685     if (nof_implementors() == 1) {
3686       st->print_cr(BULLET"implementor:    ");
3687       st->print("   ");
3688       implementor()->print_value_on(st);
3689       st->cr();
3690     }
3691   }
3692 
3693   st->print(BULLET"arrays:            "); Metadata::print_value_on_maybe_null(st, array_klasses()); st->cr();
3694   st->print(BULLET"methods:           "); print_array_on(st, methods());
3695   st->print(BULLET"method ordering:   "); print_array_on(st, method_ordering());
3696   st->print(BULLET"default_methods:   "); print_array_on(st, default_methods());












3697   if (default_vtable_indices() != NULL) {
3698     st->print(BULLET"default vtable indices:   "); print_array_on(st, default_vtable_indices());
3699   }
3700   st->print(BULLET"local interfaces:  "); print_array_on(st, local_interfaces());
3701   st->print(BULLET"trans. interfaces: "); print_array_on(st, transitive_interfaces());
3702   st->print(BULLET"constants:         "); constants()->print_value_on(st);         st->cr();
3703   if (class_loader_data() != NULL) {
3704     st->print(BULLET"class loader data:  ");
3705     class_loader_data()->print_value_on(st);
3706     st->cr();
3707   }
3708   if (source_file_name() != NULL) {
3709     st->print(BULLET"source file:       ");
3710     source_file_name()->print_value_on(st);
3711     st->cr();
3712   }
3713   if (source_debug_extension() != NULL) {
3714     st->print(BULLET"source debug extension:       ");
3715     st->print("%s", source_debug_extension());
3716     st->cr();
3717   }
3718   st->print(BULLET"class annotations:       "); class_annotations()->print_value_on(st); st->cr();
3719   st->print(BULLET"class type annotations:  "); class_type_annotations()->print_value_on(st); st->cr();
3720   st->print(BULLET"field annotations:       "); fields_annotations()->print_value_on(st); st->cr();
3721   st->print(BULLET"field type annotations:  "); fields_type_annotations()->print_value_on(st); st->cr();

3727          pv_node = pv_node->previous_versions()) {
3728       if (!have_pv)
3729         st->print(BULLET"previous version:  ");
3730       have_pv = true;
3731       pv_node->constants()->print_value_on(st);
3732     }
3733     if (have_pv) st->cr();
3734   }
3735 
3736   if (generic_signature() != NULL) {
3737     st->print(BULLET"generic signature: ");
3738     generic_signature()->print_value_on(st);
3739     st->cr();
3740   }
3741   st->print(BULLET"inner classes:     "); inner_classes()->print_value_on(st);     st->cr();
3742   st->print(BULLET"nest members:     "); nest_members()->print_value_on(st);     st->cr();
3743   if (record_components() != NULL) {
3744     st->print(BULLET"record components:     "); record_components()->print_value_on(st);     st->cr();
3745   }
3746   st->print(BULLET"permitted subclasses:     "); permitted_subclasses()->print_value_on(st);     st->cr();
3747   st->print(BULLET"preload classes:     "); preload_classes()->print_value_on(st); st->cr();
3748   if (java_mirror() != NULL) {
3749     st->print(BULLET"java mirror:       ");
3750     java_mirror()->print_value_on(st);
3751     st->cr();
3752   } else {
3753     st->print_cr(BULLET"java mirror:       NULL");
3754   }
3755   st->print(BULLET"vtable length      %d  (start addr: " PTR_FORMAT ")", vtable_length(), p2i(start_of_vtable())); st->cr();
3756   if (vtable_length() > 0 && (Verbose || WizardMode))  print_vtable(start_of_vtable(), vtable_length(), st);
3757   st->print(BULLET"itable length      %d (start addr: " PTR_FORMAT ")", itable_length(), p2i(start_of_itable())); st->cr();
3758   if (itable_length() > 0 && (Verbose || WizardMode))  print_vtable(NULL, start_of_itable(), itable_length(), st);
3759   st->print_cr(BULLET"---- static fields (%d words):", static_field_size());
3760   FieldPrinter print_static_field(st);
3761   ((InstanceKlass*)this)->do_local_static_fields(&print_static_field);
3762   st->print_cr(BULLET"---- non-static fields (%d words):", nonstatic_field_size());
3763   FieldPrinter print_nonstatic_field(st);
3764   InstanceKlass* ik = const_cast<InstanceKlass*>(this);
3765   ik->print_nonstatic_fields(&print_nonstatic_field);
3766 
3767   st->print(BULLET"non-static oop maps: ");
3768   OopMapBlock* map     = start_of_nonstatic_oop_maps();
3769   OopMapBlock* end_map = map + nonstatic_oop_map_count();
3770   while (map < end_map) {
3771     st->print("%d-%d ", map->offset(), map->offset() + heapOopSize*(map->count() - 1));
3772     map++;
3773   }
3774   st->cr();
3775 }
3776 
3777 void InstanceKlass::print_value_on(outputStream* st) const {
3778   assert(is_klass(), "must be klass");
< prev index next >