Example usage for org.objectweb.asm.tree InsnList get

List of usage examples for org.objectweb.asm.tree InsnList get

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList get.

Prototype

public AbstractInsnNode get(final int index) 

Source Link

Document

Returns the instruction whose index is given.

Usage

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InstructionComparator.java

License:Open Source License

public static List<AbstractInsnNode> insnListFindEnd(InsnList haystack, InsnList needle) {
    LinkedList<AbstractInsnNode> callNodes = new LinkedList<AbstractInsnNode>();
    for (int callPoint : insnListFind(haystack, needle)) {
        callNodes.add(haystack.get(callPoint + needle.size() - 1));
    }//from www.j  a  v a 2 s . co m
    return callNodes;
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InstructionComparator.java

License:Open Source License

private static InsnListSection insnListMatchesL(InsnList haystack, InsnList needle, int start,
        HashSet<LabelNode> controlFlowLabels) {
    int h = start, n = 0;
    for (; h < haystack.size() && n < needle.size(); h++) {
        AbstractInsnNode insn = haystack.get(h);
        if (insn.getType() == 15) {
            continue;
        }//from  w w w . j  ava 2 s  . c  o m
        if (insn.getType() == 8 && !controlFlowLabels.contains(insn)) {
            continue;
        }

        if (!insnEqual(haystack.get(h), needle.get(n))) {
            return null;
        }
        n++;
    }
    if (n != needle.size()) {
        return null;
    }

    return new InsnListSection(haystack, start, h - 1);
}

From source file:org.apache.flink.api.java.sca.ModifiedASMAnalyzer.java

License:Apache License

@Override
protected void newControlFlowEdge(int insn, int successor) {
    try {/*from   ww w  .  j a v  a 2  s  .com*/
        if (jumpModificationState == PRE_STATE) {
            jumpModificationState = MOD_STATE;
        } else if (jumpModificationState == MOD_STATE) {
            // this modification swaps the top 2 values on the queue stack
            // it ensures that the "TRUE" path will be executed first, which is important
            // 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);
                // check if instruction is a goto instruction
                // if yes this is loop structure
                if (insns.get(eventInsn) instanceof JumpInsnNode) {
                    jumpModificationState = WAIT_FOR_INSN_STATE;
                }
                // no loop -> end of modification
                else {
                    jumpModificationState = DO_NOTHING;
                }
            }
            // this modification changes the merge priority of certain frames (the expression part of the IF)
            else 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;
                }
            }
        }
        // wait for the goto instruction
        else if (jumpModificationState == WAIT_FOR_INSN_STATE && insn == eventInsn) {
            jumpModificationState = DO_NOTHING;
            final Frame[] frames = (Frame[]) accessField(Analyzer.class, "frames").get(this);
            // merge the goto instruction frame with the frame that follows
            // 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);
            }
            // finally set a merge priority for the last instruction of the loop (before the IF expression)
            else if (jumpModification == IFNE_MOD) {
                ((ModifiedASMFrame) frames[eventInsn + 1]).mergePriority = true;
            }
        }
    } catch (Exception e) {
        throw new CodeAnalyzerException("Unable to do jump modifications.", e);
    }
}

From source file:org.brutusin.instrumentation.utils.Helper.java

License:Apache License

public static void viewByteCode(byte[] bytecode) {
    ClassReader cr = new ClassReader(bytecode);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);/*from w  w  w . j a  v  a 2  s  . c  om*/
    final List<MethodNode> mns = cn.methods;
    Printer printer = new Textifier();
    TraceMethodVisitor mp = new TraceMethodVisitor(printer);
    for (MethodNode mn : mns) {
        InsnList inList = mn.instructions;
        System.out.println(mn.name);
        for (int i = 0; i < inList.size(); i++) {
            inList.get(i).accept(mp);
            StringWriter sw = new StringWriter();
            printer.print(new PrintWriter(sw));
            printer.getText().clear();
            System.out.print(sw.toString());
        }
    }
}

From source file:org.coldswap.asm.method.PublicFloatMethodReplacer.java

License:Open Source License

private InsnList replaceReturn(InsnList insnList, Type retType) {
    final Type rretType = retType;
    int retOpcode = MethodUtil.getRetOpcodeToReplace(retType);
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode absIns = insnList.get(i);
        int opcode = absIns.getOpcode();
        if (opcode == retOpcode) {
            // if tries to return a Reference type into a primitive then
            // remove the unbox( we return an Object). If a primitive is returned
            // into a  primitive then we must try to box from primitive to Object/Integer, etc..

            // check if an unbox takes place before return
            final boolean[] isBoxUnbox = { false, false };
            AbstractInsnNode valueOf = null;
            AbstractInsnNode primitiveValue = null;
            if (i > 1) {
                valueOf = insnList.get(i - 1);
                primitiveValue = insnList.get(i - 2);
                if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) {
                    valueOf.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if (AutoBoxing.isPrimitive(rretType.getDescriptor())) {
                                if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) {
                                    isBoxUnbox[0] = true;
                                }// w w w.  j  av a2  s .  c  o  m
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }

                if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) {
                                isBoxUnbox[1] = true;
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }
            }

            if (isBoxUnbox[0] && isBoxUnbox[1]) {
                // remove indexes
                insnList.remove(valueOf);
                insnList.remove(primitiveValue);
            } else {
                InsnList iList = new InsnList();
                iList.add(AutoBoxing.box(retType));
                iList.add(new InsnNode(Opcodes.ARETURN));
                insnList.insertBefore(absIns, iList);
                insnList.remove(absIns);

            }
        }
    }
    return insnList;
}

From source file:org.coldswap.asm.method.PublicObjectMethodReplacer.java

License:Open Source License

private InsnList replaceReturn(InsnList insnList, Type retType) {
    final Type rretType = retType;
    int retOpcode = MethodUtil.getRetOpcodeToReplace(retType);
    for (int i = 0; i < insnList.size(); i++) {
        AbstractInsnNode absIns = insnList.get(i);
        int opcode = absIns.getOpcode();
        if (opcode == retOpcode) {
            // if tries to return a Reference type into a primitive then
            // remove the unbox( we return an Object). If a primitive is returned
            // into a  primitive then we must try to box from primitive to Object/Integer, etc..

            // check if an unbox takes place before return
            final boolean[] isBoxUnbox = { false, false };
            AbstractInsnNode valueOf = null;
            AbstractInsnNode primitiveValue = null;
            if (i > 1) {
                valueOf = insnList.get(i - 1);
                primitiveValue = insnList.get(i - 2);
                if (valueOf.getOpcode() == Opcodes.INVOKESTATIC) {
                    valueOf.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if (AutoBoxing.isPrimitive(rretType.getDescriptor())) {
                                if ((AutoBoxing.getBoxClassName(rretType) + ".valueOf").equals(s + s2)) {
                                    isBoxUnbox[0] = true;
                                }//from  w  w  w . j a v  a 2  s. c om
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }

                if (isBoxUnbox[0] && primitiveValue.getOpcode() == Opcodes.INVOKEVIRTUAL) {
                    primitiveValue.accept(new MethodVisitor(Opcodes.ASM5) {
                        @Override
                        public void visitMethodInsn(int i, String s, String s2, String s3) {
                            if ((s + s2).equals(AutoBoxing.getUnBoxInvoke(rretType))) {
                                isBoxUnbox[1] = true;
                            }
                            super.visitMethodInsn(i, s, s2, s3);
                        }
                    });
                }
            }

            if (isBoxUnbox[0] && isBoxUnbox[1]) {
                // remove indexes
                insnList.remove(valueOf);
                insnList.remove(primitiveValue);
            } else {
                InsnList iList = new InsnList();
                iList.add(AutoBoxing.box(retType));
                iList.add(new InsnNode(Opcodes.ARETURN));
                insnList.insertBefore(absIns, iList);
                insnList.remove(absIns);
            }
        }
    }
    return insnList;
}

From source file:org.eclipse.objectteams.otredyn.bytecode.asm.AbstractCreateDispatchCodeAdapter.java

License:Open Source License

private LabelNode findBreakLabel(InsnList instructions) {
    for (int i = instructions.size() - 1; i >= 0; i--) {
        AbstractInsnNode node = instructions.get(i);
        if (node.getType() == AbstractInsnNode.LABEL) {
            return (LabelNode) node;
        }//from   w w w  .  j  av a2s  .c o  m
    }
    throw new RuntimeException("Can't find break label to create dispatch code");
}

From source file:org.epoxide.surge.asm.ASMUtils.java

License:Creative Commons License

/**
 * Removes a specific set of instructions (the needle) from a much larger set of
 * instructions (the hay stack). Be cautious when using this method, as it is almost never
 * a good idea to remove instructions./*ww  w .j a v  a  2s  .  c o m*/
 *
 * @param haystack: A large list of instructions which is being searched through.
 * @param needle: A specific list of instructions which are to be removed from the larger
 *        instruction list.
 */
public static void removeNeedleFromHaystack(InsnList haystack, InsnList needle) {

    final int firstInd = haystack.indexOf(findFirstNodeFromNeedle(haystack, needle));
    final int lastInd = haystack.indexOf(findLastNodeFromNeedle(haystack, needle));
    final List<AbstractInsnNode> realNeedle = new ArrayList<>();

    for (int i = firstInd; i <= lastInd; i++) {
        realNeedle.add(haystack.get(i));
    }

    for (final AbstractInsnNode node : realNeedle) {
        haystack.remove(node);
    }
}

From source file:org.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

public static List<AbstractInsnNode> insnListFindStart(InsnList haystack, InsnList needle) {

    final LinkedList<AbstractInsnNode> callNodes = new LinkedList<>();

    for (final int callPoint : insnListFind(haystack, needle)) {
        callNodes.add(haystack.get(callPoint));
    }/*from   w w w . ja va2 s. c o  m*/

    return callNodes;
}

From source file:org.epoxide.surge.asm.InstructionComparator.java

License:Creative Commons License

public static List<AbstractInsnNode> insnListFindEnd(InsnList haystack, InsnList needle) {

    final LinkedList<AbstractInsnNode> callNodes = new LinkedList<>();

    for (final int callPoint : insnListFind(haystack, needle)) {
        callNodes.add(haystack.get(callPoint + needle.size() - 1));
    }/*from  w  ww  .  j  a  va2s  .  com*/

    return callNodes;
}