As explained in this answer, ASM’s approach of calculating the (most specific) common superclass does not necessarily reproduce the stackmap frames of the original class. It does not only need to access the classes (which you could work-around), but may access classes, the original code never referred to, either because the original code used a more abstract type or an interface type or because the original frames actually dropped the subsequently unused value instead of declaring a merged type.
So the preferable approach is to calculate the stackmap frames based on the original frames, according to the code modifications you made. For your intended use case, that’s quiet easy, as you’re not changing the branch structure of the code but just inject code which leaves the stack state exactly as it was before the inserted code fragment.
So in principle, it should be possible to just use the original frames. To achieve this, don’t specify the COMPUTE_FRAMES
to the ClassWriter
and don’t specify SKIP_FRAMES
to the ClassReader
. You only have to adjust the maximum stack size if the original size was less than two, to ensure that there’s room for your method arguments.
The actual problems with your Agent come from the attempt to use source code lines for determining the code locations for inserting the calls. To illustrate this, consider the following example:
public class Example {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
System.out.println(i);
}
}
}
I use the following code to show, which ASM calls will be made to your visitor:
public static void main(String[] args) throws IOException {
ClassReader cr = new ClassReader("Example");
cr.accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
System.out.println(name+desc);
return new PrintingVisitor();
}
}, 0);
}
static class PrintingVisitor extends MethodVisitor {
final Map<Label,Integer> labels = new HashMap<>();
public PrintingVisitor() {
super(Opcodes.ASM5);
}
private String name(Label label) {
return "label_"+labels.merge(label, labels.size(), (a,b) -> a);
}
@Override public void visitCode() {
System.out.println("visitCode()");
}
@Override public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) {
System.out.println("visitFrame()");
}
@Override public void visitLabel(Label label) {
System.out.println("."+name(label));
}
@Override public void visitLineNumber(int line, Label start) {
System.out.println(".line "+line+", "+name(start));
}
@Override public void visitJumpInsn(int opcode, Label label) {
System.out.println(get(opcode)+" "+name(label));
}
@Override public void visitInsn(int opcode) {
System.out.println(get(opcode));
}
@Override
public void visitIincInsn(int var, int increment) {
System.out.println("iinc "+var+", "+increment);
}
@Override public void visitEnd() {
System.out.println();
}
}
static String get(int opcode) {
// for simplification, just the ones we need
switch(opcode) {
case Opcodes.RETURN: return "return";
case Opcodes.ICONST_0: return "iconst_0";
case Opcodes.ILOAD: return "iload";
case Opcodes.IF_ICMPGE: return "if_icmpge";
case Opcodes.GOTO: return "goto";
default: return "<"+opcode+">";
}
}
Which produces (when compiled with javac
):
main([Ljava/lang/String;)V
visitCode()
.label_0
.line 3, label_0
iconst_0
.label_1
visitFrame()
if_icmpge label_2
.label_3
.line 4, label_3
.label_4
.line 3, label_4
iinc 1, 1
goto label_1
.label_2
.line 6, label_2
visitFrame()
return
.label_5
Which demonstrates:
- The “first line”, i.e. line 3 is reported two times, as the loop generates code at its end which is associated with the location of the
for
loop statement
- The “last line”, i.e. line 6 is reported before the
visitFrame()
that describes the stack state of the branch target of the loop end. The label_2
is used for both, reporting the source code line and as target of the if_icmpge
instruction. When delegating the visitLabel
call to the ClassWriter
, you’re defining the branch target and the branch target requires the stackmap frame, so there must be no code between the visitLabel
and visitFrame
calls, but the visitLineNumber
call, which you used to insert code, is made right between them.
The solution:
Inject code right at the visitCode()
call for the beginning of the method. That’s before anything else happens and won’t conflict with any subsequent operation:
@Override public void visitCode() {
super.visitCode();
mv.visitMethodInsn(INVOKESTATIC, "de/ugoe/cs/listener/CallHelper", "raiseDepth", "()V", false);
}
For injecting code at the end of the method, just use the precise instructions which can end a method, i.e.
@Override public void visitInsn(int opcode) {
switch(opcode) {
case RETURN: case ARETURN: case IRETURN: case LRETURN: case FRETURN: case DRETURN:
case ATHROW:
mv.visitMethodInsn(INVOKESTATIC, "de/ugoe/cs/listener/CallHelper", "lowerDepth", "()V", false);
}
super.visitInsn(opcode);
}
Note that this is not sufficient to get a finally
like semantic of calling the method in every case. E.g. when an invoked method throws an exception or the runtime generates it like when dereferencing null
or dividing by zero, the method might not get called, but your original code had the issue.
For injecting code at arbitrary source code lines, there is no straight-forward solution. As shown, source code lines do not map 1:1 to bytecode locations and the reported locations might be at places where injecting is not possible. It’s much better to pick an additional criteria like an easy-to-identify code construct, for example, a known method invocation, to insert before or after it.
invokestatic
calls doesn’t change the code structure, hence, it should be possible while keeping the original stackmaps. Problems may arise due to the attempt to insert at source code lines. There is no 1:1 mapping between source code lines and bytecode locations, further, ASM report the lines usingLabel
s, which may interact with the way ASM reports the stackmap frames (i.e. also usingLabel
s.