src/share/vm/oops/klassVtable.cpp

changeset 435
a61af66fc99e
child 451
f8236e79048a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/oops/klassVtable.cpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,1322 @@
     1.4 +/*
     1.5 + * Copyright 1997-2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "incls/_precompiled.incl"
    1.29 +#include "incls/_klassVtable.cpp.incl"
    1.30 +
    1.31 +inline instanceKlass* klassVtable::ik() const {
    1.32 +  Klass* k = _klass()->klass_part();
    1.33 +  assert(k->oop_is_instance(), "not an instanceKlass");
    1.34 +  return (instanceKlass*)k;
    1.35 +}
    1.36 +
    1.37 +
    1.38 +// this function computes the vtable size (including the size needed for miranda
    1.39 +// methods) and the number of miranda methods in this class
    1.40 +// Note on Miranda methods: Let's say there is a class C that implements
    1.41 +// interface I.  Let's say there is a method m in I that neither C nor any
    1.42 +// of its super classes implement (i.e there is no method of any access, with
    1.43 +// the same name and signature as m), then m is a Miranda method which is
    1.44 +// entered as a public abstract method in C's vtable.  From then on it should
    1.45 +// treated as any other public method in C for method over-ride purposes.
    1.46 +void klassVtable::compute_vtable_size_and_num_mirandas(int &vtable_length,
    1.47 +                                                       int &num_miranda_methods,
    1.48 +                                                       klassOop super,
    1.49 +                                                       objArrayOop methods,
    1.50 +                                                       AccessFlags class_flags,
    1.51 +                                                       oop classloader,
    1.52 +                                                       symbolOop classname,
    1.53 +                                                       objArrayOop local_interfaces
    1.54 +                                                       ) {
    1.55 +
    1.56 +  No_Safepoint_Verifier nsv;
    1.57 +
    1.58 +  // set up default result values
    1.59 +  vtable_length = 0;
    1.60 +  num_miranda_methods = 0;
    1.61 +
    1.62 +  // start off with super's vtable length
    1.63 +  instanceKlass* sk = (instanceKlass*)super->klass_part();
    1.64 +  vtable_length = super == NULL ? 0 : sk->vtable_length();
    1.65 +
    1.66 +  // go thru each method in the methods table to see if it needs a new entry
    1.67 +  int len = methods->length();
    1.68 +  for (int i = 0; i < len; i++) {
    1.69 +    assert(methods->obj_at(i)->is_method(), "must be a methodOop");
    1.70 +    methodOop m = methodOop(methods->obj_at(i));
    1.71 +
    1.72 +    if (needs_new_vtable_entry(m, super, classloader, classname, class_flags)) {
    1.73 +      vtable_length += vtableEntry::size(); // we need a new entry
    1.74 +    }
    1.75 +  }
    1.76 +
    1.77 +  // compute the number of mirandas methods that must be added to the end
    1.78 +  num_miranda_methods = get_num_mirandas(super, methods, local_interfaces);
    1.79 +  vtable_length += (num_miranda_methods * vtableEntry::size());
    1.80 +
    1.81 +  if (Universe::is_bootstrapping() && vtable_length == 0) {
    1.82 +    // array classes don't have their superclass set correctly during
    1.83 +    // bootstrapping
    1.84 +    vtable_length = Universe::base_vtable_size();
    1.85 +  }
    1.86 +
    1.87 +  if (super == NULL && !Universe::is_bootstrapping() &&
    1.88 +      vtable_length != Universe::base_vtable_size()) {
    1.89 +    // Someone is attempting to redefine java.lang.Object incorrectly.  The
    1.90 +    // only way this should happen is from
    1.91 +    // SystemDictionary::resolve_from_stream(), which will detect this later
    1.92 +    // and throw a security exception.  So don't assert here to let
    1.93 +    // the exception occur.
    1.94 +    vtable_length = Universe::base_vtable_size();
    1.95 +  }
    1.96 +  assert(super != NULL || vtable_length == Universe::base_vtable_size(),
    1.97 +         "bad vtable size for class Object");
    1.98 +  assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");
    1.99 +  assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");
   1.100 +}
   1.101 +
   1.102 +int klassVtable::index_of(methodOop m, int len) const {
   1.103 +  assert(m->vtable_index() >= 0, "do not ask this of non-vtable methods");
   1.104 +  return m->vtable_index();
   1.105 +}
   1.106 +
   1.107 +int klassVtable::initialize_from_super(KlassHandle super) {
   1.108 +  if (super.is_null()) {
   1.109 +    return 0;
   1.110 +  } else {
   1.111 +    // copy methods from superKlass
   1.112 +    // can't inherit from array class, so must be instanceKlass
   1.113 +    assert(super->oop_is_instance(), "must be instance klass");
   1.114 +    instanceKlass* sk = (instanceKlass*)super()->klass_part();
   1.115 +    klassVtable* superVtable = sk->vtable();
   1.116 +    assert(superVtable->length() <= _length, "vtable too short");
   1.117 +#ifdef ASSERT
   1.118 +    superVtable->verify(tty, true);
   1.119 +#endif
   1.120 +    superVtable->copy_vtable_to(table());
   1.121 +#ifndef PRODUCT
   1.122 +    if (PrintVtables && Verbose) {
   1.123 +      tty->print_cr("copy vtable from %s to %s size %d", sk->internal_name(), klass()->internal_name(), _length);
   1.124 +    }
   1.125 +#endif
   1.126 +    return superVtable->length();
   1.127 +  }
   1.128 +}
   1.129 +
   1.130 +// Revised lookup semantics   introduced 1.3 (Kestral beta)
   1.131 +void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {
   1.132 +
   1.133 +  // Note:  Arrays can have intermediate array supers.  Use java_super to skip them.
   1.134 +  KlassHandle super (THREAD, klass()->java_super());
   1.135 +  int nofNewEntries = 0;
   1.136 +
   1.137 +
   1.138 +  if (PrintVtables && !klass()->oop_is_array()) {
   1.139 +    ResourceMark rm(THREAD);
   1.140 +    tty->print_cr("Initializing: %s", _klass->name()->as_C_string());
   1.141 +  }
   1.142 +
   1.143 +#ifdef ASSERT
   1.144 +  oop* end_of_obj = (oop*)_klass() + _klass()->size();
   1.145 +  oop* end_of_vtable = (oop*)&table()[_length];
   1.146 +  assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");
   1.147 +#endif
   1.148 +
   1.149 +  if (Universe::is_bootstrapping()) {
   1.150 +    // just clear everything
   1.151 +    for (int i = 0; i < _length; i++) table()[i].clear();
   1.152 +    return;
   1.153 +  }
   1.154 +
   1.155 +  int super_vtable_len = initialize_from_super(super);
   1.156 +  if (klass()->oop_is_array()) {
   1.157 +    assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");
   1.158 +  } else {
   1.159 +    assert(_klass->oop_is_instance(), "must be instanceKlass");
   1.160 +
   1.161 +    objArrayHandle methods(THREAD, ik()->methods());
   1.162 +    int len = methods()->length();
   1.163 +    int initialized = super_vtable_len;
   1.164 +
   1.165 +    // update_super_vtable can stop for gc - ensure using handles
   1.166 +    for (int i = 0; i < len; i++) {
   1.167 +      HandleMark hm(THREAD);
   1.168 +      assert(methods()->obj_at(i)->is_method(), "must be a methodOop");
   1.169 +      methodHandle mh(THREAD, (methodOop)methods()->obj_at(i));
   1.170 +
   1.171 +      bool needs_new_entry = update_super_vtable(ik(), mh, super_vtable_len, checkconstraints, CHECK);
   1.172 +
   1.173 +      if (needs_new_entry) {
   1.174 +        put_method_at(mh(), initialized);
   1.175 +        mh()->set_vtable_index(initialized); // set primary vtable index
   1.176 +        initialized++;
   1.177 +      }
   1.178 +    }
   1.179 +
   1.180 +    // add miranda methods; it will also update the value of initialized
   1.181 +    fill_in_mirandas(initialized);
   1.182 +
   1.183 +    // In class hierachieswhere the accesibility is not increasing (i.e., going from private ->
   1.184 +    // package_private -> publicprotected), the vtable might actually be smaller than our initial
   1.185 +    // calculation.
   1.186 +    assert(initialized <= _length, "vtable initialization failed");
   1.187 +    for(;initialized < _length; initialized++) {
   1.188 +      put_method_at(NULL, initialized);
   1.189 +    }
   1.190 +    NOT_PRODUCT(verify(tty, true));
   1.191 +  }
   1.192 +}
   1.193 +
   1.194 +// Interates through the vtables to find the broadest access level. This
   1.195 +// will always be monotomic for valid Java programs - but not neccesarily
   1.196 +// for incompatible class files.
   1.197 +klassVtable::AccessType klassVtable::vtable_accessibility_at(int i) {
   1.198 +  // This vtable is not implementing the specific method
   1.199 +  if (i >= length()) return acc_private;
   1.200 +
   1.201 +  // Compute AccessType for current method. public or protected we are done.
   1.202 +  methodOop m = method_at(i);
   1.203 +  if (m->is_protected() || m->is_public()) return acc_publicprotected;
   1.204 +
   1.205 +  AccessType acc = m->is_package_private() ? acc_package_private : acc_private;
   1.206 +
   1.207 +  // Compute AccessType for method in super classes
   1.208 +  klassOop super = klass()->super();
   1.209 +  AccessType super_acc = (super != NULL) ? instanceKlass::cast(klass()->super())->vtable()->vtable_accessibility_at(i)
   1.210 +                                         : acc_private;
   1.211 +
   1.212 +  // Merge
   1.213 +  return (AccessType)MAX2((int)acc, (int)super_acc);
   1.214 +}
   1.215 +
   1.216 +
   1.217 +// Update child's copy of super vtable for overrides
   1.218 +// OR return true if a new vtable entry is required
   1.219 +// Only called for instanceKlass's, i.e. not for arrays
   1.220 +// If that changed, could not use _klass as handle for klass
   1.221 +bool klassVtable::update_super_vtable(instanceKlass* klass, methodHandle target_method, int super_vtable_len, bool checkconstraints, TRAPS) {
   1.222 +  ResourceMark rm;
   1.223 +  bool allocate_new = true;
   1.224 +  assert(klass->oop_is_instance(), "must be instanceKlass");
   1.225 +
   1.226 +  // Initialize the method's vtable index to "nonvirtual".
   1.227 +  // If we allocate a vtable entry, we will update it to a non-negative number.
   1.228 +  target_method()->set_vtable_index(methodOopDesc::nonvirtual_vtable_index);
   1.229 +
   1.230 +  // Static and <init> methods are never in
   1.231 +  if (target_method()->is_static() || target_method()->name() ==  vmSymbols::object_initializer_name()) {
   1.232 +    return false;
   1.233 +  }
   1.234 +
   1.235 +  if (klass->is_final() || target_method()->is_final()) {
   1.236 +    // a final method never needs a new entry; final methods can be statically
   1.237 +    // resolved and they have to be present in the vtable only if they override
   1.238 +    // a super's method, in which case they re-use its entry
   1.239 +    allocate_new = false;
   1.240 +  }
   1.241 +
   1.242 +  // we need a new entry if there is no superclass
   1.243 +  if (klass->super() == NULL) {
   1.244 +    return allocate_new;
   1.245 +  }
   1.246 +
   1.247 +  // private methods always have a new entry in the vtable
   1.248 +  if (target_method()->is_private()) {
   1.249 +    return allocate_new;
   1.250 +  }
   1.251 +
   1.252 +  // search through the vtable and update overridden entries
   1.253 +  // Since check_signature_loaders acquires SystemDictionary_lock
   1.254 +  // which can block for gc, once we are in this loop, use handles, not
   1.255 +  // unhandled oops unless they are reinitialized for each loop
   1.256 +  // handles for name, signature, klass, target_method
   1.257 +  // not for match_method, holder
   1.258 +
   1.259 +  symbolHandle name(THREAD,target_method()->name());
   1.260 +  symbolHandle signature(THREAD,target_method()->signature());
   1.261 +  for(int i = 0; i < super_vtable_len; i++) {
   1.262 +    methodOop match_method = method_at(i);
   1.263 +    // Check if method name matches
   1.264 +    if (match_method->name() == name() && match_method->signature() == signature()) {
   1.265 +
   1.266 +      instanceKlass* holder = (THREAD, instanceKlass::cast(match_method->method_holder()));
   1.267 +
   1.268 +      // Check if the match_method is accessable from current class
   1.269 +
   1.270 +      bool same_package_init = false;
   1.271 +      bool same_package_flag = false;
   1.272 +      bool simple_match = match_method->is_public()  || match_method->is_protected();
   1.273 +      if (!simple_match) {
   1.274 +        same_package_init = true;
   1.275 +        same_package_flag = holder->is_same_class_package(_klass->class_loader(), _klass->name());
   1.276 +
   1.277 +        simple_match = match_method->is_package_private() && same_package_flag;
   1.278 +      }
   1.279 +      // match_method is the superclass' method. Note we can't override
   1.280 +      // and shouldn't access superclass' ACC_PRIVATE methods
   1.281 +      // (although they have been copied into our vtable)
   1.282 +      // A simple form of this statement is:
   1.283 +      // if ( (match_method->is_public()  || match_method->is_protected()) ||
   1.284 +      //    (match_method->is_package_private() && holder->is_same_class_package(klass->class_loader(), klass->name()))) {
   1.285 +      //
   1.286 +      // The complexity is introduced it avoid recomputing 'is_same_class_package' which is expensive.
   1.287 +      if (simple_match) {
   1.288 +        // Check if target_method and match_method has same level of accessibility. The accesibility of the
   1.289 +        // match method is the "most-general" visibility of all entries at it's particular vtable index for
   1.290 +        // all superclasses. This check must be done before we override the current entry in the vtable.
   1.291 +        AccessType at = vtable_accessibility_at(i);
   1.292 +        bool same_access = false;
   1.293 +
   1.294 +        if (  (at == acc_publicprotected && (target_method()->is_public() || target_method()->is_protected())
   1.295 +           || (at == acc_package_private && (target_method()->is_package_private() &&
   1.296 +                                            (( same_package_init && same_package_flag) ||
   1.297 +                                             (!same_package_init && holder->is_same_class_package(_klass->class_loader(), _klass->name()))))))) {
   1.298 +           same_access = true;
   1.299 +        }
   1.300 +
   1.301 +        if (checkconstraints) {
   1.302 +        // Override vtable entry if passes loader constraint check
   1.303 +        // if loader constraint checking requested
   1.304 +        // No need to visit his super, since he and his super
   1.305 +        // have already made any needed loader constraints.
   1.306 +        // Since loader constraints are transitive, it is enough
   1.307 +        // to link to the first super, and we get all the others.
   1.308 +          symbolHandle signature(THREAD, target_method()->signature());
   1.309 +          Handle this_loader(THREAD, _klass->class_loader());
   1.310 +          instanceKlassHandle super_klass(THREAD, _klass->super());
   1.311 +          Handle super_loader(THREAD, super_klass->class_loader());
   1.312 +
   1.313 +          if (this_loader() != super_loader()) {
   1.314 +            ResourceMark rm(THREAD);
   1.315 +            char* failed_type_name =
   1.316 +              SystemDictionary::check_signature_loaders(signature, this_loader,
   1.317 +                                                        super_loader, true,
   1.318 +                                                        CHECK_(false));
   1.319 +            if (failed_type_name != NULL) {
   1.320 +              const char* msg = "loader constraint violation: when resolving "
   1.321 +                "overridden method \"%s\" the class loader (instance"
   1.322 +                " of %s) of the current class, %s, and its superclass loader "
   1.323 +                "(instance of %s), have different Class objects for the type "
   1.324 +                "%s used in the signature";
   1.325 +              char* sig = target_method()->name_and_sig_as_C_string();
   1.326 +              const char* loader1 = SystemDictionary::loader_name(this_loader());
   1.327 +              char* current = _klass->name()->as_C_string();
   1.328 +              const char* loader2 = SystemDictionary::loader_name(super_loader());
   1.329 +              size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
   1.330 +                strlen(current) + strlen(loader2) + strlen(failed_type_name);
   1.331 +              char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
   1.332 +              jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
   1.333 +                           failed_type_name);
   1.334 +              THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);
   1.335 +            }
   1.336 +          }
   1.337 +        }
   1.338 +        put_method_at(target_method(), i);
   1.339 +
   1.340 +
   1.341 +        if (same_access) {
   1.342 +          // target and match has same accessiblity - share entry
   1.343 +          allocate_new = false;
   1.344 +          target_method()->set_vtable_index(i);
   1.345 +#ifndef PRODUCT
   1.346 +          if (PrintVtables && Verbose) {
   1.347 +            AccessType targetacc;
   1.348 +            if (target_method()->is_protected() ||
   1.349 +                 target_method()->is_public()) {
   1.350 +               targetacc =  acc_publicprotected;
   1.351 +            } else {
   1.352 +              targetacc = target_method()->is_package_private() ? acc_package_private : acc_private;
   1.353 +            }
   1.354 +            tty->print_cr("overriding with %s::%s index %d, original flags: %x overriders flags: %x",
   1.355 +             _klass->internal_name(), (target_method() != NULL) ?
   1.356 +             target_method()->name()->as_C_string() : "<NULL>", i,
   1.357 +             at, targetacc);
   1.358 +          }
   1.359 +#endif /*PRODUCT*/
   1.360 +        } else {
   1.361 +#ifndef PRODUCT
   1.362 +          if (PrintVtables && Verbose) {
   1.363 +            AccessType targetacc;
   1.364 +            if (target_method()->is_protected() ||
   1.365 +                 target_method()->is_public()) {
   1.366 +               targetacc =  acc_publicprotected;
   1.367 +            } else {
   1.368 +              targetacc = target_method()->is_package_private() ? acc_package_private : acc_private;
   1.369 +            }
   1.370 +            tty->print_cr("override %s %s::%s at index %d, original flags: %x overriders flags: %x",
   1.371 +            allocate_new ? "+ new" : "only",
   1.372 +            _klass->internal_name(), (target_method() != NULL) ?
   1.373 +            target_method()->name()->as_C_string() : "<NULL>", i,
   1.374 +            at, targetacc);
   1.375 +           }
   1.376 +#endif /*PRODUCT*/
   1.377 +        }
   1.378 +      }
   1.379 +    }
   1.380 +  }
   1.381 +  return allocate_new;
   1.382 +}
   1.383 +
   1.384 +
   1.385 +
   1.386 +void klassVtable::put_method_at(methodOop m, int index) {
   1.387 +  assert(m->is_oop_or_null(), "Not an oop or null");
   1.388 +#ifndef PRODUCT
   1.389 +  if (PrintVtables && Verbose) {
   1.390 +    tty->print_cr("adding %s::%s at index %d", _klass->internal_name(),
   1.391 +      (m != NULL) ? m->name()->as_C_string() : "<NULL>", index);
   1.392 +  }
   1.393 +  assert(unchecked_method_at(index)->is_oop_or_null(), "Not an oop or null");
   1.394 +#endif
   1.395 +  table()[index].set(m);
   1.396 +}
   1.397 +
   1.398 +// Find out if a method "m" with superclass "super", loader "classloader" and
   1.399 +// name "classname" needs a new vtable entry.  Let P be a class package defined
   1.400 +// by "classloader" and "classname".
   1.401 +// NOTE: The logic used here is very similar to the one used for computing
   1.402 +// the vtables indices for a method. We cannot directly use that function because,
   1.403 +// when the Universe is boostrapping, a super's vtable might not be initialized.
   1.404 +bool klassVtable::needs_new_vtable_entry(methodOop target_method,
   1.405 +                                         klassOop super,
   1.406 +                                         oop classloader,
   1.407 +                                         symbolOop classname,
   1.408 +                                         AccessFlags class_flags) {
   1.409 +  if ((class_flags.is_final() || target_method->is_final()) ||
   1.410 +      // a final method never needs a new entry; final methods can be statically
   1.411 +      // resolved and they have to be present in the vtable only if they override
   1.412 +      // a super's method, in which case they re-use its entry
   1.413 +      (target_method->is_static()) ||
   1.414 +      // static methods don't need to be in vtable
   1.415 +      (target_method->name() ==  vmSymbols::object_initializer_name())
   1.416 +      // <init> is never called dynamically-bound
   1.417 +      ) {
   1.418 +    return false;
   1.419 +  }
   1.420 +
   1.421 +  // we need a new entry if there is no superclass
   1.422 +  if (super == NULL) {
   1.423 +    return true;
   1.424 +  }
   1.425 +
   1.426 +  // private methods always have a new entry in the vtable
   1.427 +  if (target_method->is_private()) {
   1.428 +    return true;
   1.429 +  }
   1.430 +
   1.431 +  // search through the super class hierarchy to see if we need
   1.432 +  // a new entry
   1.433 +  symbolOop name = target_method->name();
   1.434 +  symbolOop signature = target_method->signature();
   1.435 +  klassOop k = super;
   1.436 +  methodOop match_method = NULL;
   1.437 +  instanceKlass *holder = NULL;
   1.438 +  while (k != NULL) {
   1.439 +    // lookup through the hierarchy for a method with matching name and sign.
   1.440 +    match_method = instanceKlass::cast(k)->lookup_method(name, signature);
   1.441 +    if (match_method == NULL) {
   1.442 +      break; // we still have to search for a matching miranda method
   1.443 +    }
   1.444 +    // get the class holding the matching method
   1.445 +    holder = instanceKlass::cast(match_method->method_holder());
   1.446 +
   1.447 +    if (!match_method->is_static()) { // we want only instance method matches
   1.448 +      if ((target_method->is_public() || target_method->is_protected()) &&
   1.449 +          (match_method->is_public()  || match_method->is_protected())) {
   1.450 +        // target and match are public/protected; we do not need a new entry
   1.451 +        return false;
   1.452 +      }
   1.453 +
   1.454 +      if (target_method->is_package_private() &&
   1.455 +          match_method->is_package_private() &&
   1.456 +          holder->is_same_class_package(classloader, classname)) {
   1.457 +        // target and match are P private; we do not need a new entry
   1.458 +        return false;
   1.459 +      }
   1.460 +    }
   1.461 +
   1.462 +    k = holder->super(); // haven't found a match yet; continue to look
   1.463 +  }
   1.464 +
   1.465 +  // if the target method is public or protected it may have a matching
   1.466 +  // miranda method in the super, whose entry it should re-use.
   1.467 +  if (target_method->is_public() || target_method->is_protected()) {
   1.468 +    instanceKlass *sk = instanceKlass::cast(super);
   1.469 +    if (sk->has_miranda_methods()) {
   1.470 +      if (sk->lookup_method_in_all_interfaces(name, signature) != NULL) {
   1.471 +        return false;  // found a matching miranda; we do not need a new entry
   1.472 +      }
   1.473 +    }
   1.474 +  }
   1.475 +
   1.476 +  return true; // found no match; we need a new entry
   1.477 +}
   1.478 +
   1.479 +// Support for miranda methods
   1.480 +
   1.481 +// get the vtable index of a miranda method with matching "name" and "signature"
   1.482 +int klassVtable::index_of_miranda(symbolOop name, symbolOop signature) {
   1.483 +  // search from the bottom, might be faster
   1.484 +  for (int i = (length() - 1); i >= 0; i--) {
   1.485 +    methodOop m = table()[i].method();
   1.486 +    if (is_miranda_entry_at(i) &&
   1.487 +        m->name() == name && m->signature() == signature) {
   1.488 +      return i;
   1.489 +    }
   1.490 +  }
   1.491 +  return methodOopDesc::invalid_vtable_index;
   1.492 +}
   1.493 +
   1.494 +// check if an entry is miranda
   1.495 +bool klassVtable::is_miranda_entry_at(int i) {
   1.496 +  methodOop m = method_at(i);
   1.497 +  klassOop method_holder = m->method_holder();
   1.498 +  instanceKlass *mhk = instanceKlass::cast(method_holder);
   1.499 +
   1.500 +  // miranda methods are interface methods in a class's vtable
   1.501 +  if (mhk->is_interface()) {
   1.502 +    assert(m->is_public() && m->is_abstract(), "should be public and abstract");
   1.503 +    assert(ik()->implements_interface(method_holder) , "this class should implement the interface");
   1.504 +    assert(is_miranda(m, ik()->methods(), ik()->super()), "should be a miranda_method");
   1.505 +    return true;
   1.506 +  }
   1.507 +  return false;
   1.508 +}
   1.509 +
   1.510 +// check if a method is a miranda method, given a class's methods table and it's super
   1.511 +// the caller must make sure that the method belongs to an interface implemented by the class
   1.512 +bool klassVtable::is_miranda(methodOop m, objArrayOop class_methods, klassOop super) {
   1.513 +  symbolOop name = m->name();
   1.514 +  symbolOop signature = m->signature();
   1.515 +  if (instanceKlass::find_method(class_methods, name, signature) == NULL) {
   1.516 +     // did not find it in the method table of the current class
   1.517 +    if (super == NULL) {
   1.518 +      // super doesn't exist
   1.519 +      return true;
   1.520 +    } else {
   1.521 +      if (instanceKlass::cast(super)->lookup_method(name, signature) == NULL) {
   1.522 +        // super class hierarchy does not implement it
   1.523 +        return true;
   1.524 +      }
   1.525 +    }
   1.526 +  }
   1.527 +  return false;
   1.528 +}
   1.529 +
   1.530 +void klassVtable::add_new_mirandas_to_list(GrowableArray<methodOop>* list_of_current_mirandas,
   1.531 +                                           objArrayOop current_interface_methods,
   1.532 +                                           objArrayOop class_methods,
   1.533 +                                           klassOop super) {
   1.534 +  // iterate thru the current interface's method to see if it a miranda
   1.535 +  int num_methods = current_interface_methods->length();
   1.536 +  for (int i = 0; i < num_methods; i++) {
   1.537 +    methodOop im = methodOop(current_interface_methods->obj_at(i));
   1.538 +    bool is_duplicate = false;
   1.539 +    int num_of_current_mirandas = list_of_current_mirandas->length();
   1.540 +    // check for duplicate mirandas in different interfaces we implement
   1.541 +    for (int j = 0; j < num_of_current_mirandas; j++) {
   1.542 +      methodOop miranda = list_of_current_mirandas->at(j);
   1.543 +      if ((im->name() == miranda->name()) &&
   1.544 +          (im->signature() == miranda->signature())) {
   1.545 +        is_duplicate = true;
   1.546 +        break;
   1.547 +      }
   1.548 +    }
   1.549 +
   1.550 +    if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable
   1.551 +      if (is_miranda(im, class_methods, super)) { // is it a miranda at all?
   1.552 +        instanceKlass *sk = instanceKlass::cast(super);
   1.553 +        // check if it is a duplicate of a super's miranda
   1.554 +        if (sk->lookup_method_in_all_interfaces(im->name(), im->signature()) == NULL) {
   1.555 +          list_of_current_mirandas->append(im);
   1.556 +        }
   1.557 +      }
   1.558 +    }
   1.559 +  }
   1.560 +}
   1.561 +
   1.562 +void klassVtable::get_mirandas(GrowableArray<methodOop>* mirandas,
   1.563 +                               klassOop super, objArrayOop class_methods,
   1.564 +                               objArrayOop local_interfaces) {
   1.565 +  assert((mirandas->length() == 0) , "current mirandas must be 0");
   1.566 +
   1.567 +  // iterate thru the local interfaces looking for a miranda
   1.568 +  int num_local_ifs = local_interfaces->length();
   1.569 +  for (int i = 0; i < num_local_ifs; i++) {
   1.570 +    instanceKlass *ik = instanceKlass::cast(klassOop(local_interfaces->obj_at(i)));
   1.571 +    add_new_mirandas_to_list(mirandas, ik->methods(), class_methods, super);
   1.572 +    // iterate thru each local's super interfaces
   1.573 +    objArrayOop super_ifs = ik->transitive_interfaces();
   1.574 +    int num_super_ifs = super_ifs->length();
   1.575 +    for (int j = 0; j < num_super_ifs; j++) {
   1.576 +      instanceKlass *sik = instanceKlass::cast(klassOop(super_ifs->obj_at(j)));
   1.577 +      add_new_mirandas_to_list(mirandas, sik->methods(), class_methods, super);
   1.578 +    }
   1.579 +  }
   1.580 +}
   1.581 +
   1.582 +// get number of mirandas
   1.583 +int klassVtable::get_num_mirandas(klassOop super, objArrayOop class_methods, objArrayOop local_interfaces) {
   1.584 +  ResourceMark rm;
   1.585 +  GrowableArray<methodOop>* mirandas = new GrowableArray<methodOop>(20);
   1.586 +  get_mirandas(mirandas, super, class_methods, local_interfaces);
   1.587 +  return mirandas->length();
   1.588 +}
   1.589 +
   1.590 +// fill in mirandas
   1.591 +void klassVtable::fill_in_mirandas(int& initialized) {
   1.592 +  ResourceMark rm;
   1.593 +  GrowableArray<methodOop>* mirandas = new GrowableArray<methodOop>(20);
   1.594 +  instanceKlass *this_ik = ik();
   1.595 +  get_mirandas(mirandas, this_ik->super(), this_ik->methods(), this_ik->local_interfaces());
   1.596 +  int num_mirandas = mirandas->length();
   1.597 +  for (int i = 0; i < num_mirandas; i++) {
   1.598 +    put_method_at(mirandas->at(i), initialized);
   1.599 +    initialized++;
   1.600 +  }
   1.601 +}
   1.602 +
   1.603 +void klassVtable::copy_vtable_to(vtableEntry* start) {
   1.604 +  Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());
   1.605 +}
   1.606 +
   1.607 +void klassVtable::adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
   1.608 +                                        int methods_length, bool * trace_name_printed) {
   1.609 +  // search the vtable for uses of either obsolete or EMCP methods
   1.610 +  for (int j = 0; j < methods_length; j++) {
   1.611 +    methodOop old_method = old_methods[j];
   1.612 +    methodOop new_method = new_methods[j];
   1.613 +
   1.614 +    // In the vast majority of cases we could get the vtable index
   1.615 +    // by using:  old_method->vtable_index()
   1.616 +    // However, there are rare cases, eg. sun.awt.X11.XDecoratedPeer.getX()
   1.617 +    // in sun.awt.X11.XFramePeer where methods occur more than once in the
   1.618 +    // vtable, so, alas, we must do an exhaustive search.
   1.619 +    for (int index = 0; index < length(); index++) {
   1.620 +      if (unchecked_method_at(index) == old_method) {
   1.621 +        put_method_at(new_method, index);
   1.622 +
   1.623 +        if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
   1.624 +          if (!(*trace_name_printed)) {
   1.625 +            // RC_TRACE_MESG macro has an embedded ResourceMark
   1.626 +            RC_TRACE_MESG(("adjust: name=%s",
   1.627 +                           Klass::cast(old_method->method_holder())->external_name()));
   1.628 +            *trace_name_printed = true;
   1.629 +          }
   1.630 +          // RC_TRACE macro has an embedded ResourceMark
   1.631 +          RC_TRACE(0x00100000, ("vtable method update: %s(%s)",
   1.632 +                                new_method->name()->as_C_string(),
   1.633 +                                new_method->signature()->as_C_string()));
   1.634 +        }
   1.635 +      }
   1.636 +    }
   1.637 +  }
   1.638 +}
   1.639 +
   1.640 +
   1.641 +// Garbage collection
   1.642 +void klassVtable::oop_follow_contents() {
   1.643 +  int len = length();
   1.644 +  for (int i = 0; i < len; i++) {
   1.645 +    MarkSweep::mark_and_push(adr_method_at(i));
   1.646 +  }
   1.647 +}
   1.648 +
   1.649 +#ifndef SERIALGC
   1.650 +void klassVtable::oop_follow_contents(ParCompactionManager* cm) {
   1.651 +  int len = length();
   1.652 +  for (int i = 0; i < len; i++) {
   1.653 +    PSParallelCompact::mark_and_push(cm, adr_method_at(i));
   1.654 +  }
   1.655 +}
   1.656 +#endif // SERIALGC
   1.657 +
   1.658 +void klassVtable::oop_adjust_pointers() {
   1.659 +  int len = length();
   1.660 +  for (int i = 0; i < len; i++) {
   1.661 +    MarkSweep::adjust_pointer(adr_method_at(i));
   1.662 +  }
   1.663 +}
   1.664 +
   1.665 +#ifndef SERIALGC
   1.666 +void klassVtable::oop_update_pointers(ParCompactionManager* cm) {
   1.667 +  const int n = length();
   1.668 +  for (int i = 0; i < n; i++) {
   1.669 +    PSParallelCompact::adjust_pointer(adr_method_at(i));
   1.670 +  }
   1.671 +}
   1.672 +
   1.673 +void klassVtable::oop_update_pointers(ParCompactionManager* cm,
   1.674 +                                      HeapWord* beg_addr, HeapWord* end_addr) {
   1.675 +  const int n = length();
   1.676 +  const int entry_size = vtableEntry::size();
   1.677 +
   1.678 +  int beg_idx = 0;
   1.679 +  HeapWord* const method_0 = (HeapWord*)adr_method_at(0);
   1.680 +  if (beg_addr > method_0) {
   1.681 +    // it's safe to use cast, as we have guarantees on vtable size to be sane
   1.682 +    beg_idx = int((pointer_delta(beg_addr, method_0) + entry_size - 1) / entry_size);
   1.683 +  }
   1.684 +
   1.685 +  oop* const beg_oop = adr_method_at(beg_idx);
   1.686 +  oop* const end_oop = MIN2((oop*)end_addr, adr_method_at(n));
   1.687 +  for (oop* cur_oop = beg_oop; cur_oop < end_oop; cur_oop += entry_size) {
   1.688 +    PSParallelCompact::adjust_pointer(cur_oop);
   1.689 +  }
   1.690 +}
   1.691 +#endif // SERIALGC
   1.692 +
   1.693 +// Iterators
   1.694 +void klassVtable::oop_oop_iterate(OopClosure* blk) {
   1.695 +  int len = length();
   1.696 +  for (int i = 0; i < len; i++) {
   1.697 +    blk->do_oop(adr_method_at(i));
   1.698 +  }
   1.699 +}
   1.700 +
   1.701 +void klassVtable::oop_oop_iterate_m(OopClosure* blk, MemRegion mr) {
   1.702 +  int len = length();
   1.703 +  int i;
   1.704 +  for (i = 0; i < len; i++) {
   1.705 +    if ((HeapWord*)adr_method_at(i) >= mr.start()) break;
   1.706 +  }
   1.707 +  for (; i < len; i++) {
   1.708 +    oop* adr = adr_method_at(i);
   1.709 +    if ((HeapWord*)adr < mr.end()) blk->do_oop(adr);
   1.710 +  }
   1.711 +}
   1.712 +
   1.713 +//-----------------------------------------------------------------------------------------
   1.714 +// Itable code
   1.715 +
   1.716 +// Initialize a itableMethodEntry
   1.717 +void itableMethodEntry::initialize(methodOop m) {
   1.718 +  if (m == NULL) return;
   1.719 +
   1.720 +  _method = m;
   1.721 +}
   1.722 +
   1.723 +klassItable::klassItable(instanceKlassHandle klass) {
   1.724 +  _klass = klass;
   1.725 +
   1.726 +  if (klass->itable_length() > 0) {
   1.727 +    itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();
   1.728 +    if (offset_entry  != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized
   1.729 +      // First offset entry points to the first method_entry
   1.730 +      intptr_t* method_entry  = (intptr_t *)(((address)klass->as_klassOop()) + offset_entry->offset());
   1.731 +      intptr_t* end         = klass->end_of_itable();
   1.732 +
   1.733 +      _table_offset      = (intptr_t*)offset_entry - (intptr_t*)klass->as_klassOop();
   1.734 +      _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();
   1.735 +      _size_method_table = (end - method_entry)                  / itableMethodEntry::size();
   1.736 +      assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");
   1.737 +      return;
   1.738 +    }
   1.739 +  }
   1.740 +
   1.741 +  // This lenght of the itable was either zero, or it has not yet been initialized.
   1.742 +  _table_offset      = 0;
   1.743 +  _size_offset_table = 0;
   1.744 +  _size_method_table = 0;
   1.745 +}
   1.746 +
   1.747 +// Garbage Collection
   1.748 +
   1.749 +void klassItable::oop_follow_contents() {
   1.750 +  // offset table
   1.751 +  itableOffsetEntry* ioe = offset_entry(0);
   1.752 +  for(int i = 0; i < _size_offset_table; i++) {
   1.753 +    MarkSweep::mark_and_push((oop*)&ioe->_interface);
   1.754 +    ioe++;
   1.755 +  }
   1.756 +
   1.757 +  // method table
   1.758 +  itableMethodEntry* ime = method_entry(0);
   1.759 +  for(int j = 0; j < _size_method_table; j++) {
   1.760 +    MarkSweep::mark_and_push((oop*)&ime->_method);
   1.761 +    ime++;
   1.762 +  }
   1.763 +}
   1.764 +
   1.765 +#ifndef SERIALGC
   1.766 +void klassItable::oop_follow_contents(ParCompactionManager* cm) {
   1.767 +  // offset table
   1.768 +  itableOffsetEntry* ioe = offset_entry(0);
   1.769 +  for(int i = 0; i < _size_offset_table; i++) {
   1.770 +    PSParallelCompact::mark_and_push(cm, (oop*)&ioe->_interface);
   1.771 +    ioe++;
   1.772 +  }
   1.773 +
   1.774 +  // method table
   1.775 +  itableMethodEntry* ime = method_entry(0);
   1.776 +  for(int j = 0; j < _size_method_table; j++) {
   1.777 +    PSParallelCompact::mark_and_push(cm, (oop*)&ime->_method);
   1.778 +    ime++;
   1.779 +  }
   1.780 +}
   1.781 +#endif // SERIALGC
   1.782 +
   1.783 +void klassItable::oop_adjust_pointers() {
   1.784 +  // offset table
   1.785 +  itableOffsetEntry* ioe = offset_entry(0);
   1.786 +  for(int i = 0; i < _size_offset_table; i++) {
   1.787 +    MarkSweep::adjust_pointer((oop*)&ioe->_interface);
   1.788 +    ioe++;
   1.789 +  }
   1.790 +
   1.791 +  // method table
   1.792 +  itableMethodEntry* ime = method_entry(0);
   1.793 +  for(int j = 0; j < _size_method_table; j++) {
   1.794 +    MarkSweep::adjust_pointer((oop*)&ime->_method);
   1.795 +    ime++;
   1.796 +  }
   1.797 +}
   1.798 +
   1.799 +#ifndef SERIALGC
   1.800 +void klassItable::oop_update_pointers(ParCompactionManager* cm) {
   1.801 +  // offset table
   1.802 +  itableOffsetEntry* ioe = offset_entry(0);
   1.803 +  for(int i = 0; i < _size_offset_table; i++) {
   1.804 +    PSParallelCompact::adjust_pointer((oop*)&ioe->_interface);
   1.805 +    ioe++;
   1.806 +  }
   1.807 +
   1.808 +  // method table
   1.809 +  itableMethodEntry* ime = method_entry(0);
   1.810 +  for(int j = 0; j < _size_method_table; j++) {
   1.811 +    PSParallelCompact::adjust_pointer((oop*)&ime->_method);
   1.812 +    ime++;
   1.813 +  }
   1.814 +}
   1.815 +
   1.816 +void klassItable::oop_update_pointers(ParCompactionManager* cm,
   1.817 +                                      HeapWord* beg_addr, HeapWord* end_addr) {
   1.818 +  // offset table
   1.819 +  itableOffsetEntry* ioe = offset_entry(0);
   1.820 +  for(int i = 0; i < _size_offset_table; i++) {
   1.821 +    oop* p = (oop*)&ioe->_interface;
   1.822 +    PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
   1.823 +    ioe++;
   1.824 +  }
   1.825 +
   1.826 +  // method table
   1.827 +  itableMethodEntry* ime = method_entry(0);
   1.828 +  for(int j = 0; j < _size_method_table; j++) {
   1.829 +    oop* p = (oop*)&ime->_method;
   1.830 +    PSParallelCompact::adjust_pointer(p, beg_addr, end_addr);
   1.831 +    ime++;
   1.832 +  }
   1.833 +}
   1.834 +#endif // SERIALGC
   1.835 +
   1.836 +// Iterators
   1.837 +void klassItable::oop_oop_iterate(OopClosure* blk) {
   1.838 +  // offset table
   1.839 +  itableOffsetEntry* ioe = offset_entry(0);
   1.840 +  for(int i = 0; i < _size_offset_table; i++) {
   1.841 +    blk->do_oop((oop*)&ioe->_interface);
   1.842 +    ioe++;
   1.843 +  }
   1.844 +
   1.845 +  // method table
   1.846 +  itableMethodEntry* ime = method_entry(0);
   1.847 +  for(int j = 0; j < _size_method_table; j++) {
   1.848 +    blk->do_oop((oop*)&ime->_method);
   1.849 +    ime++;
   1.850 +  }
   1.851 +}
   1.852 +
   1.853 +void klassItable::oop_oop_iterate_m(OopClosure* blk, MemRegion mr) {
   1.854 +  // offset table
   1.855 +  itableOffsetEntry* ioe = offset_entry(0);
   1.856 +  for(int i = 0; i < _size_offset_table; i++) {
   1.857 +    oop* adr = (oop*)&ioe->_interface;
   1.858 +    if (mr.contains(adr)) blk->do_oop(adr);
   1.859 +    ioe++;
   1.860 +  }
   1.861 +
   1.862 +  // method table
   1.863 +  itableMethodEntry* ime = method_entry(0);
   1.864 +  for(int j = 0; j < _size_method_table; j++) {
   1.865 +    oop* adr = (oop*)&ime->_method;
   1.866 +    if (mr.contains(adr)) blk->do_oop(adr);
   1.867 +    ime++;
   1.868 +  }
   1.869 +}
   1.870 +
   1.871 +
   1.872 +static int initialize_count = 0;
   1.873 +
   1.874 +// Initialization
   1.875 +void klassItable::initialize_itable(bool checkconstraints, TRAPS) {
   1.876 +  // Cannot be setup doing bootstrapping
   1.877 +  if (Universe::is_bootstrapping()) return;
   1.878 +
   1.879 +  int num_interfaces = nof_interfaces();
   1.880 +  if (num_interfaces > 0) {
   1.881 +    if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count, _klass->name()->as_C_string());
   1.882 +
   1.883 +    // In debug mode, we got an extra NULL/NULL entry
   1.884 +    debug_only(num_interfaces--);
   1.885 +    assert(num_interfaces > 0, "to few interfaces in offset itable");
   1.886 +
   1.887 +    // Interate through all interfaces
   1.888 +    int i;
   1.889 +    for(i = 0; i < num_interfaces; i++) {
   1.890 +      itableOffsetEntry* ioe = offset_entry(i);
   1.891 +      KlassHandle interf_h (THREAD, ioe->interface_klass());
   1.892 +      assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");
   1.893 +      initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);
   1.894 +    }
   1.895 +
   1.896 +#ifdef ASSERT
   1.897 +    // Check that the last entry is empty
   1.898 +    itableOffsetEntry* ioe = offset_entry(i);
   1.899 +    assert(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");
   1.900 +#endif
   1.901 +  }
   1.902 +}
   1.903 +
   1.904 +
   1.905 +void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {
   1.906 +  objArrayHandle methods(THREAD, instanceKlass::cast(interf_h())->methods());
   1.907 +  int nof_methods = methods()->length();
   1.908 +  HandleMark hm;
   1.909 +  KlassHandle klass = _klass;
   1.910 +  assert(nof_methods > 0, "at least one method must exist for interface to be in vtable")
   1.911 +  Handle interface_loader (THREAD, instanceKlass::cast(interf_h())->class_loader());
   1.912 +  int ime_num = 0;
   1.913 +
   1.914 +  // Skip first methodOop if it is a class initializer
   1.915 +  int i = ((methodOop)methods()->obj_at(0))->name() != vmSymbols::class_initializer_name() ? 0 : 1;
   1.916 +
   1.917 +  // m, method_name, method_signature, klass reset each loop so they
   1.918 +  // don't need preserving across check_signature_loaders call
   1.919 +  // methods needs a handle in case of gc from check_signature_loaders
   1.920 +  for(; i < nof_methods; i++) {
   1.921 +    methodOop m = (methodOop)methods()->obj_at(i);
   1.922 +    symbolOop method_name = m->name();
   1.923 +    symbolOop method_signature = m->signature();
   1.924 +
   1.925 +    // This is same code as in Linkresolver::lookup_instance_method_in_klasses
   1.926 +    methodOop target = klass->uncached_lookup_method(method_name, method_signature);
   1.927 +    while (target != NULL && target->is_static()) {
   1.928 +      // continue with recursive lookup through the superclass
   1.929 +      klassOop super = Klass::cast(target->method_holder())->super();
   1.930 +      target = (super == NULL) ? methodOop(NULL) : Klass::cast(super)->uncached_lookup_method(method_name, method_signature);
   1.931 +    }
   1.932 +    if (target == NULL || !target->is_public() || target->is_abstract()) {
   1.933 +      // Entry do not resolve. Leave it empty
   1.934 +    } else {
   1.935 +      // Entry did resolve, check loader constraints before initializing
   1.936 +      // if checkconstraints requested
   1.937 +      methodHandle  target_h (THREAD, target); // preserve across gc
   1.938 +      if (checkconstraints) {
   1.939 +        Handle method_holder_loader (THREAD, instanceKlass::cast(target->method_holder())->class_loader());
   1.940 +        if (method_holder_loader() != interface_loader()) {
   1.941 +          ResourceMark rm(THREAD);
   1.942 +          char* failed_type_name =
   1.943 +            SystemDictionary::check_signature_loaders(method_signature,
   1.944 +                                                      method_holder_loader,
   1.945 +                                                      interface_loader,
   1.946 +                                                      true, CHECK);
   1.947 +          if (failed_type_name != NULL) {
   1.948 +            const char* msg = "loader constraint violation in interface "
   1.949 +              "itable initialization: when resolving method \"%s\" the class"
   1.950 +              " loader (instance of %s) of the current class, %s, "
   1.951 +              "and the class loader (instance of %s) for interface "
   1.952 +              "%s have different Class objects for the type %s "
   1.953 +              "used in the signature";
   1.954 +            char* sig = target_h()->name_and_sig_as_C_string();
   1.955 +            const char* loader1 = SystemDictionary::loader_name(method_holder_loader());
   1.956 +            char* current = klass->name()->as_C_string();
   1.957 +            const char* loader2 = SystemDictionary::loader_name(interface_loader());
   1.958 +            char* iface = instanceKlass::cast(interf_h())->name()->as_C_string();
   1.959 +            size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
   1.960 +              strlen(current) + strlen(loader2) + strlen(iface) +
   1.961 +              strlen(failed_type_name);
   1.962 +            char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
   1.963 +            jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
   1.964 +                         iface, failed_type_name);
   1.965 +            THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
   1.966 +          }
   1.967 +        }
   1.968 +      }
   1.969 +
   1.970 +      // ime may have moved during GC so recalculate address
   1.971 +      itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target_h());
   1.972 +    }
   1.973 +    // Progress to next entry
   1.974 +    ime_num++;
   1.975 +  }
   1.976 +}
   1.977 +
   1.978 +// Update entry for specic methodOop
   1.979 +void klassItable::initialize_with_method(methodOop m) {
   1.980 +  itableMethodEntry* ime = method_entry(0);
   1.981 +  for(int i = 0; i < _size_method_table; i++) {
   1.982 +    if (ime->method() == m) {
   1.983 +      ime->initialize(m);
   1.984 +    }
   1.985 +    ime++;
   1.986 +  }
   1.987 +}
   1.988 +
   1.989 +void klassItable::adjust_method_entries(methodOop* old_methods, methodOop* new_methods,
   1.990 +                                        int methods_length, bool * trace_name_printed) {
   1.991 +  // search the itable for uses of either obsolete or EMCP methods
   1.992 +  for (int j = 0; j < methods_length; j++) {
   1.993 +    methodOop old_method = old_methods[j];
   1.994 +    methodOop new_method = new_methods[j];
   1.995 +    itableMethodEntry* ime = method_entry(0);
   1.996 +
   1.997 +    for (int i = 0; i < _size_method_table; i++) {
   1.998 +      if (ime->method() == old_method) {
   1.999 +        ime->initialize(new_method);
  1.1000 +
  1.1001 +        if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {
  1.1002 +          if (!(*trace_name_printed)) {
  1.1003 +            // RC_TRACE_MESG macro has an embedded ResourceMark
  1.1004 +            RC_TRACE_MESG(("adjust: name=%s",
  1.1005 +              Klass::cast(old_method->method_holder())->external_name()));
  1.1006 +            *trace_name_printed = true;
  1.1007 +          }
  1.1008 +          // RC_TRACE macro has an embedded ResourceMark
  1.1009 +          RC_TRACE(0x00200000, ("itable method update: %s(%s)",
  1.1010 +            new_method->name()->as_C_string(),
  1.1011 +            new_method->signature()->as_C_string()));
  1.1012 +        }
  1.1013 +        break;
  1.1014 +      }
  1.1015 +      ime++;
  1.1016 +    }
  1.1017 +  }
  1.1018 +}
  1.1019 +
  1.1020 +
  1.1021 +// Setup
  1.1022 +class InterfaceVisiterClosure : public StackObj {
  1.1023 + public:
  1.1024 +  virtual void doit(klassOop intf, int method_count) = 0;
  1.1025 +};
  1.1026 +
  1.1027 +// Visit all interfaces with at-least one method (excluding <clinit>)
  1.1028 +void visit_all_interfaces(objArrayOop transitive_intf, InterfaceVisiterClosure *blk) {
  1.1029 +  // Handle array argument
  1.1030 +  for(int i = 0; i < transitive_intf->length(); i++) {
  1.1031 +    klassOop intf = (klassOop)transitive_intf->obj_at(i);
  1.1032 +    assert(Klass::cast(intf)->is_interface(), "sanity check");
  1.1033 +
  1.1034 +    // Find no. of methods excluding a <clinit>
  1.1035 +    int method_count = instanceKlass::cast(intf)->methods()->length();
  1.1036 +    if (method_count > 0) {
  1.1037 +      methodOop m = (methodOop)instanceKlass::cast(intf)->methods()->obj_at(0);
  1.1038 +      assert(m != NULL && m->is_method(), "sanity check");
  1.1039 +      if (m->name() == vmSymbols::object_initializer_name()) {
  1.1040 +        method_count--;
  1.1041 +      }
  1.1042 +    }
  1.1043 +
  1.1044 +    // Only count interfaces with at least one method
  1.1045 +    if (method_count > 0) {
  1.1046 +      blk->doit(intf, method_count);
  1.1047 +    }
  1.1048 +  }
  1.1049 +}
  1.1050 +
  1.1051 +class CountInterfacesClosure : public InterfaceVisiterClosure {
  1.1052 + private:
  1.1053 +  int _nof_methods;
  1.1054 +  int _nof_interfaces;
  1.1055 + public:
  1.1056 +   CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }
  1.1057 +
  1.1058 +   int nof_methods() const    { return _nof_methods; }
  1.1059 +   int nof_interfaces() const { return _nof_interfaces; }
  1.1060 +
  1.1061 +   void doit(klassOop intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }
  1.1062 +};
  1.1063 +
  1.1064 +class SetupItableClosure : public InterfaceVisiterClosure  {
  1.1065 + private:
  1.1066 +  itableOffsetEntry* _offset_entry;
  1.1067 +  itableMethodEntry* _method_entry;
  1.1068 +  address            _klass_begin;
  1.1069 + public:
  1.1070 +  SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {
  1.1071 +    _klass_begin  = klass_begin;
  1.1072 +    _offset_entry = offset_entry;
  1.1073 +    _method_entry = method_entry;
  1.1074 +  }
  1.1075 +
  1.1076 +  itableMethodEntry* method_entry() const { return _method_entry; }
  1.1077 +
  1.1078 +  void doit(klassOop intf, int method_count) {
  1.1079 +    int offset = ((address)_method_entry) - _klass_begin;
  1.1080 +    _offset_entry->initialize(intf, offset);
  1.1081 +    _offset_entry++;
  1.1082 +    _method_entry += method_count;
  1.1083 +  }
  1.1084 +};
  1.1085 +
  1.1086 +int klassItable::compute_itable_size(objArrayHandle transitive_interfaces) {
  1.1087 +  // Count no of interfaces and total number of interface methods
  1.1088 +  CountInterfacesClosure cic;
  1.1089 +  visit_all_interfaces(transitive_interfaces(), &cic);
  1.1090 +
  1.1091 +  // Add one extra entry in debug mode, so we can null-terminate the table
  1.1092 +  int nof_methods    = cic.nof_methods();
  1.1093 +  int nof_interfaces = cic.nof_interfaces();
  1.1094 +  debug_only(if (nof_interfaces > 0) nof_interfaces++);
  1.1095 +
  1.1096 +  int itable_size = calc_itable_size(nof_interfaces, nof_methods);
  1.1097 +
  1.1098 +  // Statistics
  1.1099 +  update_stats(itable_size * HeapWordSize);
  1.1100 +
  1.1101 +  return itable_size;
  1.1102 +}
  1.1103 +
  1.1104 +
  1.1105 +// Fill out offset table and interface klasses into the itable space
  1.1106 +void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {
  1.1107 +  if (klass->itable_length() == 0) return;
  1.1108 +  assert(!klass->is_interface(), "Should have zero length itable");
  1.1109 +
  1.1110 +  // Count no of interfaces and total number of interface methods
  1.1111 +  CountInterfacesClosure cic;
  1.1112 +  visit_all_interfaces(klass->transitive_interfaces(), &cic);
  1.1113 +  int nof_methods    = cic.nof_methods();
  1.1114 +  int nof_interfaces = cic.nof_interfaces();
  1.1115 +
  1.1116 +  // Add one extra entry in debug mode, so we can null-terminate the table
  1.1117 +  debug_only(if (nof_interfaces > 0) nof_interfaces++);
  1.1118 +
  1.1119 +  assert(compute_itable_size(objArrayHandle(klass->transitive_interfaces())) ==
  1.1120 +         calc_itable_size(nof_interfaces, nof_methods),
  1.1121 +         "mismatch calculation of itable size");
  1.1122 +
  1.1123 +  // Fill-out offset table
  1.1124 +  itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();
  1.1125 +  itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);
  1.1126 +  intptr_t* end               = klass->end_of_itable();
  1.1127 +  assert((oop*)(ime + nof_methods) <= klass->start_of_static_fields(), "wrong offset calculation (1)");
  1.1128 +  assert((oop*)(end) == (oop*)(ime + nof_methods),                     "wrong offset calculation (2)");
  1.1129 +
  1.1130 +  // Visit all interfaces and initialize itable offset table
  1.1131 +  SetupItableClosure sic((address)klass->as_klassOop(), ioe, ime);
  1.1132 +  visit_all_interfaces(klass->transitive_interfaces(), &sic);
  1.1133 +
  1.1134 +#ifdef ASSERT
  1.1135 +  ime  = sic.method_entry();
  1.1136 +  oop* v = (oop*) klass->end_of_itable();
  1.1137 +  assert( (oop*)(ime) == v, "wrong offset calculation (2)");
  1.1138 +#endif
  1.1139 +}
  1.1140 +
  1.1141 +
  1.1142 +// m must be a method in an interface
  1.1143 +int klassItable::compute_itable_index(methodOop m) {
  1.1144 +  klassOop intf = m->method_holder();
  1.1145 +  assert(instanceKlass::cast(intf)->is_interface(), "sanity check");
  1.1146 +  objArrayOop methods = instanceKlass::cast(intf)->methods();
  1.1147 +  int index = 0;
  1.1148 +  while(methods->obj_at(index) != m) {
  1.1149 +    index++;
  1.1150 +    assert(index < methods->length(), "should find index for resolve_invoke");
  1.1151 +  }
  1.1152 +  // Adjust for <clinit>, which is left out of table if first method
  1.1153 +  if (methods->length() > 0 && ((methodOop)methods->obj_at(0))->name() == vmSymbols::class_initializer_name()) {
  1.1154 +    index--;
  1.1155 +  }
  1.1156 +  return index;
  1.1157 +}
  1.1158 +
  1.1159 +void klassVtable::verify(outputStream* st, bool forced) {
  1.1160 +  // make sure table is initialized
  1.1161 +  if (!Universe::is_fully_initialized()) return;
  1.1162 +#ifndef PRODUCT
  1.1163 +  // avoid redundant verifies
  1.1164 +  if (!forced && _verify_count == Universe::verify_count()) return;
  1.1165 +  _verify_count = Universe::verify_count();
  1.1166 +#endif
  1.1167 +  oop* end_of_obj = (oop*)_klass() + _klass()->size();
  1.1168 +  oop* end_of_vtable = (oop *)&table()[_length];
  1.1169 +  if (end_of_vtable > end_of_obj) {
  1.1170 +    fatal1("klass %s: klass object too short (vtable extends beyond end)",
  1.1171 +          _klass->internal_name());
  1.1172 +  }
  1.1173 +
  1.1174 +  for (int i = 0; i < _length; i++) table()[i].verify(this, st);
  1.1175 +  // verify consistency with superKlass vtable
  1.1176 +  klassOop super = _klass->super();
  1.1177 +  if (super != NULL) {
  1.1178 +    instanceKlass* sk = instanceKlass::cast(super);
  1.1179 +    klassVtable* vt = sk->vtable();
  1.1180 +    for (int i = 0; i < vt->length(); i++) {
  1.1181 +      verify_against(st, vt, i);
  1.1182 +    }
  1.1183 +  }
  1.1184 +}
  1.1185 +
  1.1186 +void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {
  1.1187 +  vtableEntry* vte = &vt->table()[index];
  1.1188 +  if (vte->method()->name()      != table()[index].method()->name() ||
  1.1189 +      vte->method()->signature() != table()[index].method()->signature()) {
  1.1190 +    fatal("mismatched name/signature of vtable entries");
  1.1191 +  }
  1.1192 +}
  1.1193 +
  1.1194 +#ifndef PRODUCT
  1.1195 +void klassVtable::print() {
  1.1196 +  ResourceMark rm;
  1.1197 +  tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());
  1.1198 +  for (int i = 0; i < length(); i++) {
  1.1199 +    table()[i].print();
  1.1200 +    tty->cr();
  1.1201 +  }
  1.1202 +}
  1.1203 +#endif
  1.1204 +
  1.1205 +void vtableEntry::verify(klassVtable* vt, outputStream* st) {
  1.1206 +  NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));
  1.1207 +  assert(method() != NULL, "must have set method");
  1.1208 +  method()->verify();
  1.1209 +  // we sub_type, because it could be a miranda method
  1.1210 +  if (!vt->klass()->is_subtype_of(method()->method_holder())) {
  1.1211 +#ifndef PRODUCT
  1.1212 +    print();
  1.1213 +#endif
  1.1214 +    fatal1("vtableEntry %#lx: method is from subclass", this);
  1.1215 +  }
  1.1216 +}
  1.1217 +
  1.1218 +#ifndef PRODUCT
  1.1219 +
  1.1220 +void vtableEntry::print() {
  1.1221 +  ResourceMark rm;
  1.1222 +  tty->print("vtableEntry %s: ", method()->name()->as_C_string());
  1.1223 +  if (Verbose) {
  1.1224 +    tty->print("m %#lx ", (address)method());
  1.1225 +  }
  1.1226 +}
  1.1227 +
  1.1228 +class VtableStats : AllStatic {
  1.1229 + public:
  1.1230 +  static int no_klasses;                // # classes with vtables
  1.1231 +  static int no_array_klasses;          // # array classes
  1.1232 +  static int no_instance_klasses;       // # instanceKlasses
  1.1233 +  static int sum_of_vtable_len;         // total # of vtable entries
  1.1234 +  static int sum_of_array_vtable_len;   // total # of vtable entries in array klasses only
  1.1235 +  static int fixed;                     // total fixed overhead in bytes
  1.1236 +  static int filler;                    // overhead caused by filler bytes
  1.1237 +  static int entries;                   // total bytes consumed by vtable entries
  1.1238 +  static int array_entries;             // total bytes consumed by array vtable entries
  1.1239 +
  1.1240 +  static void do_class(klassOop k) {
  1.1241 +    Klass* kl = k->klass_part();
  1.1242 +    klassVtable* vt = kl->vtable();
  1.1243 +    if (vt == NULL) return;
  1.1244 +    no_klasses++;
  1.1245 +    if (kl->oop_is_instance()) {
  1.1246 +      no_instance_klasses++;
  1.1247 +      kl->array_klasses_do(do_class);
  1.1248 +    }
  1.1249 +    if (kl->oop_is_array()) {
  1.1250 +      no_array_klasses++;
  1.1251 +      sum_of_array_vtable_len += vt->length();
  1.1252 +    }
  1.1253 +    sum_of_vtable_len += vt->length();
  1.1254 +  }
  1.1255 +
  1.1256 +  static void compute() {
  1.1257 +    SystemDictionary::classes_do(do_class);
  1.1258 +    fixed  = no_klasses * oopSize;      // vtable length
  1.1259 +    // filler size is a conservative approximation
  1.1260 +    filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(instanceKlass) - sizeof(arrayKlass) - 1);
  1.1261 +    entries = sizeof(vtableEntry) * sum_of_vtable_len;
  1.1262 +    array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len;
  1.1263 +  }
  1.1264 +};
  1.1265 +
  1.1266 +int VtableStats::no_klasses = 0;
  1.1267 +int VtableStats::no_array_klasses = 0;
  1.1268 +int VtableStats::no_instance_klasses = 0;
  1.1269 +int VtableStats::sum_of_vtable_len = 0;
  1.1270 +int VtableStats::sum_of_array_vtable_len = 0;
  1.1271 +int VtableStats::fixed = 0;
  1.1272 +int VtableStats::filler = 0;
  1.1273 +int VtableStats::entries = 0;
  1.1274 +int VtableStats::array_entries = 0;
  1.1275 +
  1.1276 +void klassVtable::print_statistics() {
  1.1277 +  ResourceMark rm;
  1.1278 +  HandleMark hm;
  1.1279 +  VtableStats::compute();
  1.1280 +  tty->print_cr("vtable statistics:");
  1.1281 +  tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses);
  1.1282 +  int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries;
  1.1283 +  tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed);
  1.1284 +  tty->print_cr("%6d bytes filler overhead", VtableStats::filler);
  1.1285 +  tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries);
  1.1286 +  tty->print_cr("%6d bytes total", total);
  1.1287 +}
  1.1288 +
  1.1289 +bool klassVtable::check_no_old_entries() {
  1.1290 +  // Check that there really is no entry
  1.1291 +  for (int i = 0; i < length(); i++) {
  1.1292 +    methodOop m = unchecked_method_at(i);
  1.1293 +    if (m != NULL) {
  1.1294 +        if (m->is_old()) {
  1.1295 +            return false;
  1.1296 +        }
  1.1297 +    }
  1.1298 +  }
  1.1299 +  return true;
  1.1300 +}
  1.1301 +
  1.1302 +void klassVtable::dump_vtable() {
  1.1303 +  tty->print_cr("vtable dump --");
  1.1304 +  for (int i = 0; i < length(); i++) {
  1.1305 +    methodOop m = unchecked_method_at(i);
  1.1306 +    if (m != NULL) {
  1.1307 +      tty->print("      (%5d)  ", i);
  1.1308 +      m->access_flags().print_on(tty);
  1.1309 +      tty->print(" --  ");
  1.1310 +      m->print_name(tty);
  1.1311 +      tty->cr();
  1.1312 +    }
  1.1313 +  }
  1.1314 +}
  1.1315 +
  1.1316 +int  klassItable::_total_classes;   // Total no. of classes with itables
  1.1317 +long klassItable::_total_size;      // Total no. of bytes used for itables
  1.1318 +
  1.1319 +void klassItable::print_statistics() {
  1.1320 + tty->print_cr("itable statistics:");
  1.1321 + tty->print_cr("%6d classes with itables", _total_classes);
  1.1322 + tty->print_cr("%6d K uses for itables (average by class: %d bytes)", _total_size / K, _total_size / _total_classes);
  1.1323 +}
  1.1324 +
  1.1325 +#endif // PRODUCT

mercurial