Skip ctors and class initializers, and exception frames for now in parallel executor. Found a case of ifeq with one side null vs ifnull, so, need to write some if logic.

This commit is contained in:
Adam
2015-12-31 18:49:48 -05:00
parent c5941c5f6a
commit 6bb02614ca
14 changed files with 252 additions and 94 deletions

View File

@@ -1,8 +1,9 @@
package net.runelite.deob.attributes.code.instruction.types;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
import net.runelite.deob.execution.InstructionContext;
public interface MappableInstruction
{
void map(InstructionContext ctx, InstructionContext other);
void map(ParallelExecutorMapping mappings, InstructionContext ctx, InstructionContext other);
}

View File

@@ -16,6 +16,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import net.runelite.deob.attributes.code.instruction.types.MappableInstruction;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
public abstract class If extends Instruction implements JumpingInstruction, ComparisonInstruction, MappableInstruction
{
@@ -96,7 +97,7 @@ public abstract class If extends Instruction implements JumpingInstruction, Comp
}
@Override
public final/*XXX tmp*/ void map(InstructionContext ctx, InstructionContext other)
public final/*XXX tmp*/ void map(ParallelExecutorMapping mapping, InstructionContext ctx, InstructionContext other)
{
InstructionContext oneLhs = ctx.getPops().get(0).getPushed().resolve(ctx.getPops().get(0)),
oneRhs = ctx.getPops().get(1).getPushed().resolve(ctx.getPops().get(1)),
@@ -116,7 +117,5 @@ public abstract class If extends Instruction implements JumpingInstruction, Comp
branch2.other = branch1;
// we can map these if they are getfield instructions?
//
}
}

View File

@@ -16,6 +16,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import net.runelite.deob.attributes.code.instruction.types.MappableInstruction;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
public abstract class If0 extends Instruction implements JumpingInstruction, ComparisonInstruction, MappableInstruction
{
@@ -97,7 +98,7 @@ public abstract class If0 extends Instruction implements JumpingInstruction, Com
}
@Override
public final/*XXX tmp*/ void map(InstructionContext ctx, InstructionContext other)
public final/*XXX tmp*/ void map(ParallelExecutorMapping mapping, InstructionContext ctx, InstructionContext other)
{
InstructionContext one = ctx.getPops().get(0).getPushed().resolve(ctx.getPops().get(0)),
two = other.getPops().get(0).getPushed().resolve(other.getPops().get(0));

View File

@@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
import net.runelite.deob.execution.Execution;
import net.runelite.deob.execution.Value;
@@ -159,9 +160,14 @@ public class InvokeInterface extends Instruction implements InvokeInstruction
}
@Override
public void map(InstructionContext ctx, InstructionContext other)
public void map(ParallelExecutorMapping mapping, InstructionContext ctx, InstructionContext other)
{
List<net.runelite.deob.Method> myMethods = this.getMethods(),
otherMethods = ((InvokeInterface) other.getInstruction()).getMethods();
assert myMethods.size() == otherMethods.size();
for (int i = 0; i < myMethods.size(); ++i)
mapping.map(myMethods.get(i), otherMethods.get(i));
}
}

View File

@@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
import net.runelite.deob.execution.Execution;
import net.runelite.deob.execution.Value;
@@ -152,9 +153,16 @@ public class InvokeSpecial extends Instruction implements InvokeInstruction
}
@Override
public void map(InstructionContext ctx, InstructionContext other)
public void map(ParallelExecutorMapping mapping, InstructionContext ctx, InstructionContext other)
{
List<net.runelite.deob.Method> myMethods = this.getMethods(),
otherMethods = ((InvokeSpecial) other.getInstruction()).getMethods();
List<net.runelite.deob.Method> m1 = this.myMethods;
List<net.runelite.deob.Method> m2 = ((InvokeSpecial) other.getInstruction()).myMethods;
assert myMethods.size() == otherMethods.size();
for (int i = 0; i < myMethods.size(); ++i)
mapping.map(myMethods.get(i), otherMethods.get(i));
}
}

View File

@@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
import net.runelite.deob.execution.Execution;
import net.runelite.deob.execution.Value;
@@ -162,9 +163,14 @@ public class InvokeStatic extends Instruction implements InvokeInstruction
}
@Override
public void map(InstructionContext ctx, InstructionContext other)
public void map(ParallelExecutorMapping mapping, InstructionContext ctx, InstructionContext other)
{
List<net.runelite.deob.Method> myMethods = this.getMethods(),
otherMethods = ((InvokeStatic) other.getInstruction()).getMethods();
assert myMethods.size() == otherMethods.size();
for (int i = 0; i < myMethods.size(); ++i)
mapping.map(myMethods.get(i), otherMethods.get(i));
}
}

View File

@@ -22,6 +22,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
import net.runelite.deob.execution.Execution;
import net.runelite.deob.execution.Value;
@@ -163,9 +164,14 @@ public class InvokeVirtual extends Instruction implements InvokeInstruction
}
@Override
public void map(InstructionContext ctx, InstructionContext other)
public void map(ParallelExecutorMapping mapping, InstructionContext ctx, InstructionContext other)
{
List<net.runelite.deob.Method> myMethods = this.getMethods(),
otherMethods = ((InvokeVirtual) other.getInstruction()).getMethods();
assert myMethods.size() == otherMethods.size();
for (int i = 0; i < myMethods.size(); ++i)
mapping.map(myMethods.get(i), otherMethods.get(i));
}
}

View File

@@ -17,6 +17,8 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import net.runelite.deob.Method;
import net.runelite.deob.attributes.code.instruction.types.PushConstantInstruction;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
public class PutField extends Instruction implements SetFieldInstruction
{
@@ -94,13 +96,29 @@ public class PutField extends Instruction implements SetFieldInstruction
if (myField != null)
field = myField.getPoolField();
}
private boolean isConstantAssignment(InstructionContext ctx)
{
return ctx.getPops().get(0).getPushed().getInstruction() instanceof PushConstantInstruction;
}
@Override
public void map(InstructionContext ctx, InstructionContext other)
public void map(ParallelExecutorMapping mapping, InstructionContext ctx, InstructionContext other)
{
net.runelite.deob.Field myField = this.getMyField(),
otherField = ((PutField) other.getInstruction()).getMyField();
System.out.println("MAPPING " + myField + " -> " + otherField);
// it appears ConstantValue field attributes are inlined into the constructor
// and their orders scrambled, so don't accept constant value assignments?
// if (ctx.getFrame().getMethod().getName().equals("<init>"))
// {
// //assert isConstantAssignment(ctx) == isConstantAssignment(other);
// //if (isConstantAssignment(ctx))
// return;
// }
// XXX field types must be the same
mapping.map(myField, otherField);
}
}

View File

@@ -17,6 +17,7 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import net.runelite.deob.Method;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
public class PutStatic extends Instruction implements SetFieldInstruction
{
@@ -95,11 +96,11 @@ public class PutStatic extends Instruction implements SetFieldInstruction
}
@Override
public void map(InstructionContext ctx, InstructionContext other)
public void map(ParallelExecutorMapping mapping, InstructionContext ctx, InstructionContext other)
{
net.runelite.deob.Field myField = this.getMyField(),
otherField = ((PutStatic) other.getInstruction()).getMyField();
System.out.println("MAPPING " + myField + " -> " + otherField);
mapping.map(myField, otherField);
}
}

View File

@@ -0,0 +1,112 @@
package net.runelite.deob.deobfuscators.rename;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.runelite.deob.ClassGroup;
import net.runelite.deob.Method;
import net.runelite.deob.attributes.code.Instruction;
import net.runelite.deob.attributes.code.instruction.types.MappableInstruction;
import net.runelite.deob.execution.Execution;
import net.runelite.deob.execution.Frame;
import net.runelite.deob.execution.InstructionContext;
import net.runelite.deob.execution.ParallellMappingExecutor;
import net.runelite.deob.util.JarUtil;
public class MappingExecutorUtil
{
public static boolean isMappable(Method m1, Method m2)
{
assert (m1.getCode() == null) == (m2.getCode() == null);
if (m1.getCode() == null || m2.getCode() == null)
return false;
List<Instruction> l1 = m1.getCode().getInstructions().getInstructions().stream().filter(i -> i instanceof MappableInstruction).collect(Collectors.toList());
List<Instruction> l2 = m2.getCode().getInstructions().getInstructions().stream().filter(i -> i instanceof MappableInstruction).collect(Collectors.toList());
if (l1.size() != l2.size())
return false;
Map<Class, Integer> map1 = new HashMap<>(),
map2 = new HashMap<>();
for (int i = 0; i < l1.size(); ++i)
{
Instruction i1 = l1.get(i);
Integer c = map1.get(i1.getClass());
if (c == null)
map1.put(i1.getClass(), 1);
else
map1.put(i1.getClass(), c + 1);
}
for (int i = 0; i < l2.size(); ++i)
{
Instruction i2 = l2.get(i);
Integer c = map2.get(i2.getClass());
if (c == null)
map2.put(i2.getClass(), 1);
else
map2.put(i2.getClass(), c + 1);
}
return map1.equals(map2);
}
public static ParallelExecutorMapping map(Method m1, Method m2)
{
ClassGroup group1 = m1.getMethods().getClassFile().getGroup();
ClassGroup group2 = m2.getMethods().getClassFile().getGroup();
Execution e = new Execution(group1);
e.step = true;
Frame frame = new Frame(e, m1);
frame.initialize();
e.frames.add(frame);
Execution e2 = new Execution(group2);
e2.step = true;
Frame frame2 = new Frame(e2, m2);
frame2.initialize();
e2.frames.add(frame2);
frame.other = frame2;
frame2.other = frame;
ParallellMappingExecutor parallel = new ParallellMappingExecutor(e, e2);
ParallelExecutorMapping mappings = new ParallelExecutorMapping();
while (parallel.step())
{
// get what each frame is paused/exited on
InstructionContext p1 = parallel.getP1(), p2 = parallel.getP2();
assert e.paused;
assert e2.paused;
System.out.println(p1.getInstruction() + " <-> " + p2.getInstruction());
assert p1.getInstruction().getInstructions().getCode().getAttributes().getMethod() == m1;
assert p2.getInstruction().getInstructions().getCode().getAttributes().getMethod() == m2;
assert p1.getInstruction() instanceof MappableInstruction;
assert p2.getInstruction() instanceof MappableInstruction;
assert p1.getInstruction().getClass().equals(p2.getInstruction().getClass());
MappableInstruction mi1 = (MappableInstruction) p1.getInstruction();
mi1.map(mappings, p1, p2);
e.paused = e2.paused = false;
}
return mappings;
}
}

View File

@@ -0,0 +1,24 @@
package net.runelite.deob.deobfuscators.rename;
import java.util.HashMap;
import java.util.Map;
public class ParallelExecutorMapping
{
private Map<Object, Object> map = new HashMap<>();
public void map(Object one, Object two)
{
if (one.toString().equals("Z class6.field120"))
{
int i =5;
}
assert !map.containsKey(one) || map.get(one) == two;
map.put(one, two);
}
public Map<Object, Object> getMap()
{
return map;
}
}

View File

@@ -172,6 +172,8 @@ public class Rename2
v2.is(v1);
System.out.println(mname(m) + " is " + mname(opt.get()));
executeMethod(m, opt.get());
}
}
@@ -307,6 +309,53 @@ public class Rename2
}
}
private void executeNewMethods()
{
for (Vertex v : g1.getVerticies())
{
Vertex other = v.getOther();
if (other == null || !(v.getObject() instanceof Method))
continue;
executeMethod((Method) v.getObject(), (Method) other.getObject());
}
}
static int count = 0;
private void executeMethod(Method m1, Method m2)
{
// assert (m1.getCode() == null) == (m2.getCode() == null);
//
// if (m1.getCode() == null)
// return;
//
if (!MappingExecutorUtil.isMappable(m1, m2))
return;
if (m1.getName().equals("<clinit>") || m1.getName().equals("<init>"))
return;
ParallelExecutorMapping mapping = MappingExecutorUtil.map(m1, m2);
System.out.println("EXEC " + count++ + " " + mname(m1) + " " + mname(m2) + " " + mapping);
for (Entry<Object, Object> e : mapping.getMap().entrySet())
{
if (e.getKey() instanceof Method)
{
Method m = (Method) e.getKey();
if (m.isStatic())
continue;
}
Vertex v1 = g1.getVertexFor(e.getKey()),
v2 = g2.getVertexFor(e.getValue());
v1.is(v2);
v2.is(v1);
}
// XXX next is use mappings. and then use executeMethod() on more than just the initial method mappings
}
public NameMappings run(ClassGroup one, ClassGroup two)
{
Execution eone = new Execution(one);
@@ -389,6 +438,8 @@ public class Rename2
int after = g1.solved(null);
System.out.println("After " + after);
executeNewMethods();
if (before == after)
break;
}

View File

@@ -238,6 +238,9 @@ public class Frame
private void processExceptions(Instruction i)
{
if (this.execution.step)
return; // no frame.other
Code code = method.getCode();
InstructionContext ictx = instructions.get(instructions.size() - 1);