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

The following examples show how to use com.android.dx.rop.code.InsnList. 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: Label.java    From dexmaker with Apache License 2.0 6 votes vote down vote up
BasicBlock toBasicBlock() {
    InsnList result = new InsnList(instructions.size());
    for (int i = 0; i < instructions.size(); i++) {
        result.set(i, instructions.get(i));
    }
    result.setImmutable();

    int primarySuccessorIndex = -1;
    IntList successors = new IntList();
    for (Label catchLabel : catchLabels) {
        successors.add(catchLabel.id);
    }
    if (primarySuccessor != null) {
        primarySuccessorIndex = primarySuccessor.id;
        successors.add(primarySuccessorIndex);
    }
    if (alternateSuccessor != null) {
        successors.add(alternateSuccessor.id);
    }
    successors.setImmutable();

    return new BasicBlock(id, result, successors, primarySuccessorIndex);
}
 
Example #2
Source File: Label.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
BasicBlock toBasicBlock() {
    InsnList result = new InsnList(instructions.size());
    for (int i = 0; i < instructions.size(); i++) {
        result.set(i, instructions.get(i));
    }
    result.setImmutable();

    int primarySuccessorIndex = -1;
    IntList successors = new IntList();
    for (Label catchLabel : catchLabels) {
        successors.add(catchLabel.id);
    }
    if (primarySuccessor != null) {
        primarySuccessorIndex = primarySuccessor.id;
        successors.add(primarySuccessorIndex);
    }
    if (alternateSuccessor != null) {
        successors.add(alternateSuccessor.id);
    }
    successors.setImmutable();

    return new BasicBlock(id, result, successors, primarySuccessorIndex);
}
 
Example #3
Source File: SsaToRop.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an insn list to rop form.
 *
 * @param ssaInsns {@code non-null;} old instructions
 * @return {@code non-null;} immutable instruction list
 */
private InsnList convertInsns(ArrayList<SsaInsn> ssaInsns) {
    int insnCount = ssaInsns.size();
    InsnList result = new InsnList(insnCount);

    for (int i = 0; i < insnCount; i++) {
        result.set(i, ssaInsns.get(i).toRopInsn());
    }

    result.setImmutable();

    return result;
}
 
Example #4
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 #5
Source File: SsaToRop.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an insn list to rop form.
 *
 * @param ssaInsns {@code non-null;} old instructions
 * @return {@code non-null;} immutable instruction list
 */
private InsnList convertInsns(ArrayList<SsaInsn> ssaInsns) {
    int insnCount = ssaInsns.size();
    InsnList result = new InsnList(insnCount);

    for (int i = 0; i < insnCount; i++) {
        result.set(i, ssaInsns.get(i).toRopInsn());
    }

    result.setImmutable();

    return result;
}
 
Example #6
Source File: Ropper.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all {@code move-return-address} instructions, returning a new
 * {@code InsnList} if necessary. The {@code move-return-address}
 * insns are dead code after subroutines have been inlined.
 *
 * @param insns {@code InsnList} that may contain
 * {@code move-return-address} insns
 * @return {@code InsnList} with {@code move-return-address} removed
 */
private InsnList filterMoveReturnAddressInsns(InsnList insns) {
    int sz;
    int newSz = 0;

    // First see if we need to filter, and if so what the new size will be
    sz = insns.size();
    for (int i = 0; i < sz; i++) {
        if (insns.get(i).getOpcode() != Rops.MOVE_RETURN_ADDRESS) {
            newSz++;
        }
    }

    if (newSz == sz) {
        return insns;
    }

    // Make a new list without the MOVE_RETURN_ADDRESS insns
    InsnList newInsns = new InsnList(newSz);

    int newIndex = 0;
    for (int i = 0; i < sz; i++) {
        Insn insn = insns.get(i);
        if (insn.getOpcode() != Rops.MOVE_RETURN_ADDRESS) {
            newInsns.set(newIndex++, insn);
        }
    }

    newInsns.setImmutable();
    return newInsns;
}
 
Example #7
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 #8
Source File: SsaToRop.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an insn list to rop form.
 *
 * @param ssaInsns {@code non-null;} old instructions
 * @return {@code non-null;} immutable instruction list
 */
private InsnList convertInsns(ArrayList<SsaInsn> ssaInsns) {
    int insnCount = ssaInsns.size();
    InsnList result = new InsnList(insnCount);

    for (int i = 0; i < insnCount; i++) {
        result.set(i, ssaInsns.get(i).toRopInsn());
    }

    result.setImmutable();

    return result;
}
 
Example #9
Source File: Ropper.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all {@code move-return-address} instructions, returning a new
 * {@code InsnList} if necessary. The {@code move-return-address}
 * insns are dead code after subroutines have been inlined.
 *
 * @param insns {@code InsnList} that may contain
 * {@code move-return-address} insns
 * @return {@code InsnList} with {@code move-return-address} removed
 */
private InsnList filterMoveReturnAddressInsns(InsnList insns) {
    int sz;
    int newSz = 0;

    // First see if we need to filter, and if so what the new size will be
    sz = insns.size();
    for (int i = 0; i < sz; i++) {
        if (insns.get(i).getOpcode() != Rops.MOVE_RETURN_ADDRESS) {
            newSz++;
        }
    }

    if (newSz == sz) {
        return insns;
    }

    // Make a new list without the MOVE_RETURN_ADDRESS insns
    InsnList newInsns = new InsnList(newSz);

    int newIndex = 0;
    for (int i = 0; i < sz; i++) {
        Insn insn = insns.get(i);
        if (insn.getOpcode() != Rops.MOVE_RETURN_ADDRESS) {
            newInsns.set(newIndex++, insn);
        }
    }

    newInsns.setImmutable();
    return newInsns;
}
 
Example #10
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 #11
Source File: SsaToRop.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an insn list to rop form.
 *
 * @param ssaInsns {@code non-null;} old instructions
 * @return {@code non-null;} immutable instruction list
 */
private InsnList convertInsns(ArrayList<SsaInsn> ssaInsns) {
    int insnCount = ssaInsns.size();
    InsnList result = new InsnList(insnCount);

    for (int i = 0; i < insnCount; i++) {
        result.set(i, ssaInsns.get(i).toRopInsn());
    }

    result.setImmutable();

    return result;
}
 
Example #12
Source File: Ropper.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all {@code move-return-address} instructions, returning a new
 * {@code InsnList} if necessary. The {@code move-return-address}
 * insns are dead code after subroutines have been inlined.
 *
 * @param insns {@code InsnList} that may contain
 * {@code move-return-address} insns
 * @return {@code InsnList} with {@code move-return-address} removed
 */
private InsnList filterMoveReturnAddressInsns(InsnList insns) {
    int sz;
    int newSz = 0;

    // First see if we need to filter, and if so what the new size will be
    sz = insns.size();
    for (int i = 0; i < sz; i++) {
        if (insns.get(i).getOpcode() != Rops.MOVE_RETURN_ADDRESS) {
            newSz++;
        }
    }

    if (newSz == sz) {
        return insns;
    }

    // Make a new list without the MOVE_RETURN_ADDRESS insns
    InsnList newInsns = new InsnList(newSz);

    int newIndex = 0;
    for (int i = 0; i < sz; i++) {
        Insn insn = insns.get(i);
        if (insn.getOpcode() != Rops.MOVE_RETURN_ADDRESS) {
            newInsns.set(newIndex++, insn);
        }
    }

    newInsns.setImmutable();
    return newInsns;
}
 
Example #13
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 #14
Source File: Ropper.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Removes all {@code move-return-address} instructions, returning a new
 * {@code InsnList} if necessary. The {@code move-return-address}
 * insns are dead code after subroutines have been inlined.
 *
 * @param insns {@code InsnList} that may contain
 * {@code move-return-address} insns
 * @return {@code InsnList} with {@code move-return-address} removed
 */
private InsnList filterMoveReturnAddressInsns(InsnList insns) {
    int sz;
    int newSz = 0;

    // First see if we need to filter, and if so what the new size will be
    sz = insns.size();
    for (int i = 0; i < sz; i++) {
        if (insns.get(i).getOpcode() != Rops.MOVE_RETURN_ADDRESS) {
            newSz++;
        }
    }

    if (newSz == sz) {
        return insns;
    }

    // Make a new list without the MOVE_RETURN_ADDRESS insns
    InsnList newInsns = new InsnList(newSz);

    int newIndex = 0;
    for (int i = 0; i < sz; i++) {
        Insn insn = insns.get(i);
        if (insn.getOpcode() != Rops.MOVE_RETURN_ADDRESS) {
            newInsns.set(newIndex++, insn);
        }
    }

    newInsns.setImmutable();
    return newInsns;
}
 
Example #15
Source File: BlockDumper.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Does a registerizing dump.
 *
 * @param meth {@code non-null;} method data to dump
 */
private void ropDump(ConcreteMethod meth) {
    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    BytecodeArray code = meth.getCode();
    ByteArray bytes = code.getBytes();
    RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods(), dexOptions);
    StringBuilder sb = new StringBuilder(2000);

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

    BasicBlockList blocks = rmeth.getBlocks();
    int[] order = blocks.getLabelsInOrder();

    sb.append("first " + Hex.u2(rmeth.getFirstLabel()) + "\n");

    for (int label : order) {
        BasicBlock bb = blocks.get(blocks.indexOfLabel(label));
        sb.append("block ");
        sb.append(Hex.u2(label));
        sb.append("\n");

        IntList preds = rmeth.labelToPredecessors(label);
        int psz = preds.size();
        for (int i = 0; i < psz; i++) {
            sb.append("  pred ");
            sb.append(Hex.u2(preds.get(i)));
            sb.append("\n");
        }

        InsnList il = bb.getInsns();
        int ilsz = il.size();
        for (int i = 0; i < ilsz; i++) {
            Insn one = il.get(i);
            sb.append("  ");
            sb.append(il.get(i).toHuman());
            sb.append("\n");
        }

        IntList successors = bb.getSuccessors();
        int ssz = successors.size();
        if (ssz == 0) {
            sb.append("  returns\n");
        } else {
            int primary = bb.getPrimarySuccessor();
            for (int i = 0; i < ssz; i++) {
                int succ = successors.get(i);
                sb.append("  next ");
                sb.append(Hex.u2(succ));

                if ((ssz != 1) && (succ == primary)) {
                    sb.append(" *");
                }

                sb.append("\n");
            }
        }
    }

    suppressDump = false;
    parsed(bytes, 0, bytes.size(), sb.toString());
    suppressDump = true;
}
 
Example #16
Source File: BlockDumper.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Does a registerizing dump.
 *
 * @param meth {@code non-null;} method data to dump
 */
private void ropDump(ConcreteMethod meth) {
    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    BytecodeArray code = meth.getCode();
    ByteArray bytes = code.getBytes();
    RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods(), dexOptions);
    StringBuilder sb = new StringBuilder(2000);

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

    BasicBlockList blocks = rmeth.getBlocks();
    int[] order = blocks.getLabelsInOrder();

    sb.append("first " + Hex.u2(rmeth.getFirstLabel()) + "\n");

    for (int label : order) {
        BasicBlock bb = blocks.get(blocks.indexOfLabel(label));
        sb.append("block ");
        sb.append(Hex.u2(label));
        sb.append("\n");

        IntList preds = rmeth.labelToPredecessors(label);
        int psz = preds.size();
        for (int i = 0; i < psz; i++) {
            sb.append("  pred ");
            sb.append(Hex.u2(preds.get(i)));
            sb.append("\n");
        }

        InsnList il = bb.getInsns();
        int ilsz = il.size();
        for (int i = 0; i < ilsz; i++) {
            Insn one = il.get(i);
            sb.append("  ");
            sb.append(il.get(i).toHuman());
            sb.append("\n");
        }

        IntList successors = bb.getSuccessors();
        int ssz = successors.size();
        if (ssz == 0) {
            sb.append("  returns\n");
        } else {
            int primary = bb.getPrimarySuccessor();
            for (int i = 0; i < ssz; i++) {
                int succ = successors.get(i);
                sb.append("  next ");
                sb.append(Hex.u2(succ));

                if ((ssz != 1) && (succ == primary)) {
                    sb.append(" *");
                }

                sb.append("\n");
            }
        }
    }

    suppressDump = false;
    parsed(bytes, 0, bytes.size(), sb.toString());
    suppressDump = true;
}
 
Example #17
Source File: BlockDumper.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Does a registerizing dump.
 *
 * @param meth {@code non-null;} method data to dump
 */
private void ropDump(ConcreteMethod meth) {
    TranslationAdvice advice = DexTranslationAdvice.THE_ONE;
    BytecodeArray code = meth.getCode();
    ByteArray bytes = code.getBytes();
    RopMethod rmeth = Ropper.convert(meth, advice, classFile.getMethods());
    StringBuffer sb = new StringBuffer(2000);

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

    BasicBlockList blocks = rmeth.getBlocks();
    int[] order = blocks.getLabelsInOrder();

    sb.append("first " + Hex.u2(rmeth.getFirstLabel()) + "\n");

    for (int label : order) {
        BasicBlock bb = blocks.get(blocks.indexOfLabel(label));
        sb.append("block ");
        sb.append(Hex.u2(label));
        sb.append("\n");

        IntList preds = rmeth.labelToPredecessors(label);
        int psz = preds.size();
        for (int i = 0; i < psz; i++) {
            sb.append("  pred ");
            sb.append(Hex.u2(preds.get(i)));
            sb.append("\n");
        }

        InsnList il = bb.getInsns();
        int ilsz = il.size();
        for (int i = 0; i < ilsz; i++) {
            Insn one = il.get(i);
            sb.append("  ");
            sb.append(il.get(i).toHuman());
            sb.append("\n");
        }

        IntList successors = bb.getSuccessors();
        int ssz = successors.size();
        if (ssz == 0) {
            sb.append("  returns\n");
        } else {
            int primary = bb.getPrimarySuccessor();
            for (int i = 0; i < ssz; i++) {
                int succ = successors.get(i);
                sb.append("  next ");
                sb.append(Hex.u2(succ));

                if ((ssz != 1) && (succ == primary)) {
                    sb.append(" *");
                }

                sb.append("\n");
            }
        }
    }

    suppressDump = false;
    setAt(bytes, 0);
    parsed(bytes, 0, bytes.size(), sb.toString());
    suppressDump = true;
}