< prev index next >

src/hotspot/share/interpreter/linkResolver.cpp

Print this page




 247 
 248   // Coming from the constant pool always checks access
 249   _check_access  = true;
 250 }
 251 
 252 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 253    // resolve klass
 254   _resolved_klass = pool->klass_ref_at(index, CHECK);
 255 
 256   // Get name, signature, and static klass
 257   _name          = pool->name_ref_at(index);
 258   _signature     = pool->signature_ref_at(index);
 259   _tag           = pool->tag_ref_at(index);
 260   _current_klass = pool->pool_holder();
 261   _current_method = methodHandle();
 262 
 263   // Coming from the constant pool always checks access
 264   _check_access  = true;
 265 }
 266 




 267 #ifndef PRODUCT
 268 void LinkInfo::print() {
 269   ResourceMark rm;
 270   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
 271                 _resolved_klass->name()->as_C_string(),
 272                 _name->as_C_string(),
 273                 _signature->as_C_string(),
 274                 _current_klass == NULL ? "(none)" : _current_klass->name()->as_C_string(),
 275                 _check_access ? "true" : "false");
 276 }
 277 #endif // PRODUCT
 278 //------------------------------------------------------------------------------------------------------------------------
 279 // Klass resolution
 280 
 281 void LinkResolver::check_klass_accessability(Klass* ref_klass, Klass* sel_klass,
 282                                              bool fold_type_to_class, TRAPS) {
 283   Klass* base_klass = sel_klass;
 284   if (fold_type_to_class) {
 285     if (sel_klass->is_objArray_klass()) {
 286       base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();


 572     jint new_flags = flags.as_int();
 573     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 574     new_flags = new_flags | JVM_ACC_PUBLIC;
 575     flags.set_flags(new_flags);
 576   }
 577 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 578 
 579   bool can_access = Reflection::verify_member_access(ref_klass,
 580                                                      resolved_klass,
 581                                                      sel_klass,
 582                                                      flags,
 583                                                      true, false, CHECK);
 584   // Any existing exceptions that may have been thrown, for example LinkageErrors
 585   // from nest-host resolution, have been allowed to propagate.
 586   if (!can_access) {
 587     ResourceMark rm(THREAD);
 588     bool same_module = (sel_klass->module() == ref_klass->module());
 589     Exceptions::fthrow(
 590       THREAD_AND_LOCATION,
 591       vmSymbols::java_lang_IllegalAccessError(),
 592       "class %s tried to access %s%s%smethod '%s' (%s%s%s)",
 593       ref_klass->external_name(),
 594       sel_method->is_abstract()  ? "abstract "  : "",
 595       sel_method->is_protected() ? "protected " : "",
 596       sel_method->is_private()   ? "private "   : "",
 597       sel_method->external_name(),


 598       (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 599       (same_module) ? "" : "; ",
 600       (same_module) ? "" : sel_klass->class_in_module_of_loader()
 601     );
 602     return;
 603   }
 604 }
 605 
 606 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 607                                                      const constantPoolHandle& pool, int index, TRAPS) {
 608   // This method is used only
 609   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 610   // and
 611   // (2) in Bytecode_invoke::static_target
 612   // It appears to fail when applied to an invokeinterface call site.
 613   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 614   // resolve klass
 615   if (code == Bytecodes::_invokedynamic) {
 616     Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
 617     Symbol* method_name = vmSymbols::invoke_name();


 647 // Check and print a loader constraint violation message for method or interface method
 648 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 649                                                    const methodHandle& resolved_method,
 650                                                    const char* method_type, TRAPS) {
 651   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 652   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 653 
 654   ResourceMark rm(THREAD);
 655   Symbol* failed_type_symbol =
 656     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 657                                               resolved_loader, true, CHECK);
 658   if (failed_type_symbol != NULL) {
 659     Klass* current_class = link_info.current_klass();
 660     ClassLoaderData* current_loader_data = current_class->class_loader_data();
 661     assert(current_loader_data != NULL, "current class has no class loader data");
 662     Klass* resolved_method_class = resolved_method->method_holder();
 663     ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
 664     assert(target_loader_data != NULL, "resolved method's class has no class loader data");
 665 
 666     stringStream ss;
 667     ss.print("loader constraint violation: when resolving %s '", method_type);
 668     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 669     ss.print("' the class loader %s of the current class, %s,"
 670              " and the class loader %s for the method's defining class, %s, have"
 671              " different Class objects for the type %s used in the signature (%s; %s)",


 672              current_loader_data->loader_name_and_id(),
 673              current_class->name()->as_C_string(),
 674              target_loader_data->loader_name_and_id(),
 675              resolved_method_class->name()->as_C_string(),
 676              failed_type_symbol->as_C_string(),
 677              current_class->class_in_module_of_loader(false, true),
 678              resolved_method_class->class_in_module_of_loader(false, true));
 679     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
 680   }
 681 }
 682 
 683 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 684                                                   Klass* current_klass,
 685                                                   Klass* sel_klass, TRAPS) {
 686   Handle ref_loader(THREAD, current_klass->class_loader());
 687   Handle sel_loader(THREAD, sel_klass->class_loader());
 688 
 689   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 690   Symbol* failed_type_symbol =
 691     SystemDictionary::check_signature_loaders(sig,


 715 }
 716 
 717 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 718                                           Bytecodes::Code code, TRAPS) {
 719 
 720   Handle nested_exception;
 721   Klass* resolved_klass = link_info.resolved_klass();
 722 
 723   // 1. For invokevirtual, cannot call an interface method
 724   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 725     ResourceMark rm(THREAD);
 726     char buf[200];
 727     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 728         resolved_klass->external_name());
 729     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 730   }
 731 
 732   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 733   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 734     ResourceMark rm(THREAD);
 735     stringStream ss;
 736     ss.print("Method '");
 737     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 738     ss.print("' must be Methodref constant");
 739     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 740   }
 741 
 742   // 3. lookup method in resolved klass and its super klasses
 743   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 744 
 745   // 4. lookup method in all the interfaces implemented by the resolved klass
 746   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 747     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 748 
 749     if (resolved_method.is_null()) {
 750       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 751       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD);
 752       if (HAS_PENDING_EXCEPTION) {
 753         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 754         CLEAR_PENDING_EXCEPTION;
 755       }
 756     }
 757   }
 758 
 759   // 5. method lookup failed
 760   if (resolved_method.is_null()) {
 761     ResourceMark rm(THREAD);
 762     stringStream ss;
 763     ss.print("'");
 764     Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
 765     ss.print("'");
 766     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 767                      ss.as_string(), nested_exception, NULL);



 768   }
 769 
 770   // 6. access checks, access checking may be turned off when calling from within the VM.
 771   Klass* current_klass = link_info.current_klass();
 772   if (link_info.check_access()) {
 773     assert(current_klass != NULL , "current_klass should not be null");
 774 
 775     // check if method can be accessed by the referring class
 776     check_method_accessability(current_klass,
 777                                resolved_klass,
 778                                resolved_method->method_holder(),
 779                                resolved_method,
 780                                CHECK_NULL);
 781 
 782     // check loader constraints
 783     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 784   }
 785 
 786   return resolved_method;
 787 }


 819   st->cr();
 820 #endif // PRODUCT
 821 }
 822 
 823 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 824 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 825 
 826   Klass* resolved_klass = link_info.resolved_klass();
 827 
 828   // check if klass is interface
 829   if (!resolved_klass->is_interface()) {
 830     ResourceMark rm(THREAD);
 831     char buf[200];
 832     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 833     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 834   }
 835 
 836   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 837   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 838     ResourceMark rm(THREAD);
 839     stringStream ss;
 840     ss.print("Method '");
 841     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 842     ss.print("' must be InterfaceMethodref constant");
 843     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 844   }
 845 
 846   // lookup method in this interface or its super, java.lang.Object
 847   // JDK8: also look for static methods
 848   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 849 
 850   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 851     // lookup method in all the super-interfaces
 852     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 853   }
 854 
 855   if (resolved_method.is_null()) {
 856     // no method found
 857     ResourceMark rm(THREAD);
 858     stringStream ss;
 859     ss.print("'");
 860     Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
 861     ss.print("'");
 862     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
 863   }
 864 
 865   if (link_info.check_access()) {
 866     // JDK8 adds non-public interface methods, and accessability check requirement
 867     Klass* current_klass = link_info.current_klass();
 868 
 869     assert(current_klass != NULL , "current_klass should not be null");
 870 
 871     // check if method can be accessed by the referring class
 872     check_method_accessability(current_klass,
 873                                resolved_klass,
 874                                resolved_method->method_holder(),
 875                                resolved_method,
 876                                CHECK_NULL);
 877 
 878     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 879   }
 880 
 881   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 882     ResourceMark rm(THREAD);
 883     stringStream ss;
 884     ss.print("Expected instance not static method '");
 885     Method::print_external_name(&ss, resolved_klass,
 886                                 resolved_method->name(), resolved_method->signature());
 887     ss.print("'");
 888     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 889   }
 890 
 891   if (log_develop_is_enabled(Trace, itables)) {
 892     char buf[200];
 893     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 894                  Bytecodes::name(code));
 895     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
 896                             resolved_method, true);
 897   }
 898 
 899   return resolved_method;
 900 }
 901 
 902 //------------------------------------------------------------------------------------------------------------------------
 903 // Field resolution
 904 
 905 void LinkResolver::check_field_accessability(Klass* ref_klass,
 906                                              Klass* resolved_klass,
 907                                              Klass* sel_klass,
 908                                              const fieldDescriptor& fd,


1069 
1070   // setup result
1071   result.set_static(resolved_klass, resolved_method, CHECK);
1072 }
1073 
1074 // throws linktime exceptions
1075 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1076 
1077   Klass* resolved_klass = link_info.resolved_klass();
1078   methodHandle resolved_method;
1079   if (!resolved_klass->is_interface()) {
1080     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1081   } else {
1082     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1083   }
1084   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1085 
1086   // check if static
1087   if (!resolved_method->is_static()) {
1088     ResourceMark rm(THREAD);
1089     stringStream ss;
1090     ss.print("Expected static method '");
1091     resolved_method()->print_external_name(&ss);
1092     ss.print("'");
1093     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1094   }
1095   return resolved_method;
1096 }
1097 
1098 
1099 void LinkResolver::resolve_special_call(CallInfo& result,
1100                                         Handle recv,
1101                                         const LinkInfo& link_info,
1102                                         TRAPS) {
1103   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1104   runtime_resolve_special_method(result, link_info, resolved_method, recv, CHECK);
1105 }
1106 
1107 // throws linktime exceptions
1108 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1109                                                            TRAPS) {
1110 
1111   // Invokespecial is called for multiple special reasons:
1112   // <init>
1113   // local private method invocation, for classes and interfaces
1114   // superclass.method, which can also resolve to a default method
1115   // and the selected method is recalculated relative to the direct superclass
1116   // superinterface.method, which explicitly does not check shadowing
1117   Klass* resolved_klass = link_info.resolved_klass();
1118   methodHandle resolved_method;
1119 
1120   if (!resolved_klass->is_interface()) {
1121     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1122   } else {
1123     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1124   }
1125 
1126   // check if method name is <init>, that it is found in same klass as static type
1127   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1128       resolved_method->method_holder() != resolved_klass) {
1129     ResourceMark rm(THREAD);
1130     stringStream ss;
1131     ss.print("%s: method '", resolved_klass->external_name());
1132     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1133     ss.print(" %s(", resolved_method->name()->as_C_string());
1134     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1135     ss.print(")' not found");
1136     Exceptions::fthrow(
1137       THREAD_AND_LOCATION,
1138       vmSymbols::java_lang_NoSuchMethodError(),
1139       "%s", ss.as_string());




1140     return NULL;
1141   }
1142 
1143   // ensure that invokespecial's interface method reference is in
1144   // a direct superinterface, not an indirect superinterface
1145   Klass* current_klass = link_info.current_klass();
1146   if (current_klass != NULL && resolved_klass->is_interface()) {
1147     InstanceKlass* ck = InstanceKlass::cast(current_klass);
1148     InstanceKlass *klass_to_check = !ck->is_unsafe_anonymous() ?
1149                                     ck :
1150                                     ck->unsafe_anonymous_host();
1151     // Disable verification for the dynamically-generated reflection bytecodes.
1152     bool is_reflect = klass_to_check->is_subclass_of(
1153                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1154 
1155     if (!is_reflect &&
1156         !klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1157       ResourceMark rm(THREAD);
1158       stringStream ss;
1159       ss.print("Interface method reference: '");
1160       resolved_method->print_external_name(&ss);
1161       ss.print("', is in an indirect superinterface of %s",
1162                current_klass->external_name());
1163       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());


1164     }
1165   }
1166 
1167   // check if not static
1168   if (resolved_method->is_static()) {
1169     ResourceMark rm(THREAD);
1170     stringStream ss;
1171     ss.print("Expecting non-static method '");
1172     resolved_method->print_external_name(&ss);
1173     ss.print("'");
1174     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());


1175   }
1176 
1177   if (log_develop_is_enabled(Trace, itables)) {
1178     trace_method_resolution("invokespecial resolved method: caller-class:",
1179                             current_klass, resolved_klass, resolved_method, true);
1180   }
1181 
1182   return resolved_method;
1183 }
1184 
1185 // throws runtime exceptions
1186 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1187                                                   const LinkInfo& link_info,
1188                                                   const methodHandle& resolved_method,
1189                                                   Handle recv, TRAPS) {
1190 
1191   Klass* resolved_klass = link_info.resolved_klass();
1192 
1193   // resolved method is selected method unless we have an old-style lookup
1194   // for a superclass method


1200       // check if the method is not <init>
1201       resolved_method->name() != vmSymbols::object_initializer_name()) {
1202 
1203     Klass* current_klass = link_info.current_klass();
1204 
1205     // Check if the class of the resolved_klass is a superclass
1206     // (not supertype in order to exclude interface classes) of the current class.
1207     // This check is not performed for super.invoke for interface methods
1208     // in super interfaces.
1209     if (current_klass->is_subclass_of(resolved_klass) &&
1210         current_klass != resolved_klass) {
1211       // Lookup super method
1212       Klass* super_klass = current_klass->super();
1213       sel_method = lookup_instance_method_in_klasses(super_klass,
1214                                                      resolved_method->name(),
1215                                                      resolved_method->signature(),
1216                                                      Klass::find_private, CHECK);
1217       // check if found
1218       if (sel_method.is_null()) {
1219         ResourceMark rm(THREAD);
1220         stringStream ss;
1221         ss.print("'");
1222         resolved_method->print_external_name(&ss);
1223         ss.print("'");
1224         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1225       // check loader constraints if found a different method
1226       } else if (sel_method() != resolved_method()) {
1227         check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1228       }
1229     }
1230 
1231     // Check that the class of objectref (the receiver) is the current class or interface,
1232     // or a subtype of the current class or interface (the sender), otherwise invokespecial
1233     // throws IllegalAccessError.
1234     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1235     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1236     // a class.  If the sender is an interface, the check has to be performed at runtime.
1237     InstanceKlass* sender = InstanceKlass::cast(current_klass);
1238     sender = sender->is_unsafe_anonymous() ? sender->unsafe_anonymous_host() : sender;
1239     if (sender->is_interface() && recv.not_null()) {
1240       Klass* receiver_klass = recv->klass();
1241       if (!receiver_klass->is_subtype_of(sender)) {
1242         ResourceMark rm(THREAD);
1243         char buf[500];
1244         jio_snprintf(buf, sizeof(buf),
1245                      "Receiver class %s must be the current class or a subtype of interface %s",
1246                      receiver_klass->external_name(),
1247                      sender->external_name());
1248         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1249       }
1250     }
1251   }
1252 
1253   // check if not static
1254   if (sel_method->is_static()) {
1255     ResourceMark rm(THREAD);
1256     stringStream ss;
1257     ss.print("Expecting non-static method '");
1258     resolved_method->print_external_name(&ss);
1259     ss.print("'");
1260     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1261   }
1262 
1263   // check if abstract
1264   if (sel_method->is_abstract()) {
1265     ResourceMark rm(THREAD);
1266     stringStream ss;
1267     ss.print("'");
1268     Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());
1269     ss.print("'");
1270     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1271   }
1272 
1273   if (log_develop_is_enabled(Trace, itables)) {
1274     trace_method_resolution("invokespecial selected method: resolved-class:",
1275                             resolved_klass, resolved_klass, sel_method, true);
1276   }
1277 
1278   // setup result
1279   result.set_static(resolved_klass, sel_method, CHECK);
1280 }
1281 
1282 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1283                                         const LinkInfo& link_info,
1284                                         bool check_null_and_abstract, TRAPS) {
1285   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1286   runtime_resolve_virtual_method(result, resolved_method,
1287                                  link_info.resolved_klass(),
1288                                  recv, receiver_klass,
1289                                  check_null_and_abstract, CHECK);
1290 }
1291 
1292 // throws linktime exceptions
1293 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1294                                                            TRAPS) {
1295   // normal method resolution
1296   methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1297 
1298   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1299   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1300 
1301   // check if private interface method
1302   Klass* resolved_klass = link_info.resolved_klass();
1303   Klass* current_klass = link_info.current_klass();
1304 
1305   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1306   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1307     ResourceMark rm(THREAD);
1308     stringStream ss;
1309     ss.print("private interface method requires invokespecial, not invokevirtual: method '");
1310     resolved_method->print_external_name(&ss);
1311     ss.print("', caller-class: %s",
1312              (current_klass == NULL ? "<null>" : current_klass->internal_name()));
1313     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());

1314   }
1315 
1316   // check if not static
1317   if (resolved_method->is_static()) {
1318     ResourceMark rm(THREAD);
1319     stringStream ss;
1320     ss.print("Expecting non-static method '");
1321     resolved_method->print_external_name(&ss);
1322     ss.print("'");
1323     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1324   }
1325 
1326   if (log_develop_is_enabled(Trace, vtables)) {
1327     trace_method_resolution("invokevirtual resolved method: caller-class:",
1328                             current_klass, resolved_klass, resolved_method, false);
1329   }
1330 
1331   return resolved_method;
1332 }
1333 
1334 // throws runtime exceptions
1335 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1336                                                   const methodHandle& resolved_method,
1337                                                   Klass* resolved_klass,
1338                                                   Handle recv,
1339                                                   Klass* recv_klass,
1340                                                   bool check_null_and_abstract,
1341                                                   TRAPS) {
1342 
1343   // setup default return values


1452     selected_method = lookup_instance_method_in_klasses(recv_klass,
1453                                                         resolved_method->name(),
1454                                                         resolved_method->signature(),
1455                                                         Klass::skip_private, CHECK);
1456 
1457     if (selected_method.is_null() && !check_null_and_abstract) {
1458       // In theory this is a harmless placeholder value, but
1459       // in practice leaving in null affects the nsk default method tests.
1460       // This needs further study.
1461       selected_method = resolved_method;
1462     }
1463     // check if method exists
1464     if (selected_method.is_null()) {
1465       // Pass arguments for generating a verbose error message.
1466       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1467     }
1468     // check access
1469     // Throw Illegal Access Error if selected_method is not public.
1470     if (!selected_method->is_public()) {
1471       ResourceMark rm(THREAD);
1472       stringStream ss;
1473       ss.print("'");
1474       Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1475       ss.print("'");
1476       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1477     }
1478     // check if abstract
1479     if (check_null_and_abstract && selected_method->is_abstract()) {
1480       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1481     }
1482   }
1483 
1484   if (log_develop_is_enabled(Trace, itables)) {
1485     trace_method_resolution("invokeinterface selected method: receiver-class:",
1486                             recv_klass, resolved_klass, selected_method, true);
1487   }
1488   // setup result
1489   if (resolved_method->has_vtable_index()) {
1490     int vtable_index = resolved_method->vtable_index();
1491     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1492     assert(vtable_index == selected_method->vtable_index(), "sanity check");
1493     result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1494   } else if (resolved_method->has_itable_index()) {
1495     int itable_index = resolved_method()->itable_index();
1496     log_develop_trace(itables)("  -- itable index: %d", itable_index);


1789   Exceptions::wrap_dynamic_exception(CHECK);
1790 }
1791 
1792 // Selected method is abstract.
1793 void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,
1794                                                const methodHandle& selected_method,
1795                                                Klass *recv_klass, TRAPS) {
1796   Klass *resolved_klass = resolved_method->method_holder();
1797   ResourceMark rm(THREAD);
1798   stringStream ss;
1799 
1800   if (recv_klass != NULL) {
1801     ss.print("Receiver class %s does not define or inherit an "
1802              "implementation of the",
1803              recv_klass->external_name());
1804   } else {
1805     ss.print("Missing implementation of");
1806   }
1807 
1808   assert(resolved_method.not_null(), "Sanity");
1809   ss.print(" resolved method '%s%s",
1810            resolved_method->is_abstract() ? "abstract " : "",
1811            resolved_method->is_private()  ? "private "  : "");
1812   resolved_method->signature()->print_as_signature_external_return_type(&ss);
1813   ss.print(" %s(", resolved_method->name()->as_C_string());
1814   resolved_method->signature()->print_as_signature_external_parameters(&ss);
1815   ss.print(")' of %s %s.",
1816            resolved_klass->external_kind(),
1817            resolved_klass->external_name());
1818 
1819   if (selected_method.not_null() && !(resolved_method == selected_method)) {
1820     ss.print(" Selected method is '%s%s",
1821              selected_method->is_abstract() ? "abstract " : "",
1822              selected_method->is_private()  ? "private "  : "");
1823     selected_method->print_external_name(&ss);
1824     ss.print("'.");
1825   }
1826 
1827   THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1828 }


 247 
 248   // Coming from the constant pool always checks access
 249   _check_access  = true;
 250 }
 251 
 252 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 253    // resolve klass
 254   _resolved_klass = pool->klass_ref_at(index, CHECK);
 255 
 256   // Get name, signature, and static klass
 257   _name          = pool->name_ref_at(index);
 258   _signature     = pool->signature_ref_at(index);
 259   _tag           = pool->tag_ref_at(index);
 260   _current_klass = pool->pool_holder();
 261   _current_method = methodHandle();
 262 
 263   // Coming from the constant pool always checks access
 264   _check_access  = true;
 265 }
 266 
 267 char* LinkInfo::method_string() const {
 268   return Method::name_and_sig_as_C_string(_resolved_klass, _name, _signature);
 269 }
 270 
 271 #ifndef PRODUCT
 272 void LinkInfo::print() {
 273   ResourceMark rm;
 274   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
 275                 _resolved_klass->name()->as_C_string(),
 276                 _name->as_C_string(),
 277                 _signature->as_C_string(),
 278                 _current_klass == NULL ? "(none)" : _current_klass->name()->as_C_string(),
 279                 _check_access ? "true" : "false");
 280 }
 281 #endif // PRODUCT
 282 //------------------------------------------------------------------------------------------------------------------------
 283 // Klass resolution
 284 
 285 void LinkResolver::check_klass_accessability(Klass* ref_klass, Klass* sel_klass,
 286                                              bool fold_type_to_class, TRAPS) {
 287   Klass* base_klass = sel_klass;
 288   if (fold_type_to_class) {
 289     if (sel_klass->is_objArray_klass()) {
 290       base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();


 576     jint new_flags = flags.as_int();
 577     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 578     new_flags = new_flags | JVM_ACC_PUBLIC;
 579     flags.set_flags(new_flags);
 580   }
 581 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 582 
 583   bool can_access = Reflection::verify_member_access(ref_klass,
 584                                                      resolved_klass,
 585                                                      sel_klass,
 586                                                      flags,
 587                                                      true, false, CHECK);
 588   // Any existing exceptions that may have been thrown, for example LinkageErrors
 589   // from nest-host resolution, have been allowed to propagate.
 590   if (!can_access) {
 591     ResourceMark rm(THREAD);
 592     bool same_module = (sel_klass->module() == ref_klass->module());
 593     Exceptions::fthrow(
 594       THREAD_AND_LOCATION,
 595       vmSymbols::java_lang_IllegalAccessError(),
 596       "class %s tried to access %s%s%smethod %s.%s%s (%s%s%s)",
 597       ref_klass->external_name(),
 598       sel_method->is_abstract()  ? "abstract "  : "",
 599       sel_method->is_protected() ? "protected " : "",
 600       sel_method->is_private()   ? "private "   : "",
 601       sel_klass->external_name(),
 602       sel_method->name()->as_C_string(),
 603       sel_method->signature()->as_C_string(),
 604       (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 605       (same_module) ? "" : "; ",
 606       (same_module) ? "" : sel_klass->class_in_module_of_loader()
 607     );
 608     return;
 609   }
 610 }
 611 
 612 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 613                                                      const constantPoolHandle& pool, int index, TRAPS) {
 614   // This method is used only
 615   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 616   // and
 617   // (2) in Bytecode_invoke::static_target
 618   // It appears to fail when applied to an invokeinterface call site.
 619   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 620   // resolve klass
 621   if (code == Bytecodes::_invokedynamic) {
 622     Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
 623     Symbol* method_name = vmSymbols::invoke_name();


 653 // Check and print a loader constraint violation message for method or interface method
 654 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 655                                                    const methodHandle& resolved_method,
 656                                                    const char* method_type, TRAPS) {
 657   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 658   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 659 
 660   ResourceMark rm(THREAD);
 661   Symbol* failed_type_symbol =
 662     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 663                                               resolved_loader, true, CHECK);
 664   if (failed_type_symbol != NULL) {
 665     Klass* current_class = link_info.current_klass();
 666     ClassLoaderData* current_loader_data = current_class->class_loader_data();
 667     assert(current_loader_data != NULL, "current class has no class loader data");
 668     Klass* resolved_method_class = resolved_method->method_holder();
 669     ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
 670     assert(target_loader_data != NULL, "resolved method's class has no class loader data");
 671 
 672     stringStream ss;
 673     ss.print("loader constraint violation: when resolving %s"
 674              " \"%s\" the class loader %s of the current class, %s,"

 675              " and the class loader %s for the method's defining class, %s, have"
 676              " different Class objects for the type %s used in the signature (%s; %s)",
 677              method_type,
 678              link_info.method_string(),
 679              current_loader_data->loader_name_and_id(),
 680              current_class->name()->as_C_string(),
 681              target_loader_data->loader_name_and_id(),
 682              resolved_method_class->name()->as_C_string(),
 683              failed_type_symbol->as_C_string(),
 684              current_class->class_in_module_of_loader(false, true),
 685              resolved_method_class->class_in_module_of_loader(false, true));
 686     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
 687   }
 688 }
 689 
 690 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 691                                                   Klass* current_klass,
 692                                                   Klass* sel_klass, TRAPS) {
 693   Handle ref_loader(THREAD, current_klass->class_loader());
 694   Handle sel_loader(THREAD, sel_klass->class_loader());
 695 
 696   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 697   Symbol* failed_type_symbol =
 698     SystemDictionary::check_signature_loaders(sig,


 722 }
 723 
 724 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 725                                           Bytecodes::Code code, TRAPS) {
 726 
 727   Handle nested_exception;
 728   Klass* resolved_klass = link_info.resolved_klass();
 729 
 730   // 1. For invokevirtual, cannot call an interface method
 731   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 732     ResourceMark rm(THREAD);
 733     char buf[200];
 734     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 735         resolved_klass->external_name());
 736     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 737   }
 738 
 739   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 740   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 741     ResourceMark rm(THREAD);
 742     char buf[200];
 743     jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string());
 744     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);


 745   }
 746 
 747   // 3. lookup method in resolved klass and its super klasses
 748   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 749 
 750   // 4. lookup method in all the interfaces implemented by the resolved klass
 751   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 752     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 753 
 754     if (resolved_method.is_null()) {
 755       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 756       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD);
 757       if (HAS_PENDING_EXCEPTION) {
 758         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 759         CLEAR_PENDING_EXCEPTION;
 760       }
 761     }
 762   }
 763 
 764   // 5. method lookup failed
 765   if (resolved_method.is_null()) {
 766     ResourceMark rm(THREAD);




 767     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 768                     Method::name_and_sig_as_C_string(resolved_klass,
 769                                                      link_info.name(),
 770                                                      link_info.signature()),
 771                     nested_exception, NULL);
 772   }
 773 
 774   // 6. access checks, access checking may be turned off when calling from within the VM.
 775   Klass* current_klass = link_info.current_klass();
 776   if (link_info.check_access()) {
 777     assert(current_klass != NULL , "current_klass should not be null");
 778 
 779     // check if method can be accessed by the referring class
 780     check_method_accessability(current_klass,
 781                                resolved_klass,
 782                                resolved_method->method_holder(),
 783                                resolved_method,
 784                                CHECK_NULL);
 785 
 786     // check loader constraints
 787     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 788   }
 789 
 790   return resolved_method;
 791 }


 823   st->cr();
 824 #endif // PRODUCT
 825 }
 826 
 827 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 828 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 829 
 830   Klass* resolved_klass = link_info.resolved_klass();
 831 
 832   // check if klass is interface
 833   if (!resolved_klass->is_interface()) {
 834     ResourceMark rm(THREAD);
 835     char buf[200];
 836     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 837     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 838   }
 839 
 840   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 841   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 842     ResourceMark rm(THREAD);
 843     char buf[200];
 844     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 845     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);


 846   }
 847 
 848   // lookup method in this interface or its super, java.lang.Object
 849   // JDK8: also look for static methods
 850   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 851 
 852   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 853     // lookup method in all the super-interfaces
 854     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 855   }
 856 
 857   if (resolved_method.is_null()) {
 858     // no method found
 859     ResourceMark rm(THREAD);
 860     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 861                    Method::name_and_sig_as_C_string(resolved_klass,
 862                                                     link_info.name(),
 863                                                     link_info.signature()));

 864   }
 865 
 866   if (link_info.check_access()) {
 867     // JDK8 adds non-public interface methods, and accessability check requirement
 868     Klass* current_klass = link_info.current_klass();
 869 
 870     assert(current_klass != NULL , "current_klass should not be null");
 871 
 872     // check if method can be accessed by the referring class
 873     check_method_accessability(current_klass,
 874                                resolved_klass,
 875                                resolved_method->method_holder(),
 876                                resolved_method,
 877                                CHECK_NULL);
 878 
 879     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 880   }
 881 
 882   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 883     ResourceMark rm(THREAD);
 884     char buf[200];
 885     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 886                  Method::name_and_sig_as_C_string(resolved_klass,
 887                  resolved_method->name(), resolved_method->signature()));
 888     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);

 889   }
 890 
 891   if (log_develop_is_enabled(Trace, itables)) {
 892     char buf[200];
 893     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 894                  Bytecodes::name(code));
 895     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
 896                             resolved_method, true);
 897   }
 898 
 899   return resolved_method;
 900 }
 901 
 902 //------------------------------------------------------------------------------------------------------------------------
 903 // Field resolution
 904 
 905 void LinkResolver::check_field_accessability(Klass* ref_klass,
 906                                              Klass* resolved_klass,
 907                                              Klass* sel_klass,
 908                                              const fieldDescriptor& fd,


1069 
1070   // setup result
1071   result.set_static(resolved_klass, resolved_method, CHECK);
1072 }
1073 
1074 // throws linktime exceptions
1075 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1076 
1077   Klass* resolved_klass = link_info.resolved_klass();
1078   methodHandle resolved_method;
1079   if (!resolved_klass->is_interface()) {
1080     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1081   } else {
1082     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1083   }
1084   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1085 
1086   // check if static
1087   if (!resolved_method->is_static()) {
1088     ResourceMark rm(THREAD);
1089     char buf[200];
1090     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1091                                                       resolved_method->name(),
1092                                                       resolved_method->signature()));
1093     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1094   }
1095   return resolved_method;
1096 }
1097 
1098 
1099 void LinkResolver::resolve_special_call(CallInfo& result,
1100                                         Handle recv,
1101                                         const LinkInfo& link_info,
1102                                         TRAPS) {
1103   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1104   runtime_resolve_special_method(result, link_info, resolved_method, recv, CHECK);
1105 }
1106 
1107 // throws linktime exceptions
1108 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1109                                                            TRAPS) {
1110 
1111   // Invokespecial is called for multiple special reasons:
1112   // <init>
1113   // local private method invocation, for classes and interfaces
1114   // superclass.method, which can also resolve to a default method
1115   // and the selected method is recalculated relative to the direct superclass
1116   // superinterface.method, which explicitly does not check shadowing
1117   Klass* resolved_klass = link_info.resolved_klass();
1118   methodHandle resolved_method;
1119 
1120   if (!resolved_klass->is_interface()) {
1121     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1122   } else {
1123     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1124   }
1125 
1126   // check if method name is <init>, that it is found in same klass as static type
1127   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1128       resolved_method->method_holder() != resolved_klass) {
1129     ResourceMark rm(THREAD);






1130     Exceptions::fthrow(
1131       THREAD_AND_LOCATION,
1132       vmSymbols::java_lang_NoSuchMethodError(),
1133       "%s: method %s%s not found",
1134       resolved_klass->external_name(),
1135       resolved_method->name()->as_C_string(),
1136       resolved_method->signature()->as_C_string()
1137     );
1138     return NULL;
1139   }
1140 
1141   // ensure that invokespecial's interface method reference is in
1142   // a direct superinterface, not an indirect superinterface
1143   Klass* current_klass = link_info.current_klass();
1144   if (current_klass != NULL && resolved_klass->is_interface()) {
1145     InstanceKlass* ck = InstanceKlass::cast(current_klass);
1146     InstanceKlass *klass_to_check = !ck->is_unsafe_anonymous() ?
1147                                     ck :
1148                                     ck->unsafe_anonymous_host();
1149     // Disable verification for the dynamically-generated reflection bytecodes.
1150     bool is_reflect = klass_to_check->is_subclass_of(
1151                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1152 
1153     if (!is_reflect &&
1154         !klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1155       ResourceMark rm(THREAD);
1156       char buf[200];
1157       jio_snprintf(buf, sizeof(buf),
1158                    "Interface method reference: %s, is in an indirect superinterface of %s",
1159                    Method::name_and_sig_as_C_string(resolved_klass,
1160                                                                            resolved_method->name(),
1161                                                                            resolved_method->signature()),
1162                    current_klass->external_name());
1163       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1164     }
1165   }
1166 
1167   // check if not static
1168   if (resolved_method->is_static()) {
1169     ResourceMark rm(THREAD);
1170     char buf[200];
1171     jio_snprintf(buf, sizeof(buf),
1172                  "Expecting non-static method %s",
1173                  Method::name_and_sig_as_C_string(resolved_klass,
1174                                                   resolved_method->name(),
1175                                                   resolved_method->signature()));
1176     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1177   }
1178 
1179   if (log_develop_is_enabled(Trace, itables)) {
1180     trace_method_resolution("invokespecial resolved method: caller-class:",
1181                             current_klass, resolved_klass, resolved_method, true);
1182   }
1183 
1184   return resolved_method;
1185 }
1186 
1187 // throws runtime exceptions
1188 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1189                                                   const LinkInfo& link_info,
1190                                                   const methodHandle& resolved_method,
1191                                                   Handle recv, TRAPS) {
1192 
1193   Klass* resolved_klass = link_info.resolved_klass();
1194 
1195   // resolved method is selected method unless we have an old-style lookup
1196   // for a superclass method


1202       // check if the method is not <init>
1203       resolved_method->name() != vmSymbols::object_initializer_name()) {
1204 
1205     Klass* current_klass = link_info.current_klass();
1206 
1207     // Check if the class of the resolved_klass is a superclass
1208     // (not supertype in order to exclude interface classes) of the current class.
1209     // This check is not performed for super.invoke for interface methods
1210     // in super interfaces.
1211     if (current_klass->is_subclass_of(resolved_klass) &&
1212         current_klass != resolved_klass) {
1213       // Lookup super method
1214       Klass* super_klass = current_klass->super();
1215       sel_method = lookup_instance_method_in_klasses(super_klass,
1216                                                      resolved_method->name(),
1217                                                      resolved_method->signature(),
1218                                                      Klass::find_private, CHECK);
1219       // check if found
1220       if (sel_method.is_null()) {
1221         ResourceMark rm(THREAD);
1222         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1223                   Method::name_and_sig_as_C_string(resolved_klass,
1224                                             resolved_method->name(),
1225                                             resolved_method->signature()));

1226       // check loader constraints if found a different method
1227       } else if (sel_method() != resolved_method()) {
1228         check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1229       }
1230     }
1231 
1232     // Check that the class of objectref (the receiver) is the current class or interface,
1233     // or a subtype of the current class or interface (the sender), otherwise invokespecial
1234     // throws IllegalAccessError.
1235     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1236     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1237     // a class.  If the sender is an interface, the check has to be performed at runtime.
1238     InstanceKlass* sender = InstanceKlass::cast(current_klass);
1239     sender = sender->is_unsafe_anonymous() ? sender->unsafe_anonymous_host() : sender;
1240     if (sender->is_interface() && recv.not_null()) {
1241       Klass* receiver_klass = recv->klass();
1242       if (!receiver_klass->is_subtype_of(sender)) {
1243         ResourceMark rm(THREAD);
1244         char buf[500];
1245         jio_snprintf(buf, sizeof(buf),
1246                      "Receiver class %s must be the current class or a subtype of interface %s",
1247                      receiver_klass->name()->as_C_string(),
1248                      sender->name()->as_C_string());
1249         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1250       }
1251     }
1252   }
1253 
1254   // check if not static
1255   if (sel_method->is_static()) {
1256     ResourceMark rm(THREAD);
1257     char buf[200];
1258     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1259                                                                                       resolved_method->name(),
1260                                                                                       resolved_method->signature()));
1261     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1262   }
1263 
1264   // check if abstract
1265   if (sel_method->is_abstract()) {
1266     ResourceMark rm(THREAD);
1267     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1268               Method::name_and_sig_as_C_string(resolved_klass,
1269                                                sel_method->name(),
1270                                                sel_method->signature()));

1271   }
1272 
1273   if (log_develop_is_enabled(Trace, itables)) {
1274     trace_method_resolution("invokespecial selected method: resolved-class:",
1275                             resolved_klass, resolved_klass, sel_method, true);
1276   }
1277 
1278   // setup result
1279   result.set_static(resolved_klass, sel_method, CHECK);
1280 }
1281 
1282 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1283                                         const LinkInfo& link_info,
1284                                         bool check_null_and_abstract, TRAPS) {
1285   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1286   runtime_resolve_virtual_method(result, resolved_method,
1287                                  link_info.resolved_klass(),
1288                                  recv, receiver_klass,
1289                                  check_null_and_abstract, CHECK);
1290 }
1291 
1292 // throws linktime exceptions
1293 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1294                                                            TRAPS) {
1295   // normal method resolution
1296   methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1297 
1298   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1299   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1300 
1301   // check if private interface method
1302   Klass* resolved_klass = link_info.resolved_klass();
1303   Klass* current_klass = link_info.current_klass();
1304 
1305   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1306   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1307     ResourceMark rm(THREAD);
1308     char buf[200];
1309     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1310                  Method::name_and_sig_as_C_string(resolved_klass,
1311                                                   resolved_method->name(),
1312                                                   resolved_method->signature()),
1313                    (current_klass == NULL ? "<NULL>" : current_klass->internal_name()));
1314     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1315   }
1316 
1317   // check if not static
1318   if (resolved_method->is_static()) {
1319     ResourceMark rm(THREAD);
1320     char buf[200];
1321     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1322                                                                                            resolved_method->name(),
1323                                                                                            resolved_method->signature()));
1324     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1325   }
1326 
1327   if (log_develop_is_enabled(Trace, vtables)) {
1328     trace_method_resolution("invokevirtual resolved method: caller-class:",
1329                             current_klass, resolved_klass, resolved_method, false);
1330   }
1331 
1332   return resolved_method;
1333 }
1334 
1335 // throws runtime exceptions
1336 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1337                                                   const methodHandle& resolved_method,
1338                                                   Klass* resolved_klass,
1339                                                   Handle recv,
1340                                                   Klass* recv_klass,
1341                                                   bool check_null_and_abstract,
1342                                                   TRAPS) {
1343 
1344   // setup default return values


1453     selected_method = lookup_instance_method_in_klasses(recv_klass,
1454                                                         resolved_method->name(),
1455                                                         resolved_method->signature(),
1456                                                         Klass::skip_private, CHECK);
1457 
1458     if (selected_method.is_null() && !check_null_and_abstract) {
1459       // In theory this is a harmless placeholder value, but
1460       // in practice leaving in null affects the nsk default method tests.
1461       // This needs further study.
1462       selected_method = resolved_method;
1463     }
1464     // check if method exists
1465     if (selected_method.is_null()) {
1466       // Pass arguments for generating a verbose error message.
1467       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1468     }
1469     // check access
1470     // Throw Illegal Access Error if selected_method is not public.
1471     if (!selected_method->is_public()) {
1472       ResourceMark rm(THREAD);
1473       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1474                 Method::name_and_sig_as_C_string(recv_klass,
1475                                                  selected_method->name(),
1476                                                  selected_method->signature()));

1477     }
1478     // check if abstract
1479     if (check_null_and_abstract && selected_method->is_abstract()) {
1480       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1481     }
1482   }
1483 
1484   if (log_develop_is_enabled(Trace, itables)) {
1485     trace_method_resolution("invokeinterface selected method: receiver-class:",
1486                             recv_klass, resolved_klass, selected_method, true);
1487   }
1488   // setup result
1489   if (resolved_method->has_vtable_index()) {
1490     int vtable_index = resolved_method->vtable_index();
1491     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1492     assert(vtable_index == selected_method->vtable_index(), "sanity check");
1493     result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1494   } else if (resolved_method->has_itable_index()) {
1495     int itable_index = resolved_method()->itable_index();
1496     log_develop_trace(itables)("  -- itable index: %d", itable_index);


1789   Exceptions::wrap_dynamic_exception(CHECK);
1790 }
1791 
1792 // Selected method is abstract.
1793 void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,
1794                                                const methodHandle& selected_method,
1795                                                Klass *recv_klass, TRAPS) {
1796   Klass *resolved_klass = resolved_method->method_holder();
1797   ResourceMark rm(THREAD);
1798   stringStream ss;
1799 
1800   if (recv_klass != NULL) {
1801     ss.print("Receiver class %s does not define or inherit an "
1802              "implementation of the",
1803              recv_klass->external_name());
1804   } else {
1805     ss.print("Missing implementation of");
1806   }
1807 
1808   assert(resolved_method.not_null(), "Sanity");
1809   ss.print(" resolved method %s%s%s%s of %s %s.",
1810            resolved_method->is_abstract() ? "abstract " : "",
1811            resolved_method->is_private()  ? "private "  : "",
1812            resolved_method->name()->as_C_string(),
1813            resolved_method->signature()->as_C_string(),


1814            resolved_klass->external_kind(),
1815            resolved_klass->external_name());
1816 
1817   if (selected_method.not_null() && !(resolved_method == selected_method)) {
1818     ss.print(" Selected method is %s%s%s.",
1819              selected_method->is_abstract() ? "abstract " : "",
1820              selected_method->is_private()  ? "private "  : "",
1821              selected_method->name_and_sig_as_C_string());

1822   }
1823 
1824   THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1825 }
< prev index next >