src/share/vm/oops/klassVtable.cpp

Tue, 05 Nov 2013 17:38:04 -0800

author
kvn
date
Tue, 05 Nov 2013 17:38:04 -0800
changeset 6472
2b8e28fdf503
parent 5848
ac9cb1d5a202
child 6080
fce21ac5968d
permissions
-rw-r--r--

Merge

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

mercurial