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

QUESTION: Can bytebuddy enhance native method in jdk libraries? #1439

Closed
RTxin opened this issue May 17, 2023 · 5 comments
Closed

QUESTION: Can bytebuddy enhance native method in jdk libraries? #1439

RTxin opened this issue May 17, 2023 · 5 comments
Assignees
Labels
Milestone

Comments

@RTxin
Copy link

RTxin commented May 17, 2023

The test cases under the test package seem to be supported by the native method that can enhance the custom class.
I tried to modify the test case with the intention of enhancing java.lang.string#intern and it seemed to cause a java SecurityException.
So I want to consult you, how should I operate this example.

        classLoader = new ByteArrayClassLoader.ChildFirst(getClass().getClassLoader(),
                ClassFileLocator.ForClassLoader.readToNames(String.class),
                ByteArrayClassLoader.PersistenceHandler.MANIFEST);
        assertThat(ByteBuddyAgent.install(), instanceOf(Instrumentation.class));
        AgentBuilder builder = new AgentBuilder.Default()
                .disableClassFormatChanges()
                .enableNativeMethodPrefix("bar")
                .ignore(none())
                .with(AgentBuilder.InitializationStrategy.NoOp.INSTANCE)
                .type(ElementMatchers.is(String.class), ElementMatchers.is(classLoader))
                .transform(new FooTransformer())
                ;
        ClassFileTransformer classFileTransformer=builder.installOnByteBuddyAgent();
        try {
            Class<?> type = classLoader.loadClass(String.class.getName());
            System.out.println(type.getDeclaredMethod("intern").invoke(type.getDeclaredConstructor(String.class).newInstance("foo")));
        } finally {
            assertThat(ByteBuddyAgent.getInstrumentation().removeTransformer(classFileTransformer), is(true));
        }
@raphw
Copy link
Owner

raphw commented May 17, 2023

This would require adding a method to a loaded class for a native method prefix. That is not possible.

@raphw raphw self-assigned this May 17, 2023
@raphw raphw added the question label May 17, 2023
@raphw raphw added this to the 1.14.4 milestone May 17, 2023
@RTxin
Copy link
Author

RTxin commented May 17, 2023

hi @raphw ,So you mean ByteBuddy has no way to enhance all classes native method? Can it only enhance the custom class that has not been loaded by ClassLoader's Native Method?

Can't you replace the whole method using the annotations' @RunTimeType 'and' @supercall '?

@RuntimeType
public static Object intercept(@SuperCall Callable<?> zuper) throws Throwable;

This is just found a replaceable bytecode demo

protected TServer(AbstractServerArgs abstractServerArgs) {
    TServer tServer = this;
    // enhance by first agent.
    delegate$6386i10.intercept((Object)tServer, new Object[]{abstractServerArgs2});
}
private /* synthetic */ TServer(AbstractServerArgs args, auxiliary.Ig29GTq8 ig29GTq8) {
    /*117*/     ...
    /*120*/    .....
}

@RTxin
Copy link
Author

RTxin commented May 17, 2023

My problem is similar to issue #716

@raphw
Copy link
Owner

raphw commented May 19, 2023

Using a prefix requires adding a method to the class file and that is not possible if the class is already loaded. That's why this is not possible

@RTxin
Copy link
Author

RTxin commented May 22, 2023

I've got it,Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants