Skip to content

Commit

Permalink
feat: handle java.lang.invoke.BoundMethodHandle correctly (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Sep 5, 2023
1 parent 85fd790 commit a0fb3f0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ public static boolean isGeneratedClassExtendingMagicAccessor(byte[] classfileByt
return false;
}
}

/**
* During the runtime the jvm binds methodhandles to the class {@link java.lang.invoke.BoundMethodHandle}.
* These classes are generated at runtime and should be ignored. We can identify them by checking if the super class is {@link java.lang.invoke.BoundMethodHandle}.
* @param classfileBytes the class file bytes
* @return true if the class is a bound method handle, false otherwise.
*/
public static boolean isBoundMethodHandle(byte[] classfileBytes) {
ClassReader reader = new ClassReader(classfileBytes);
System.err.println(reader.getSuperName());
return reader.getSuperName().equals("java/lang/invoke/BoundMethodHandle");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@ public byte[] transform(
private static byte[] isLoadedClassWhitelisted(String className, byte[] classfileBuffer) {
Map<String, List<Provenance>> fingerprints = options.getFingerprints();
if (RuntimeClass.isProxyClass(classfileBuffer)
|| RuntimeClass.isGeneratedClassExtendingMagicAccessor(classfileBuffer)) {
return classfileBuffer;
}
if (className.contains("$")) {
// FIXME: we need to check inner classes without loading them. Maybe add the hashes for inner classes in the
// fingerprints?
|| RuntimeClass.isGeneratedClassExtendingMagicAccessor(classfileBuffer)
|| RuntimeClass.isBoundMethodHandle(classfileBuffer)) {
return classfileBuffer;
}
for (String expectedClassName : fingerprints.keySet()) {
Expand Down
7 changes: 7 additions & 0 deletions watchdog-agent/src/test/java/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,11 @@ void verifyJdkIndexerFindsOrgXmlSax() throws Exception {
var var = options.getJdkFingerprints().keySet().stream().collect(Collectors.toSet());
assertThat(var).contains("org/xml/sax/helpers/NamespaceSupport");
}

@Test
void verifyJdkIndexerFindsMethodHandle() throws Exception {
Options options = new Options("skipShutdown=true");
boolean containsKey = options.getJdkFingerprints().containsKey("java/lang/invoke/BoundMethodHandle");
assertThat(containsKey).isTrue();
}
}

0 comments on commit a0fb3f0

Please sign in to comment.