annotate src/share/vm/oops/klassVtable.cpp @ 5699:9d15b81d5d1b

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