< prev index next >

src/hotspot/share/oops/klassVtable.cpp

Print this page




 240             put_method_at(mh(), initialized);
 241             if (is_preinitialized_vtable()) {
 242               // At runtime initialize_vtable is rerun for a shared class
 243               // (loaded by the non-boot loader) as part of link_class_impl().
 244               // The dumptime vtable index should be the same as the runtime index.
 245               assert(def_vtable_indices->at(i) == initialized,
 246                      "dump time vtable index is different from runtime index");
 247             } else {
 248               def_vtable_indices->at_put(i, initialized); //set vtable index
 249             }
 250             initialized++;
 251           }
 252         }
 253       }
 254     }
 255 
 256     // add miranda methods; it will also return the updated initialized
 257     // Interfaces do not need interface methods in their vtables
 258     // This includes miranda methods and during later processing, default methods
 259     if (!ik()->is_interface()) {
 260       initialized = fill_in_mirandas(initialized);
 261     }
 262 
 263     // In class hierarchies where the accessibility is not increasing (i.e., going from private ->
 264     // package_private -> public/protected), the vtable might actually be smaller than our initial
 265     // calculation, for classfile versions for which we do not do transitive override
 266     // calculations.
 267     if (ik()->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) {
 268       assert(initialized == _length, "vtable initialization failed");
 269     } else {
 270       assert(initialized <= _length, "vtable initialization failed");
 271       for(;initialized < _length; initialized++) {
 272         table()[initialized].clear();
 273       }
 274     }
 275     NOT_PRODUCT(verify(tty, true));
 276   }
 277 }
 278 
 279 // Called for cases where a method does not override its superclass' vtable entry
 280 // For bytecodes not produced by javac together it is possible that a method does not override


 347                    sig, i);
 348     } else {
 349       ls.print("NOT overriding with %s index %d, original flags: ",
 350                    sig, i);
 351     }
 352     super_method->print_linkage_flags(&ls);
 353     ls.print("overriders flags: ");
 354     target_method->print_linkage_flags(&ls);
 355     ls.cr();
 356   }
 357 #endif
 358 }
 359 
 360 // Update child's copy of super vtable for overrides
 361 // OR return true if a new vtable entry is required.
 362 // Only called for InstanceKlass's, i.e. not for arrays
 363 // If that changed, could not use _klass as handle for klass
 364 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, const methodHandle& target_method,
 365                                           int super_vtable_len, int default_index,
 366                                           bool checkconstraints, TRAPS) {
 367   ResourceMark rm;
 368   bool allocate_new = true;
 369   assert(klass->is_instance_klass(), "must be InstanceKlass");
 370 
 371   Array<int>* def_vtable_indices = NULL;
 372   bool is_default = false;
 373 
 374   // default methods are non-private concrete methods in superinterfaces which are added
 375   // to the vtable with their real method_holder.
 376   // Since vtable and itable indices share the same storage, don't touch
 377   // the default method's real vtable/itable index.
 378   // default_vtable_indices stores the vtable value relative to this inheritor
 379   if (default_index >= 0 ) {
 380     is_default = true;
 381     def_vtable_indices = klass->default_vtable_indices();
 382     assert(!target_method()->is_private(), "private interface method flagged as default");
 383     assert(def_vtable_indices != NULL, "def vtable alloc?");
 384     assert(default_index <= def_vtable_indices->length(), "def vtable len?");
 385   } else {
 386     assert(klass == target_method()->method_holder(), "caller resp.");
 387     // Initialize the method's vtable index to "nonvirtual".


 885     add_new_mirandas_to_lists(new_mirandas, all_mirandas,
 886                               ik->methods(), class_methods,
 887                               default_methods, super, is_interface);
 888     // iterate thru each local's super interfaces
 889     Array<Klass*>* super_ifs = ik->transitive_interfaces();
 890     int num_super_ifs = super_ifs->length();
 891     for (int j = 0; j < num_super_ifs; j++) {
 892       InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
 893       add_new_mirandas_to_lists(new_mirandas, all_mirandas,
 894                                 sik->methods(), class_methods,
 895                                 default_methods, super, is_interface);
 896     }
 897   }
 898 }
 899 
 900 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
 901 // and append them into the vtable starting at index initialized,
 902 // return the new value of initialized.
 903 // Miranda methods use vtable entries, but do not get assigned a vtable_index
 904 // The vtable_index is discovered by searching from the end of the vtable
 905 int klassVtable::fill_in_mirandas(int initialized) {

 906   GrowableArray<Method*> mirandas(20);
 907   get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
 908                ik()->default_methods(), ik()->local_interfaces(),
 909                klass()->is_interface());
 910   for (int i = 0; i < mirandas.length(); i++) {
 911     if (log_develop_is_enabled(Trace, vtables)) {
 912       Method* meth = mirandas.at(i);
 913       ResourceMark rm(Thread::current());
 914       LogTarget(Trace, vtables) lt;
 915       LogStream ls(lt);
 916       if (meth != NULL) {
 917         char* sig = meth->name_and_sig_as_C_string();
 918         ls.print("fill in mirandas with %s index %d, flags: ",
 919                      sig, initialized);
 920         meth->print_linkage_flags(&ls);
 921         ls.cr();
 922       }
 923     }
 924     put_method_at(mirandas.at(i), initialized);
 925     ++initialized;
 926   }
 927   return initialized;
 928 }
 929 
 930 // Copy this class's vtable to the vtable beginning at start.
 931 // Used to copy superclass vtable to prefix of subclass's vtable.
 932 void klassVtable::copy_vtable_to(vtableEntry* start) {
 933   Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());


1068       _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
1069       _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
1070       assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
1071       return;
1072     }
1073   }
1074 
1075   // The length of the itable was either zero, or it has not yet been initialized.
1076   _table_offset      = 0;
1077   _size_offset_table = 0;
1078   _size_method_table = 0;
1079 }
1080 
1081 static int initialize_count = 0;
1082 
1083 // Initialization
1084 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1085   if (_klass->is_interface()) {
1086     // This needs to go after vtable indices are assigned but
1087     // before implementors need to know the number of itable indices.
1088     assign_itable_indices_for_interface(_klass);
1089   }
1090 
1091   // Cannot be setup doing bootstrapping, interfaces don't have
1092   // itables, and klass with only ones entry have empty itables
1093   if (Universe::is_bootstrapping() ||
1094       _klass->is_interface() ||
1095       _klass->itable_length() == itableOffsetEntry::size()) return;
1096 
1097   // There's alway an extra itable entry so we can null-terminate it.
1098   guarantee(size_offset_table() >= 1, "too small");
1099   int num_interfaces = size_offset_table() - 1;
1100   if (num_interfaces > 0) {

1101     log_develop_debug(itables)("%3d: Initializing itables for %s", ++initialize_count,
1102                        _klass->name()->as_C_string());
1103 
1104 
1105     // Iterate through all interfaces
1106     int i;
1107     for(i = 0; i < num_interfaces; i++) {
1108       itableOffsetEntry* ioe = offset_entry(i);
1109       HandleMark hm(THREAD);
1110       Klass* interf = ioe->interface_klass();
1111       assert(interf != NULL && ioe->offset() != 0, "bad offset entry in itable");
1112       initialize_itable_for_interface(ioe->offset(), interf, checkconstraints, CHECK);
1113     }
1114 
1115   }
1116   // Check that the last entry is empty
1117   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1118   guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1119 }
1120 
1121 
1122 inline bool interface_method_needs_itable_index(Method* m) {
1123   if (m->is_static())           return false;   // e.g., Stream.empty
1124   if (m->is_initializer())      return false;   // <init> or <clinit>
1125   if (m->is_private())          return false;   // uses direct call
1126   // If an interface redeclares a method from java.lang.Object,
1127   // it should already have a vtable index, don't touch it.
1128   // e.g., CharSequence.toString (from initialize_vtable)
1129   // if (m->has_vtable_index())  return false; // NO!
1130   return true;
1131 }
1132 
1133 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1134   // an interface does not have an itable, but its methods need to be numbered

1135   log_develop_debug(itables)("%3d: Initializing itable indices for interface %s",
1136                              ++initialize_count, klass->name()->as_C_string());
1137   Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1138   int nof_methods = methods->length();
1139   int ime_num = 0;
1140   for (int i = 0; i < nof_methods; i++) {
1141     Method* m = methods->at(i);
1142     if (interface_method_needs_itable_index(m)) {
1143       assert(!m->is_final_method(), "no final interface methods");
1144       // If m is already assigned a vtable index, do not disturb it.
1145       if (log_develop_is_enabled(Trace, itables)) {
1146         ResourceMark rm;
1147         LogTarget(Trace, itables) lt;
1148         LogStream ls(lt);
1149         assert(m != NULL, "methods can never be null");
1150         const char* sig = m->name_and_sig_as_C_string();
1151         if (m->has_vtable_index()) {
1152           ls.print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1153         } else {
1154           ls.print("itable index %d for method: %s, flags: ", ime_num, sig);
1155         }
1156         m->print_linkage_flags(&ls);
1157         ls.cr();
1158       }
1159       if (!m->has_vtable_index()) {
1160         // A shared method could have an initialized itable_index that
1161         // is < 0.
1162         assert(m->vtable_index() == Method::pending_itable_index ||
1163                m->is_shared(),
1164                "set by initialize_vtable");
1165         m->set_itable_index(ime_num);
1166         // Progress to next itable entry


1490     if (++index == methods->length())
1491       return NULL;
1492     m = methods->at(index);
1493   }
1494   assert(m->itable_index() == itable_index, "correct inverse");
1495 
1496   return m;
1497 }
1498 
1499 void klassVtable::verify(outputStream* st, bool forced) {
1500   // make sure table is initialized
1501   if (!Universe::is_fully_initialized()) return;
1502 #ifndef PRODUCT
1503   // avoid redundant verifies
1504   if (!forced && _verify_count == Universe::verify_count()) return;
1505   _verify_count = Universe::verify_count();
1506 #endif
1507   oop* end_of_obj = (oop*)_klass + _klass->size();
1508   oop* end_of_vtable = (oop *)&table()[_length];
1509   if (end_of_vtable > end_of_obj) {

1510     fatal("klass %s: klass object too short (vtable extends beyond end)",
1511           _klass->internal_name());
1512   }
1513 
1514   for (int i = 0; i < _length; i++) table()[i].verify(this, st);
1515   // verify consistency with superKlass vtable
1516   Klass* super = _klass->super();
1517   if (super != NULL) {
1518     InstanceKlass* sk = InstanceKlass::cast(super);
1519     klassVtable vt = sk->vtable();
1520     for (int i = 0; i < vt.length(); i++) {
1521       verify_against(st, &vt, i);
1522     }
1523   }
1524 }
1525 
1526 void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
1527   vtableEntry* vte = &vt->table()[index];
1528   if (vte->method()->name()      != table()[index].method()->name() ||
1529       vte->method()->signature() != table()[index].method()->signature()) {




 240             put_method_at(mh(), initialized);
 241             if (is_preinitialized_vtable()) {
 242               // At runtime initialize_vtable is rerun for a shared class
 243               // (loaded by the non-boot loader) as part of link_class_impl().
 244               // The dumptime vtable index should be the same as the runtime index.
 245               assert(def_vtable_indices->at(i) == initialized,
 246                      "dump time vtable index is different from runtime index");
 247             } else {
 248               def_vtable_indices->at_put(i, initialized); //set vtable index
 249             }
 250             initialized++;
 251           }
 252         }
 253       }
 254     }
 255 
 256     // add miranda methods; it will also return the updated initialized
 257     // Interfaces do not need interface methods in their vtables
 258     // This includes miranda methods and during later processing, default methods
 259     if (!ik()->is_interface()) {
 260       initialized = fill_in_mirandas(initialized, THREAD);
 261     }
 262 
 263     // In class hierarchies where the accessibility is not increasing (i.e., going from private ->
 264     // package_private -> public/protected), the vtable might actually be smaller than our initial
 265     // calculation, for classfile versions for which we do not do transitive override
 266     // calculations.
 267     if (ik()->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) {
 268       assert(initialized == _length, "vtable initialization failed");
 269     } else {
 270       assert(initialized <= _length, "vtable initialization failed");
 271       for(;initialized < _length; initialized++) {
 272         table()[initialized].clear();
 273       }
 274     }
 275     NOT_PRODUCT(verify(tty, true));
 276   }
 277 }
 278 
 279 // Called for cases where a method does not override its superclass' vtable entry
 280 // For bytecodes not produced by javac together it is possible that a method does not override


 347                    sig, i);
 348     } else {
 349       ls.print("NOT overriding with %s index %d, original flags: ",
 350                    sig, i);
 351     }
 352     super_method->print_linkage_flags(&ls);
 353     ls.print("overriders flags: ");
 354     target_method->print_linkage_flags(&ls);
 355     ls.cr();
 356   }
 357 #endif
 358 }
 359 
 360 // Update child's copy of super vtable for overrides
 361 // OR return true if a new vtable entry is required.
 362 // Only called for InstanceKlass's, i.e. not for arrays
 363 // If that changed, could not use _klass as handle for klass
 364 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, const methodHandle& target_method,
 365                                           int super_vtable_len, int default_index,
 366                                           bool checkconstraints, TRAPS) {
 367   ResourceMark rm(THREAD);
 368   bool allocate_new = true;
 369   assert(klass->is_instance_klass(), "must be InstanceKlass");
 370 
 371   Array<int>* def_vtable_indices = NULL;
 372   bool is_default = false;
 373 
 374   // default methods are non-private concrete methods in superinterfaces which are added
 375   // to the vtable with their real method_holder.
 376   // Since vtable and itable indices share the same storage, don't touch
 377   // the default method's real vtable/itable index.
 378   // default_vtable_indices stores the vtable value relative to this inheritor
 379   if (default_index >= 0 ) {
 380     is_default = true;
 381     def_vtable_indices = klass->default_vtable_indices();
 382     assert(!target_method()->is_private(), "private interface method flagged as default");
 383     assert(def_vtable_indices != NULL, "def vtable alloc?");
 384     assert(default_index <= def_vtable_indices->length(), "def vtable len?");
 385   } else {
 386     assert(klass == target_method()->method_holder(), "caller resp.");
 387     // Initialize the method's vtable index to "nonvirtual".


 885     add_new_mirandas_to_lists(new_mirandas, all_mirandas,
 886                               ik->methods(), class_methods,
 887                               default_methods, super, is_interface);
 888     // iterate thru each local's super interfaces
 889     Array<Klass*>* super_ifs = ik->transitive_interfaces();
 890     int num_super_ifs = super_ifs->length();
 891     for (int j = 0; j < num_super_ifs; j++) {
 892       InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
 893       add_new_mirandas_to_lists(new_mirandas, all_mirandas,
 894                                 sik->methods(), class_methods,
 895                                 default_methods, super, is_interface);
 896     }
 897   }
 898 }
 899 
 900 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
 901 // and append them into the vtable starting at index initialized,
 902 // return the new value of initialized.
 903 // Miranda methods use vtable entries, but do not get assigned a vtable_index
 904 // The vtable_index is discovered by searching from the end of the vtable
 905 int klassVtable::fill_in_mirandas(int initialized, TRAPS) {
 906   ResourceMark rm(THREAD);
 907   GrowableArray<Method*> mirandas(20);
 908   get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
 909                ik()->default_methods(), ik()->local_interfaces(),
 910                klass()->is_interface());
 911   for (int i = 0; i < mirandas.length(); i++) {
 912     if (log_develop_is_enabled(Trace, vtables)) {
 913       Method* meth = mirandas.at(i);

 914       LogTarget(Trace, vtables) lt;
 915       LogStream ls(lt);
 916       if (meth != NULL) {
 917         char* sig = meth->name_and_sig_as_C_string();
 918         ls.print("fill in mirandas with %s index %d, flags: ",
 919                      sig, initialized);
 920         meth->print_linkage_flags(&ls);
 921         ls.cr();
 922       }
 923     }
 924     put_method_at(mirandas.at(i), initialized);
 925     ++initialized;
 926   }
 927   return initialized;
 928 }
 929 
 930 // Copy this class's vtable to the vtable beginning at start.
 931 // Used to copy superclass vtable to prefix of subclass's vtable.
 932 void klassVtable::copy_vtable_to(vtableEntry* start) {
 933   Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());


1068       _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
1069       _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
1070       assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
1071       return;
1072     }
1073   }
1074 
1075   // The length of the itable was either zero, or it has not yet been initialized.
1076   _table_offset      = 0;
1077   _size_offset_table = 0;
1078   _size_method_table = 0;
1079 }
1080 
1081 static int initialize_count = 0;
1082 
1083 // Initialization
1084 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1085   if (_klass->is_interface()) {
1086     // This needs to go after vtable indices are assigned but
1087     // before implementors need to know the number of itable indices.
1088     assign_itable_indices_for_interface(_klass, THREAD);
1089   }
1090 
1091   // Cannot be setup doing bootstrapping, interfaces don't have
1092   // itables, and klass with only ones entry have empty itables
1093   if (Universe::is_bootstrapping() ||
1094       _klass->is_interface() ||
1095       _klass->itable_length() == itableOffsetEntry::size()) return;
1096 
1097   // There's alway an extra itable entry so we can null-terminate it.
1098   guarantee(size_offset_table() >= 1, "too small");
1099   int num_interfaces = size_offset_table() - 1;
1100   if (num_interfaces > 0) {
1101     ResourceMark rm(THREAD);
1102     log_develop_debug(itables)("%3d: Initializing itables for %s", ++initialize_count,
1103                        _klass->name()->as_C_string());
1104 
1105 
1106     // Iterate through all interfaces
1107     int i;
1108     for(i = 0; i < num_interfaces; i++) {
1109       itableOffsetEntry* ioe = offset_entry(i);
1110       HandleMark hm(THREAD);
1111       Klass* interf = ioe->interface_klass();
1112       assert(interf != NULL && ioe->offset() != 0, "bad offset entry in itable");
1113       initialize_itable_for_interface(ioe->offset(), interf, checkconstraints, CHECK);
1114     }
1115 
1116   }
1117   // Check that the last entry is empty
1118   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1119   guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1120 }
1121 
1122 
1123 inline bool interface_method_needs_itable_index(Method* m) {
1124   if (m->is_static())           return false;   // e.g., Stream.empty
1125   if (m->is_initializer())      return false;   // <init> or <clinit>
1126   if (m->is_private())          return false;   // uses direct call
1127   // If an interface redeclares a method from java.lang.Object,
1128   // it should already have a vtable index, don't touch it.
1129   // e.g., CharSequence.toString (from initialize_vtable)
1130   // if (m->has_vtable_index())  return false; // NO!
1131   return true;
1132 }
1133 
1134 int klassItable::assign_itable_indices_for_interface(Klass* klass, TRAPS) {
1135   // an interface does not have an itable, but its methods need to be numbered
1136   ResourceMark rm(THREAD);
1137   log_develop_debug(itables)("%3d: Initializing itable indices for interface %s",
1138                              ++initialize_count, klass->name()->as_C_string());
1139   Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1140   int nof_methods = methods->length();
1141   int ime_num = 0;
1142   for (int i = 0; i < nof_methods; i++) {
1143     Method* m = methods->at(i);
1144     if (interface_method_needs_itable_index(m)) {
1145       assert(!m->is_final_method(), "no final interface methods");
1146       // If m is already assigned a vtable index, do not disturb it.
1147       if (log_develop_is_enabled(Trace, itables)) {

1148         LogTarget(Trace, itables) lt;
1149         LogStream ls(lt);
1150         assert(m != NULL, "methods can never be null");
1151         const char* sig = m->name_and_sig_as_C_string();
1152         if (m->has_vtable_index()) {
1153           ls.print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1154         } else {
1155           ls.print("itable index %d for method: %s, flags: ", ime_num, sig);
1156         }
1157         m->print_linkage_flags(&ls);
1158         ls.cr();
1159       }
1160       if (!m->has_vtable_index()) {
1161         // A shared method could have an initialized itable_index that
1162         // is < 0.
1163         assert(m->vtable_index() == Method::pending_itable_index ||
1164                m->is_shared(),
1165                "set by initialize_vtable");
1166         m->set_itable_index(ime_num);
1167         // Progress to next itable entry


1491     if (++index == methods->length())
1492       return NULL;
1493     m = methods->at(index);
1494   }
1495   assert(m->itable_index() == itable_index, "correct inverse");
1496 
1497   return m;
1498 }
1499 
1500 void klassVtable::verify(outputStream* st, bool forced) {
1501   // make sure table is initialized
1502   if (!Universe::is_fully_initialized()) return;
1503 #ifndef PRODUCT
1504   // avoid redundant verifies
1505   if (!forced && _verify_count == Universe::verify_count()) return;
1506   _verify_count = Universe::verify_count();
1507 #endif
1508   oop* end_of_obj = (oop*)_klass + _klass->size();
1509   oop* end_of_vtable = (oop *)&table()[_length];
1510   if (end_of_vtable > end_of_obj) {
1511     ResourceMark rm;
1512     fatal("klass %s: klass object too short (vtable extends beyond end)",
1513           _klass->internal_name());
1514   }
1515 
1516   for (int i = 0; i < _length; i++) table()[i].verify(this, st);
1517   // verify consistency with superKlass vtable
1518   Klass* super = _klass->super();
1519   if (super != NULL) {
1520     InstanceKlass* sk = InstanceKlass::cast(super);
1521     klassVtable vt = sk->vtable();
1522     for (int i = 0; i < vt.length(); i++) {
1523       verify_against(st, &vt, i);
1524     }
1525   }
1526 }
1527 
1528 void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
1529   vtableEntry* vte = &vt->table()[index];
1530   if (vte->method()->name()      != table()[index].method()->name() ||
1531       vte->method()->signature() != table()[index].method()->signature()) {


< prev index next >