< prev index next >

src/hotspot/share/interpreter/linkResolver.cpp

Print this page

 943     Exceptions::fthrow(THREAD_AND_LOCATION,
 944                        vmSymbols::java_lang_IllegalAccessError(),
 945                        "%s",
 946                        ss.as_string()
 947                        );
 948     return;
 949   }
 950 }
 951 
 952 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 953   LinkInfo link_info(pool, index, method, CHECK);
 954   resolve_field(fd, link_info, byte, true, CHECK);
 955 }
 956 
 957 void LinkResolver::resolve_field(fieldDescriptor& fd,
 958                                  const LinkInfo& link_info,
 959                                  Bytecodes::Code byte, bool initialize_class,
 960                                  TRAPS) {
 961   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 962          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||

 963          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 964          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 965 
 966   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 967   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);

 968   // Check if there's a resolved klass containing the field
 969   Klass* resolved_klass = link_info.resolved_klass();
 970   Symbol* field = link_info.name();
 971   Symbol* sig = link_info.signature();
 972 















 973   // Resolve instance field
 974   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 975   // check if field exists; i.e., if a klass containing the field def has been selected
 976   if (sel_klass == NULL) {
 977     ResourceMark rm(THREAD);
 978     stringStream ss;
 979     ss.print("Class %s does not have member field '", resolved_klass->external_name());
 980     sig->print_as_field_external_type(&ss);
 981     ss.print(" %s'", field->as_C_string());
 982     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
 983   }
 984 
 985   // Access checking may be turned off when calling from within the VM.
 986   Klass* current_klass = link_info.current_klass();
 987   if (link_info.check_access()) {
 988 
 989     // check access
 990     check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 991 
 992     // check for errors
 993     if (is_static != fd.is_static()) {
 994       ResourceMark rm(THREAD);
 995       char msg[200];
 996       jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
 997       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 998     }
 999 
1000     // A final field can be modified only
1001     // (1) by methods declared in the class declaring the field and
1002     // (2) by the <clinit> method (in case of a static field)
1003     //     or by the <init> method (in case of an instance field).


1004     if (is_put && fd.access_flags().is_final()) {
1005 
1006       if (sel_klass != current_klass) {
1007         ResourceMark rm(THREAD);
1008         stringStream ss;
1009         ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1010                  is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1011                 current_klass->external_name());
1012         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());









1013       }
1014 
1015       if (fd.constants()->pool_holder()->major_version() >= 53) {
1016         Method* m = link_info.current_method();
1017         assert(m != NULL, "information about the current method must be available for 'put' bytecodes");
1018         bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1019                                                    fd.is_static() &&
1020                                                    !m->is_static_initializer());
1021         bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1022                                                      !fd.is_static() &&
1023                                                      !m->is_object_initializer());
1024 
1025         if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1026           ResourceMark rm(THREAD);
1027           stringStream ss;
1028           ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1029                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1030                    m->name()->as_C_string(),
1031                    is_static ? "<clinit>" : "<init>");
1032           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1033         }
1034       }
1035     }
1036 
1037     // initialize resolved_klass if necessary
1038     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1039     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1040     //
1041     // note 2: we don't want to force initialization if we are just checking
1042     //         if the field access is legal; e.g., during compilation
1043     if (is_static && initialize_class) {

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


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

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

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

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

 943     Exceptions::fthrow(THREAD_AND_LOCATION,
 944                        vmSymbols::java_lang_IllegalAccessError(),
 945                        "%s",
 946                        ss.as_string()
 947                        );
 948     return;
 949   }
 950 }
 951 
 952 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 953   LinkInfo link_info(pool, index, method, CHECK);
 954   resolve_field(fd, link_info, byte, true, CHECK);
 955 }
 956 
 957 void LinkResolver::resolve_field(fieldDescriptor& fd,
 958                                  const LinkInfo& link_info,
 959                                  Bytecodes::Code byte, bool initialize_class,
 960                                  TRAPS) {
 961   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 962          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 963          byte == Bytecodes::_withfield ||
 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 || byte == Bytecodes::_withfield);
 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 
 976   if (byte == Bytecodes::_withfield && !resolved_klass->is_inline_klass()) {
 977     ResourceMark rm(THREAD);
 978     char msg[200];
 979     jio_snprintf(msg, sizeof(msg), "Bytecode withfield cannot be used on identity class %s", resolved_klass->external_name());
 980     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 981   }
 982 
 983   if (is_put && !is_static && byte != Bytecodes::_withfield && resolved_klass->is_inline_klass()) {
 984     ResourceMark rm(THREAD);
 985     char msg[200];
 986     jio_snprintf(msg, sizeof(msg), "Bytecode putfield cannot be used on primitive class %s", resolved_klass->external_name());
 987     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 988   }
 989 
 990   // Resolve instance field
 991   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 992   // check if field exists; i.e., if a klass containing the field def has been selected
 993   if (sel_klass == NULL) {
 994     ResourceMark rm(THREAD);
 995     stringStream ss;
 996     ss.print("Class %s does not have member field '", resolved_klass->external_name());
 997     sig->print_as_field_external_type(&ss);
 998     ss.print(" %s'", field->as_C_string());
 999     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string());
1000   }
1001 
1002   // Access checking may be turned off when calling from within the VM.
1003   Klass* current_klass = link_info.current_klass();
1004   if (link_info.check_access()) {
1005 
1006     // check access
1007     check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
1008 
1009     // check for errors
1010     if (is_static != fd.is_static()) {
1011       ResourceMark rm(THREAD);
1012       char msg[200];
1013       jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1014       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1015     }
1016 
1017     // A final field can be modified only
1018     // (1) by methods declared in the class declaring the field and
1019     // (2) by the <clinit> method (in case of a static field)
1020     //     or by the <init> method (in case of an instance field).
1021     // (3) by withfield when field is in a value type and the
1022     //     selected class and current class are nest mates.
1023     if (is_put && fd.access_flags().is_final()) {
1024 
1025       if (sel_klass != current_klass) {
1026         // If byte code is a withfield check if they are nestmates.
1027         bool are_nestmates = false;
1028         if (sel_klass->is_instance_klass() &&
1029             InstanceKlass::cast(sel_klass)->is_inline_klass() &&
1030             current_klass->is_instance_klass()) {
1031           are_nestmates = InstanceKlass::cast(current_klass)->has_nestmate_access_to(InstanceKlass::cast(sel_klass), THREAD);
1032         }
1033         if (!are_nestmates) {
1034           ResourceMark rm(THREAD);
1035           stringStream ss;
1036           ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1037                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1038                     current_klass->external_name());
1039           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1040         }
1041       }
1042 
1043       if (fd.constants()->pool_holder()->major_version() >= 53) {
1044         Method* m = link_info.current_method();
1045         assert(m != NULL, "information about the current method must be available for 'put' bytecodes");
1046         bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1047                                                    fd.is_static() &&
1048                                                    !m->is_class_initializer());
1049         bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1050                                                      !fd.is_static() &&
1051                                                      !m->is_object_constructor());
1052 
1053         if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1054           ResourceMark rm(THREAD);
1055           stringStream ss;
1056           ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1057                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1058                    m->name()->as_C_string(),
1059                    is_static ? "<clinit>" : "<init>");
1060           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1061         }
1062       }
1063     }
1064 
1065     // initialize resolved_klass if necessary
1066     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1067     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1068     //
1069     // note 2: we don't want to force initialization if we are just checking
1070     //         if the field access is legal; e.g., during compilation
1071     if (is_static && initialize_class) {

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

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>, which is never inherited
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()) {

1653 
1654 
1655 
1656 //------------------------------------------------------------------------------------------------------------------------
1657 // ConstantPool entries
1658 
1659 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1660   switch (byte) {
1661     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1662     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
1663     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1664     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1665     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1666     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1667     default                         :                                                            break;
1668   }
1669   return;
1670 }
1671 
1672 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1673                                   const methodHandle& attached_method,
1674                                   Bytecodes::Code byte, bool check_null_and_abstract, TRAPS) {
1675   Klass* defc = attached_method->method_holder();
1676   Symbol* name = attached_method->name();
1677   Symbol* type = attached_method->signature();
1678   LinkInfo link_info(defc, name, type);
1679   Klass* recv_klass = recv.is_null() ? defc : recv->klass();
1680   switch(byte) {
1681     case Bytecodes::_invokevirtual:
1682       resolve_virtual_call(result, recv, recv_klass, link_info,
1683                            check_null_and_abstract, CHECK);
1684       break;
1685     case Bytecodes::_invokeinterface:
1686       resolve_interface_call(result, recv, recv_klass, link_info,
1687                              check_null_and_abstract, CHECK);
1688       break;
1689     case Bytecodes::_invokestatic:
1690       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1691       break;
1692     case Bytecodes::_invokespecial:
1693       resolve_special_call(result, recv, link_info, CHECK);
1694       break;
1695     default:
1696       fatal("bad call: %s", Bytecodes::name(byte));
1697       break;
1698   }
1699 }
1700 
1701 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1702   LinkInfo link_info(pool, index, CHECK);
1703   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1704 }
1705 
1706 
1707 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
< prev index next >