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

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


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

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

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

1725   switch(byte) {
1726     case Bytecodes::_invokevirtual:
1727       resolve_virtual_call(result, recv, recv->klass(), link_info,
1728                            /*check_null_and_abstract=*/true, CHECK);
1729       break;
1730     case Bytecodes::_invokeinterface:
1731       resolve_interface_call(result, recv, recv->klass(), link_info,
1732                              /*check_null_and_abstract=*/true, 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,

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

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

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

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