Example usage for org.objectweb.asm.tree InsnList add

List of usage examples for org.objectweb.asm.tree InsnList add

Introduction

In this page you can find the example usage for org.objectweb.asm.tree InsnList add.

Prototype

public void add(final InsnList insnList) 

Source Link

Document

Adds the given instructions to the end of this list.

Usage

From source file:org.evosuite.instrumentation.coverage.DefUseInstrumentation.java

License:Open Source License

private InsnList getMethodInstrumentation(BytecodeInstruction call, boolean staticContext,
        InsnList instrumentation, MethodNode mn) {

    String descriptor = call.getMethodCallDescriptor();
    Type[] args = Type.getArgumentTypes(descriptor);
    int loc = getNextLocalNum(mn);
    Map<Integer, Integer> to = new HashMap<Integer, Integer>();
    for (int i = args.length - 1; i >= 0; i--) {
        Type type = args[i];//  www .  jav  a 2 s  . co m
        instrumentation.add(new VarInsnNode(type.getOpcode(Opcodes.ISTORE), loc));
        to.put(i, loc);
        loc++;
    }

    // instrumentation.add(new InsnNode(Opcodes.DUP));//callee
    addObjectInstrumentation(call, instrumentation, mn);
    addCallingObjectInstrumentation(staticContext, instrumentation);
    // field method calls get special treatment:
    // during instrumentation it is not clear whether a field method
    // call constitutes a definition or a use. So the instrumentation
    // will call a special function of the ExecutionTracer which will
    // redirect the call to either passedUse() or passedDefinition()
    // using the information available during runtime (the CCFGs)
    instrumentation.add(new LdcInsnNode(DefUsePool.getDefUseCounter()));
    instrumentation
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
                    "passedFieldMethodCall", "(Ljava/lang/Object;Ljava/lang/Object;I)V"));

    for (int i = 0; i < args.length; i++) {
        Type type = args[i];
        instrumentation.add(new VarInsnNode(type.getOpcode(Opcodes.ILOAD), to.get(i)));
    }

    return instrumentation;
}

From source file:org.evosuite.instrumentation.coverage.LCSAJsInstrumentation.java

License:Open Source License

@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();/*  ww w. j a  v a 2 s .c  om*/
        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);
                }
            }
        }
    }
}

From source file:org.evosuite.instrumentation.coverage.MutationInstrumentation.java

License:Open Source License

/**
 * <p>/*from   www  .  j a  va 2s .  c  om*/
 * 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();

    // call mutationTouched(mutationObject.getId());
    // 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);
}

From source file:org.evosuite.instrumentation.mutation.DeleteField.java

License:Open Source License

/** {@inheritDoc} */
@Override//from  w  w w  .  j ava  2s. c  o m
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {
    List<Mutation> mutations = new LinkedList<Mutation>();

    FieldInsnNode node = (FieldInsnNode) instruction.getASMNode();
    Type fieldType = Type.getType(node.desc);

    // insert mutation into bytecode with conditional
    InsnList mutation = new InsnList();
    logger.debug("Mutation deletefield for statement " + node.name + node.desc);
    if (node.getOpcode() == Opcodes.GETFIELD) {
        logger.debug("Deleting source of type " + node.owner);
        mutation.add(new InsnNode(Opcodes.POP));
    }
    mutation.add(getDefault(fieldType));
    // insert mutation into pool
    Mutation mutationObject = MutationPool.addMutation(className, methodName,
            NAME + " " + node.name + node.desc, instruction, mutation, getInfectionDistance(node, mutation));

    mutations.add(mutationObject);
    return mutations;
}

From source file:org.evosuite.instrumentation.mutation.DeleteField.java

License:Open Source License

/**
 * <p>//w  ww  .j a  va 2s.  c  o  m
 * getInfectionDistance
 * </p>
 * 
 * @param original
 *            a {@link org.objectweb.asm.tree.FieldInsnNode} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(FieldInsnNode original, InsnList mutant) {
    InsnList distance = new InsnList();

    if (original.getOpcode() == Opcodes.GETFIELD)
        distance.add(new InsnNode(Opcodes.DUP)); //make sure to re-load this for GETFIELD

    distance.add(new FieldInsnNode(original.getOpcode(), original.owner, original.name, original.desc));
    Type type = Type.getType(original.desc);

    if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("[")) {
        ReplaceVariable.addReferenceDistanceCheck(distance, type, mutant);
    } else {
        ReplaceVariable.addPrimitiveDistanceCheck(distance, type, mutant);
    }

    return distance;
}

From source file:org.evosuite.instrumentation.mutation.DeleteStatement.java

License:Open Source License

/** {@inheritDoc} */
@Override//from   w ww .  ja  va  2  s. co m
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {

    List<Mutation> mutations = new LinkedList<Mutation>();

    MethodInsnNode node = (MethodInsnNode) instruction.getASMNode();
    Type returnType = Type.getReturnType(node.desc);

    // insert mutation into bytecode with conditional
    InsnList mutation = new InsnList();
    logger.info("Mutation deletestatement for statement " + node.name + node.desc);
    for (Type argType : Type.getArgumentTypes(node.desc)) {
        if (argType.getSize() == 0)
            logger.info("Ignoring parameter of type " + argType);
        else if (argType.getSize() == 2) {
            mutation.insert(new InsnNode(Opcodes.POP2));
            logger.debug("Deleting parameter of 2 type " + argType);
        } else {
            logger.debug("Deleting parameter of 1 type " + argType);
            mutation.insert(new InsnNode(Opcodes.POP));
        }
    }
    if (node.getOpcode() == Opcodes.INVOKEVIRTUAL) {
        logger.debug("Deleting callee of type " + node.owner);
        mutation.add(new InsnNode(Opcodes.POP));
    } else if (node.getOpcode() == Opcodes.INVOKEINTERFACE) {
        boolean isStatic = false;
        try {
            Class<?> clazz = Class.forName(node.owner.replace('/', '.'), false,
                    DeleteStatement.class.getClassLoader());
            for (java.lang.reflect.Method method : clazz.getMethods()) {
                if (method.getName().equals(node.name)) {
                    if (Type.getMethodDescriptor(method).equals(node.desc)) {
                        if (Modifier.isStatic(method.getModifiers()))
                            isStatic = true;
                    }
                }
            }
        } catch (ClassNotFoundException e) {
            logger.warn("Could not find class: " + node.owner + ", this is likely a severe problem");
        }
        if (!isStatic) {
            logger.info("Deleting callee of type " + node.owner);
            mutation.add(new InsnNode(Opcodes.POP));
        }
    }
    mutation.add(getDefault(returnType));

    // insert mutation into pool
    Mutation mutationObject = MutationPool.addMutation(className, methodName,
            NAME + " " + node.name + node.desc, instruction, mutation, Mutation.getDefaultInfectionDistance());

    mutations.add(mutationObject);
    return mutations;
}

From source file:org.evosuite.instrumentation.mutation.InsertUnaryOperator.java

License:Open Source License

/** {@inheritDoc} */
@Override/*from   w  w  w.  ja  v a2 s .c om*/
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {
    // TODO - need to keep InsnList in Mutation, not only Instruction?

    // Mutation: Insert an INEG _after_ an iload 
    List<Mutation> mutations = new LinkedList<Mutation>();
    List<InsnList> mutationCode = new LinkedList<InsnList>();
    List<String> descriptions = new LinkedList<String>();

    if (instruction.getASMNode() instanceof VarInsnNode) {
        try {
            InsnList mutation = new InsnList();
            VarInsnNode node = (VarInsnNode) instruction.getASMNode();

            // insert mutation into bytecode with conditional
            mutation.add(new VarInsnNode(node.getOpcode(), node.var));
            mutation.add(new InsnNode(getNegation(node.getOpcode())));
            mutationCode.add(mutation);

            if (!mn.localVariables.isEmpty())
                descriptions.add("Negation of " + getName(mn, node));
            else
                descriptions.add("Negation");

            if (node.getOpcode() == Opcodes.ILOAD) {
                if (frame.getStack(frame.getStackSize() - 1) != BooleanValueInterpreter.BOOLEAN_VALUE) {
                    mutation = new InsnList();
                    mutation.add(new IincInsnNode(node.var, 1));
                    mutation.add(new VarInsnNode(node.getOpcode(), node.var));
                    if (!mn.localVariables.isEmpty())
                        descriptions.add("IINC 1 " + getName(mn, node));
                    else
                        descriptions.add("IINC 1");
                    mutationCode.add(mutation);

                    mutation = new InsnList();
                    mutation.add(new IincInsnNode(node.var, -1));
                    mutation.add(new VarInsnNode(node.getOpcode(), node.var));
                    if (!mn.localVariables.isEmpty())
                        descriptions.add("IINC -1 " + getName(mn, node));
                    else
                        descriptions.add("IINC -1");
                    mutationCode.add(mutation);
                }
            }
        } catch (VariableNotFoundException e) {
            logger.info("Could not find variable: " + e);
            return new ArrayList<Mutation>();
        }
    } else {
        InsnList mutation = new InsnList();
        FieldInsnNode node = (FieldInsnNode) instruction.getASMNode();
        Type type = Type.getType(node.desc);
        mutation.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
        mutation.add(new InsnNode(getNegation(type)));
        descriptions.add("Negation");
        mutationCode.add(mutation);

        if (type == Type.INT_TYPE) {
            mutation = new InsnList();
            mutation.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
            mutation.add(new InsnNode(Opcodes.ICONST_1));
            mutation.add(new InsnNode(Opcodes.IADD));
            descriptions.add("+1");
            mutationCode.add(mutation);

            mutation = new InsnList();
            mutation.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
            mutation.add(new InsnNode(Opcodes.ICONST_M1));
            mutation.add(new InsnNode(Opcodes.IADD));
            descriptions.add("-1");
            mutationCode.add(mutation);
        }
    }

    int i = 0;
    for (InsnList mutation : mutationCode) {
        // insert mutation into pool
        Mutation mutationObject = MutationPool.addMutation(className, methodName,
                NAME + " " + descriptions.get(i++), instruction, mutation,
                Mutation.getDefaultInfectionDistance());

        mutations.add(mutationObject);
    }
    return mutations;
}

From source file:org.evosuite.instrumentation.mutation.ReplaceArithmeticOperator.java

License:Open Source License

/**
 * <p>getInfectionDistance</p>
 *
 * @param opcodeOrig a int./*from  w  ww. jav  a 2  s. com*/
 * @param opcodeNew a int.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(int opcodeOrig, int opcodeNew) {
    InsnList distance = new InsnList();

    if (opcodesInt.contains(opcodeOrig)) {
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceArithmeticOperator.class), "getInfectionDistanceInt",
                "(IIII)D", false));
    } else if (opcodesLong.contains(opcodeOrig)) {
        distance.add(new VarInsnNode(Opcodes.LSTORE, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new VarInsnNode(Opcodes.LLOAD, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2_X2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceArithmeticOperator.class), "getInfectionDistanceLong",
                "(JJII)D", false));
        numVariable += 2;
    } else if (opcodesFloat.contains(opcodeOrig)) {
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceArithmeticOperator.class), "getInfectionDistanceFloat",
                "(FFII)D", false));
    } else if (opcodesDouble.contains(opcodeOrig)) {
        distance.add(new VarInsnNode(Opcodes.DSTORE, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new VarInsnNode(Opcodes.DLOAD, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2_X2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceArithmeticOperator.class), "getInfectionDistanceDouble",
                "(DDII)D", false));
        numVariable += 2;
    }

    return distance;
}

From source file:org.evosuite.instrumentation.mutation.ReplaceBitwiseOperator.java

License:Open Source License

/**
 * <p>getInfectionDistance</p>
 *
 * @param opcodeOrig a int./*  w w  w .  ja  va  2  s. c  o m*/
 * @param opcodeNew a int.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(int opcodeOrig, int opcodeNew) {
    InsnList distance = new InsnList();

    if (opcodesInt.contains(opcodeOrig)) {
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceBitwiseOperator.class), "getInfectionDistanceInt",
                "(IIII)D", false));
    } else if (opcodesIntShift.contains(opcodeOrig)) {
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceBitwiseOperator.class), "getInfectionDistanceInt",
                "(IIII)D", false));
    } else if (opcodesLong.contains(opcodeOrig)) {

        distance.add(new VarInsnNode(Opcodes.LSTORE, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new VarInsnNode(Opcodes.LLOAD, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2_X2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceBitwiseOperator.class), "getInfectionDistanceLong",
                "(JJII)D", false));
        numVariable += 2;

    } else if (opcodesLongShift.contains(opcodeOrig)) {
        distance.add(new VarInsnNode(Opcodes.ISTORE, numVariable));
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new VarInsnNode(Opcodes.ILOAD, numVariable));
        distance.add(new InsnNode(Opcodes.DUP_X2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceBitwiseOperator.class), "getInfectionDistanceLong",
                "(JIII)D", false));
        numVariable += 1;
    }

    return distance;
}

From source file:org.evosuite.instrumentation.mutation.ReplaceComparisonOperator.java

License:Open Source License

/**
 * <p>getInfectionDistance</p>
 *
 * @param opcodeOrig a int.//from  ww  w  . jav  a 2 s. co  m
 * @param opcodeNew a int.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(int opcodeOrig, int opcodeNew) {
    InsnList distance = new InsnList();
    switch (opcodeOrig) {
    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPLE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
        distance.add(new InsnNode(Opcodes.DUP2));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceComparisonOperator.class), "getInfectionDistance",
                "(IIII)D", false));
        break;

    case Opcodes.IFEQ:
    case Opcodes.IFNE:
    case Opcodes.IFLE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
        distance.add(new InsnNode(Opcodes.DUP));
        distance.add(new LdcInsnNode(opcodeOrig));
        distance.add(new LdcInsnNode(opcodeNew));
        distance.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(ReplaceComparisonOperator.class), "getInfectionDistance", "(III)D",
                false));
        break;

    default:
        distance.add(new LdcInsnNode(0.0));
    }

    return distance;
}