Remove unused methods

This commit is contained in:
Adam
2015-04-25 15:30:22 -04:00
parent a9f953b46a
commit 267efc7940
5 changed files with 77 additions and 4 deletions

View File

@@ -93,6 +93,11 @@ public class ClassFile
return pool; return pool;
} }
public Interfaces getInterfaces()
{
return interfaces;
}
public Fields getFields() public Fields getFields()
{ {
return fields; return fields;

View File

@@ -8,6 +8,7 @@ import java.io.DataOutputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarFile; import java.util.jar.JarFile;
@@ -37,7 +38,7 @@ public class Deob
group.buildInstructionGraph(); group.buildInstructionGraph();
group.buildCallGraph(); group.buildCallGraph();
//checkCallGraph(group); checkCallGraph(group);
//execute(group); //execute(group);
@@ -69,15 +70,23 @@ public class Deob
private static void checkCallGraph(ClassGroup group) private static void checkCallGraph(ClassGroup group)
{ {
int i = 0;
for (ClassFile cf : group.getClasses()) for (ClassFile cf : group.getClasses())
{ {
for (Method m : cf.getMethods().getMethods()) for (Method m : new ArrayList<>(cf.getMethods().getMethods()))
{ {
if (m.callsFrom.isEmpty()) /* assume obfuscated names are <= 2 chars */
if (m.getName().length() > 2)
continue;
if (!m.isUsed())
{ {
System.out.println(cf.getName() + " " + m.getName()); System.out.println(cf.getName() + " " + m.getName());
cf.getMethods().removeMethod(m);
++i;
} }
} }
} }
System.out.println("Removed " + i + " methods");
} }
} }

View File

@@ -5,6 +5,8 @@ import info.sigterm.deob.pool.Class;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Interfaces public class Interfaces
{ {
@@ -26,6 +28,19 @@ public class Interfaces
interfaces[i] = is.readUnsignedShort(); interfaces[i] = is.readUnsignedShort();
} }
public List<ClassFile> getInterfaces()
{
List<ClassFile> l = new ArrayList<>();
for (int i : interfaces)
{
Class clazz = (Class) classFile.getPool().getEntry(i);
ClassFile iface = classFile.getGroup().findClass(clazz.getName());
if (iface != null)
l.add(iface);
}
return l;
}
public void write(DataOutputStream out) throws IOException public void write(DataOutputStream out) throws IOException
{ {
out.writeShort(count); out.writeShort(count);

View File

@@ -71,6 +71,28 @@ public class Method
return (Code) attributes.findType(AttributeType.CODE); return (Code) attributes.findType(AttributeType.CODE);
} }
public List<Method> getOverriddenMethods()
{
List<Method> m = new ArrayList<Method>();
ClassFile parent = methods.getClassFile().getParent();
if (parent != null)
{
Method other = parent.getMethods().findMethod(getName(), getDescriptor());
if (other != null)
m.add(other);
}
for (ClassFile inter : methods.getClassFile().getInterfaces().getInterfaces())
{
Method other = inter.getMethods().findMethod(getName(), getDescriptor());
if (other != null)
m.add(other);
}
return m;
}
public void buildInstructionGraph() public void buildInstructionGraph()
{ {
Code code = getCode(); Code code = getCode();
@@ -87,6 +109,20 @@ public class Method
code.buildCallGraph(); code.buildCallGraph();
} }
public boolean isUsed()
{
if (!callsFrom.isEmpty())
return true;
for (Method sm : getOverriddenMethods())
{
if (sm.isUsed())
return true;
}
return false;
}
public void addCallTo(Instruction ins, Method method) public void addCallTo(Instruction ins, Method method)
{ {
Node node = new Node(this, method, ins); Node node = new Node(this, method, ins);

View File

@@ -36,7 +36,7 @@ public class Methods
public void removeMethod(Method m) public void removeMethod(Method m)
{ {
m.remove(); m.remove();
methods.remove(methods); methods.remove(m);
} }
public ClassFile getClassFile() public ClassFile getClassFile()
@@ -57,6 +57,14 @@ public class Methods
return null; return null;
} }
public Method findMethod(String name, String type)
{
for (Method m : methods)
if (m.getName().equals(name) && m.getDescriptor().equals(type))
return m;
return null;
}
public Method findMethod(String name) public Method findMethod(String name)
{ {
for (Method m : methods) for (Method m : methods)