5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "classfile/javaClasses.inline.hpp"
26 #include "classfile/stringTable.hpp"
27 #include "classfile/symbolTable.hpp"
28 #include "classfile/systemDictionary.hpp"
29 #include "classfile/vmClasses.hpp"
30 #include "code/codeCache.hpp"
31 #include "code/dependencyContext.hpp"
32 #include "compiler/compileBroker.hpp"
33 #include "interpreter/interpreter.hpp"
34 #include "interpreter/oopMapCache.hpp"
35 #include "interpreter/linkResolver.hpp"
36 #include "jvm_io.h"
37 #include "logging/log.hpp"
38 #include "logging/logStream.hpp"
39 #include "memory/allocation.inline.hpp"
40 #include "memory/oopFactory.hpp"
41 #include "memory/resourceArea.hpp"
42 #include "memory/universe.hpp"
43 #include "oops/klass.inline.hpp"
44 #include "oops/objArrayKlass.hpp"
45 #include "oops/objArrayOop.inline.hpp"
46 #include "oops/oop.inline.hpp"
47 #include "oops/typeArrayOop.inline.hpp"
48 #include "prims/methodHandles.hpp"
49 #include "runtime/deoptimization.hpp"
50 #include "runtime/fieldDescriptor.inline.hpp"
51 #include "runtime/handles.inline.hpp"
52 #include "runtime/interfaceSupport.inline.hpp"
53 #include "runtime/javaCalls.hpp"
54 #include "runtime/jniHandles.inline.hpp"
55 #include "runtime/timerTrace.hpp"
56 #include "runtime/reflection.hpp"
57 #include "runtime/safepointVerifiers.hpp"
58 #include "runtime/signature.hpp"
59 #include "runtime/stubRoutines.hpp"
60 #include "sanitizers/leak.hpp"
61 #include "utilities/exceptions.hpp"
62
63
64 /*
65 * JSR 292 reference implementation: method handles
66 * The JDK 7 reference implementation represented method handle
67 * combinations as chains. Each link in the chain had a "vmentry"
68 * field which pointed at a bit of assembly code which performed
69 * one transformation before dispatching to the next link in the chain.
70 *
71 * The current reference implementation pushes almost all code generation
72 * responsibility to (trusted) Java code. A method handle contains a
73 * pointer to its "LambdaForm", which embodies all details of the method
74 * handle's behavior. The LambdaForm is a normal Java object, managed
75 * by a runtime coded in Java.
76 */
77
78 bool MethodHandles::_enabled = false; // set true after successful native linkage
79 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = nullptr;
80
1017 int count = 0;
1018 #define INC_COUNT(scope,value) \
1019 ++count;
1020 #define CHECK_REQ(req_expr) \
1021 if (which < count) return ok; \
1022 ok = (req_expr);
1023 EACH_NAMED_CON(INC_COUNT, CHECK_REQ);
1024 #undef INC_COUNT
1025 #undef CHECK_REQ
1026 assert(count == con_value_count, "");
1027 if (which < count) return ok;
1028 return false;
1029 }
1030
1031 #undef ONE_PLUS
1032 #undef VALUE_COMMA
1033 #undef STRING_NULL
1034 #undef EACH_NAMED_CON
1035 #endif // PRODUCT
1036
1037 JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) {
1038 #ifndef PRODUCT
1039 if (advertise_con_value(which)) {
1040 assert(which >= 0 && which < con_value_count, "");
1041 int con = con_values[which];
1042 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh));
1043 if (box.not_null() && box->klass() == Universe::objectArrayKlass() && box->length() > 0) {
1044 const char* str = &con_names[0];
1045 for (int i = 0; i < which; i++)
1046 str += strlen(str) + 1; // skip name and null
1047 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint
1048 box->obj_at_put(0, name);
1049 }
1050 return con;
1051 }
1052 #endif
1053 return 0;
1054 }
1055 JVM_END
1056
1057 // void init(MemberName self, AccessibleObject ref)
1058 JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) {
1059 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
1060 if (target_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
1061 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1062 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
1063 MethodHandles::init_MemberName(mname, target, CHECK);
1064 }
1065 JVM_END
1066
1067 // void expand(MemberName self)
1068 JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1069 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
1070 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1071 MethodHandles::expand_MemberName(mname, 0, CHECK);
1072 }
1073 JVM_END
1074
1075 // void resolve(MemberName self, Class<?> caller)
1076 JVM_ENTRY(jobject, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh,
1077 jint lookup_mode, jboolean speculative_resolve)) {
1078 if (mname_jh == nullptr) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); }
1079 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1080
1081 // The trusted Java code that calls this method should already have performed
1082 // access checks on behalf of the given caller. But, we can verify this.
1083 // This only verifies from the context of the lookup class. It does not
1084 // verify the lookup context for a Lookup object teleported from one module
1085 // to another. Such Lookup object can only access the intersection of the set
1086 // of accessible classes from both lookup class and previous lookup class.
1087 if (VerifyMethodHandles && (lookup_mode & LM_TRUSTED) == LM_TRUSTED && caller_jh != nullptr &&
1088 java_lang_invoke_MemberName::clazz(mname()) != nullptr) {
1089 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
1090 if (reference_klass != nullptr && reference_klass->is_objArray_klass()) {
1091 reference_klass = ObjArrayKlass::cast(reference_klass)->bottom_klass();
1092 }
1093
1094 // Reflection::verify_class_access can only handle instance classes.
1095 if (reference_klass != nullptr && reference_klass->is_instance_klass()) {
1096 // Emulate LinkResolver::check_klass_accessability.
1142
1143 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) {
1144 if (mname == nullptr ||
1145 java_lang_invoke_MemberName::clazz(mname) == nullptr) {
1146 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved");
1147 } else {
1148 int flags = java_lang_invoke_MemberName::flags(mname);
1149 if ((flags & IS_FIELD) != 0 &&
1150 (must_be_static
1151 ? (flags & JVM_ACC_STATIC) != 0
1152 : (flags & JVM_ACC_STATIC) == 0)) {
1153 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname);
1154 return (jlong) vmindex;
1155 }
1156 }
1157 const char* msg = (must_be_static ? "static field required" : "non-static field required");
1158 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg);
1159 return 0;
1160 }
1161
1162 JVM_ENTRY(jlong, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1163 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD);
1164 }
1165 JVM_END
1166
1167 JVM_ENTRY(jlong, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1168 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD);
1169 }
1170 JVM_END
1171
1172 JVM_ENTRY(jobject, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1173 // use the other function to perform sanity checks:
1174 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL);
1175 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh));
1176 return JNIHandles::make_local(THREAD, clazz);
1177 }
1178 JVM_END
1179
1180 JVM_ENTRY(jobject, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1181 if (mname_jh == nullptr) return nullptr;
1182 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1183 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname());
1184 objArrayHandle result = oopFactory::new_objArray_handle(vmClasses::Object_klass(), 2, CHECK_NULL);
1185 jvalue vmindex_value; vmindex_value.j = (long)vmindex;
1186 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL);
1187 result->obj_at_put(0, x);
1188
1189 int flags = java_lang_invoke_MemberName::flags(mname());
1190 if ((flags & IS_FIELD) != 0) {
1191 x = java_lang_invoke_MemberName::clazz(mname());
1192 } else {
1193 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
1194 assert(vmtarget != nullptr && vmtarget->is_method(), "vmtarget is only method");
1195 x = mname();
1196 }
1197 result->obj_at_put(1, x);
1198 return JNIHandles::make_local(THREAD, result());
1199 }
1200 JVM_END
1201
1202 JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
1203 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
1204 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh));
1205 DeoptimizationScope deopt_scope;
1206 {
1207 // Walk all nmethods depending on this call site.
1208 MutexLocker mu(thread, Compile_lock);
1209 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target);
1210 java_lang_invoke_CallSite::set_target(call_site(), target());
1211 // This is assumed to be an 'atomic' operation by verification.
1212 // So keep it under lock for now.
1213 deopt_scope.deoptimize_marked();
1214 }
1215 }
1216 JVM_END
1217
1218 JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
1219 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
1220 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh));
1221 DeoptimizationScope deopt_scope;
1222 {
1223 // Walk all nmethods depending on this call site.
1224 MutexLocker mu(thread, Compile_lock);
1225 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target);
1226 java_lang_invoke_CallSite::set_target_volatile(call_site(), target());
1227 // This is assumed to be an 'atomic' operation by verification.
1228 // So keep it under lock for now.
1229 deopt_scope.deoptimize_marked();
1230 }
1231 }
1232 JVM_END
1233
1234 JVM_ENTRY(void, MHN_copyOutBootstrapArguments(JNIEnv* env, jobject igcls,
1235 jobject caller_jh, jintArray index_info_jh,
1236 jint start, jint end,
1237 jobjectArray buf_jh, jint pos,
1238 jboolean resolve, jobject ifna_jh)) {
1239 // caller->constants->copy_bootstrap_arguments_at performs a runtime
1240 // range check, but let's assert earlier as well.
1241 assert(start < end && start >= 0, "invariant");
1242 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller_jh));
1243 if (caller_k == nullptr || !caller_k->is_instance_klass()) {
1244 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad caller");
1245 }
1246 InstanceKlass* caller = InstanceKlass::cast(caller_k);
1247 typeArrayOop index_info_oop = (typeArrayOop) JNIHandles::resolve(index_info_jh);
1248 if (index_info_oop == nullptr ||
1249 index_info_oop->klass() != Universe::intArrayKlass() ||
1250 typeArrayOop(index_info_oop)->length() < 2) {
1251 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad index info (0)");
1252 }
1253 typeArrayHandle index_info(THREAD, index_info_oop);
1254 int bss_index_in_pool = index_info->int_at(1);
1258 index_info->int_at(0)
1259 != caller->constants()->bootstrap_argument_count_at(bss_index_in_pool)) {
1260 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad index info (1)");
1261 }
1262
1263 objArrayHandle buf(THREAD, (objArrayOop)JNIHandles::resolve(buf_jh));
1264
1265 Handle ifna(THREAD, JNIHandles::resolve(ifna_jh));
1266 caller->constants()->
1267 copy_bootstrap_arguments_at(bss_index_in_pool,
1268 start, end, buf, pos,
1269 (resolve == JNI_TRUE), ifna, CHECK);
1270 }
1271 JVM_END
1272
1273 /**
1274 * Throws a java/lang/UnsupportedOperationException unconditionally.
1275 * This is required by the specification of MethodHandle.invoke if
1276 * invoked directly.
1277 */
1278 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
1279 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively");
1280 return nullptr;
1281 }
1282 JVM_END
1283
1284 /**
1285 * Throws a java/lang/UnsupportedOperationException unconditionally.
1286 * This is required by the specification of MethodHandle.invokeExact if
1287 * invoked directly.
1288 */
1289 JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
1290 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively");
1291 return nullptr;
1292 }
1293 JVM_END
1294
1295 /**
1296 * Throws a java/lang/UnsupportedOperationException unconditionally.
1297 * This is required by the specification of VarHandle.{access-mode} if
1298 * invoked directly.
1299 */
1300 JVM_ENTRY(jobject, VH_UOE(JNIEnv* env, jobject vh, jobjectArray args)) {
1301 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "VarHandle access mode methods cannot be invoked reflectively");
1302 return nullptr;
1303 }
1304 JVM_END
1305
1306
1307 /// JVM_RegisterMethodHandleMethods
1308
1309 #define LANG "Ljava/lang/"
1362 {CC "getAndSet", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1363 {CC "getAndSetAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1364 {CC "getAndSetRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1365 {CC "getAndAdd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1366 {CC "getAndAddAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1367 {CC "getAndAddRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1368 {CC "getAndBitwiseOr", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1369 {CC "getAndBitwiseOrAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1370 {CC "getAndBitwiseOrRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1371 {CC "getAndBitwiseAnd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1372 {CC "getAndBitwiseAndAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1373 {CC "getAndBitwiseAndRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1374 {CC "getAndBitwiseXor", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1375 {CC "getAndBitwiseXorAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1376 {CC "getAndBitwiseXorRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}
1377 };
1378
1379 /**
1380 * This one function is exported, used by NativeLookup.
1381 */
1382 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
1383 assert(!MethodHandles::enabled(), "must not be enabled");
1384 assert(vmClasses::MethodHandle_klass() != nullptr, "should be present");
1385 assert(vmClasses::VarHandle_klass() != nullptr, "should be present");
1386
1387 oop mh_mirror = vmClasses::MethodHandle_klass()->java_mirror();
1388 oop vh_mirror = vmClasses::VarHandle_klass()->java_mirror();
1389 jclass MH_class = (jclass) JNIHandles::make_local(THREAD, mh_mirror);
1390 jclass VH_class = (jclass) JNIHandles::make_local(THREAD, vh_mirror);
1391
1392 {
1393 ThreadToNativeFromVM ttnfv(thread);
1394
1395 int status = env->RegisterNatives(MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod));
1396 guarantee(status == JNI_OK && !env->ExceptionCheck(),
1397 "register java.lang.invoke.MethodHandleNative natives");
1398
1399 status = env->RegisterNatives(MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod));
1400 guarantee(status == JNI_OK && !env->ExceptionCheck(),
1401 "register java.lang.invoke.MethodHandle natives");
1402
1403 status = env->RegisterNatives(VH_class, VH_methods, sizeof(VH_methods)/sizeof(JNINativeMethod));
1404 guarantee(status == JNI_OK && !env->ExceptionCheck(),
1405 "register java.lang.invoke.VarHandle natives");
1406 }
1407
1408 log_debug(methodhandles, indy)("MethodHandle support loaded (using LambdaForms)");
1409
1410 MethodHandles::set_enabled(true);
1411 }
1412 JVM_END
|
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "cds/cds_globals.hpp"
26 #include "classfile/javaClasses.inline.hpp"
27 #include "classfile/stringTable.hpp"
28 #include "classfile/symbolTable.hpp"
29 #include "classfile/systemDictionary.hpp"
30 #include "classfile/systemDictionaryShared.hpp"
31 #include "classfile/vmClasses.hpp"
32 #include "code/codeCache.hpp"
33 #include "code/dependencyContext.hpp"
34 #include "compiler/compileBroker.hpp"
35 #include "interpreter/interpreter.hpp"
36 #include "interpreter/oopMapCache.hpp"
37 #include "interpreter/linkResolver.hpp"
38 #include "jvm_io.h"
39 #include "logging/log.hpp"
40 #include "logging/logStream.hpp"
41 #include "memory/allocation.inline.hpp"
42 #include "memory/oopFactory.hpp"
43 #include "memory/resourceArea.hpp"
44 #include "memory/universe.hpp"
45 #include "oops/klass.inline.hpp"
46 #include "oops/objArrayKlass.hpp"
47 #include "oops/objArrayOop.inline.hpp"
48 #include "oops/oop.inline.hpp"
49 #include "oops/typeArrayOop.inline.hpp"
50 #include "prims/methodHandles.hpp"
51 #include "runtime/deoptimization.hpp"
52 #include "runtime/fieldDescriptor.inline.hpp"
53 #include "runtime/handles.inline.hpp"
54 #include "runtime/interfaceSupport.inline.hpp"
55 #include "runtime/java.hpp"
56 #include "runtime/javaCalls.hpp"
57 #include "runtime/jniHandles.inline.hpp"
58 #include "runtime/perfData.inline.hpp"
59 #include "runtime/timerTrace.hpp"
60 #include "runtime/reflection.hpp"
61 #include "runtime/safepointVerifiers.hpp"
62 #include "runtime/signature.hpp"
63 #include "runtime/stubRoutines.hpp"
64 #include "sanitizers/leak.hpp"
65 #include "services/management.hpp"
66 #include "utilities/exceptions.hpp"
67
68
69 /*
70 * JSR 292 reference implementation: method handles
71 * The JDK 7 reference implementation represented method handle
72 * combinations as chains. Each link in the chain had a "vmentry"
73 * field which pointed at a bit of assembly code which performed
74 * one transformation before dispatching to the next link in the chain.
75 *
76 * The current reference implementation pushes almost all code generation
77 * responsibility to (trusted) Java code. A method handle contains a
78 * pointer to its "LambdaForm", which embodies all details of the method
79 * handle's behavior. The LambdaForm is a normal Java object, managed
80 * by a runtime coded in Java.
81 */
82
83 bool MethodHandles::_enabled = false; // set true after successful native linkage
84 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = nullptr;
85
1022 int count = 0;
1023 #define INC_COUNT(scope,value) \
1024 ++count;
1025 #define CHECK_REQ(req_expr) \
1026 if (which < count) return ok; \
1027 ok = (req_expr);
1028 EACH_NAMED_CON(INC_COUNT, CHECK_REQ);
1029 #undef INC_COUNT
1030 #undef CHECK_REQ
1031 assert(count == con_value_count, "");
1032 if (which < count) return ok;
1033 return false;
1034 }
1035
1036 #undef ONE_PLUS
1037 #undef VALUE_COMMA
1038 #undef STRING_NULL
1039 #undef EACH_NAMED_CON
1040 #endif // PRODUCT
1041
1042 JVM_ENTRY_PROF(jint, MHN_getNamedCon, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) {
1043 #ifndef PRODUCT
1044 if (advertise_con_value(which)) {
1045 assert(which >= 0 && which < con_value_count, "");
1046 int con = con_values[which];
1047 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh));
1048 if (box.not_null() && box->klass() == Universe::objectArrayKlass() && box->length() > 0) {
1049 const char* str = &con_names[0];
1050 for (int i = 0; i < which; i++)
1051 str += strlen(str) + 1; // skip name and null
1052 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint
1053 box->obj_at_put(0, name);
1054 }
1055 return con;
1056 }
1057 #endif
1058 return 0;
1059 }
1060 JVM_END
1061
1062 // void init(MemberName self, AccessibleObject ref)
1063 JVM_ENTRY_PROF(void, MHN_init_Mem, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) {
1064 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
1065 if (target_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
1066 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1067 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
1068 MethodHandles::init_MemberName(mname, target, CHECK);
1069 }
1070 JVM_END
1071
1072 // void expand(MemberName self)
1073 JVM_ENTRY_PROF(void, MHN_expand_Mem, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1074 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
1075 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1076 MethodHandles::expand_MemberName(mname, 0, CHECK);
1077 }
1078 JVM_END
1079
1080 // void resolve(MemberName self, Class<?> caller)
1081 JVM_ENTRY_PROF(jobject, MHN_resolve_Mem, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh,
1082 jint lookup_mode, jboolean speculative_resolve)) {
1083 if (mname_jh == nullptr) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); }
1084 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1085
1086 // The trusted Java code that calls this method should already have performed
1087 // access checks on behalf of the given caller. But, we can verify this.
1088 // This only verifies from the context of the lookup class. It does not
1089 // verify the lookup context for a Lookup object teleported from one module
1090 // to another. Such Lookup object can only access the intersection of the set
1091 // of accessible classes from both lookup class and previous lookup class.
1092 if (VerifyMethodHandles && (lookup_mode & LM_TRUSTED) == LM_TRUSTED && caller_jh != nullptr &&
1093 java_lang_invoke_MemberName::clazz(mname()) != nullptr) {
1094 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
1095 if (reference_klass != nullptr && reference_klass->is_objArray_klass()) {
1096 reference_klass = ObjArrayKlass::cast(reference_klass)->bottom_klass();
1097 }
1098
1099 // Reflection::verify_class_access can only handle instance classes.
1100 if (reference_klass != nullptr && reference_klass->is_instance_klass()) {
1101 // Emulate LinkResolver::check_klass_accessability.
1147
1148 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) {
1149 if (mname == nullptr ||
1150 java_lang_invoke_MemberName::clazz(mname) == nullptr) {
1151 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved");
1152 } else {
1153 int flags = java_lang_invoke_MemberName::flags(mname);
1154 if ((flags & IS_FIELD) != 0 &&
1155 (must_be_static
1156 ? (flags & JVM_ACC_STATIC) != 0
1157 : (flags & JVM_ACC_STATIC) == 0)) {
1158 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname);
1159 return (jlong) vmindex;
1160 }
1161 }
1162 const char* msg = (must_be_static ? "static field required" : "non-static field required");
1163 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg);
1164 return 0;
1165 }
1166
1167 JVM_ENTRY_PROF(jlong, MHN_objectFieldOffset, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1168 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD);
1169 }
1170 JVM_END
1171
1172 JVM_ENTRY_PROF(jlong, MHN_staticFieldOffset, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1173 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD);
1174 }
1175 JVM_END
1176
1177 JVM_ENTRY_PROF(jobject, MHN_staticFieldBase, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1178 // use the other function to perform sanity checks:
1179 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL);
1180 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh));
1181 return JNIHandles::make_local(THREAD, clazz);
1182 }
1183 JVM_END
1184
1185 JVM_ENTRY_PROF(jobject, MHN_getMemberVMInfo, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1186 if (mname_jh == nullptr) return nullptr;
1187 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1188 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname());
1189 objArrayHandle result = oopFactory::new_objArray_handle(vmClasses::Object_klass(), 2, CHECK_NULL);
1190 jvalue vmindex_value; vmindex_value.j = (long)vmindex;
1191 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL);
1192 result->obj_at_put(0, x);
1193
1194 int flags = java_lang_invoke_MemberName::flags(mname());
1195 if ((flags & IS_FIELD) != 0) {
1196 x = java_lang_invoke_MemberName::clazz(mname());
1197 } else {
1198 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
1199 assert(vmtarget != nullptr && vmtarget->is_method(), "vmtarget is only method");
1200 x = mname();
1201 }
1202 result->obj_at_put(1, x);
1203 return JNIHandles::make_local(THREAD, result());
1204 }
1205 JVM_END
1206
1207 JVM_ENTRY_PROF(void, MHN_setCallSiteTargetNormal, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
1208 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
1209 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh));
1210 DeoptimizationScope deopt_scope;
1211 {
1212 // Walk all nmethods depending on this call site.
1213 MutexLocker mu(thread, Compile_lock);
1214 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target);
1215 java_lang_invoke_CallSite::set_target(call_site(), target());
1216 // This is assumed to be an 'atomic' operation by verification.
1217 // So keep it under lock for now.
1218 deopt_scope.deoptimize_marked();
1219 }
1220 }
1221 JVM_END
1222
1223 JVM_ENTRY_PROF(void, MHN_setCallSiteTargetVolatile, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
1224 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
1225 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh));
1226 DeoptimizationScope deopt_scope;
1227 {
1228 // Walk all nmethods depending on this call site.
1229 MutexLocker mu(thread, Compile_lock);
1230 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target);
1231 java_lang_invoke_CallSite::set_target_volatile(call_site(), target());
1232 // This is assumed to be an 'atomic' operation by verification.
1233 // So keep it under lock for now.
1234 deopt_scope.deoptimize_marked();
1235 }
1236 }
1237 JVM_END
1238
1239 JVM_ENTRY_PROF(void, MHN_copyOutBootstrapArguments, MHN_copyOutBootstrapArguments(JNIEnv* env, jobject igcls,
1240 jobject caller_jh, jintArray index_info_jh,
1241 jint start, jint end,
1242 jobjectArray buf_jh, jint pos,
1243 jboolean resolve, jobject ifna_jh)) {
1244 // caller->constants->copy_bootstrap_arguments_at performs a runtime
1245 // range check, but let's assert earlier as well.
1246 assert(start < end && start >= 0, "invariant");
1247 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller_jh));
1248 if (caller_k == nullptr || !caller_k->is_instance_klass()) {
1249 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad caller");
1250 }
1251 InstanceKlass* caller = InstanceKlass::cast(caller_k);
1252 typeArrayOop index_info_oop = (typeArrayOop) JNIHandles::resolve(index_info_jh);
1253 if (index_info_oop == nullptr ||
1254 index_info_oop->klass() != Universe::intArrayKlass() ||
1255 typeArrayOop(index_info_oop)->length() < 2) {
1256 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad index info (0)");
1257 }
1258 typeArrayHandle index_info(THREAD, index_info_oop);
1259 int bss_index_in_pool = index_info->int_at(1);
1263 index_info->int_at(0)
1264 != caller->constants()->bootstrap_argument_count_at(bss_index_in_pool)) {
1265 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad index info (1)");
1266 }
1267
1268 objArrayHandle buf(THREAD, (objArrayOop)JNIHandles::resolve(buf_jh));
1269
1270 Handle ifna(THREAD, JNIHandles::resolve(ifna_jh));
1271 caller->constants()->
1272 copy_bootstrap_arguments_at(bss_index_in_pool,
1273 start, end, buf, pos,
1274 (resolve == JNI_TRUE), ifna, CHECK);
1275 }
1276 JVM_END
1277
1278 /**
1279 * Throws a java/lang/UnsupportedOperationException unconditionally.
1280 * This is required by the specification of MethodHandle.invoke if
1281 * invoked directly.
1282 */
1283 JVM_ENTRY_PROF(jobject, MH_invoke_UOE, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
1284 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively");
1285 return nullptr;
1286 }
1287 JVM_END
1288
1289 /**
1290 * Throws a java/lang/UnsupportedOperationException unconditionally.
1291 * This is required by the specification of MethodHandle.invokeExact if
1292 * invoked directly.
1293 */
1294 JVM_ENTRY_PROF(jobject, MH_invokeExact_UOE, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
1295 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively");
1296 return nullptr;
1297 }
1298 JVM_END
1299
1300 /**
1301 * Throws a java/lang/UnsupportedOperationException unconditionally.
1302 * This is required by the specification of VarHandle.{access-mode} if
1303 * invoked directly.
1304 */
1305 JVM_ENTRY(jobject, VH_UOE(JNIEnv* env, jobject vh, jobjectArray args)) {
1306 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "VarHandle access mode methods cannot be invoked reflectively");
1307 return nullptr;
1308 }
1309 JVM_END
1310
1311
1312 /// JVM_RegisterMethodHandleMethods
1313
1314 #define LANG "Ljava/lang/"
1367 {CC "getAndSet", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1368 {CC "getAndSetAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1369 {CC "getAndSetRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1370 {CC "getAndAdd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1371 {CC "getAndAddAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1372 {CC "getAndAddRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1373 {CC "getAndBitwiseOr", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1374 {CC "getAndBitwiseOrAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1375 {CC "getAndBitwiseOrRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1376 {CC "getAndBitwiseAnd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1377 {CC "getAndBitwiseAndAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1378 {CC "getAndBitwiseAndRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1379 {CC "getAndBitwiseXor", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1380 {CC "getAndBitwiseXorAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1381 {CC "getAndBitwiseXorRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}
1382 };
1383
1384 /**
1385 * This one function is exported, used by NativeLookup.
1386 */
1387 JVM_ENTRY_PROF(void, JVM_RegisterMethodHandleMethods, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
1388 assert(!MethodHandles::enabled(), "must not be enabled");
1389 assert(vmClasses::MethodHandle_klass() != nullptr, "should be present");
1390 assert(vmClasses::VarHandle_klass() != nullptr, "should be present");
1391
1392 oop mh_mirror = vmClasses::MethodHandle_klass()->java_mirror();
1393 oop vh_mirror = vmClasses::VarHandle_klass()->java_mirror();
1394 jclass MH_class = (jclass) JNIHandles::make_local(THREAD, mh_mirror);
1395 jclass VH_class = (jclass) JNIHandles::make_local(THREAD, vh_mirror);
1396
1397 {
1398 ThreadToNativeFromVM ttnfv(thread);
1399
1400 int status = env->RegisterNatives(MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod));
1401 guarantee(status == JNI_OK && !env->ExceptionCheck(),
1402 "register java.lang.invoke.MethodHandleNative natives");
1403
1404 status = env->RegisterNatives(MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod));
1405 guarantee(status == JNI_OK && !env->ExceptionCheck(),
1406 "register java.lang.invoke.MethodHandle natives");
1407
1408 status = env->RegisterNatives(VH_class, VH_methods, sizeof(VH_methods)/sizeof(JNINativeMethod));
1409 guarantee(status == JNI_OK && !env->ExceptionCheck(),
1410 "register java.lang.invoke.VarHandle natives");
1411 }
1412
1413 log_debug(methodhandles, indy)("MethodHandle support loaded (using LambdaForms)");
1414
1415 MethodHandles::set_enabled(true);
1416 }
1417 JVM_END
1418
1419 #define DO_COUNTERS(macro) \
1420 macro(MHN_init_Mem) \
1421 macro(MHN_expand_Mem) \
1422 macro(MHN_resolve_Mem) \
1423 macro(MHN_getNamedCon) \
1424 macro(MHN_objectFieldOffset) \
1425 macro(MHN_setCallSiteTargetNormal) \
1426 macro(MHN_setCallSiteTargetVolatile) \
1427 macro(MHN_copyOutBootstrapArguments) \
1428 macro(MHN_staticFieldOffset) \
1429 macro(MHN_staticFieldBase) \
1430 macro(MHN_getMemberVMInfo) \
1431 macro(MH_invoke_UOE) \
1432 macro(MH_invokeExact_UOE) \
1433 macro(JVM_RegisterMethodHandleMethods)
1434
1435 #define INIT_COUNTER(name) \
1436 NEWPERFTICKCOUNTERS(_perf_##name##_timer, SUN_RT, #name); \
1437 NEWPERFEVENTCOUNTER(_perf_##name##_count, SUN_RT, #name "_count"); \
1438
1439 void MethodHandles::init_counters() {
1440 if (UsePerfData) {
1441 EXCEPTION_MARK;
1442
1443 DO_COUNTERS(INIT_COUNTER)
1444
1445 if (HAS_PENDING_EXCEPTION) {
1446 vm_exit_during_initialization("perf_mhn_init failed unexpectedly");
1447 }
1448 }
1449 }
1450
1451 #undef INIT_COUNTER
1452
1453 #define PRINT_COUNTER(name) {\
1454 jlong count = _perf_##name##_count->get_value(); \
1455 if (count > 0) { \
1456 st->print_cr(" %-40s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #name, \
1457 _perf_##name##_timer->elapsed_counter_value_us(), \
1458 _perf_##name##_timer->thread_counter_value_us(), \
1459 count); \
1460 }}
1461
1462 void MethodHandles::print_counters_on(outputStream* st) {
1463 if (ProfileVMCalls && UsePerfData) {
1464 DO_COUNTERS(PRINT_COUNTER)
1465 } else {
1466 st->print_cr(" MHN: no info (%s is disabled)", (UsePerfData ? "ProfileVMCalls" : "UsePerfData"));
1467 }
1468 }
1469
1470 #undef PRINT_COUNTER
|