< prev index next >

src/hotspot/share/interpreter/linkResolver.cpp

Print this page

   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/archiveUtils.hpp"

  27 #include "classfile/defaultMethods.hpp"
  28 #include "classfile/javaClasses.hpp"
  29 #include "classfile/systemDictionary.hpp"
  30 #include "classfile/vmClasses.hpp"
  31 #include "classfile/vmSymbols.hpp"
  32 #include "compiler/compilationPolicy.hpp"
  33 #include "compiler/compileBroker.hpp"
  34 #include "gc/shared/collectedHeap.inline.hpp"
  35 #include "interpreter/bootstrapInfo.hpp"
  36 #include "interpreter/bytecode.hpp"
  37 #include "interpreter/interpreterRuntime.hpp"
  38 #include "interpreter/linkResolver.hpp"
  39 #include "jvm.h"
  40 #include "logging/log.hpp"
  41 #include "logging/logStream.hpp"
  42 #include "memory/resourceArea.hpp"
  43 #include "oops/constantPool.inline.hpp"
  44 #include "oops/cpCache.inline.hpp"
  45 #include "oops/instanceKlass.inline.hpp"
  46 #include "oops/klass.inline.hpp"
  47 #include "oops/method.inline.hpp"
  48 #include "oops/objArrayKlass.hpp"
  49 #include "oops/objArrayOop.hpp"
  50 #include "oops/oop.inline.hpp"
  51 #include "oops/resolvedIndyEntry.hpp"
  52 #include "oops/symbolHandle.hpp"
  53 #include "prims/methodHandles.hpp"
  54 #include "runtime/fieldDescriptor.inline.hpp"
  55 #include "runtime/frame.inline.hpp"
  56 #include "runtime/handles.inline.hpp"
  57 #include "runtime/javaThread.hpp"

  58 #include "runtime/reflection.hpp"
  59 #include "runtime/safepointVerifiers.hpp"
  60 #include "runtime/sharedRuntime.hpp"
  61 #include "runtime/signature.hpp"
  62 #include "runtime/vmThread.hpp"
  63 #include "utilities/macros.hpp"
  64 #if INCLUDE_JFR
  65 #include "jfr/jfr.hpp"
  66 #endif
  67 
  68 //------------------------------------------------------------------------------------------------------------------------
  69 // Implementation of CallInfo
  70 
  71 
  72 void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {
  73   int vtable_index = Method::nonvirtual_vtable_index;
  74   set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  75 }
  76 
  77 

 101 
 102 void CallInfo::set_handle(Klass* resolved_klass,
 103                           const methodHandle& resolved_method,
 104                           Handle resolved_appendix, TRAPS) {
 105   guarantee(resolved_method.not_null(), "resolved method is null");
 106   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
 107          resolved_method->is_compiled_lambda_form(),
 108          "linkMethod must return one of these");
 109   int vtable_index = Method::nonvirtual_vtable_index;
 110   assert(!resolved_method->has_vtable_index(), "");
 111   set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
 112   _resolved_appendix = resolved_appendix;
 113 }
 114 
 115 void CallInfo::set_common(Klass* resolved_klass,
 116                           const methodHandle& resolved_method,
 117                           const methodHandle& selected_method,
 118                           CallKind kind,
 119                           int index,
 120                           TRAPS) {
 121   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");


 122   _resolved_klass  = resolved_klass;
 123   _resolved_method = resolved_method;
 124   _selected_method = selected_method;
 125   _call_kind       = kind;
 126   _call_index      = index;
 127   _resolved_appendix = Handle();
 128   DEBUG_ONLY(verify());  // verify before making side effects
 129 
 130   CompilationPolicy::compile_if_required(selected_method, THREAD);


 131 }
 132 
 133 // utility query for unreflecting a method
 134 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
 135   Klass* resolved_method_holder = resolved_method->method_holder();
 136   if (resolved_klass == nullptr) { // 2nd argument defaults to holder of 1st
 137     resolved_klass = resolved_method_holder;
 138   }
 139   _resolved_klass  = resolved_klass;
 140   _resolved_method = methodHandle(THREAD, resolved_method);
 141   _selected_method = methodHandle(THREAD, resolved_method);
 142   // classify:
 143   CallKind kind = CallInfo::unknown_kind;
 144   int index = resolved_method->vtable_index();
 145   if (resolved_method->can_be_statically_bound()) {
 146     kind = CallInfo::direct_call;
 147   } else if (!resolved_method_holder->is_interface()) {
 148     // Could be an Object method inherited into an interface, but still a vtable call.
 149     kind = CallInfo::vtable_call;
 150   } else if (!resolved_klass->is_interface()) {

 933              sel_klass->external_name(),
 934              fd.name()->as_C_string(),
 935              (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 936              (same_module) ? "" : "; ",
 937              (same_module) ? "" : sel_klass->class_in_module_of_loader()
 938              );
 939     // For private access see if there was a problem with nest host
 940     // resolution, and if so report that as part of the message.
 941     if (fd.is_private()) {
 942       print_nest_host_error_on(&ss, ref_klass, sel_klass);
 943     }
 944     Exceptions::fthrow(THREAD_AND_LOCATION,
 945                        vmSymbols::java_lang_IllegalAccessError(),
 946                        "%s",
 947                        ss.as_string()
 948                        );
 949     return;
 950   }
 951 }
 952 
 953 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {





 954   LinkInfo link_info(pool, index, method, byte, CHECK);
 955   resolve_field(fd, link_info, byte, true, CHECK);
 956 }
 957 
 958 void LinkResolver::resolve_field(fieldDescriptor& fd,
 959                                  const LinkInfo& link_info,
 960                                  Bytecodes::Code byte, bool initialize_class,

 961                                  TRAPS) {
 962   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 963          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 964          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 965          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 966 
 967   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 968   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 969   // Check if there's a resolved klass containing the field
 970   Klass* resolved_klass = link_info.resolved_klass();
 971   Symbol* field = link_info.name();
 972   Symbol* sig = link_info.signature();
 973 
 974   // Resolve instance field
 975   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 976   // check if field exists; i.e., if a klass containing the field def has been selected
 977   if (sel_klass == nullptr) {
 978     ResourceMark rm(THREAD);
 979     stringStream ss;
 980     ss.print("Class %s does not have member field '", resolved_klass->external_name());

1073 
1074   // The resolved class can change as a result of this resolution.
1075   Klass* resolved_klass = resolved_method->method_holder();
1076 
1077   // Initialize klass (this should only happen if everything is ok)
1078   if (initialize_class && resolved_klass->should_be_initialized()) {
1079     resolved_klass->initialize(CHECK);
1080     // Use updated LinkInfo to reresolve with resolved method holder
1081     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1082                       link_info.current_klass(),
1083                       link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
1084                       link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
1085     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1086   }
1087 
1088   // setup result
1089   result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1090   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1091 }
1092 




1093 // throws linktime exceptions
1094 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1095 
1096   Klass* resolved_klass = link_info.resolved_klass();
1097   Method* resolved_method;
1098   if (!resolved_klass->is_interface()) {
1099     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1100   } else {
1101     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1102   }
1103   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1104 
1105   // check if static
1106   if (!resolved_method->is_static()) {
1107     ResourceMark rm(THREAD);
1108     stringStream ss;
1109     ss.print("Expected static method '");
1110     resolved_method->print_external_name(&ss);
1111     ss.print("'");
1112     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1113   }
1114   return resolved_method;
1115 }
1116 
1117 
1118 void LinkResolver::resolve_special_call(CallInfo& result,
1119                                         Handle recv,
1120                                         const LinkInfo& link_info,
1121                                         TRAPS) {
1122   Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);
1123   runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);
1124 }
1125 




1126 // throws linktime exceptions
1127 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1128 
1129   // Invokespecial is called for multiple special reasons:
1130   // <init>
1131   // local private method invocation, for classes and interfaces
1132   // superclass.method, which can also resolve to a default method
1133   // and the selected method is recalculated relative to the direct superclass
1134   // superinterface.method, which explicitly does not check shadowing
1135   Klass* resolved_klass = link_info.resolved_klass();
1136   Method* resolved_method = nullptr;
1137 
1138   if (!resolved_klass->is_interface()) {
1139     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1140   } else {
1141     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1142   }
1143 
1144   // check if method name is <init>, that it is found in same klass as static type
1145   if (resolved_method->name() == vmSymbols::object_initializer_name() &&

1287     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1288   }
1289 
1290   if (log_develop_is_enabled(Trace, itables)) {
1291     trace_method_resolution("invokespecial selected method: resolved-class:",
1292                             resolved_klass, resolved_klass, sel_method(), true);
1293   }
1294 
1295   // setup result
1296   result.set_static(resolved_klass, sel_method, CHECK);
1297   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1298 }
1299 
1300 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1301                                         const LinkInfo& link_info,
1302                                         bool check_null_and_abstract, TRAPS) {
1303   Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1304   runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1305                                  link_info.resolved_klass(),
1306                                  recv, receiver_klass,
1307                                  check_null_and_abstract, CHECK);










1308 }
1309 
1310 // throws linktime exceptions
1311 Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1312                                                            TRAPS) {
1313   // normal method resolution
1314   Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1315 
1316   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1317   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1318 
1319   // check if private interface method
1320   Klass* resolved_klass = link_info.resolved_klass();
1321   Klass* current_klass = link_info.current_klass();
1322 
1323   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1324   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1325     ResourceMark rm(THREAD);
1326     stringStream ss;
1327     ss.print("private interface method requires invokespecial, not invokevirtual: method '");

1339     resolved_method->print_external_name(&ss);
1340     ss.print("'");
1341     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1342   }
1343 
1344   if (log_develop_is_enabled(Trace, vtables)) {
1345     trace_method_resolution("invokevirtual resolved method: caller-class:",
1346                             current_klass, resolved_klass, resolved_method, false);
1347   }
1348 
1349   return resolved_method;
1350 }
1351 
1352 // throws runtime exceptions
1353 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1354                                                   const methodHandle& resolved_method,
1355                                                   Klass* resolved_klass,
1356                                                   Handle recv,
1357                                                   Klass* recv_klass,
1358                                                   bool check_null_and_abstract,

1359                                                   TRAPS) {
1360 
1361   // setup default return values
1362   int vtable_index = Method::invalid_vtable_index;
1363   methodHandle selected_method;
1364 
1365   // runtime method resolution
1366   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1367     THROW(vmSymbols::java_lang_NullPointerException());
1368   }
1369 
1370   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1371   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1372   // a missing receiver might result in a bogus lookup.
1373   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1374 
1375   // do lookup based on receiver klass using the vtable index
1376   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1377     vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1378     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1379 
1380     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));


1381   } else {
1382     // at this point we are sure that resolved_method is virtual and not
1383     // a default or miranda method; therefore, it must have a valid vtable index.
1384     assert(!resolved_method->has_itable_index(), "");
1385     vtable_index = resolved_method->vtable_index();
1386     // We could get a negative vtable_index of nonvirtual_vtable_index for private
1387     // methods, or for final methods. Private methods never appear in the vtable
1388     // and never override other methods. As an optimization, final methods are
1389     // never put in the vtable, unless they override an existing method.
1390     // So if we do get nonvirtual_vtable_index, it means the selected method is the
1391     // resolved method, and it can never be changed by an override.
1392     if (vtable_index == Method::nonvirtual_vtable_index) {
1393       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1394       selected_method = resolved_method;


1395     } else {
1396       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));


1397     }
1398   }
1399 
1400   // check if method exists
1401   if (selected_method.is_null()) {
1402     throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1403   }

1404 
1405   // check if abstract
1406   if (check_null_and_abstract && selected_method->is_abstract()) {
1407     // Pass arguments for generating a verbose error message.
1408     throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);

1409   }
1410 
1411   if (log_develop_is_enabled(Trace, vtables)) {
1412     trace_method_resolution("invokevirtual selected method: receiver-class:",
1413                             recv_klass, resolved_klass, selected_method(),
1414                             false, vtable_index);
1415   }
1416   // setup result
1417   result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1418   JFR_ONLY(Jfr::on_resolution(result, CHECK);)


1419 }
1420 
1421 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1422                                           const LinkInfo& link_info,
1423                                           bool check_null_and_abstract, TRAPS) {
1424   // throws linktime exceptions
1425   Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1426   methodHandle mh(THREAD, resolved_method);
1427   runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),
1428                                    recv, recv_klass, check_null_and_abstract, CHECK);









1429 }
1430 
1431 Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1432                                                              TRAPS) {
1433   // normal interface method resolution
1434   Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1435   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1436   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1437 
1438   return resolved_method;
1439 }
1440 
1441 // throws runtime exceptions
1442 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1443                                                     const methodHandle& resolved_method,
1444                                                     Klass* resolved_klass,
1445                                                     Handle recv,
1446                                                     Klass* recv_klass,
1447                                                     bool check_null_and_abstract, TRAPS) {

1448 
1449   // check if receiver exists
1450   if (check_null_and_abstract && recv.is_null()) {
1451     THROW(vmSymbols::java_lang_NullPointerException());
1452   }
1453 
1454   // check if receiver klass implements the resolved interface
1455   if (!recv_klass->is_subtype_of(resolved_klass)) {
1456     ResourceMark rm(THREAD);
1457     char buf[200];
1458     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1459                  recv_klass->external_name(),
1460                  resolved_klass->external_name());
1461     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1462   }
1463 
1464   methodHandle selected_method = resolved_method;




1465 
1466   // resolve the method in the receiver class, unless it is private
1467   if (!resolved_method()->is_private()) {
1468     // do lookup based on receiver klass
1469     // This search must match the linktime preparation search for itable initialization
1470     // to correctly enforce loader constraints for interface method inheritance.
1471     // Private methods are skipped as the resolved method was not private.
1472     Method* method = lookup_instance_method_in_klasses(recv_klass,
1473                                                        resolved_method->name(),
1474                                                        resolved_method->signature(),
1475                                                        Klass::PrivateLookupMode::skip);
1476     selected_method = methodHandle(THREAD, method);
1477 
1478     if (selected_method.is_null() && !check_null_and_abstract) {
1479       // In theory this is a harmless placeholder value, but
1480       // in practice leaving in null affects the nsk default method tests.
1481       // This needs further study.
1482       selected_method = resolved_method;
1483     }
1484     // check if method exists
1485     if (selected_method.is_null()) {
1486       // Pass arguments for generating a verbose error message.
1487       throw_abstract_method_error(resolved_method, recv_klass, CHECK);

1493       stringStream ss;
1494       ss.print("'");
1495       Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1496       ss.print("'");
1497       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1498     }
1499     // check if abstract
1500     if (check_null_and_abstract && selected_method->is_abstract()) {
1501       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1502     }
1503   }
1504 
1505   if (log_develop_is_enabled(Trace, itables)) {
1506     trace_method_resolution("invokeinterface selected method: receiver-class:",
1507                             recv_klass, resolved_klass, selected_method(), true);
1508   }
1509   // setup result
1510   if (resolved_method->has_vtable_index()) {
1511     int vtable_index = resolved_method->vtable_index();
1512     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1513     assert(vtable_index == selected_method->vtable_index(), "sanity check");
1514     result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1515   } else if (resolved_method->has_itable_index()) {
1516     int itable_index = resolved_method()->itable_index();
1517     log_develop_trace(itables)("  -- itable index: %d", itable_index);
1518     result.set_interface(resolved_klass, resolved_method, selected_method, itable_index, CHECK);
1519   } else {
1520     int index = resolved_method->vtable_index();
1521     log_develop_trace(itables)("  -- non itable/vtable index: %d", index);
1522     assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1523     assert(resolved_method()->is_private() ||
1524            (resolved_method()->is_final() && resolved_method->method_holder() == vmClasses::Object_klass()),
1525            "Should only have non-virtual invokeinterface for private or final-Object methods!");
1526     assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1527     // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1528     result.set_virtual(resolved_klass, resolved_method, resolved_method, index, CHECK);
1529   }
1530   JFR_ONLY(Jfr::on_resolution(result, CHECK);)


1531 }
1532 
1533 
1534 Method* LinkResolver::linktime_resolve_interface_method_or_null(
1535                                                  const LinkInfo& link_info) {
1536   EXCEPTION_MARK;
1537   Method* method_result = linktime_resolve_interface_method(link_info, THREAD);
1538   if (HAS_PENDING_EXCEPTION) {
1539     CLEAR_PENDING_EXCEPTION;
1540     return nullptr;
1541   } else {
1542     return method_result;
1543   }
1544 }
1545 
1546 Method* LinkResolver::linktime_resolve_virtual_method_or_null(
1547                                                  const LinkInfo& link_info) {
1548   EXCEPTION_MARK;
1549   Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);
1550   if (HAS_PENDING_EXCEPTION) {

1689   LinkInfo link_info(pool, index, Bytecodes::_invokeinterface, CHECK);
1690   Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass();
1691   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1692 }
1693 
1694 bool LinkResolver::resolve_previously_linked_invokehandle(CallInfo& result, const LinkInfo& link_info, const constantPoolHandle& pool, int index, TRAPS) {
1695   ResolvedMethodEntry* method_entry = pool->cache()->resolved_method_entry_at(index);
1696   if (method_entry->method() != nullptr) {
1697     Klass* resolved_klass = link_info.resolved_klass();
1698     methodHandle method(THREAD, method_entry->method());
1699     Handle     appendix(THREAD, pool->cache()->appendix_if_resolved(method_entry));
1700     result.set_handle(resolved_klass, method, appendix, CHECK_false);
1701     JFR_ONLY(Jfr::on_resolution(result, CHECK_false);)
1702     return true;
1703   } else {
1704     return false;
1705   }
1706 }
1707 
1708 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {




1709   LinkInfo link_info(pool, index, Bytecodes::_invokehandle, CHECK);




1710   if (log_is_enabled(Info, methodhandles)) {
1711     ResourceMark rm(THREAD);
1712     log_info(methodhandles)("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1713                             link_info.signature()->as_C_string());
1714   }
1715   { // Check if the call site has been bound already, and short circuit:
1716     bool is_done = resolve_previously_linked_invokehandle(result, link_info, pool, index, CHECK);
1717     if (is_done) return;
1718   }
1719   resolve_handle_call(result, link_info, CHECK);
1720 }
1721 
1722 void LinkResolver::resolve_handle_call(CallInfo& result,
1723                                        const LinkInfo& link_info,
1724                                        TRAPS) {
1725   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1726   Klass* resolved_klass = link_info.resolved_klass();
1727   assert(resolved_klass == vmClasses::MethodHandle_klass() ||
1728          resolved_klass == vmClasses::VarHandle_klass(), "");
1729   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1730   Handle resolved_appendix;
1731   Method* m = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1732   methodHandle resolved_method(THREAD, m);
1733 
1734   if (link_info.check_access()) {
1735     Symbol* name = link_info.name();
1736     vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
1737     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
1738       // Check if method can be accessed by the referring class.

1740       assert(iid == vmIntrinsicID::_invokeBasic, "%s", vmIntrinsics::name_at(iid));
1741 
1742       Klass* current_klass = link_info.current_klass();
1743       assert(current_klass != nullptr , "current_klass should not be null");
1744       check_method_accessability(current_klass,
1745                                  resolved_klass,
1746                                  resolved_method->method_holder(),
1747                                  resolved_method,
1748                                  CHECK);
1749     } else {
1750       // Java code is free to arbitrarily link signature-polymorphic invokers.
1751       assert(iid == vmIntrinsics::_invokeGeneric, "not an invoker: %s", vmIntrinsics::name_at(iid));
1752       assert(MethodHandles::is_signature_polymorphic_public_name(resolved_klass, name), "not public");
1753     }
1754   }
1755   result.set_handle(resolved_klass, resolved_method, resolved_appendix, CHECK);
1756   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1757 }
1758 
1759 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {




1760   int index = pool->decode_invokedynamic_index(indy_index);
1761   int pool_index = pool->resolved_indy_entry_at(index)->constant_pool_index();
1762 
1763   // Resolve the bootstrap specifier (BSM + optional arguments).
1764   BootstrapInfo bootstrap_specifier(pool, pool_index, index);
1765 
1766   // Check if CallSite has been bound already or failed already, and short circuit:
1767   {
1768     bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1769     if (is_done) return;
1770   }
1771 
1772   // The initial step in Call Site Specifier Resolution is to resolve the symbolic
1773   // reference to a method handle which will be the bootstrap method for a dynamic
1774   // call site.  If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1775   // method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1776   // method's CP index for the CONSTANT_MethodHandle_info.
1777   // Any subsequent invokedynamic instruction which shares
1778   // this bootstrap method will encounter the resolution of MethodHandleInError.
1779 

   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "cds/archiveUtils.hpp"
  27 #include "classfile/classLoader.hpp"
  28 #include "classfile/defaultMethods.hpp"
  29 #include "classfile/javaClasses.hpp"
  30 #include "classfile/systemDictionary.hpp"
  31 #include "classfile/vmClasses.hpp"
  32 #include "classfile/vmSymbols.hpp"
  33 #include "compiler/compilationPolicy.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "gc/shared/collectedHeap.inline.hpp"
  36 #include "interpreter/bootstrapInfo.hpp"
  37 #include "interpreter/bytecode.hpp"
  38 #include "interpreter/interpreterRuntime.hpp"
  39 #include "interpreter/linkResolver.hpp"
  40 #include "jvm.h"
  41 #include "logging/log.hpp"
  42 #include "logging/logStream.hpp"
  43 #include "memory/resourceArea.hpp"
  44 #include "oops/constantPool.inline.hpp"
  45 #include "oops/cpCache.inline.hpp"
  46 #include "oops/instanceKlass.inline.hpp"
  47 #include "oops/klass.inline.hpp"
  48 #include "oops/method.inline.hpp"
  49 #include "oops/objArrayKlass.hpp"
  50 #include "oops/objArrayOop.hpp"
  51 #include "oops/oop.inline.hpp"
  52 #include "oops/resolvedIndyEntry.hpp"
  53 #include "oops/symbolHandle.hpp"
  54 #include "prims/methodHandles.hpp"
  55 #include "runtime/fieldDescriptor.inline.hpp"
  56 #include "runtime/frame.inline.hpp"
  57 #include "runtime/handles.inline.hpp"
  58 #include "runtime/javaThread.inline.hpp"
  59 #include "runtime/perfData.hpp"
  60 #include "runtime/reflection.hpp"
  61 #include "runtime/safepointVerifiers.hpp"
  62 #include "runtime/sharedRuntime.hpp"
  63 #include "runtime/signature.hpp"
  64 #include "runtime/vmThread.hpp"
  65 #include "utilities/macros.hpp"
  66 #if INCLUDE_JFR
  67 #include "jfr/jfr.hpp"
  68 #endif
  69 
  70 //------------------------------------------------------------------------------------------------------------------------
  71 // Implementation of CallInfo
  72 
  73 
  74 void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {
  75   int vtable_index = Method::nonvirtual_vtable_index;
  76   set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  77 }
  78 
  79 

 103 
 104 void CallInfo::set_handle(Klass* resolved_klass,
 105                           const methodHandle& resolved_method,
 106                           Handle resolved_appendix, TRAPS) {
 107   guarantee(resolved_method.not_null(), "resolved method is null");
 108   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
 109          resolved_method->is_compiled_lambda_form(),
 110          "linkMethod must return one of these");
 111   int vtable_index = Method::nonvirtual_vtable_index;
 112   assert(!resolved_method->has_vtable_index(), "");
 113   set_common(resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
 114   _resolved_appendix = resolved_appendix;
 115 }
 116 
 117 void CallInfo::set_common(Klass* resolved_klass,
 118                           const methodHandle& resolved_method,
 119                           const methodHandle& selected_method,
 120                           CallKind kind,
 121                           int index,
 122                           TRAPS) {
 123   if (selected_method.not_null()) {
 124     assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
 125   }
 126   _resolved_klass  = resolved_klass;
 127   _resolved_method = resolved_method;
 128   _selected_method = selected_method;
 129   _call_kind       = kind;
 130   _call_index      = index;
 131   _resolved_appendix = Handle();
 132   DEBUG_ONLY(verify());  // verify before making side effects
 133 
 134   if (selected_method.not_null()) {
 135     CompilationPolicy::compile_if_required(selected_method, THREAD);
 136   }
 137 }
 138 
 139 // utility query for unreflecting a method
 140 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
 141   Klass* resolved_method_holder = resolved_method->method_holder();
 142   if (resolved_klass == nullptr) { // 2nd argument defaults to holder of 1st
 143     resolved_klass = resolved_method_holder;
 144   }
 145   _resolved_klass  = resolved_klass;
 146   _resolved_method = methodHandle(THREAD, resolved_method);
 147   _selected_method = methodHandle(THREAD, resolved_method);
 148   // classify:
 149   CallKind kind = CallInfo::unknown_kind;
 150   int index = resolved_method->vtable_index();
 151   if (resolved_method->can_be_statically_bound()) {
 152     kind = CallInfo::direct_call;
 153   } else if (!resolved_method_holder->is_interface()) {
 154     // Could be an Object method inherited into an interface, but still a vtable call.
 155     kind = CallInfo::vtable_call;
 156   } else if (!resolved_klass->is_interface()) {

 939              sel_klass->external_name(),
 940              fd.name()->as_C_string(),
 941              (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 942              (same_module) ? "" : "; ",
 943              (same_module) ? "" : sel_klass->class_in_module_of_loader()
 944              );
 945     // For private access see if there was a problem with nest host
 946     // resolution, and if so report that as part of the message.
 947     if (fd.is_private()) {
 948       print_nest_host_error_on(&ss, ref_klass, sel_klass);
 949     }
 950     Exceptions::fthrow(THREAD_AND_LOCATION,
 951                        vmSymbols::java_lang_IllegalAccessError(),
 952                        "%s",
 953                        ss.as_string()
 954                        );
 955     return;
 956   }
 957 }
 958 
 959 void LinkResolver::resolve_field_access(fieldDescriptor& fd,
 960                                         const constantPoolHandle& pool,
 961                                         int index,
 962                                         const methodHandle& method,
 963                                         Bytecodes::Code byte,
 964                                         bool initialize_class, TRAPS) {
 965   LinkInfo link_info(pool, index, method, byte, CHECK);
 966   resolve_field(fd, link_info, byte, initialize_class, CHECK);
 967 }
 968 
 969 void LinkResolver::resolve_field(fieldDescriptor& fd,
 970                                  const LinkInfo& link_info,
 971                                  Bytecodes::Code byte,
 972                                  bool initialize_class,
 973                                  TRAPS) {
 974   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 975          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 976          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 977          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 978 
 979   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 980   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 981   // Check if there's a resolved klass containing the field
 982   Klass* resolved_klass = link_info.resolved_klass();
 983   Symbol* field = link_info.name();
 984   Symbol* sig = link_info.signature();
 985 
 986   // Resolve instance field
 987   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 988   // check if field exists; i.e., if a klass containing the field def has been selected
 989   if (sel_klass == nullptr) {
 990     ResourceMark rm(THREAD);
 991     stringStream ss;
 992     ss.print("Class %s does not have member field '", resolved_klass->external_name());

1085 
1086   // The resolved class can change as a result of this resolution.
1087   Klass* resolved_klass = resolved_method->method_holder();
1088 
1089   // Initialize klass (this should only happen if everything is ok)
1090   if (initialize_class && resolved_klass->should_be_initialized()) {
1091     resolved_klass->initialize(CHECK);
1092     // Use updated LinkInfo to reresolve with resolved method holder
1093     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1094                       link_info.current_klass(),
1095                       link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
1096                       link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
1097     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1098   }
1099 
1100   // setup result
1101   result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1102   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1103 }
1104 
1105 void LinkResolver::cds_resolve_static_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1106   resolve_static_call(result, link_info, /*initialize_class*/false, CHECK);
1107 }
1108 
1109 // throws linktime exceptions
1110 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1111 
1112   Klass* resolved_klass = link_info.resolved_klass();
1113   Method* resolved_method;
1114   if (!resolved_klass->is_interface()) {
1115     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1116   } else {
1117     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1118   }
1119   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1120 
1121   // check if static
1122   if (!resolved_method->is_static()) {
1123     ResourceMark rm(THREAD);
1124     stringStream ss;
1125     ss.print("Expected static method '");
1126     resolved_method->print_external_name(&ss);
1127     ss.print("'");
1128     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1129   }
1130   return resolved_method;
1131 }
1132 
1133 
1134 void LinkResolver::resolve_special_call(CallInfo& result,
1135                                         Handle recv,
1136                                         const LinkInfo& link_info,
1137                                         TRAPS) {
1138   Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);
1139   runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);
1140 }
1141 
1142 void LinkResolver::cds_resolve_special_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1143   resolve_special_call(result, Handle(), link_info, CHECK);
1144 }
1145 
1146 // throws linktime exceptions
1147 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1148 
1149   // Invokespecial is called for multiple special reasons:
1150   // <init>
1151   // local private method invocation, for classes and interfaces
1152   // superclass.method, which can also resolve to a default method
1153   // and the selected method is recalculated relative to the direct superclass
1154   // superinterface.method, which explicitly does not check shadowing
1155   Klass* resolved_klass = link_info.resolved_klass();
1156   Method* resolved_method = nullptr;
1157 
1158   if (!resolved_klass->is_interface()) {
1159     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1160   } else {
1161     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1162   }
1163 
1164   // check if method name is <init>, that it is found in same klass as static type
1165   if (resolved_method->name() == vmSymbols::object_initializer_name() &&

1307     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1308   }
1309 
1310   if (log_develop_is_enabled(Trace, itables)) {
1311     trace_method_resolution("invokespecial selected method: resolved-class:",
1312                             resolved_klass, resolved_klass, sel_method(), true);
1313   }
1314 
1315   // setup result
1316   result.set_static(resolved_klass, sel_method, CHECK);
1317   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1318 }
1319 
1320 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1321                                         const LinkInfo& link_info,
1322                                         bool check_null_and_abstract, TRAPS) {
1323   Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1324   runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1325                                  link_info.resolved_klass(),
1326                                  recv, receiver_klass,
1327                                  check_null_and_abstract,
1328                                  /*need_selected_method*/true, CHECK);
1329 }
1330 
1331 void LinkResolver::cds_resolve_virtual_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1332   Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1333   runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1334                                  link_info.resolved_klass(),
1335                                  Handle(), nullptr,
1336                                  /*check_null_and_abstract*/false,
1337                                  /*need_selected_method*/false, CHECK);
1338 }
1339 
1340 // throws linktime exceptions
1341 Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1342                                                            TRAPS) {
1343   // normal method resolution
1344   Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1345 
1346   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1347   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1348 
1349   // check if private interface method
1350   Klass* resolved_klass = link_info.resolved_klass();
1351   Klass* current_klass = link_info.current_klass();
1352 
1353   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1354   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1355     ResourceMark rm(THREAD);
1356     stringStream ss;
1357     ss.print("private interface method requires invokespecial, not invokevirtual: method '");

1369     resolved_method->print_external_name(&ss);
1370     ss.print("'");
1371     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1372   }
1373 
1374   if (log_develop_is_enabled(Trace, vtables)) {
1375     trace_method_resolution("invokevirtual resolved method: caller-class:",
1376                             current_klass, resolved_klass, resolved_method, false);
1377   }
1378 
1379   return resolved_method;
1380 }
1381 
1382 // throws runtime exceptions
1383 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1384                                                   const methodHandle& resolved_method,
1385                                                   Klass* resolved_klass,
1386                                                   Handle recv,
1387                                                   Klass* recv_klass,
1388                                                   bool check_null_and_abstract,
1389                                                   bool need_selected_method,
1390                                                   TRAPS) {
1391 
1392   // setup default return values
1393   int vtable_index = Method::invalid_vtable_index;
1394   methodHandle selected_method;
1395 
1396   // runtime method resolution
1397   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1398     THROW(vmSymbols::java_lang_NullPointerException());
1399   }
1400 
1401   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1402   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1403   // a missing receiver might result in a bogus lookup.
1404   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1405 
1406   // do lookup based on receiver klass using the vtable index
1407   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1408     vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1409     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1410 
1411     if (need_selected_method) {
1412       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1413     }
1414   } else {
1415     // at this point we are sure that resolved_method is virtual and not
1416     // a default or miranda method; therefore, it must have a valid vtable index.
1417     assert(!resolved_method->has_itable_index(), "");
1418     vtable_index = resolved_method->vtable_index();
1419     // We could get a negative vtable_index of nonvirtual_vtable_index for private
1420     // methods, or for final methods. Private methods never appear in the vtable
1421     // and never override other methods. As an optimization, final methods are
1422     // never put in the vtable, unless they override an existing method.
1423     // So if we do get nonvirtual_vtable_index, it means the selected method is the
1424     // resolved method, and it can never be changed by an override.
1425     if (vtable_index == Method::nonvirtual_vtable_index) {
1426       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1427       if (need_selected_method) {
1428         selected_method = resolved_method;
1429       }
1430     } else {
1431       if (need_selected_method) {
1432         selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1433       }
1434     }
1435   }
1436 
1437   if (need_selected_method) {
1438     // check if method exists
1439     if (selected_method.is_null()) {
1440       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1441     }
1442 
1443     // check if abstract
1444     if (check_null_and_abstract && selected_method->is_abstract()) {
1445       // Pass arguments for generating a verbose error message.
1446       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1447     }
1448   }
1449 
1450   if (log_develop_is_enabled(Trace, vtables)) {
1451     trace_method_resolution("invokevirtual selected method: receiver-class:",
1452                             recv_klass, resolved_klass, selected_method(),
1453                             false, vtable_index);
1454   }
1455   // setup result
1456   result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1457   if (selected_method.not_null()) {
1458     JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1459   }
1460 }
1461 
1462 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1463                                           const LinkInfo& link_info,
1464                                           bool check_null_and_abstract, TRAPS) {
1465   // throws linktime exceptions
1466   Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1467   methodHandle mh(THREAD, resolved_method);
1468   runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),
1469                                    recv, recv_klass, check_null_and_abstract,
1470                                    /*need_selected_method*/ true, CHECK);
1471 }
1472 
1473 void LinkResolver::cds_resolve_interface_call(CallInfo& result, const LinkInfo& link_info, TRAPS) {
1474   Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1475   runtime_resolve_interface_method(result, methodHandle(THREAD, resolved_method), link_info.resolved_klass(),
1476                                    Handle(), nullptr,
1477                                    /*check_null_and_abstract*/ false,
1478                                    /*need_selected_method*/ false, CHECK);
1479 }
1480 
1481 Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1482                                                              TRAPS) {
1483   // normal interface method resolution
1484   Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1485   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1486   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1487 
1488   return resolved_method;
1489 }
1490 
1491 // throws runtime exceptions
1492 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1493                                                     const methodHandle& resolved_method,
1494                                                     Klass* resolved_klass,
1495                                                     Handle recv,
1496                                                     Klass* recv_klass,
1497                                                     bool check_null_and_abstract,
1498                                                     bool need_selected_method, TRAPS) {
1499 
1500   // check if receiver exists
1501   if (check_null_and_abstract && recv.is_null()) {
1502     THROW(vmSymbols::java_lang_NullPointerException());
1503   }
1504 
1505   // check if receiver klass implements the resolved interface
1506   if (need_selected_method && !recv_klass->is_subtype_of(resolved_klass)) {
1507     ResourceMark rm(THREAD);
1508     char buf[200];
1509     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1510                  recv_klass->external_name(),
1511                  resolved_klass->external_name());
1512     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1513   }
1514 
1515   methodHandle selected_method;
1516 
1517   if (need_selected_method) {
1518     selected_method = resolved_method;
1519   }
1520 
1521   // resolve the method in the receiver class, unless it is private
1522   if (need_selected_method && !resolved_method()->is_private()) {
1523     // do lookup based on receiver klass
1524     // This search must match the linktime preparation search for itable initialization
1525     // to correctly enforce loader constraints for interface method inheritance.
1526     // Private methods are skipped as the resolved method was not private.
1527     Method* method = lookup_instance_method_in_klasses(recv_klass,
1528                                                        resolved_method->name(),
1529                                                        resolved_method->signature(),
1530                                                        Klass::PrivateLookupMode::skip);
1531     selected_method = methodHandle(THREAD, method);
1532 
1533     if (selected_method.is_null() && !check_null_and_abstract) {
1534       // In theory this is a harmless placeholder value, but
1535       // in practice leaving in null affects the nsk default method tests.
1536       // This needs further study.
1537       selected_method = resolved_method;
1538     }
1539     // check if method exists
1540     if (selected_method.is_null()) {
1541       // Pass arguments for generating a verbose error message.
1542       throw_abstract_method_error(resolved_method, recv_klass, CHECK);

1548       stringStream ss;
1549       ss.print("'");
1550       Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1551       ss.print("'");
1552       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1553     }
1554     // check if abstract
1555     if (check_null_and_abstract && selected_method->is_abstract()) {
1556       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1557     }
1558   }
1559 
1560   if (log_develop_is_enabled(Trace, itables)) {
1561     trace_method_resolution("invokeinterface selected method: receiver-class:",
1562                             recv_klass, resolved_klass, selected_method(), true);
1563   }
1564   // setup result
1565   if (resolved_method->has_vtable_index()) {
1566     int vtable_index = resolved_method->vtable_index();
1567     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1568     assert(!need_selected_method || vtable_index == selected_method->vtable_index(), "sanity check");
1569     result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1570   } else if (resolved_method->has_itable_index()) {
1571     int itable_index = resolved_method()->itable_index();
1572     log_develop_trace(itables)("  -- itable index: %d", itable_index);
1573     result.set_interface(resolved_klass, resolved_method, selected_method, itable_index, CHECK);
1574   } else {
1575     int index = resolved_method->vtable_index();
1576     log_develop_trace(itables)("  -- non itable/vtable index: %d", index);
1577     assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1578     assert(resolved_method()->is_private() ||
1579            (resolved_method()->is_final() && resolved_method->method_holder() == vmClasses::Object_klass()),
1580            "Should only have non-virtual invokeinterface for private or final-Object methods!");
1581     assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1582     // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1583     result.set_virtual(resolved_klass, resolved_method, resolved_method, index, CHECK);
1584   }
1585   if (need_selected_method) {
1586     JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1587   }
1588 }
1589 
1590 
1591 Method* LinkResolver::linktime_resolve_interface_method_or_null(
1592                                                  const LinkInfo& link_info) {
1593   EXCEPTION_MARK;
1594   Method* method_result = linktime_resolve_interface_method(link_info, THREAD);
1595   if (HAS_PENDING_EXCEPTION) {
1596     CLEAR_PENDING_EXCEPTION;
1597     return nullptr;
1598   } else {
1599     return method_result;
1600   }
1601 }
1602 
1603 Method* LinkResolver::linktime_resolve_virtual_method_or_null(
1604                                                  const LinkInfo& link_info) {
1605   EXCEPTION_MARK;
1606   Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);
1607   if (HAS_PENDING_EXCEPTION) {

1746   LinkInfo link_info(pool, index, Bytecodes::_invokeinterface, CHECK);
1747   Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass();
1748   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1749 }
1750 
1751 bool LinkResolver::resolve_previously_linked_invokehandle(CallInfo& result, const LinkInfo& link_info, const constantPoolHandle& pool, int index, TRAPS) {
1752   ResolvedMethodEntry* method_entry = pool->cache()->resolved_method_entry_at(index);
1753   if (method_entry->method() != nullptr) {
1754     Klass* resolved_klass = link_info.resolved_klass();
1755     methodHandle method(THREAD, method_entry->method());
1756     Handle     appendix(THREAD, pool->cache()->appendix_if_resolved(method_entry));
1757     result.set_handle(resolved_klass, method, appendix, CHECK_false);
1758     JFR_ONLY(Jfr::on_resolution(result, CHECK_false);)
1759     return true;
1760   } else {
1761     return false;
1762   }
1763 }
1764 
1765 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1766   PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokehandle_time(),
1767                             ClassLoader::perf_resolve_invokehandle_count(),
1768                             THREAD->class_being_initialized() == nullptr);
1769 
1770   LinkInfo link_info(pool, index, Bytecodes::_invokehandle, CHECK);
1771   { // Check if the call site has been bound already, and short circuit:
1772     bool is_done = resolve_previously_linked_invokehandle(result, link_info, pool, index, CHECK);
1773     if (is_done) return;
1774   }
1775   if (log_is_enabled(Info, methodhandles)) {
1776     ResourceMark rm(THREAD);
1777     log_info(methodhandles)("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1778                             link_info.signature()->as_C_string());
1779   }




1780   resolve_handle_call(result, link_info, CHECK);
1781 }
1782 
1783 void LinkResolver::resolve_handle_call(CallInfo& result,
1784                                        const LinkInfo& link_info,
1785                                        TRAPS) {
1786   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1787   Klass* resolved_klass = link_info.resolved_klass();
1788   assert(resolved_klass == vmClasses::MethodHandle_klass() ||
1789          resolved_klass == vmClasses::VarHandle_klass(), "");
1790   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1791   Handle resolved_appendix;
1792   Method* m = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1793   methodHandle resolved_method(THREAD, m);
1794 
1795   if (link_info.check_access()) {
1796     Symbol* name = link_info.name();
1797     vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
1798     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
1799       // Check if method can be accessed by the referring class.

1801       assert(iid == vmIntrinsicID::_invokeBasic, "%s", vmIntrinsics::name_at(iid));
1802 
1803       Klass* current_klass = link_info.current_klass();
1804       assert(current_klass != nullptr , "current_klass should not be null");
1805       check_method_accessability(current_klass,
1806                                  resolved_klass,
1807                                  resolved_method->method_holder(),
1808                                  resolved_method,
1809                                  CHECK);
1810     } else {
1811       // Java code is free to arbitrarily link signature-polymorphic invokers.
1812       assert(iid == vmIntrinsics::_invokeGeneric, "not an invoker: %s", vmIntrinsics::name_at(iid));
1813       assert(MethodHandles::is_signature_polymorphic_public_name(resolved_klass, name), "not public");
1814     }
1815   }
1816   result.set_handle(resolved_klass, resolved_method, resolved_appendix, CHECK);
1817   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1818 }
1819 
1820 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {
1821   PerfTraceTimedEvent timer(ClassLoader::perf_resolve_invokedynamic_time(),
1822                             ClassLoader::perf_resolve_invokedynamic_count(),
1823                             THREAD->class_being_initialized() == nullptr);
1824 
1825   int index = pool->decode_invokedynamic_index(indy_index);
1826   int pool_index = pool->resolved_indy_entry_at(index)->constant_pool_index();
1827 
1828   // Resolve the bootstrap specifier (BSM + optional arguments).
1829   BootstrapInfo bootstrap_specifier(pool, pool_index, index);
1830 
1831   // Check if CallSite has been bound already or failed already, and short circuit:
1832   {
1833     bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1834     if (is_done) return;
1835   }
1836 
1837   // The initial step in Call Site Specifier Resolution is to resolve the symbolic
1838   // reference to a method handle which will be the bootstrap method for a dynamic
1839   // call site.  If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1840   // method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1841   // method's CP index for the CONSTANT_MethodHandle_info.
1842   // Any subsequent invokedynamic instruction which shares
1843   // this bootstrap method will encounter the resolution of MethodHandleInError.
1844 
< prev index next >