src/share/vm/oops/klassVtable.cpp

Thu, 29 May 2014 09:56:06 -0700

author
asaha
date
Thu, 29 May 2014 09:56:06 -0700
changeset 6782
f73af4455d7d
parent 6779
364b73402247
parent 6680
78bbf4d43a14
child 6876
710a3c8b516e
child 7636
fdde6a70ea85
child 7720
02e2c04a3289
permissions
-rw-r--r--

Merge

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

mercurial