문제

I have been trying to use ASM framework to inject bytecode at my interested location and I have been successful till now.Currently I am trying to inject code which basically creates a new instance/object of a class and after reading a bit I found that this can be achieved using INVOKESPECIAL (I hope my understanding was right of INVOKESPECIAL "INVOKESPECIAL for private methods and constructors").

Below is the code snippet I used to create 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 constructor of the class takes in 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 anyone help me in understanding whether my usage/understanding of INVOKESPECIAL is right, if right then where am I doing wrong?

Thanks

도움이 되었습니까?

해결책 2

Question like "how do I generate {some Java code} with ASM" has been answered in ASM FAQ:

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

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

다른 팁

I don't remeber exactly what newLocal() does, but I do know that the method does not insert a NEW instruction into the bytecode. It merly reserves space in some ASM internal local variable handling mechanisms.

Try instead using something like

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

Good luck

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top