Search in sources :

Example 26 with InsnArg

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

the class TestIfCondition method testNormalize.

@Test
public void testNormalize() {
    // 'a != false' => 'a == true'
    InsnArg a = mockArg();
    IfCondition c = makeCondition(IfOp.NE, a, LiteralArg.FALSE);
    IfCondition simp = simplify(c);
    assertEquals(simp.getMode(), Mode.COMPARE);
    Compare compare = simp.getCompare();
    assertEquals(compare.getA(), a);
    assertEquals(compare.getB(), LiteralArg.TRUE);
}
Also used : InsnArg(jadx.core.dex.instructions.args.InsnArg) Compare(jadx.core.dex.regions.conditions.Compare) IfCondition(jadx.core.dex.regions.conditions.IfCondition) Test(org.junit.Test)

Example 27 with InsnArg

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

the class NameGen method makeNameFromInsn.

private String makeNameFromInsn(InsnNode insn) {
    switch(insn.getType()) {
        case INVOKE:
            InvokeNode inv = (InvokeNode) insn;
            return makeNameFromInvoke(inv.getCallMth());
        case CONSTRUCTOR:
            ConstructorInsn co = (ConstructorInsn) insn;
            return makeNameForObject(co.getClassType().getType());
        case ARRAY_LENGTH:
            return "length";
        case ARITH:
        case TERNARY:
        case CAST:
            for (InsnArg arg : insn.getArguments()) {
                if (arg.isInsnWrap()) {
                    InsnNode wrapInsn = ((InsnWrapArg) arg).getWrapInsn();
                    String wName = makeNameFromInsn(wrapInsn);
                    if (wName != null) {
                        return wName;
                    }
                }
            }
            break;
        default:
            break;
    }
    return null;
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) InvokeNode(jadx.core.dex.instructions.InvokeNode) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg) ConstructorInsn(jadx.core.dex.instructions.mods.ConstructorInsn)

Example 28 with InsnArg

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

the class RegionGen method makeSwitch.

private CodeWriter makeSwitch(SwitchRegion sw, CodeWriter code) throws CodegenException {
    SwitchNode insn = (SwitchNode) sw.getHeader().getInstructions().get(0);
    InsnArg arg = insn.getArg(0);
    code.startLine("switch (");
    addArg(code, arg, false);
    code.add(") {");
    code.incIndent();
    int size = sw.getKeys().size();
    for (int i = 0; i < size; i++) {
        List<Object> keys = sw.getKeys().get(i);
        IContainer c = sw.getCases().get(i);
        for (Object k : keys) {
            code.startLine("case ");
            if (k instanceof FieldNode) {
                FieldNode fn = (FieldNode) k;
                if (fn.getParentClass().isEnum()) {
                    code.add(fn.getAlias());
                } else {
                    staticField(code, fn.getFieldInfo());
                    // print original value, sometimes replace with incorrect field
                    FieldInitAttr valueAttr = fn.get(AType.FIELD_INIT);
                    if (valueAttr != null && valueAttr.getValue() != null) {
                        code.add(" /*").add(valueAttr.getValue().toString()).add("*/");
                    }
                }
            } else if (k instanceof Integer) {
                code.add(TypeGen.literalToString((Integer) k, arg.getType(), mth));
            } else {
                throw new JadxRuntimeException("Unexpected key in switch: " + (k != null ? k.getClass() : null));
            }
            code.add(':');
        }
        makeRegionIndent(code, c);
    }
    if (sw.getDefaultCase() != null) {
        code.startLine("default:");
        makeRegionIndent(code, sw.getDefaultCase());
    }
    code.decIndent();
    code.startLine('}');
    return code;
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IContainer(jadx.core.dex.nodes.IContainer) SwitchNode(jadx.core.dex.instructions.SwitchNode) FieldInitAttr(jadx.core.dex.nodes.parser.FieldInitAttr)

Example 29 with InsnArg

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

the class RegionGen method makeCatchBlock.

private void makeCatchBlock(CodeWriter code, ExceptionHandler handler) throws CodegenException {
    IContainer region = handler.getHandlerRegion();
    if (region == null) {
        return;
    }
    code.startLine("} catch (");
    InsnArg arg = handler.getArg();
    if (arg instanceof RegisterArg) {
        declareVar(code, (RegisterArg) arg);
    } else if (arg instanceof NamedArg) {
        if (handler.isCatchAll()) {
            code.add("Throwable");
        } else {
            useClass(code, handler.getCatchType());
        }
        code.add(' ');
        code.add(mgen.getNameGen().assignNamedArg((NamedArg) arg));
    }
    code.add(") {");
    makeRegionIndent(code, region);
}
Also used : RegisterArg(jadx.core.dex.instructions.args.RegisterArg) InsnArg(jadx.core.dex.instructions.args.InsnArg) NamedArg(jadx.core.dex.instructions.args.NamedArg) IContainer(jadx.core.dex.nodes.IContainer)

Example 30 with InsnArg

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

the class InsnGen method inlineMethod.

private boolean inlineMethod(MethodNode callMthNode, InvokeNode insn, CodeWriter code) throws CodegenException {
    MethodInlineAttr mia = callMthNode.get(AType.METHOD_INLINE);
    if (mia == null) {
        return false;
    }
    InsnNode inl = mia.getInsn();
    if (callMthNode.getMethodInfo().getArgumentsTypes().isEmpty()) {
        makeInsn(inl, code, Flags.BODY_ONLY);
    } else {
        // remap args
        InsnArg[] regs = new InsnArg[callMthNode.getRegsCount()];
        List<RegisterArg> callArgs = callMthNode.getArguments(true);
        for (int i = 0; i < callArgs.size(); i++) {
            InsnArg arg = insn.getArg(i);
            RegisterArg callArg = callArgs.get(i);
            regs[callArg.getRegNum()] = arg;
        }
        // replace args
        InsnNode inlCopy = inl.copy();
        List<RegisterArg> inlArgs = new ArrayList<RegisterArg>();
        inlCopy.getRegisterArgs(inlArgs);
        for (RegisterArg r : inlArgs) {
            int regNum = r.getRegNum();
            if (regNum >= regs.length) {
                LOG.warn("Unknown register number {} in method call: {} from {}", r, callMthNode, mth);
            } else {
                InsnArg repl = regs[regNum];
                if (repl == null) {
                    LOG.warn("Not passed register {} in method call: {} from {}", r, callMthNode, mth);
                } else {
                    inlCopy.replaceArg(r, repl);
                }
            }
        }
        makeInsn(inlCopy, code, Flags.BODY_ONLY);
    }
    return true;
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) InsnArg(jadx.core.dex.instructions.args.InsnArg) ArrayList(java.util.ArrayList) MethodInlineAttr(jadx.core.dex.attributes.nodes.MethodInlineAttr)

Aggregations

InsnArg (jadx.core.dex.instructions.args.InsnArg)52 InsnNode (jadx.core.dex.nodes.InsnNode)32 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)24 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)19 InsnWrapArg (jadx.core.dex.instructions.args.InsnWrapArg)15 ArgType (jadx.core.dex.instructions.args.ArgType)9 SSAVar (jadx.core.dex.instructions.args.SSAVar)9 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)8 BlockNode (jadx.core.dex.nodes.BlockNode)8 FieldNode (jadx.core.dex.nodes.FieldNode)7 FieldInfo (jadx.core.dex.info.FieldInfo)6 ArithNode (jadx.core.dex.instructions.ArithNode)5 InvokeNode (jadx.core.dex.instructions.InvokeNode)5 ConstClassNode (jadx.core.dex.instructions.ConstClassNode)4 PhiInsn (jadx.core.dex.instructions.PhiInsn)4 ClassNode (jadx.core.dex.nodes.ClassNode)4 PhiListAttr (jadx.core.dex.attributes.nodes.PhiListAttr)3 MethodInfo (jadx.core.dex.info.MethodInfo)3 SwitchNode (jadx.core.dex.instructions.SwitchNode)3 MethodNode (jadx.core.dex.nodes.MethodNode)3