ASM 字节码获取在调用某些方法时作为参数传递的值
ASM Bytecode get values passed as arguments on calls of some methods
我已经和 ASM 斗争了 5 天了,但我无法解决我的问题。要求是从方法调用中获取作为参数传递的所有值。我知道有很多工具可以实现这一点。我去了ASM(不知道自己做的对不对)
例如,如果我有这些方法
public void test2(String a, int b , String c, boolean ba, long d, String e){
}
public String giveMeAString(){
return "oneString";
}
public String giveMeAnotherString(){
return "anotherString";
}
public void test(){
test2("firstParameter", 2907, giveMeAString(),true, 1992, giveMeAnotherString());
}
我要保存
["firstParameter",2907,"oneString",真实,1992,"anotherString"]
我已经阅读了这些主题:
How to get the return value in asm?
Java method parameters values in ASM
Tracing method invocation arguments in bytecode using ASM
get function arguments values using java asm for bytecode instrimentation
其中解释了将参数放入局部变量,但我如何访问它们?
如何获得该参数的值?我不明白我怎样才能得到那个参数..我不知道,例如打印它们。任何的想法?
一旦它们进入堆栈,我不明白如何将值复制到变量。
谢谢!
这是我正在使用的代码:
public static class ClassPrinterVisitor extends ClassVisitor {
private String name;
private String desc;
private String signature;
private Type[] paramTypes;
private boolean isStatic;
private String className;
private String methodName;
private String methodDesc;
private String owner;
private int access;
public ClassPrinterVisitor(int api, ClassVisitor cv) {
super(api, cv);
}
public ClassPrinterVisitor(int api) {
super(api);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
MethodVisitor oriMv = new MethodVisitor(Opcodes.ASM4) {
};
final MethodVisitor instMv2 = new MethodPrinterVisitor(access, desc, oriMv, Type.getArgumentTypes(desc), (access & Opcodes.ACC_STATIC) != 0, className,
name, desc);
return instMv2;
}
private class MethodPrinterVisitor extends MethodVisitor {
List<Object> params = new ArrayList<>();
List<Object> params2 = new ArrayList<>();
private Type[] paramTypes;
private boolean isStatic;
private String className;
private String methodName;
private String methodDesc;
public MethodPrinterVisitor(int api, MethodVisitor mv) {
super(api, mv);
}
public MethodPrinterVisitor(int access, String desc, MethodVisitor mv, Type[] paramTypes, boolean isStatic, String classname,
String methodname, String methoddesc) {
super(Opcodes.ASM4, mv);
this.paramTypes = paramTypes;
this.isStatic = isStatic;
this.className = classname;
this.methodName = methodname;
this.methodDesc = methoddesc;
}
@Override
public void visitLdcInsn(Object var1) {
if (var1 != null) {
params.add(var1);
super.visitLdcInsn(var1);
System.out.printf("arg: %s %n", var1.toString());
}
}
@Override
public void visitInsn(int var1) {
if(this.mv != null) {
this.mv.visitInsn(var1);
}
}
@Override
public void visitIntInsn(int var1, int var2) {
if(this.mv != null) {
this.mv.visitIntInsn(var1, var2);
}
}
@Override
public void visitVarInsn(int var1, int var2) {
if(this.mv != null) {
this.mv.visitVarInsn(var1, var2);
}
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
Pattern pattern = Pattern.compile("[a-zA-Z0-9._]*");
System.out.printf("---------------------------%n");
System.out.printf("Class %s calls method %s from class %s%n", ClassPrinterVisitor.this.name, name, owner);
System.out.printf("Desc: %s signature: %s%n", ClassPrinterVisitor.this.desc, ClassPrinterVisitor.this.signature);
for (Object p : params) {
Matcher matcher = pattern.matcher(p.toString());
if (!p.toString().isEmpty() && !p.toString().startsWith(".") && matcher.matches()) {
System.out.printf("visitLdcInsn: %s %n", p);
}
}
System.out.printf("---------------------------%n%n");
params = new ArrayList<>();
}
@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, className, name, signature);
super.visitCode();
}
}
}
当使用 ASM 访问方法的字节码时,您不能“在调用某些方法时获取作为参数传递的值”,我认为您可能错误地理解了这些引用。
ASM 只是一个用于字节码操作的库。在对方法进行操作时,字节码中显示的数字只是局部变量索引(参数和局部变量),您无法获取变量引用的 VALUE(如果它不是常量)。
获取这些局部变量索引的VALUEs的唯一可能是执行被访问的字节码。方法的字节码是静态的,这些变量索引引用的值是未知的。当执行一个方法的字节码时,这些索引引用了一些对象,VM 知道。
您需要实施 Data Flow Analysis,对于您的情况,它将是 向后 数据流分析。
看看 ASM 的 SourceInterpreter,
这个 SO 回答
由于Control Flow,单个参数可能有多个可能的值。由于外部输入,您可能无法确定一个值(尽管您可能能够从更广泛的上下文中确定诸如 nullness 之类的东西)。
对于给出的示例来说并不难,只有常量或对相同 class 上的方法的调用产生常量 - 但扩展到一般情况可能是一项重要的工作。可能值得看一下执行静态代码分析的 "checker" 类型框架。
我已经和 ASM 斗争了 5 天了,但我无法解决我的问题。要求是从方法调用中获取作为参数传递的所有值。我知道有很多工具可以实现这一点。我去了ASM(不知道自己做的对不对)
例如,如果我有这些方法
public void test2(String a, int b , String c, boolean ba, long d, String e){
}
public String giveMeAString(){
return "oneString";
}
public String giveMeAnotherString(){
return "anotherString";
}
public void test(){
test2("firstParameter", 2907, giveMeAString(),true, 1992, giveMeAnotherString());
}
我要保存 ["firstParameter",2907,"oneString",真实,1992,"anotherString"]
我已经阅读了这些主题:
How to get the return value in asm?
Java method parameters values in ASM
Tracing method invocation arguments in bytecode using ASM
get function arguments values using java asm for bytecode instrimentation
其中解释了将参数放入局部变量,但我如何访问它们?
如何获得该参数的值?我不明白我怎样才能得到那个参数..我不知道,例如打印它们。任何的想法? 一旦它们进入堆栈,我不明白如何将值复制到变量。
谢谢!
这是我正在使用的代码:
public static class ClassPrinterVisitor extends ClassVisitor {
private String name;
private String desc;
private String signature;
private Type[] paramTypes;
private boolean isStatic;
private String className;
private String methodName;
private String methodDesc;
private String owner;
private int access;
public ClassPrinterVisitor(int api, ClassVisitor cv) {
super(api, cv);
}
public ClassPrinterVisitor(int api) {
super(api);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
MethodVisitor oriMv = new MethodVisitor(Opcodes.ASM4) {
};
final MethodVisitor instMv2 = new MethodPrinterVisitor(access, desc, oriMv, Type.getArgumentTypes(desc), (access & Opcodes.ACC_STATIC) != 0, className,
name, desc);
return instMv2;
}
private class MethodPrinterVisitor extends MethodVisitor {
List<Object> params = new ArrayList<>();
List<Object> params2 = new ArrayList<>();
private Type[] paramTypes;
private boolean isStatic;
private String className;
private String methodName;
private String methodDesc;
public MethodPrinterVisitor(int api, MethodVisitor mv) {
super(api, mv);
}
public MethodPrinterVisitor(int access, String desc, MethodVisitor mv, Type[] paramTypes, boolean isStatic, String classname,
String methodname, String methoddesc) {
super(Opcodes.ASM4, mv);
this.paramTypes = paramTypes;
this.isStatic = isStatic;
this.className = classname;
this.methodName = methodname;
this.methodDesc = methoddesc;
}
@Override
public void visitLdcInsn(Object var1) {
if (var1 != null) {
params.add(var1);
super.visitLdcInsn(var1);
System.out.printf("arg: %s %n", var1.toString());
}
}
@Override
public void visitInsn(int var1) {
if(this.mv != null) {
this.mv.visitInsn(var1);
}
}
@Override
public void visitIntInsn(int var1, int var2) {
if(this.mv != null) {
this.mv.visitIntInsn(var1, var2);
}
}
@Override
public void visitVarInsn(int var1, int var2) {
if(this.mv != null) {
this.mv.visitVarInsn(var1, var2);
}
}
@Override
public void visitMethodInsn(int opcode, String owner, String name, String desc) {
Pattern pattern = Pattern.compile("[a-zA-Z0-9._]*");
System.out.printf("---------------------------%n");
System.out.printf("Class %s calls method %s from class %s%n", ClassPrinterVisitor.this.name, name, owner);
System.out.printf("Desc: %s signature: %s%n", ClassPrinterVisitor.this.desc, ClassPrinterVisitor.this.signature);
for (Object p : params) {
Matcher matcher = pattern.matcher(p.toString());
if (!p.toString().isEmpty() && !p.toString().startsWith(".") && matcher.matches()) {
System.out.printf("visitLdcInsn: %s %n", p);
}
}
System.out.printf("---------------------------%n%n");
params = new ArrayList<>();
}
@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, className, name, signature);
super.visitCode();
}
}
}
当使用 ASM 访问方法的字节码时,您不能“在调用某些方法时获取作为参数传递的值”,我认为您可能错误地理解了这些引用。
ASM 只是一个用于字节码操作的库。在对方法进行操作时,字节码中显示的数字只是局部变量索引(参数和局部变量),您无法获取变量引用的 VALUE(如果它不是常量)。
获取这些局部变量索引的VALUEs的唯一可能是执行被访问的字节码。方法的字节码是静态的,这些变量索引引用的值是未知的。当执行一个方法的字节码时,这些索引引用了一些对象,VM 知道。
您需要实施 Data Flow Analysis,对于您的情况,它将是 向后 数据流分析。
看看 ASM 的 SourceInterpreter,
这个 SO 回答
由于Control Flow,单个参数可能有多个可能的值。由于外部输入,您可能无法确定一个值(尽管您可能能够从更广泛的上下文中确定诸如 nullness 之类的东西)。
对于给出的示例来说并不难,只有常量或对相同 class 上的方法的调用产生常量 - 但扩展到一般情况可能是一项重要的工作。可能值得看一下执行静态代码分析的 "checker" 类型框架。