Try and replace unused blocks with unreached code, seeing some problems somewhere

This commit is contained in:
Adam
2015-08-08 20:45:16 -04:00
parent 12318efcaf
commit da0b7403b4
9 changed files with 203 additions and 67 deletions

View File

@@ -78,7 +78,7 @@ public class IllegalStateExceptions implements Deobfuscator
}
if (!found)
{
System.out.println("Unable to locate instruction ctx to remove stack for illegalstateexception " + ins + " in " + m);
System.out.println("Unable to locate instruction ctx to remove stack for illegalstateexception " + ins.getType().getName() + " in method " + m.getName() + " class " + m.getMethods().getClassFile().getName());
continue;
}

View File

@@ -87,22 +87,36 @@ public class MethodInliner implements Deobfuscator
assert m != invokedMethod;
// XXX do this later
// if (!invokedMethod.getDescriptor().getReturnValue().getType().equals("V")
// || invokedMethod.getDescriptor().size() != 0)
// {
// System.out.println(invokedMethod.getName());
// continue;
// }
int invokeIdx = ins.getInstructions().indexOf(i);
assert invokeIdx != -1;
int lvtIndex = code.getMaxLocals(), startLvtIndex = lvtIndex;
int lvtIndex = code.getMaxLocals(),
//startLvtIndex = lvtIndex,
theirLocals = invokedMethod.getCode().getMaxLocals();
if (lvtIndex + theirLocals > 127)
continue;
if (invokedMethod.isSynchronized())
continue;
if (!invokedMethod.getCode().getExceptions().getExceptions().isEmpty())
continue;
// assign variables on stack to lvt
Signature descriptor = invokedMethod.getDescriptor();
for (int j = 0; j < descriptor.size(); ++j)
Map<Integer, Integer> lvtIndexes = new HashMap<>();
for (int j = 0, idx = 0; j < descriptor.size(); ++j)
{
lvtIndexes.put(j, idx);
idx += descriptor.getTypeOfArg(j).getSlots();
}
for (int j = descriptor.size() - 1; j >= 0; --j)
{
Type type = descriptor.getTypeOfArg(j);
int paramLvtIndex = lvtIndexes.get(j);
// insert instruction to store top of stack in lvt
@@ -111,24 +125,25 @@ public class MethodInliner implements Deobfuscator
{
switch (type.getType())
{
case "B":
case "Z":
case "C":
case "S":
case "I":
storeIns = new IStore(ins, lvtIndex);
lvtIndex += type.getSlots();
storeIns = new IStore(ins, lvtIndex + paramLvtIndex);
//lvtIndex += type.getSlots();
break;
case "J":
storeIns = new LStore(ins, lvtIndex);
lvtIndex += type.getSlots();
storeIns = new LStore(ins, lvtIndex + paramLvtIndex);
//lvtIndex += type.getSlots();
break;
case "F":
storeIns = new FStore(ins, lvtIndex);
lvtIndex += type.getSlots();
storeIns = new FStore(ins, lvtIndex + paramLvtIndex);
//lvtIndex += type.getSlots();
break;
case "D":
storeIns = new DStore(ins, lvtIndex);
lvtIndex += type.getSlots();
storeIns = new DStore(ins, lvtIndex + paramLvtIndex);
//lvtIndex += type.getSlots();
break;
}
}
@@ -136,8 +151,8 @@ public class MethodInliner implements Deobfuscator
if (type.getArrayDims() != 0 || type.getType().startsWith("L"))
{
assert storeIns == null;
storeIns = new AStore(ins, lvtIndex);
lvtIndex += type.getSlots();
storeIns = new AStore(ins, lvtIndex + paramLvtIndex);
//lvtIndex += type.getSlots();
}
assert storeIns != null;
@@ -145,7 +160,7 @@ public class MethodInliner implements Deobfuscator
ins.getInstructions().add(invokeIdx++, storeIns);
}
inline(m, i, invokedMethod, startLvtIndex);
inline(m, i, invokedMethod, /*start*/lvtIndex);
++inlineCount;
break;
}
@@ -236,7 +251,16 @@ public class MethodInliner implements Deobfuscator
@Override
public void run(ClassGroup group)
{
while (pass(group) > 0);
int total = 0;
int i;
do
{
i = pass(group);
total += i;
}
while (i > 0);
System.out.println("[TOTAL] Inlined " + total + " methods");
}
private int pass(ClassGroup group)

View File

@@ -0,0 +1,85 @@
package info.sigterm.deob.deobfuscators;
import info.sigterm.deob.ClassFile;
import info.sigterm.deob.ClassGroup;
import info.sigterm.deob.Deobfuscator;
import info.sigterm.deob.Method;
import info.sigterm.deob.attributes.code.Instruction;
import info.sigterm.deob.attributes.code.Instructions;
import info.sigterm.deob.execution.Execution;
import java.util.ArrayList;
import java.util.List;
public class UnreachedCode implements Deobfuscator
{
private Execution execution;
private int removeUnused(Method m)
{
Instructions ins = m.getCode().getInstructions();
int count = 0;
List<Instruction> insCopy = new ArrayList<>(ins.getInstructions());
for (int j = 0; j < insCopy.size(); ++j)
//for (Instruction i : new ArrayList<>(ins.getInstructions()))
{
Instruction i = insCopy.get(j);
if (!execution.executed.contains(i))
{
for (Instruction i2 : i.from)
i2.jump.remove(i);
i.from.clear(); // if this is never executed, anything that jumps here ia also never executed?
// if this is an exception handler, the exception handler is never used...
for (info.sigterm.deob.attributes.code.Exception e : new ArrayList<>(m.getCode().getExceptions().getExceptions()))
{
if (e.getStart() == i)
{
e.setStart(insCopy.get(j + 1));
if (e.getStart() == e.getEnd())
{
m.getCode().getExceptions().remove(e);
continue;
}
}
if (e.getHandler() == i)
{
m.getCode().getExceptions().remove(e);
}
}
ins.remove(i);
++count;
}
}
return count;
}
@Override
public void run(ClassGroup group)
{
group.buildClassGraph();
execution = new Execution(group);
execution.populateInitialMethods();
execution.run();
int count = 0;
for (ClassFile cf : group.getClasses())
{
for (Method m : cf.getMethods().getMethods())
{
if (m.getCode() == null)
continue;
count += removeUnused(m);
}
}
System.out.println("Removed " + count + " unused instructions");
}
}

View File

@@ -21,7 +21,10 @@ public class UnusedBlocks implements Deobfuscator
for (Method m : new ArrayList<>(methods))
{
if (m.getCode() == null)
{
methods.remove(m);
continue;
}
Instructions ins = m.getCode().getInstructions();
ins.buildBlocks();