Search in sources :

Example 6 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class BlockFinallyExtract method visit.

@Override
public void visit(MethodNode mth) {
    if (mth.isNoCode() || mth.isNoExceptionHandlers()) {
        return;
    }
    boolean reloadBlocks = false;
    for (ExceptionHandler excHandler : mth.getExceptionHandlers()) {
        if (processExceptionHandler(mth, excHandler)) {
            reloadBlocks = true;
        }
    }
    if (reloadBlocks) {
        mergeReturnBlocks(mth);
        BlockProcessor.rerun(mth);
    }
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler)

Example 7 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class BlockSplitter method connectExceptionHandlers.

private static void connectExceptionHandlers(Map<Integer, BlockNode> blocksMap, BlockNode block, InsnNode insn) {
    CatchAttr catches = insn.get(AType.CATCH_BLOCK);
    SplitterBlockAttr spl = block.get(AType.SPLITTER_BLOCK);
    if (catches == null || spl == null) {
        return;
    }
    BlockNode splitterBlock = spl.getBlock();
    boolean tryEnd = insn.contains(AFlag.TRY_LEAVE);
    for (ExceptionHandler h : catches.getTryBlock().getHandlers()) {
        BlockNode handlerBlock = getBlock(h.getHandleOffset(), blocksMap);
        // skip self loop in handler
        if (splitterBlock != handlerBlock) {
            if (!handlerBlock.contains(AType.SPLITTER_BLOCK)) {
                handlerBlock.addAttr(spl);
            }
            connect(splitterBlock, handlerBlock);
        }
        if (tryEnd) {
            connect(block, handlerBlock);
        }
    }
}
Also used : BlockNode(jadx.core.dex.nodes.BlockNode) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) SplitterBlockAttr(jadx.core.dex.trycatch.SplitterBlockAttr) CatchAttr(jadx.core.dex.trycatch.CatchAttr)

Example 8 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class MethodNode method initTryCatches.

private void initTryCatches(Code mthCode) {
    InsnNode[] insnByOffset = instructions;
    CatchHandler[] catchBlocks = mthCode.getCatchHandlers();
    Try[] tries = mthCode.getTries();
    if (catchBlocks.length == 0 && tries.length == 0) {
        return;
    }
    int hc = 0;
    Set<Integer> addrs = new HashSet<Integer>();
    List<TryCatchBlock> catches = new ArrayList<TryCatchBlock>(catchBlocks.length);
    for (CatchHandler handler : catchBlocks) {
        TryCatchBlock tcBlock = new TryCatchBlock();
        catches.add(tcBlock);
        for (int i = 0; i < handler.getAddresses().length; i++) {
            int addr = handler.getAddresses()[i];
            ClassInfo type = ClassInfo.fromDex(parentClass.dex(), handler.getTypeIndexes()[i]);
            tcBlock.addHandler(this, addr, type);
            addrs.add(addr);
            hc++;
        }
        int addr = handler.getCatchAllAddress();
        if (addr >= 0) {
            tcBlock.addHandler(this, addr, null);
            addrs.add(addr);
            hc++;
        }
    }
    if (hc > 0 && hc != addrs.size()) {
        // each handler must be only in one try/catch block
        for (TryCatchBlock ct1 : catches) {
            for (TryCatchBlock ct2 : catches) {
                if (ct1 != ct2 && ct2.containsAllHandlers(ct1)) {
                    for (ExceptionHandler h : ct1.getHandlers()) {
                        ct2.removeHandler(this, h);
                        h.setTryBlock(ct1);
                    }
                }
            }
        }
    }
    // attach EXC_HANDLER attributes to instructions
    addrs.clear();
    for (TryCatchBlock ct : catches) {
        for (ExceptionHandler eh : ct.getHandlers()) {
            int addr = eh.getHandleOffset();
            ExcHandlerAttr ehAttr = new ExcHandlerAttr(ct, eh);
            insnByOffset[addr].addAttr(ehAttr);
        }
    }
    // attach TRY_ENTER, TRY_LEAVE attributes to instructions
    for (Try aTry : tries) {
        int catchNum = aTry.getCatchHandlerIndex();
        TryCatchBlock catchBlock = catches.get(catchNum);
        int offset = aTry.getStartAddress();
        int end = offset + aTry.getInstructionCount() - 1;
        InsnNode insn = insnByOffset[offset];
        insn.add(AFlag.TRY_ENTER);
        while (offset <= end && offset >= 0) {
            insn = insnByOffset[offset];
            catchBlock.addInsn(insn);
            offset = InsnDecoder.getNextInsnOffset(insnByOffset, offset);
        }
        if (insnByOffset[end] != null) {
            insnByOffset[end].add(AFlag.TRY_LEAVE);
        } else {
            insn.add(AFlag.TRY_LEAVE);
        }
    }
}
Also used : ExcHandlerAttr(jadx.core.dex.trycatch.ExcHandlerAttr) ArrayList(java.util.ArrayList) CatchHandler(com.android.dex.Code.CatchHandler) ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) Try(com.android.dex.Code.Try) TryCatchBlock(jadx.core.dex.trycatch.TryCatchBlock) HashSet(java.util.HashSet) ClassInfo(jadx.core.dex.info.ClassInfo)

Example 9 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class TryCatchRegion method setTryCatchBlock.

public void setTryCatchBlock(TryCatchBlock tryCatchBlock) {
    this.tryCatchBlock = tryCatchBlock;
    int count = tryCatchBlock.getHandlersCount();
    this.catchRegions = new LinkedHashMap<ExceptionHandler, IContainer>(count);
    for (ExceptionHandler handler : tryCatchBlock.getHandlers()) {
        IContainer handlerRegion = handler.getHandlerRegion();
        if (handlerRegion != null) {
            if (handler.isFinally()) {
                finallyRegion = handlerRegion;
            } else {
                catchRegions.put(handler, handlerRegion);
            }
        }
    }
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) IContainer(jadx.core.dex.nodes.IContainer)

Example 10 with ExceptionHandler

use of jadx.core.dex.trycatch.ExceptionHandler in project jadx by skylot.

the class RegionMaker method processHandlersOutBlocks.

/**
	 * Search handlers successor blocks not included in any region.
	 */
protected IRegion processHandlersOutBlocks(MethodNode mth, Set<TryCatchBlock> tcs) {
    Set<IBlock> allRegionBlocks = new HashSet<IBlock>();
    RegionUtils.getAllRegionBlocks(mth.getRegion(), allRegionBlocks);
    Set<IBlock> succBlocks = new HashSet<IBlock>();
    for (TryCatchBlock tc : tcs) {
        for (ExceptionHandler handler : tc.getHandlers()) {
            IContainer region = handler.getHandlerRegion();
            if (region != null) {
                IBlock lastBlock = RegionUtils.getLastBlock(region);
                if (lastBlock instanceof BlockNode) {
                    succBlocks.addAll(((BlockNode) lastBlock).getSuccessors());
                }
                RegionUtils.getAllRegionBlocks(region, allRegionBlocks);
            }
        }
    }
    succBlocks.removeAll(allRegionBlocks);
    if (succBlocks.isEmpty()) {
        return null;
    }
    Region excOutRegion = new Region(mth.getRegion());
    for (IBlock block : succBlocks) {
        if (block instanceof BlockNode) {
            excOutRegion.add(makeRegion((BlockNode) block, new RegionStack(mth)));
        }
    }
    return excOutRegion;
}
Also used : ExceptionHandler(jadx.core.dex.trycatch.ExceptionHandler) BlockNode(jadx.core.dex.nodes.BlockNode) IBlock(jadx.core.dex.nodes.IBlock) Region(jadx.core.dex.regions.Region) IRegion(jadx.core.dex.nodes.IRegion) SwitchRegion(jadx.core.dex.regions.SwitchRegion) SynchronizedRegion(jadx.core.dex.regions.SynchronizedRegion) LoopRegion(jadx.core.dex.regions.loops.LoopRegion) IfRegion(jadx.core.dex.regions.conditions.IfRegion) TryCatchBlock(jadx.core.dex.trycatch.TryCatchBlock) IContainer(jadx.core.dex.nodes.IContainer) HashSet(java.util.HashSet)

Aggregations

ExceptionHandler (jadx.core.dex.trycatch.ExceptionHandler)14 TryCatchBlock (jadx.core.dex.trycatch.TryCatchBlock)7 BlockNode (jadx.core.dex.nodes.BlockNode)6 HashSet (java.util.HashSet)5 IContainer (jadx.core.dex.nodes.IContainer)4 InsnNode (jadx.core.dex.nodes.InsnNode)4 CatchAttr (jadx.core.dex.trycatch.CatchAttr)4 ExcHandlerAttr (jadx.core.dex.trycatch.ExcHandlerAttr)4 ArrayList (java.util.ArrayList)4 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)3 SplitterBlockAttr (jadx.core.dex.trycatch.SplitterBlockAttr)3 ArgType (jadx.core.dex.instructions.args.ArgType)2 BitSet (java.util.BitSet)2 CatchHandler (com.android.dex.Code.CatchHandler)1 Try (com.android.dex.Code.Try)1 ClassInfo (jadx.core.dex.info.ClassInfo)1 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)1 NamedArg (jadx.core.dex.instructions.args.NamedArg)1 SSAVar (jadx.core.dex.instructions.args.SSAVar)1 IBlock (jadx.core.dex.nodes.IBlock)1