Search in sources :

Example 26 with BlockNode

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

the class EliminatePhiNodes method removePhiInstructions.

private static void removePhiInstructions(MethodNode mth) {
    for (BlockNode block : mth.getBasicBlocks()) {
        PhiListAttr phiList = block.get(AType.PHI_LIST);
        if (phiList == null) {
            continue;
        }
        List<PhiInsn> list = phiList.getList();
        for (PhiInsn phiInsn : list) {
            removeInsn(mth, block, phiInsn);
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) PhiInsn(jadx.core.dex.instructions.PhiInsn) PhiListAttr(jadx.core.dex.attributes.nodes.PhiListAttr)

Example 27 with BlockNode

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

the class SSATransform method renameVar.

private static void renameVar(MethodNode mth, SSAVar[] vars, int[] vers, BlockNode block) {
    SSAVar[] inputVars = Arrays.copyOf(vars, vars.length);
    for (InsnNode insn : block.getInstructions()) {
        if (insn.getType() != InsnType.PHI) {
            for (InsnArg arg : insn.getArguments()) {
                if (!arg.isRegister()) {
                    continue;
                }
                RegisterArg reg = (RegisterArg) arg;
                int regNum = reg.getRegNum();
                SSAVar var = vars[regNum];
                if (var == null) {
                    throw new JadxRuntimeException("Not initialized variable reg: " + regNum + ", insn: " + insn + ", block:" + block + ", method: " + mth);
                }
                var.use(reg);
            }
        }
        RegisterArg result = insn.getResult();
        if (result != null) {
            int regNum = result.getRegNum();
            vars[regNum] = newSSAVar(mth, vers, result, regNum);
        }
    }
    for (BlockNode s : block.getSuccessors()) {
        PhiListAttr phiList = s.get(AType.PHI_LIST);
        if (phiList == null) {
            continue;
        }
        for (PhiInsn phiInsn : phiList.getList()) {
            int regNum = phiInsn.getResult().getRegNum();
            SSAVar var = vars[regNum];
            if (var == null) {
                continue;
            }
            RegisterArg arg = phiInsn.bindArg(block);
            var.use(arg);
            var.setUsedInPhi(phiInsn);
        }
    }
    for (BlockNode domOn : block.getDominatesOn()) {
        renameVar(mth, vars, vers, domOn);
    }
    System.arraycopy(inputVars, 0, vars, 0, vars.length);
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) SSAVar(jadx.core.dex.instructions.args.SSAVar) PhiInsn(jadx.core.dex.instructions.PhiInsn) InsnArg(jadx.core.dex.instructions.args.InsnArg) PhiListAttr(jadx.core.dex.attributes.nodes.PhiListAttr) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 28 with BlockNode

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

the class BlockUtils method getPathCross.

public static BlockNode getPathCross(MethodNode mth, BlockNode b1, BlockNode b2) {
    if (b1 == null || b2 == null) {
        return null;
    }
    BitSet b = new BitSet();
    b.or(b1.getDomFrontier());
    b.and(b2.getDomFrontier());
    b.clear(b1.getId());
    b.clear(b2.getId());
    if (b.cardinality() == 1) {
        BlockNode end = mth.getBasicBlocks().get(b.nextSetBit(0));
        if (isPathExists(b1, end) && isPathExists(b2, end)) {
            return end;
        }
    }
    if (isPathExists(b1, b2)) {
        return b2;
    }
    if (isPathExists(b2, b1)) {
        return b1;
    }
    return null;
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) BitSet(java.util.BitSet)

Example 29 with BlockNode

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

the class DebugUtils method checkPHI.

private static void checkPHI(MethodNode mth) {
    for (BlockNode block : mth.getBasicBlocks()) {
        List<PhiInsn> phis = new ArrayList<PhiInsn>();
        for (InsnNode insn : block.getInstructions()) {
            if (insn.getType() == InsnType.PHI) {
                PhiInsn phi = (PhiInsn) insn;
                phis.add(phi);
                if (phi.getArgsCount() != phi.getBlockBinds().size()) {
                    throw new JadxRuntimeException("Incorrect args and binds in PHI");
                }
                if (phi.getArgsCount() == 0) {
                    throw new JadxRuntimeException("No args and binds in PHI");
                }
                for (InsnArg arg : insn.getArguments()) {
                    if (arg instanceof RegisterArg) {
                        BlockNode b = phi.getBlockByArg((RegisterArg) arg);
                        if (b == null) {
                            throw new JadxRuntimeException("Predecessor block not found");
                        }
                    } else {
                        throw new JadxRuntimeException("Not register in phi insn");
                    }
                }
            }
        }
        PhiListAttr phiListAttr = block.get(AType.PHI_LIST);
        if (phiListAttr == null) {
            if (!phis.isEmpty()) {
                throw new JadxRuntimeException("Missing PHI list attribute");
            }
        } else {
            List<PhiInsn> phiList = phiListAttr.getList();
            if (phiList.isEmpty()) {
                throw new JadxRuntimeException("Empty PHI list attribute");
            }
            if (!phis.containsAll(phiList) || !phiList.containsAll(phis)) {
                throw new JadxRuntimeException("Instructions not match");
            }
        }
    }
    for (SSAVar ssaVar : mth.getSVars()) {
        PhiInsn usedInPhi = ssaVar.getUsedInPhi();
        if (usedInPhi != null) {
            boolean found = false;
            for (RegisterArg useArg : ssaVar.getUseList()) {
                InsnNode parentInsn = useArg.getParentInsn();
                if (parentInsn != null && parentInsn == usedInPhi) {
                    found = true;
                }
            }
            if (!found) {
                throw new JadxRuntimeException("Used in phi incorrect");
            }
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) PhiInsn(jadx.core.dex.instructions.PhiInsn) SSAVar(jadx.core.dex.instructions.args.SSAVar) InsnArg(jadx.core.dex.instructions.args.InsnArg) PhiListAttr(jadx.core.dex.attributes.nodes.PhiListAttr) ArrayList(java.util.ArrayList) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 30 with BlockNode

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

the class TryCatchBlock method removeWholeBlock.

private void removeWholeBlock(MethodNode mth) {
    // self destruction
    for (Iterator<ExceptionHandler> it = handlers.iterator(); it.hasNext(); ) {
        ExceptionHandler h = it.next();
        unbindHandler(h);
        it.remove();
    }
    for (InsnNode insn : insns) {
        insn.removeAttr(attr);
    }
    insns.clear();
    if (mth.getBasicBlocks() != null) {
        for (BlockNode block : mth.getBasicBlocks()) {
            block.removeAttr(attr);
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) InsnNode(jadx.core.dex.nodes.InsnNode)

Aggregations

BlockNode (jadx.core.dex.nodes.BlockNode)220 InsnNode (jadx.core.dex.nodes.InsnNode)91 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)43 ArrayList (java.util.ArrayList)41 BitSet (java.util.BitSet)31 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)29 InsnArg (jadx.core.dex.instructions.args.InsnArg)25 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)21 ExceptionHandler (jadx.core.dex.trycatch.ExceptionHandler)19 PhiInsn (jadx.core.dex.instructions.PhiInsn)14 SSAVar (jadx.core.dex.instructions.args.SSAVar)14 Nullable (org.jetbrains.annotations.Nullable)14 LoopInfo (jadx.core.dex.attributes.nodes.LoopInfo)13 IRegion (jadx.core.dex.nodes.IRegion)13 HashSet (java.util.HashSet)13 MethodNode (jadx.core.dex.nodes.MethodNode)12 Region (jadx.core.dex.regions.Region)12 LoopRegion (jadx.core.dex.regions.loops.LoopRegion)12 InsnType (jadx.core.dex.instructions.InsnType)11 SynchronizedRegion (jadx.core.dex.regions.SynchronizedRegion)11