Wed, 13 Nov 2013 07:31:26 -0800
8027229: ICCE expected for >=2 maximally specific default methods.
Summary: Need to process defaults for interfaces for invokespecial
Reviewed-by: lfoltan, hseigel, coleenp, jrose
1.1 --- a/src/share/vm/classfile/classFileParser.cpp Fri Nov 08 23:49:20 2013 +0000 1.2 +++ b/src/share/vm/classfile/classFileParser.cpp Wed Nov 13 07:31:26 2013 -0800 1.3 @@ -4080,7 +4080,7 @@ 1.4 1.5 // Generate any default methods - default methods are interface methods 1.6 // that have a default implementation. This is new with Lambda project. 1.7 - if (has_default_methods && !access_flags.is_interface() ) { 1.8 + if (has_default_methods ) { 1.9 DefaultMethods::generate_default_methods( 1.10 this_klass(), &all_mirandas, CHECK_(nullHandle)); 1.11 }
2.1 --- a/src/share/vm/classfile/defaultMethods.cpp Fri Nov 08 23:49:20 2013 +0000 2.2 +++ b/src/share/vm/classfile/defaultMethods.cpp Wed Nov 13 07:31:26 2013 -0800 2.3 @@ -171,8 +171,12 @@ 2.4 } 2.5 bool is_cancelled() const { return _cancelled; } 2.6 2.7 + // This code used to skip interface classes because their only 2.8 + // superclass was j.l.Object which would be also covered by class 2.9 + // superclass hierarchy walks. Now that the starting point can be 2.10 + // an interface, we must ensure we catch j.l.Object as the super. 2.11 static bool has_super(InstanceKlass* cls) { 2.12 - return cls->super() != NULL && !cls->is_interface(); 2.13 + return cls->super() != NULL; 2.14 } 2.15 2.16 Node* node_at_depth(int i) const { 2.17 @@ -391,16 +395,21 @@ 2.18 return; 2.19 } 2.20 2.21 + // Qualified methods are maximally-specific methods 2.22 + // These include public, instance concrete (=default) and abstract methods 2.23 GrowableArray<Method*> qualified_methods; 2.24 int num_defaults = 0; 2.25 int default_index = -1; 2.26 + int qualified_index = -1; 2.27 for (int i = 0; i < _members.length(); ++i) { 2.28 Pair<Method*,QualifiedState> entry = _members.at(i); 2.29 if (entry.second == QUALIFIED) { 2.30 qualified_methods.append(entry.first); 2.31 - default_index++; 2.32 + qualified_index++; 2.33 if (entry.first->is_default_method()) { 2.34 num_defaults++; 2.35 + default_index = qualified_index; 2.36 + 2.37 } 2.38 } 2.39 } 2.40 @@ -408,16 +417,10 @@ 2.41 if (qualified_methods.length() == 0) { 2.42 _exception_message = generate_no_defaults_message(CHECK); 2.43 _exception_name = vmSymbols::java_lang_AbstractMethodError(); 2.44 - } else if (qualified_methods.length() == 1) { 2.45 - // leave abstract methods alone, they will be found via normal search path 2.46 - Method* method = qualified_methods.at(0); 2.47 - if (!method->is_abstract()) { 2.48 - _selected_target = qualified_methods.at(0); 2.49 - } 2.50 - // If only one qualified method is default, select that 2.51 + // If only one qualified method is default, select that 2.52 } else if (num_defaults == 1) { 2.53 _selected_target = qualified_methods.at(default_index); 2.54 - } else { 2.55 + } else if (num_defaults > 1) { 2.56 _exception_message = generate_conflicts_message(&qualified_methods,CHECK); 2.57 _exception_name = vmSymbols::java_lang_IncompatibleClassChangeError(); 2.58 if (TraceDefaultMethods) { 2.59 @@ -425,6 +428,7 @@ 2.60 tty->print_cr(""); 2.61 } 2.62 } 2.63 + // leave abstract methods alone, they will be found via normal search path 2.64 } 2.65 2.66 bool contains_signature(Symbol* query) { 2.67 @@ -704,8 +708,10 @@ 2.68 Method* m = iklass->find_method(_method_name, _method_signature); 2.69 // private interface methods are not candidates for default methods 2.70 // invokespecial to private interface methods doesn't use default method logic 2.71 + // The overpasses are your supertypes' errors, we do not include them 2.72 // future: take access controls into account for superclass methods 2.73 - if (m != NULL && !m->is_static() && (!iklass->is_interface() || m->is_public())) { 2.74 + if (m != NULL && !m->is_static() && !m->is_overpass() && 2.75 + (!iklass->is_interface() || m->is_public())) { 2.76 if (_family == NULL) { 2.77 _family = new StatefulMethodFamily(); 2.78 } 2.79 @@ -781,7 +787,8 @@ 2.80 #ifndef PRODUCT 2.81 if (TraceDefaultMethods) { 2.82 ResourceMark rm; // be careful with these! 2.83 - tty->print_cr("Class %s requires default method processing", 2.84 + tty->print_cr("%s %s requires default method processing", 2.85 + klass->is_interface() ? "Interface" : "Class", 2.86 klass->name()->as_klass_external_name()); 2.87 PrintHierarchy printer; 2.88 printer.run(klass); 2.89 @@ -806,7 +813,7 @@ 2.90 } 2.91 #ifndef PRODUCT 2.92 if (TraceDefaultMethods) { 2.93 - tty->print_cr("Creating overpasses..."); 2.94 + tty->print_cr("Creating defaults and overpasses..."); 2.95 } 2.96 #endif // ndef PRODUCT 2.97 2.98 @@ -1076,7 +1083,9 @@ 2.99 klass->set_initial_method_idnum(new_size); 2.100 2.101 ClassLoaderData* cld = klass->class_loader_data(); 2.102 - MetadataFactory::free_array(cld, original_methods); 2.103 + if (original_methods ->length() > 0) { 2.104 + MetadataFactory::free_array(cld, original_methods); 2.105 + } 2.106 if (original_ordering->length() > 0) { 2.107 klass->set_method_ordering(merged_ordering); 2.108 MetadataFactory::free_array(cld, original_ordering);
3.1 --- a/src/share/vm/interpreter/linkResolver.cpp Fri Nov 08 23:49:20 2013 +0000 3.2 +++ b/src/share/vm/interpreter/linkResolver.cpp Wed Nov 13 07:31:26 2013 -0800 3.3 @@ -152,11 +152,13 @@ 3.4 // Could be an Object method inherited into an interface, but still a vtable call. 3.5 kind = CallInfo::vtable_call; 3.6 } else if (!resolved_klass->is_interface()) { 3.7 - // A miranda method. Compute the vtable index. 3.8 + // A default or miranda method. Compute the vtable index. 3.9 ResourceMark rm; 3.10 klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable(); 3.11 - index = vt->index_of_miranda(resolved_method->name(), 3.12 - resolved_method->signature()); 3.13 + index = LinkResolver::vtable_index_of_interface_method(resolved_klass, 3.14 + resolved_method); 3.15 + assert(index >= 0 , "we should have valid vtable index at this point"); 3.16 + 3.17 kind = CallInfo::vtable_call; 3.18 } else if (resolved_method->has_vtable_index()) { 3.19 // Can occur if an interface redeclares a method of Object. 3.20 @@ -279,7 +281,7 @@ 3.21 } 3.22 3.23 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass, 3.24 - methodHandle resolved_method, TRAPS) { 3.25 + methodHandle resolved_method) { 3.26 3.27 int vtable_index = Method::invalid_vtable_index; 3.28 Symbol* name = resolved_method->name(); 3.29 @@ -295,7 +297,7 @@ 3.30 } 3.31 if (vtable_index == Method::invalid_vtable_index) { 3.32 // get vtable_index for miranda methods 3.33 - ResourceMark rm(THREAD); 3.34 + ResourceMark rm; 3.35 klassVtable *vt = InstanceKlass::cast(klass())->vtable(); 3.36 vtable_index = vt->index_of_miranda(name, signature); 3.37 } 3.38 @@ -691,7 +693,7 @@ 3.39 ); 3.40 resolved_method->access_flags().print_on(tty); 3.41 if (resolved_method->is_default_method()) { 3.42 - tty->print("default"); 3.43 + tty->print("default "); 3.44 } 3.45 if (resolved_method->is_overpass()) { 3.46 tty->print("overpass"); 3.47 @@ -937,7 +939,7 @@ 3.48 ); 3.49 resolved_method->access_flags().print_on(tty); 3.50 if (resolved_method->is_default_method()) { 3.51 - tty->print("default"); 3.52 + tty->print("default "); 3.53 } 3.54 if (resolved_method->is_overpass()) { 3.55 tty->print("overpass"); 3.56 @@ -1017,7 +1019,7 @@ 3.57 ); 3.58 sel_method->access_flags().print_on(tty); 3.59 if (sel_method->is_default_method()) { 3.60 - tty->print("default"); 3.61 + tty->print("default "); 3.62 } 3.63 if (sel_method->is_overpass()) { 3.64 tty->print("overpass"); 3.65 @@ -1081,7 +1083,7 @@ 3.66 ); 3.67 resolved_method->access_flags().print_on(tty); 3.68 if (resolved_method->is_default_method()) { 3.69 - tty->print("default"); 3.70 + tty->print("default "); 3.71 } 3.72 if (resolved_method->is_overpass()) { 3.73 tty->print("overpass"); 3.74 @@ -1118,7 +1120,7 @@ 3.75 // do lookup based on receiver klass using the vtable index 3.76 if (resolved_method->method_holder()->is_interface()) { // miranda method 3.77 vtable_index = vtable_index_of_interface_method(resolved_klass, 3.78 - resolved_method, CHECK); 3.79 + resolved_method); 3.80 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); 3.81 3.82 InstanceKlass* inst = InstanceKlass::cast(recv_klass()); 3.83 @@ -1175,7 +1177,7 @@ 3.84 ); 3.85 selected_method->access_flags().print_on(tty); 3.86 if (selected_method->is_default_method()) { 3.87 - tty->print("default"); 3.88 + tty->print("default "); 3.89 } 3.90 if (selected_method->is_overpass()) { 3.91 tty->print("overpass"); 3.92 @@ -1268,14 +1270,6 @@ 3.93 sel_method->name(), 3.94 sel_method->signature())); 3.95 } 3.96 - // setup result 3.97 - if (!resolved_method->has_itable_index()) { 3.98 - int vtable_index = resolved_method->vtable_index(); 3.99 - assert(vtable_index == sel_method->vtable_index(), "sanity check"); 3.100 - result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK); 3.101 - return; 3.102 - } 3.103 - int itable_index = resolved_method()->itable_index(); 3.104 3.105 if (TraceItables && Verbose) { 3.106 ResourceMark rm(THREAD); 3.107 @@ -1289,14 +1283,22 @@ 3.108 ); 3.109 sel_method->access_flags().print_on(tty); 3.110 if (sel_method->is_default_method()) { 3.111 - tty->print("default"); 3.112 + tty->print("default "); 3.113 } 3.114 if (sel_method->is_overpass()) { 3.115 tty->print("overpass"); 3.116 } 3.117 tty->cr(); 3.118 } 3.119 - result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK); 3.120 + // setup result 3.121 + if (!resolved_method->has_itable_index()) { 3.122 + int vtable_index = resolved_method->vtable_index(); 3.123 + assert(vtable_index == sel_method->vtable_index(), "sanity check"); 3.124 + result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK); 3.125 + } else { 3.126 + int itable_index = resolved_method()->itable_index(); 3.127 + result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK); 3.128 + } 3.129 } 3.130 3.131
4.1 --- a/src/share/vm/interpreter/linkResolver.hpp Fri Nov 08 23:49:20 2013 +0000 4.2 +++ b/src/share/vm/interpreter/linkResolver.hpp Wed Nov 13 07:31:26 2013 -0800 4.3 @@ -130,7 +130,6 @@ 4.4 static void lookup_polymorphic_method (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, 4.5 KlassHandle current_klass, Handle *appendix_result_or_null, Handle *method_type_result, TRAPS); 4.6 4.7 - static int vtable_index_of_interface_method(KlassHandle klass, methodHandle resolved_method, TRAPS); 4.8 static void resolve_klass (KlassHandle& result, constantPoolHandle pool, int index, TRAPS); 4.9 4.10 static void resolve_pool (KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature, KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS); 4.11 @@ -186,6 +185,7 @@ 4.12 static methodHandle resolve_interface_call_or_null(KlassHandle receiver_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 4.13 static methodHandle resolve_static_call_or_null (KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 4.14 static methodHandle resolve_special_call_or_null (KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 4.15 + static int vtable_index_of_interface_method(KlassHandle klass, methodHandle resolved_method); 4.16 4.17 // same as above for compile-time resolution; returns vtable_index if current_klass if linked 4.18 static int resolve_virtual_vtable_index (KlassHandle receiver_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass);
5.1 --- a/src/share/vm/oops/klassVtable.cpp Fri Nov 08 23:49:20 2013 +0000 5.2 +++ b/src/share/vm/oops/klassVtable.cpp Wed Nov 13 07:31:26 2013 -0800 5.3 @@ -86,7 +86,11 @@ 5.4 get_mirandas(&new_mirandas, all_mirandas, super, methods, NULL, local_interfaces); 5.5 *num_new_mirandas = new_mirandas.length(); 5.6 5.7 - vtable_length += *num_new_mirandas * vtableEntry::size(); 5.8 + // Interfaces do not need interface methods in their vtables 5.9 + // This includes miranda methods and during later processing, default methods 5.10 + if (!class_flags.is_interface()) { 5.11 + vtable_length += *num_new_mirandas * vtableEntry::size(); 5.12 + } 5.13 5.14 if (Universe::is_bootstrapping() && vtable_length == 0) { 5.15 // array classes don't have their superclass set correctly during 5.16 @@ -224,7 +228,11 @@ 5.17 } 5.18 5.19 // add miranda methods; it will also return the updated initialized 5.20 - initialized = fill_in_mirandas(initialized); 5.21 + // Interfaces do not need interface methods in their vtables 5.22 + // This includes miranda methods and during later processing, default methods 5.23 + if (!ik()->is_interface()) { 5.24 + initialized = fill_in_mirandas(initialized); 5.25 + } 5.26 5.27 // In class hierarchies where the accessibility is not increasing (i.e., going from private -> 5.28 // package_private -> public/protected), the vtable might actually be smaller than our initial 5.29 @@ -264,12 +272,12 @@ 5.30 _klass->internal_name(), sig, vtable_index); 5.31 super_method->access_flags().print_on(tty); 5.32 if (super_method->is_default_method()) { 5.33 - tty->print("default"); 5.34 + tty->print("default "); 5.35 } 5.36 tty->print("overriders flags: "); 5.37 target_method->access_flags().print_on(tty); 5.38 if (target_method->is_default_method()) { 5.39 - tty->print("default"); 5.40 + tty->print("default "); 5.41 } 5.42 } 5.43 #endif /*PRODUCT*/ 5.44 @@ -332,9 +340,15 @@ 5.45 // An interface never allocates new vtable slots, only inherits old ones. 5.46 // This method will either be assigned its own itable index later, 5.47 // or be assigned an inherited vtable index in the loop below. 5.48 - // default methods store their vtable indices in the inheritors default_vtable_indices 5.49 - assert (default_index == -1, "interfaces don't store resolved default methods"); 5.50 - target_method()->set_vtable_index(Method::pending_itable_index); 5.51 + // default methods inherited by classes store their vtable indices 5.52 + // in the inheritor's default_vtable_indices 5.53 + // default methods inherited by interfaces may already have a 5.54 + // valid itable index, if so, don't change it 5.55 + // overpass methods in an interface will be assigned an itable index later 5.56 + // by an inheriting class 5.57 + if (!is_default || !target_method()->has_itable_index()) { 5.58 + target_method()->set_vtable_index(Method::pending_itable_index); 5.59 + } 5.60 } 5.61 5.62 // we need a new entry if there is no superclass 5.63 @@ -441,7 +455,7 @@ 5.64 target_klass->internal_name(), sig, i); 5.65 super_method->access_flags().print_on(tty); 5.66 if (super_method->is_default_method()) { 5.67 - tty->print("default"); 5.68 + tty->print("default "); 5.69 } 5.70 if (super_method->is_overpass()) { 5.71 tty->print("overpass"); 5.72 @@ -449,7 +463,7 @@ 5.73 tty->print("overriders flags: "); 5.74 target_method->access_flags().print_on(tty); 5.75 if (target_method->is_default_method()) { 5.76 - tty->print("default"); 5.77 + tty->print("default "); 5.78 } 5.79 if (target_method->is_overpass()) { 5.80 tty->print("overpass"); 5.81 @@ -468,7 +482,7 @@ 5.82 target_klass->internal_name(), sig,i); 5.83 super_method->access_flags().print_on(tty); 5.84 if (super_method->is_default_method()) { 5.85 - tty->print("default"); 5.86 + tty->print("default "); 5.87 } 5.88 if (super_method->is_overpass()) { 5.89 tty->print("overpass"); 5.90 @@ -476,7 +490,7 @@ 5.91 tty->print("overriders flags: "); 5.92 target_method->access_flags().print_on(tty); 5.93 if (target_method->is_default_method()) { 5.94 - tty->print("default"); 5.95 + tty->print("default "); 5.96 } 5.97 if (target_method->is_overpass()) { 5.98 tty->print("overpass"); 5.99 @@ -494,8 +508,18 @@ 5.100 #ifndef PRODUCT 5.101 if (PrintVtables && Verbose) { 5.102 ResourceMark rm; 5.103 - tty->print_cr("adding %s::%s at index %d", _klass->internal_name(), 5.104 - (m != NULL) ? m->name()->as_C_string() : "<NULL>", index); 5.105 + const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>"; 5.106 + tty->print("adding %s at index %d, flags: ", sig, index); 5.107 + if (m != NULL) { 5.108 + m->access_flags().print_on(tty); 5.109 + if (m->is_default_method()) { 5.110 + tty->print("default "); 5.111 + } 5.112 + if (m->is_overpass()) { 5.113 + tty->print("overpass"); 5.114 + } 5.115 + } 5.116 + tty->cr(); 5.117 } 5.118 #endif 5.119 table()[index].set(m); 5.120 @@ -631,8 +655,10 @@ 5.121 if (mhk->is_interface()) { 5.122 assert(m->is_public(), "should be public"); 5.123 assert(ik()->implements_interface(method_holder) , "this class should implement the interface"); 5.124 - assert(is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super()), "should be a miranda_method"); 5.125 - return true; 5.126 + // the search could find a miranda or a default method 5.127 + if (is_miranda(m, ik()->methods(), ik()->default_methods(), ik()->super())) { 5.128 + return true; 5.129 + } 5.130 } 5.131 return false; 5.132 } 5.133 @@ -644,9 +670,10 @@ 5.134 // the caller must make sure that the method belongs to an interface implemented by the class 5.135 // Miranda methods only include public interface instance methods 5.136 // Not private methods, not static methods, not default == concrete abstract 5.137 +// Miranda methods also do not include overpass methods in interfaces 5.138 bool klassVtable::is_miranda(Method* m, Array<Method*>* class_methods, 5.139 Array<Method*>* default_methods, Klass* super) { 5.140 - if (m->is_static() || m->is_private()) { 5.141 + if (m->is_static() || m->is_private() || m->is_overpass()) { 5.142 return false; 5.143 } 5.144 Symbol* name = m->name(); 5.145 @@ -744,6 +771,8 @@ 5.146 // Discover miranda methods ("miranda" = "interface abstract, no binding"), 5.147 // and append them into the vtable starting at index initialized, 5.148 // return the new value of initialized. 5.149 +// Miranda methods use vtable entries, but do not get assigned a vtable_index 5.150 +// The vtable_index is discovered by searching from the end of the vtable 5.151 int klassVtable::fill_in_mirandas(int initialized) { 5.152 GrowableArray<Method*> mirandas(20); 5.153 get_mirandas(&mirandas, NULL, ik()->super(), ik()->methods(), 5.154 @@ -758,7 +787,7 @@ 5.155 sig, initialized); 5.156 meth->access_flags().print_on(tty); 5.157 if (meth->is_default_method()) { 5.158 - tty->print("default"); 5.159 + tty->print("default "); 5.160 } 5.161 tty->cr(); 5.162 } 5.163 @@ -858,7 +887,7 @@ 5.164 tty->print(" (%5d) ", i); 5.165 m->access_flags().print_on(tty); 5.166 if (m->is_default_method()) { 5.167 - tty->print("default"); 5.168 + tty->print("default "); 5.169 } 5.170 if (m->is_overpass()) { 5.171 tty->print("overpass"); 5.172 @@ -977,6 +1006,25 @@ 5.173 if (interface_method_needs_itable_index(m)) { 5.174 assert(!m->is_final_method(), "no final interface methods"); 5.175 // If m is already assigned a vtable index, do not disturb it. 5.176 + if (TraceItables && Verbose) { 5.177 + ResourceMark rm; 5.178 + const char* sig = (m != NULL) ? m->name_and_sig_as_C_string() : "<NULL>"; 5.179 + if (m->has_vtable_index()) { 5.180 + tty->print("itable index %d for method: %s, flags: ", m->vtable_index(), sig); 5.181 + } else { 5.182 + tty->print("itable index %d for method: %s, flags: ", ime_num, sig); 5.183 + } 5.184 + if (m != NULL) { 5.185 + m->access_flags().print_on(tty); 5.186 + if (m->is_default_method()) { 5.187 + tty->print("default "); 5.188 + } 5.189 + if (m->is_overpass()) { 5.190 + tty->print("overpass"); 5.191 + } 5.192 + } 5.193 + tty->cr(); 5.194 + } 5.195 if (!m->has_vtable_index()) { 5.196 assert(m->vtable_index() == Method::pending_itable_index, "set by initialize_vtable"); 5.197 m->set_itable_index(ime_num); 5.198 @@ -1079,7 +1127,7 @@ 5.199 tty->print("target_method flags: "); 5.200 target()->access_flags().print_on(tty); 5.201 if (target()->is_default_method()) { 5.202 - tty->print("default"); 5.203 + tty->print("default "); 5.204 } 5.205 tty->cr(); 5.206 } 5.207 @@ -1158,7 +1206,7 @@ 5.208 tty->print(" (%5d) ", i); 5.209 m->access_flags().print_on(tty); 5.210 if (m->is_default_method()) { 5.211 - tty->print("default"); 5.212 + tty->print("default "); 5.213 } 5.214 tty->print(" -- "); 5.215 m->print_name(tty);