src/share/vm/oops/klassVtable.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8141564.01 Sdiff src/share/vm/oops

src/share/vm/oops/klassVtable.cpp

Print this page




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "classfile/vmSymbols.hpp"
  28 #include "gc/shared/gcLocker.hpp"

  29 #include "memory/resourceArea.hpp"
  30 #include "memory/universe.inline.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/klassVtable.hpp"
  33 #include "oops/method.hpp"
  34 #include "oops/objArrayOop.hpp"
  35 #include "oops/oop.inline.hpp"
  36 #include "prims/jvmtiRedefineClassesTrace.hpp"
  37 #include "runtime/arguments.hpp"
  38 #include "runtime/handles.inline.hpp"
  39 #include "utilities/copy.hpp"
  40 
  41 inline InstanceKlass* klassVtable::ik() const {
  42   return InstanceKlass::cast(_klass());
  43 }
  44 
  45 
  46 // this function computes the vtable size (including the size needed for miranda
  47 // methods) and the number of miranda methods in this class.
  48 // Note on Miranda methods: Let's say there is a class C that implements


 117 
 118 int klassVtable::index_of(Method* m, int len) const {
 119   assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
 120   return m->vtable_index();
 121 }
 122 
 123 // Copy super class's vtable to the first part (prefix) of this class's vtable,
 124 // and return the number of entries copied.  Expects that 'super' is the Java
 125 // super class (arrays can have "array" super classes that must be skipped).
 126 int klassVtable::initialize_from_super(KlassHandle super) {
 127   if (super.is_null()) {
 128     return 0;
 129   } else {
 130     // copy methods from superKlass
 131     klassVtable* superVtable = super->vtable();
 132     assert(superVtable->length() <= _length, "vtable too short");
 133 #ifdef ASSERT
 134     superVtable->verify(tty, true);
 135 #endif
 136     superVtable->copy_vtable_to(table());
 137 #ifndef PRODUCT
 138     if (PrintVtables && Verbose) {
 139       ResourceMark rm;
 140       tty->print_cr("copy vtable from %s to %s size %d", super->internal_name(), klass()->internal_name(), _length);
 141     }
 142 #endif
 143     return superVtable->length();
 144   }
 145 }
 146 
 147 //
 148 // Revised lookup semantics   introduced 1.3 (Kestrel beta)
 149 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
 150 
 151   // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
 152   KlassHandle super (THREAD, klass()->java_super());
 153   int nofNewEntries = 0;
 154 
 155   if (PrintVtables && !klass()->is_array_klass()) {
 156     ResourceMark rm(THREAD);
 157     tty->print_cr("Initializing: %s", _klass->name()->as_C_string());
 158   }
 159 
 160 #ifdef ASSERT
 161   oop* end_of_obj = (oop*)_klass() + _klass()->size();
 162   oop* end_of_vtable = (oop*)&table()[_length];
 163   assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
 164 #endif
 165 
 166   if (Universe::is_bootstrapping()) {
 167     // just clear everything
 168     for (int i = 0; i < _length; i++) table()[i].clear();
 169     return;
 170   }
 171 
 172   int super_vtable_len = initialize_from_super(super);
 173   if (klass()->is_array_klass()) {
 174     assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
 175   } else {
 176     assert(_klass->is_instance_klass(), "must be InstanceKlass");
 177 


 254 // P2.B extends A, package private m
 255 // P1.C extends B, public m
 256 // P1.C.m needs to override P1.A.m and can not override P2.B.m
 257 // Therefore: all package private methods need their own vtable entries for
 258 // them to be the root of an inheritance overriding decision
 259 // Package private methods may also override other vtable entries
 260 InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method,
 261                             int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) {
 262   InstanceKlass* superk = initialsuper;
 263   while (superk != NULL && superk->super() != NULL) {
 264     InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super());
 265     klassVtable* ssVtable = supersuperklass->vtable();
 266     if (vtable_index < ssVtable->length()) {
 267       Method* super_method = ssVtable->method_at(vtable_index);
 268 #ifndef PRODUCT
 269       Symbol* name= target_method()->name();
 270       Symbol* signature = target_method()->signature();
 271       assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
 272 #endif
 273       if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) {
 274 #ifndef PRODUCT
 275         if (PrintVtables && Verbose) {
 276           ResourceMark rm(THREAD);

 277           char* sig = target_method()->name_and_sig_as_C_string();
 278           tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
 279            supersuperklass->internal_name(),
 280            _klass->internal_name(), sig, vtable_index);
 281            super_method->access_flags().print_on(tty);
 282            if (super_method->is_default_method()) {
 283              tty->print("default ");

 284            }
 285            tty->print("overriders flags: ");
 286            target_method->access_flags().print_on(tty);
 287            if (target_method->is_default_method()) {
 288              tty->print("default ");
 289            }
 290         }
 291 #endif /*PRODUCT*/
 292         break; // return found superk
 293       }
 294     } else  {
 295       // super class has no vtable entry here, stop transitive search
 296       superk = (InstanceKlass*)NULL;
 297       break;
 298     }
 299     // if no override found yet, continue to search up
 300     superk = superk->super() == NULL ? NULL : InstanceKlass::cast(superk->super());
 301   }
 302 
 303   return superk;
 304 }
 305 
























 306 // Update child's copy of super vtable for overrides
 307 // OR return true if a new vtable entry is required.
 308 // Only called for InstanceKlass's, i.e. not for arrays
 309 // If that changed, could not use _klass as handle for klass
 310 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
 311                                           int super_vtable_len, int default_index,
 312                                           bool checkconstraints, TRAPS) {
 313   ResourceMark rm;
 314   bool allocate_new = true;
 315   assert(klass->is_instance_klass(), "must be InstanceKlass");
 316 
 317   Array<int>* def_vtable_indices = NULL;
 318   bool is_default = false;
 319   // default methods are concrete methods in superinterfaces which are added to the vtable
 320   // with their real method_holder
 321   // Since vtable and itable indices share the same storage, don't touch
 322   // the default method's real vtable/itable index
 323   // default_vtable_indices stores the vtable value relative to this inheritor
 324   if (default_index >= 0 ) {
 325     is_default = true;


 441               size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
 442                 strlen(current) + strlen(loader2) + strlen(failed_type_name);
 443               char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 444               jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
 445                            failed_type_name);
 446               THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
 447             }
 448           }
 449        }
 450 
 451        put_method_at(target_method(), i);
 452        if (!is_default) {
 453          target_method()->set_vtable_index(i);
 454        } else {
 455          if (def_vtable_indices != NULL) {
 456            def_vtable_indices->at_put(default_index, i);
 457          }
 458          assert(super_method->is_default_method() || super_method->is_overpass()
 459                 || super_method->is_abstract(), "default override error");
 460        }
 461 
 462 
 463 #ifndef PRODUCT
 464         if (PrintVtables && Verbose) {
 465           ResourceMark rm(THREAD);
 466           char* sig = target_method()->name_and_sig_as_C_string();
 467           tty->print("overriding with %s::%s index %d, original flags: ",
 468            target_klass->internal_name(), sig, i);
 469            super_method->access_flags().print_on(tty);
 470            if (super_method->is_default_method()) {
 471              tty->print("default ");
 472            }
 473            if (super_method->is_overpass()) {
 474              tty->print("overpass");
 475            }
 476            tty->print("overriders flags: ");
 477            target_method->access_flags().print_on(tty);
 478            if (target_method->is_default_method()) {
 479              tty->print("default ");
 480            }
 481            if (target_method->is_overpass()) {
 482              tty->print("overpass");
 483            }
 484            tty->cr();
 485         }
 486 #endif /*PRODUCT*/
 487       } else {
 488         // allocate_new = true; default. We might override one entry,
 489         // but not override another. Once we override one, not need new
 490 #ifndef PRODUCT
 491         if (PrintVtables && Verbose) {
 492           ResourceMark rm(THREAD);
 493           char* sig = target_method()->name_and_sig_as_C_string();
 494           tty->print("NOT overriding with %s::%s index %d, original flags: ",
 495            target_klass->internal_name(), sig,i);
 496            super_method->access_flags().print_on(tty);
 497            if (super_method->is_default_method()) {
 498              tty->print("default ");
 499            }
 500            if (super_method->is_overpass()) {
 501              tty->print("overpass");
 502            }
 503            tty->print("overriders flags: ");
 504            target_method->access_flags().print_on(tty);
 505            if (target_method->is_default_method()) {
 506              tty->print("default ");
 507            }
 508            if (target_method->is_overpass()) {
 509              tty->print("overpass");
 510            }
 511            tty->cr();
 512         }
 513 #endif /*PRODUCT*/
 514       }

 515     }
 516   }
 517   return allocate_new;
 518 }
 519 
 520 void klassVtable::put_method_at(Method* m, int index) {
 521 #ifndef PRODUCT
 522   if (PrintVtables && Verbose) {
 523     ResourceMark rm;

 524     const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
 525     tty->print("adding %s at index %d, flags: ", sig, index);
 526     if (m != NULL) {
 527       m->access_flags().print_on(tty);
 528       if (m->is_default_method()) {
 529         tty->print("default ");
 530       }
 531       if (m->is_overpass()) {
 532         tty->print("overpass");
 533       }

 534     }
 535     tty->cr();
 536   }
 537 #endif
 538   table()[index].set(m);
 539 }
 540 
 541 // Find out if a method "m" with superclass "super", loader "classloader" and
 542 // name "classname" needs a new vtable entry.  Let P be a class package defined
 543 // by "classloader" and "classname".
 544 // NOTE: The logic used here is very similar to the one used for computing
 545 // the vtables indices for a method. We cannot directly use that function because,
 546 // we allocate the InstanceKlass at load time, and that requires that the
 547 // superclass has been loaded.
 548 // However, the vtable entries are filled in at link time, and therefore
 549 // the superclass' vtable may not yet have been filled in.
 550 bool klassVtable::needs_new_vtable_entry(methodHandle target_method,
 551                                          Klass* super,
 552                                          Handle classloader,
 553                                          Symbol* classname,
 554                                          AccessFlags class_flags,
 555                                          TRAPS) {
 556   if (class_flags.is_interface()) {
 557     // Interfaces do not use vtables, except for java.lang.Object methods,


 834     int num_super_ifs = super_ifs->length();
 835     for (int j = 0; j < num_super_ifs; j++) {
 836       InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
 837       add_new_mirandas_to_lists(new_mirandas, all_mirandas,
 838                                 sik->methods(), class_methods,
 839                                 default_methods, super);
 840     }
 841   }
 842 }
 843 
 844 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
 845 // and append them into the vtable starting at index initialized,
 846 // return the new value of initialized.
 847 // Miranda methods use vtable entries, but do not get assigned a vtable_index
 848 // The vtable_index is discovered by searching from the end of the vtable
 849 int klassVtable::fill_in_mirandas(int initialized) {
 850   GrowableArray<Method*> mirandas(20);
 851   get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
 852                ik()->default_methods(), ik()->local_interfaces());
 853   for (int i = 0; i < mirandas.length(); i++) {
 854     if (PrintVtables && Verbose) {
 855       Method* meth = mirandas.at(i);
 856       ResourceMark rm(Thread::current());

 857       if (meth != NULL) {
 858         char* sig = meth->name_and_sig_as_C_string();
 859         tty->print("fill in mirandas with %s index %d, flags: ",
 860           sig, initialized);
 861         meth->access_flags().print_on(tty);
 862         if (meth->is_default_method()) {
 863           tty->print("default ");
 864         }
 865         tty->cr();
 866       }
 867     }
 868     put_method_at(mirandas.at(i), initialized);
 869     ++initialized;
 870   }
 871   return initialized;
 872 }
 873 
 874 // Copy this class's vtable to the vtable beginning at start.
 875 // Used to copy superclass vtable to prefix of subclass's vtable.
 876 void klassVtable::copy_vtable_to(vtableEntry* start) {
 877   Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
 878 }
 879 
 880 #if INCLUDE_JVMTI
 881 bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
 882   // If old_method is default, find this vtable index in default_vtable_indices
 883   // and replace that method in the _default_methods list
 884   bool updated = false;
 885 


1018 static int initialize_count = 0;
1019 
1020 // Initialization
1021 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
1022   if (_klass->is_interface()) {
1023     // This needs to go after vtable indices are assigned but
1024     // before implementors need to know the number of itable indices.
1025     assign_itable_indices_for_interface(_klass());
1026   }
1027 
1028   // Cannot be setup doing bootstrapping, interfaces don't have
1029   // itables, and klass with only ones entry have empty itables
1030   if (Universe::is_bootstrapping() ||
1031       _klass->is_interface() ||
1032       _klass->itable_length() == itableOffsetEntry::size()) return;
1033 
1034   // There's alway an extra itable entry so we can null-terminate it.
1035   guarantee(size_offset_table() >= 1, "too small");
1036   int num_interfaces = size_offset_table() - 1;
1037   if (num_interfaces > 0) {
1038     if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count,
1039                                     _klass->name()->as_C_string());
1040 
1041 
1042     // Iterate through all interfaces
1043     int i;
1044     for(i = 0; i < num_interfaces; i++) {
1045       itableOffsetEntry* ioe = offset_entry(i);
1046       HandleMark hm(THREAD);
1047       KlassHandle interf_h (THREAD, ioe->interface_klass());
1048       assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1049       initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1050     }
1051 
1052   }
1053   // Check that the last entry is empty
1054   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1055   guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1056 }
1057 
1058 
1059 inline bool interface_method_needs_itable_index(Method* m) {
1060   if (m->is_static())           return false;   // e.g., Stream.empty
1061   if (m->is_initializer())      return false;   // <init> or <clinit>
1062   // If an interface redeclares a method from java.lang.Object,
1063   // it should already have a vtable index, don't touch it.
1064   // e.g., CharSequence.toString (from initialize_vtable)
1065   // if (m->has_vtable_index())  return false; // NO!
1066   return true;
1067 }
1068 
1069 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1070   // an interface does not have an itable, but its methods need to be numbered
1071   if (TraceItables) tty->print_cr("%3d: Initializing itable indices for interface %s", ++initialize_count,
1072                                   klass->name()->as_C_string());
1073   Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1074   int nof_methods = methods->length();
1075   int ime_num = 0;
1076   for (int i = 0; i < nof_methods; i++) {
1077     Method* m = methods->at(i);
1078     if (interface_method_needs_itable_index(m)) {
1079       assert(!m->is_final_method(), "no final interface methods");
1080       // If m is already assigned a vtable index, do not disturb it.
1081       if (TraceItables && Verbose) {
1082         ResourceMark rm;

1083         const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
1084         if (m->has_vtable_index()) {
1085           tty->print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1086         } else {
1087           tty->print("itable index %d for method: %s, flags: ", ime_num, sig);
1088         }
1089         if (m != NULL) {
1090           m->access_flags().print_on(tty);
1091           if (m->is_default_method()) {
1092             tty->print("default ");
1093           }
1094           if (m->is_overpass()) {
1095             tty->print("overpass");
1096           }
1097         }
1098         tty->cr();
1099       }
1100       if (!m->has_vtable_index()) {
1101         assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable");
1102         m->set_itable_index(ime_num);
1103         // Progress to next itable entry
1104         ime_num++;
1105       }
1106     }
1107   }
1108   assert(ime_num == method_count_for_interface(klass), "proper sizing");
1109   return ime_num;
1110 }
1111 
1112 int klassItable::method_count_for_interface(Klass* interf) {
1113   assert(interf->is_instance_klass(), "must be");
1114   assert(interf->is_interface(), "must be");
1115   Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1116   int nof_methods = methods->length();
1117   int length = 0;
1118   while (nof_methods > 0) {


1182             const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
1183             char* current = _klass->name()->as_C_string();
1184             const char* loader2 = SystemDictionary::loader_name(interface_loader());
1185             char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
1186             char* failed_type_name = failed_type_symbol->as_C_string();
1187             size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
1188               strlen(current) + strlen(loader2) + strlen(iface) +
1189               strlen(failed_type_name);
1190             char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
1191             jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
1192                          iface, failed_type_name);
1193             THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
1194           }
1195         }
1196       }
1197 
1198       // ime may have moved during GC so recalculate address
1199       int ime_num = m->itable_index();
1200       assert(ime_num < ime_count, "oob");
1201       itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
1202       if (TraceItables && Verbose) {
1203         ResourceMark rm(THREAD);
1204         if (target() != NULL) {

1205           char* sig = target()->name_and_sig_as_C_string();
1206           tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1207                     interf_h()->internal_name(), ime_num, sig,
1208                     target()->method_holder()->internal_name());
1209           tty->print("target_method flags: ");
1210           target()->access_flags().print_on(tty);
1211           if (target()->is_default_method()) {
1212             tty->print("default ");
1213           }
1214           tty->cr();
1215         }
1216       }
1217     }
1218   }
1219 }
1220 
1221 // Update entry for specific Method*
1222 void klassItable::initialize_with_method(Method* m) {
1223   itableMethodEntry* ime = method_entry(0);
1224   for(int i = 0; i < _size_method_table; i++) {
1225     if (ime->method() == m) {
1226       ime->initialize(m);
1227     }
1228     ime++;
1229   }
1230 }
1231 
1232 #if INCLUDE_JVMTI
1233 // search the itable for uses of either obsolete or EMCP methods
1234 void klassItable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  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/resourceArea.hpp"
  31 #include "memory/universe.inline.hpp"
  32 #include "oops/instanceKlass.hpp"
  33 #include "oops/klassVtable.hpp"
  34 #include "oops/method.hpp"
  35 #include "oops/objArrayOop.hpp"
  36 #include "oops/oop.inline.hpp"
  37 #include "prims/jvmtiRedefineClassesTrace.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 
  47 // this function computes the vtable size (including the size needed for miranda
  48 // methods) and the number of miranda methods in this class.
  49 // Note on Miranda methods: Let's say there is a class C that implements


 118 
 119 int klassVtable::index_of(Method* m, int len) const {
 120   assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
 121   return m->vtable_index();
 122 }
 123 
 124 // Copy super class's vtable to the first part (prefix) of this class's vtable,
 125 // and return the number of entries copied.  Expects that 'super' is the Java
 126 // super class (arrays can have "array" super classes that must be skipped).
 127 int klassVtable::initialize_from_super(KlassHandle super) {
 128   if (super.is_null()) {
 129     return 0;
 130   } else {
 131     // copy methods from superKlass
 132     klassVtable* superVtable = super->vtable();
 133     assert(superVtable->length() <= _length, "vtable too short");
 134 #ifdef ASSERT
 135     superVtable->verify(tty, true);
 136 #endif
 137     superVtable->copy_vtable_to(table());


 138     ResourceMark rm;
 139     log_develop_trace(vtables)("copy vtable from %s to %s size %d",
 140                                super->internal_name(), klass()->internal_name(),
 141                                _length);
 142     return superVtable->length();
 143   }
 144 }
 145 
 146 //
 147 // Revised lookup semantics   introduced 1.3 (Kestrel beta)
 148 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
 149 
 150   // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
 151   KlassHandle super (THREAD, klass()->java_super());
 152   int nofNewEntries = 0;
 153 
 154   if (develop_log_is_enabled(Debug, vtables) && !klass()->is_array_klass()) {
 155     ResourceMark rm(THREAD);
 156     log_develop_debug(vtables)("Initializing: %s", _klass->name()->as_C_string());
 157   }
 158 
 159 #ifdef ASSERT
 160   oop* end_of_obj = (oop*)_klass() + _klass()->size();
 161   oop* end_of_vtable = (oop*)&table()[_length];
 162   assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
 163 #endif
 164 
 165   if (Universe::is_bootstrapping()) {
 166     // just clear everything
 167     for (int i = 0; i < _length; i++) table()[i].clear();
 168     return;
 169   }
 170 
 171   int super_vtable_len = initialize_from_super(super);
 172   if (klass()->is_array_klass()) {
 173     assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
 174   } else {
 175     assert(_klass->is_instance_klass(), "must be InstanceKlass");
 176 


 253 // P2.B extends A, package private m
 254 // P1.C extends B, public m
 255 // P1.C.m needs to override P1.A.m and can not override P2.B.m
 256 // Therefore: all package private methods need their own vtable entries for
 257 // them to be the root of an inheritance overriding decision
 258 // Package private methods may also override other vtable entries
 259 InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method,
 260                             int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) {
 261   InstanceKlass* superk = initialsuper;
 262   while (superk != NULL && superk->super() != NULL) {
 263     InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super());
 264     klassVtable* ssVtable = supersuperklass->vtable();
 265     if (vtable_index < ssVtable->length()) {
 266       Method* super_method = ssVtable->method_at(vtable_index);
 267 #ifndef PRODUCT
 268       Symbol* name= target_method()->name();
 269       Symbol* signature = target_method()->signature();
 270       assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
 271 #endif
 272       if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) {
 273         if (develop_log_is_enabled(Trace, vtables)) {

 274           ResourceMark rm(THREAD);
 275           outputStream* logst = LogHandle(vtables)::trace_stream();
 276           char* sig = target_method()->name_and_sig_as_C_string();
 277           logst->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
 278                        supersuperklass->internal_name(),
 279                        _klass->internal_name(), sig, vtable_index);
 280           super_method->print_linkage_flags(logst);
 281           logst->print("overriders flags: ");
 282           target_method->print_linkage_flags(logst);
 283           logst->cr();
 284         }
 285 






 286         break; // return found superk
 287       }
 288     } else  {
 289       // super class has no vtable entry here, stop transitive search
 290       superk = (InstanceKlass*)NULL;
 291       break;
 292     }
 293     // if no override found yet, continue to search up
 294     superk = superk->super() == NULL ? NULL : InstanceKlass::cast(superk->super());
 295   }
 296 
 297   return superk;
 298 }
 299 
 300 static void log_vtables(int i, bool allocate_new, methodHandle target_method,
 301                         KlassHandle target_klass, Method* super_method,
 302                         Thread* thread) {
 303 #ifndef PRODUCT
 304   if (develop_log_is_enabled(Trace, vtables)) {
 305     ResourceMark rm(thread);
 306     outputStream* logst = LogHandle(vtables)::trace_stream();
 307     char* sig = target_method()->name_and_sig_as_C_string();
 308     if (allocate_new) {
 309       // allocate_new = true; default. We might override one entry,
 310       logst->print("NOT overriding with %s::%s index %d, original flags: ",
 311                    target_klass->internal_name(), sig, i);
 312     } else {
 313       logst->print("overriding with %s::%s index %d, original flags: ",
 314                    target_klass->internal_name(), sig, i);
 315     }
 316     super_method->print_linkage_flags(logst);
 317     logst->print("overriders flags: ");
 318     target_method->print_linkage_flags(logst);
 319     logst->cr();
 320   }
 321 #endif
 322 }
 323 
 324 // Update child's copy of super vtable for overrides
 325 // OR return true if a new vtable entry is required.
 326 // Only called for InstanceKlass's, i.e. not for arrays
 327 // If that changed, could not use _klass as handle for klass
 328 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
 329                                           int super_vtable_len, int default_index,
 330                                           bool checkconstraints, TRAPS) {
 331   ResourceMark rm;
 332   bool allocate_new = true;
 333   assert(klass->is_instance_klass(), "must be InstanceKlass");
 334 
 335   Array<int>* def_vtable_indices = NULL;
 336   bool is_default = false;
 337   // default methods are concrete methods in superinterfaces which are added to the vtable
 338   // with their real method_holder
 339   // Since vtable and itable indices share the same storage, don't touch
 340   // the default method's real vtable/itable index
 341   // default_vtable_indices stores the vtable value relative to this inheritor
 342   if (default_index >= 0 ) {
 343     is_default = true;


 459               size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
 460                 strlen(current) + strlen(loader2) + strlen(failed_type_name);
 461               char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 462               jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
 463                            failed_type_name);
 464               THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
 465             }
 466           }
 467         }
 468 
 469         put_method_at(target_method(), i);
 470         if (!is_default) {
 471           target_method()->set_vtable_index(i);
 472         } else {
 473           if (def_vtable_indices != NULL) {
 474             def_vtable_indices->at_put(default_index, i);
 475           }
 476           assert(super_method->is_default_method() || super_method->is_overpass()
 477                  || super_method->is_abstract(), "default override error");
 478         }





















































 479       }
 480       log_vtables(i, allocate_new, target_method, target_klass, super_method, THREAD);
 481     }
 482   }
 483   return allocate_new;
 484 }
 485 
 486 void klassVtable::put_method_at(Method* m, int index) {
 487   if (develop_log_is_enabled(Trace, vtables)) {

 488     ResourceMark rm;
 489     outputStream* logst = LogHandle(vtables)::trace_stream();
 490     const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
 491     logst->print("adding %s at index %d, flags: ", sig, index);
 492     if (m != NULL) {
 493       m->print_linkage_flags(logst);





 494     }
 495     logst->cr();
 496   }



 497   table()[index].set(m);
 498 }
 499 
 500 // Find out if a method "m" with superclass "super", loader "classloader" and
 501 // name "classname" needs a new vtable entry.  Let P be a class package defined
 502 // by "classloader" and "classname".
 503 // NOTE: The logic used here is very similar to the one used for computing
 504 // the vtables indices for a method. We cannot directly use that function because,
 505 // we allocate the InstanceKlass at load time, and that requires that the
 506 // superclass has been loaded.
 507 // However, the vtable entries are filled in at link time, and therefore
 508 // the superclass' vtable may not yet have been filled in.
 509 bool klassVtable::needs_new_vtable_entry(methodHandle target_method,
 510                                          Klass* super,
 511                                          Handle classloader,
 512                                          Symbol* classname,
 513                                          AccessFlags class_flags,
 514                                          TRAPS) {
 515   if (class_flags.is_interface()) {
 516     // Interfaces do not use vtables, except for java.lang.Object methods,


 793     int num_super_ifs = super_ifs->length();
 794     for (int j = 0; j < num_super_ifs; j++) {
 795       InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
 796       add_new_mirandas_to_lists(new_mirandas, all_mirandas,
 797                                 sik->methods(), class_methods,
 798                                 default_methods, super);
 799     }
 800   }
 801 }
 802 
 803 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
 804 // and append them into the vtable starting at index initialized,
 805 // return the new value of initialized.
 806 // Miranda methods use vtable entries, but do not get assigned a vtable_index
 807 // The vtable_index is discovered by searching from the end of the vtable
 808 int klassVtable::fill_in_mirandas(int initialized) {
 809   GrowableArray<Method*> mirandas(20);
 810   get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
 811                ik()->default_methods(), ik()->local_interfaces());
 812   for (int i = 0; i < mirandas.length(); i++) {
 813     if (develop_log_is_enabled(Trace, vtables)) {
 814       Method* meth = mirandas.at(i);
 815       ResourceMark rm(Thread::current());
 816       outputStream* logst = LogHandle(vtables)::trace_stream();
 817       if (meth != NULL) {
 818         char* sig = meth->name_and_sig_as_C_string();
 819         logst->print("fill in mirandas with %s index %d, flags: ",
 820                      sig, initialized);
 821         meth->print_linkage_flags(logst);
 822         logst->cr();



 823       }
 824     }
 825     put_method_at(mirandas.at(i), initialized);
 826     ++initialized;
 827   }
 828   return initialized;
 829 }
 830 
 831 // Copy this class's vtable to the vtable beginning at start.
 832 // Used to copy superclass vtable to prefix of subclass's vtable.
 833 void klassVtable::copy_vtable_to(vtableEntry* start) {
 834   Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
 835 }
 836 
 837 #if INCLUDE_JVMTI
 838 bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
 839   // If old_method is default, find this vtable index in default_vtable_indices
 840   // and replace that method in the _default_methods list
 841   bool updated = false;
 842 


 975 static int initialize_count = 0;
 976 
 977 // Initialization
 978 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
 979   if (_klass->is_interface()) {
 980     // This needs to go after vtable indices are assigned but
 981     // before implementors need to know the number of itable indices.
 982     assign_itable_indices_for_interface(_klass());
 983   }
 984 
 985   // Cannot be setup doing bootstrapping, interfaces don't have
 986   // itables, and klass with only ones entry have empty itables
 987   if (Universe::is_bootstrapping() ||
 988       _klass->is_interface() ||
 989       _klass->itable_length() == itableOffsetEntry::size()) return;
 990 
 991   // There's alway an extra itable entry so we can null-terminate it.
 992   guarantee(size_offset_table() >= 1, "too small");
 993   int num_interfaces = size_offset_table() - 1;
 994   if (num_interfaces > 0) {
 995     log_develop_debug(itables)("%3d: Initializing itables for %s", ++initialize_count,
 996                        _klass->name()->as_C_string());
 997 
 998 
 999     // Iterate through all interfaces
1000     int i;
1001     for(i = 0; i < num_interfaces; i++) {
1002       itableOffsetEntry* ioe = offset_entry(i);
1003       HandleMark hm(THREAD);
1004       KlassHandle interf_h (THREAD, ioe->interface_klass());
1005       assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
1006       initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
1007     }
1008 
1009   }
1010   // Check that the last entry is empty
1011   itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
1012   guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
1013 }
1014 
1015 
1016 inline bool interface_method_needs_itable_index(Method* m) {
1017   if (m->is_static())           return false;   // e.g., Stream.empty
1018   if (m->is_initializer())      return false;   // <init> or <clinit>
1019   // If an interface redeclares a method from java.lang.Object,
1020   // it should already have a vtable index, don't touch it.
1021   // e.g., CharSequence.toString (from initialize_vtable)
1022   // if (m->has_vtable_index())  return false; // NO!
1023   return true;
1024 }
1025 
1026 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
1027   // an interface does not have an itable, but its methods need to be numbered
1028   log_develop_debug(itables)("%3d: Initializing itable indices for interface %s",
1029                              ++initialize_count, klass->name()->as_C_string());
1030   Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
1031   int nof_methods = methods->length();
1032   int ime_num = 0;
1033   for (int i = 0; i < nof_methods; i++) {
1034     Method* m = methods->at(i);
1035     if (interface_method_needs_itable_index(m)) {
1036       assert(!m->is_final_method(), "no final interface methods");
1037       // If m is already assigned a vtable index, do not disturb it.
1038       if (develop_log_is_enabled(Trace, itables)) {
1039         ResourceMark rm;
1040         outputStream* logst = LogHandle(itables)::trace_stream();
1041         const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
1042         if (m->has_vtable_index()) {
1043           logst->print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);
1044         } else {
1045           logst->print("itable index %d for method: %s, flags: ", ime_num, sig);
1046         }
1047         m->print_linkage_flags(logst);
1048         logst->cr();








1049       }
1050       if (!m->has_vtable_index()) {
1051         assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable");
1052         m->set_itable_index(ime_num);
1053         // Progress to next itable entry
1054         ime_num++;
1055       }
1056     }
1057   }
1058   assert(ime_num == method_count_for_interface(klass), "proper sizing");
1059   return ime_num;
1060 }
1061 
1062 int klassItable::method_count_for_interface(Klass* interf) {
1063   assert(interf->is_instance_klass(), "must be");
1064   assert(interf->is_interface(), "must be");
1065   Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
1066   int nof_methods = methods->length();
1067   int length = 0;
1068   while (nof_methods > 0) {


1132             const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
1133             char* current = _klass->name()->as_C_string();
1134             const char* loader2 = SystemDictionary::loader_name(interface_loader());
1135             char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
1136             char* failed_type_name = failed_type_symbol->as_C_string();
1137             size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
1138               strlen(current) + strlen(loader2) + strlen(iface) +
1139               strlen(failed_type_name);
1140             char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
1141             jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
1142                          iface, failed_type_name);
1143             THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
1144           }
1145         }
1146       }
1147 
1148       // ime may have moved during GC so recalculate address
1149       int ime_num = m->itable_index();
1150       assert(ime_num < ime_count, "oob");
1151       itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
1152       if (develop_log_is_enabled(Trace, itables)) {
1153         ResourceMark rm(THREAD);
1154         if (target() != NULL) {
1155           outputStream* logst = LogHandle(itables)::trace_stream();
1156           char* sig = target()->name_and_sig_as_C_string();
1157           logst->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1158                        interf_h()->internal_name(), ime_num, sig,
1159                        target()->method_holder()->internal_name());
1160           logst->print("target_method flags: ");
1161           target()->print_linkage_flags(logst);
1162           logst->cr();



1163         }
1164       }
1165     }
1166   }
1167 }
1168 
1169 // Update entry for specific Method*
1170 void klassItable::initialize_with_method(Method* m) {
1171   itableMethodEntry* ime = method_entry(0);
1172   for(int i = 0; i < _size_method_table; i++) {
1173     if (ime->method() == m) {
1174       ime->initialize(m);
1175     }
1176     ime++;
1177   }
1178 }
1179 
1180 #if INCLUDE_JVMTI
1181 // search the itable for uses of either obsolete or EMCP methods
1182 void klassItable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {


src/share/vm/oops/klassVtable.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File