From e3a6310572888bdc785762d6a1313a12225ee306 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 27 Mar 2016 10:44:55 -0400 Subject: [PATCH] Get rid of most hard type comparisons in mapper --- .../asm/attributes/code/instructions/If.java | 13 ++++--- .../asm/attributes/code/instructions/If0.java | 22 ++--------- .../code/instructions/InvokeInterface.java | 10 ++--- .../code/instructions/InvokeSpecial.java | 10 ++--- .../code/instructions/InvokeStatic.java | 4 +- .../code/instructions/InvokeVirtual.java | 18 ++++----- .../code/instructions/PutField.java | 8 ++-- .../code/instructions/PutStatic.java | 12 ++++-- .../rename/MappingExecutorUtil.java | 37 +++++++++++++++++++ .../rename/MethodSignatureMapper.java | 4 +- .../rename/StaticMethodSignatureMapper.java | 4 +- .../net/runelite/deob/injection/Inject.java | 1 + .../deob/injection/InjectReplace.java | 1 + 13 files changed, 90 insertions(+), 54 deletions(-) diff --git a/src/main/java/net/runelite/asm/attributes/code/instructions/If.java b/src/main/java/net/runelite/asm/attributes/code/instructions/If.java index d1636dc178..5fa2d93261 100644 --- a/src/main/java/net/runelite/asm/attributes/code/instructions/If.java +++ b/src/main/java/net/runelite/asm/attributes/code/instructions/If.java @@ -162,7 +162,7 @@ public abstract class If extends Instruction implements JumpingInstruction, Comp { Field f1 = f1s.get(0), f2 = f2s.get(0); - assert f1.getType().equals(f2.getType()); + assert MappingExecutorUtil.isMaybeEqual(f1.getType(), f2.getType()); mapping.map(f1, f2); @@ -186,7 +186,10 @@ public abstract class If extends Instruction implements JumpingInstruction, Comp Field j1 = f1s.get(1), j2 = f2s.get(1); - assert !(couldBeSame(f1, f2) && couldBeSame(j1, j2) && couldBeSame(f1, j2) && couldBeSame(j1, f2)); + if (couldBeSame(f1, f2) && couldBeSame(j1, j2) && couldBeSame(f1, j2) && couldBeSame(j1, f2)) + { + return; // ambiguous + } if (couldBeSame(f1, f2) && couldBeSame(j1, j2)) { @@ -250,10 +253,10 @@ public abstract class If extends Instruction implements JumpingInstruction, Comp return false; if (!f1.isStatic()) - if (!f1.getFields().getClassFile().getName().equals(f2.getFields().getClassFile().getName())) + if (!MappingExecutorUtil.isMaybeEqual(f1.getFields().getClassFile(), f2.getFields().getClassFile())) return false; - return f1.getType().equals(f2.getType()); + return MappingExecutorUtil.isMaybeEqual(f1.getType(), f2.getType()); } protected boolean isSameField(InstructionContext thisIc, InstructionContext otherIc) @@ -277,7 +280,7 @@ public abstract class If extends Instruction implements JumpingInstruction, Comp Field j1 = f1s.get(1), j2 = f2s.get(1); if (couldBeSame(f1, f2) && couldBeSame(j1, j2) && couldBeSame(f1, j2) && couldBeSame(j1, f2)) - return false; // ambiguous + return true; if (couldBeSame(f1, f2) && couldBeSame(j1, j2)) return true; diff --git a/src/main/java/net/runelite/asm/attributes/code/instructions/If0.java b/src/main/java/net/runelite/asm/attributes/code/instructions/If0.java index 302ea7381d..8d9216ae6b 100644 --- a/src/main/java/net/runelite/asm/attributes/code/instructions/If0.java +++ b/src/main/java/net/runelite/asm/attributes/code/instructions/If0.java @@ -135,20 +135,6 @@ public abstract class If0 extends Instruction implements JumpingInstruction, Com f2.other = branch1; branch1.other = f2; - // switch frame order in executor frame list - -// Execution e = f1.getExecution(), -// e2 = f2.getExecution(); -// -// int i = e2.frames.indexOf(f2), -// i2 = e2.frames.indexOf(branch2); -// -// e2.frames.remove(i); -// e2.frames.add(i, branch2); -// -// e2.frames.remove(i2); -// e2.frames.add(i2, f2); - this.mapArguments(mapping, ctx, other); } @@ -157,8 +143,8 @@ public abstract class If0 extends Instruction implements JumpingInstruction, Com Field f1 = getComparedField(ctx), f2 = getComparedField(other); if (f1 == null || f2 == null) return; - - assert f1.getType().equals(f2.getType()); + + assert MappingExecutorUtil.isMaybeEqual(f1.getType(), f2.getType()); mapping.map(f1, f2); } @@ -199,10 +185,10 @@ public abstract class If0 extends Instruction implements JumpingInstruction, Com return false; if (!f1.isStatic()) - if (!f1.getFields().getClassFile().getName().equals(f2.getFields().getClassFile().getName())) + if (!MappingExecutorUtil.isMaybeEqual(f1.getFields().getClassFile(), f2.getFields().getClassFile())) return false; - return f1.getType().equals(f2.getType()); + return MappingExecutorUtil.isMaybeEqual(f1.getType(), f2.getType()); } @Override diff --git a/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeInterface.java b/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeInterface.java index b2fadd4454..922815b1a5 100644 --- a/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeInterface.java +++ b/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeInterface.java @@ -242,8 +242,8 @@ public class InvokeInterface extends Instruction implements InvokeInstruction InvokeInterface thisIi = (InvokeInterface) thisIc.getInstruction(), otherIi = (InvokeInterface) otherIc.getInstruction(); - - if (!thisIi.method.getNameAndType().getDescriptor().equals(otherIi.method.getNameAndType().getDescriptor())) + + if (!MappingExecutorUtil.isMaybeEqual(thisIi.method.getNameAndType().getDescriptor(), otherIi.method.getNameAndType().getDescriptor())) return false; List thisMethods = thisIi.getMethods(), @@ -262,11 +262,11 @@ public class InvokeInterface extends Instruction implements InvokeInstruction { net.runelite.asm.Method m1 = thisMethods.get(i); net.runelite.asm.Method m2 = otherMethods.get(i); - - if (!m1.getMethods().getClassFile().getPoolClass().equals(m2.getMethods().getClassFile().getPoolClass())) + + if (!MappingExecutorUtil.isMaybeEqual(m1.getMethods().getClassFile(), m2.getMethods().getClassFile())) return false; - if (!m1.getNameAndType().getDescriptor().equals(m2.getNameAndType().getDescriptor())) + if (!MappingExecutorUtil.isMaybeEqual(m1.getNameAndType().getDescriptor(), m2.getNameAndType().getDescriptor())) return false; } diff --git a/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeSpecial.java b/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeSpecial.java index 85fbeed866..d6c3a790b7 100644 --- a/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeSpecial.java +++ b/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeSpecial.java @@ -243,8 +243,8 @@ public class InvokeSpecial extends Instruction implements InvokeInstruction InvokeSpecial thisIi = (InvokeSpecial) thisIc.getInstruction(), otherIi = (InvokeSpecial) otherIc.getInstruction(); - - if (!thisIi.method.getNameAndType().getDescriptor().equals(otherIi.method.getNameAndType().getDescriptor())) + + if (!MappingExecutorUtil.isMaybeEqual(thisIi.method.getNameAndType().getDescriptor(), otherIi.method.getNameAndType().getDescriptor())) return false; List thisMethods = thisIi.getMethods(), @@ -266,11 +266,11 @@ public class InvokeSpecial extends Instruction implements InvokeInstruction { net.runelite.asm.Method m1 = thisMethods.get(i); net.runelite.asm.Method m2 = otherMethods.get(i); - - if (!m1.getMethods().getClassFile().getPoolClass().equals(m2.getMethods().getClassFile().getPoolClass())) + + if (!MappingExecutorUtil.isMaybeEqual(m1.getMethods().getClassFile(), m2.getMethods().getClassFile())) return false; - if (!m1.getNameAndType().getDescriptor().equals(m2.getNameAndType().getDescriptor())) + if (!MappingExecutorUtil.isMaybeEqual(m1.getNameAndType().getDescriptor(), m2.getNameAndType().getDescriptor())) return false; } diff --git a/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeStatic.java b/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeStatic.java index 141a461db9..2c7176da70 100644 --- a/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeStatic.java +++ b/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeStatic.java @@ -213,8 +213,8 @@ public class InvokeStatic extends Instruction implements InvokeInstruction InvokeStatic thisIi = (InvokeStatic) thisIc.getInstruction(), otherIi = (InvokeStatic) otherIc.getInstruction(); - - if (!thisIi.method.getNameAndType().getDescriptor().equals(otherIi.method.getNameAndType().getDescriptor())) + + if (!MappingExecutorUtil.isMaybeEqual(thisIi.method.getNameAndType().getDescriptor(), otherIi.method.getNameAndType().getDescriptor())) return false; List thisMethods = thisIi.getMethods(), diff --git a/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeVirtual.java b/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeVirtual.java index 63aff4f237..9b0fa38501 100644 --- a/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeVirtual.java +++ b/src/main/java/net/runelite/asm/attributes/code/instructions/InvokeVirtual.java @@ -177,8 +177,8 @@ public class InvokeVirtual extends Instruction implements InvokeInstruction List myMethods = this.getMethods(), otherMethods = otherIv.getMethods(); - - assert method.getNameAndType().getDescriptor().equals(otherIv.method.getNameAndType().getDescriptor()); + + assert MappingExecutorUtil.isMaybeEqual(method.getNameAndType().getDescriptor(), otherIv.method.getNameAndType().getDescriptor()); assert myMethods.size() == otherMethods.size(); for (int i = 0; i < myMethods.size(); ++i) @@ -186,7 +186,7 @@ public class InvokeVirtual extends Instruction implements InvokeInstruction net.runelite.asm.Method m1 = myMethods.get(i); net.runelite.asm.Method m2 = otherMethods.get(i); - assert m1.getMethods().getClassFile().getName().equals(m2.getMethods().getClassFile().getName()); + assert MappingExecutorUtil.isMaybeEqual(m1.getMethods().getClassFile(), m2.getMethods().getClassFile()); mapping.map(m1, m2); } @@ -248,8 +248,8 @@ public class InvokeVirtual extends Instruction implements InvokeInstruction InvokeVirtual thisIi = (InvokeVirtual) thisIc.getInstruction(), otherIi = (InvokeVirtual) otherIc.getInstruction(); - - if (!thisIi.method.getNameAndType().getDescriptor().equals(otherIi.method.getNameAndType().getDescriptor())) + + if (!MappingExecutorUtil.isMaybeEqual(thisIi.method.getNameAndType().getDescriptor(), otherIi.method.getNameAndType().getDescriptor())) return false; List thisMethods = thisIi.getMethods(), @@ -268,11 +268,11 @@ public class InvokeVirtual extends Instruction implements InvokeInstruction { net.runelite.asm.Method m1 = thisMethods.get(i); net.runelite.asm.Method m2 = otherMethods.get(i); - - if (!m1.getMethods().getClassFile().getPoolClass().equals(m2.getMethods().getClassFile().getPoolClass())) + + if (!MappingExecutorUtil.isMaybeEqual(m1.getMethods().getClassFile(), m2.getMethods().getClassFile())) return false; - - if (!m1.getNameAndType().getDescriptor().equals(m2.getNameAndType().getDescriptor())) + + if (!MappingExecutorUtil.isMaybeEqual(m1.getNameAndType().getDescriptor(), m2.getNameAndType().getDescriptor())) return false; } diff --git a/src/main/java/net/runelite/asm/attributes/code/instructions/PutField.java b/src/main/java/net/runelite/asm/attributes/code/instructions/PutField.java index 5a4b69f0d2..d86e011b5c 100644 --- a/src/main/java/net/runelite/asm/attributes/code/instructions/PutField.java +++ b/src/main/java/net/runelite/asm/attributes/code/instructions/PutField.java @@ -106,7 +106,7 @@ public class PutField extends Instruction implements SetFieldInstruction net.runelite.asm.Field myField = this.getMyField(); net.runelite.asm.Field otherField = ((PutField) other.getInstruction()).getMyField(); - assert myField.getType().equals(otherField.getType()); + assert MappingExecutorUtil.isMaybeEqual(myField.getType(), otherField.getType()); mapping.map(myField, otherField); @@ -149,9 +149,9 @@ public class PutField extends Instruction implements SetFieldInstruction if ((f1 != null) != (f2 != null)) return false; - - return f1.getFields().getClassFile().getPoolClass().equals(f2.getFields().getClassFile().getPoolClass()) - && f1.getType().equals(f2.getType()); + + return MappingExecutorUtil.isMaybeEqual(f1.getFields().getClassFile(), f2.getFields().getClassFile()) + && MappingExecutorUtil.isMaybeEqual(f1.getType(), f2.getType()); } @Override diff --git a/src/main/java/net/runelite/asm/attributes/code/instructions/PutStatic.java b/src/main/java/net/runelite/asm/attributes/code/instructions/PutStatic.java index 5436a720c4..8ac43c7300 100644 --- a/src/main/java/net/runelite/asm/attributes/code/instructions/PutStatic.java +++ b/src/main/java/net/runelite/asm/attributes/code/instructions/PutStatic.java @@ -103,8 +103,8 @@ public class PutStatic extends Instruction implements SetFieldInstruction { net.runelite.asm.Field myField = this.getMyField(); net.runelite.asm.Field otherField = ((PutStatic) other.getInstruction()).getMyField(); - - assert myField.getType().equals(otherField.getType()); + + assert MappingExecutorUtil.isMaybeEqual(myField.getType(), otherField.getType()); mapping.map(myField, otherField); @@ -137,9 +137,15 @@ public class PutStatic extends Instruction implements SetFieldInstruction PutStatic thisPf = (PutStatic) thisIc.getInstruction(), otherPf = (PutStatic) otherIc.getInstruction(); + + net.runelite.asm.Field f1 = thisPf.getMyField(); + net.runelite.asm.Field f2 = otherPf.getMyField(); + + if ((f1 != null) != (f2 != null)) + return false; /* The class names are random */ - return thisPf.getField().getNameAndType().getDescriptorType().equals(otherPf.getField().getNameAndType().getDescriptorType()); + return MappingExecutorUtil.isMaybeEqual(f1.getType(), f2.getType()); } @Override diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/MappingExecutorUtil.java b/src/main/java/net/runelite/deob/deobfuscators/rename/MappingExecutorUtil.java index f093fc68c8..70df4f4214 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/rename/MappingExecutorUtil.java +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/MappingExecutorUtil.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import net.runelite.asm.ClassFile; import net.runelite.asm.ClassGroup; import net.runelite.asm.Method; import net.runelite.asm.attributes.code.Instruction; @@ -23,6 +24,7 @@ import net.runelite.asm.execution.StackContext; import net.runelite.asm.execution.VariableContext; import net.runelite.asm.execution.Variables; import net.runelite.asm.signature.Signature; +import net.runelite.asm.signature.Type; public class MappingExecutorUtil { @@ -292,4 +294,39 @@ public class MappingExecutorUtil return ctx; } + + public static boolean isMaybeEqual(Type t1, Type t2) + { + if (t1.isPrimitive() != t2.isPrimitive()) + return false; + + if (t1.getArrayDims() != t2.getArrayDims()) + return false; + + return true; + } + + public static boolean isMaybeEqual(Signature s1, Signature s2) + { + if (s1.size() != s2.size()) + return false; + + if (!isMaybeEqual(s1.getReturnValue(), s2.getReturnValue())) + return false; + + for (int i = 0; i < s1.size(); ++i) + { + Type t1 = s1.getTypeOfArg(i), t2 = s2.getTypeOfArg(i); + + if (!isMaybeEqual(t1, t2)) + return false; + } + + return true; + } + + public static boolean isMaybeEqual(ClassFile cf1, ClassFile cf2) + { + return true; + } } diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/MethodSignatureMapper.java b/src/main/java/net/runelite/deob/deobfuscators/rename/MethodSignatureMapper.java index 484737983f..a612da4e92 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/rename/MethodSignatureMapper.java +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/MethodSignatureMapper.java @@ -22,8 +22,8 @@ public class MethodSignatureMapper { if (m2.getCode() == null) continue; - - if (!m.getDescriptor().equals(m2.getDescriptor())) + + if (!MappingExecutorUtil.isMaybeEqual(m.getDescriptor(), m2.getDescriptor())) continue; boolean isConstructor2 = m2.getName().equals(""); diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/StaticMethodSignatureMapper.java b/src/main/java/net/runelite/deob/deobfuscators/rename/StaticMethodSignatureMapper.java index 477756118e..f941fec8a7 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/rename/StaticMethodSignatureMapper.java +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/StaticMethodSignatureMapper.java @@ -26,7 +26,9 @@ public class StaticMethodSignatureMapper private List getStaticMethodsOfSignature(ClassGroup group, Signature sig) { - return getStaticMethods(group).stream().filter(m -> m.getDescriptor().equals(sig)).collect(Collectors.toList()); + return getStaticMethods(group).stream().filter( + m -> MappingExecutorUtil.isMaybeEqual(m.getDescriptor(), sig) + ).collect(Collectors.toList()); } public void map(ClassGroup group1, ClassGroup group2) diff --git a/src/main/java/net/runelite/deob/injection/Inject.java b/src/main/java/net/runelite/deob/injection/Inject.java index fbe053cbdc..5bea03d375 100644 --- a/src/main/java/net/runelite/deob/injection/Inject.java +++ b/src/main/java/net/runelite/deob/injection/Inject.java @@ -355,6 +355,7 @@ public class Inject Type lastGarbageArgumentType = null; + assert false; if (!deobfuscatedMethod.getDescriptor().equals(invokeMethod.getDescriptor())) { // allow for obfuscated method to have a single bogus signature at the end diff --git a/src/main/java/net/runelite/deob/injection/InjectReplace.java b/src/main/java/net/runelite/deob/injection/InjectReplace.java index 01c6c33dac..5b579623af 100644 --- a/src/main/java/net/runelite/deob/injection/InjectReplace.java +++ b/src/main/java/net/runelite/deob/injection/InjectReplace.java @@ -232,6 +232,7 @@ public class InjectReplace // Rename method to override m.setName(obfuscatedMethodToOverride.getName()); + assert false; if (!m.getDescriptor().equals(obfuscatedMethodToOverride.getDescriptor())) { // Obfuscation can add garbage parameter.