Search in sources :

Example 1 with JadxError

use of jadx.core.dex.attributes.nodes.JadxError in project jadx by skylot.

the class CustomStringConcat method buildStringConcat.

public static InsnNode buildStringConcat(InsnData insn, boolean isRange, List<EncodedValue> values) {
    try {
        int argsCount = values.size() - 3 + insn.getRegsCount();
        InsnNode concat = new InsnNode(InsnType.STR_CONCAT, argsCount);
        String recipe = (String) values.get(3).getValue();
        processRecipe(recipe, concat, values, insn);
        int resReg = insn.getResultReg();
        if (resReg != -1) {
            concat.setResult(InsnArg.reg(resReg, ArgType.STRING));
        }
        return concat;
    } catch (Exception e) {
        InsnNode nop = new InsnNode(InsnType.NOP, 0);
        nop.add(AFlag.SYNTHETIC);
        nop.addAttr(AType.JADX_ERROR, new JadxError("Failed to process dynamic string concat: " + e.getMessage(), e));
        return nop;
    }
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) JadxError(jadx.core.dex.attributes.nodes.JadxError) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 2 with JadxError

use of jadx.core.dex.attributes.nodes.JadxError in project jadx by skylot.

the class MethodGen method addFallbackInsns.

public static void addFallbackInsns(ICodeWriter code, MethodNode mth, InsnNode[] insnArr, FallbackOption option) {
    int startIndent = code.getIndent();
    InsnGen insnGen = new InsnGen(getFallbackMethodGen(mth), true);
    InsnNode prevInsn = null;
    for (InsnNode insn : insnArr) {
        if (insn == null) {
            continue;
        }
        if (insn.contains(AType.JADX_ERROR)) {
            for (JadxError error : insn.getAll(AType.JADX_ERROR)) {
                code.startLine("// ").add(error.getError());
            }
            continue;
        }
        if (option != BLOCK_DUMP && needLabel(insn, prevInsn)) {
            code.decIndent();
            code.startLine(getLabelName(insn.getOffset()) + ':');
            code.incIndent();
        }
        if (insn.getType() == InsnType.NOP) {
            continue;
        }
        try {
            boolean escapeComment = isCommentEscapeNeeded(insn, option);
            if (escapeComment) {
                code.decIndent();
                code.startLine("*/");
                code.startLine("//  ");
            } else {
                code.startLineWithNum(insn.getSourceLine());
            }
            InsnCodeOffset.attach(code, insn);
            RegisterArg resArg = insn.getResult();
            if (resArg != null) {
                ArgType varType = resArg.getInitType();
                if (varType.isTypeKnown()) {
                    code.add(varType.toString()).add(' ');
                }
            }
            insnGen.makeInsn(insn, code, InsnGen.Flags.INLINE);
            if (escapeComment) {
                code.startLine("/*");
                code.incIndent();
            }
            CatchAttr catchAttr = insn.get(AType.EXC_CATCH);
            if (catchAttr != null) {
                code.add("     // " + catchAttr);
            }
            CodeGenUtils.addCodeComments(code, mth, insn);
        } catch (Exception e) {
            LOG.debug("Error generate fallback instruction: ", e.getCause());
            code.setIndent(startIndent);
            code.startLine("// error: " + insn);
        }
        prevInsn = insn;
    }
}
Also used : ArgType(jadx.core.dex.instructions.args.ArgType) InsnNode(jadx.core.dex.nodes.InsnNode) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) CatchAttr(jadx.core.dex.trycatch.CatchAttr) JadxError(jadx.core.dex.attributes.nodes.JadxError) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException) CodegenException(jadx.core.utils.exceptions.CodegenException)

Example 3 with JadxError

use of jadx.core.dex.attributes.nodes.JadxError in project jadx by skylot.

the class ErrorsCounter method addError.

private synchronized <N extends IDexNode & IAttributeNode> String addError(N node, String error, @Nullable Throwable e) {
    errorNodes.add(node);
    errorsCount++;
    String msg = formatMsg(node, error);
    if (PRINT_MTH_SIZE && node instanceof MethodNode) {
        msg = "[" + ((MethodNode) node).getInsnsCount() + "] " + msg;
    }
    if (e == null) {
        LOG.error(msg);
    } else if (e instanceof StackOverflowError) {
        LOG.error("{}, error: StackOverflowError", msg);
    } else if (e instanceof JadxOverflowException) {
        // don't print full stack trace
        String details = e.getMessage();
        e = new JadxOverflowException(details);
        if (details == null || details.isEmpty()) {
            LOG.error("{}", msg);
        } else {
            LOG.error("{}, details: {}", msg, details);
        }
    } else {
        LOG.error(msg, e);
    }
    node.addAttr(AType.JADX_ERROR, new JadxError(error, e));
    return msg;
}
Also used : MethodNode(jadx.core.dex.nodes.MethodNode) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException) JadxError(jadx.core.dex.attributes.nodes.JadxError)

Example 4 with JadxError

use of jadx.core.dex.attributes.nodes.JadxError in project jadx by skylot.

the class InsnDecoder method process.

public InsnNode[] process(ICodeReader codeReader) {
    InsnNode[] instructions = new InsnNode[codeReader.getUnitsCount()];
    codeReader.visitInstructions(rawInsn -> {
        int offset = rawInsn.getOffset();
        InsnNode insn;
        try {
            rawInsn.decode();
            insn = decode(rawInsn);
        } catch (Exception e) {
            method.addError("Failed to decode insn: " + rawInsn + ", method: " + method, e);
            insn = new InsnNode(InsnType.NOP, 0);
            insn.addAttr(AType.JADX_ERROR, new JadxError("decode failed: " + e.getMessage(), e));
        }
        insn.setOffset(offset);
        instructions[offset] = insn;
    });
    return instructions;
}
Also used : InsnNode(jadx.core.dex.nodes.InsnNode) JadxError(jadx.core.dex.attributes.nodes.JadxError) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException) DecodeException(jadx.core.utils.exceptions.DecodeException)

Example 5 with JadxError

use of jadx.core.dex.attributes.nodes.JadxError in project jadx by skylot.

the class MethodGen method addFallbackMethodCode.

public void addFallbackMethodCode(ICodeWriter code, FallbackOption fallbackOption) {
    if (fallbackOption != FALLBACK_MODE) {
        // preserve error before unload
        List<JadxError> errors = mth.getAll(AType.JADX_ERROR);
        try {
            // load original instructions
            mth.unload();
            mth.load();
            for (IDexTreeVisitor visitor : Jadx.getFallbackPassesList()) {
                DepthTraversal.visit(visitor, mth);
            }
            errors.forEach(err -> mth.addAttr(AType.JADX_ERROR, err));
        } catch (Exception e) {
            LOG.error("Error reload instructions in fallback mode:", e);
            code.startLine("// Can't load method instructions: " + e.getMessage());
            return;
        } finally {
            errors.forEach(err -> mth.addAttr(AType.JADX_ERROR, err));
        }
    }
    InsnNode[] insnArr = mth.getInstructions();
    if (insnArr == null) {
        code.startLine("// Can't load method instructions.");
        return;
    }
    if (fallbackOption == COMMENTED_DUMP && mth.getCommentsLevel() != CommentsLevel.DEBUG) {
        long insnCountEstimate = Stream.of(insnArr).filter(Objects::nonNull).filter(insn -> insn.getType() != InsnType.NOP).count();
        if (insnCountEstimate > 100) {
            code.incIndent();
            code.startLine("Method dump skipped, instructions count: " + insnArr.length);
            if (code.isMetadataSupported()) {
                code.startLine("To view this dump change 'Code comments level' option to 'DEBUG'");
            } else {
                code.startLine("To view this dump add '--comments-level debug' option");
            }
            code.decIndent();
            return;
        }
    }
    code.incIndent();
    if (mth.getThisArg() != null) {
        code.startLine(nameGen.useArg(mth.getThisArg())).add(" = this;");
    }
    addFallbackInsns(code, mth, insnArr, fallbackOption);
    code.decIndent();
}
Also used : IDexTreeVisitor(jadx.core.dex.visitors.IDexTreeVisitor) ArgType(jadx.core.dex.instructions.args.ArgType) CodeVar(jadx.core.dex.instructions.args.CodeVar) IDexTreeVisitor(jadx.core.dex.visitors.IDexTreeVisitor) JumpInfo(jadx.core.dex.attributes.nodes.JumpInfo) MethodNode(jadx.core.dex.nodes.MethodNode) Consts(jadx.core.Consts) AType(jadx.core.dex.attributes.AType) AFlag(jadx.core.dex.attributes.AFlag) LoggerFactory(org.slf4j.LoggerFactory) InsnType(jadx.core.dex.instructions.InsnType) CatchAttr(jadx.core.dex.trycatch.CatchAttr) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException) AccessInfo(jadx.core.dex.info.AccessInfo) InsnCodeOffset(jadx.api.data.annotations.InsnCodeOffset) IfNode(jadx.core.dex.instructions.IfNode) InsnUtils(jadx.core.utils.InsnUtils) CodeGenUtils(jadx.core.utils.CodeGenUtils) CommentsLevel(jadx.api.CommentsLevel) InsnNode(jadx.core.dex.nodes.InsnNode) SSAVar(jadx.core.dex.instructions.args.SSAVar) VarDeclareRef(jadx.api.data.annotations.VarDeclareRef) MethodOverrideAttr(jadx.core.dex.attributes.nodes.MethodOverrideAttr) CodegenException(jadx.core.utils.exceptions.CodegenException) Jadx(jadx.core.Jadx) EncodedValue(jadx.api.plugins.input.data.annotations.EncodedValue) JadxAttrType(jadx.api.plugins.input.data.attributes.JadxAttrType) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) DepthTraversal(jadx.core.dex.visitors.DepthTraversal) BLOCK_DUMP(jadx.core.codegen.MethodGen.FallbackOption.BLOCK_DUMP) AnnotationMethodParamsAttr(jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr) ConstStringNode(jadx.core.dex.instructions.ConstStringNode) FALLBACK_MODE(jadx.core.codegen.MethodGen.FallbackOption.FALLBACK_MODE) Objects(java.util.Objects) JadxError(jadx.core.dex.attributes.nodes.JadxError) List(java.util.List) Stream(java.util.stream.Stream) AccessFlags(jadx.api.plugins.input.data.AccessFlags) COMMENTED_DUMP(jadx.core.codegen.MethodGen.FallbackOption.COMMENTED_DUMP) Collections(java.util.Collections) ICodeWriter(jadx.api.ICodeWriter) Utils(jadx.core.utils.Utils) InsnNode(jadx.core.dex.nodes.InsnNode) Objects(java.util.Objects) JadxError(jadx.core.dex.attributes.nodes.JadxError) JadxOverflowException(jadx.core.utils.exceptions.JadxOverflowException) CodegenException(jadx.core.utils.exceptions.CodegenException)

Aggregations

JadxError (jadx.core.dex.attributes.nodes.JadxError)5 InsnNode (jadx.core.dex.nodes.InsnNode)4 JadxOverflowException (jadx.core.utils.exceptions.JadxOverflowException)3 ArgType (jadx.core.dex.instructions.args.ArgType)2 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)2 MethodNode (jadx.core.dex.nodes.MethodNode)2 CatchAttr (jadx.core.dex.trycatch.CatchAttr)2 CodegenException (jadx.core.utils.exceptions.CodegenException)2 JadxRuntimeException (jadx.core.utils.exceptions.JadxRuntimeException)2 CommentsLevel (jadx.api.CommentsLevel)1 ICodeWriter (jadx.api.ICodeWriter)1 InsnCodeOffset (jadx.api.data.annotations.InsnCodeOffset)1 VarDeclareRef (jadx.api.data.annotations.VarDeclareRef)1 AccessFlags (jadx.api.plugins.input.data.AccessFlags)1 EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)1 JadxAttrType (jadx.api.plugins.input.data.attributes.JadxAttrType)1 AnnotationMethodParamsAttr (jadx.api.plugins.input.data.attributes.types.AnnotationMethodParamsAttr)1 Consts (jadx.core.Consts)1 Jadx (jadx.core.Jadx)1 BLOCK_DUMP (jadx.core.codegen.MethodGen.FallbackOption.BLOCK_DUMP)1