问题 1 也许是最困难的。您需要通过识别某种模式来找出插入指令的位置。如果你假设firstPar.check()
仅被调用一次,那么您可以查找以下字节码指令if(firstPar.check())
:
ALOAD 1
INVOKEVIRTUAL Data.check ()Z
IFEQ L3
where L3
是跳转标签如果check
return false
.
对于问题 2,请注意字节码指令if(firstPar.check() && !Utilities.someOtherCheck(firstPar, secondPar))
is:
ALOAD 1
INVOKEVIRTUAL Data.check ()Z
IFEQ L3
ALOAD 1
ALOAD 2
INVOKESTATIC Utilities.someOtherCheck (LData;LData;)Z
IFNE L3
因此,您需要在后面插入 4 条新指令IFEQ L3
.
您可以通过使用Tree API http://asm.ow2.org/asm50/javadoc/user/org/objectweb/asm/tree/package-summary.html,您可以在其中创建一个适配器targetMethod
通过子类化ClassVisitor
and MethodNode
:
private static class ClassAdapter extends ClassVisitor {
public ClassAdapter(ClassVisitor cv) {
super(Opcodes.ASM5, cv);
}
@Override
public MethodVisitor visitMethod(int access, String name, String desc,
String signature, String[] exceptions) {
MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
if (name.equals("targetMethod"))
return new MethodAdapter(access, name, desc, signature, exceptions, mv);
else
return mv;
}
}
private static class MethodAdapter extends MethodNode {
public MethodAdapter(int access, String name, String desc,
String signature, String[] exceptions, MethodVisitor mv) {
super(Opcodes.ASM5, access, name, desc, signature, exceptions);
this.mv = mv;
}
// More to come ...
}
Inside MethodAdapter
,你可以覆盖visitEnd
迭代所有instructions
在方法内部,尝试检测上述 3 条指令,并在它们后面插入 4 条新指令:
@Override
public void visitEnd() {
// Iterates all instructions in the method
ListIterator<AbstractInsnNode> itr = instructions.iterator();
while (itr.hasNext()) {
// Checks whether the instruction is ALOAD 1
AbstractInsnNode node = itr.next();
if (node.getOpcode() != Opcodes.ALOAD
|| ((VarInsnNode) node).var != 1)
continue;
// Checks whether the next instruction is INVOKEVIRTUAL
if (node.getNext() == null
|| node.getNext().getOpcode() != Opcodes.INVOKEVIRTUAL)
continue;
// Checks the invoked method name and signature
MethodInsnNode next = (MethodInsnNode) node.getNext();
if (!next.owner.equals("Data")
|| !next.name.equals("check")
|| !next.desc.equals("()Z"))
continue;
// Checks whether the next of the next instruction is IFEQ
AbstractInsnNode next2 = next.getNext();
if (next2 == null
|| next2.getOpcode() != Opcodes.IFEQ)
continue;
// Creates a list instructions to be inserted
InsnList list = new InsnList();
list.add(new VarInsnNode(Opcodes.ALOAD, 1));
list.add(new VarInsnNode(Opcodes.ALOAD, 2));
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
"Utilities", "someOtherCheck",
"(LData;LData;)Z", false));
list.add(new JumpInsnNode(Opcodes.IFNE, ((JumpInsnNode) next2).label));
// Inserts the list, updates maxStack to at least 2, and we are done
instructions.insert(next2, list);
maxStack = Math.max(2, maxStack);
break;
}
accept(mv);
}
要使用适配器,您可以将其与ClassReader
and a ClassWriter
。下面我也链一个TraceClassVisitor
打印 tmp 目录中的日志文件:
ClassReader reader = new ClassReader("Target");
ClassWriter writer = new ClassWriter(reader, 0);
TraceClassVisitor printer = new TraceClassVisitor(writer,
new PrintWriter(System.getProperty("java.io.tmpdir") + File.separator + "Target.log"));
ClassAdapter adapter = new ClassAdapter(printer);
reader.accept(adapter, 0);
byte[] b = writer.toByteArray(); // The modified bytecode