private void AssertStringsEqual(string sExp, SsaState ssa) { var sw = new StringWriter(); ssa.Write(sw); ssa.Procedure.Write(false, sw); var sActual = sw.ToString(); if (sExp != sActual) { Console.WriteLine("{0}", sActual); Assert.AreEqual(sExp, sActual); } }
private Context( SsaState ssa, Identifier fp, Dictionary <Identifier, Tuple <Expression, BitRange> > idState, ProcedureFlow procFlow, Dictionary <int, Expression> stack, ExpressionValueComparer cmp) { this.ssa = ssa; this.FramePointer = fp; this.IdState = idState; this.ProcFlow = procFlow; this.StackState = stack; this.cmp = cmp; }
public void Setup() { program = new Program(); sc = new ServiceContainer(); program.Architecture = new X86ArchitectureFlat32(sc, "x86-protected-32"); program.Platform = new DefaultPlatform(sc, program.Architecture); crw = new CallRewriter(program.Platform, new ProgramDataFlow(), new FakeDecompilerEventListener()); proc = new Procedure(program.Architecture, "foo", Address.Ptr32(0x00123400), program.Architecture.CreateFrame()); flow = new ProcedureFlow(proc); ssa = new SsaState(proc); pb = new ProgramBuilder(); ssaStates = new List <SsaState>(); eventListener = new FakeDecompilerEventListener(); sc.AddService <DecompilerEventListener>(eventListener); }
private void Prepare(ProcedureBuilder mock) { var program = new Program() { Architecture = mock.Architecture, }; var sst = new SsaTransform( program, mock.Procedure, new HashSet <Procedure>(), null, new ProgramDataFlow()); sst.Transform(); ssa = sst.SsaState; }
protected override void RunTest(Program program, TextWriter writer) { DataFlowAnalysis dfa = new DataFlowAnalysis(program, dynamicLinker.Object, new FakeDecompilerEventListener()); var ssts = dfa.UntangleProcedures(); foreach (var sst in ssts) { SsaState ssa = sst.SsaState; ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, program.Platform); cce.Transform(); DeadCode.Eliminate(ssa); ssa.Write(writer); ssa.Procedure.Write(false, writer); } }
protected override void RunTest(Program prog, TextWriter writer) { var progFlow = new ProgramDataFlow(); foreach (Procedure proc in prog.Procedures.Values) { var gr = proc.CreateBlockDominatorGraph(); Aliases alias = new Aliases(proc, prog.Architecture); alias.Transform(); SsaTransform sst = new SsaTransform(progFlow, proc, gr); SsaState ssa = sst.SsaState; DumpProc(proc, ssa, writer); ValueNumbering vn = new ValueNumbering(ssa.Identifiers); vn.Write(writer); writer.WriteLine(); } }
private void Build(Procedure proc, IProcessorArchitecture arch) { var platform = new DefaultPlatform(null, arch); var program = new Program() { Architecture = arch, Platform = platform, }; this.proc = proc; var dynamicLinker = new Mock <IDynamicLinker>().Object; var gr = proc.CreateBlockDominatorGraph(); SsaTransform sst = new SsaTransform( program, proc, new HashSet <Procedure>(), null, new ProgramDataFlow()); sst.Transform(); this.ssa = sst.SsaState; this.ssaIds = ssa.Identifiers; var listener = new FakeDecompilerEventListener(); ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, platform, listener); cce.Transform(); DeadCode.Eliminate(ssa); var segmentMap = new SegmentMap(Address.Ptr32(0x00400000)); ValuePropagator vp = new ValuePropagator( segmentMap, ssa, program.CallGraph, null, listener); vp.Transform(); Coalescer coa = new Coalescer(ssa); coa.Transform(); DeadCode.Eliminate(ssa); }
private void BuildTest(Action <ProcedureBuilder> builder) { var pb = new ProcedureBuilder(); builder(pb); var dflow = new ProgramDataFlow(); var program = new Program() { Architecture = pb.Architecture, }; var sst = new SsaTransform( program, pb.Procedure, new HashSet <Procedure>(), null, dflow); this.ssa = sst.Transform(); }
protected override void RunTest(Program program, TextWriter writer) { var progFlow = new ProgramDataFlow(); foreach (Procedure proc in program.Procedures.Values) { var gr = proc.CreateBlockDominatorGraph(); Aliases alias = new Aliases(proc); alias.Transform(); SsaTransform sst = new SsaTransform(progFlow, proc, null, gr, new HashSet <RegisterStorage>()); SsaState ssa = sst.SsaState; DumpProc(proc, ssa, writer); ValueNumbering vn = new ValueNumbering(ssa.Identifiers, segmentMap, null); vn.Write(writer); writer.WriteLine(); } }
protected override void RunTest(Program prog, FileUnitTester fut) { foreach (Procedure proc in prog.Procedures.Values) { DominatorGraph gr = new DominatorGraph(proc); Aliases alias = new Aliases(proc, prog.Architecture); alias.Transform(); SsaTransform sst = new SsaTransform(proc, gr, true); SsaState ssa = sst.SsaState; DeadCode.Eliminate(proc, ssa); ValueNumbering vn = new ValueNumbering(ssa.Identifiers); ssa.Write(fut.TextWriter); proc.Write(false, fut.TextWriter); vn.Write(fut.TextWriter); } }
private void RunUnitTest(ProcedureBuilder m, string outfile) { var proc = m.Procedure; var sst = new SsaTransform( new ProgramDataFlow(), proc, null, proc.CreateBlockDominatorGraph(), new HashSet <RegisterStorage>()); ssa = sst.SsaState; using (var fut = new FileUnitTester(outfile)) { ssa.Write(fut.TextWriter); proc.Write(false, fut.TextWriter); fut.AssertFilesEqual(); } }
public void VpDbp() { Procedure proc = new DpbMock().Procedure; var gr = proc.CreateBlockDominatorGraph(); SsaTransform sst = new SsaTransform(new ProgramDataFlow(), proc, null, gr, new HashSet <RegisterStorage>()); SsaState ssa = sst.SsaState; ValuePropagator vp = new ValuePropagator(segmentMap, ssa, listener); vp.Transform(); using (FileUnitTester fut = new FileUnitTester("Analysis/VpDbp.txt")) { proc.Write(false, fut.TextWriter); fut.TextWriter.WriteLine(); fut.AssertFilesEqual(); } }
protected override void RunTest(Program prog, TextWriter writer) { DataFlowAnalysis dfa = new DataFlowAnalysis(prog, new FakeDecompilerEventListener()); dfa.UntangleProcedures(); foreach (Procedure proc in prog.Procedures.Values) { Aliases alias = new Aliases(proc, prog.Architecture); alias.Transform(); SsaTransform sst = new SsaTransform(dfa.ProgramDataFlow, proc, proc.CreateBlockDominatorGraph()); SsaState ssa = sst.SsaState; ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa.Identifiers, prog.Platform); cce.Transform(); DeadCode.Eliminate(proc, ssa); ssa.Write(writer); proc.Write(false, writer); } }
public void VpPhiLoops() { var m = new ProcedureBuilder(); var ssa = new SsaState(m.Procedure, null); ssaIds = ssa.Identifiers; var fp = Reg16("fp"); var a = Reg16("a"); var b = Reg16("b"); var c = Reg16("c"); var d = Reg16("d"); var x = Reg16("x"); var y = Reg16("y"); var z = Reg16("z"); m.Emit(m.Assign(y, m.IAdd(x, 4))); m.Emit(m.Assign(z, m.ISub(x, 8))); m.Emit(m.Assign(a, m.ISub(fp, 12))); m.Emit(m.Assign(b, m.ISub(fp, 12))); m.Emit(m.Assign(c, m.ISub(y, 4))); m.Emit(m.Assign(d, m.IAdd(z, 8))); var phiStm = m.Phi(x, a, b, c, d); var stms = m.Procedure.EntryBlock.Succ[0].Statements; stms.ForEach(stm => { var ass = stm.Instruction as Assignment; if (ass != null) { ssaIds[ass.Dst].DefStatement = stm; } var phiAss = stm.Instruction as PhiAssignment; if (phiAss != null) { ssaIds[phiAss.Dst].DefStatement = stm; } }); var vp = new ValuePropagator(arch, ssa, listener); vp.Transform(); Assert.AreEqual("x = fp - 0x000C", phiStm.Instruction.ToString()); }
private void RunTest(Action <ProcedureBuilder> builder) { builder(m); var importResolver = new Mock <IImportResolver>(); var sst = new SsaTransform( program, m.Procedure, new HashSet <Procedure>(), importResolver.Object, new ProgramDataFlow()); sst.Transform(); sst.RenameFrameAccesses = true; sst.Transform(); sst.AddUsesToExitBlock(); sst.RemoveDeadSsaIdentifiers(); rw = new LongAddRewriter(sst.SsaState); this.ssa = sst.SsaState; }
public void VpDbp() { Procedure proc = new DpbMock().Procedure; var gr = proc.CreateBlockDominatorGraph(); SsaTransform sst = new SsaTransform(new ProgramDataFlow(), proc, null, gr); SsaState ssa = sst.SsaState; ssa.DebugDump(true); ValuePropagator vp = new ValuePropagator(arch, ssa.Identifiers, proc); vp.Transform(); using (FileUnitTester fut = new FileUnitTester("Analysis/VpDbp.txt")) { proc.Write(false, fut.TextWriter); fut.TextWriter.WriteLine(); fut.AssertFilesEqual(); } }
private void RunTest(Action <ProcedureBuilder> builder) { builder(m); var dynamicLinker = new Mock <IDynamicLinker>(); var sst = new SsaTransform( program, m.Procedure, new HashSet <Procedure>(), dynamicLinker.Object, new ProgramDataFlow()); sst.Transform(); sst.RenameFrameAccesses = true; sst.Transform(); sst.AddUsesToExitBlock(); sst.RemoveDeadSsaIdentifiers(); rw = new LongAddRewriter(sst.SsaState, new FakeDecompilerEventListener()); this.ssa = sst.SsaState; }
public void Liv_CreateIncInitialValue() { var pb = new ProcedureBuilder(); ssa = new SsaState(pb.Procedure); LinearInductionVariableFinder liv = new LinearInductionVariableFinder(ssa, null); liv.Context.InitialValue = Constant.Word32(0); liv.Context.PhiStatement = new Statement(0, null, null); liv.Context.PhiIdentifier = new Identifier("foo_0", PrimitiveType.Word32, null); ssa.Identifiers.Add(liv.Context.PhiIdentifier, new SsaIdentifier(liv.Context.PhiIdentifier, liv.Context.PhiIdentifier, liv.Context.PhiStatement, null, false)); liv.Context.DeltaValue = Constant.Word32(1); liv.Context.DeltaStatement = new Statement(0, new Assignment(new Identifier("foo_1", PrimitiveType.Word32, null), new BinaryExpression(Operator.IAdd, PrimitiveType.Word32, liv.Context.PhiIdentifier, liv.Context.DeltaValue)), null); ssa.Identifiers[liv.Context.PhiIdentifier].Uses.Add(liv.Context.DeltaStatement); LinearInductionVariable iv = liv.CreateInductionVariable(); Assert.AreEqual("(1<32> 1<32> ?)", iv.ToString()); }
private void Build(Procedure proc, IProcessorArchitecture arch) { var platform = new DefaultPlatform(null, arch); this.proc = proc; var importResolver = new Mock <IImportResolver>().Object; Aliases alias = new Aliases(proc); alias.Transform(); var gr = proc.CreateBlockDominatorGraph(); SsaTransform sst = new SsaTransform( new ProgramDataFlow(), proc, null, gr, new HashSet <RegisterStorage>()); SsaState ssa = sst.SsaState; this.ssaIds = ssa.Identifiers; ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, platform); cce.Transform(); DeadCode.Eliminate(proc, ssa); var segmentMap = new SegmentMap(Address.Ptr32(0x00400000)); ValuePropagator vp = new ValuePropagator( segmentMap, ssa, new CallGraph(), null, new FakeDecompilerEventListener()); vp.Transform(); Coalescer coa = new Coalescer(proc, ssa); coa.Transform(); DeadCode.Eliminate(proc, ssa); }
public void Liv_CreateBareMinimum() { var pb = new ProcedureBuilder(); ssa = new SsaState(pb.Procedure); Identifier id0 = new Identifier("foo", PrimitiveType.Word32, new TemporaryStorage("foo", 1, PrimitiveType.Word32)); Identifier id1 = new Identifier("bar", PrimitiveType.Word32, new TemporaryStorage("bar", 1, PrimitiveType.Word32)); Identifier phi = new Identifier("i_3", PrimitiveType.Word32, null); ssa.Identifiers.Add(id0, new SsaIdentifier(id0, id0, null, null, false)); ssa.Identifiers.Add(id1, new SsaIdentifier(id1, id1, null, null, false)); ssa.Identifiers.Add(phi, new SsaIdentifier(phi, phi, null, null, false)); var liv = new LinearInductionVariableFinder(ssa, null); liv.Context.PhiStatement = new Statement(0, null, null); liv.Context.PhiIdentifier = phi; liv.Context.DeltaValue = Constant.Word32(1); LinearInductionVariable iv = liv.CreateInductionVariable(); Assert.AreEqual("(? 1<32> ?)", iv.ToString()); }
protected override void RunTest(Program program, TextWriter writer) { var listener = new FakeDecompilerEventListener(); var sc = new ServiceContainer(); sc.AddService <DecompilerEventListener>(listener); DataFlowAnalysis dfa = new DataFlowAnalysis(program, dynamicLinker.Object, sc); var ssts = dfa.UntangleProcedures(); foreach (var sst in ssts) { SsaState ssa = sst.SsaState; ConditionCodeEliminator cce = new ConditionCodeEliminator(program, ssa, listener); cce.Transform(); DeadCode.Eliminate(ssa); ssa.Write(writer); ssa.Validate(s => writer.WriteLine("*** SSA state invalid: {0}", s)); ssa.Procedure.Write(false, writer); } }
private void BuildTest(Action <ProcedureBuilder> bld) { var m = new ProcedureBuilder(); bld(m); var proc = m.Procedure; var flow = new ProgramDataFlow(); var program = new Program() { Architecture = m.Architecture, }; var sst = new SsaTransform( program, proc, new HashSet <Procedure>(), null, flow); this.ssa = sst.Transform(); this.ctx = new SsaEvaluationContext(m.Architecture, ssa.Identifiers, null); }
//[Test] public void VnLoopTest() { Program program = this.RewriteCodeFragment( @".i86 mov ax,1 mov bx,1 isdone: cmp ax,10 jz done inc ax inc bx jmp isdone done: mov [0002],ax mov [0004],bx ret "); using (FileUnitTester fut = new FileUnitTester("Analysis/VnLoopTest.txt")) { Procedure proc = program.Procedures.Values[0]; var gr = proc.CreateBlockDominatorGraph(); Aliases alias = new Aliases(proc, program.Architecture); alias.Transform(); SsaTransform sst = new SsaTransform(new ProgramDataFlow(), proc, null, gr, new HashSet <RegisterStorage>()); SsaState ssa = sst.SsaState; DumpProc(proc, ssa, fut.TextWriter); DeadCode.Eliminate(proc, ssa); DumpProc(proc, ssa, fut.TextWriter); ValueNumbering vn = new ValueNumbering(ssa.Identifiers, null); vn.Write(fut.TextWriter); fut.AssertFilesEqual(); } }
public void VpCopyPropagate() { var ssa = new SsaState(new Procedure("foo", new Frame(PrimitiveType.Pointer32)), null); ssaIds = ssa.Identifiers; Identifier x = Reg32("x"); Identifier y = Reg32("y"); Identifier z = Reg32("z"); Identifier w = Reg32("w"); Statement stmX = new Statement(0, new Assignment(x, new MemoryAccess(MemoryIdentifier.GlobalMemory, Constant.Word32(0x10004000), PrimitiveType.Word32)), null); Statement stmY = new Statement(1, new Assignment(y, x), null); Statement stmZ = new Statement(2, new Assignment(z, new BinaryExpression(Operator.IAdd, PrimitiveType.Word32, y, Constant.Word32(2))), null); Statement stmW = new Statement(3, new Assignment(w, y), null); ssaIds[x].DefStatement = stmX; ssaIds[y].DefStatement = stmY; ssaIds[z].DefStatement = stmZ; ssaIds[w].DefStatement = stmW; ssaIds[x].Uses.Add(stmY); ssaIds[y].Uses.Add(stmZ); ssaIds[y].Uses.Add(stmW); Assert.AreEqual("x = Mem0[0x10004000:word32]", stmX.Instruction.ToString()); Assert.AreEqual("y = x", stmY.Instruction.ToString()); Assert.AreEqual("z = y + 0x00000002", stmZ.Instruction.ToString()); Assert.AreEqual("w = y", stmW.Instruction.ToString()); ValuePropagator vp = new ValuePropagator(arch, ssa, listener); vp.Transform(stmX); vp.Transform(stmY); vp.Transform(stmZ); vp.Transform(stmW); Assert.AreEqual("x = Mem0[0x10004000:word32]", stmX.Instruction.ToString()); Assert.AreEqual("y = x", stmY.Instruction.ToString()); Assert.AreEqual("z = x + 0x00000002", stmZ.Instruction.ToString()); Assert.AreEqual("w = x", stmW.Instruction.ToString()); Assert.AreEqual(3, ssaIds[x].Uses.Count); Assert.AreEqual(0, ssaIds[y].Uses.Count); }
private Procedure RunTest(string sExp, SsaState ssa) { pf.ProcedureFlows[ssa.Procedure] = new ProcedureFlow(ssa.Procedure); var urf = new UsedRegisterFinder( pf, new Procedure[] { ssa.Procedure }, NullDecompilerEventListener.Instance); var flow = urf.ComputeLiveIn(ssa, true); var sw = new StringWriter(); sw.Write("Used: "); sw.Write(string.Join(",", flow.BitsUsed.OrderBy(p => p.Key.ToString()))); var sActual = sw.ToString(); if (sActual != sExp) { ssa.Procedure.Dump(true); Debug.WriteLine(sActual); Assert.AreEqual(sExp, sActual); } return(ssa.Procedure); }
public void VpPhiWithConstants() { Constant c1 = Constant.Word16(0x4711); Constant c2 = Constant.Word16(0x4711); Identifier r1 = Reg16("r1"); Identifier r2 = Reg16("r2"); Identifier r3 = Reg16("r3"); var stm1 = new Statement(1, new Assignment(r1, c1), null); var stm2 = new Statement(2, new Assignment(r2, c2), null); var proc = new Procedure("foo", arch.CreateFrame()); var ssa = new SsaState(proc, null); var r1Sid = ssa.Identifiers.Add(r1, null, null, false); var r2Sid = ssa.Identifiers.Add(r2, null, null, false); r1Sid.DefStatement = stm1; r2Sid.DefStatement = stm2; var vp = new ValuePropagator(arch, ssa, listener); Instruction instr = new PhiAssignment(r3, new PhiFunction(r1.DataType, r1, r2)); instr = instr.Accept(vp); Assert.AreEqual("r3 = 0x4711", instr.ToString()); }
private void Prepare(Procedure proc) { this.proc = proc; doms = proc.CreateBlockDominatorGraph(); SsaTransform sst = new SsaTransform(new ProgramDataFlow(), proc, doms); SsaState ssa = sst.SsaState; ssaIds = ssa.Identifiers; var arch = new FakeArchitecture(); var cce = new ConditionCodeEliminator(ssaIds, new DefaultPlatform(null, arch)); cce.Transform(); DeadCode.Eliminate(proc, ssa); var vp = new ValuePropagator(arch, ssa.Identifiers, proc); vp.Transform(); DeadCode.Eliminate(proc, ssa); }
private void Build(Program program) { var eventListener = new FakeDecompilerEventListener(); DataFlowAnalysis dfa = new DataFlowAnalysis(program, null, eventListener); dfa.UntangleProcedures(); foreach (Procedure proc in program.Procedures.Values) { Aliases alias = new Aliases(proc, program.Architecture); alias.Transform(); var gr = proc.CreateBlockDominatorGraph(); SsaTransform sst = new SsaTransform(dfa.ProgramDataFlow, proc, null, gr, new HashSet <RegisterStorage>()); SsaState ssa = sst.SsaState; ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, program.Platform); cce.Transform(); DeadCode.Eliminate(proc, ssa); var vp = new ValuePropagator(program.Architecture, ssa, eventListener); vp.Transform(); DeadCode.Eliminate(proc, ssa); Coalescer coa = new Coalescer(proc, ssa); coa.Transform(); DeadCode.Eliminate(proc, ssa); LiveCopyInserter lci = new LiveCopyInserter(proc, ssa.Identifiers); lci.Transform(); WebBuilder web = new WebBuilder(proc, ssa.Identifiers, new Dictionary <Identifier, LinearInductionVariable>()); web.Transform(); ssa.ConvertBack(false); } }
private void Build(Procedure proc, IProcessorArchitecture arch) { var platform = new DefaultPlatform(null, arch); var program = new Program() { Architecture = arch, Platform = platform, }; this.proc = proc; var dynamicLinker = new Mock <IDynamicLinker>().Object; var listener = new FakeDecompilerEventListener(); var sst = new SsaTransform( program, proc, new HashSet <Procedure>(), dynamicLinker, new ProgramDataFlow()); sst.Transform(); ssa = sst.SsaState; ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, platform, listener); cce.Transform(); var segmentMap = new SegmentMap(Address.Ptr32(0x00123400)); ValuePropagator vp = new ValuePropagator(segmentMap, ssa, program.CallGraph, dynamicLinker, listener); vp.Transform(); DeadCode.Eliminate(ssa); Coalescer coa = new Coalescer(ssa); coa.Transform(); DeadCode.Eliminate(ssa); sla = new SsaLivenessAnalysis(ssa); sla2 = new SsaLivenessAnalysis2(ssa); sla2.Analyze(); }
private void RunUnitTest(ProcedureBuilder m, string outfile) { var flow = new ProgramDataFlow(); var dynamicLinker = new Mock <IDynamicLinker>(); var proc = m.Procedure; var platform = new FakePlatform(null, m.Architecture) { Test_CreateTrashedRegisters = () => new HashSet <RegisterStorage>() { (RegisterStorage)r1.Storage, (RegisterStorage)r2.Storage, } }; var program = new Program() { Architecture = m.Architecture, Platform = platform, }; var sst = new SsaTransform( program, proc, new HashSet <Procedure>(), dynamicLinker.Object, flow); sst.Transform(); ssa = sst.SsaState; using (var fut = new FileUnitTester(outfile)) { ssa.Write(fut.TextWriter); proc.Write(false, fut.TextWriter); fut.AssertFilesEqual(); ssa.Validate(s => Assert.Fail(s)); } }