804 // some other thread put this in error state |
803 // some other thread put this in error state |
805 throw_resolution_error(this_cp, which, CHECK); |
804 throw_resolution_error(this_cp, which, CHECK); |
806 } |
805 } |
807 } |
806 } |
808 |
807 |
|
808 BasicType ConstantPool::basic_type_for_constant_at(int which) { |
|
809 constantTag tag = tag_at(which); |
|
810 if (tag.is_dynamic_constant() || |
|
811 tag.is_dynamic_constant_in_error()) { |
|
812 // have to look at the signature for this one |
|
813 Symbol* constant_type = uncached_signature_ref_at(which); |
|
814 return FieldType::basic_type(constant_type); |
|
815 } |
|
816 return tag.basic_type(); |
|
817 } |
|
818 |
809 // Called to resolve constants in the constant pool and return an oop. |
819 // Called to resolve constants in the constant pool and return an oop. |
810 // Some constant pool entries cache their resolved oop. This is also |
820 // Some constant pool entries cache their resolved oop. This is also |
811 // called to create oops from constants to use in arguments for invokedynamic |
821 // called to create oops from constants to use in arguments for invokedynamic |
812 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp, int index, int cache_index, TRAPS) { |
822 oop ConstantPool::resolve_constant_at_impl(const constantPoolHandle& this_cp, |
|
823 int index, int cache_index, |
|
824 bool* status_return, TRAPS) { |
813 oop result_oop = NULL; |
825 oop result_oop = NULL; |
814 Handle throw_exception; |
826 Handle throw_exception; |
815 |
827 |
816 if (cache_index == _possible_index_sentinel) { |
828 if (cache_index == _possible_index_sentinel) { |
817 // It is possible that this constant is one which is cached in the objects. |
829 // It is possible that this constant is one which is cached in the objects. |
818 // We'll do a linear search. This should be OK because this usage is rare. |
830 // We'll do a linear search. This should be OK because this usage is rare. |
|
831 // FIXME: If bootstrap specifiers stress this code, consider putting in |
|
832 // a reverse index. Binary search over a short array should do it. |
819 assert(index > 0, "valid index"); |
833 assert(index > 0, "valid index"); |
820 cache_index = this_cp->cp_to_object_index(index); |
834 cache_index = this_cp->cp_to_object_index(index); |
821 } |
835 } |
822 assert(cache_index == _no_index_sentinel || cache_index >= 0, ""); |
836 assert(cache_index == _no_index_sentinel || cache_index >= 0, ""); |
823 assert(index == _no_index_sentinel || index >= 0, ""); |
837 assert(index == _no_index_sentinel || index >= 0, ""); |
824 |
838 |
825 if (cache_index >= 0) { |
839 if (cache_index >= 0) { |
826 result_oop = this_cp->resolved_references()->obj_at(cache_index); |
840 result_oop = this_cp->resolved_references()->obj_at(cache_index); |
827 if (result_oop != NULL) { |
841 if (result_oop != NULL) { |
|
842 if (result_oop == Universe::the_null_sentinel()) { |
|
843 DEBUG_ONLY(int temp_index = (index >= 0 ? index : this_cp->object_to_cp_index(cache_index))); |
|
844 assert(this_cp->tag_at(temp_index).is_dynamic_constant(), "only condy uses the null sentinel"); |
|
845 result_oop = NULL; |
|
846 } |
|
847 if (status_return != NULL) (*status_return) = true; |
828 return result_oop; |
848 return result_oop; |
829 // That was easy... |
849 // That was easy... |
830 } |
850 } |
831 index = this_cp->object_to_cp_index(cache_index); |
851 index = this_cp->object_to_cp_index(cache_index); |
832 } |
852 } |
833 |
853 |
834 jvalue prim_value; // temp used only in a few cases below |
854 jvalue prim_value; // temp used only in a few cases below |
835 |
855 |
836 constantTag tag = this_cp->tag_at(index); |
856 constantTag tag = this_cp->tag_at(index); |
|
857 |
|
858 if (status_return != NULL) { |
|
859 // don't trigger resolution if the constant might need it |
|
860 switch (tag.value()) { |
|
861 case JVM_CONSTANT_Class: |
|
862 { |
|
863 CPKlassSlot kslot = this_cp->klass_slot_at(index); |
|
864 int resolved_klass_index = kslot.resolved_klass_index(); |
|
865 if (this_cp->resolved_klasses()->at(resolved_klass_index) == NULL) { |
|
866 (*status_return) = false; |
|
867 return NULL; |
|
868 } |
|
869 // the klass is waiting in the CP; go get it |
|
870 break; |
|
871 } |
|
872 case JVM_CONSTANT_String: |
|
873 case JVM_CONSTANT_Integer: |
|
874 case JVM_CONSTANT_Float: |
|
875 case JVM_CONSTANT_Long: |
|
876 case JVM_CONSTANT_Double: |
|
877 // these guys trigger OOM at worst |
|
878 break; |
|
879 default: |
|
880 (*status_return) = false; |
|
881 return NULL; |
|
882 } |
|
883 // from now on there is either success or an OOME |
|
884 (*status_return) = true; |
|
885 } |
837 |
886 |
838 switch (tag.value()) { |
887 switch (tag.value()) { |
839 |
888 |
840 case JVM_CONSTANT_UnresolvedClass: |
889 case JVM_CONSTANT_UnresolvedClass: |
841 case JVM_CONSTANT_UnresolvedClassInError: |
890 case JVM_CONSTANT_UnresolvedClassInError: |
846 // ldc wants the java mirror. |
895 // ldc wants the java mirror. |
847 result_oop = resolved->java_mirror(); |
896 result_oop = resolved->java_mirror(); |
848 break; |
897 break; |
849 } |
898 } |
850 |
899 |
|
900 case JVM_CONSTANT_Dynamic: |
|
901 { |
|
902 Klass* current_klass = this_cp->pool_holder(); |
|
903 Symbol* constant_name = this_cp->uncached_name_ref_at(index); |
|
904 Symbol* constant_type = this_cp->uncached_signature_ref_at(index); |
|
905 |
|
906 // The initial step in resolving an unresolved symbolic reference to a |
|
907 // dynamically-computed constant is to resolve the symbolic reference to a |
|
908 // method handle which will be the bootstrap method for the dynamically-computed |
|
909 // constant. If resolution of the java.lang.invoke.MethodHandle for the bootstrap |
|
910 // method fails, then a MethodHandleInError is stored at the corresponding |
|
911 // bootstrap method's CP index for the CONSTANT_MethodHandle_info. No need to |
|
912 // set a DynamicConstantInError here since any subsequent use of this |
|
913 // bootstrap method will encounter the resolution of MethodHandleInError. |
|
914 oop bsm_info = this_cp->resolve_bootstrap_specifier_at(index, THREAD); |
|
915 Exceptions::wrap_dynamic_exception(CHECK_NULL); |
|
916 assert(bsm_info != NULL, ""); |
|
917 // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_Dynamic. |
|
918 Handle bootstrap_specifier = Handle(THREAD, bsm_info); |
|
919 |
|
920 // Resolve the Dynamically-Computed constant to invoke the BSM in order to obtain the resulting oop. |
|
921 Handle value = SystemDictionary::link_dynamic_constant(current_klass, |
|
922 index, |
|
923 bootstrap_specifier, |
|
924 constant_name, |
|
925 constant_type, |
|
926 THREAD); |
|
927 result_oop = value(); |
|
928 Exceptions::wrap_dynamic_exception(THREAD); |
|
929 if (HAS_PENDING_EXCEPTION) { |
|
930 // Resolution failure of the dynamically-computed constant, save_and_throw_exception |
|
931 // will check for a LinkageError and store a DynamicConstantInError. |
|
932 save_and_throw_exception(this_cp, index, tag, CHECK_NULL); |
|
933 } |
|
934 BasicType type = FieldType::basic_type(constant_type); |
|
935 if (!is_reference_type(type)) { |
|
936 // Make sure the primitive value is properly boxed. |
|
937 // This is a JDK responsibility. |
|
938 const char* fail = NULL; |
|
939 if (result_oop == NULL) { |
|
940 fail = "null result instead of box"; |
|
941 } else if (!is_java_primitive(type)) { |
|
942 // FIXME: support value types via unboxing |
|
943 fail = "can only handle references and primitives"; |
|
944 } else if (!java_lang_boxing_object::is_instance(result_oop, type)) { |
|
945 fail = "primitive is not properly boxed"; |
|
946 } |
|
947 if (fail != NULL) { |
|
948 // Since this exception is not a LinkageError, throw exception |
|
949 // but do not save a DynamicInError resolution result. |
|
950 // See section 5.4.3 of the VM spec. |
|
951 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), fail); |
|
952 } |
|
953 } |
|
954 break; |
|
955 } |
|
956 |
851 case JVM_CONSTANT_String: |
957 case JVM_CONSTANT_String: |
852 assert(cache_index != _no_index_sentinel, "should have been set"); |
958 assert(cache_index != _no_index_sentinel, "should have been set"); |
853 if (this_cp->is_pseudo_string_at(index)) { |
959 if (this_cp->is_pseudo_string_at(index)) { |
854 result_oop = this_cp->pseudo_string_at(index, cache_index); |
960 result_oop = this_cp->pseudo_string_at(index, cache_index); |
855 break; |
961 break; |
856 } |
962 } |
857 result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL); |
963 result_oop = string_at_impl(this_cp, index, cache_index, CHECK_NULL); |
858 break; |
964 break; |
859 |
965 |
|
966 case JVM_CONSTANT_DynamicInError: |
860 case JVM_CONSTANT_MethodHandleInError: |
967 case JVM_CONSTANT_MethodHandleInError: |
861 case JVM_CONSTANT_MethodTypeInError: |
968 case JVM_CONSTANT_MethodTypeInError: |
862 { |
969 { |
863 throw_resolution_error(this_cp, index, CHECK_NULL); |
970 throw_resolution_error(this_cp, index, CHECK_NULL); |
864 break; |
971 break; |
977 return str; |
1089 return str; |
978 } |
1090 } |
979 |
1091 |
980 |
1092 |
981 oop ConstantPool::resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS) { |
1093 oop ConstantPool::resolve_bootstrap_specifier_at_impl(const constantPoolHandle& this_cp, int index, TRAPS) { |
982 assert(this_cp->tag_at(index).is_invoke_dynamic(), "Corrupted constant pool"); |
1094 assert((this_cp->tag_at(index).is_invoke_dynamic() || |
983 |
1095 this_cp->tag_at(index).is_dynamic_constant()), "Corrupted constant pool"); |
984 Handle bsm; |
1096 Handle bsm; |
985 int argc; |
1097 int argc; |
986 { |
1098 { |
987 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&type], plus optional arguments |
1099 // JVM_CONSTANT_InvokeDynamic is an ordered pair of [bootm, name&mtype], plus optional arguments |
988 // The bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry. |
1100 // JVM_CONSTANT_Dynamic is an ordered pair of [bootm, name&ftype], plus optional arguments |
|
1101 // In both cases, the bootm, being a JVM_CONSTANT_MethodHandle, has its own cache entry. |
989 // It is accompanied by the optional arguments. |
1102 // It is accompanied by the optional arguments. |
990 int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index); |
1103 int bsm_index = this_cp->invoke_dynamic_bootstrap_method_ref_index_at(index); |
991 oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL); |
1104 oop bsm_oop = this_cp->resolve_possibly_cached_constant_at(bsm_index, CHECK_NULL); |
992 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) { |
1105 if (!java_lang_invoke_MethodHandle::is_instance(bsm_oop)) { |
993 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle"); |
1106 THROW_MSG_NULL(vmSymbols::java_lang_LinkageError(), "BSM not an MethodHandle"); |
994 } |
1107 } |
995 |
1108 |
996 // Extract the optional static arguments. |
1109 // Extract the optional static arguments. |
997 argc = this_cp->invoke_dynamic_argument_count_at(index); |
1110 argc = this_cp->invoke_dynamic_argument_count_at(index); |
998 if (argc == 0) return bsm_oop; |
1111 |
|
1112 // if there are no static arguments, return the bsm by itself: |
|
1113 if (argc == 0 && UseBootstrapCallInfo < 2) return bsm_oop; |
999 |
1114 |
1000 bsm = Handle(THREAD, bsm_oop); |
1115 bsm = Handle(THREAD, bsm_oop); |
1001 } |
1116 } |
1002 |
1117 |
|
1118 // We are going to return an ordered pair of {bsm, info}, using a 2-array. |
1003 objArrayHandle info; |
1119 objArrayHandle info; |
1004 { |
1120 { |
1005 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 1+argc, CHECK_NULL); |
1121 objArrayOop info_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), 2, CHECK_NULL); |
1006 info = objArrayHandle(THREAD, info_oop); |
1122 info = objArrayHandle(THREAD, info_oop); |
1007 } |
1123 } |
1008 |
1124 |
1009 info->obj_at_put(0, bsm()); |
1125 info->obj_at_put(0, bsm()); |
1010 for (int i = 0; i < argc; i++) { |
1126 |
|
1127 bool use_BSCI; |
|
1128 switch (UseBootstrapCallInfo) { |
|
1129 default: use_BSCI = true; break; // stress mode |
|
1130 case 0: use_BSCI = false; break; // stress mode |
|
1131 case 1: // normal mode |
|
1132 // If we were to support an alternative mode of BSM invocation, |
|
1133 // we'd convert to pull mode here if the BSM could be a candidate |
|
1134 // for that alternative mode. We can't easily test for things |
|
1135 // like varargs here, but we can get away with approximate testing, |
|
1136 // since the JDK runtime will make up the difference either way. |
|
1137 // For now, exercise the pull-mode path if the BSM is of arity 2, |
|
1138 // or if there is a potential condy loop (see below). |
|
1139 oop mt_oop = java_lang_invoke_MethodHandle::type(bsm()); |
|
1140 use_BSCI = (java_lang_invoke_MethodType::ptype_count(mt_oop) == 2); |
|
1141 break; |
|
1142 } |
|
1143 |
|
1144 // Here's a reason to use BSCI even if it wasn't requested: |
|
1145 // If a condy uses a condy argument, we want to avoid infinite |
|
1146 // recursion (condy loops) in the C code. It's OK in Java, |
|
1147 // because Java has stack overflow checking, so we punt |
|
1148 // potentially cyclic cases from C to Java. |
|
1149 if (!use_BSCI && this_cp->tag_at(index).is_dynamic_constant()) { |
|
1150 bool found_unresolved_condy = false; |
|
1151 for (int i = 0; i < argc; i++) { |
|
1152 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); |
|
1153 if (this_cp->tag_at(arg_index).is_dynamic_constant()) { |
|
1154 // potential recursion point condy -> condy |
|
1155 bool found_it = false; |
|
1156 this_cp->find_cached_constant_at(arg_index, found_it, CHECK_NULL); |
|
1157 if (!found_it) { found_unresolved_condy = true; break; } |
|
1158 } |
|
1159 } |
|
1160 if (found_unresolved_condy) |
|
1161 use_BSCI = true; |
|
1162 } |
|
1163 |
|
1164 const int SMALL_ARITY = 5; |
|
1165 if (use_BSCI && argc <= SMALL_ARITY && UseBootstrapCallInfo <= 2) { |
|
1166 // If there are only a few arguments, and none of them need linking, |
|
1167 // push them, instead of asking the JDK runtime to turn around and |
|
1168 // pull them, saving a JVM/JDK transition in some simple cases. |
|
1169 bool all_resolved = true; |
|
1170 for (int i = 0; i < argc; i++) { |
|
1171 bool found_it = false; |
|
1172 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); |
|
1173 this_cp->find_cached_constant_at(arg_index, found_it, CHECK_NULL); |
|
1174 if (!found_it) { all_resolved = false; break; } |
|
1175 } |
|
1176 if (all_resolved) |
|
1177 use_BSCI = false; |
|
1178 } |
|
1179 |
|
1180 if (!use_BSCI) { |
|
1181 // return {bsm, {arg...}}; resolution of arguments is done immediately, before JDK code is called |
|
1182 objArrayOop args_oop = oopFactory::new_objArray(SystemDictionary::Object_klass(), argc, CHECK_NULL); |
|
1183 info->obj_at_put(1, args_oop); // may overwrite with args[0] below |
|
1184 objArrayHandle args(THREAD, args_oop); |
|
1185 copy_bootstrap_arguments_at_impl(this_cp, index, 0, argc, args, 0, true, Handle(), CHECK_NULL); |
|
1186 if (argc == 1) { |
|
1187 // try to discard the singleton array |
|
1188 oop arg_oop = args->obj_at(0); |
|
1189 if (arg_oop != NULL && !arg_oop->is_array()) { |
|
1190 // JVM treats arrays and nulls specially in this position, |
|
1191 // but other things are just single arguments |
|
1192 info->obj_at_put(1, arg_oop); |
|
1193 } |
|
1194 } |
|
1195 } else { |
|
1196 // return {bsm, {arg_count, pool_index}}; JDK code must pull the arguments as needed |
|
1197 typeArrayOop ints_oop = oopFactory::new_typeArray(T_INT, 2, CHECK_NULL); |
|
1198 ints_oop->int_at_put(0, argc); |
|
1199 ints_oop->int_at_put(1, index); |
|
1200 info->obj_at_put(1, ints_oop); |
|
1201 } |
|
1202 return info(); |
|
1203 } |
|
1204 |
|
1205 void ConstantPool::copy_bootstrap_arguments_at_impl(const constantPoolHandle& this_cp, int index, |
|
1206 int start_arg, int end_arg, |
|
1207 objArrayHandle info, int pos, |
|
1208 bool must_resolve, Handle if_not_available, |
|
1209 TRAPS) { |
|
1210 int argc; |
|
1211 int limit = pos + end_arg - start_arg; |
|
1212 // checks: index in range [0..this_cp->length), |
|
1213 // tag at index, start..end in range [0..argc], |
|
1214 // info array non-null, pos..limit in [0..info.length] |
|
1215 if ((0 >= index || index >= this_cp->length()) || |
|
1216 !(this_cp->tag_at(index).is_invoke_dynamic() || |
|
1217 this_cp->tag_at(index).is_dynamic_constant()) || |
|
1218 (0 > start_arg || start_arg > end_arg) || |
|
1219 (end_arg > (argc = this_cp->invoke_dynamic_argument_count_at(index))) || |
|
1220 (0 > pos || pos > limit) || |
|
1221 (info.is_null() || limit > info->length())) { |
|
1222 // An index or something else went wrong; throw an error. |
|
1223 // Since this is an internal API, we don't expect this, |
|
1224 // so we don't bother to craft a nice message. |
|
1225 THROW_MSG(vmSymbols::java_lang_LinkageError(), "bad BSM argument access"); |
|
1226 } |
|
1227 // now we can loop safely |
|
1228 int info_i = pos; |
|
1229 for (int i = start_arg; i < end_arg; i++) { |
1011 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); |
1230 int arg_index = this_cp->invoke_dynamic_argument_index_at(index, i); |
1012 oop arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK_NULL); |
1231 oop arg_oop; |
1013 info->obj_at_put(1+i, arg_oop); |
1232 if (must_resolve) { |
1014 } |
1233 arg_oop = this_cp->resolve_possibly_cached_constant_at(arg_index, CHECK); |
1015 |
1234 } else { |
1016 return info(); |
1235 bool found_it = false; |
|
1236 arg_oop = this_cp->find_cached_constant_at(arg_index, found_it, CHECK); |
|
1237 if (!found_it) arg_oop = if_not_available(); |
|
1238 } |
|
1239 info->obj_at_put(info_i++, arg_oop); |
|
1240 } |
1017 } |
1241 } |
1018 |
1242 |
1019 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) { |
1243 oop ConstantPool::string_at_impl(const constantPoolHandle& this_cp, int which, int obj_index, TRAPS) { |
1020 // If the string has already been interned, this entry will be non-null |
1244 // If the string has already been interned, this entry will be non-null |
1021 oop str = this_cp->resolved_references()->obj_at(obj_index); |
1245 oop str = this_cp->resolved_references()->obj_at(obj_index); |
|
1246 assert(str != Universe::the_null_sentinel(), ""); |
1022 if (str != NULL) return str; |
1247 if (str != NULL) return str; |
1023 Symbol* sym = this_cp->unresolved_string_at(which); |
1248 Symbol* sym = this_cp->unresolved_string_at(which); |
1024 str = StringTable::intern(sym, CHECK_(NULL)); |
1249 str = StringTable::intern(sym, CHECK_(NULL)); |
1025 this_cp->string_at_put(which, obj_index, str); |
1250 this_cp->string_at_put(which, obj_index, str); |
1026 assert(java_lang_String::is_instance(str), "must be string"); |
1251 assert(java_lang_String::is_instance(str), "must be string"); |