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