--- old/src/share/vm/interpreter/linkResolver.cpp 2015-07-14 14:34:20.729000306 -0400 +++ new/src/share/vm/interpreter/linkResolver.cpp 2015-07-14 14:34:20.613000310 -0400 @@ -379,7 +379,8 @@ if (!resolved_method->is_abstract() && (InstanceKlass::cast(klass())->default_methods() != NULL)) { int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(), - name, signature, Klass::find_overpass, Klass::find_static); + name, signature, Klass::find_overpass, + Klass::find_static, Klass::find_private); if (index >= 0 ) { vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index); } @@ -1189,7 +1190,7 @@ assert(resolved_method->method_holder()->is_linked(), "must be linked"); // do lookup based on receiver klass using the vtable index - if (resolved_method->method_holder()->is_interface()) { // miranda method + if (resolved_method->method_holder()->is_interface()) { // default or miranda method vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method); assert(vtable_index >= 0 , "we should have valid vtable index at this point"); @@ -1198,7 +1199,7 @@ selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); } else { // at this point we are sure that resolved_method is virtual and not - // a miranda method; therefore, it must have a valid vtable index. + // a default or miranda method; therefore, it must have a valid vtable index. assert(!resolved_method->has_itable_index(), ""); vtable_index = resolved_method->vtable_index(); // We could get a negative vtable_index for final methods, --- old/src/share/vm/oops/instanceKlass.cpp 2015-07-14 14:34:21.189000290 -0400 +++ new/src/share/vm/oops/instanceKlass.cpp 2015-07-14 14:34:21.069000295 -0400 @@ -1381,12 +1381,14 @@ // find_method looks up the name/signature in the local methods array Method* InstanceKlass::find_method(Symbol* name, Symbol* signature) const { - return find_method_impl(name, signature, find_overpass, find_static); + return find_method_impl(name, signature, find_overpass, find_static, find_private); } Method* InstanceKlass::find_method_impl(Symbol* name, Symbol* signature, - OverpassLookupMode overpass_mode, StaticLookupMode static_mode) const { - return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode); + OverpassLookupMode overpass_mode, + StaticLookupMode static_mode, + PrivateLookupMode private_mode) const { + return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode); } // find_instance_method looks up the name/signature in the local methods array @@ -1394,7 +1396,7 @@ Method* InstanceKlass::find_instance_method( Array* methods, Symbol* name, Symbol* signature) { Method* meth = InstanceKlass::find_method_impl(methods, name, signature, - find_overpass, skip_static); + find_overpass, skip_static, find_private); assert(((meth == NULL) || !meth->is_static()), "find_instance_method should have skipped statics"); return meth; } @@ -1405,22 +1407,51 @@ return InstanceKlass::find_instance_method(methods(), name, signature); } +// Find looks up the name/signature in the local methods array +// and filters on the overpass, static and private flags +// This returns the first one found +// note that the local methods array can have up to one overpass, one static +// and one instance (private or not) with the same name/signature +Method* InstanceKlass::find_local_method(Symbol* name, Symbol* signature, + OverpassLookupMode overpass_mode, + StaticLookupMode static_mode, + PrivateLookupMode private_mode) const { + return InstanceKlass::find_method_impl(methods(), name, signature, overpass_mode, static_mode, private_mode); +} + +// Find looks up the name/signature in the local methods array +// and filters on the overpass, static and private flags +// This returns the first one found +// note that the local methods array can have up to one overpass, one static +// and one instance (private or not) with the same name/signature +Method* InstanceKlass::find_local_method(Array* methods, + Symbol* name, Symbol* signature, + OverpassLookupMode overpass_mode, + StaticLookupMode static_mode, + PrivateLookupMode private_mode) { + return InstanceKlass::find_method_impl(methods, name, signature, overpass_mode, static_mode, private_mode); +} + + // find_method looks up the name/signature in the local methods array Method* InstanceKlass::find_method( Array* methods, Symbol* name, Symbol* signature) { - return InstanceKlass::find_method_impl(methods, name, signature, find_overpass, find_static); + return InstanceKlass::find_method_impl(methods, name, signature, find_overpass, find_static, find_private); } Method* InstanceKlass::find_method_impl( - Array* methods, Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode, StaticLookupMode static_mode) { - int hit = find_method_index(methods, name, signature, overpass_mode, static_mode); + Array* methods, Symbol* name, Symbol* signature, + OverpassLookupMode overpass_mode, StaticLookupMode static_mode, + PrivateLookupMode private_mode) { + int hit = find_method_index(methods, name, signature, overpass_mode, static_mode, private_mode); return hit >= 0 ? methods->at(hit): NULL; } -bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static) { - return (m->signature() == signature) && +bool InstanceKlass::method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static, bool skipping_private) { + return ((m->signature() == signature) && (!skipping_overpass || !m->is_overpass()) && - (!skipping_static || !m->is_static()); + (!skipping_static || !m->is_static()) && + (!skipping_private || !m->is_private())); } // Used directly for default_methods to find the index into the @@ -1430,17 +1461,25 @@ // the search continues to find a potential non-overpass match. This capability // is important during method resolution to prefer a static method, for example, // over an overpass method. +// There is the possibility in any method to have the same name/signature +// for a static method, an overpass method and a local instance method +// To correctly catch a given method, the search criteria may need +// to explicitly skip the other two. For local instance methods, it +// is often necessary to skip private methods int InstanceKlass::find_method_index( - Array* methods, Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode, StaticLookupMode static_mode) { + Array* methods, Symbol* name, Symbol* signature, + OverpassLookupMode overpass_mode, StaticLookupMode static_mode, + PrivateLookupMode private_mode) { bool skipping_overpass = (overpass_mode == skip_overpass); bool skipping_static = (static_mode == skip_static); + bool skipping_private = (private_mode == skip_private); int hit = binary_search(methods, name); if (hit != -1) { Method* m = methods->at(hit); // Do linear search to find matching signature. First, quick check // for common case, ignoring overpasses if requested. - if (method_matches(m, signature, skipping_overpass, skipping_static)) return hit; + if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return hit; // search downwards through overloaded methods int i; @@ -1448,18 +1487,18 @@ Method* m = methods->at(i); assert(m->is_method(), "must be method"); if (m->name() != name) break; - if (method_matches(m, signature, skipping_overpass, skipping_static)) return i; + if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i; } // search upwards for (i = hit + 1; i < methods->length(); ++i) { Method* m = methods->at(i); assert(m->is_method(), "must be method"); if (m->name() != name) break; - if (method_matches(m, signature, skipping_overpass, skipping_static)) return i; + if (method_matches(m, signature, skipping_overpass, skipping_static, skipping_private)) return i; } // not found #ifdef ASSERT - int index = (skipping_overpass || skipping_static) ? -1 : linear_search(methods, name, signature); + int index = (skipping_overpass || skipping_static || skipping_private) ? -1 : linear_search(methods, name, signature); assert(index == -1, err_msg("binary search should have found entry %d", index)); #endif } @@ -1489,7 +1528,7 @@ OverpassLookupMode overpass_local_mode = overpass_mode; Klass* klass = const_cast(this); while (klass != NULL) { - Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, overpass_local_mode, find_static); + Method* method = InstanceKlass::cast(klass)->find_method_impl(name, signature, overpass_local_mode, find_static, find_private); if (method != NULL) { return method; } --- old/src/share/vm/oops/instanceKlass.hpp 2015-07-14 14:34:21.665000274 -0400 +++ new/src/share/vm/oops/instanceKlass.hpp 2015-07-14 14:34:21.549000278 -0400 @@ -503,12 +503,28 @@ Method* find_instance_method(Symbol* name, Symbol* signature); static Method* find_instance_method(Array* methods, Symbol* name, Symbol* signature); + // find a local method (returns NULL if not found) + Method* find_local_method(Symbol* name, Symbol* signature, + OverpassLookupMode overpass_mode, + StaticLookupMode static_mode, + PrivateLookupMode private_mode) const; + + // find a local method from given methods array (returns NULL if not found) + static Method* find_local_method(Array* methods, + Symbol* name, Symbol* signature, + OverpassLookupMode overpass_mode, + StaticLookupMode static_mode, + PrivateLookupMode private_mode); + // true if method matches signature and conforms to skipping_X conditions. - static bool method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static); + static bool method_matches(Method* m, Symbol* signature, bool skipping_overpass, bool skipping_static, bool skipping_private); - // find a local method index in default_methods (returns -1 if not found) - static int find_method_index(Array* methods, Symbol* name, Symbol* signature, - OverpassLookupMode overpass_mode, StaticLookupMode static_mode); + // find a local method index in methods or default_methods (returns -1 if not found) + static int find_method_index(Array* methods, + Symbol* name, Symbol* signature, + OverpassLookupMode overpass_mode, + StaticLookupMode static_mode, + PrivateLookupMode private_mode); // lookup operation (returns NULL if not found) Method* uncached_lookup_method(Symbol* name, Symbol* signature, OverpassLookupMode overpass_mode) const; @@ -1153,9 +1169,14 @@ // find a local method (returns NULL if not found) Method* find_method_impl(Symbol* name, Symbol* signature, - OverpassLookupMode overpass_mode, StaticLookupMode static_mode) const; - static Method* find_method_impl(Array* methods, Symbol* name, Symbol* signature, - OverpassLookupMode overpass_mode, StaticLookupMode static_mode); + OverpassLookupMode overpass_mode, + StaticLookupMode static_mode, + PrivateLookupMode private_mode) const; + static Method* find_method_impl(Array* methods, + Symbol* name, Symbol* signature, + OverpassLookupMode overpass_mode, + StaticLookupMode static_mode, + PrivateLookupMode private_mode); // Free CHeap allocated fields. void release_C_heap_structures(); --- old/src/share/vm/oops/klass.hpp 2015-07-14 14:34:22.093000259 -0400 +++ new/src/share/vm/oops/klass.hpp 2015-07-14 14:34:21.977000263 -0400 @@ -161,6 +161,7 @@ enum DefaultsLookupMode { find_defaults, skip_defaults }; enum OverpassLookupMode { find_overpass, skip_overpass }; enum StaticLookupMode { find_static, skip_static }; + enum PrivateLookupMode { find_private, skip_private }; bool is_klass() const volatile { return true; } --- old/src/share/vm/oops/klassVtable.cpp 2015-07-14 14:34:22.525000244 -0400 +++ new/src/share/vm/oops/klassVtable.cpp 2015-07-14 14:34:22.409000248 -0400 @@ -683,7 +683,6 @@ if (mhk->is_interface()) { assert(m->is_public(), "should be public"); assert(ik()->implements_interface(method_holder) , "this class should implement the interface"); - // the search could find a miranda or a default method if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) { return true; } @@ -691,25 +690,41 @@ return false; } -// check if a method is a miranda method, given a class's methods table, -// its default_method table and its super -// Miranda methods are calculated twice: -// first: before vtable size calculation: including abstract and superinterface default +// Check if a method is a miranda method, given a class's methods table, +// its default_method table and its super class. +// "Miranda" means an abstract non-private method that would not be +// overridden. A "miranda" method should only include non-private interface +// instance methods, not private methods, not static methods, +// not default methods (concrete interface methods), not overpass methods. +// If a given class already has a local (including overpass) method, a +// default method, or any of its superclasses has the same which would have +// overridden an abstract method, then this is not a miranda method. +// +// Miranda methods are checked multiple times. +// Pass 1: during class load/class file parsing: before vtable size calculation: +// include superinterface abstract and default methods (non-private instance). // We include potential default methods to give them space in the vtable. -// During the first run, the default_methods list is empty -// This is seen by default method creation -// Second: recalculated during vtable initialization: only include abstract methods. +// During the first run, the current instanceKlass has not yet been +// created, the superclasses and superinterfaces do have instanceKlasses +// but may not have vtables, the default_methods list is empty, no overpasses. +// This is seen by default method creation. +// +// Pass 2: recalculated during vtable initialization: only include abstract methods. // During the second run, default_methods is set up, so concrete methods from // superinterfaces with matching names/signatures to default_methods are already // in the default_methods list and do not need to be appended to the vtable -// as mirandas -// This is seen by link resolution and selection. -// "miranda" means not static, not defined by this class. -// private methods in interfaces do not belong in the miranda list. -// the caller must make sure that the method belongs to an interface implemented by the class -// Miranda methods only include public interface instance methods -// Not private methods, not static methods, not default == concrete abstract -// Miranda methods also do not include overpass methods in interfaces +// as mirandas. Abstract methods may already have been handled via +// overpasses - either local or superclass overpasses, which may be +// in the vtable already. +// +// Pass 3: They are also checked by link resolution and selection, +// for invocation on a method (not interface method) reference that +// resolves to a method with an interface as its method_holder. +// Used as part of walking from the bottom of the vtable to find +// the vtable index for the miranda method. +// +// Part of the Miranda Rights in the US mean that if you do not have +// an attorney one will be appointed to you. bool klassVtable::is_miranda(Method* m, Array* class_methods, Array* default_methods, Klass* super) { if (m->is_static() || m->is_private() || m->is_overpass()) { @@ -717,44 +732,36 @@ } Symbol* name = m->name(); Symbol* signature = m->signature(); - Method* mo; - if ((mo = InstanceKlass::find_instance_method(class_methods, name, signature)) == NULL) { - // did not find it in the method table of the current class - if ((default_methods == NULL) || - InstanceKlass::find_method(default_methods, name, signature) == NULL) { - if (super == NULL) { - // super doesn't exist - return true; - } - - mo = InstanceKlass::cast(super)->lookup_method(name, signature); - while (mo != NULL && mo->access_flags().is_static() - && mo->method_holder() != NULL - && mo->method_holder()->super() != NULL) - { - mo = mo->method_holder()->super()->uncached_lookup_method(name, signature, Klass::find_overpass); - } - if (mo == NULL || mo->access_flags().is_private() ) { - // super class hierarchy does not implement it or protection is different - return true; - } - } - } else { - // if the local class has a private method, the miranda will not - // override it, so a vtable slot is needed - if (mo->access_flags().is_private()) { - - // Second round, weed out any superinterface methods that turned - // into default methods, i.e. were concrete not abstract in the end - if ((default_methods == NULL) || - InstanceKlass::find_method(default_methods, name, signature) == NULL) { - return true; - } - } + // First look in local methods to see if already covered + if (InstanceKlass::find_local_method(class_methods, name, signature, + Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL) + { + return false; } - return false; + // Check local default methods + if ((default_methods != NULL) && + (InstanceKlass::find_method(default_methods, name, signature) != NULL)) + { + return false; + } + + InstanceKlass* cursuper; + // Iterate on all superclasses, which should have instanceKlasses + // Note that we explicitly look for overpasses at each level. + // Overpasses may or may not exist supers for pass 1, + // they should have been created for pass 2 and later. + + for (cursuper = InstanceKlass::cast(super); cursuper != NULL; cursuper = (InstanceKlass*)cursuper->super()) + { + if (cursuper->find_local_method(name, signature, + Klass::find_overpass, Klass::skip_static, Klass::skip_private) != NULL) { + return false; + } + } + + return true; } // Scans current_interface_methods for miranda methods that do not