Example usage for org.objectweb.asm.tree LabelNode getLabel

List of usage examples for org.objectweb.asm.tree LabelNode getLabel

Introduction

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

Prototype

public Label getLabel() 

Source Link

Document

Returns the label encapsulated by this node.

Usage

From source file:cl.inria.stiq.instrumenter.BCIUtils.java

License:Open Source License

private static void checkLabel(LabelNode aLabelNode) {
    assert aLabelNode.getLabel().info == null || aLabelNode.getLabel().info == aLabelNode;
}

From source file:com.sun.tdk.jcov.instrument.BranchCodeMethodAdapter.java

License:Open Source License

private String labelString(LabelNode lnode) {
    Label lab = lnode.getLabel();
    return lab.toString();
}

From source file:com.triage.bytecodemaster.TestObjectReferenceSwitches.java

public void testReplacingThisWithOtherVariable() throws Exception {

    final String FIELDPROXYWORKED = "FIELDPROXYWORKED";

    //set up the proxy object. this is the object that will receive
    //the proxied calls
    TestSubClass tcc = new TestSubClass();
    tcc.setBaseString(FIELDPROXYWORKED);

    TestBaseClassHolder.setTestBase(tcc);

    //get the dynamic source that has the donor body in it
    ClassNode donorSource = loadGroovyTestClassAsBytecode(GROOVY_CLASS_FIELDREF);
    MethodNode donorMethod = findMethod(donorSource, "before_whatDoIThinkAbout");

    System.out.println("Donor");
    printMethodNode(donorMethod);//w w  w  .  j a va 2s  .com

    //alright here's the strategy:  (1) inject a new local variable that points 
    //   to our remote instance,
    //  (2) inject code that sets this local to the value of a method call,
    //  (3) change references to 'this' ( ALOAD0 or ALOAD 0 ) to ALOAD1

    InsnList instructionsToInject = donorMethod.instructions;

    //make a new local variable
    LabelNode begin = new LabelNode();
    LabelNode end = new LabelNode();
    instructionsToInject.insertBefore(instructionsToInject.getFirst(), begin);
    instructionsToInject.add(end);

    Type type = Type.getObjectType("com/triage/bytecodemaster/fortesting/TestBaseClass");
    int variableIndex = donorMethod.maxLocals;
    donorMethod.maxLocals += type.getSize();
    donorMethod.visitLocalVariable("proxy", type.getDescriptor(), null, begin.getLabel(), end.getLabel(),
            variableIndex);

    //set the value of the local variable with a new instruction at the top
    //fetch a reference to our proxy object
    MethodInsnNode getTestBase = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "com/triage/bytecodemaster/fortesting/TestBaseClassHolder", "getTestBase",
            "()Lcom/triage/bytecodemaster/fortesting/TestBaseClass;");

    //insert after begin label
    instructionsToInject.insert(begin, getTestBase);

    //store reference
    VarInsnNode setRef = new VarInsnNode(Opcodes.ASTORE, variableIndex);

    //insert store after fetch
    instructionsToInject.insert(getTestBase, setRef);

    //replace all references to 'this'  with the new variable

    for (int currentIndex = 0; currentIndex < instructionsToInject.size(); currentIndex++) {
        AbstractInsnNode node = instructionsToInject.get(currentIndex);
        if (node.getOpcode() == Opcodes.ALOAD) {
            VarInsnNode vin = (VarInsnNode) node;

            //'this' is var index 0. ours is var index varindex
            if (vin.var == 0) {
                vin.var = variableIndex;
            }
        }
    }

    System.out.println(">>>>>>>>>Finished Modifying<<<<<<<<");
    printMethodNode(donorMethod);
    String NEWCLASSNAME = "ScriptTestClass";

    //write a class 
    Class c = createClassFromClassNode(donorSource, NEWCLASSNAME);

    Object o = c.newInstance();
    Method m = o.getClass().getDeclaredMethod("before_whatDoIThinkAbout", String.class);

    //should return HAHAHA not baseStringValue 
    String result = (String) m.invoke(o, new Object[] { "AAAA" });
    System.out.println("TestDonorClass.whatDoIThinkAbout Result: " + result);
    assertTrue(result.equals(FIELDPROXYWORKED + "AAAA"));
}

From source file:com.triage.bytecodemaster.TestObjectReferenceSwitches.java

@Test
public void testInjectingIntoMethodWithLotsOfParameters() throws Exception {
    final String FIELDPROXYWORKED = "FIELDPROXYWORKED";

    //set up the proxy object. this is the object that will receive
    //the proxied calls
    TestSubClass tcc = new TestSubClass();
    tcc.setBaseString(FIELDPROXYWORKED);

    TestBaseClassHolder.setTestBase(tcc);

    //get the dynamic source that has the donor body in it
    ClassNode donorSource = loadGroovyTestClassAsBytecode(GROOVY_CLASS_FIELDREF2);
    MethodNode donorMethod = findMethod(donorSource, "before_whatDoIThinkAbout");

    System.out.println("Donor Method Before Modifications:");
    printMethodNode(donorMethod);//from   w ww  . j av a  2s.c o  m

    String TARGETCLASSNAME = "com.triage.bytecodemaster.fortesting.Concatenator";
    ClassNode targetSource = loadLocalClass(TARGETCLASSNAME);

    ClassNode exampleSource = loadLocalClass("com.triage.bytecodemaster.fortesting.JustLikeGroovyClass");
    MethodNode exampleMethod = findMethod(exampleSource, "before_whatDoIThinkAbout");

    System.out.println("Example Method-- Should be just like the Donor Source");
    printMethodNode(exampleMethod);
    MethodNode targetMethod = findMethod(targetSource, "concat");
    System.out.println("Target Method <Before Mods>");
    printMethodNode(targetMethod);

    //alright here's the strategy:  (1) inject a new local variable that points 
    //   to our remote instance,
    //  (2) inject code that sets this local to the value of a method call,
    //  (3) change references to 'this' ( ALOAD0 or ALOAD 0 ) to ALOAD1

    InsnList instructionsToInject = donorMethod.instructions;
    InsnList targetInstructions = targetMethod.instructions;

    //make a new local variable in the donor method.
    //this variable needs to have a slot high enough that it doesnt
    //conflict with either the target or the source method
    //it will hold references to the objects we replace with 'this'
    LabelNode begin = new LabelNode();
    LabelNode end = new LabelNode();
    targetInstructions.insertBefore(targetInstructions.getFirst(), begin);
    targetInstructions.add(end);

    Type type = Type.getObjectType("com/triage/bytecodemaster/fortesting/TestBaseClass");
    int variableIndex = targetMethod.maxLocals;
    targetMethod.maxLocals += type.getSize();
    targetMethod.visitLocalVariable("proxy", type.getDescriptor(), null, begin.getLabel(), end.getLabel(),
            variableIndex);

    //set the value of the local variable with a new instruction at the top
    //fetch a reference to our proxy object
    MethodInsnNode getTestBase = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "com/triage/bytecodemaster/fortesting/TestBaseClassHolder", "getTestBase",
            "()Lcom/triage/bytecodemaster/fortesting/TestBaseClass;");

    //insert after begin label
    targetInstructions.insert(begin, getTestBase);

    //store reference
    VarInsnNode setRef = new VarInsnNode(Opcodes.ASTORE, variableIndex);

    //insert store after fetch
    targetInstructions.insert(getTestBase, setRef);

    //replace all references to 'this' in the DONOR method with the new variable
    //in the TARGET code

    for (int currentIndex = 0; currentIndex < instructionsToInject.size(); currentIndex++) {
        AbstractInsnNode node = instructionsToInject.get(currentIndex);
        if (node.getOpcode() == Opcodes.ALOAD) {
            VarInsnNode vin = (VarInsnNode) node;

            //'this' is var index 0. ours is var index varindex
            if (vin.var == 0) {
                vin.var = variableIndex;
            }
        }

        //remove return methods. this will prevent a return. it should cause the donor 
        //method to have parameters that overlap with the target, which has more parameters
        if (node.getOpcode() == Opcodes.RETURN || node.getOpcode() == Opcodes.ARETURN) {
            instructionsToInject.remove(node);
        }
    }

    System.out.println(">>>>>>>>>Finished Modifying Donor Method <<<<<<<<");
    printMethodNode(donorMethod);
    String NEWCLASSNAME = "ScriptTestClass";

    //stash instructions at the beginning of the original method, 
    //but after populating the new variable
    targetInstructions.insert(setRef, instructionsToInject);

    System.out.println("Modified Target:");
    printMethodNode(targetMethod);

    //write a class 
    Class c = createClassFromClassNode(targetSource, TARGETCLASSNAME);

    Object o = c.newInstance();
    Method m = o.getClass().getDeclaredMethod("concat", String.class, String.class, String.class, String.class);

    //should return HAHAHA not baseStringValue 
    String result = (String) m.invoke(o, new Object[] { "A", "B", "C", "D" });
    System.out.println("Concatenator.concat Result: " + result);
    assertTrue(result.equals("ABCD"));

}

From source file:com.triage.bytecodemaster.TestObjectReferenceSwitches.java

public void testMergedInReplacingThisWithOtherVariable() throws Exception {

    final String FIELDPROXYWORKED = "FIELDPROXYWORKED";

    //set up the proxy object. this is the object that will receive
    //the proxied calls
    TestSubClass tcc = new TestSubClass();
    tcc.setBaseString(FIELDPROXYWORKED);

    TestBaseClassHolder.setTestBase(tcc);

    //get the dynamic source that has the donor body in it
    ClassNode donorSource = loadGroovyTestClassAsBytecode(GROOVY_CLASS_FIELDREF);
    MethodNode donorMethod = findMethod(donorSource, "before_whatDoIThinkAbout");

    //load the target class
    String TARGETCLASSNAME = "com.triage.bytecodemaster.fortesting.TestFriend";
    ClassNode targetSource = loadLocalClass(TARGETCLASSNAME);
    MethodNode targetMethod = findMethod(targetSource, "whatDoIThinkAbout");

    System.out.println("Target");
    printMethodNode(targetMethod);//from  w ww  .  j a  v a2s .  c o m

    System.out.println("Donor");
    printMethodNode(donorMethod);

    //alright here's the strategy:  (1) inject a new local variable that points 
    //   to our remote instance,
    //  (2) inject code that sets this local to the value of a method call,
    //  (3) change references to 'this' ( ALOAD0 or ALOAD 0 ) to ALOAD1

    InsnList instructionsToInject = donorMethod.instructions;

    //make a new local variable
    LabelNode begin = new LabelNode();
    LabelNode end = new LabelNode();
    instructionsToInject.insertBefore(instructionsToInject.getFirst(), begin);
    instructionsToInject.add(end);

    Type type = Type.getObjectType("com/triage/bytecodemaster/fortesting/TestBaseClass");
    int variableIndex = donorMethod.maxLocals;
    donorMethod.maxLocals += type.getSize();
    donorMethod.visitLocalVariable("proxy", type.getDescriptor(), null, begin.getLabel(), end.getLabel(),
            variableIndex);

    //set the value of the local variable with a new instruction at the top
    //fetch a reference to our proxy object
    MethodInsnNode getTestBase = new MethodInsnNode(Opcodes.INVOKESTATIC,
            "com/triage/bytecodemaster/fortesting/TestBaseClassHolder", "getTestBase",
            "()Lcom/triage/bytecodemaster/fortesting/TestBaseClass;");

    //insert after begin label
    instructionsToInject.insert(begin, getTestBase);

    //store reference
    VarInsnNode setRef = new VarInsnNode(Opcodes.ASTORE, variableIndex);

    //insert store after fetch
    instructionsToInject.insert(getTestBase, setRef);

    //replace all references to 'this'  with the new variable

    for (int currentIndex = 0; currentIndex < instructionsToInject.size(); currentIndex++) {
        AbstractInsnNode node = instructionsToInject.get(currentIndex);
        if (node.getOpcode() == Opcodes.ALOAD) {
            VarInsnNode vin = (VarInsnNode) node;

            //'this' is var index 0. ours is var index varindex
            if (vin.var == 0) {
                vin.var = variableIndex;
            }
        }
    }

    System.out.println(">>>>>>>>>Finished Modifying<<<<<<<<");
    printMethodNode(donorMethod);

    //insert the donorMethod
    targetMethod.instructions.insert(instructionsToInject);

    System.out.println(">>>>>>>>>Final Method<<<<<<<<");
    printMethodNode(targetMethod);

    //write a class 
    Class c = createClassFromClassNode(targetSource, TARGETCLASSNAME);

    Object o = c.newInstance();
    Method m = o.getClass().getDeclaredMethod("whatDoIThinkAbout", TestPerson.class);
    TestPerson testPerson = new TestPerson();
    testPerson.setName("AAAA");
    //should return HAHAHA not baseStringValue 
    String result = (String) m.invoke(o, new Object[] { testPerson });
    System.out.println("TestFriend.whatDoIThinkAbout Result: " + result);
    assertTrue(result.equals(FIELDPROXYWORKED + "AAAA"));
}

From source file:de.loskutov.bco.asm.CommentedASMifierClassVisitor.java

License:Open Source License

@Override
public void visitLabel(Label label) {
    addIndex(-1);//from w w w. j a  va  2 s . c  o m
    super.visitLabel(label);

    InsnList instructions = currMethod.meth.instructions;
    LabelNode currLabel = null;
    for (int i = 0; i < instructions.size(); i++) {
        AbstractInsnNode insnNode = instructions.get(i);
        if (insnNode instanceof LabelNode) {
            LabelNode labelNode = (LabelNode) insnNode;
            if (labelNode.getLabel() == label) {
                currLabel = labelNode;
            }
        }
    }
    setCurrentLabel(currLabel);
}

From source file:kilim.analysis.BasicBlock.java

License:Open Source License

/**
 * Absorb as many instructions until the next label or the next transfer of
 * control instruction. In the first pass we may end up creating many many
 * BBs because there may be a lot of non-target labels (esp. when debug
 * information is available). The constraints are as follows:
 *   1. A transfer of control instruction must be the last instruction. It 
 *      may also be the first (and only) instruction
 *   2. A labeled instruction must be the first instruction in a BB. It
 *      may optionally be the last (and only) instruction
 *   3. A pausable method is treated like a labeled instruction, and is 
 *      given a label if there isn't one already. Constraint 2 applies.
 *//*from w ww.j  ava  2 s.  co  m*/
@SuppressWarnings("unchecked")
int initialize(int pos) {
    AbstractInsnNode ain;
    startPos = pos;

    BasicBlock bb;
    boolean endOfBB = false;
    boolean hasFollower = true;
    int size = flow.instructions.size();
    for (; pos < size; pos++) {
        if (pos > startPos && flow.getLabelAt(pos) != null) {
            pos--;
            hasFollower = true;
            endOfBB = true;
            break;
        }
        ain = getInstruction(pos);
        int opcode = ain.getOpcode();
        switch (opcode) {
        case ALOAD:
        case ILOAD:
        case LLOAD:
        case FLOAD:
        case DLOAD:
            usage.read(((VarInsnNode) ain).var);
            break;

        case ISTORE:
        case LSTORE:
        case FSTORE:
        case DSTORE:
        case ASTORE:
            usage.write(((VarInsnNode) ain).var);
            break;

        case IINC:
            int v = ((IincInsnNode) ain).var;
            usage.read(v);
            usage.write(v);
            break;

        case IFEQ:
        case IFNE:
        case IFLT:
        case IFGE:
        case IFGT:
        case IFLE:
        case IFNULL:
        case IFNONNULL:
        case IF_ICMPEQ:
        case IF_ICMPNE:
        case IF_ICMPLT:
        case IF_ICMPGE:
        case IF_ICMPGT:
        case IF_ICMPLE:
        case IF_ACMPEQ:
        case IF_ACMPNE:
        case JSR:
        case GOTO:
            bb = flow.getOrCreateBasicBlock(((JumpInsnNode) ain).label.getLabel());
            if (opcode == JSR) {
                bb.setFlag(IS_SUBROUTINE);
                hasFollower = false;
            }
            addSuccessor(bb);
            if (opcode == GOTO) {
                hasFollower = false;
            }
            endOfBB = true;
            break;

        case RET:
        case IRETURN:
        case LRETURN:
        case FRETURN:
        case DRETURN:
        case ARETURN:
        case RETURN:
        case ATHROW:
            hasFollower = false;
            endOfBB = true;
            break;

        case TABLESWITCH:
        case LOOKUPSWITCH:
            LabelNode defaultLabel;
            List<LabelNode> otherLabels;
            if (opcode == TABLESWITCH) {
                defaultLabel = ((TableSwitchInsnNode) ain).dflt;
                otherLabels = ((TableSwitchInsnNode) ain).labels;
            } else {
                defaultLabel = ((LookupSwitchInsnNode) ain).dflt;
                otherLabels = ((LookupSwitchInsnNode) ain).labels;
            }
            for (Iterator<LabelNode> it = otherLabels.iterator(); it.hasNext();) {
                addSuccessor(flow.getOrCreateBasicBlock(((LabelNode) it.next()).getLabel()));
            }
            addSuccessor(flow.getOrCreateBasicBlock(defaultLabel.getLabel()));
            endOfBB = true;
            hasFollower = false;
            break;

        case INVOKEVIRTUAL:
        case INVOKESTATIC:
        case INVOKEINTERFACE:
        case INVOKEDYNAMIC: // TODO this is new and mysterious
        case INVOKESPECIAL:
            if (flow.isPausableMethodInsn((MethodInsnNode) ain)) {
                if (pos == startPos) {
                    setFlag(PAUSABLE);
                } else {
                    bb = flow.getOrCreateBasicBlock(flow.getOrCreateLabelAtPos(pos));
                    bb.setFlag(PAUSABLE);
                    addSuccessor(bb);
                    pos--; // don't consume this instruction
                    hasFollower = true;
                    endOfBB = true;
                }
            }
            break;

        default:
            if (opcode >= 26 && opcode <= 45)
                throw new IllegalStateException("instruction variants not expected here");

            break;
        }

        if (endOfBB)
            break;
    }
    endPos = pos;
    if (hasFollower && (pos + 1) < flow.instructions.size()) {
        // add the following basic block as a successor
        Label l = flow.getOrCreateLabelAtPos(pos + 1);
        bb = flow.getOrCreateBasicBlock(l);
        addFollower(bb);
    }

    return pos;
}

From source file:kilim.analysis.MethodFlow.java

License:Open Source License

int getLabelPosition(LabelNode l) {
    return labelToPosMap.get(l.getLabel());
}

From source file:nova.core.wrapper.mc.forge.v17.asm.lib.InsnListPrinter.java

License:Open Source License

private void _visitInsn(AbstractInsnNode insn) {
    switch (insn.getType()) {
    case 0://  ww  w . j a  v  a2s  . c om
        visitInsn(insn.getOpcode());
        break;
    case 1:
        IntInsnNode iinsn = (IntInsnNode) insn;
        visitIntInsn(iinsn.getOpcode(), iinsn.operand);
        break;
    case 2:
        VarInsnNode vinsn = (VarInsnNode) insn;
        visitVarInsn(vinsn.getOpcode(), vinsn.var);
        break;
    case 3:
        TypeInsnNode tinsn = (TypeInsnNode) insn;
        visitTypeInsn(tinsn.getOpcode(), tinsn.desc);
        break;
    case 4:
        FieldInsnNode finsn = (FieldInsnNode) insn;
        visitFieldInsn(finsn.getOpcode(), finsn.owner, finsn.name, finsn.desc);
        break;
    case 5:
        MethodInsnNode minsn = (MethodInsnNode) insn;
        visitMethodInsn(minsn.getOpcode(), minsn.owner, minsn.name, minsn.desc);
        break;
    case 6:
        InvokeDynamicInsnNode idinsn = (InvokeDynamicInsnNode) insn;
        visitInvokeDynamicInsn(idinsn.name, idinsn.desc, idinsn.bsm, idinsn.bsmArgs);
        break;
    case 7:
        JumpInsnNode jinsn = (JumpInsnNode) insn;
        visitJumpInsn(jinsn.getOpcode(), jinsn.label.getLabel());
        break;
    case 8:
        LabelNode linsn = (LabelNode) insn;
        visitLabel(linsn.getLabel());
        break;
    case 9:
        LdcInsnNode ldcinsn = (LdcInsnNode) insn;
        visitLdcInsn(ldcinsn.cst);
        break;
    case 10:
        IincInsnNode iiinsn = (IincInsnNode) insn;
        visitIincInsn(iiinsn.var, iiinsn.incr);
        break;
    case 11:
        TableSwitchInsnNode tsinsn = (TableSwitchInsnNode) insn;
        Label[] tslables = new Label[tsinsn.labels.size()];
        for (int i = 0; i < tslables.length; i++) {
            tslables[i] = tsinsn.labels.get(i).getLabel();
        }
        visitTableSwitchInsn(tsinsn.min, tsinsn.max, tsinsn.dflt.getLabel(), tslables);
        break;
    case 12:
        LookupSwitchInsnNode lsinsn = (LookupSwitchInsnNode) insn;
        Label[] lslables = new Label[lsinsn.labels.size()];
        for (int i = 0; i < lslables.length; i++) {
            lslables[i] = lsinsn.labels.get(i).getLabel();
        }
        int[] lskeys = new int[lsinsn.keys.size()];
        for (int i = 0; i < lskeys.length; i++) {
            lskeys[i] = lsinsn.keys.get(i);
        }
        visitLookupSwitchInsn(lsinsn.dflt.getLabel(), lskeys, lslables);
        break;
    case 13:
        MultiANewArrayInsnNode ainsn = (MultiANewArrayInsnNode) insn;
        visitMultiANewArrayInsn(ainsn.desc, ainsn.dims);
        break;
    case 14:
        FrameNode fnode = (FrameNode) insn;
        switch (fnode.type) {
        case -1:
        case 0:
            visitFrame(fnode.type, fnode.local.size(), fnode.local.toArray(), fnode.stack.size(),
                    fnode.stack.toArray());
            break;
        case 1:
            visitFrame(fnode.type, fnode.local.size(), fnode.local.toArray(), 0, null);
            break;
        case 2:
            visitFrame(fnode.type, fnode.local.size(), null, 0, null);
            break;
        case 3:
            visitFrame(fnode.type, 0, null, 0, null);
            break;
        case 4:
            visitFrame(fnode.type, 0, null, 1, fnode.stack.toArray());
        }
        break;
    case 15:
        LineNumberNode lnode = (LineNumberNode) insn;
        visitLineNumber(lnode.line, lnode.start.getLabel());
        break;
    }
}

From source file:org.evosuite.graphs.cfg.BytecodeInstructionPool.java

License:Open Source License

/**
 * <p>//from   ww w  .j  a  va  2 s  . c o m
 * registerInstruction
 * </p>
 * 
 * @param instruction
 *            a {@link org.evosuite.graphs.cfg.BytecodeInstruction} object.
 */
public void registerInstruction(BytecodeInstruction instruction) {
    String className = instruction.getClassName();
    String methodName = instruction.getMethodName();

    if (!instructionMap.containsKey(className))
        instructionMap.put(className, new HashMap<String, List<BytecodeInstruction>>());
    if (!instructionMap.get(className).containsKey(methodName))
        instructionMap.get(className).put(methodName, new ArrayList<BytecodeInstruction>());

    instructionMap.get(className).get(methodName).add(instruction);
    logger.debug("Registering instruction " + instruction);
    List<BytecodeInstruction> instructions = instructionMap.get(className).get(methodName);
    if (instructions.size() > 1) {
        BytecodeInstruction previous = instructions.get(instructions.size() - 2);
        if (previous.isLabel()) {
            LabelNode ln = (LabelNode) previous.asmNode;
            if (ln.getLabel() instanceof AnnotatedLabel) {
                AnnotatedLabel aLabel = (AnnotatedLabel) ln.getLabel();
                if (aLabel.isStartTag()) {
                    if (aLabel.shouldIgnore()) {
                        logger.debug("Ignoring artificial branch: " + instruction);
                        return;
                    }
                }
            }
        }
    }

    if (instruction.isActualBranch()) {
        BranchPool.getInstance(classLoader).registerAsBranch(instruction);
    }
}