From 55819297b63a395711b8a2583807b8abf8683a83 Mon Sep 17 00:00:00 2001 From: Pierre De Rop Date: Tue, 30 Jan 2024 22:46:02 +0100 Subject: [PATCH] Remove IntrinsicCandidate annotation on non-native delegate methods. (#393) Since JDK16, a new @IntrinsicCandidate annotation has been introduced; It indicates that an annotated method may be (but is not guaranteed to be) intrinsified. For example, the native Unsafe.park method is annotated with it: @IntrinsicCandidate public native void park(boolean isAbsolute, long time); So, when BlockHound is instrumenting a native method, it first renames the native method using a prefix. For example, Unsafe.park is renamed to Unsafe.$$BlockHound$$_park And then the origin method (Unsafe.park) is rewritten in order to delegate the call to the renamed method, but the origin method is also rewritten to be non-native, so we end up with something like this after instrumentation: class Unsafe { ... public native void $$BlockHound$$_park(boolean isAbsolute, long time); @IntrinsicCandidate public void park(boolean isAbsolute, long time) { // delegates the call to $$BlockHound$$_park } Now, in JDK16 and JDK17, when CheckIntrinsicsflag is enabled (true by default), the "native" keyword was optional, but in this JDK 18 change, now, a warning message is logged in case a method annotated with @IntrinsicCandidate is not native, while it's present in internal JDK list of the native methods that can be intrinsified. The warning seems to be not harmful, but this PR just tries to avoid it by removing the @IntrinsicCandidate annotation from the proxy/delegate method (from public void park non-native method). Fixes #392 --- .../NativeWrappingClassFileTransformer.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/agent/src/main/java/reactor/blockhound/NativeWrappingClassFileTransformer.java b/agent/src/main/java/reactor/blockhound/NativeWrappingClassFileTransformer.java index e5b1d7e..7e72d40 100644 --- a/agent/src/main/java/reactor/blockhound/NativeWrappingClassFileTransformer.java +++ b/agent/src/main/java/reactor/blockhound/NativeWrappingClassFileTransformer.java @@ -36,6 +36,14 @@ class NativeWrappingClassFileTransformer implements ClassFileTransformer { private final Map>> blockingMethods; + public static final boolean IS_JDK_18_OR_NEWER; + + static { + String javaVersion = System.getProperty("java.specification.version"); + double version = Double.parseDouble(javaVersion); + IS_JDK_18_OR_NEWER = version >= 18.0; + } + NativeWrappingClassFileTransformer(final Map>> blockingMethods) { this.blockingMethods = blockingMethods; } @@ -105,6 +113,14 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str delegatingMethodVisitor.visitCode(); return new MethodVisitor(ASM7, delegatingMethodVisitor) { + @Override + public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) { + // See #392 + if (IS_JDK_18_OR_NEWER && descriptor.equals("Ljdk/internal/vm/annotation/IntrinsicCandidate;")) { + return null; // remove the intrinsic annotation + } + return super.visitAnnotation(descriptor, visible); + } @Override public void visitEnd() {