Search in sources :

Example 1 with TryCatchNode

use of com.googlecode.d2j.node.TryCatchNode in project dex2jar by pxb1988.

the class Dex2IrAdapter method convert.

public IrMethod convert(DexCodeNode codeNode) {
    if (codeNode.tryStmts != null) {
        for (TryCatchNode n : codeNode.tryStmts) {
            n.accept(this);
        }
    }
    if (codeNode.debugNode != null) {
        DexDebugVisitor ddv = this.visitDebug();
        if (ddv != null) {
            codeNode.debugNode.accept(ddv);
            ddv.visitEnd();
        }
    }
    lastIsInvokeOrFilledNewArray = false;
    if (codeNode.totalRegister >= 0) {
        this.visitRegister(codeNode.totalRegister);
    }
    for (DexStmtNode n : codeNode.stmts) {
        n.accept(this);
        if (n instanceof FilledNewArrayStmtNode) {
            lastIsInvokeOrFilledNewArray = true;
        } else if (n instanceof MethodStmtNode) {
            lastIsInvokeOrFilledNewArray = !((MethodStmtNode) n).method.getReturnType().equals("V");
        } else if (!(n instanceof DexLabelStmtNode)) {
            lastIsInvokeOrFilledNewArray = false;
        }
    }
    visitEnd();
    return irMethod;
}
Also used : DexDebugVisitor(com.googlecode.d2j.visitors.DexDebugVisitor) MethodStmtNode(com.googlecode.d2j.node.insn.MethodStmtNode) TryCatchNode(com.googlecode.d2j.node.TryCatchNode) DexStmtNode(com.googlecode.d2j.node.insn.DexStmtNode) DexLabelStmtNode(com.googlecode.d2j.node.insn.DexLabelStmtNode) FilledNewArrayStmtNode(com.googlecode.d2j.node.insn.FilledNewArrayStmtNode)

Example 2 with TryCatchNode

use of com.googlecode.d2j.node.TryCatchNode in project dex2jar by pxb1988.

the class Dex2IRConverter method fixExceptionHandlers.

/**
     * issue 63
     * <pre>
     * L1:
     *    STMTs
     * L2:
     *    RETURN
     * L1~L2 > L2 Exception
     * </pre>
     * <p/>
     * fix to
     * <p/>
     * <pre>
     * L1:
     *    STMTs
     * L2:
     *    RETURN
     * L3:
     *    goto L2
     * L1~L2 > L3 Exception
     * </pre>
     */
private void fixExceptionHandlers() {
    if (dexCodeNode.tryStmts == null) {
        return;
    }
    Queue<Integer> q = new LinkedList<>();
    Set<Integer> handlers = new TreeSet<>();
    for (TryCatchNode tcb : dexCodeNode.tryStmts) {
        for (DexLabel h : tcb.handler) {
            int index = indexOf(h);
            // add the next insn after label
            q.add(index + 1);
            handlers.add(index);
        }
    }
    q.add(0);
    Map<Integer, DexLabel> needChange = new HashMap<>();
    BitSet access = new BitSet(insnList.size());
    while (!q.isEmpty()) {
        Integer key = q.poll();
        int index = key;
        if (access.get(index)) {
            continue;
        } else {
            access.set(index);
        }
        if (handlers.contains(key)) {
            // the cfg goes to a exception handler
            needChange.put(key, null);
        }
        DexStmtNode node = insnList.get(key);
        if (node.op == null) {
            q.add(index + 1);
        } else {
            Op op = node.op;
            if (op.canContinue()) {
                q.add(index + 1);
            }
            if (op.canBranch()) {
                JumpStmtNode jump = (JumpStmtNode) node;
                q.add(indexOf(jump.label));
            }
            if (op.canSwitch()) {
                for (DexLabel dexLabel : ((BaseSwitchStmtNode) node).labels) {
                    q.add(indexOf(dexLabel));
                }
            }
        }
    }
    if (needChange.size() > 0) {
        for (TryCatchNode tcb : dexCodeNode.tryStmts) {
            DexLabel[] handler = tcb.handler;
            for (int i = 0; i < handler.length; i++) {
                DexLabel h = handler[i];
                int index = indexOf(h);
                if (needChange.containsKey(index)) {
                    DexLabel n = needChange.get(index);
                    if (n == null) {
                        n = new DexLabel();
                        needChange.put(index, n);
                        DexLabelStmtNode dexStmtNode = new DexLabelStmtNode(n);
                        dexStmtNode.__index = insnList.size();
                        insnList.add(dexStmtNode);
                        labelMap.put(n, dexStmtNode);
                        JumpStmtNode jumpStmtNode = new JumpStmtNode(Op.GOTO, 0, 0, h);
                        jumpStmtNode.__index = insnList.size();
                        insnList.add(jumpStmtNode);
                    }
                    handler[i] = n;
                }
            }
        }
    }
}
Also used : Op(com.googlecode.d2j.reader.Op) TryCatchNode(com.googlecode.d2j.node.TryCatchNode) DexLabel(com.googlecode.d2j.DexLabel)

Example 3 with TryCatchNode

use of com.googlecode.d2j.node.TryCatchNode in project dex2jar by pxb1988.

the class Dex2IRConverter method initExceptionHandlers.

private void initExceptionHandlers(DexCodeNode dexCodeNode, BitSet[] exBranch, BitSet handlers) {
    if (dexCodeNode.tryStmts != null) {
        for (TryCatchNode tcb : dexCodeNode.tryStmts) {
            target.traps.add(new Trap(getLabel(tcb.start), getLabel(tcb.end), getLabels(tcb.handler), tcb.type));
            for (DexLabel h : tcb.handler) {
                handlers.set(indexOf(h));
            }
            int endIndex = indexOf(tcb.end);
            for (int p = indexOf(tcb.start) + 1; p < endIndex; p++) {
                DexStmtNode stmt = insnList.get(p);
                if (stmt.op != null && stmt.op.canThrow()) {
                    BitSet x = exBranch[p];
                    if (x == null) {
                        x = exBranch[p] = new BitSet(insnList.size());
                    }
                    for (DexLabel h : tcb.handler) {
                        int hIndex = indexOf(h);
                        x.set(hIndex);
                        parentCount[hIndex]++;
                    }
                }
            }
        }
    }
}
Also used : TryCatchNode(com.googlecode.d2j.node.TryCatchNode) Trap(com.googlecode.dex2jar.ir.Trap) DexLabel(com.googlecode.d2j.DexLabel)

Aggregations

TryCatchNode (com.googlecode.d2j.node.TryCatchNode)3 DexLabel (com.googlecode.d2j.DexLabel)2 DexLabelStmtNode (com.googlecode.d2j.node.insn.DexLabelStmtNode)1 DexStmtNode (com.googlecode.d2j.node.insn.DexStmtNode)1 FilledNewArrayStmtNode (com.googlecode.d2j.node.insn.FilledNewArrayStmtNode)1 MethodStmtNode (com.googlecode.d2j.node.insn.MethodStmtNode)1 Op (com.googlecode.d2j.reader.Op)1 DexDebugVisitor (com.googlecode.d2j.visitors.DexDebugVisitor)1 Trap (com.googlecode.dex2jar.ir.Trap)1