Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
701 views
in Technique[技术] by (71.8m points)

jvm - Java method parameters values in ASM

I am trying to get the values of a Java program's method's parameters. I am using ASM to instrument the bytecode and getting these values. However, I'm running into some troubles.

Here is the visitCode() method used to instrument the code. What it is doing is :

  1. Create an empty array to store the collected parameters.
  2. For each parameter, load its value into the array.
  3. Send this array to my agent's OnMethodEntry method (which where the values will be used).

.

@Override
public void visitCode() {
    int paramLength = paramTypes.length;

    // Create array with length equal to number of parameters
    mv.visitIntInsn(Opcodes.BIPUSH, paramLength);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitVarInsn(Opcodes.ASTORE, paramLength);

    // Fill the created array with method parameters
    int i = 0;
    for (Type tp : paramTypes) {
        mv.visitVarInsn(Opcodes.ALOAD, paramLength);
        mv.visitIntInsn(Opcodes.BIPUSH, i);

        if (tp.equals(Type.BOOLEAN_TYPE) || tp.equals(Type.BYTE_TYPE) || tp.equals(Type.CHAR_TYPE) || tp.equals(Type.SHORT_TYPE) || tp.equals(Type.INT_TYPE))
            mv.visitVarInsn(Opcodes.ILOAD, i);
        else if (tp.equals(Type.LONG_TYPE)) {
            mv.visitVarInsn(Opcodes.LLOAD, i);
            i++;
        }
        else if (tp.equals(Type.FLOAT_TYPE))
            mv.visitVarInsn(Opcodes.FLOAD, i);
        else if (tp.equals(Type.DOUBLE_TYPE)) {
            mv.visitVarInsn(Opcodes.DLOAD, i);
            i++;
        }
        else
            mv.visitVarInsn(Opcodes.ALOAD, i);

        mv.visitInsn(Opcodes.AASTORE);
        i++;
    }

    // Load id, class name and method name
    this.visitLdcInsn(new Integer(this.methodID));
    this.visitLdcInsn(this.className);
    this.visitLdcInsn(this.methodName);

    // Load the array of parameters that we created
    this.visitVarInsn(Opcodes.ALOAD, paramLength);

    mv.visitMethodInsn(Opcodes.INVOKESTATIC,
            "jalen/MethodStats",
            "onMethodEntry",
            "(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V");
    super.visitCode();
}

However, this is not working when apparently the method have more than one parameter.

The class file obtained shows things like this :

static void moveDisk(char arg0, char arg1, PrintStream arg2) {
Object[] arrayOfObject = new Object[3]; arrayOfObject[0] = ???; arrayOfObject[1] = ???;
Object localObject;
arrayOfObject[2] = localObject; MethodStats.onMethodEntry(5, "hanoi/TowersOfHanoi", "moveDisk", arrayOfObject);

Where 2 local objects are created instead of loading the parameters.

The bytecode doesn't show anything weird :

static void moveDisk(char, char, java.io.PrintStream);
Code:
   0: bipush        3
   2: anewarray     #4                  // class java/lang/Object
   5: astore_3      
   6: aload_3       
   7: bipush        0
   9: iload_0       
  10: aastore       
  11: aload_3       
  12: bipush        1
  14: iload_1       
  15: aastore       
  16: aload_3       
  17: bipush        2
  19: aload_2       
  20: aastore       
  21: ldc           #118                // int 5
  23: ldc           #12                 // String hanoi/TowersOfHanoi
  25: ldc           #119                // String moveDisk
  27: aload_3       
  28: invokestatic  #19                 // Method jalen/MethodStats.onMethodEntry:(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V

And finally, the error showed is (when using -noverify):

param: [Ljava.lang.String;@420e54f3
Exception in thread "Jalen Agent" java.lang.NullPointerException
at hanoi.TowersOfHanoi.solveHanoi(TowersOfHanoi.java)
at hanoi.TowersOfHanoi.main(TowersOfHanoi.java:29)

Otherwise, it is:

Exception in thread "Jalen Agent" java.lang.VerifyError: (class: hanoi/TowersOfHanoi, method: moveDisk signature: (CCLjava/io/PrintStream;)V) Expecting to find object/array on stack
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
    at java.lang.Class.getMethod0(Class.java:2685)
    at java.lang.Class.getMethod(Class.java:1620)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484)

Normally, this should rather work as I am just loading information from the stack frame. I tried also to check static & non static methods (as of the stack explained here : http://www.artima.com/insidejvm/ed2/jvm8.html), but still with no success.

Any idea on why this is happening, or possibly an idea of a solution ?

Thanks :)

EDIT:

It is now working when boxing up primitive types (thanks to suggestions by int3 below :) ). Here is the working code of visitCode() method :

@Override
public void visitCode() {
    int paramLength = paramTypes.length;

    // Create array with length equal to number of parameters
    mv.visitIntInsn(Opcodes.BIPUSH, paramLength);
    mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
    mv.visitVarInsn(Opcodes.ASTORE, paramLength);

    // Fill the created array with method parameters
    int i = 0;
    for (Type tp : paramTypes) {
        mv.visitVarInsn(Opcodes.ALOAD, paramLength);
        mv.visitIntInsn(Opcodes.BIPUSH, i);

        if (tp.equals(Type.BOOLEAN_TYPE)) {
            mv.visitVarInsn(Opcodes.ILOAD, i);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
        }
        else if (tp.equals(Type.BYTE_TYPE)) {
            mv.visitVarInsn(Opcodes.ILOAD, i);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
        }
        else if (tp.equals(Type.CHAR_TYPE)) {
            mv.visitVarInsn(Opcodes.ILOAD, i);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
        }
        else if (tp.equals(Type.SHORT_TYPE)) {
            mv.visitVarInsn(Opcodes.ILOAD, i);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
        }
        else if (tp.equals(Type.INT_TYPE)) {
            mv.visitVarInsn(Opcodes.ILOAD, i);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
        }
        else if (tp.equals(Type.LONG_TYPE)) {
            mv.visitVarInsn(Opcodes.LLOAD, i);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
            i++;
        }
        else if (tp.equals(Type.FLOAT_TYPE)) {
            mv.visitVarInsn(Opcodes.FLOAD, i);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
        }
        else if (tp.equals(Type.DOUBLE_TYPE)) {
            mv.visitVarInsn(Opcodes.DLOAD, i);
            mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
            i++;
        }
        else
            mv.visitVarInsn(Opcodes.ALOAD, i);

        mv.visitInsn(Opcodes.AASTORE);
        i++;
    }

    // Load id, class name and method name
    this.visitLdcInsn(new Integer(this.methodID));
    this.visitLdcInsn(this.className);
    this.visitLdcInsn(this.methodName);

    // Load the array of parameters that we created
    this.visitVarInsn(Opcodes.ALOAD, paramLength);

    mv.visitMethodInsn(Opcodes.INVOKESTATIC,
            "jalen/MethodStats",
            "onMethodEntry",
            "(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V");
    super.visitCode();
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are using aastore to store a char into an object array, which is a type error. aastore should only be used to store objects and arrays, which is probably why the error says 'expected object/array on stack'. Characters should be stored in a char array using castore. However, since you want this to work for arbitrary signatures, you'll probably want to box up the primitive types into objects which you can then use aastore upon -- e.g. char should be boxed up in a java.lang.Character object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...