Change mapping logic to make more sense, which seems to work better

This commit is contained in:
Adam
2016-04-16 17:51:49 -04:00
parent d380b60910
commit 8c38a8cc6b
6 changed files with 121 additions and 61 deletions

View File

@@ -0,0 +1,40 @@
package net.runelite.deob.deobfuscators.mapping;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.runelite.asm.Method;
import net.runelite.deob.deobfuscators.rename.MappingExecutorUtil;
import net.runelite.deob.deobfuscators.rename.ParallelExecutorMapping;
public class ExecutionMapper
{
// method1 maps to one of methods2, find out based on mappings
private Method method1;
private Collection<Method> methods2;
private Map<Method, ParallelExecutorMapping> mappings = new HashMap<>();
public ExecutionMapper(Method method1, Collection<Method> methods2)
{
this.method1 = method1;
this.methods2 = methods2;
}
public ParallelExecutorMapping run()
{
ParallelExecutorMapping highest = null;
for (Method m : methods2)
{
ParallelExecutorMapping mapping = MappingExecutorUtil.map(method1, m);
mappings.put(m, mapping);
if (highest == null || mapping.same > highest.same)
highest = mapping;
}
return highest;
}
}

View File

@@ -14,6 +14,7 @@ import net.runelite.asm.attributes.Annotations;
import net.runelite.asm.attributes.code.Instruction; import net.runelite.asm.attributes.code.Instruction;
import net.runelite.asm.execution.ParallellMappingExecutor; import net.runelite.asm.execution.ParallellMappingExecutor;
import net.runelite.asm.signature.Type; import net.runelite.asm.signature.Type;
import net.runelite.deob.deobfuscators.mapping.ExecutionMapper;
public class Mapper public class Mapper
{ {
@@ -55,11 +56,12 @@ public class Mapper
{ {
Collection<Method> methods = msm.getMap().get(m); Collection<Method> methods = msm.getMap().get(m);
for (Method other : methods) ExecutionMapper em = new ExecutionMapper(m, methods);
{
HashMap<Object, Object> all = new HashMap(); ParallelExecutorMapping mapping = em.run();
map(all, pmes, m, other); mapping.map(mapping.m1, mapping.m2);
}
pmes.add(mapping);
} }
ParallelExecutorMapping finalm = new ParallelExecutorMapping(one, two); ParallelExecutorMapping finalm = new ParallelExecutorMapping(one, two);
@@ -79,12 +81,13 @@ public class Mapper
for (Method m : smsm.getMap().keySet()) for (Method m : smsm.getMap().keySet())
{ {
Collection<Method> methods = smsm.getMap().get(m); Collection<Method> methods = smsm.getMap().get(m);
for (Method other : methods) ExecutionMapper em = new ExecutionMapper(m, methods);
{
HashMap<Object, Object> all = new HashMap(); ParallelExecutorMapping mapping = em.run();
map(all, pmes, m, other); mapping.map(mapping.m1, mapping.m2);
}
pmes.add(mapping);
} }
ParallelExecutorMapping finalm = new ParallelExecutorMapping(one, two); ParallelExecutorMapping finalm = new ParallelExecutorMapping(one, two);
@@ -94,41 +97,6 @@ public class Mapper
return finalm; return finalm;
} }
private void map(Map<Object, Object> all, List<ParallelExecutorMapping> result, Method m1, Method m2)
{
if (all.containsKey(m1))
return;
all.put(m1, m2);
assert (m1.getCode() == null) == (m2.getCode() == null);
if (m1.getCode() == null)
return;
ParallelExecutorMapping mappings = MappingExecutorUtil.map(m1, m2);
if (mappings.getMap().isEmpty() && mappings.crashed)
return;
mappings.map(m1, m2);
result.add(mappings);
for (Map.Entry<Object, Object> e : mappings.getMap().entrySet())
{
if (e.getKey() instanceof Method)
{
Method n1 = (Method) e.getKey(),
n2 = (Method) e.getValue();
map(all, result, n1, n2);
}
else
{
all.put(e.getKey(), e.getValue());
}
}
}
private ParallelExecutorMapping mapPackets(ParallelExecutorMapping pem, ClassGroup group1, ClassGroup group2) private ParallelExecutorMapping mapPackets(ParallelExecutorMapping pem, ClassGroup group1, ClassGroup group2)
{ {
Method packetMethod = this.findPacketMethod(); Method packetMethod = this.findPacketMethod();

View File

@@ -54,7 +54,7 @@ public class MappingExecutorUtil
parallel.mappings = mappings; parallel.mappings = mappings;
outer: int same = 0;
while (parallel.step()) while (parallel.step())
{ {
// get what each frame is paused/exited on // get what each frame is paused/exited on
@@ -83,9 +83,12 @@ public class MappingExecutorUtil
continue; continue;
} }
++same;
mi1.map(mappings, p1, p2); mi1.map(mappings, p1, p2);
e.paused = e2.paused = false; e.paused = e2.paused = false;
} }
mappings.same = same;
return mappings; return mappings;
} }

View File

@@ -17,12 +17,11 @@ public class ParallelExecutorMapping
{ {
private ClassGroup group, group2; private ClassGroup group, group2;
private Multimap<Object, Mapping> map = HashMultimap.create(); private Multimap<Object, Mapping> map = HashMultimap.create();
//private Map<Object, Object> map = new HashMap<>();
//private List<Object> order = new ArrayList<>();
public Method m1, m2; public Method m1, m2;
public boolean crashed; public boolean crashed;
public List<PacketHandler> packetHandler1 = new ArrayList<>(); public List<PacketHandler> packetHandler1 = new ArrayList<>();
public List<PacketHandler> packetHandler2 = new ArrayList<>(); public List<PacketHandler> packetHandler2 = new ArrayList<>();
public int same;
public ParallelExecutorMapping(ClassGroup group, ClassGroup group2) public ParallelExecutorMapping(ClassGroup group, ClassGroup group2)
{ {
@@ -71,18 +70,8 @@ public class ParallelExecutorMapping
} }
} }
// public void mergeButNotOverride(ParallelExecutorMapping other)
// {
// assert this != other;
// for (Object o : other.map.keySet())
// if (map.containsKey(o) == false)
// map.put(o, other.map.get(o));
// }
public void map(Object one, Object two) public void map(Object one, Object two)
{ {
//mapClass(one, two);
Mapping m = getMapping(one, two); Mapping m = getMapping(one, two);
belongs(one, group); belongs(one, group);

View File

@@ -9,7 +9,7 @@ import org.junit.Test;
public class AnnotationMapperTest public class AnnotationMapperTest
{ {
private static final String JAR1 = "C:\\Users\\Adam\\.m2\\repository\\net\\runelite\\rs\\rs-client\\1.0-SNAPSHOT\\rs-client-1.0-SNAPSHOT.jar", private static final String JAR1 = "C:\\Users\\Adam\\.m2\\repository\\net\\runelite\\rs\\rs-client\\1.0-SNAPSHOT\\rs-client-1.0-SNAPSHOT.jar",
JAR2 = "d:/rs/07/gamepack_v20_deobfuscated.jar", JAR2 = "d:/rs/07/gamepack_v21_deobfuscated.jar",
OUT = "d:/rs/07/adamout.jar"; OUT = "d:/rs/07/adamout.jar";
@Test @Test

View File

@@ -2,7 +2,13 @@ package net.runelite.deob.deobfuscators.rename;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import net.runelite.asm.ClassFile;
import net.runelite.asm.ClassGroup; import net.runelite.asm.ClassGroup;
import net.runelite.asm.Field;
import net.runelite.asm.Method;
import net.runelite.asm.attributes.Annotations;
import net.runelite.asm.attributes.annotation.Annotation;
import net.runelite.asm.signature.Type;
import static net.runelite.deob.deobfuscators.rename.MapStaticTest.print; import static net.runelite.deob.deobfuscators.rename.MapStaticTest.print;
import static net.runelite.deob.deobfuscators.rename.MapStaticTest.summary; import static net.runelite.deob.deobfuscators.rename.MapStaticTest.summary;
import net.runelite.deob.util.JarUtil; import net.runelite.deob.util.JarUtil;
@@ -12,10 +18,12 @@ import org.junit.Test;
public class MapperTest public class MapperTest
{ {
private static final String JAR1 = "C:\\Users\\Adam\\.m2\\repository\\net\\runelite\\rs\\rs-client\\1.0-SNAPSHOT\\rs-client-1.0-SNAPSHOT.jar", private static final String JAR1 = "C:\\Users\\Adam\\.m2\\repository\\net\\runelite\\rs\\rs-client\\1.0-SNAPSHOT\\rs-client-1.0-SNAPSHOT.jar",
JAR2 = "d:/rs/07/gamepack_v19_deobfuscated.jar"; JAR2 = "d:/rs/07/gamepack_v21_deobfuscated.jar";
// private static final String JAR1 = MapStaticTest.class.getResource("/adamin1.jar").getFile(), // private static final String JAR1 = MapStaticTest.class.getResource("/adamin1.jar").getFile(),
// JAR2 = MapStaticTest.class.getResource("/adamin2.jar").getFile(); // JAR2 = MapStaticTest.class.getResource("/adamin2.jar").getFile();
private static final Type EXPORT = new Type("Lnet/runelite/mapping/Export;");
@Test @Test
public void testRun() throws IOException public void testRun() throws IOException
{ {
@@ -25,6 +33,8 @@ public class MapperTest
Mapper mapper = new Mapper(group1, group2); Mapper mapper = new Mapper(group1, group2);
mapper.run(); mapper.run();
ParallelExecutorMapping mapping = mapper.getMapping(); ParallelExecutorMapping mapping = mapper.getMapping();
checkMappings(group1, group2, mapping);
summary(mapping, group1); summary(mapping, group1);
@@ -34,4 +44,54 @@ public class MapperTest
System.out.println("GROUP 1 " + sg1); System.out.println("GROUP 1 " + sg1);
System.out.println("GROUP 2 " + sg2); System.out.println("GROUP 2 " + sg2);
} }
private void checkMappings(ClassGroup group1, ClassGroup group2, ParallelExecutorMapping mapping)
{
for (ClassFile cf : group1.getClasses())
{
for (Field f : cf.getFields().getFields())
{
String export = getExport(f.getAttributes().getAnnotations());
if (export == null)
continue;
Field other = (Field) mapping.get(f);
if (other != null)
{
// System.out.println("Mapped field " + f + " -> " + other);
continue;
}
System.out.println("UNMAPPED FIELD " + export + ": " + f + " -> null");
}
for (Method m : cf.getMethods().getMethods())
{
String export = getExport(m.getAttributes().getAnnotations());
if (export == null)
continue;
Method other = (Method) mapping.get(m);
if (other != null)
{
// System.out.println("Mapped method " + m + " -> " + other);
continue;
}
System.out.println("UNMAPPED METHOD " + export + ": " + m + " -> null");
}
}
}
private String getExport(Annotations an)
{
if (an == null)
return null;
Annotation a = an.find(EXPORT);
if (a == null)
return null;
return a.getElement().getString();
}
} }