From 734f15fa4a8e086ef37d34c819a90af0850879b9 Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 10 Oct 2015 21:15:07 -0400 Subject: [PATCH] More tests/fixes I dont know if this is right --- .../code/instructions/IConst_M1.java | 6 + .../deobfuscators/arithmetic/ModArith.java | 22 ++-- .../MultiplicationDeobfuscator.java | 5 +- .../MultiplicationDeobfuscatorTest.java | 103 ++++++++++++++++++ 4 files changed, 122 insertions(+), 14 deletions(-) diff --git a/src/main/java/net/runelite/deob/attributes/code/instructions/IConst_M1.java b/src/main/java/net/runelite/deob/attributes/code/instructions/IConst_M1.java index 7e41464342..bfbe243c6b 100644 --- a/src/main/java/net/runelite/deob/attributes/code/instructions/IConst_M1.java +++ b/src/main/java/net/runelite/deob/attributes/code/instructions/IConst_M1.java @@ -49,4 +49,10 @@ public class IConst_M1 extends Instruction implements PushConstantInstruction { return new LDC_W(this.getInstructions(), entry); } + + @Override + public Instruction makeGeneric() + { + return new LDC_W(this.getInstructions(), getConstant()); + } } diff --git a/src/main/java/net/runelite/deob/deobfuscators/arithmetic/ModArith.java b/src/main/java/net/runelite/deob/deobfuscators/arithmetic/ModArith.java index 7cfc6f9622..8e0c14b301 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/arithmetic/ModArith.java +++ b/src/main/java/net/runelite/deob/deobfuscators/arithmetic/ModArith.java @@ -310,16 +310,16 @@ public class ModArith implements Deobfuscator public void run(ClassGroup group) { this.group = group; - runOnce(); - if (true) return; - - int passes = 0, total = 0, i; - while ((i = runOnce()) > 0) - { - ++passes; - total += i; - } - System.out.println("Finished arith deob on " + total + " fields in " + passes + " passes"); + //return runOnce(); +// if (true) return; +// +// int passes = 0, total = 0, i; +// while ((i = runOnce()) > 0) +// { +// ++passes; +// total += i; +// } +// System.out.println("Finished arith deob on " + total + " fields in " + passes + " passes"); } // // private void translateSetFields(Execution e) @@ -395,7 +395,7 @@ public class ModArith implements Deobfuscator } } - private int runOnce() + public int runOnce() { group.buildClassGraph(); diff --git a/src/main/java/net/runelite/deob/deobfuscators/arithmetic/MultiplicationDeobfuscator.java b/src/main/java/net/runelite/deob/deobfuscators/arithmetic/MultiplicationDeobfuscator.java index 440c7ba9b4..b162082d7b 100644 --- a/src/main/java/net/runelite/deob/deobfuscators/arithmetic/MultiplicationDeobfuscator.java +++ b/src/main/java/net/runelite/deob/deobfuscators/arithmetic/MultiplicationDeobfuscator.java @@ -107,8 +107,7 @@ public class MultiplicationDeobfuscator implements Deobfuscator { if (i.getInstruction() instanceof PushConstantInstruction) { - if (i.getInstruction() instanceof BiPush || i.getInstruction() instanceof SiPush - || i.getInstruction() instanceof IConst_M1) + if (i.getInstruction() instanceof BiPush || i.getInstruction() instanceof SiPush) throw new IllegalStateException(); // a constant of imul @@ -194,7 +193,7 @@ public class MultiplicationDeobfuscator implements Deobfuscator else { System.out.println("dup ins " + otherCtxI.getInstruction()); - throw new IllegalStateException(); + //throw new IllegalStateException(); } } } diff --git a/src/test/java/net/runelite/deob/deobfuscators/arithmetic/MultiplicationDeobfuscatorTest.java b/src/test/java/net/runelite/deob/deobfuscators/arithmetic/MultiplicationDeobfuscatorTest.java index aff4323282..b803bc3d12 100644 --- a/src/test/java/net/runelite/deob/deobfuscators/arithmetic/MultiplicationDeobfuscatorTest.java +++ b/src/test/java/net/runelite/deob/deobfuscators/arithmetic/MultiplicationDeobfuscatorTest.java @@ -13,6 +13,7 @@ import net.runelite.deob.attributes.code.instructions.IConst_0; import net.runelite.deob.attributes.code.instructions.IConst_1; import net.runelite.deob.attributes.code.instructions.IConst_2; import net.runelite.deob.attributes.code.instructions.IConst_3; +import net.runelite.deob.attributes.code.instructions.IConst_M1; import net.runelite.deob.attributes.code.instructions.IDiv; import net.runelite.deob.attributes.code.instructions.ILoad; import net.runelite.deob.attributes.code.instructions.IMul; @@ -358,4 +359,106 @@ public class MultiplicationDeobfuscatorTest Assert.assertEquals(1, constant2.getConstantAsInt()); Assert.assertEquals(1, constant3.getConstantAsInt()); } + + @Test + public void testDupX1_6() + { + ClassGroup group = ClassGroupFactory.generateGroup(); + Code code = group.findClass("test").findMethod("func").getCode(); + Instructions ins = code.getInstructions(); + + code.setMaxStack(2); + + Instruction[] prepareVariables = { + new IConst_3(ins), + new IStore_0(ins), + new IConst_2(ins), + new IStore(ins, 1) + }; + + for (Instruction i : prepareVariables) + ins.addInstruction(i); + + LDC_W constant1 = new LDC_W(ins, 575391417), + constant2 = new LDC_W(ins, -497786999); + + Instruction body[] = { + new ILoad(ins, 0), + new ILoad(ins, 1), + new Dup_X1(ins), + new Pop(ins), + new Pop(ins), + constant1, + new IMul(ins), + constant2, + new IMul(ins), + new Pop(ins), + new VReturn(ins) + }; + + for (Instruction i : body) + ins.addInstruction(i); + + Execution e = new Execution(group); + e.populateInitialMethods(); + e.run(); + + assert constant1.getConstantAsInt() * constant2.getConstantAsInt() == 1; + + Deobfuscator d = new MultiplicationDeobfuscator(); + d.run(group); + + Assert.assertEquals(1, constant1.getConstantAsInt()); + Assert.assertEquals(1, constant2.getConstantAsInt()); + } + + @Test + public void testDupX1_7() + { + ClassGroup group = ClassGroupFactory.generateGroup(); + Code code = group.findClass("test").findMethod("func").getCode(); + Instructions ins = code.getInstructions(); + + code.setMaxStack(2); + + Instruction[] prepareVariables = { + new IConst_3(ins), + new IStore_0(ins), + new IConst_2(ins), + new IStore(ins, 1) + }; + + for (Instruction i : prepareVariables) + ins.addInstruction(i); + + LDC_W constant1 = new LDC_W(ins, 2131037801), + constant2 = new LDC_W(ins, -1306959399), + constant3 = new LDC_W(ins, -1); + + Instruction body[] = { + constant3, + constant1, + new IMul(ins), + constant2, + new IMul(ins), + new Pop(ins), + new VReturn(ins) + }; + + for (Instruction i : body) + ins.addInstruction(i); + + Execution e = new Execution(group); + e.populateInitialMethods(); + e.run(); + + assert constant1.getConstantAsInt() * constant2.getConstantAsInt() == 1; + + Deobfuscator d = new MultiplicationDeobfuscator(); + d.run(group); + + Assert.assertEquals(1, constant1.getConstantAsInt()); + Assert.assertEquals(-1, constant2.getConstantAsInt()); + Assert.assertEquals(1, constant3.getConstantAsInt()); + } }