src/hotspot/share/interpreter/linkResolver.cpp
changeset 50735 2f2af62dfac7
parent 50325 462453f3c7f6
child 50761 cb07f4b539fc
equal deleted inserted replaced
50734:0828a0f6676b 50735:2f2af62dfac7
   385 
   385 
   386 // returns first instance method
   386 // returns first instance method
   387 // Looks up method in classes, then looks up local default methods
   387 // Looks up method in classes, then looks up local default methods
   388 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
   388 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
   389                                                              Symbol* name,
   389                                                              Symbol* name,
   390                                                              Symbol* signature, TRAPS) {
   390                                                              Symbol* signature,
   391   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
   391                                                              Klass::PrivateLookupMode private_mode, TRAPS) {
       
   392   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
   392 
   393 
   393   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
   394   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
   394     Klass* super_klass = result->method_holder()->super();
   395     Klass* super_klass = result->method_holder()->super();
   395     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
   396     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
   396   }
   397   }
   397 
   398 
   398   if (klass->is_array_klass()) {
   399   if (klass->is_array_klass()) {
   399     // Only consider klass and super klass for arrays
   400     // Only consider klass and super klass for arrays
   400     return methodHandle(THREAD, result);
   401     return methodHandle(THREAD, result);
   580     new_flags = new_flags | JVM_ACC_PUBLIC;
   581     new_flags = new_flags | JVM_ACC_PUBLIC;
   581     flags.set_flags(new_flags);
   582     flags.set_flags(new_flags);
   582   }
   583   }
   583 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
   584 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
   584 
   585 
   585   if (!Reflection::verify_field_access(ref_klass,
   586   bool can_access = Reflection::verify_member_access(ref_klass,
   586                                        resolved_klass,
   587                                                      resolved_klass,
   587                                        sel_klass,
   588                                                      sel_klass,
   588                                        flags,
   589                                                      flags,
   589                                        true)) {
   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) {
   590     ResourceMark rm(THREAD);
   594     ResourceMark rm(THREAD);
   591     Exceptions::fthrow(
   595     Exceptions::fthrow(
   592       THREAD_AND_LOCATION,
   596       THREAD_AND_LOCATION,
   593       vmSymbols::java_lang_IllegalAccessError(),
   597       vmSymbols::java_lang_IllegalAccessError(),
   594       "tried to access method %s.%s%s from class %s",
   598       "tried to access method %s.%s%s from class %s",
   755                                                      link_info.name(),
   759                                                      link_info.name(),
   756                                                      link_info.signature()),
   760                                                      link_info.signature()),
   757                     nested_exception, NULL);
   761                     nested_exception, NULL);
   758   }
   762   }
   759 
   763 
   760   // 5. access checks, access checking may be turned off when calling from within the VM.
   764   // 6. access checks, access checking may be turned off when calling from within the VM.
   761   Klass* current_klass = link_info.current_klass();
   765   Klass* current_klass = link_info.current_klass();
   762   if (link_info.check_access()) {
   766   if (link_info.check_access()) {
   763     assert(current_klass != NULL , "current_klass should not be null");
   767     assert(current_klass != NULL , "current_klass should not be null");
   764 
   768 
   765     // check if method can be accessed by the referring class
   769     // check if method can be accessed by the referring class
   769                                resolved_method,
   773                                resolved_method,
   770                                CHECK_NULL);
   774                                CHECK_NULL);
   771 
   775 
   772     // check loader constraints
   776     // check loader constraints
   773     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
   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;
   774   }
   796   }
   775 
   797 
   776   return resolved_method;
   798   return resolved_method;
   777 }
   799 }
   778 
   800 
   872                  Method::name_and_sig_as_C_string(resolved_klass,
   894                  Method::name_and_sig_as_C_string(resolved_klass,
   873                  resolved_method->name(), resolved_method->signature()));
   895                  resolved_method->name(), resolved_method->signature()));
   874     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   896     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   875   }
   897   }
   876 
   898 
   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)) {
   899   if (log_develop_is_enabled(Trace, itables)) {
   891     char buf[200];
   900     char buf[200];
   892     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
   901     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
   893                  Bytecodes::name(code));
   902                  Bytecodes::name(code));
   894     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
   903     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
   904 void LinkResolver::check_field_accessability(Klass* ref_klass,
   913 void LinkResolver::check_field_accessability(Klass* ref_klass,
   905                                              Klass* resolved_klass,
   914                                              Klass* resolved_klass,
   906                                              Klass* sel_klass,
   915                                              Klass* sel_klass,
   907                                              const fieldDescriptor& fd,
   916                                              const fieldDescriptor& fd,
   908                                              TRAPS) {
   917                                              TRAPS) {
   909   if (!Reflection::verify_field_access(ref_klass,
   918   bool can_access = Reflection::verify_member_access(ref_klass,
   910                                        resolved_klass,
   919                                                      resolved_klass,
   911                                        sel_klass,
   920                                                      sel_klass,
   912                                        fd.access_flags(),
   921                                                      fd.access_flags(),
   913                                        true)) {
   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) {
   914     ResourceMark rm(THREAD);
   926     ResourceMark rm(THREAD);
   915     Exceptions::fthrow(
   927     Exceptions::fthrow(
   916       THREAD_AND_LOCATION,
   928       THREAD_AND_LOCATION,
   917       vmSymbols::java_lang_IllegalAccessError(),
   929       vmSymbols::java_lang_IllegalAccessError(),
   918       "tried to access field %s.%s from class %s",
   930       "tried to access field %s.%s from class %s",
  1126       resolved_method->signature()->as_C_string()
  1138       resolved_method->signature()->as_C_string()
  1127     );
  1139     );
  1128     return NULL;
  1140     return NULL;
  1129   }
  1141   }
  1130 
  1142 
  1131   // check if invokespecial's interface method reference is in an indirect superinterface
  1143   // ensure that invokespecial's interface method reference is in
       
  1144   // a direct superinterface, not an indirect superinterface
  1132   Klass* current_klass = link_info.current_klass();
  1145   Klass* current_klass = link_info.current_klass();
  1133   if (current_klass != NULL && resolved_klass->is_interface()) {
  1146   if (current_klass != NULL && resolved_klass->is_interface()) {
  1134     InstanceKlass* ck = InstanceKlass::cast(current_klass);
  1147     InstanceKlass* ck = InstanceKlass::cast(current_klass);
  1135     InstanceKlass *klass_to_check = !ck->is_anonymous() ?
  1148     InstanceKlass *klass_to_check = !ck->is_anonymous() ?
  1136                                     ck :
  1149                                     ck :
  1144       ResourceMark rm(THREAD);
  1157       ResourceMark rm(THREAD);
  1145       char buf[200];
  1158       char buf[200];
  1146       jio_snprintf(buf, sizeof(buf),
  1159       jio_snprintf(buf, sizeof(buf),
  1147                    "Interface method reference: %s, is in an indirect superinterface of %s",
  1160                    "Interface method reference: %s, is in an indirect superinterface of %s",
  1148                    Method::name_and_sig_as_C_string(resolved_klass,
  1161                    Method::name_and_sig_as_C_string(resolved_klass,
  1149                                                     resolved_method->name(),
  1162                                                                            resolved_method->name(),
  1150                                                     resolved_method->signature()),
  1163                                                                            resolved_method->signature()),
  1151                    current_klass->external_name());
  1164                    current_klass->external_name());
  1152       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1165       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1153     }
  1166     }
  1154   }
  1167   }
  1155 
  1168 
  1190   if (link_info.check_access() &&
  1203   if (link_info.check_access() &&
  1191       // check if the method is not <init>
  1204       // check if the method is not <init>
  1192       resolved_method->name() != vmSymbols::object_initializer_name()) {
  1205       resolved_method->name() != vmSymbols::object_initializer_name()) {
  1193 
  1206 
  1194      // check if this is an old-style super call and do a new lookup if so
  1207      // 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
  1208      // a) check if ACC_SUPER flag is set for the current class
  1196     Klass* current_klass = link_info.current_klass();
  1209     Klass* current_klass = link_info.current_klass();
  1197     if ((current_klass->is_super() || !AllowNonVirtualCalls) &&
  1210     if ((current_klass->is_super() || !AllowNonVirtualCalls) &&
  1198         // b) check if the class of the resolved_klass is a superclass
  1211         // 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.
  1212         // (not supertype in order to exclude interface classes) of the current class.
  1200         // This check is not performed for super.invoke for interface methods
  1213         // This check is not performed for super.invoke for interface methods
  1201         // in super interfaces.
  1214         // in super interfaces.
  1202         current_klass->is_subclass_of(resolved_klass) &&
  1215         current_klass->is_subclass_of(resolved_klass) &&
  1203         current_klass != resolved_klass) {
  1216         current_klass != resolved_klass
       
  1217         ) {
  1204       // Lookup super method
  1218       // Lookup super method
  1205       Klass* super_klass = current_klass->super();
  1219       Klass* super_klass = current_klass->super();
  1206       sel_method = lookup_instance_method_in_klasses(super_klass,
  1220       sel_method = lookup_instance_method_in_klasses(super_klass,
  1207                            resolved_method->name(),
  1221                                                      resolved_method->name(),
  1208                            resolved_method->signature(), CHECK);
  1222                                                      resolved_method->signature(),
       
  1223                                                      Klass::find_private, CHECK);
  1209       // check if found
  1224       // check if found
  1210       if (sel_method.is_null()) {
  1225       if (sel_method.is_null()) {
  1211         ResourceMark rm(THREAD);
  1226         ResourceMark rm(THREAD);
  1212         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1227         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1213                   Method::name_and_sig_as_C_string(resolved_klass,
  1228                   Method::name_and_sig_as_C_string(resolved_klass,
  1354   } else {
  1369   } else {
  1355     // at this point we are sure that resolved_method is virtual and not
  1370     // 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.
  1371     // a default or miranda method; therefore, it must have a valid vtable index.
  1357     assert(!resolved_method->has_itable_index(), "");
  1372     assert(!resolved_method->has_itable_index(), "");
  1358     vtable_index = resolved_method->vtable_index();
  1373     vtable_index = resolved_method->vtable_index();
  1359     // We could get a negative vtable_index for final methods,
  1374     // We could get a negative vtable_index of nonvirtual_vtable_index for private
  1360     // because as an optimization they are never put in the vtable,
  1375     // methods, or for final methods. Private methods never appear in the vtable
  1361     // unless they override an existing method.
  1376     // and never override other methods. As an optimization, final methods are
  1362     // If we do get a negative, it means the resolved method is the the selected
  1377     // never put in the vtable, unless they override an existing method.
  1363     // method, and it can never be changed by an override.
  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.
  1364     if (vtable_index == Method::nonvirtual_vtable_index) {
  1380     if (vtable_index == Method::nonvirtual_vtable_index) {
  1365       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
  1381       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
  1366       selected_method = resolved_method;
  1382       selected_method = resolved_method;
  1367     } else {
  1383     } else {
  1368       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
  1384       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
  1413                                                     const methodHandle& resolved_method,
  1429                                                     const methodHandle& resolved_method,
  1414                                                     Klass* resolved_klass,
  1430                                                     Klass* resolved_klass,
  1415                                                     Handle recv,
  1431                                                     Handle recv,
  1416                                                     Klass* recv_klass,
  1432                                                     Klass* recv_klass,
  1417                                                     bool check_null_and_abstract, TRAPS) {
  1433                                                     bool check_null_and_abstract, TRAPS) {
       
  1434 
  1418   // check if receiver exists
  1435   // check if receiver exists
  1419   if (check_null_and_abstract && recv.is_null()) {
  1436   if (check_null_and_abstract && recv.is_null()) {
  1420     THROW(vmSymbols::java_lang_NullPointerException());
  1437     THROW(vmSymbols::java_lang_NullPointerException());
  1421   }
  1438   }
  1422 
  1439 
  1428                  recv_klass->external_name(),
  1445                  recv_klass->external_name(),
  1429                  resolved_klass->external_name());
  1446                  resolved_klass->external_name());
  1430     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1447     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1431   }
  1448   }
  1432 
  1449 
  1433   // do lookup based on receiver klass
  1450   methodHandle selected_method = resolved_method;
  1434   // This search must match the linktime preparation search for itable initialization
  1451 
  1435   // to correctly enforce loader constraints for interface method inheritance
  1452   // resolve the method in the receiver class, unless it is private
  1436   methodHandle selected_method = lookup_instance_method_in_klasses(recv_klass,
  1453   if (!resolved_method()->is_private()) {
  1437                                                   resolved_method->name(),
  1454     // do lookup based on receiver klass
  1438                                                   resolved_method->signature(), CHECK);
  1455     // This search must match the linktime preparation search for itable initialization
  1439   if (selected_method.is_null() && !check_null_and_abstract) {
  1456     // to correctly enforce loader constraints for interface method inheritance.
  1440     // In theory this is a harmless placeholder value, but
  1457     // Private methods are skipped as the resolved method was not private.
  1441     // in practice leaving in null affects the nsk default method tests.
  1458     selected_method = lookup_instance_method_in_klasses(recv_klass,
  1442     // This needs further study.
  1459                                                         resolved_method->name(),
  1443     selected_method = resolved_method;
  1460                                                         resolved_method->signature(),
  1444   }
  1461                                                         Klass::skip_private, CHECK);
  1445   // check if method exists
  1462 
  1446   if (selected_method.is_null()) {
  1463     if (selected_method.is_null() && !check_null_and_abstract) {
  1447     // Pass arguments for generating a verbose error message.
  1464       // In theory this is a harmless placeholder value, but
  1448     throw_abstract_method_error(resolved_method, recv_klass, CHECK);
  1465       // in practice leaving in null affects the nsk default method tests.
  1449   }
  1466       // This needs further study.
  1450   // check access
  1467       selected_method = resolved_method;
  1451   // Throw Illegal Access Error if selected_method is not public.
  1468     }
  1452   if (!selected_method->is_public()) {
  1469     // check if method exists
  1453     ResourceMark rm(THREAD);
  1470     if (selected_method.is_null()) {
  1454     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
  1471       // Pass arguments for generating a verbose error message.
  1455               Method::name_and_sig_as_C_string(recv_klass,
  1472       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
  1456                                                selected_method->name(),
  1473     }
  1457                                                selected_method->signature()));
  1474     // check access
  1458   }
  1475     // Throw Illegal Access Error if selected_method is not public.
  1459   // check if abstract
  1476     if (!selected_method->is_public()) {
  1460   if (check_null_and_abstract && selected_method->is_abstract()) {
  1477       ResourceMark rm(THREAD);
  1461     throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
  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     }
  1462   }
  1487   }
  1463 
  1488 
  1464   if (log_develop_is_enabled(Trace, itables)) {
  1489   if (log_develop_is_enabled(Trace, itables)) {
  1465     trace_method_resolution("invokeinterface selected method: receiver-class:",
  1490     trace_method_resolution("invokeinterface selected method: receiver-class:",
  1466                             recv_klass, resolved_klass, selected_method, true);
  1491                             recv_klass, resolved_klass, selected_method, true);
  1467   }
  1492   }
  1468   // setup result
  1493   // setup result
  1469   if (!resolved_method->has_itable_index()) {
  1494   if (resolved_method->has_vtable_index()) {
  1470     int vtable_index = resolved_method->vtable_index();
  1495     int vtable_index = resolved_method->vtable_index();
       
  1496     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
  1471     assert(vtable_index == selected_method->vtable_index(), "sanity check");
  1497     assert(vtable_index == selected_method->vtable_index(), "sanity check");
  1472     result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, 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);
  1473   } else {
  1503   } else {
  1474     int itable_index = resolved_method()->itable_index();
  1504     int index = resolved_method->vtable_index();
  1475     result.set_interface(resolved_klass, recv_klass, resolved_method, selected_method, itable_index, CHECK);
  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);
  1476   }
  1513   }
  1477 }
  1514 }
  1478 
  1515 
  1479 
  1516 
  1480 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
  1517 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(