< prev index next >

src/hotspot/share/interpreter/linkResolver.cpp

Print this page

 969     // For private access see if there was a problem with nest host
 970     // resolution, and if so report that as part of the message.
 971     if (fd.is_private()) {
 972       print_nest_host_error_on(&ss, ref_klass, sel_klass);
 973     }
 974     // Names are all known to be < 64k so we know this formatted message is not excessively large.
 975     Exceptions::fthrow(THREAD_AND_LOCATION,
 976                        vmSymbols::java_lang_IllegalAccessError(),
 977                        "%s",
 978                        ss.as_string()
 979                        );
 980     return;
 981   }
 982 }
 983 
 984 void LinkResolver::resolve_field_access(fieldDescriptor& fd,
 985                                         const constantPoolHandle& pool,
 986                                         int index,
 987                                         const methodHandle& method,
 988                                         Bytecodes::Code byte,
 989                                         bool initialize_class, TRAPS) {
 990   LinkInfo link_info(pool, index, method, byte, CHECK);
 991   resolve_field(fd, link_info, byte, initialize_class, CHECK);
 992 }
 993 
 994 void LinkResolver::resolve_field(fieldDescriptor& fd,
 995                                  const LinkInfo& link_info,
 996                                  Bytecodes::Code byte, bool initialize_class,
 997                                  TRAPS) {
 998   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 999          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
1000          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
1001          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
1002 
1003   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
1004   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
1005   // Check if there's a resolved klass containing the field
1006   Klass* resolved_klass = link_info.resolved_klass();
1007   Symbol* field = link_info.name();
1008   Symbol* sig = link_info.signature();
1009 
1010   // Resolve instance field
1011   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
1012   // check if field exists; i.e., if a klass containing the field def has been selected
1013   if (sel_klass == nullptr) {
1014     ResourceMark rm(THREAD);
1015     stringStream ss;
1016     ss.print("Class %s does not have member field '", resolved_klass->external_name());

1060                                                      !m->is_object_initializer());
1061 
1062         if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1063           ResourceMark rm(THREAD);
1064           stringStream ss;
1065           ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1066                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1067                    m->name()->as_C_string(),
1068                    is_static ? "<clinit>" : "<init>");
1069           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1070         }
1071       }
1072     }
1073 
1074     // initialize resolved_klass if necessary
1075     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1076     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1077     //
1078     // note 2: we don't want to force initialization if we are just checking
1079     //         if the field access is legal; e.g., during compilation
1080     if (is_static && initialize_class) {
1081       sel_klass->initialize(CHECK);




1082     }
1083   }
1084 
1085   if (link_info.check_loader_constraints() && (sel_klass != current_klass) && (current_klass != nullptr)) {
1086     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1087   }
1088 
1089   // return information. note that the klass is set to the actual klass containing the
1090   // field, otherwise access of static fields in superclasses will not work.
1091 }
1092 
1093 
1094 //------------------------------------------------------------------------------------------------------------------------
1095 // Invoke resolution
1096 //
1097 // Naming conventions:
1098 //
1099 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1100 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1101 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1102 // recv_klass         the receiver klass
1103 
1104 
1105 void LinkResolver::resolve_static_call(CallInfo& result,
1106                                        const LinkInfo& link_info,
1107                                        bool initialize_class, TRAPS) {
1108   Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);
1109 
1110   // The resolved class can change as a result of this resolution.
1111   Klass* resolved_klass = resolved_method->method_holder();
1112 
1113   // Initialize klass (this should only happen if everything is ok)
1114   if (initialize_class && resolved_klass->should_be_initialized()) {
1115     resolved_klass->initialize(CHECK);




1116     // Use updated LinkInfo to reresolve with resolved method holder
1117     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1118                       link_info.current_klass(),
1119                       link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
1120                       link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
1121     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1122   }
1123 
1124   // setup result
1125   result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1126   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1127 }
1128 
1129 // throws linktime exceptions
1130 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1131 
1132   Klass* resolved_klass = link_info.resolved_klass();
1133   Method* resolved_method;
1134   if (!resolved_klass->is_interface()) {
1135     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);

1657   }
1658   return info.selected_method();
1659 }
1660 
1661 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1662                                                const LinkInfo& link_info) {
1663   EXCEPTION_MARK;
1664   CallInfo info;
1665   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1666                        /*check_null_or_abstract*/false, THREAD);
1667   if (HAS_PENDING_EXCEPTION) {
1668     CLEAR_PENDING_EXCEPTION;
1669     return Method::invalid_vtable_index;
1670   }
1671   return info.vtable_index();
1672 }
1673 
1674 Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1675   EXCEPTION_MARK;
1676   CallInfo info;
1677   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1678   if (HAS_PENDING_EXCEPTION) {
1679     CLEAR_PENDING_EXCEPTION;
1680     return nullptr;
1681   }
1682   return info.selected_method();
1683 }
1684 
1685 Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1686   EXCEPTION_MARK;
1687   CallInfo info;
1688   resolve_special_call(info, Handle(), link_info, THREAD);
1689   if (HAS_PENDING_EXCEPTION) {
1690     CLEAR_PENDING_EXCEPTION;
1691     return nullptr;
1692   }
1693   return info.selected_method();
1694 }
1695 
1696 
1697 
1698 //------------------------------------------------------------------------------------------------------------------------
1699 // ConstantPool entries
1700 
1701 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1702   switch (byte) {
1703     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1704     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
1705     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1706     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1707     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1708     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1709     default                         :                                                            break;
1710   }
1711   return;
1712 }
1713 
1714 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1715                              const methodHandle& attached_method,
1716                              Bytecodes::Code byte, TRAPS) {
1717   Klass* defc = attached_method->method_holder();
1718   Symbol* name = attached_method->name();
1719   Symbol* type = attached_method->signature();
1720   LinkInfo link_info(defc, name, type);
1721   switch(byte) {
1722     case Bytecodes::_invokevirtual:
1723       resolve_virtual_call(result, recv, recv->klass(), link_info,
1724                            /*check_null_and_abstract=*/true, CHECK);
1725       break;
1726     case Bytecodes::_invokeinterface:
1727       resolve_interface_call(result, recv, recv->klass(), link_info,
1728                              /*check_null_and_abstract=*/true, CHECK);
1729       break;
1730     case Bytecodes::_invokestatic:
1731       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1732       break;
1733     case Bytecodes::_invokespecial:
1734       resolve_special_call(result, recv, link_info, CHECK);
1735       break;
1736     default:
1737       fatal("bad call: %s", Bytecodes::name(byte));
1738       break;
1739   }
1740 }
1741 
1742 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1743   LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1744   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1745 }
1746 
1747 
1748 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1749                                          const constantPoolHandle& pool, int index, TRAPS) {
1750   LinkInfo link_info(pool, index, Bytecodes::_invokespecial, CHECK);
1751   resolve_special_call(result, recv, link_info, CHECK);
1752 }
1753 
1754 
1755 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1756                                           const constantPoolHandle& pool, int index,
1757                                           TRAPS) {
1758 
1759   LinkInfo link_info(pool, index, Bytecodes::_invokevirtual, CHECK);
1760   Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass();
1761   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1762 }
1763 
1764 

 969     // For private access see if there was a problem with nest host
 970     // resolution, and if so report that as part of the message.
 971     if (fd.is_private()) {
 972       print_nest_host_error_on(&ss, ref_klass, sel_klass);
 973     }
 974     // Names are all known to be < 64k so we know this formatted message is not excessively large.
 975     Exceptions::fthrow(THREAD_AND_LOCATION,
 976                        vmSymbols::java_lang_IllegalAccessError(),
 977                        "%s",
 978                        ss.as_string()
 979                        );
 980     return;
 981   }
 982 }
 983 
 984 void LinkResolver::resolve_field_access(fieldDescriptor& fd,
 985                                         const constantPoolHandle& pool,
 986                                         int index,
 987                                         const methodHandle& method,
 988                                         Bytecodes::Code byte,
 989                                         StaticMode static_mode, TRAPS) {
 990   LinkInfo link_info(pool, index, method, byte, CHECK);
 991   resolve_field(fd, link_info, byte, static_mode, CHECK);
 992 }
 993 
 994 void LinkResolver::resolve_field(fieldDescriptor& fd,
 995                                  const LinkInfo& link_info,
 996                                  Bytecodes::Code byte, StaticMode static_mode,
 997                                  TRAPS) {
 998   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 999          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
1000          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
1001          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
1002 
1003   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
1004   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
1005   // Check if there's a resolved klass containing the field
1006   Klass* resolved_klass = link_info.resolved_klass();
1007   Symbol* field = link_info.name();
1008   Symbol* sig = link_info.signature();
1009 
1010   // Resolve instance field
1011   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
1012   // check if field exists; i.e., if a klass containing the field def has been selected
1013   if (sel_klass == nullptr) {
1014     ResourceMark rm(THREAD);
1015     stringStream ss;
1016     ss.print("Class %s does not have member field '", resolved_klass->external_name());

1060                                                      !m->is_object_initializer());
1061 
1062         if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1063           ResourceMark rm(THREAD);
1064           stringStream ss;
1065           ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1066                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1067                    m->name()->as_C_string(),
1068                    is_static ? "<clinit>" : "<init>");
1069           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1070         }
1071       }
1072     }
1073 
1074     // initialize resolved_klass if necessary
1075     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1076     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1077     //
1078     // note 2: we don't want to force initialization if we are just checking
1079     //         if the field access is legal; e.g., during compilation
1080     if (is_static) {
1081       if (static_mode == StaticMode::initialize_klass) {
1082         sel_klass->initialize(CHECK);
1083       } else if (static_mode == StaticMode::initialize_klass_preemptable) {
1084         sel_klass->initialize_preemptable(CHECK);
1085       }
1086     }
1087   }
1088 
1089   if (link_info.check_loader_constraints() && (sel_klass != current_klass) && (current_klass != nullptr)) {
1090     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1091   }
1092 
1093   // return information. note that the klass is set to the actual klass containing the
1094   // field, otherwise access of static fields in superclasses will not work.
1095 }
1096 
1097 
1098 //------------------------------------------------------------------------------------------------------------------------
1099 // Invoke resolution
1100 //
1101 // Naming conventions:
1102 //
1103 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1104 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1105 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1106 // recv_klass         the receiver klass
1107 
1108 
1109 void LinkResolver::resolve_static_call(CallInfo& result,
1110                                        const LinkInfo& link_info,
1111                                        StaticMode static_mode, TRAPS) {
1112   Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);
1113 
1114   // The resolved class can change as a result of this resolution.
1115   Klass* resolved_klass = resolved_method->method_holder();
1116 
1117   // Initialize klass (this should only happen if everything is ok)
1118   if (static_mode != StaticMode::dont_initialize_klass && resolved_klass->should_be_initialized()) {
1119     if (static_mode == StaticMode::initialize_klass) {
1120       resolved_klass->initialize(CHECK);
1121     } else if (static_mode == StaticMode::initialize_klass_preemptable) {
1122       resolved_klass->initialize_preemptable(CHECK);
1123     }
1124     // Use updated LinkInfo to reresolve with resolved method holder
1125     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1126                       link_info.current_klass(),
1127                       link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip,
1128                       link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip);
1129     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1130   }
1131 
1132   // setup result
1133   result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1134   JFR_ONLY(Jfr::on_resolution(result, CHECK);)
1135 }
1136 
1137 // throws linktime exceptions
1138 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1139 
1140   Klass* resolved_klass = link_info.resolved_klass();
1141   Method* resolved_method;
1142   if (!resolved_klass->is_interface()) {
1143     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);

1665   }
1666   return info.selected_method();
1667 }
1668 
1669 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1670                                                const LinkInfo& link_info) {
1671   EXCEPTION_MARK;
1672   CallInfo info;
1673   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1674                        /*check_null_or_abstract*/false, THREAD);
1675   if (HAS_PENDING_EXCEPTION) {
1676     CLEAR_PENDING_EXCEPTION;
1677     return Method::invalid_vtable_index;
1678   }
1679   return info.vtable_index();
1680 }
1681 
1682 Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1683   EXCEPTION_MARK;
1684   CallInfo info;
1685   resolve_static_call(info, link_info, StaticMode::dont_initialize_klass, THREAD);
1686   if (HAS_PENDING_EXCEPTION) {
1687     CLEAR_PENDING_EXCEPTION;
1688     return nullptr;
1689   }
1690   return info.selected_method();
1691 }
1692 
1693 Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1694   EXCEPTION_MARK;
1695   CallInfo info;
1696   resolve_special_call(info, Handle(), link_info, THREAD);
1697   if (HAS_PENDING_EXCEPTION) {
1698     CLEAR_PENDING_EXCEPTION;
1699     return nullptr;
1700   }
1701   return info.selected_method();
1702 }
1703 
1704 
1705 
1706 //------------------------------------------------------------------------------------------------------------------------
1707 // ConstantPool entries
1708 
1709 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, StaticMode static_mode, TRAPS) {
1710   switch (byte) {
1711     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, static_mode, CHECK); break;
1712     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index,              CHECK); break;
1713     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index,              CHECK); break;
1714     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index,              CHECK); break;
1715     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index,              CHECK); break;
1716     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index,              CHECK); break;
1717     default                         :                                                                         break;
1718   }
1719   return;
1720 }
1721 
1722 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1723                              const methodHandle& attached_method,
1724                              Bytecodes::Code byte, TRAPS) {
1725   Klass* defc = attached_method->method_holder();
1726   Symbol* name = attached_method->name();
1727   Symbol* type = attached_method->signature();
1728   LinkInfo link_info(defc, name, type);
1729   switch(byte) {
1730     case Bytecodes::_invokevirtual:
1731       resolve_virtual_call(result, recv, recv->klass(), link_info,
1732                            /*check_null_and_abstract=*/true, CHECK);
1733       break;
1734     case Bytecodes::_invokeinterface:
1735       resolve_interface_call(result, recv, recv->klass(), link_info,
1736                              /*check_null_and_abstract=*/true, CHECK);
1737       break;
1738     case Bytecodes::_invokestatic:
1739       resolve_static_call(result, link_info, StaticMode::dont_initialize_klass, CHECK);
1740       break;
1741     case Bytecodes::_invokespecial:
1742       resolve_special_call(result, recv, link_info, CHECK);
1743       break;
1744     default:
1745       fatal("bad call: %s", Bytecodes::name(byte));
1746       break;
1747   }
1748 }
1749 
1750 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, StaticMode static_mode, TRAPS) {
1751   LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1752   resolve_static_call(result, link_info, static_mode, CHECK);
1753 }
1754 
1755 
1756 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1757                                          const constantPoolHandle& pool, int index, TRAPS) {
1758   LinkInfo link_info(pool, index, Bytecodes::_invokespecial, CHECK);
1759   resolve_special_call(result, recv, link_info, CHECK);
1760 }
1761 
1762 
1763 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1764                                           const constantPoolHandle& pool, int index,
1765                                           TRAPS) {
1766 
1767   LinkInfo link_info(pool, index, Bytecodes::_invokevirtual, CHECK);
1768   Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass();
1769   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1770 }
1771 
1772 
< prev index next >