views:

261

answers:

1

Hi,

I am using ASM in order to do bytecode instrumentation for Java programs. What I'm doing is simple - When instrumenting a method, if the instruction is a PUTFIELD, simply do a DUP_X1 right before the instruction, then visit the PUTFIELD and inject a function call with an argument that includes the DUP'ed stack entry.

                public void visitFieldInsn(
                                int opcode,
                                String owner,  // owner of this field...
                                String name,
                                String desc) {

                    boolean did_dup = false;

                    if(opcode == Opcodes.PUTFIELD) {
                        if(!owner.equals("java/lang/System")) {
                            if (desc.startsWith("L")) {
                                mv.visitInsn(Opcodes.DUP_X1);                                       
                                did_dup = true;
                            }
                        }

                    } 

                    mv.visitFieldInsn(opcode, owner, name, desc);

                    if (did_dup) {
                        mv.visitVarInsn(Opcodes.ALOAD, 0);
                        mv.visitMethodInsn(Opcodes.INVOKESTATIC, "com/amir/ASide", "updateG", "(Ljava/lang/Object;Ljava/lang/Object;)V");
                    }

                 }

The code looks like this BEFORE and AFTER instrumentation, respectively :

public void setA(ClassA classa)
{
    refA = classa;
    eyeColor = classa.eyeColor;
}


public void setA(ClassA classa)
{
    ASide.updateG(refA = classa, this);
    ASide.updateG(eyeColor = classa.eyeColor, this);
}

But when I run the instrumented code, I get :

java.lang.VerifyError: Expecting to find object/array on stack

Can you offer has any help on this?

+1  A: 

There is some holes in your instrumentation. For instance, it doesn't check if you are inside static method, so this variable is present.

Eugene Kuleshov
Solved along time ago but you are correct. And of course you are correct, you are the author of ASM!!! Thanks again, I'm a huge fan
Amir Afghani