Hard to follow, exception removal and unused block (only from the now

removed exceptions from what I can tell)
This commit is contained in:
Adam
2015-05-10 16:42:47 -04:00
parent 2edf9d2117
commit fa3e9c0262
14 changed files with 181 additions and 8 deletions

View File

@@ -2,6 +2,7 @@ package info.sigterm.deob;
import info.sigterm.deob.execution.Execution;
import info.sigterm.deob.pool.NameAndType;
import info.sigterm.deob.attributes.Code;
import info.sigterm.deob.attributes.code.Instruction;
import info.sigterm.deob.attributes.code.instruction.types.LVTInstruction;
@@ -43,7 +44,9 @@ public class Deob
group.buildCallGraph();
checkCallGraph(group);
checkParameters(group);
removeExceptionObfuscation(group);
checkBlockGraph(group);
//checkParameters(group);
//execute(group);
@@ -95,6 +98,68 @@ public class Deob
System.out.println("Removed " + i + " methods");
}
private static void removeExceptionObfuscation(ClassGroup group)
{
int i = 0;
for (ClassFile cf : group.getClasses())
{
for (Method m : new ArrayList<>(cf.getMethods().getMethods()))
{
Code c = m.getCode();
if (c == null)
continue;
for (info.sigterm.deob.attributes.code.Exception e : new ArrayList<>(c.getExceptions().getExceptions()))
{
if (e.getCatchType() != null && e.getCatchType().getName().equals("java/lang/RuntimeException"))
{
c.getExceptions().remove(e);
++i;
}
}
}
}
System.out.println("Removed " + i + " exception handlers");
}
private static void checkBlockGraph(ClassGroup group)
{
int i = 0;
for (ClassFile cf : group.getClasses())
{
for (Method m : new ArrayList<>(cf.getMethods().getMethods()))
{
if (m.getCode() == null)
continue;
boolean check = false, remove = false;
for (Instruction ins : new ArrayList<>(m.getCode().getInstructions().getInstructions()))
{
if (remove)
{
m.getCode().getInstructions().remove(ins);
}
if (check)
{
if (ins.from.isEmpty() && ins.exce.isEmpty())
{
remove = true;
m.getCode().getInstructions().remove(ins);
++i;
}
check = false;
}
if (ins.isTerminal())
{
check = true;
remove = false;
}
}
}
}
System.out.println("Removed " + i + " unused blocks");
}
private static boolean parameterUsed(int num, List lv)
{
if (lv.isEmpty())
@@ -126,6 +191,7 @@ public class Deob
if (!parameterUsed(i, lv))
{
//m.removeParameter(i);
System.out.println("Not used param " + i + " of " + cf.getName() + " " + m.getName() + " static: " + m.isStatic());
++count;
}

View File

@@ -55,6 +55,17 @@ public class Method
{
assert callsFrom.isEmpty();
}
protected void removeParameter(int i)
{
// If this is a non static method parameter 0 is this
if (!this.isStatic())
--i;
arguments.remove(i);
// XXX now remove from code.
}
public Methods getMethods()
{

View File

@@ -14,7 +14,7 @@ public class Exception
private int startPc;
private int endPc;
private int handlerPc;
private Class cacheType;
private Class catchType;
public Exception(Exceptions exceptions) throws IOException
{
@@ -26,7 +26,16 @@ public class Exception
startPc = is.readUnsignedShort();
endPc = is.readUnsignedShort();
handlerPc = is.readUnsignedShort();
cacheType = pool.getClass(is.readUnsignedShort());
catchType = pool.getClass(is.readUnsignedShort());
Instruction ins = exceptions.getCode().getInstructions().findInstruction(handlerPc);
ins.exce.add(this);
}
protected void remove()
{
Instruction ins = exceptions.getCode().getInstructions().findInstruction(handlerPc);
ins.exce.remove(this);
}
public void write(DataOutputStream out) throws IOException
@@ -36,7 +45,7 @@ public class Exception
out.writeShort(startPc);
out.writeShort(endPc);
out.writeShort(handlerPc);
out.writeShort(cacheType == null ? 0 : pool.make(cacheType));
out.writeShort(catchType == null ? 0 : pool.make(catchType));
}
public Exceptions getExceptions()
@@ -58,4 +67,9 @@ public class Exception
{
return handlerPc;
}
public Class getCatchType()
{
return catchType;
}
}

View File

@@ -26,6 +26,12 @@ public class Exceptions
exceptions.add(new Exception(this));
}
public void remove(Exception e)
{
e.remove();
exceptions.remove(e);
}
public void write(DataOutputStream out) throws IOException
{
out.writeShort(exceptions.size());
@@ -38,9 +44,14 @@ public class Exceptions
return code;
}
public Collection<Exception> getHandlersForPc(int pc)
public List<Exception> getExceptions()
{
ArrayList<Exception> matches = new ArrayList<Exception>();
return exceptions;
}
public List<Exception> getHandlersForPc(int pc)
{
List<Exception> matches = new ArrayList<>();
for (Exception e : exceptions)
{

View File

@@ -6,6 +6,7 @@ import info.sigterm.deob.execution.Frame;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public abstract class Instruction
{
@@ -15,8 +16,9 @@ public abstract class Instruction
private int pc; // offset into method this instructions resides at
protected int length = 1; // length of this instruction
private ArrayList<Instruction> jump = new ArrayList<Instruction>(); // instructions which this instruction jumps to
private ArrayList<Instruction> from = new ArrayList<Instruction>(); // instructions which jump to this instruction
public List<Instruction> jump = new ArrayList<>(), // instructions which this instruction jumps to
from = new ArrayList<>(); // instructions which jump to this instruction
public List<Exception> exce = new ArrayList<>(); // exception handlers which start here
public Instruction(Instructions instructions, InstructionType type, int pc)
{
@@ -25,6 +27,16 @@ public abstract class Instruction
this.pc = pc;
}
protected void remove()
{
for (Instruction i : jump)
i.from.remove(this);
jump.clear();
assert from.isEmpty();
assert exce.isEmpty();
}
public void write(DataOutputStream out, int pc) throws IOException
{
out.writeByte(type.getCode());
@@ -78,4 +90,10 @@ public abstract class Instruction
}
public abstract void execute(Frame e);
/* does this terminate a block? */
public boolean isTerminal()
{
return false;
}
}

View File

@@ -55,6 +55,12 @@ public class Instructions
return instructions;
}
public void remove(Instruction ins)
{
ins.remove();
instructions.remove(ins);
}
public void write(DataOutputStream out) throws IOException
{
ByteArrayOutputStream b = new ByteArrayOutputStream();

View File

@@ -21,4 +21,10 @@ public class AThrow extends Instruction
ObjectInstance exception = (ObjectInstance) e.getStack().pop();
e.getPath().throwException(this, exception);
}
@Override
public boolean isTerminal()
{
return true;
}
}

View File

@@ -40,4 +40,10 @@ public class Goto extends Instruction
{
e.jump(offset);
}
@Override
public boolean isTerminal()
{
return true;
}
}

View File

@@ -40,4 +40,10 @@ public class GotoW extends Instruction
{
e.jump(offset);
}
@Override
public boolean isTerminal()
{
return true;
}
}

View File

@@ -83,4 +83,10 @@ public class LookupSwitch extends Instruction
Path p = e.getPath().dup();
p.getCurrentFrame().jump(def);
}
@Override
public boolean isTerminal()
{
return true;
}
}

View File

@@ -20,4 +20,10 @@ public class Return extends Instruction
Object ret = e.getStack().pop();
e.getPath().returnFrame(this, ret);
}
@Override
public boolean isTerminal()
{
return true;
}
}

View File

@@ -79,4 +79,10 @@ public class TableSwitch extends Instruction
Path p = e.getPath().dup();
p.getCurrentFrame().jump(def);
}
@Override
public boolean isTerminal()
{
return true;
}
}

View File

@@ -20,4 +20,10 @@ public class VReturn extends Instruction
// XXX exceptions?
e.getPath().returnFrame();
}
@Override
public boolean isTerminal()
{
return true;
}
}

View File

@@ -59,6 +59,11 @@ public class Signature
return arguments.size();
}
public void remove(int i)
{
arguments.remove(i);
}
public Type getReturnValue()
{
return rv;