use of org.evosuite.graphs.cfg.RawControlFlowGraph in project evosuite by EvoSuite.
the class DefUseInstrumentation 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")
@Override
public void analyze(ClassLoader classLoader, MethodNode mn, String className, String methodName, int access) {
RawControlFlowGraph completeCFG = GraphPool.getInstance(classLoader).getRawCFG(className, methodName);
logger.info("Applying DefUse instrumentation on CFG with " + completeCFG.vertexCount() + " nodes");
Iterator<AbstractInsnNode> j = mn.instructions.iterator();
while (j.hasNext()) {
AbstractInsnNode in = j.next();
for (BytecodeInstruction v : completeCFG.vertexSet()) {
if ((ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) || ArrayUtil.contains(Properties.CRITERION, Criterion.ALLDEFS)) && in.equals(v.getASMNode()) && v.isDefUse()) {
boolean isValidDU = false;
if (v.isMethodCallOfField()) {
// keep track of field method calls, though we do not
// know
// how to handle them at this point during the analysis
// (need complete CCFGs first)
isValidDU = DefUsePool.addAsFieldMethodCall(v);
} else {
// keep track of uses
if (v.isUse())
isValidDU = DefUsePool.addAsUse(v);
// keep track of definitions
if (v.isDefinition())
isValidDU = DefUsePool.addAsDefinition(v) || isValidDU;
}
if (isValidDU) {
boolean staticContext = v.isStaticDefUse() || ((access & Opcodes.ACC_STATIC) > 0);
// adding instrumentation for defuse-coverage
InsnList instrumentation = getInstrumentation(v, staticContext, className, methodName, mn);
if (instrumentation == null)
throw new IllegalStateException("error instrumenting node " + v.toString());
if (v.isMethodCallOfField())
mn.instructions.insertBefore(v.getASMNode(), instrumentation);
else if (v.isArrayStoreInstruction())
mn.instructions.insertBefore(v.getSourceOfArrayReference().getASMNode(), instrumentation);
else // mn.instructions.insertBefore(v.getSourceOfArrayReference().getASMNode(), instrumentation);
if (v.isUse())
mn.instructions.insert(v.getASMNode(), instrumentation);
else
mn.instructions.insertBefore(v.getASMNode(), instrumentation);
}
}
}
}
}
use of org.evosuite.graphs.cfg.RawControlFlowGraph in project evosuite by EvoSuite.
the class ClassCallGraph method compute.
private void compute() {
Map<String, RawControlFlowGraph> cfgs = GraphPool.getInstance(classLoader).getRawCFGs(className);
if (cfgs == null)
throw new IllegalStateException("did not find CFGs for a class I was supposed to compute the CCG of");
// add nodes
for (String method : cfgs.keySet()) addVertex(new ClassCallNode(method));
// add vertices
for (ClassCallNode methodNode : graph.vertexSet()) {
RawControlFlowGraph rcfg = cfgs.get(methodNode.getMethod());
List<BytecodeInstruction> calls = rcfg.determineMethodCallsToOwnClass();
// System.out.println(calls.size()+" method calls from "+methodNode);
for (BytecodeInstruction call : calls) {
// System.out.println(" to "+call.getCalledMethod()+" in "+call.getCalledMethodsClass());
ClassCallNode calledMethod = getNodeByMethodName(call.getCalledMethod());
if (calledMethod != null) {
ClassCallEdge e = new ClassCallEdge(call);
addEdge(methodNode, calledMethod, e);
}
}
}
}
use of org.evosuite.graphs.cfg.RawControlFlowGraph in project evosuite by EvoSuite.
the class ClassStatisticsPrinter method printClassStatistics.
/**
* Identify all JUnit tests starting with the given name prefix, instrument
* and run tests
*/
public static void printClassStatistics() {
ExecutionTracer.disable();
ExecutionTracer.setCheckCallerThread(false);
try {
DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(ClassPathHandler.getInstance().getClassPathElementsForTargetProject()));
Sandbox.goingToExecuteSUTCode();
TestGenerationContext.getInstance().goingToExecuteSUTCode();
Sandbox.goingToExecuteUnsafeCodeOnSameThread();
// Load SUT without initialising it
Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
if (targetClass != null) {
LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
} else {
LoggingUtils.getEvoLogger().info("* Error while initializing target class, not continuing");
return;
}
int publicMethods = 0;
int nonpublicMethods = 0;
int staticMethods = 0;
int staticFields = 0;
for (Method method : targetClass.getDeclaredMethods()) {
if (method.getName().equals(ClassResetter.STATIC_RESET))
continue;
if (Modifier.isPublic(method.getModifiers())) {
publicMethods++;
} else {
nonpublicMethods++;
}
if (Modifier.isStatic(method.getModifiers())) {
LoggingUtils.getEvoLogger().info("Static: " + method);
staticMethods++;
}
}
for (Constructor<?> constructor : targetClass.getDeclaredConstructors()) {
if (Modifier.isPublic(constructor.getModifiers())) {
publicMethods++;
} else {
nonpublicMethods++;
}
}
for (Field field : targetClass.getDeclaredFields()) {
if (Modifier.isStatic(field.getModifiers())) {
staticFields++;
}
}
LoggingUtils.getEvoLogger().info("* Abstract: " + Modifier.isAbstract(targetClass.getModifiers()));
LoggingUtils.getEvoLogger().info("* Public methods/constructors: " + publicMethods);
LoggingUtils.getEvoLogger().info("* Non-Public methods/constructors: " + nonpublicMethods);
LoggingUtils.getEvoLogger().info("* Static methods: " + staticMethods);
LoggingUtils.getEvoLogger().info("* Inner classes: " + targetClass.getDeclaredClasses().length);
LoggingUtils.getEvoLogger().info("* Total fields: " + targetClass.getDeclaredFields().length);
LoggingUtils.getEvoLogger().info("* Static fields: " + staticFields);
LoggingUtils.getEvoLogger().info("* Type parameters: " + targetClass.getTypeParameters().length);
} catch (Throwable e) {
LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
return;
} finally {
Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
Sandbox.doneWithExecutingSUTCode();
TestGenerationContext.getInstance().doneWithExecutingSUTCode();
}
LoggingUtils.getEvoLogger().info("* Subclasses: " + (TestCluster.getInheritanceTree().getSubclasses(Properties.TARGET_CLASS).size() - 1));
LoggingUtils.getEvoLogger().info("* Superclasses/interfaces: " + (TestCluster.getInheritanceTree().getSuperclasses(Properties.TARGET_CLASS).size() - 1));
LoggingUtils.getEvoLogger().info("* Lines of code: " + LinePool.getNumLines());
LoggingUtils.getEvoLogger().info("* Methods without branches: " + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getNumBranchlessMethods());
LoggingUtils.getEvoLogger().info("* Total branch predicates: " + BranchPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getBranchCounter());
double complexity = 0.0;
int maxComplexity = 0;
for (Entry<String, RawControlFlowGraph> entry : GraphPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getRawCFGs(Properties.TARGET_CLASS).entrySet()) {
int c = entry.getValue().getCyclomaticComplexity();
if (c > maxComplexity)
maxComplexity = c;
complexity += c;
// LoggingUtils.getEvoLogger().info("* Complexity of method "+entry.getKey()+": "+entry.getValue().getCyclomaticComplexity());
}
LoggingUtils.getEvoLogger().info("* Average cyclomatic complexity: " + (complexity / CFGMethodAdapter.getNumMethods(TestGenerationContext.getInstance().getClassLoaderForSUT())));
LoggingUtils.getEvoLogger().info("* Maximum cyclomatic complexity: " + maxComplexity);
StringBuilder allGoals = new StringBuilder();
List<TestFitnessFactory<?>> factories = TestGenerationStrategy.getFitnessFactories();
int numCriterion = 0;
for (TestFitnessFactory<?> factory : factories) {
List<TestFitnessFunction> goals = (List<TestFitnessFunction>) factory.getCoverageGoals();
LoggingUtils.getEvoLogger().info("* Criterion " + Properties.CRITERION[numCriterion++] + ": " + goals.size());
if (Properties.PRINT_GOALS) {
if (factory instanceof LineCoverageFactory) {
Collections.sort(goals, new Comparator<TestFitnessFunction>() {
@Override
public int compare(TestFitnessFunction l1, TestFitnessFunction l2) {
return Integer.compare(((LineCoverageTestFitness) l1).getLine(), ((LineCoverageTestFitness) l2).getLine());
}
});
}
for (TestFitnessFunction goal : goals) {
allGoals.append(goal.toString() + java.lang.System.getProperty("line.separator"));
}
}
}
if (allGoals.length() > 0 && Properties.PRINT_GOALS) {
if (Properties.WRITE_ALL_GOALS_FILE) {
FileIOUtils.writeFile(allGoals.toString(), Properties.ALL_GOALS_FILE);
} else {
LoggingUtils.getEvoLogger().info(allGoals.toString());
}
}
}
use of org.evosuite.graphs.cfg.RawControlFlowGraph in project evosuite by EvoSuite.
the class DefUseCoverageTestFitness method getInstructionsBeforeGoalUse.
/**
* Returns a set containing all CFGVertices in the goal use method that come
* before the goal use.
*
* Look at ControlFlowGraph.getPreviousInstructionInMethod() for details
*
* @return a {@link java.util.Set} object.
*/
public Set<BytecodeInstruction> getInstructionsBeforeGoalUse() {
RawControlFlowGraph cfg = GraphPool.getInstance(TestGenerationContext.getInstance().getClassLoaderForSUT()).getRawCFG(goalUse.getClassName(), goalUse.getMethodName());
BytecodeInstruction useVertex = cfg.getInstruction(goalUse.getInstructionId());
Set<BytecodeInstruction> r = cfg.getPreviousInstructionsInMethod(useVertex);
// }
return r;
}
use of org.evosuite.graphs.cfg.RawControlFlowGraph in project evosuite by EvoSuite.
the class LCSAJsInstrumentation method addInstrumentation.
@SuppressWarnings("unchecked")
private void addInstrumentation(ClassLoader classLoader, MethodNode mn, String className, String methodName) {
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.isForcedBranch()) {
LCSAJPool.addLCSAJBranch(BranchPool.getInstance(classLoader).getBranchForInstruction(v));
int branchId = BranchPool.getInstance(classLoader).getActualBranchIdForNormalBranchInstruction(v);
InsnList instrumentation = new InsnList();
instrumentation.add(new LdcInsnNode(v.getASMNode().getOpcode()));
instrumentation.add(new LdcInsnNode(branchId));
instrumentation.add(new LdcInsnNode(v.getInstructionId()));
instrumentation.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "org/evosuite/testcase/ExecutionTracer", "passedUnconditionalBranch", "(III)V"));
if (v.isLabel())
mn.instructions.insert(v.getASMNode(), instrumentation);
else
mn.instructions.insertBefore(v.getASMNode(), instrumentation);
}
}
}
}
}
Aggregations