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

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

Introduction

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

Prototype

@Override
public ListIterator<AbstractInsnNode> iterator() 

Source Link

Document

Returns an iterator over the instructions in this list.

Usage

From source file:de.tuberlin.uebb.jbop.optimizer.loop.ForLoopUnroller.java

License:Open Source License

@Override
public InsnList optimize(final InsnList original, final MethodNode method) {
    optimized = false;// www  . j  a v a2s . co  m
    final Iterator<AbstractInsnNode> iterator = original.iterator();
    final InsnList insn = new InsnList();
    final LinkedList<AbstractInsnNode> skipped = new LinkedList<>();
    while (iterator.hasNext()) {
        final AbstractInsnNode currentNode = iterator.next();
        final Loop loop = LoopMatcher.getLoop(currentNode);

        if ((loop == null) || !loop.isPlain()) {
            skipped.add(currentNode);
            continue;
        }
        final AbstractInsnNode last = skipped.getLast();
        skipped.remove(last);
        correctIteratorPosition(iterator, loop.getEndOfLoop());

        optimized = true;
        for (final AbstractInsnNode node : skipped) {
            insn.add(node);
        }
        skipped.clear();
        original.remove(last);
        final ForLoop forLoop = LoopMatcher.toForLoop(loop);
        insn.add(forLoop.getInsnList(method));
    }
    for (final AbstractInsnNode node : skipped) {
        insn.add(node);
    }
    return insn;

}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.BlockTest.java

License:Open Source License

/**
 * Tests the type erasure./*from   www  .ja va  2  s . c om*/
 */
@Test
public void testTypeErasure() {
    final InsnList list = new InsnList();
    list.add(new InsnNode(Opcodes.ICONST_5));
    list.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT));
    list.add(new VarInsnNode(Opcodes.ASTORE, 3));
    list.add(new VarInsnNode(Opcodes.ILOAD, 1));
    list.add(new VarInsnNode(Opcodes.ILOAD, 2));
    list.add(new InsnNode(Opcodes.IADD));
    list.add(new InsnNode(Opcodes.ICONST_0));
    list.add(new VarInsnNode(Opcodes.ALOAD, 3));
    list.add(new InsnNode(Opcodes.IASTORE));
    list.add(new InsnNode(Opcodes.ICONST_5));
    list.add(new IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT));
    list.add(new VarInsnNode(Opcodes.ASTORE, 4));
    list.add(new VarInsnNode(Opcodes.ILOAD, 1));
    list.add(new VarInsnNode(Opcodes.ILOAD, 2));
    list.add(new InsnNode(Opcodes.IADD));
    list.add(new InsnNode(Opcodes.ICONST_1));
    final VarInsnNode insn = new VarInsnNode(Opcodes.ALOAD, 3);
    list.add(insn);
    list.add(new InsnNode(Opcodes.IASTORE));
    list.add(new InsnNode(Opcodes.RETURN));

    final ListIterator<AbstractInsnNode> iterator = list.iterator();

    final Block block = new Block(1, new Type[] { Type.INT_TYPE, Type.INT_TYPE }, Type.INT_TYPE);
    while (iterator.hasNext()) {
        block.addInsn(iterator.next(), true);
    }

    final Type findType = block.findType(insn);
    assertEquals(Type.getType("[I"), findType);
}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.MethodSplitter.java

License:Open Source License

private InsnList clean(final InsnList original) {
    final ListIterator<AbstractInsnNode> iterator = original.iterator();

    while (iterator.hasNext()) {
        final AbstractInsnNode next = iterator.next();
        if ((next instanceof SplitMarkNode) || (next.getOpcode() == NOP)) {
            original.remove(next);//from  w w  w .  j ava 2  s.c  o m
        }
    }
    return original;
}

From source file:de.tuberlin.uebb.jbop.optimizer.methodsplitter.MethodSplitter.java

License:Open Source License

private List<Block> getBlocks(final InsnList original, final MethodNode methodNode) {
    final ListIterator<AbstractInsnNode> iterator = original.iterator();
    final Type[] args = Type.getArgumentTypes(methodNode.desc);

    final Type returnType = Type.getReturnType(methodNode.desc);
    final Block currentBlock = new Block(-1, args, returnType);
    final List<Block> blocks = new ArrayList<>();
    boolean added = false;
    int num = 0;/*from  w w w .j  a v a2  s  .  co  m*/
    Block methodBlock = new Block(num, args, returnType);

    while (iterator.hasNext()) {
        final AbstractInsnNode current = iterator.next();
        if (current instanceof SplitMarkNode) {
            final int methodLength = currentBlock.getSize() + methodBlock.getSize();
            if (methodLength < maxInsns) {
                methodBlock.add(currentBlock);
                currentBlock.clear();
            } else {
                if (methodBlock.getSize() == 0) {
                    methodBlock.add(currentBlock);
                    currentBlock.clear();
                }
                added = true;
                blocks.add(methodBlock);
                num++;
                methodBlock = new Block(num, args, returnType);
                if (currentBlock.getSize() > 0) {
                    methodBlock.add(currentBlock);
                    currentBlock.clear();
                }
            }
        } else {
            currentBlock.addInsn(current);
            added = false;
        }
    }

    if (!added) {
        methodBlock.add(currentBlock);
        blocks.add(methodBlock);
    }

    return blocks;
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.FinalFieldInliner.java

License:Open Source License

@Override
public InsnList optimize(final InsnList original, final MethodNode methodNode) throws JBOPClassException {
    optimized = false;// w  w w .  j  a  v  a2 s. c o m
    final ListIterator<AbstractInsnNode> iterator = original.iterator();
    final GetFieldChainInliner fieldChainInliner = new GetFieldChainInliner();
    while (iterator.hasNext()) {
        final AbstractInsnNode currentNode = iterator.next();
        if (!NodeHelper.isAload(currentNode)) {
            continue;
        }

        fieldChainInliner.setInputObject(instance);
        fieldChainInliner.setIterator(iterator);
        fieldChainInliner.optimize(original, methodNode);
        if (fieldChainInliner.isOptimized()) {
            original.remove(currentNode);
            optimized = true;
        }
    }
    return original;
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.RemoveUnusedLocalVars.java

License:Open Source License

@Override
public InsnList optimize(final InsnList original, final MethodNode methodNode) {
    optimized = false;/*from   w  w  w  .  jav  a 2s. co  m*/
    final List<VarInsnNode> stores = new ArrayList<>();
    final List<VarInsnNode> users = new ArrayList<>();
    final List<IincInsnNode> iincs = new ArrayList<>();
    findNodes(original, stores, users, iincs);
    final List<AbstractInsnNode> toBeRemoved = new ArrayList<>();
    for (final VarInsnNode node : stores) {
        if (!usersContains(users, node)) {
            toBeRemoved.add(node);
            toBeRemoved.addAll(iincsContains(iincs, node));
        }
    }
    // final InsnList newList = new InsnList();
    final Iterator<AbstractInsnNode> iterator = original.iterator();
    while (iterator.hasNext()) {
        final AbstractInsnNode currentNode = iterator.next();
        if (toBeRemoved.contains(currentNode)) {
            if (currentNode.getOpcode() == Opcodes.IINC) {
                original.remove(currentNode);
                optimized = true;
                continue;
            }
            final AbstractInsnNode firstOfStack = NodeHelper.getFirstOfStack(currentNode);
            if (firstOfStack != null) {
                optimized = true;
                AbstractInsnNode remove = firstOfStack;
                while (remove != currentNode) {
                    final AbstractInsnNode toRemove = remove;
                    remove = remove.getNext();
                    original.remove(toRemove);
                }
                original.remove(currentNode);
            }
        }
    }
    return original;
}

From source file:de.tuberlin.uebb.jbop.optimizer.var.RemoveUnusedLocalVars.java

License:Open Source License

private void findNodes(final InsnList original, final List<VarInsnNode> stores, final List<VarInsnNode> users, //
        final List<IincInsnNode> iincs) {
    final Iterator<AbstractInsnNode> iterator = original.iterator();
    while (iterator.hasNext()) {
        final AbstractInsnNode node = iterator.next();
        if ((node.getOpcode() >= Opcodes.ISTORE) && (node.getOpcode() <= Opcodes.ASTORE)) {
            stores.add((VarInsnNode) node);
        } else if ((node.getOpcode() >= Opcodes.ILOAD) && (node.getOpcode() <= Opcodes.ALOAD)) {
            users.add((VarInsnNode) node);
        } else if (node.getOpcode() == Opcodes.IINC) {
            iincs.add((IincInsnNode) node);
        }/*w  ww  .j a v a2  s  .c  o  m*/
    }
}

From source file:de.unisb.cs.st.javalanche.mutation.bytecodeMutations.removeSystemExit.RemoveSystemExitMethodNode.java

License:Open Source License

@SuppressWarnings("unchecked")
// Call to pre-1.5 Code
@Override//from w  w w .j a v  a 2  s. c  o m
public void visitEnd() {
    MethodNode mn = (MethodNode) mv;
    InsnList insns = mn.instructions;
    Iterator i = insns.iterator();
    AbstractInsnNode prev = null;
    InsnList newInstrucionList = new InsnList();
    while (i.hasNext()) {
        boolean addInstruction = true;
        AbstractInsnNode i1 = (AbstractInsnNode) i.next();
        if (i1 instanceof MethodInsnNode) {
            MethodInsnNode methotInsnNode = (MethodInsnNode) i1;
            if (methotInsnNode.name.equals("exit") && methotInsnNode.owner.equals("java/lang/System")) {
                logger.info("Replacing System.exit ");
                newInstrucionList.remove(prev);
                // insns.remove(i1);
                InsnList il = new InsnList();
                Label mutationStartLabel = new Label();
                mutationStartLabel.info = new MutationMarker(true);

                il.add(new LabelNode(mutationStartLabel));
                il.add(new TypeInsnNode(Opcodes.NEW, "java/lang/RuntimeException"));
                il.add(new InsnNode(Opcodes.DUP));
                il.add(new LdcInsnNode("Replaced System.exit()"));
                il.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/lang/RuntimeException", "<init>",
                        "(Ljava/lang/String;)V"));
                il.add(new InsnNode(Opcodes.ATHROW));

                Label mutationEndLabel = new Label();
                mutationEndLabel.info = new MutationMarker(false);
                il.add(new LabelNode(mutationEndLabel));
                newInstrucionList.add(il);
                addInstruction = false;
            }
        }
        if (addInstruction) {
            try {
                insns.remove(i1);
                newInstrucionList.add(i1);
            } catch (Exception e) {
                logger.error(e);
            }

        }
        prev = i1;
    }
    mn.instructions = newInstrucionList;
    mn.accept(next);
}

From source file:net.cazzar.corelib.asm.MethodTransformer.java

License:Open Source License

/**
 * Transform the insn list for use in a obf environment
 *
 * @param insnList the instructions to fiddle with
 *
 * @return the new insnList/*from ww w.j a v a 2 s. co  m*/
 */
public InsnList transformInsns(InsnList insnList) {
    if (!CoreMod.getRuntimeDeobfuscationEnabled())
        return insnList;

    InsnList insns = new InsnList();

    for (ListIterator<AbstractInsnNode> i = insnList.iterator(); i.hasNext();) {
        AbstractInsnNode currNode = i.next();
        if (currNode instanceof FieldInsnNode) {
            FieldInsnNode node = (FieldInsnNode) currNode;

            node.name = getMapping(node.name);
        } else if (currNode instanceof MethodInsnNode) {
            MethodInsnNode node = (MethodInsnNode) currNode;

            String mtd = getMapping(node.name);
            node.name = getMapping(node.name);
        }

        insns.add(currNode);
    }

    return insns;
}

From source file:org.brutusin.instrumentation.Instrumentator.java

License:Apache License

private void addTraceReturn() {

    InsnList il = this.mn.instructions;

    Iterator<AbstractInsnNode> it = il.iterator();
    while (it.hasNext()) {
        AbstractInsnNode abstractInsnNode = it.next();

        switch (abstractInsnNode.getOpcode()) {
        case Opcodes.RETURN:
            il.insertBefore(abstractInsnNode, getVoidReturnTraceInstructions());
            break;
        case Opcodes.IRETURN:
        case Opcodes.LRETURN:
        case Opcodes.FRETURN:
        case Opcodes.ARETURN:
        case Opcodes.DRETURN:
            il.insertBefore(abstractInsnNode, getReturnTraceInstructions());
        }//from   ww w  .  ja  va2s .  c om
    }
}