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

Why not javaagent? #1

Open
erxson opened this issue Jun 21, 2024 · 1 comment
Open

Why not javaagent? #1

erxson opened this issue Jun 21, 2024 · 1 comment

Comments

@erxson
Copy link

erxson commented Jun 21, 2024

You could make a much simpler injector for java agent hack. What are the advantages of your method?

@Lefraudeur
Copy link
Owner

Lefraudeur commented Jun 21, 2024

for now not much, ig

  • no jar file on disk
  • allows to load the classes in a different classloader (might be possible with javaagent with some weird tricks)
  • Because the classes are loaded in a new URLClassLoader instance, you can unload the classes by deleting the URLClassLoader
  • depending on the jvm, and on the launch command, the VirtualMachine attach api to load the java agent at runtime might be disabled (I think it can be bypassed by calling loadAgent0 with jni manually, instead of using the java VirtualMachine class)

I'm still reading the jvm source code to understand the class loading process and clear some of the traces it leaves.
I made another, bit simpler injector with the same advantages here: https://github.com/Lefraudeur/RuntimeJarLoader

Overall this can in fact be considered as a java agent, it's just not using the usual java Attach api.
If by "java agent" you mean a jar that follows the usual structure, so an agentmain method with a Instrumentation object instance in the parametes, then ye you are right, this would be easier.
However the goal here is to not store the jar file on disk and to be able to unload it.
So I have to use c++ with jvmti instead.
The project is far from done, I plan on making a mixin like json file, where you can put the event hooks you want to apply.

I don't see the point of making an injector for java agents because it already exists, it's a feature builtin in the jdk, it has already been done before.
https://docs.oracle.com/en/java/javase/22/docs/api/java.instrument/java/lang/instrument/package-summary.html
https://docs.oracle.com/en/java/javase/22/docs/api/jdk.attach/com/sun/tools/attach/VirtualMachine.html

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

No branches or pull requests

2 participants