Skip to content

Commit

Permalink
Remove IntrinsicCandidate annotation on non-native delegate methods. (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
pderop committed Jan 30, 2024
1 parent 92524dd commit 5581929
Showing 1 changed file with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class NativeWrappingClassFileTransformer implements ClassFileTransformer {

private final Map<String, Map<String, Set<String>>> 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<String, Map<String, Set<String>>> blockingMethods) {
this.blockingMethods = blockingMethods;
}
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 5581929

Please sign in to comment.