Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/oops/klassVtable.cpp
32285 views
/*1* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/systemDictionary.hpp"26#include "classfile/vmSymbols.hpp"27#include "gc_implementation/shared/markSweep.inline.hpp"28#include "memory/gcLocker.hpp"29#include "memory/metaspaceShared.hpp"30#include "memory/resourceArea.hpp"31#include "memory/universe.inline.hpp"32#include "oops/instanceKlass.hpp"33#include "oops/klassVtable.hpp"34#include "oops/method.hpp"35#include "oops/objArrayOop.hpp"36#include "oops/oop.inline.hpp"37#include "prims/jvmtiRedefineClassesTrace.hpp"38#include "runtime/arguments.hpp"39#include "runtime/handles.inline.hpp"40#include "utilities/copy.hpp"4142PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC4344inline InstanceKlass* klassVtable::ik() const {45Klass* k = _klass();46assert(k->oop_is_instance(), "not an InstanceKlass");47return (InstanceKlass*)k;48}4950bool klassVtable::is_preinitialized_vtable() {51return _klass->is_shared() && !MetaspaceShared::remapped_readwrite();52}535455// this function computes the vtable size (including the size needed for miranda56// methods) and the number of miranda methods in this class.57// Note on Miranda methods: Let's say there is a class C that implements58// interface I, and none of C's superclasses implements I.59// Let's say there is an abstract method m in I that neither C60// nor any of its super classes implement (i.e there is no method of any access,61// with the same name and signature as m), then m is a Miranda method which is62// entered as a public abstract method in C's vtable. From then on it should63// treated as any other public method in C for method over-ride purposes.64void klassVtable::compute_vtable_size_and_num_mirandas(65int* vtable_length_ret, int* num_new_mirandas,66GrowableArray<Method*>* all_mirandas, Klass* super,67Array<Method*>* methods, AccessFlags class_flags,68Handle classloader, Symbol* classname, Array<Klass*>* local_interfaces,69TRAPS) {70No_Safepoint_Verifier nsv;7172// set up default result values73int vtable_length = 0;7475// start off with super's vtable length76InstanceKlass* sk = (InstanceKlass*)super;77vtable_length = super == NULL ? 0 : sk->vtable_length();7879// go thru each method in the methods table to see if it needs a new entry80int len = methods->length();81for (int i = 0; i < len; i++) {82assert(methods->at(i)->is_method(), "must be a Method*");83methodHandle mh(THREAD, methods->at(i));8485if (needs_new_vtable_entry(mh, super, classloader, classname, class_flags, THREAD)) {86vtable_length += vtableEntry::size(); // we need a new entry87}88}8990GrowableArray<Method*> new_mirandas(20);91// compute the number of mirandas methods that must be added to the end92get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces);93*num_new_mirandas = new_mirandas.length();9495// Interfaces do not need interface methods in their vtables96// This includes miranda methods and during later processing, default methods97if (!class_flags.is_interface()) {98vtable_length += *num_new_mirandas * vtableEntry::size();99}100101if (Universe::is_bootstrapping() && vtable_length == 0) {102// array classes don't have their superclass set correctly during103// bootstrapping104vtable_length = Universe::base_vtable_size();105}106107if (super == NULL && !Universe::is_bootstrapping() &&108vtable_length != Universe::base_vtable_size()) {109// Someone is attempting to redefine java.lang.Object incorrectly. The110// only way this should happen is from111// SystemDictionary::resolve_from_stream(), which will detect this later112// and throw a security exception. So don't assert here to let113// the exception occur.114vtable_length = Universe::base_vtable_size();115}116assert(super != NULL || vtable_length == Universe::base_vtable_size(),117"bad vtable size for class Object");118assert(vtable_length % vtableEntry::size() == 0, "bad vtable length");119assert(vtable_length >= Universe::base_vtable_size(), "vtable too small");120121*vtable_length_ret = vtable_length;122}123124int klassVtable::index_of(Method* m, int len) const {125assert(m->has_vtable_index(), "do not ask this of non-vtable methods");126return m->vtable_index();127}128129// Copy super class's vtable to the first part (prefix) of this class's vtable,130// and return the number of entries copied. Expects that 'super' is the Java131// super class (arrays can have "array" super classes that must be skipped).132int klassVtable::initialize_from_super(KlassHandle super) {133if (super.is_null()) {134return 0;135} else if (is_preinitialized_vtable()) {136// A shared class' vtable is preinitialized at dump time. No need to copy137// methods from super class for shared class, as that was already done138// during archiving time. However, if Jvmti has redefined a class,139// copy super class's vtable in case the super class has changed.140return super->vtable()->length();141} else {142// copy methods from superKlass143// can't inherit from array class, so must be InstanceKlass144assert(super->oop_is_instance(), "must be instance klass");145InstanceKlass* sk = (InstanceKlass*)super();146klassVtable* superVtable = sk->vtable();147assert(superVtable->length() <= _length, "vtable too short");148#ifdef ASSERT149superVtable->verify(tty, true);150#endif151superVtable->copy_vtable_to(table());152#ifndef PRODUCT153if (PrintVtables && Verbose) {154ResourceMark rm;155tty->print_cr("copy vtable from %s to %s size %d", sk->internal_name(), klass()->internal_name(), _length);156}157#endif158return superVtable->length();159}160}161162//163// Revised lookup semantics introduced 1.3 (Kestrel beta)164void klassVtable::initialize_vtable(bool checkconstraints, TRAPS) {165166// Note: Arrays can have intermediate array supers. Use java_super to skip them.167KlassHandle super (THREAD, klass()->java_super());168int nofNewEntries = 0;169170bool is_shared = _klass->is_shared();171172if (PrintVtables && !klass()->oop_is_array()) {173ResourceMark rm(THREAD);174tty->print_cr("Initializing: %s", _klass->name()->as_C_string());175}176177#ifdef ASSERT178oop* end_of_obj = (oop*)_klass() + _klass()->size();179oop* end_of_vtable = (oop*)&table()[_length];180assert(end_of_vtable <= end_of_obj, "vtable extends beyond end");181#endif182183if (Universe::is_bootstrapping()) {184assert(!is_shared, "sanity");185// just clear everything186for (int i = 0; i < _length; i++) table()[i].clear();187return;188}189190int super_vtable_len = initialize_from_super(super);191if (klass()->oop_is_array()) {192assert(super_vtable_len == _length, "arrays shouldn't introduce new methods");193} else {194assert(_klass->oop_is_instance(), "must be InstanceKlass");195196Array<Method*>* methods = ik()->methods();197int len = methods->length();198int initialized = super_vtable_len;199200// Check each of this class's methods against super;201// if override, replace in copy of super vtable, otherwise append to end202for (int i = 0; i < len; i++) {203// update_inherited_vtable can stop for gc - ensure using handles204HandleMark hm(THREAD);205assert(methods->at(i)->is_method(), "must be a Method*");206methodHandle mh(THREAD, methods->at(i));207208bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, -1, checkconstraints, CHECK);209210if (needs_new_entry) {211put_method_at(mh(), initialized);212mh()->set_vtable_index(initialized); // set primary vtable index213initialized++;214}215}216217// update vtable with default_methods218Array<Method*>* default_methods = ik()->default_methods();219if (default_methods != NULL) {220len = default_methods->length();221if (len > 0) {222Array<int>* def_vtable_indices = NULL;223if ((def_vtable_indices = ik()->default_vtable_indices()) == NULL) {224assert(!is_shared, "shared class def_vtable_indices does not exist");225def_vtable_indices = ik()->create_new_default_vtable_indices(len, CHECK);226} else {227assert(def_vtable_indices->length() == len, "reinit vtable len?");228}229for (int i = 0; i < len; i++) {230HandleMark hm(THREAD);231assert(default_methods->at(i)->is_method(), "must be a Method*");232methodHandle mh(THREAD, default_methods->at(i));233234bool needs_new_entry = update_inherited_vtable(ik(), mh, super_vtable_len, i, checkconstraints, CHECK);235236// needs new entry237if (needs_new_entry) {238put_method_at(mh(), initialized);239if (is_preinitialized_vtable()) {240// At runtime initialize_vtable is rerun for a shared class241// (loaded by the non-boot loader) as part of link_class_impl().242// The dumptime vtable index should be the same as the runtime index.243assert(def_vtable_indices->at(i) == initialized,244"dump time vtable index is different from runtime index");245} else {246def_vtable_indices->at_put(i, initialized); //set vtable index247}248initialized++;249}250}251}252}253254// add miranda methods; it will also return the updated initialized255// Interfaces do not need interface methods in their vtables256// This includes miranda methods and during later processing, default methods257if (!ik()->is_interface()) {258initialized = fill_in_mirandas(initialized);259}260261// In class hierarchies where the accessibility is not increasing (i.e., going from private ->262// package_private -> public/protected), the vtable might actually be smaller than our initial263// calculation.264assert(initialized <= _length, "vtable initialization failed");265for(;initialized < _length; initialized++) {266put_method_at(NULL, initialized);267}268NOT_PRODUCT(verify(tty, true));269}270}271272// Called for cases where a method does not override its superclass' vtable entry273// For bytecodes not produced by javac together it is possible that a method does not override274// the superclass's method, but might indirectly override a super-super class's vtable entry275// If none found, return a null superk, else return the superk of the method this does override276// For public and protected methods: if they override a superclass, they will277// also be overridden themselves appropriately.278// Private methods do not override and are not overridden.279// Package Private methods are trickier:280// e.g. P1.A, pub m281// P2.B extends A, package private m282// P1.C extends B, public m283// P1.C.m needs to override P1.A.m and can not override P2.B.m284// Therefore: all package private methods need their own vtable entries for285// them to be the root of an inheritance overriding decision286// Package private methods may also override other vtable entries287InstanceKlass* klassVtable::find_transitive_override(InstanceKlass* initialsuper, methodHandle target_method,288int vtable_index, Handle target_loader, Symbol* target_classname, Thread * THREAD) {289InstanceKlass* superk = initialsuper;290while (superk != NULL && superk->super() != NULL) {291klassVtable* ssVtable = (superk->super())->vtable();292if (vtable_index < ssVtable->length()) {293Method* super_method = ssVtable->method_at(vtable_index);294// get the class holding the matching method295// make sure you use that class for is_override296InstanceKlass* supermethodholder = super_method->method_holder();297#ifndef PRODUCT298Symbol* name= target_method()->name();299Symbol* signature = target_method()->signature();300assert(super_method->name() == name && super_method->signature() == signature, "vtable entry name/sig mismatch");301#endif302303if (supermethodholder->is_override(super_method, target_loader, target_classname, THREAD)) {304#ifndef PRODUCT305if (PrintVtables && Verbose) {306ResourceMark rm(THREAD);307char* sig = target_method()->name_and_sig_as_C_string();308tty->print("transitive overriding superclass %s with %s::%s index %d, original flags: ",309supermethodholder->internal_name(),310_klass->internal_name(), sig, vtable_index);311super_method->access_flags().print_on(tty);312if (super_method->is_default_method()) {313tty->print("default ");314}315tty->print("overriders flags: ");316target_method->access_flags().print_on(tty);317if (target_method->is_default_method()) {318tty->print("default ");319}320}321#endif /*PRODUCT*/322break; // return found superk323}324} else {325// super class has no vtable entry here, stop transitive search326superk = (InstanceKlass*)NULL;327break;328}329// if no override found yet, continue to search up330superk = InstanceKlass::cast(superk->super());331}332333return superk;334}335336// Update child's copy of super vtable for overrides337// OR return true if a new vtable entry is required.338// Only called for InstanceKlass's, i.e. not for arrays339// If that changed, could not use _klass as handle for klass340bool klassVtable::update_inherited_vtable(InstanceKlass* klass, methodHandle target_method,341int super_vtable_len, int default_index,342bool checkconstraints, TRAPS) {343ResourceMark rm;344bool allocate_new = true;345assert(klass->oop_is_instance(), "must be InstanceKlass");346347Array<int>* def_vtable_indices = NULL;348bool is_default = false;349// default methods are concrete methods in superinterfaces which are added to the vtable350// with their real method_holder351// Since vtable and itable indices share the same storage, don't touch352// the default method's real vtable/itable index353// default_vtable_indices stores the vtable value relative to this inheritor354if (default_index >= 0 ) {355is_default = true;356def_vtable_indices = klass->default_vtable_indices();357assert(def_vtable_indices != NULL, "def vtable alloc?");358assert(default_index <= def_vtable_indices->length(), "def vtable len?");359} else {360assert(klass == target_method()->method_holder(), "caller resp.");361// Initialize the method's vtable index to "nonvirtual".362// If we allocate a vtable entry, we will update it to a non-negative number.363target_method()->set_vtable_index(Method::nonvirtual_vtable_index);364}365366// Static and <init> methods are never in367if (target_method()->is_static() || target_method()->name() == vmSymbols::object_initializer_name()) {368return false;369}370371if (target_method->is_final_method(klass->access_flags())) {372// a final method never needs a new entry; final methods can be statically373// resolved and they have to be present in the vtable only if they override374// a super's method, in which case they re-use its entry375allocate_new = false;376} else if (klass->is_interface()) {377allocate_new = false; // see note below in needs_new_vtable_entry378// An interface never allocates new vtable slots, only inherits old ones.379// This method will either be assigned its own itable index later,380// or be assigned an inherited vtable index in the loop below.381// default methods inherited by classes store their vtable indices382// in the inheritor's default_vtable_indices383// default methods inherited by interfaces may already have a384// valid itable index, if so, don't change it385// overpass methods in an interface will be assigned an itable index later386// by an inheriting class387if (!is_default || !target_method()->has_itable_index()) {388target_method()->set_vtable_index(Method::pending_itable_index);389}390}391392// we need a new entry if there is no superclass393Klass* super = klass->super();394if (super == NULL) {395return allocate_new;396}397398// private methods in classes always have a new entry in the vtable399// specification interpretation since classic has400// private methods not overriding401// JDK8 adds private methods in interfaces which require invokespecial402if (target_method()->is_private()) {403return allocate_new;404}405406// search through the vtable and update overridden entries407// Since check_signature_loaders acquires SystemDictionary_lock408// which can block for gc, once we are in this loop, use handles409// For classfiles built with >= jdk7, we now look for transitive overrides410411Symbol* name = target_method()->name();412Symbol* signature = target_method()->signature();413414KlassHandle target_klass(THREAD, target_method()->method_holder());415if (target_klass == NULL) {416target_klass = _klass;417}418419Handle target_loader(THREAD, target_klass->class_loader());420421Symbol* target_classname = target_klass->name();422for(int i = 0; i < super_vtable_len; i++) {423Method* super_method;424if (is_preinitialized_vtable()) {425// If this is a shared class, the vtable is already in the final state (fully426// initialized). Need to look at the super's vtable.427klassVtable* superVtable = super->vtable();428super_method = superVtable->method_at(i);429} else {430super_method = method_at(i);431}432// Check if method name matches433if (super_method->name() == name && super_method->signature() == signature) {434435// get super_klass for method_holder for the found method436InstanceKlass* super_klass = super_method->method_holder();437438// private methods are also never overridden439if (!super_method->is_private() &&440(is_default441|| ((super_klass->is_override(super_method, target_loader, target_classname, THREAD))442|| ((klass->major_version() >= VTABLE_TRANSITIVE_OVERRIDE_VERSION)443&& ((super_klass = find_transitive_override(super_klass,444target_method, i, target_loader,445target_classname, THREAD))446!= (InstanceKlass*)NULL)))))447{448// Package private methods always need a new entry to root their own449// overriding. They may also override other methods.450if (!target_method()->is_package_private()) {451allocate_new = false;452}453454if (checkconstraints) {455// Override vtable entry if passes loader constraint check456// if loader constraint checking requested457// No need to visit his super, since he and his super458// have already made any needed loader constraints.459// Since loader constraints are transitive, it is enough460// to link to the first super, and we get all the others.461Handle super_loader(THREAD, super_klass->class_loader());462463if (target_loader() != super_loader()) {464ResourceMark rm(THREAD);465Symbol* failed_type_symbol =466SystemDictionary::check_signature_loaders(signature, target_loader,467super_loader, true,468CHECK_(false));469if (failed_type_symbol != NULL) {470const char* msg = "loader constraint violation: when resolving "471"overridden method \"%s\" the class loader (instance"472" of %s) of the current class, %s, and its superclass loader "473"(instance of %s), have different Class objects for the type "474"%s used in the signature";475char* sig = target_method()->name_and_sig_as_C_string();476const char* loader1 = SystemDictionary::loader_name(target_loader());477char* current = target_klass->name()->as_C_string();478const char* loader2 = SystemDictionary::loader_name(super_loader());479char* failed_type_name = failed_type_symbol->as_C_string();480size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +481strlen(current) + strlen(loader2) + strlen(failed_type_name);482char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);483jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,484failed_type_name);485THROW_MSG_(vmSymbols::java_lang_LinkageError(), buf, false);486}487}488}489490put_method_at(target_method(), i);491if (!is_default) {492target_method()->set_vtable_index(i);493} else {494if (def_vtable_indices != NULL) {495if (is_preinitialized_vtable()) {496// At runtime initialize_vtable is rerun as part of link_class_impl()497// for a shared class loaded by the non-boot loader.498// The dumptime vtable index should be the same as the runtime index.499assert(def_vtable_indices->at(default_index) == i,500"dump time vtable index is different from runtime index");501} else {502def_vtable_indices->at_put(default_index, i);503}504}505assert(super_method->is_default_method() || super_method->is_overpass()506|| super_method->is_abstract(), "default override error");507}508509510#ifndef PRODUCT511if (PrintVtables && Verbose) {512ResourceMark rm(THREAD);513char* sig = target_method()->name_and_sig_as_C_string();514tty->print("overriding with %s::%s index %d, original flags: ",515target_klass->internal_name(), sig, i);516super_method->access_flags().print_on(tty);517if (super_method->is_default_method()) {518tty->print("default ");519}520if (super_method->is_overpass()) {521tty->print("overpass");522}523tty->print("overriders flags: ");524target_method->access_flags().print_on(tty);525if (target_method->is_default_method()) {526tty->print("default ");527}528if (target_method->is_overpass()) {529tty->print("overpass");530}531tty->cr();532}533#endif /*PRODUCT*/534} else {535// allocate_new = true; default. We might override one entry,536// but not override another. Once we override one, not need new537#ifndef PRODUCT538if (PrintVtables && Verbose) {539ResourceMark rm(THREAD);540char* sig = target_method()->name_and_sig_as_C_string();541tty->print("NOT overriding with %s::%s index %d, original flags: ",542target_klass->internal_name(), sig,i);543super_method->access_flags().print_on(tty);544if (super_method->is_default_method()) {545tty->print("default ");546}547if (super_method->is_overpass()) {548tty->print("overpass");549}550tty->print("overriders flags: ");551target_method->access_flags().print_on(tty);552if (target_method->is_default_method()) {553tty->print("default ");554}555if (target_method->is_overpass()) {556tty->print("overpass");557}558tty->cr();559}560#endif /*PRODUCT*/561}562}563}564return allocate_new;565}566567void klassVtable::put_method_at(Method* m, int index) {568if (is_preinitialized_vtable()) {569// At runtime initialize_vtable is rerun as part of link_class_impl()570// for shared class loaded by the non-boot loader to obtain the loader571// constraints based on the runtime classloaders' context. The dumptime572// method at the vtable index should be the same as the runtime method.573assert(table()[index].method() == m,574"archived method is different from the runtime method");575} else {576#ifndef PRODUCT577if (PrintVtables && Verbose) {578ResourceMark rm;579const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";580tty->print("adding %s at index %d, flags: ", sig, index);581if (m != NULL) {582m->access_flags().print_on(tty);583if (m->is_default_method()) {584tty->print("default ");585}586if (m->is_overpass()) {587tty->print("overpass");588}589}590tty->cr();591}592#endif593table()[index].set(m);594}595}596597// Find out if a method "m" with superclass "super", loader "classloader" and598// name "classname" needs a new vtable entry. Let P be a class package defined599// by "classloader" and "classname".600// NOTE: The logic used here is very similar to the one used for computing601// the vtables indices for a method. We cannot directly use that function because,602// we allocate the InstanceKlass at load time, and that requires that the603// superclass has been loaded.604// However, the vtable entries are filled in at link time, and therefore605// the superclass' vtable may not yet have been filled in.606bool klassVtable::needs_new_vtable_entry(methodHandle target_method,607Klass* super,608Handle classloader,609Symbol* classname,610AccessFlags class_flags,611TRAPS) {612if (class_flags.is_interface()) {613// Interfaces do not use vtables, except for java.lang.Object methods,614// so there is no point to assigning615// a vtable index to any of their local methods. If we refrain from doing this,616// we can use Method::_vtable_index to hold the itable index617return false;618}619620if (target_method->is_final_method(class_flags) ||621// a final method never needs a new entry; final methods can be statically622// resolved and they have to be present in the vtable only if they override623// a super's method, in which case they re-use its entry624(target_method()->is_static()) ||625// static methods don't need to be in vtable626(target_method()->name() == vmSymbols::object_initializer_name())627// <init> is never called dynamically-bound628) {629return false;630}631632// Concrete interface methods do not need new entries, they override633// abstract method entries using default inheritance rules634if (target_method()->method_holder() != NULL &&635target_method()->method_holder()->is_interface() &&636!target_method()->is_abstract() ) {637return false;638}639640// we need a new entry if there is no superclass641if (super == NULL) {642return true;643}644645// private methods in classes always have a new entry in the vtable646// specification interpretation since classic has647// private methods not overriding648// JDK8 adds private methods in interfaces which require invokespecial649if (target_method()->is_private()) {650return true;651}652653// Package private methods always need a new entry to root their own654// overriding. This allows transitive overriding to work.655if (target_method()->is_package_private()) {656return true;657}658659// search through the super class hierarchy to see if we need660// a new entry661ResourceMark rm(THREAD);662Symbol* name = target_method()->name();663Symbol* signature = target_method()->signature();664Klass* k = super;665Method* super_method = NULL;666InstanceKlass *holder = NULL;667Method* recheck_method = NULL;668bool found_pkg_prvt_method = false;669while (k != NULL) {670// lookup through the hierarchy for a method with matching name and sign.671super_method = InstanceKlass::cast(k)->lookup_method(name, signature);672if (super_method == NULL) {673break; // we still have to search for a matching miranda method674}675// get the class holding the matching method676// make sure you use that class for is_override677InstanceKlass* superk = super_method->method_holder();678// we want only instance method matches679// pretend private methods are not in the super vtable680// since we do override around them: e.g. a.m pub/b.m private/c.m pub,681// ignore private, c.m pub does override a.m pub682// For classes that were not javac'd together, we also do transitive overriding around683// methods that have less accessibility684if ((!super_method->is_static()) &&685(!super_method->is_private())) {686if (superk->is_override(super_method, classloader, classname, THREAD)) {687return false;688// else keep looking for transitive overrides689}690// If we get here then one of the super classes has a package private method691// that will not get overridden because it is in a different package. But,692// that package private method does "override" any matching methods in super693// interfaces, so there will be no miranda vtable entry created. So, set flag694// to TRUE for use below, in case there are no methods in super classes that695// this target method overrides.696assert(super_method->is_package_private(), "super_method must be package private");697assert(!superk->is_same_class_package(classloader(), classname),698"Must be different packages");699found_pkg_prvt_method = true;700}701702// Start with lookup result and continue to search up703k = superk->super(); // haven't found an override match yet; continue to look704}705706// If found_pkg_prvt_method is set, then the ONLY matching method in the707// superclasses is package private in another package. That matching method will708// prevent a miranda vtable entry from being created. Because the target method can not709// override the package private method in another package, then it needs to be the root710// for its own vtable entry.711if (found_pkg_prvt_method) {712return true;713}714715// if the target method is public or protected it may have a matching716// miranda method in the super, whose entry it should re-use.717// Actually, to handle cases that javac would not generate, we need718// this check for all access permissions.719InstanceKlass *sk = InstanceKlass::cast(super);720if (sk->has_miranda_methods()) {721if (sk->lookup_method_in_all_interfaces(name, signature, Klass::find_defaults) != NULL) {722return false; // found a matching miranda; we do not need a new entry723}724}725return true; // found no match; we need a new entry726}727728// Support for miranda methods729730// get the vtable index of a miranda method with matching "name" and "signature"731int klassVtable::index_of_miranda(Symbol* name, Symbol* signature) {732// search from the bottom, might be faster733for (int i = (length() - 1); i >= 0; i--) {734Method* m = table()[i].method();735if (is_miranda_entry_at(i) &&736m->name() == name && m->signature() == signature) {737return i;738}739}740return Method::invalid_vtable_index;741}742743// check if an entry at an index is miranda744// requires that method m at entry be declared ("held") by an interface.745bool klassVtable::is_miranda_entry_at(int i) {746Method* m = method_at(i);747Klass* method_holder = m->method_holder();748InstanceKlass *mhk = InstanceKlass::cast(method_holder);749750// miranda methods are public abstract instance interface methods in a class's vtable751if (mhk->is_interface()) {752assert(m->is_public(), "should be public");753assert(ik()->implements_interface(method_holder) , "this class should implement the interface");754if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) {755return true;756}757}758return false;759}760761// Check if a method is a miranda method, given a class's methods array,762// its default_method table and its super class.763// "Miranda" means an abstract non-private method that would not be764// overridden for the local class.765// A "miranda" method should only include non-private interface766// instance methods, i.e. not private methods, not static methods,767// not default methods (concrete interface methods), not overpass methods.768// If a given class already has a local (including overpass) method, a769// default method, or any of its superclasses has the same which would have770// overridden an abstract method, then this is not a miranda method.771//772// Miranda methods are checked multiple times.773// Pass 1: during class load/class file parsing: before vtable size calculation:774// include superinterface abstract and default methods (non-private instance).775// We include potential default methods to give them space in the vtable.776// During the first run, the current instanceKlass has not yet been777// created, the superclasses and superinterfaces do have instanceKlasses778// but may not have vtables, the default_methods list is empty, no overpasses.779// This is seen by default method creation.780//781// Pass 2: recalculated during vtable initialization: only include abstract methods.782// The goal of pass 2 is to walk through the superinterfaces to see if any of783// the superinterface methods (which were all abstract pre-default methods)784// need to be added to the vtable.785// With the addition of default methods, we have three new challenges:786// overpasses, static interface methods and private interface methods.787// Static and private interface methods do not get added to the vtable and788// are not seen by the method resolution process, so we skip those.789// Overpass methods are already in the vtable, so vtable lookup will790// find them and we don't need to add a miranda method to the end of791// the vtable. So we look for overpass methods and if they are found we792// return false. Note that we inherit our superclasses vtable, so793// the superclass' search also needs to use find_overpass so that if794// one is found we return false.795// False means - we don't need a miranda method added to the vtable.796//797// During the second run, default_methods is set up, so concrete methods from798// superinterfaces with matching names/signatures to default_methods are already799// in the default_methods list and do not need to be appended to the vtable800// as mirandas. Abstract methods may already have been handled via801// overpasses - either local or superclass overpasses, which may be802// in the vtable already.803//804// Pass 3: They are also checked by link resolution and selection,805// for invocation on a method (not interface method) reference that806// resolves to a method with an interface as its method_holder.807// Used as part of walking from the bottom of the vtable to find808// the vtable index for the miranda method.809//810// Part of the Miranda Rights in the US mean that if you do not have811// an attorney one will be appointed for you.812bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods,813Array<Method*>* default_methods, Klass* super) {814if (m->is_static() || m->is_private() || m->is_overpass()) {815return false;816}817Symbol* name = m->name();818Symbol* signature = m->signature();819820// First look in local methods to see if already covered821if (InstanceKlass::find_local_method(class_methods, name, signature,822Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL)823{824return false;825}826827// Check local default methods828if ((default_methods != NULL) &&829(InstanceKlass::find_method(default_methods, name, signature) != NULL))830{831return false;832}833834InstanceKlass* cursuper;835// Iterate on all superclasses, which should have instanceKlasses836// Note that we explicitly look for overpasses at each level.837// Overpasses may or may not exist for supers for pass 1,838// they should have been created for pass 2 and later.839840for (cursuper = InstanceKlass::cast(super); cursuper != NULL; cursuper = (InstanceKlass*)cursuper->super())841{842if (cursuper->find_local_method(name, signature,843Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL) {844return false;845}846}847848return true;849}850851// Scans current_interface_methods for miranda methods that do not852// already appear in new_mirandas, or default methods, and are also not defined-and-non-private853// in super (superclass). These mirandas are added to all_mirandas if it is854// not null; in addition, those that are not duplicates of miranda methods855// inherited by super from its interfaces are added to new_mirandas.856// Thus, new_mirandas will be the set of mirandas that this class introduces,857// all_mirandas will be the set of all mirandas applicable to this class858// including all defined in superclasses.859void klassVtable::add_new_mirandas_to_lists(860GrowableArray<Method*>* new_mirandas, GrowableArray<Method*>* all_mirandas,861Array<Method*>* current_interface_methods, Array<Method*>* class_methods,862Array<Method*>* default_methods, Klass* super) {863864// iterate thru the current interface's method to see if it a miranda865int num_methods = current_interface_methods->length();866for (int i = 0; i < num_methods; i++) {867Method* im = current_interface_methods->at(i);868bool is_duplicate = false;869int num_of_current_mirandas = new_mirandas->length();870// check for duplicate mirandas in different interfaces we implement871for (int j = 0; j < num_of_current_mirandas; j++) {872Method* miranda = new_mirandas->at(j);873if ((im->name() == miranda->name()) &&874(im->signature() == miranda->signature())) {875is_duplicate = true;876break;877}878}879880if (!is_duplicate) { // we don't want duplicate miranda entries in the vtable881if (is_miranda(im, class_methods, default_methods, super)) { // is it a miranda at all?882InstanceKlass *sk = InstanceKlass::cast(super);883// check if it is a duplicate of a super's miranda884if (sk->lookup_method_in_all_interfaces(im->name(), im->signature(), Klass::find_defaults) == NULL) {885new_mirandas->append(im);886}887if (all_mirandas != NULL) {888all_mirandas->append(im);889}890}891}892}893}894895void klassVtable::get_mirandas(GrowableArray<Method*>* new_mirandas,896GrowableArray<Method*>* all_mirandas,897Klass* super, Array<Method*>* class_methods,898Array<Method*>* default_methods,899Array<Klass*>* local_interfaces) {900assert((new_mirandas->length() == 0) , "current mirandas must be 0");901902// iterate thru the local interfaces looking for a miranda903int num_local_ifs = local_interfaces->length();904for (int i = 0; i < num_local_ifs; i++) {905InstanceKlass *ik = InstanceKlass::cast(local_interfaces->at(i));906add_new_mirandas_to_lists(new_mirandas, all_mirandas,907ik->methods(), class_methods,908default_methods, super);909// iterate thru each local's super interfaces910Array<Klass*>* super_ifs = ik->transitive_interfaces();911int num_super_ifs = super_ifs->length();912for (int j = 0; j < num_super_ifs; j++) {913InstanceKlass *sik = InstanceKlass::cast(super_ifs->at(j));914add_new_mirandas_to_lists(new_mirandas, all_mirandas,915sik->methods(), class_methods,916default_methods, super);917}918}919}920921// Discover miranda methods ("miranda" = "interface abstract, no binding"),922// and append them into the vtable starting at index initialized,923// return the new value of initialized.924// Miranda methods use vtable entries, but do not get assigned a vtable_index925// The vtable_index is discovered by searching from the end of the vtable926int klassVtable::fill_in_mirandas(int initialized) {927GrowableArray<Method*> mirandas(20);928get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(),929ik()->default_methods(), ik()->local_interfaces());930for (int i = 0; i < mirandas.length(); i++) {931if (PrintVtables && Verbose) {932Method* meth = mirandas.at(i);933ResourceMark rm(Thread::current());934if (meth != NULL) {935char* sig = meth->name_and_sig_as_C_string();936tty->print("fill in mirandas with %s index %d, flags: ",937sig, initialized);938meth->access_flags().print_on(tty);939if (meth->is_default_method()) {940tty->print("default ");941}942tty->cr();943}944}945put_method_at(mirandas.at(i), initialized);946++initialized;947}948return initialized;949}950951// Copy this class's vtable to the vtable beginning at start.952// Used to copy superclass vtable to prefix of subclass's vtable.953void klassVtable::copy_vtable_to(vtableEntry* start) {954Copy::disjoint_words((HeapWord*)table(), (HeapWord*)start, _length * vtableEntry::size());955}956957#if INCLUDE_JVMTI958bool klassVtable::adjust_default_method(int vtable_index, Method* old_method, Method* new_method) {959// If old_method is default, find this vtable index in default_vtable_indices960// and replace that method in the _default_methods list961bool updated = false;962963Array<Method*>* default_methods = ik()->default_methods();964if (default_methods != NULL) {965int len = default_methods->length();966for (int idx = 0; idx < len; idx++) {967if (vtable_index == ik()->default_vtable_indices()->at(idx)) {968if (default_methods->at(idx) == old_method) {969default_methods->at_put(idx, new_method);970updated = true;971}972break;973}974}975}976return updated;977}978979// search the vtable for uses of either obsolete or EMCP methods980void klassVtable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {981int prn_enabled = 0;982for (int index = 0; index < length(); index++) {983Method* old_method = unchecked_method_at(index);984if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {985continue; // skip uninteresting entries986}987assert(!old_method->is_deleted(), "vtable methods may not be deleted");988989Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());990991assert(new_method != NULL, "method_with_idnum() should not be NULL");992assert(old_method != new_method, "sanity check");993994put_method_at(new_method, index);995// For default methods, need to update the _default_methods array996// which can only have one method entry for a given signature997bool updated_default = false;998if (old_method->is_default_method()) {999updated_default = adjust_default_method(index, old_method, new_method);1000}10011002if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {1003if (!(*trace_name_printed)) {1004// RC_TRACE_MESG macro has an embedded ResourceMark1005RC_TRACE_MESG(("adjust: klassname=%s for methods from name=%s",1006klass()->external_name(),1007old_method->method_holder()->external_name()));1008*trace_name_printed = true;1009}1010// RC_TRACE macro has an embedded ResourceMark1011RC_TRACE(0x00100000, ("vtable method update: %s(%s), updated default = %s",1012new_method->name()->as_C_string(),1013new_method->signature()->as_C_string(),1014updated_default ? "true" : "false"));1015}1016}1017}10181019// a vtable should never contain old or obsolete methods1020bool klassVtable::check_no_old_or_obsolete_entries() {1021for (int i = 0; i < length(); i++) {1022Method* m = unchecked_method_at(i);1023if (m != NULL &&1024(NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {1025return false;1026}1027}1028return true;1029}10301031void klassVtable::dump_vtable() {1032tty->print_cr("vtable dump --");1033for (int i = 0; i < length(); i++) {1034Method* m = unchecked_method_at(i);1035if (m != NULL) {1036tty->print(" (%5d) ", i);1037m->access_flags().print_on(tty);1038if (m->is_default_method()) {1039tty->print("default ");1040}1041if (m->is_overpass()) {1042tty->print("overpass");1043}1044tty->print(" -- ");1045m->print_name(tty);1046tty->cr();1047}1048}1049}1050#endif // INCLUDE_JVMTI10511052// CDS/RedefineClasses support - clear vtables so they can be reinitialized1053void klassVtable::clear_vtable() {1054for (int i = 0; i < _length; i++) table()[i].clear();1055}10561057bool klassVtable::is_initialized() {1058return _length == 0 || table()[0].method() != NULL;1059}10601061//-----------------------------------------------------------------------------------------1062// Itable code10631064// Initialize a itableMethodEntry1065void itableMethodEntry::initialize(Method* m) {1066if (m == NULL) return;10671068if (MetaspaceShared::is_in_shared_space((void*)&_method) &&1069!MetaspaceShared::remapped_readwrite()) {1070// At runtime initialize_itable is rerun as part of link_class_impl()1071// for a shared class loaded by the non-boot loader.1072// The dumptime itable method entry should be the same as the runtime entry.1073assert(_method == m, "sanity");1074} else {1075_method = m;1076}1077}10781079klassItable::klassItable(instanceKlassHandle klass) {1080_klass = klass;10811082if (klass->itable_length() > 0) {1083itableOffsetEntry* offset_entry = (itableOffsetEntry*)klass->start_of_itable();1084if (offset_entry != NULL && offset_entry->interface_klass() != NULL) { // Check that itable is initialized1085// First offset entry points to the first method_entry1086intptr_t* method_entry = (intptr_t *)(((address)klass()) + offset_entry->offset());1087intptr_t* end = klass->end_of_itable();10881089_table_offset = (intptr_t*)offset_entry - (intptr_t*)klass();1090_size_offset_table = (method_entry - ((intptr_t*)offset_entry)) / itableOffsetEntry::size();1091_size_method_table = (end - method_entry) / itableMethodEntry::size();1092assert(_table_offset >= 0 && _size_offset_table >= 0 && _size_method_table >= 0, "wrong computation");1093return;1094}1095}10961097// The length of the itable was either zero, or it has not yet been initialized.1098_table_offset = 0;1099_size_offset_table = 0;1100_size_method_table = 0;1101}11021103static int initialize_count = 0;11041105// Initialization1106void klassItable::initialize_itable(bool checkconstraints, TRAPS) {1107if (_klass->is_interface()) {1108// This needs to go after vtable indices are assigned but1109// before implementors need to know the number of itable indices.1110assign_itable_indices_for_interface(_klass());1111}11121113// Cannot be setup doing bootstrapping, interfaces don't have1114// itables, and klass with only ones entry have empty itables1115if (Universe::is_bootstrapping() ||1116_klass->is_interface() ||1117_klass->itable_length() == itableOffsetEntry::size()) return;11181119// There's alway an extra itable entry so we can null-terminate it.1120guarantee(size_offset_table() >= 1, "too small");1121int num_interfaces = size_offset_table() - 1;1122if (num_interfaces > 0) {1123if (TraceItables) tty->print_cr("%3d: Initializing itables for %s", ++initialize_count,1124_klass->name()->as_C_string());112511261127// Iterate through all interfaces1128int i;1129for(i = 0; i < num_interfaces; i++) {1130itableOffsetEntry* ioe = offset_entry(i);1131HandleMark hm(THREAD);1132KlassHandle interf_h (THREAD, ioe->interface_klass());1133assert(interf_h() != NULL && ioe->offset() != 0, "bad offset entry in itable");1134initialize_itable_for_interface(ioe->offset(), interf_h, checkconstraints, CHECK);1135}11361137}1138// Check that the last entry is empty1139itableOffsetEntry* ioe = offset_entry(size_offset_table() - 1);1140guarantee(ioe->interface_klass() == NULL && ioe->offset() == 0, "terminator entry missing");1141}114211431144inline bool interface_method_needs_itable_index(Method* m) {1145if (m->is_static()) return false; // e.g., Stream.empty1146if (m->is_initializer()) return false; // <init> or <clinit>1147// If an interface redeclares a method from java.lang.Object,1148// it should already have a vtable index, don't touch it.1149// e.g., CharSequence.toString (from initialize_vtable)1150// if (m->has_vtable_index()) return false; // NO!1151return true;1152}11531154int klassItable::assign_itable_indices_for_interface(Klass* klass) {1155// an interface does not have an itable, but its methods need to be numbered1156if (TraceItables) tty->print_cr("%3d: Initializing itable indices for interface %s", ++initialize_count,1157klass->name()->as_C_string());1158Array<Method*>* methods = InstanceKlass::cast(klass)->methods();1159int nof_methods = methods->length();1160int ime_num = 0;1161for (int i = 0; i < nof_methods; i++) {1162Method* m = methods->at(i);1163if (interface_method_needs_itable_index(m)) {1164assert(!m->is_final_method(), "no final interface methods");1165// If m is already assigned a vtable index, do not disturb it.1166if (TraceItables && Verbose) {1167ResourceMark rm;1168const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>";1169if (m->has_vtable_index()) {1170tty->print("vtable index %d for method: %s, flags: ", m->vtable_index(), sig);1171} else {1172tty->print("itable index %d for method: %s, flags: ", ime_num, sig);1173}1174if (m != NULL) {1175m->access_flags().print_on(tty);1176if (m->is_default_method()) {1177tty->print("default ");1178}1179if (m->is_overpass()) {1180tty->print("overpass");1181}1182}1183tty->cr();1184}1185if (!m->has_vtable_index()) {1186// A shared method could have an initialized itable_index that1187// is < 0.1188assert(m->vtable_index() == Method::pending_itable_index ||1189m->is_shared(),1190"set by initialize_vtable");1191m->set_itable_index(ime_num);1192// Progress to next itable entry1193ime_num++;1194}1195}1196}1197assert(ime_num == method_count_for_interface(klass), "proper sizing");1198return ime_num;1199}12001201int klassItable::method_count_for_interface(Klass* interf) {1202assert(interf->oop_is_instance(), "must be");1203assert(interf->is_interface(), "must be");1204Array<Method*>* methods = InstanceKlass::cast(interf)->methods();1205int nof_methods = methods->length();1206int length = 0;1207while (nof_methods > 0) {1208Method* m = methods->at(nof_methods-1);1209if (m->has_itable_index()) {1210length = m->itable_index() + 1;1211break;1212}1213nof_methods -= 1;1214}1215#ifdef ASSERT1216int nof_methods_copy = nof_methods;1217while (nof_methods_copy > 0) {1218Method* mm = methods->at(--nof_methods_copy);1219assert(!mm->has_itable_index() || mm->itable_index() < length, "");1220}1221#endif //ASSERT1222// return the rightmost itable index, plus one; or 0 if no methods have1223// itable indices1224return length;1225}122612271228void klassItable::initialize_itable_for_interface(int method_table_offset, KlassHandle interf_h, bool checkconstraints, TRAPS) {1229Array<Method*>* methods = InstanceKlass::cast(interf_h())->methods();1230int nof_methods = methods->length();1231HandleMark hm;1232Handle interface_loader (THREAD, InstanceKlass::cast(interf_h())->class_loader());12331234int ime_count = method_count_for_interface(interf_h());1235for (int i = 0; i < nof_methods; i++) {1236Method* m = methods->at(i);1237methodHandle target;1238if (m->has_itable_index()) {1239// This search must match the runtime resolution, i.e. selection search for invokeinterface1240// to correctly enforce loader constraints for interface method inheritance1241LinkResolver::lookup_instance_method_in_klasses(target, _klass, m->name(), m->signature(), CHECK);1242}1243if (target == NULL || !target->is_public() || target->is_abstract()) {1244// Entry does not resolve. Leave it empty for AbstractMethodError.1245if (!(target == NULL) && !target->is_public()) {1246// Stuff an IllegalAccessError throwing method in there instead.1247itableOffsetEntry::method_entry(_klass(), method_table_offset)[m->itable_index()].1248initialize(Universe::throw_illegal_access_error());1249}1250} else {1251// Entry did resolve, check loader constraints before initializing1252// if checkconstraints requested1253if (checkconstraints) {1254Handle method_holder_loader (THREAD, target->method_holder()->class_loader());1255if (method_holder_loader() != interface_loader()) {1256ResourceMark rm(THREAD);1257Symbol* failed_type_symbol =1258SystemDictionary::check_signature_loaders(m->signature(),1259method_holder_loader,1260interface_loader,1261true, CHECK);1262if (failed_type_symbol != NULL) {1263const char* msg = "loader constraint violation in interface "1264"itable initialization: when resolving method \"%s\" the class"1265" loader (instance of %s) of the current class, %s, "1266"and the class loader (instance of %s) for interface "1267"%s have different Class objects for the type %s "1268"used in the signature";1269char* sig = target()->name_and_sig_as_C_string();1270const char* loader1 = SystemDictionary::loader_name(method_holder_loader());1271char* current = _klass->name()->as_C_string();1272const char* loader2 = SystemDictionary::loader_name(interface_loader());1273char* iface = InstanceKlass::cast(interf_h())->name()->as_C_string();1274char* failed_type_name = failed_type_symbol->as_C_string();1275size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +1276strlen(current) + strlen(loader2) + strlen(iface) +1277strlen(failed_type_name);1278char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);1279jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,1280iface, failed_type_name);1281THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);1282}1283}1284}12851286// ime may have moved during GC so recalculate address1287int ime_num = m->itable_index();1288assert(ime_num < ime_count, "oob");1289itableOffsetEntry::method_entry(_klass(), method_table_offset)[ime_num].initialize(target());1290if (TraceItables && Verbose) {1291ResourceMark rm(THREAD);1292if (target() != NULL) {1293char* sig = target()->name_and_sig_as_C_string();1294tty->print("interface: %s, ime_num: %d, target: %s, method_holder: %s ",1295interf_h()->internal_name(), ime_num, sig,1296target()->method_holder()->internal_name());1297tty->print("target_method flags: ");1298target()->access_flags().print_on(tty);1299if (target()->is_default_method()) {1300tty->print("default ");1301}1302tty->cr();1303}1304}1305}1306}1307}13081309// Update entry for specific Method*1310void klassItable::initialize_with_method(Method* m) {1311itableMethodEntry* ime = method_entry(0);1312for(int i = 0; i < _size_method_table; i++) {1313if (ime->method() == m) {1314ime->initialize(m);1315}1316ime++;1317}1318}13191320#if INCLUDE_JVMTI1321// search the itable for uses of either obsolete or EMCP methods1322void klassItable::adjust_method_entries(InstanceKlass* holder, bool * trace_name_printed) {13231324itableMethodEntry* ime = method_entry(0);1325for (int i = 0; i < _size_method_table; i++, ime++) {1326Method* old_method = ime->method();1327if (old_method == NULL || old_method->method_holder() != holder || !old_method->is_old()) {1328continue; // skip uninteresting entries1329}1330assert(!old_method->is_deleted(), "itable methods may not be deleted");13311332Method* new_method = holder->method_with_idnum(old_method->orig_method_idnum());13331334assert(new_method != NULL, "method_with_idnum() should not be NULL");1335assert(old_method != new_method, "sanity check");13361337ime->initialize(new_method);13381339if (RC_TRACE_IN_RANGE(0x00100000, 0x00400000)) {1340if (!(*trace_name_printed)) {1341// RC_TRACE_MESG macro has an embedded ResourceMark1342RC_TRACE_MESG(("adjust: name=%s",1343old_method->method_holder()->external_name()));1344*trace_name_printed = true;1345}1346// RC_TRACE macro has an embedded ResourceMark1347RC_TRACE(0x00200000, ("itable method update: %s(%s)",1348new_method->name()->as_C_string(),1349new_method->signature()->as_C_string()));1350}1351}1352}13531354// an itable should never contain old or obsolete methods1355bool klassItable::check_no_old_or_obsolete_entries() {1356itableMethodEntry* ime = method_entry(0);1357for (int i = 0; i < _size_method_table; i++) {1358Method* m = ime->method();1359if (m != NULL &&1360(NOT_PRODUCT(!m->is_valid() ||) m->is_old() || m->is_obsolete())) {1361return false;1362}1363ime++;1364}1365return true;1366}13671368void klassItable::dump_itable() {1369itableMethodEntry* ime = method_entry(0);1370tty->print_cr("itable dump --");1371for (int i = 0; i < _size_method_table; i++) {1372Method* m = ime->method();1373if (m != NULL) {1374tty->print(" (%5d) ", i);1375m->access_flags().print_on(tty);1376if (m->is_default_method()) {1377tty->print("default ");1378}1379tty->print(" -- ");1380m->print_name(tty);1381tty->cr();1382}1383ime++;1384}1385}1386#endif // INCLUDE_JVMTI13871388// Setup1389class InterfaceVisiterClosure : public StackObj {1390public:1391virtual void doit(Klass* intf, int method_count) = 0;1392};13931394// Visit all interfaces with at least one itable method1395void visit_all_interfaces(Array<Klass*>* transitive_intf, InterfaceVisiterClosure *blk) {1396// Handle array argument1397for(int i = 0; i < transitive_intf->length(); i++) {1398Klass* intf = transitive_intf->at(i);1399assert(intf->is_interface(), "sanity check");14001401// Find no. of itable methods1402int method_count = 0;1403// method_count = klassItable::method_count_for_interface(intf);1404Array<Method*>* methods = InstanceKlass::cast(intf)->methods();1405if (methods->length() > 0) {1406for (int i = methods->length(); --i >= 0; ) {1407if (interface_method_needs_itable_index(methods->at(i))) {1408method_count++;1409}1410}1411}14121413// Visit all interfaces which either have any methods or can participate in receiver type check.1414// We do not bother to count methods in transitive interfaces, although that would allow us to skip1415// this step in the rare case of a zero-method interface extending another zero-method interface.1416if (method_count > 0 || InstanceKlass::cast(intf)->transitive_interfaces()->length() > 0) {1417blk->doit(intf, method_count);1418}1419}1420}14211422class CountInterfacesClosure : public InterfaceVisiterClosure {1423private:1424int _nof_methods;1425int _nof_interfaces;1426public:1427CountInterfacesClosure() { _nof_methods = 0; _nof_interfaces = 0; }14281429int nof_methods() const { return _nof_methods; }1430int nof_interfaces() const { return _nof_interfaces; }14311432void doit(Klass* intf, int method_count) { _nof_methods += method_count; _nof_interfaces++; }1433};14341435class SetupItableClosure : public InterfaceVisiterClosure {1436private:1437itableOffsetEntry* _offset_entry;1438itableMethodEntry* _method_entry;1439address _klass_begin;1440public:1441SetupItableClosure(address klass_begin, itableOffsetEntry* offset_entry, itableMethodEntry* method_entry) {1442_klass_begin = klass_begin;1443_offset_entry = offset_entry;1444_method_entry = method_entry;1445}14461447itableMethodEntry* method_entry() const { return _method_entry; }14481449void doit(Klass* intf, int method_count) {1450int offset = ((address)_method_entry) - _klass_begin;1451_offset_entry->initialize(intf, offset);1452_offset_entry++;1453_method_entry += method_count;1454}1455};14561457int klassItable::compute_itable_size(Array<Klass*>* transitive_interfaces) {1458// Count no of interfaces and total number of interface methods1459CountInterfacesClosure cic;1460visit_all_interfaces(transitive_interfaces, &cic);14611462// There's alway an extra itable entry so we can null-terminate it.1463int itable_size = calc_itable_size(cic.nof_interfaces() + 1, cic.nof_methods());14641465// Statistics1466update_stats(itable_size * HeapWordSize);14671468return itable_size;1469}147014711472// Fill out offset table and interface klasses into the itable space1473void klassItable::setup_itable_offset_table(instanceKlassHandle klass) {1474if (klass->itable_length() == 0) return;1475assert(!klass->is_interface(), "Should have zero length itable");14761477// Count no of interfaces and total number of interface methods1478CountInterfacesClosure cic;1479visit_all_interfaces(klass->transitive_interfaces(), &cic);1480int nof_methods = cic.nof_methods();1481int nof_interfaces = cic.nof_interfaces();14821483// Add one extra entry so we can null-terminate the table1484nof_interfaces++;14851486assert(compute_itable_size(klass->transitive_interfaces()) ==1487calc_itable_size(nof_interfaces, nof_methods),1488"mismatch calculation of itable size");14891490// Fill-out offset table1491itableOffsetEntry* ioe = (itableOffsetEntry*)klass->start_of_itable();1492itableMethodEntry* ime = (itableMethodEntry*)(ioe + nof_interfaces);1493intptr_t* end = klass->end_of_itable();1494assert((oop*)(ime + nof_methods) <= (oop*)klass->start_of_nonstatic_oop_maps(), "wrong offset calculation (1)");1495assert((oop*)(end) == (oop*)(ime + nof_methods), "wrong offset calculation (2)");14961497// Visit all interfaces and initialize itable offset table1498SetupItableClosure sic((address)klass(), ioe, ime);1499visit_all_interfaces(klass->transitive_interfaces(), &sic);15001501#ifdef ASSERT1502ime = sic.method_entry();1503oop* v = (oop*) klass->end_of_itable();1504assert( (oop*)(ime) == v, "wrong offset calculation (2)");1505#endif1506}150715081509// inverse to itable_index1510Method* klassItable::method_for_itable_index(Klass* intf, int itable_index) {1511assert(InstanceKlass::cast(intf)->is_interface(), "sanity check");1512assert(intf->verify_itable_index(itable_index), "");1513Array<Method*>* methods = InstanceKlass::cast(intf)->methods();15141515if (itable_index < 0 || itable_index >= method_count_for_interface(intf))1516return NULL; // help caller defend against bad indices15171518int index = itable_index;1519Method* m = methods->at(index);1520int index2 = -1;1521while (!m->has_itable_index() ||1522(index2 = m->itable_index()) != itable_index) {1523assert(index2 < itable_index, "monotonic");1524if (++index == methods->length())1525return NULL;1526m = methods->at(index);1527}1528assert(m->itable_index() == itable_index, "correct inverse");15291530return m;1531}15321533void klassVtable::verify(outputStream* st, bool forced) {1534// make sure table is initialized1535if (!Universe::is_fully_initialized()) return;1536#ifndef PRODUCT1537// avoid redundant verifies1538if (!forced && _verify_count == Universe::verify_count()) return;1539_verify_count = Universe::verify_count();1540#endif1541oop* end_of_obj = (oop*)_klass() + _klass()->size();1542oop* end_of_vtable = (oop *)&table()[_length];1543if (end_of_vtable > end_of_obj) {1544fatal(err_msg("klass %s: klass object too short (vtable extends beyond "1545"end)", _klass->internal_name()));1546}15471548for (int i = 0; i < _length; i++) table()[i].verify(this, st);1549// verify consistency with superKlass vtable1550Klass* super = _klass->super();1551if (super != NULL) {1552InstanceKlass* sk = InstanceKlass::cast(super);1553klassVtable* vt = sk->vtable();1554for (int i = 0; i < vt->length(); i++) {1555verify_against(st, vt, i);1556}1557}1558}15591560void klassVtable::verify_against(outputStream* st, klassVtable* vt, int index) {1561vtableEntry* vte = &vt->table()[index];1562if (vte->method()->name() != table()[index].method()->name() ||1563vte->method()->signature() != table()[index].method()->signature()) {1564fatal("mismatched name/signature of vtable entries");1565}1566}15671568#ifndef PRODUCT1569void klassVtable::print() {1570ResourceMark rm;1571tty->print("klassVtable for klass %s (length %d):\n", _klass->internal_name(), length());1572for (int i = 0; i < length(); i++) {1573table()[i].print();1574tty->cr();1575}1576}1577#endif15781579void vtableEntry::verify(klassVtable* vt, outputStream* st) {1580NOT_PRODUCT(FlagSetting fs(IgnoreLockingAssertions, true));1581assert(method() != NULL, "must have set method");1582method()->verify();1583// we sub_type, because it could be a miranda method1584if (!vt->klass()->is_subtype_of(method()->method_holder())) {1585#ifndef PRODUCT1586print();1587#endif1588fatal(err_msg("vtableEntry " PTR_FORMAT ": method is from subclass", this));1589}1590}15911592#ifndef PRODUCT15931594void vtableEntry::print() {1595ResourceMark rm;1596tty->print("vtableEntry %s: ", method()->name()->as_C_string());1597if (Verbose) {1598tty->print("m %#lx ", (address)method());1599}1600}16011602class VtableStats : AllStatic {1603public:1604static int no_klasses; // # classes with vtables1605static int no_array_klasses; // # array classes1606static int no_instance_klasses; // # instanceKlasses1607static int sum_of_vtable_len; // total # of vtable entries1608static int sum_of_array_vtable_len; // total # of vtable entries in array klasses only1609static int fixed; // total fixed overhead in bytes1610static int filler; // overhead caused by filler bytes1611static int entries; // total bytes consumed by vtable entries1612static int array_entries; // total bytes consumed by array vtable entries16131614static void do_class(Klass* k) {1615Klass* kl = k;1616klassVtable* vt = kl->vtable();1617if (vt == NULL) return;1618no_klasses++;1619if (kl->oop_is_instance()) {1620no_instance_klasses++;1621kl->array_klasses_do(do_class);1622}1623if (kl->oop_is_array()) {1624no_array_klasses++;1625sum_of_array_vtable_len += vt->length();1626}1627sum_of_vtable_len += vt->length();1628}16291630static void compute() {1631SystemDictionary::classes_do(do_class);1632fixed = no_klasses * oopSize; // vtable length1633// filler size is a conservative approximation1634filler = oopSize * (no_klasses - no_instance_klasses) * (sizeof(InstanceKlass) - sizeof(ArrayKlass) - 1);1635entries = sizeof(vtableEntry) * sum_of_vtable_len;1636array_entries = sizeof(vtableEntry) * sum_of_array_vtable_len;1637}1638};16391640int VtableStats::no_klasses = 0;1641int VtableStats::no_array_klasses = 0;1642int VtableStats::no_instance_klasses = 0;1643int VtableStats::sum_of_vtable_len = 0;1644int VtableStats::sum_of_array_vtable_len = 0;1645int VtableStats::fixed = 0;1646int VtableStats::filler = 0;1647int VtableStats::entries = 0;1648int VtableStats::array_entries = 0;16491650void klassVtable::print_statistics() {1651ResourceMark rm;1652HandleMark hm;1653VtableStats::compute();1654tty->print_cr("vtable statistics:");1655tty->print_cr("%6d classes (%d instance, %d array)", VtableStats::no_klasses, VtableStats::no_instance_klasses, VtableStats::no_array_klasses);1656int total = VtableStats::fixed + VtableStats::filler + VtableStats::entries;1657tty->print_cr("%6d bytes fixed overhead (refs + vtable object header)", VtableStats::fixed);1658tty->print_cr("%6d bytes filler overhead", VtableStats::filler);1659tty->print_cr("%6d bytes for vtable entries (%d for arrays)", VtableStats::entries, VtableStats::array_entries);1660tty->print_cr("%6d bytes total", total);1661}16621663int klassItable::_total_classes; // Total no. of classes with itables1664long klassItable::_total_size; // Total no. of bytes used for itables16651666void klassItable::print_statistics() {1667tty->print_cr("itable statistics:");1668tty->print_cr("%6d classes with itables", _total_classes);1669tty->print_cr("%6d K uses for itables (average by class: %d bytes)", _total_size / K, _total_size / _total_classes);1670}16711672#endif // PRODUCT167316741675