Search in sources :

Example 31 with FieldInfo

use of jadx.core.dex.info.FieldInfo in project jadx by skylot.

the class ShadowFieldVisitor method searchShadowedFields.

private static Map<FieldInfo, FieldFixType> searchShadowedFields(ClassNode thisCls) {
    List<FieldNode> allFields = collectAllInstanceFields(thisCls);
    if (allFields.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, List<FieldNode>> mapByName = groupByName(allFields);
    mapByName.entrySet().removeIf(entry -> entry.getValue().size() == 1);
    if (mapByName.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<FieldInfo, FieldFixType> fixMap = new HashMap<>();
    for (List<FieldNode> fields : mapByName.values()) {
        boolean fromThisCls = fields.get(0).getParentClass() == thisCls;
        if (fromThisCls && fields.size() == 2) {
            // only one super class contains same field => can use super
            FieldNode otherField = fields.get(1);
            if (otherField.getParentClass() != thisCls) {
                fixMap.put(otherField.getFieldInfo(), FieldFixType.SUPER);
            }
        } else {
            // several super classes contains same field => can't use super, need cast to exact class
            for (FieldNode field : fields) {
                if (field.getParentClass() != thisCls) {
                    fixMap.put(field.getFieldInfo(), FieldFixType.CAST);
                }
            }
        }
    }
    return fixMap;
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) FieldInfo(jadx.core.dex.info.FieldInfo)

Example 32 with FieldInfo

use of jadx.core.dex.info.FieldInfo in project jadx by skylot.

the class SimplifyVisitor method convertFieldArith.

/**
 * Convert field arith operation to arith instruction
 * (IPUT (ARITH (IGET, lit)) -> ARITH ((IGET)) <op>= lit))
 */
private static ArithNode convertFieldArith(MethodNode mth, InsnNode insn) {
    InsnArg arg = insn.getArg(0);
    if (!arg.isInsnWrap()) {
        return null;
    }
    InsnNode wrap = ((InsnWrapArg) arg).getWrapInsn();
    InsnType wrapType = wrap.getType();
    if (wrapType != InsnType.ARITH && wrapType != InsnType.STR_CONCAT || !wrap.getArg(0).isInsnWrap()) {
        return null;
    }
    InsnArg getWrap = wrap.getArg(0);
    InsnNode get = ((InsnWrapArg) getWrap).getWrapInsn();
    InsnType getType = get.getType();
    if (getType != InsnType.IGET && getType != InsnType.SGET) {
        return null;
    }
    FieldInfo field = (FieldInfo) ((IndexInsnNode) insn).getIndex();
    FieldInfo innerField = (FieldInfo) ((IndexInsnNode) get).getIndex();
    if (!field.equals(innerField)) {
        return null;
    }
    try {
        if (getType == InsnType.IGET && insn.getType() == InsnType.IPUT) {
            InsnArg reg = get.getArg(0);
            InsnArg putReg = insn.getArg(1);
            if (!reg.equals(putReg)) {
                return null;
            }
        }
        InsnArg fArg = getWrap.duplicate();
        InsnRemover.unbindInsn(mth, get);
        if (insn.getType() == InsnType.IPUT) {
            InsnRemover.unbindArgUsage(mth, insn.getArg(1));
        }
        if (wrapType == InsnType.ARITH) {
            ArithNode ar = (ArithNode) wrap;
            return ArithNode.oneArgOp(ar.getOp(), fArg, ar.getArg(1));
        }
        int argsCount = wrap.getArgsCount();
        InsnNode concat = new InsnNode(InsnType.STR_CONCAT, argsCount - 1);
        for (int i = 1; i < argsCount; i++) {
            concat.addArg(wrap.getArg(i));
        }
        return ArithNode.oneArgOp(ArithOp.ADD, fArg, InsnArg.wrapArg(concat));
    } catch (Exception e) {
        LOG.debug("Can't convert field arith insn: {}, mth: {}", insn, mth, e);
    }
    return null;
}
Also used : IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnNode(jadx.core.dex.nodes.InsnNode) InsnArg(jadx.core.dex.instructions.args.InsnArg) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg) ArithNode(jadx.core.dex.instructions.ArithNode) InsnType(jadx.core.dex.instructions.InsnType) FieldInfo(jadx.core.dex.info.FieldInfo) JadxRuntimeException(jadx.core.utils.exceptions.JadxRuntimeException)

Example 33 with FieldInfo

use of jadx.core.dex.info.FieldInfo in project jadx by skylot.

the class RenameVisitor method checkFields.

private static void checkFields(Deobfuscator deobfuscator, ClassNode cls, JadxArgs args) {
    Set<String> names = new HashSet<>();
    for (FieldNode field : cls.getFields()) {
        FieldInfo fieldInfo = field.getFieldInfo();
        String fieldName = fieldInfo.getAlias();
        boolean notUnique = !names.add(fieldName);
        boolean notValid = args.isRenameValid() && !NameMapper.isValidIdentifier(fieldName);
        boolean notPrintable = args.isRenamePrintable() && !NameMapper.isAllCharsPrintable(fieldName);
        if (notUnique || notValid || notPrintable) {
            deobfuscator.forceRenameField(field);
            field.addAttr(new RenameReasonAttr(field, notValid, notPrintable));
            if (notUnique) {
                field.addAttr(new RenameReasonAttr(field).append("collision with other field name"));
            }
        }
    }
}
Also used : FieldNode(jadx.core.dex.nodes.FieldNode) RenameReasonAttr(jadx.core.dex.attributes.nodes.RenameReasonAttr) FieldInfo(jadx.core.dex.info.FieldInfo) HashSet(java.util.HashSet)

Example 34 with FieldInfo

use of jadx.core.dex.info.FieldInfo in project jadx by skylot.

the class ExtractFieldInit method insnUseExcludedField.

private static boolean insnUseExcludedField(FieldInitInfo initInfo, Set<FieldInfo> excludedFields) {
    if (excludedFields.isEmpty()) {
        return false;
    }
    IndexInsnNode insn = initInfo.putInsn;
    boolean staticField = insn.getType() == InsnType.SPUT;
    InsnType useType = staticField ? InsnType.SGET : InsnType.IGET;
    // exclude if init code use any excluded field
    Boolean exclude = insn.visitInsns(innerInsn -> {
        if (innerInsn.getType() == useType) {
            FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) innerInsn).getIndex();
            if (excludedFields.contains(fieldInfo)) {
                return true;
            }
        }
        return null;
    });
    return Objects.equals(exclude, Boolean.TRUE);
}
Also used : InsnType(jadx.core.dex.instructions.InsnType) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) FieldInfo(jadx.core.dex.info.FieldInfo)

Example 35 with FieldInfo

use of jadx.core.dex.info.FieldInfo in project jadx by skylot.

the class ExtractFieldInit method processFieldsDependencies.

private static List<FieldNode> processFieldsDependencies(ClassNode cls, List<FieldInitInfo> inits) {
    List<FieldNode> orderedFields = Utils.collectionMap(inits, v -> v.fieldNode);
    // collect dependant fields
    Map<FieldNode, List<FieldNode>> deps = new HashMap<>(inits.size());
    for (FieldInitInfo initInfo : inits) {
        IndexInsnNode insn = initInfo.putInsn;
        boolean staticField = insn.getType() == InsnType.SPUT;
        InsnType useType = staticField ? InsnType.SGET : InsnType.IGET;
        insn.visitInsns(subInsn -> {
            if (subInsn.getType() == useType) {
                FieldInfo fieldInfo = (FieldInfo) ((IndexInsnNode) subInsn).getIndex();
                if (fieldInfo.getDeclClass().equals(cls.getClassInfo())) {
                    FieldNode depField = cls.searchField(fieldInfo);
                    if (depField != null) {
                        deps.computeIfAbsent(initInfo.fieldNode, k -> new ArrayList<>()).add(depField);
                    }
                }
            }
        });
    }
    if (deps.isEmpty()) {
        return orderedFields;
    }
    // build new list with deps fields before usage field
    List<FieldNode> result = new ArrayList<>();
    for (FieldNode field : orderedFields) {
        int idx = result.indexOf(field);
        List<FieldNode> fieldDeps = deps.get(field);
        if (fieldDeps == null) {
            if (idx == -1) {
                result.add(field);
            }
            continue;
        }
        if (idx == -1) {
            for (FieldNode depField : fieldDeps) {
                if (!result.contains(depField)) {
                    result.add(depField);
                }
            }
            result.add(field);
            continue;
        }
        for (FieldNode depField : fieldDeps) {
            int depIdx = result.indexOf(depField);
            if (depIdx == -1) {
                result.add(idx, depField);
            } else if (depIdx > idx) {
                result.remove(depIdx);
                result.add(idx, depField);
            }
        }
    }
    return result;
}
Also used : MethodNode(jadx.core.dex.nodes.MethodNode) AType(jadx.core.dex.attributes.AType) AFlag(jadx.core.dex.attributes.AFlag) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) InsnType(jadx.core.dex.instructions.InsnType) JadxException(jadx.core.utils.exceptions.JadxException) HashMap(java.util.HashMap) FieldInfo(jadx.core.dex.info.FieldInfo) BlockUtils(jadx.core.utils.BlockUtils) AccessInfo(jadx.core.dex.info.AccessInfo) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ClassNode(jadx.core.dex.nodes.ClassNode) Map(java.util.Map) InsnNode(jadx.core.dex.nodes.InsnNode) FieldNode(jadx.core.dex.nodes.FieldNode) CodeShrinkVisitor(jadx.core.dex.visitors.shrink.CodeShrinkVisitor) InsnArg(jadx.core.dex.instructions.args.InsnArg) FieldInitInsnAttr(jadx.core.dex.attributes.FieldInitInsnAttr) InsnWrapArg(jadx.core.dex.instructions.args.InsnWrapArg) JadxAttrType(jadx.api.plugins.input.data.attributes.JadxAttrType) RegisterArg(jadx.core.dex.instructions.args.RegisterArg) Set(java.util.Set) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) BlockNode(jadx.core.dex.nodes.BlockNode) InsnRemover(jadx.core.utils.InsnRemover) Collections(java.util.Collections) Utils(jadx.core.utils.Utils) FieldNode(jadx.core.dex.nodes.FieldNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InsnType(jadx.core.dex.instructions.InsnType) ArrayList(java.util.ArrayList) List(java.util.List) IndexInsnNode(jadx.core.dex.instructions.IndexInsnNode) FieldInfo(jadx.core.dex.info.FieldInfo)

Aggregations

FieldInfo (jadx.core.dex.info.FieldInfo)41 FieldNode (jadx.core.dex.nodes.FieldNode)24 IndexInsnNode (jadx.core.dex.instructions.IndexInsnNode)20 InsnNode (jadx.core.dex.nodes.InsnNode)16 InsnArg (jadx.core.dex.instructions.args.InsnArg)15 InsnWrapArg (jadx.core.dex.instructions.args.InsnWrapArg)8 ClassNode (jadx.core.dex.nodes.ClassNode)8 ArgType (jadx.core.dex.instructions.args.ArgType)7 ArrayList (java.util.ArrayList)7 InsnType (jadx.core.dex.instructions.InsnType)6 MethodNode (jadx.core.dex.nodes.MethodNode)6 Nullable (org.jetbrains.annotations.Nullable)6 ConstClassNode (jadx.core.dex.instructions.ConstClassNode)5 ConstStringNode (jadx.core.dex.instructions.ConstStringNode)5 RegisterArg (jadx.core.dex.instructions.args.RegisterArg)5 BlockNode (jadx.core.dex.nodes.BlockNode)5 HashMap (java.util.HashMap)5 List (java.util.List)5 EncodedValue (jadx.api.plugins.input.data.annotations.EncodedValue)4 MethodInfo (jadx.core.dex.info.MethodInfo)4