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;
}
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;
}
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()));
}
}
}
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);
}
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;
}
Aggregations