From 459d133b5d637a5587d31b541ffccba2613c458c Mon Sep 17 00:00:00 2001 From: Daniel Ramos Date: Sun, 30 Apr 2017 13:34:21 -0400 Subject: [PATCH] Fixed issue where renamed classes/methods referenced in a different dex file would not be renamed properly. Deobfuscation only modified the Class info for the InfoStorage of the Dex file the class belonged to. If a class in another Dex file referenced it, it would not know of the rename. This commit moves InfoStorage to the RootNode. This allows all classes to know of each other regardless of the Dex file. A dexId field was added to the DexNode class to allow the the MethodInfo.fromDex function to continue to use method index to locate methods. The getMethod and putMethod functions in InfoStorage was modified to take a DexNode. The DexNode id is used to create a unique key used for the lookup into the methods HashMap. --- .../main/java/jadx/core/dex/info/ClassInfo.java | 4 ++-- .../main/java/jadx/core/dex/info/FieldInfo.java | 2 +- .../main/java/jadx/core/dex/info/InfoStorage.java | 13 +++++++++---- .../main/java/jadx/core/dex/info/MethodInfo.java | 4 ++-- .../src/main/java/jadx/core/dex/nodes/DexNode.java | 14 +++++++------- .../main/java/jadx/core/dex/nodes/RootNode.java | 9 ++++++++- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/jadx-core/src/main/java/jadx/core/dex/info/ClassInfo.java b/jadx-core/src/main/java/jadx/core/dex/info/ClassInfo.java index 41dd29c4..522c558d 100644 --- a/jadx-core/src/main/java/jadx/core/dex/info/ClassInfo.java +++ b/jadx-core/src/main/java/jadx/core/dex/info/ClassInfo.java @@ -35,12 +35,12 @@ public final class ClassInfo { if (type.isArray()) { type = ArgType.OBJECT; } - ClassInfo cls = dex.getInfoStorage().getCls(type); + ClassInfo cls = dex.root().getInfoStorage().getCls(type); if (cls != null) { return cls; } cls = new ClassInfo(dex, type); - return dex.getInfoStorage().putCls(cls); + return dex.root().getInfoStorage().putCls(cls); } public static ClassInfo fromDex(DexNode dex, int clsIndex) { diff --git a/jadx-core/src/main/java/jadx/core/dex/info/FieldInfo.java b/jadx-core/src/main/java/jadx/core/dex/info/FieldInfo.java index ba106091..000c3767 100644 --- a/jadx-core/src/main/java/jadx/core/dex/info/FieldInfo.java +++ b/jadx-core/src/main/java/jadx/core/dex/info/FieldInfo.java @@ -22,7 +22,7 @@ public final class FieldInfo { public static FieldInfo from(DexNode dex, ClassInfo declClass, String name, ArgType type) { FieldInfo field = new FieldInfo(declClass, name, type); - return dex.getInfoStorage().getField(field); + return dex.root().getInfoStorage().getField(field); } public static FieldInfo fromDex(DexNode dex, int index) { diff --git a/jadx-core/src/main/java/jadx/core/dex/info/InfoStorage.java b/jadx-core/src/main/java/jadx/core/dex/info/InfoStorage.java index e8c18e34..34ad24fc 100644 --- a/jadx-core/src/main/java/jadx/core/dex/info/InfoStorage.java +++ b/jadx-core/src/main/java/jadx/core/dex/info/InfoStorage.java @@ -1,6 +1,7 @@ package jadx.core.dex.info; import jadx.core.dex.instructions.args.ArgType; +import jadx.core.dex.nodes.DexNode; import java.util.HashMap; import java.util.Map; @@ -22,13 +23,17 @@ public class InfoStorage { } } - public MethodInfo getMethod(int mtdId) { - return methods.get(mtdId); + private int generateMethodLookupId(DexNode dex, int mthId) { + return (dex.getDexId()<<16)|mthId; } - public MethodInfo putMethod(int mthId, MethodInfo mth) { + public MethodInfo getMethod(DexNode dex, int mtdId) { + return methods.get(generateMethodLookupId(dex,mtdId)); + } + + public MethodInfo putMethod(DexNode dex, int mthId, MethodInfo mth) { synchronized (methods) { - MethodInfo prev = methods.put(mthId, mth); + MethodInfo prev = methods.put(generateMethodLookupId(dex,mthId), mth); return prev == null ? mth : prev; } } diff --git a/jadx-core/src/main/java/jadx/core/dex/info/MethodInfo.java b/jadx-core/src/main/java/jadx/core/dex/info/MethodInfo.java index 261f9fe4..c63cc650 100644 --- a/jadx-core/src/main/java/jadx/core/dex/info/MethodInfo.java +++ b/jadx-core/src/main/java/jadx/core/dex/info/MethodInfo.java @@ -34,12 +34,12 @@ public final class MethodInfo { } public static MethodInfo fromDex(DexNode dex, int mthIndex) { - MethodInfo mth = dex.getInfoStorage().getMethod(mthIndex); + MethodInfo mth = dex.root().getInfoStorage().getMethod(dex, mthIndex); if (mth != null) { return mth; } mth = new MethodInfo(dex, mthIndex); - return dex.getInfoStorage().putMethod(mthIndex, mth); + return dex.root().getInfoStorage().putMethod(dex, mthIndex, mth); } public String makeSignature(boolean includeRetType) { diff --git a/jadx-core/src/main/java/jadx/core/dex/nodes/DexNode.java b/jadx-core/src/main/java/jadx/core/dex/nodes/DexNode.java index 248f3c0c..4706d75a 100644 --- a/jadx-core/src/main/java/jadx/core/dex/nodes/DexNode.java +++ b/jadx-core/src/main/java/jadx/core/dex/nodes/DexNode.java @@ -35,16 +35,16 @@ public class DexNode implements IDexNode { private final RootNode root; private final Dex dexBuf; private final DexFile file; + private final int dexId; private final List classes = new ArrayList(); private final Map clsMap = new HashMap(); - private final InfoStorage infoStorage = new InfoStorage(); - - public DexNode(RootNode root, DexFile input) { + public DexNode(RootNode root, DexFile input, int dexId) { this.root = root; this.file = input; this.dexBuf = input.getDexBuf(); + this.dexId = dexId; } public void loadClasses() throws DecodeException { @@ -153,10 +153,6 @@ public class DexNode implements IDexNode { return null; } - public InfoStorage getInfoStorage() { - return infoStorage; - } - public DexFile getDexFile() { return file; } @@ -214,6 +210,10 @@ public class DexNode implements IDexNode { return this; } + public int getDexId() { + return dexId; + } + @Override public String toString() { return "DEX"; diff --git a/jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java b/jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java index d121c88d..281d3c01 100644 --- a/jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java +++ b/jadx-core/src/main/java/jadx/core/dex/nodes/RootNode.java @@ -7,6 +7,7 @@ import jadx.api.ResourcesLoader; import jadx.core.clsp.ClspGraph; import jadx.core.dex.info.ClassInfo; import jadx.core.dex.info.ConstStorage; +import jadx.core.dex.info.InfoStorage; import jadx.core.utils.ErrorsCounter; import jadx.core.utils.StringUtils; import jadx.core.utils.android.AndroidResourcesUtils; @@ -34,6 +35,7 @@ public class RootNode { private final IJadxArgs args; private final StringUtils stringUtils; private final ConstStorage constValues; + private final InfoStorage infoStorage = new InfoStorage(); private List dexNodes; @Nullable @@ -53,7 +55,7 @@ public class RootNode { for (DexFile dexFile : input.getDexFiles()) { try { LOG.debug("Load: {}", dexFile); - DexNode dexNode = new DexNode(this, dexFile); + DexNode dexNode = new DexNode(this, dexFile, dexNodes.size()); dexNodes.add(dexNode); } catch (Exception e) { throw new DecodeException("Error decode file: " + dexFile, e); @@ -197,4 +199,9 @@ public class RootNode { public ConstStorage getConstValues() { return constValues; } + + public InfoStorage getInfoStorage() { + return infoStorage; + } + } -- GitLab