< prev index next >

src/share/vm/oops/klassVtable.cpp

Print this page




  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "gc/shared/gcLocker.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/metaspaceShared.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "memory/universe.inline.hpp"
  33 #include "oops/instanceKlass.hpp"
  34 #include "oops/klassVtable.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/objArrayOop.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "runtime/arguments.hpp"
  39 #include "runtime/handles.inline.hpp"
  40 #include "utilities/copy.hpp"
  41 
  42 inline InstanceKlass* klassVtable::ik() const {
  43   return InstanceKlass::cast(_klass());
  44 }
  45 
  46 bool klassVtable::is_preinitialized_vtable() {
  47   return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
  48 }
  49 
  50 
  51 // this function computes the vtable size (including the size needed for miranda
  52 // methods) and the number of miranda methods in this class.
  53 // Note on Miranda methods: Let's say there is a class C that implements
  54 // interface I, and none of C's superclasses implements I.
  55 // Let's say there is an abstract method m in I that neither C
  56 // nor any of its super classes implement (i.e there is no method of any access,
  57 // with the same name and signature as m), then m is a Miranda method which is
  58 // entered as a public abstract method in C's vtable.  From then on it should
  59 // treated as any other public method in C for method over-ride purposes.
  60 void klassVtable::compute_vtable_size_and_num_mirandas(
  61     int* vtable_length_ret, int* num_new_mirandas,
  62     GrowableArray<Method*>* all_mirandas, const Klass* super,
  63     Array<Method*>* methods, AccessFlags class_flags, u2 major_version,


 111       // SystemDictionary::resolve_from_stream(), which will detect this later
 112       // and throw a security exception.  So don't assert here to let
 113       // the exception occur.
 114       vtable_length = Universe::base_vtable_size();
 115     }
 116   }
 117   assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");
 118   assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");
 119 
 120   *vtable_length_ret = vtable_length;
 121 }
 122 
 123 int klassVtable::index_of(Method* m, int len) const {
 124   assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
 125   return m->vtable_index();
 126 }
 127 
 128 // Copy super class's vtable to the first part (prefix) of this class's vtable,
 129 // and return the number of entries copied.  Expects that 'super' is the Java
 130 // super class (arrays can have "array" super classes that must be skipped).
 131 int klassVtable::initialize_from_super(KlassHandle super) {
 132   if (super.is_null()) {
 133     return 0;
 134   } else if (is_preinitialized_vtable()) {
 135     // A shared class' vtable is preinitialized at dump time. No need to copy
 136     // methods from super class for shared class, as that was already done
 137     // during archiving time. However, if Jvmti has redefined a class,
 138     // copy super class's vtable in case the super class has changed.
 139     return super->vtable()->length();
 140   } else {
 141     // copy methods from superKlass
 142     klassVtable* superVtable = super->vtable();
 143     assert(superVtable->length() <= _length, "vtable too short");
 144 #ifdef ASSERT
 145     superVtable->verify(tty, true);
 146 #endif
 147     superVtable->copy_vtable_to(table());
 148     if (log_develop_is_enabled(Trace, vtables)) {
 149       ResourceMark rm;
 150       log_develop_trace(vtables)("copy vtable from %s to %s size %d",
 151                                  super->internal_name(), klass()->internal_name(),
 152                                  _length);
 153     }
 154     return superVtable->length();
 155   }
 156 }
 157 
 158 //
 159 // Revised lookup semantics   introduced 1.3 (Kestrel beta)
 160 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
 161 
 162   // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
 163   KlassHandle super (THREAD, klass()->java_super());
 164   int nofNewEntries = 0;
 165 
 166   bool is_shared = _klass->is_shared();
 167 
 168   if (!klass()->is_array_klass()) {
 169     ResourceMark rm(THREAD);
 170     log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
 171   }
 172 
 173 #ifdef ASSERT
 174   oop* end_of_obj = (oop*)_klass() + _klass()->size();
 175   oop* end_of_vtable = (oop*)&table()[_length];
 176   assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
 177 #endif
 178 
 179   if (Universe::is_bootstrapping()) {
 180     assert(!is_shared, "sanity");
 181     // just clear everything
 182     for (int i = 0; i < _length; i++) table()[i].clear();
 183     return;
 184   }
 185 
 186   int super_vtable_len = initialize_from_super(super);
 187   if (klass()->is_array_klass()) {
 188     assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
 189   } else {
 190     assert(_klass->is_instance_klass(), "must be InstanceKlass");
 191 
 192     Array<Method*>* methods = ik()->methods();
 193     int len = methods->length();
 194     int initialized = super_vtable_len;
 195 
 196     // Check each of this class's methods against super;
 197     // if override, replace in copy of super vtable, otherwise append to end
 198     for (int i = 0; i < len; i++) {
 199       // update_inherited_vtable can stop for gc - ensure using handles
 200       HandleMark hm(THREAD);
 201       assert(methods->at(i)->is_method(), "must be a Method*");
 202       methodHandle mh(THREAD, methods->at(i));
 203 
 204       bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK);
 205 
 206       if (needs_new_entry) {
 207         put_method_at(mh(), initialized);


 310           logst->print("overriders flags: ");
 311           target_method->print_linkage_flags(logst);
 312           logst->cr();
 313         }
 314 
 315         break; // return found superk
 316       }
 317     } else  {
 318       // super class has no vtable entry here, stop transitive search
 319       superk = (InstanceKlass*)NULL;
 320       break;
 321     }
 322     // if no override found yet, continue to search up
 323     superk = superk->super() == NULL ? NULL : InstanceKlass::cast(superk->super());
 324   }
 325 
 326   return superk;
 327 }
 328 
 329 static void log_vtables(int i, bool overrides, methodHandle target_method,
 330                         KlassHandle target_klass, Method* super_method,
 331                         Thread* thread) {
 332 #ifndef PRODUCT
 333   if (log_develop_is_enabled(Trace, vtables)) {
 334     ResourceMark rm(thread);
 335     outputStream* logst = Log(vtables)::trace_stream();
 336     char* sig = target_method()->name_and_sig_as_C_string();
 337     if (overrides) {
 338       logst->print("overriding with %s index %d, original flags: ",
 339                    sig, i);
 340     } else {
 341       logst->print("NOT overriding with %s index %d, original flags: ",
 342                    sig, i);
 343     }
 344     super_method->print_linkage_flags(logst);
 345     logst->print("overriders flags: ");
 346     target_method->print_linkage_flags(logst);
 347     logst->cr();
 348   }
 349 #endif
 350 }


 415   if (super == NULL) {
 416     return allocate_new;
 417   }
 418 
 419   // private methods in classes always have a new entry in the vtable
 420   // specification interpretation since classic has
 421   // private methods not overriding
 422   // JDK8 adds private methods in interfaces which require invokespecial
 423   if (target_method()->is_private()) {
 424     return allocate_new;
 425   }
 426 
 427   // search through the vtable and update overridden entries
 428   // Since check_signature_loaders acquires SystemDictionary_lock
 429   // which can block for gc, once we are in this loop, use handles
 430   // For classfiles built with >= jdk7, we now look for transitive overrides
 431 
 432   Symbol* name = target_method()->name();
 433   Symbol* signature = target_method()->signature();
 434 
 435   KlassHandle target_klass(THREAD, target_method()->method_holder());
 436   if (target_klass == NULL) {
 437     target_klass = _klass;
 438   }
 439 
 440   Handle target_loader(THREAD, target_klass->class_loader());
 441 
 442   Symbol* target_classname = target_klass->name();
 443   for(int i = 0; i < super_vtable_len; i++) {
 444     Method* super_method;
 445     if (is_preinitialized_vtable()) {
 446       // If this is a shared class, the vtable is already in the final state (fully
 447       // initialized). Need to look at the super's vtable.
 448       klassVtable* superVtable = super->vtable();
 449       super_method = superVtable->method_at(i);
 450     } else {
 451       super_method = method_at(i);
 452     }
 453     // Check if method name matches
 454     if (super_method->name() == name && super_method->signature() == signature) {
 455 


 938     assert(!old_method->is_deleted(), "vtable methods may not be deleted");
 939 
 940     Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
 941 
 942     assert(new_method != NULL, "method_with_idnum() should not be NULL");
 943     assert(old_method != new_method, "sanity check");
 944 
 945     put_method_at(new_method, index);
 946     // For default methods, need to update the _default_methods array
 947     // which can only have one method entry for a given signature
 948     bool updated_default = false;
 949     if (old_method->is_default_method()) {
 950       updated_default = adjust_default_method(index, old_method, new_method);
 951     }
 952 
 953     if (log_is_enabled(Info, redefine, class, update)) {
 954       ResourceMark rm;
 955       if (!(*trace_name_printed)) {
 956         log_info(redefine, class, update)
 957           ("adjust: klassname=%s for methods from name=%s",
 958            klass()->external_name(), old_method->method_holder()->external_name());
 959         *trace_name_printed = true;
 960       }
 961       log_debug(redefine, class, update, vtables)
 962         ("vtable method update: %s(%s), updated default = %s",
 963          new_method->name()->as_C_string(), new_method->signature()->as_C_string(), updated_default ? "true" : "false");
 964     }
 965   }
 966 }
 967 
 968 // a vtable should never contain old or obsolete methods
 969 bool klassVtable::check_no_old_or_obsolete_entries() {
 970   for (int i = 0; i < length(); i++) {
 971     Method* m = unchecked_method_at(i);
 972     if (m != NULL &&
 973         (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
 974       return false;
 975     }
 976   }
 977   return true;
 978 }


1008 }
1009 
1010 //-----------------------------------------------------------------------------------------
1011 // Itable code
1012 
1013 // Initialize a itableMethodEntry
1014 void itableMethodEntry::initialize(Method* m) {
1015   if (m == NULL) return;
1016 
1017   if (MetaspaceShared::is_in_shared_space((void*)&_method) &&
1018      !MetaspaceShared::remapped_readwrite()) {
1019     // At runtime initialize_itable is rerun as part of link_class_impl()
1020     // for a shared class loaded by the non-boot loader.
1021     // The dumptime itable method entry should be the same as the runtime entry.
1022     assert(_method == m, "sanity");
1023   } else {
1024     _method = m;
1025   }
1026 }
1027 
1028 klassItable::klassItable(instanceKlassHandle klass) {
1029   _klass = klass;
1030 
1031   if (klass->itable_length() > 0) {
1032     itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
1033     if (offset_entry  != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
1034       // First offset entry points to the first method_entry
1035       intptr_t* method_entry  = (intptr_t *)(((address)klass()) + offset_entry->offset());
1036       intptr_t* end         = klass->end_of_itable();
1037 
1038       _table_offset      = (intptr_t*)offset_entry - (intptr_t*)klass();
1039       _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
1040       _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
1041       assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
1042       return;
1043     }
1044   }
1045 
1046   // The length of the itable was either zero, or it has not yet been initialized.
1047   _table_offset      = 0;
1048   _size_offset_table = 0;
1049   _size_method_table = 0;
1050 }
1051 
1052 static int initialize_count = 0;
1053 
1054 // Initialization
1055 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1056   if (_klass->is_interface()) {
1057     // This needs to go after vtable indices are assigned but
1058     // before implementors need to know the number of itable indices.
1059     assign_itable_indices_for_interface(_klass());
1060   }
1061 
1062   // Cannot be setup doing bootstrapping, interfaces don't have
1063   // itables, and klass with only ones entry have empty itables
1064   if (Universe::is_bootstrapping() ||
1065       _klass->is_interface() ||
1066       _klass->itable_length() == itableOffsetEntry::size()) return;
1067 
1068   // There's alway an extra itable entry so we can null-terminate it.
1069   guarantee(size_offset_table() >= 1, "too small");
1070   int num_interfaces = size_offset_table() - 1;
1071   if (num_interfaces > 0) {
1072     log_develop_debug(itables)("%3d: Initializing itables for %s", ++initialize_count,
1073                        _klass->name()->as_C_string());
1074 
1075 
1076     // Iterate through all interfaces
1077     int i;
1078     for(i = 0; i < num_interfaces; i++) {
1079       itableOffsetEntry* ioe = offset_entry(i);
1080       HandleMark hm(THREAD);
1081       KlassHandle interf_h (THREAD, ioe->interface_klass());
1082       assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1083       initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1084     }
1085 
1086   }
1087   // Check that the last entry is empty
1088   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1089   guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1090 }
1091 
1092 
1093 inline bool interface_method_needs_itable_index(Method* m) {
1094   if (m->is_static())           return false;   // e.g., Stream.empty
1095   if (m->is_initializer())      return false;   // <init> or <clinit>
1096   if (m->is_private())          return false;   // requires invokeSpecial
1097   // If an interface redeclares a method from java.lang.Object,
1098   // it should already have a vtable index, don't touch it.
1099   // e.g., CharSequence.toString (from initialize_vtable)
1100   // if (m->has_vtable_index())  return false; // NO!
1101   return true;
1102 }
1103 


1152     Method* m = methods->at(nof_methods-1);
1153     if (m->has_itable_index()) {
1154       length = m->itable_index() + 1;
1155       break;
1156     }
1157     nof_methods -= 1;
1158   }
1159 #ifdef ASSERT
1160   int nof_methods_copy = nof_methods;
1161   while (nof_methods_copy > 0) {
1162     Method* mm = methods->at(--nof_methods_copy);
1163     assert(!mm->has_itable_index() || mm->itable_index() < length, "");
1164   }
1165 #endif //ASSERT
1166   // return the rightmost itable index, plus one; or 0 if no methods have
1167   // itable indices
1168   return length;
1169 }
1170 
1171 
1172 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
1173   Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods();
1174   int nof_methods = methods->length();
1175   HandleMark hm;
1176   assert(nof_methods > 0, "at least one method must exist for interface to be in vtable");
1177   Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader());
1178 
1179   int ime_count = method_count_for_interface(interf_h());
1180   for (int i = 0; i < nof_methods; i++) {
1181     Method* m = methods->at(i);
1182     methodHandle target;
1183     if (m->has_itable_index()) {
1184       // This search must match the runtime resolution, i.e. selection search for invokeinterface
1185       // to correctly enforce loader constraints for interface method inheritance
1186       target = LinkResolver::lookup_instance_method_in_klasses(_klass, m->name(), m->signature(), CHECK);
1187     }
1188     if (target == NULL || !target->is_public() || target->is_abstract()) {
1189       // Entry does not resolve. Leave it empty for AbstractMethodError.
1190         if (!(target == NULL) && !target->is_public()) {
1191           // Stuff an IllegalAccessError throwing method in there instead.
1192           itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()].
1193               initialize(Universe::throw_illegal_access_error());
1194         }
1195     } else {
1196       // Entry did resolve, check loader constraints before initializing
1197       // if checkconstraints requested
1198       if (checkconstraints) {
1199         Handle method_holder_loader (THREAD, target->method_holder()->class_loader());
1200         if (method_holder_loader() != interface_loader()) {
1201           ResourceMark rm(THREAD);
1202           Symbol* failed_type_symbol =
1203             SystemDictionary::check_signature_loaders(m->signature(),
1204                                                       method_holder_loader,
1205                                                       interface_loader,
1206                                                       true, CHECK);
1207           if (failed_type_symbol != NULL) {
1208             const char* msg = "loader constraint violation in interface "
1209               "itable initialization: when resolving method \"%s\" the class"
1210               " loader (instance of %s) of the current class, %s, "
1211               "and the class loader (instance of %s) for interface "
1212               "%s have different Class objects for the type %s "
1213               "used in the signature";
1214             char* sig = target()->name_and_sig_as_C_string();
1215             const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
1216             char* current = _klass->name()->as_C_string();
1217             const char* loader2 = SystemDictionary::loader_name(interface_loader());
1218             char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
1219             char* failed_type_name = failed_type_symbol->as_C_string();
1220             size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
1221               strlen(current) + strlen(loader2) + strlen(iface) +
1222               strlen(failed_type_name);
1223             char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
1224             jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
1225                          iface, failed_type_name);
1226             THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
1227           }
1228         }
1229       }
1230 
1231       // ime may have moved during GC so recalculate address
1232       int ime_num = m->itable_index();
1233       assert(ime_num < ime_count, "oob");
1234       itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
1235       if (log_develop_is_enabled(Trace, itables)) {
1236         ResourceMark rm(THREAD);
1237         if (target() != NULL) {
1238           outputStream* logst = Log(itables)::trace_stream();
1239           char* sig = target()->name_and_sig_as_C_string();
1240           logst->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1241                        interf_h()->internal_name(), ime_num, sig,
1242                        target()->method_holder()->internal_name());
1243           logst->print("target_method flags: ");
1244           target()->print_linkage_flags(logst);
1245           logst->cr();
1246         }
1247       }
1248     }
1249   }
1250 }
1251 
1252 // Update entry for specific Method*
1253 void klassItable::initialize_with_method(Method* m) {
1254   itableMethodEntry* ime = method_entry(0);
1255   for(int i = 0; i < _size_method_table; i++) {
1256     if (ime->method() == m) {
1257       ime->initialize(m);
1258     }
1259     ime++;
1260   }
1261 }


1391     _method_entry += method_count;
1392   }
1393 };
1394 
1395 int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) {
1396   // Count no of interfaces and total number of interface methods
1397   CountInterfacesClosure cic;
1398   visit_all_interfaces(transitive_interfaces, &cic);
1399 
1400   // There's alway an extra itable entry so we can null-terminate it.
1401   int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
1402 
1403   // Statistics
1404   update_stats(itable_size * wordSize);
1405 
1406   return itable_size;
1407 }
1408 
1409 
1410 // Fill out offset table and interface klasses into the itable space
1411 void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {
1412   if (klass->itable_length() == 0) return;
1413   assert(!klass->is_interface(), "Should have zero length itable");
1414 
1415   // Count no of interfaces and total number of interface methods
1416   CountInterfacesClosure cic;
1417   visit_all_interfaces(klass->transitive_interfaces(), &cic);
1418   int nof_methods    = cic.nof_methods();
1419   int nof_interfaces = cic.nof_interfaces();
1420 
1421   // Add one extra entry so we can null-terminate the table
1422   nof_interfaces++;
1423 
1424   assert(compute_itable_size(klass->transitive_interfaces()) ==
1425          calc_itable_size(nof_interfaces, nof_methods),
1426          "mismatch calculation of itable size");
1427 
1428   // Fill-out offset table
1429   itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
1430   itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
1431   intptr_t* end               = klass->end_of_itable();
1432   assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
1433   assert((oop*)(end) == (oop*)(ime + nof_methods),                      "wrong offset calculation (2)");
1434 
1435   // Visit all interfaces and initialize itable offset table
1436   SetupItableClosure sic((address)klass(), ioe, ime);
1437   visit_all_interfaces(klass->transitive_interfaces(), &sic);
1438 
1439 #ifdef ASSERT
1440   ime  = sic.method_entry();
1441   oop* v = (oop*) klass->end_of_itable();
1442   assert( (oop*)(ime) == v, "wrong offset calculation (2)");
1443 #endif
1444 }
1445 
1446 
1447 // inverse to itable_index
1448 Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) {
1449   assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");
1450   assert(intf->verify_itable_index(itable_index), "");
1451   Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1452 
1453   if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
1454     return NULL;                // help caller defend against bad indices
1455 
1456   int index = itable_index;


1459   while (!m->has_itable_index() ||
1460          (index2 = m->itable_index()) != itable_index) {
1461     assert(index2 < itable_index, "monotonic");
1462     if (++index == methods->length())
1463       return NULL;
1464     m = methods->at(index);
1465   }
1466   assert(m->itable_index() == itable_index, "correct inverse");
1467 
1468   return m;
1469 }
1470 
1471 void klassVtable::verify(outputStream* st, bool forced) {
1472   // make sure table is initialized
1473   if (!Universe::is_fully_initialized()) return;
1474 #ifndef PRODUCT
1475   // avoid redundant verifies
1476   if (!forced && _verify_count == Universe::verify_count()) return;
1477   _verify_count = Universe::verify_count();
1478 #endif
1479   oop* end_of_obj = (oop*)_klass() + _klass()->size();
1480   oop* end_of_vtable = (oop *)&table()[_length];
1481   if (end_of_vtable > end_of_obj) {
1482     fatal("klass %s: klass object too short (vtable extends beyond end)",
1483           _klass->internal_name());
1484   }
1485 
1486   for (int i = 0; i < _length; i++) table()[i].verify(this, st);
1487   // verify consistency with superKlass vtable
1488   Klass* super = _klass->super();
1489   if (super != NULL) {
1490     InstanceKlass* sk = InstanceKlass::cast(super);
1491     klassVtable* vt = sk->vtable();
1492     for (int i = 0; i < vt->length(); i++) {
1493       verify_against(st, vt, i);
1494     }
1495   }
1496 }
1497 
1498 void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
1499   vtableEntry* vte = &vt->table()[index];
1500   if (vte->method()->name()      != table()[index].method()->name() ||
1501       vte->method()->signature() != table()[index].method()->signature()) {
1502     fatal("mismatched name/signature of vtable entries");
1503   }
1504 }
1505 
1506 #ifndef PRODUCT
1507 void klassVtable::print() {
1508   ResourceMark rm;
1509   tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());
1510   for (int i = 0; i < length(); i++) {
1511     table()[i].print();
1512     tty->cr();
1513   }
1514 }
1515 #endif
1516 
1517 void vtableEntry::verify(klassVtable* vt, outputStream* st) {
1518   NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
1519   KlassHandle vtklass_h = vt->klass();
1520   Klass* vtklass = vtklass_h();
1521   if (vtklass->is_instance_klass() &&
1522      (InstanceKlass::cast(vtklass)->major_version() >= klassVtable::VTABLE_TRANSITIVE_OVERRIDE_VERSION)) {
1523     assert(method() != NULL, "must have set method");
1524   }
1525   if (method() != NULL) {
1526     method()->verify();
1527     // we sub_type, because it could be a miranda method
1528     if (!vtklass_h->is_subtype_of(method()->method_holder())) {
1529 #ifndef PRODUCT
1530       print();
1531 #endif
1532       fatal("vtableEntry " PTR_FORMAT ": method is from subclass", p2i(this));
1533     }
1534  }
1535 }
1536 
1537 #ifndef PRODUCT
1538 
1539 void vtableEntry::print() {
1540   ResourceMark rm;
1541   tty->print("vtableEntry %s: ", method()->name()->as_C_string());
1542   if (Verbose) {
1543     tty->print("m " PTR_FORMAT " ", p2i(method()));
1544   }
1545 }
1546 
1547 class VtableStats : AllStatic {
1548  public:




  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "gc/shared/gcLocker.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/metaspaceShared.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "memory/universe.inline.hpp"
  33 #include "oops/instanceKlass.hpp"
  34 #include "oops/klassVtable.hpp"
  35 #include "oops/method.hpp"
  36 #include "oops/objArrayOop.hpp"
  37 #include "oops/oop.inline.hpp"
  38 #include "runtime/arguments.hpp"
  39 #include "runtime/handles.inline.hpp"
  40 #include "utilities/copy.hpp"
  41 
  42 inline InstanceKlass* klassVtable::ik() const {
  43   return InstanceKlass::cast(_klass);
  44 }
  45 
  46 bool klassVtable::is_preinitialized_vtable() {
  47   return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();
  48 }
  49 
  50 
  51 // this function computes the vtable size (including the size needed for miranda
  52 // methods) and the number of miranda methods in this class.
  53 // Note on Miranda methods: Let's say there is a class C that implements
  54 // interface I, and none of C's superclasses implements I.
  55 // Let's say there is an abstract method m in I that neither C
  56 // nor any of its super classes implement (i.e there is no method of any access,
  57 // with the same name and signature as m), then m is a Miranda method which is
  58 // entered as a public abstract method in C's vtable.  From then on it should
  59 // treated as any other public method in C for method over-ride purposes.
  60 void klassVtable::compute_vtable_size_and_num_mirandas(
  61     int* vtable_length_ret, int* num_new_mirandas,
  62     GrowableArray<Method*>* all_mirandas, const Klass* super,
  63     Array<Method*>* methods, AccessFlags class_flags, u2 major_version,


 111       // SystemDictionary::resolve_from_stream(), which will detect this later
 112       // and throw a security exception.  So don't assert here to let
 113       // the exception occur.
 114       vtable_length = Universe::base_vtable_size();
 115     }
 116   }
 117   assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");
 118   assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");
 119 
 120   *vtable_length_ret = vtable_length;
 121 }
 122 
 123 int klassVtable::index_of(Method* m, int len) const {
 124   assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
 125   return m->vtable_index();
 126 }
 127 
 128 // Copy super class's vtable to the first part (prefix) of this class's vtable,
 129 // and return the number of entries copied.  Expects that 'super' is the Java
 130 // super class (arrays can have "array" super classes that must be skipped).
 131 int klassVtable::initialize_from_super(Klass* super) {
 132   if (super == NULL) {
 133     return 0;
 134   } else if (is_preinitialized_vtable()) {
 135     // A shared class' vtable is preinitialized at dump time. No need to copy
 136     // methods from super class for shared class, as that was already done
 137     // during archiving time. However, if Jvmti has redefined a class,
 138     // copy super class's vtable in case the super class has changed.
 139     return super->vtable()->length();
 140   } else {
 141     // copy methods from superKlass
 142     klassVtable* superVtable = super->vtable();
 143     assert(superVtable->length() <= _length, "vtable too short");
 144 #ifdef ASSERT
 145     superVtable->verify(tty, true);
 146 #endif
 147     superVtable->copy_vtable_to(table());
 148     if (log_develop_is_enabled(Trace, vtables)) {
 149       ResourceMark rm;
 150       log_develop_trace(vtables)("copy vtable from %s to %s size %d",
 151                                  super->internal_name(), klass()->internal_name(),
 152                                  _length);
 153     }
 154     return superVtable->length();
 155   }
 156 }
 157 
 158 //
 159 // Revised lookup semantics   introduced 1.3 (Kestrel beta)
 160 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
 161 
 162   // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
 163   Klass* super = _klass->java_super();
 164   int nofNewEntries = 0;
 165 
 166   bool is_shared = _klass->is_shared();
 167 
 168   if (!_klass->is_array_klass()) {
 169     ResourceMark rm(THREAD);
 170     log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
 171   }
 172 
 173 #ifdef ASSERT
 174   oop* end_of_obj = (oop*)_klass + _klass->size();
 175   oop* end_of_vtable = (oop*)&table()[_length];
 176   assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
 177 #endif
 178 
 179   if (Universe::is_bootstrapping()) {
 180     assert(!is_shared, "sanity");
 181     // just clear everything
 182     for (int i = 0; i < _length; i++) table()[i].clear();
 183     return;
 184   }
 185 
 186   int super_vtable_len = initialize_from_super(super);
 187   if (_klass->is_array_klass()) {
 188     assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
 189   } else {
 190     assert(_klass->is_instance_klass(), "must be InstanceKlass");
 191 
 192     Array<Method*>* methods = ik()->methods();
 193     int len = methods->length();
 194     int initialized = super_vtable_len;
 195 
 196     // Check each of this class's methods against super;
 197     // if override, replace in copy of super vtable, otherwise append to end
 198     for (int i = 0; i < len; i++) {
 199       // update_inherited_vtable can stop for gc - ensure using handles
 200       HandleMark hm(THREAD);
 201       assert(methods->at(i)->is_method(), "must be a Method*");
 202       methodHandle mh(THREAD, methods->at(i));
 203 
 204       bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK);
 205 
 206       if (needs_new_entry) {
 207         put_method_at(mh(), initialized);


 310           logst->print("overriders flags: ");
 311           target_method->print_linkage_flags(logst);
 312           logst->cr();
 313         }
 314 
 315         break; // return found superk
 316       }
 317     } else  {
 318       // super class has no vtable entry here, stop transitive search
 319       superk = (InstanceKlass*)NULL;
 320       break;
 321     }
 322     // if no override found yet, continue to search up
 323     superk = superk->super() == NULL ? NULL : InstanceKlass::cast(superk->super());
 324   }
 325 
 326   return superk;
 327 }
 328 
 329 static void log_vtables(int i, bool overrides, methodHandle target_method,
 330                         Klass* target_klass, Method* super_method,
 331                         Thread* thread) {
 332 #ifndef PRODUCT
 333   if (log_develop_is_enabled(Trace, vtables)) {
 334     ResourceMark rm(thread);
 335     outputStream* logst = Log(vtables)::trace_stream();
 336     char* sig = target_method()->name_and_sig_as_C_string();
 337     if (overrides) {
 338       logst->print("overriding with %s index %d, original flags: ",
 339                    sig, i);
 340     } else {
 341       logst->print("NOT overriding with %s index %d, original flags: ",
 342                    sig, i);
 343     }
 344     super_method->print_linkage_flags(logst);
 345     logst->print("overriders flags: ");
 346     target_method->print_linkage_flags(logst);
 347     logst->cr();
 348   }
 349 #endif
 350 }


 415   if (super == NULL) {
 416     return allocate_new;
 417   }
 418 
 419   // private methods in classes always have a new entry in the vtable
 420   // specification interpretation since classic has
 421   // private methods not overriding
 422   // JDK8 adds private methods in interfaces which require invokespecial
 423   if (target_method()->is_private()) {
 424     return allocate_new;
 425   }
 426 
 427   // search through the vtable and update overridden entries
 428   // Since check_signature_loaders acquires SystemDictionary_lock
 429   // which can block for gc, once we are in this loop, use handles
 430   // For classfiles built with >= jdk7, we now look for transitive overrides
 431 
 432   Symbol* name = target_method()->name();
 433   Symbol* signature = target_method()->signature();
 434 
 435   Klass* target_klass = target_method()->method_holder();
 436   if (target_klass == NULL) {
 437     target_klass = _klass;
 438   }
 439 
 440   Handle target_loader(THREAD, target_klass->class_loader());
 441 
 442   Symbol* target_classname = target_klass->name();
 443   for(int i = 0; i < super_vtable_len; i++) {
 444     Method* super_method;
 445     if (is_preinitialized_vtable()) {
 446       // If this is a shared class, the vtable is already in the final state (fully
 447       // initialized). Need to look at the super's vtable.
 448       klassVtable* superVtable = super->vtable();
 449       super_method = superVtable->method_at(i);
 450     } else {
 451       super_method = method_at(i);
 452     }
 453     // Check if method name matches
 454     if (super_method->name() == name && super_method->signature() == signature) {
 455 


 938     assert(!old_method->is_deleted(), "vtable methods may not be deleted");
 939 
 940     Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());
 941 
 942     assert(new_method != NULL, "method_with_idnum() should not be NULL");
 943     assert(old_method != new_method, "sanity check");
 944 
 945     put_method_at(new_method, index);
 946     // For default methods, need to update the _default_methods array
 947     // which can only have one method entry for a given signature
 948     bool updated_default = false;
 949     if (old_method->is_default_method()) {
 950       updated_default = adjust_default_method(index, old_method, new_method);
 951     }
 952 
 953     if (log_is_enabled(Info, redefine, class, update)) {
 954       ResourceMark rm;
 955       if (!(*trace_name_printed)) {
 956         log_info(redefine, class, update)
 957           ("adjust: klassname=%s for methods from name=%s",
 958            _klass->external_name(), old_method->method_holder()->external_name());
 959         *trace_name_printed = true;
 960       }
 961       log_debug(redefine, class, update, vtables)
 962         ("vtable method update: %s(%s), updated default = %s",
 963          new_method->name()->as_C_string(), new_method->signature()->as_C_string(), updated_default ? "true" : "false");
 964     }
 965   }
 966 }
 967 
 968 // a vtable should never contain old or obsolete methods
 969 bool klassVtable::check_no_old_or_obsolete_entries() {
 970   for (int i = 0; i < length(); i++) {
 971     Method* m = unchecked_method_at(i);
 972     if (m != NULL &&
 973         (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
 974       return false;
 975     }
 976   }
 977   return true;
 978 }


1008 }
1009 
1010 //-----------------------------------------------------------------------------------------
1011 // Itable code
1012 
1013 // Initialize a itableMethodEntry
1014 void itableMethodEntry::initialize(Method* m) {
1015   if (m == NULL) return;
1016 
1017   if (MetaspaceShared::is_in_shared_space((void*)&_method) &&
1018      !MetaspaceShared::remapped_readwrite()) {
1019     // At runtime initialize_itable is rerun as part of link_class_impl()
1020     // for a shared class loaded by the non-boot loader.
1021     // The dumptime itable method entry should be the same as the runtime entry.
1022     assert(_method == m, "sanity");
1023   } else {
1024     _method = m;
1025   }
1026 }
1027 
1028 klassItable::klassItable(InstanceKlass* klass) {
1029   _klass = klass;
1030 
1031   if (klass->itable_length() > 0) {
1032     itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
1033     if (offset_entry  != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
1034       // First offset entry points to the first method_entry
1035       intptr_t* method_entry  = (intptr_t *)(((address)klass) + offset_entry->offset());
1036       intptr_t* end         = klass->end_of_itable();
1037 
1038       _table_offset      = (intptr_t*)offset_entry - (intptr_t*)klass;
1039       _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
1040       _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
1041       assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
1042       return;
1043     }
1044   }
1045 
1046   // The length of the itable was either zero, or it has not yet been initialized.
1047   _table_offset      = 0;
1048   _size_offset_table = 0;
1049   _size_method_table = 0;
1050 }
1051 
1052 static int initialize_count = 0;
1053 
1054 // Initialization
1055 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1056   if (_klass->is_interface()) {
1057     // This needs to go after vtable indices are assigned but
1058     // before implementors need to know the number of itable indices.
1059     assign_itable_indices_for_interface(_klass);
1060   }
1061 
1062   // Cannot be setup doing bootstrapping, interfaces don't have
1063   // itables, and klass with only ones entry have empty itables
1064   if (Universe::is_bootstrapping() ||
1065       _klass->is_interface() ||
1066       _klass->itable_length() == itableOffsetEntry::size()) return;
1067 
1068   // There's alway an extra itable entry so we can null-terminate it.
1069   guarantee(size_offset_table() >= 1, "too small");
1070   int num_interfaces = size_offset_table() - 1;
1071   if (num_interfaces > 0) {
1072     log_develop_debug(itables)("%3d: Initializing itables for %s", ++initialize_count,
1073                        _klass->name()->as_C_string());
1074 
1075 
1076     // Iterate through all interfaces
1077     int i;
1078     for(i = 0; i < num_interfaces; i++) {
1079       itableOffsetEntry* ioe = offset_entry(i);
1080       HandleMark hm(THREAD);
1081       Klass* interf = ioe->interface_klass();
1082       assert(interf != NULL && ioe->offset() != 0, "bad offset entry in itable");
1083       initialize_itable_for_interface(ioe->offset(), interf, checkconstraints, CHECK);
1084     }
1085 
1086   }
1087   // Check that the last entry is empty
1088   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1089   guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1090 }
1091 
1092 
1093 inline bool interface_method_needs_itable_index(Method* m) {
1094   if (m->is_static())           return false;   // e.g., Stream.empty
1095   if (m->is_initializer())      return false;   // <init> or <clinit>
1096   if (m->is_private())          return false;   // requires invokeSpecial
1097   // If an interface redeclares a method from java.lang.Object,
1098   // it should already have a vtable index, don't touch it.
1099   // e.g., CharSequence.toString (from initialize_vtable)
1100   // if (m->has_vtable_index())  return false; // NO!
1101   return true;
1102 }
1103 


1152     Method* m = methods->at(nof_methods-1);
1153     if (m->has_itable_index()) {
1154       length = m->itable_index() + 1;
1155       break;
1156     }
1157     nof_methods -= 1;
1158   }
1159 #ifdef ASSERT
1160   int nof_methods_copy = nof_methods;
1161   while (nof_methods_copy > 0) {
1162     Method* mm = methods->at(--nof_methods_copy);
1163     assert(!mm->has_itable_index() || mm->itable_index() < length, "");
1164   }
1165 #endif //ASSERT
1166   // return the rightmost itable index, plus one; or 0 if no methods have
1167   // itable indices
1168   return length;
1169 }
1170 
1171 
1172 void klassItable::initialize_itable_for_interface(int method_table_offset, Klass* interf, bool checkconstraints, TRAPS) {
1173   Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1174   int nof_methods = methods->length();
1175   HandleMark hm;
1176   assert(nof_methods > 0, "at least one method must exist for interface to be in vtable");
1177   Handle interface_loader (THREAD, InstanceKlass::cast(interf)->class_loader());
1178 
1179   int ime_count = method_count_for_interface(interf);
1180   for (int i = 0; i < nof_methods; i++) {
1181     Method* m = methods->at(i);
1182     methodHandle target;
1183     if (m->has_itable_index()) {
1184       // This search must match the runtime resolution, i.e. selection search for invokeinterface
1185       // to correctly enforce loader constraints for interface method inheritance
1186       target = LinkResolver::lookup_instance_method_in_klasses(_klass, m->name(), m->signature(), CHECK);
1187     }
1188     if (target == NULL || !target->is_public() || target->is_abstract()) {
1189       // Entry does not resolve. Leave it empty for AbstractMethodError.
1190         if (!(target == NULL) && !target->is_public()) {
1191           // Stuff an IllegalAccessError throwing method in there instead.
1192           itableOffsetEntry::method_entry(_klass, method_table_offset)[m->itable_index()].
1193               initialize(Universe::throw_illegal_access_error());
1194         }
1195     } else {
1196       // Entry did resolve, check loader constraints before initializing
1197       // if checkconstraints requested
1198       if (checkconstraints) {
1199         Handle method_holder_loader (THREAD, target->method_holder()->class_loader());
1200         if (method_holder_loader() != interface_loader()) {
1201           ResourceMark rm(THREAD);
1202           Symbol* failed_type_symbol =
1203             SystemDictionary::check_signature_loaders(m->signature(),
1204                                                       method_holder_loader,
1205                                                       interface_loader,
1206                                                       true, CHECK);
1207           if (failed_type_symbol != NULL) {
1208             const char* msg = "loader constraint violation in interface "
1209               "itable initialization: when resolving method \"%s\" the class"
1210               " loader (instance of %s) of the current class, %s, "
1211               "and the class loader (instance of %s) for interface "
1212               "%s have different Class objects for the type %s "
1213               "used in the signature";
1214             char* sig = target()->name_and_sig_as_C_string();
1215             const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
1216             char* current = _klass->name()->as_C_string();
1217             const char* loader2 = SystemDictionary::loader_name(interface_loader());
1218             char* iface = InstanceKlass::cast(interf)->name()->as_C_string();
1219             char* failed_type_name = failed_type_symbol->as_C_string();
1220             size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
1221               strlen(current) + strlen(loader2) + strlen(iface) +
1222               strlen(failed_type_name);
1223             char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
1224             jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
1225                          iface, failed_type_name);
1226             THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
1227           }
1228         }
1229       }
1230 
1231       // ime may have moved during GC so recalculate address
1232       int ime_num = m->itable_index();
1233       assert(ime_num < ime_count, "oob");
1234       itableOffsetEntry::method_entry(_klass, method_table_offset)[ime_num].initialize(target());
1235       if (log_develop_is_enabled(Trace, itables)) {
1236         ResourceMark rm(THREAD);
1237         if (target() != NULL) {
1238           outputStream* logst = Log(itables)::trace_stream();
1239           char* sig = target()->name_and_sig_as_C_string();
1240           logst->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1241                        interf->internal_name(), ime_num, sig,
1242                        target()->method_holder()->internal_name());
1243           logst->print("target_method flags: ");
1244           target()->print_linkage_flags(logst);
1245           logst->cr();
1246         }
1247       }
1248     }
1249   }
1250 }
1251 
1252 // Update entry for specific Method*
1253 void klassItable::initialize_with_method(Method* m) {
1254   itableMethodEntry* ime = method_entry(0);
1255   for(int i = 0; i < _size_method_table; i++) {
1256     if (ime->method() == m) {
1257       ime->initialize(m);
1258     }
1259     ime++;
1260   }
1261 }


1391     _method_entry += method_count;
1392   }
1393 };
1394 
1395 int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) {
1396   // Count no of interfaces and total number of interface methods
1397   CountInterfacesClosure cic;
1398   visit_all_interfaces(transitive_interfaces, &cic);
1399 
1400   // There's alway an extra itable entry so we can null-terminate it.
1401   int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
1402 
1403   // Statistics
1404   update_stats(itable_size * wordSize);
1405 
1406   return itable_size;
1407 }
1408 
1409 
1410 // Fill out offset table and interface klasses into the itable space
1411 void klassItable::setup_itable_offset_table(InstanceKlass* klass) {
1412   if (klass->itable_length() == 0) return;
1413   assert(!klass->is_interface(), "Should have zero length itable");
1414 
1415   // Count no of interfaces and total number of interface methods
1416   CountInterfacesClosure cic;
1417   visit_all_interfaces(klass->transitive_interfaces(), &cic);
1418   int nof_methods    = cic.nof_methods();
1419   int nof_interfaces = cic.nof_interfaces();
1420 
1421   // Add one extra entry so we can null-terminate the table
1422   nof_interfaces++;
1423 
1424   assert(compute_itable_size(klass->transitive_interfaces()) ==
1425          calc_itable_size(nof_interfaces, nof_methods),
1426          "mismatch calculation of itable size");
1427 
1428   // Fill-out offset table
1429   itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
1430   itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
1431   intptr_t* end               = klass->end_of_itable();
1432   assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
1433   assert((oop*)(end) == (oop*)(ime + nof_methods),                      "wrong offset calculation (2)");
1434 
1435   // Visit all interfaces and initialize itable offset table
1436   SetupItableClosure sic((address)klass, ioe, ime);
1437   visit_all_interfaces(klass->transitive_interfaces(), &sic);
1438 
1439 #ifdef ASSERT
1440   ime  = sic.method_entry();
1441   oop* v = (oop*) klass->end_of_itable();
1442   assert( (oop*)(ime) == v, "wrong offset calculation (2)");
1443 #endif
1444 }
1445 
1446 
1447 // inverse to itable_index
1448 Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) {
1449   assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");
1450   assert(intf->verify_itable_index(itable_index), "");
1451   Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
1452 
1453   if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
1454     return NULL;                // help caller defend against bad indices
1455 
1456   int index = itable_index;


1459   while (!m->has_itable_index() ||
1460          (index2 = m->itable_index()) != itable_index) {
1461     assert(index2 < itable_index, "monotonic");
1462     if (++index == methods->length())
1463       return NULL;
1464     m = methods->at(index);
1465   }
1466   assert(m->itable_index() == itable_index, "correct inverse");
1467 
1468   return m;
1469 }
1470 
1471 void klassVtable::verify(outputStream* st, bool forced) {
1472   // make sure table is initialized
1473   if (!Universe::is_fully_initialized()) return;
1474 #ifndef PRODUCT
1475   // avoid redundant verifies
1476   if (!forced && _verify_count == Universe::verify_count()) return;
1477   _verify_count = Universe::verify_count();
1478 #endif
1479   oop* end_of_obj = (oop*)_klass + _klass->size();
1480   oop* end_of_vtable = (oop *)&table()[_length];
1481   if (end_of_vtable > end_of_obj) {
1482     fatal("klass %s: klass object too short (vtable extends beyond end)",
1483           _klass->internal_name());
1484   }
1485 
1486   for (int i = 0; i < _length; i++) table()[i].verify(this, st);
1487   // verify consistency with superKlass vtable
1488   Klass* super = _klass->super();
1489   if (super != NULL) {
1490     InstanceKlass* sk = InstanceKlass::cast(super);
1491     klassVtable* vt = sk->vtable();
1492     for (int i = 0; i < vt->length(); i++) {
1493       verify_against(st, vt, i);
1494     }
1495   }
1496 }
1497 
1498 void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
1499   vtableEntry* vte = &vt->table()[index];
1500   if (vte->method()->name()      != table()[index].method()->name() ||
1501       vte->method()->signature() != table()[index].method()->signature()) {
1502     fatal("mismatched name/signature of vtable entries");
1503   }
1504 }
1505 
1506 #ifndef PRODUCT
1507 void klassVtable::print() {
1508   ResourceMark rm;
1509   tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());
1510   for (int i = 0; i < length(); i++) {
1511     table()[i].print();
1512     tty->cr();
1513   }
1514 }
1515 #endif
1516 
1517 void vtableEntry::verify(klassVtable* vt, outputStream* st) {
1518   NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
1519   Klass* vtklass = vt->klass();

1520   if (vtklass->is_instance_klass() &&
1521      (InstanceKlass::cast(vtklass)->major_version() >= klassVtable::VTABLE_TRANSITIVE_OVERRIDE_VERSION)) {
1522     assert(method() != NULL, "must have set method");
1523   }
1524   if (method() != NULL) {
1525     method()->verify();
1526     // we sub_type, because it could be a miranda method
1527     if (!vtklass->is_subtype_of(method()->method_holder())) {
1528 #ifndef PRODUCT
1529       print();
1530 #endif
1531       fatal("vtableEntry " PTR_FORMAT ": method is from subclass", p2i(this));
1532     }
1533  }
1534 }
1535 
1536 #ifndef PRODUCT
1537 
1538 void vtableEntry::print() {
1539   ResourceMark rm;
1540   tty->print("vtableEntry %s: ", method()->name()->as_C_string());
1541   if (Verbose) {
1542     tty->print("m " PTR_FORMAT " ", p2i(method()));
1543   }
1544 }
1545 
1546 class VtableStats : AllStatic {
1547  public:


< prev index next >