Creating a new object using ASM

I am trying to use the ASM framework to inject bytecode in my place of interest, and I have been successful so far. I am currently trying to enter code that basically creates a new instance / object of the class and after reading the bit I can achieve this using INVOKESPECIAL (I hope that my understanding was correct for INVOKESPECIAL "INVOKESPECIAL" for private methods and constructors) .

Below is the code snippet that I used to create the instance

visitor.visitLdcInsn(System.currentTimeMillis()); visitor.visitLdcInsn(System.currentTimeMillis()); visitor.visitLdcInsn(_type); visitor.visitVarInsn(ALOAD, metanamevarindex); eventObject = newLocal(Type.getType("com/vish/RequestTrackerEvent")); visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/vish/RequestTrackerEvent", "<init>", "(JJLjava/lang/String;Ljava/lang/String;)V"); visitor.visitVarInsn(ASTORE, eventObject); 

The class constructor takes 4 arguments (long, long, String, String) But whenever I do this, I get an exception like below

 java.lang.VerifyError: JVMVRFY036 stack underflow; at java.lang.J9VMInternals.verifyImpl(Native Method) at java.lang.J9VMInternals.verify(J9VMInternals.java:72) at java.lang.J9VMInternals.verify(J9VMInternals.java:70) at java.lang.J9VMInternals.initialize(J9VMInternals.java:134) 

Can someone help me in understanding the correctness of my use / understanding of INVOKESPECIAL, if right, where am I doing wrong?

thanks

+4
source share
2 answers

A question like "how do I generate {some Java code} with ASM" was answered in the ASM FAQ :

If you want to know how to generate a synchronized block, try to catch a block, finally statement or any other Java construct, write the Java code you want to create in a temporary class, compile it with javac, and then use ASMifier to get the ASM code, which will generate this class (see 10. How to get the bytecode of an existing class? ").

You can go even further by comparing the output of ASMifier before and after the conversion, as described in this article .

+1
source

I don’t remember exactly what newLocal () does, but I know that the method does not insert the new instruction into the bytecode. It merly reserves space in some internal ASM variable processing mechanisms.

Try using something like this instead

 visitor.visitTypeInst(Opcodes.NEW, "com/vish/RequestTrackerEvent"); 

Good luck.

+2
source

Source: https://habr.com/ru/post/1500645/


All Articles