use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.
the class ReplaceBitwiseOperator method isApplicable.
/* (non-Javadoc)
* @see org.evosuite.cfg.instrumentation.mutation.MutationOperator#isApplicable(org.evosuite.cfg.BytecodeInstruction)
*/
/**
* {@inheritDoc}
*/
@Override
public boolean isApplicable(BytecodeInstruction instruction) {
AbstractInsnNode node = instruction.getASMNode();
int opcode = node.getOpcode();
if (opcodesInt.contains(opcode))
return true;
else if (opcodesIntShift.contains(opcode))
return true;
else if (opcodesLong.contains(opcode))
return true;
else if (opcodesLongShift.contains(opcode))
return true;
return false;
}
use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.
the class BooleanTestabilityTransformation method insertGetBefore.
/**
* Insert a call that takes a boolean from the stack, and returns the
* appropriate distance
*
* @param position
* @param list
*/
public void insertGetBefore(AbstractInsnNode position, InsnList list) {
logger.info("Inserting get call before");
// Here, branchId is the first control dependency
// list.insertBefore(position,
// new LdcInsnNode(getControlDependentBranchID(currentMethodNode,
// position)));
// insertControlDependencyPlaceholder(currentMethodNode, position);
// branch
// approx
// value
Label label = new Label();
LabelNode labelNode = new LabelNode(label);
// BooleanTestabilityPlaceholderTransformer.addControlDependencyPlaceholder(label,
// insnNode);
currentMethodNode.instructions.insertBefore(position, labelNode);
// instructions.insertBefore(insnNode, new LdcInsnNode(0));
// mn.instructions.insertBefore(insnNode, new LdcInsnNode(0));
currentMethodNode.instructions.insertBefore(position, new LdcInsnNode(getControlDependentBranchID(currentMethodNode, position)));
currentMethodNode.instructions.insertBefore(position, new InsnNode(Opcodes.SWAP));
currentMethodNode.instructions.insertBefore(position, new LdcInsnNode(getApproximationLevel(currentMethodNode, position)));
currentMethodNode.instructions.insertBefore(position, new InsnNode(Opcodes.SWAP));
MethodInsnNode get = new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getInternalName(BooleanHelper.class), "getDistance", Type.getMethodDescriptor(Type.INT_TYPE, new Type[] { Type.INT_TYPE, Type.INT_TYPE, Type.INT_TYPE }), false);
list.insertBefore(position, get);
}
use of org.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.
the class BooleanTestabilityTransformation method isBooleanAssignment.
/**
* This helper function determines whether the boolean on the stack at the
* current position will be stored in a Boolean variable
*
* @param position
* @param mn
* @return
*/
public boolean isBooleanAssignment(AbstractInsnNode position, MethodNode mn) {
AbstractInsnNode node = position.getNext();
logger.info("Checking for ISTORE after boolean");
boolean done = false;
while (!done) {
if (node.getOpcode() == Opcodes.PUTFIELD || node.getOpcode() == Opcodes.PUTSTATIC) {
// TODO: Check whether field is static
logger.info("Checking field assignment");
FieldInsnNode fn = (FieldInsnNode) node;
if (Type.getType(DescriptorMapping.getInstance().getFieldDesc(fn.owner, fn.name, fn.desc)) == Type.BOOLEAN_TYPE) {
return true;
} else {
return false;
}
} else if (node.getOpcode() == Opcodes.ISTORE) {
logger.info("Found ISTORE after boolean");
VarInsnNode vn = (VarInsnNode) node;
// TODO: Check whether variable at this position is a boolean
if (isBooleanVariable(vn.var, mn)) {
logger.info("Assigning boolean to variable ");
return true;
} else {
logger.info("Variable is not a bool");
return false;
}
} else if (node.getOpcode() == Opcodes.IRETURN) {
logger.info("Checking return value of method " + cn.name + "." + mn.name);
if (DescriptorMapping.getInstance().isTransformedOrBooleanMethod(cn.name, mn.name, mn.desc)) {
logger.info("Method returns a bool");
return true;
} else {
logger.info("Method does not return a bool");
return false;
}
} else if (node.getOpcode() == Opcodes.BASTORE) {
// We remove all bytes, so BASTORE is only used for booleans
AbstractInsnNode start = position.getNext();
boolean reassignment = false;
while (start != node) {
if (node instanceof InsnNode) {
reassignment = true;
}
start = start.getNext();
}
logger.info("Possible assignment to array?");
if (reassignment)
return false;
else
return true;
} else if (node instanceof MethodInsnNode) {
// if it is a boolean parameter of a converted method, then it needs to be converted
// Problem: How do we know which parameter it represents?
MethodInsnNode methodNode = (MethodInsnNode) node;
String desc = DescriptorMapping.getInstance().getMethodDesc(methodNode.owner, methodNode.name, methodNode.desc);
Type[] types = Type.getArgumentTypes(desc);
if (types.length > 0 && types[types.length - 1] == Type.BOOLEAN_TYPE) {
return true;
} else {
return false;
}
} else if (node.getOpcode() == Opcodes.GOTO || node.getOpcode() == Opcodes.ICONST_0 || node.getOpcode() == Opcodes.ICONST_1 || node.getOpcode() == -1) {
logger.info("Continuing search");
// continue search
} else if (!(node instanceof LineNumberNode || node instanceof FrameNode)) {
logger.info("Search ended with opcode " + node.getOpcode());
return false;
}
if (node != mn.instructions.getLast())
node = node.getNext();
else
done = true;
}
return false;
}
use of org.objectweb.asm.tree.AbstractInsnNode 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.objectweb.asm.tree.AbstractInsnNode in project evosuite by EvoSuite.
the class BranchInstrumentation method addDefaultCaseInstrumentation.
/**
* <p>
* addDefaultCaseInstrumentation
* </p>
*
* @param v
* a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
* @param instrumentation
* a {@link org.objectweb.asm.tree.InsnList} object.
* @param mySwitch
* a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
* @param defaultLabel
* a {@link org.objectweb.asm.tree.LabelNode} object.
* @param caseLabel
* a {@link org.objectweb.asm.tree.LabelNode} object.
* @param endLabel
* a {@link org.objectweb.asm.tree.LabelNode} object.
*/
protected void addDefaultCaseInstrumentation(BytecodeInstruction v, InsnList instrumentation, AbstractInsnNode mySwitch, LabelNode defaultLabel, LabelNode caseLabel, LabelNode endLabel) {
int defaultCaseBranchId = BranchPool.getInstance(classLoader).getDefaultBranchForSwitch(v).getActualBranchId();
// add helper switch
instrumentation.add(new InsnNode(Opcodes.DUP));
instrumentation.add(mySwitch);
// add call for default case not covered
instrumentation.add(caseLabel);
addDefaultCaseNotCoveredCall(v, instrumentation, defaultCaseBranchId);
// jump over default (break)
instrumentation.add(new JumpInsnNode(Opcodes.GOTO, endLabel));
// add call for default case covered
instrumentation.add(defaultLabel);
addDefaultCaseCoveredCall(v, instrumentation, defaultCaseBranchId);
instrumentation.add(endLabel);
}
Aggregations