use of org.objectweb.asm.tree.LabelNode in project evosuite by EvoSuite.
the class MutationInstrumentation method addInstrumentation.
/**
* <p>
* addInstrumentation
* </p>
*
* @param mn
* a {@link org.objectweb.asm.tree.MethodNode} object.
* @param original
* a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
* @param mutations
* a {@link java.util.List} object.
*/
protected void addInstrumentation(MethodNode mn, AbstractInsnNode original, List<Mutation> mutations) {
InsnList instructions = new InsnList();
// TODO: All mutations in the id are touched, not just one!
for (Mutation mutation : mutations) {
instructions.add(mutation.getInfectionDistance());
instructions.add(new LdcInsnNode(mutation.getId()));
MethodInsnNode touched = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(ExecutionTracer.class), "passedMutation", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.DOUBLE_TYPE, Type.INT_TYPE }), false);
instructions.add(touched);
}
LabelNode endLabel = new LabelNode();
for (Mutation mutation : mutations) {
LabelNode nextLabel = new LabelNode();
LdcInsnNode mutationId = new LdcInsnNode(mutation.getId());
instructions.add(mutationId);
FieldInsnNode activeId = new FieldInsnNode(Opcodes.GETSTATIC, Type.getInternalName(MutationObserver.class), "activeMutation", "I");
instructions.add(activeId);
instructions.add(new JumpInsnNode(Opcodes.IF_ICMPNE, nextLabel));
instructions.add(mutation.getMutation());
instructions.add(new JumpInsnNode(Opcodes.GOTO, endLabel));
instructions.add(nextLabel);
}
mn.instructions.insertBefore(original, instructions);
mn.instructions.insert(original, endLabel);
}
use of org.objectweb.asm.tree.LabelNode 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;
}
use of org.objectweb.asm.tree.LabelNode in project evosuite by EvoSuite.
the class MethodNodeTransformer method transform.
/**
* <p>transform</p>
*
* @param mn a {@link org.objectweb.asm.tree.MethodNode} object.
*/
public void transform(MethodNode mn) {
setupLocals(mn);
Set<AbstractInsnNode> originalNodes = new HashSet<AbstractInsnNode>();
AbstractInsnNode node = mn.instructions.getFirst();
while (node != mn.instructions.getLast()) {
originalNodes.add(node);
node = node.getNext();
}
// int currentIndex = 0;
node = mn.instructions.getFirst();
// while (currentIndex < mn.instructions.size()) {
boolean finished = false;
while (!finished) {
// } else
if (node instanceof MethodInsnNode) {
node = transformMethodInsnNode(mn, (MethodInsnNode) node);
} else if (node instanceof VarInsnNode) {
node = transformVarInsnNode(mn, (VarInsnNode) node);
} else if (node instanceof FieldInsnNode) {
node = transformFieldInsnNode(mn, (FieldInsnNode) node);
} else if (node instanceof InsnNode) {
node = transformInsnNode(mn, (InsnNode) node);
} else if (node instanceof TypeInsnNode) {
node = transformTypeInsnNode(mn, (TypeInsnNode) node);
} else if (node instanceof JumpInsnNode) {
node = transformJumpInsnNode(mn, (JumpInsnNode) node);
} else if (node instanceof LabelNode) {
node = transformLabelNode(mn, (LabelNode) node);
} else if (node instanceof IntInsnNode) {
node = transformIntInsnNode(mn, (IntInsnNode) node);
} else if (node instanceof MultiANewArrayInsnNode) {
node = transformMultiANewArrayInsnNode(mn, (MultiANewArrayInsnNode) node);
}
if (node == mn.instructions.getLast()) {
finished = true;
} else {
node = node.getNext();
}
}
}
use of org.objectweb.asm.tree.LabelNode in project evosuite by EvoSuite.
the class BooleanTestabilityTransformation method insertBranchIdPlaceholder.
private void insertBranchIdPlaceholder(MethodNode mn, JumpInsnNode jumpNode) {
Label label = new Label();
LabelNode labelNode = new LabelNode(label);
// BooleanTestabilityPlaceholderTransformer.addBranchPlaceholder(label, jumpNode);
mn.instructions.insertBefore(jumpNode, labelNode);
// mn.instructions.insertBefore(jumpNode, new LdcInsnNode(0));
mn.instructions.insertBefore(jumpNode, new LdcInsnNode(getBranchID(mn, jumpNode)));
}
use of org.objectweb.asm.tree.LabelNode in project evosuite by EvoSuite.
the class BranchInstrumentation method addInstrumentationForDefaultLookupswitchCase.
/**
* <p>
* addInstrumentationForDefaultLookupswitchCase
* </p>
*
* @param v
* a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
* @param instrumentation
* a {@link org.objectweb.asm.tree.InsnList} object.
*/
protected void addInstrumentationForDefaultLookupswitchCase(BytecodeInstruction v, InsnList instrumentation) {
if (!v.isLookupSwitch())
throw new IllegalArgumentException("lookup switch expected");
// setup instructions
LookupSwitchInsnNode toInstrument = (LookupSwitchInsnNode) v.getASMNode();
LabelNode caseLabel = new LabelNode();
LabelNode defaultLabel = new LabelNode();
LabelNode endLabel = new LabelNode();
int keySize = toInstrument.keys.size();
int[] keys = new int[keySize];
LabelNode[] labels = new LabelNode[keySize];
for (int i = 0; i < keySize; i++) {
keys[i] = (Integer) toInstrument.keys.get(i);
labels[i] = caseLabel;
}
LookupSwitchInsnNode myLookup = new LookupSwitchInsnNode(defaultLabel, keys, labels);
addDefaultCaseInstrumentation(v, instrumentation, myLookup, defaultLabel, caseLabel, endLabel);
}
Aggregations