Search in sources :

Example 1 with ArgType

use of jadx.core.dex.instructions.args.ArgType in project jadx by skylot.

the class BlockExceptionHandler method markExceptionHandlers.

/**
	 * Set exception handler attribute for whole block
	 */
private static void markExceptionHandlers(BlockNode block) {
    if (block.getInstructions().isEmpty()) {
        return;
    }
    InsnNode me = block.getInstructions().get(0);
    ExcHandlerAttr handlerAttr = me.get(AType.EXC_HANDLER);
    if (handlerAttr == null || me.getType() != InsnType.MOVE_EXCEPTION) {
        return;
    }
    ExceptionHandler excHandler = handlerAttr.getHandler();
    block.addAttr(handlerAttr);
    // set correct type for 'move-exception' operation
    ArgType type = excHandler.isCatchAll() ? ArgType.THROWABLE : excHandler.getCatchType().getType();
    RegisterArg resArg = me.getResult();
    resArg = InsnArg.reg(resArg.getRegNum(), type);
    me.setResult(resArg);
    me.add(AFlag.DONT_INLINE);
    excHandler.setArg(resArg);
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) ArgType(jadx.core.dex.instructions.args.ArgType) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) ExcHandlerAttr(jadx.core.dex.trycatch.ExcHandlerAttr)

Example 2 with ArgType

use of jadx.core.dex.instructions.args.ArgType in project jadx by skylot.

the class ModVisitor method processMoveException.

private static void processMoveException(MethodNode mth, BlockNode block, InsnNode insn, InstructionRemover remover) {
    ExcHandlerAttr excHandlerAttr = block.get(AType.EXC_HANDLER);
    if (excHandlerAttr == null) {
        return;
    }
    ExceptionHandler excHandler = excHandlerAttr.getHandler();
    // result arg used both in this insn and exception handler,
    RegisterArg resArg = insn.getResult();
    ArgType type = excHandler.isCatchAll() ? ArgType.THROWABLE : excHandler.getCatchType().getType();
    String name = excHandler.isCatchAll() ? "th" : "e";
    if (resArg.getName() == null) {
        resArg.setName(name);
    }
    SSAVar sVar = insn.getResult().getSVar();
    if (sVar.getUseCount() == 0) {
        excHandler.setArg(new NamedArg(name, type));
        remover.add(insn);
    } else if (sVar.isUsedInPhi()) {
        // exception var moved to external variable => replace with 'move' insn
        InsnNode moveInsn = new InsnNode(InsnType.MOVE, 1);
        moveInsn.setResult(insn.getResult());
        NamedArg namedArg = new NamedArg(name, type);
        moveInsn.addArg(namedArg);
        excHandler.setArg(namedArg);
        replaceInsn(block, 0, moveInsn);
    }
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) ArgType(jadx.core.dex.instructions.args.ArgType) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) ExcHandlerAttr(jadx.core.dex.trycatch.ExcHandlerAttr) SSAVar(jadx.core.dex.instructions.args.SSAVar) NamedArg(jadx.core.dex.instructions.args.NamedArg)

Example 3 with ArgType

use of jadx.core.dex.instructions.args.ArgType in project jadx by skylot.

the class ModVisitor method replaceStep.

private static void replaceStep(MethodNode mth, InstructionRemover remover) {
    ClassNode parentClass = mth.getParentClass();
    for (BlockNode block : mth.getBasicBlocks()) {
        remover.setBlock(block);
        int size = block.getInstructions().size();
        for (int i = 0; i < size; i++) {
            InsnNode insn = block.getInstructions().get(i);
            switch(insn.getType()) {
                case INVOKE:
                    processInvoke(mth, block, i, remover);
                    break;
                case CONST:
                case CONST_STR:
                case CONST_CLASS:
                    {
                        FieldNode f;
                        if (insn.getType() == InsnType.CONST_STR) {
                            String s = ((ConstStringNode) insn).getString();
                            f = parentClass.getConstField(s);
                        } else if (insn.getType() == InsnType.CONST_CLASS) {
                            ArgType t = ((ConstClassNode) insn).getClsType();
                            f = parentClass.getConstField(t);
                        } else {
                            f = parentClass.getConstFieldByLiteralArg((LiteralArg) insn.getArg(0));
                        }
                        if (f != null) {
                            InsnNode inode = new IndexInsnNode(InsnType.SGET, f.getFieldInfo(), 0);
                            inode.setResult(insn.getResult());
                            replaceInsn(block, i, inode);
                        }
                        break;
                    }
                case SWITCH:
                    SwitchNode sn = (SwitchNode) insn;
                    for (int k = 0; k < sn.getCasesCount(); k++) {
                        FieldNode f = parentClass.getConstField(sn.getKeys()[k]);
                        if (f != null) {
                            sn.getKeys()[k] = f;
                        }
                    }
                    break;
                case NEW_ARRAY:
                    // create array in 'fill-array' instruction
                    int next = i + 1;
                    if (next < size) {
                        InsnNode ni = block.getInstructions().get(next);
                        if (ni.getType() == InsnType.FILL_ARRAY) {
                            ni.getResult().merge(mth.dex(), insn.getResult());
                            ArgType arrType = ((NewArrayNode) insn).getArrayType();
                            ((FillArrayNode) ni).mergeElementType(mth.dex(), arrType.getArrayElement());
                            remover.add(insn);
                        }
                    }
                    break;
                case FILL_ARRAY:
                    InsnNode filledArr = makeFilledArrayInsn(mth, (FillArrayNode) insn);
                    replaceInsn(block, i, filledArr);
                    break;
                case MOVE_EXCEPTION:
                    processMoveException(mth, block, insn, remover);
                    break;
                case ARITH:
                    ArithNode arithNode = (ArithNode) insn;
                    if (arithNode.getArgsCount() == 2) {
                        InsnArg litArg = arithNode.getArg(1);
                        if (litArg.isLiteral()) {
                            FieldNode f = parentClass.getConstFieldByLiteralArg((LiteralArg) litArg);
                            if (f != null) {
                                InsnNode fGet = new IndexInsnNode(InsnType.SGET, f.getFieldInfo(), 0);
                                insn.replaceArg(litArg, InsnArg.wrapArg(fGet));
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }
        remover.perform();
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) ArgType(jadx.core.dex.instructions.args.ArgType) ClassNode(jadx.core.dex.nodes.ClassNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) FieldNode(jadx.core.dex.nodes.FieldNode) NewArrayNode(jadx.core.dex.instructions.NewArrayNode) FilledNewArrayNode(jadx.core.dex.instructions.FilledNewArrayNode) LiteralArg(jadx.core.dex.instructions.args.LiteralArg) ArithNode(jadx.core.dex.instructions.ArithNode) SwitchNode(jadx.core.dex.instructions.SwitchNode) FillArrayNode(jadx.core.dex.instructions.FillArrayNode) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode)

Example 4 with ArgType

use of jadx.core.dex.instructions.args.ArgType in project jadx by skylot.

the class ReSugarCode method processNewArray.

/**
	 * Replace new array and sequence of array-put to new filled-array instruction.
	 */
private static InsnNode processNewArray(MethodNode mth, List<InsnNode> instructions, int i, InstructionRemover remover) {
    NewArrayNode newArrayInsn = (NewArrayNode) instructions.get(i);
    InsnArg arg = newArrayInsn.getArg(0);
    if (!arg.isLiteral()) {
        return null;
    }
    int len = (int) ((LiteralArg) arg).getLiteral();
    int size = instructions.size();
    if (len <= 0 || i + len >= size || instructions.get(i + len).getType() != InsnType.APUT) {
        return null;
    }
    ArgType arrType = newArrayInsn.getArrayType();
    InsnNode filledArr = new FilledNewArrayNode(arrType.getArrayElement(), len);
    filledArr.setResult(newArrayInsn.getResult());
    for (int j = 0; j < len; j++) {
        InsnNode put = instructions.get(i + 1 + j);
        if (put.getType() != InsnType.APUT) {
            LOG.debug("Not a APUT in expected new filled array: {}, method: {}", put, mth);
            return null;
        }
        filledArr.addArg(put.getArg(2));
        remover.add(put);
    }
    return filledArr;
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) NewArrayNode(jadx.core.dex.instructions.NewArrayNode) FilledNewArrayNode(jadx.core.dex.instructions.FilledNewArrayNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) FilledNewArrayNode(jadx.core.dex.instructions.FilledNewArrayNode)

Example 5 with ArgType

use of jadx.core.dex.instructions.args.ArgType in project jadx by skylot.

the class ConstInlineVisitor method checkInsn.

private static boolean checkInsn(MethodNode mth, InsnNode insn) {
    if (insn.getType() != InsnType.CONST || insn.contains(AFlag.DONT_INLINE)) {
        return false;
    }
    InsnArg arg = insn.getArg(0);
    if (!arg.isLiteral()) {
        return false;
    }
    long lit = ((LiteralArg) arg).getLiteral();
    SSAVar sVar = insn.getResult().getSVar();
    if (lit == 0 && checkObjectInline(sVar)) {
        if (sVar.getUseCount() == 1) {
            InsnNode assignInsn = insn.getResult().getAssignInsn();
            if (assignInsn != null) {
                assignInsn.add(AFlag.DONT_INLINE);
            }
        }
        return false;
    }
    ArgType resType = insn.getResult().getType();
    // make sure arg has correct type
    if (!arg.getType().isTypeKnown()) {
        arg.merge(mth.dex(), resType);
    }
    return replaceConst(mth, insn, lit);
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) SSAVar(jadx.core.dex.instructions.args.SSAVar) InsnArg(jadx.core.dex.instructions.args.InsnArg) LiteralArg(jadx.core.dex.instructions.args.LiteralArg)

Aggregations

ArgType (jadx.core.dex.instructions.args.ArgType)48 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)12 InsnNode (jadx.core.dex.nodes.InsnNode)11 InsnArg (jadx.core.dex.instructions.args.InsnArg)9 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)8 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)6 SSAVar (jadx.core.dex.instructions.args.SSAVar)6 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)5 ArrayList (java.util.ArrayList)5 FieldNode (jadx.core.dex.nodes.FieldNode)4 MethodNode (jadx.core.dex.nodes.MethodNode)4 Annotation (jadx.core.dex.attributes.annotations.Annotation)3 FieldInfo (jadx.core.dex.info.FieldInfo)3 FilledNewArrayNode (jadx.core.dex.instructions.FilledNewArrayNode)3 InvokeNode (jadx.core.dex.instructions.InvokeNode)3 BlockNode (jadx.core.dex.nodes.BlockNode)3 ClassNode (jadx.core.dex.nodes.ClassNode)3 DexNode (jadx.core.dex.nodes.DexNode)3 List (java.util.List)3 MethodInfo (jadx.core.dex.info.MethodInfo)2