Unverified Commit ffe739b7 authored by skylot's avatar skylot Committed by GitHub

Merge pull request #343 from Donlon/master

Solve unreplaced fields names when deobfuscation is on (#241)
parents ff7df381 bd05be6f
...@@ -42,7 +42,7 @@ import jadx.core.xmlgen.ResourcesSaver; ...@@ -42,7 +42,7 @@ import jadx.core.xmlgen.ResourcesSaver;
* jadx.load(); * jadx.load();
* jadx.save(); * jadx.save();
* </code></pre> * </code></pre>
* <p/> *
* Instead of 'save()' you can iterate over decompiled classes: * Instead of 'save()' you can iterate over decompiled classes:
* <pre><code> * <pre><code>
* for(JavaClass cls : jadx.getClasses()) { * for(JavaClass cls : jadx.getClasses()) {
......
...@@ -137,13 +137,7 @@ public class InsnGen { ...@@ -137,13 +137,7 @@ public class InsnGen {
private void instanceField(CodeWriter code, FieldInfo field, InsnArg arg) throws CodegenException { private void instanceField(CodeWriter code, FieldInfo field, InsnArg arg) throws CodegenException {
ClassNode pCls = mth.getParentClass(); ClassNode pCls = mth.getParentClass();
FieldNode fieldNode = pCls.searchField(field); FieldNode fieldNode = pCls.dex().root().deepResolveField(field);
while (fieldNode == null
&& pCls.getParentClass() != pCls
&& pCls.getParentClass() != null) {
pCls = pCls.getParentClass();
fieldNode = pCls.searchField(field);
}
if (fieldNode != null) { if (fieldNode != null) {
FieldReplaceAttr replace = fieldNode.get(AType.FIELD_REPLACE); FieldReplaceAttr replace = fieldNode.get(AType.FIELD_REPLACE);
if (replace != null) { if (replace != null) {
...@@ -163,7 +157,11 @@ public class InsnGen { ...@@ -163,7 +157,11 @@ public class InsnGen {
if (fieldNode != null) { if (fieldNode != null) {
code.attachAnnotation(fieldNode); code.attachAnnotation(fieldNode);
} }
code.add(field.getAlias()); if (fieldNode == null) {
code.add(field.getAlias());
} else {
code.add(fieldNode.getAlias());
}
} }
public static void makeStaticFieldAccess(CodeWriter code, FieldInfo field, ClassGen clsGen) { public static void makeStaticFieldAccess(CodeWriter code, FieldInfo field, ClassGen clsGen) {
...@@ -176,11 +174,15 @@ public class InsnGen { ...@@ -176,11 +174,15 @@ public class InsnGen {
} }
code.add('.'); code.add('.');
} }
FieldNode fieldNode = clsGen.getClassNode().dex().resolveField(field); FieldNode fieldNode = clsGen.getClassNode().dex().root().deepResolveField(field);
if (fieldNode != null) { if (fieldNode != null) {
code.attachAnnotation(fieldNode); code.attachAnnotation(fieldNode);
} }
code.add(field.getAlias()); if (fieldNode == null) {
code.add(field.getAlias());
} else {
code.add(fieldNode.getAlias());
}
} }
protected void staticField(CodeWriter code, FieldInfo field) { protected void staticField(CodeWriter code, FieldInfo field) {
......
...@@ -2,7 +2,9 @@ package jadx.core.dex.instructions; ...@@ -2,7 +2,9 @@ package jadx.core.dex.instructions;
import java.io.EOFException; import java.io.EOFException;
import com.android.dex.ClassData;
import com.android.dex.Code; import com.android.dex.Code;
import com.android.dex.FieldId;
import com.android.dx.io.OpcodeInfo; import com.android.dx.io.OpcodeInfo;
import com.android.dx.io.Opcodes; import com.android.dx.io.Opcodes;
import com.android.dx.io.instructions.DecodedInstruction; import com.android.dx.io.instructions.DecodedInstruction;
...@@ -10,6 +12,9 @@ import com.android.dx.io.instructions.FillArrayDataPayloadDecodedInstruction; ...@@ -10,6 +12,9 @@ import com.android.dx.io.instructions.FillArrayDataPayloadDecodedInstruction;
import com.android.dx.io.instructions.PackedSwitchPayloadDecodedInstruction; import com.android.dx.io.instructions.PackedSwitchPayloadDecodedInstruction;
import com.android.dx.io.instructions.ShortArrayCodeInput; import com.android.dx.io.instructions.ShortArrayCodeInput;
import com.android.dx.io.instructions.SparseSwitchPayloadDecodedInstruction; import com.android.dx.io.instructions.SparseSwitchPayloadDecodedInstruction;
import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.FieldNode;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
......
...@@ -323,7 +323,6 @@ public class ClassNode extends LineAttrNode implements ILoadable, IDexNode { ...@@ -323,7 +323,6 @@ public class ClassNode extends LineAttrNode implements ILoadable, IDexNode {
return null; return null;
} }
@TestOnly
public FieldNode searchFieldByName(String name) { public FieldNode searchFieldByName(String name) {
for (FieldNode f : fields) { for (FieldNode f : fields) {
if (f.getName().equals(name)) { if (f.getName().equals(name)) {
......
...@@ -95,6 +95,7 @@ public class DexNode implements IDexNode { ...@@ -95,6 +95,7 @@ public class DexNode implements IDexNode {
return resolveClass(ClassInfo.fromType(root, type)); return resolveClass(ClassInfo.fromType(root, type));
} }
@Deprecated
@Nullable @Nullable
public MethodNode resolveMethod(@NotNull MethodInfo mth) { public MethodNode resolveMethod(@NotNull MethodInfo mth) {
ClassNode cls = resolveClass(mth.getDeclClass()); ClassNode cls = resolveClass(mth.getDeclClass());
...@@ -134,6 +135,7 @@ public class DexNode implements IDexNode { ...@@ -134,6 +135,7 @@ public class DexNode implements IDexNode {
return null; return null;
} }
@Deprecated
@Nullable @Nullable
public FieldNode resolveField(FieldInfo field) { public FieldNode resolveField(FieldInfo field) {
ClassNode cls = resolveClass(field.getDeclClass()); ClassNode cls = resolveClass(field.getDeclClass());
...@@ -143,6 +145,35 @@ public class DexNode implements IDexNode { ...@@ -143,6 +145,35 @@ public class DexNode implements IDexNode {
return null; return null;
} }
@Nullable
FieldNode deepResolveField(@NotNull ClassNode cls, FieldInfo fieldInfo) {
FieldNode field = cls.searchFieldByName(fieldInfo.getName());
if (field != null) {
return field;
}
FieldNode found;
ArgType superClass = cls.getSuperClass();
if (superClass != null) {
ClassNode superNode = resolveClass(superClass);
if (superNode != null) {
found = deepResolveField(superNode, fieldInfo);
if (found != null) {
return found;
}
}
}
for (ArgType iFaceType : cls.getInterfaces()) {
ClassNode iFaceNode = resolveClass(iFaceType);
if (iFaceNode != null) {
found = deepResolveField(iFaceNode, fieldInfo);
if (found != null) {
return found;
}
}
}
return null;
}
public DexFile getDexFile() { public DexFile getDexFile() {
return file; return file;
} }
......
...@@ -5,6 +5,8 @@ import java.io.InputStream; ...@@ -5,6 +5,8 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.android.dex.Dex;
import jadx.core.dex.info.FieldInfo;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -84,12 +86,9 @@ public class RootNode { ...@@ -84,12 +86,9 @@ public class RootNode {
} }
ResTableParser parser = new ResTableParser(); ResTableParser parser = new ResTableParser();
try { try {
ResourcesLoader.decodeStream(arsc, new ResourcesLoader.ResourceDecoder() { ResourcesLoader.decodeStream(arsc, (size, is) -> {
@Override parser.decode(is);
public ResContainer decode(long size, InputStream is) throws IOException { return null;
parser.decode(is);
return null;
}
}); });
} catch (JadxException e) { } catch (JadxException e) {
LOG.error("Failed to parse '.arsc' file", e); LOG.error("Failed to parse '.arsc' file", e);
...@@ -184,6 +183,15 @@ public class RootNode { ...@@ -184,6 +183,15 @@ public class RootNode {
return cls.dex().deepResolveMethod(cls, mth.makeSignature(false)); return cls.dex().deepResolveMethod(cls, mth.makeSignature(false));
} }
@Nullable
public FieldNode deepResolveField(@NotNull FieldInfo field) {
ClassNode cls = resolveClass(field.getDeclClass());
if (cls == null) {
return null;
}
return cls.dex().deepResolveField(cls, field);
}
public List<DexNode> getDexNodes() { public List<DexNode> getDexNodes() {
return dexNodes; return dexNodes;
} }
......
...@@ -78,7 +78,7 @@ public class InsnUtils { ...@@ -78,7 +78,7 @@ public class InsnUtils {
return ((ConstClassNode) insn).getClsType(); return ((ConstClassNode) insn).getClsType();
case SGET: case SGET:
FieldInfo f = (FieldInfo) ((IndexInsnNode) insn).getIndex(); FieldInfo f = (FieldInfo) ((IndexInsnNode) insn).getIndex();
FieldNode fieldNode = dex.resolveField(f); FieldNode fieldNode = dex.root().deepResolveField(f);
if (fieldNode == null) { if (fieldNode == null) {
LOG.warn("Field {} not found in dex {}", f, dex); LOG.warn("Field {} not found in dex {}", f, dex);
return null; return null;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment