Example usage for org.objectweb.asm.tree AbstractInsnNode LABEL

List of usage examples for org.objectweb.asm.tree AbstractInsnNode LABEL

Introduction

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

Prototype

int LABEL

To view the source code for org.objectweb.asm.tree AbstractInsnNode LABEL.

Click Source Link

Document

The type of LabelNode "instructions".

Usage

From source file:br.usp.each.saeg.badua.core.internal.instr.CoverageMethodTransformer.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public void transform(final MethodNode methodNode) {

    final DefUseAnalyzer analyzer = new DefUseAnalyzer();
    try {//from  w  ww  .  j ava 2  s.  c om
        analyzer.analyze(className, methodNode);
    } catch (final AnalyzerException e) {
        throw new RuntimeException(e);
    }

    final DefUseFrame[] frames = analyzer.getDefUseFrames();
    final Variable[] variables = analyzer.getVariables();
    final int[][] successors = analyzer.getSuccessors();
    final int[][] predecessors = analyzer.getPredecessors();
    final int[][] basicBlocks = analyzer.getBasicBlocks();
    final int[] leaders = analyzer.getLeaders();

    final DefUseChain[] chains = DefUseChain.toBasicBlock(
            new DepthFirstDefUseChainSearch().search(frames, variables, successors, predecessors), leaders,
            basicBlocks);

    this.chains = chains;
    if (chains.length == 0)
        return;

    // basic block definitions
    final Set<Variable>[] defs = (Set<Variable>[]) new Set<?>[basicBlocks.length];
    for (int b = 0; b < basicBlocks.length; b++) {
        defs[b] = new HashSet<Variable>();
        for (final int insnIndex : basicBlocks[b]) {
            defs[b].addAll(frames[insnIndex].getDefinitions());
        }
    }

    // bit-sets
    final BitSet[] potcov = new BitSet[basicBlocks.length];
    final BitSet[] potcovpuse = new BitSet[basicBlocks.length];
    final BitSet[] born = new BitSet[basicBlocks.length];
    final BitSet[] disabled = new BitSet[basicBlocks.length];
    final BitSet[] sleepy = new BitSet[basicBlocks.length];
    for (int b = 0; b < basicBlocks.length; b++) {

        potcov[b] = new BitSet(chains.length);
        potcovpuse[b] = new BitSet(chains.length);
        born[b] = new BitSet(chains.length);
        disabled[b] = new BitSet(chains.length);
        sleepy[b] = new BitSet(chains.length);

        for (int i = 0; i < chains.length; i++) {

            final DefUseChain chain = chains[i];

            if (chain.target != -1 ? chain.target == b : chain.use == b) {
                potcov[b].set(i);
                if (chain.target != -1) {
                    potcovpuse[b].set(i);
                }
            }

            if (chain.def == b) {
                born[b].set(i);
            }

            if (chain.def != b && defs[b].contains(variables[chain.var])) {
                disabled[b].set(i);
            }

            if (chain.target != -1) {
                if (chain.use != b) {
                    sleepy[b].set(i);
                }
            }

        }
    }

    // first/last valid instructions
    final AbstractInsnNode[] first = new AbstractInsnNode[basicBlocks.length];
    final AbstractInsnNode[] last = new AbstractInsnNode[basicBlocks.length];
    for (int b = 0; b < basicBlocks.length; b++) {
        for (final int insnIndex : basicBlocks[b]) {
            final AbstractInsnNode insn = methodNode.instructions.get(insnIndex);

            // skip
            switch (insn.getType()) {
            case AbstractInsnNode.LABEL:
            case AbstractInsnNode.FRAME:
            case AbstractInsnNode.LINE:
                continue;
            }

            if (first[b] == null) {
                first[b] = insn;
            }
            last[b] = insn;
        }
    }

    AbstractInsnNode insn = methodNode.instructions.getFirst();
    final int windows = (chains.length + 63) / 64;
    final int[] indexes = new int[windows];
    for (int w = 0; w < windows; w++) {
        indexes[w] = idGen.nextId();
        LabelFrameNode.insertBefore(insn, methodNode.instructions, init(methodNode, w));
    }

    for (int b = 0; b < basicBlocks.length; b++) {

        final long[] lPotcov = BitSetUtils.toLongArray(potcov[b], windows);
        final long[] lPotcovpuse = BitSetUtils.toLongArray(potcovpuse[b], windows);
        final long[] lBorn = BitSetUtils.toLongArray(born[b], windows);
        final long[] lDisabled = BitSetUtils.toLongArray(disabled[b], windows);
        final long[] lSleepy = BitSetUtils.toLongArray(sleepy[b], windows);

        for (int w = 0; w < windows; w++) {

            final int nPredecessors = predecessors[basicBlocks[b][0]].length;
            final Probe p = probe(methodNode, w, nPredecessors == 0);

            p.potcov = lPotcov[w];
            p.potcovpuse = lPotcovpuse[w];
            p.born = lBorn[w];
            p.disabled = lDisabled[w];
            p.sleepy = lSleepy[w];
            p.singlePredecessor = nPredecessors == 1;

            LabelFrameNode.insertBefore(first[b], methodNode.instructions, p);

        }
        if (isReturn(last[b].getOpcode())) {
            for (int w = 0; w < windows; w++) {
                final Probe p = update(methodNode, w, indexes[w]);
                LabelFrameNode.insertBefore(last[b], methodNode.instructions, p);
            }
        }
    }

    // Finally, update the frames
    while (insn != null) {
        if (insn instanceof FrameNode) {
            final FrameNode frame = (FrameNode) insn;
            frame.local = new ArrayList<Object>(frame.local);
            int size = 0;
            for (final Object obj : frame.local) {
                size++;
                if (obj.equals(Opcodes.DOUBLE) || obj.equals(Opcodes.LONG)) {
                    size++;
                }
            }
            while (size < methodNode.maxLocals) {
                frame.local.add(Opcodes.TOP);
                size++;
            }
            final Integer type = typeOfVars();
            for (int i = 0; i < windows; i++) {
                frame.local.add(type);
                frame.local.add(type);
                frame.local.add(type);
            }
        }
        insn = insn.getNext();
    }

    methodNode.maxLocals = methodNode.maxLocals + windows * numOfVars();
    methodNode.maxStack = methodNode.maxStack + 6;
}

From source file:br.usp.each.saeg.badua.core.internal.instr.LabelFrameNode.java

License:Open Source License

private static LabelFrameNode create(final AbstractInsnNode location) {
    AbstractInsnNode insn = location.getPrevious();
    if (insn == null) {
        return null;
    }/*from   w ww  . j av  a  2  s. c  o  m*/
    while (true) {
        switch (insn.getType()) {
        case AbstractInsnNode.LABEL:
            return create((LabelFrameNode) insn);
        case AbstractInsnNode.FRAME:
        case AbstractInsnNode.LINE:
            insn = insn.getPrevious();
            continue;
        default:
            return null;
        }
    }
}

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

License:Open Source License

private static void printFrames(MethodNode aNode, Frame[] aFrames) {
    int bcIndex = 1;

    for (int i = 0; i < aFrames.length; i++) {
        Frame theFrame = aFrames[i];
        AbstractInsnNode theInsn = aNode.instructions.get(i);

        switch (theInsn.getType()) {
        case AbstractInsnNode.INSN:
        case AbstractInsnNode.INT_INSN:
        case AbstractInsnNode.VAR_INSN:
        case AbstractInsnNode.TYPE_INSN:
        case AbstractInsnNode.FIELD_INSN:
        case AbstractInsnNode.METHOD_INSN:
        case AbstractInsnNode.JUMP_INSN:
        case AbstractInsnNode.LDC_INSN:
        case AbstractInsnNode.IINC_INSN:
        case AbstractInsnNode.TABLESWITCH_INSN:
        case AbstractInsnNode.LOOKUPSWITCH_INSN:
        case AbstractInsnNode.MULTIANEWARRAY_INSN:
            TraceMethodVisitor theTraceVisitor = new TraceMethodVisitor();
            theInsn.accept(theTraceVisitor);
            StringWriter theWriter = new StringWriter();
            theTraceVisitor.print(new PrintWriter(theWriter));
            String theTraced = theWriter.toString().replace("\n", "");
            System.out.println(bcIndex + "\t" + frameString(theFrame) + " |\t" + theTraced);
            bcIndex++;//from  w ww .j  a v a2 s . c  om
            break;

        case AbstractInsnNode.FRAME:
        case AbstractInsnNode.LINE:
        case AbstractInsnNode.LABEL:
            break;
        }
    }
}

From source file:co.paralleluniverse.fibers.instrument.InstrumentMethod.java

License:Open Source License

private int getLabelIdx(LabelNode l) {
    int idx;//from  www .j av a2 s .  c o  m
    if (l instanceof BlockLabelNode) {
        idx = ((BlockLabelNode) l).idx;
    } else {
        idx = mn.instructions.indexOf(l);
    }

    // search for the "real" instruction
    for (;;) {
        int type = mn.instructions.get(idx).getType();
        if (type != AbstractInsnNode.LABEL && type != AbstractInsnNode.LINE) {
            return idx;
        }
        idx++;
    }
}

From source file:com.android.tools.klint.detector.api.LintUtils.java

License:Apache License

/**
 * Returns the previous instruction prior to the given node, ignoring label
 * and line number nodes./*from   w w  w .j av a  2s  .com*/
 *
 * @param node the node to look up the previous instruction for
 * @return the previous instruction, or null if no previous node was found
 */
@Nullable
public static AbstractInsnNode getPrevInstruction(@NonNull AbstractInsnNode node) {
    AbstractInsnNode prev = node;
    while (true) {
        prev = prev.getPrevious();
        if (prev == null) {
            return null;
        } else {
            int type = prev.getType();
            if (type != AbstractInsnNode.LINE && type != AbstractInsnNode.LABEL
                    && type != AbstractInsnNode.FRAME) {
                return prev;
            }
        }
    }
}

From source file:com.android.tools.klint.detector.api.LintUtils.java

License:Apache License

/**
 * Returns the next instruction after to the given node, ignoring label and
 * line number nodes.// w ww .  j  a v  a2 s .  com
 *
 * @param node the node to look up the next node for
 * @return the next instruction, or null if no next node was found
 */
@Nullable
public static AbstractInsnNode getNextInstruction(@NonNull AbstractInsnNode node) {
    AbstractInsnNode next = node;
    while (true) {
        next = next.getNext();
        if (next == null) {
            return null;
        } else {
            int type = next.getType();
            if (type != AbstractInsnNode.LINE && type != AbstractInsnNode.LABEL
                    && type != AbstractInsnNode.FRAME) {
                return next;
            }
        }
    }
}

From source file:com.android.tools.lint.checks.AllowAllHostnameVerifierDetector.java

License:Apache License

@SuppressWarnings("rawtypes")
@Override/*from  w  ww  . j a  va 2s . c  o  m*/
public void checkClass(@NonNull final ClassContext context, @NonNull ClassNode classNode) {
    if (!classNode.interfaces.contains("javax/net/ssl/HostnameVerifier")) {
        return;
    }
    List methodList = classNode.methods;
    for (Object m : methodList) {
        MethodNode method = (MethodNode) m;
        if ("verify".equals(method.name)) {
            InsnList nodes = method.instructions;
            boolean emptyMethod = true; // Stays true if method has no instructions
                                        // other than ICONST_1 or IRETURN.
            boolean containsIconst1 = false;

            for (int i = 0, n = nodes.size(); i < n; i++) {
                // Future work: Improve this check to be less sensitive to irrelevant
                // instructions/statements/invocations (e.g. System.out.println).
                AbstractInsnNode instruction = nodes.get(i);
                int type = instruction.getType();
                if (type != AbstractInsnNode.LABEL && type != AbstractInsnNode.LINE
                        && !(type == AbstractInsnNode.INSN && (instruction.getOpcode() == Opcodes.ICONST_1
                                || instruction.getOpcode() == Opcodes.IRETURN))) {
                    emptyMethod = false;
                    break;
                } else if (type == AbstractInsnNode.INSN && instruction.getOpcode() == Opcodes.ICONST_1) {
                    containsIconst1 = true;
                }
            }
            if (containsIconst1 && emptyMethod) {
                Location location = context.getLocation(method, classNode);
                context.report(ISSUE, location,
                        method.name + " always returns true, which "
                                + "could cause insecure network traffic due to trusting TLS/SSL "
                                + "server certificates for wrong hostnames");
            }
        }
    }
}

From source file:com.android.tools.lint.checks.TrustAllX509TrustManagerDetector.java

License:Apache License

@Override
@SuppressWarnings("rawtypes")
public void checkClass(@NonNull final ClassContext context, @NonNull ClassNode classNode) {
    if (!context.isFromClassLibrary()) {
        // Non-library code checked at the AST level
        return;// www . j ava  2 s. com
    }
    if (!classNode.interfaces.contains("javax/net/ssl/X509TrustManager")) {
        return;
    }
    List methodList = classNode.methods;
    for (Object m : methodList) {
        MethodNode method = (MethodNode) m;
        if ("checkServerTrusted".equals(method.name) || "checkClientTrusted".equals(method.name)) {
            InsnList nodes = method.instructions;
            boolean emptyMethod = true; // Stays true if method doesn't perform any "real"
                                        // operations
            for (int i = 0, n = nodes.size(); i < n; i++) {
                // Future work: Improve this check to be less sensitive to irrelevant
                // instructions/statements/invocations (e.g. System.out.println) by
                // looking for calls that could lead to a CertificateException being
                // thrown, e.g. throw statement within the method itself or invocation
                // of another method that may throw a CertificateException, and only
                // reporting an issue if none of these calls are found. ControlFlowGraph
                // may be useful here.
                AbstractInsnNode instruction = nodes.get(i);
                int type = instruction.getType();
                if (type != AbstractInsnNode.LABEL && type != AbstractInsnNode.LINE
                        && !(type == AbstractInsnNode.INSN && instruction.getOpcode() == Opcodes.RETURN)) {
                    emptyMethod = false;
                    break;
                }
            }
            if (emptyMethod) {
                Location location = context.getLocation(method, classNode);
                context.report(ISSUE, location, getErrorMessage(method.name));
            }
        }
    }
}

From source file:com.github.fge.grappa.transform.process.UnusedLabelsRemover.java

License:Apache License

@Override
public void process(@Nonnull ParserClassNode classNode, @Nonnull RuleMethod method) throws Exception {
    Objects.requireNonNull(classNode, "classNode");
    Objects.requireNonNull(method, "method");
    AbstractInsnNode current = method.instructions.getFirst();

    AbstractInsnNode next;//from  w  w w .  j  ava2  s  . co m
    boolean doRemove;
    while (current != null) {
        next = current.getNext();
        //noinspection SuspiciousMethodCalls
        doRemove = current.getType() == AbstractInsnNode.LABEL && !method.getUsedLabels().contains(current);
        if (doRemove)
            method.instructions.remove(current);
        current = next;
    }
}

From source file:com.github.fge.grappa.transform.RuleMethodInterpreter.java

License:Open Source License

private static boolean isLabelOrJump(AbstractInsnNode node) {
    return node.getType() == AbstractInsnNode.LABEL || node.getType() == AbstractInsnNode.JUMP_INSN;
}