< 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   if (resolved_klass == NULL) {
 974     ResourceMark rm(THREAD);
 975     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 976   }
 977 














 978   // Resolve instance field
 979   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 980   // check if field exists; i.e., if a klass containing the field def has been selected
 981   if (sel_klass == NULL) {
 982     ResourceMark rm(THREAD);
 983     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_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()) {
 989 
 990     // 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 != NULL, "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 = NULL;
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 NULL;
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 != NULL && resolved_klass->is_interface()) {

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

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

1650   switch(byte) {
1651     case Bytecodes::_invokevirtual:
1652       resolve_virtual_call(result, recv, recv->klass(), link_info,
1653                            /*check_null_and_abstract=*/true, CHECK);
1654       break;
1655     case Bytecodes::_invokeinterface:
1656       resolve_interface_call(result, recv, recv->klass(), link_info,
1657                              /*check_null_and_abstract=*/true, CHECK);
1658       break;
1659     case Bytecodes::_invokestatic:
1660       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1661       break;
1662     case Bytecodes::_invokespecial:
1663       resolve_special_call(result, recv, link_info, CHECK);
1664       break;
1665     default:
1666       fatal("bad call: %s", Bytecodes::name(byte));
1667       break;
1668   }
1669 }
1670 
1671 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1672   LinkInfo link_info(pool, index, CHECK);
1673   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1674 }
1675 
1676 
1677 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   if (resolved_klass == NULL) {
 976     ResourceMark rm(THREAD);
 977     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 978   }
 979 
 980   if (byte == Bytecodes::_withfield && !resolved_klass->is_inline_klass()) {
 981     ResourceMark rm(THREAD);
 982     char msg[200];
 983     jio_snprintf(msg, sizeof(msg), "Bytecode withfield cannot be used on identity class %s", resolved_klass->external_name());
 984     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 985   }
 986 
 987   if (is_put && !is_static && byte != Bytecodes::_withfield && resolved_klass->is_inline_klass()) {
 988     ResourceMark rm(THREAD);
 989     char msg[200];
 990     jio_snprintf(msg, sizeof(msg), "Bytecode putfield cannot be used on primitive class %s", resolved_klass->external_name());
 991     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 992   }
 993 
 994   // Resolve instance field
 995   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 996   // check if field exists; i.e., if a klass containing the field def has been selected
 997   if (sel_klass == NULL) {
 998     ResourceMark rm(THREAD);
 999     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_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 >