Search in sources :

Example 16 with AbstractInsnNode

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;
}
Also used : AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 17 with AbstractInsnNode

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);
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) Type(org.objectweb.asm.Type) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) Label(org.objectweb.asm.Label)

Example 18 with AbstractInsnNode

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;
}
Also used : FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) Type(org.objectweb.asm.Type) FrameNode(org.objectweb.asm.tree.FrameNode) MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) FieldInsnNode(org.objectweb.asm.tree.FieldInsnNode) LineNumberNode(org.objectweb.asm.tree.LineNumberNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) VarInsnNode(org.objectweb.asm.tree.VarInsnNode)

Example 19 with AbstractInsnNode

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;
}
Also used : LabelNode(org.objectweb.asm.tree.LabelNode) AnnotatedLabel(org.evosuite.runtime.instrumentation.AnnotatedLabel) Branch(org.evosuite.coverage.branch.Branch) BytecodeInstruction(org.evosuite.graphs.cfg.BytecodeInstruction) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) RawControlFlowGraph(org.evosuite.graphs.cfg.RawControlFlowGraph)

Example 20 with AbstractInsnNode

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);
}
Also used : MethodInsnNode(org.objectweb.asm.tree.MethodInsnNode) TableSwitchInsnNode(org.objectweb.asm.tree.TableSwitchInsnNode) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode) InsnNode(org.objectweb.asm.tree.InsnNode) LookupSwitchInsnNode(org.objectweb.asm.tree.LookupSwitchInsnNode) JumpInsnNode(org.objectweb.asm.tree.JumpInsnNode)

Aggregations

AbstractInsnNode (org.objectweb.asm.tree.AbstractInsnNode)185 MethodInsnNode (org.objectweb.asm.tree.MethodInsnNode)68 MethodNode (org.objectweb.asm.tree.MethodNode)54 InsnList (org.objectweb.asm.tree.InsnList)53 InsnNode (org.objectweb.asm.tree.InsnNode)41 VarInsnNode (org.objectweb.asm.tree.VarInsnNode)38 LdcInsnNode (org.objectweb.asm.tree.LdcInsnNode)37 FieldInsnNode (org.objectweb.asm.tree.FieldInsnNode)35 ClassNode (org.objectweb.asm.tree.ClassNode)33 LabelNode (org.objectweb.asm.tree.LabelNode)27 JumpInsnNode (org.objectweb.asm.tree.JumpInsnNode)26 Test (org.junit.Test)25 Label (org.objectweb.asm.Label)25 ClassReader (org.objectweb.asm.ClassReader)23 TypeInsnNode (org.objectweb.asm.tree.TypeInsnNode)23 HashSet (java.util.HashSet)16 Type (org.objectweb.asm.Type)15 Frame (org.objectweb.asm.tree.analysis.Frame)13 ArrayList (java.util.ArrayList)12 TryCatchBlockNode (org.objectweb.asm.tree.TryCatchBlockNode)11