Skip to content

Commit

Permalink
Fix instrumentation of Thread.sleep for JDK22 and above (#407)
Browse files Browse the repository at this point in the history
using Blockhound with JDK 22 is not working, even when using "-XX:+AllowRedefinitionToAddDeleteMethods" option:

Caused by: java.lang.IllegalStateException: The instrumentation have failed.
 It looks like you're running on JDK 13+.
 You need to add '-XX:+AllowRedefinitionToAddDeleteMethods' JVM flag.
 See #33 for more info.
         at reactor.blockhound.BlockHound$Builder.testInstrumentation(BlockHound.java:538)
         at reactor.blockhound.BlockHound$Builder.install(BlockHound.java:501)
         at reactor.blockhound.BlockHound.install(BlockHound.java:91)
         at reactor.blockhound.junit.platform.BlockHoundTestExecutionListener.<clinit>(BlockHoundTestExecutionListener.java:19)
         ... 39 more

this is because during initialization, Blockhound is trying to instrument the Thread.sleep() method, and if it does not work, then the initialization fails and assumes that the failure comes from a missing "-XX:+AllowRedefinitionToAddDeleteMethods" option.

but, here the problem is that in JDK 22, the internal native method called by Thread.sleep has changed, it's "Thread.sleepNanos0" instead of "Thread.sleep0".

This PR corrects the instrumentation for Thread.sleepNanos0 native method when the JDK version is detected to be 22+

Fixes #404
  • Loading branch information
pderop authored Apr 4, 2024
1 parent 10517c8 commit 7d6e746
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
31 changes: 19 additions & 12 deletions agent/src/main/java/reactor/blockhound/BlockHound.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ public TypePool typePool(ClassFileLocator classFileLocator, ClassLoader classLoa
public static class Builder {

private final Map<String, Map<String, Set<String>>> blockingMethods = new HashMap<String, Map<String, Set<String>>>() {{
int jdkMajorVersion = InstrumentationUtils.getJdkMajorVersion();

put("java/lang/Object", new HashMap<String, Set<String>>() {{
put("wait", singleton("(J)V"));
}});
Expand Down Expand Up @@ -194,18 +196,15 @@ public static class Builder {
put("writeBytes", singleton("([BIIZ)V"));
}});

try {
// Check if Java 9+
Class.forName("java.lang.StackWalker");

if (jdkMajorVersion >= 9) {
put("jdk/internal/misc/Unsafe", new HashMap<String, Set<String>>() {{
put("park", singleton("(ZJ)V"));
}});
put("java/lang/ProcessImpl", new HashMap<String, Set<String>>() {{
put("forkAndExec", singleton("(I[B[B[BI[BI[B[IZ)I"));
}});
}
catch (ClassNotFoundException __) {
else {
put("sun/misc/Unsafe", new HashMap<String, Set<String>>() {{
put("park", singleton("(ZJ)V"));
}});
Expand All @@ -214,19 +213,27 @@ public static class Builder {
}});
}

try {
// Check if Java 19+
Class.forName("java.lang.WrongThreadException");

if (jdkMajorVersion < 19) {
// for jdk version < 19, the native method for Thread.sleep is "sleep"
put("java/lang/Thread", new HashMap<String, Set<String>>() {{
put("sleep", singleton("(J)V"));
put("yield", singleton("()V"));
put("onSpinWait", singleton("()V"));
}});
}
else if (jdkMajorVersion >= 19 && jdkMajorVersion <= 21) {
// for jdk version in the range [19, 21], the native method for Thread.sleep is "sleep0"
put("java/lang/Thread", new HashMap<String, Set<String>>() {{
put("sleep0", singleton("(J)V"));
put("yield0", singleton("()V"));
put("onSpinWait", singleton("()V"));
}});
} catch (ClassNotFoundException __) {
}
else {
// for jdk version >= 22, the native method for Thread.sleep is "sleepNanos0"
put("java/lang/Thread", new HashMap<String, Set<String>>() {{
put("sleep", singleton("(J)V"));
put("yield", singleton("()V"));
put("sleepNanos0", singleton("(J)V"));
put("yield0", singleton("()V"));
put("onSpinWait", singleton("()V"));
}});
}
Expand Down
68 changes: 68 additions & 0 deletions agent/src/main/java/reactor/blockhound/InstrumentationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
Expand Down Expand Up @@ -72,4 +74,70 @@ public void visit(int version, int access, String name, String signature, String
super.visit(version, access | Opcodes.ACC_PUBLIC, name, signature, superName, interfaces);
}
}

/**
* Helper method to detect the current JDK major version.
* For security reasons, we don't rely on "java.version" system property, but on Runtime.version() method, which is
* available from JDK9 +
* And starting from JDK10+, we rely on Runtime.version().feature() method.
*
* @return the current jdk major version (8, 9, 10, ... 22)
*/
static int getJdkMajorVersion() {
Object version = getRuntimeVersion();

if (version == null) {
return 8; // Runtime.version() not available, JDK 8
}

return getRuntimeVersionFeature(version);
}

/**
* Detects the Runtime.version() object, or null if JDK version is < JDK 9
*
* @return the detected JDK version object or null if not available
*/
private static Object getRuntimeVersion() {
Runtime runtime = Runtime.getRuntime();
try {
Method versionMethod = runtime.getClass().getMethod("version");
return versionMethod.invoke(null);
}

catch (NoSuchMethodException e) {
// Method Runtime.version() not found -> return null, meaning JDK 8
return null; // JDK 8
}

catch (IllegalAccessException | InvocationTargetException e) {
// if the Runtime.version() method exists, we should be able to invoke it, consider this is an error state
throw new IllegalStateException("Could not invoke Runtime.version() method", e);
}
}

/**
* Extracts the major version from the JDK version object.
*
* @param version the JDK version object
* @return the major version (9, 10, ...)
*/
private static int getRuntimeVersionFeature(Object version) {
try {
Method featureMethod = version.getClass().getMethod("feature");
Object feature = featureMethod.invoke(version);
return (int) feature;
}

catch (NoSuchMethodException e) {
// Version.feature() method not found -> JDK 9 (because feature method is only available starting from JDK10 +)
return 9;
}

catch (IllegalAccessException | InvocationTargetException e) {
// if the Runtime.version().feature() method exists, we should be able to invoke it, consider this is an error state
throw new IllegalStateException("Could not invoke Runtime.version().feature() method", e);
}
}

}

0 comments on commit 7d6e746

Please sign in to comment.