src/share/vm/oops/klassVtable.cpp

Thu, 04 Apr 2019 17:56:29 +0800

author
aoqi
date
Thu, 04 Apr 2019 17:56:29 +0800
changeset 9572
624a0741915c
parent 9041
95a08233f46c
child 9756
2be326848943
permissions
-rw-r--r--

Merge

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

mercurial