Search in sources :

Example 21 with DexLabel

use of com.googlecode.d2j.DexLabel in project dex2jar by pxb1988.

the class Dex2IRConverter method dfs.

private void dfs(BitSet[] exBranch, BitSet handlers, BitSet access, DvmInterpreter<DvmValue> interpreter) {
    currentEmit = preEmit;
    Dex2IrFrame first = initFirstFrame(dexCodeNode, target);
    if (parentCount[0] > 1) {
        merge(first, 0);
    } else {
        frames[0] = first;
    }
    Stack<DexStmtNode> stack = new Stack<>();
    stack.push(insnList.get(0));
    Dex2IrFrame tmp = new Dex2IrFrame(dexCodeNode.totalRegister);
    while (!stack.isEmpty()) {
        DexStmtNode p = stack.pop();
        int index = p.__index;
        if (!access.get(index)) {
            access.set(index);
        } else {
            continue;
        }
        Dex2IrFrame frame = frames[index];
        setCurrentEmit(index);
        if (p instanceof DexLabelStmtNode) {
            emit(getLabel(((DexLabelStmtNode) p).label));
            if (handlers.get(index)) {
                Local ex = newLocal();
                emit(Stmts.nIdentity(ex, Exprs.nExceptionRef("Ljava/lang/Throwable;")));
                frame.setTmp(new DvmValue(ex));
            }
        }
        BitSet ex = exBranch[index];
        if (ex != null) {
            for (int i = ex.nextSetBit(0); i >= 0; i = ex.nextSetBit(i + 1)) {
                merge(frame, i);
                stack.push(insnList.get(i));
            }
        }
        tmp.init(frame);
        try {
            if (p.op != null) {
                switch(p.op) {
                    case RETURN_VOID:
                        emit(nReturnVoid());
                        break;
                    case GOTO:
                    case GOTO_16:
                    case GOTO_32:
                        emit(nGoto(getLabel(((JumpStmtNode) p).label)));
                        break;
                    case NOP:
                        emit(nNop());
                        break;
                    case BAD_OP:
                        emit(nThrow(nInvokeNew(new Value[] { nString("bad dex opcode") }, new String[] { "Ljava/lang/String;" }, "Ljava/lang/VerifyError;")));
                        break;
                    default:
                        tmp.execute(p, interpreter);
                        break;
                }
            }
        } catch (Exception exception) {
            throw new RuntimeException("Fail on Op " + p.op + " index " + index, exception);
        }
        if (p.op != null) {
            Op op = p.op;
            if (op.canBranch()) {
                JumpStmtNode jump = (JumpStmtNode) p;
                int targetIndex = indexOf(jump.label);
                stack.push(insnList.get(targetIndex));
                merge(tmp, targetIndex);
            }
            if (op.canSwitch()) {
                BaseSwitchStmtNode switchStmtNode = (BaseSwitchStmtNode) p;
                for (DexLabel label : switchStmtNode.labels) {
                    int targetIndex = indexOf(label);
                    stack.push(insnList.get(targetIndex));
                    merge(tmp, targetIndex);
                }
            }
            if (op.canContinue()) {
                stack.push(insnList.get(index + 1));
                merge(tmp, index + 1);
            }
        } else {
            stack.push(insnList.get(index + 1));
            merge(tmp, index + 1);
        }
        // cleanup frame it is useless
        if (parentCount[index] <= 1) {
            frames[index] = null;
        }
    }
}
Also used : Op(com.googlecode.d2j.reader.Op) Local(com.googlecode.dex2jar.ir.expr.Local) DexLabel(com.googlecode.d2j.DexLabel)

Aggregations

DexLabel (com.googlecode.d2j.DexLabel)21 Method (com.googlecode.d2j.Method)10 DexCodeVisitor (com.googlecode.d2j.visitors.DexCodeVisitor)9 DexMethodVisitor (com.googlecode.d2j.visitors.DexMethodVisitor)9 Field (com.googlecode.d2j.Field)6 Test (org.junit.Test)6 Op (com.googlecode.d2j.reader.Op)3 TryCatchNode (com.googlecode.d2j.node.TryCatchNode)2 Local (com.googlecode.dex2jar.ir.expr.Local)2 ByteBuffer (java.nio.ByteBuffer)2 DexType (com.googlecode.d2j.DexType)1 DvmInterpreter (com.googlecode.d2j.node.analysis.DvmInterpreter)1 DexClassVisitor (com.googlecode.d2j.visitors.DexClassVisitor)1 IrMethod (com.googlecode.dex2jar.ir.IrMethod)1 Trap (com.googlecode.dex2jar.ir.Trap)1 Value (com.googlecode.dex2jar.ir.expr.Value)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1