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

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

Introduction

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

Prototype

InsnList

Source Link

Usage

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

License:Open Source License

/** {@inheritDoc} */
@Override// w  w  w  .  jav a 2s . co  m
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.//w w w  . java 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(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  v  a 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

/** {@inheritDoc} */
@Override//  ww w.  j  a  v  a 2s .co  m
public List<Mutation> apply(MethodNode mn, String className, String methodName, BytecodeInstruction instruction,
        Frame frame) {
    JumpInsnNode node = (JumpInsnNode) instruction.getASMNode();
    List<Mutation> mutations = new LinkedList<Mutation>();
    LabelNode target = node.label;

    boolean isBoolean = frame.getStack(frame.getStackSize() - 1) == BooleanValueInterpreter.BOOLEAN_VALUE;

    for (Integer op : getOperators(node.getOpcode(), isBoolean)) {
        logger.debug("Adding replacement " + op);
        if (op >= 0) {
            // insert mutation into bytecode with conditional
            JumpInsnNode mutation = new JumpInsnNode(op, target);
            // insert mutation into pool
            Mutation mutationObject = MutationPool.addMutation(className, methodName,
                    NAME + " " + getOp(node.getOpcode()) + " -> " + getOp(op), instruction, mutation,
                    getInfectionDistance(node.getOpcode(), op));
            mutations.add(mutationObject);
        } else {
            // Replace relational operator with TRUE/FALSE

            InsnList mutation = new InsnList();
            if (opcodesInt.contains(node.getOpcode()))
                mutation.add(new InsnNode(Opcodes.POP));
            else
                mutation.add(new InsnNode(Opcodes.POP2));
            if (op == TRUE) {
                mutation.add(new LdcInsnNode(1));
            } else {
                mutation.add(new LdcInsnNode(0));
            }
            mutation.add(new JumpInsnNode(Opcodes.IFNE, target));
            Mutation mutationObject = MutationPool.addMutation(className, methodName,
                    NAME + " " + getOp(node.getOpcode()) + " -> " + op, instruction, mutation,
                    getInfectionDistance(node.getOpcode(), op));
            mutations.add(mutationObject);
        }
    }

    return mutations;
}

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

License:Open Source License

/**
 * <p>getInfectionDistance</p>
 *
 * @param opcodeOrig a int.//from  w w  w  .j av a  2s .  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();
    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;
}

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

License:Open Source License

/**
 * <p>/*  www. j  a v a 2s. c  om*/
 * copy
 * </p>
 *
 * @param orig
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public static InsnList copy(InsnList orig) {
    Iterator<?> it = orig.iterator();
    InsnList copy = new InsnList();
    while (it.hasNext()) {
        AbstractInsnNode node = (AbstractInsnNode) it.next();

        if (node instanceof VarInsnNode) {
            VarInsnNode vn = (VarInsnNode) node;
            copy.add(new VarInsnNode(vn.getOpcode(), vn.var));
        } else if (node instanceof FieldInsnNode) {
            FieldInsnNode fn = (FieldInsnNode) node;
            copy.add(new FieldInsnNode(fn.getOpcode(), fn.owner, fn.name, fn.desc));
        } else if (node instanceof InsnNode) {
            if (node.getOpcode() != Opcodes.POP)
                copy.add(new InsnNode(node.getOpcode()));
        } else if (node instanceof LdcInsnNode) {
            copy.add(new LdcInsnNode(((LdcInsnNode) node).cst));
        } else {
            throw new RuntimeException("Unexpected node type: " + node.getClass());
        }
    }
    return copy;
}

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

License:Open Source License

/**
 * <p>/*from w w  w .j  av a2  s. c o m*/
 * getInfectionDistance
 * </p>
 *
 * @param type
 *            a {@link org.objectweb.asm.Type} object.
 * @param original
 *            a {@link org.objectweb.asm.tree.AbstractInsnNode} object.
 * @param mutant
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public InsnList getInfectionDistance(Type type, AbstractInsnNode original, InsnList mutant) {
    // TODO: Treat reference types different!

    InsnList distance = new InsnList();

    if (original instanceof VarInsnNode) {
        VarInsnNode node = (VarInsnNode) original;
        distance.add(new VarInsnNode(node.getOpcode(), node.var));
        if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("["))
            addReferenceDistanceCheck(distance, type, mutant);
        else
            addPrimitiveDistanceCheck(distance, type, mutant);

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

        FieldInsnNode node = (FieldInsnNode) original;
        distance.add(new FieldInsnNode(node.getOpcode(), node.owner, node.name, node.desc));
        if (type.getDescriptor().startsWith("L") || type.getDescriptor().startsWith("["))
            addReferenceDistanceCheck(distance, type, mutant);
        else
            addPrimitiveDistanceCheck(distance, type, mutant);

    } else if (original instanceof IincInsnNode) {
        distance.add(Mutation.getDefaultInfectionDistance());
    }
    return distance;
}

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

License:Open Source License

private Map<String, InsnList> getLocalReplacements(MethodNode mn, String desc, AbstractInsnNode node,
        Frame frame) {/* w  w  w . j  a va 2  s  .  co m*/
    Map<String, InsnList> replacements = new HashMap<String, InsnList>();

    //if (desc.equals("I"))
    //   return replacements;

    int otherNum = -1;
    if (node instanceof VarInsnNode) {
        VarInsnNode vNode = (VarInsnNode) node;
        otherNum = vNode.var;
    }
    if (otherNum == -1)
        return replacements;

    int currentId = mn.instructions.indexOf(node);
    logger.info("Looking for replacements at position " + currentId + " of variable " + otherNum + " of type "
            + desc);

    //   return replacements;

    for (Object v : mn.localVariables) {
        LocalVariableNode localVar = (LocalVariableNode) v;
        int startId = mn.instructions.indexOf(localVar.start);
        int endId = mn.instructions.indexOf(localVar.end);
        logger.info("Checking local variable " + localVar.name + " of type " + localVar.desc + " at index "
                + localVar.index);
        if (!localVar.desc.equals(desc))
            logger.info("- Types do not match");
        if (localVar.index == otherNum)
            logger.info("- Replacement = original");
        if (currentId < startId)
            logger.info("- Out of scope (start)");
        if (currentId > endId)
            logger.info("- Out of scope (end)");
        BasicValue newValue = (BasicValue) frame.getLocal(localVar.index);
        if (newValue == BasicValue.UNINITIALIZED_VALUE)
            logger.info("- Not initialized");

        if (localVar.desc.equals(desc) && localVar.index != otherNum && currentId >= startId
                && currentId <= endId && newValue != BasicValue.UNINITIALIZED_VALUE) {

            logger.info("Adding local variable " + localVar.name + " of type " + localVar.desc + " at index "
                    + localVar.index + ",  " + startId + "-" + endId + ", " + currentId);
            InsnList list = new InsnList();
            if (node.getOpcode() == Opcodes.GETFIELD) {
                list.add(new InsnNode(Opcodes.POP)); // Remove field owner from stack
            }

            list.add(new VarInsnNode(getLoadOpcode(localVar), localVar.index));
            replacements.put(localVar.name, list);
        }
    }
    return replacements;
}

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

License:Open Source License

private Map<String, InsnList> getLocalReplacementsInc(MethodNode mn, String desc, IincInsnNode node,
        Frame frame) {//from   w ww. j a va  2s  .c om
    Map<String, InsnList> replacements = new HashMap<String, InsnList>();

    int otherNum = -1;
    otherNum = node.var;
    int currentId = mn.instructions.indexOf(node);

    for (Object v : mn.localVariables) {
        LocalVariableNode localVar = (LocalVariableNode) v;
        int startId = mn.instructions.indexOf(localVar.start);
        int endId = mn.instructions.indexOf(localVar.end);
        logger.info("Checking local variable " + localVar.name + " of type " + localVar.desc + " at index "
                + localVar.index);
        if (!localVar.desc.equals(desc))
            logger.info("- Types do not match: " + localVar.name);
        if (localVar.index == otherNum)
            logger.info("- Replacement = original " + localVar.name);
        if (currentId < startId)
            logger.info("- Out of scope (start) " + localVar.name);
        if (currentId > endId)
            logger.info("- Out of scope (end) " + localVar.name);
        BasicValue newValue = (BasicValue) frame.getLocal(localVar.index);
        if (newValue == BasicValue.UNINITIALIZED_VALUE)
            logger.info("- Not initialized");

        if (localVar.desc.equals(desc) && localVar.index != otherNum && currentId >= startId
                && currentId <= endId && newValue != BasicValue.UNINITIALIZED_VALUE) {

            logger.info("Adding local variable " + localVar.name + " of type " + localVar.desc + " at index "
                    + localVar.index);
            InsnList list = new InsnList();
            list.add(new IincInsnNode(localVar.index, node.incr));
            replacements.put(localVar.name, list);
        }
    }
    return replacements;
}

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

License:Open Source License

private Map<String, InsnList> getFieldReplacements(MethodNode mn, String className, String desc,
        AbstractInsnNode node) {/*from w  w w. j  av a  2s.  c o m*/
    Map<String, InsnList> alternatives = new HashMap<String, InsnList>();

    boolean isStatic = (mn.access & Opcodes.ACC_STATIC) == Opcodes.ACC_STATIC;

    String otherName = "";
    if (node instanceof FieldInsnNode) {
        FieldInsnNode fNode = (FieldInsnNode) node;
        otherName = fNode.name;
    }
    try {
        logger.info("Checking class " + className);
        Class<?> clazz = Class.forName(className, false, ReplaceVariable.class.getClassLoader());

        for (Field field : TestClusterUtils.getFields(clazz)) {
            // We have to use a special version of canUse to avoid
            // that we access the CUT before it is fully initialised
            if (!canUse(field))
                continue;

            Type type = Type.getType(field.getType());
            logger.info("Checking replacement field variable " + field.getName());

            if (field.getName().equals(otherName))
                continue;

            if (isStatic && !(Modifier.isStatic(field.getModifiers())))
                continue;

            if (type.getDescriptor().equals(desc)) {
                logger.info("Adding replacement field variable " + field.getName());
                InsnList list = new InsnList();
                if (node.getOpcode() == Opcodes.GETFIELD) {
                    list.add(new InsnNode(Opcodes.POP)); // Remove field owner from stack
                }

                // new fieldinsnnode
                if (Modifier.isStatic(field.getModifiers()))
                    list.add(new FieldInsnNode(Opcodes.GETSTATIC, className.replace('.', '/'), field.getName(),
                            type.getDescriptor()));
                else {
                    list.add(new VarInsnNode(Opcodes.ALOAD, 0)); // this
                    list.add(new FieldInsnNode(Opcodes.GETFIELD, className.replace('.', '/'), field.getName(),
                            type.getDescriptor()));
                }
                alternatives.put(field.getName(), list);
            } else {
                logger.info("Descriptor does not match: " + field.getName() + " - " + type.getDescriptor());
            }
        }
    } catch (Throwable t) {
        logger.info("Class not found: " + className);
        // TODO Auto-generated catch block
        //e.printStackTrace();
    }
    return alternatives;
}