byte-buddy icon indicating copy to clipboard operation
byte-buddy copied to clipboard

Question about transformation on java.util.concurrent.ThreadPoolExecutor

Open panghuchi opened this issue 3 years ago • 4 comments

Hi @raphw

I have a question about transformation on java.util.concurrent.ThreadPoolExecutor. Here are the details. we have two agent, all implemented by bute-buddy. In agent 1, we will inject some class into bootstrap class loader by using Unsafe Strategy. In agent 2, based on agent1 we will transform java.util.concurrent.ThreadPoolExecutor to intercept ThreadPoolExecutor#submit method. Each agent can run success independently. Error from agent2 will be reported when the application is started when we attach two agent together,

ERROR 2023-01-12 12:25:43.058 main MetaDataAgent : index=0, batch=[class java.util.concurrent.ScheduledThreadPoolExecutor, class java.util.concurrent.ThreadPoolExecutor, class java.util.concurrent.AbstractExecutorService], types=[class java.util.concurrent.ScheduledThreadPoolExecutor, class java.util.concurrent.ThreadPoolExecutor, class java.util.concurrent.AbstractExecutorService] java.lang.UnsupportedOperationException: class redefinition failed: attempted to change superclass or interfaces at sun.instrument.InstrumentationImpl.retransformClasses0(Native Method) at sun.instrument.InstrumentationImpl.retransformClasses(InstrumentationImpl.java:144) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498)

so what should I do? thanks.

panghuchi avatar Jan 12 '23 06:01 panghuchi

It seems like you are changing the class shape in one agent. The other agent then resets the shape and this is not possible.

Have you set .disableClassFormatChanges() and .with(RetransformationStrategy.RETRANSFORM)?

raphw avatar Jan 12 '23 14:01 raphw

thanks for response.

I will define a new field in target transformed class in two agents. also it will implement a new custom interface, so I can store some custom object in new custom field and visit filed by custom interface.

I have set RetransformationStrategy.RETRANSFORM, but didn't set disableClassFormatChanges().

I found a rule of transform failure when target class is ScheduledThreadPoolExecutor and ThreadPoolExecutor, ScheduledThreadPoolExecutor inherited from ThreadPoolExecutor and ThreadPoolExecutor inherited from AbstractExecutorService.

Also transformation will be success when target class has no inheritance relationship. for example java.util.concurrent.CompletableFuture$ThreadPerTaskExecutor.

panghuchi avatar Jan 13 '23 00:01 panghuchi

If you set the above option, you are no longer allowed to add fields. But in general, it's a good approach to avoid this for potentially loaded classes as the VM does not allow adding fields on retransformation.

Instead, store state in a weak map and store it somewhere where it can be accessed. For example on the boot loader.

raphw avatar Jan 13 '23 22:01 raphw

Thanks for your advice. I will try to store state in a weak map.

panghuchi avatar Jan 16 '23 03:01 panghuchi