< prev index next >

src/hotspot/share/interpreter/linkResolver.cpp

Print this page

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

1132 
1133 // throws linktime exceptions
1134 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1135 
1136   // Invokespecial is called for multiple special reasons:
1137   // <init>
1138   // local private method invocation, for classes and interfaces
1139   // superclass.method, which can also resolve to a default method
1140   // and the selected method is recalculated relative to the direct superclass
1141   // superinterface.method, which explicitly does not check shadowing
1142   Klass* resolved_klass = link_info.resolved_klass();
1143   Method* resolved_method = nullptr;
1144 
1145   if (!resolved_klass->is_interface()) {
1146     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1147   } else {
1148     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1149   }
1150 
1151   // check if method name is <init>, that it is found in same klass as static type


1152   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1153       resolved_method->method_holder() != resolved_klass) {
1154     ResourceMark rm(THREAD);
1155     stringStream ss;
1156     ss.print("%s: method '", resolved_klass->external_name());
1157     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1158     ss.print(" %s(", resolved_method->name()->as_C_string());
1159     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1160     ss.print(")' not found");
1161     Exceptions::fthrow(
1162       THREAD_AND_LOCATION,
1163       vmSymbols::java_lang_NoSuchMethodError(),
1164       "%s", ss.as_string());
1165     return nullptr;
1166   }
1167 
1168   // ensure that invokespecial's interface method reference is in
1169   // a direct superinterface, not an indirect superinterface
1170   Klass* current_klass = link_info.current_klass();
1171   if (current_klass != nullptr && resolved_klass->is_interface()) {

1203   }
1204 
1205   return resolved_method;
1206 }
1207 
1208 // throws runtime exceptions
1209 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1210                                                   const LinkInfo& link_info,
1211                                                   const methodHandle& resolved_method,
1212                                                   Handle recv, TRAPS) {
1213 
1214   Klass* resolved_klass = link_info.resolved_klass();
1215 
1216   // resolved method is selected method unless we have an old-style lookup
1217   // for a superclass method
1218   // Invokespecial for a superinterface, resolved method is selected method,
1219   // no checks for shadowing
1220   methodHandle sel_method(THREAD, resolved_method());
1221 
1222   if (link_info.check_access() &&
1223       // check if the method is not <init>
1224       resolved_method->name() != vmSymbols::object_initializer_name()) {
1225 
1226     Klass* current_klass = link_info.current_klass();
1227 
1228     // Check if the class of the resolved_klass is a superclass
1229     // (not supertype in order to exclude interface classes) of the current class.
1230     // This check is not performed for super.invoke for interface methods
1231     // in super interfaces.
1232     if (current_klass->is_subclass_of(resolved_klass) &&
1233         current_klass != resolved_klass) {
1234       // Lookup super method
1235       Klass* super_klass = current_klass->super();
1236       Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1237                                                      resolved_method->name(),
1238                                                      resolved_method->signature(),
1239                                                      Klass::PrivateLookupMode::find);
1240       sel_method = methodHandle(THREAD, instance_method);
1241 
1242       // check if found
1243       if (sel_method.is_null()) {

1625 
1626 
1627 
1628 //------------------------------------------------------------------------------------------------------------------------
1629 // ConstantPool entries
1630 
1631 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1632   switch (byte) {
1633     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1634     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
1635     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1636     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1637     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1638     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1639     default                         :                                                            break;
1640   }
1641   return;
1642 }
1643 
1644 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1645                              const methodHandle& attached_method,
1646                              Bytecodes::Code byte, TRAPS) {
1647   Klass* defc = attached_method->method_holder();
1648   Symbol* name = attached_method->name();
1649   Symbol* type = attached_method->signature();
1650   LinkInfo link_info(defc, name, type);

1651   switch(byte) {
1652     case Bytecodes::_invokevirtual:
1653       resolve_virtual_call(result, recv, recv->klass(), link_info,
1654                            /*check_null_and_abstract=*/true, CHECK);
1655       break;
1656     case Bytecodes::_invokeinterface:
1657       resolve_interface_call(result, recv, recv->klass(), link_info,
1658                              /*check_null_and_abstract=*/true, CHECK);
1659       break;
1660     case Bytecodes::_invokestatic:
1661       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1662       break;
1663     case Bytecodes::_invokespecial:
1664       resolve_special_call(result, recv, link_info, CHECK);
1665       break;
1666     default:
1667       fatal("bad call: %s", Bytecodes::name(byte));
1668       break;
1669   }
1670 }
1671 
1672 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1673   LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1674   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1675 }
1676 
1677 
1678 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,

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

1133 
1134 // throws linktime exceptions
1135 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1136 
1137   // Invokespecial is called for multiple special reasons:
1138   // <init>
1139   // local private method invocation, for classes and interfaces
1140   // superclass.method, which can also resolve to a default method
1141   // and the selected method is recalculated relative to the direct superclass
1142   // superinterface.method, which explicitly does not check shadowing
1143   Klass* resolved_klass = link_info.resolved_klass();
1144   Method* resolved_method = nullptr;
1145 
1146   if (!resolved_klass->is_interface()) {
1147     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1148   } else {
1149     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1150   }
1151 
1152   // check if method name is <init>, that it is found in same klass as static type
1153   // Since this method is never inherited from a super, any appearance here under
1154   // the wrong class would be an error.
1155   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1156       resolved_method->method_holder() != resolved_klass) {
1157     ResourceMark rm(THREAD);
1158     stringStream ss;
1159     ss.print("%s: method '", resolved_klass->external_name());
1160     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1161     ss.print(" %s(", resolved_method->name()->as_C_string());
1162     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1163     ss.print(")' not found");
1164     Exceptions::fthrow(
1165       THREAD_AND_LOCATION,
1166       vmSymbols::java_lang_NoSuchMethodError(),
1167       "%s", ss.as_string());
1168     return nullptr;
1169   }
1170 
1171   // ensure that invokespecial's interface method reference is in
1172   // a direct superinterface, not an indirect superinterface
1173   Klass* current_klass = link_info.current_klass();
1174   if (current_klass != nullptr && resolved_klass->is_interface()) {

1206   }
1207 
1208   return resolved_method;
1209 }
1210 
1211 // throws runtime exceptions
1212 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1213                                                   const LinkInfo& link_info,
1214                                                   const methodHandle& resolved_method,
1215                                                   Handle recv, TRAPS) {
1216 
1217   Klass* resolved_klass = link_info.resolved_klass();
1218 
1219   // resolved method is selected method unless we have an old-style lookup
1220   // for a superclass method
1221   // Invokespecial for a superinterface, resolved method is selected method,
1222   // no checks for shadowing
1223   methodHandle sel_method(THREAD, resolved_method());
1224 
1225   if (link_info.check_access() &&
1226       // check if the method is not <init>, which is never inherited
1227       resolved_method->name() != vmSymbols::object_initializer_name()) {
1228 
1229     Klass* current_klass = link_info.current_klass();
1230 
1231     // Check if the class of the resolved_klass is a superclass
1232     // (not supertype in order to exclude interface classes) of the current class.
1233     // This check is not performed for super.invoke for interface methods
1234     // in super interfaces.
1235     if (current_klass->is_subclass_of(resolved_klass) &&
1236         current_klass != resolved_klass) {
1237       // Lookup super method
1238       Klass* super_klass = current_klass->super();
1239       Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1240                                                      resolved_method->name(),
1241                                                      resolved_method->signature(),
1242                                                      Klass::PrivateLookupMode::find);
1243       sel_method = methodHandle(THREAD, instance_method);
1244 
1245       // check if found
1246       if (sel_method.is_null()) {

1628 
1629 
1630 
1631 //------------------------------------------------------------------------------------------------------------------------
1632 // ConstantPool entries
1633 
1634 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1635   switch (byte) {
1636     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1637     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
1638     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1639     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1640     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1641     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1642     default                         :                                                            break;
1643   }
1644   return;
1645 }
1646 
1647 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1648                                   const methodHandle& attached_method,
1649                                   Bytecodes::Code byte, bool check_null_and_abstract, TRAPS) {
1650   Klass* defc = attached_method->method_holder();
1651   Symbol* name = attached_method->name();
1652   Symbol* type = attached_method->signature();
1653   LinkInfo link_info(defc, name, type);
1654   Klass* recv_klass = recv.is_null() ? defc : recv->klass();
1655   switch(byte) {
1656     case Bytecodes::_invokevirtual:
1657       resolve_virtual_call(result, recv, recv_klass, link_info,
1658                            check_null_and_abstract, CHECK);
1659       break;
1660     case Bytecodes::_invokeinterface:
1661       resolve_interface_call(result, recv, recv_klass, link_info,
1662                              check_null_and_abstract, CHECK);
1663       break;
1664     case Bytecodes::_invokestatic:
1665       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1666       break;
1667     case Bytecodes::_invokespecial:
1668       resolve_special_call(result, recv, link_info, CHECK);
1669       break;
1670     default:
1671       fatal("bad call: %s", Bytecodes::name(byte));
1672       break;
1673   }
1674 }
1675 
1676 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1677   LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK);
1678   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1679 }
1680 
1681 
1682 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
< prev index next >