Java Code Examples for com.android.dx.rop.code.BasicBlock#getLabel()

The following examples show how to use com.android.dx.rop.code.BasicBlock#getLabel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlockAddresses.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the address arrays.
 */
private void setupArrays(RopMethod method) {
    BasicBlockList blocks = method.getBlocks();
    int sz = blocks.size();

    for (int i = 0; i < sz; i++) {
        BasicBlock one = blocks.get(i);
        int label = one.getLabel();
        Insn insn = one.getInsns().get(0);

        starts[label] = new CodeAddress(insn.getPosition());

        SourcePosition pos = one.getLastInsn().getPosition();

        lasts[label] = new CodeAddress(pos);
        ends[label] = new CodeAddress(pos);
    }
}
 
Example 2
Source File: IdenticalBlockCombiner.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces one of a block's successors with a different label. Constructs
 * an updated BasicBlock instance and places it in {@code newBlocks}.
 *
 * @param block block to replace
 * @param oldLabel label of successor to replace
 * @param newLabel label of new successor
 */
private void replaceSucc(BasicBlock block, int oldLabel, int newLabel) {
    IntList newSuccessors = block.getSuccessors().mutableCopy();
    int newPrimarySuccessor;

    newSuccessors.set(newSuccessors.indexOf(oldLabel), newLabel);
    newPrimarySuccessor = block.getPrimarySuccessor();

    if (newPrimarySuccessor == oldLabel) {
        newPrimarySuccessor = newLabel;
    }

    newSuccessors.setImmutable();

    BasicBlock newBB = new BasicBlock(block.getLabel(),
            block.getInsns(), newSuccessors, newPrimarySuccessor);

    newBlocks.set(newBlocks.indexOfLabel(block.getLabel()), newBB);
}
 
Example 3
Source File: IdenticalBlockCombiner.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Replaces one of a block's successors with a different label. Constructs
 * an updated BasicBlock instance and places it in {@code newBlocks}.
 *
 * @param block block to replace
 * @param oldLabel label of successor to replace
 * @param newLabel label of new successor
 */
private void replaceSucc(BasicBlock block, int oldLabel, int newLabel) {
    IntList newSuccessors = block.getSuccessors().mutableCopy();
    int newPrimarySuccessor;

    newSuccessors.set(newSuccessors.indexOf(oldLabel), newLabel);
    newPrimarySuccessor = block.getPrimarySuccessor();

    if (newPrimarySuccessor == oldLabel) {
        newPrimarySuccessor = newLabel;
    }

    newSuccessors.setImmutable();

    BasicBlock newBB = new BasicBlock(block.getLabel(),
            block.getInsns(), newSuccessors, newPrimarySuccessor);

    newBlocks.set(newBlocks.indexOfLabel(block.getLabel()), newBB);
}
 
Example 4
Source File: BlockAddresses.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up the address arrays.
 */
private void setupArrays(RopMethod method) {
    BasicBlockList blocks = method.getBlocks();
    int sz = blocks.size();

    for (int i = 0; i < sz; i++) {
        BasicBlock one = blocks.get(i);
        int label = one.getLabel();
        Insn insn = one.getInsns().get(0);

        starts[label] = new CodeAddress(insn.getPosition());

        SourcePosition pos = one.getLastInsn().getPosition();

        lasts[label] = new CodeAddress(pos);
        ends[label] = new CodeAddress(pos);
    }
}
 
Example 5
Source File: Ropper.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Searches {@link #result} for a block with the given label. Returns its
 * index if found, or returns {@code -1} if there is no such block.
 *
 * @param label the label to look for
 * @return {@code >= -1;} the index for the block with the given label or
 * {@code -1} if there is no such block
 */
private int labelToResultIndex(int label) {
    int sz = result.size();
    for (int i = 0; i < sz; i++) {
        BasicBlock one = result.get(i);
        if (one.getLabel() == label) {
            return i;
        }
    }

    return -1;
}
 
Example 6
Source File: SsaBasicBlock.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new SSA basic block from a ROP form basic block.
 *
 * @param rmeth original method
 * @param basicBlockIndex index this block will have
 * @param parent method of this block predecessor set will be
 * updated
 * @return new instance
 */
public static SsaBasicBlock newFromRop(RopMethod rmeth,
        int basicBlockIndex, final SsaMethod parent) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    BasicBlock bb = ropBlocks.get(basicBlockIndex);
    SsaBasicBlock result =
        new SsaBasicBlock(basicBlockIndex, bb.getLabel(), parent);
    InsnList ropInsns = bb.getInsns();

    result.insns.ensureCapacity(ropInsns.size());

    for (int i = 0, sz = ropInsns.size() ; i < sz ; i++) {
        result.insns.add(new NormalSsaInsn (ropInsns.get(i), result));
    }

    result.predecessors = SsaMethod.bitSetFromLabelList(
            ropBlocks,
            rmeth.labelToPredecessors(bb.getLabel()));

    result.successors
            = SsaMethod.bitSetFromLabelList(ropBlocks, bb.getSuccessors());

    result.successorList
            = SsaMethod.indexListFromLabelList(ropBlocks,
                bb.getSuccessors());

    if (result.successorList.size() != 0) {
        int primarySuccessor = bb.getPrimarySuccessor();

        result.primarySuccessor = (primarySuccessor < 0)
                ? -1 : ropBlocks.indexOfLabel(primarySuccessor);
    }

    return result;
}
 
Example 7
Source File: SsaBasicBlock.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new SSA basic block from a ROP form basic block.
 *
 * @param rmeth original method
 * @param basicBlockIndex index this block will have
 * @param parent method of this block predecessor set will be
 * updated
 * @return new instance
 */
public static SsaBasicBlock newFromRop(RopMethod rmeth,
        int basicBlockIndex, final SsaMethod parent) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    BasicBlock bb = ropBlocks.get(basicBlockIndex);
    SsaBasicBlock result =
        new SsaBasicBlock(basicBlockIndex, bb.getLabel(), parent);
    InsnList ropInsns = bb.getInsns();

    result.insns.ensureCapacity(ropInsns.size());

    for (int i = 0, sz = ropInsns.size() ; i < sz ; i++) {
        result.insns.add(new NormalSsaInsn (ropInsns.get(i), result));
    }

    result.predecessors = SsaMethod.bitSetFromLabelList(
            ropBlocks,
            rmeth.labelToPredecessors(bb.getLabel()));

    result.successors
            = SsaMethod.bitSetFromLabelList(ropBlocks, bb.getSuccessors());

    result.successorList
            = SsaMethod.indexListFromLabelList(ropBlocks,
                bb.getSuccessors());

    if (result.successorList.size() != 0) {
        int primarySuccessor = bb.getPrimarySuccessor();

        result.primarySuccessor = (primarySuccessor < 0)
                ? -1 : ropBlocks.indexOfLabel(primarySuccessor);
    }

    return result;
}
 
Example 8
Source File: SsaBasicBlock.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new SSA basic block from a ROP form basic block.
 *
 * @param rmeth original method
 * @param basicBlockIndex index this block will have
 * @param parent method of this block predecessor set will be
 * updated
 * @return new instance
 */
public static SsaBasicBlock newFromRop(RopMethod rmeth,
        int basicBlockIndex, final SsaMethod parent) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    BasicBlock bb = ropBlocks.get(basicBlockIndex);
    SsaBasicBlock result =
        new SsaBasicBlock(basicBlockIndex, bb.getLabel(), parent);
    InsnList ropInsns = bb.getInsns();

    result.insns.ensureCapacity(ropInsns.size());

    for (int i = 0, sz = ropInsns.size() ; i < sz ; i++) {
        result.insns.add(new NormalSsaInsn (ropInsns.get(i), result));
    }

    result.predecessors = SsaMethod.bitSetFromLabelList(
            ropBlocks,
            rmeth.labelToPredecessors(bb.getLabel()));

    result.successors
            = SsaMethod.bitSetFromLabelList(ropBlocks, bb.getSuccessors());

    result.successorList
            = SsaMethod.indexListFromLabelList(ropBlocks,
                bb.getSuccessors());

    if (result.successorList.size() != 0) {
        int primarySuccessor = bb.getPrimarySuccessor();

        result.primarySuccessor = (primarySuccessor < 0)
                ? -1 : ropBlocks.indexOfLabel(primarySuccessor);
    }

    return result;
}
 
Example 9
Source File: Ropper.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Searches {@link #result} for a block with the given label. Returns its
 * index if found, or returns {@code -1} if there is no such block.
 *
 * @param label the label to look for
 * @return {@code >= -1;} the index for the block with the given label or
 * {@code -1} if there is no such block
 */
private int labelToResultIndex(int label) {
    int sz = result.size();
    for (int i = 0; i < sz; i++) {
        BasicBlock one = result.get(i);
        if (one.getLabel() == label) {
            return i;
        }
    }

    return -1;
}
 
Example 10
Source File: DotDumper.java    From Box with Apache License 2.0 4 votes vote down vote up
@Override
public void endParsingMember(ByteArray bytes, int offset, String name,
                             String descriptor, Member member) {
    if (!(member instanceof Method)) {
        return;
    }

    if (!shouldDumpMethod(name)) {
        return;
    }

    ConcreteMethod meth = new ConcreteMethod((Method) member, classFile,
                                             true, true);

    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    RopMethod rmeth =
        Ropper.convert(meth, advice, classFile.getMethods(), dexOptions);

    if (optimize) {
        boolean isStatic = AccessFlags.isStatic(meth.getAccessFlags());
        rmeth = Optimizer.optimize(rmeth,
                BaseDumper.computeParamWidth(meth, isStatic), isStatic,
                true, advice);
    }

    System.out.println("digraph "  + name + "{");

    System.out.println("\tfirst -> n"
            + Hex.u2(rmeth.getFirstLabel()) + ";");

    BasicBlockList blocks = rmeth.getBlocks();

    int sz = blocks.size();
    for (int i = 0; i < sz; i++) {
        BasicBlock bb = blocks.get(i);
        int label = bb.getLabel();
        IntList successors = bb.getSuccessors();

        if (successors.size() == 0) {
            System.out.println("\tn" + Hex.u2(label) + " -> returns;");
        } else if (successors.size() == 1) {
            System.out.println("\tn" + Hex.u2(label) + " -> n"
                    + Hex.u2(successors.get(0)) + ";");
        } else {
            System.out.print("\tn" + Hex.u2(label) + " -> {");
            for (int j = 0; j < successors.size(); j++ ) {
                int successor = successors.get(j);

                if (successor != bb.getPrimarySuccessor()) {
                    System.out.print(" n" + Hex.u2(successor) + " ");
                }

            }
            System.out.println("};");

            System.out.println("\tn" + Hex.u2(label) + " -> n"
                    + Hex.u2(bb.getPrimarySuccessor())
                    + " [label=\"primary\"];");


        }
    }

    System.out.println("}");
}
 
Example 11
Source File: Ropper.java    From Box with Apache License 2.0 3 votes vote down vote up
/**
 * Gets an unreserved and available label.
 * Labels are distributed this way:
 * <ul>
 * <li>[0, maxLabel[ are the labels of the blocks directly
 * corresponding to the input bytecode.</li>
 * <li>[maxLabel, maxLabel + method.getCatches().size()[ are reserved for exception setup
 * blocks.</li>
 * <li>[maxLabel + method.getCatches().size(),
 * maxLabel + method.getCatches().size() + SPECIAL_LABEL_COUNT[ are reserved for special blocks,
 * ie param assignement, return and synch blocks.</li>
 * <li>[maxLabel method.getCatches().size() + SPECIAL_LABEL_COUNT, getAvailableLabel()[ assigned
 *  labels. Note that some
 * of the assigned labels may not be used any more if they were assigned to a block that was
 * deleted since.</li>
 * </ul>
 *
 * @return {@code >= 0;} an available label with the guaranty that all greater labels are
 * also available.
 */
private int getAvailableLabel() {
    int candidate = getMinimumUnreservedLabel();

    for (BasicBlock bb : result) {
        int label = bb.getLabel();
        if (label >= candidate) {
            candidate = label + 1;
        }
    }

    return candidate;
}
 
Example 12
Source File: Ropper.java    From J2ME-Loader with Apache License 2.0 3 votes vote down vote up
/**
 * Gets an unreserved and available label.
 * Labels are distributed this way:
 * <ul>
 * <li>[0, maxLabel[ are the labels of the blocks directly
 * corresponding to the input bytecode.</li>
 * <li>[maxLabel, maxLabel + method.getCatches().size()[ are reserved for exception setup
 * blocks.</li>
 * <li>[maxLabel + method.getCatches().size(),
 * maxLabel + method.getCatches().size() + SPECIAL_LABEL_COUNT[ are reserved for special blocks,
 * ie param assignement, return and synch blocks.</li>
 * <li>[maxLabel method.getCatches().size() + SPECIAL_LABEL_COUNT, getAvailableLabel()[ assigned
 *  labels. Note that some
 * of the assigned labels may not be used any more if they were assigned to a block that was
 * deleted since.</li>
 * </ul>
 *
 * @return {@code >= 0;} an available label with the guaranty that all greater labels are
 * also available.
 */
private int getAvailableLabel() {
    int candidate = getMinimumUnreservedLabel();

    for (BasicBlock bb : result) {
        int label = bb.getLabel();
        if (label >= candidate) {
            candidate = label + 1;
        }
    }

    return candidate;
}
 
Example 13
Source File: BlockAddresses.java    From Box with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the instance for the final instruction of the given block.
 *
 * @param block {@code non-null;} the block in question
 * @return {@code non-null;} the appropriate instance
 */
public CodeAddress getLast(BasicBlock block) {
    return lasts[block.getLabel()];
}
 
Example 14
Source File: BlockAddresses.java    From J2ME-Loader with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the instance for the end (address after the final instruction)
 * of the given block.
 *
 * @param block {@code non-null;} the block in question
 * @return {@code non-null;} the appropriate instance
 */
public CodeAddress getEnd(BasicBlock block) {
    return ends[block.getLabel()];
}
 
Example 15
Source File: BlockAddresses.java    From buck with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the instance for the final instruction of the given block.
 *
 * @param block {@code non-null;} the block in question
 * @return {@code non-null;} the appropriate instance
 */
public CodeAddress getLast(BasicBlock block) {
    return lasts[block.getLabel()];
}
 
Example 16
Source File: BlockAddresses.java    From Box with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the instance for the end (address after the final instruction)
 * of the given block.
 *
 * @param block {@code non-null;} the block in question
 * @return {@code non-null;} the appropriate instance
 */
public CodeAddress getEnd(BasicBlock block) {
    return ends[block.getLabel()];
}
 
Example 17
Source File: BlockAddresses.java    From Box with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the instance for the start of the given block.
 *
 * @param block {@code non-null;} the block in question
 * @return {@code non-null;} the appropriate instance
 */
public CodeAddress getStart(BasicBlock block) {
    return starts[block.getLabel()];
}
 
Example 18
Source File: BlockAddresses.java    From buck with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the instance for the start of the given block.
 *
 * @param block {@code non-null;} the block in question
 * @return {@code non-null;} the appropriate instance
 */
public CodeAddress getStart(BasicBlock block) {
    return starts[block.getLabel()];
}
 
Example 19
Source File: BlockAddresses.java    From Box with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the instance for the end (address after the final instruction)
 * of the given block.
 *
 * @param block {@code non-null;} the block in question
 * @return {@code non-null;} the appropriate instance
 */
public CodeAddress getEnd(BasicBlock block) {
    return ends[block.getLabel()];
}
 
Example 20
Source File: BlockAddresses.java    From Box with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the instance for the final instruction of the given block.
 *
 * @param block {@code non-null;} the block in question
 * @return {@code non-null;} the appropriate instance
 */
public CodeAddress getLast(BasicBlock block) {
    return lasts[block.getLabel()];
}