From ab692b25f0d66df602c997ebafbdd0532d5e2008 Mon Sep 17 00:00:00 2001 From: Tomas Zezula Date: Wed, 1 Apr 2020 12:30:23 +0200 Subject: [PATCH] [GR-18476] Using reflection to invoke HotSpotJVMCIRuntime.getCurrentJavaThread. --- .../libgraal/HotSpotGraalManagement.java | 4 ++- .../management/libgraal/MBeanProxy.java | 31 ++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/compiler/src/org.graalvm.compiler.hotspot.management.libgraal/src/org/graalvm/compiler/hotspot/management/libgraal/HotSpotGraalManagement.java b/compiler/src/org.graalvm.compiler.hotspot.management.libgraal/src/org/graalvm/compiler/hotspot/management/libgraal/HotSpotGraalManagement.java index c2fc2451b014..c42470e5a735 100644 --- a/compiler/src/org.graalvm.compiler.hotspot.management.libgraal/src/org/graalvm/compiler/hotspot/management/libgraal/HotSpotGraalManagement.java +++ b/compiler/src/org.graalvm.compiler.hotspot.management.libgraal/src/org/graalvm/compiler/hotspot/management/libgraal/HotSpotGraalManagement.java @@ -56,7 +56,9 @@ public HotSpotGraalManagement() { */ @Override public void initialize(HotSpotGraalRuntime runtime, GraalHotSpotVMConfig config) { - initializeJNI(config); + if (!initializeJNI(config)) { + return; + } HotSpotGraalRuntimeMBean mbean = getBean(); if (mbean == null) { if (runtime.getManagement() != this) { diff --git a/compiler/src/org.graalvm.compiler.hotspot.management.libgraal/src/org/graalvm/compiler/hotspot/management/libgraal/MBeanProxy.java b/compiler/src/org.graalvm.compiler.hotspot.management.libgraal/src/org/graalvm/compiler/hotspot/management/libgraal/MBeanProxy.java index c25ff087c7e8..1782f8109970 100644 --- a/compiler/src/org.graalvm.compiler.hotspot.management.libgraal/src/org/graalvm/compiler/hotspot/management/libgraal/MBeanProxy.java +++ b/compiler/src/org.graalvm.compiler.hotspot.management.libgraal/src/org/graalvm/compiler/hotspot/management/libgraal/MBeanProxy.java @@ -24,6 +24,7 @@ */ package org.graalvm.compiler.hotspot.management.libgraal; +import java.lang.reflect.Method; import static org.graalvm.libgraal.jni.JNIUtil.createString; import static org.graalvm.libgraal.jni.JNIUtil.getBinaryName; @@ -49,6 +50,17 @@ class MBeanProxy { + private static final Method getCurrentJavaThreadMethod; + static { + Method m; + try { + m = HotSpotJVMCIRuntime.class.getMethod("getCurrentJavaThread"); + } catch (NoSuchMethodException e) { + m = null; + } + getCurrentJavaThreadMethod = m; + } + // Classes defined in HotSpot heap by JNI, the values are filled by LibGraalFeature. private static final String HS_BEAN_CLASS_NAME = null; private static final byte[] HS_BEAN_CLASS = null; @@ -159,13 +171,16 @@ ObjectName poll() { return objName; } - static void initializeJNI(GraalHotSpotVMConfig config) { + static boolean initializeJNI(GraalHotSpotVMConfig config) { + if (getCurrentJavaThreadMethod == null) { + return false; + } if (jniEnvOffset == 0) { synchronized (MBeanProxy.class) { if (jniEnvOffset == 0) { if (config.jniEnvironmentOffset == Integer.MIN_VALUE) { // Old unsupported JVMCI version. - return; + return false; } memPoolBean = new LibGraalMemoryPoolMBean(); jniEnvOffset = config.jniEnvironmentOffset; @@ -179,6 +194,7 @@ static void initializeJNI(GraalHotSpotVMConfig config) { } } } + return true; } static JNI.JClass getHotSpotEntryPoints() { @@ -192,8 +208,15 @@ static JNI.JNIEnv getCurrentJNIEnv() { if (jniEnvOffset == 0) { throw new IllegalStateException("JniEnvOffset is not yet initialized."); } - long currentJavaThreadAddr = HotSpotJVMCIRuntime.runtime().getCurrentJavaThread(); - return WordFactory.pointer(currentJavaThreadAddr + jniEnvOffset); + if (getCurrentJavaThreadMethod == null) { + throw new IllegalStateException("CurrentJavaThread not supported by JVMCI."); + } + try { + long currentJavaThreadAddr = (Long) getCurrentJavaThreadMethod.invoke(HotSpotJVMCIRuntime.runtime()); + return WordFactory.pointer(currentJavaThreadAddr + jniEnvOffset); + } catch (ReflectiveOperationException reflectiveException) { + throw new RuntimeException("Failed to invoke HotSpotJVMCIRuntime::getCurrentJavaThread", reflectiveException); + } } /**