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 "precompiled.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/vmClasses.hpp"
31 #include "code/codeCache.hpp"
32 #include "code/dependencyContext.hpp"
33 #include "compiler/compileBroker.hpp"
34 #include "interpreter/interpreter.hpp"
35 #include "interpreter/oopMapCache.hpp"
36 #include "interpreter/linkResolver.hpp"
37 #include "jvm_io.h"
38 #include "logging/log.hpp"
39 #include "logging/logStream.hpp"
40 #include "memory/allocation.inline.hpp"
41 #include "memory/oopFactory.hpp"
42 #include "memory/resourceArea.hpp"
43 #include "memory/universe.hpp"
44 #include "oops/klass.inline.hpp"
45 #include "oops/objArrayKlass.hpp"
46 #include "oops/objArrayOop.inline.hpp"
47 #include "oops/oop.inline.hpp"
48 #include "oops/typeArrayOop.inline.hpp"
49 #include "prims/methodHandles.hpp"
50 #include "runtime/deoptimization.hpp"
51 #include "runtime/fieldDescriptor.inline.hpp"
52 #include "runtime/handles.inline.hpp"
53 #include "runtime/interfaceSupport.inline.hpp"
54 #include "runtime/javaCalls.hpp"
55 #include "runtime/jniHandles.inline.hpp"
56 #include "runtime/timerTrace.hpp"
57 #include "runtime/reflection.hpp"
58 #include "runtime/safepointVerifiers.hpp"
59 #include "runtime/signature.hpp"
60 #include "runtime/stubRoutines.hpp"
61 #include "sanitizers/leak.hpp"
62 #include "utilities/exceptions.hpp"
63
64
65 /*
66 * JSR 292 reference implementation: method handles
67 * The JDK 7 reference implementation represented method handle
68 * combinations as chains. Each link in the chain had a "vmentry"
69 * field which pointed at a bit of assembly code which performed
70 * one transformation before dispatching to the next link in the chain.
71 *
72 * The current reference implementation pushes almost all code generation
73 * responsibility to (trusted) Java code. A method handle contains a
74 * pointer to its "LambdaForm", which embodies all details of the method
75 * handle's behavior. The LambdaForm is a normal Java object, managed
76 * by a runtime coded in Java.
77 */
78
79 bool MethodHandles::_enabled = false; // set true after successful native linkage
80 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = nullptr;
81
1027 int count = 0;
1028 #define INC_COUNT(scope,value) \
1029 ++count;
1030 #define CHECK_REQ(req_expr) \
1031 if (which < count) return ok; \
1032 ok = (req_expr);
1033 EACH_NAMED_CON(INC_COUNT, CHECK_REQ);
1034 #undef INC_COUNT
1035 #undef CHECK_REQ
1036 assert(count == con_value_count, "");
1037 if (which < count) return ok;
1038 return false;
1039 }
1040
1041 #undef ONE_PLUS
1042 #undef VALUE_COMMA
1043 #undef STRING_NULL
1044 #undef EACH_NAMED_CON
1045 #endif // PRODUCT
1046
1047 JVM_ENTRY(jint, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) {
1048 #ifndef PRODUCT
1049 if (advertise_con_value(which)) {
1050 assert(which >= 0 && which < con_value_count, "");
1051 int con = con_values[which];
1052 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh));
1053 if (box.not_null() && box->klass() == Universe::objectArrayKlass() && box->length() > 0) {
1054 const char* str = &con_names[0];
1055 for (int i = 0; i < which; i++)
1056 str += strlen(str) + 1; // skip name and null
1057 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint
1058 box->obj_at_put(0, name);
1059 }
1060 return con;
1061 }
1062 #endif
1063 return 0;
1064 }
1065 JVM_END
1066
1067 // void init(MemberName self, AccessibleObject ref)
1068 JVM_ENTRY(void, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) {
1069 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
1070 if (target_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
1071 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1072 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
1073 MethodHandles::init_MemberName(mname, target, CHECK);
1074 }
1075 JVM_END
1076
1077 // void expand(MemberName self)
1078 JVM_ENTRY(void, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1079 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
1080 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1081 MethodHandles::expand_MemberName(mname, 0, CHECK);
1082 }
1083 JVM_END
1084
1085 // void resolve(MemberName self, Class<?> caller)
1086 JVM_ENTRY(jobject, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh,
1087 jint lookup_mode, jboolean speculative_resolve)) {
1088 if (mname_jh == nullptr) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); }
1089 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1090
1091 // The trusted Java code that calls this method should already have performed
1092 // access checks on behalf of the given caller. But, we can verify this.
1093 // This only verifies from the context of the lookup class. It does not
1094 // verify the lookup context for a Lookup object teleported from one module
1095 // to another. Such Lookup object can only access the intersection of the set
1096 // of accessible classes from both lookup class and previous lookup class.
1097 if (VerifyMethodHandles && (lookup_mode & LM_TRUSTED) == LM_TRUSTED && caller_jh != nullptr &&
1098 java_lang_invoke_MemberName::clazz(mname()) != nullptr) {
1099 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
1100 if (reference_klass != nullptr && reference_klass->is_objArray_klass()) {
1101 reference_klass = ObjArrayKlass::cast(reference_klass)->bottom_klass();
1102 }
1103
1104 // Reflection::verify_class_access can only handle instance classes.
1105 if (reference_klass != nullptr && reference_klass->is_instance_klass()) {
1106 // Emulate LinkResolver::check_klass_accessability.
1152
1153 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) {
1154 if (mname == nullptr ||
1155 java_lang_invoke_MemberName::clazz(mname) == nullptr) {
1156 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved");
1157 } else {
1158 int flags = java_lang_invoke_MemberName::flags(mname);
1159 if ((flags & IS_FIELD) != 0 &&
1160 (must_be_static
1161 ? (flags & JVM_ACC_STATIC) != 0
1162 : (flags & JVM_ACC_STATIC) == 0)) {
1163 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname);
1164 return (jlong) vmindex;
1165 }
1166 }
1167 const char* msg = (must_be_static ? "static field required" : "non-static field required");
1168 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg);
1169 return 0;
1170 }
1171
1172 JVM_ENTRY(jlong, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1173 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD);
1174 }
1175 JVM_END
1176
1177 JVM_ENTRY(jlong, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1178 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD);
1179 }
1180 JVM_END
1181
1182 JVM_ENTRY(jobject, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1183 // use the other function to perform sanity checks:
1184 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL);
1185 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh));
1186 return JNIHandles::make_local(THREAD, clazz);
1187 }
1188 JVM_END
1189
1190 JVM_ENTRY(jobject, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1191 if (mname_jh == nullptr) return nullptr;
1192 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1193 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname());
1194 objArrayHandle result = oopFactory::new_objArray_handle(vmClasses::Object_klass(), 2, CHECK_NULL);
1195 jvalue vmindex_value; vmindex_value.j = (long)vmindex;
1196 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL);
1197 result->obj_at_put(0, x);
1198
1199 int flags = java_lang_invoke_MemberName::flags(mname());
1200 if ((flags & IS_FIELD) != 0) {
1201 x = java_lang_invoke_MemberName::clazz(mname());
1202 } else {
1203 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
1204 assert(vmtarget != nullptr && vmtarget->is_method(), "vmtarget is only method");
1205 x = mname();
1206 }
1207 result->obj_at_put(1, x);
1208 return JNIHandles::make_local(THREAD, result());
1209 }
1210 JVM_END
1211
1212 JVM_ENTRY(void, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
1213 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
1214 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh));
1215 DeoptimizationScope deopt_scope;
1216 {
1217 // Walk all nmethods depending on this call site.
1218 MutexLocker mu(thread, Compile_lock);
1219 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target);
1220 java_lang_invoke_CallSite::set_target(call_site(), target());
1221 // This is assumed to be an 'atomic' operation by verification.
1222 // So keep it under lock for now.
1223 deopt_scope.deoptimize_marked();
1224 }
1225 }
1226 JVM_END
1227
1228 JVM_ENTRY(void, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
1229 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
1230 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh));
1231 DeoptimizationScope deopt_scope;
1232 {
1233 // Walk all nmethods depending on this call site.
1234 MutexLocker mu(thread, Compile_lock);
1235 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target);
1236 java_lang_invoke_CallSite::set_target_volatile(call_site(), target());
1237 // This is assumed to be an 'atomic' operation by verification.
1238 // So keep it under lock for now.
1239 deopt_scope.deoptimize_marked();
1240 }
1241 }
1242 JVM_END
1243
1244 JVM_ENTRY(void, MHN_copyOutBootstrapArguments(JNIEnv* env, jobject igcls,
1245 jobject caller_jh, jintArray index_info_jh,
1246 jint start, jint end,
1247 jobjectArray buf_jh, jint pos,
1248 jboolean resolve, jobject ifna_jh)) {
1249 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller_jh));
1250 if (caller_k == nullptr || !caller_k->is_instance_klass()) {
1251 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad caller");
1252 }
1253 InstanceKlass* caller = InstanceKlass::cast(caller_k);
1254 typeArrayOop index_info_oop = (typeArrayOop) JNIHandles::resolve(index_info_jh);
1255 if (index_info_oop == nullptr ||
1256 index_info_oop->klass() != Universe::intArrayKlass() ||
1257 typeArrayOop(index_info_oop)->length() < 2) {
1258 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad index info (0)");
1259 }
1260 typeArrayHandle index_info(THREAD, index_info_oop);
1261 int bss_index_in_pool = index_info->int_at(1);
1262 // While we are here, take a quick look at the index info:
1263 if (bss_index_in_pool <= 0 ||
1264 bss_index_in_pool >= caller->constants()->length() ||
1307 }
1308 }
1309
1310 // Store the pseudo-argument, and advance the pointers.
1311 buf->obj_at_put(pos++, pseudo_arg);
1312 ++start;
1313 }
1314 }
1315 // When we are done with this there may be regular arguments to process too.
1316 }
1317 Handle ifna(THREAD, JNIHandles::resolve(ifna_jh));
1318 caller->constants()->
1319 copy_bootstrap_arguments_at(bss_index_in_pool,
1320 start, end, buf, pos,
1321 (resolve == JNI_TRUE), ifna, CHECK);
1322 }
1323 JVM_END
1324
1325 // It is called by a Cleaner object which ensures that dropped CallSites properly
1326 // deallocate their dependency information.
1327 JVM_ENTRY(void, MHN_clearCallSiteContext(JNIEnv* env, jobject igcls, jobject context_jh)) {
1328 Handle context(THREAD, JNIHandles::resolve_non_null(context_jh));
1329 DeoptimizationScope deopt_scope;
1330 {
1331 NoSafepointVerifier nsv;
1332 MutexLocker ml(THREAD, CodeCache_lock, Mutex::_no_safepoint_check_flag);
1333 DependencyContext deps = java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(context());
1334 deps.remove_and_mark_for_deoptimization_all_dependents(&deopt_scope);
1335 // This is assumed to be an 'atomic' operation by verification.
1336 // So keep it under lock for now.
1337 deopt_scope.deoptimize_marked();
1338 }
1339 }
1340 JVM_END
1341
1342 /**
1343 * Throws a java/lang/UnsupportedOperationException unconditionally.
1344 * This is required by the specification of MethodHandle.invoke if
1345 * invoked directly.
1346 */
1347 JVM_ENTRY(jobject, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
1348 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively");
1349 return nullptr;
1350 }
1351 JVM_END
1352
1353 /**
1354 * Throws a java/lang/UnsupportedOperationException unconditionally.
1355 * This is required by the specification of MethodHandle.invokeExact if
1356 * invoked directly.
1357 */
1358 JVM_ENTRY(jobject, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
1359 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively");
1360 return nullptr;
1361 }
1362 JVM_END
1363
1364 /**
1365 * Throws a java/lang/UnsupportedOperationException unconditionally.
1366 * This is required by the specification of VarHandle.{access-mode} if
1367 * invoked directly.
1368 */
1369 JVM_ENTRY(jobject, VH_UOE(JNIEnv* env, jobject vh, jobjectArray args)) {
1370 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "VarHandle access mode methods cannot be invoked reflectively");
1371 return nullptr;
1372 }
1373 JVM_END
1374
1375
1376 /// JVM_RegisterMethodHandleMethods
1377
1378 #define LANG "Ljava/lang/"
1379 #define JLINV "Ljava/lang/invoke/"
1380
1381 #define OBJ LANG "Object;"
1382 #define CLS LANG "Class;"
1383 #define STRG LANG "String;"
1384 #define CS JLINV "CallSite;"
1385 #define MT JLINV "MethodType;"
1386 #define MH JLINV "MethodHandle;"
1387 #define MEM JLINV "MemberName;"
1388 #define CTX JLINV "MethodHandleNatives$CallSiteContext;"
1389
1390 #define CC (char*) /*cast a literal from (const char*)*/
1391 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
1392
1393 // These are the native methods on java.lang.invoke.MethodHandleNatives.
1394 static JNINativeMethod MHN_methods[] = {
1395 {CC "init", CC "(" MEM "" OBJ ")V", FN_PTR(MHN_init_Mem)},
1396 {CC "expand", CC "(" MEM ")V", FN_PTR(MHN_expand_Mem)},
1397 {CC "resolve", CC "(" MEM "" CLS "IZ)" MEM, FN_PTR(MHN_resolve_Mem)},
1398 // static native int getNamedCon(int which, Object[] name)
1399 {CC "getNamedCon", CC "(I[" OBJ ")I", FN_PTR(MHN_getNamedCon)},
1400 {CC "objectFieldOffset", CC "(" MEM ")J", FN_PTR(MHN_objectFieldOffset)},
1401 {CC "setCallSiteTargetNormal", CC "(" CS "" MH ")V", FN_PTR(MHN_setCallSiteTargetNormal)},
1402 {CC "setCallSiteTargetVolatile", CC "(" CS "" MH ")V", FN_PTR(MHN_setCallSiteTargetVolatile)},
1403 {CC "copyOutBootstrapArguments", CC "(" CLS "[III[" OBJ "IZ" OBJ ")V", FN_PTR(MHN_copyOutBootstrapArguments)},
1404 {CC "clearCallSiteContext", CC "(" CTX ")V", FN_PTR(MHN_clearCallSiteContext)},
1405 {CC "staticFieldOffset", CC "(" MEM ")J", FN_PTR(MHN_staticFieldOffset)},
1406 {CC "staticFieldBase", CC "(" MEM ")" OBJ, FN_PTR(MHN_staticFieldBase)},
1407 {CC "getMemberVMInfo", CC "(" MEM ")" OBJ, FN_PTR(MHN_getMemberVMInfo)}
1408 };
1409
1410 static JNINativeMethod MH_methods[] = {
1411 // UnsupportedOperationException throwers
1412 {CC "invoke", CC "([" OBJ ")" OBJ, FN_PTR(MH_invoke_UOE)},
1413 {CC "invokeExact", CC "([" OBJ ")" OBJ, FN_PTR(MH_invokeExact_UOE)}
1414 };
1415 static JNINativeMethod VH_methods[] = {
1416 // UnsupportedOperationException throwers
1417 {CC "get", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1433 {CC "getAndSet", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1434 {CC "getAndSetAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1435 {CC "getAndSetRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1436 {CC "getAndAdd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1437 {CC "getAndAddAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1438 {CC "getAndAddRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1439 {CC "getAndBitwiseOr", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1440 {CC "getAndBitwiseOrAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1441 {CC "getAndBitwiseOrRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1442 {CC "getAndBitwiseAnd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1443 {CC "getAndBitwiseAndAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1444 {CC "getAndBitwiseAndRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1445 {CC "getAndBitwiseXor", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1446 {CC "getAndBitwiseXorAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1447 {CC "getAndBitwiseXorRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}
1448 };
1449
1450 /**
1451 * This one function is exported, used by NativeLookup.
1452 */
1453 JVM_ENTRY(void, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
1454 assert(!MethodHandles::enabled(), "must not be enabled");
1455 assert(vmClasses::MethodHandle_klass() != nullptr, "should be present");
1456 assert(vmClasses::VarHandle_klass() != nullptr, "should be present");
1457
1458 oop mh_mirror = vmClasses::MethodHandle_klass()->java_mirror();
1459 oop vh_mirror = vmClasses::VarHandle_klass()->java_mirror();
1460 jclass MH_class = (jclass) JNIHandles::make_local(THREAD, mh_mirror);
1461 jclass VH_class = (jclass) JNIHandles::make_local(THREAD, vh_mirror);
1462
1463 {
1464 ThreadToNativeFromVM ttnfv(thread);
1465
1466 int status = env->RegisterNatives(MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod));
1467 guarantee(status == JNI_OK && !env->ExceptionOccurred(),
1468 "register java.lang.invoke.MethodHandleNative natives");
1469
1470 status = env->RegisterNatives(MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod));
1471 guarantee(status == JNI_OK && !env->ExceptionOccurred(),
1472 "register java.lang.invoke.MethodHandle natives");
1473
1474 status = env->RegisterNatives(VH_class, VH_methods, sizeof(VH_methods)/sizeof(JNINativeMethod));
1475 guarantee(status == JNI_OK && !env->ExceptionOccurred(),
1476 "register java.lang.invoke.VarHandle natives");
1477 }
1478
1479 log_debug(methodhandles, indy)("MethodHandle support loaded (using LambdaForms)");
1480
1481 MethodHandles::set_enabled(true);
1482 }
1483 JVM_END
|
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 "precompiled.hpp"
26 #include "cds/cds_globals.hpp"
27 #include "classfile/javaClasses.inline.hpp"
28 #include "classfile/stringTable.hpp"
29 #include "classfile/symbolTable.hpp"
30 #include "classfile/systemDictionary.hpp"
31 #include "classfile/systemDictionaryShared.hpp"
32 #include "classfile/vmClasses.hpp"
33 #include "code/codeCache.hpp"
34 #include "code/dependencyContext.hpp"
35 #include "compiler/compileBroker.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "interpreter/oopMapCache.hpp"
38 #include "interpreter/linkResolver.hpp"
39 #include "jvm_io.h"
40 #include "logging/log.hpp"
41 #include "logging/logStream.hpp"
42 #include "memory/allocation.inline.hpp"
43 #include "memory/oopFactory.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "memory/universe.hpp"
46 #include "oops/klass.inline.hpp"
47 #include "oops/objArrayKlass.hpp"
48 #include "oops/objArrayOop.inline.hpp"
49 #include "oops/oop.inline.hpp"
50 #include "oops/typeArrayOop.inline.hpp"
51 #include "prims/methodHandles.hpp"
52 #include "runtime/deoptimization.hpp"
53 #include "runtime/fieldDescriptor.inline.hpp"
54 #include "runtime/handles.inline.hpp"
55 #include "runtime/interfaceSupport.inline.hpp"
56 #include "runtime/java.hpp"
57 #include "runtime/javaCalls.hpp"
58 #include "runtime/jniHandles.inline.hpp"
59 #include "runtime/perfData.inline.hpp"
60 #include "runtime/timerTrace.hpp"
61 #include "runtime/reflection.hpp"
62 #include "runtime/safepointVerifiers.hpp"
63 #include "runtime/signature.hpp"
64 #include "runtime/stubRoutines.hpp"
65 #include "sanitizers/leak.hpp"
66 #include "services/management.hpp"
67 #include "utilities/exceptions.hpp"
68
69
70 /*
71 * JSR 292 reference implementation: method handles
72 * The JDK 7 reference implementation represented method handle
73 * combinations as chains. Each link in the chain had a "vmentry"
74 * field which pointed at a bit of assembly code which performed
75 * one transformation before dispatching to the next link in the chain.
76 *
77 * The current reference implementation pushes almost all code generation
78 * responsibility to (trusted) Java code. A method handle contains a
79 * pointer to its "LambdaForm", which embodies all details of the method
80 * handle's behavior. The LambdaForm is a normal Java object, managed
81 * by a runtime coded in Java.
82 */
83
84 bool MethodHandles::_enabled = false; // set true after successful native linkage
85 MethodHandlesAdapterBlob* MethodHandles::_adapter_code = nullptr;
86
1032 int count = 0;
1033 #define INC_COUNT(scope,value) \
1034 ++count;
1035 #define CHECK_REQ(req_expr) \
1036 if (which < count) return ok; \
1037 ok = (req_expr);
1038 EACH_NAMED_CON(INC_COUNT, CHECK_REQ);
1039 #undef INC_COUNT
1040 #undef CHECK_REQ
1041 assert(count == con_value_count, "");
1042 if (which < count) return ok;
1043 return false;
1044 }
1045
1046 #undef ONE_PLUS
1047 #undef VALUE_COMMA
1048 #undef STRING_NULL
1049 #undef EACH_NAMED_CON
1050 #endif // PRODUCT
1051
1052 JVM_ENTRY_PROF(jint, MHN_getNamedCon, MHN_getNamedCon(JNIEnv *env, jobject igcls, jint which, jobjectArray box_jh)) {
1053 #ifndef PRODUCT
1054 if (advertise_con_value(which)) {
1055 assert(which >= 0 && which < con_value_count, "");
1056 int con = con_values[which];
1057 objArrayHandle box(THREAD, (objArrayOop) JNIHandles::resolve(box_jh));
1058 if (box.not_null() && box->klass() == Universe::objectArrayKlass() && box->length() > 0) {
1059 const char* str = &con_names[0];
1060 for (int i = 0; i < which; i++)
1061 str += strlen(str) + 1; // skip name and null
1062 oop name = java_lang_String::create_oop_from_str(str, CHECK_0); // possible safepoint
1063 box->obj_at_put(0, name);
1064 }
1065 return con;
1066 }
1067 #endif
1068 return 0;
1069 }
1070 JVM_END
1071
1072 // void init(MemberName self, AccessibleObject ref)
1073 JVM_ENTRY_PROF(void, MHN_init_Mem, MHN_init_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jobject target_jh)) {
1074 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
1075 if (target_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "target is null"); }
1076 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1077 Handle target(THREAD, JNIHandles::resolve_non_null(target_jh));
1078 MethodHandles::init_MemberName(mname, target, CHECK);
1079 }
1080 JVM_END
1081
1082 // void expand(MemberName self)
1083 JVM_ENTRY_PROF(void, MHN_expand_Mem, MHN_expand_Mem(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1084 if (mname_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "mname is null"); }
1085 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1086 MethodHandles::expand_MemberName(mname, 0, CHECK);
1087 }
1088 JVM_END
1089
1090 // void resolve(MemberName self, Class<?> caller)
1091 JVM_ENTRY_PROF(jobject, MHN_resolve_Mem, MHN_resolve_Mem(JNIEnv *env, jobject igcls, jobject mname_jh, jclass caller_jh,
1092 jint lookup_mode, jboolean speculative_resolve)) {
1093 if (mname_jh == nullptr) { THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "mname is null"); }
1094 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1095
1096 // The trusted Java code that calls this method should already have performed
1097 // access checks on behalf of the given caller. But, we can verify this.
1098 // This only verifies from the context of the lookup class. It does not
1099 // verify the lookup context for a Lookup object teleported from one module
1100 // to another. Such Lookup object can only access the intersection of the set
1101 // of accessible classes from both lookup class and previous lookup class.
1102 if (VerifyMethodHandles && (lookup_mode & LM_TRUSTED) == LM_TRUSTED && caller_jh != nullptr &&
1103 java_lang_invoke_MemberName::clazz(mname()) != nullptr) {
1104 Klass* reference_klass = java_lang_Class::as_Klass(java_lang_invoke_MemberName::clazz(mname()));
1105 if (reference_klass != nullptr && reference_klass->is_objArray_klass()) {
1106 reference_klass = ObjArrayKlass::cast(reference_klass)->bottom_klass();
1107 }
1108
1109 // Reflection::verify_class_access can only handle instance classes.
1110 if (reference_klass != nullptr && reference_klass->is_instance_klass()) {
1111 // Emulate LinkResolver::check_klass_accessability.
1157
1158 static jlong find_member_field_offset(oop mname, bool must_be_static, TRAPS) {
1159 if (mname == nullptr ||
1160 java_lang_invoke_MemberName::clazz(mname) == nullptr) {
1161 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "mname not resolved");
1162 } else {
1163 int flags = java_lang_invoke_MemberName::flags(mname);
1164 if ((flags & IS_FIELD) != 0 &&
1165 (must_be_static
1166 ? (flags & JVM_ACC_STATIC) != 0
1167 : (flags & JVM_ACC_STATIC) == 0)) {
1168 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname);
1169 return (jlong) vmindex;
1170 }
1171 }
1172 const char* msg = (must_be_static ? "static field required" : "non-static field required");
1173 THROW_MSG_0(vmSymbols::java_lang_InternalError(), msg);
1174 return 0;
1175 }
1176
1177 JVM_ENTRY_PROF(jlong, MHN_objectFieldOffset, MHN_objectFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1178 return find_member_field_offset(JNIHandles::resolve(mname_jh), false, THREAD);
1179 }
1180 JVM_END
1181
1182 JVM_ENTRY_PROF(jlong, MHN_staticFieldOffset, MHN_staticFieldOffset(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1183 return find_member_field_offset(JNIHandles::resolve(mname_jh), true, THREAD);
1184 }
1185 JVM_END
1186
1187 JVM_ENTRY_PROF(jobject, MHN_staticFieldBase, MHN_staticFieldBase(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1188 // use the other function to perform sanity checks:
1189 jlong ignore = find_member_field_offset(JNIHandles::resolve(mname_jh), true, CHECK_NULL);
1190 oop clazz = java_lang_invoke_MemberName::clazz(JNIHandles::resolve_non_null(mname_jh));
1191 return JNIHandles::make_local(THREAD, clazz);
1192 }
1193 JVM_END
1194
1195 JVM_ENTRY_PROF(jobject, MHN_getMemberVMInfo, MHN_getMemberVMInfo(JNIEnv *env, jobject igcls, jobject mname_jh)) {
1196 if (mname_jh == nullptr) return nullptr;
1197 Handle mname(THREAD, JNIHandles::resolve_non_null(mname_jh));
1198 intptr_t vmindex = java_lang_invoke_MemberName::vmindex(mname());
1199 objArrayHandle result = oopFactory::new_objArray_handle(vmClasses::Object_klass(), 2, CHECK_NULL);
1200 jvalue vmindex_value; vmindex_value.j = (long)vmindex;
1201 oop x = java_lang_boxing_object::create(T_LONG, &vmindex_value, CHECK_NULL);
1202 result->obj_at_put(0, x);
1203
1204 int flags = java_lang_invoke_MemberName::flags(mname());
1205 if ((flags & IS_FIELD) != 0) {
1206 x = java_lang_invoke_MemberName::clazz(mname());
1207 } else {
1208 Method* vmtarget = java_lang_invoke_MemberName::vmtarget(mname());
1209 assert(vmtarget != nullptr && vmtarget->is_method(), "vmtarget is only method");
1210 x = mname();
1211 }
1212 result->obj_at_put(1, x);
1213 return JNIHandles::make_local(THREAD, result());
1214 }
1215 JVM_END
1216
1217 JVM_ENTRY_PROF(void, MHN_setCallSiteTargetNormal, MHN_setCallSiteTargetNormal(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
1218 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
1219 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh));
1220 DeoptimizationScope deopt_scope;
1221 {
1222 // Walk all nmethods depending on this call site.
1223 MutexLocker mu(thread, Compile_lock);
1224 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target);
1225 java_lang_invoke_CallSite::set_target(call_site(), target());
1226 // This is assumed to be an 'atomic' operation by verification.
1227 // So keep it under lock for now.
1228 deopt_scope.deoptimize_marked();
1229 }
1230 }
1231 JVM_END
1232
1233 // Make sure the class is linked. If the class cannot be verified, etc, an exception will be thrown.
1234 JVM_ENTRY_PROF(void, MHN_checkArchivable, MHN_checkArchivable(JNIEnv *env, jobject igcls, jclass klass_jh)) {
1235 if (klass_jh == nullptr) { THROW_MSG(vmSymbols::java_lang_InternalError(), "klass is null"); }
1236
1237 if (CDSConfig::is_dumping_invokedynamic()) {
1238 Klass* klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(klass_jh));
1239 if (klass != nullptr && klass->is_instance_klass()) {
1240 // klass could be null during very early VM start-up
1241 InstanceKlass* ik = InstanceKlass::cast(klass);
1242 ik->link_class(THREAD); // exception will be thrown if unverifiable
1243 if (!ik->is_linked()) {
1244 assert(HAS_PENDING_EXCEPTION, "must be");
1245 ResourceMark rm;
1246 log_warning(cds)("Cannot use unverifiable class %s in MethodType",
1247 klass->external_name());
1248 return;
1249 }
1250
1251 if (SystemDictionaryShared::is_jfr_event_class(ik)) {
1252 ResourceMark rm;
1253 log_warning(cds)("Cannot use JFR event class %s in MethodType",
1254 klass->external_name());
1255 Exceptions::fthrow(THREAD_AND_LOCATION,
1256 vmSymbols::java_lang_NoClassDefFoundError(),
1257 "Class %s, or one of its supertypes, is a JFR event class",
1258 klass->external_name());
1259 }
1260 }
1261 }
1262 }
1263 JVM_END
1264
1265 JVM_ENTRY_PROF(void, MHN_setCallSiteTargetVolatile, MHN_setCallSiteTargetVolatile(JNIEnv* env, jobject igcls, jobject call_site_jh, jobject target_jh)) {
1266 Handle call_site(THREAD, JNIHandles::resolve_non_null(call_site_jh));
1267 Handle target (THREAD, JNIHandles::resolve_non_null(target_jh));
1268 DeoptimizationScope deopt_scope;
1269 {
1270 // Walk all nmethods depending on this call site.
1271 MutexLocker mu(thread, Compile_lock);
1272 MethodHandles::mark_dependent_nmethods(&deopt_scope, call_site, target);
1273 java_lang_invoke_CallSite::set_target_volatile(call_site(), target());
1274 // This is assumed to be an 'atomic' operation by verification.
1275 // So keep it under lock for now.
1276 deopt_scope.deoptimize_marked();
1277 }
1278 }
1279 JVM_END
1280
1281 JVM_ENTRY_PROF(void, MHN_copyOutBootstrapArguments, MHN_copyOutBootstrapArguments(JNIEnv* env, jobject igcls,
1282 jobject caller_jh, jintArray index_info_jh,
1283 jint start, jint end,
1284 jobjectArray buf_jh, jint pos,
1285 jboolean resolve, jobject ifna_jh)) {
1286 Klass* caller_k = java_lang_Class::as_Klass(JNIHandles::resolve(caller_jh));
1287 if (caller_k == nullptr || !caller_k->is_instance_klass()) {
1288 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad caller");
1289 }
1290 InstanceKlass* caller = InstanceKlass::cast(caller_k);
1291 typeArrayOop index_info_oop = (typeArrayOop) JNIHandles::resolve(index_info_jh);
1292 if (index_info_oop == nullptr ||
1293 index_info_oop->klass() != Universe::intArrayKlass() ||
1294 typeArrayOop(index_info_oop)->length() < 2) {
1295 THROW_MSG(vmSymbols::java_lang_InternalError(), "bad index info (0)");
1296 }
1297 typeArrayHandle index_info(THREAD, index_info_oop);
1298 int bss_index_in_pool = index_info->int_at(1);
1299 // While we are here, take a quick look at the index info:
1300 if (bss_index_in_pool <= 0 ||
1301 bss_index_in_pool >= caller->constants()->length() ||
1344 }
1345 }
1346
1347 // Store the pseudo-argument, and advance the pointers.
1348 buf->obj_at_put(pos++, pseudo_arg);
1349 ++start;
1350 }
1351 }
1352 // When we are done with this there may be regular arguments to process too.
1353 }
1354 Handle ifna(THREAD, JNIHandles::resolve(ifna_jh));
1355 caller->constants()->
1356 copy_bootstrap_arguments_at(bss_index_in_pool,
1357 start, end, buf, pos,
1358 (resolve == JNI_TRUE), ifna, CHECK);
1359 }
1360 JVM_END
1361
1362 // It is called by a Cleaner object which ensures that dropped CallSites properly
1363 // deallocate their dependency information.
1364 JVM_ENTRY_PROF(void, MHN_clearCallSiteContext, MHN_clearCallSiteContext(JNIEnv* env, jobject igcls, jobject context_jh)) {
1365 Handle context(THREAD, JNIHandles::resolve_non_null(context_jh));
1366 DeoptimizationScope deopt_scope;
1367 {
1368 NoSafepointVerifier nsv;
1369 MutexLocker ml(THREAD, CodeCache_lock, Mutex::_no_safepoint_check_flag);
1370 DependencyContext deps = java_lang_invoke_MethodHandleNatives_CallSiteContext::vmdependencies(context());
1371 deps.remove_and_mark_for_deoptimization_all_dependents(&deopt_scope);
1372 // This is assumed to be an 'atomic' operation by verification.
1373 // So keep it under lock for now.
1374 deopt_scope.deoptimize_marked();
1375 }
1376 }
1377 JVM_END
1378
1379 /**
1380 * Throws a java/lang/UnsupportedOperationException unconditionally.
1381 * This is required by the specification of MethodHandle.invoke if
1382 * invoked directly.
1383 */
1384 JVM_ENTRY_PROF(jobject, MH_invoke_UOE, MH_invoke_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
1385 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invoke cannot be invoked reflectively");
1386 return nullptr;
1387 }
1388 JVM_END
1389
1390 /**
1391 * Throws a java/lang/UnsupportedOperationException unconditionally.
1392 * This is required by the specification of MethodHandle.invokeExact if
1393 * invoked directly.
1394 */
1395 JVM_ENTRY_PROF(jobject, MH_invokeExact_UOE, MH_invokeExact_UOE(JNIEnv* env, jobject mh, jobjectArray args)) {
1396 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "MethodHandle.invokeExact cannot be invoked reflectively");
1397 return nullptr;
1398 }
1399 JVM_END
1400
1401 /**
1402 * Throws a java/lang/UnsupportedOperationException unconditionally.
1403 * This is required by the specification of VarHandle.{access-mode} if
1404 * invoked directly.
1405 */
1406 JVM_ENTRY(jobject, VH_UOE(JNIEnv* env, jobject vh, jobjectArray args)) {
1407 THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "VarHandle access mode methods cannot be invoked reflectively");
1408 return nullptr;
1409 }
1410 JVM_END
1411
1412
1413 /// JVM_RegisterMethodHandleMethods
1414
1415 #define LANG "Ljava/lang/"
1416 #define JLINV "Ljava/lang/invoke/"
1417
1418 #define OBJ LANG "Object;"
1419 #define CLS LANG "Class;"
1420 #define STRG LANG "String;"
1421 #define CS JLINV "CallSite;"
1422 #define MT JLINV "MethodType;"
1423 #define MH JLINV "MethodHandle;"
1424 #define MEM JLINV "MemberName;"
1425 #define CTX JLINV "MethodHandleNatives$CallSiteContext;"
1426
1427 #define CC (char*) /*cast a literal from (const char*)*/
1428 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f)
1429
1430 // These are the native methods on java.lang.invoke.MethodHandleNatives.
1431 static JNINativeMethod MHN_methods[] = {
1432 {CC "init", CC "(" MEM "" OBJ ")V", FN_PTR(MHN_init_Mem)},
1433 {CC "expand", CC "(" MEM ")V", FN_PTR(MHN_expand_Mem)},
1434 {CC "resolve", CC "(" MEM "" CLS "IZ)" MEM, FN_PTR(MHN_resolve_Mem)},
1435 {CC "checkArchivable", CC "(" CLS ")V", FN_PTR(MHN_checkArchivable)},
1436 // static native int getNamedCon(int which, Object[] name)
1437 {CC "getNamedCon", CC "(I[" OBJ ")I", FN_PTR(MHN_getNamedCon)},
1438 {CC "objectFieldOffset", CC "(" MEM ")J", FN_PTR(MHN_objectFieldOffset)},
1439 {CC "setCallSiteTargetNormal", CC "(" CS "" MH ")V", FN_PTR(MHN_setCallSiteTargetNormal)},
1440 {CC "setCallSiteTargetVolatile", CC "(" CS "" MH ")V", FN_PTR(MHN_setCallSiteTargetVolatile)},
1441 {CC "copyOutBootstrapArguments", CC "(" CLS "[III[" OBJ "IZ" OBJ ")V", FN_PTR(MHN_copyOutBootstrapArguments)},
1442 {CC "clearCallSiteContext", CC "(" CTX ")V", FN_PTR(MHN_clearCallSiteContext)},
1443 {CC "staticFieldOffset", CC "(" MEM ")J", FN_PTR(MHN_staticFieldOffset)},
1444 {CC "staticFieldBase", CC "(" MEM ")" OBJ, FN_PTR(MHN_staticFieldBase)},
1445 {CC "getMemberVMInfo", CC "(" MEM ")" OBJ, FN_PTR(MHN_getMemberVMInfo)}
1446 };
1447
1448 static JNINativeMethod MH_methods[] = {
1449 // UnsupportedOperationException throwers
1450 {CC "invoke", CC "([" OBJ ")" OBJ, FN_PTR(MH_invoke_UOE)},
1451 {CC "invokeExact", CC "([" OBJ ")" OBJ, FN_PTR(MH_invokeExact_UOE)}
1452 };
1453 static JNINativeMethod VH_methods[] = {
1454 // UnsupportedOperationException throwers
1455 {CC "get", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1471 {CC "getAndSet", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1472 {CC "getAndSetAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1473 {CC "getAndSetRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1474 {CC "getAndAdd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1475 {CC "getAndAddAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1476 {CC "getAndAddRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1477 {CC "getAndBitwiseOr", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1478 {CC "getAndBitwiseOrAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1479 {CC "getAndBitwiseOrRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1480 {CC "getAndBitwiseAnd", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1481 {CC "getAndBitwiseAndAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1482 {CC "getAndBitwiseAndRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1483 {CC "getAndBitwiseXor", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1484 {CC "getAndBitwiseXorAcquire", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)},
1485 {CC "getAndBitwiseXorRelease", CC "([" OBJ ")" OBJ, FN_PTR(VH_UOE)}
1486 };
1487
1488 /**
1489 * This one function is exported, used by NativeLookup.
1490 */
1491 JVM_ENTRY_PROF(void, JVM_RegisterMethodHandleMethods, JVM_RegisterMethodHandleMethods(JNIEnv *env, jclass MHN_class)) {
1492 assert(!MethodHandles::enabled(), "must not be enabled");
1493 assert(vmClasses::MethodHandle_klass() != nullptr, "should be present");
1494 assert(vmClasses::VarHandle_klass() != nullptr, "should be present");
1495
1496 oop mh_mirror = vmClasses::MethodHandle_klass()->java_mirror();
1497 oop vh_mirror = vmClasses::VarHandle_klass()->java_mirror();
1498 jclass MH_class = (jclass) JNIHandles::make_local(THREAD, mh_mirror);
1499 jclass VH_class = (jclass) JNIHandles::make_local(THREAD, vh_mirror);
1500
1501 {
1502 ThreadToNativeFromVM ttnfv(thread);
1503
1504 int status = env->RegisterNatives(MHN_class, MHN_methods, sizeof(MHN_methods)/sizeof(JNINativeMethod));
1505 guarantee(status == JNI_OK && !env->ExceptionOccurred(),
1506 "register java.lang.invoke.MethodHandleNative natives");
1507
1508 status = env->RegisterNatives(MH_class, MH_methods, sizeof(MH_methods)/sizeof(JNINativeMethod));
1509 guarantee(status == JNI_OK && !env->ExceptionOccurred(),
1510 "register java.lang.invoke.MethodHandle natives");
1511
1512 status = env->RegisterNatives(VH_class, VH_methods, sizeof(VH_methods)/sizeof(JNINativeMethod));
1513 guarantee(status == JNI_OK && !env->ExceptionOccurred(),
1514 "register java.lang.invoke.VarHandle natives");
1515 }
1516
1517 log_debug(methodhandles, indy)("MethodHandle support loaded (using LambdaForms)");
1518
1519 MethodHandles::set_enabled(true);
1520 }
1521 JVM_END
1522
1523 #define DO_COUNTERS(macro) \
1524 macro(MHN_init_Mem) \
1525 macro(MHN_expand_Mem) \
1526 macro(MHN_resolve_Mem) \
1527 macro(MHN_checkArchivable) \
1528 macro(MHN_getNamedCon) \
1529 macro(MHN_objectFieldOffset) \
1530 macro(MHN_setCallSiteTargetNormal) \
1531 macro(MHN_setCallSiteTargetVolatile) \
1532 macro(MHN_copyOutBootstrapArguments) \
1533 macro(MHN_clearCallSiteContext) \
1534 macro(MHN_staticFieldOffset) \
1535 macro(MHN_staticFieldBase) \
1536 macro(MHN_getMemberVMInfo) \
1537 macro(MH_invoke_UOE) \
1538 macro(MH_invokeExact_UOE) \
1539 macro(JVM_RegisterMethodHandleMethods)
1540
1541 #define INIT_COUNTER(name) \
1542 NEWPERFTICKCOUNTERS(_perf_##name##_timer, SUN_RT, #name); \
1543 NEWPERFEVENTCOUNTER(_perf_##name##_count, SUN_RT, #name "_count"); \
1544
1545 void MethodHandles::init_counters() {
1546 if (UsePerfData) {
1547 EXCEPTION_MARK;
1548
1549 DO_COUNTERS(INIT_COUNTER)
1550
1551 if (HAS_PENDING_EXCEPTION) {
1552 vm_exit_during_initialization("perf_mhn_init failed unexpectedly");
1553 }
1554 }
1555 }
1556
1557 #undef INIT_COUNTER
1558
1559 #define PRINT_COUNTER(name) {\
1560 jlong count = _perf_##name##_count->get_value(); \
1561 if (count > 0) { \
1562 st->print_cr(" %-40s = " JLONG_FORMAT_W(6) "us (elapsed) " JLONG_FORMAT_W(6) "us (thread) (" JLONG_FORMAT_W(5) " events)", #name, \
1563 _perf_##name##_timer->elapsed_counter_value_us(), \
1564 _perf_##name##_timer->thread_counter_value_us(), \
1565 count); \
1566 }}
1567
1568 void MethodHandles::print_counters_on(outputStream* st) {
1569 if (ProfileVMCalls && UsePerfData) {
1570 DO_COUNTERS(PRINT_COUNTER)
1571 } else {
1572 st->print_cr(" MHN: no info (%s is disabled)", (UsePerfData ? "ProfileVMCalls" : "UsePerfData"));
1573 }
1574 }
1575
1576 #undef PRINT_COUNTER
|