0

I am trying to develop a javaagent. It also provides support for annotations.

In the preMain method I am trying to scan the classpath for annotations. Then adding the transformer using instrumentatino.addTransformer() method.

Because the classes are already loaded during the annotation processing, the transformation does not happen (if I remove the annotation processing code and retransformation code {see below}, things work fine).

To overcome this, I am now trying to retransform the classes. All the necessary setup related to manifest entry and enabling canRetransform flag in addTransformer method is done.

My code roughly looks like this:

annotationProcessor.processAnnotation();
instrumentation.addTransformer(new DummyTransformer(), true);
try {
  instrumentation.retransformClasses(instrumentation.getAllLoadedClasses());
} catch (UnmodifiableClassException e) {
  e.printStackTrace();
}

The DummyTransformer does not do anything. It simply returns the classBytes as it is (also I tried it returning null on every call)

What happens here is that I am getting this error

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x000000010c5254dd, pid=5912, tid=0x0000000000001603
#
# JRE version: OpenJDK Runtime Environment (8.0_282) (build 1.8.0_282-bre_2021_01_20_16_06-b00)
# Java VM: OpenJDK 64-Bit Server VM (25.282-b00 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# V  [libjvm.dylib+0x5254dd]  Symbol::as_C_string() const+0xd
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /some/obfuscated/path/hs_err_pid5912.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#
zsh: abort      java  -cp build/libs/jvm-agent-0.1-all.jar 

Initially I thought the transformer is misbehaving, but as I experimented with DummyTransformer, returning null or the same bytes produces similar results.

Yasin
  • 1,906
  • 1
  • 21
  • 37
  • I do not think this problem can be solved without reproducing it. Your code snippet alone together with the superficial explanation are helpful, but not enough. I would like to know what the "annotation processor" does, even before transformation. I also would like to see the rest of the code, especially the transformer, as trivial as it might be from your perspective. So please provide an [MCVE](https://stackoverflow.com/help/mcve), ideally on GitHub. Thank you. – kriegaex Feb 04 '21 at 00:46
  • BTW, I see `java -cp build/libs/jvm-agent-0.1-all.jar` in your log as a command line. There does not seem to be a `-javaagent:` parameter. Are you hot-attaching the agent? If so, I also want to see how you are doing that. – kriegaex Feb 04 '21 at 00:49
  • @kriegaex here is the command line java -javaagent:build/libs/jvm-agent-0.1-all.jar -cp build/libs/jvm-agent-0.1-all.jar some.package.name.HelloWorldApplication – Yasin Feb 04 '21 at 05:49

1 Answers1

0

The issue was that I was trying to retransform all the classes using instrumentation.getAllLoadedClasses(). Instead, I filtered out classes of my interest based on the package names and it worked with a limited set of classes.

Maybe there could be some classes that were loaded that were not supposed to be retransformed.

Yasin
  • 1,906
  • 1
  • 21
  • 37
  • Well, yes of course. I could have told you that. But you said that you wanted to retransform all classes. When I tried, the JVM just threw an exception in that case, but it didn't segfault. This is why I was and still am interested in the MCVE. If there is a problem in the JVM, this is worth knowing and tracking. – kriegaex Feb 04 '21 at 08:23
  • I tried to look at some other projects and tried to create MCVE. I couldn't reproduce the issue. That guided me towards the solution. Thank you @kriegaex – Yasin Feb 04 '21 at 08:52
  • I got `UnmodifiableClassException` though when working on MCVE. – Yasin Feb 04 '21 at 08:54
  • 1
    Yes, I got the same exception type when trying to reproduce your problem, but like I said, never a segfault. anyway, I am happy your problem is solved. If you can ever reproduce the segfault, please make sure to create a Java bug ticket if it does not exist yet, like the error message suggests (there even is a URL). Ideally, also create a core dump. – kriegaex Feb 04 '21 at 12:18