< prev index next >

src/hotspot/share/interpreter/linkResolver.cpp

Print this page
rev 50604 : imported patch jep181-rev1
rev 50605 : imported patch jep181-rev2
rev 50606 : imported patch jep181-rev3


 370     Array<Method*>* default_methods = ik->default_methods();
 371     if (default_methods != NULL) {
 372       result = InstanceKlass::find_method(default_methods, name, signature);
 373     }
 374   }
 375 
 376   if (checkpolymorphism && result != NULL) {
 377     vmIntrinsics::ID iid = result->intrinsic_id();
 378     if (MethodHandles::is_signature_polymorphic(iid)) {
 379       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 380       return NULL;
 381     }
 382   }
 383   return result;
 384 }
 385 
 386 // returns first instance method
 387 // Looks up method in classes, then looks up local default methods
 388 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 389                                                              Symbol* name,
 390                                                              Symbol* signature, TRAPS) {
 391   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);

 392 
 393   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 394     Klass* super_klass = result->method_holder()->super();
 395     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 396   }
 397 
 398   if (klass->is_array_klass()) {
 399     // Only consider klass and super klass for arrays
 400     return methodHandle(THREAD, result);
 401   }
 402 
 403   if (result == NULL) {
 404     Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
 405     if (default_methods != NULL) {
 406       result = InstanceKlass::find_method(default_methods, name, signature);
 407       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 408     }
 409   }
 410   return methodHandle(THREAD, result);
 411 }
 412 
 413 int LinkResolver::vtable_index_of_interface_method(Klass* klass,
 414                                                    const methodHandle& resolved_method) {
 415 


 565 
 566   // Special case:  arrays always override "clone". JVMS 2.15.
 567   // If the resolved klass is an array class, and the declaring class
 568   // is java.lang.Object and the method is "clone", set the flags
 569   // to public.
 570   //
 571   // We'll check for the method name first, as that's most likely
 572   // to be false (so we'll short-circuit out of these tests).
 573   if (sel_method->name() == vmSymbols::clone_name() &&
 574       sel_klass == SystemDictionary::Object_klass() &&
 575       resolved_klass->is_array_klass()) {
 576     // We need to change "protected" to "public".
 577     assert(flags.is_protected(), "clone not protected?");
 578     jint new_flags = flags.as_int();
 579     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 580     new_flags = new_flags | JVM_ACC_PUBLIC;
 581     flags.set_flags(new_flags);
 582   }
 583 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 584 
 585   if (!Reflection::verify_field_access(ref_klass,
 586                                        resolved_klass,
 587                                        sel_klass,
 588                                        flags,
 589                                        true)) {



 590     ResourceMark rm(THREAD);
 591     Exceptions::fthrow(
 592       THREAD_AND_LOCATION,
 593       vmSymbols::java_lang_IllegalAccessError(),
 594       "tried to access method %s.%s%s from class %s",
 595       sel_klass->external_name(),
 596       sel_method->name()->as_C_string(),
 597       sel_method->signature()->as_C_string(),
 598       ref_klass->external_name()
 599     );
 600     return;
 601   }
 602 }
 603 
 604 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 605                                                      const constantPoolHandle& pool, int index, TRAPS) {
 606   // This method is used only
 607   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 608   // and
 609   // (2) in Bytecode_invoke::static_target


 740     if (resolved_method.is_null()) {
 741       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 742       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 743       if (HAS_PENDING_EXCEPTION) {
 744         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 745         CLEAR_PENDING_EXCEPTION;
 746       }
 747     }
 748   }
 749 
 750   // 5. method lookup failed
 751   if (resolved_method.is_null()) {
 752     ResourceMark rm(THREAD);
 753     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 754                     Method::name_and_sig_as_C_string(resolved_klass,
 755                                                      link_info.name(),
 756                                                      link_info.signature()),
 757                     nested_exception, NULL);
 758   }
 759 
 760   // 5. access checks, access checking may be turned off when calling from within the VM.
 761   Klass* current_klass = link_info.current_klass();
 762   if (link_info.check_access()) {
 763     assert(current_klass != NULL , "current_klass should not be null");
 764 
 765     // check if method can be accessed by the referring class
 766     check_method_accessability(current_klass,
 767                                resolved_klass,
 768                                resolved_method->method_holder(),
 769                                resolved_method,
 770                                CHECK_NULL);
 771 
 772     // check loader constraints
 773     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 774   }
 775 


















 776   return resolved_method;
 777 }
 778 
 779 static void trace_method_resolution(const char* prefix,
 780                                     Klass* klass,
 781                                     Klass* resolved_klass,
 782                                     const methodHandle& method,
 783                                     bool logitables,
 784                                     int index = -1) {
 785 #ifndef PRODUCT
 786   ResourceMark rm;
 787   Log(itables) logi;
 788   LogStream lsi(logi.trace());
 789   Log(vtables) logv;
 790   LogStream lsv(logv.trace());
 791   outputStream* st;
 792   if (logitables) {
 793     st = &lsi;
 794   } else {
 795     st = &lsv;


 857 
 858     // check if method can be accessed by the referring class
 859     check_method_accessability(current_klass,
 860                                resolved_klass,
 861                                resolved_method->method_holder(),
 862                                resolved_method,
 863                                CHECK_NULL);
 864 
 865     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 866   }
 867 
 868   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 869     ResourceMark rm(THREAD);
 870     char buf[200];
 871     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 872                  Method::name_and_sig_as_C_string(resolved_klass,
 873                  resolved_method->name(), resolved_method->signature()));
 874     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 875   }
 876 
 877   if (code == Bytecodes::_invokeinterface && resolved_method->is_private()) {
 878     ResourceMark rm(THREAD);
 879     char buf[200];
 880 
 881     Klass* current_klass = link_info.current_klass();
 882     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s, caller-class:%s",
 883                  Method::name_and_sig_as_C_string(resolved_klass,
 884                                                   resolved_method->name(),
 885                                                   resolved_method->signature()),
 886                                                   (current_klass == NULL ? "<NULL>" : current_klass->internal_name()));
 887      THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 888   }
 889 
 890   if (log_develop_is_enabled(Trace, itables)) {
 891     char buf[200];
 892     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 893                  Bytecodes::name(code));
 894     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
 895                             resolved_method, true);
 896   }
 897 
 898   return resolved_method;
 899 }
 900 
 901 //------------------------------------------------------------------------------------------------------------------------
 902 // Field resolution
 903 
 904 void LinkResolver::check_field_accessability(Klass* ref_klass,
 905                                              Klass* resolved_klass,
 906                                              Klass* sel_klass,
 907                                              const fieldDescriptor& fd,
 908                                              TRAPS) {
 909   if (!Reflection::verify_field_access(ref_klass,
 910                                        resolved_klass,
 911                                        sel_klass,
 912                                        fd.access_flags(),
 913                                        true)) {



 914     ResourceMark rm(THREAD);
 915     Exceptions::fthrow(
 916       THREAD_AND_LOCATION,
 917       vmSymbols::java_lang_IllegalAccessError(),
 918       "tried to access field %s.%s from class %s",
 919       sel_klass->external_name(),
 920       fd.name()->as_C_string(),
 921       ref_klass->external_name()
 922     );
 923     return;
 924   }
 925 }
 926 
 927 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 928   LinkInfo link_info(pool, index, method, CHECK);
 929   resolve_field(fd, link_info, byte, true, CHECK);
 930 }
 931 
 932 void LinkResolver::resolve_field(fieldDescriptor& fd,
 933                                  const LinkInfo& link_info,


1111     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1112   } else {
1113     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1114   }
1115 
1116   // check if method name is <init>, that it is found in same klass as static type
1117   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1118       resolved_method->method_holder() != resolved_klass) {
1119     ResourceMark rm(THREAD);
1120     Exceptions::fthrow(
1121       THREAD_AND_LOCATION,
1122       vmSymbols::java_lang_NoSuchMethodError(),
1123       "%s: method %s%s not found",
1124       resolved_klass->external_name(),
1125       resolved_method->name()->as_C_string(),
1126       resolved_method->signature()->as_C_string()
1127     );
1128     return NULL;
1129   }
1130 
1131   // check if invokespecial's interface method reference is in an indirect superinterface

1132   Klass* current_klass = link_info.current_klass();
1133   if (current_klass != NULL && resolved_klass->is_interface()) {
1134     InstanceKlass* ck = InstanceKlass::cast(current_klass);
1135     InstanceKlass *klass_to_check = !ck->is_anonymous() ?
1136                                     ck :
1137                                     InstanceKlass::cast(ck->host_klass());
1138     // Disable verification for the dynamically-generated reflection bytecodes.
1139     bool is_reflect = klass_to_check->is_subclass_of(
1140                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1141 
1142     if (!is_reflect &&
1143         !klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1144       ResourceMark rm(THREAD);
1145       char buf[200];
1146       jio_snprintf(buf, sizeof(buf),
1147                    "Interface method reference: %s, is in an indirect superinterface of %s",
1148                    Method::name_and_sig_as_C_string(resolved_klass,
1149                                                     resolved_method->name(),
1150                                                     resolved_method->signature()),
1151                    current_klass->external_name());


1183 
1184   // resolved method is selected method unless we have an old-style lookup
1185   // for a superclass method
1186   // Invokespecial for a superinterface, resolved method is selected method,
1187   // no checks for shadowing
1188   methodHandle sel_method(THREAD, resolved_method());
1189 
1190   if (link_info.check_access() &&
1191       // check if the method is not <init>
1192       resolved_method->name() != vmSymbols::object_initializer_name()) {
1193 
1194      // check if this is an old-style super call and do a new lookup if so
1195         // a) check if ACC_SUPER flag is set for the current class
1196     Klass* current_klass = link_info.current_klass();
1197     if ((current_klass->is_super() || !AllowNonVirtualCalls) &&
1198         // b) check if the class of the resolved_klass is a superclass
1199         // (not supertype in order to exclude interface classes) of the current class.
1200         // This check is not performed for super.invoke for interface methods
1201         // in super interfaces.
1202         current_klass->is_subclass_of(resolved_klass) &&
1203         current_klass != resolved_klass) {

1204       // Lookup super method
1205       Klass* super_klass = current_klass->super();
1206       sel_method = lookup_instance_method_in_klasses(super_klass,
1207                            resolved_method->name(),
1208                            resolved_method->signature(), CHECK);

1209       // check if found
1210       if (sel_method.is_null()) {
1211         ResourceMark rm(THREAD);
1212         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1213                   Method::name_and_sig_as_C_string(resolved_klass,
1214                                             resolved_method->name(),
1215                                             resolved_method->signature()));
1216       // check loader constraints if found a different method
1217       } else if (sel_method() != resolved_method()) {
1218         check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1219       }
1220     }
1221 
1222     // Check that the class of objectref (the receiver) is the current class or interface,
1223     // or a subtype of the current class or interface (the sender), otherwise invokespecial
1224     // throws IllegalAccessError.
1225     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1226     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1227     // a class.  If the sender is an interface, the check has to be performed at runtime.
1228     InstanceKlass* sender = InstanceKlass::cast(current_klass);


1339   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1340     THROW(vmSymbols::java_lang_NullPointerException());
1341   }
1342 
1343   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1344   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1345   // a missing receiver might result in a bogus lookup.
1346   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1347 
1348   // do lookup based on receiver klass using the vtable index
1349   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1350     vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1351     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1352 
1353     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1354   } else {
1355     // at this point we are sure that resolved_method is virtual and not
1356     // a default or miranda method; therefore, it must have a valid vtable index.
1357     assert(!resolved_method->has_itable_index(), "");
1358     vtable_index = resolved_method->vtable_index();
1359     // We could get a negative vtable_index for final methods,
1360     // because as an optimization they are never put in the vtable,
1361     // unless they override an existing method.
1362     // If we do get a negative, it means the resolved method is the the selected
1363     // method, and it can never be changed by an override.

1364     if (vtable_index == Method::nonvirtual_vtable_index) {
1365       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1366       selected_method = resolved_method;
1367     } else {
1368       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1369     }
1370   }
1371 
1372   // check if method exists
1373   if (selected_method.is_null()) {
1374     throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1375   }
1376 
1377   // check if abstract
1378   if (check_null_and_abstract && selected_method->is_abstract()) {
1379     // Pass arguments for generating a verbose error message.
1380     throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1381   }
1382 
1383   if (log_develop_is_enabled(Trace, vtables)) {


1398                                    recv, recv_klass, check_null_and_abstract, CHECK);
1399 }
1400 
1401 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1402                                                              TRAPS) {
1403   // normal interface method resolution
1404   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1405   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1406   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1407 
1408   return resolved_method;
1409 }
1410 
1411 // throws runtime exceptions
1412 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1413                                                     const methodHandle& resolved_method,
1414                                                     Klass* resolved_klass,
1415                                                     Handle recv,
1416                                                     Klass* recv_klass,
1417                                                     bool check_null_and_abstract, TRAPS) {

1418   // check if receiver exists
1419   if (check_null_and_abstract && recv.is_null()) {
1420     THROW(vmSymbols::java_lang_NullPointerException());
1421   }
1422 
1423   // check if receiver klass implements the resolved interface
1424   if (!recv_klass->is_subtype_of(resolved_klass)) {
1425     ResourceMark rm(THREAD);
1426     char buf[200];
1427     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1428                  recv_klass->external_name(),
1429                  resolved_klass->external_name());
1430     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1431   }
1432 




1433   // do lookup based on receiver klass
1434   // This search must match the linktime preparation search for itable initialization
1435   // to correctly enforce loader constraints for interface method inheritance
1436   methodHandle selected_method = lookup_instance_method_in_klasses(recv_klass,

1437                                                   resolved_method->name(),
1438                                                   resolved_method->signature(), CHECK);


1439   if (selected_method.is_null() && !check_null_and_abstract) {
1440     // In theory this is a harmless placeholder value, but
1441     // in practice leaving in null affects the nsk default method tests.
1442     // This needs further study.
1443     selected_method = resolved_method;
1444   }
1445   // check if method exists
1446   if (selected_method.is_null()) {
1447     // Pass arguments for generating a verbose error message.
1448     throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1449   }
1450   // check access
1451   // Throw Illegal Access Error if selected_method is not public.
1452   if (!selected_method->is_public()) {
1453     ResourceMark rm(THREAD);
1454     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1455               Method::name_and_sig_as_C_string(recv_klass,
1456                                                selected_method->name(),
1457                                                selected_method->signature()));
1458   }
1459   // check if abstract
1460   if (check_null_and_abstract && selected_method->is_abstract()) {
1461     throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1462   }

1463 
1464   if (log_develop_is_enabled(Trace, itables)) {
1465     trace_method_resolution("invokeinterface selected method: receiver-class:",
1466                             recv_klass, resolved_klass, selected_method, true);
1467   }
1468   // setup result
1469   if (!resolved_method->has_itable_index()) {
1470     int vtable_index = resolved_method->vtable_index();

1471     assert(vtable_index == selected_method->vtable_index(), "sanity check");
1472     result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1473   } else {
1474     int itable_index = resolved_method()->itable_index();

1475     result.set_interface(resolved_klass, recv_klass, resolved_method, selected_method, itable_index, CHECK);










1476   }
1477 }
1478 
1479 
1480 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1481                                                  const LinkInfo& link_info) {
1482   EXCEPTION_MARK;
1483   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1484   if (HAS_PENDING_EXCEPTION) {
1485     CLEAR_PENDING_EXCEPTION;
1486     return methodHandle();
1487   } else {
1488     return method_result;
1489   }
1490 }
1491 
1492 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1493                                                  const LinkInfo& link_info) {
1494   EXCEPTION_MARK;
1495   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);




 370     Array<Method*>* default_methods = ik->default_methods();
 371     if (default_methods != NULL) {
 372       result = InstanceKlass::find_method(default_methods, name, signature);
 373     }
 374   }
 375 
 376   if (checkpolymorphism && result != NULL) {
 377     vmIntrinsics::ID iid = result->intrinsic_id();
 378     if (MethodHandles::is_signature_polymorphic(iid)) {
 379       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 380       return NULL;
 381     }
 382   }
 383   return result;
 384 }
 385 
 386 // returns first instance method
 387 // Looks up method in classes, then looks up local default methods
 388 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 389                                                              Symbol* name,
 390                                                              Symbol* signature,
 391                                                              Klass::PrivateLookupMode private_mode, TRAPS) {
 392   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
 393 
 394   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 395     Klass* super_klass = result->method_holder()->super();
 396     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
 397   }
 398 
 399   if (klass->is_array_klass()) {
 400     // Only consider klass and super klass for arrays
 401     return methodHandle(THREAD, result);
 402   }
 403 
 404   if (result == NULL) {
 405     Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
 406     if (default_methods != NULL) {
 407       result = InstanceKlass::find_method(default_methods, name, signature);
 408       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 409     }
 410   }
 411   return methodHandle(THREAD, result);
 412 }
 413 
 414 int LinkResolver::vtable_index_of_interface_method(Klass* klass,
 415                                                    const methodHandle& resolved_method) {
 416 


 566 
 567   // Special case:  arrays always override "clone". JVMS 2.15.
 568   // If the resolved klass is an array class, and the declaring class
 569   // is java.lang.Object and the method is "clone", set the flags
 570   // to public.
 571   //
 572   // We'll check for the method name first, as that's most likely
 573   // to be false (so we'll short-circuit out of these tests).
 574   if (sel_method->name() == vmSymbols::clone_name() &&
 575       sel_klass == SystemDictionary::Object_klass() &&
 576       resolved_klass->is_array_klass()) {
 577     // We need to change "protected" to "public".
 578     assert(flags.is_protected(), "clone not protected?");
 579     jint new_flags = flags.as_int();
 580     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 581     new_flags = new_flags | JVM_ACC_PUBLIC;
 582     flags.set_flags(new_flags);
 583   }
 584 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 585 
 586   bool can_access = Reflection::verify_member_access(ref_klass,
 587                                                      resolved_klass,
 588                                                      sel_klass,
 589                                                      flags,
 590                                                      true, false, CHECK);
 591   // Any existing exceptions that may have been thrown, for example LinkageErrors
 592   // from nest-host resolution, have been allowed to propagate.
 593   if (!can_access) {
 594     ResourceMark rm(THREAD);
 595     Exceptions::fthrow(
 596       THREAD_AND_LOCATION,
 597       vmSymbols::java_lang_IllegalAccessError(),
 598       "tried to access method %s.%s%s from class %s",
 599       sel_klass->external_name(),
 600       sel_method->name()->as_C_string(),
 601       sel_method->signature()->as_C_string(),
 602       ref_klass->external_name()
 603     );
 604     return;
 605   }
 606 }
 607 
 608 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 609                                                      const constantPoolHandle& pool, int index, TRAPS) {
 610   // This method is used only
 611   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 612   // and
 613   // (2) in Bytecode_invoke::static_target


 744     if (resolved_method.is_null()) {
 745       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 746       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 747       if (HAS_PENDING_EXCEPTION) {
 748         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 749         CLEAR_PENDING_EXCEPTION;
 750       }
 751     }
 752   }
 753 
 754   // 5. method lookup failed
 755   if (resolved_method.is_null()) {
 756     ResourceMark rm(THREAD);
 757     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 758                     Method::name_and_sig_as_C_string(resolved_klass,
 759                                                      link_info.name(),
 760                                                      link_info.signature()),
 761                     nested_exception, NULL);
 762   }
 763 
 764   // 6. access checks, access checking may be turned off when calling from within the VM.
 765   Klass* current_klass = link_info.current_klass();
 766   if (link_info.check_access()) {
 767     assert(current_klass != NULL , "current_klass should not be null");
 768 
 769     // check if method can be accessed by the referring class
 770     check_method_accessability(current_klass,
 771                                resolved_klass,
 772                                resolved_method->method_holder(),
 773                                resolved_method,
 774                                CHECK_NULL);
 775 
 776     // check loader constraints
 777     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 778   }
 779 
 780   // For private method invocation we should only find the method in the resolved class.
 781   // If that is not the case then we have a found a supertype method that we have nestmate
 782   // access to.
 783   if (resolved_method->is_private() && resolved_method->method_holder() != resolved_klass) {
 784     ResourceMark rm(THREAD);
 785     DEBUG_ONLY(bool is_nestmate = InstanceKlass::cast(link_info.current_klass())->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD);)
 786     assert(is_nestmate, "was only expecting nestmates to get here!");
 787     Exceptions::fthrow(
 788       THREAD_AND_LOCATION,
 789       vmSymbols::java_lang_NoSuchMethodError(),
 790       "%s: method %s%s not found",
 791       resolved_klass->external_name(),
 792       resolved_method->name()->as_C_string(),
 793       resolved_method->signature()->as_C_string()
 794     );
 795     return NULL;
 796   }
 797 
 798   return resolved_method;
 799 }
 800 
 801 static void trace_method_resolution(const char* prefix,
 802                                     Klass* klass,
 803                                     Klass* resolved_klass,
 804                                     const methodHandle& method,
 805                                     bool logitables,
 806                                     int index = -1) {
 807 #ifndef PRODUCT
 808   ResourceMark rm;
 809   Log(itables) logi;
 810   LogStream lsi(logi.trace());
 811   Log(vtables) logv;
 812   LogStream lsv(logv.trace());
 813   outputStream* st;
 814   if (logitables) {
 815     st = &lsi;
 816   } else {
 817     st = &lsv;


 879 
 880     // check if method can be accessed by the referring class
 881     check_method_accessability(current_klass,
 882                                resolved_klass,
 883                                resolved_method->method_holder(),
 884                                resolved_method,
 885                                CHECK_NULL);
 886 
 887     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 888   }
 889 
 890   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 891     ResourceMark rm(THREAD);
 892     char buf[200];
 893     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 894                  Method::name_and_sig_as_C_string(resolved_klass,
 895                  resolved_method->name(), resolved_method->signature()));
 896     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 897   }
 898 













 899   if (log_develop_is_enabled(Trace, itables)) {
 900     char buf[200];
 901     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 902                  Bytecodes::name(code));
 903     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
 904                             resolved_method, true);
 905   }
 906 
 907   return resolved_method;
 908 }
 909 
 910 //------------------------------------------------------------------------------------------------------------------------
 911 // Field resolution
 912 
 913 void LinkResolver::check_field_accessability(Klass* ref_klass,
 914                                              Klass* resolved_klass,
 915                                              Klass* sel_klass,
 916                                              const fieldDescriptor& fd,
 917                                              TRAPS) {
 918   bool can_access = Reflection::verify_member_access(ref_klass,
 919                                                      resolved_klass,
 920                                                      sel_klass,
 921                                                      fd.access_flags(),
 922                                                      true, false, CHECK);
 923   // Any existing exceptions that may have been thrown, for example LinkageErrors
 924   // from nest-host resolution, have been allowed to propagate.
 925   if (!can_access) {
 926     ResourceMark rm(THREAD);
 927     Exceptions::fthrow(
 928       THREAD_AND_LOCATION,
 929       vmSymbols::java_lang_IllegalAccessError(),
 930       "tried to access field %s.%s from class %s",
 931       sel_klass->external_name(),
 932       fd.name()->as_C_string(),
 933       ref_klass->external_name()
 934     );
 935     return;
 936   }
 937 }
 938 
 939 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 940   LinkInfo link_info(pool, index, method, CHECK);
 941   resolve_field(fd, link_info, byte, true, CHECK);
 942 }
 943 
 944 void LinkResolver::resolve_field(fieldDescriptor& fd,
 945                                  const LinkInfo& link_info,


1123     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1124   } else {
1125     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1126   }
1127 
1128   // check if method name is <init>, that it is found in same klass as static type
1129   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1130       resolved_method->method_holder() != resolved_klass) {
1131     ResourceMark rm(THREAD);
1132     Exceptions::fthrow(
1133       THREAD_AND_LOCATION,
1134       vmSymbols::java_lang_NoSuchMethodError(),
1135       "%s: method %s%s not found",
1136       resolved_klass->external_name(),
1137       resolved_method->name()->as_C_string(),
1138       resolved_method->signature()->as_C_string()
1139     );
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_anonymous() ?
1149                                     ck :
1150                                     InstanceKlass::cast(ck->host_klass());
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       char buf[200];
1159       jio_snprintf(buf, sizeof(buf),
1160                    "Interface method reference: %s, is in an indirect superinterface of %s",
1161                    Method::name_and_sig_as_C_string(resolved_klass,
1162                                                                            resolved_method->name(),
1163                                                                            resolved_method->signature()),
1164                    current_klass->external_name());


1196 
1197   // resolved method is selected method unless we have an old-style lookup
1198   // for a superclass method
1199   // Invokespecial for a superinterface, resolved method is selected method,
1200   // no checks for shadowing
1201   methodHandle sel_method(THREAD, resolved_method());
1202 
1203   if (link_info.check_access() &&
1204       // check if the method is not <init>
1205       resolved_method->name() != vmSymbols::object_initializer_name()) {
1206 
1207      // check if this is an old-style super call and do a new lookup if so
1208      // a) check if ACC_SUPER flag is set for the current class
1209     Klass* current_klass = link_info.current_klass();
1210     if ((current_klass->is_super() || !AllowNonVirtualCalls) &&
1211         // b) check if the class of the resolved_klass is a superclass
1212         // (not supertype in order to exclude interface classes) of the current class.
1213         // This check is not performed for super.invoke for interface methods
1214         // in super interfaces.
1215         current_klass->is_subclass_of(resolved_klass) &&
1216         current_klass != resolved_klass
1217         ) {
1218       // Lookup super method
1219       Klass* super_klass = current_klass->super();
1220       sel_method = lookup_instance_method_in_klasses(super_klass,
1221                                                      resolved_method->name(),
1222                                                      resolved_method->signature(),
1223                                                      Klass::find_private, CHECK);
1224       // check if found
1225       if (sel_method.is_null()) {
1226         ResourceMark rm(THREAD);
1227         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1228                   Method::name_and_sig_as_C_string(resolved_klass,
1229                                             resolved_method->name(),
1230                                             resolved_method->signature()));
1231       // check loader constraints if found a different method
1232       } else if (sel_method() != resolved_method()) {
1233         check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1234       }
1235     }
1236 
1237     // Check that the class of objectref (the receiver) is the current class or interface,
1238     // or a subtype of the current class or interface (the sender), otherwise invokespecial
1239     // throws IllegalAccessError.
1240     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1241     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1242     // a class.  If the sender is an interface, the check has to be performed at runtime.
1243     InstanceKlass* sender = InstanceKlass::cast(current_klass);


1354   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1355     THROW(vmSymbols::java_lang_NullPointerException());
1356   }
1357 
1358   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1359   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1360   // a missing receiver might result in a bogus lookup.
1361   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1362 
1363   // do lookup based on receiver klass using the vtable index
1364   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1365     vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1366     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1367 
1368     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1369   } else {
1370     // at this point we are sure that resolved_method is virtual and not
1371     // a default or miranda method; therefore, it must have a valid vtable index.
1372     assert(!resolved_method->has_itable_index(), "");
1373     vtable_index = resolved_method->vtable_index();
1374     // We could get a negative vtable_index of nonvirtual_vtable_index for private
1375     // methods, or for final methods. Private methods never appear in the vtable
1376     // and never override other methods. As an optimization, final methods are
1377     // never put in the vtable, unless they override an existing method.
1378     // So if we do get nonvirtual_vtable_index, it means the selected method is the
1379     // resolved method, and it can never be changed by an override.
1380     if (vtable_index == Method::nonvirtual_vtable_index) {
1381       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1382       selected_method = resolved_method;
1383     } else {
1384       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1385     }
1386   }
1387 
1388   // check if method exists
1389   if (selected_method.is_null()) {
1390     throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1391   }
1392 
1393   // check if abstract
1394   if (check_null_and_abstract && selected_method->is_abstract()) {
1395     // Pass arguments for generating a verbose error message.
1396     throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1397   }
1398 
1399   if (log_develop_is_enabled(Trace, vtables)) {


1414                                    recv, recv_klass, check_null_and_abstract, CHECK);
1415 }
1416 
1417 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1418                                                              TRAPS) {
1419   // normal interface method resolution
1420   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1421   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1422   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1423 
1424   return resolved_method;
1425 }
1426 
1427 // throws runtime exceptions
1428 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1429                                                     const methodHandle& resolved_method,
1430                                                     Klass* resolved_klass,
1431                                                     Handle recv,
1432                                                     Klass* recv_klass,
1433                                                     bool check_null_and_abstract, TRAPS) {
1434 
1435   // check if receiver exists
1436   if (check_null_and_abstract && recv.is_null()) {
1437     THROW(vmSymbols::java_lang_NullPointerException());
1438   }
1439 
1440   // check if receiver klass implements the resolved interface
1441   if (!recv_klass->is_subtype_of(resolved_klass)) {
1442     ResourceMark rm(THREAD);
1443     char buf[200];
1444     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1445                  recv_klass->external_name(),
1446                  resolved_klass->external_name());
1447     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1448   }
1449 
1450   methodHandle selected_method = resolved_method;
1451 
1452   // resolve the method in the receiver class, unless it is private
1453   if (!resolved_method()->is_private()) {
1454     // do lookup based on receiver klass
1455     // This search must match the linktime preparation search for itable initialization
1456     // to correctly enforce loader constraints for interface method inheritance.
1457     // Private methods are skipped as the resolved method was not private.
1458     selected_method = lookup_instance_method_in_klasses(recv_klass,
1459                                                         resolved_method->name(),
1460                                                         resolved_method->signature(),
1461                                                         Klass::skip_private, CHECK);
1462 
1463     if (selected_method.is_null() && !check_null_and_abstract) {
1464       // In theory this is a harmless placeholder value, but
1465       // in practice leaving in null affects the nsk default method tests.
1466       // This needs further study.
1467       selected_method = resolved_method;
1468     }
1469     // check if method exists
1470     if (selected_method.is_null()) {
1471       // Pass arguments for generating a verbose error message.
1472       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1473     }
1474     // check access
1475     // Throw Illegal Access Error if selected_method is not public.
1476     if (!selected_method->is_public()) {
1477       ResourceMark rm(THREAD);
1478       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1479                 Method::name_and_sig_as_C_string(recv_klass,
1480                                                  selected_method->name(),
1481                                                  selected_method->signature()));
1482     }
1483     // check if abstract
1484     if (check_null_and_abstract && selected_method->is_abstract()) {
1485       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1486     }
1487   }
1488 
1489   if (log_develop_is_enabled(Trace, itables)) {
1490     trace_method_resolution("invokeinterface selected method: receiver-class:",
1491                             recv_klass, resolved_klass, selected_method, true);
1492   }
1493   // setup result
1494   if (resolved_method->has_vtable_index()) {
1495     int vtable_index = resolved_method->vtable_index();
1496     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1497     assert(vtable_index == selected_method->vtable_index(), "sanity check");
1498     result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1499   } else if (resolved_method->has_itable_index()) {
1500     int itable_index = resolved_method()->itable_index();
1501     log_develop_trace(itables)("  -- itable index: %d", itable_index);
1502     result.set_interface(resolved_klass, recv_klass, resolved_method, selected_method, itable_index, CHECK);
1503   } else {
1504     int index = resolved_method->vtable_index();
1505     log_develop_trace(itables)("  -- non itable/vtable index: %d", index);
1506     assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1507     assert(resolved_method()->is_private() ||
1508            (resolved_method()->is_final() && resolved_method->method_holder() == SystemDictionary::Object_klass()),
1509            "Should only have non-virtual invokeinterface for private or final-Object methods!");
1510     assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1511     // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1512     result.set_virtual(resolved_klass, resolved_klass, resolved_method, resolved_method, index, CHECK);
1513   }
1514 }
1515 
1516 
1517 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1518                                                  const LinkInfo& link_info) {
1519   EXCEPTION_MARK;
1520   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1521   if (HAS_PENDING_EXCEPTION) {
1522     CLEAR_PENDING_EXCEPTION;
1523     return methodHandle();
1524   } else {
1525     return method_result;
1526   }
1527 }
1528 
1529 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1530                                                  const LinkInfo& link_info) {
1531   EXCEPTION_MARK;
1532   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);


< prev index next >