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());
981 sig->print_as_field_external_type(&ss);
982 ss.print(" %s'", field->as_C_string());
983 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
984 }
985
986 // Access checking may be turned off when calling from within the VM.
987 Klass* current_klass = link_info.current_klass();
988 if (link_info.check_access()) {
989
990 // check access
991 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
992
993 // check for errors
994 if (is_static != fd.is_static()) {
995 ResourceMark rm(THREAD);
996 char msg[200];
997 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
998 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
999 }
1000
1001 // A final field can be modified only
1002 // (1) by methods declared in the class declaring the field and
1003 // (2) by the <clinit> method (in case of a static field)
1004 // or by the <init> method (in case of an instance field).
1005 if (is_put && fd.access_flags().is_final()) {
1006
1007 if (sel_klass != current_klass) {
1008 ResourceMark rm(THREAD);
1009 stringStream ss;
1010 ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1011 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1012 current_klass->external_name());
1013 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1014 }
1015
1016 if (fd.constants()->pool_holder()->major_version() >= 53) {
1017 Method* m = link_info.current_method();
1018 assert(m != nullptr, "information about the current method must be available for 'put' bytecodes");
1019 bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1020 fd.is_static() &&
1021 !m->is_static_initializer());
1022 bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1023 !fd.is_static() &&
1024 !m->is_object_initializer());
1025
1026 if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1027 ResourceMark rm(THREAD);
1028 stringStream ss;
1029 ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1030 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1031 m->name()->as_C_string(),
1032 is_static ? "<clinit>" : "<init>");
1033 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1034 }
1035 }
1036 }
1037
1038 // initialize resolved_klass if necessary
1039 // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1040 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1041 //
1042 // note 2: we don't want to force initialization if we are just checking
1043 // if the field access is legal; e.g., during compilation
1044 if (is_static && initialize_class) {
1132
1133 // throws linktime exceptions
1134 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1135
1136 // Invokespecial is called for multiple special reasons:
1137 // <init>
1138 // local private method invocation, for classes and interfaces
1139 // superclass.method, which can also resolve to a default method
1140 // and the selected method is recalculated relative to the direct superclass
1141 // superinterface.method, which explicitly does not check shadowing
1142 Klass* resolved_klass = link_info.resolved_klass();
1143 Method* resolved_method = nullptr;
1144
1145 if (!resolved_klass->is_interface()) {
1146 resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1147 } else {
1148 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1149 }
1150
1151 // check if method name is <init>, that it is found in same klass as static type
1152 if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1153 resolved_method->method_holder() != resolved_klass) {
1154 ResourceMark rm(THREAD);
1155 stringStream ss;
1156 ss.print("%s: method '", resolved_klass->external_name());
1157 resolved_method->signature()->print_as_signature_external_return_type(&ss);
1158 ss.print(" %s(", resolved_method->name()->as_C_string());
1159 resolved_method->signature()->print_as_signature_external_parameters(&ss);
1160 ss.print(")' not found");
1161 Exceptions::fthrow(
1162 THREAD_AND_LOCATION,
1163 vmSymbols::java_lang_NoSuchMethodError(),
1164 "%s", ss.as_string());
1165 return nullptr;
1166 }
1167
1168 // ensure that invokespecial's interface method reference is in
1169 // a direct superinterface, not an indirect superinterface
1170 Klass* current_klass = link_info.current_klass();
1171 if (current_klass != nullptr && resolved_klass->is_interface()) {
1203 }
1204
1205 return resolved_method;
1206 }
1207
1208 // throws runtime exceptions
1209 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1210 const LinkInfo& link_info,
1211 const methodHandle& resolved_method,
1212 Handle recv, TRAPS) {
1213
1214 Klass* resolved_klass = link_info.resolved_klass();
1215
1216 // resolved method is selected method unless we have an old-style lookup
1217 // for a superclass method
1218 // Invokespecial for a superinterface, resolved method is selected method,
1219 // no checks for shadowing
1220 methodHandle sel_method(THREAD, resolved_method());
1221
1222 if (link_info.check_access() &&
1223 // check if the method is not <init>
1224 resolved_method->name() != vmSymbols::object_initializer_name()) {
1225
1226 Klass* current_klass = link_info.current_klass();
1227
1228 // Check if the class of the resolved_klass is a superclass
1229 // (not supertype in order to exclude interface classes) of the current class.
1230 // This check is not performed for super.invoke for interface methods
1231 // in super interfaces.
1232 if (current_klass->is_subclass_of(resolved_klass) &&
1233 current_klass != resolved_klass) {
1234 // Lookup super method
1235 Klass* super_klass = current_klass->super();
1236 Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1237 resolved_method->name(),
1238 resolved_method->signature(),
1239 Klass::PrivateLookupMode::find);
1240 sel_method = methodHandle(THREAD, instance_method);
1241
1242 // check if found
1243 if (sel_method.is_null()) {
1625
1626
1627
1628 //------------------------------------------------------------------------------------------------------------------------
1629 // ConstantPool entries
1630
1631 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1632 switch (byte) {
1633 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1634 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;
1635 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1636 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1637 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1638 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1639 default : break;
1640 }
1641 return;
1642 }
1643
1644 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1645 const methodHandle& attached_method,
1646 Bytecodes::Code byte, TRAPS) {
1647 Klass* defc = attached_method->method_holder();
1648 Symbol* name = attached_method->name();
1649 Symbol* type = attached_method->signature();
1650 LinkInfo link_info(defc, name, type);
1651 switch(byte) {
1652 case Bytecodes::_invokevirtual:
1653 resolve_virtual_call(result, recv, recv->klass(), link_info,
1654 /*check_null_and_abstract=*/true, CHECK);
1655 break;
1656 case Bytecodes::_invokeinterface:
1657 resolve_interface_call(result, recv, recv->klass(), link_info,
1658 /*check_null_and_abstract=*/true, CHECK);
1659 break;
1660 case Bytecodes::_invokestatic:
1661 resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1662 break;
1663 case Bytecodes::_invokespecial:
1664 resolve_special_call(result, recv, link_info, CHECK);
1665 break;
1666 default:
1667 fatal("bad call: %s", Bytecodes::name(byte));
1668 break;
1669 }
1670 }
1671
1672 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1673 LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1674 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1675 }
1676
1677
1678 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
|
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::_withfield ||
965 byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield ||
966 (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
967
968 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
969 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic ||
970 byte == Bytecodes::_nofast_putfield || byte == Bytecodes::_withfield);
971 // Check if there's a resolved klass containing the field
972 Klass* resolved_klass = link_info.resolved_klass();
973 Symbol* field = link_info.name();
974 Symbol* sig = link_info.signature();
975
976
977 if (byte == Bytecodes::_withfield && !resolved_klass->is_inline_klass()) {
978 ResourceMark rm(THREAD);
979 char msg[200];
980 jio_snprintf(msg, sizeof(msg), "Bytecode withfield cannot be used on identity class %s", resolved_klass->external_name());
981 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
982 }
983
984 if (is_put && !is_static && byte != Bytecodes::_withfield && resolved_klass->is_inline_klass()) {
985 ResourceMark rm(THREAD);
986 char msg[200];
987 jio_snprintf(msg, sizeof(msg), "Bytecode putfield cannot be used on primitive class %s", resolved_klass->external_name());
988 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
989 }
990
991 // Resolve instance field
992 Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
993 // check if field exists; i.e., if a klass containing the field def has been selected
994 if (sel_klass == nullptr) {
995 ResourceMark rm(THREAD);
996 stringStream ss;
997 ss.print("Class %s does not have member field '", resolved_klass->external_name());
998 sig->print_as_field_external_type(&ss);
999 ss.print(" %s'", field->as_C_string());
1000 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
1001 }
1002
1003 // Access checking may be turned off when calling from within the VM.
1004 Klass* current_klass = link_info.current_klass();
1005 if (link_info.check_access()) {
1006
1007 // check access
1008 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
1009
1010 // check for errors
1011 if (is_static != fd.is_static()) {
1012 ResourceMark rm(THREAD);
1013 char msg[200];
1014 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1015 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1016 }
1017
1018 // A final field can be modified only
1019 // (1) by methods declared in the class declaring the field and
1020 // (2) by the <clinit> method (in case of a static field)
1021 // or by the <init> method (in case of an instance field).
1022 // (3) by withfield when field is in a value type and the
1023 // selected class and current class are nest mates.
1024 if (is_put && fd.access_flags().is_final()) {
1025
1026 if (sel_klass != current_klass) {
1027 // If byte code is a withfield check if they are nestmates.
1028 bool are_nestmates = false;
1029 if (sel_klass->is_instance_klass() &&
1030 InstanceKlass::cast(sel_klass)->is_inline_klass() &&
1031 current_klass->is_instance_klass()) {
1032 are_nestmates = InstanceKlass::cast(current_klass)->has_nestmate_access_to(InstanceKlass::cast(sel_klass), THREAD);
1033 }
1034 if (!are_nestmates) {
1035 ResourceMark rm(THREAD);
1036 stringStream ss;
1037 ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1038 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1039 current_klass->external_name());
1040 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1041 }
1042 }
1043
1044 if (fd.constants()->pool_holder()->major_version() >= 53) {
1045 Method* m = link_info.current_method();
1046 assert(m != nullptr, "information about the current method must be available for 'put' bytecodes");
1047 bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1048 fd.is_static() &&
1049 !m->is_class_initializer());
1050 bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1051 !fd.is_static() &&
1052 !m->is_object_constructor());
1053
1054 if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1055 ResourceMark rm(THREAD);
1056 stringStream ss;
1057 ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1058 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1059 m->name()->as_C_string(),
1060 is_static ? "<clinit>" : "<init>");
1061 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1062 }
1063 }
1064 }
1065
1066 // initialize resolved_klass if necessary
1067 // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1068 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1069 //
1070 // note 2: we don't want to force initialization if we are just checking
1071 // if the field access is legal; e.g., during compilation
1072 if (is_static && initialize_class) {
1160
1161 // throws linktime exceptions
1162 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1163
1164 // Invokespecial is called for multiple special reasons:
1165 // <init>
1166 // local private method invocation, for classes and interfaces
1167 // superclass.method, which can also resolve to a default method
1168 // and the selected method is recalculated relative to the direct superclass
1169 // superinterface.method, which explicitly does not check shadowing
1170 Klass* resolved_klass = link_info.resolved_klass();
1171 Method* resolved_method = nullptr;
1172
1173 if (!resolved_klass->is_interface()) {
1174 resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1175 } else {
1176 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1177 }
1178
1179 // check if method name is <init>, that it is found in same klass as static type
1180 // Since this method is never inherited from a super, any appearance here under
1181 // the wrong class would be an error.
1182 if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1183 resolved_method->method_holder() != resolved_klass) {
1184 ResourceMark rm(THREAD);
1185 stringStream ss;
1186 ss.print("%s: method '", resolved_klass->external_name());
1187 resolved_method->signature()->print_as_signature_external_return_type(&ss);
1188 ss.print(" %s(", resolved_method->name()->as_C_string());
1189 resolved_method->signature()->print_as_signature_external_parameters(&ss);
1190 ss.print(")' not found");
1191 Exceptions::fthrow(
1192 THREAD_AND_LOCATION,
1193 vmSymbols::java_lang_NoSuchMethodError(),
1194 "%s", ss.as_string());
1195 return nullptr;
1196 }
1197
1198 // ensure that invokespecial's interface method reference is in
1199 // a direct superinterface, not an indirect superinterface
1200 Klass* current_klass = link_info.current_klass();
1201 if (current_klass != nullptr && resolved_klass->is_interface()) {
1233 }
1234
1235 return resolved_method;
1236 }
1237
1238 // throws runtime exceptions
1239 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1240 const LinkInfo& link_info,
1241 const methodHandle& resolved_method,
1242 Handle recv, TRAPS) {
1243
1244 Klass* resolved_klass = link_info.resolved_klass();
1245
1246 // resolved method is selected method unless we have an old-style lookup
1247 // for a superclass method
1248 // Invokespecial for a superinterface, resolved method is selected method,
1249 // no checks for shadowing
1250 methodHandle sel_method(THREAD, resolved_method());
1251
1252 if (link_info.check_access() &&
1253 // check if the method is not <init>, which is never inherited
1254 resolved_method->name() != vmSymbols::object_initializer_name()) {
1255
1256 Klass* current_klass = link_info.current_klass();
1257
1258 // Check if the class of the resolved_klass is a superclass
1259 // (not supertype in order to exclude interface classes) of the current class.
1260 // This check is not performed for super.invoke for interface methods
1261 // in super interfaces.
1262 if (current_klass->is_subclass_of(resolved_klass) &&
1263 current_klass != resolved_klass) {
1264 // Lookup super method
1265 Klass* super_klass = current_klass->super();
1266 Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1267 resolved_method->name(),
1268 resolved_method->signature(),
1269 Klass::PrivateLookupMode::find);
1270 sel_method = methodHandle(THREAD, instance_method);
1271
1272 // check if found
1273 if (sel_method.is_null()) {
1655
1656
1657
1658 //------------------------------------------------------------------------------------------------------------------------
1659 // ConstantPool entries
1660
1661 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1662 switch (byte) {
1663 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1664 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;
1665 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1666 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1667 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1668 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1669 default : break;
1670 }
1671 return;
1672 }
1673
1674 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1675 const methodHandle& attached_method,
1676 Bytecodes::Code byte, bool check_null_and_abstract, TRAPS) {
1677 Klass* defc = attached_method->method_holder();
1678 Symbol* name = attached_method->name();
1679 Symbol* type = attached_method->signature();
1680 LinkInfo link_info(defc, name, type);
1681 Klass* recv_klass = recv.is_null() ? defc : recv->klass();
1682 switch(byte) {
1683 case Bytecodes::_invokevirtual:
1684 resolve_virtual_call(result, recv, recv_klass, link_info,
1685 check_null_and_abstract, CHECK);
1686 break;
1687 case Bytecodes::_invokeinterface:
1688 resolve_interface_call(result, recv, recv_klass, link_info,
1689 check_null_and_abstract, CHECK);
1690 break;
1691 case Bytecodes::_invokestatic:
1692 resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1693 break;
1694 case Bytecodes::_invokespecial:
1695 resolve_special_call(result, recv, link_info, CHECK);
1696 break;
1697 default:
1698 fatal("bad call: %s", Bytecodes::name(byte));
1699 break;
1700 }
1701 }
1702
1703 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1704 LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1705 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1706 }
1707
1708
1709 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
|