ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决How appropriate is it to use same layout file and fragments with diiferent data in Android App?

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

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();
}
java jvm bytecode java-bytecode-asm bytecode-manipulation
|
  this question
edited Dec 17 '12 at 13:31 asked Dec 17 '12 at 9:09 Adel 141 2 11      How can I return the arguments back to the stack so that the function can continue to use them? –  Shay Aug 5 '13 at 15:00

 | 

1 Answers
1

解决方法

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.


|
  this answer
answered Dec 17 '12 at 10:01 int3 8,330 5 40 74      Thanks for your answer. Indeed, I should've used castore. I'll box up the primitive types and report back whether this solves the problem or not. –  Adel Dec 17 '12 at 12:46 1   Yep, it is working when converting the primitive types. Thanks :) –  Adel Dec 17 '12 at 13:27

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。