Skip to content

Not able to intercept 'loadLibrary()' in some java versions #1856

@dmitry-bluerock

Description

@dmitry-bluerock

Hello,

I'm trying to intercept an external library load in java. There is a public API in java: System.loadLibrary() and Runtime.loadLibrary(). However the next code works for java 8 and 11. But doesn't work for java 17, 21, 25:

        new AgentBuilder.Default()
                .disableClassFormatChanges()
                .with(RETRANSFORMATION)
                .ignore(none())
                .type(any())
                .transform((builder, type, classLoader, module, pd) -> builder.visit(
                        Advice.to(LoadLibraryAdvice.class)
                                .on(ElementMatchers.isMethod().and(named("loadLibrary")))))

LoadLibraryAdvice contains just debug output to confirm if it's working:

public class LoadLibraryAdvice {
    @Advice.OnMethodEnter
    static void onEnter(
            @Advice.Origin Method method,
            @Advice.AllArguments(typing = DYNAMIC) Object[] args) {
        System.out.println("intercepted: " + method.getDeclaringClass().getName() + "@" + method.getName());
        for (Object a : args)
            System.out.println(" > " + a.toString());
    }
}

On java versions 8 and 11 it works and I see debug output (including internal call to java.lang.ClassLoader.loadLibrary).
On java versions 17, 21, 25 I don't see any output and also I don't see transformation debug log for System/Runtime/ClassLoader classes.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions