src/share/vm/oops/klassVtable.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/oops/klassVtable.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,1545 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/systemDictionary.hpp"
    1.30 +#include "classfile/vmSymbols.hpp"
    1.31 +#include "gc_implementation/shared/markSweep.inline.hpp"
    1.32 +#include "memory/gcLocker.hpp"
    1.33 +#include "memory/resourceArea.hpp"
    1.34 +#include "memory/universe.inline.hpp"
    1.35 +#include "oops/instanceKlass.hpp"
    1.36 +#include "oops/klassVtable.hpp"
    1.37 +#include "oops/method.hpp"
    1.38 +#include "oops/objArrayOop.hpp"
    1.39 +#include "oops/oop.inline.hpp"
    1.40 +#include "prims/jvmtiRedefineClassesTrace.hpp"
    1.41 +#include "runtime/arguments.hpp"
    1.42 +#include "runtime/handles.inline.hpp"
    1.43 +#include "utilities/copy.hpp"
    1.44 +
    1.45 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
    1.46 +
    1.47 +inline InstanceKlass* klassVtable::ik() const {
    1.48 +  Klass* k = _klass();
    1.49 +  assert(k->oop_is_instance(), "not an InstanceKlass");
    1.50 +  return (InstanceKlass*)k;
    1.51 +}
    1.52 +
    1.53 +
    1.54 +// this function computes the vtable size (including the size needed for miranda
    1.55 +// methods) and the number of miranda methods in this class.
    1.56 +// Note on Miranda methods: Let's say there is a class C that implements
    1.57 +// interface I, and none of C's superclasses implements I.
    1.58 +// Let's say there is an abstract method m in I that neither C
    1.59 +// nor any of its super classes implement (i.e there is no method of any access,
    1.60 +// with the same name and signature as m), then m is a Miranda method which is
    1.61 +// entered as a public abstract method in C's vtable.  From then on it should
    1.62 +// treated as any other public method in C for method over-ride purposes.
    1.63 +void klassVtable::compute_vtable_size_and_num_mirandas(
    1.64 +    int* vtable_length_ret, int* num_new_mirandas,
    1.65 +    GrowableArray<Method*>* all_mirandas, Klass* super,
    1.66 +    Array<Method*>* methods, AccessFlags class_flags,
    1.67 +    Handle classloader, Symbol* classname, Array<Klass*>* local_interfaces,
    1.68 +    TRAPS) {
    1.69 +  No_Safepoint_Verifier nsv;
    1.70 +
    1.71 +  // set up default result values
    1.72 +  int vtable_length = 0;
    1.73 +
    1.74 +  // start off with super's vtable length
    1.75 +  InstanceKlass* sk = (InstanceKlass*)super;
    1.76 +  vtable_length = super == NULL ? 0 : sk->vtable_length();
    1.77 +
    1.78 +  // go thru each method in the methods table to see if it needs a new entry
    1.79 +  int len = methods->length();
    1.80 +  for (int i = 0; i < len; i++) {
    1.81 +    assert(methods->at(i)->is_method(), "must be a Method*");
    1.82 +    methodHandle mh(THREAD, methods->at(i));
    1.83 +
    1.84 +    if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, THREAD)) {
    1.85 +      vtable_length += vtableEntry::size(); // we need a new entry
    1.86 +    }
    1.87 +  }
    1.88 +
    1.89 +  GrowableArray<Method*> new_mirandas(20);
    1.90 +  // compute the number of mirandas methods that must be added to the end
    1.91 +  get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces);
    1.92 +  *num_new_mirandas = new_mirandas.length();
    1.93 +
    1.94 +  // Interfaces do not need interface methods in their vtables
    1.95 +  // This includes miranda methods and during later processing, default methods
    1.96 +  if (!class_flags.is_interface()) {
    1.97 +    vtable_length += *num_new_mirandas * vtableEntry::size();
    1.98 +  }
    1.99 +
   1.100 +  if (Universe::is_bootstrapping() && vtable_length == 0) {
   1.101 +    // array classes don't have their superclass set correctly during
   1.102 +    // bootstrapping
   1.103 +    vtable_length = Universe::base_vtable_size();
   1.104 +  }
   1.105 +
   1.106 +  if (super == NULL && !Universe::is_bootstrapping() &&
   1.107 +      vtable_length != Universe::base_vtable_size()) {
   1.108 +    // Someone is attempting to redefine java.lang.Object incorrectly.  The
   1.109 +    // only way this should happen is from
   1.110 +    // SystemDictionary::resolve_from_stream(), which will detect this later
   1.111 +    // and throw a security exception.  So don't assert here to let
   1.112 +    // the exception occur.
   1.113 +    vtable_length = Universe::base_vtable_size();
   1.114 +  }
   1.115 +  assert(super != NULL || vtable_length == Universe::base_vtable_size(),
   1.116 +         "bad vtable size for class Object");
   1.117 +  assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");
   1.118 +  assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");
   1.119 +
   1.120 +  *vtable_length_ret = vtable_length;
   1.121 +}
   1.122 +
   1.123 +int klassVtable::index_of(Method* m, int len) const {
   1.124 +  assert(m->has_vtable_index(), "do not ask this of non-vtable methods");
   1.125 +  return m->vtable_index();
   1.126 +}
   1.127 +
   1.128 +// Copy super class's vtable to the first part (prefix) of this class's vtable,
   1.129 +// and return the number of entries copied.  Expects that 'super' is the Java
   1.130 +// super class (arrays can have "array" super classes that must be skipped).
   1.131 +int klassVtable::initialize_from_super(KlassHandle super) {
   1.132 +  if (super.is_null()) {
   1.133 +    return 0;
   1.134 +  } else {
   1.135 +    // copy methods from superKlass
   1.136 +    // can't inherit from array class, so must be InstanceKlass
   1.137 +    assert(super->oop_is_instance(), "must be instance klass");
   1.138 +    InstanceKlass* sk = (InstanceKlass*)super();
   1.139 +    klassVtable* superVtable = sk->vtable();
   1.140 +    assert(superVtable->length() <= _length, "vtable too short");
   1.141 +#ifdef ASSERT
   1.142 +    superVtable->verify(tty, true);
   1.143 +#endif
   1.144 +    superVtable->copy_vtable_to(table());
   1.145 +#ifndef PRODUCT
   1.146 +    if (PrintVtables && Verbose) {
   1.147 +      ResourceMark rm;
   1.148 +      tty->print_cr("copy vtable from %s to %s size %d", sk->internal_name(), klass()->internal_name(), _length);
   1.149 +    }
   1.150 +#endif
   1.151 +    return superVtable->length();
   1.152 +  }
   1.153 +}
   1.154 +
   1.155 +//
   1.156 +// Revised lookup semantics   introduced 1.3 (Kestrel beta)
   1.157 +void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
   1.158 +
   1.159 +  // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
   1.160 +  KlassHandle super (THREAD, klass()->java_super());
   1.161 +  int nofNewEntries = 0;
   1.162 +
   1.163 +  if (PrintVtables && !klass()->oop_is_array()) {
   1.164 +    ResourceMark rm(THREAD);
   1.165 +    tty->print_cr("Initializing: %s", _klass->name()->as_C_string());
   1.166 +  }
   1.167 +
   1.168 +#ifdef ASSERT
   1.169 +  oop* end_of_obj = (oop*)_klass() + _klass()->size();
   1.170 +  oop* end_of_vtable = (oop*)&table()[_length];
   1.171 +  assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
   1.172 +#endif
   1.173 +
   1.174 +  if (Universe::is_bootstrapping()) {
   1.175 +    // just clear everything
   1.176 +    for (int i = 0; i < _length; i++) table()[i].clear();
   1.177 +    return;
   1.178 +  }
   1.179 +
   1.180 +  int super_vtable_len = initialize_from_super(super);
   1.181 +  if (klass()->oop_is_array()) {
   1.182 +    assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
   1.183 +  } else {
   1.184 +    assert(_klass->oop_is_instance(), "must be InstanceKlass");
   1.185 +
   1.186 +    Array<Method*>* methods = ik()->methods();
   1.187 +    int len = methods->length();
   1.188 +    int initialized = super_vtable_len;
   1.189 +
   1.190 +    // Check each of this class's methods against super;
   1.191 +    // if override, replace in copy of super vtable, otherwise append to end
   1.192 +    for (int i = 0; i < len; i++) {
   1.193 +      // update_inherited_vtable can stop for gc - ensure using handles
   1.194 +      HandleMark hm(THREAD);
   1.195 +      assert(methods->at(i)->is_method(), "must be a Method*");
   1.196 +      methodHandle mh(THREAD, methods->at(i));
   1.197 +
   1.198 +      bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK);
   1.199 +
   1.200 +      if (needs_new_entry) {
   1.201 +        put_method_at(mh(), initialized);
   1.202 +        mh()->set_vtable_index(initialized); // set primary vtable index
   1.203 +        initialized++;
   1.204 +      }
   1.205 +    }
   1.206 +
   1.207 +    // update vtable with default_methods
   1.208 +    Array<Method*>* default_methods = ik()->default_methods();
   1.209 +    if (default_methods != NULL) {
   1.210 +      len = default_methods->length();
   1.211 +      if (len > 0) {
   1.212 +        Array<int>* def_vtable_indices = NULL;
   1.213 +        if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) {
   1.214 +          def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK);
   1.215 +        } else {
   1.216 +          assert(def_vtable_indices->length() == len, "reinit vtable len?");
   1.217 +        }
   1.218 +        for (int i = 0; i < len; i++) {
   1.219 +          HandleMark hm(THREAD);
   1.220 +          assert(default_methods->at(i)->is_method(), "must be a Method*");
   1.221 +          methodHandle mh(THREAD, default_methods->at(i));
   1.222 +
   1.223 +          bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK);
   1.224 +
   1.225 +          // needs new entry
   1.226 +          if (needs_new_entry) {
   1.227 +            put_method_at(mh(), initialized);
   1.228 +            def_vtable_indices->at_put(i, initialized); //set vtable index
   1.229 +            initialized++;
   1.230 +          }
   1.231 +        }
   1.232 +      }
   1.233 +    }
   1.234 +
   1.235 +    // add miranda methods; it will also return the updated initialized
   1.236 +    // Interfaces do not need interface methods in their vtables
   1.237 +    // This includes miranda methods and during later processing, default methods
   1.238 +    if (!ik()->is_interface()) {
   1.239 +      initialized = fill_in_mirandas(initialized);
   1.240 +    }
   1.241 +
   1.242 +    // In class hierarchies where the accessibility is not increasing (i.e., going from private ->
   1.243 +    // package_private -> public/protected), the vtable might actually be smaller than our initial
   1.244 +    // calculation.
   1.245 +    assert(initialized <= _length, "vtable initialization failed");
   1.246 +    for(;initialized < _length; initialized++) {
   1.247 +      put_method_at(NULL, initialized);
   1.248 +    }
   1.249 +    NOT_PRODUCT(verify(tty, true));
   1.250 +  }
   1.251 +}
   1.252 +
   1.253 +// Called for cases where a method does not override its superclass' vtable entry
   1.254 +// For bytecodes not produced by javac together it is possible that a method does not override
   1.255 +// the superclass's method, but might indirectly override a super-super class's vtable entry
   1.256 +// If none found, return a null superk, else return the superk of the method this does override
   1.257 +// For public and protected methods: if they override a superclass, they will
   1.258 +// also be overridden themselves appropriately.
   1.259 +// Private methods do not override and are not overridden.
   1.260 +// Package Private methods are trickier:
   1.261 +// e.g. P1.A, pub m
   1.262 +// P2.B extends A, package private m
   1.263 +// P1.C extends B, public m
   1.264 +// P1.C.m needs to override P1.A.m and can not override P2.B.m
   1.265 +// Therefore: all package private methods need their own vtable entries for
   1.266 +// them to be the root of an inheritance overriding decision
   1.267 +// Package private methods may also override other vtable entries
   1.268 +InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method,
   1.269 +                            int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) {
   1.270 +  InstanceKlass* superk = initialsuper;
   1.271 +  while (superk != NULL && superk->super() != NULL) {
   1.272 +    InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super());
   1.273 +    klassVtable* ssVtable = supersuperklass->vtable();
   1.274 +    if (vtable_index < ssVtable->length()) {
   1.275 +      Method* super_method = ssVtable->method_at(vtable_index);
   1.276 +#ifndef PRODUCT
   1.277 +      Symbol* name= target_method()->name();
   1.278 +      Symbol* signature = target_method()->signature();
   1.279 +      assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");
   1.280 +#endif
   1.281 +      if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) {
   1.282 +#ifndef PRODUCT
   1.283 +        if (PrintVtables && Verbose) {
   1.284 +          ResourceMark rm(THREAD);
   1.285 +          char* sig = target_method()->name_and_sig_as_C_string();
   1.286 +          tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",
   1.287 +           supersuperklass->internal_name(),
   1.288 +           _klass->internal_name(), sig, vtable_index);
   1.289 +           super_method->access_flags().print_on(tty);
   1.290 +           if (super_method->is_default_method()) {
   1.291 +             tty->print("default ");
   1.292 +           }
   1.293 +           tty->print("overriders flags: ");
   1.294 +           target_method->access_flags().print_on(tty);
   1.295 +           if (target_method->is_default_method()) {
   1.296 +             tty->print("default ");
   1.297 +           }
   1.298 +        }
   1.299 +#endif /*PRODUCT*/
   1.300 +        break; // return found superk
   1.301 +      }
   1.302 +    } else  {
   1.303 +      // super class has no vtable entry here, stop transitive search
   1.304 +      superk = (InstanceKlass*)NULL;
   1.305 +      break;
   1.306 +    }
   1.307 +    // if no override found yet, continue to search up
   1.308 +    superk = InstanceKlass::cast(superk->super());
   1.309 +  }
   1.310 +
   1.311 +  return superk;
   1.312 +}
   1.313 +
   1.314 +// Update child's copy of super vtable for overrides
   1.315 +// OR return true if a new vtable entry is required.
   1.316 +// Only called for InstanceKlass's, i.e. not for arrays
   1.317 +// If that changed, could not use _klass as handle for klass
   1.318 +bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,
   1.319 +                                          int super_vtable_len, int default_index,
   1.320 +                                          bool checkconstraints, TRAPS) {
   1.321 +  ResourceMark rm;
   1.322 +  bool allocate_new = true;
   1.323 +  assert(klass->oop_is_instance(), "must be InstanceKlass");
   1.324 +
   1.325 +  Array<int>* def_vtable_indices = NULL;
   1.326 +  bool is_default = false;
   1.327 +  // default methods are concrete methods in superinterfaces which are added to the vtable
   1.328 +  // with their real method_holder
   1.329 +  // Since vtable and itable indices share the same storage, don't touch
   1.330 +  // the default method's real vtable/itable index
   1.331 +  // default_vtable_indices stores the vtable value relative to this inheritor
   1.332 +  if (default_index >= 0 ) {
   1.333 +    is_default = true;
   1.334 +    def_vtable_indices = klass->default_vtable_indices();
   1.335 +    assert(def_vtable_indices != NULL, "def vtable alloc?");
   1.336 +    assert(default_index <= def_vtable_indices->length(), "def vtable len?");
   1.337 +  } else {
   1.338 +    assert(klass == target_method()->method_holder(), "caller resp.");
   1.339 +    // Initialize the method's vtable index to "nonvirtual".
   1.340 +    // If we allocate a vtable entry, we will update it to a non-negative number.
   1.341 +    target_method()->set_vtable_index(Method::nonvirtual_vtable_index);
   1.342 +  }
   1.343 +
   1.344 +  // Static and <init> methods are never in
   1.345 +  if (target_method()->is_static() || target_method()->name() ==  vmSymbols::object_initializer_name()) {
   1.346 +    return false;
   1.347 +  }
   1.348 +
   1.349 +  if (target_method->is_final_method(klass->access_flags())) {
   1.350 +    // a final method never needs a new entry; final methods can be statically
   1.351 +    // resolved and they have to be present in the vtable only if they override
   1.352 +    // a super's method, in which case they re-use its entry
   1.353 +    allocate_new = false;
   1.354 +  } else if (klass->is_interface()) {
   1.355 +    allocate_new = false;  // see note below in needs_new_vtable_entry
   1.356 +    // An interface never allocates new vtable slots, only inherits old ones.
   1.357 +    // This method will either be assigned its own itable index later,
   1.358 +    // or be assigned an inherited vtable index in the loop below.
   1.359 +    // default methods inherited by classes store their vtable indices
   1.360 +    // in the inheritor's default_vtable_indices
   1.361 +    // default methods inherited by interfaces may already have a
   1.362 +    // valid itable index, if so, don't change it
   1.363 +    // overpass methods in an interface will be assigned an itable index later
   1.364 +    // by an inheriting class
   1.365 +    if (!is_default || !target_method()->has_itable_index()) {
   1.366 +      target_method()->set_vtable_index(Method::pending_itable_index);
   1.367 +    }
   1.368 +  }
   1.369 +
   1.370 +  // we need a new entry if there is no superclass
   1.371 +  if (klass->super() == NULL) {
   1.372 +    return allocate_new;
   1.373 +  }
   1.374 +
   1.375 +  // private methods in classes always have a new entry in the vtable
   1.376 +  // specification interpretation since classic has
   1.377 +  // private methods not overriding
   1.378 +  // JDK8 adds private methods in interfaces which require invokespecial
   1.379 +  if (target_method()->is_private()) {
   1.380 +    return allocate_new;
   1.381 +  }
   1.382 +
   1.383 +  // search through the vtable and update overridden entries
   1.384 +  // Since check_signature_loaders acquires SystemDictionary_lock
   1.385 +  // which can block for gc, once we are in this loop, use handles
   1.386 +  // For classfiles built with >= jdk7, we now look for transitive overrides
   1.387 +
   1.388 +  Symbol* name = target_method()->name();
   1.389 +  Symbol* signature = target_method()->signature();
   1.390 +
   1.391 +  KlassHandle target_klass(THREAD, target_method()->method_holder());
   1.392 +  if (target_klass == NULL) {
   1.393 +    target_klass = _klass;
   1.394 +  }
   1.395 +
   1.396 +  Handle target_loader(THREAD, target_klass->class_loader());
   1.397 +
   1.398 +  Symbol* target_classname = target_klass->name();
   1.399 +  for(int i = 0; i < super_vtable_len; i++) {
   1.400 +    Method* super_method = method_at(i);
   1.401 +    // Check if method name matches
   1.402 +    if (super_method->name() == name && super_method->signature() == signature) {
   1.403 +
   1.404 +      // get super_klass for method_holder for the found method
   1.405 +      InstanceKlass* super_klass =  super_method->method_holder();
   1.406 +
   1.407 +      if (is_default
   1.408 +          || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD))
   1.409 +          || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION)
   1.410 +          && ((super_klass = find_transitive_override(super_klass,
   1.411 +                             target_method, i, target_loader,
   1.412 +                             target_classname, THREAD))
   1.413 +                             != (InstanceKlass*)NULL))))
   1.414 +        {
   1.415 +        // Package private methods always need a new entry to root their own
   1.416 +        // overriding. They may also override other methods.
   1.417 +        if (!target_method()->is_package_private()) {
   1.418 +          allocate_new = false;
   1.419 +        }
   1.420 +
   1.421 +        if (checkconstraints) {
   1.422 +        // Override vtable entry if passes loader constraint check
   1.423 +        // if loader constraint checking requested
   1.424 +        // No need to visit his super, since he and his super
   1.425 +        // have already made any needed loader constraints.
   1.426 +        // Since loader constraints are transitive, it is enough
   1.427 +        // to link to the first super, and we get all the others.
   1.428 +          Handle super_loader(THREAD, super_klass->class_loader());
   1.429 +
   1.430 +          if (target_loader() != super_loader()) {
   1.431 +            ResourceMark rm(THREAD);
   1.432 +            Symbol* failed_type_symbol =
   1.433 +              SystemDictionary::check_signature_loaders(signature, target_loader,
   1.434 +                                                        super_loader, true,
   1.435 +                                                        CHECK_(false));
   1.436 +            if (failed_type_symbol != NULL) {
   1.437 +              const char* msg = "loader constraint violation: when resolving "
   1.438 +                "overridden method \"%s\" the class loader (instance"
   1.439 +                " of %s) of the current class, %s, and its superclass loader "
   1.440 +                "(instance of %s), have different Class objects for the type "
   1.441 +                "%s used in the signature";
   1.442 +              char* sig = target_method()->name_and_sig_as_C_string();
   1.443 +              const char* loader1 = SystemDictionary::loader_name(target_loader());
   1.444 +              char* current = target_klass->name()->as_C_string();
   1.445 +              const char* loader2 = SystemDictionary::loader_name(super_loader());
   1.446 +              char* failed_type_name = failed_type_symbol->as_C_string();
   1.447 +              size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
   1.448 +                strlen(current) + strlen(loader2) + strlen(failed_type_name);
   1.449 +              char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
   1.450 +              jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
   1.451 +                           failed_type_name);
   1.452 +              THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
   1.453 +            }
   1.454 +          }
   1.455 +       }
   1.456 +
   1.457 +       put_method_at(target_method(), i);
   1.458 +       if (!is_default) {
   1.459 +         target_method()->set_vtable_index(i);
   1.460 +       } else {
   1.461 +         if (def_vtable_indices != NULL) {
   1.462 +           def_vtable_indices->at_put(default_index, i);
   1.463 +         }
   1.464 +         assert(super_method->is_default_method() || super_method->is_overpass()
   1.465 +                || super_method->is_abstract(), "default override error");
   1.466 +       }
   1.467 +
   1.468 +
   1.469 +#ifndef PRODUCT
   1.470 +        if (PrintVtables && Verbose) {
   1.471 +          ResourceMark rm(THREAD);
   1.472 +          char* sig = target_method()->name_and_sig_as_C_string();
   1.473 +          tty->print("overriding with %s::%s index %d, original flags: ",
   1.474 +           target_klass->internal_name(), sig, i);
   1.475 +           super_method->access_flags().print_on(tty);
   1.476 +           if (super_method->is_default_method()) {
   1.477 +             tty->print("default ");
   1.478 +           }
   1.479 +           if (super_method->is_overpass()) {
   1.480 +             tty->print("overpass");
   1.481 +           }
   1.482 +           tty->print("overriders flags: ");
   1.483 +           target_method->access_flags().print_on(tty);
   1.484 +           if (target_method->is_default_method()) {
   1.485 +             tty->print("default ");
   1.486 +           }
   1.487 +           if (target_method->is_overpass()) {
   1.488 +             tty->print("overpass");
   1.489 +           }
   1.490 +           tty->cr();
   1.491 +        }
   1.492 +#endif /*PRODUCT*/
   1.493 +      } else {
   1.494 +        // allocate_new = true; default. We might override one entry,
   1.495 +        // but not override another. Once we override one, not need new
   1.496 +#ifndef PRODUCT
   1.497 +        if (PrintVtables && Verbose) {
   1.498 +          ResourceMark rm(THREAD);
   1.499 +          char* sig = target_method()->name_and_sig_as_C_string();
   1.500 +          tty->print("NOT overriding with %s::%s index %d, original flags: ",
   1.501 +           target_klass->internal_name(), sig,i);
   1.502 +           super_method->access_flags().print_on(tty);
   1.503 +           if (super_method->is_default_method()) {
   1.504 +             tty->print("default ");
   1.505 +           }
   1.506 +           if (super_method->is_overpass()) {
   1.507 +             tty->print("overpass");
   1.508 +           }
   1.509 +           tty->print("overriders flags: ");
   1.510 +           target_method->access_flags().print_on(tty);
   1.511 +           if (target_method->is_default_method()) {
   1.512 +             tty->print("default ");
   1.513 +           }
   1.514 +           if (target_method->is_overpass()) {
   1.515 +             tty->print("overpass");
   1.516 +           }
   1.517 +           tty->cr();
   1.518 +        }
   1.519 +#endif /*PRODUCT*/
   1.520 +      }
   1.521 +    }
   1.522 +  }
   1.523 +  return allocate_new;
   1.524 +}
   1.525 +
   1.526 +void klassVtable::put_method_at(Method* m, int index) {
   1.527 +#ifndef PRODUCT
   1.528 +  if (PrintVtables && Verbose) {
   1.529 +    ResourceMark rm;
   1.530 +    const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
   1.531 +    tty->print("adding %s at index %d, flags: ", sig, index);
   1.532 +    if (m != NULL) {
   1.533 +      m->access_flags().print_on(tty);
   1.534 +      if (m->is_default_method()) {
   1.535 +        tty->print("default ");
   1.536 +      }
   1.537 +      if (m->is_overpass()) {
   1.538 +        tty->print("overpass");
   1.539 +      }
   1.540 +    }
   1.541 +    tty->cr();
   1.542 +  }
   1.543 +#endif
   1.544 +  table()[index].set(m);
   1.545 +}
   1.546 +
   1.547 +// Find out if a method "m" with superclass "super", loader "classloader" and
   1.548 +// name "classname" needs a new vtable entry.  Let P be a class package defined
   1.549 +// by "classloader" and "classname".
   1.550 +// NOTE: The logic used here is very similar to the one used for computing
   1.551 +// the vtables indices for a method. We cannot directly use that function because,
   1.552 +// we allocate the InstanceKlass at load time, and that requires that the
   1.553 +// superclass has been loaded.
   1.554 +// However, the vtable entries are filled in at link time, and therefore
   1.555 +// the superclass' vtable may not yet have been filled in.
   1.556 +bool klassVtable::needs_new_vtable_entry(methodHandle target_method,
   1.557 +                                         Klass* super,
   1.558 +                                         Handle classloader,
   1.559 +                                         Symbol* classname,
   1.560 +                                         AccessFlags class_flags,
   1.561 +                                         TRAPS) {
   1.562 +  if (class_flags.is_interface()) {
   1.563 +    // Interfaces do not use vtables, except for java.lang.Object methods,
   1.564 +    // so there is no point to assigning
   1.565 +    // a vtable index to any of their local methods.  If we refrain from doing this,
   1.566 +    // we can use Method::_vtable_index to hold the itable index
   1.567 +    return false;
   1.568 +  }
   1.569 +
   1.570 +  if (target_method->is_final_method(class_flags) ||
   1.571 +      // a final method never needs a new entry; final methods can be statically
   1.572 +      // resolved and they have to be present in the vtable only if they override
   1.573 +      // a super's method, in which case they re-use its entry
   1.574 +      (target_method()->is_static()) ||
   1.575 +      // static methods don't need to be in vtable
   1.576 +      (target_method()->name() ==  vmSymbols::object_initializer_name())
   1.577 +      // <init> is never called dynamically-bound
   1.578 +      ) {
   1.579 +    return false;
   1.580 +  }
   1.581 +
   1.582 +  // Concrete interface methods do not need new entries, they override
   1.583 +  // abstract method entries using default inheritance rules
   1.584 +  if (target_method()->method_holder() != NULL &&
   1.585 +      target_method()->method_holder()->is_interface()  &&
   1.586 +      !target_method()->is_abstract() ) {
   1.587 +    return false;
   1.588 +  }
   1.589 +
   1.590 +  // we need a new entry if there is no superclass
   1.591 +  if (super == NULL) {
   1.592 +    return true;
   1.593 +  }
   1.594 +
   1.595 +  // private methods in classes always have a new entry in the vtable
   1.596 +  // specification interpretation since classic has
   1.597 +  // private methods not overriding
   1.598 +  // JDK8 adds private  methods in interfaces which require invokespecial
   1.599 +  if (target_method()->is_private()) {
   1.600 +    return true;
   1.601 +  }
   1.602 +
   1.603 +  // Package private methods always need a new entry to root their own
   1.604 +  // overriding. This allows transitive overriding to work.
   1.605 +  if (target_method()->is_package_private()) {
   1.606 +    return true;
   1.607 +  }
   1.608 +
   1.609 +  // search through the super class hierarchy to see if we need
   1.610 +  // a new entry
   1.611 +  ResourceMark rm;
   1.612 +  Symbol* name = target_method()->name();
   1.613 +  Symbol* signature = target_method()->signature();
   1.614 +  Klass* k = super;
   1.615 +  Method* super_method = NULL;
   1.616 +  InstanceKlass *holder = NULL;
   1.617 +  Method* recheck_method =  NULL;
   1.618 +  while (k != NULL) {
   1.619 +    // lookup through the hierarchy for a method with matching name and sign.
   1.620 +    super_method = InstanceKlass::cast(k)->lookup_method(name, signature);
   1.621 +    if (super_method == NULL) {
   1.622 +      break; // we still have to search for a matching miranda method
   1.623 +    }
   1.624 +    // get the class holding the matching method
   1.625 +    // make sure you use that class for is_override
   1.626 +    InstanceKlass* superk = super_method->method_holder();
   1.627 +    // we want only instance method matches
   1.628 +    // pretend private methods are not in the super vtable
   1.629 +    // since we do override around them: e.g. a.m pub/b.m private/c.m pub,
   1.630 +    // ignore private, c.m pub does override a.m pub
   1.631 +    // For classes that were not javac'd together, we also do transitive overriding around
   1.632 +    // methods that have less accessibility
   1.633 +    if ((!super_method->is_static()) &&
   1.634 +       (!super_method->is_private())) {
   1.635 +      if (superk->is_override(super_method, classloader, classname, THREAD)) {
   1.636 +        return false;
   1.637 +      // else keep looking for transitive overrides
   1.638 +      }
   1.639 +    }
   1.640 +
   1.641 +    // Start with lookup result and continue to search up
   1.642 +    k = superk->super(); // haven't found an override match yet; continue to look
   1.643 +  }
   1.644 +
   1.645 +  // if the target method is public or protected it may have a matching
   1.646 +  // miranda method in the super, whose entry it should re-use.
   1.647 +  // Actually, to handle cases that javac would not generate, we need
   1.648 +  // this check for all access permissions.
   1.649 +  InstanceKlass *sk = InstanceKlass::cast(super);
   1.650 +  if (sk->has_miranda_methods()) {
   1.651 +    if (sk->lookup_method_in_all_interfaces(name, signature, Klass::normal) != NULL) {
   1.652 +      return false;  // found a matching miranda; we do not need a new entry
   1.653 +    }
   1.654 +  }
   1.655 +  return true; // found no match; we need a new entry
   1.656 +}
   1.657 +
   1.658 +// Support for miranda methods
   1.659 +
   1.660 +// get the vtable index of a miranda method with matching "name" and "signature"
   1.661 +int klassVtable::index_of_miranda(Symbol* name, Symbol* signature) {
   1.662 +  // search from the bottom, might be faster
   1.663 +  for (int i = (length() - 1); i >= 0; i--) {
   1.664 +    Method* m = table()[i].method();
   1.665 +    if (is_miranda_entry_at(i) &&
   1.666 +        m->name() == name && m->signature() == signature) {
   1.667 +      return i;
   1.668 +    }
   1.669 +  }
   1.670 +  return Method::invalid_vtable_index;
   1.671 +}
   1.672 +
   1.673 +// check if an entry at an index is miranda
   1.674 +// requires that method m at entry be declared ("held") by an interface.
   1.675 +bool klassVtable::is_miranda_entry_at(int i) {
   1.676 +  Method* m = method_at(i);
   1.677 +  Klass* method_holder = m->method_holder();
   1.678 +  InstanceKlass *mhk = InstanceKlass::cast(method_holder);
   1.679 +
   1.680 +  // miranda methods are public abstract instance interface methods in a class's vtable
   1.681 +  if (mhk->is_interface()) {
   1.682 +    assert(m->is_public(), "should be public");
   1.683 +    assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
   1.684 +    // the search could find a miranda or a default method
   1.685 +    if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) {
   1.686 +      return true;
   1.687 +    }
   1.688 +  }
   1.689 +  return false;
   1.690 +}
   1.691 +
   1.692 +// check if a method is a miranda method, given a class's methods table,
   1.693 +// its default_method table  and its super
   1.694 +// Miranda methods are calculated twice:
   1.695 +// first: before vtable size calculation: including abstract and default
   1.696 +// This is seen by default method creation
   1.697 +// Second: recalculated during vtable initialization: only abstract
   1.698 +// This is seen by link resolution and selection.
   1.699 +// "miranda" means not static, not defined by this class.
   1.700 +// private methods in interfaces do not belong in the miranda list.
   1.701 +// the caller must make sure that the method belongs to an interface implemented by the class
   1.702 +// Miranda methods only include public interface instance methods
   1.703 +// Not private methods, not static methods, not default == concrete abstract
   1.704 +// Miranda methods also do not include overpass methods in interfaces
   1.705 +bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,
   1.706 +                             Array<Method*>* default_methods, Klass* super) {
   1.707 +  if (m->is_static() || m->is_private() || m->is_overpass()) {
   1.708 +    return false;
   1.709 +  }
   1.710 +  Symbol* name = m->name();
   1.711 +  Symbol* signature = m->signature();
   1.712 +
   1.713 +  if (InstanceKlass::find_instance_method(class_methods, name, signature) == NULL) {
   1.714 +    // did not find it in the method table of the current class
   1.715 +    if ((default_methods == NULL) ||
   1.716 +        InstanceKlass::find_method(default_methods, name, signature) == NULL) {
   1.717 +      if (super == NULL) {
   1.718 +        // super doesn't exist
   1.719 +        return true;
   1.720 +      }
   1.721 +
   1.722 +      Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature);
   1.723 +      while (mo != NULL && mo->access_flags().is_static()
   1.724 +             && mo->method_holder() != NULL
   1.725 +             && mo->method_holder()->super() != NULL)
   1.726 +      {
   1.727 +         mo = mo->method_holder()->super()->uncached_lookup_method(name, signature, Klass::normal);
   1.728 +      }
   1.729 +      if (mo == NULL || mo->access_flags().is_private() ) {
   1.730 +        // super class hierarchy does not implement it or protection is different
   1.731 +        return true;
   1.732 +      }
   1.733 +    }
   1.734 +  }
   1.735 +
   1.736 +  return false;
   1.737 +}
   1.738 +
   1.739 +// Scans current_interface_methods for miranda methods that do not
   1.740 +// already appear in new_mirandas, or default methods,  and are also not defined-and-non-private
   1.741 +// in super (superclass).  These mirandas are added to all_mirandas if it is
   1.742 +// not null; in addition, those that are not duplicates of miranda methods
   1.743 +// inherited by super from its interfaces are added to new_mirandas.
   1.744 +// Thus, new_mirandas will be the set of mirandas that this class introduces,
   1.745 +// all_mirandas will be the set of all mirandas applicable to this class
   1.746 +// including all defined in superclasses.
   1.747 +void klassVtable::add_new_mirandas_to_lists(
   1.748 +    GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,
   1.749 +    Array<Method*>* current_interface_methods, Array<Method*>* class_methods,
   1.750 +    Array<Method*>* default_methods, Klass* super) {
   1.751 +
   1.752 +  // iterate thru the current interface's method to see if it a miranda
   1.753 +  int num_methods = current_interface_methods->length();
   1.754 +  for (int i = 0; i < num_methods; i++) {
   1.755 +    Method* im = current_interface_methods->at(i);
   1.756 +    bool is_duplicate = false;
   1.757 +    int num_of_current_mirandas = new_mirandas->length();
   1.758 +    // check for duplicate mirandas in different interfaces we implement
   1.759 +    for (int j = 0; j < num_of_current_mirandas; j++) {
   1.760 +      Method* miranda = new_mirandas->at(j);
   1.761 +      if ((im->name() == miranda->name()) &&
   1.762 +          (im->signature() == miranda->signature())) {
   1.763 +        is_duplicate = true;
   1.764 +        break;
   1.765 +      }
   1.766 +    }
   1.767 +
   1.768 +    if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
   1.769 +      if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all?
   1.770 +        InstanceKlass *sk = InstanceKlass::cast(super);
   1.771 +        // check if it is a duplicate of a super's miranda
   1.772 +        if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::normal) == NULL) {
   1.773 +          new_mirandas->append(im);
   1.774 +        }
   1.775 +        if (all_mirandas != NULL) {
   1.776 +          all_mirandas->append(im);
   1.777 +        }
   1.778 +      }
   1.779 +    }
   1.780 +  }
   1.781 +}
   1.782 +
   1.783 +void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,
   1.784 +                               GrowableArray<Method*>* all_mirandas,
   1.785 +                               Klass* super, Array<Method*>* class_methods,
   1.786 +                               Array<Method*>* default_methods,
   1.787 +                               Array<Klass*>* local_interfaces) {
   1.788 +  assert((new_mirandas->length() == 0) , "current mirandas must be 0");
   1.789 +
   1.790 +  // iterate thru the local interfaces looking for a miranda
   1.791 +  int num_local_ifs = local_interfaces->length();
   1.792 +  for (int i = 0; i < num_local_ifs; i++) {
   1.793 +    InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));
   1.794 +    add_new_mirandas_to_lists(new_mirandas, all_mirandas,
   1.795 +                              ik->methods(), class_methods,
   1.796 +                              default_methods, super);
   1.797 +    // iterate thru each local's super interfaces
   1.798 +    Array<Klass*>* super_ifs = ik->transitive_interfaces();
   1.799 +    int num_super_ifs = super_ifs->length();
   1.800 +    for (int j = 0; j < num_super_ifs; j++) {
   1.801 +      InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));
   1.802 +      add_new_mirandas_to_lists(new_mirandas, all_mirandas,
   1.803 +                                sik->methods(), class_methods,
   1.804 +                                default_methods, super);
   1.805 +    }
   1.806 +  }
   1.807 +}
   1.808 +
   1.809 +// Discover miranda methods ("miranda" = "interface abstract, no binding"),
   1.810 +// and append them into the vtable starting at index initialized,
   1.811 +// return the new value of initialized.
   1.812 +// Miranda methods use vtable entries, but do not get assigned a vtable_index
   1.813 +// The vtable_index is discovered by searching from the end of the vtable
   1.814 +int klassVtable::fill_in_mirandas(int initialized) {
   1.815 +  GrowableArray<Method*> mirandas(20);
   1.816 +  get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),
   1.817 +               ik()->default_methods(), ik()->local_interfaces());
   1.818 +  for (int i = 0; i < mirandas.length(); i++) {
   1.819 +    if (PrintVtables && Verbose) {
   1.820 +      Method* meth = mirandas.at(i);
   1.821 +      ResourceMark rm(Thread::current());
   1.822 +      if (meth != NULL) {
   1.823 +        char* sig = meth->name_and_sig_as_C_string();
   1.824 +        tty->print("fill in mirandas with %s index %d, flags: ",
   1.825 +          sig, initialized);
   1.826 +        meth->access_flags().print_on(tty);
   1.827 +        if (meth->is_default_method()) {
   1.828 +          tty->print("default ");
   1.829 +        }
   1.830 +        tty->cr();
   1.831 +      }
   1.832 +    }
   1.833 +    put_method_at(mirandas.at(i), initialized);
   1.834 +    ++initialized;
   1.835 +  }
   1.836 +  return initialized;
   1.837 +}
   1.838 +
   1.839 +// Copy this class's vtable to the vtable beginning at start.
   1.840 +// Used to copy superclass vtable to prefix of subclass's vtable.
   1.841 +void klassVtable::copy_vtable_to(vtableEntry* start) {
   1.842 +  Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
   1.843 +}
   1.844 +
   1.845 +#if INCLUDE_JVMTI
   1.846 +bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {
   1.847 +  // If old_method is default, find this vtable index in default_vtable_indices
   1.848 +  // and replace that method in the _default_methods list
   1.849 +  bool updated = false;
   1.850 +
   1.851 +  Array<Method*>* default_methods = ik()->default_methods();
   1.852 +  if (default_methods != NULL) {
   1.853 +    int len = default_methods->length();
   1.854 +    for (int idx = 0; idx < len; idx++) {
   1.855 +      if (vtable_index == ik()->default_vtable_indices()->at(idx)) {
   1.856 +        if (default_methods->at(idx) == old_method) {
   1.857 +          default_methods->at_put(idx, new_method);
   1.858 +          updated = true;
   1.859 +        }
   1.860 +        break;
   1.861 +      }
   1.862 +    }
   1.863 +  }
   1.864 +  return updated;
   1.865 +}
   1.866 +void klassVtable::adjust_method_entries(Method** old_methods, Method** new_methods,
   1.867 +                                        int methods_length, bool * trace_name_printed) {
   1.868 +  // search the vtable for uses of either obsolete or EMCP methods
   1.869 +  for (int j = 0; j < methods_length; j++) {
   1.870 +    Method* old_method = old_methods[j];
   1.871 +    Method* new_method = new_methods[j];
   1.872 +
   1.873 +    // In the vast majority of cases we could get the vtable index
   1.874 +    // by using:  old_method->vtable_index()
   1.875 +    // However, there are rare cases, eg. sun.awt.X11.XDecoratedPeer.getX()
   1.876 +    // in sun.awt.X11.XFramePeer where methods occur more than once in the
   1.877 +    // vtable, so, alas, we must do an exhaustive search.
   1.878 +    for (int index = 0; index < length(); index++) {
   1.879 +      if (unchecked_method_at(index) == old_method) {
   1.880 +        put_method_at(new_method, index);
   1.881 +          // For default methods, need to update the _default_methods array
   1.882 +          // which can only have one method entry for a given signature
   1.883 +          bool updated_default = false;
   1.884 +          if (old_method->is_default_method()) {
   1.885 +            updated_default = adjust_default_method(index, old_method, new_method);
   1.886 +          }
   1.887 +
   1.888 +        if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
   1.889 +          if (!(*trace_name_printed)) {
   1.890 +            // RC_TRACE_MESG macro has an embedded ResourceMark
   1.891 +            RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s",
   1.892 +                           klass()->external_name(),
   1.893 +                           old_method->method_holder()->external_name()));
   1.894 +            *trace_name_printed = true;
   1.895 +          }
   1.896 +          // RC_TRACE macro has an embedded ResourceMark
   1.897 +          RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s",
   1.898 +                                new_method->name()->as_C_string(),
   1.899 +                                new_method->signature()->as_C_string(),
   1.900 +                                updated_default ? "true" : "false"));
   1.901 +        }
   1.902 +        // cannot 'break' here; see for-loop comment above.
   1.903 +      }
   1.904 +    }
   1.905 +  }
   1.906 +}
   1.907 +
   1.908 +// a vtable should never contain old or obsolete methods
   1.909 +bool klassVtable::check_no_old_or_obsolete_entries() {
   1.910 +  for (int i = 0; i < length(); i++) {
   1.911 +    Method* m = unchecked_method_at(i);
   1.912 +    if (m != NULL &&
   1.913 +        (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
   1.914 +      return false;
   1.915 +    }
   1.916 +  }
   1.917 +  return true;
   1.918 +}
   1.919 +
   1.920 +void klassVtable::dump_vtable() {
   1.921 +  tty->print_cr("vtable dump --");
   1.922 +  for (int i = 0; i < length(); i++) {
   1.923 +    Method* m = unchecked_method_at(i);
   1.924 +    if (m != NULL) {
   1.925 +      tty->print("      (%5d)  ", i);
   1.926 +      m->access_flags().print_on(tty);
   1.927 +      if (m->is_default_method()) {
   1.928 +        tty->print("default ");
   1.929 +      }
   1.930 +      if (m->is_overpass()) {
   1.931 +        tty->print("overpass");
   1.932 +      }
   1.933 +      tty->print(" --  ");
   1.934 +      m->print_name(tty);
   1.935 +      tty->cr();
   1.936 +    }
   1.937 +  }
   1.938 +}
   1.939 +#endif // INCLUDE_JVMTI
   1.940 +
   1.941 +// CDS/RedefineClasses support - clear vtables so they can be reinitialized
   1.942 +void klassVtable::clear_vtable() {
   1.943 +  for (int i = 0; i < _length; i++) table()[i].clear();
   1.944 +}
   1.945 +
   1.946 +bool klassVtable::is_initialized() {
   1.947 +  return _length == 0 || table()[0].method() != NULL;
   1.948 +}
   1.949 +
   1.950 +//-----------------------------------------------------------------------------------------
   1.951 +// Itable code
   1.952 +
   1.953 +// Initialize a itableMethodEntry
   1.954 +void itableMethodEntry::initialize(Method* m) {
   1.955 +  if (m == NULL) return;
   1.956 +
   1.957 +  _method = m;
   1.958 +}
   1.959 +
   1.960 +klassItable::klassItable(instanceKlassHandle klass) {
   1.961 +  _klass = klass;
   1.962 +
   1.963 +  if (klass->itable_length() > 0) {
   1.964 +    itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
   1.965 +    if (offset_entry  != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
   1.966 +      // First offset entry points to the first method_entry
   1.967 +      intptr_t* method_entry  = (intptr_t *)(((address)klass()) + offset_entry->offset());
   1.968 +      intptr_t* end         = klass->end_of_itable();
   1.969 +
   1.970 +      _table_offset      = (intptr_t*)offset_entry - (intptr_t*)klass();
   1.971 +      _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
   1.972 +      _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
   1.973 +      assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
   1.974 +      return;
   1.975 +    }
   1.976 +  }
   1.977 +
   1.978 +  // The length of the itable was either zero, or it has not yet been initialized.
   1.979 +  _table_offset      = 0;
   1.980 +  _size_offset_table = 0;
   1.981 +  _size_method_table = 0;
   1.982 +}
   1.983 +
   1.984 +static int initialize_count = 0;
   1.985 +
   1.986 +// Initialization
   1.987 +void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
   1.988 +  if (_klass->is_interface()) {
   1.989 +    // This needs to go after vtable indices are assigned but
   1.990 +    // before implementors need to know the number of itable indices.
   1.991 +    assign_itable_indices_for_interface(_klass());
   1.992 +  }
   1.993 +
   1.994 +  // Cannot be setup doing bootstrapping, interfaces don't have
   1.995 +  // itables, and klass with only ones entry have empty itables
   1.996 +  if (Universe::is_bootstrapping() ||
   1.997 +      _klass->is_interface() ||
   1.998 +      _klass->itable_length() == itableOffsetEntry::size()) return;
   1.999 +
  1.1000 +  // There's alway an extra itable entry so we can null-terminate it.
  1.1001 +  guarantee(size_offset_table() >= 1, "too small");
  1.1002 +  int num_interfaces = size_offset_table() - 1;
  1.1003 +  if (num_interfaces > 0) {
  1.1004 +    if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count,
  1.1005 +                                    _klass->name()->as_C_string());
  1.1006 +
  1.1007 +
  1.1008 +    // Iterate through all interfaces
  1.1009 +    int i;
  1.1010 +    for(i = 0; i < num_interfaces; i++) {
  1.1011 +      itableOffsetEntry* ioe = offset_entry(i);
  1.1012 +      HandleMark hm(THREAD);
  1.1013 +      KlassHandle interf_h (THREAD, ioe->interface_klass());
  1.1014 +      assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
  1.1015 +      initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
  1.1016 +    }
  1.1017 +
  1.1018 +  }
  1.1019 +  // Check that the last entry is empty
  1.1020 +  itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);
  1.1021 +  guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
  1.1022 +}
  1.1023 +
  1.1024 +
  1.1025 +inline bool interface_method_needs_itable_index(Method* m) {
  1.1026 +  if (m->is_static())           return false;   // e.g., Stream.empty
  1.1027 +  if (m->is_initializer())      return false;   // <init> or <clinit>
  1.1028 +  // If an interface redeclares a method from java.lang.Object,
  1.1029 +  // it should already have a vtable index, don't touch it.
  1.1030 +  // e.g., CharSequence.toString (from initialize_vtable)
  1.1031 +  // if (m->has_vtable_index())  return false; // NO!
  1.1032 +  return true;
  1.1033 +}
  1.1034 +
  1.1035 +int klassItable::assign_itable_indices_for_interface(Klass* klass) {
  1.1036 +  // an interface does not have an itable, but its methods need to be numbered
  1.1037 +  if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count,
  1.1038 +                                  klass->name()->as_C_string());
  1.1039 +  Array<Method*>* methods = InstanceKlass::cast(klass)->methods();
  1.1040 +  int nof_methods = methods->length();
  1.1041 +  int ime_num = 0;
  1.1042 +  for (int i = 0; i < nof_methods; i++) {
  1.1043 +    Method* m = methods->at(i);
  1.1044 +    if (interface_method_needs_itable_index(m)) {
  1.1045 +      assert(!m->is_final_method(), "no final interface methods");
  1.1046 +      // If m is already assigned a vtable index, do not disturb it.
  1.1047 +      if (TraceItables && Verbose) {
  1.1048 +        ResourceMark rm;
  1.1049 +        const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";
  1.1050 +        if (m->has_vtable_index()) {
  1.1051 +          tty->print("itable index %d for method: %s, flags: ", m->vtable_index(), sig);
  1.1052 +        } else {
  1.1053 +          tty->print("itable index %d for method: %s, flags: ", ime_num, sig);
  1.1054 +        }
  1.1055 +        if (m != NULL) {
  1.1056 +          m->access_flags().print_on(tty);
  1.1057 +          if (m->is_default_method()) {
  1.1058 +            tty->print("default ");
  1.1059 +          }
  1.1060 +          if (m->is_overpass()) {
  1.1061 +            tty->print("overpass");
  1.1062 +          }
  1.1063 +        }
  1.1064 +        tty->cr();
  1.1065 +      }
  1.1066 +      if (!m->has_vtable_index()) {
  1.1067 +        assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable");
  1.1068 +        m->set_itable_index(ime_num);
  1.1069 +        // Progress to next itable entry
  1.1070 +        ime_num++;
  1.1071 +      }
  1.1072 +    }
  1.1073 +  }
  1.1074 +  assert(ime_num == method_count_for_interface(klass), "proper sizing");
  1.1075 +  return ime_num;
  1.1076 +}
  1.1077 +
  1.1078 +int klassItable::method_count_for_interface(Klass* interf) {
  1.1079 +  assert(interf->oop_is_instance(), "must be");
  1.1080 +  assert(interf->is_interface(), "must be");
  1.1081 +  Array<Method*>* methods = InstanceKlass::cast(interf)->methods();
  1.1082 +  int nof_methods = methods->length();
  1.1083 +  while (nof_methods > 0) {
  1.1084 +    Method* m = methods->at(nof_methods-1);
  1.1085 +    if (m->has_itable_index()) {
  1.1086 +      int length = m->itable_index() + 1;
  1.1087 +#ifdef ASSERT
  1.1088 +      while (nof_methods = 0) {
  1.1089 +        m = methods->at(--nof_methods);
  1.1090 +        assert(!m->has_itable_index() || m->itable_index() < length, "");
  1.1091 +      }
  1.1092 +#endif //ASSERT
  1.1093 +      return length;  // return the rightmost itable index, plus one
  1.1094 +    }
  1.1095 +    nof_methods -= 1;
  1.1096 +  }
  1.1097 +  // no methods have itable indices
  1.1098 +  return 0;
  1.1099 +}
  1.1100 +
  1.1101 +
  1.1102 +void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
  1.1103 +  Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods();
  1.1104 +  int nof_methods = methods->length();
  1.1105 +  HandleMark hm;
  1.1106 +  assert(nof_methods > 0, "at least one method must exist for interface to be in vtable");
  1.1107 +  Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader());
  1.1108 +
  1.1109 +  int ime_count = method_count_for_interface(interf_h());
  1.1110 +  for (int i = 0; i < nof_methods; i++) {
  1.1111 +    Method* m = methods->at(i);
  1.1112 +    methodHandle target;
  1.1113 +    if (m->has_itable_index()) {
  1.1114 +      // This search must match the runtime resolution, i.e. selection search for invokeinterface
  1.1115 +      // to correctly enforce loader constraints for interface method inheritance
  1.1116 +      LinkResolver::lookup_instance_method_in_klasses(target, _klass, m->name(), m->signature(), CHECK);
  1.1117 +    }
  1.1118 +    if (target == NULL || !target->is_public() || target->is_abstract()) {
  1.1119 +      // Entry does not resolve. Leave it empty for AbstractMethodError.
  1.1120 +        if (!(target == NULL) && !target->is_public()) {
  1.1121 +          // Stuff an IllegalAccessError throwing method in there instead.
  1.1122 +          itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()].
  1.1123 +              initialize(Universe::throw_illegal_access_error());
  1.1124 +        }
  1.1125 +    } else {
  1.1126 +      // Entry did resolve, check loader constraints before initializing
  1.1127 +      // if checkconstraints requested
  1.1128 +      if (checkconstraints) {
  1.1129 +        Handle method_holder_loader (THREAD, target->method_holder()->class_loader());
  1.1130 +        if (method_holder_loader() != interface_loader()) {
  1.1131 +          ResourceMark rm(THREAD);
  1.1132 +          Symbol* failed_type_symbol =
  1.1133 +            SystemDictionary::check_signature_loaders(m->signature(),
  1.1134 +                                                      method_holder_loader,
  1.1135 +                                                      interface_loader,
  1.1136 +                                                      true, CHECK);
  1.1137 +          if (failed_type_symbol != NULL) {
  1.1138 +            const char* msg = "loader constraint violation in interface "
  1.1139 +              "itable initialization: when resolving method \"%s\" the class"
  1.1140 +              " loader (instance of %s) of the current class, %s, "
  1.1141 +              "and the class loader (instance of %s) for interface "
  1.1142 +              "%s have different Class objects for the type %s "
  1.1143 +              "used in the signature";
  1.1144 +            char* sig = target()->name_and_sig_as_C_string();
  1.1145 +            const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
  1.1146 +            char* current = _klass->name()->as_C_string();
  1.1147 +            const char* loader2 = SystemDictionary::loader_name(interface_loader());
  1.1148 +            char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();
  1.1149 +            char* failed_type_name = failed_type_symbol->as_C_string();
  1.1150 +            size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
  1.1151 +              strlen(current) + strlen(loader2) + strlen(iface) +
  1.1152 +              strlen(failed_type_name);
  1.1153 +            char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
  1.1154 +            jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
  1.1155 +                         iface, failed_type_name);
  1.1156 +            THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
  1.1157 +          }
  1.1158 +        }
  1.1159 +      }
  1.1160 +
  1.1161 +      // ime may have moved during GC so recalculate address
  1.1162 +      int ime_num = m->itable_index();
  1.1163 +      assert(ime_num < ime_count, "oob");
  1.1164 +      itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());
  1.1165 +      if (TraceItables && Verbose) {
  1.1166 +        ResourceMark rm(THREAD);
  1.1167 +        if (target() != NULL) {
  1.1168 +          char* sig = target()->name_and_sig_as_C_string();
  1.1169 +          tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",
  1.1170 +                    interf_h()->internal_name(), ime_num, sig,
  1.1171 +                    target()->method_holder()->internal_name());
  1.1172 +          tty->print("target_method flags: ");
  1.1173 +          target()->access_flags().print_on(tty);
  1.1174 +          if (target()->is_default_method()) {
  1.1175 +            tty->print("default ");
  1.1176 +          }
  1.1177 +          tty->cr();
  1.1178 +        }
  1.1179 +      }
  1.1180 +    }
  1.1181 +  }
  1.1182 +}
  1.1183 +
  1.1184 +// Update entry for specific Method*
  1.1185 +void klassItable::initialize_with_method(Method* m) {
  1.1186 +  itableMethodEntry* ime = method_entry(0);
  1.1187 +  for(int i = 0; i < _size_method_table; i++) {
  1.1188 +    if (ime->method() == m) {
  1.1189 +      ime->initialize(m);
  1.1190 +    }
  1.1191 +    ime++;
  1.1192 +  }
  1.1193 +}
  1.1194 +
  1.1195 +#if INCLUDE_JVMTI
  1.1196 +void klassItable::adjust_method_entries(Method** old_methods, Method** new_methods,
  1.1197 +                                        int methods_length, bool * trace_name_printed) {
  1.1198 +  // search the itable for uses of either obsolete or EMCP methods
  1.1199 +  for (int j = 0; j < methods_length; j++) {
  1.1200 +    Method* old_method = old_methods[j];
  1.1201 +    Method* new_method = new_methods[j];
  1.1202 +    itableMethodEntry* ime = method_entry(0);
  1.1203 +
  1.1204 +    // The itable can describe more than one interface and the same
  1.1205 +    // method signature can be specified by more than one interface.
  1.1206 +    // This means we have to do an exhaustive search to find all the
  1.1207 +    // old_method references.
  1.1208 +    for (int i = 0; i < _size_method_table; i++) {
  1.1209 +      if (ime->method() == old_method) {
  1.1210 +        ime->initialize(new_method);
  1.1211 +
  1.1212 +        if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
  1.1213 +          if (!(*trace_name_printed)) {
  1.1214 +            // RC_TRACE_MESG macro has an embedded ResourceMark
  1.1215 +            RC_TRACE_MESG(("adjust: name=%s",
  1.1216 +              old_method->method_holder()->external_name()));
  1.1217 +            *trace_name_printed = true;
  1.1218 +          }
  1.1219 +          // RC_TRACE macro has an embedded ResourceMark
  1.1220 +          RC_TRACE(0x00200000, ("itable method update: %s(%s)",
  1.1221 +            new_method->name()->as_C_string(),
  1.1222 +            new_method->signature()->as_C_string()));
  1.1223 +        }
  1.1224 +        // cannot 'break' here; see for-loop comment above.
  1.1225 +      }
  1.1226 +      ime++;
  1.1227 +    }
  1.1228 +  }
  1.1229 +}
  1.1230 +
  1.1231 +// an itable should never contain old or obsolete methods
  1.1232 +bool klassItable::check_no_old_or_obsolete_entries() {
  1.1233 +  itableMethodEntry* ime = method_entry(0);
  1.1234 +  for (int i = 0; i < _size_method_table; i++) {
  1.1235 +    Method* m = ime->method();
  1.1236 +    if (m != NULL &&
  1.1237 +        (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {
  1.1238 +      return false;
  1.1239 +    }
  1.1240 +    ime++;
  1.1241 +  }
  1.1242 +  return true;
  1.1243 +}
  1.1244 +
  1.1245 +void klassItable::dump_itable() {
  1.1246 +  itableMethodEntry* ime = method_entry(0);
  1.1247 +  tty->print_cr("itable dump --");
  1.1248 +  for (int i = 0; i < _size_method_table; i++) {
  1.1249 +    Method* m = ime->method();
  1.1250 +    if (m != NULL) {
  1.1251 +      tty->print("      (%5d)  ", i);
  1.1252 +      m->access_flags().print_on(tty);
  1.1253 +      if (m->is_default_method()) {
  1.1254 +        tty->print("default ");
  1.1255 +      }
  1.1256 +      tty->print(" --  ");
  1.1257 +      m->print_name(tty);
  1.1258 +      tty->cr();
  1.1259 +    }
  1.1260 +    ime++;
  1.1261 +  }
  1.1262 +}
  1.1263 +#endif // INCLUDE_JVMTI
  1.1264 +
  1.1265 +
  1.1266 +// Setup
  1.1267 +class InterfaceVisiterClosure : public StackObj {
  1.1268 + public:
  1.1269 +  virtual void doit(Klass* intf, int method_count) = 0;
  1.1270 +};
  1.1271 +
  1.1272 +// Visit all interfaces with at least one itable method
  1.1273 +void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) {
  1.1274 +  // Handle array argument
  1.1275 +  for(int i = 0; i < transitive_intf->length(); i++) {
  1.1276 +    Klass* intf = transitive_intf->at(i);
  1.1277 +    assert(intf->is_interface(), "sanity check");
  1.1278 +
  1.1279 +    // Find no. of itable methods
  1.1280 +    int method_count = 0;
  1.1281 +    // method_count = klassItable::method_count_for_interface(intf);
  1.1282 +    Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
  1.1283 +    if (methods->length() > 0) {
  1.1284 +      for (int i = methods->length(); --i >= 0; ) {
  1.1285 +        if (interface_method_needs_itable_index(methods->at(i))) {
  1.1286 +          method_count++;
  1.1287 +        }
  1.1288 +      }
  1.1289 +    }
  1.1290 +
  1.1291 +    // Only count interfaces with at least one method
  1.1292 +    if (method_count > 0) {
  1.1293 +      blk->doit(intf, method_count);
  1.1294 +    }
  1.1295 +  }
  1.1296 +}
  1.1297 +
  1.1298 +class CountInterfacesClosure : public InterfaceVisiterClosure {
  1.1299 + private:
  1.1300 +  int _nof_methods;
  1.1301 +  int _nof_interfaces;
  1.1302 + public:
  1.1303 +   CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }
  1.1304 +
  1.1305 +   int nof_methods() const    { return _nof_methods; }
  1.1306 +   int nof_interfaces() const { return _nof_interfaces; }
  1.1307 +
  1.1308 +   void doit(Klass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }
  1.1309 +};
  1.1310 +
  1.1311 +class SetupItableClosure : public InterfaceVisiterClosure  {
  1.1312 + private:
  1.1313 +  itableOffsetEntry* _offset_entry;
  1.1314 +  itableMethodEntry* _method_entry;
  1.1315 +  address            _klass_begin;
  1.1316 + public:
  1.1317 +  SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {
  1.1318 +    _klass_begin  = klass_begin;
  1.1319 +    _offset_entry = offset_entry;
  1.1320 +    _method_entry = method_entry;
  1.1321 +  }
  1.1322 +
  1.1323 +  itableMethodEntry* method_entry() const { return _method_entry; }
  1.1324 +
  1.1325 +  void doit(Klass* intf, int method_count) {
  1.1326 +    int offset = ((address)_method_entry) - _klass_begin;
  1.1327 +    _offset_entry->initialize(intf, offset);
  1.1328 +    _offset_entry++;
  1.1329 +    _method_entry += method_count;
  1.1330 +  }
  1.1331 +};
  1.1332 +
  1.1333 +int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) {
  1.1334 +  // Count no of interfaces and total number of interface methods
  1.1335 +  CountInterfacesClosure cic;
  1.1336 +  visit_all_interfaces(transitive_interfaces, &cic);
  1.1337 +
  1.1338 +  // There's alway an extra itable entry so we can null-terminate it.
  1.1339 +  int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());
  1.1340 +
  1.1341 +  // Statistics
  1.1342 +  update_stats(itable_size * HeapWordSize);
  1.1343 +
  1.1344 +  return itable_size;
  1.1345 +}
  1.1346 +
  1.1347 +
  1.1348 +// Fill out offset table and interface klasses into the itable space
  1.1349 +void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {
  1.1350 +  if (klass->itable_length() == 0) return;
  1.1351 +  assert(!klass->is_interface(), "Should have zero length itable");
  1.1352 +
  1.1353 +  // Count no of interfaces and total number of interface methods
  1.1354 +  CountInterfacesClosure cic;
  1.1355 +  visit_all_interfaces(klass->transitive_interfaces(), &cic);
  1.1356 +  int nof_methods    = cic.nof_methods();
  1.1357 +  int nof_interfaces = cic.nof_interfaces();
  1.1358 +
  1.1359 +  // Add one extra entry so we can null-terminate the table
  1.1360 +  nof_interfaces++;
  1.1361 +
  1.1362 +  assert(compute_itable_size(klass->transitive_interfaces()) ==
  1.1363 +         calc_itable_size(nof_interfaces, nof_methods),
  1.1364 +         "mismatch calculation of itable size");
  1.1365 +
  1.1366 +  // Fill-out offset table
  1.1367 +  itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
  1.1368 +  itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
  1.1369 +  intptr_t* end               = klass->end_of_itable();
  1.1370 +  assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");
  1.1371 +  assert((oop*)(end) == (oop*)(ime + nof_methods),                      "wrong offset calculation (2)");
  1.1372 +
  1.1373 +  // Visit all interfaces and initialize itable offset table
  1.1374 +  SetupItableClosure sic((address)klass(), ioe, ime);
  1.1375 +  visit_all_interfaces(klass->transitive_interfaces(), &sic);
  1.1376 +
  1.1377 +#ifdef ASSERT
  1.1378 +  ime  = sic.method_entry();
  1.1379 +  oop* v = (oop*) klass->end_of_itable();
  1.1380 +  assert( (oop*)(ime) == v, "wrong offset calculation (2)");
  1.1381 +#endif
  1.1382 +}
  1.1383 +
  1.1384 +
  1.1385 +// inverse to itable_index
  1.1386 +Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) {
  1.1387 +  assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");
  1.1388 +  assert(intf->verify_itable_index(itable_index), "");
  1.1389 +  Array<Method*>* methods = InstanceKlass::cast(intf)->methods();
  1.1390 +
  1.1391 +  if (itable_index < 0 || itable_index >= method_count_for_interface(intf))
  1.1392 +    return NULL;                // help caller defend against bad indices
  1.1393 +
  1.1394 +  int index = itable_index;
  1.1395 +  Method* m = methods->at(index);
  1.1396 +  int index2 = -1;
  1.1397 +  while (!m->has_itable_index() ||
  1.1398 +         (index2 = m->itable_index()) != itable_index) {
  1.1399 +    assert(index2 < itable_index, "monotonic");
  1.1400 +    if (++index == methods->length())
  1.1401 +      return NULL;
  1.1402 +    m = methods->at(index);
  1.1403 +  }
  1.1404 +  assert(m->itable_index() == itable_index, "correct inverse");
  1.1405 +
  1.1406 +  return m;
  1.1407 +}
  1.1408 +
  1.1409 +void klassVtable::verify(outputStream* st, bool forced) {
  1.1410 +  // make sure table is initialized
  1.1411 +  if (!Universe::is_fully_initialized()) return;
  1.1412 +#ifndef PRODUCT
  1.1413 +  // avoid redundant verifies
  1.1414 +  if (!forced && _verify_count == Universe::verify_count()) return;
  1.1415 +  _verify_count = Universe::verify_count();
  1.1416 +#endif
  1.1417 +  oop* end_of_obj = (oop*)_klass() + _klass()->size();
  1.1418 +  oop* end_of_vtable = (oop *)&table()[_length];
  1.1419 +  if (end_of_vtable > end_of_obj) {
  1.1420 +    fatal(err_msg("klass %s: klass object too short (vtable extends beyond "
  1.1421 +                  "end)", _klass->internal_name()));
  1.1422 +  }
  1.1423 +
  1.1424 +  for (int i = 0; i < _length; i++) table()[i].verify(this, st);
  1.1425 +  // verify consistency with superKlass vtable
  1.1426 +  Klass* super = _klass->super();
  1.1427 +  if (super != NULL) {
  1.1428 +    InstanceKlass* sk = InstanceKlass::cast(super);
  1.1429 +    klassVtable* vt = sk->vtable();
  1.1430 +    for (int i = 0; i < vt->length(); i++) {
  1.1431 +      verify_against(st, vt, i);
  1.1432 +    }
  1.1433 +  }
  1.1434 +}
  1.1435 +
  1.1436 +void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
  1.1437 +  vtableEntry* vte = &vt->table()[index];
  1.1438 +  if (vte->method()->name()      != table()[index].method()->name() ||
  1.1439 +      vte->method()->signature() != table()[index].method()->signature()) {
  1.1440 +    fatal("mismatched name/signature of vtable entries");
  1.1441 +  }
  1.1442 +}
  1.1443 +
  1.1444 +#ifndef PRODUCT
  1.1445 +void klassVtable::print() {
  1.1446 +  ResourceMark rm;
  1.1447 +  tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());
  1.1448 +  for (int i = 0; i < length(); i++) {
  1.1449 +    table()[i].print();
  1.1450 +    tty->cr();
  1.1451 +  }
  1.1452 +}
  1.1453 +#endif
  1.1454 +
  1.1455 +void vtableEntry::verify(klassVtable* vt, outputStream* st) {
  1.1456 +  NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
  1.1457 +  assert(method() != NULL, "must have set method");
  1.1458 +  method()->verify();
  1.1459 +  // we sub_type, because it could be a miranda method
  1.1460 +  if (!vt->klass()->is_subtype_of(method()->method_holder())) {
  1.1461 +#ifndef PRODUCT
  1.1462 +    print();
  1.1463 +#endif
  1.1464 +    fatal(err_msg("vtableEntry " PTR_FORMAT ": method is from subclass", this));
  1.1465 +  }
  1.1466 +}
  1.1467 +
  1.1468 +#ifndef PRODUCT
  1.1469 +
  1.1470 +void vtableEntry::print() {
  1.1471 +  ResourceMark rm;
  1.1472 +  tty->print("vtableEntry %s: ", method()->name()->as_C_string());
  1.1473 +  if (Verbose) {
  1.1474 +    tty->print("m %#lx ", (address)method());
  1.1475 +  }
  1.1476 +}
  1.1477 +
  1.1478 +class VtableStats : AllStatic {
  1.1479 + public:
  1.1480 +  static int no_klasses;                // # classes with vtables
  1.1481 +  static int no_array_klasses;          // # array classes
  1.1482 +  static int no_instance_klasses;       // # instanceKlasses
  1.1483 +  static int sum_of_vtable_len;         // total # of vtable entries
  1.1484 +  static int sum_of_array_vtable_len;   // total # of vtable entries in array klasses only
  1.1485 +  static int fixed;                     // total fixed overhead in bytes
  1.1486 +  static int filler;                    // overhead caused by filler bytes
  1.1487 +  static int entries;                   // total bytes consumed by vtable entries
  1.1488 +  static int array_entries;             // total bytes consumed by array vtable entries
  1.1489 +
  1.1490 +  static void do_class(Klass* k) {
  1.1491 +    Klass* kl = k;
  1.1492 +    klassVtable* vt = kl->vtable();
  1.1493 +    if (vt == NULL) return;
  1.1494 +    no_klasses++;
  1.1495 +    if (kl->oop_is_instance()) {
  1.1496 +      no_instance_klasses++;
  1.1497 +      kl->array_klasses_do(do_class);
  1.1498 +    }
  1.1499 +    if (kl->oop_is_array()) {
  1.1500 +      no_array_klasses++;
  1.1501 +      sum_of_array_vtable_len += vt->length();
  1.1502 +    }
  1.1503 +    sum_of_vtable_len += vt->length();
  1.1504 +  }
  1.1505 +
  1.1506 +  static void compute() {
  1.1507 +    SystemDictionary::classes_do(do_class);
  1.1508 +    fixed  = no_klasses * oopSize;      // vtable length
  1.1509 +    // filler size is a conservative approximation
  1.1510 +    filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(InstanceKlass) - sizeof(ArrayKlass) - 1);
  1.1511 +    entries = sizeof(vtableEntry) * sum_of_vtable_len;
  1.1512 +    array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len;
  1.1513 +  }
  1.1514 +};
  1.1515 +
  1.1516 +int VtableStats::no_klasses = 0;
  1.1517 +int VtableStats::no_array_klasses = 0;
  1.1518 +int VtableStats::no_instance_klasses = 0;
  1.1519 +int VtableStats::sum_of_vtable_len = 0;
  1.1520 +int VtableStats::sum_of_array_vtable_len = 0;
  1.1521 +int VtableStats::fixed = 0;
  1.1522 +int VtableStats::filler = 0;
  1.1523 +int VtableStats::entries = 0;
  1.1524 +int VtableStats::array_entries = 0;
  1.1525 +
  1.1526 +void klassVtable::print_statistics() {
  1.1527 +  ResourceMark rm;
  1.1528 +  HandleMark hm;
  1.1529 +  VtableStats::compute();
  1.1530 +  tty->print_cr("vtable statistics:");
  1.1531 +  tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses);
  1.1532 +  int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries;
  1.1533 +  tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed);
  1.1534 +  tty->print_cr("%6d bytes filler overhead", VtableStats::filler);
  1.1535 +  tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries);
  1.1536 +  tty->print_cr("%6d bytes total", total);
  1.1537 +}
  1.1538 +
  1.1539 +int  klassItable::_total_classes;   // Total no. of classes with itables
  1.1540 +long klassItable::_total_size;      // Total no. of bytes used for itables
  1.1541 +
  1.1542 +void klassItable::print_statistics() {
  1.1543 + tty->print_cr("itable statistics:");
  1.1544 + tty->print_cr("%6d classes with itables", _total_classes);
  1.1545 + tty->print_cr("%6d K uses for itables (average by class: %d bytes)", _total_size / K, _total_size / _total_classes);
  1.1546 +}
  1.1547 +
  1.1548 +#endif // PRODUCT

mercurial