src/share/vm/oops/klassVtable.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8141564.03 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;


 379 
 380   Symbol* name = target_method()->name();
 381   Symbol* signature = target_method()->signature();
 382 
 383   KlassHandle target_klass(THREAD, target_method()->method_holder());
 384   if (target_klass == NULL) {
 385     target_klass = _klass;
 386   }
 387 
 388   Handle target_loader(THREAD, target_klass->class_loader());
 389 
 390   Symbol* target_classname = target_klass->name();
 391   for(int i = 0; i < super_vtable_len; i++) {
 392     Method* super_method = method_at(i);
 393     // Check if method name matches
 394     if (super_method->name() == name && super_method->signature() == signature) {
 395 
 396       // get super_klass for method_holder for the found method
 397       InstanceKlass* super_klass =  super_method->method_holder();
 398 



 399       // private methods are also never overridden
 400       if (!super_method->is_private() &&
 401           (is_default
 402           || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD))
 403           || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION)
 404           && ((super_klass = find_transitive_override(super_klass,
 405                              target_method, i, target_loader,
 406                              target_classname, THREAD))
 407                              != (InstanceKlass*)NULL)))))
 408         {
 409         // Package private methods always need a new entry to root their own
 410         // overriding. They may also override other methods.
 411         if (!target_method()->is_package_private()) {
 412           allocate_new = false;
 413         }
 414 
 415         if (checkconstraints) {
 416         // Override vtable entry if passes loader constraint check
 417         // if loader constraint checking requested
 418         // No need to visit his super, since he and his super


 432                 "overridden method \"%s\" the class loader (instance"
 433                 " of %s) of the current class, %s, and its superclass loader "
 434                 "(instance of %s), have different Class objects for the type "
 435                 "%s used in the signature";
 436               char* sig = target_method()->name_and_sig_as_C_string();
 437               const char* loader1 = SystemDictionary::loader_name(target_loader());
 438               char* current = target_klass->name()->as_C_string();
 439               const char* loader2 = SystemDictionary::loader_name(super_loader());
 440               char* failed_type_name = failed_type_symbol->as_C_string();
 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                                          const 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,


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

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


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


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


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

1206           char* sig = target()->name_and_sig_as_C_string();
1207           tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
1208                     interf_h()->internal_name(), ime_num, sig,
1209                     target()->method_holder()->internal_name());
1210           tty->print("target_method flags: ");
1211           target()->access_flags().print_on(tty);
1212           if (target()->is_default_method()) {
1213             tty->print("default ");
1214           }
1215           tty->cr();
1216         }
1217       }
1218     }
1219   }
1220 }
1221 
1222 // Update entry for specific Method*
1223 void klassItable::initialize_with_method(Method* m) {
1224   itableMethodEntry* ime = method_entry(0);
1225   for(int i = 0; i < _size_method_table; i++) {
1226     if (ime->method() == m) {
1227       ime->initialize(m);
1228     }
1229     ime++;
1230   }
1231 }
1232 
1233 #if INCLUDE_JVMTI
1234 // search the itable for uses of either obsolete or EMCP methods
1235 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 (!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 overrides, 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 (overrides) {
 309       logst->print("overriding with %s::%s index %d, original flags: ",
 310                    target_klass->internal_name(), sig, i);
 311     } else {
 312       logst->print("NOT overriding with %s::%s index %d, original flags: ",
 313                    target_klass->internal_name(), sig, i);
 314     }
 315     super_method->print_linkage_flags(logst);
 316     logst->print("overriders flags: ");
 317     target_method->print_linkage_flags(logst);
 318     logst->cr();
 319   }
 320 #endif
 321 }
 322 
 323 // Update child's copy of super vtable for overrides
 324 // OR return true if a new vtable entry is required.
 325 // Only called for InstanceKlass's, i.e. not for arrays
 326 // If that changed, could not use _klass as handle for klass
 327 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
 328                                           int super_vtable_len, int default_index,
 329                                           bool checkconstraints, TRAPS) {
 330   ResourceMark rm;
 331   bool allocate_new = true;
 332   assert(klass->is_instance_klass(), "must be InstanceKlass");
 333 
 334   Array<int>* def_vtable_indices = NULL;
 335   bool is_default = false;
 336   // default methods are concrete methods in superinterfaces which are added to the vtable
 337   // with their real method_holder
 338   // Since vtable and itable indices share the same storage, don't touch
 339   // the default method's real vtable/itable index
 340   // default_vtable_indices stores the vtable value relative to this inheritor
 341   if (default_index >= 0 ) {
 342     is_default = true;


 396 
 397   Symbol* name = target_method()->name();
 398   Symbol* signature = target_method()->signature();
 399 
 400   KlassHandle target_klass(THREAD, target_method()->method_holder());
 401   if (target_klass == NULL) {
 402     target_klass = _klass;
 403   }
 404 
 405   Handle target_loader(THREAD, target_klass->class_loader());
 406 
 407   Symbol* target_classname = target_klass->name();
 408   for(int i = 0; i < super_vtable_len; i++) {
 409     Method* super_method = method_at(i);
 410     // Check if method name matches
 411     if (super_method->name() == name && super_method->signature() == signature) {
 412 
 413       // get super_klass for method_holder for the found method
 414       InstanceKlass* super_klass =  super_method->method_holder();
 415 
 416       // Whether the method is being overridden
 417       bool overrides = false;
 418 
 419       // private methods are also never overridden
 420       if (!super_method->is_private() &&
 421           (is_default
 422           || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD))
 423           || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION)
 424           && ((super_klass = find_transitive_override(super_klass,
 425                              target_method, i, target_loader,
 426                              target_classname, THREAD))
 427                              != (InstanceKlass*)NULL)))))
 428         {
 429         // Package private methods always need a new entry to root their own
 430         // overriding. They may also override other methods.
 431         if (!target_method()->is_package_private()) {
 432           allocate_new = false;
 433         }
 434 
 435         if (checkconstraints) {
 436         // Override vtable entry if passes loader constraint check
 437         // if loader constraint checking requested
 438         // No need to visit his super, since he and his super


 452                 "overridden method \"%s\" the class loader (instance"
 453                 " of %s) of the current class, %s, and its superclass loader "
 454                 "(instance of %s), have different Class objects for the type "
 455                 "%s used in the signature";
 456               char* sig = target_method()->name_and_sig_as_C_string();
 457               const char* loader1 = SystemDictionary::loader_name(target_loader());
 458               char* current = target_klass->name()->as_C_string();
 459               const char* loader2 = SystemDictionary::loader_name(super_loader());
 460               char* failed_type_name = failed_type_symbol->as_C_string();
 461               size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
 462                 strlen(current) + strlen(loader2) + strlen(failed_type_name);
 463               char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 464               jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
 465                            failed_type_name);
 466               THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
 467             }
 468           }
 469         }
 470 
 471         put_method_at(target_method(), i);
 472         overrides = true;
 473         if (!is_default) {
 474           target_method()->set_vtable_index(i);
 475         } else {
 476           if (def_vtable_indices != NULL) {
 477             def_vtable_indices->at_put(default_index, i);
 478           }
 479           assert(super_method->is_default_method() || super_method->is_overpass()
 480                  || super_method->is_abstract(), "default override error");
 481         }


























 482       } else {
 483         overrides = false;

























 484       }
 485       log_vtables(i, overrides, target_method, target_klass, super_method, THREAD);
 486     }
 487   }
 488   return allocate_new;
 489 }
 490 
 491 void klassVtable::put_method_at(Method* m, int index) {
 492   if (develop_log_is_enabled(Trace, vtables)) {

 493     ResourceMark rm;
 494     outputStream* logst = LogHandle(vtables)::trace_stream();
 495     const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
 496     logst->print("adding %s at index %d, flags: ", sig, index);
 497     if (m != NULL) {
 498       m->print_linkage_flags(logst);






 499     }
 500     logst->cr();
 501   }

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


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



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


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








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


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



1170         }
1171       }
1172     }
1173   }
1174 }
1175 
1176 // Update entry for specific Method*
1177 void klassItable::initialize_with_method(Method* m) {
1178   itableMethodEntry* ime = method_entry(0);
1179   for(int i = 0; i < _size_method_table; i++) {
1180     if (ime->method() == m) {
1181       ime->initialize(m);
1182     }
1183     ime++;
1184   }
1185 }
1186 
1187 #if INCLUDE_JVMTI
1188 // search the itable for uses of either obsolete or EMCP methods
1189 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