Search in sources :

Example 1 with DecodeException

use of jadx.core.utils.exceptions.DecodeException in project jadx by skylot.

the class DebugInfoParser method process.

public void process() throws DecodeException {
    int addr = 0;
    int line = section.readUleb128();
    int paramsCount = section.readUleb128();
    List<RegisterArg> mthArgs = mth.getArguments(false);
    for (int i = 0; i < paramsCount; i++) {
        int id = section.readUleb128() - 1;
        if (id != DexNode.NO_INDEX) {
            String name = dex.getString(id);
            if (i < mthArgs.size()) {
                mthArgs.get(i).setName(name);
            }
        }
    }
    for (RegisterArg arg : mthArgs) {
        int rn = arg.getRegNum();
        locals[rn] = new LocalVar(arg);
        activeRegisters[rn] = arg;
    }
    // process '0' instruction
    addrChange(-1, 1, line);
    setLine(addr, line);
    int c = section.readByte() & 0xFF;
    while (c != DBG_END_SEQUENCE) {
        switch(c) {
            case DBG_ADVANCE_PC:
                {
                    int addrInc = section.readUleb128();
                    addr = addrChange(addr, addrInc, line);
                    setLine(addr, line);
                    break;
                }
            case DBG_ADVANCE_LINE:
                {
                    line += section.readSleb128();
                    break;
                }
            case DBG_START_LOCAL:
                {
                    int regNum = section.readUleb128();
                    int nameId = section.readUleb128() - 1;
                    int type = section.readUleb128() - 1;
                    LocalVar var = new LocalVar(dex, regNum, nameId, type, DexNode.NO_INDEX);
                    startVar(var, addr, line);
                    break;
                }
            case DBG_START_LOCAL_EXTENDED:
                {
                    int regNum = section.readUleb128();
                    int nameId = section.readUleb128() - 1;
                    int type = section.readUleb128() - 1;
                    int sign = section.readUleb128() - 1;
                    LocalVar var = new LocalVar(dex, regNum, nameId, type, sign);
                    startVar(var, addr, line);
                    break;
                }
            case DBG_RESTART_LOCAL:
                {
                    int regNum = section.readUleb128();
                    LocalVar var = locals[regNum];
                    if (var != null) {
                        if (var.end(addr, line)) {
                            setVar(var);
                        }
                        var.start(addr, line);
                    }
                    break;
                }
            case DBG_END_LOCAL:
                {
                    int regNum = section.readUleb128();
                    LocalVar var = locals[regNum];
                    if (var != null) {
                        var.end(addr, line);
                        setVar(var);
                    }
                    break;
                }
            case DBG_SET_PROLOGUE_END:
            case DBG_SET_EPILOGUE_BEGIN:
                // do nothing
                break;
            case DBG_SET_FILE:
                {
                    int idx = section.readUleb128() - 1;
                    if (idx != DexNode.NO_INDEX) {
                        String sourceFile = dex.getString(idx);
                        mth.addAttr(new SourceFileAttr(sourceFile));
                    }
                    break;
                }
            default:
                {
                    if (c >= DBG_FIRST_SPECIAL) {
                        int adjustedOpcode = c - DBG_FIRST_SPECIAL;
                        int addrInc = adjustedOpcode / DBG_LINE_RANGE;
                        addr = addrChange(addr, addrInc, line);
                        line += DBG_LINE_BASE + adjustedOpcode % DBG_LINE_RANGE;
                        setLine(addr, line);
                    } else {
                        throw new DecodeException("Unknown debug insn code: " + c);
                    }
                    break;
                }
        }
        c = section.readByte() & 0xFF;
    }
    for (LocalVar var : locals) {
        if (var != null && !var.isEnd()) {
            var.end(mth.getCodeSize() - 1, line);
            setVar(var);
        }
    }
    setSourceLines(addr, insnByOffset.length, line);
}
Also used : RegisterArg(jadx.core.dex.instructions.args.RegisterArg) SourceFileAttr(jadx.core.dex.attributes.nodes.SourceFileAttr) DecodeException(jadx.core.utils.exceptions.DecodeException)

Example 2 with DecodeException

use of jadx.core.utils.exceptions.DecodeException in project jadx by skylot.

the class InsnDecoder method decodeInsns.

public void decodeInsns(Code mthCode) throws DecodeException {
    short[] encodedInstructions = mthCode.getInstructions();
    int size = encodedInstructions.length;
    DecodedInstruction[] decoded = new DecodedInstruction[size];
    ShortArrayCodeInput in = new ShortArrayCodeInput(encodedInstructions);
    try {
        while (in.hasMore()) {
            decoded[in.cursor()] = DecodedInstruction.decode(in);
        }
    } catch (Exception e) {
        throw new DecodeException(method, "", e);
    }
    insnArr = decoded;
}
Also used : DecodedInstruction(com.android.dx.io.instructions.DecodedInstruction) FillArrayDataPayloadDecodedInstruction(com.android.dx.io.instructions.FillArrayDataPayloadDecodedInstruction) SparseSwitchPayloadDecodedInstruction(com.android.dx.io.instructions.SparseSwitchPayloadDecodedInstruction) PackedSwitchPayloadDecodedInstruction(com.android.dx.io.instructions.PackedSwitchPayloadDecodedInstruction) ShortArrayCodeInput(com.android.dx.io.instructions.ShortArrayCodeInput) DecodeException(jadx.core.utils.exceptions.DecodeException) DecodeException(jadx.core.utils.exceptions.DecodeException)

Example 3 with DecodeException

use of jadx.core.utils.exceptions.DecodeException in project jadx by skylot.

the class ClsSet method load.

public void load(InputStream input) throws IOException, DecodeException {
    DataInputStream in = new DataInputStream(input);
    try {
        byte[] header = new byte[JADX_CLS_SET_HEADER.length()];
        int readHeaderLength = in.read(header);
        int version = in.readByte();
        if (readHeaderLength != JADX_CLS_SET_HEADER.length() || !JADX_CLS_SET_HEADER.equals(new String(header, STRING_CHARSET)) || version != VERSION) {
            throw new DecodeException("Wrong jadx class set header");
        }
        int count = in.readInt();
        classes = new NClass[count];
        for (int i = 0; i < count; i++) {
            String name = readString(in);
            classes[i] = new NClass(name, i);
        }
        for (int i = 0; i < count; i++) {
            int pCount = in.readByte();
            NClass[] parents = new NClass[pCount];
            for (int j = 0; j < pCount; j++) {
                parents[j] = classes[in.readInt()];
            }
            classes[i].setParents(parents);
        }
    } finally {
        close(in);
    }
}
Also used : DataInputStream(java.io.DataInputStream) DecodeException(jadx.core.utils.exceptions.DecodeException)

Example 4 with DecodeException

use of jadx.core.utils.exceptions.DecodeException in project jadx by skylot.

the class InputFile method loadFromJar.

private static Dex loadFromJar(File jarFile) throws DecodeException {
    try {
        LOG.info("converting to dex: {} ...", jarFile.getName());
        JavaToDex j2d = new JavaToDex();
        byte[] ba = j2d.convert(jarFile.getAbsolutePath());
        if (ba.length == 0) {
            throw new JadxException(j2d.isError() ? j2d.getDxErrors() : "Empty dx output");
        }
        if (j2d.isError()) {
            LOG.warn("dx message: {}", j2d.getDxErrors());
        }
        return new Dex(ba);
    } catch (Throwable e) {
        throw new DecodeException("java class to dex conversion error:\n " + e.getMessage(), e);
    }
}
Also used : JadxException(jadx.core.utils.exceptions.JadxException) Dex(com.android.dex.Dex) DecodeException(jadx.core.utils.exceptions.DecodeException)

Example 5 with DecodeException

use of jadx.core.utils.exceptions.DecodeException in project jadx by skylot.

the class MethodGen method addFallbackMethodCode.

public void addFallbackMethodCode(CodeWriter code) {
    if (mth.getInstructions() == null) {
        JadxErrorAttr errorAttr = mth.get(AType.JADX_ERROR);
        if (errorAttr == null || errorAttr.getCause() == null || !errorAttr.getCause().getClass().equals(DecodeException.class)) {
            // load original instructions
            try {
                mth.load();
                DepthTraversal.visit(new FallbackModeVisitor(), mth);
            } catch (DecodeException e) {
                LOG.error("Error reload instructions in fallback mode:", e);
                code.startLine("// Can't load method instructions: " + e.getMessage());
                return;
            }
        }
    }
    InsnNode[] insnArr = mth.getInstructions();
    if (insnArr == null) {
        code.startLine("// Can't load method instructions.");
        return;
    }
    if (mth.getThisArg() != null) {
        code.startLine(nameGen.useArg(mth.getThisArg())).add(" = this;");
    }
    addFallbackInsns(code, mth, insnArr, true);
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) FallbackModeVisitor(jadx.core.dex.visitors.FallbackModeVisitor) DecodeException(jadx.core.utils.exceptions.DecodeException) JadxErrorAttr(jadx.core.dex.attributes.nodes.JadxErrorAttr)

Aggregations

DecodeException (jadx.core.utils.exceptions.DecodeException)10 InsnNode (jadx.core.dex.nodes.InsnNode)2 JadxException (jadx.core.utils.exceptions.JadxException)2 IOException (java.io.IOException)2 Code (com.android.dex.Code)1 Dex (com.android.dex.Dex)1 DecodedInstruction (com.android.dx.io.instructions.DecodedInstruction)1 FillArrayDataPayloadDecodedInstruction (com.android.dx.io.instructions.FillArrayDataPayloadDecodedInstruction)1 PackedSwitchPayloadDecodedInstruction (com.android.dx.io.instructions.PackedSwitchPayloadDecodedInstruction)1 ShortArrayCodeInput (com.android.dx.io.instructions.ShortArrayCodeInput)1 SparseSwitchPayloadDecodedInstruction (com.android.dx.io.instructions.SparseSwitchPayloadDecodedInstruction)1 ClspGraph (jadx.core.clsp.ClspGraph)1 Annotation (jadx.core.dex.attributes.annotations.Annotation)1 Visibility (jadx.core.dex.attributes.annotations.Annotation.Visibility)1 EnumMapAttr (jadx.core.dex.attributes.nodes.EnumMapAttr)1 JadxErrorAttr (jadx.core.dex.attributes.nodes.JadxErrorAttr)1 SourceFileAttr (jadx.core.dex.attributes.nodes.SourceFileAttr)1 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)1 InsnDecoder (jadx.core.dex.instructions.InsnDecoder)1 ArgType (jadx.core.dex.instructions.args.ArgType)1