Search in sources :

Example 1 with AnnotatedLabel

use of org.evosuite.runtime.instrumentation.AnnotatedLabel in project evosuite by EvoSuite.

the class BranchInstrumentation method analyze.

/*
	 * (non-Javadoc)
	 *
	 * @see
	 * org.evosuite.cfg.MethodInstrumentation#analyze(org.objectweb
	 * .asm.tree.MethodNode, org.jgrapht.Graph, java.lang.String,
	 * java.lang.String, int)
	 */
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {
    this.classLoader = classLoader;
    RawControlFlowGraph graph = GraphPool.getInstance(classLoader).getRawCFG(className, methodName);
    Iterator<AbstractInsnNode> j = mn.instructions.iterator();
    while (j.hasNext()) {
        AbstractInsnNode in = j.next();
        for (BytecodeInstruction v : graph.vertexSet()) {
            // If this is in the CFG and it's a branch...
            if (in.equals(v.getASMNode())) {
                if (v.isBranch()) {
                    if (in.getPrevious() instanceof LabelNode) {
                        LabelNode label = (LabelNode) in.getPrevious();
                        if (label.getLabel() instanceof AnnotatedLabel) {
                            AnnotatedLabel aLabel = (AnnotatedLabel) label.getLabel();
                            if (aLabel.isStartTag()) {
                                if (!aLabel.shouldIgnore()) {
                                    logger.debug("Found artificial branch: " + v);
                                    Branch b = BranchPool.getInstance(classLoader).getBranchForInstruction(v);
                                    b.setInstrumented(true);
                                } else {
                                    continue;
                                }
                            }
                        }
                    }
                    mn.instructions.insertBefore(v.getASMNode(), getInstrumentation(v));
                } else if (v.isSwitch()) {
                    mn.instructions.insertBefore(v.getASMNode(), getSwitchInstrumentation(v, mn, className, methodName));
                }
            }
        }
    }
    mn.maxStack += 4;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel) Branch(org.evosuite.coverage.branch.Branch) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph)

Example 2 with AnnotatedLabel

use of org.evosuite.runtime.instrumentation.AnnotatedLabel in project evosuite by EvoSuite.

the class ExceptionTransformationMethodAdapter method tagBranch.

public void tagBranch() {
    Label dummyTag = new AnnotatedLabel(false, true);
    // dummyTag.info = Boolean.TRUE;
    super.visitLabel(dummyTag);
}
Also used : AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel) Label(org.objectweb.asm.Label) AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel)

Example 3 with AnnotatedLabel

use of org.evosuite.runtime.instrumentation.AnnotatedLabel in project evosuite by EvoSuite.

the class BytecodeInstructionPool method registerInstruction.

/**
 * <p>
 * registerInstruction
 * </p>
 *
 * @param instruction
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 */
public void registerInstruction(BytecodeInstruction instruction) {
    String className = instruction.getClassName();
    String methodName = instruction.getMethodName();
    if (!instructionMap.containsKey(className))
        instructionMap.put(className, new LinkedHashMap<>());
    if (!instructionMap.get(className).containsKey(methodName))
        instructionMap.get(className).put(methodName, new ArrayList<>());
    instructionMap.get(className).get(methodName).add(instruction);
    logger.debug("Registering instruction " + instruction);
    List<BytecodeInstruction> instructions = instructionMap.get(className).get(methodName);
    if (instructions.size() > 1) {
        BytecodeInstruction previous = instructions.get(instructions.size() - 2);
        if (previous.isLabel()) {
            LabelNode ln = (LabelNode) previous.asmNode;
            if (ln.getLabel() instanceof AnnotatedLabel) {
                AnnotatedLabel aLabel = (AnnotatedLabel) ln.getLabel();
                if (aLabel.isStartTag()) {
                    if (aLabel.shouldIgnore()) {
                        logger.debug("Ignoring artificial branch: " + instruction);
                        return;
                    }
                }
            }
        }
    }
    if (instruction.isActualBranch()) {
        BranchPool.getInstance(classLoader).registerAsBranch(instruction);
    }
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel)

Example 4 with AnnotatedLabel

use of org.evosuite.runtime.instrumentation.AnnotatedLabel in project evosuite by EvoSuite.

the class ErrorConditionMethodAdapter method tagBranchExit.

public void tagBranchExit() {
    Label dummyTag = new AnnotatedLabel(false, false);
    // dummyTag.info = Boolean.FALSE;
    super.visitLabel(dummyTag);
}
Also used : AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel) Label(org.objectweb.asm.Label) AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel)

Example 5 with AnnotatedLabel

use of org.evosuite.runtime.instrumentation.AnnotatedLabel 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)

Aggregations

AnnotatedLabel (org.evosuite.runtime.instrumentation.AnnotatedLabel)7 Label (org.objectweb.asm.Label)4 LabelNode (org.objectweb.asm.tree.LabelNode)3 BytecodeInstruction (org.evosuite.graphs.cfg.BytecodeInstruction)2 RawControlFlowGraph (org.evosuite.graphs.cfg.RawControlFlowGraph)2 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Branch (org.evosuite.coverage.branch.Branch)1 Mutation (org.evosuite.coverage.mutation.Mutation)1 MutationOperator (org.evosuite.instrumentation.mutation.MutationOperator)1 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)1 Frame (org.objectweb.asm.tree.analysis.Frame)1