Search in sources :

Example 61 with LabelNode

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

the class MutationInstrumentation method addInstrumentation.

/**
 * <p>
 * addInstrumentation
 * </p>
 *
 * @param mn
 *            a {@link org.objectweb.asm.tree.MethodNode} object.
 * @param original
 *            a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 * @param mutations
 *            a {@link java.util.List} object.
 */
protected void addInstrumentation(MethodNode mn, AbstractInsnNode original, List<Mutation> mutations) {
    InsnList instructions = new InsnList();
    // TODO: All mutations in the id are touched, not just one!
    for (Mutation mutation : mutations) {
        instructions.add(mutation.getInfectionDistance());
        instructions.add(new LdcInsnNode(mutation.getId()));
        MethodInsnNode touched = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(ExecutionTracer.class), "passedMutation", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.DOUBLE_TYPE, Type.INT_TYPE }), false);
        instructions.add(touched);
    }
    LabelNode endLabel = new LabelNode();
    for (Mutation mutation : mutations) {
        LabelNode nextLabel = new LabelNode();
        LdcInsnNode mutationId = new LdcInsnNode(mutation.getId());
        instructions.add(mutationId);
        FieldInsnNode activeId = new FieldInsnNode(Opcodes.GETSTATIC, Type.getInternalName(MutationObserver.class), "activeMutation", "I");
        instructions.add(activeId);
        instructions.add(new JumpInsnNode(Opcodes.IF_ICMPNE, nextLabel));
        instructions.add(mutation.getMutation());
        instructions.add(new JumpInsnNode(Opcodes.GOTO, endLabel));
        instructions.add(nextLabel);
    }
    mn.instructions.insertBefore(original, instructions);
    mn.instructions.insert(original, endLabel);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) Type(org.objectweb.asm.Type) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) MutationObserver(org.evosuite.coverage.mutation.MutationObserver) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) Mutation(org.evosuite.coverage.mutation.Mutation) InsnList(org.objectweb.asm.tree.InsnList) ExecutionTracer(org.evosuite.testcase.execution.ExecutionTracer)

Example 62 with LabelNode

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

the class MutationInstrumentation method analyze.

/* (non-Javadoc)
	 * @see org.evosuite.cfg.instrumentation.MethodInstrumentation#analyze(org.objectweb.asm.tree.MethodNode, java.lang.String, java.lang.String, int)
	 */
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {
    if (methodName.startsWith("<clinit>"))
        return;
    if (methodName.startsWith(ClassResetter.STATIC_RESET))
        return;
    RawControlFlowGraph graph = GraphPool.getInstance(classLoader).getRawCFG(className, methodName);
    Iterator<AbstractInsnNode> j = mn.instructions.iterator();
    getFrames(mn, className);
    boolean constructorInvoked = false;
    if (!methodName.startsWith("<init>"))
        constructorInvoked = true;
    logger.info("Applying mutation operators ");
    int frameIndex = 0;
    int numMutants = 0;
    if (frames.length != mn.instructions.size()) {
        logger.error("Number of frames does not match number number of bytecode instructions: " + frames.length + "/" + mn.instructions.size());
        logger.error("Skipping mutation of method " + className + "." + methodName);
        return;
    }
    // + " vs " + mn.instructions.size();
    while (j.hasNext()) {
        Frame currentFrame = frames[frameIndex++];
        AbstractInsnNode in = j.next();
        if (!constructorInvoked) {
            if (in.getOpcode() == Opcodes.INVOKESPECIAL) {
                if (className.matches(".*\\$\\d+$")) {
                    // so best not mutate the constructor
                    continue;
                }
                MethodInsnNode cn = (MethodInsnNode) in;
                Set<String> superClasses = new HashSet<String>();
                if (DependencyAnalysis.getInheritanceTree() != null && DependencyAnalysis.getInheritanceTree().hasClass(className))
                    superClasses.addAll(DependencyAnalysis.getInheritanceTree().getSuperclasses(className));
                superClasses.add(className);
                String classNameWithDots = ResourceList.getClassNameFromResourcePath(cn.owner);
                if (superClasses.contains(classNameWithDots)) {
                    constructorInvoked = true;
                }
            } else {
                continue;
            }
        }
        boolean inInstrumentation = false;
        for (BytecodeInstruction v : graph.vertexSet()) {
            // If the bytecode is instrumented by EvoSuite, then don't mutate
            if (v.isLabel()) {
                LabelNode labelNode = (LabelNode) v.getASMNode();
                if (labelNode.getLabel() instanceof AnnotatedLabel) {
                    AnnotatedLabel aLabel = (AnnotatedLabel) labelNode.getLabel();
                    if (aLabel.isStartTag()) {
                        inInstrumentation = true;
                    } else {
                        inInstrumentation = false;
                    }
                }
            }
            if (inInstrumentation) {
                continue;
            }
            // If this is in the CFG
            if (in.equals(v.getASMNode())) {
                logger.info(v.toString());
                List<Mutation> mutations = new LinkedList<Mutation>();
                // TODO: More than one mutation operator might apply to the same instruction
                for (MutationOperator mutationOperator : mutationOperators) {
                    if (numMutants++ > Properties.MAX_MUTANTS_PER_METHOD) {
                        logger.info("Reached maximum number of mutants per method");
                        break;
                    }
                    // logger.info("Checking mutation operator on instruction " + v);
                    if (mutationOperator.isApplicable(v)) {
                        logger.info("Applying mutation operator " + mutationOperator.getClass().getSimpleName());
                        mutations.addAll(mutationOperator.apply(mn, className, methodName, v, currentFrame));
                    }
                }
                if (!mutations.isEmpty()) {
                    logger.info("Adding instrumentation for mutation");
                    // InsnList instrumentation = getInstrumentation(in, mutations);
                    addInstrumentation(mn, in, mutations);
                }
            }
            if (numMutants > Properties.MAX_MUTANTS_PER_METHOD) {
                break;
            }
        }
    }
    j = mn.instructions.iterator();
    logger.info("Result of mutation: ");
    while (j.hasNext()) {
        AbstractInsnNode in = j.next();
        logger.info(new BytecodeInstruction(classLoader, className, methodName, 0, 0, in).toString());
    }
    logger.info("Done.");
// mn.maxStack += 3;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) Frame(org.objectweb.asm.tree.analysis.Frame) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) LinkedList(java.util.LinkedList) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph) MutationOperator(org.evosuite.instrumentation.mutation.MutationOperator) AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) Mutation(org.evosuite.coverage.mutation.Mutation) HashSet(java.util.HashSet)

Example 63 with LabelNode

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

the class MethodNodeTransformer method transform.

/**
 * <p>transform</p>
 *
 * @param mn a {@link org.objectweb.asm.tree.MethodNode} object.
 */
public void transform(MethodNode mn) {
    setupLocals(mn);
    Set<AbstractInsnNode> originalNodes = new HashSet<AbstractInsnNode>();
    AbstractInsnNode node = mn.instructions.getFirst();
    while (node != mn.instructions.getLast()) {
        originalNodes.add(node);
        node = node.getNext();
    }
    // int currentIndex = 0;
    node = mn.instructions.getFirst();
    // while (currentIndex < mn.instructions.size()) {
    boolean finished = false;
    while (!finished) {
        // } else
        if (node instanceof MethodInsnNode) {
            node = transformMethodInsnNode(mn, (MethodInsnNode) node);
        } else if (node instanceof VarInsnNode) {
            node = transformVarInsnNode(mn, (VarInsnNode) node);
        } else if (node instanceof FieldInsnNode) {
            node = transformFieldInsnNode(mn, (FieldInsnNode) node);
        } else if (node instanceof InsnNode) {
            node = transformInsnNode(mn, (InsnNode) node);
        } else if (node instanceof TypeInsnNode) {
            node = transformTypeInsnNode(mn, (TypeInsnNode) node);
        } else if (node instanceof JumpInsnNode) {
            node = transformJumpInsnNode(mn, (JumpInsnNode) node);
        } else if (node instanceof LabelNode) {
            node = transformLabelNode(mn, (LabelNode) node);
        } else if (node instanceof IntInsnNode) {
            node = transformIntInsnNode(mn, (IntInsnNode) node);
        } else if (node instanceof MultiANewArrayInsnNode) {
            node = transformMultiANewArrayInsnNode(mn, (MultiANewArrayInsnNode) node);
        }
        if (node == mn.instructions.getLast()) {
            finished = true;
        } else {
            node = node.getNext();
        }
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) IntInsnNode(org.objectweb.asm.tree.IntInsnNode) MultiANewArrayInsnNode(org.objectweb.asm.tree.MultiANewArrayInsnNode) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) HashSet(java.util.HashSet)

Example 64 with LabelNode

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

the class BooleanTestabilityTransformation method insertBranchIdPlaceholder.

private void insertBranchIdPlaceholder(MethodNode mn, JumpInsnNode jumpNode) {
    Label label = new Label();
    LabelNode labelNode = new LabelNode(label);
    // BooleanTestabilityPlaceholderTransformer.addBranchPlaceholder(label, jumpNode);
    mn.instructions.insertBefore(jumpNode, labelNode);
    // mn.instructions.insertBefore(jumpNode, new LdcInsnNode(0));
    mn.instructions.insertBefore(jumpNode, new LdcInsnNode(getBranchID(mn, jumpNode)));
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) Label(org.objectweb.asm.Label)

Example 65 with LabelNode

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

the class BranchInstrumentation method addInstrumentationForDefaultLookupswitchCase.

/**
 * <p>
 * addInstrumentationForDefaultLookupswitchCase
 * </p>
 *
 * @param v
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 * @param instrumentation
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 */
protected void addInstrumentationForDefaultLookupswitchCase(BytecodeInstruction v, InsnList instrumentation) {
    if (!v.isLookupSwitch())
        throw new IllegalArgumentException("lookup switch expected");
    // setup instructions
    LookupSwitchInsnNode toInstrument = (LookupSwitchInsnNode) v.getASMNode();
    LabelNode caseLabel = new LabelNode();
    LabelNode defaultLabel = new LabelNode();
    LabelNode endLabel = new LabelNode();
    int keySize = toInstrument.keys.size();
    int[] keys = new int[keySize];
    LabelNode[] labels = new LabelNode[keySize];
    for (int i = 0; i < keySize; i++) {
        keys[i] = (Integer) toInstrument.keys.get(i);
        labels[i] = caseLabel;
    }
    LookupSwitchInsnNode myLookup = new LookupSwitchInsnNode(defaultLabel, keys, labels);
    addDefaultCaseInstrumentation(v, instrumentation, myLookup, defaultLabel, caseLabel, endLabel);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) LookupSwitchInsnNode(org.objectweb.asm.tree.LookupSwitchInsnNode)

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