Search in sources :

Example 56 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project CodeChickenLib by Chicken-Bones.

the class InsnComparator method getControlFlowLabels.

public static Set<LabelNode> getControlFlowLabels(InsnList list) {
    HashSet<LabelNode> controlFlowLabels = new HashSet<LabelNode>();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        switch(insn.getType()) {
            case JUMP_INSN:
                JumpInsnNode jinsn = (JumpInsnNode) insn;
                controlFlowLabels.add(jinsn.label);
                break;
            case TABLESWITCH_INSN:
                TableSwitchInsnNode tsinsn = (TableSwitchInsnNode) insn;
                controlFlowLabels.add(tsinsn.dflt);
                for (LabelNode label : tsinsn.labels) controlFlowLabels.add(label);
                break;
            case LOOKUPSWITCH_INSN:
                LookupSwitchInsnNode lsinsn = (LookupSwitchInsnNode) insn;
                controlFlowLabels.add(lsinsn.dflt);
                for (LabelNode label : lsinsn.labels) controlFlowLabels.add(label);
                break;
        }
    }
    return controlFlowLabels;
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 57 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project CodeChickenLib by Chicken-Bones.

the class InsnComparator method getImportantList.

public static InsnListSection getImportantList(InsnListSection list) {
    if (list.size() == 0)
        return list;
    Set<LabelNode> controlFlowLabels = getControlFlowLabels(list);
    Map<LabelNode, LabelNode> labelMap = Maps.asMap(controlFlowLabels, new Function<LabelNode, LabelNode>() {

        @Override
        public LabelNode apply(LabelNode input) {
            return input;
        }
    });
    InsnListSection importantNodeList = new InsnListSection();
    for (AbstractInsnNode insn : list) if (insnImportant(insn, controlFlowLabels))
        importantNodeList.add(insn.clone(labelMap));
    return importantNodeList;
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 58 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project CodeChickenLib by Chicken-Bones.

the class ASMBlock method applyLabels.

public ASMBlock applyLabels(InsnListSection list2) {
    if (labels.isEmpty())
        return new ASMBlock(list2);
    Set<LabelNode> cFlowLabels1 = labels.values();
    Set<LabelNode> cFlowLabels2 = InsnComparator.getControlFlowLabels(list2);
    ASMBlock block = new ASMBlock(list2);
    HashMap<LabelNode, LabelNode> labelMap = new HashMap<LabelNode, LabelNode>();
    for (int i = 0, k = 0; i < list.size() && k < list2.size(); ) {
        AbstractInsnNode insn1 = list.get(i);
        if (!InsnComparator.insnImportant(insn1, cFlowLabels1)) {
            i++;
            continue;
        }
        AbstractInsnNode insn2 = list2.get(k);
        if (!InsnComparator.insnImportant(insn2, cFlowLabels2)) {
            k++;
            continue;
        }
        if (insn1.getOpcode() != insn2.getOpcode())
            throw new IllegalArgumentException("Lists do not match:\n" + list + "\n\n" + list2);
        switch(insn1.getType()) {
            case LABEL:
                labelMap.put((LabelNode) insn1, (LabelNode) insn2);
                break;
            case JUMP_INSN:
                labelMap.put(((JumpInsnNode) insn1).label, ((JumpInsnNode) insn2).label);
                break;
        }
        i++;
        k++;
    }
    for (Entry<String, LabelNode> entry : labels.entrySet()) block.labels.put(entry.getKey(), labelMap.get(entry.getValue()));
    return block;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 59 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project neo4j by neo4j.

the class ByteCodeVerifier method detailedMessage.

private static String detailedMessage(String errorMessage, MethodNode method, Frame[] frames, AbstractInsnNode errorLocation) {
    StringWriter message = new StringWriter();
    try (PrintWriter out = new PrintWriter(message)) {
        List<Integer> localLengths = new ArrayList<>();
        List<Integer> stackLengths = new ArrayList<>();
        for (Frame frame : frames) {
            if (frame != null) {
                for (int i = 0; i < frame.getLocals(); i++) {
                    insert(i, frame.getLocal(i), localLengths);
                }
                for (int i = 0; i < frame.getStackSize(); i++) {
                    insert(i, frame.getStack(i), stackLengths);
                }
            }
        }
        Textifier formatted = new Textifier();
        TraceMethodVisitor mv = new TraceMethodVisitor(formatted);
        out.println(errorMessage);
        out.append("\t\tin ").append(method.name).append(method.desc).println();
        for (int i = 0; i < method.instructions.size(); i++) {
            AbstractInsnNode insn = method.instructions.get(i);
            insn.accept(mv);
            Frame frame = frames[i];
            out.append("\t\t");
            out.append(insn == errorLocation ? ">>> " : "    ");
            out.format("%05d [", i);
            if (frame == null) {
                padding(out, localLengths.listIterator(), '?');
                out.append(" : ");
                padding(out, stackLengths.listIterator(), '?');
            } else {
                emit(out, localLengths, frame::getLocal, frame.getLocals());
                padding(out, localLengths.listIterator(frame.getLocals()), '-');
                out.append(" : ");
                emit(out, stackLengths, frame::getStack, frame.getStackSize());
                padding(out, stackLengths.listIterator(frame.getStackSize()), ' ');
            }
            out.print("] : ");
            out.print(formatted.text.get(formatted.text.size() - 1));
        }
        for (int j = 0; j < method.tryCatchBlocks.size(); j++) {
            method.tryCatchBlocks.get(j).accept(mv);
            out.print(" " + formatted.text.get(formatted.text.size() - 1));
        }
    }
    return message.toString();
}
Also used : Frame(org.objectweb.asm.tree.analysis.Frame) StringWriter(java.io.StringWriter) ArrayList(java.util.ArrayList) Textifier(org.objectweb.asm.util.Textifier) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) TraceMethodVisitor(org.objectweb.asm.util.TraceMethodVisitor) PrintWriter(java.io.PrintWriter)

Example 60 with AbstractInsnNode

use of org.objectweb.asm.tree.AbstractInsnNode in project pinpoint by naver.

the class ASMMethodNodeAdapter method addAfterInterceptor.

public void addAfterInterceptor(final int interceptorId, final InterceptorDefinition interceptorDefinition, final int apiId) {
    initInterceptorLocalVariables(interceptorId, interceptorDefinition, apiId);
    // add try catch block.
    final ASMTryCatch tryCatch = new ASMTryCatch(this.methodNode);
    this.methodNode.instructions.insertBefore(this.methodVariables.getEnterInsnNode(), tryCatch.getStartLabelNode());
    this.methodNode.instructions.insert(this.methodVariables.getExitInsnNode(), tryCatch.getEndLabelNode());
    // find return.
    AbstractInsnNode insnNode = this.methodNode.instructions.getFirst();
    while (insnNode != null) {
        final int opcode = insnNode.getOpcode();
        if (this.methodVariables.isReturnCode(opcode)) {
            final InsnList instructions = new InsnList();
            this.methodVariables.storeResultVar(instructions, opcode);
            invokeAfterInterceptor(instructions, interceptorDefinition, false);
            this.methodNode.instructions.insertBefore(insnNode, instructions);
        }
        insnNode = insnNode.getNext();
    }
    // try catch handler.
    InsnList instructions = new InsnList();
    this.methodVariables.storeThrowableVar(instructions);
    invokeAfterInterceptor(instructions, interceptorDefinition, true);
    // throw exception.
    this.methodVariables.loadInterceptorThrowVar(instructions);
    this.methodNode.instructions.insert(tryCatch.getEndLabelNode(), instructions);
    tryCatch.sort();
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList)

Aggregations

AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)185 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)68 MethodNode (org.objectweb.asm.tree.MethodNode)54 InsnList (org.objectweb.asm.tree.InsnList)53 InsnNode (org.objectweb.asm.tree.InsnNode)41 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)38 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)37 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)35 ClassNode (org.objectweb.asm.tree.ClassNode)33 LabelNode (org.objectweb.asm.tree.LabelNode)27 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 Test (org.junit.Test)25 Label (org.objectweb.asm.Label)25 ClassReader (org.objectweb.asm.ClassReader)23 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)23 HashSet (java.util.HashSet)16 Type (org.objectweb.asm.Type)15 Frame (org.objectweb.asm.tree.analysis.Frame)13 ArrayList (java.util.ArrayList)12 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)11