public void TerConstants()
        {
            var     arch    = new FakeArchitecture();
            Program program = new Program(
                new SegmentMap(Address.Ptr32(0x10000)),
                arch,
                new DefaultPlatform(null, arch));

            SetupPreStages(program);
            Constant     r   = Constant.Real32(3.0F);
            Constant     i   = Constant.Int32(1);
            Identifier   x   = new Identifier("x", PrimitiveType.Word32, null);
            Assignment   ass = new Assignment(x, r);
            TypeVariable tvR = r.TypeVariable = program.TypeFactory.CreateTypeVariable();
            TypeVariable tvI = i.TypeVariable = program.TypeFactory.CreateTypeVariable();
            TypeVariable tvX = x.TypeVariable = program.TypeFactory.CreateTypeVariable();

            program.TypeStore.TypeVariables.AddRange(new TypeVariable[] { tvR, tvI, tvX });
            UnionType u = program.TypeFactory.CreateUnionType(null, null, new DataType[] { r.DataType, i.DataType });

            tvR.OriginalDataType = r.DataType;
            tvI.OriginalDataType = i.DataType;
            tvX.OriginalDataType = x.DataType;
            tvR.DataType         = u;
            tvI.DataType         = u;
            tvX.DataType         = u;
            ctn.RenameAllTypes(program.TypeStore);
            var         ter   = new TypedExpressionRewriter(program, null);
            Instruction instr = ter.TransformAssignment(ass);

            Assert.AreEqual("x.u0 = 3.0F", instr.ToString());
        }
        public void EP_LValue()
        {
            var        arch     = new FakeArchitecture();
            var        platform = new FakePlatform(null, arch);
            var        p        = new ProgramBuilder(arch);
            Identifier r2       = null;
            Identifier sp       = null;
            var        proc     = p.Add("main", (m) =>
            {
                r2 = m.Register("r2");
                sp = m.Frame.EnsureRegister(arch.StackRegister);
                m.Store(m.ISub(sp, 12), m.ISub(sp, 16));
                m.Store(m.ISub(sp, 12), m.Word32(2));
            });

            var ctx        = new SymbolicEvaluationContext(arch, proc.Frame);
            var simplifier = new ExpressionSimplifier(ctx, listener);
            var ep         = new ExpressionPropagator(platform, simplifier, ctx, new ProgramDataFlow());

            ctx.RegisterState[arch.StackRegister] = proc.Frame.FramePointer;

            var stms   = proc.EntryBlock.Succ[0].Statements;
            var instr1 = stms[0].Instruction.Accept(ep);

            Assert.AreEqual("dwLoc0C = fp - 0x00000010", instr1.ToString());
            var instr2 = stms[1].Instruction.Accept(ep);

            Assert.AreEqual("dwLoc0C = 0x00000002", instr2.ToString());
        }
Exemplo n.º 3
0
        public void Jprjs_Programs()
        {
            var arch    = new FakeArchitecture(new ServiceContainer());
            var proc1   = new Procedure(arch, "fn00123400", Address.Ptr32(0x00123400), new Frame(PrimitiveType.Ptr32));
            var proc2   = new Procedure(arch, "fn00123500", Address.Ptr32(0x00123500), new Frame(PrimitiveType.Ptr32));
            var project = new Project
            {
                Programs =
                {
                    new Program
                    {
                        Name       = "prog1.exe",
                        Procedures =
                        {
                            { Address.Ptr32(0x00123400), proc1 },
                            { Address.Ptr32(0x00123500), proc2 },
                        }
                    }
                }
            };

            var sExp =
                "{" +
                "'programs':[" +
                "{'name':'prog1.exe'," +
                "'procedures':[" +
                "{'address':'00123400','name':'fn00123400'}," +
                "{'address':'00123500','name':'fn00123500'}" +
                "]}" +
                "]" +
                "}";

            RunTest(sExp, project);
        }
        public void EP_StackReference()
        {
            var arch     = new FakeArchitecture();
            var platform = new FakePlatform(null, arch);
            var p        = new ProgramBuilder(arch);
            var proc     = p.Add("main", (m) =>
            {
                var sp = m.Frame.EnsureRegister(m.Architecture.StackRegister);
                var r1 = m.Register(1);
                m.Assign(sp, m.ISub(sp, 4));
                m.Assign(r1, m.Mem32(m.IAdd(sp, 8)));
                m.Return();
            });

            var ctx        = new SymbolicEvaluationContext(arch, proc.Frame);
            var simplifier = new ExpressionSimplifier(ctx, listener);
            var ep         = new ExpressionPropagator(platform, simplifier, ctx, new ProgramDataFlow());

            ctx.RegisterState[arch.StackRegister] = proc.Frame.FramePointer;

            var stms     = proc.EntryBlock.Succ[0].Statements;
            var newInstr = stms[0].Instruction.Accept(ep);

            Assert.AreEqual("r63 = fp - 0x00000004", newInstr.ToString());
            newInstr = stms[1].Instruction.Accept(ep);
            Assert.AreEqual("r1 = dwArg04", newInstr.ToString());
        }
        public void EP_AddrOf()
        {
            var        arch = new FakeArchitecture();
            var        platform = new FakePlatform(null, arch);
            var        p = new ProgramBuilder(arch);
            Identifier r2 = null, r3 = null;
            var        proc = p.Add("main", (m) =>
            {
                r2 = m.Register("r2");
                r3 = m.Register("r3");
                m.Assign(r2, 0x1234);                                      // after which R2 has a definite value
                m.SideEffect(m.Fn("Foo", m.Out(PrimitiveType.Ptr32, r2))); // Can't promise R2 is preserved after call, so should be invalid.
                m.Assign(r3, r2);
            });

            var ctx        = new SymbolicEvaluationContext(arch, proc.Frame);
            var simplifier = new ExpressionSimplifier(ctx, listener);
            var ep         = new ExpressionPropagator(platform, simplifier, ctx, new ProgramDataFlow());

            ctx.RegisterState[arch.StackRegister] = proc.Frame.FramePointer;

            var stms = proc.EntryBlock.Succ[0].Statements;

            stms[0].Instruction.Accept(ep);
            Assert.AreEqual("0x00001234", ctx.GetValue(r2).ToString());
            var instr2 = stms[1].Instruction.Accept(ep);

            Assert.AreEqual("Foo(out r2)", instr2.ToString());
            Assert.AreEqual("<invalid>", ctx.GetValue(r2).ToString());
            var instr3 = stms[2].Instruction.Accept(ep);

            Assert.AreEqual("r3 = r2", instr3.ToString());
            Assert.AreEqual("<invalid>", ctx.GetValue(r2).ToString());
            Assert.AreEqual("<invalid>", ctx.GetValue(r3).ToString());
        }
Exemplo n.º 6
0
        public void Hexldr_LoadOneSegtTests()
        {
            string data =
                @"
:020000040000FA
:10010000214601360121470136007EFE09D2190140
:100110002146017E17C20001FF5F16002148011928
:10012000194E79234623965778239EDA3F01B2CAA7
:100130003F0156702B5E712B722B732146013421C7
:00000001FF
";
            var hex     = new HexLoader(sc, "foo.text", Encoding.ASCII.GetBytes(data));
            var arch    = new FakeArchitecture(sc);
            var program = hex.LoadProgram(Address.Ptr32(0x00), arch, new DefaultPlatform(sc, arch));

            Assert.AreEqual(1, program.SegmentMap.Segments.Count, "Wrong number of segments");

            var bmem0 = (ByteMemoryArea)program.SegmentMap.Segments.Values[0].MemoryArea;

            Assert.AreEqual(Address.Ptr32(0x100), bmem0.BaseAddress);
            Assert.AreEqual(64, bmem0.Length);
            Assert.AreEqual(0x21, bmem0.Bytes[0]);
            Assert.AreEqual(0x21, bmem0.Bytes[0x10]);
            Assert.AreEqual(0x19, bmem0.Bytes[0x20]);
            Assert.AreEqual(0x34, bmem0.Bytes[0x3E]);
        }
        public void EP_IndirectCall()
        {
            var arch = new FakeArchitecture();
            var p    = new ProgramBuilder(arch);
            var proc = p.Add("main", (m) =>
            {
                var r1 = m.Register("r1");

                m.Assign(r1, m.Word32(0x42));
                m.Call(r1, 4);
                m.Return();
            });

            var platform = new FakePlatform(null, arch)
            {
                Test_CreateTrashedRegisters = () => new HashSet <RegisterStorage>()
            };
            var ctx        = new SymbolicEvaluationContext(arch, proc.Frame);
            var simplifier = new ExpressionSimplifier(ctx, listener);
            var ep         = new ExpressionPropagator(platform, simplifier, ctx, new ProgramDataFlow());

            ctx.RegisterState[arch.StackRegister] = proc.Frame.FramePointer;
            var stms = proc.EntryBlock.Succ[0].Statements;

            stms[0].Instruction.Accept(ep);
            var newInstr = stms[1].Instruction.Accept(ep);

            Assert.AreEqual("call 0x00000042 (retsize: 4; depth: 4)", newInstr.ToString());
        }
Exemplo n.º 8
0
        private void Prepare(Procedure proc)
        {
            var listener      = new FakeDecompilerEventListener();
            var dynamicLinker = new Mock <IDynamicLinker>().Object;

            doms = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(
                new Program(),
                proc,
                new HashSet <Procedure>(),
                dynamicLinker,
                new ProgramDataFlow());

            sst.Transform();
            this.ssa = sst.SsaState;

            var arch = new FakeArchitecture();
            var cce  = new ConditionCodeEliminator(ssa, new DefaultPlatform(null, arch));

            cce.Transform();

            DeadCode.Eliminate(ssa);

            var segmentMap = new SegmentMap(Address.Ptr32(0x00123400));
            var vp         = new ValuePropagator(segmentMap, ssa, new CallGraph(), dynamicLinker, listener);

            vp.Transform();

            DeadCode.Eliminate(ssa);
        }
Exemplo n.º 9
0
        public void EP_IndirectCall()
        {
            var arch = new FakeArchitecture();
            var p    = new ProgramBuilder(arch);
            var proc = p.Add("main", (m) =>
            {
                var r1 = m.Register("r1");

                m.Assign(r1, m.Word32(0x42));
                m.Emit(new CallInstruction(r1, new CallSite(4, 0)));
                m.Return();
            });

            var ctx        = new SymbolicEvaluationContext(arch, proc.Frame);
            var simplifier = new ExpressionSimplifier(ctx);
            var ep         = new ExpressionPropagator(arch, simplifier, ctx, new ProgramDataFlow());

            ctx.RegisterState[arch.StackRegister] = proc.Frame.FramePointer;
            var stms = proc.EntryBlock.Succ[0].Statements;

            stms[0].Instruction.Accept(ep);
            var newInstr = stms[1].Instruction.Accept(ep);

            Assert.AreEqual("call 0x00000042 (retsize: 4; depth: 4)", newInstr.ToString());
        }
Exemplo n.º 10
0
        private void Prepare(Procedure proc)
        {
            var listener = new FakeDecompilerEventListener();

            this.proc = proc;
            doms      = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(
                new ProgramDataFlow(),
                proc,
                null,
                doms,
                new HashSet <RegisterStorage>());
            SsaState ssa = sst.SsaState;

            ssaIds = ssa.Identifiers;

            var arch = new FakeArchitecture();
            var cce  = new ConditionCodeEliminator(ssa, new DefaultPlatform(null, arch));

            cce.Transform();

            DeadCode.Eliminate(proc, ssa);

            var vp = new ValuePropagator(arch, ssa, listener);

            vp.Transform();

            DeadCode.Eliminate(proc, ssa);
        }
Exemplo n.º 11
0
        private void Prepare(Procedure proc)
        {
            var listener       = new FakeDecompilerEventListener();
            var importResolver = MockRepository.GenerateStub <IImportResolver>();

            importResolver.Replay();
            this.proc = proc;
            doms      = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(
                new ProgramDataFlow(),
                proc,
                importResolver,
                doms,
                new HashSet <RegisterStorage>());
            SsaState ssa = sst.SsaState;

            ssaIds = ssa.Identifiers;

            var arch = new FakeArchitecture();
            var cce  = new ConditionCodeEliminator(ssa, new DefaultPlatform(null, arch));

            cce.Transform();

            DeadCode.Eliminate(proc, ssa);

            var segmentMap = new SegmentMap(Address.Ptr32(0x00123400));
            var vp         = new ValuePropagator(segmentMap, ssa, importResolver, listener);

            vp.Transform();

            DeadCode.Eliminate(proc, ssa);
        }
Exemplo n.º 12
0
        public void Setup()
        {
            bmem = new ByteMemoryArea(Address.Ptr32(0x00100000), new byte[1024]);
            var arch = new FakeArchitecture(new ServiceContainer());

            this.program = new Program
            {
                Architecture = arch,
                SegmentMap   = new SegmentMap(
                    bmem.BaseAddress,
                    new ImageSegment(".text", bmem, AccessMode.ReadWriteExecute)),
                Platform = new DefaultPlatform(null, arch),
            };
            store   = program.TypeStore;
            factory = program.TypeFactory;
            globals = program.Globals;
            store.EnsureExpressionTypeVariable(factory, globals);

            StructureType s = new StructureType(null, 0);

            s.Fields.Add(0x00100000, PrimitiveType.Word32, null);

            TypeVariable     tvGlobals = store.EnsureExpressionTypeVariable(factory, globals);
            EquivalenceClass eqGlobals = new EquivalenceClass(tvGlobals);

            eqGlobals.DataType = s;
            var globalsPtr = new Pointer(eqGlobals, 32);

            globals.TypeVariable.DataType         = globalsPtr;
            globals.TypeVariable.OriginalDataType = globalsPtr;
            globals.DataType = globalsPtr;
        }
Exemplo n.º 13
0
        public void Setup()
        {
            mem = new MemoryArea(Address.Ptr32(0x00100000), new byte[1024]);
            var arch = new FakeArchitecture();

            this.program = new Program
            {
                Architecture = arch,
                ImageMap     = new ImageMap(
                    mem.BaseAddress,
                    new ImageSegment(".text", mem, AccessMode.ReadWriteExecute)),
                Platform = new DefaultPlatform(null, arch),
            };
            store   = program.TypeStore;
            factory = program.TypeFactory;
            globals = program.Globals;
            store.EnsureExpressionTypeVariable(factory, globals);

            StructureType s = new StructureType(null, 0);

            s.Fields.Add(0x00100000, PrimitiveType.Word32, null);

            TypeVariable     tvGlobals = store.EnsureExpressionTypeVariable(factory, globals);
            EquivalenceClass eqGlobals = new EquivalenceClass(tvGlobals);

            eqGlobals.DataType            = s;
            globals.TypeVariable.DataType = new Pointer(eqGlobals, 4);
            globals.DataType = globals.TypeVariable.DataType;

            tcr = new TypedConstantRewriter(program);
        }
Exemplo n.º 14
0
        public void Setup()
        {
            var image   = new LoadedImage(Address.Ptr32(0x00100000), new byte[1024]);
            var arch    = new FakeArchitecture();
            var program = new Program
            {
                Image        = image,
                Architecture = arch,
                ImageMap     = image.CreateImageMap(),
                Platform     = new DefaultPlatform(null, arch),
            };

            store   = program.TypeStore;
            factory = program.TypeFactory;
            globals = program.Globals;
            store.EnsureExpressionTypeVariable(factory, globals);

            StructureType s = new StructureType(null, 0);

            s.Fields.Add(0x00100000, PrimitiveType.Word32, null);

            TypeVariable     tvGlobals = store.EnsureExpressionTypeVariable(factory, globals);
            EquivalenceClass eqGlobals = new EquivalenceClass(tvGlobals);

            eqGlobals.DataType            = s;
            globals.TypeVariable.DataType = new Pointer(eqGlobals, 4);
            globals.DataType = globals.TypeVariable.DataType;

            tcr = new TypedConstantRewriter(program);
        }
Exemplo n.º 15
0
 public void Setup()
 {
     arch = new FakeArchitecture();
     proc = new Procedure("Test", new Frame(arch.FramePointerType));
     flow = new ProcedureFlow(proc, arch);
     ctx  = new SymbolicEvaluationContext(arch, proc.Frame);
     trs  = new TrashedRegisterSummarizer(arch, proc, flow, ctx);
 }
Exemplo n.º 16
0
        public void Setup()
        {
            var sc = new ServiceContainer();

            this.arch     = new FakeArchitecture(sc);
            this.platform = new DefaultPlatform(sc, arch);
            symbolTable   = new SymbolTable(platform);
        }
Exemplo n.º 17
0
        private void Given_Program(uint uAddrBase)
        {
            var addrBase = Address.Ptr32(uAddrBase);
            var sc       = new ServiceContainer();
            var arch     = new FakeArchitecture(sc);

            this.program = new Program(new SegmentMap(addrBase), arch, new DefaultPlatform(sc, arch));
        }
Exemplo n.º 18
0
        public void Setup()
        {
            arch = new FakeArchitecture();
            sp   = new RegisterStorage("sp", 42, 0, PrimitiveType.Ptr32);
            arch.StackRegister = sp;

            idSp = new Identifier(sp.Name, sp.DataType, sp);
            m    = new ExpressionEmitter();
        }
Exemplo n.º 19
0
        private static Program CreateProgram()
        {
            var arch = new FakeArchitecture(new ServiceContainer());

            return(new Program
            {
                Architecture = arch,
                Platform = new DefaultPlatform(arch.Services, arch),
            });
        }
Exemplo n.º 20
0
        public void Setup()
        {
            this.m       = new ExpressionEmitter();
            this.store   = new TypeStore();
            this.factory = new TypeFactory();
            var arch     = new FakeArchitecture();
            var platform = new DefaultPlatform(null, arch);

            this.exa = new ExpressionTypeAscender(platform, store, factory);
        }
Exemplo n.º 21
0
        private static Program CreateProgram()
        {
            var arch = new FakeArchitecture();

            return(new Program
            {
                Architecture = arch,
                Platform = new DefaultPlatform(null, arch),
            });
        }
Exemplo n.º 22
0
        public void Setup()
        {
            var arch     = new FakeArchitecture();
            var platform = new FakePlatform(null, arch);

            this.program = new Program
            {
                Architecture = arch,
                Platform     = platform,
            };
        }
Exemplo n.º 23
0
        public void Setup()
        {
            sp   = new RegisterStorage("sp", 42, PrimitiveType.Pointer32);
            arch = new FakeArchitecture();
            arch.StackRegister = sp;

            sce = new FakeProcessorState(arch);

            idSp = new Identifier(sp.Name, sp.DataType, sp);
            m    = new ExpressionEmitter();
        }
Exemplo n.º 24
0
        public void Setup()
        {
            mr             = new MockRepository();
            fakeArch       = new FakeArchitecture();
            importResolver = mr.StrictMock <IImportResolver>();
            callSigs       = new Dictionary <Address, ProcedureSignature>();
            arch           = fakeArch;
            var r1 = arch.GetRegister(1);

            reg1 = new Identifier(r1.Name, PrimitiveType.Word32, r1);
        }
Exemplo n.º 25
0
        private static Program Given_FlatProgram()
        {
            var arch     = new FakeArchitecture(new ServiceContainer());
            var platform = new FakePlatform(arch.Services, arch);
            var mem      = new ByteMemoryArea(Address.Ptr32(0x00123300), new byte[0x1000]);
            var segment  = new ImageSegment(".data", mem, AccessMode.ReadWrite);
            var segments = new SegmentMap(segment.Address, segment);
            var program  = new Program(segments, arch, platform);

            return(program);
        }
Exemplo n.º 26
0
        public void Setup()
        {
            sp = new RegisterStorage("sp", 42, 0, PrimitiveType.Pointer32);
            arch = new FakeArchitecture();
            arch.StackRegister = sp;

            sce = new FakeProcessorState(arch);

            idSp = new Identifier(sp.Name, sp.DataType, sp);
            m = new ExpressionEmitter();
        }
Exemplo n.º 27
0
        public void Setup()
        {
            var sc       = new ServiceContainer();
            var arch     = new FakeArchitecture(sc);
            var platform = new FakePlatform(sc, arch);

            this.program = new Program
            {
                Architecture = arch,
                Platform     = platform,
            };
        }
Exemplo n.º 28
0
 public void SetUp()
 {
     store   = new TypeStore();
     factory = new TypeFactory();
     aen     = new ExpressionNormalizer(PrimitiveType.Pointer32);
     eqb     = new EquivalenceClassBuilder(factory, store);
     arch    = new FakeArchitecture();
     program = new Program();
     program.Architecture = arch;
     program.Platform     = new DefaultPlatform(null, arch);
     dtb = new DataTypeBuilder(factory, store, program.Platform);
 }
Exemplo n.º 29
0
        public void Setup()
        {
            this.m       = new ExpressionEmitter();
            this.store   = new TypeStore();
            this.factory = new TypeFactory();
            var arch     = new FakeArchitecture(new ServiceContainer());
            var platform = new DefaultPlatform(null, arch);

            program = new Program {
                Architecture = arch, Platform = platform
            };
            this.exa = new ExpressionTypeAscender(program, store, factory);
        }
Exemplo n.º 30
0
 public void SetUp()
 {
     store                = new TypeStore();
     factory              = new TypeFactory();
     listener             = new FakeDecompilerEventListener();
     aen                  = new ExpressionNormalizer(PrimitiveType.Ptr32);
     eqb                  = new EquivalenceClassBuilder(factory, store, listener);
     arch                 = new FakeArchitecture(new ServiceContainer());
     program              = new Program();
     program.Architecture = arch;
     program.Platform     = new DefaultPlatform(null, arch);
     dtb                  = new DataTypeBuilder(factory, store, program.Platform);
 }
Exemplo n.º 31
0
 public void Setup()
 {
     this.m       = new ExpressionEmitter();
     this.store   = new TypeStore();
     this.factory = new TypeFactory();
     this.arch    = new FakeArchitecture();
     this.program = new Program {
         Architecture = arch, Platform = new DefaultPlatform(null, arch)
     };
     this.exa = new ExpressionTypeAscender(program, store, factory);
     this.exd = new ExpressionTypeDescender(program, store, factory);
     store.EnsureExpressionTypeVariable(factory, program.Globals, "globals_t");
 }
Exemplo n.º 32
0
 void RunTest(string c_code, string expectedXml)
 {
     StringReader reader = null;
     StringWriter writer = null;
     try
     {
         reader = new StringReader(c_code);
         writer = new StringWriter();
         var xWriter = new XmlnsHidingWriter(writer)
         {
             Formatting = Formatting.Indented
         };
         var arch = new FakeArchitecture();
         var platform = new DefaultPlatform(null, arch);
         var xc = new XmlConverter(reader, xWriter, platform);
         xc.Convert();
         writer.Flush();
         Assert.AreEqual(expectedXml, writer.ToString());
     }
     catch
     {
         Debug.WriteLine(writer.ToString());
         throw;
     }
     finally
     {
         if (writer != null)
             writer.Dispose();
         if (reader != null)
             reader.Dispose();
     }
 }