com.android.dx.rop.code.BasicBlockList Java Examples

The following examples show how to use com.android.dx.rop.code.BasicBlockList. 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: StdCatchBuilder.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean hasAnyCatches() {
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        if (catches.size() != 0) {
            return true;
        }
    }

    return false;
}
 
Example #2
Source File: StdCatchBuilder.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
   @Override
public HashSet<Type> getCatchTypes() {
       HashSet<Type> result = new HashSet<Type>(20);
       BasicBlockList blocks = method.getBlocks();
       int size = blocks.size();

       for (int i = 0; i < size; i++) {
           BasicBlock block = blocks.get(i);
           TypeList catches = block.getLastInsn().getCatches();
           int catchSize = catches.size();

           for (int j = 0; j < catchSize; j++) {
               result.add(catches.getType(j));
           }
       }

       return result;
   }
 
Example #3
Source File: StdCatchBuilder.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
   @Override
public boolean hasAnyCatches() {
       BasicBlockList blocks = method.getBlocks();
       int size = blocks.size();

       for (int i = 0; i < size; i++) {
           BasicBlock block = blocks.get(i);
           TypeList catches = block.getLastInsn().getCatches();
           if (catches.size() != 0) {
               return true;
           }
       }

       return false;
   }
 
Example #4
Source File: BlockAddresses.java    From J2ME-Loader 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 6 votes vote down vote up
/**
 * Extracts the resulting {@link RopMethod} from the instance.
 *
 * @return {@code non-null;} the method object
 */
private RopMethod getRopMethod() {

    // Construct the final list of blocks.

    int sz = result.size();
    BasicBlockList bbl = new BasicBlockList(sz);
    for (int i = 0; i < sz; i++) {
        bbl.set(i, result.get(i));
    }
    bbl.setImmutable();

    // Construct the method object to wrap it all up.

    /*
     * Note: The parameter assignment block is always the first
     * that should be executed, hence the second argument to the
     * constructor.
     */
    return new RopMethod(bbl, getSpecialLabel(PARAM_ASSIGNMENT));
}
 
Example #6
Source File: SsaMethod.java    From Box with Apache License 2.0 6 votes vote down vote up
private void convertRopToSsaBlocks(RopMethod rmeth) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    int sz = ropBlocks.size();

    blocks = new ArrayList<SsaBasicBlock>(sz + 2);

    for (int i = 0; i < sz; i++) {
        SsaBasicBlock sbb = SsaBasicBlock.newFromRop(rmeth, i, this);
        blocks.add(sbb);
    }

    // Add an no-op entry block.
    int origEntryBlockIndex = rmeth.getBlocks()
            .indexOfLabel(rmeth.getFirstLabel());

    SsaBasicBlock entryBlock
            = blocks.get(origEntryBlockIndex).insertNewPredecessor();

    entryBlockIndex = entryBlock.getIndex();
    exitBlockIndex = -1; // This gets made later.
}
 
Example #7
Source File: SsaMethod.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
private void convertRopToSsaBlocks(RopMethod rmeth) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    int sz = ropBlocks.size();

    blocks = new ArrayList<SsaBasicBlock>(sz + 2);

    for (int i = 0; i < sz; i++) {
        SsaBasicBlock sbb = SsaBasicBlock.newFromRop(rmeth, i, this);
        blocks.add(sbb);
    }

    // Add an no-op entry block.
    int origEntryBlockIndex = rmeth.getBlocks()
            .indexOfLabel(rmeth.getFirstLabel());

    SsaBasicBlock entryBlock
            = blocks.get(origEntryBlockIndex).insertNewPredecessor();

    entryBlockIndex = entryBlock.getIndex();
    exitBlockIndex = -1; // This gets made later.
}
 
Example #8
Source File: Ropper.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the resulting {@link RopMethod} from the instance.
 *
 * @return {@code non-null;} the method object
 */
private RopMethod getRopMethod() {

    // Construct the final list of blocks.

    int sz = result.size();
    BasicBlockList bbl = new BasicBlockList(sz);
    for (int i = 0; i < sz; i++) {
        bbl.set(i, result.get(i));
    }
    bbl.setImmutable();

    // Construct the method object to wrap it all up.

    /*
     * Note: The parameter assignment block is always the first
     * that should be executed, hence the second argument to the
     * constructor.
     */
    return new RopMethod(bbl, getSpecialLabel(PARAM_ASSIGNMENT));
}
 
Example #9
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 #10
Source File: StdCatchBuilder.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public HashSet<Type> getCatchTypes() {
    HashSet<Type> result = new HashSet<Type>(20);
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        int catchSize = catches.size();

        for (int j = 0; j < catchSize; j++) {
            result.add(catches.getType(j));
        }
    }

    return result;
}
 
Example #11
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 #12
Source File: StdCatchBuilder.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
public HashSet<Type> getCatchTypes() {
    HashSet<Type> result = new HashSet<Type>(20);
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        int catchSize = catches.size();

        for (int j = 0; j < catchSize; j++) {
            result.add(catches.getType(j));
        }
    }

    return result;
}
 
Example #13
Source File: Ropper.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the resulting {@link RopMethod} from the instance.
 *
 * @return {@code non-null;} the method object
 */
private RopMethod getRopMethod() {

    // Construct the final list of blocks.

    int sz = result.size();
    BasicBlockList bbl = new BasicBlockList(sz);
    for (int i = 0; i < sz; i++) {
        bbl.set(i, result.get(i));
    }
    bbl.setImmutable();

    // Construct the method object to wrap it all up.

    /*
     * Note: The parameter assignment block is always the first
     * that should be executed, hence the second argument to the
     * constructor.
     */
    return new RopMethod(bbl, getSpecialLabel(PARAM_ASSIGNMENT));
}
 
Example #14
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 #15
Source File: StdCatchBuilder.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean hasAnyCatches() {
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        if (catches.size() != 0) {
            return true;
        }
    }

    return false;
}
 
Example #16
Source File: StdCatchBuilder.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public HashSet<Type> getCatchTypes() {
    HashSet<Type> result = new HashSet<Type>(20);
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        int catchSize = catches.size();

        for (int j = 0; j < catchSize; j++) {
            result.add(catches.getType(j));
        }
    }

    return result;
}
 
Example #17
Source File: SsaMethod.java    From buck with Apache License 2.0 6 votes vote down vote up
private void convertRopToSsaBlocks(RopMethod rmeth) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    int sz = ropBlocks.size();

    blocks = new ArrayList<SsaBasicBlock>(sz + 2);

    for (int i = 0; i < sz; i++) {
        SsaBasicBlock sbb = SsaBasicBlock.newFromRop(rmeth, i, this);
        blocks.add(sbb);
    }

    // Add an no-op entry block.
    int origEntryBlockIndex = rmeth.getBlocks()
            .indexOfLabel(rmeth.getFirstLabel());

    SsaBasicBlock entryBlock
            = blocks.get(origEntryBlockIndex).insertNewPredecessor();

    entryBlockIndex = entryBlock.getIndex();
    exitBlockIndex = -1; // This gets made later.
}
 
Example #18
Source File: Ropper.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts the resulting {@link RopMethod} from the instance.
 *
 * @return {@code non-null;} the method object
 */
private RopMethod getRopMethod() {

    // Construct the final list of blocks.

    int sz = result.size();
    BasicBlockList bbl = new BasicBlockList(sz);
    for (int i = 0; i < sz; i++) {
        bbl.set(i, result.get(i));
    }
    bbl.setImmutable();

    // Construct the method object to wrap it all up.

    /*
     * Note: The parameter assignment block is always the first
     * that should be executed, hence the second argument to the
     * constructor.
     */
    return new RopMethod(bbl, getSpecialLabel(PARAM_ASSIGNMENT));
}
 
Example #19
Source File: SsaMethod.java    From Box with Apache License 2.0 6 votes vote down vote up
private void convertRopToSsaBlocks(RopMethod rmeth) {
    BasicBlockList ropBlocks = rmeth.getBlocks();
    int sz = ropBlocks.size();

    blocks = new ArrayList<SsaBasicBlock>(sz + 2);

    for (int i = 0; i < sz; i++) {
        SsaBasicBlock sbb = SsaBasicBlock.newFromRop(rmeth, i, this);
        blocks.add(sbb);
    }

    // Add an no-op entry block.
    int origEntryBlockIndex = rmeth.getBlocks()
            .indexOfLabel(rmeth.getFirstLabel());

    SsaBasicBlock entryBlock
            = blocks.get(origEntryBlockIndex).insertNewPredecessor();

    entryBlockIndex = entryBlock.getIndex();
    exitBlockIndex = -1; // This gets made later.
}
 
Example #20
Source File: SsaToRop.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * @return rop-form basic block list
 */
private BasicBlockList convertBasicBlocks() {
    ArrayList<SsaBasicBlock> blocks = ssaMeth.getBlocks();

    // Exit block may be null.
    SsaBasicBlock exitBlock = ssaMeth.getExitBlock();

    ssaMeth.computeReachability();
    int ropBlockCount = ssaMeth.getCountReachableBlocks();

    // Don't count the exit block, if it exists and is reachable.
    ropBlockCount -= (exitBlock != null && exitBlock.isReachable()) ? 1 : 0;

    BasicBlockList result = new BasicBlockList(ropBlockCount);

    // Convert all the reachable blocks except the exit block.
    int ropBlockIndex = 0;
    for (SsaBasicBlock b : blocks) {
        if (b.isReachable() && b != exitBlock) {
            result.set(ropBlockIndex++, convertBasicBlock(b));
        }
    }

    // The exit block, which is discarded, must do nothing.
    if (exitBlock != null && exitBlock.getInsns().size() != 0) {
        throw new RuntimeException(
                "Exit block must have no insns when leaving SSA form");
    }

    return result;
}
 
Example #21
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 #22
Source File: BlockAddresses.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param method {@code non-null;} the method to have block addresses for
 */
public BlockAddresses(RopMethod method) {
    BasicBlockList blocks = method.getBlocks();
    int maxLabel = blocks.getMaxLabel();

    this.starts = new CodeAddress[maxLabel];
    this.lasts = new CodeAddress[maxLabel];
    this.ends = new CodeAddress[maxLabel];

    setupArrays(method);
}
 
Example #23
Source File: SsaMethod.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a BitSet of block indices from a basic block list and a list
 * of labels taken from Rop form.
 *
 * @param blocks Rop blocks
 * @param labelList list of rop block labels
 * @return BitSet of block indices
 */
static BitSet bitSetFromLabelList(BasicBlockList blocks,
        IntList labelList) {
    BitSet result = new BitSet(blocks.size());

    for (int i = 0, sz = labelList.size(); i < sz; i++) {
        result.set(blocks.indexOfLabel(labelList.get(i)));
    }

    return result;
}
 
Example #24
Source File: Code.java    From dexmaker with Apache License 2.0 5 votes vote down vote up
BasicBlockList toBasicBlocks() {
    if (!localsInitialized) {
        initializeLocals();
    }

    cleanUpLabels();

    BasicBlockList result = new BasicBlockList(labels.size());
    for (int i = 0; i < labels.size(); i++) {
        result.set(i, labels.get(i).toBasicBlock());
    }
    return result;
}
 
Example #25
Source File: RopTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Performs initial creation of output instructions based on the
 * original blocks.
 */
private void outputInstructions() {
    BasicBlockList blocks = method.getBlocks();
    int[] order = this.order;
    int len = order.length;

    // Process the blocks in output order.
    for (int i = 0; i < len; i++) {
        int nextI = i + 1;
        int nextLabel = (nextI == order.length) ? -1 : order[nextI];
        outputBlock(blocks.labelToBlock(order[i]), nextLabel);
    }
}
 
Example #26
Source File: SsaMethod.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Builds an IntList of block indices from a basic block list and a list
 * of labels taken from Rop form.
 *
 * @param ropBlocks Rop blocks
 * @param labelList list of rop block labels
 * @return IntList of block indices
 */
public static IntList indexListFromLabelList(BasicBlockList ropBlocks,
        IntList labelList) {

    IntList result = new IntList(labelList.size());

    for (int i = 0, sz = labelList.size(); i < sz; i++) {
        result.add(ropBlocks.indexOfLabel(labelList.get(i)));
    }

    return result;
}
 
Example #27
Source File: SsaMethod.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a BitSet of block indices from a basic block list and a list
 * of labels taken from Rop form.
 *
 * @param blocks Rop blocks
 * @param labelList list of rop block labels
 * @return BitSet of block indices
 */
static BitSet bitSetFromLabelList(BasicBlockList blocks,
        IntList labelList) {
    BitSet result = new BitSet(blocks.size());

    for (int i = 0, sz = labelList.size(); i < sz; i++) {
        result.set(blocks.indexOfLabel(labelList.get(i)));
    }

    return result;
}
 
Example #28
Source File: SsaMethod.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Builds an IntList of block indices from a basic block list and a list
 * of labels taken from Rop form.
 *
 * @param ropBlocks Rop blocks
 * @param labelList list of rop block labels
 * @return IntList of block indices
 */
public static IntList indexListFromLabelList(BasicBlockList ropBlocks,
        IntList labelList) {

    IntList result = new IntList(labelList.size());

    for (int i = 0, sz = labelList.size(); i < sz; i++) {
        result.add(ropBlocks.indexOfLabel(labelList.get(i)));
    }

    return result;
}
 
Example #29
Source File: SsaBasicBlock.java    From J2ME-Loader 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 #30
Source File: SsaToRop.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * @return rop-form basic block list
 */
private BasicBlockList convertBasicBlocks() {
    ArrayList<SsaBasicBlock> blocks = ssaMeth.getBlocks();

    // Exit block may be null.
    SsaBasicBlock exitBlock = ssaMeth.getExitBlock();

    ssaMeth.computeReachability();
    int ropBlockCount = ssaMeth.getCountReachableBlocks();

    // Don't count the exit block, if it exists and is reachable.
    ropBlockCount -= (exitBlock != null && exitBlock.isReachable()) ? 1 : 0;

    BasicBlockList result = new BasicBlockList(ropBlockCount);

    // Convert all the reachable blocks except the exit block.
    int ropBlockIndex = 0;
    for (SsaBasicBlock b : blocks) {
        if (b.isReachable() && b != exitBlock) {
            result.set(ropBlockIndex++, convertBasicBlock(b));
        }
    }

    // The exit block, which is discarded, must do nothing.
    if (exitBlock != null && exitBlock.getInsns().size() != 0) {
        throw new RuntimeException(
                "Exit block must have no insns when leaving SSA form");
    }

    return result;
}