annotate src/share/vm/oops/klassVtable.cpp @ 13584:2dfe246179c0

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