From 60d4cd94fd68222f9447ae2644e23d341d59da11 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 9 Nov 2015 19:58:15 -0500 Subject: [PATCH] This does 326 methods. --- .../deob/deobfuscators/rename/Rename.java | 61 ++++++---- .../runelite/deob/execution/Execution.java | 14 +++ .../net/runelite/deob/execution/Frame.java | 107 ++--------------- .../deob/execution/MethodContext.java | 113 ++++++++++++++++++ 4 files changed, 174 insertions(+), 121 deletions(-) create mode 100644 src/main/java/net/runelite/deob/execution/MethodContext.java diff --git a/src/main/java/net/runelite/deob/deobfuscators/rename/Rename.java b/src/main/java/net/runelite/deob/deobfuscators/rename/Rename.java index 8140d4c88b..24a968c072 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/rename/Rename.java +++ b/src/main/java/net/runelite/deob/deobfuscators/rename/Rename.java @@ -32,14 +32,14 @@ public class Rename private boolean compare(Frame f1, Frame f2) { - Graph g1 = f1.getGraph(), g2 = f2.getGraph(); + Graph g1 = f1.getMethodCtx().getGraph(), g2 = f2.getMethodCtx().getGraph(); IsomorphismTester isoTest = new TypedVF2IsomorphismTester(); if (!isoTest.areIsomorphic(g1, g2)) return false; Map mapping = isoTest.findIsomorphism(g1, g2); - Map map1 = f1.getIdMap(), map2 = f2.getIdMap(); + Map map1 = f1.getMethodCtx().getIdMap(), map2 = f2.getMethodCtx().getIdMap(); for (Entry e : mapping.entrySet()) { @@ -78,17 +78,26 @@ public class Rename List f1 = eone.processedFrames.stream().filter(f -> f.getMethod() == one).collect(Collectors.toList()); List f2 = etwo.processedFrames.stream().filter(f -> f.getMethod() == two).collect(Collectors.toList()); + Frame p1 = null, p2 = null; outer: + for (Frame fr1 : f1) + for (Frame fr2 : f2) + { + if (p1 == null) p1 = fr1; + if (p2 == null) p2 = fr2; + + assert fr1.getMethodCtx() == p1.getMethodCtx(); + assert fr2.getMethodCtx() == p2.getMethodCtx(); + } + for (Frame fr1 : f1) for (Frame fr2 : f2) { compare(fr1, fr2); - //if (compare(fr1, fr2)) - // break outer; + break; } System.out.println("end"); - //return; } public void run(ClassGroup one, ClassGroup two) { @@ -108,6 +117,8 @@ public class Rename { Method m1 = initial1.get(i), m2 = initial2.get(i); + assert m1.getName().equals(m2.getName()); + objMap.put(m1, m2); } @@ -116,27 +127,27 @@ public class Rename // initial2.get(0).getMethod() // ); // processed.add(initial1.get(0).getMethod()); - process( - one.findClass("class143").findMethod("run"), - two.findClass("class143").findMethod("run") - ); +// process( +// one.findClass("class143").findMethod("run"), +// two.findClass("class143").findMethod("run") +// ); // processed.add(one.findClass("client").findMethod("init")); -// for (;;) -// { -// Optional next = objMap.keySet().stream() -// .filter(m -> !processed.contains(m)) -// .findAny(); -// if (!next.isPresent()) -// break; -// -// Method m = (Method) next.get(); -// Method m2 = (Method) objMap.get(m); -// -// System.out.println("Scanning " + m.getName() + " -> " + m2.getName()); -// process(m, m2); -// processed.add(m); -// } + for (;;) + { + Optional next = objMap.keySet().stream() + .filter(m -> !processed.contains(m)) + .findAny(); + if (!next.isPresent()) + break; + + Method m = (Method) next.get(); + Method m2 = (Method) objMap.get(m); + + System.out.println("Scanning " + m.getName() + " -> " + m2.getName()); + process(m, m2); + processed.add(m); + } for (Entry e : objMap.entrySet()) { @@ -146,6 +157,6 @@ public class Rename System.out.println("FINAL " + m1.getMethods().getClassFile().getName() + "." + m1.getName() + " -> " + m2.getMethods().getClassFile().getName() + "." + m2.getName()); } - System.out.println("done"); + System.out.println("done count " + objMap.size()); } } diff --git a/src/main/java/net/runelite/deob/execution/Execution.java b/src/main/java/net/runelite/deob/execution/Execution.java index d630b2c1c9..9dbefab8c8 100644 --- a/src/main/java/net/runelite/deob/execution/Execution.java +++ b/src/main/java/net/runelite/deob/execution/Execution.java @@ -8,8 +8,10 @@ import net.runelite.deob.attributes.code.Instruction; import java.util.ArrayList; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import net.runelite.deob.deobfuscators.arithmetic.Encryption; import org.apache.commons.collections4.map.MultiValueMap; @@ -24,6 +26,7 @@ public class Execution private MultiValueMap invokes = new MultiValueMap<>(); private Encryption encryption; public MultiValueMap contexts = new MultiValueMap<>(); + private Map methodContexts = new HashMap<>(); public Execution(ClassGroup group) { @@ -128,4 +131,15 @@ public class Execution { return contexts.getCollection(i); } + + public MethodContext getMethodContext(Method m) + { + MethodContext c = methodContexts.get(m); + if (c != null) + return c; + + c = new MethodContext(m); + methodContexts.put(m, c); + return c; + } } diff --git a/src/main/java/net/runelite/deob/execution/Frame.java b/src/main/java/net/runelite/deob/execution/Frame.java index 9bf7a90eab..6f4501f6f0 100644 --- a/src/main/java/net/runelite/deob/execution/Frame.java +++ b/src/main/java/net/runelite/deob/execution/Frame.java @@ -29,15 +29,7 @@ public class Frame private Stack stack; private Variables variables; private List instructions = new ArrayList<>(); // instructions executed in this frame - private MultiValueMap visited = new MultiValueMap<>(); // shared - - private IdGen ids = new IdGen(); - private Map idMap = new HashMap<>(); - private Map insMap = new HashMap<>(); - private Graph graph = new SparseDirectedGraph(); // shared. - private int prevVertex = -1; - - public static long num; + private MethodContext ctx; public Frame(Execution execution, Method method) { @@ -48,6 +40,7 @@ public class Frame stack = new Stack(code.getMaxStack()); variables = new Variables(code.getMaxLocals()); + ctx = execution.getMethodContext(method); } public void initialize() @@ -87,7 +80,7 @@ public class Frame { StackContext argument = pops.remove(0); - variables.set(lvtOffset, new VariableContext(ctx, argument));//new Type(nat.getDescriptor().getTypeOfArg(i)).toStackType())); + variables.set(lvtOffset, new VariableContext(ctx, argument)); lvtOffset += nat.getDescriptor().getTypeOfArg(i).getSlots(); } @@ -105,12 +98,7 @@ public class Frame this.cur = other.cur; this.stack = new Stack(other.stack); this.variables = new Variables(other.variables); - this.visited = other.visited; - - this.ids = other.ids; - this.idMap = other.idMap; - this.graph = other.graph; - this.prevVertex = other.prevVertex; + this.ctx = other.ctx; } public Frame dup() @@ -149,6 +137,11 @@ public class Frame { return variables; } + + public MethodContext getMethodCtx() + { + return ctx; + } public void addInstructionContext(InstructionContext i) { @@ -201,7 +194,7 @@ public class Frame if (!executing) break; - buildGraph(oldCur); + ctx.buildGraph(oldCur); if (oldCur == cur) { @@ -244,23 +237,13 @@ public class Frame } } - private boolean hasJumped(InstructionContext from, Instruction to) - { - Collection i = visited.getCollection(from); - if (i != null && i.contains(to)) - return true; - - visited.put(from, to); - return false; - } - public void jump(InstructionContext from, Instruction to) { assert to != null; assert to.getInstructions() == method.getCode().getInstructions(); assert method.getCode().getInstructions().getInstructions().contains(to); - if (hasJumped(from, to)) + if (ctx.hasJumped(from, to)) { executing = false; return; @@ -268,72 +251,4 @@ public class Frame cur = to; } - - public Graph getGraph() - { - return graph; - } - - public Map getIdMap() - { - return idMap; - } - - private int getIdFor(Instruction i) - { - if (insMap.containsKey(i)) - return insMap.get(i); - - int id = ids.get(); - - graph.add(id); - - this.idMap.put(id, i); - this.insMap.put(i, id); - - return id; - } - - private void buildGraph(Instruction i) - { - if (i instanceof InvokeInstruction) - { - InvokeInstruction ii = (InvokeInstruction) i; - - List methods = ii.getMethods(); - if (methods.isEmpty()) - return; - } -// else if (i instanceof FieldInstruction) -// { -// FieldInstruction fi = (FieldInstruction) i; -// -// if (fi.getMyField() == null) -// return; -// } - else - { - return; - } - - if (prevVertex == -1) - { - int id = getIdFor(i); - //int id = ids.get(); - //graph.add(id); - prevVertex = id; - //this.idMap.put(id, i); - return; - } - - int id = getIdFor(i); - //int id = ids.get(); - //graph.add(id); - //idMap.put(id, i); - - DirectedEdge edge = new SimpleDirectedEdge(prevVertex, id); - graph.add(edge); - - prevVertex = id; - } } diff --git a/src/main/java/net/runelite/deob/execution/MethodContext.java b/src/main/java/net/runelite/deob/execution/MethodContext.java new file mode 100644 index 0000000000..df76b68c7f --- /dev/null +++ b/src/main/java/net/runelite/deob/execution/MethodContext.java @@ -0,0 +1,113 @@ +package net.runelite.deob.execution; + +import edu.ucla.sspace.graph.DirectedEdge; +import edu.ucla.sspace.graph.Graph; +import edu.ucla.sspace.graph.SimpleDirectedEdge; +import edu.ucla.sspace.graph.SparseDirectedGraph; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import net.runelite.deob.Method; +import net.runelite.deob.attributes.code.Instruction; +import net.runelite.deob.attributes.code.instruction.types.InvokeInstruction; +import net.runelite.deob.util.IdGen; +import org.apache.commons.collections4.map.MultiValueMap; + +public class MethodContext +{ + private Method method; + + private MultiValueMap visited = new MultiValueMap<>(); + private IdGen ids = new IdGen(); + private Map idMap = new HashMap<>(); + private Map insMap = new HashMap<>(); + private Graph graph = new SparseDirectedGraph(); + private int prevVertex = -1; + + public MethodContext(Method method) + { + this.method = method; + } + + public Map getIdMap() + { + return idMap; + } + + public Graph getGraph() + { + return graph; + } + + protected boolean hasJumped(InstructionContext from, Instruction to) + { + Collection i = visited.getCollection(from); + if (i != null && i.contains(to)) + return true; + + visited.put(from, to); + return false; + } + + private int getIdFor(Instruction i) + { + if (insMap.containsKey(i)) + return insMap.get(i); + + int id = ids.get(); + + graph.add(id); + + this.idMap.put(id, i); + this.insMap.put(i, id); + + return id; + } + + protected void buildGraph(Instruction i) + { + if (i instanceof InvokeInstruction) + { + InvokeInstruction ii = (InvokeInstruction) i; + + List methods = ii.getMethods(); + if (methods.isEmpty()) + return; + } +// else if (i instanceof FieldInstruction) +// { +// FieldInstruction fi = (FieldInstruction) i; +// +// if (fi.getMyField() == null) +// return; +// } + else + { + return; + } + + if (prevVertex == -1) + { + int id = getIdFor(i); + //int id = ids.get(); + //graph.add(id); + prevVertex = id; + //this.idMap.put(id, i); + return; + } + + int id = getIdFor(i); + //int id = ids.get(); + //graph.add(id); + //idMap.put(id, i); + + if (id == prevVertex) + return; + + DirectedEdge edge = new SimpleDirectedEdge(prevVertex, id); + graph.add(edge); + + prevVertex = id; + } +}