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

The following examples show how to use com.android.dx.rop.code.Insn. 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: SsaMethod.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Checks to see if the given SSA reg is ever associated with a local
 * local variable. Each SSA reg may be associated with at most one
 * local var.
 *
 * @param spec {@code non-null;} ssa reg
 * @return true if reg is ever associated with a local
 */
public boolean isRegALocal(RegisterSpec spec) {
    SsaInsn defn = getDefinitionForRegister(spec.getReg());

    if (defn == null) {
        // version 0 registers are never used as locals
        return false;
    }

    // Does the definition have a local associated with it?
    if (defn.getLocalAssignment() != null) return true;

    // If not, is there a mark-local insn?
    for (SsaInsn use : getUseListForRegister(spec.getReg())) {
        Insn insn = use.getOriginalRopInsn();

        if (insn != null
                && insn.getOpcode().getOpcode() == RegOps.MARK_LOCAL) {
            return true;
        }
    }

    return false;
}
 
Example #2
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Looks forward to the current block's primary successor, returning
 * the RegisterSpec of the result of the move-result-pseudo at the
 * top of that block or null if none.
 *
 * @return {@code null-ok;} result of move-result-pseudo at the beginning of
 * primary successor
 */
private RegisterSpec getNextMoveResultPseudo()
{
    int label = block.getPrimarySuccessor();

    if (label < 0) {
        return null;
    }

    Insn insn
            = method.getBlocks().labelToBlock(label).getInsns().get(0);

    if (insn.getOpcode().getOpcode() != RegOps.MOVE_RESULT_PSEUDO) {
        return null;
    } else {
        return insn.getResult();
    }
}
 
Example #3
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 #4
Source File: RopTranslator.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Looks forward to the current block's primary successor, returning
 * the RegisterSpec of the result of the move-result-pseudo at the
 * top of that block or null if none.
 *
 * @return {@code null-ok;} result of move-result-pseudo at the beginning of
 * primary successor
 */
private RegisterSpec getNextMoveResultPseudo()
{
    int label = block.getPrimarySuccessor();

    if (label < 0) {
        return null;
    }

    Insn insn
            = method.getBlocks().labelToBlock(label).getInsns().get(0);

    if (insn.getOpcode().getOpcode() != RegOps.MOVE_RESULT_PSEUDO) {
        return null;
    } else {
        return insn.getResult();
    }
}
 
Example #5
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 #6
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 #7
Source File: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Looks forward to the current block's primary successor, returning
 * the RegisterSpec of the result of the move-result-pseudo at the
 * top of that block or null if none.
 *
 * @return {@code null-ok;} result of move-result-pseudo at the beginning of
 * primary successor
 */
private RegisterSpec getNextMoveResultPseudo()
{
    int label = block.getPrimarySuccessor();

    if (label < 0) {
        return null;
    }

    Insn insn
            = method.getBlocks().labelToBlock(label).getInsns().get(0);

    if (insn.getOpcode().getOpcode() != RegOps.MOVE_RESULT_PSEUDO) {
        return null;
    } else {
        return insn.getResult();
    }
}
 
Example #8
Source File: RopTranslator.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Looks forward to the current block's primary successor, returning
 * the RegisterSpec of the result of the move-result-pseudo at the
 * top of that block or null if none.
 *
 * @return {@code null-ok;} result of move-result-pseudo at the beginning of
 * primary successor
 */
private RegisterSpec getNextMoveResultPseudo()
{
    int label = block.getPrimarySuccessor();

    if (label < 0) {
        return null;
    }

    Insn insn
            = method.getBlocks().labelToBlock(label).getInsns().get(0);

    if (insn.getOpcode().getOpcode() != RegOps.MOVE_RESULT_PSEUDO) {
        return null;
    } else {
        return insn.getResult();
    }
}
 
Example #9
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 #10
Source File: SCCP.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces branches that have constant conditions with gotos
 */
private void replaceBranches() {
    for (SsaInsn insn : branchWorklist) {
        // Find if a successor block is never executed
        int oldSuccessor = -1;
        SsaBasicBlock block = insn.getBlock();
        int successorSize = block.getSuccessorList().size();
        for (int i = 0; i < successorSize; i++) {
            int successorBlock = block.getSuccessorList().get(i);
            if (!executableBlocks.get(successorBlock)) {
                oldSuccessor = successorBlock;
            }
        }

        /*
         * Prune branches that have already been handled and ones that no
         * longer have constant conditions (no nonexecutable successors)
         */
        if (successorSize != 2 || oldSuccessor == -1) continue;

        // Replace branch with goto
        Insn originalRopInsn = insn.getOriginalRopInsn();
        block.replaceLastInsn(new PlainInsn(Rops.GOTO,
            originalRopInsn.getPosition(), null, RegisterSpecList.EMPTY));
        block.removeSuccessor(oldSuccessor);
    }
}
 
Example #11
Source File: RopperMachine.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param ropper {@code non-null;} ropper controlling this instance
 * @param method {@code non-null;} method being converted
 * @param advice {@code non-null;} translation advice to use
 * @param methods {@code non-null;} list of methods defined by the class
 *     that defines {@code method}.
 */
public RopperMachine(Ropper ropper, ConcreteMethod method,
        TranslationAdvice advice, MethodList methods) {
    super(method.getEffectiveDescriptor());

    if (methods == null) {
        throw new NullPointerException("methods == null");
    }

    if (ropper == null) {
        throw new NullPointerException("ropper == null");
    }

    if (advice == null) {
        throw new NullPointerException("advice == null");
    }

    this.ropper = ropper;
    this.method = method;
    this.methods = methods;
    this.advice = advice;
    this.maxLocals = method.getMaxLocals();
    this.insns = new ArrayList<Insn>(25);
    this.catches = null;
    this.catchesUsed = false;
    this.returns = false;
    this.primarySuccessorIndex = -1;
    this.extraBlockCount = 0;
    this.blockCanThrow = false;
    this.returnOp = null;
    this.returnPosition = null;
}
 
Example #12
Source File: LiteralOpUpgrader.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example #13
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 #14
Source File: RopperMachine.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param ropper {@code non-null;} ropper controlling this instance
 * @param method {@code non-null;} method being converted
 * @param advice {@code non-null;} translation advice to use
 * @param methods {@code non-null;} list of methods defined by the class
 *     that defines {@code method}.
 */
public RopperMachine(Ropper ropper, ConcreteMethod method,
        TranslationAdvice advice, MethodList methods) {
    super(method.getEffectiveDescriptor());

    if (methods == null) {
        throw new NullPointerException("methods == null");
    }

    if (ropper == null) {
        throw new NullPointerException("ropper == null");
    }

    if (advice == null) {
        throw new NullPointerException("advice == null");
    }

    this.ropper = ropper;
    this.method = method;
    this.methods = methods;
    this.advice = advice;
    this.maxLocals = method.getMaxLocals();
    this.insns = new ArrayList<Insn>(25);
    this.catches = null;
    this.catchesUsed = false;
    this.returns = false;
    this.primarySuccessorIndex = -1;
    this.extraBlockCount = 0;
    this.blockCanThrow = false;
    this.returnOp = null;
    this.returnPosition = null;
}
 
Example #15
Source File: RopTranslator.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a {@link LocalStart} to the output if the given
 * instruction in fact introduces a local variable.
 *
 * @param insn {@code non-null;} instruction in question
 */
public void addIntroductionIfNecessary(Insn insn) {
    RegisterSpec spec = locals.getAssignment(insn);

    if (spec != null) {
        addOutput(new LocalStart(insn.getPosition(), spec));
    }
}
 
Example #16
Source File: RopTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a {@link LocalStart} to the output if the given
 * instruction in fact introduces a local variable.
 *
 * @param insn {@code non-null;} instruction in question
 */
public void addIntroductionIfNecessary(Insn insn) {
    RegisterSpec spec = locals.getAssignment(insn);

    if (spec != null) {
        addOutput(new LocalStart(insn.getPosition(), spec));
    }
}
 
Example #17
Source File: SsaBasicBlock.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces the last insn in this block. The provided insn must have
 * some branchingness.
 *
 * @param insn {@code non-null;} rop-form insn to add, which must branch.
 */
public void replaceLastInsn(Insn insn) {
    if (insn.getOpcode().getBranchingness() == Rop.BRANCH_NONE) {
        throw new IllegalArgumentException("last insn must branch");
    }

    SsaInsn oldInsn = insns.get(insns.size() - 1);
    SsaInsn newInsn = SsaInsn.makeFromRop(insn, this);

    insns.set(insns.size() - 1, newInsn);

    parent.onInsnRemoved(oldInsn);
    parent.onInsnAdded(newInsn);
}
 
Example #18
Source File: LiteralOpUpgrader.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example #19
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 #20
Source File: RopTranslator.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a {@link LocalStart} to the output if the given
 * instruction in fact introduces a local variable.
 *
 * @param insn {@code non-null;} instruction in question
 */
public void addIntroductionIfNecessary(Insn insn) {
    RegisterSpec spec = locals.getAssignment(insn);

    if (spec != null) {
        addOutput(new LocalStart(insn.getPosition(), spec));
    }
}
 
Example #21
Source File: SsaBasicBlock.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces the last insn in this block. The provided insn must have
 * some branchingness.
 *
 * @param insn {@code non-null;} rop-form insn to add, which must branch.
 */
public void replaceLastInsn(Insn insn) {
    if (insn.getOpcode().getBranchingness() == Rop.BRANCH_NONE) {
        throw new IllegalArgumentException("last insn must branch");
    }

    SsaInsn oldInsn = insns.get(insns.size() - 1);
    SsaInsn newInsn = SsaInsn.makeFromRop(insn, this);

    insns.set(insns.size() - 1, newInsn);

    parent.onInsnRemoved(oldInsn);
    parent.onInsnAdded(newInsn);
}
 
Example #22
Source File: RopperMachine.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param ropper {@code non-null;} ropper controlling this instance
 * @param method {@code non-null;} method being converted
 * @param advice {@code non-null;} translation advice to use
 * @param methods {@code non-null;} list of methods defined by the class
 *     that defines {@code method}.
 */
public RopperMachine(Ropper ropper, ConcreteMethod method,
        TranslationAdvice advice, MethodList methods) {
    super(method.getEffectiveDescriptor());

    if (methods == null) {
        throw new NullPointerException("methods == null");
    }

    if (ropper == null) {
        throw new NullPointerException("ropper == null");
    }

    if (advice == null) {
        throw new NullPointerException("advice == null");
    }

    this.ropper = ropper;
    this.method = method;
    this.methods = methods;
    this.advice = advice;
    this.maxLocals = method.getMaxLocals();
    this.insns = new ArrayList<Insn>(25);
    this.catches = null;
    this.catchesUsed = false;
    this.returns = false;
    this.primarySuccessorIndex = -1;
    this.extraBlockCount = 0;
    this.blockCanThrow = false;
    this.returnOp = null;
    this.returnPosition = null;
}
 
Example #23
Source File: RopTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see if the move-param instructions that occur in this
 * method happen to slot the params in an order at the top of the
 * stack frame that matches dalvik's calling conventions. This will
 * alway result in "true" for methods that have run through the
 * SSA optimizer.
 *
 * @param paramSize size, in register units, of all the parameters
 * to this method
 */
private static boolean calculateParamsAreInOrder(RopMethod method,
        final int paramSize) {
    final boolean[] paramsAreInOrder = { true };
    final int initialRegCount = method.getBlocks().getRegCount();

    /*
     * We almost could just check the first block here, but the
     * {@code cf} layer will put in a second move-param in a
     * subsequent block in the case of synchronized methods.
     */
    method.getBlocks().forEachInsn(new Insn.BaseVisitor() {
        @Override
        public void visitPlainCstInsn(PlainCstInsn insn) {
            if (insn.getOpcode().getOpcode()== RegOps.MOVE_PARAM) {
                int param =
                    ((CstInteger) insn.getConstant()).getValue();

                paramsAreInOrder[0] = paramsAreInOrder[0]
                        && ((initialRegCount - paramSize + param)
                            == insn.getResult().getReg());
            }
        }
    });

    return paramsAreInOrder[0];
}
 
Example #24
Source File: SsaBasicBlock.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces the last insn in this block. The provided insn must have
 * some branchingness.
 *
 * @param insn {@code non-null;} rop-form insn to add, which must branch.
 */
public void replaceLastInsn(Insn insn) {
    if (insn.getOpcode().getBranchingness() == Rop.BRANCH_NONE) {
        throw new IllegalArgumentException("last insn must branch");
    }

    SsaInsn oldInsn = insns.get(insns.size() - 1);
    SsaInsn newInsn = SsaInsn.makeFromRop(insn, this);

    insns.set(insns.size() - 1, newInsn);

    parent.onInsnRemoved(oldInsn);
    parent.onInsnAdded(newInsn);
}
 
Example #25
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 #26
Source File: RopperMachine.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance.
 *
 * @param ropper {@code non-null;} ropper controlling this instance
 * @param method {@code non-null;} method being converted
 * @param advice {@code non-null;} translation advice to use
 * @param methods {@code non-null;} list of methods defined by the class
 *     that defines {@code method}.
 */
public RopperMachine(Ropper ropper, ConcreteMethod method,
        TranslationAdvice advice, MethodList methods) {
    super(method.getEffectiveDescriptor());

    if (methods == null) {
        throw new NullPointerException("methods == null");
    }

    if (ropper == null) {
        throw new NullPointerException("ropper == null");
    }

    if (advice == null) {
        throw new NullPointerException("advice == null");
    }

    this.ropper = ropper;
    this.method = method;
    this.methods = methods;
    this.advice = advice;
    this.maxLocals = method.getMaxLocals();
    this.insns = new ArrayList<Insn>(25);
    this.catches = null;
    this.catchesUsed = false;
    this.returns = false;
    this.primarySuccessorIndex = -1;
    this.extraBlockCount = 0;
    this.blockCanThrow = false;
    this.returnOp = null;
    this.returnPosition = null;
}
 
Example #27
Source File: LiteralOpUpgrader.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Tries to replace an instruction with a const instruction. The given
 * instruction must have a constant result for it to be replaced.
 *
 * @param insn {@code non-null;} instruction to try to replace
 * @return true if the instruction was replaced
 */
private boolean tryReplacingWithConstant(NormalSsaInsn insn) {
    Insn originalRopInsn = insn.getOriginalRopInsn();
    Rop opcode = originalRopInsn.getOpcode();
    RegisterSpec result = insn.getResult();

    if (result != null && !ssaMeth.isRegALocal(result) &&
            opcode.getOpcode() != RegOps.CONST) {
        TypeBearer type = insn.getResult().getTypeBearer();
        if (type.isConstant() && type.getBasicType() == Type.BT_INT) {
            // Replace the instruction with a constant
            replacePlainInsn(insn, RegisterSpecList.EMPTY,
                    RegOps.CONST, (Constant) type);

            // Remove the source as well if this is a move-result-pseudo
            if (opcode.getOpcode() == RegOps.MOVE_RESULT_PSEUDO) {
                int pred = insn.getBlock().getPredecessors().nextSetBit(0);
                ArrayList<SsaInsn> predInsns =
                        ssaMeth.getBlocks().get(pred).getInsns();
                NormalSsaInsn sourceInsn =
                        (NormalSsaInsn) predInsns.get(predInsns.size()-1);
                replacePlainInsn(sourceInsn, RegisterSpecList.EMPTY,
                        RegOps.GOTO, null);
            }
            return true;
        }
    }
    return false;
}
 
Example #28
Source File: RopTranslator.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Checks to see if the move-param instructions that occur in this
 * method happen to slot the params in an order at the top of the
 * stack frame that matches dalvik's calling conventions. This will
 * alway result in "true" for methods that have run through the
 * SSA optimizer.
 *
 * @param paramSize size, in register units, of all the parameters
 * to this method
 */
private static boolean calculateParamsAreInOrder(RopMethod method,
        final int paramSize) {
    final boolean[] paramsAreInOrder = { true };
    final int initialRegCount = method.getBlocks().getRegCount();

    /*
     * We almost could just check the first block here, but the
     * {@code cf} layer will put in a second move-param in a
     * subsequent block in the case of synchronized methods.
     */
    method.getBlocks().forEachInsn(new Insn.BaseVisitor() {
        @Override
        public void visitPlainCstInsn(PlainCstInsn insn) {
            if (insn.getOpcode().getOpcode()== RegOps.MOVE_PARAM) {
                int param =
                    ((CstInteger) insn.getConstant()).getValue();

                paramsAreInOrder[0] = paramsAreInOrder[0]
                        && ((initialRegCount - paramSize + param)
                            == insn.getResult().getReg());
            }
        }
    });

    return paramsAreInOrder[0];
}
 
Example #29
Source File: NormalSsaInsn.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public Insn getOriginalRopInsn() {
    return insn;
}
 
Example #30
Source File: SsaMethod.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Deletes all insns in the set from this method.
 *
 * @param deletedInsns {@code non-null;} insns to delete
 */
public void deleteInsns(Set<SsaInsn> deletedInsns) {
    for (SsaInsn deletedInsn : deletedInsns) {
        SsaBasicBlock block = deletedInsn.getBlock();
        ArrayList<SsaInsn> insns = block.getInsns();

        for (int i = insns.size() - 1; i >= 0; i--) {
            SsaInsn insn = insns.get(i);

            if (deletedInsn == insn) {
                onInsnRemoved(insn);
                insns.remove(i);
                break;
            }
        }

        // Check to see if we need to add a GOTO

        int insnsSz = insns.size();
        SsaInsn lastInsn = (insnsSz == 0) ? null : insns.get(insnsSz - 1);

        if (block != getExitBlock() && (insnsSz == 0
                || lastInsn.getOriginalRopInsn() == null
                || lastInsn.getOriginalRopInsn().getOpcode()
                    .getBranchingness() == Rop.BRANCH_NONE)) {
            // We managed to eat a throwable insn

            Insn gotoInsn = new PlainInsn(Rops.GOTO,
                    SourcePosition.NO_INFO, null, RegisterSpecList.EMPTY);
            insns.add(SsaInsn.makeFromRop(gotoInsn, block));

            // Remove secondary successors from this block
            BitSet succs = block.getSuccessors();
            for (int i = succs.nextSetBit(0); i >= 0;
                     i = succs.nextSetBit(i + 1)) {
                if (i != block.getPrimarySuccessorIndex()) {
                    block.removeSuccessor(i);
                }
            }
        }
    }
}