< 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                                         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());
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 && initialize_class) {

1165 
1166 // throws linktime exceptions
1167 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1168 
1169   // Invokespecial is called for multiple special reasons:
1170   // <init>
1171   // local private method invocation, for classes and interfaces
1172   // superclass.method, which can also resolve to a default method
1173   // and the selected method is recalculated relative to the direct superclass
1174   // superinterface.method, which explicitly does not check shadowing
1175   Klass* resolved_klass = link_info.resolved_klass();
1176   Method* resolved_method = nullptr;
1177 
1178   if (!resolved_klass->is_interface()) {
1179     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1180   } else {
1181     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1182   }
1183 
1184   // check if method name is <init>, that it is found in same klass as static type


1185   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1186       resolved_method->method_holder() != resolved_klass) {
1187     ResourceMark rm(THREAD);
1188     stringStream ss;
1189     ss.print("%s: method '", resolved_klass->external_name());
1190     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1191     ss.print(" %s(", resolved_method->name()->as_C_string());
1192     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1193     ss.print(")' not found");
1194     // Names are all known to be < 64k so we know this formatted message is not excessively large.
1195     Exceptions::fthrow(
1196       THREAD_AND_LOCATION,
1197       vmSymbols::java_lang_NoSuchMethodError(),
1198       "%s", ss.as_string());
1199     return nullptr;
1200   }
1201 
1202   // ensure that invokespecial's interface method reference is in
1203   // a direct superinterface, not an indirect superinterface
1204   Klass* current_klass = link_info.current_klass();

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

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,

 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 ||
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 && initialize_class) {

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

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

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