src/share/vm/oops/klassVtable.cpp

Wed, 22 Jan 2014 17:42:23 -0800

author
kvn
date
Wed, 22 Jan 2014 17:42:23 -0800
changeset 6503
a9becfeecd1b
parent 6195
5832cdaf89c6
child 6632
386dd1c71858
child 6759
c02077c4b79c
permissions
-rw-r--r--

Merge

duke@435 1 /*
dcubed@4562 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/systemDictionary.hpp"
stefank@2314 27 #include "classfile/vmSymbols.hpp"
stefank@2314 28 #include "gc_implementation/shared/markSweep.inline.hpp"
stefank@2314 29 #include "memory/gcLocker.hpp"
stefank@2314 30 #include "memory/resourceArea.hpp"
stefank@2314 31 #include "memory/universe.inline.hpp"
stefank@2314 32 #include "oops/instanceKlass.hpp"
stefank@2314 33 #include "oops/klassVtable.hpp"
coleenp@4037 34 #include "oops/method.hpp"
stefank@2314 35 #include "oops/objArrayOop.hpp"
stefank@2314 36 #include "oops/oop.inline.hpp"
stefank@2314 37 #include "prims/jvmtiRedefineClassesTrace.hpp"
stefank@2314 38 #include "runtime/arguments.hpp"
stefank@2314 39 #include "runtime/handles.inline.hpp"
stefank@2314 40 #include "utilities/copy.hpp"
duke@435 41
coleenp@4037 42 inline InstanceKlass* klassVtable::ik() const {
coleenp@4037 43 Klass* k = _klass();
coleenp@4037 44 assert(k->oop_is_instance(), "not an InstanceKlass");
coleenp@4037 45 return (InstanceKlass*)k;
duke@435 46 }
duke@435 47
duke@435 48
duke@435 49 // this function computes the vtable size (including the size needed for miranda
drchase@5732 50 // methods) and the number of miranda methods in this class.
duke@435 51 // Note on Miranda methods: Let's say there is a class C that implements
drchase@5732 52 // interface I, and none of C's superclasses implements I.
drchase@5732 53 // Let's say there is an abstract method m in I that neither C
drchase@5732 54 // nor any of its super classes implement (i.e there is no method of any access,
drchase@5732 55 // with the same name and signature as m), then m is a Miranda method which is
duke@435 56 // entered as a public abstract method in C's vtable. From then on it should
duke@435 57 // treated as any other public method in C for method over-ride purposes.
kamg@4245 58 void klassVtable::compute_vtable_size_and_num_mirandas(
kamg@4245 59 int* vtable_length_ret, int* num_new_mirandas,
kamg@4245 60 GrowableArray<Method*>* all_mirandas, Klass* super,
kamg@4245 61 Array<Method*>* methods, AccessFlags class_flags,
kamg@4245 62 Handle classloader, Symbol* classname, Array<Klass*>* local_interfaces,
kamg@4245 63 TRAPS) {
duke@435 64 No_Safepoint_Verifier nsv;
duke@435 65
duke@435 66 // set up default result values
kamg@4245 67 int vtable_length = 0;
duke@435 68
duke@435 69 // start off with super's vtable length
coleenp@4037 70 InstanceKlass* sk = (InstanceKlass*)super;
duke@435 71 vtable_length = super == NULL ? 0 : sk->vtable_length();
duke@435 72
duke@435 73 // go thru each method in the methods table to see if it needs a new entry
duke@435 74 int len = methods->length();
duke@435 75 for (int i = 0; i < len; i++) {
coleenp@4037 76 assert(methods->at(i)->is_method(), "must be a Method*");
coleenp@4037 77 methodHandle mh(THREAD, methods->at(i));
duke@435 78
acorn@1087 79 if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, THREAD)) {
duke@435 80 vtable_length += vtableEntry::size(); // we need a new entry
duke@435 81 }
duke@435 82 }
duke@435 83
kamg@4245 84 GrowableArray<Method*> new_mirandas(20);
duke@435 85 // compute the number of mirandas methods that must be added to the end
acorn@5848 86 get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces);
kamg@4245 87 *num_new_mirandas = new_mirandas.length();
kamg@4245 88
acorn@6080 89 // Interfaces do not need interface methods in their vtables
acorn@6080 90 // This includes miranda methods and during later processing, default methods
acorn@6080 91 if (!class_flags.is_interface()) {
acorn@6080 92 vtable_length += *num_new_mirandas * vtableEntry::size();
acorn@6080 93 }
duke@435 94
duke@435 95 if (Universe::is_bootstrapping() && vtable_length == 0) {
duke@435 96 // array classes don't have their superclass set correctly during
duke@435 97 // bootstrapping
duke@435 98 vtable_length = Universe::base_vtable_size();
duke@435 99 }
duke@435 100
duke@435 101 if (super == NULL && !Universe::is_bootstrapping() &&
duke@435 102 vtable_length != Universe::base_vtable_size()) {
duke@435 103 // Someone is attempting to redefine java.lang.Object incorrectly. The
duke@435 104 // only way this should happen is from
duke@435 105 // SystemDictionary::resolve_from_stream(), which will detect this later
duke@435 106 // and throw a security exception. So don't assert here to let
duke@435 107 // the exception occur.
duke@435 108 vtable_length = Universe::base_vtable_size();
duke@435 109 }
duke@435 110 assert(super != NULL || vtable_length == Universe::base_vtable_size(),
duke@435 111 "bad vtable size for class Object");
duke@435 112 assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");
duke@435 113 assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");
kamg@4245 114
kamg@4245 115 *vtable_length_ret = vtable_length;
duke@435 116 }
duke@435 117
coleenp@4037 118 int klassVtable::index_of(Method* m, int len) const {
drchase@5732 119 assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
duke@435 120 return m->vtable_index();
duke@435 121 }
duke@435 122
drchase@5732 123 // Copy super class's vtable to the first part (prefix) of this class's vtable,
drchase@5732 124 // and return the number of entries copied. Expects that 'super' is the Java
drchase@5732 125 // super class (arrays can have "array" super classes that must be skipped).
duke@435 126 int klassVtable::initialize_from_super(KlassHandle super) {
duke@435 127 if (super.is_null()) {
duke@435 128 return 0;
duke@435 129 } else {
duke@435 130 // copy methods from superKlass
coleenp@4037 131 // can't inherit from array class, so must be InstanceKlass
duke@435 132 assert(super->oop_is_instance(), "must be instance klass");
coleenp@4037 133 InstanceKlass* sk = (InstanceKlass*)super();
duke@435 134 klassVtable* superVtable = sk->vtable();
duke@435 135 assert(superVtable->length() <= _length, "vtable too short");
duke@435 136 #ifdef ASSERT
duke@435 137 superVtable->verify(tty, true);
duke@435 138 #endif
duke@435 139 superVtable->copy_vtable_to(table());
duke@435 140 #ifndef PRODUCT
duke@435 141 if (PrintVtables && Verbose) {
acorn@1087 142 ResourceMark rm;
duke@435 143 tty->print_cr("copy vtable from %s to %s size %d", sk->internal_name(), klass()->internal_name(), _length);
duke@435 144 }
duke@435 145 #endif
duke@435 146 return superVtable->length();
duke@435 147 }
duke@435 148 }
duke@435 149
drchase@5732 150 //
drchase@5732 151 // Revised lookup semantics introduced 1.3 (Kestrel beta)
duke@435 152 void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
duke@435 153
duke@435 154 // Note: Arrays can have intermediate array supers. Use java_super to skip them.
duke@435 155 KlassHandle super (THREAD, klass()->java_super());
duke@435 156 int nofNewEntries = 0;
duke@435 157
duke@435 158 if (PrintVtables && !klass()->oop_is_array()) {
duke@435 159 ResourceMark rm(THREAD);
duke@435 160 tty->print_cr("Initializing: %s", _klass->name()->as_C_string());
duke@435 161 }
duke@435 162
duke@435 163 #ifdef ASSERT
duke@435 164 oop* end_of_obj = (oop*)_klass() + _klass()->size();
duke@435 165 oop* end_of_vtable = (oop*)&table()[_length];
duke@435 166 assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
duke@435 167 #endif
duke@435 168
duke@435 169 if (Universe::is_bootstrapping()) {
duke@435 170 // just clear everything
duke@435 171 for (int i = 0; i < _length; i++) table()[i].clear();
duke@435 172 return;
duke@435 173 }
duke@435 174
duke@435 175 int super_vtable_len = initialize_from_super(super);
duke@435 176 if (klass()->oop_is_array()) {
duke@435 177 assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
duke@435 178 } else {
coleenp@4037 179 assert(_klass->oop_is_instance(), "must be InstanceKlass");
duke@435 180
coleenp@4037 181 Array<Method*>* methods = ik()->methods();
coleenp@4037 182 int len = methods->length();
duke@435 183 int initialized = super_vtable_len;
duke@435 184
drchase@5732 185 // Check each of this class's methods against super;
drchase@5732 186 // if override, replace in copy of super vtable, otherwise append to end
duke@435 187 for (int i = 0; i < len; i++) {
drchase@5732 188 // update_inherited_vtable can stop for gc - ensure using handles
duke@435 189 HandleMark hm(THREAD);
coleenp@4037 190 assert(methods->at(i)->is_method(), "must be a Method*");
coleenp@4037 191 methodHandle mh(THREAD, methods->at(i));
duke@435 192
acorn@5848 193 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK);
duke@435 194
duke@435 195 if (needs_new_entry) {
duke@435 196 put_method_at(mh(), initialized);
duke@435 197 mh()->set_vtable_index(initialized); // set primary vtable index
duke@435 198 initialized++;
duke@435 199 }
duke@435 200 }
duke@435 201
acorn@5848 202 // update vtable with default_methods
acorn@5848 203 Array<Method*>* default_methods = ik()->default_methods();
acorn@5848 204 if (default_methods != NULL) {
acorn@5848 205 len = default_methods->length();
acorn@5848 206 if (len > 0) {
acorn@5848 207 Array<int>* def_vtable_indices = NULL;
acorn@5848 208 if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) {
acorn@5848 209 def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK);
acorn@5848 210 } else {
acorn@5848 211 assert(def_vtable_indices->length() == len, "reinit vtable len?");
acorn@5848 212 }
acorn@5848 213 for (int i = 0; i < len; i++) {
acorn@5848 214 HandleMark hm(THREAD);
acorn@5848 215 assert(default_methods->at(i)->is_method(), "must be a Method*");
acorn@5848 216 methodHandle mh(THREAD, default_methods->at(i));
acorn@5848 217
acorn@5848 218 bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK);
acorn@5848 219
acorn@5848 220 // needs new entry
acorn@5848 221 if (needs_new_entry) {
acorn@5848 222 put_method_at(mh(), initialized);
acorn@5848 223 def_vtable_indices->at_put(i, initialized); //set vtable index
acorn@5848 224 initialized++;
acorn@5848 225 }
acorn@5848 226 }
acorn@5848 227 }
acorn@5848 228 }
acorn@5848 229
acorn@5848 230 // add miranda methods; it will also return the updated initialized
acorn@6080 231 // Interfaces do not need interface methods in their vtables
acorn@6080 232 // This includes miranda methods and during later processing, default methods
acorn@6080 233 if (!ik()->is_interface()) {
acorn@6080 234 initialized = fill_in_mirandas(initialized);
acorn@6080 235 }
duke@435 236
acorn@1087 237 // In class hierarchies where the accessibility is not increasing (i.e., going from private ->
drchase@5732 238 // package_private -> public/protected), the vtable might actually be smaller than our initial
duke@435 239 // calculation.
duke@435 240 assert(initialized <= _length, "vtable initialization failed");
duke@435 241 for(;initialized < _length; initialized++) {
duke@435 242 put_method_at(NULL, initialized);
duke@435 243 }
duke@435 244 NOT_PRODUCT(verify(tty, true));
duke@435 245 }
duke@435 246 }
duke@435 247
acorn@1087 248 // Called for cases where a method does not override its superclass' vtable entry
acorn@1087 249 // For bytecodes not produced by javac together it is possible that a method does not override
acorn@1087 250 // the superclass's method, but might indirectly override a super-super class's vtable entry
acorn@1087 251 // If none found, return a null superk, else return the superk of the method this does override
coleenp@4037 252 InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method,
coleenp@2497 253 int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) {
coleenp@4037 254 InstanceKlass* superk = initialsuper;
acorn@1087 255 while (superk != NULL && superk->super() != NULL) {
coleenp@4037 256 InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super());
acorn@1087 257 klassVtable* ssVtable = supersuperklass->vtable();
acorn@1087 258 if (vtable_index < ssVtable->length()) {
coleenp@4037 259 Method* super_method = ssVtable->method_at(vtable_index);
acorn@1087 260 #ifndef PRODUCT
coleenp@2497 261 Symbol* name= target_method()->name();
coleenp@2497 262 Symbol* signature = target_method()->signature();
coleenp@2497 263 assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
acorn@1087 264 #endif
acorn@1087 265 if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) {
acorn@1087 266 #ifndef PRODUCT
acorn@1087 267 if (PrintVtables && Verbose) {
acorn@1087 268 ResourceMark rm(THREAD);
acorn@5848 269 char* sig = target_method()->name_and_sig_as_C_string();
acorn@1087 270 tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
acorn@1087 271 supersuperklass->internal_name(),
acorn@5848 272 _klass->internal_name(), sig, vtable_index);
acorn@1087 273 super_method->access_flags().print_on(tty);
acorn@5848 274 if (super_method->is_default_method()) {
acorn@6080 275 tty->print("default ");
acorn@5848 276 }
acorn@1087 277 tty->print("overriders flags: ");
acorn@1087 278 target_method->access_flags().print_on(tty);
acorn@5848 279 if (target_method->is_default_method()) {
acorn@6080 280 tty->print("default ");
acorn@5848 281 }
acorn@1087 282 }
acorn@1087 283 #endif /*PRODUCT*/
acorn@1087 284 break; // return found superk
acorn@1087 285 }
acorn@1087 286 } else {
acorn@1087 287 // super class has no vtable entry here, stop transitive search
coleenp@4037 288 superk = (InstanceKlass*)NULL;
acorn@1087 289 break;
acorn@1087 290 }
acorn@1087 291 // if no override found yet, continue to search up
coleenp@4037 292 superk = InstanceKlass::cast(superk->super());
acorn@1087 293 }
duke@435 294
acorn@1087 295 return superk;
duke@435 296 }
duke@435 297
duke@435 298 // Update child's copy of super vtable for overrides
drchase@5732 299 // OR return true if a new vtable entry is required.
coleenp@4037 300 // Only called for InstanceKlass's, i.e. not for arrays
duke@435 301 // If that changed, could not use _klass as handle for klass
acorn@5848 302 bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
acorn@5848 303 int super_vtable_len, int default_index,
acorn@5848 304 bool checkconstraints, TRAPS) {
duke@435 305 ResourceMark rm;
duke@435 306 bool allocate_new = true;
coleenp@4037 307 assert(klass->oop_is_instance(), "must be InstanceKlass");
duke@435 308
acorn@5848 309 Array<int>* def_vtable_indices = NULL;
acorn@5848 310 bool is_default = false;
acorn@5848 311 // default methods are concrete methods in superinterfaces which are added to the vtable
acorn@5848 312 // with their real method_holder
acorn@5848 313 // Since vtable and itable indices share the same storage, don't touch
acorn@5848 314 // the default method's real vtable/itable index
acorn@5848 315 // default_vtable_indices stores the vtable value relative to this inheritor
acorn@5848 316 if (default_index >= 0 ) {
acorn@5848 317 is_default = true;
acorn@5848 318 def_vtable_indices = klass->default_vtable_indices();
acorn@5848 319 assert(def_vtable_indices != NULL, "def vtable alloc?");
acorn@5848 320 assert(default_index <= def_vtable_indices->length(), "def vtable len?");
acorn@5848 321 } else {
acorn@5848 322 assert(klass == target_method()->method_holder(), "caller resp.");
acorn@5848 323 // Initialize the method's vtable index to "nonvirtual".
acorn@5848 324 // If we allocate a vtable entry, we will update it to a non-negative number.
acorn@5848 325 target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
acorn@5848 326 }
duke@435 327
duke@435 328 // Static and <init> methods are never in
duke@435 329 if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) {
duke@435 330 return false;
duke@435 331 }
duke@435 332
drchase@5732 333 if (target_method->is_final_method(klass->access_flags())) {
duke@435 334 // a final method never needs a new entry; final methods can be statically
duke@435 335 // resolved and they have to be present in the vtable only if they override
duke@435 336 // a super's method, in which case they re-use its entry
duke@435 337 allocate_new = false;
drchase@5732 338 } else if (klass->is_interface()) {
drchase@5732 339 allocate_new = false; // see note below in needs_new_vtable_entry
drchase@5732 340 // An interface never allocates new vtable slots, only inherits old ones.
drchase@5732 341 // This method will either be assigned its own itable index later,
drchase@5732 342 // or be assigned an inherited vtable index in the loop below.
acorn@6080 343 // default methods inherited by classes store their vtable indices
acorn@6080 344 // in the inheritor's default_vtable_indices
acorn@6080 345 // default methods inherited by interfaces may already have a
acorn@6080 346 // valid itable index, if so, don't change it
acorn@6080 347 // overpass methods in an interface will be assigned an itable index later
acorn@6080 348 // by an inheriting class
acorn@6080 349 if (!is_default || !target_method()->has_itable_index()) {
acorn@6080 350 target_method()->set_vtable_index(Method::pending_itable_index);
acorn@6080 351 }
duke@435 352 }
duke@435 353
duke@435 354 // we need a new entry if there is no superclass
duke@435 355 if (klass->super() == NULL) {
duke@435 356 return allocate_new;
duke@435 357 }
duke@435 358
acorn@5786 359 // private methods in classes always have a new entry in the vtable
acorn@1087 360 // specification interpretation since classic has
acorn@1087 361 // private methods not overriding
acorn@5786 362 // JDK8 adds private methods in interfaces which require invokespecial
duke@435 363 if (target_method()->is_private()) {
duke@435 364 return allocate_new;
duke@435 365 }
duke@435 366
duke@435 367 // search through the vtable and update overridden entries
duke@435 368 // Since check_signature_loaders acquires SystemDictionary_lock
acorn@1087 369 // which can block for gc, once we are in this loop, use handles
acorn@1087 370 // For classfiles built with >= jdk7, we now look for transitive overrides
duke@435 371
coleenp@2497 372 Symbol* name = target_method()->name();
coleenp@2497 373 Symbol* signature = target_method()->signature();
acorn@5848 374
acorn@5848 375 KlassHandle target_klass(THREAD, target_method()->method_holder());
acorn@5848 376 if (target_klass == NULL) {
acorn@5848 377 target_klass = _klass;
acorn@5848 378 }
acorn@5848 379
acorn@5848 380 Handle target_loader(THREAD, target_klass->class_loader());
acorn@5848 381
acorn@5848 382 Symbol* target_classname = target_klass->name();
duke@435 383 for(int i = 0; i < super_vtable_len; i++) {
coleenp@4037 384 Method* super_method = method_at(i);
duke@435 385 // Check if method name matches
coleenp@2497 386 if (super_method->name() == name && super_method->signature() == signature) {
duke@435 387
acorn@1087 388 // get super_klass for method_holder for the found method
coleenp@4251 389 InstanceKlass* super_klass = super_method->method_holder();
duke@435 390
acorn@5848 391 if (is_default
acorn@5848 392 || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD))
acorn@5848 393 || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION)
acorn@5848 394 && ((super_klass = find_transitive_override(super_klass,
acorn@5848 395 target_method, i, target_loader,
acorn@5848 396 target_classname, THREAD))
acorn@5848 397 != (InstanceKlass*)NULL))))
acorn@5848 398 {
acorn@1087 399 // overriding, so no new entry
acorn@1087 400 allocate_new = false;
duke@435 401
duke@435 402 if (checkconstraints) {
duke@435 403 // Override vtable entry if passes loader constraint check
duke@435 404 // if loader constraint checking requested
duke@435 405 // No need to visit his super, since he and his super
duke@435 406 // have already made any needed loader constraints.
duke@435 407 // Since loader constraints are transitive, it is enough
duke@435 408 // to link to the first super, and we get all the others.
duke@435 409 Handle super_loader(THREAD, super_klass->class_loader());
duke@435 410
acorn@1087 411 if (target_loader() != super_loader()) {
duke@435 412 ResourceMark rm(THREAD);
acorn@4840 413 Symbol* failed_type_symbol =
acorn@1087 414 SystemDictionary::check_signature_loaders(signature, target_loader,
duke@435 415 super_loader, true,
duke@435 416 CHECK_(false));
acorn@4840 417 if (failed_type_symbol != NULL) {
duke@435 418 const char* msg = "loader constraint violation: when resolving "
duke@435 419 "overridden method \"%s\" the class loader (instance"
duke@435 420 " of %s) of the current class, %s, and its superclass loader "
duke@435 421 "(instance of %s), have different Class objects for the type "
duke@435 422 "%s used in the signature";
duke@435 423 char* sig = target_method()->name_and_sig_as_C_string();
acorn@1087 424 const char* loader1 = SystemDictionary::loader_name(target_loader());
acorn@5848 425 char* current = target_klass->name()->as_C_string();
duke@435 426 const char* loader2 = SystemDictionary::loader_name(super_loader());
acorn@4840 427 char* failed_type_name = failed_type_symbol->as_C_string();
duke@435 428 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
duke@435 429 strlen(current) + strlen(loader2) + strlen(failed_type_name);
duke@435 430 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
duke@435 431 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
duke@435 432 failed_type_name);
duke@435 433 THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
duke@435 434 }
duke@435 435 }
acorn@1087 436 }
acorn@1087 437
acorn@5848 438 put_method_at(target_method(), i);
acorn@5848 439 if (!is_default) {
acorn@5848 440 target_method()->set_vtable_index(i);
acorn@5848 441 } else {
acorn@5848 442 if (def_vtable_indices != NULL) {
acorn@5848 443 def_vtable_indices->at_put(default_index, i);
acorn@5848 444 }
acorn@5848 445 assert(super_method->is_default_method() || super_method->is_overpass()
acorn@5848 446 || super_method->is_abstract(), "default override error");
acorn@5848 447 }
acorn@5848 448
acorn@5848 449
acorn@1087 450 #ifndef PRODUCT
acorn@1087 451 if (PrintVtables && Verbose) {
acorn@5848 452 ResourceMark rm(THREAD);
acorn@5848 453 char* sig = target_method()->name_and_sig_as_C_string();
acorn@1087 454 tty->print("overriding with %s::%s index %d, original flags: ",
acorn@5848 455 target_klass->internal_name(), sig, i);
acorn@1087 456 super_method->access_flags().print_on(tty);
acorn@5848 457 if (super_method->is_default_method()) {
acorn@6080 458 tty->print("default ");
acorn@5848 459 }
acorn@5848 460 if (super_method->is_overpass()) {
acorn@5848 461 tty->print("overpass");
acorn@5848 462 }
acorn@1087 463 tty->print("overriders flags: ");
acorn@1087 464 target_method->access_flags().print_on(tty);
acorn@5848 465 if (target_method->is_default_method()) {
acorn@6080 466 tty->print("default ");
acorn@5848 467 }
acorn@5848 468 if (target_method->is_overpass()) {
acorn@5848 469 tty->print("overpass");
acorn@5848 470 }
acorn@1087 471 tty->cr();
duke@435 472 }
acorn@1087 473 #endif /*PRODUCT*/
acorn@1087 474 } else {
acorn@1087 475 // allocate_new = true; default. We might override one entry,
acorn@1087 476 // but not override another. Once we override one, not need new
duke@435 477 #ifndef PRODUCT
acorn@1087 478 if (PrintVtables && Verbose) {
acorn@5848 479 ResourceMark rm(THREAD);
acorn@5848 480 char* sig = target_method()->name_and_sig_as_C_string();
acorn@1087 481 tty->print("NOT overriding with %s::%s index %d, original flags: ",
acorn@5848 482 target_klass->internal_name(), sig,i);
acorn@1087 483 super_method->access_flags().print_on(tty);
acorn@5848 484 if (super_method->is_default_method()) {
acorn@6080 485 tty->print("default ");
acorn@5848 486 }
acorn@5848 487 if (super_method->is_overpass()) {
acorn@5848 488 tty->print("overpass");
acorn@5848 489 }
acorn@1087 490 tty->print("overriders flags: ");
acorn@1087 491 target_method->access_flags().print_on(tty);
acorn@5848 492 if (target_method->is_default_method()) {
acorn@6080 493 tty->print("default ");
acorn@5848 494 }
acorn@5848 495 if (target_method->is_overpass()) {
acorn@5848 496 tty->print("overpass");
acorn@5848 497 }
acorn@1087 498 tty->cr();
acorn@1087 499 }
duke@435 500 #endif /*PRODUCT*/
duke@435 501 }
duke@435 502 }
duke@435 503 }
duke@435 504 return allocate_new;
duke@435 505 }
duke@435 506
coleenp@4037 507 void klassVtable::put_method_at(Method* m, int index) {
duke@435 508 #ifndef PRODUCT
duke@435 509 if (PrintVtables && Verbose) {
acorn@1087 510 ResourceMark rm;
acorn@6080 511 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
acorn@6080 512 tty->print("adding %s at index %d, flags: ", sig, index);
acorn@6080 513 if (m != NULL) {
acorn@6080 514 m->access_flags().print_on(tty);
acorn@6080 515 if (m->is_default_method()) {
acorn@6080 516 tty->print("default ");
acorn@6080 517 }
acorn@6080 518 if (m->is_overpass()) {
acorn@6080 519 tty->print("overpass");
acorn@6080 520 }
acorn@6080 521 }
acorn@6080 522 tty->cr();
duke@435 523 }
duke@435 524 #endif
duke@435 525 table()[index].set(m);
duke@435 526 }
duke@435 527
duke@435 528 // Find out if a method "m" with superclass "super", loader "classloader" and
duke@435 529 // name "classname" needs a new vtable entry. Let P be a class package defined
duke@435 530 // by "classloader" and "classname".
duke@435 531 // NOTE: The logic used here is very similar to the one used for computing
duke@435 532 // the vtables indices for a method. We cannot directly use that function because,
coleenp@4037 533 // we allocate the InstanceKlass at load time, and that requires that the
acorn@1087 534 // superclass has been loaded.
acorn@1087 535 // However, the vtable entries are filled in at link time, and therefore
acorn@1087 536 // the superclass' vtable may not yet have been filled in.
acorn@1087 537 bool klassVtable::needs_new_vtable_entry(methodHandle target_method,
coleenp@4037 538 Klass* super,
acorn@1087 539 Handle classloader,
coleenp@2497 540 Symbol* classname,
acorn@1087 541 AccessFlags class_flags,
acorn@1087 542 TRAPS) {
drchase@5732 543 if (class_flags.is_interface()) {
drchase@5732 544 // Interfaces do not use vtables, so there is no point to assigning
drchase@5732 545 // a vtable index to any of their methods. If we refrain from doing this,
drchase@5732 546 // we can use Method::_vtable_index to hold the itable index
drchase@5732 547 return false;
drchase@5732 548 }
kamg@4245 549
drchase@5732 550 if (target_method->is_final_method(class_flags) ||
duke@435 551 // a final method never needs a new entry; final methods can be statically
duke@435 552 // resolved and they have to be present in the vtable only if they override
duke@435 553 // a super's method, in which case they re-use its entry
acorn@1087 554 (target_method()->is_static()) ||
duke@435 555 // static methods don't need to be in vtable
acorn@1087 556 (target_method()->name() == vmSymbols::object_initializer_name())
duke@435 557 // <init> is never called dynamically-bound
duke@435 558 ) {
duke@435 559 return false;
duke@435 560 }
duke@435 561
acorn@5848 562 // Concrete interface methods do not need new entries, they override
acorn@5848 563 // abstract method entries using default inheritance rules
acorn@5848 564 if (target_method()->method_holder() != NULL &&
acorn@5848 565 target_method()->method_holder()->is_interface() &&
acorn@5848 566 !target_method()->is_abstract() ) {
acorn@5848 567 return false;
acorn@5848 568 }
acorn@5848 569
duke@435 570 // we need a new entry if there is no superclass
duke@435 571 if (super == NULL) {
duke@435 572 return true;
duke@435 573 }
duke@435 574
acorn@5786 575 // private methods in classes always have a new entry in the vtable
acorn@1087 576 // specification interpretation since classic has
acorn@1087 577 // private methods not overriding
acorn@5848 578 // JDK8 adds private methods in interfaces which require invokespecial
acorn@1087 579 if (target_method()->is_private()) {
duke@435 580 return true;
duke@435 581 }
duke@435 582
duke@435 583 // search through the super class hierarchy to see if we need
duke@435 584 // a new entry
acorn@1087 585 ResourceMark rm;
coleenp@2497 586 Symbol* name = target_method()->name();
coleenp@2497 587 Symbol* signature = target_method()->signature();
coleenp@4037 588 Klass* k = super;
coleenp@4037 589 Method* super_method = NULL;
coleenp@4037 590 InstanceKlass *holder = NULL;
coleenp@4037 591 Method* recheck_method = NULL;
duke@435 592 while (k != NULL) {
duke@435 593 // lookup through the hierarchy for a method with matching name and sign.
coleenp@4037 594 super_method = InstanceKlass::cast(k)->lookup_method(name, signature);
acorn@1087 595 if (super_method == NULL) {
duke@435 596 break; // we still have to search for a matching miranda method
duke@435 597 }
duke@435 598 // get the class holding the matching method
acorn@1087 599 // make sure you use that class for is_override
coleenp@4251 600 InstanceKlass* superk = super_method->method_holder();
acorn@1087 601 // we want only instance method matches
acorn@1087 602 // pretend private methods are not in the super vtable
acorn@1087 603 // since we do override around them: e.g. a.m pub/b.m private/c.m pub,
acorn@1087 604 // ignore private, c.m pub does override a.m pub
acorn@1087 605 // For classes that were not javac'd together, we also do transitive overriding around
acorn@1087 606 // methods that have less accessibility
acorn@1087 607 if ((!super_method->is_static()) &&
acorn@1087 608 (!super_method->is_private())) {
acorn@1087 609 if (superk->is_override(super_method, classloader, classname, THREAD)) {
duke@435 610 return false;
acorn@1087 611 // else keep looking for transitive overrides
duke@435 612 }
duke@435 613 }
duke@435 614
acorn@1087 615 // Start with lookup result and continue to search up
acorn@1087 616 k = superk->super(); // haven't found an override match yet; continue to look
duke@435 617 }
duke@435 618
duke@435 619 // if the target method is public or protected it may have a matching
duke@435 620 // miranda method in the super, whose entry it should re-use.
acorn@1087 621 // Actually, to handle cases that javac would not generate, we need
acorn@1087 622 // this check for all access permissions.
coleenp@4037 623 InstanceKlass *sk = InstanceKlass::cast(super);
acorn@1087 624 if (sk->has_miranda_methods()) {
hseigel@6195 625 if (sk->lookup_method_in_all_interfaces(name, signature, false) != NULL) {
acorn@1087 626 return false; // found a matching miranda; we do not need a new entry
duke@435 627 }
duke@435 628 }
duke@435 629 return true; // found no match; we need a new entry
duke@435 630 }
duke@435 631
duke@435 632 // Support for miranda methods
duke@435 633
duke@435 634 // get the vtable index of a miranda method with matching "name" and "signature"
coleenp@2497 635 int klassVtable::index_of_miranda(Symbol* name, Symbol* signature) {
duke@435 636 // search from the bottom, might be faster
duke@435 637 for (int i = (length() - 1); i >= 0; i--) {
coleenp@4037 638 Method* m = table()[i].method();
duke@435 639 if (is_miranda_entry_at(i) &&
duke@435 640 m->name() == name && m->signature() == signature) {
duke@435 641 return i;
duke@435 642 }
duke@435 643 }
coleenp@4037 644 return Method::invalid_vtable_index;
duke@435 645 }
duke@435 646
drchase@5732 647 // check if an entry at an index is miranda
drchase@5732 648 // requires that method m at entry be declared ("held") by an interface.
duke@435 649 bool klassVtable::is_miranda_entry_at(int i) {
coleenp@4037 650 Method* m = method_at(i);
coleenp@4037 651 Klass* method_holder = m->method_holder();
coleenp@4037 652 InstanceKlass *mhk = InstanceKlass::cast(method_holder);
duke@435 653
acorn@5786 654 // miranda methods are public abstract instance interface methods in a class's vtable
duke@435 655 if (mhk->is_interface()) {
kamg@4245 656 assert(m->is_public(), "should be public");
duke@435 657 assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
acorn@6080 658 // the search could find a miranda or a default method
acorn@6080 659 if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) {
acorn@6080 660 return true;
acorn@6080 661 }
duke@435 662 }
duke@435 663 return false;
duke@435 664 }
duke@435 665
acorn@5848 666 // check if a method is a miranda method, given a class's methods table,
acorn@5848 667 // its default_method table and its super
acorn@6145 668 // Miranda methods are calculated twice:
acorn@6145 669 // first: before vtable size calculation: including abstract and default
acorn@6145 670 // This is seen by default method creation
acorn@6145 671 // Second: recalculated during vtable initialization: only abstract
acorn@6145 672 // This is seen by link resolution and selection.
acorn@5848 673 // "miranda" means not static, not defined by this class.
acorn@5848 674 // private methods in interfaces do not belong in the miranda list.
duke@435 675 // the caller must make sure that the method belongs to an interface implemented by the class
acorn@5786 676 // Miranda methods only include public interface instance methods
acorn@5848 677 // Not private methods, not static methods, not default == concrete abstract
acorn@6080 678 // Miranda methods also do not include overpass methods in interfaces
acorn@5848 679 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
acorn@5848 680 Array<Method*>* default_methods, Klass* super) {
acorn@6080 681 if (m->is_static() || m->is_private() || m->is_overpass()) {
bharadwaj@4998 682 return false;
bharadwaj@4998 683 }
coleenp@2497 684 Symbol* name = m->name();
coleenp@2497 685 Symbol* signature = m->signature();
acorn@6145 686
acorn@6145 687 if (InstanceKlass::find_instance_method(class_methods, name, signature) == NULL) {
dsamersoff@2360 688 // did not find it in the method table of the current class
acorn@5848 689 if ((default_methods == NULL) ||
acorn@5848 690 InstanceKlass::find_method(default_methods, name, signature) == NULL) {
acorn@5848 691 if (super == NULL) {
acorn@5848 692 // super doesn't exist
acorn@5848 693 return true;
acorn@5848 694 }
dsamersoff@2360 695
acorn@5848 696 Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature);
acorn@6145 697 while (mo != NULL && mo->access_flags().is_static()
acorn@6145 698 && mo->method_holder() != NULL
acorn@6145 699 && mo->method_holder()->super() != NULL)
acorn@6145 700 {
acorn@6145 701 mo = mo->method_holder()->super()->uncached_lookup_method(name, signature);
acorn@6145 702 }
acorn@5848 703 if (mo == NULL || mo->access_flags().is_private() ) {
acorn@5848 704 // super class hierarchy does not implement it or protection is different
acorn@5848 705 return true;
acorn@5848 706 }
duke@435 707 }
duke@435 708 }
dsamersoff@2360 709
duke@435 710 return false;
duke@435 711 }
duke@435 712
drchase@5732 713 // Scans current_interface_methods for miranda methods that do not
acorn@5848 714 // already appear in new_mirandas, or default methods, and are also not defined-and-non-private
drchase@5732 715 // in super (superclass). These mirandas are added to all_mirandas if it is
drchase@5732 716 // not null; in addition, those that are not duplicates of miranda methods
drchase@5732 717 // inherited by super from its interfaces are added to new_mirandas.
drchase@5732 718 // Thus, new_mirandas will be the set of mirandas that this class introduces,
drchase@5732 719 // all_mirandas will be the set of all mirandas applicable to this class
drchase@5732 720 // including all defined in superclasses.
kamg@4245 721 void klassVtable::add_new_mirandas_to_lists(
kamg@4245 722 GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,
kamg@4245 723 Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
acorn@5848 724 Array<Method*>* default_methods, Klass* super) {
acorn@5848 725
duke@435 726 // iterate thru the current interface's method to see if it a miranda
duke@435 727 int num_methods = current_interface_methods->length();
duke@435 728 for (int i = 0; i < num_methods; i++) {
coleenp@4037 729 Method* im = current_interface_methods->at(i);
duke@435 730 bool is_duplicate = false;
kamg@4245 731 int num_of_current_mirandas = new_mirandas->length();
duke@435 732 // check for duplicate mirandas in different interfaces we implement
duke@435 733 for (int j = 0; j < num_of_current_mirandas; j++) {
kamg@4245 734 Method* miranda = new_mirandas->at(j);
duke@435 735 if ((im->name() == miranda->name()) &&
duke@435 736 (im->signature() == miranda->signature())) {
duke@435 737 is_duplicate = true;
duke@435 738 break;
duke@435 739 }
duke@435 740 }
duke@435 741
duke@435 742 if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
acorn@5848 743 if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all?
coleenp@4037 744 InstanceKlass *sk = InstanceKlass::cast(super);
duke@435 745 // check if it is a duplicate of a super's miranda
hseigel@6195 746 if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), false) == NULL) {
kamg@4245 747 new_mirandas->append(im);
kamg@4245 748 }
kamg@4245 749 if (all_mirandas != NULL) {
kamg@4245 750 all_mirandas->append(im);
duke@435 751 }
duke@435 752 }
duke@435 753 }
duke@435 754 }
duke@435 755 }
duke@435 756
kamg@4245 757 void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
kamg@4245 758 GrowableArray<Method*>* all_mirandas,
coleenp@4037 759 Klass* super, Array<Method*>* class_methods,
acorn@5848 760 Array<Method*>* default_methods,
coleenp@4037 761 Array<Klass*>* local_interfaces) {
kamg@4245 762 assert((new_mirandas->length() == 0) , "current mirandas must be 0");
duke@435 763
duke@435 764 // iterate thru the local interfaces looking for a miranda
duke@435 765 int num_local_ifs = local_interfaces->length();
duke@435 766 for (int i = 0; i < num_local_ifs; i++) {
coleenp@4037 767 InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
kamg@4245 768 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
acorn@5848 769 ik->methods(), class_methods,
acorn@5848 770 default_methods, super);
duke@435 771 // iterate thru each local's super interfaces
coleenp@4037 772 Array<Klass*>* super_ifs = ik->transitive_interfaces();
duke@435 773 int num_super_ifs = super_ifs->length();
duke@435 774 for (int j = 0; j < num_super_ifs; j++) {
coleenp@4037 775 InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
kamg@4245 776 add_new_mirandas_to_lists(new_mirandas, all_mirandas,
acorn@5848 777 sik->methods(), class_methods,
acorn@5848 778 default_methods, super);
duke@435 779 }
duke@435 780 }
duke@435 781 }
duke@435 782
drchase@5732 783 // Discover miranda methods ("miranda" = "interface abstract, no binding"),
drchase@5732 784 // and append them into the vtable starting at index initialized,
drchase@5732 785 // return the new value of initialized.
acorn@6080 786 // Miranda methods use vtable entries, but do not get assigned a vtable_index
acorn@6080 787 // The vtable_index is discovered by searching from the end of the vtable
drchase@5732 788 int klassVtable::fill_in_mirandas(int initialized) {
kamg@4245 789 GrowableArray<Method*> mirandas(20);
kamg@4245 790 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
acorn@5848 791 ik()->default_methods(), ik()->local_interfaces());
kamg@4245 792 for (int i = 0; i < mirandas.length(); i++) {
acorn@5848 793 if (PrintVtables && Verbose) {
acorn@5848 794 Method* meth = mirandas.at(i);
acorn@5848 795 ResourceMark rm(Thread::current());
acorn@5848 796 if (meth != NULL) {
acorn@5848 797 char* sig = meth->name_and_sig_as_C_string();
acorn@5848 798 tty->print("fill in mirandas with %s index %d, flags: ",
acorn@5848 799 sig, initialized);
acorn@5848 800 meth->access_flags().print_on(tty);
acorn@5848 801 if (meth->is_default_method()) {
acorn@6080 802 tty->print("default ");
acorn@5848 803 }
acorn@5848 804 tty->cr();
acorn@5848 805 }
acorn@5848 806 }
drchase@5732 807 put_method_at(mirandas.at(i), initialized);
drchase@5732 808 ++initialized;
duke@435 809 }
drchase@5732 810 return initialized;
duke@435 811 }
duke@435 812
drchase@5732 813 // Copy this class's vtable to the vtable beginning at start.
drchase@5732 814 // Used to copy superclass vtable to prefix of subclass's vtable.
duke@435 815 void klassVtable::copy_vtable_to(vtableEntry* start) {
duke@435 816 Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
duke@435 817 }
duke@435 818
dcubed@4562 819 #if INCLUDE_JVMTI
acorn@5848 820 bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
acorn@5848 821 // If old_method is default, find this vtable index in default_vtable_indices
acorn@5848 822 // and replace that method in the _default_methods list
acorn@5848 823 bool updated = false;
acorn@5848 824
acorn@5848 825 Array<Method*>* default_methods = ik()->default_methods();
acorn@5848 826 if (default_methods != NULL) {
acorn@5848 827 int len = default_methods->length();
acorn@5848 828 for (int idx = 0; idx < len; idx++) {
acorn@5848 829 if (vtable_index == ik()->default_vtable_indices()->at(idx)) {
acorn@5848 830 if (default_methods->at(idx) == old_method) {
acorn@5848 831 default_methods->at_put(idx, new_method);
acorn@5848 832 updated = true;
acorn@5848 833 }
acorn@5848 834 break;
acorn@5848 835 }
acorn@5848 836 }
acorn@5848 837 }
acorn@5848 838 return updated;
acorn@5848 839 }
coleenp@4037 840 void klassVtable::adjust_method_entries(Method** old_methods, Method** new_methods,
duke@435 841 int methods_length, bool * trace_name_printed) {
duke@435 842 // search the vtable for uses of either obsolete or EMCP methods
duke@435 843 for (int j = 0; j < methods_length; j++) {
coleenp@4037 844 Method* old_method = old_methods[j];
coleenp@4037 845 Method* new_method = new_methods[j];
duke@435 846
duke@435 847 // In the vast majority of cases we could get the vtable index
duke@435 848 // by using: old_method->vtable_index()
duke@435 849 // However, there are rare cases, eg. sun.awt.X11.XDecoratedPeer.getX()
duke@435 850 // in sun.awt.X11.XFramePeer where methods occur more than once in the
duke@435 851 // vtable, so, alas, we must do an exhaustive search.
duke@435 852 for (int index = 0; index < length(); index++) {
duke@435 853 if (unchecked_method_at(index) == old_method) {
duke@435 854 put_method_at(new_method, index);
acorn@5848 855 // For default methods, need to update the _default_methods array
acorn@5848 856 // which can only have one method entry for a given signature
acorn@5848 857 bool updated_default = false;
acorn@5848 858 if (old_method->is_default_method()) {
acorn@5848 859 updated_default = adjust_default_method(index, old_method, new_method);
acorn@5848 860 }
duke@435 861
duke@435 862 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
duke@435 863 if (!(*trace_name_printed)) {
duke@435 864 // RC_TRACE_MESG macro has an embedded ResourceMark
acorn@5848 865 RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s",
acorn@5848 866 klass()->external_name(),
coleenp@4251 867 old_method->method_holder()->external_name()));
duke@435 868 *trace_name_printed = true;
duke@435 869 }
duke@435 870 // RC_TRACE macro has an embedded ResourceMark
acorn@5848 871 RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s",
duke@435 872 new_method->name()->as_C_string(),
acorn@5848 873 new_method->signature()->as_C_string(),
acorn@5848 874 updated_default ? "true" : "false"));
duke@435 875 }
dcubed@4562 876 // cannot 'break' here; see for-loop comment above.
duke@435 877 }
duke@435 878 }
duke@435 879 }
duke@435 880 }
duke@435 881
dcubed@4562 882 // a vtable should never contain old or obsolete methods
dcubed@4562 883 bool klassVtable::check_no_old_or_obsolete_entries() {
dcubed@4562 884 for (int i = 0; i < length(); i++) {
dcubed@4562 885 Method* m = unchecked_method_at(i);
dcubed@4562 886 if (m != NULL &&
dcubed@4562 887 (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
dcubed@4562 888 return false;
dcubed@4562 889 }
dcubed@4562 890 }
dcubed@4562 891 return true;
dcubed@4562 892 }
dcubed@4562 893
dcubed@4562 894 void klassVtable::dump_vtable() {
dcubed@4562 895 tty->print_cr("vtable dump --");
dcubed@4562 896 for (int i = 0; i < length(); i++) {
dcubed@4562 897 Method* m = unchecked_method_at(i);
dcubed@4562 898 if (m != NULL) {
dcubed@4562 899 tty->print(" (%5d) ", i);
dcubed@4562 900 m->access_flags().print_on(tty);
acorn@5848 901 if (m->is_default_method()) {
acorn@6080 902 tty->print("default ");
acorn@5848 903 }
acorn@5848 904 if (m->is_overpass()) {
acorn@5848 905 tty->print("overpass");
acorn@5848 906 }
dcubed@4562 907 tty->print(" -- ");
dcubed@4562 908 m->print_name(tty);
dcubed@4562 909 tty->cr();
dcubed@4562 910 }
dcubed@4562 911 }
dcubed@4562 912 }
dcubed@4562 913 #endif // INCLUDE_JVMTI
dcubed@4562 914
coleenp@2777 915 // CDS/RedefineClasses support - clear vtables so they can be reinitialized
coleenp@2777 916 void klassVtable::clear_vtable() {
coleenp@2777 917 for (int i = 0; i < _length; i++) table()[i].clear();
coleenp@2777 918 }
coleenp@2777 919
coleenp@2777 920 bool klassVtable::is_initialized() {
coleenp@2777 921 return _length == 0 || table()[0].method() != NULL;
coleenp@2777 922 }
coleenp@2777 923
duke@435 924 //-----------------------------------------------------------------------------------------
duke@435 925 // Itable code
duke@435 926
duke@435 927 // Initialize a itableMethodEntry
coleenp@4037 928 void itableMethodEntry::initialize(Method* m) {
duke@435 929 if (m == NULL) return;
duke@435 930
duke@435 931 _method = m;
duke@435 932 }
duke@435 933
duke@435 934 klassItable::klassItable(instanceKlassHandle klass) {
duke@435 935 _klass = klass;
duke@435 936
duke@435 937 if (klass->itable_length() > 0) {
duke@435 938 itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
duke@435 939 if (offset_entry != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
duke@435 940 // First offset entry points to the first method_entry
coleenp@4037 941 intptr_t* method_entry = (intptr_t *)(((address)klass()) + offset_entry->offset());
duke@435 942 intptr_t* end = klass->end_of_itable();
duke@435 943
coleenp@4037 944 _table_offset = (intptr_t*)offset_entry - (intptr_t*)klass();
duke@435 945 _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
duke@435 946 _size_method_table = (end - method_entry) / itableMethodEntry::size();
duke@435 947 assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
duke@435 948 return;
duke@435 949 }
duke@435 950 }
duke@435 951
dcubed@451 952 // The length of the itable was either zero, or it has not yet been initialized.
duke@435 953 _table_offset = 0;
duke@435 954 _size_offset_table = 0;
duke@435 955 _size_method_table = 0;
duke@435 956 }
duke@435 957
duke@435 958 static int initialize_count = 0;
duke@435 959
duke@435 960 // Initialization
duke@435 961 void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
drchase@5732 962 if (_klass->is_interface()) {
acorn@5848 963 // This needs to go after vtable indices are assigned but
acorn@5848 964 // before implementors need to know the number of itable indices.
acorn@5848 965 assign_itable_indices_for_interface(_klass());
drchase@5732 966 }
drchase@5732 967
dcubed@451 968 // Cannot be setup doing bootstrapping, interfaces don't have
dcubed@451 969 // itables, and klass with only ones entry have empty itables
dcubed@451 970 if (Universe::is_bootstrapping() ||
dcubed@451 971 _klass->is_interface() ||
dcubed@451 972 _klass->itable_length() == itableOffsetEntry::size()) return;
duke@435 973
dcubed@451 974 // There's alway an extra itable entry so we can null-terminate it.
dcubed@451 975 guarantee(size_offset_table() >= 1, "too small");
dcubed@451 976 int num_interfaces = size_offset_table() - 1;
duke@435 977 if (num_interfaces > 0) {
dcubed@451 978 if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count,
dcubed@451 979 _klass->name()->as_C_string());
duke@435 980
duke@435 981
acorn@1087 982 // Iterate through all interfaces
duke@435 983 int i;
duke@435 984 for(i = 0; i < num_interfaces; i++) {
duke@435 985 itableOffsetEntry* ioe = offset_entry(i);
coleenp@4037 986 HandleMark hm(THREAD);
duke@435 987 KlassHandle interf_h (THREAD, ioe->interface_klass());
duke@435 988 assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
duke@435 989 initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
duke@435 990 }
duke@435 991
duke@435 992 }
dcubed@451 993 // Check that the last entry is empty
dcubed@451 994 itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
dcubed@451 995 guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
duke@435 996 }
duke@435 997
duke@435 998
drchase@5732 999 inline bool interface_method_needs_itable_index(Method* m) {
drchase@5732 1000 if (m->is_static()) return false; // e.g., Stream.empty
drchase@5732 1001 if (m->is_initializer()) return false; // <init> or <clinit>
drchase@5732 1002 // If an interface redeclares a method from java.lang.Object,
drchase@5732 1003 // it should already have a vtable index, don't touch it.
drchase@5732 1004 // e.g., CharSequence.toString (from initialize_vtable)
drchase@5732 1005 // if (m->has_vtable_index()) return false; // NO!
drchase@5732 1006 return true;
drchase@5732 1007 }
drchase@5732 1008
acorn@5848 1009 int klassItable::assign_itable_indices_for_interface(Klass* klass) {
drchase@5732 1010 // an interface does not have an itable, but its methods need to be numbered
drchase@5732 1011 if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count,
drchase@5732 1012 klass->name()->as_C_string());
drchase@5732 1013 Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
drchase@5732 1014 int nof_methods = methods->length();
drchase@5732 1015 int ime_num = 0;
drchase@5732 1016 for (int i = 0; i < nof_methods; i++) {
drchase@5732 1017 Method* m = methods->at(i);
drchase@5732 1018 if (interface_method_needs_itable_index(m)) {
drchase@5732 1019 assert(!m->is_final_method(), "no final interface methods");
drchase@5732 1020 // If m is already assigned a vtable index, do not disturb it.
acorn@6080 1021 if (TraceItables && Verbose) {
acorn@6080 1022 ResourceMark rm;
acorn@6080 1023 const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
acorn@6080 1024 if (m->has_vtable_index()) {
acorn@6080 1025 tty->print("itable index %d for method: %s, flags: ", m->vtable_index(), sig);
acorn@6080 1026 } else {
acorn@6080 1027 tty->print("itable index %d for method: %s, flags: ", ime_num, sig);
acorn@6080 1028 }
acorn@6080 1029 if (m != NULL) {
acorn@6080 1030 m->access_flags().print_on(tty);
acorn@6080 1031 if (m->is_default_method()) {
acorn@6080 1032 tty->print("default ");
acorn@6080 1033 }
acorn@6080 1034 if (m->is_overpass()) {
acorn@6080 1035 tty->print("overpass");
acorn@6080 1036 }
acorn@6080 1037 }
acorn@6080 1038 tty->cr();
acorn@6080 1039 }
drchase@5732 1040 if (!m->has_vtable_index()) {
drchase@5732 1041 assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable");
drchase@5732 1042 m->set_itable_index(ime_num);
drchase@5732 1043 // Progress to next itable entry
drchase@5732 1044 ime_num++;
drchase@5732 1045 }
drchase@5732 1046 }
drchase@5732 1047 }
drchase@5732 1048 assert(ime_num == method_count_for_interface(klass), "proper sizing");
drchase@5732 1049 return ime_num;
drchase@5732 1050 }
drchase@5732 1051
drchase@5732 1052 int klassItable::method_count_for_interface(Klass* interf) {
drchase@5732 1053 assert(interf->oop_is_instance(), "must be");
drchase@5732 1054 assert(interf->is_interface(), "must be");
drchase@5732 1055 Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
drchase@5732 1056 int nof_methods = methods->length();
drchase@5732 1057 while (nof_methods > 0) {
drchase@5732 1058 Method* m = methods->at(nof_methods-1);
drchase@5732 1059 if (m->has_itable_index()) {
drchase@5732 1060 int length = m->itable_index() + 1;
drchase@5732 1061 #ifdef ASSERT
drchase@5732 1062 while (nof_methods = 0) {
drchase@5732 1063 m = methods->at(--nof_methods);
drchase@5732 1064 assert(!m->has_itable_index() || m->itable_index() < length, "");
drchase@5732 1065 }
drchase@5732 1066 #endif //ASSERT
drchase@5732 1067 return length; // return the rightmost itable index, plus one
drchase@5732 1068 }
drchase@5732 1069 nof_methods -= 1;
drchase@5732 1070 }
acorn@5848 1071 // no methods have itable indices
drchase@5732 1072 return 0;
drchase@5732 1073 }
drchase@5732 1074
drchase@5732 1075
duke@435 1076 void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
coleenp@4037 1077 Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods();
coleenp@4037 1078 int nof_methods = methods->length();
duke@435 1079 HandleMark hm;
jcoomes@1844 1080 assert(nof_methods > 0, "at least one method must exist for interface to be in vtable");
coleenp@4037 1081 Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader());
duke@435 1082
drchase@5732 1083 int ime_count = method_count_for_interface(interf_h());
drchase@5732 1084 for (int i = 0; i < nof_methods; i++) {
coleenp@4037 1085 Method* m = methods->at(i);
drchase@5732 1086 methodHandle target;
drchase@5732 1087 if (m->has_itable_index()) {
hseigel@6195 1088 // This search must match the runtime resolution, i.e. selection search for invokeinterface
hseigel@6195 1089 // to correctly enforce loader constraints for interface method inheritance
drchase@5732 1090 LinkResolver::lookup_instance_method_in_klasses(target, _klass, m->name(), m->signature(), CHECK);
duke@435 1091 }
duke@435 1092 if (target == NULL || !target->is_public() || target->is_abstract()) {
drchase@6134 1093 // Entry does not resolve. Leave it empty for AbstractMethodError.
drchase@6134 1094 if (!(target == NULL) && !target->is_public()) {
drchase@6134 1095 // Stuff an IllegalAccessError throwing method in there instead.
drchase@6134 1096 itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()].
drchase@6134 1097 initialize(Universe::throw_illegal_access_error());
drchase@6134 1098 }
duke@435 1099 } else {
duke@435 1100 // Entry did resolve, check loader constraints before initializing
duke@435 1101 // if checkconstraints requested
duke@435 1102 if (checkconstraints) {
coleenp@4251 1103 Handle method_holder_loader (THREAD, target->method_holder()->class_loader());
duke@435 1104 if (method_holder_loader() != interface_loader()) {
duke@435 1105 ResourceMark rm(THREAD);
acorn@4840 1106 Symbol* failed_type_symbol =
drchase@5732 1107 SystemDictionary::check_signature_loaders(m->signature(),
duke@435 1108 method_holder_loader,
duke@435 1109 interface_loader,
duke@435 1110 true, CHECK);
acorn@4840 1111 if (failed_type_symbol != NULL) {
duke@435 1112 const char* msg = "loader constraint violation in interface "
duke@435 1113 "itable initialization: when resolving method \"%s\" the class"
duke@435 1114 " loader (instance of %s) of the current class, %s, "
duke@435 1115 "and the class loader (instance of %s) for interface "
duke@435 1116 "%s have different Class objects for the type %s "
duke@435 1117 "used in the signature";
drchase@5732 1118 char* sig = target()->name_and_sig_as_C_string();
duke@435 1119 const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
drchase@5732 1120 char* current = _klass->name()->as_C_string();
duke@435 1121 const char* loader2 = SystemDictionary::loader_name(interface_loader());
coleenp@4037 1122 char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
acorn@4840 1123 char* failed_type_name = failed_type_symbol->as_C_string();
duke@435 1124 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
duke@435 1125 strlen(current) + strlen(loader2) + strlen(iface) +
duke@435 1126 strlen(failed_type_name);
duke@435 1127 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
duke@435 1128 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
duke@435 1129 iface, failed_type_name);
duke@435 1130 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
duke@435 1131 }
duke@435 1132 }
duke@435 1133 }
duke@435 1134
duke@435 1135 // ime may have moved during GC so recalculate address
drchase@5732 1136 int ime_num = m->itable_index();
drchase@5732 1137 assert(ime_num < ime_count, "oob");
drchase@5732 1138 itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
acorn@5848 1139 if (TraceItables && Verbose) {
acorn@5848 1140 ResourceMark rm(THREAD);
acorn@5848 1141 if (target() != NULL) {
acorn@5848 1142 char* sig = target()->name_and_sig_as_C_string();
acorn@5848 1143 tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
acorn@5848 1144 interf_h()->internal_name(), ime_num, sig,
acorn@5848 1145 target()->method_holder()->internal_name());
acorn@5848 1146 tty->print("target_method flags: ");
acorn@5848 1147 target()->access_flags().print_on(tty);
acorn@5848 1148 if (target()->is_default_method()) {
acorn@6080 1149 tty->print("default ");
acorn@5848 1150 }
acorn@5848 1151 tty->cr();
acorn@5848 1152 }
acorn@5848 1153 }
duke@435 1154 }
duke@435 1155 }
duke@435 1156 }
duke@435 1157
coleenp@4037 1158 // Update entry for specific Method*
coleenp@4037 1159 void klassItable::initialize_with_method(Method* m) {
duke@435 1160 itableMethodEntry* ime = method_entry(0);
duke@435 1161 for(int i = 0; i < _size_method_table; i++) {
duke@435 1162 if (ime->method() == m) {
duke@435 1163 ime->initialize(m);
duke@435 1164 }
duke@435 1165 ime++;
duke@435 1166 }
duke@435 1167 }
duke@435 1168
dcubed@4562 1169 #if INCLUDE_JVMTI
coleenp@4037 1170 void klassItable::adjust_method_entries(Method** old_methods, Method** new_methods,
duke@435 1171 int methods_length, bool * trace_name_printed) {
duke@435 1172 // search the itable for uses of either obsolete or EMCP methods
duke@435 1173 for (int j = 0; j < methods_length; j++) {
coleenp@4037 1174 Method* old_method = old_methods[j];
coleenp@4037 1175 Method* new_method = new_methods[j];
duke@435 1176 itableMethodEntry* ime = method_entry(0);
duke@435 1177
dcubed@1045 1178 // The itable can describe more than one interface and the same
dcubed@1045 1179 // method signature can be specified by more than one interface.
dcubed@1045 1180 // This means we have to do an exhaustive search to find all the
dcubed@1045 1181 // old_method references.
duke@435 1182 for (int i = 0; i < _size_method_table; i++) {
duke@435 1183 if (ime->method() == old_method) {
duke@435 1184 ime->initialize(new_method);
duke@435 1185
duke@435 1186 if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
duke@435 1187 if (!(*trace_name_printed)) {
duke@435 1188 // RC_TRACE_MESG macro has an embedded ResourceMark
duke@435 1189 RC_TRACE_MESG(("adjust: name=%s",
coleenp@4251 1190 old_method->method_holder()->external_name()));
duke@435 1191 *trace_name_printed = true;
duke@435 1192 }
duke@435 1193 // RC_TRACE macro has an embedded ResourceMark
duke@435 1194 RC_TRACE(0x00200000, ("itable method update: %s(%s)",
duke@435 1195 new_method->name()->as_C_string(),
duke@435 1196 new_method->signature()->as_C_string()));
duke@435 1197 }
dcubed@4562 1198 // cannot 'break' here; see for-loop comment above.
duke@435 1199 }
duke@435 1200 ime++;
duke@435 1201 }
duke@435 1202 }
duke@435 1203 }
duke@435 1204
dcubed@4562 1205 // an itable should never contain old or obsolete methods
dcubed@4562 1206 bool klassItable::check_no_old_or_obsolete_entries() {
dcubed@4562 1207 itableMethodEntry* ime = method_entry(0);
dcubed@4562 1208 for (int i = 0; i < _size_method_table; i++) {
dcubed@4562 1209 Method* m = ime->method();
dcubed@4562 1210 if (m != NULL &&
dcubed@4562 1211 (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
dcubed@4562 1212 return false;
dcubed@4562 1213 }
dcubed@4562 1214 ime++;
dcubed@4562 1215 }
dcubed@4562 1216 return true;
dcubed@4562 1217 }
dcubed@4562 1218
dcubed@4562 1219 void klassItable::dump_itable() {
dcubed@4562 1220 itableMethodEntry* ime = method_entry(0);
dcubed@4562 1221 tty->print_cr("itable dump --");
dcubed@4562 1222 for (int i = 0; i < _size_method_table; i++) {
dcubed@4562 1223 Method* m = ime->method();
dcubed@4562 1224 if (m != NULL) {
dcubed@4562 1225 tty->print(" (%5d) ", i);
dcubed@4562 1226 m->access_flags().print_on(tty);
acorn@5848 1227 if (m->is_default_method()) {
acorn@6080 1228 tty->print("default ");
acorn@5848 1229 }
dcubed@4562 1230 tty->print(" -- ");
dcubed@4562 1231 m->print_name(tty);
dcubed@4562 1232 tty->cr();
dcubed@4562 1233 }
dcubed@4562 1234 ime++;
dcubed@4562 1235 }
dcubed@4562 1236 }
dcubed@4562 1237 #endif // INCLUDE_JVMTI
dcubed@4562 1238
duke@435 1239
duke@435 1240 // Setup
duke@435 1241 class InterfaceVisiterClosure : public StackObj {
duke@435 1242 public:
coleenp@4037 1243 virtual void doit(Klass* intf, int method_count) = 0;
duke@435 1244 };
duke@435 1245
drchase@5732 1246 // Visit all interfaces with at least one itable method
coleenp@4037 1247 void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) {
duke@435 1248 // Handle array argument
duke@435 1249 for(int i = 0; i < transitive_intf->length(); i++) {
coleenp@4037 1250 Klass* intf = transitive_intf->at(i);
hseigel@4278 1251 assert(intf->is_interface(), "sanity check");
duke@435 1252
drchase@5732 1253 // Find no. of itable methods
drchase@5732 1254 int method_count = 0;
drchase@5732 1255 // method_count = klassItable::method_count_for_interface(intf);
drchase@5732 1256 Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
drchase@5732 1257 if (methods->length() > 0) {
drchase@5732 1258 for (int i = methods->length(); --i >= 0; ) {
drchase@5732 1259 if (interface_method_needs_itable_index(methods->at(i))) {
drchase@5732 1260 method_count++;
drchase@5732 1261 }
duke@435 1262 }
duke@435 1263 }
duke@435 1264
duke@435 1265 // Only count interfaces with at least one method
duke@435 1266 if (method_count > 0) {
duke@435 1267 blk->doit(intf, method_count);
duke@435 1268 }
duke@435 1269 }
duke@435 1270 }
duke@435 1271
duke@435 1272 class CountInterfacesClosure : public InterfaceVisiterClosure {
duke@435 1273 private:
duke@435 1274 int _nof_methods;
duke@435 1275 int _nof_interfaces;
duke@435 1276 public:
duke@435 1277 CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }
duke@435 1278
duke@435 1279 int nof_methods() const { return _nof_methods; }
duke@435 1280 int nof_interfaces() const { return _nof_interfaces; }
duke@435 1281
coleenp@4037 1282 void doit(Klass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }
duke@435 1283 };
duke@435 1284
duke@435 1285 class SetupItableClosure : public InterfaceVisiterClosure {
duke@435 1286 private:
duke@435 1287 itableOffsetEntry* _offset_entry;
duke@435 1288 itableMethodEntry* _method_entry;
duke@435 1289 address _klass_begin;
duke@435 1290 public:
duke@435 1291 SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {
duke@435 1292 _klass_begin = klass_begin;
duke@435 1293 _offset_entry = offset_entry;
duke@435 1294 _method_entry = method_entry;
duke@435 1295 }
duke@435 1296
duke@435 1297 itableMethodEntry* method_entry() const { return _method_entry; }
duke@435 1298
coleenp@4037 1299 void doit(Klass* intf, int method_count) {
duke@435 1300 int offset = ((address)_method_entry) - _klass_begin;
duke@435 1301 _offset_entry->initialize(intf, offset);
duke@435 1302 _offset_entry++;
duke@435 1303 _method_entry += method_count;
duke@435 1304 }
duke@435 1305 };
duke@435 1306
coleenp@4037 1307 int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) {
duke@435 1308 // Count no of interfaces and total number of interface methods
duke@435 1309 CountInterfacesClosure cic;
coleenp@4037 1310 visit_all_interfaces(transitive_interfaces, &cic);
duke@435 1311
dcubed@451 1312 // There's alway an extra itable entry so we can null-terminate it.
dcubed@451 1313 int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
duke@435 1314
duke@435 1315 // Statistics
duke@435 1316 update_stats(itable_size * HeapWordSize);
duke@435 1317
duke@435 1318 return itable_size;
duke@435 1319 }
duke@435 1320
duke@435 1321
duke@435 1322 // Fill out offset table and interface klasses into the itable space
duke@435 1323 void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {
duke@435 1324 if (klass->itable_length() == 0) return;
duke@435 1325 assert(!klass->is_interface(), "Should have zero length itable");
duke@435 1326
duke@435 1327 // Count no of interfaces and total number of interface methods
duke@435 1328 CountInterfacesClosure cic;
duke@435 1329 visit_all_interfaces(klass->transitive_interfaces(), &cic);
duke@435 1330 int nof_methods = cic.nof_methods();
duke@435 1331 int nof_interfaces = cic.nof_interfaces();
duke@435 1332
dcubed@451 1333 // Add one extra entry so we can null-terminate the table
dcubed@451 1334 nof_interfaces++;
duke@435 1335
coleenp@4037 1336 assert(compute_itable_size(klass->transitive_interfaces()) ==
duke@435 1337 calc_itable_size(nof_interfaces, nof_methods),
duke@435 1338 "mismatch calculation of itable size");
duke@435 1339
duke@435 1340 // Fill-out offset table
duke@435 1341 itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
duke@435 1342 itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
duke@435 1343 intptr_t* end = klass->end_of_itable();
never@2658 1344 assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
coleenp@548 1345 assert((oop*)(end) == (oop*)(ime + nof_methods), "wrong offset calculation (2)");
duke@435 1346
duke@435 1347 // Visit all interfaces and initialize itable offset table
coleenp@4037 1348 SetupItableClosure sic((address)klass(), ioe, ime);
duke@435 1349 visit_all_interfaces(klass->transitive_interfaces(), &sic);
duke@435 1350
duke@435 1351 #ifdef ASSERT
duke@435 1352 ime = sic.method_entry();
duke@435 1353 oop* v = (oop*) klass->end_of_itable();
duke@435 1354 assert( (oop*)(ime) == v, "wrong offset calculation (2)");
duke@435 1355 #endif
duke@435 1356 }
duke@435 1357
duke@435 1358
drchase@5732 1359 // inverse to itable_index
coleenp@4037 1360 Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) {
coleenp@4037 1361 assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");
drchase@5732 1362 assert(intf->verify_itable_index(itable_index), "");
coleenp@4037 1363 Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
jrose@1100 1364
drchase@5732 1365 if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
acorn@5848 1366 return NULL; // help caller defend against bad indices
jrose@1100 1367
drchase@5732 1368 int index = itable_index;
coleenp@4037 1369 Method* m = methods->at(index);
drchase@5732 1370 int index2 = -1;
drchase@5732 1371 while (!m->has_itable_index() ||
drchase@5732 1372 (index2 = m->itable_index()) != itable_index) {
drchase@5732 1373 assert(index2 < itable_index, "monotonic");
drchase@5732 1374 if (++index == methods->length())
drchase@5732 1375 return NULL;
drchase@5732 1376 m = methods->at(index);
drchase@5732 1377 }
drchase@5732 1378 assert(m->itable_index() == itable_index, "correct inverse");
jrose@1100 1379
jrose@1100 1380 return m;
jrose@1100 1381 }
jrose@1100 1382
duke@435 1383 void klassVtable::verify(outputStream* st, bool forced) {
duke@435 1384 // make sure table is initialized
duke@435 1385 if (!Universe::is_fully_initialized()) return;
duke@435 1386 #ifndef PRODUCT
duke@435 1387 // avoid redundant verifies
duke@435 1388 if (!forced && _verify_count == Universe::verify_count()) return;
duke@435 1389 _verify_count = Universe::verify_count();
duke@435 1390 #endif
duke@435 1391 oop* end_of_obj = (oop*)_klass() + _klass()->size();
duke@435 1392 oop* end_of_vtable = (oop *)&table()[_length];
duke@435 1393 if (end_of_vtable > end_of_obj) {
jcoomes@1845 1394 fatal(err_msg("klass %s: klass object too short (vtable extends beyond "
jcoomes@1845 1395 "end)", _klass->internal_name()));
duke@435 1396 }
duke@435 1397
duke@435 1398 for (int i = 0; i < _length; i++) table()[i].verify(this, st);
duke@435 1399 // verify consistency with superKlass vtable
coleenp@4037 1400 Klass* super = _klass->super();
duke@435 1401 if (super != NULL) {
coleenp@4037 1402 InstanceKlass* sk = InstanceKlass::cast(super);
duke@435 1403 klassVtable* vt = sk->vtable();
duke@435 1404 for (int i = 0; i < vt->length(); i++) {
duke@435 1405 verify_against(st, vt, i);
duke@435 1406 }
duke@435 1407 }
duke@435 1408 }
duke@435 1409
duke@435 1410 void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
duke@435 1411 vtableEntry* vte = &vt->table()[index];
duke@435 1412 if (vte->method()->name() != table()[index].method()->name() ||
duke@435 1413 vte->method()->signature() != table()[index].method()->signature()) {
duke@435 1414 fatal("mismatched name/signature of vtable entries");
duke@435 1415 }
duke@435 1416 }
duke@435 1417
duke@435 1418 #ifndef PRODUCT
duke@435 1419 void klassVtable::print() {
duke@435 1420 ResourceMark rm;
duke@435 1421 tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());
duke@435 1422 for (int i = 0; i < length(); i++) {
duke@435 1423 table()[i].print();
duke@435 1424 tty->cr();
duke@435 1425 }
duke@435 1426 }
duke@435 1427 #endif
duke@435 1428
duke@435 1429 void vtableEntry::verify(klassVtable* vt, outputStream* st) {
duke@435 1430 NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
duke@435 1431 assert(method() != NULL, "must have set method");
duke@435 1432 method()->verify();
duke@435 1433 // we sub_type, because it could be a miranda method
duke@435 1434 if (!vt->klass()->is_subtype_of(method()->method_holder())) {
duke@435 1435 #ifndef PRODUCT
duke@435 1436 print();
duke@435 1437 #endif
jcoomes@1845 1438 fatal(err_msg("vtableEntry " PTR_FORMAT ": method is from subclass", this));
duke@435 1439 }
duke@435 1440 }
duke@435 1441
duke@435 1442 #ifndef PRODUCT
duke@435 1443
duke@435 1444 void vtableEntry::print() {
duke@435 1445 ResourceMark rm;
duke@435 1446 tty->print("vtableEntry %s: ", method()->name()->as_C_string());
duke@435 1447 if (Verbose) {
duke@435 1448 tty->print("m %#lx ", (address)method());
duke@435 1449 }
duke@435 1450 }
duke@435 1451
duke@435 1452 class VtableStats : AllStatic {
duke@435 1453 public:
duke@435 1454 static int no_klasses; // # classes with vtables
duke@435 1455 static int no_array_klasses; // # array classes
duke@435 1456 static int no_instance_klasses; // # instanceKlasses
duke@435 1457 static int sum_of_vtable_len; // total # of vtable entries
duke@435 1458 static int sum_of_array_vtable_len; // total # of vtable entries in array klasses only
duke@435 1459 static int fixed; // total fixed overhead in bytes
duke@435 1460 static int filler; // overhead caused by filler bytes
duke@435 1461 static int entries; // total bytes consumed by vtable entries
duke@435 1462 static int array_entries; // total bytes consumed by array vtable entries
duke@435 1463
coleenp@4037 1464 static void do_class(Klass* k) {
coleenp@4037 1465 Klass* kl = k;
duke@435 1466 klassVtable* vt = kl->vtable();
duke@435 1467 if (vt == NULL) return;
duke@435 1468 no_klasses++;
duke@435 1469 if (kl->oop_is_instance()) {
duke@435 1470 no_instance_klasses++;
duke@435 1471 kl->array_klasses_do(do_class);
duke@435 1472 }
duke@435 1473 if (kl->oop_is_array()) {
duke@435 1474 no_array_klasses++;
duke@435 1475 sum_of_array_vtable_len += vt->length();
duke@435 1476 }
duke@435 1477 sum_of_vtable_len += vt->length();
duke@435 1478 }
duke@435 1479
duke@435 1480 static void compute() {
duke@435 1481 SystemDictionary::classes_do(do_class);
duke@435 1482 fixed = no_klasses * oopSize; // vtable length
duke@435 1483 // filler size is a conservative approximation
coleenp@4142 1484 filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(InstanceKlass) - sizeof(ArrayKlass) - 1);
duke@435 1485 entries = sizeof(vtableEntry) * sum_of_vtable_len;
duke@435 1486 array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len;
duke@435 1487 }
duke@435 1488 };
duke@435 1489
duke@435 1490 int VtableStats::no_klasses = 0;
duke@435 1491 int VtableStats::no_array_klasses = 0;
duke@435 1492 int VtableStats::no_instance_klasses = 0;
duke@435 1493 int VtableStats::sum_of_vtable_len = 0;
duke@435 1494 int VtableStats::sum_of_array_vtable_len = 0;
duke@435 1495 int VtableStats::fixed = 0;
duke@435 1496 int VtableStats::filler = 0;
duke@435 1497 int VtableStats::entries = 0;
duke@435 1498 int VtableStats::array_entries = 0;
duke@435 1499
duke@435 1500 void klassVtable::print_statistics() {
duke@435 1501 ResourceMark rm;
duke@435 1502 HandleMark hm;
duke@435 1503 VtableStats::compute();
duke@435 1504 tty->print_cr("vtable statistics:");
duke@435 1505 tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses);
duke@435 1506 int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries;
duke@435 1507 tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed);
duke@435 1508 tty->print_cr("%6d bytes filler overhead", VtableStats::filler);
duke@435 1509 tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries);
duke@435 1510 tty->print_cr("%6d bytes total", total);
duke@435 1511 }
duke@435 1512
duke@435 1513 int klassItable::_total_classes; // Total no. of classes with itables
duke@435 1514 long klassItable::_total_size; // Total no. of bytes used for itables
duke@435 1515
duke@435 1516 void klassItable::print_statistics() {
duke@435 1517 tty->print_cr("itable statistics:");
duke@435 1518 tty->print_cr("%6d classes with itables", _total_classes);
duke@435 1519 tty->print_cr("%6d K uses for itables (average by class: %d bytes)", _total_size / K, _total_size / _total_classes);
duke@435 1520 }
duke@435 1521
duke@435 1522 #endif // PRODUCT

mercurial