Search in sources :

Example 71 with InsnNode

use of jadx.core.dex.nodes.InsnNode in project jadx by skylot.

the class ModVisitor method processInvoke.

private static void processInvoke(MethodNode mth, BlockNode block, int insnNumber, InstructionRemover remover) {
    ClassNode parentClass = mth.getParentClass();
    InsnNode insn = block.getInstructions().get(insnNumber);
    InvokeNode inv = (InvokeNode) insn;
    MethodInfo callMth = inv.getCallMth();
    if (!callMth.isConstructor()) {
        return;
    }
    InsnNode instArgAssignInsn = ((RegisterArg) inv.getArg(0)).getAssignInsn();
    ConstructorInsn co = new ConstructorInsn(mth, inv);
    boolean remove = false;
    if (co.isSuper() && (co.getArgsCount() == 0 || parentClass.isEnum())) {
        remove = true;
    } else if (co.isThis() && co.getArgsCount() == 0) {
        MethodNode defCo = parentClass.searchMethodByName(callMth.getShortId());
        if (defCo == null || defCo.isNoCode()) {
            // default constructor not implemented
            remove = true;
        }
    }
    // remove super() call in instance initializer
    if (parentClass.isAnonymous() && mth.isDefaultConstructor() && co.isSuper()) {
        remove = true;
    }
    if (remove) {
        remover.add(insn);
        return;
    }
    if (co.isNewInstance()) {
        InsnNode newInstInsn = removeAssignChain(instArgAssignInsn, remover, InsnType.NEW_INSTANCE);
        if (newInstInsn != null) {
            RegisterArg instArg = newInstInsn.getResult();
            RegisterArg resultArg = co.getResult();
            if (!resultArg.equals(instArg)) {
                // replace all usages of 'instArg' with result of this constructor instruction
                for (RegisterArg useArg : new ArrayList<RegisterArg>(instArg.getSVar().getUseList())) {
                    RegisterArg dup = resultArg.duplicate();
                    InsnNode parentInsn = useArg.getParentInsn();
                    parentInsn.replaceArg(useArg, dup);
                    dup.setParentInsn(parentInsn);
                    resultArg.getSVar().use(dup);
                }
            }
        }
    }
    ConstructorInsn replace = processConstructor(mth, co);
    if (replace != null) {
        co = replace;
    }
    replaceInsn(block, insnNumber, co);
    processAnonymousConstructor(mth, co);
}
Also used : ClassNode(jadx.core.dex.nodes.ClassNode) ConstClassNode(jadx.core.dex.instructions.ConstClassNode) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) MethodNode(jadx.core.dex.nodes.MethodNode) InvokeNode(jadx.core.dex.instructions.InvokeNode) ArrayList(java.util.ArrayList) MethodInfo(jadx.core.dex.info.MethodInfo) ConstructorInsn(jadx.core.dex.instructions.mods.ConstructorInsn)

Example 72 with InsnNode

use of jadx.core.dex.nodes.InsnNode in project jadx by skylot.

the class ModVisitor method makeFilledArrayInsn.

private static InsnNode makeFilledArrayInsn(MethodNode mth, FillArrayNode insn) {
    ArgType insnArrayType = insn.getResult().getType();
    ArgType insnElementType = insnArrayType.getArrayElement();
    ArgType elType = insn.getElementType();
    if (!elType.isTypeKnown() && insnElementType.isPrimitive()) {
        if (elType.contains(insnElementType.getPrimitiveType())) {
            elType = insnElementType;
        }
    }
    if (!elType.equals(insnElementType) && !insnArrayType.equals(ArgType.OBJECT)) {
        ErrorsCounter.methodError(mth, "Incorrect type for fill-array insn " + InsnUtils.formatOffset(insn.getOffset()) + ", element type: " + elType + ", insn element type: " + insnElementType);
    }
    if (!elType.isTypeKnown()) {
        LOG.warn("Unknown array element type: {} in mth: {}", elType, mth);
        elType = insnElementType.isTypeKnown() ? insnElementType : elType.selectFirst();
        if (elType == null) {
            throw new JadxRuntimeException("Null array element type");
        }
    }
    insn.mergeElementType(mth.dex(), elType);
    elType = insn.getElementType();
    List<LiteralArg> list = insn.getLiteralArgs();
    InsnNode filledArr = new FilledNewArrayNode(elType, list.size());
    filledArr.setResult(insn.getResult());
    for (LiteralArg arg : list) {
        FieldNode f = mth.getParentClass().getConstFieldByLiteralArg(arg);
        if (f != null) {
            InsnNode fGet = new IndexInsnNode(InsnType.SGET, f.getFieldInfo(), 0);
            filledArr.addArg(InsnArg.wrapArg(fGet));
        } else {
            filledArr.addArg(arg);
        }
    }
    return filledArr;
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) FieldNode(jadx.core.dex.nodes.FieldNode) FilledNewArrayNode(jadx.core.dex.instructions.FilledNewArrayNode) LiteralArg(jadx.core.dex.instructions.args.LiteralArg) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode)

Example 73 with InsnNode

use of jadx.core.dex.nodes.InsnNode in project jadx by skylot.

the class ReSugarCode method visit.

@Override
public void visit(MethodNode mth) throws JadxException {
    if (mth.isNoCode()) {
        return;
    }
    InstructionRemover remover = new InstructionRemover(mth);
    for (BlockNode block : mth.getBasicBlocks()) {
        remover.setBlock(block);
        List<InsnNode> instructions = block.getInstructions();
        int size = instructions.size();
        for (int i = 0; i < size; i++) {
            InsnNode replacedInsn = process(mth, instructions, i, remover);
            if (replacedInsn != null) {
                instructions.set(i, replacedInsn);
            }
        }
        remover.perform();
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) InstructionRemover(jadx.core.utils.InstructionRemover)

Example 74 with InsnNode

use of jadx.core.dex.nodes.InsnNode in project jadx by skylot.

the class ReSugarCode method checkEnumMapAccess.

public static EnumMapInfo checkEnumMapAccess(MethodNode mth, InsnNode checkInsn) {
    InsnArg sgetArg = checkInsn.getArg(0);
    InsnArg invArg = checkInsn.getArg(1);
    if (!sgetArg.isInsnWrap() || !invArg.isInsnWrap()) {
        return null;
    }
    InsnNode invInsn = ((InsnWrapArg) invArg).getWrapInsn();
    InsnNode sgetInsn = ((InsnWrapArg) sgetArg).getWrapInsn();
    if (invInsn.getType() != InsnType.INVOKE || sgetInsn.getType() != InsnType.SGET) {
        return null;
    }
    InvokeNode inv = (InvokeNode) invInsn;
    if (!inv.getCallMth().getShortId().equals("ordinal()I")) {
        return null;
    }
    ClassNode enumCls = mth.dex().resolveClass(inv.getCallMth().getDeclClass());
    if (enumCls == null || !enumCls.isEnum()) {
        return null;
    }
    Object index = ((IndexInsnNode) sgetInsn).getIndex();
    if (!(index instanceof FieldInfo)) {
        return null;
    }
    FieldNode enumMapField = mth.dex().resolveField((FieldInfo) index);
    if (enumMapField == null || !enumMapField.getAccessFlags().isSynthetic()) {
        return null;
    }
    return new EnumMapInfo(inv.getArg(0), enumMapField);
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) ClassNode(jadx.core.dex.nodes.ClassNode) FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) InvokeNode(jadx.core.dex.instructions.InvokeNode) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) FieldInfo(jadx.core.dex.info.FieldInfo)

Example 75 with InsnNode

use of jadx.core.dex.nodes.InsnNode in project jadx by skylot.

the class ReSugarCode method addToEnumMap.

private static void addToEnumMap(MethodNode mth, EnumMapAttr mapAttr, InsnNode aputInsn) {
    InsnArg litArg = aputInsn.getArg(2);
    if (!litArg.isLiteral()) {
        return;
    }
    EnumMapInfo mapInfo = checkEnumMapAccess(mth, aputInsn);
    if (mapInfo == null) {
        return;
    }
    InsnArg enumArg = mapInfo.getArg();
    FieldNode field = mapInfo.getMapField();
    if (field == null || !enumArg.isInsnWrap()) {
        return;
    }
    InsnNode sget = ((InsnWrapArg) enumArg).getWrapInsn();
    if (!(sget instanceof IndexInsnNode)) {
        return;
    }
    Object index = ((IndexInsnNode) sget).getIndex();
    if (!(index instanceof FieldInfo)) {
        return;
    }
    FieldNode fieldNode = mth.dex().resolveField((FieldInfo) index);
    if (fieldNode == null) {
        return;
    }
    int literal = (int) ((LiteralArg) litArg).getLiteral();
    mapAttr.add(field, literal, fieldNode);
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) FieldNode(jadx.core.dex.nodes.FieldNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) FieldInfo(jadx.core.dex.info.FieldInfo)

Aggregations

InsnNode (jadx.core.dex.nodes.InsnNode)123 BlockNode (jadx.core.dex.nodes.BlockNode)41 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)39 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)35 InsnArg (jadx.core.dex.instructions.args.InsnArg)32 InsnWrapArg (jadx.core.dex.instructions.args.InsnWrapArg)19 SSAVar (jadx.core.dex.instructions.args.SSAVar)16 ArrayList (java.util.ArrayList)14 ArgType (jadx.core.dex.instructions.args.ArgType)11 PhiInsn (jadx.core.dex.instructions.PhiInsn)10 FieldNode (jadx.core.dex.nodes.FieldNode)10 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)8 LiteralArg (jadx.core.dex.instructions.args.LiteralArg)7 ClassNode (jadx.core.dex.nodes.ClassNode)7 MethodNode (jadx.core.dex.nodes.MethodNode)7 FieldInfo (jadx.core.dex.info.FieldInfo)6 ArithNode (jadx.core.dex.instructions.ArithNode)6 ConstructorInsn (jadx.core.dex.instructions.mods.ConstructorInsn)6 InsnType (jadx.core.dex.instructions.InsnType)5 IContainer (jadx.core.dex.nodes.IContainer)5