Search in sources :

Example 1 with RawControlFlowGraph

use of org.evosuite.graphs.cfg.RawControlFlowGraph in project evosuite by EvoSuite.

the class DefUseCoverageTestFitness method getInstructionsAfterGoalDefinition.

/**
 * Returns a set containing all CFGVertices in the goal definition method
 * that come after the definition.
 *
 * Look at ControlFlowGraph.getLaterInstructionInMethod() for details
 *
 * @return a {@link java.util.Set} object.
 */
public Set<BytecodeInstruction> getInstructionsAfterGoalDefinition() {
    RawControlFlowGraph cfg = GraphPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getRawCFG(goalDefinition.getClassName(), goalDefinition.getMethodName());
    BytecodeInstruction defVertex = cfg.getInstruction(goalDefinition.getInstructionId());
    Set<BytecodeInstruction> r = cfg.getLaterInstructionsInMethod(defVertex);
    // }
    return r;
}
Also used : BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph)

Example 2 with RawControlFlowGraph

use of org.evosuite.graphs.cfg.RawControlFlowGraph 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 3 with RawControlFlowGraph

use of org.evosuite.graphs.cfg.RawControlFlowGraph in project evosuite by EvoSuite.

the class ClassControlFlowGraph method connectPublicMethodsToFrame.

/**
 * Adds a CCFGFrameEdge from the CCFGFrameNode CALL to the
 * CCFGMethodEntryNode of each public method and from their
 * CCFGMethodExitNode to the CCFGFrameNode RETURN.
 */
private void connectPublicMethodsToFrame() {
    for (ClassCallNode ccgNode : ccg.vertexSet()) {
        RawControlFlowGraph cfg = getRCFG(ccgNode);
        if (cfg.isPublicMethod()) {
            addEdge(getFrameNode(FrameNodeType.CALL), methodEntries.get(ccgNode.getMethod()), new CCFGFrameEdge());
            addEdge(methodExits.get(ccgNode.getMethod()), getFrameNode(FrameNodeType.RETURN), new CCFGFrameEdge());
            publicMethods.add(methodEntries.get(ccgNode.getMethod()));
        }
    }
}
Also used : ClassCallNode(org.evosuite.graphs.ccg.ClassCallNode) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph)

Example 4 with RawControlFlowGraph

use of org.evosuite.graphs.cfg.RawControlFlowGraph in project evosuite by EvoSuite.

the class ClassControlFlowGraph method importCFGs.

private void importCFGs() {
    Map<RawControlFlowGraph, Map<BytecodeInstruction, CCFGCodeNode>> tempMap = new HashMap<RawControlFlowGraph, Map<BytecodeInstruction, CCFGCodeNode>>();
    // replace each class call node with corresponding CFG
    for (ClassCallNode ccgNode : ccg.vertexSet()) {
        RawControlFlowGraph cfg = getRCFG(ccgNode);
        tempMap.put(cfg, importCFG(cfg));
    }
    connectCFGs(tempMap);
}
Also used : HashMap(java.util.HashMap) ClassCallNode(org.evosuite.graphs.ccg.ClassCallNode) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) HashMap(java.util.HashMap) Map(java.util.Map) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph)

Example 5 with RawControlFlowGraph

use of org.evosuite.graphs.cfg.RawControlFlowGraph 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

RawControlFlowGraph (org.evosuite.graphs.cfg.RawControlFlowGraph)11 BytecodeInstruction (org.evosuite.graphs.cfg.BytecodeInstruction)9 AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)4 LinkedList (java.util.LinkedList)2 ClassCallNode (org.evosuite.graphs.ccg.ClassCallNode)2 AnnotatedLabel (org.evosuite.runtime.instrumentation.AnnotatedLabel)2 InsnList (org.objectweb.asm.tree.InsnList)2 LabelNode (org.objectweb.asm.tree.LabelNode)2 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Branch (org.evosuite.coverage.branch.Branch)1 LineCoverageFactory (org.evosuite.coverage.line.LineCoverageFactory)1 LineCoverageTestFitness (org.evosuite.coverage.line.LineCoverageTestFitness)1 Mutation (org.evosuite.coverage.mutation.Mutation)1 PrimePath (org.evosuite.coverage.path.PrimePath)1