< prev index next >

src/hotspot/share/interpreter/linkResolver.cpp

Print this page

 984 void LinkResolver::resolve_field_access(fieldDescriptor& fd,
 985                                         const constantPoolHandle& pool,
 986                                         int index,
 987                                         const methodHandle& method,
 988                                         Bytecodes::Code byte,
 989                                         ClassInitMode init_mode, TRAPS) {
 990   LinkInfo link_info(pool, index, method, byte, CHECK);
 991   resolve_field(fd, link_info, byte, init_mode, CHECK);
 992 }
 993 
 994 void LinkResolver::resolve_field(fieldDescriptor& fd,
 995                                  const LinkInfo& link_info,
 996                                  Bytecodes::Code byte, ClassInitMode init_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());
1017     sig->print_as_field_external_type(&ss);
1018     ss.print(" %s'", field->as_C_string());
1019     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
1020   }
1021 
1022   // Access checking may be turned off when calling from within the VM.
1023   Klass* current_klass = link_info.current_klass();
1024   if (link_info.check_access()) {

1027     check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
1028 
1029     // check for errors
1030     if (is_static != fd.is_static()) {
1031       ResourceMark rm(THREAD);
1032       char msg[200];
1033       jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1034       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1035     }
1036 
1037     // A final field can be modified only
1038     // (1) by methods declared in the class declaring the field and
1039     // (2) by the <clinit> method (in case of a static field)
1040     //     or by the <init> method (in case of an instance field).
1041     if (is_put && fd.access_flags().is_final()) {
1042 
1043       if (sel_klass != current_klass) {
1044         ResourceMark rm(THREAD);
1045         stringStream ss;
1046         ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1047                  is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1048                 current_klass->external_name());
1049         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1050       }
1051 
1052       if (fd.constants()->pool_holder()->major_version() >= 53) {
1053         Method* m = link_info.current_method();
1054         assert(m != nullptr, "information about the current method must be available for 'put' bytecodes");
1055         bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1056                                                    fd.is_static() &&
1057                                                    !m->is_static_initializer());
1058         bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1059                                                      !fd.is_static() &&
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) {

1177 
1178 // throws linktime exceptions
1179 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1180 
1181   // Invokespecial is called for multiple special reasons:
1182   // <init>
1183   // local private method invocation, for classes and interfaces
1184   // superclass.method, which can also resolve to a default method
1185   // and the selected method is recalculated relative to the direct superclass
1186   // superinterface.method, which explicitly does not check shadowing
1187   Klass* resolved_klass = link_info.resolved_klass();
1188   Method* resolved_method = nullptr;
1189 
1190   if (!resolved_klass->is_interface()) {
1191     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1192   } else {
1193     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1194   }
1195 
1196   // check if method name is <init>, that it is found in same klass as static type


1197   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1198       resolved_method->method_holder() != resolved_klass) {
1199     ResourceMark rm(THREAD);
1200     stringStream ss;
1201     ss.print("%s: method '", resolved_klass->external_name());
1202     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1203     ss.print(" %s(", resolved_method->name()->as_C_string());
1204     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1205     ss.print(")' not found");
1206     // Names are all known to be < 64k so we know this formatted message is not excessively large.
1207     Exceptions::fthrow(
1208       THREAD_AND_LOCATION,
1209       vmSymbols::java_lang_NoSuchMethodError(),
1210       "%s", ss.as_string());
1211     return nullptr;
1212   }
1213 
1214   // ensure that invokespecial's interface method reference is in
1215   // a direct superinterface, not an indirect superinterface or unrelated interface
1216   Klass* current_klass = link_info.current_klass();

1243   }
1244 
1245   return resolved_method;
1246 }
1247 
1248 // throws runtime exceptions
1249 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1250                                                   const LinkInfo& link_info,
1251                                                   const methodHandle& resolved_method,
1252                                                   Handle recv, TRAPS) {
1253 
1254   Klass* resolved_klass = link_info.resolved_klass();
1255 
1256   // resolved method is selected method unless we have an old-style lookup
1257   // for a superclass method
1258   // Invokespecial for a superinterface, resolved method is selected method,
1259   // no checks for shadowing
1260   methodHandle sel_method(THREAD, resolved_method());
1261 
1262   if (link_info.check_access() &&
1263       // check if the method is not <init>
1264       resolved_method->name() != vmSymbols::object_initializer_name()) {
1265 
1266     Klass* current_klass = link_info.current_klass();
1267 
1268     // Check if the class of the resolved_klass is a superclass
1269     // (not supertype in order to exclude interface classes) of the current class.
1270     // This check is not performed for super.invoke for interface methods
1271     // in super interfaces.
1272     if (current_klass->is_subclass_of(resolved_klass) &&
1273         current_klass != resolved_klass) {
1274       // Lookup super method
1275       Klass* super_klass = current_klass->super();
1276       Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1277                                                      resolved_method->name(),
1278                                                      resolved_method->signature(),
1279                                                      Klass::PrivateLookupMode::find);
1280       sel_method = methodHandle(THREAD, instance_method);
1281 
1282       // check if found
1283       if (sel_method.is_null()) {

1707 
1708 
1709 
1710 //------------------------------------------------------------------------------------------------------------------------
1711 // ConstantPool entries
1712 
1713 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, ClassInitMode init_mode, TRAPS) {
1714   switch (byte) {
1715     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, init_mode, CHECK); break;
1716     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index,            CHECK); break;
1717     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index,            CHECK); break;
1718     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index,            CHECK); break;
1719     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index,            CHECK); break;
1720     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index,            CHECK); break;
1721     default                         :                                                                       break;
1722   }
1723   return;
1724 }
1725 
1726 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1727                              const methodHandle& attached_method,
1728                              Bytecodes::Code byte, TRAPS) {
1729   Klass* defc = attached_method->method_holder();
1730   Symbol* name = attached_method->name();
1731   Symbol* type = attached_method->signature();
1732   LinkInfo link_info(defc, name, type);

1733   switch(byte) {
1734     case Bytecodes::_invokevirtual:
1735       resolve_virtual_call(result, recv, recv->klass(), link_info,
1736                            /*check_null_and_abstract=*/true, CHECK);
1737       break;
1738     case Bytecodes::_invokeinterface:
1739       resolve_interface_call(result, recv, recv->klass(), link_info,
1740                              /*check_null_and_abstract=*/true, CHECK);
1741       break;
1742     case Bytecodes::_invokestatic:
1743       resolve_static_call(result, link_info, ClassInitMode::dont_init, CHECK);
1744       break;
1745     case Bytecodes::_invokespecial:
1746       resolve_special_call(result, recv, link_info, CHECK);
1747       break;
1748     default:
1749       fatal("bad call: %s", Bytecodes::name(byte));
1750       break;
1751   }
1752 }
1753 
1754 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, ClassInitMode init_mode, TRAPS) {
1755   LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1756   resolve_static_call(result, link_info, init_mode, CHECK);
1757 }
1758 
1759 
1760 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,

 984 void LinkResolver::resolve_field_access(fieldDescriptor& fd,
 985                                         const constantPoolHandle& pool,
 986                                         int index,
 987                                         const methodHandle& method,
 988                                         Bytecodes::Code byte,
 989                                         ClassInitMode init_mode, TRAPS) {
 990   LinkInfo link_info(pool, index, method, byte, CHECK);
 991   resolve_field(fd, link_info, byte, init_mode, CHECK);
 992 }
 993 
 994 void LinkResolver::resolve_field(fieldDescriptor& fd,
 995                                  const LinkInfo& link_info,
 996                                  Bytecodes::Code byte, ClassInitMode init_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 ||
1005                     byte == Bytecodes::_nofast_putfield);
1006   // Check if there's a resolved klass containing the field
1007   Klass* resolved_klass = link_info.resolved_klass();
1008   Symbol* field = link_info.name();
1009   Symbol* sig = link_info.signature();
1010 
1011   // Resolve instance field
1012   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
1013   // check if field exists; i.e., if a klass containing the field def has been selected
1014   if (sel_klass == nullptr) {
1015     ResourceMark rm(THREAD);
1016     stringStream ss;
1017     ss.print("Class %s does not have member field '", resolved_klass->external_name());
1018     sig->print_as_field_external_type(&ss);
1019     ss.print(" %s'", field->as_C_string());
1020     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
1021   }
1022 
1023   // Access checking may be turned off when calling from within the VM.
1024   Klass* current_klass = link_info.current_klass();
1025   if (link_info.check_access()) {

1028     check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
1029 
1030     // check for errors
1031     if (is_static != fd.is_static()) {
1032       ResourceMark rm(THREAD);
1033       char msg[200];
1034       jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1035       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1036     }
1037 
1038     // A final field can be modified only
1039     // (1) by methods declared in the class declaring the field and
1040     // (2) by the <clinit> method (in case of a static field)
1041     //     or by the <init> method (in case of an instance field).
1042     if (is_put && fd.access_flags().is_final()) {
1043 
1044       if (sel_klass != current_klass) {
1045         ResourceMark rm(THREAD);
1046         stringStream ss;
1047         ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1048                   is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1049                   current_klass->external_name());
1050         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1051       }
1052 
1053       if (fd.constants()->pool_holder()->major_version() >= 53) {
1054         Method* m = link_info.current_method();
1055         assert(m != nullptr, "information about the current method must be available for 'put' bytecodes");
1056         bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1057                                                    fd.is_static() &&
1058                                                    !m->is_class_initializer());
1059         bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1060                                                      !fd.is_static() &&
1061                                                      !m->is_object_constructor());
1062 
1063         if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1064           ResourceMark rm(THREAD);
1065           stringStream ss;
1066           ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1067                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1068                    m->name()->as_C_string(),
1069                    is_static ? "<clinit>" : "<init>");
1070           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1071         }
1072       }
1073     }
1074 
1075     // initialize resolved_klass if necessary
1076     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1077     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1078     //
1079     // note 2: we don't want to force initialization if we are just checking
1080     //         if the field access is legal; e.g., during compilation
1081     if (is_static) {

1178 
1179 // throws linktime exceptions
1180 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1181 
1182   // Invokespecial is called for multiple special reasons:
1183   // <init>
1184   // local private method invocation, for classes and interfaces
1185   // superclass.method, which can also resolve to a default method
1186   // and the selected method is recalculated relative to the direct superclass
1187   // superinterface.method, which explicitly does not check shadowing
1188   Klass* resolved_klass = link_info.resolved_klass();
1189   Method* resolved_method = nullptr;
1190 
1191   if (!resolved_klass->is_interface()) {
1192     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1193   } else {
1194     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1195   }
1196 
1197   // check if method name is <init>, that it is found in same klass as static type
1198   // Since this method is never inherited from a super, any appearance here under
1199   // the wrong class would be an error.
1200   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1201       resolved_method->method_holder() != resolved_klass) {
1202     ResourceMark rm(THREAD);
1203     stringStream ss;
1204     ss.print("%s: method '", resolved_klass->external_name());
1205     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1206     ss.print(" %s(", resolved_method->name()->as_C_string());
1207     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1208     ss.print(")' not found");
1209     // Names are all known to be < 64k so we know this formatted message is not excessively large.
1210     Exceptions::fthrow(
1211       THREAD_AND_LOCATION,
1212       vmSymbols::java_lang_NoSuchMethodError(),
1213       "%s", ss.as_string());
1214     return nullptr;
1215   }
1216 
1217   // ensure that invokespecial's interface method reference is in
1218   // a direct superinterface, not an indirect superinterface or unrelated interface
1219   Klass* current_klass = link_info.current_klass();

1246   }
1247 
1248   return resolved_method;
1249 }
1250 
1251 // throws runtime exceptions
1252 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1253                                                   const LinkInfo& link_info,
1254                                                   const methodHandle& resolved_method,
1255                                                   Handle recv, TRAPS) {
1256 
1257   Klass* resolved_klass = link_info.resolved_klass();
1258 
1259   // resolved method is selected method unless we have an old-style lookup
1260   // for a superclass method
1261   // Invokespecial for a superinterface, resolved method is selected method,
1262   // no checks for shadowing
1263   methodHandle sel_method(THREAD, resolved_method());
1264 
1265   if (link_info.check_access() &&
1266       // check if the method is not <init>, which is never inherited
1267       resolved_method->name() != vmSymbols::object_initializer_name()) {
1268 
1269     Klass* current_klass = link_info.current_klass();
1270 
1271     // Check if the class of the resolved_klass is a superclass
1272     // (not supertype in order to exclude interface classes) of the current class.
1273     // This check is not performed for super.invoke for interface methods
1274     // in super interfaces.
1275     if (current_klass->is_subclass_of(resolved_klass) &&
1276         current_klass != resolved_klass) {
1277       // Lookup super method
1278       Klass* super_klass = current_klass->super();
1279       Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1280                                                      resolved_method->name(),
1281                                                      resolved_method->signature(),
1282                                                      Klass::PrivateLookupMode::find);
1283       sel_method = methodHandle(THREAD, instance_method);
1284 
1285       // check if found
1286       if (sel_method.is_null()) {

1710 
1711 
1712 
1713 //------------------------------------------------------------------------------------------------------------------------
1714 // ConstantPool entries
1715 
1716 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, ClassInitMode init_mode, TRAPS) {
1717   switch (byte) {
1718     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, init_mode, CHECK); break;
1719     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index,            CHECK); break;
1720     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index,            CHECK); break;
1721     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index,            CHECK); break;
1722     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index,            CHECK); break;
1723     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index,            CHECK); break;
1724     default                         :                                                                       break;
1725   }
1726   return;
1727 }
1728 
1729 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1730                                   const methodHandle& attached_method,
1731                                   Bytecodes::Code byte, bool check_null_and_abstract, TRAPS) {
1732   Klass* defc = attached_method->method_holder();
1733   Symbol* name = attached_method->name();
1734   Symbol* type = attached_method->signature();
1735   LinkInfo link_info(defc, name, type);
1736   Klass* recv_klass = recv.is_null() ? defc : recv->klass();
1737   switch(byte) {
1738     case Bytecodes::_invokevirtual:
1739       resolve_virtual_call(result, recv, recv_klass, link_info,
1740                            check_null_and_abstract, CHECK);
1741       break;
1742     case Bytecodes::_invokeinterface:
1743       resolve_interface_call(result, recv, recv_klass, link_info,
1744                              check_null_and_abstract, CHECK);
1745       break;
1746     case Bytecodes::_invokestatic:
1747       resolve_static_call(result, link_info, ClassInitMode::dont_init, CHECK);
1748       break;
1749     case Bytecodes::_invokespecial:
1750       resolve_special_call(result, recv, link_info, CHECK);
1751       break;
1752     default:
1753       fatal("bad call: %s", Bytecodes::name(byte));
1754       break;
1755   }
1756 }
1757 
1758 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, ClassInitMode init_mode, TRAPS) {
1759   LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1760   resolve_static_call(result, link_info, init_mode, CHECK);
1761 }
1762 
1763 
1764 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
< prev index next >