Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix instrumentation for Thread.sleep with jdk22 #407

Merged

Conversation

pderop
Copy link
Member

@pderop pderop commented Apr 4, 2024

using Blockhound with JDK 22 is not working, even when using "-XX:+AllowRedefinitionToAddDeleteMethods" option:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.2.5:test (default-test) on project jup: 
[ERROR] 
[ERROR] Please refer to /workspace/code/jup/target/surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
[ERROR] There was an error in the forked process
[ERROR] java.util.ServiceConfigurationError: org.junit.platform.launcher.TestExecutionListener: Provider reactor.blockhound.junit.platform.BlockHoundTestExecutionListener could not be instantiated
[ERROR] org.apache.maven.surefire.api.util.SurefireReflectionException: java.util.ServiceConfigurationError: org.junit.platform.launcher.TestExecutionListener: Provider reactor.blockhound.junit.platform.BlockHoundTestExecutionListener could not be instantiated
[ERROR]         at org.apache.maven.surefire.api.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:129)
[ERROR]         at org.apache.maven.surefire.api.util.ReflectionUtils.invokeGetter(ReflectionUtils.java:62)
[ERROR]         at org.apache.maven.surefire.junitplatform.LazyLauncher.launcher(LazyLauncher.java:68)
[ERROR]         at org.apache.maven.surefire.junitplatform.LazyLauncher.discover(LazyLauncher.java:50)
[ERROR]         at org.apache.maven.surefire.junitplatform.TestPlanScannerFilter.accept(TestPlanScannerFilter.java:52)
[ERROR]         at org.apache.maven.surefire.api.util.DefaultScanResult.applyFilter(DefaultScanResult.java:87)
[ERROR]         at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.scanClasspath(JUnitPlatformProvider.java:142)
[ERROR]         at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:122)
[ERROR]         at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
[ERROR]         at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
[ERROR]         at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
[ERROR]         at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)
[ERROR] Caused by: java.util.ServiceConfigurationError: org.junit.platform.launcher.TestExecutionListener: Provider reactor.blockhound.junit.platform.BlockHoundTestExecutionListener could not be instantiated
[ERROR]         at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:582)
[ERROR]         at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:809)
[ERROR]         at java.base/java.util.ServiceLoader$ProviderImpl.get(ServiceLoader.java:725)
[ERROR]         at java.base/java.util.ServiceLoader$3.next(ServiceLoader.java:1397)
[ERROR]         at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
[ERROR]         at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1939)
[ERROR]         at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:556)
[ERROR]         at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:546)
[ERROR]         at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
[ERROR]         at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
[ERROR]         at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:265)
[ERROR]         at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:611)
[ERROR]         at org.junit.platform.launcher.core.LauncherFactory.registerTestExecutionListeners(LauncherFactory.java:195)
[ERROR]         at org.junit.platform.launcher.core.LauncherFactory.createDefaultLauncher(LauncherFactory.java:142)
[ERROR]         at org.junit.platform.launcher.core.LauncherFactory.lambda$openSession$1(LauncherFactory.java:101)
[ERROR]         at org.junit.platform.launcher.core.DefaultLauncherSession.<init>(DefaultLauncherSession.java:53)
[ERROR]         at org.junit.platform.launcher.core.LauncherFactory.openSession(LauncherFactory.java:100)
[ERROR]         at org.junit.platform.launcher.core.LauncherFactory.openSession(LauncherFactory.java:82)
[ERROR]         at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
[ERROR]         at java.base/java.lang.reflect.Method.invoke(Method.java:580)
[ERROR]         at org.apache.maven.surefire.api.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:125)
[ERROR]         ... 11 more
[ERROR] Caused by: java.lang.ExceptionInInitializerError
[ERROR]         at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method)
[ERROR]         at java.base/jdk.internal.misc.Unsafe.ensureClassInitialized(Unknown Source)
[ERROR]         at java.base/jdk.internal.reflect.MethodHandleAccessorFactory.ensureClassInitialized(MethodHandleAccessorFactory.java:340)
[ERROR]         at java.base/jdk.internal.reflect.MethodHandleAccessorFactory.newConstructorAccessor(MethodHandleAccessorFactory.java:103)
[ERROR]         at java.base/jdk.internal.reflect.ReflectionFactory.newConstructorAccessor(ReflectionFactory.java:173)
[ERROR]         at java.base/java.lang.reflect.Constructor.acquireConstructorAccessor(Constructor.java:549)
[ERROR]         at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
[ERROR]         at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:486)
[ERROR]         at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:785)
[ERROR]         ... 30 more
[ERROR] Caused by: java.lang.IllegalStateException: The instrumentation have failed.
[ERROR] It looks like you're running on JDK 13+.
[ERROR] You need to add '-XX:+AllowRedefinitionToAddDeleteMethods' JVM flag.
[ERROR] See https://github.com/reactor/BlockHound/issues/33 for more info.
[ERROR]         at reactor.blockhound.BlockHound$Builder.testInstrumentation(BlockHound.java:538)
[ERROR]         at reactor.blockhound.BlockHound$Builder.install(BlockHound.java:501)
[ERROR]         at reactor.blockhound.BlockHound.install(BlockHound.java:91)
[ERROR]         at reactor.blockhound.junit.platform.BlockHoundTestExecutionListener.<clinit>(BlockHoundTestExecutionListener.java:19)
[ERROR]         ... 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

@pderop pderop added the type/bug A general bug label Apr 4, 2024
@pderop pderop added this to the 1.0.9.RELEASE milestone Apr 4, 2024
@pderop pderop self-assigned this Apr 4, 2024
@pderop pderop changed the title Fix instrumentation for sleep with jdk22 Fix instrumentation for Thread.sleep with jdk22 Apr 4, 2024
@pderop pderop force-pushed the fix-instrumentation-for-sleep-with-jdk22 branch from 826e1c4 to bbe61a1 Compare April 4, 2024 17:29
@pderop pderop requested a review from violetagg April 4, 2024 17:31
@pderop
Copy link
Member Author

pderop commented Apr 4, 2024

@violetagg , can you take a look ?

@pderop pderop merged commit 7d6e746 into reactor:master Apr 4, 2024
2 checks passed
@pderop pderop deleted the fix-instrumentation-for-sleep-with-jdk22 branch April 4, 2024 17:48
@pderop pderop mentioned this pull request Apr 4, 2024
pderop added a commit that referenced this pull request Apr 17, 2024
in #407, the BlockHound class is detecting the current jdk major version using InstrumentationUtil.jdkMajorVersion() method that has been introduced in #407 PR (the new InstrumentationUtil.jdkMajorVersion() method is relying on the jdk Runtime.getRuntime() class).

Now, there is still another class (NativeWrappingClassFileTransformer) which needs to determine the current jdk major version. Let's update it in order to reuse the InstrumentationUtil instead of relying of java system properties.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Java 22 Support
2 participants