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()) {
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) {
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() &&
1146 resolved_method->method_holder() != resolved_klass) {
1147 ResourceMark rm(THREAD);
1148 stringStream ss;
1149 ss.print("%s: method '", resolved_klass->external_name());
1150 resolved_method->signature()->print_as_signature_external_return_type(&ss);
1151 ss.print(" %s(", resolved_method->name()->as_C_string());
1152 resolved_method->signature()->print_as_signature_external_parameters(&ss);
1153 ss.print(")' not found");
1154 Exceptions::fthrow(
1155 THREAD_AND_LOCATION,
1156 vmSymbols::java_lang_NoSuchMethodError(),
1157 "%s", ss.as_string());
1158 return nullptr;
1159 }
1160
1161 // ensure that invokespecial's interface method reference is in
1162 // a direct superinterface, not an indirect superinterface
1163 Klass* current_klass = link_info.current_klass();
1164 if (current_klass != nullptr && resolved_klass->is_interface()) {
1196 }
1197
1198 return resolved_method;
1199 }
1200
1201 // throws runtime exceptions
1202 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1203 const LinkInfo& link_info,
1204 const methodHandle& resolved_method,
1205 Handle recv, TRAPS) {
1206
1207 Klass* resolved_klass = link_info.resolved_klass();
1208
1209 // resolved method is selected method unless we have an old-style lookup
1210 // for a superclass method
1211 // Invokespecial for a superinterface, resolved method is selected method,
1212 // no checks for shadowing
1213 methodHandle sel_method(THREAD, resolved_method());
1214
1215 if (link_info.check_access() &&
1216 // check if the method is not <init>
1217 resolved_method->name() != vmSymbols::object_initializer_name()) {
1218
1219 Klass* current_klass = link_info.current_klass();
1220
1221 // Check if the class of the resolved_klass is a superclass
1222 // (not supertype in order to exclude interface classes) of the current class.
1223 // This check is not performed for super.invoke for interface methods
1224 // in super interfaces.
1225 if (current_klass->is_subclass_of(resolved_klass) &&
1226 current_klass != resolved_klass) {
1227 // Lookup super method
1228 Klass* super_klass = current_klass->super();
1229 Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1230 resolved_method->name(),
1231 resolved_method->signature(),
1232 Klass::PrivateLookupMode::find);
1233 sel_method = methodHandle(THREAD, instance_method);
1234
1235 // check if found
1236 if (sel_method.is_null()) {
1618
1619
1620
1621 //------------------------------------------------------------------------------------------------------------------------
1622 // ConstantPool entries
1623
1624 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1625 switch (byte) {
1626 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1627 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;
1628 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1629 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1630 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1631 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1632 default : break;
1633 }
1634 return;
1635 }
1636
1637 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1638 const methodHandle& attached_method,
1639 Bytecodes::Code byte, TRAPS) {
1640 Klass* defc = attached_method->method_holder();
1641 Symbol* name = attached_method->name();
1642 Symbol* type = attached_method->signature();
1643 LinkInfo link_info(defc, name, type);
1644 switch(byte) {
1645 case Bytecodes::_invokevirtual:
1646 resolve_virtual_call(result, recv, recv->klass(), link_info,
1647 /*check_null_and_abstract=*/true, CHECK);
1648 break;
1649 case Bytecodes::_invokeinterface:
1650 resolve_interface_call(result, recv, recv->klass(), link_info,
1651 /*check_null_and_abstract=*/true, CHECK);
1652 break;
1653 case Bytecodes::_invokestatic:
1654 resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1655 break;
1656 case Bytecodes::_invokespecial:
1657 resolve_special_call(result, recv, link_info, CHECK);
1658 break;
1659 default:
1660 fatal("bad call: %s", Bytecodes::name(byte));
1661 break;
1662 }
1663 }
1664
1665 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1666 LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1667 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1668 }
1669
1670
1671 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
|
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 ||
969 byte == Bytecodes::_nofast_putfield);
970 // Check if there's a resolved klass containing the field
971 Klass* resolved_klass = link_info.resolved_klass();
972 Symbol* field = link_info.name();
973 Symbol* sig = link_info.signature();
974
975 // Resolve instance field
976 Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
977 // check if field exists; i.e., if a klass containing the field def has been selected
978 if (sel_klass == nullptr) {
979 ResourceMark rm(THREAD);
980 stringStream ss;
981 ss.print("Class %s does not have member field '", resolved_klass->external_name());
982 sig->print_as_field_external_type(&ss);
983 ss.print(" %s'", field->as_C_string());
984 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
985 }
986
987 // Access checking may be turned off when calling from within the VM.
988 Klass* current_klass = link_info.current_klass();
989 if (link_info.check_access()) {
992 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
993
994 // check for errors
995 if (is_static != fd.is_static()) {
996 ResourceMark rm(THREAD);
997 char msg[200];
998 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
999 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1000 }
1001
1002 // A final field can be modified only
1003 // (1) by methods declared in the class declaring the field and
1004 // (2) by the <clinit> method (in case of a static field)
1005 // or by the <init> method (in case of an instance field).
1006 if (is_put && fd.access_flags().is_final()) {
1007
1008 if (sel_klass != current_klass) {
1009 ResourceMark rm(THREAD);
1010 stringStream ss;
1011 ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1012 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1013 current_klass->external_name());
1014 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1015 }
1016
1017 if (fd.constants()->pool_holder()->major_version() >= 53) {
1018 Method* m = link_info.current_method();
1019 assert(m != nullptr, "information about the current method must be available for 'put' bytecodes");
1020 bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1021 fd.is_static() &&
1022 !m->is_class_initializer());
1023 bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1024 !fd.is_static() &&
1025 !m->is_object_constructor());
1026
1027 if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1028 ResourceMark rm(THREAD);
1029 stringStream ss;
1030 ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1031 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1032 m->name()->as_C_string(),
1033 is_static ? "<clinit>" : "<init>");
1034 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1035 }
1036 }
1037 }
1038
1039 // initialize resolved_klass if necessary
1040 // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1041 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1042 //
1043 // note 2: we don't want to force initialization if we are just checking
1044 // if the field access is legal; e.g., during compilation
1045 if (is_static && initialize_class) {
1126
1127 // throws linktime exceptions
1128 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1129
1130 // Invokespecial is called for multiple special reasons:
1131 // <init>
1132 // local private method invocation, for classes and interfaces
1133 // superclass.method, which can also resolve to a default method
1134 // and the selected method is recalculated relative to the direct superclass
1135 // superinterface.method, which explicitly does not check shadowing
1136 Klass* resolved_klass = link_info.resolved_klass();
1137 Method* resolved_method = nullptr;
1138
1139 if (!resolved_klass->is_interface()) {
1140 resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1141 } else {
1142 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1143 }
1144
1145 // check if method name is <init>, that it is found in same klass as static type
1146 // Since this method is never inherited from a super, any appearance here under
1147 // the wrong class would be an error.
1148 if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1149 resolved_method->method_holder() != resolved_klass) {
1150 ResourceMark rm(THREAD);
1151 stringStream ss;
1152 ss.print("%s: method '", resolved_klass->external_name());
1153 resolved_method->signature()->print_as_signature_external_return_type(&ss);
1154 ss.print(" %s(", resolved_method->name()->as_C_string());
1155 resolved_method->signature()->print_as_signature_external_parameters(&ss);
1156 ss.print(")' not found");
1157 Exceptions::fthrow(
1158 THREAD_AND_LOCATION,
1159 vmSymbols::java_lang_NoSuchMethodError(),
1160 "%s", ss.as_string());
1161 return nullptr;
1162 }
1163
1164 // ensure that invokespecial's interface method reference is in
1165 // a direct superinterface, not an indirect superinterface
1166 Klass* current_klass = link_info.current_klass();
1167 if (current_klass != nullptr && resolved_klass->is_interface()) {
1199 }
1200
1201 return resolved_method;
1202 }
1203
1204 // throws runtime exceptions
1205 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1206 const LinkInfo& link_info,
1207 const methodHandle& resolved_method,
1208 Handle recv, TRAPS) {
1209
1210 Klass* resolved_klass = link_info.resolved_klass();
1211
1212 // resolved method is selected method unless we have an old-style lookup
1213 // for a superclass method
1214 // Invokespecial for a superinterface, resolved method is selected method,
1215 // no checks for shadowing
1216 methodHandle sel_method(THREAD, resolved_method());
1217
1218 if (link_info.check_access() &&
1219 // check if the method is not <init>, which is never inherited
1220 resolved_method->name() != vmSymbols::object_initializer_name()) {
1221
1222 Klass* current_klass = link_info.current_klass();
1223
1224 // Check if the class of the resolved_klass is a superclass
1225 // (not supertype in order to exclude interface classes) of the current class.
1226 // This check is not performed for super.invoke for interface methods
1227 // in super interfaces.
1228 if (current_klass->is_subclass_of(resolved_klass) &&
1229 current_klass != resolved_klass) {
1230 // Lookup super method
1231 Klass* super_klass = current_klass->super();
1232 Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1233 resolved_method->name(),
1234 resolved_method->signature(),
1235 Klass::PrivateLookupMode::find);
1236 sel_method = methodHandle(THREAD, instance_method);
1237
1238 // check if found
1239 if (sel_method.is_null()) {
1621
1622
1623
1624 //------------------------------------------------------------------------------------------------------------------------
1625 // ConstantPool entries
1626
1627 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1628 switch (byte) {
1629 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1630 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;
1631 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1632 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1633 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1634 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1635 default : break;
1636 }
1637 return;
1638 }
1639
1640 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1641 const methodHandle& attached_method,
1642 Bytecodes::Code byte, bool check_null_and_abstract, TRAPS) {
1643 Klass* defc = attached_method->method_holder();
1644 Symbol* name = attached_method->name();
1645 Symbol* type = attached_method->signature();
1646 LinkInfo link_info(defc, name, type);
1647 Klass* recv_klass = recv.is_null() ? defc : recv->klass();
1648 switch(byte) {
1649 case Bytecodes::_invokevirtual:
1650 resolve_virtual_call(result, recv, recv_klass, link_info,
1651 check_null_and_abstract, CHECK);
1652 break;
1653 case Bytecodes::_invokeinterface:
1654 resolve_interface_call(result, recv, recv_klass, link_info,
1655 check_null_and_abstract, CHECK);
1656 break;
1657 case Bytecodes::_invokestatic:
1658 resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1659 break;
1660 case Bytecodes::_invokespecial:
1661 resolve_special_call(result, recv, link_info, CHECK);
1662 break;
1663 default:
1664 fatal("bad call: %s", Bytecodes::name(byte));
1665 break;
1666 }
1667 }
1668
1669 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1670 LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1671 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1672 }
1673
1674
1675 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
|