duke@435: /* lfoltan@6632: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "gc_implementation/shared/markSweep.inline.hpp" stefank@2314: #include "memory/gcLocker.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "memory/universe.inline.hpp" stefank@2314: #include "oops/instanceKlass.hpp" stefank@2314: #include "oops/klassVtable.hpp" coleenp@4037: #include "oops/method.hpp" stefank@2314: #include "oops/objArrayOop.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "prims/jvmtiRedefineClassesTrace.hpp" stefank@2314: #include "runtime/arguments.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "utilities/copy.hpp" duke@435: drchase@6680: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC drchase@6680: coleenp@4037: inline InstanceKlass* klassVtable::ik() const { coleenp@4037: Klass* k = _klass(); coleenp@4037: assert(k->oop_is_instance(), "not an InstanceKlass"); coleenp@4037: return (InstanceKlass*)k; duke@435: } duke@435: duke@435: duke@435: // this function computes the vtable size (including the size needed for miranda drchase@5732: // methods) and the number of miranda methods in this class. duke@435: // Note on Miranda methods: Let's say there is a class C that implements drchase@5732: // interface I, and none of C's superclasses implements I. drchase@5732: // Let's say there is an abstract method m in I that neither C drchase@5732: // nor any of its super classes implement (i.e there is no method of any access, drchase@5732: // with the same name and signature as m), then m is a Miranda method which is duke@435: // entered as a public abstract method in C's vtable. From then on it should duke@435: // treated as any other public method in C for method over-ride purposes. kamg@4245: void klassVtable::compute_vtable_size_and_num_mirandas( kamg@4245: int* vtable_length_ret, int* num_new_mirandas, kamg@4245: GrowableArray* all_mirandas, Klass* super, kamg@4245: Array* methods, AccessFlags class_flags, kamg@4245: Handle classloader, Symbol* classname, Array* local_interfaces, kamg@4245: TRAPS) { duke@435: No_Safepoint_Verifier nsv; duke@435: duke@435: // set up default result values kamg@4245: int vtable_length = 0; duke@435: duke@435: // start off with super's vtable length coleenp@4037: InstanceKlass* sk = (InstanceKlass*)super; duke@435: vtable_length = super == NULL ? 0 : sk->vtable_length(); duke@435: duke@435: // go thru each method in the methods table to see if it needs a new entry duke@435: int len = methods->length(); duke@435: for (int i = 0; i < len; i++) { coleenp@4037: assert(methods->at(i)->is_method(), "must be a Method*"); coleenp@4037: methodHandle mh(THREAD, methods->at(i)); duke@435: acorn@1087: if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, THREAD)) { duke@435: vtable_length += vtableEntry::size(); // we need a new entry duke@435: } duke@435: } duke@435: kamg@4245: GrowableArray new_mirandas(20); duke@435: // compute the number of mirandas methods that must be added to the end acorn@5848: get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces); kamg@4245: *num_new_mirandas = new_mirandas.length(); kamg@4245: acorn@6080: // Interfaces do not need interface methods in their vtables acorn@6080: // This includes miranda methods and during later processing, default methods acorn@6080: if (!class_flags.is_interface()) { acorn@6080: vtable_length += *num_new_mirandas * vtableEntry::size(); acorn@6080: } duke@435: duke@435: if (Universe::is_bootstrapping() && vtable_length == 0) { duke@435: // array classes don't have their superclass set correctly during duke@435: // bootstrapping duke@435: vtable_length = Universe::base_vtable_size(); duke@435: } duke@435: duke@435: if (super == NULL && !Universe::is_bootstrapping() && duke@435: vtable_length != Universe::base_vtable_size()) { duke@435: // Someone is attempting to redefine java.lang.Object incorrectly. The duke@435: // only way this should happen is from duke@435: // SystemDictionary::resolve_from_stream(), which will detect this later duke@435: // and throw a security exception. So don't assert here to let duke@435: // the exception occur. duke@435: vtable_length = Universe::base_vtable_size(); duke@435: } duke@435: assert(super != NULL || vtable_length == Universe::base_vtable_size(), duke@435: "bad vtable size for class Object"); duke@435: assert(vtable_length % vtableEntry::size() == 0, "bad vtable length"); duke@435: assert(vtable_length >= Universe::base_vtable_size(), "vtable too small"); kamg@4245: kamg@4245: *vtable_length_ret = vtable_length; duke@435: } duke@435: coleenp@4037: int klassVtable::index_of(Method* m, int len) const { drchase@5732: assert(m->has_vtable_index(), "do not ask this of non-vtable methods"); duke@435: return m->vtable_index(); duke@435: } duke@435: drchase@5732: // Copy super class's vtable to the first part (prefix) of this class's vtable, drchase@5732: // and return the number of entries copied. Expects that 'super' is the Java drchase@5732: // super class (arrays can have "array" super classes that must be skipped). duke@435: int klassVtable::initialize_from_super(KlassHandle super) { duke@435: if (super.is_null()) { duke@435: return 0; duke@435: } else { duke@435: // copy methods from superKlass coleenp@4037: // can't inherit from array class, so must be InstanceKlass duke@435: assert(super->oop_is_instance(), "must be instance klass"); coleenp@4037: InstanceKlass* sk = (InstanceKlass*)super(); duke@435: klassVtable* superVtable = sk->vtable(); duke@435: assert(superVtable->length() <= _length, "vtable too short"); duke@435: #ifdef ASSERT duke@435: superVtable->verify(tty, true); duke@435: #endif duke@435: superVtable->copy_vtable_to(table()); duke@435: #ifndef PRODUCT duke@435: if (PrintVtables && Verbose) { acorn@1087: ResourceMark rm; duke@435: tty->print_cr("copy vtable from %s to %s size %d", sk->internal_name(), klass()->internal_name(), _length); duke@435: } duke@435: #endif duke@435: return superVtable->length(); duke@435: } duke@435: } duke@435: drchase@5732: // drchase@5732: // Revised lookup semantics introduced 1.3 (Kestrel beta) duke@435: void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) { duke@435: duke@435: // Note: Arrays can have intermediate array supers. Use java_super to skip them. duke@435: KlassHandle super (THREAD, klass()->java_super()); duke@435: int nofNewEntries = 0; duke@435: duke@435: if (PrintVtables && !klass()->oop_is_array()) { duke@435: ResourceMark rm(THREAD); duke@435: tty->print_cr("Initializing: %s", _klass->name()->as_C_string()); duke@435: } duke@435: duke@435: #ifdef ASSERT duke@435: oop* end_of_obj = (oop*)_klass() + _klass()->size(); duke@435: oop* end_of_vtable = (oop*)&table()[_length]; duke@435: assert(end_of_vtable <= end_of_obj, "vtable extends beyond end"); duke@435: #endif duke@435: duke@435: if (Universe::is_bootstrapping()) { duke@435: // just clear everything duke@435: for (int i = 0; i < _length; i++) table()[i].clear(); duke@435: return; duke@435: } duke@435: duke@435: int super_vtable_len = initialize_from_super(super); duke@435: if (klass()->oop_is_array()) { duke@435: assert(super_vtable_len == _length, "arrays shouldn't introduce new methods"); duke@435: } else { coleenp@4037: assert(_klass->oop_is_instance(), "must be InstanceKlass"); duke@435: coleenp@4037: Array* methods = ik()->methods(); coleenp@4037: int len = methods->length(); duke@435: int initialized = super_vtable_len; duke@435: drchase@5732: // Check each of this class's methods against super; drchase@5732: // if override, replace in copy of super vtable, otherwise append to end duke@435: for (int i = 0; i < len; i++) { drchase@5732: // update_inherited_vtable can stop for gc - ensure using handles duke@435: HandleMark hm(THREAD); coleenp@4037: assert(methods->at(i)->is_method(), "must be a Method*"); coleenp@4037: methodHandle mh(THREAD, methods->at(i)); duke@435: acorn@5848: bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK); duke@435: duke@435: if (needs_new_entry) { duke@435: put_method_at(mh(), initialized); duke@435: mh()->set_vtable_index(initialized); // set primary vtable index duke@435: initialized++; duke@435: } duke@435: } duke@435: acorn@5848: // update vtable with default_methods acorn@5848: Array* default_methods = ik()->default_methods(); acorn@5848: if (default_methods != NULL) { acorn@5848: len = default_methods->length(); acorn@5848: if (len > 0) { acorn@5848: Array* def_vtable_indices = NULL; acorn@5848: if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) { acorn@5848: def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK); acorn@5848: } else { acorn@5848: assert(def_vtable_indices->length() == len, "reinit vtable len?"); acorn@5848: } acorn@5848: for (int i = 0; i < len; i++) { acorn@5848: HandleMark hm(THREAD); acorn@5848: assert(default_methods->at(i)->is_method(), "must be a Method*"); acorn@5848: methodHandle mh(THREAD, default_methods->at(i)); acorn@5848: acorn@5848: bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK); acorn@5848: acorn@5848: // needs new entry acorn@5848: if (needs_new_entry) { acorn@5848: put_method_at(mh(), initialized); acorn@5848: def_vtable_indices->at_put(i, initialized); //set vtable index acorn@5848: initialized++; acorn@5848: } acorn@5848: } acorn@5848: } acorn@5848: } acorn@5848: acorn@5848: // add miranda methods; it will also return the updated initialized acorn@6080: // Interfaces do not need interface methods in their vtables acorn@6080: // This includes miranda methods and during later processing, default methods acorn@6080: if (!ik()->is_interface()) { acorn@6080: initialized = fill_in_mirandas(initialized); acorn@6080: } duke@435: acorn@1087: // In class hierarchies where the accessibility is not increasing (i.e., going from private -> drchase@5732: // package_private -> public/protected), the vtable might actually be smaller than our initial duke@435: // calculation. duke@435: assert(initialized <= _length, "vtable initialization failed"); duke@435: for(;initialized < _length; initialized++) { duke@435: put_method_at(NULL, initialized); duke@435: } duke@435: NOT_PRODUCT(verify(tty, true)); duke@435: } duke@435: } duke@435: acorn@1087: // Called for cases where a method does not override its superclass' vtable entry acorn@1087: // For bytecodes not produced by javac together it is possible that a method does not override acorn@1087: // the superclass's method, but might indirectly override a super-super class's vtable entry acorn@1087: // If none found, return a null superk, else return the superk of the method this does override coleenp@4037: InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method, coleenp@2497: int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) { coleenp@4037: InstanceKlass* superk = initialsuper; acorn@1087: while (superk != NULL && superk->super() != NULL) { coleenp@4037: InstanceKlass* supersuperklass = InstanceKlass::cast(superk->super()); acorn@1087: klassVtable* ssVtable = supersuperklass->vtable(); acorn@1087: if (vtable_index < ssVtable->length()) { coleenp@4037: Method* super_method = ssVtable->method_at(vtable_index); acorn@1087: #ifndef PRODUCT coleenp@2497: Symbol* name= target_method()->name(); coleenp@2497: Symbol* signature = target_method()->signature(); coleenp@2497: assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch"); acorn@1087: #endif acorn@1087: if (supersuperklass->is_override(super_method, target_loader, target_classname, THREAD)) { acorn@1087: #ifndef PRODUCT acorn@1087: if (PrintVtables && Verbose) { acorn@1087: ResourceMark rm(THREAD); acorn@5848: char* sig = target_method()->name_and_sig_as_C_string(); acorn@1087: tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ", acorn@1087: supersuperklass->internal_name(), acorn@5848: _klass->internal_name(), sig, vtable_index); acorn@1087: super_method->access_flags().print_on(tty); acorn@5848: if (super_method->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@1087: tty->print("overriders flags: "); acorn@1087: target_method->access_flags().print_on(tty); acorn@5848: if (target_method->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@1087: } acorn@1087: #endif /*PRODUCT*/ acorn@1087: break; // return found superk acorn@1087: } acorn@1087: } else { acorn@1087: // super class has no vtable entry here, stop transitive search coleenp@4037: superk = (InstanceKlass*)NULL; acorn@1087: break; acorn@1087: } acorn@1087: // if no override found yet, continue to search up coleenp@4037: superk = InstanceKlass::cast(superk->super()); acorn@1087: } duke@435: acorn@1087: return superk; duke@435: } duke@435: duke@435: // Update child's copy of super vtable for overrides drchase@5732: // OR return true if a new vtable entry is required. coleenp@4037: // Only called for InstanceKlass's, i.e. not for arrays duke@435: // If that changed, could not use _klass as handle for klass acorn@5848: bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method, acorn@5848: int super_vtable_len, int default_index, acorn@5848: bool checkconstraints, TRAPS) { duke@435: ResourceMark rm; duke@435: bool allocate_new = true; coleenp@4037: assert(klass->oop_is_instance(), "must be InstanceKlass"); duke@435: acorn@5848: Array* def_vtable_indices = NULL; acorn@5848: bool is_default = false; acorn@5848: // default methods are concrete methods in superinterfaces which are added to the vtable acorn@5848: // with their real method_holder acorn@5848: // Since vtable and itable indices share the same storage, don't touch acorn@5848: // the default method's real vtable/itable index acorn@5848: // default_vtable_indices stores the vtable value relative to this inheritor acorn@5848: if (default_index >= 0 ) { acorn@5848: is_default = true; acorn@5848: def_vtable_indices = klass->default_vtable_indices(); acorn@5848: assert(def_vtable_indices != NULL, "def vtable alloc?"); acorn@5848: assert(default_index <= def_vtable_indices->length(), "def vtable len?"); acorn@5848: } else { acorn@5848: assert(klass == target_method()->method_holder(), "caller resp."); acorn@5848: // Initialize the method's vtable index to "nonvirtual". acorn@5848: // If we allocate a vtable entry, we will update it to a non-negative number. acorn@5848: target_method()->set_vtable_index(Method::nonvirtual_vtable_index); acorn@5848: } duke@435: duke@435: // Static and methods are never in duke@435: if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) { duke@435: return false; duke@435: } duke@435: drchase@5732: if (target_method->is_final_method(klass->access_flags())) { duke@435: // a final method never needs a new entry; final methods can be statically duke@435: // resolved and they have to be present in the vtable only if they override duke@435: // a super's method, in which case they re-use its entry duke@435: allocate_new = false; drchase@5732: } else if (klass->is_interface()) { drchase@5732: allocate_new = false; // see note below in needs_new_vtable_entry drchase@5732: // An interface never allocates new vtable slots, only inherits old ones. drchase@5732: // This method will either be assigned its own itable index later, drchase@5732: // or be assigned an inherited vtable index in the loop below. acorn@6080: // default methods inherited by classes store their vtable indices acorn@6080: // in the inheritor's default_vtable_indices acorn@6080: // default methods inherited by interfaces may already have a acorn@6080: // valid itable index, if so, don't change it acorn@6080: // overpass methods in an interface will be assigned an itable index later acorn@6080: // by an inheriting class acorn@6080: if (!is_default || !target_method()->has_itable_index()) { acorn@6080: target_method()->set_vtable_index(Method::pending_itable_index); acorn@6080: } duke@435: } duke@435: duke@435: // we need a new entry if there is no superclass duke@435: if (klass->super() == NULL) { duke@435: return allocate_new; duke@435: } duke@435: acorn@5786: // private methods in classes always have a new entry in the vtable acorn@1087: // specification interpretation since classic has acorn@1087: // private methods not overriding acorn@5786: // JDK8 adds private methods in interfaces which require invokespecial duke@435: if (target_method()->is_private()) { duke@435: return allocate_new; duke@435: } duke@435: duke@435: // search through the vtable and update overridden entries duke@435: // Since check_signature_loaders acquires SystemDictionary_lock acorn@1087: // which can block for gc, once we are in this loop, use handles acorn@1087: // For classfiles built with >= jdk7, we now look for transitive overrides duke@435: coleenp@2497: Symbol* name = target_method()->name(); coleenp@2497: Symbol* signature = target_method()->signature(); acorn@5848: acorn@5848: KlassHandle target_klass(THREAD, target_method()->method_holder()); acorn@5848: if (target_klass == NULL) { acorn@5848: target_klass = _klass; acorn@5848: } acorn@5848: acorn@5848: Handle target_loader(THREAD, target_klass->class_loader()); acorn@5848: acorn@5848: Symbol* target_classname = target_klass->name(); duke@435: for(int i = 0; i < super_vtable_len; i++) { coleenp@4037: Method* super_method = method_at(i); duke@435: // Check if method name matches coleenp@2497: if (super_method->name() == name && super_method->signature() == signature) { duke@435: acorn@1087: // get super_klass for method_holder for the found method coleenp@4251: InstanceKlass* super_klass = super_method->method_holder(); duke@435: acorn@5848: if (is_default acorn@5848: || ((super_klass->is_override(super_method, target_loader, target_classname, THREAD)) acorn@5848: || ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION) acorn@5848: && ((super_klass = find_transitive_override(super_klass, acorn@5848: target_method, i, target_loader, acorn@5848: target_classname, THREAD)) acorn@5848: != (InstanceKlass*)NULL)))) acorn@5848: { acorn@1087: // overriding, so no new entry acorn@1087: allocate_new = false; duke@435: duke@435: if (checkconstraints) { duke@435: // Override vtable entry if passes loader constraint check duke@435: // if loader constraint checking requested duke@435: // No need to visit his super, since he and his super duke@435: // have already made any needed loader constraints. duke@435: // Since loader constraints are transitive, it is enough duke@435: // to link to the first super, and we get all the others. duke@435: Handle super_loader(THREAD, super_klass->class_loader()); duke@435: acorn@1087: if (target_loader() != super_loader()) { duke@435: ResourceMark rm(THREAD); acorn@4840: Symbol* failed_type_symbol = acorn@1087: SystemDictionary::check_signature_loaders(signature, target_loader, duke@435: super_loader, true, duke@435: CHECK_(false)); acorn@4840: if (failed_type_symbol != NULL) { duke@435: const char* msg = "loader constraint violation: when resolving " duke@435: "overridden method \"%s\" the class loader (instance" duke@435: " of %s) of the current class, %s, and its superclass loader " duke@435: "(instance of %s), have different Class objects for the type " duke@435: "%s used in the signature"; duke@435: char* sig = target_method()->name_and_sig_as_C_string(); acorn@1087: const char* loader1 = SystemDictionary::loader_name(target_loader()); acorn@5848: char* current = target_klass->name()->as_C_string(); duke@435: const char* loader2 = SystemDictionary::loader_name(super_loader()); acorn@4840: char* failed_type_name = failed_type_symbol->as_C_string(); duke@435: size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + duke@435: strlen(current) + strlen(loader2) + strlen(failed_type_name); duke@435: char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); duke@435: jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2, duke@435: failed_type_name); duke@435: THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false); duke@435: } duke@435: } acorn@1087: } acorn@1087: acorn@5848: put_method_at(target_method(), i); acorn@5848: if (!is_default) { acorn@5848: target_method()->set_vtable_index(i); acorn@5848: } else { acorn@5848: if (def_vtable_indices != NULL) { acorn@5848: def_vtable_indices->at_put(default_index, i); acorn@5848: } acorn@5848: assert(super_method->is_default_method() || super_method->is_overpass() acorn@5848: || super_method->is_abstract(), "default override error"); acorn@5848: } acorn@5848: acorn@5848: acorn@1087: #ifndef PRODUCT acorn@1087: if (PrintVtables && Verbose) { acorn@5848: ResourceMark rm(THREAD); acorn@5848: char* sig = target_method()->name_and_sig_as_C_string(); acorn@1087: tty->print("overriding with %s::%s index %d, original flags: ", acorn@5848: target_klass->internal_name(), sig, i); acorn@1087: super_method->access_flags().print_on(tty); acorn@5848: if (super_method->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@5848: if (super_method->is_overpass()) { acorn@5848: tty->print("overpass"); acorn@5848: } acorn@1087: tty->print("overriders flags: "); acorn@1087: target_method->access_flags().print_on(tty); acorn@5848: if (target_method->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@5848: if (target_method->is_overpass()) { acorn@5848: tty->print("overpass"); acorn@5848: } acorn@1087: tty->cr(); duke@435: } acorn@1087: #endif /*PRODUCT*/ acorn@1087: } else { acorn@1087: // allocate_new = true; default. We might override one entry, acorn@1087: // but not override another. Once we override one, not need new duke@435: #ifndef PRODUCT acorn@1087: if (PrintVtables && Verbose) { acorn@5848: ResourceMark rm(THREAD); acorn@5848: char* sig = target_method()->name_and_sig_as_C_string(); acorn@1087: tty->print("NOT overriding with %s::%s index %d, original flags: ", acorn@5848: target_klass->internal_name(), sig,i); acorn@1087: super_method->access_flags().print_on(tty); acorn@5848: if (super_method->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@5848: if (super_method->is_overpass()) { acorn@5848: tty->print("overpass"); acorn@5848: } acorn@1087: tty->print("overriders flags: "); acorn@1087: target_method->access_flags().print_on(tty); acorn@5848: if (target_method->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@5848: if (target_method->is_overpass()) { acorn@5848: tty->print("overpass"); acorn@5848: } acorn@1087: tty->cr(); acorn@1087: } duke@435: #endif /*PRODUCT*/ duke@435: } duke@435: } duke@435: } duke@435: return allocate_new; duke@435: } duke@435: coleenp@4037: void klassVtable::put_method_at(Method* m, int index) { duke@435: #ifndef PRODUCT duke@435: if (PrintVtables && Verbose) { acorn@1087: ResourceMark rm; acorn@6080: const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : ""; acorn@6080: tty->print("adding %s at index %d, flags: ", sig, index); acorn@6080: if (m != NULL) { acorn@6080: m->access_flags().print_on(tty); acorn@6080: if (m->is_default_method()) { acorn@6080: tty->print("default "); acorn@6080: } acorn@6080: if (m->is_overpass()) { acorn@6080: tty->print("overpass"); acorn@6080: } acorn@6080: } acorn@6080: tty->cr(); duke@435: } duke@435: #endif duke@435: table()[index].set(m); duke@435: } duke@435: duke@435: // Find out if a method "m" with superclass "super", loader "classloader" and duke@435: // name "classname" needs a new vtable entry. Let P be a class package defined duke@435: // by "classloader" and "classname". duke@435: // NOTE: The logic used here is very similar to the one used for computing duke@435: // the vtables indices for a method. We cannot directly use that function because, coleenp@4037: // we allocate the InstanceKlass at load time, and that requires that the acorn@1087: // superclass has been loaded. acorn@1087: // However, the vtable entries are filled in at link time, and therefore acorn@1087: // the superclass' vtable may not yet have been filled in. acorn@1087: bool klassVtable::needs_new_vtable_entry(methodHandle target_method, coleenp@4037: Klass* super, acorn@1087: Handle classloader, coleenp@2497: Symbol* classname, acorn@1087: AccessFlags class_flags, acorn@1087: TRAPS) { drchase@5732: if (class_flags.is_interface()) { drchase@5732: // Interfaces do not use vtables, so there is no point to assigning drchase@5732: // a vtable index to any of their methods. If we refrain from doing this, drchase@5732: // we can use Method::_vtable_index to hold the itable index drchase@5732: return false; drchase@5732: } kamg@4245: drchase@5732: if (target_method->is_final_method(class_flags) || duke@435: // a final method never needs a new entry; final methods can be statically duke@435: // resolved and they have to be present in the vtable only if they override duke@435: // a super's method, in which case they re-use its entry acorn@1087: (target_method()->is_static()) || duke@435: // static methods don't need to be in vtable acorn@1087: (target_method()->name() == vmSymbols::object_initializer_name()) duke@435: // is never called dynamically-bound duke@435: ) { duke@435: return false; duke@435: } duke@435: acorn@5848: // Concrete interface methods do not need new entries, they override acorn@5848: // abstract method entries using default inheritance rules acorn@5848: if (target_method()->method_holder() != NULL && acorn@5848: target_method()->method_holder()->is_interface() && acorn@5848: !target_method()->is_abstract() ) { acorn@5848: return false; acorn@5848: } acorn@5848: duke@435: // we need a new entry if there is no superclass duke@435: if (super == NULL) { duke@435: return true; duke@435: } duke@435: acorn@5786: // private methods in classes always have a new entry in the vtable acorn@1087: // specification interpretation since classic has acorn@1087: // private methods not overriding acorn@5848: // JDK8 adds private methods in interfaces which require invokespecial acorn@1087: if (target_method()->is_private()) { duke@435: return true; duke@435: } duke@435: duke@435: // search through the super class hierarchy to see if we need duke@435: // a new entry acorn@1087: ResourceMark rm; coleenp@2497: Symbol* name = target_method()->name(); coleenp@2497: Symbol* signature = target_method()->signature(); coleenp@4037: Klass* k = super; coleenp@4037: Method* super_method = NULL; coleenp@4037: InstanceKlass *holder = NULL; coleenp@4037: Method* recheck_method = NULL; duke@435: while (k != NULL) { duke@435: // lookup through the hierarchy for a method with matching name and sign. coleenp@4037: super_method = InstanceKlass::cast(k)->lookup_method(name, signature); acorn@1087: if (super_method == NULL) { duke@435: break; // we still have to search for a matching miranda method duke@435: } duke@435: // get the class holding the matching method acorn@1087: // make sure you use that class for is_override coleenp@4251: InstanceKlass* superk = super_method->method_holder(); acorn@1087: // we want only instance method matches acorn@1087: // pretend private methods are not in the super vtable acorn@1087: // since we do override around them: e.g. a.m pub/b.m private/c.m pub, acorn@1087: // ignore private, c.m pub does override a.m pub acorn@1087: // For classes that were not javac'd together, we also do transitive overriding around acorn@1087: // methods that have less accessibility acorn@1087: if ((!super_method->is_static()) && acorn@1087: (!super_method->is_private())) { acorn@1087: if (superk->is_override(super_method, classloader, classname, THREAD)) { duke@435: return false; acorn@1087: // else keep looking for transitive overrides duke@435: } duke@435: } duke@435: acorn@1087: // Start with lookup result and continue to search up acorn@1087: k = superk->super(); // haven't found an override match yet; continue to look duke@435: } duke@435: duke@435: // if the target method is public or protected it may have a matching duke@435: // miranda method in the super, whose entry it should re-use. acorn@1087: // Actually, to handle cases that javac would not generate, we need acorn@1087: // this check for all access permissions. coleenp@4037: InstanceKlass *sk = InstanceKlass::cast(super); acorn@1087: if (sk->has_miranda_methods()) { lfoltan@6632: if (sk->lookup_method_in_all_interfaces(name, signature, Klass::normal) != NULL) { acorn@1087: return false; // found a matching miranda; we do not need a new entry duke@435: } duke@435: } duke@435: return true; // found no match; we need a new entry duke@435: } duke@435: duke@435: // Support for miranda methods duke@435: duke@435: // get the vtable index of a miranda method with matching "name" and "signature" coleenp@2497: int klassVtable::index_of_miranda(Symbol* name, Symbol* signature) { duke@435: // search from the bottom, might be faster duke@435: for (int i = (length() - 1); i >= 0; i--) { coleenp@4037: Method* m = table()[i].method(); duke@435: if (is_miranda_entry_at(i) && duke@435: m->name() == name && m->signature() == signature) { duke@435: return i; duke@435: } duke@435: } coleenp@4037: return Method::invalid_vtable_index; duke@435: } duke@435: drchase@5732: // check if an entry at an index is miranda drchase@5732: // requires that method m at entry be declared ("held") by an interface. duke@435: bool klassVtable::is_miranda_entry_at(int i) { coleenp@4037: Method* m = method_at(i); coleenp@4037: Klass* method_holder = m->method_holder(); coleenp@4037: InstanceKlass *mhk = InstanceKlass::cast(method_holder); duke@435: acorn@5786: // miranda methods are public abstract instance interface methods in a class's vtable duke@435: if (mhk->is_interface()) { kamg@4245: assert(m->is_public(), "should be public"); duke@435: assert(ik()->implements_interface(method_holder) , "this class should implement the interface"); acorn@6080: // the search could find a miranda or a default method acorn@6080: if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) { acorn@6080: return true; acorn@6080: } duke@435: } duke@435: return false; duke@435: } duke@435: acorn@5848: // check if a method is a miranda method, given a class's methods table, acorn@5848: // its default_method table and its super acorn@6145: // Miranda methods are calculated twice: acorn@6145: // first: before vtable size calculation: including abstract and default acorn@6145: // This is seen by default method creation acorn@6145: // Second: recalculated during vtable initialization: only abstract acorn@6145: // This is seen by link resolution and selection. acorn@5848: // "miranda" means not static, not defined by this class. acorn@5848: // private methods in interfaces do not belong in the miranda list. duke@435: // the caller must make sure that the method belongs to an interface implemented by the class acorn@5786: // Miranda methods only include public interface instance methods acorn@5848: // Not private methods, not static methods, not default == concrete abstract acorn@6080: // Miranda methods also do not include overpass methods in interfaces acorn@5848: bool klassVtable::is_miranda(Method* m, Array* class_methods, acorn@5848: Array* default_methods, Klass* super) { acorn@6080: if (m->is_static() || m->is_private() || m->is_overpass()) { bharadwaj@4998: return false; bharadwaj@4998: } coleenp@2497: Symbol* name = m->name(); coleenp@2497: Symbol* signature = m->signature(); acorn@6145: acorn@6145: if (InstanceKlass::find_instance_method(class_methods, name, signature) == NULL) { dsamersoff@2360: // did not find it in the method table of the current class acorn@5848: if ((default_methods == NULL) || acorn@5848: InstanceKlass::find_method(default_methods, name, signature) == NULL) { acorn@5848: if (super == NULL) { acorn@5848: // super doesn't exist acorn@5848: return true; acorn@5848: } dsamersoff@2360: acorn@5848: Method* mo = InstanceKlass::cast(super)->lookup_method(name, signature); acorn@6145: while (mo != NULL && mo->access_flags().is_static() acorn@6145: && mo->method_holder() != NULL acorn@6145: && mo->method_holder()->super() != NULL) acorn@6145: { lfoltan@6632: mo = mo->method_holder()->super()->uncached_lookup_method(name, signature, Klass::normal); acorn@6145: } acorn@5848: if (mo == NULL || mo->access_flags().is_private() ) { acorn@5848: // super class hierarchy does not implement it or protection is different acorn@5848: return true; acorn@5848: } duke@435: } duke@435: } dsamersoff@2360: duke@435: return false; duke@435: } duke@435: drchase@5732: // Scans current_interface_methods for miranda methods that do not acorn@5848: // already appear in new_mirandas, or default methods, and are also not defined-and-non-private drchase@5732: // in super (superclass). These mirandas are added to all_mirandas if it is drchase@5732: // not null; in addition, those that are not duplicates of miranda methods drchase@5732: // inherited by super from its interfaces are added to new_mirandas. drchase@5732: // Thus, new_mirandas will be the set of mirandas that this class introduces, drchase@5732: // all_mirandas will be the set of all mirandas applicable to this class drchase@5732: // including all defined in superclasses. kamg@4245: void klassVtable::add_new_mirandas_to_lists( kamg@4245: GrowableArray* new_mirandas, GrowableArray* all_mirandas, kamg@4245: Array* current_interface_methods, Array* class_methods, acorn@5848: Array* default_methods, Klass* super) { acorn@5848: duke@435: // iterate thru the current interface's method to see if it a miranda duke@435: int num_methods = current_interface_methods->length(); duke@435: for (int i = 0; i < num_methods; i++) { coleenp@4037: Method* im = current_interface_methods->at(i); duke@435: bool is_duplicate = false; kamg@4245: int num_of_current_mirandas = new_mirandas->length(); duke@435: // check for duplicate mirandas in different interfaces we implement duke@435: for (int j = 0; j < num_of_current_mirandas; j++) { kamg@4245: Method* miranda = new_mirandas->at(j); duke@435: if ((im->name() == miranda->name()) && duke@435: (im->signature() == miranda->signature())) { duke@435: is_duplicate = true; duke@435: break; duke@435: } duke@435: } duke@435: duke@435: if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable acorn@5848: if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all? coleenp@4037: InstanceKlass *sk = InstanceKlass::cast(super); duke@435: // check if it is a duplicate of a super's miranda lfoltan@6632: if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::normal) == NULL) { kamg@4245: new_mirandas->append(im); kamg@4245: } kamg@4245: if (all_mirandas != NULL) { kamg@4245: all_mirandas->append(im); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: } duke@435: kamg@4245: void klassVtable::get_mirandas(GrowableArray* new_mirandas, kamg@4245: GrowableArray* all_mirandas, coleenp@4037: Klass* super, Array* class_methods, acorn@5848: Array* default_methods, coleenp@4037: Array* local_interfaces) { kamg@4245: assert((new_mirandas->length() == 0) , "current mirandas must be 0"); duke@435: duke@435: // iterate thru the local interfaces looking for a miranda duke@435: int num_local_ifs = local_interfaces->length(); duke@435: for (int i = 0; i < num_local_ifs; i++) { coleenp@4037: InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i)); kamg@4245: add_new_mirandas_to_lists(new_mirandas, all_mirandas, acorn@5848: ik->methods(), class_methods, acorn@5848: default_methods, super); duke@435: // iterate thru each local's super interfaces coleenp@4037: Array* super_ifs = ik->transitive_interfaces(); duke@435: int num_super_ifs = super_ifs->length(); duke@435: for (int j = 0; j < num_super_ifs; j++) { coleenp@4037: InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j)); kamg@4245: add_new_mirandas_to_lists(new_mirandas, all_mirandas, acorn@5848: sik->methods(), class_methods, acorn@5848: default_methods, super); duke@435: } duke@435: } duke@435: } duke@435: drchase@5732: // Discover miranda methods ("miranda" = "interface abstract, no binding"), drchase@5732: // and append them into the vtable starting at index initialized, drchase@5732: // return the new value of initialized. acorn@6080: // Miranda methods use vtable entries, but do not get assigned a vtable_index acorn@6080: // The vtable_index is discovered by searching from the end of the vtable drchase@5732: int klassVtable::fill_in_mirandas(int initialized) { kamg@4245: GrowableArray mirandas(20); kamg@4245: get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(), acorn@5848: ik()->default_methods(), ik()->local_interfaces()); kamg@4245: for (int i = 0; i < mirandas.length(); i++) { acorn@5848: if (PrintVtables && Verbose) { acorn@5848: Method* meth = mirandas.at(i); acorn@5848: ResourceMark rm(Thread::current()); acorn@5848: if (meth != NULL) { acorn@5848: char* sig = meth->name_and_sig_as_C_string(); acorn@5848: tty->print("fill in mirandas with %s index %d, flags: ", acorn@5848: sig, initialized); acorn@5848: meth->access_flags().print_on(tty); acorn@5848: if (meth->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@5848: tty->cr(); acorn@5848: } acorn@5848: } drchase@5732: put_method_at(mirandas.at(i), initialized); drchase@5732: ++initialized; duke@435: } drchase@5732: return initialized; duke@435: } duke@435: drchase@5732: // Copy this class's vtable to the vtable beginning at start. drchase@5732: // Used to copy superclass vtable to prefix of subclass's vtable. duke@435: void klassVtable::copy_vtable_to(vtableEntry* start) { duke@435: Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size()); duke@435: } duke@435: dcubed@4562: #if INCLUDE_JVMTI acorn@5848: bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) { acorn@5848: // If old_method is default, find this vtable index in default_vtable_indices acorn@5848: // and replace that method in the _default_methods list acorn@5848: bool updated = false; acorn@5848: acorn@5848: Array* default_methods = ik()->default_methods(); acorn@5848: if (default_methods != NULL) { acorn@5848: int len = default_methods->length(); acorn@5848: for (int idx = 0; idx < len; idx++) { acorn@5848: if (vtable_index == ik()->default_vtable_indices()->at(idx)) { acorn@5848: if (default_methods->at(idx) == old_method) { acorn@5848: default_methods->at_put(idx, new_method); acorn@5848: updated = true; acorn@5848: } acorn@5848: break; acorn@5848: } acorn@5848: } acorn@5848: } acorn@5848: return updated; acorn@5848: } coleenp@4037: void klassVtable::adjust_method_entries(Method** old_methods, Method** new_methods, duke@435: int methods_length, bool * trace_name_printed) { duke@435: // search the vtable for uses of either obsolete or EMCP methods duke@435: for (int j = 0; j < methods_length; j++) { coleenp@4037: Method* old_method = old_methods[j]; coleenp@4037: Method* new_method = new_methods[j]; duke@435: duke@435: // In the vast majority of cases we could get the vtable index duke@435: // by using: old_method->vtable_index() duke@435: // However, there are rare cases, eg. sun.awt.X11.XDecoratedPeer.getX() duke@435: // in sun.awt.X11.XFramePeer where methods occur more than once in the duke@435: // vtable, so, alas, we must do an exhaustive search. duke@435: for (int index = 0; index < length(); index++) { duke@435: if (unchecked_method_at(index) == old_method) { duke@435: put_method_at(new_method, index); acorn@5848: // For default methods, need to update the _default_methods array acorn@5848: // which can only have one method entry for a given signature acorn@5848: bool updated_default = false; acorn@5848: if (old_method->is_default_method()) { acorn@5848: updated_default = adjust_default_method(index, old_method, new_method); acorn@5848: } duke@435: duke@435: if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { duke@435: if (!(*trace_name_printed)) { duke@435: // RC_TRACE_MESG macro has an embedded ResourceMark acorn@5848: RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s", acorn@5848: klass()->external_name(), coleenp@4251: old_method->method_holder()->external_name())); duke@435: *trace_name_printed = true; duke@435: } duke@435: // RC_TRACE macro has an embedded ResourceMark acorn@5848: RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s", duke@435: new_method->name()->as_C_string(), acorn@5848: new_method->signature()->as_C_string(), acorn@5848: updated_default ? "true" : "false")); duke@435: } dcubed@4562: // cannot 'break' here; see for-loop comment above. duke@435: } duke@435: } duke@435: } duke@435: } duke@435: dcubed@4562: // a vtable should never contain old or obsolete methods dcubed@4562: bool klassVtable::check_no_old_or_obsolete_entries() { dcubed@4562: for (int i = 0; i < length(); i++) { dcubed@4562: Method* m = unchecked_method_at(i); dcubed@4562: if (m != NULL && dcubed@4562: (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) { dcubed@4562: return false; dcubed@4562: } dcubed@4562: } dcubed@4562: return true; dcubed@4562: } dcubed@4562: dcubed@4562: void klassVtable::dump_vtable() { dcubed@4562: tty->print_cr("vtable dump --"); dcubed@4562: for (int i = 0; i < length(); i++) { dcubed@4562: Method* m = unchecked_method_at(i); dcubed@4562: if (m != NULL) { dcubed@4562: tty->print(" (%5d) ", i); dcubed@4562: m->access_flags().print_on(tty); acorn@5848: if (m->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@5848: if (m->is_overpass()) { acorn@5848: tty->print("overpass"); acorn@5848: } dcubed@4562: tty->print(" -- "); dcubed@4562: m->print_name(tty); dcubed@4562: tty->cr(); dcubed@4562: } dcubed@4562: } dcubed@4562: } dcubed@4562: #endif // INCLUDE_JVMTI dcubed@4562: coleenp@2777: // CDS/RedefineClasses support - clear vtables so they can be reinitialized coleenp@2777: void klassVtable::clear_vtable() { coleenp@2777: for (int i = 0; i < _length; i++) table()[i].clear(); coleenp@2777: } coleenp@2777: coleenp@2777: bool klassVtable::is_initialized() { coleenp@2777: return _length == 0 || table()[0].method() != NULL; coleenp@2777: } coleenp@2777: duke@435: //----------------------------------------------------------------------------------------- duke@435: // Itable code duke@435: duke@435: // Initialize a itableMethodEntry coleenp@4037: void itableMethodEntry::initialize(Method* m) { duke@435: if (m == NULL) return; duke@435: duke@435: _method = m; duke@435: } duke@435: duke@435: klassItable::klassItable(instanceKlassHandle klass) { duke@435: _klass = klass; duke@435: duke@435: if (klass->itable_length() > 0) { duke@435: itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable(); duke@435: if (offset_entry != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized duke@435: // First offset entry points to the first method_entry coleenp@4037: intptr_t* method_entry = (intptr_t *)(((address)klass()) + offset_entry->offset()); duke@435: intptr_t* end = klass->end_of_itable(); duke@435: coleenp@4037: _table_offset = (intptr_t*)offset_entry - (intptr_t*)klass(); duke@435: _size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size(); duke@435: _size_method_table = (end - method_entry) / itableMethodEntry::size(); duke@435: assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation"); duke@435: return; duke@435: } duke@435: } duke@435: dcubed@451: // The length of the itable was either zero, or it has not yet been initialized. duke@435: _table_offset = 0; duke@435: _size_offset_table = 0; duke@435: _size_method_table = 0; duke@435: } duke@435: duke@435: static int initialize_count = 0; duke@435: duke@435: // Initialization duke@435: void klassItable::initialize_itable(bool checkconstraints, TRAPS) { drchase@5732: if (_klass->is_interface()) { acorn@5848: // This needs to go after vtable indices are assigned but acorn@5848: // before implementors need to know the number of itable indices. acorn@5848: assign_itable_indices_for_interface(_klass()); drchase@5732: } drchase@5732: dcubed@451: // Cannot be setup doing bootstrapping, interfaces don't have dcubed@451: // itables, and klass with only ones entry have empty itables dcubed@451: if (Universe::is_bootstrapping() || dcubed@451: _klass->is_interface() || dcubed@451: _klass->itable_length() == itableOffsetEntry::size()) return; duke@435: dcubed@451: // There's alway an extra itable entry so we can null-terminate it. dcubed@451: guarantee(size_offset_table() >= 1, "too small"); dcubed@451: int num_interfaces = size_offset_table() - 1; duke@435: if (num_interfaces > 0) { dcubed@451: if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count, dcubed@451: _klass->name()->as_C_string()); duke@435: duke@435: acorn@1087: // Iterate through all interfaces duke@435: int i; duke@435: for(i = 0; i < num_interfaces; i++) { duke@435: itableOffsetEntry* ioe = offset_entry(i); coleenp@4037: HandleMark hm(THREAD); duke@435: KlassHandle interf_h (THREAD, ioe->interface_klass()); duke@435: assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable"); duke@435: initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK); duke@435: } duke@435: duke@435: } dcubed@451: // Check that the last entry is empty dcubed@451: itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1); dcubed@451: guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing"); duke@435: } duke@435: duke@435: drchase@5732: inline bool interface_method_needs_itable_index(Method* m) { drchase@5732: if (m->is_static()) return false; // e.g., Stream.empty drchase@5732: if (m->is_initializer()) return false; // or drchase@5732: // If an interface redeclares a method from java.lang.Object, drchase@5732: // it should already have a vtable index, don't touch it. drchase@5732: // e.g., CharSequence.toString (from initialize_vtable) drchase@5732: // if (m->has_vtable_index()) return false; // NO! drchase@5732: return true; drchase@5732: } drchase@5732: acorn@5848: int klassItable::assign_itable_indices_for_interface(Klass* klass) { drchase@5732: // an interface does not have an itable, but its methods need to be numbered drchase@5732: if (TraceItables) tty->print_cr("%3d: Initializing itable for interface %s", ++initialize_count, drchase@5732: klass->name()->as_C_string()); drchase@5732: Array* methods = InstanceKlass::cast(klass)->methods(); drchase@5732: int nof_methods = methods->length(); drchase@5732: int ime_num = 0; drchase@5732: for (int i = 0; i < nof_methods; i++) { drchase@5732: Method* m = methods->at(i); drchase@5732: if (interface_method_needs_itable_index(m)) { drchase@5732: assert(!m->is_final_method(), "no final interface methods"); drchase@5732: // If m is already assigned a vtable index, do not disturb it. acorn@6080: if (TraceItables && Verbose) { acorn@6080: ResourceMark rm; acorn@6080: const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : ""; acorn@6080: if (m->has_vtable_index()) { acorn@6080: tty->print("itable index %d for method: %s, flags: ", m->vtable_index(), sig); acorn@6080: } else { acorn@6080: tty->print("itable index %d for method: %s, flags: ", ime_num, sig); acorn@6080: } acorn@6080: if (m != NULL) { acorn@6080: m->access_flags().print_on(tty); acorn@6080: if (m->is_default_method()) { acorn@6080: tty->print("default "); acorn@6080: } acorn@6080: if (m->is_overpass()) { acorn@6080: tty->print("overpass"); acorn@6080: } acorn@6080: } acorn@6080: tty->cr(); acorn@6080: } drchase@5732: if (!m->has_vtable_index()) { drchase@5732: assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable"); drchase@5732: m->set_itable_index(ime_num); drchase@5732: // Progress to next itable entry drchase@5732: ime_num++; drchase@5732: } drchase@5732: } drchase@5732: } drchase@5732: assert(ime_num == method_count_for_interface(klass), "proper sizing"); drchase@5732: return ime_num; drchase@5732: } drchase@5732: drchase@5732: int klassItable::method_count_for_interface(Klass* interf) { drchase@5732: assert(interf->oop_is_instance(), "must be"); drchase@5732: assert(interf->is_interface(), "must be"); drchase@5732: Array* methods = InstanceKlass::cast(interf)->methods(); drchase@5732: int nof_methods = methods->length(); drchase@5732: while (nof_methods > 0) { drchase@5732: Method* m = methods->at(nof_methods-1); drchase@5732: if (m->has_itable_index()) { drchase@5732: int length = m->itable_index() + 1; drchase@5732: #ifdef ASSERT drchase@5732: while (nof_methods = 0) { drchase@5732: m = methods->at(--nof_methods); drchase@5732: assert(!m->has_itable_index() || m->itable_index() < length, ""); drchase@5732: } drchase@5732: #endif //ASSERT drchase@5732: return length; // return the rightmost itable index, plus one drchase@5732: } drchase@5732: nof_methods -= 1; drchase@5732: } acorn@5848: // no methods have itable indices drchase@5732: return 0; drchase@5732: } drchase@5732: drchase@5732: duke@435: void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) { coleenp@4037: Array* methods = InstanceKlass::cast(interf_h())->methods(); coleenp@4037: int nof_methods = methods->length(); duke@435: HandleMark hm; jcoomes@1844: assert(nof_methods > 0, "at least one method must exist for interface to be in vtable"); coleenp@4037: Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader()); duke@435: drchase@5732: int ime_count = method_count_for_interface(interf_h()); drchase@5732: for (int i = 0; i < nof_methods; i++) { coleenp@4037: Method* m = methods->at(i); drchase@5732: methodHandle target; drchase@5732: if (m->has_itable_index()) { hseigel@6195: // This search must match the runtime resolution, i.e. selection search for invokeinterface hseigel@6195: // to correctly enforce loader constraints for interface method inheritance drchase@5732: LinkResolver::lookup_instance_method_in_klasses(target, _klass, m->name(), m->signature(), CHECK); duke@435: } duke@435: if (target == NULL || !target->is_public() || target->is_abstract()) { drchase@6134: // Entry does not resolve. Leave it empty for AbstractMethodError. drchase@6134: if (!(target == NULL) && !target->is_public()) { drchase@6134: // Stuff an IllegalAccessError throwing method in there instead. drchase@6134: itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()]. drchase@6134: initialize(Universe::throw_illegal_access_error()); drchase@6134: } duke@435: } else { duke@435: // Entry did resolve, check loader constraints before initializing duke@435: // if checkconstraints requested duke@435: if (checkconstraints) { coleenp@4251: Handle method_holder_loader (THREAD, target->method_holder()->class_loader()); duke@435: if (method_holder_loader() != interface_loader()) { duke@435: ResourceMark rm(THREAD); acorn@4840: Symbol* failed_type_symbol = drchase@5732: SystemDictionary::check_signature_loaders(m->signature(), duke@435: method_holder_loader, duke@435: interface_loader, duke@435: true, CHECK); acorn@4840: if (failed_type_symbol != NULL) { duke@435: const char* msg = "loader constraint violation in interface " duke@435: "itable initialization: when resolving method \"%s\" the class" duke@435: " loader (instance of %s) of the current class, %s, " duke@435: "and the class loader (instance of %s) for interface " duke@435: "%s have different Class objects for the type %s " duke@435: "used in the signature"; drchase@5732: char* sig = target()->name_and_sig_as_C_string(); duke@435: const char* loader1 = SystemDictionary::loader_name(method_holder_loader()); drchase@5732: char* current = _klass->name()->as_C_string(); duke@435: const char* loader2 = SystemDictionary::loader_name(interface_loader()); coleenp@4037: char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string(); acorn@4840: char* failed_type_name = failed_type_symbol->as_C_string(); duke@435: size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + duke@435: strlen(current) + strlen(loader2) + strlen(iface) + duke@435: strlen(failed_type_name); duke@435: char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); duke@435: jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2, duke@435: iface, failed_type_name); duke@435: THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // ime may have moved during GC so recalculate address drchase@5732: int ime_num = m->itable_index(); drchase@5732: assert(ime_num < ime_count, "oob"); drchase@5732: itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target()); acorn@5848: if (TraceItables && Verbose) { acorn@5848: ResourceMark rm(THREAD); acorn@5848: if (target() != NULL) { acorn@5848: char* sig = target()->name_and_sig_as_C_string(); acorn@5848: tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ", acorn@5848: interf_h()->internal_name(), ime_num, sig, acorn@5848: target()->method_holder()->internal_name()); acorn@5848: tty->print("target_method flags: "); acorn@5848: target()->access_flags().print_on(tty); acorn@5848: if (target()->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } acorn@5848: tty->cr(); acorn@5848: } acorn@5848: } duke@435: } duke@435: } duke@435: } duke@435: coleenp@4037: // Update entry for specific Method* coleenp@4037: void klassItable::initialize_with_method(Method* m) { duke@435: itableMethodEntry* ime = method_entry(0); duke@435: for(int i = 0; i < _size_method_table; i++) { duke@435: if (ime->method() == m) { duke@435: ime->initialize(m); duke@435: } duke@435: ime++; duke@435: } duke@435: } duke@435: dcubed@4562: #if INCLUDE_JVMTI coleenp@4037: void klassItable::adjust_method_entries(Method** old_methods, Method** new_methods, duke@435: int methods_length, bool * trace_name_printed) { duke@435: // search the itable for uses of either obsolete or EMCP methods duke@435: for (int j = 0; j < methods_length; j++) { coleenp@4037: Method* old_method = old_methods[j]; coleenp@4037: Method* new_method = new_methods[j]; duke@435: itableMethodEntry* ime = method_entry(0); duke@435: dcubed@1045: // The itable can describe more than one interface and the same dcubed@1045: // method signature can be specified by more than one interface. dcubed@1045: // This means we have to do an exhaustive search to find all the dcubed@1045: // old_method references. duke@435: for (int i = 0; i < _size_method_table; i++) { duke@435: if (ime->method() == old_method) { duke@435: ime->initialize(new_method); duke@435: duke@435: if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) { duke@435: if (!(*trace_name_printed)) { duke@435: // RC_TRACE_MESG macro has an embedded ResourceMark duke@435: RC_TRACE_MESG(("adjust: name=%s", coleenp@4251: old_method->method_holder()->external_name())); duke@435: *trace_name_printed = true; duke@435: } duke@435: // RC_TRACE macro has an embedded ResourceMark duke@435: RC_TRACE(0x00200000, ("itable method update: %s(%s)", duke@435: new_method->name()->as_C_string(), duke@435: new_method->signature()->as_C_string())); duke@435: } dcubed@4562: // cannot 'break' here; see for-loop comment above. duke@435: } duke@435: ime++; duke@435: } duke@435: } duke@435: } duke@435: dcubed@4562: // an itable should never contain old or obsolete methods dcubed@4562: bool klassItable::check_no_old_or_obsolete_entries() { dcubed@4562: itableMethodEntry* ime = method_entry(0); dcubed@4562: for (int i = 0; i < _size_method_table; i++) { dcubed@4562: Method* m = ime->method(); dcubed@4562: if (m != NULL && dcubed@4562: (NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) { dcubed@4562: return false; dcubed@4562: } dcubed@4562: ime++; dcubed@4562: } dcubed@4562: return true; dcubed@4562: } dcubed@4562: dcubed@4562: void klassItable::dump_itable() { dcubed@4562: itableMethodEntry* ime = method_entry(0); dcubed@4562: tty->print_cr("itable dump --"); dcubed@4562: for (int i = 0; i < _size_method_table; i++) { dcubed@4562: Method* m = ime->method(); dcubed@4562: if (m != NULL) { dcubed@4562: tty->print(" (%5d) ", i); dcubed@4562: m->access_flags().print_on(tty); acorn@5848: if (m->is_default_method()) { acorn@6080: tty->print("default "); acorn@5848: } dcubed@4562: tty->print(" -- "); dcubed@4562: m->print_name(tty); dcubed@4562: tty->cr(); dcubed@4562: } dcubed@4562: ime++; dcubed@4562: } dcubed@4562: } dcubed@4562: #endif // INCLUDE_JVMTI dcubed@4562: duke@435: duke@435: // Setup duke@435: class InterfaceVisiterClosure : public StackObj { duke@435: public: coleenp@4037: virtual void doit(Klass* intf, int method_count) = 0; duke@435: }; duke@435: drchase@5732: // Visit all interfaces with at least one itable method coleenp@4037: void visit_all_interfaces(Array* transitive_intf, InterfaceVisiterClosure *blk) { duke@435: // Handle array argument duke@435: for(int i = 0; i < transitive_intf->length(); i++) { coleenp@4037: Klass* intf = transitive_intf->at(i); hseigel@4278: assert(intf->is_interface(), "sanity check"); duke@435: drchase@5732: // Find no. of itable methods drchase@5732: int method_count = 0; drchase@5732: // method_count = klassItable::method_count_for_interface(intf); drchase@5732: Array* methods = InstanceKlass::cast(intf)->methods(); drchase@5732: if (methods->length() > 0) { drchase@5732: for (int i = methods->length(); --i >= 0; ) { drchase@5732: if (interface_method_needs_itable_index(methods->at(i))) { drchase@5732: method_count++; drchase@5732: } duke@435: } duke@435: } duke@435: duke@435: // Only count interfaces with at least one method duke@435: if (method_count > 0) { duke@435: blk->doit(intf, method_count); duke@435: } duke@435: } duke@435: } duke@435: duke@435: class CountInterfacesClosure : public InterfaceVisiterClosure { duke@435: private: duke@435: int _nof_methods; duke@435: int _nof_interfaces; duke@435: public: duke@435: CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; } duke@435: duke@435: int nof_methods() const { return _nof_methods; } duke@435: int nof_interfaces() const { return _nof_interfaces; } duke@435: coleenp@4037: void doit(Klass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; } duke@435: }; duke@435: duke@435: class SetupItableClosure : public InterfaceVisiterClosure { duke@435: private: duke@435: itableOffsetEntry* _offset_entry; duke@435: itableMethodEntry* _method_entry; duke@435: address _klass_begin; duke@435: public: duke@435: SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) { duke@435: _klass_begin = klass_begin; duke@435: _offset_entry = offset_entry; duke@435: _method_entry = method_entry; duke@435: } duke@435: duke@435: itableMethodEntry* method_entry() const { return _method_entry; } duke@435: coleenp@4037: void doit(Klass* intf, int method_count) { duke@435: int offset = ((address)_method_entry) - _klass_begin; duke@435: _offset_entry->initialize(intf, offset); duke@435: _offset_entry++; duke@435: _method_entry += method_count; duke@435: } duke@435: }; duke@435: coleenp@4037: int klassItable::compute_itable_size(Array* transitive_interfaces) { duke@435: // Count no of interfaces and total number of interface methods duke@435: CountInterfacesClosure cic; coleenp@4037: visit_all_interfaces(transitive_interfaces, &cic); duke@435: dcubed@451: // There's alway an extra itable entry so we can null-terminate it. dcubed@451: int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods()); duke@435: duke@435: // Statistics duke@435: update_stats(itable_size * HeapWordSize); duke@435: duke@435: return itable_size; duke@435: } duke@435: duke@435: duke@435: // Fill out offset table and interface klasses into the itable space duke@435: void klassItable::setup_itable_offset_table(instanceKlassHandle klass) { duke@435: if (klass->itable_length() == 0) return; duke@435: assert(!klass->is_interface(), "Should have zero length itable"); duke@435: duke@435: // Count no of interfaces and total number of interface methods duke@435: CountInterfacesClosure cic; duke@435: visit_all_interfaces(klass->transitive_interfaces(), &cic); duke@435: int nof_methods = cic.nof_methods(); duke@435: int nof_interfaces = cic.nof_interfaces(); duke@435: dcubed@451: // Add one extra entry so we can null-terminate the table dcubed@451: nof_interfaces++; duke@435: coleenp@4037: assert(compute_itable_size(klass->transitive_interfaces()) == duke@435: calc_itable_size(nof_interfaces, nof_methods), duke@435: "mismatch calculation of itable size"); duke@435: duke@435: // Fill-out offset table duke@435: itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable(); duke@435: itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces); duke@435: intptr_t* end = klass->end_of_itable(); never@2658: assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)"); coleenp@548: assert((oop*)(end) == (oop*)(ime + nof_methods), "wrong offset calculation (2)"); duke@435: duke@435: // Visit all interfaces and initialize itable offset table coleenp@4037: SetupItableClosure sic((address)klass(), ioe, ime); duke@435: visit_all_interfaces(klass->transitive_interfaces(), &sic); duke@435: duke@435: #ifdef ASSERT duke@435: ime = sic.method_entry(); duke@435: oop* v = (oop*) klass->end_of_itable(); duke@435: assert( (oop*)(ime) == v, "wrong offset calculation (2)"); duke@435: #endif duke@435: } duke@435: duke@435: drchase@5732: // inverse to itable_index coleenp@4037: Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) { coleenp@4037: assert(InstanceKlass::cast(intf)->is_interface(), "sanity check"); drchase@5732: assert(intf->verify_itable_index(itable_index), ""); coleenp@4037: Array* methods = InstanceKlass::cast(intf)->methods(); jrose@1100: drchase@5732: if (itable_index < 0 || itable_index >= method_count_for_interface(intf)) acorn@5848: return NULL; // help caller defend against bad indices jrose@1100: drchase@5732: int index = itable_index; coleenp@4037: Method* m = methods->at(index); drchase@5732: int index2 = -1; drchase@5732: while (!m->has_itable_index() || drchase@5732: (index2 = m->itable_index()) != itable_index) { drchase@5732: assert(index2 < itable_index, "monotonic"); drchase@5732: if (++index == methods->length()) drchase@5732: return NULL; drchase@5732: m = methods->at(index); drchase@5732: } drchase@5732: assert(m->itable_index() == itable_index, "correct inverse"); jrose@1100: jrose@1100: return m; jrose@1100: } jrose@1100: duke@435: void klassVtable::verify(outputStream* st, bool forced) { duke@435: // make sure table is initialized duke@435: if (!Universe::is_fully_initialized()) return; duke@435: #ifndef PRODUCT duke@435: // avoid redundant verifies duke@435: if (!forced && _verify_count == Universe::verify_count()) return; duke@435: _verify_count = Universe::verify_count(); duke@435: #endif duke@435: oop* end_of_obj = (oop*)_klass() + _klass()->size(); duke@435: oop* end_of_vtable = (oop *)&table()[_length]; duke@435: if (end_of_vtable > end_of_obj) { jcoomes@1845: fatal(err_msg("klass %s: klass object too short (vtable extends beyond " jcoomes@1845: "end)", _klass->internal_name())); duke@435: } duke@435: duke@435: for (int i = 0; i < _length; i++) table()[i].verify(this, st); duke@435: // verify consistency with superKlass vtable coleenp@4037: Klass* super = _klass->super(); duke@435: if (super != NULL) { coleenp@4037: InstanceKlass* sk = InstanceKlass::cast(super); duke@435: klassVtable* vt = sk->vtable(); duke@435: for (int i = 0; i < vt->length(); i++) { duke@435: verify_against(st, vt, i); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) { duke@435: vtableEntry* vte = &vt->table()[index]; duke@435: if (vte->method()->name() != table()[index].method()->name() || duke@435: vte->method()->signature() != table()[index].method()->signature()) { duke@435: fatal("mismatched name/signature of vtable entries"); duke@435: } duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void klassVtable::print() { duke@435: ResourceMark rm; duke@435: tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length()); duke@435: for (int i = 0; i < length(); i++) { duke@435: table()[i].print(); duke@435: tty->cr(); duke@435: } duke@435: } duke@435: #endif duke@435: duke@435: void vtableEntry::verify(klassVtable* vt, outputStream* st) { duke@435: NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true)); duke@435: assert(method() != NULL, "must have set method"); duke@435: method()->verify(); duke@435: // we sub_type, because it could be a miranda method duke@435: if (!vt->klass()->is_subtype_of(method()->method_holder())) { duke@435: #ifndef PRODUCT duke@435: print(); duke@435: #endif jcoomes@1845: fatal(err_msg("vtableEntry " PTR_FORMAT ": method is from subclass", this)); duke@435: } duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: void vtableEntry::print() { duke@435: ResourceMark rm; duke@435: tty->print("vtableEntry %s: ", method()->name()->as_C_string()); duke@435: if (Verbose) { duke@435: tty->print("m %#lx ", (address)method()); duke@435: } duke@435: } duke@435: duke@435: class VtableStats : AllStatic { duke@435: public: duke@435: static int no_klasses; // # classes with vtables duke@435: static int no_array_klasses; // # array classes duke@435: static int no_instance_klasses; // # instanceKlasses duke@435: static int sum_of_vtable_len; // total # of vtable entries duke@435: static int sum_of_array_vtable_len; // total # of vtable entries in array klasses only duke@435: static int fixed; // total fixed overhead in bytes duke@435: static int filler; // overhead caused by filler bytes duke@435: static int entries; // total bytes consumed by vtable entries duke@435: static int array_entries; // total bytes consumed by array vtable entries duke@435: coleenp@4037: static void do_class(Klass* k) { coleenp@4037: Klass* kl = k; duke@435: klassVtable* vt = kl->vtable(); duke@435: if (vt == NULL) return; duke@435: no_klasses++; duke@435: if (kl->oop_is_instance()) { duke@435: no_instance_klasses++; duke@435: kl->array_klasses_do(do_class); duke@435: } duke@435: if (kl->oop_is_array()) { duke@435: no_array_klasses++; duke@435: sum_of_array_vtable_len += vt->length(); duke@435: } duke@435: sum_of_vtable_len += vt->length(); duke@435: } duke@435: duke@435: static void compute() { duke@435: SystemDictionary::classes_do(do_class); duke@435: fixed = no_klasses * oopSize; // vtable length duke@435: // filler size is a conservative approximation coleenp@4142: filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(InstanceKlass) - sizeof(ArrayKlass) - 1); duke@435: entries = sizeof(vtableEntry) * sum_of_vtable_len; duke@435: array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len; duke@435: } duke@435: }; duke@435: duke@435: int VtableStats::no_klasses = 0; duke@435: int VtableStats::no_array_klasses = 0; duke@435: int VtableStats::no_instance_klasses = 0; duke@435: int VtableStats::sum_of_vtable_len = 0; duke@435: int VtableStats::sum_of_array_vtable_len = 0; duke@435: int VtableStats::fixed = 0; duke@435: int VtableStats::filler = 0; duke@435: int VtableStats::entries = 0; duke@435: int VtableStats::array_entries = 0; duke@435: duke@435: void klassVtable::print_statistics() { duke@435: ResourceMark rm; duke@435: HandleMark hm; duke@435: VtableStats::compute(); duke@435: tty->print_cr("vtable statistics:"); duke@435: tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses); duke@435: int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries; duke@435: tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed); duke@435: tty->print_cr("%6d bytes filler overhead", VtableStats::filler); duke@435: tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries); duke@435: tty->print_cr("%6d bytes total", total); duke@435: } duke@435: duke@435: int klassItable::_total_classes; // Total no. of classes with itables duke@435: long klassItable::_total_size; // Total no. of bytes used for itables duke@435: duke@435: void klassItable::print_statistics() { duke@435: tty->print_cr("itable statistics:"); duke@435: tty->print_cr("%6d classes with itables", _total_classes); duke@435: tty->print_cr("%6d K uses for itables (average by class: %d bytes)", _total_size / K, _total_size / _total_classes); duke@435: } duke@435: duke@435: #endif // PRODUCT