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.coverage.mutation.Mutation.java

License:Open Source License

/**
 * <p>//from  www. j  av  a  2 s  .  c om
 * getDefaultInfectionDistance
 * </p>
 * 
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
public static InsnList getDefaultInfectionDistance() {
    InsnList defaultDistance = new InsnList();
    defaultDistance.add(new LdcInsnNode(0.0));
    return defaultDistance;
}

From source file:org.evosuite.instrumentation.ContainerTransformation.java

License:Open Source License

private static InsnList createNewIfThenElse(MethodInsnNode n) {
    LabelNode labelIsNotEmpty = new LabelNode();
    LabelNode labelEndif = new LabelNode();
    InsnList il = new InsnList();
    il.add(n);//from  w  w  w.  j av a  2  s . co m
    il.add(new JumpInsnNode(Opcodes.IFLE, labelIsNotEmpty));
    il.add(new InsnNode(Opcodes.ICONST_1));
    il.add(new JumpInsnNode(Opcodes.GOTO, labelEndif));
    il.add(labelIsNotEmpty);
    il.add(new InsnNode(Opcodes.ICONST_0));
    il.add(labelEndif);
    return il;
}

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

License:Open Source License

/**
 * <p>//  w w  w  .  jav a 2s  . c  o m
 * getInstrumentation
 * </p>
 *
 * @param instruction
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
protected InsnList getInstrumentation(BytecodeInstruction instruction) {
    if (instruction == null)
        throw new IllegalArgumentException("null given");
    if (!instruction.isActualBranch())
        throw new IllegalArgumentException("branch instruction expected");
    if (!BranchPool.getInstance(classLoader).isKnownAsNormalBranchInstruction(instruction))
        throw new IllegalArgumentException(
                "expect given instruction to be known by the BranchPool as a normal branch instruction");

    int opcode = instruction.getASMNode().getOpcode();
    int instructionId = instruction.getInstructionId();
    int branchId = BranchPool.getInstance(classLoader).getActualBranchIdForNormalBranchInstruction(instruction);
    if (branchId < 0)
        throw new IllegalStateException("expect BranchPool to know branchId for all branch instructions");

    InsnList instrumentation = new InsnList();

    switch (opcode) {
    case Opcodes.IFEQ:
    case Opcodes.IFNE:
    case Opcodes.IFLT:
    case Opcodes.IFGE:
    case Opcodes.IFGT:
    case Opcodes.IFLE:
        instrumentation.add(new InsnNode(Opcodes.DUP));
        instrumentation.add(new LdcInsnNode(opcode));
        // instrumentation.add(new LdcInsnNode(id));
        instrumentation.add(new LdcInsnNode(branchId));
        instrumentation.add(new LdcInsnNode(instructionId));
        instrumentation.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, EXECUTION_TRACER, "passedBranch", "(IIII)V", false));
        logger.debug("Adding passedBranch val=?, opcode=" + opcode + ", branch=" + branchId + ", bytecode_id="
                + instructionId);

        break;
    case Opcodes.IF_ICMPEQ:
    case Opcodes.IF_ICMPNE:
    case Opcodes.IF_ICMPLT:
    case Opcodes.IF_ICMPGE:
    case Opcodes.IF_ICMPGT:
    case Opcodes.IF_ICMPLE:
        instrumentation.add(new InsnNode(Opcodes.DUP2));
        instrumentation.add(new LdcInsnNode(opcode));
        // instrumentation.add(new LdcInsnNode(id));
        instrumentation.add(new LdcInsnNode(branchId));
        instrumentation.add(new LdcInsnNode(instructionId));
        instrumentation.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, EXECUTION_TRACER, "passedBranch", "(IIIII)V", false));
        break;
    case Opcodes.IF_ACMPEQ:
    case Opcodes.IF_ACMPNE:
        instrumentation.add(new InsnNode(Opcodes.DUP2));
        instrumentation.add(new LdcInsnNode(opcode));
        // instrumentation.add(new LdcInsnNode(id));
        instrumentation.add(new LdcInsnNode(branchId));
        instrumentation.add(new LdcInsnNode(instructionId));
        instrumentation.add(new MethodInsnNode(Opcodes.INVOKESTATIC, EXECUTION_TRACER, "passedBranch",
                "(Ljava/lang/Object;Ljava/lang/Object;III)V", false));
        break;
    case Opcodes.IFNULL:
    case Opcodes.IFNONNULL:
        instrumentation.add(new InsnNode(Opcodes.DUP));
        instrumentation.add(new LdcInsnNode(opcode));
        // instrumentation.add(new LdcInsnNode(id));
        instrumentation.add(new LdcInsnNode(branchId));
        instrumentation.add(new LdcInsnNode(instructionId));
        instrumentation.add(new MethodInsnNode(Opcodes.INVOKESTATIC, EXECUTION_TRACER, "passedBranch",
                "(Ljava/lang/Object;III)V", false));
        break;
    }
    return instrumentation;
}

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

License:Open Source License

/**
 * Creates the instrumentation for switch statements as follows:
 *
 * For each case <key>: in the switch, two calls to the ExecutionTracer are
 * added to the instrumentation, indicating whether the case is hit directly
 * or not. This is done by addInstrumentationForSwitchCases().
 *
 * Additionally in order to trace the execution of the default: case of the
 * switch, the following instrumentation is added using
 * addDefaultCaseInstrumentation():/*from  ww  w  . j  a v  a  2 s  . c o  m*/
 *
 * A new switch, holding the same <key>s as the original switch we want to
 * cover. All cases point to a label after which a call to the
 * ExecutionTracer is added, indicating that the default case was not hit
 * directly. Symmetrically the new switch has a default case: holding a call
 * to the ExecutionTracer to indicate that the default will be hit directly.
 *
 * @param v
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 * @param mn
 *            a {@link org.objectweb.asm.tree.MethodNode} object.
 * @param className
 *            a {@link java.lang.String} object.
 * @param methodName
 *            a {@link java.lang.String} object.
 * @return a {@link org.objectweb.asm.tree.InsnList} object.
 */
protected InsnList getSwitchInstrumentation(BytecodeInstruction v, MethodNode mn, String className,
        String methodName) {
    InsnList instrumentation = new InsnList();

    if (!v.isSwitch())
        throw new IllegalArgumentException("switch instruction expected");

    addInstrumentationForDefaultSwitchCase(v, instrumentation);

    addInstrumentationForSwitchCases(v, instrumentation, className, methodName);

    return instrumentation;
}

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

License:Open Source License

/**
 * Creates the instrumentation needed to track defs and uses
 * //from w w  w .  j av  a 2 s . co m
 */
private InsnList getInstrumentation(BytecodeInstruction v, boolean staticContext, String className,
        String methodName, MethodNode mn) {
    InsnList instrumentation = new InsnList();

    if (!v.isDefUse()) {
        logger.warn("unexpected DefUseInstrumentation call for a non-DU-instruction");
        return instrumentation;
    }

    if (DefUsePool.isKnownAsFieldMethodCall(v)) {
        return getMethodInstrumentation(v, staticContext, instrumentation, mn);
    }

    if (DefUsePool.isKnownAsUse(v)) {
        // The actual object that is defined is on the stack _after_ the load instruction
        addObjectInstrumentation(v, instrumentation, mn);
        addCallingObjectInstrumentation(staticContext, instrumentation);
        instrumentation.add(new LdcInsnNode(DefUsePool.getUseCounter()));
        instrumentation.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, PackageInfo.getNameWithSlash(ExecutionTracer.class),
                        "passedUse", "(Ljava/lang/Object;Ljava/lang/Object;I)V"));
    }
    if (DefUsePool.isKnownAsDefinition(v)) {
        // The actual object that is defined is on the stack _before_ the store instruction
        addObjectInstrumentation(v, instrumentation, mn);
        addCallingObjectInstrumentation(staticContext, instrumentation);
        instrumentation.add(new LdcInsnNode(DefUsePool.getDefCounter()));
        instrumentation.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
                PackageInfo.getNameWithSlash(org.evosuite.testcase.execution.ExecutionTracer.class),
                "passedDefinition", "(Ljava/lang/Object;Ljava/lang/Object;I)V"));
    }

    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();/*  w w  w  .java 2s  . c o m*/
        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   w w w . j  a v a2  s.c  o m
 * 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//ww w . ja  v a 2 s  .  c om
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>//from   w w w. j av  a2 s.  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 .j a  v  a  2  s  . c  o 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;
}