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

License:Open Source License

/**
 * <p>//from w  w w.ja v a 2  s  .  c  o m
 * 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);
    il.add(new JumpInsnNode(Opcodes.IFLE, labelIsNotEmpty));
    il.add(new InsnNode(Opcodes.ICONST_1));
    il.add(new JumpInsnNode(Opcodes.GOTO, labelEndif));
    il.add(labelIsNotEmpty);//from w  w w  . j  a  v  a  2  s.  co m
    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>/*from w w w . j av a 2s  . co 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

/**
 * For each actual case <key>: of a switch this method adds instrumentation
 * for the Branch corresponding to that case to the given instruction list.
 *
 * @param v/*w ww  .j a  va  2 s  . c o  m*/
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 * @param instrumentation
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @param className
 *            a {@link java.lang.String} object.
 * @param methodName
 *            a {@link java.lang.String} object.
 */
protected void addInstrumentationForSwitchCases(BytecodeInstruction v, InsnList instrumentation,
        String className, String methodName) {

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

    List<Branch> caseBranches = BranchPool.getInstance(classLoader).getCaseBranchesForSwitch(v);

    if (caseBranches == null || caseBranches.isEmpty())
        throw new IllegalStateException(
                "expect BranchPool to know at least one Branch for each switch instruction");

    for (Branch targetCaseBranch : caseBranches) {
        if (targetCaseBranch.isDefaultCase())
            continue; // handled elsewhere

        Integer targetCaseValue = targetCaseBranch.getTargetCaseValue();
        Integer targetCaseBranchId = targetCaseBranch.getActualBranchId();

        instrumentation.add(new InsnNode(Opcodes.DUP));
        instrumentation.add(new LdcInsnNode(targetCaseValue));
        instrumentation.add(new LdcInsnNode(Opcodes.IF_ICMPEQ));
        instrumentation.add(new LdcInsnNode(targetCaseBranchId));
        instrumentation.add(new LdcInsnNode(v.getInstructionId()));
        instrumentation.add(
                new MethodInsnNode(Opcodes.INVOKESTATIC, EXECUTION_TRACER, "passedBranch", "(IIIII)V", false));
    }
}

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

License:Open Source License

/**
 * <p>/*from  w w w  .j  ava 2  s  . co m*/
 * 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);

}

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

License:Open Source License

/**
 * <p>//from  ww w . j av a 2 s  .c o  m
 * addDefaultCaseCoveredCall
 * </p>
 *
 * @param v
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 * @param instrumentation
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @param defaultCaseBranchId
 *            a int.
 */
protected void addDefaultCaseCoveredCall(BytecodeInstruction v, InsnList instrumentation,
        int defaultCaseBranchId) {

    instrumentation.add(new LdcInsnNode(0));
    instrumentation.add(new LdcInsnNode(Opcodes.IFEQ));
    instrumentation.add(new LdcInsnNode(defaultCaseBranchId));
    instrumentation.add(new LdcInsnNode(v.getInstructionId()));
    instrumentation
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, EXECUTION_TRACER, "passedBranch", "(IIII)V", false));

}

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

License:Open Source License

/**
 * <p>//from   w  w w.  ja  v a 2 s  .  com
 * addDefaultCaseNotCoveredCall
 * </p>
 *
 * @param v
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 * @param instrumentation
 *            a {@link org.objectweb.asm.tree.InsnList} object.
 * @param defaultCaseBranchId
 *            a int.
 */
protected void addDefaultCaseNotCoveredCall(BytecodeInstruction v, InsnList instrumentation,
        int defaultCaseBranchId) {

    instrumentation.add(new LdcInsnNode(0));
    instrumentation.add(new LdcInsnNode(Opcodes.IFNE));
    instrumentation.add(new LdcInsnNode(defaultCaseBranchId));
    instrumentation.add(new LdcInsnNode(v.getInstructionId()));
    instrumentation
            .add(new MethodInsnNode(Opcodes.INVOKESTATIC, EXECUTION_TRACER, "passedBranch", "(IIII)V", false));
}

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

License:Open Source License

/**
 * Creates the instrumentation needed to track defs and uses
 * /*  w  ww  . java2s .c  o 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.DefUseInstrumentation.java

License:Open Source License

private void addCallingObjectInstrumentation(boolean staticContext, InsnList instrumentation) {
    // the object on which the DU is covered is passed by the
    // instrumentation.
    // If we are in a static context, null is passed instead
    if (staticContext) {
        instrumentation.add(new InsnNode(Opcodes.ACONST_NULL));
    } else {//from   w  w  w . j a  v  a2  s  . co m
        instrumentation.add(new VarInsnNode(Opcodes.ALOAD, 0)); // "this"
    }
}

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

License:Open Source License

private void addObjectInstrumentation(BytecodeInstruction instruction, InsnList instrumentation,
        MethodNode mn) {// w  w w . jav  a2  s. co m
    if (instruction.isLocalVariableDefinition()) {
        if (instruction.getASMNode().getOpcode() == Opcodes.ALOAD) {
            instrumentation.add(new InsnNode(Opcodes.DUP));
        } else {
            instrumentation.add(new InsnNode(Opcodes.ACONST_NULL));
        }
    } else if (instruction.isLocalVariableUse()) {
        if (instruction.getASMNode().getOpcode() == Opcodes.ASTORE) {
            instrumentation.add(new InsnNode(Opcodes.DUP));
        } else {
            instrumentation.add(new InsnNode(Opcodes.ACONST_NULL));
        }
    } else if (instruction.isArrayStoreInstruction()) {
        // Object, index, value
        instrumentation.add(new InsnNode(Opcodes.DUP));
        //      } else if(instruction.isArrayLoadInstruction()) {
        //         instrumentation.add(new InsnNode(Opcodes.DUP));
    } else if (instruction.isFieldNodeDU()) {
        // TODO: FieldNodeDU takes care of ArrayStore - why?
        Type type = Type.getType(instruction.getFieldType());
        if (type.getSort() == Type.OBJECT) {
            instrumentation.add(new InsnNode(Opcodes.DUP));
        } else {
            instrumentation.add(new InsnNode(Opcodes.ACONST_NULL));
        }
    } else if (instruction.isMethodCall()) {
        Type type = Type.getReturnType(instruction.getMethodCallDescriptor());
        if (type.getSort() == Type.OBJECT) {
            instrumentation.add(new InsnNode(Opcodes.DUP));
        } else {
            instrumentation.add(new InsnNode(Opcodes.ACONST_NULL));
        }
    }
}