src/share/vm/oops/klassVtable.cpp

Thu, 22 May 2014 15:52:41 -0400

author
drchase
date
Thu, 22 May 2014 15:52:41 -0400
changeset 6680
78bbf4d43a14
parent 6632
386dd1c71858
child 6782
f73af4455d7d
permissions
-rw-r--r--

8037816: Fix for 8036122 breaks build with Xcode5/clang
8043029: Change 8037816 breaks HS build with older GCC versions which don't support diagnostic pragmas
8043164: Format warning in traceStream.hpp
Summary: Backport of main fix + two corrections, enables clang compilation, turns on format attributes, corrects/mutes warnings
Reviewed-by: kvn, coleenp, iveresov, twisti

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

mercurial