Search in sources :

Example 6 with InsnList

use of org.objectweb.asm.tree.InsnList in project flink by apache.

the class ModifiedASMAnalyzer method newControlFlowEdge.

@Override
protected void newControlFlowEdge(int insn, int successor) {
    try {
        if (jumpModificationState == PRE_STATE) {
            jumpModificationState = MOD_STATE;
        } else if (jumpModificationState == MOD_STATE) {
            // for a later merge
            if (jumpModification == IFEQ_MOD) {
                final int top = accessField(Analyzer.class, "top").getInt(this);
                final int[] queue = (int[]) accessField(Analyzer.class, "queue").get(this);
                final int tmp = queue[top - 2];
                queue[top - 2] = queue[top - 1];
                queue[top - 1] = tmp;
                eventInsn = queue[top - 2] - 1;
                final InsnList insns = (InsnList) accessField(Analyzer.class, "insns").get(this);
                // if yes this is loop structure
                if (insns.get(eventInsn) instanceof JumpInsnNode) {
                    jumpModificationState = WAIT_FOR_INSN_STATE;
                } else // no loop -> end of modification
                {
                    jumpModificationState = DO_NOTHING;
                }
            } else // this modification changes the merge priority of certain frames (the expression part of the IF)
            if (jumpModification == IFNE_MOD) {
                final Frame[] frames = (Frame[]) accessField(Analyzer.class, "frames").get(this);
                final Field indexField = accessField(AbstractInsnNode.class, "index");
                final InsnList insns = (InsnList) accessField(Analyzer.class, "insns").get(this);
                final AbstractInsnNode gotoInsnn = insns.get(successor - 1);
                // check for a loop
                if (gotoInsnn instanceof JumpInsnNode) {
                    jumpModificationState = WAIT_FOR_INSN_STATE;
                    // sets a merge priority for all instructions (the expression of the IF)
                    // from the label the goto instruction points to until the evaluation with IFEQ
                    final int idx = indexField.getInt(accessField(JumpInsnNode.class, "label").get(gotoInsnn));
                    for (int i = idx; i <= insn; i++) {
                        ((ModifiedASMFrame) frames[i]).mergePriority = true;
                    }
                    eventInsn = idx - 2;
                } else {
                    jumpModificationState = DO_NOTHING;
                }
            }
        } else // wait for the goto instruction
        if (jumpModificationState == WAIT_FOR_INSN_STATE && insn == eventInsn) {
            jumpModificationState = DO_NOTHING;
            final Frame[] frames = (Frame[]) accessField(Analyzer.class, "frames").get(this);
            // this ensures that local variables are kept
            if (jumpModification == IFEQ_MOD) {
                interpreter.rightMergePriority = true;
                final Field top = accessField(Frame.class, "top");
                top.setInt(frames[eventInsn], top.getInt(frames[eventInsn + 1]));
                frames[eventInsn + 1].merge(frames[eventInsn], interpreter);
            } else // finally set a merge priority for the last instruction of the loop (before the IF expression)
            if (jumpModification == IFNE_MOD) {
                ((ModifiedASMFrame) frames[eventInsn + 1]).mergePriority = true;
            }
        }
    } catch (Exception e) {
        throw new CodeAnalyzerException("Unable to do jump modifications.", e);
    }
}
Also used : Field(java.lang.reflect.Field) Frame(org.objectweb.asm.tree.analysis.Frame) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) Analyzer(org.objectweb.asm.tree.analysis.Analyzer) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 7 with InsnList

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

the class ASMClassNodeAdapter method addGetterMethod.

public void addGetterMethod(final String methodName, final ASMFieldNodeAdapter fieldNode) {
    if (methodName == null || fieldNode == null) {
        throw new IllegalArgumentException("method name or fieldNode annotation must not be null.");
    }
    // no argument is ().
    final String desc = "()" + fieldNode.getDesc();
    final MethodNode methodNode = new MethodNode(Opcodes.ACC_PUBLIC, methodName, desc, null, null);
    if (methodNode.instructions == null) {
        methodNode.instructions = new InsnList();
    }
    final InsnList instructions = methodNode.instructions;
    // load this.
    instructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
    // get fieldNode.
    instructions.add(new FieldInsnNode(Opcodes.GETFIELD, classNode.name, fieldNode.getName(), fieldNode.getDesc()));
    // return of type.
    final Type type = Type.getType(fieldNode.getDesc());
    instructions.add(new InsnNode(type.getOpcode(Opcodes.IRETURN)));
    if (this.classNode.methods == null) {
        this.classNode.methods = new ArrayList<MethodNode>();
    }
    this.classNode.methods.add(methodNode);
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) MethodNode(org.objectweb.asm.tree.MethodNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 8 with InsnList

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

the class ASMMethodNodeAdapter method addDelegator.

public void addDelegator(final String superClassInternalName) {
    if (superClassInternalName == null) {
        throw new IllegalArgumentException("super class internal name must not be null.");
    }
    final InsnList instructions = this.methodNode.instructions;
    if (isStatic()) {
        this.methodVariables.initLocalVariables(instructions);
        // load parameters
        this.methodVariables.loadArgs(instructions);
        // invoke static
        instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, superClassInternalName, this.methodNode.name, this.methodNode.desc, false));
    } else {
        this.methodVariables.initLocalVariables(instructions);
        // load this
        this.methodVariables.loadVar(instructions, 0);
        // load parameters
        this.methodVariables.loadArgs(instructions);
        // invoke special
        instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, superClassInternalName, this.methodNode.name, this.methodNode.desc, false));
    }
    // return
    this.methodVariables.returnValue(instructions);
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) InsnList(org.objectweb.asm.tree.InsnList)

Example 9 with InsnList

use of org.objectweb.asm.tree.InsnList in project spring-loaded by spring-projects.

the class TypeDiffComputer method computeAnyMethodDifferences.

/**
	 * Determine if there any differences between the methods supplied. A MethodDelta object is built to record any
	 * differences and stored against the type delta.
	 *
	 * @param oMethod 'old' method
	 * @param nMethod 'new' method
	 * @param td the type delta where changes are currently being accumulated
	 */
private static void computeAnyMethodDifferences(MethodNode oMethod, MethodNode nMethod, TypeDelta td) {
    MethodDelta md = new MethodDelta(oMethod.name, oMethod.desc);
    if (oMethod.access != nMethod.access) {
        md.setAccessChanged(oMethod.access, nMethod.access);
    }
    // TODO annotations
    InsnList oInstructions = oMethod.instructions;
    InsnList nInstructions = nMethod.instructions;
    if (oInstructions.size() != nInstructions.size()) {
        md.setInstructionsChanged(oInstructions.toArray(), nInstructions.toArray());
    } else {
        // TODO Just interested in constructors right now - should add others
        if (oMethod.name.charAt(0) == '<') {
            String oInvokeSpecialDescriptor = null;
            String nInvokeSpecialDescriptor = null;
            int oUninitCount = 0;
            int nUninitCount = 0;
            boolean codeChange = false;
            for (int i = 0, max = oInstructions.size(); i < max; i++) {
                AbstractInsnNode oInstruction = oInstructions.get(i);
                AbstractInsnNode nInstruction = nInstructions.get(i);
                if (!codeChange) {
                    if (!sameInstruction(oInstruction, nInstruction)) {
                        codeChange = true;
                    }
                }
                if (oInstruction.getType() == AbstractInsnNode.TYPE_INSN) {
                    if (oInstruction.getOpcode() == Opcodes.NEW) {
                        oUninitCount++;
                    }
                }
                if (nInstruction.getType() == AbstractInsnNode.TYPE_INSN) {
                    if (nInstruction.getOpcode() == Opcodes.NEW) {
                        nUninitCount++;
                    }
                }
                if (oInstruction.getType() == AbstractInsnNode.METHOD_INSN) {
                    MethodInsnNode mi = (MethodInsnNode) oInstruction;
                    if (mi.getOpcode() == INVOKESPECIAL && mi.name.equals("<init>")) {
                        if (oUninitCount == 0) {
                            // this is the one!
                            oInvokeSpecialDescriptor = mi.desc;
                        } else {
                            oUninitCount--;
                        }
                    }
                }
                if (nInstruction.getType() == AbstractInsnNode.METHOD_INSN) {
                    MethodInsnNode mi = (MethodInsnNode) nInstruction;
                    if (mi.getOpcode() == INVOKESPECIAL && mi.name.equals("<init>")) {
                        if (nUninitCount == 0) {
                            // this is the one!
                            nInvokeSpecialDescriptor = mi.desc;
                        } else {
                            nUninitCount--;
                        }
                    }
                }
            }
            // Has the invokespecial changed?
            if (oInvokeSpecialDescriptor == null) {
                if (nInvokeSpecialDescriptor != null) {
                    md.setInvokespecialChanged(oInvokeSpecialDescriptor, nInvokeSpecialDescriptor);
                }
            } else {
                if (!oInvokeSpecialDescriptor.equals(nInvokeSpecialDescriptor)) {
                    md.setInvokespecialChanged(oInvokeSpecialDescriptor, nInvokeSpecialDescriptor);
                }
            }
            if (codeChange) {
                md.setCodeChanged(oInstructions.toArray(), nInstructions.toArray());
            }
        }
    }
    if (md.hasAnyChanges()) {
        // it needs recording
        td.addChangedMethod(md);
    }
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 10 with InsnList

use of org.objectweb.asm.tree.InsnList in project Bookshelf by Darkhax-Minecraft.

the class InstructionComparator method getImportantList.

// TODO: Add documentation
public static InsnList getImportantList(InsnList list) {
    if (list.size() == 0) {
        return list;
    }
    final HashMap<LabelNode, LabelNode> labels = new HashMap<>();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode) {
            labels.put((LabelNode) insn, (LabelNode) insn);
        }
    }
    final InsnList importantNodeList = new InsnList();
    for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
        if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
            continue;
        }
        importantNodeList.add(insn.clone(labels));
    }
    return importantNodeList;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) HashMap(java.util.HashMap) LineNumberNode(org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList)

Aggregations

InsnList (org.objectweb.asm.tree.InsnList)11 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)5 MethodNode (org.objectweb.asm.tree.MethodNode)4 InsnNode (org.objectweb.asm.tree.InsnNode)3 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)3 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)3 Type (org.objectweb.asm.Type)2 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)2 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)2 LabelNode (org.objectweb.asm.tree.LabelNode)2 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 RemappingClassAdapter (org.objectweb.asm.commons.RemappingClassAdapter)1 ClassNode (org.objectweb.asm.tree.ClassNode)1 LineNumberNode (org.objectweb.asm.tree.LineNumberNode)1 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)1 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)1 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)1 Analyzer (org.objectweb.asm.tree.analysis.Analyzer)1 Frame (org.objectweb.asm.tree.analysis.Frame)1