Search in sources :

Example 76 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project evosuite by EvoSuite.

the class LCSAJsInstrumentation method analyze.

/* (non-Javadoc)
	 * @see org.evosuite.cfg.MethodInstrumentation#analyze(org.objectweb.asm.tree.MethodNode, org.jgrapht.Graph, java.lang.String, java.lang.String)
	 */
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
// using external lib
@Override
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {
    Queue<LCSAJ> lcsaj_queue = new LinkedList<LCSAJ>();
    HashSet<Integer> targets_reached = new HashSet<Integer>();
    AbstractInsnNode start = mn.instructions.getFirst();
    int startID = 0;
    // TODO: This should replace the hack below
    if (methodName.startsWith("<init>")) {
        Iterator<AbstractInsnNode> j = mn.instructions.iterator();
        boolean constructorInvoked = false;
        while (j.hasNext()) {
            AbstractInsnNode in = j.next();
            startID++;
            if (!constructorInvoked) {
                if (in.getOpcode() == Opcodes.INVOKESPECIAL) {
                    MethodInsnNode cn = (MethodInsnNode) in;
                    Collection<String> superClasses = DependencyAnalysis.getInheritanceTree().getSuperclasses(className);
                    superClasses.add(className);
                    String classNameWithDots = ResourceList.getClassNameFromResourcePath(cn.owner);
                    if (superClasses.contains(classNameWithDots)) {
                        constructorInvoked = true;
                        break;
                    }
                } else {
                    continue;
                }
            }
        }
    }
    /*
		if (methodName.startsWith("<init>")) {
			if (mn.instructions.size() >= 4) {
				start = mn.instructions.get(4);
				startID = 4;
			}
		}
		*/
    LCSAJ a = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, startID, start));
    lcsaj_queue.add(a);
    targets_reached.add(0);
    ArrayList<TryCatchBlockNode> tc_blocks = (ArrayList<TryCatchBlockNode>) mn.tryCatchBlocks;
    for (TryCatchBlockNode t : tc_blocks) {
        LCSAJ b = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, mn.instructions.indexOf(t.handler), t.handler));
        lcsaj_queue.add(b);
    }
    while (!lcsaj_queue.isEmpty()) {
        LCSAJ currentLCSAJ = lcsaj_queue.poll();
        int position = mn.instructions.indexOf(currentLCSAJ.getLastNodeAccessed());
        // go to next bytecode instruction
        position++;
        if (position >= mn.instructions.size()) {
            // New LCSAJ for current + return
            LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            continue;
        }
        AbstractInsnNode next = mn.instructions.get(position);
        currentLCSAJ.lookupInstruction(position, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, position, next));
        if (next instanceof JumpInsnNode) {
            JumpInsnNode jump = (JumpInsnNode) next;
            // New LCSAJ for current + jump to target
            LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            LabelNode target = jump.label;
            int targetPosition = mn.instructions.indexOf(target);
            if (jump.getOpcode() != Opcodes.GOTO) {
                LCSAJ copy = new LCSAJ(currentLCSAJ);
                lcsaj_queue.add(copy);
            }
            if (!targets_reached.contains(targetPosition)) {
                LCSAJ c = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, targetPosition, target));
                lcsaj_queue.add(c);
                targets_reached.add(targetPosition);
            }
        } else if (next instanceof TableSwitchInsnNode) {
            TableSwitchInsnNode tswitch = (TableSwitchInsnNode) next;
            List<LabelNode> allTargets = tswitch.labels;
            for (LabelNode target : allTargets) {
                int targetPosition = mn.instructions.indexOf(target);
                if (!targets_reached.contains(targetPosition)) {
                    LCSAJ b = new LCSAJ(className, methodName, BytecodeInstructionPool.getInstance(classLoader).getInstruction(className, methodName, targetPosition, target));
                    lcsaj_queue.add(b);
                    targets_reached.add(targetPosition);
                }
            }
        } else if (next instanceof InsnNode) {
            InsnNode insn = (InsnNode) next;
            // New LCSAJ for current + throw / return
            if (insn.getOpcode() == Opcodes.ATHROW || insn.getOpcode() == Opcodes.RETURN || insn.getOpcode() == Opcodes.ARETURN || insn.getOpcode() == Opcodes.IRETURN || insn.getOpcode() == Opcodes.DRETURN || insn.getOpcode() == Opcodes.LRETURN || insn.getOpcode() == Opcodes.FRETURN) {
                LCSAJPool.add_lcsaj(className, methodName, currentLCSAJ);
            } else
                lcsaj_queue.add(currentLCSAJ);
        } else
            lcsaj_queue.add(currentLCSAJ);
    }
    if (Properties.STRATEGY != Strategy.EVOSUITE)
        addInstrumentation(classLoader, mn, className, methodName);
// if (Properties.WRITE_CFG)
// for (LCSAJ l : LCSAJPool.getLCSAJs(className, methodName)) {
// LCSAJGraph graph = new LCSAJGraph(l, false);
// String graphDestination = "evosuite-graphs/LCSAJGraphs/" + className
// + "/" + methodName;
// File dir = new File(graphDestination);
// if (dir.mkdirs())
// graph.generate(new File(graphDestination + "/LCSAJGraph no: "
// + l.getID() + ".dot"));
// else if (dir.exists())
// graph.generate(new File(graphDestination + "/LCSAJGraph no: "
// + l.getID() + ".dot"));
// }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) TryCatchBlockNode(org.objectweb.asm.tree.TryCatchBlockNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) ArrayList(java.util.ArrayList) LCSAJ(org.evosuite.coverage.lcsaj.LCSAJ) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) LinkedList(java.util.LinkedList) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) InsnList(org.objectweb.asm.tree.InsnList) List(java.util.List) ResourceList(org.evosuite.classpath.ResourceList) HashSet(java.util.HashSet)

Example 77 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project evosuite by EvoSuite.

the class AnnotatedMethodNode method getLabelNode.

/**
 * {@inheritDoc}
 *
 * Returns the LabelNode corresponding to the given Label. Creates a new
 * LabelNode if necessary. The default implementation of this method uses
 * the {@link Label#info} field to store associations between labels and
 * label nodes.
 */
@Override
protected LabelNode getLabelNode(final Label l) {
    if (l instanceof AnnotatedLabel) {
        AnnotatedLabel al = (AnnotatedLabel) l;
        al.setParent(new LabelNode(al));
        return al.getParent();
    } else {
        if (!(l.info instanceof LabelNode)) {
            l.info = new LabelNode(l);
        }
        return (LabelNode) l.info;
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode)

Example 78 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project Random-Things by lumien231.

the class ClassTransformer method patchWorldClass.

private byte[] patchWorldClass(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found World Class: " + classNode.name);
    MethodNode getRedstonePower = null;
    MethodNode getStrongPower = null;
    MethodNode isRainingAt = null;
    MethodNode canSnowAt = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_175651_c"))) {
            getRedstonePower = mn;
        } else if (mn.name.equals(MCPNames.method("func_175627_a")) && mn.desc.equals("(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;)I")) {
            getStrongPower = mn;
        } else if (mn.name.equals(MCPNames.method("func_175727_C"))) {
            isRainingAt = mn;
        } else if (mn.name.equals(MCPNames.method("func_175708_f"))) {
            canSnowAt = mn;
        }
    }
    if (getRedstonePower != null) {
        logger.log(Level.DEBUG, "- Found getRedstonePower (1/4)");
        InsnList toInsert = new InsnList();
        LabelNode l1 = new LabelNode(new Label());
        toInsert.add(new VarInsnNode(ALOAD, 0));
        toInsert.add(new VarInsnNode(ALOAD, 1));
        toInsert.add(new VarInsnNode(ALOAD, 2));
        toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "getRedstonePower", "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;)I", false));
        toInsert.add(new InsnNode(DUP));
        toInsert.add(new JumpInsnNode(IFEQ, l1));
        toInsert.add(new InsnNode(IRETURN));
        toInsert.add(l1);
        toInsert.add(new InsnNode(POP));
        getRedstonePower.instructions.insert(toInsert);
    }
    if (getStrongPower != null) {
        logger.log(Level.DEBUG, "- Found getStrongPower (2/4)");
        InsnList toInsert = new InsnList();
        LabelNode l1 = new LabelNode(new Label());
        toInsert.add(new VarInsnNode(ALOAD, 0));
        toInsert.add(new VarInsnNode(ALOAD, 1));
        toInsert.add(new VarInsnNode(ALOAD, 2));
        toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "getStrongPower", "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/util/EnumFacing;)I", false));
        toInsert.add(new InsnNode(DUP));
        toInsert.add(new JumpInsnNode(IFEQ, l1));
        toInsert.add(new InsnNode(IRETURN));
        toInsert.add(l1);
        toInsert.add(new InsnNode(POP));
        getStrongPower.instructions.insert(toInsert);
    }
    if (isRainingAt != null) {
        logger.log(Level.DEBUG, "- Found isRainingAt (3/4)");
        AbstractInsnNode returnNode = null;
        for (int i = 0; i < isRainingAt.instructions.size(); i++) {
            AbstractInsnNode ain = isRainingAt.instructions.get(i);
            if (ain.getOpcode() == Opcodes.IRETURN) {
                returnNode = ain;
            }
        }
        InsnList toInsert = new InsnList();
        LabelNode returnLabel = new LabelNode(new Label());
        toInsert.add(new InsnNode(Opcodes.DUP));
        toInsert.add(new JumpInsnNode(IFEQ, returnLabel));
        toInsert.add(new InsnNode(POP));
        toInsert.add(new VarInsnNode(ALOAD, 0));
        toInsert.add(new VarInsnNode(ALOAD, 1));
        toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "shouldRain", "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)Z", false));
        toInsert.add(returnLabel);
        isRainingAt.instructions.insertBefore(returnNode, toInsert);
    }
    if (canSnowAt != null) {
        logger.log(Level.DEBUG, "- Found canSnowAt (4/4)");
        AbstractInsnNode returnNode = null;
        for (int i = 0; i < canSnowAt.instructions.size(); i++) {
            AbstractInsnNode ain = canSnowAt.instructions.get(i);
            if (ain.getOpcode() == Opcodes.IRETURN) {
                returnNode = ain;
            }
        }
        InsnList toInsert = new InsnList();
        LabelNode returnLabel = new LabelNode(new Label());
        toInsert.add(new InsnNode(Opcodes.DUP));
        toInsert.add(new JumpInsnNode(IFEQ, returnLabel));
        toInsert.add(new InsnNode(POP));
        toInsert.add(new VarInsnNode(ALOAD, 0));
        toInsert.add(new VarInsnNode(ALOAD, 1));
        toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "canSnowAt", "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)Z", false));
        toInsert.add(returnLabel);
        canSnowAt.instructions.insertBefore(returnNode, toInsert);
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) Label(org.objectweb.asm.Label) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 79 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project Random-Things by lumien231.

the class ClassTransformer method patchBlockRendererDispatcher.

private byte[] patchBlockRendererDispatcher(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found BlockRendererDispatcher Class: " + classNode.name);
    MethodNode renderBlock = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_175018_a"))) {
            renderBlock = mn;
        }
    }
    if (renderBlock != null) {
        logger.log(Level.DEBUG, "- Found renderBlock (1/1)");
        InsnList toInsert = new InsnList();
        LabelNode l1 = new LabelNode(new Label());
        toInsert.add(new VarInsnNode(ALOAD, 0));
        toInsert.add(new VarInsnNode(ALOAD, 1));
        toInsert.add(new VarInsnNode(ALOAD, 2));
        toInsert.add(new VarInsnNode(ALOAD, 3));
        toInsert.add(new VarInsnNode(ALOAD, 4));
        toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "renderBlock", "(Lnet/minecraft/client/renderer/BlockRendererDispatcher;Lnet/minecraft/block/state/IBlockState;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/world/IBlockAccess;Lnet/minecraft/client/renderer/BufferBuilder;)I", false));
        toInsert.add(new InsnNode(DUP));
        toInsert.add(new InsnNode(ICONST_2));
        toInsert.add(new JumpInsnNode(IF_ICMPEQ, l1));
        toInsert.add(new InsnNode(IRETURN));
        toInsert.add(l1);
        toInsert.add(new InsnNode(POP));
        renderBlock.instructions.insert(toInsert);
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) Label(org.objectweb.asm.Label) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnList(org.objectweb.asm.tree.InsnList) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 80 with LabelNode

use of org.objectweb.asm.tree.LabelNode in project Random-Things by lumien231.

the class ClassTransformer method patchEntityRenderer.

private byte[] patchEntityRenderer(byte[] basicClass) {
    ClassNode classNode = new ClassNode();
    ClassReader classReader = new ClassReader(basicClass);
    classReader.accept(classNode, 0);
    logger.log(Level.DEBUG, "Found EntityRenderer Class: " + classNode.name);
    MethodNode renderRainSnow = null;
    MethodNode addRainParticles = null;
    for (MethodNode mn : classNode.methods) {
        if (mn.name.equals(MCPNames.method("func_78474_d"))) {
            renderRainSnow = mn;
        } else if (mn.name.equals(MCPNames.method("func_78484_h"))) {
            addRainParticles = mn;
        }
    }
    if (renderRainSnow != null) {
        logger.log(Level.DEBUG, "- Found renderRainSnow");
        VarInsnNode insnPoint = null;
        for (int i = 0; i < renderRainSnow.instructions.size(); i++) {
            AbstractInsnNode ain = renderRainSnow.instructions.get(i);
            if (ain instanceof MethodInsnNode) {
                MethodInsnNode min = (MethodInsnNode) ain;
                if (min.name.equals(MCPNames.method("func_76738_d"))) {
                    logger.log(Level.DEBUG, "- Found canRain");
                    insnPoint = (VarInsnNode) renderRainSnow.instructions.get(i - 1);
                }
                if (min.name.equals(MCPNames.method("func_76746_c"))) {
                    logger.log(Level.DEBUG, "- Found getEnableSnow");
                    int jumpCounter = i + 1;
                    int worldIndex = 5;
                    int blockPosIndex = 21;
                    // Optifine Why :'(
                    for (LocalVariableNode lv : renderRainSnow.localVariables) {
                        if (lv.desc.equals("Lnet/minecraft/client/multiplayer/WorldClient;") || lv.desc.equals("Lnet/minecraft/world/World;")) {
                            worldIndex = lv.index;
                        } else if (lv.desc.equals("Lnet/minecraft/util/math/BlockPos$MutableBlockPos;")) {
                            blockPosIndex = lv.index;
                        }
                    }
                    AbstractInsnNode jumpNode;
                    while (!((jumpNode = renderRainSnow.instructions.get(jumpCounter)) instanceof JumpInsnNode)) {
                        jumpCounter++;
                    }
                    JumpInsnNode jin = (JumpInsnNode) jumpNode;
                    LabelNode labelNode = jin.label;
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(ALOAD, worldIndex));
                    toInsert.add(new VarInsnNode(ALOAD, blockPosIndex));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "shouldRain", "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)Z", false));
                    toInsert.add(new JumpInsnNode(IFEQ, labelNode));
                    renderRainSnow.instructions.insertBefore(insnPoint, toInsert);
                    i += 4;
                }
            }
        }
    }
    if (addRainParticles != null) {
        logger.log(Level.DEBUG, "- Found addRainParticles");
        for (int i = 0; i < addRainParticles.instructions.size(); i++) {
            AbstractInsnNode ain = addRainParticles.instructions.get(i);
            if (ain instanceof JumpInsnNode) {
                JumpInsnNode jin = (JumpInsnNode) ain;
                if (jin.getOpcode() == Opcodes.IF_ICMPGT) {
                    LabelNode jumpTarget = jin.label;
                    InsnList toInsert = new InsnList();
                    toInsert.add(new VarInsnNode(ALOAD, 3));
                    toInsert.add(new VarInsnNode(ALOAD, 15));
                    toInsert.add(new MethodInsnNode(INVOKESTATIC, asmHandler, "shouldRain", "(Lnet/minecraft/world/World;Lnet/minecraft/util/math/BlockPos;)Z", false));
                    toInsert.add(new JumpInsnNode(IFEQ, jumpTarget));
                    addRainParticles.instructions.insert(jin, toInsert);
                    break;
                }
            }
        }
    }
    CustomClassWriter writer = new CustomClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
    classNode.accept(writer);
    return writer.toByteArray();
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) ClassNode(org.objectweb.asm.tree.ClassNode) MethodNode(org.objectweb.asm.tree.MethodNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) ClassReader(org.objectweb.asm.ClassReader) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) InsnList(org.objectweb.asm.tree.InsnList) LocalVariableNode(org.objectweb.asm.tree.LocalVariableNode)

Aggregations

LabelNode (org.objectweb.asm.tree.LabelNode)89 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)37 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)32 Label (org.objectweb.asm.Label)28 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)23 InsnList (org.objectweb.asm.tree.InsnList)22 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)21 InsnNode (org.objectweb.asm.tree.InsnNode)20 MethodNode (org.objectweb.asm.tree.MethodNode)19 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)17 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)16 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)15 ClassNode (org.objectweb.asm.tree.ClassNode)14 ClassReader (org.objectweb.asm.ClassReader)12 LineNumberNode (org.objectweb.asm.tree.LineNumberNode)10 Type (org.objectweb.asm.Type)8 LocalVariableNode (org.objectweb.asm.tree.LocalVariableNode)8 LinkedList (java.util.LinkedList)6 ArrayList (java.util.ArrayList)5 LookupSwitchInsnNode (org.objectweb.asm.tree.LookupSwitchInsnNode)5