Ejemplo n.º 1
0
 public void Setup()
 {
     mr = new MockRepository();
     program = new Program();
     proc = new Procedure("testProc", new Frame(PrimitiveType.Word32));
     block = proc.AddBlock("l00100000");
     trace = new RtlTrace(0x00100000);
     r0 = new Identifier("r0", PrimitiveType.Word32, new RegisterStorage("r0", 0, 0, PrimitiveType.Word32));
     r1 = new Identifier("r1", PrimitiveType.Word32, new RegisterStorage("r1", 1, 0, PrimitiveType.Word32));
     r2 = new Identifier("r2", PrimitiveType.Word32, new RegisterStorage("r2", 2, 0, PrimitiveType.Word32));
     sp = new Identifier("sp", PrimitiveType.Word32, new RegisterStorage("sp", 15, 0, PrimitiveType.Word32));
     grf = proc.Frame.EnsureFlagGroup(Registers.eflags, 3, "SCZ", PrimitiveType.Byte);
     var sc = new ServiceContainer();
     var listener = mr.Stub<DecompilerEventListener>();
     scanner = mr.StrictMock<IScanner>();
     arch = mr.Stub<IProcessorArchitecture>();
     program.Architecture = arch;
     program.SegmentMap = new SegmentMap(
         Address.Ptr32(0x00100000),
         new ImageSegment(
             ".text",
             new MemoryArea(Address.Ptr32(0x00100000), new byte[0x20000]),
             AccessMode.ReadExecute));
     arch.Replay();
     program.Platform = new DefaultPlatform(null, arch);
     arch.BackToRecord();
     arch.Stub(s => s.StackRegister).Return((RegisterStorage)sp.Storage);
     arch.Stub(s => s.PointerType).Return(PrimitiveType.Pointer32);
     scanner.Stub(s => s.Services).Return(sc);
     sc.AddService<DecompilerEventListener>(listener);
 }
Ejemplo n.º 2
0
		public void SetUp()
		{
            var arch = new IntelArchitecture(ProcessorMode.Real);
            prog = new Program() { Architecture = arch };
            asm = new X86TextAssembler(arch);
			configFile = null;
		}
Ejemplo n.º 3
0
        public static void Rewrite(
            Program program,
            ProgramDataFlow summaries,
            DecompilerEventListener eventListener)
        {
            GlobalCallRewriter crw = new GlobalCallRewriter(program, summaries, eventListener);

            foreach (Procedure proc in program.Procedures.Values)
            {
                if (eventListener.IsCanceled())
                {
                    return;
                }
                ProcedureFlow flow = crw.mpprocflow[proc];
                flow.Dump(proc.Architecture);
                crw.AdjustLiveOut(flow);
                crw.EnsureSignature(proc, flow);
                crw.AddUseInstructionsForOutArguments(proc);
            }

            foreach (Procedure proc in program.Procedures.Values)
            {
                if (eventListener.IsCanceled())
                {
                    return;
                }
                crw.RewriteCalls(proc);
                crw.RewriteReturns(proc);
            }
        }
Ejemplo n.º 4
0
        public void HSC_x86_FindCallsToProcedure()
        {
#if OLD
            var image = new LoadedImage(Address.Ptr32(0x001000), new byte[] {
                0xE8, 0x0B, 0x00, 0x00,  0x00, 0xE8, 0x07, 0x00,
                0x00, 0x00, 0xC3, 0x00,  0x00, 0x00, 0x00, 0x00,
                0xC3, 0xC3                                      // 1010, 1011
            });
            prog = new Program
            {
                Image = image,
                ImageMap = image.CreateImageMap(),
                Architecture = new IntelArchitecture(ProcessorMode.Protected32),
            };
#else
            Given_Image32(0x001000, 
                "E8 0B 00 00 00 E8 07 00 " +
                "00 00 C3 00 00 00 00 00 " +
                "C3 C3 ");                                     // 1010, 1011
            Given_x86_32();
#endif
            Given_RewriterHost();
            mr.ReplayAll();

            Assert.AreEqual(18, prog.Image.Length);

            var hsc = new HeuristicScanner(prog, host, eventListener);
            var linAddrs = hsc.FindCallOpcodes(new Address[]{
                Address.Ptr32(0x1010),
                Address.Ptr32(0x1011)}).ToList();

            Assert.AreEqual(2, linAddrs.Count);
            Assert.IsTrue(linAddrs.Contains(Address.Ptr32(0x1000)));
            Assert.IsTrue(linAddrs.Contains(Address.Ptr32(0x1005)));
        }
Ejemplo n.º 5
0
 protected Program RewriteX86RealFragment(string asmFragment, Address addrBase)
 {
     var asm = new X86TextAssembler(sc, new X86ArchitectureReal());
     program = asm.AssembleFragment(addrBase, asmFragment);
     program.Platform = new DefaultPlatform(null, program.Architecture);
     program.EntryPoints.Add(new EntryPoint(addrBase, program.Architecture.CreateProcessorState()));
     return RewriteProgram();
 }
Ejemplo n.º 6
0
 public ExpressionTypeAscender(
     Program program,
     TypeStore store,
     TypeFactory factory) :
         base(program, factory)
 {
     this.unifier = new DataTypeBuilderUnifier(factory, store);
 }
Ejemplo n.º 7
0
		public void SetUp()
		{
            var arch = new X86ArchitectureReal();
            program = new Program() { Architecture = arch };
            sc = new ServiceContainer();
            sc.AddService<IFileSystemService>(new FileSystemServiceImpl());
            asm = new X86TextAssembler(sc, arch);
			configFile = null;
		}
Ejemplo n.º 8
0
 private void AssertProgram(string sExp, Program program)
 {
     var sw = new StringWriter();
     program.Procedures.Values.First().Write(false, sw);
     var sActual = sw.ToString();
     if (sExp != sActual) 
         Debug.WriteLine(sActual);
     Assert.AreEqual(sExp, sw.ToString());
 }
Ejemplo n.º 9
0
 public void Setup()
 {
     this.mr = new MockRepository();
     var arch = mr.Stub<IProcessorArchitecture>();
     var platform = mr.Stub<IPlatform>();
     arch.Stub(a => a.WordWidth).Return(PrimitiveType.Word32);
     platform.Stub(p => p.Architecture).Return(arch);
     platform.Stub(p => p.GetByteSizeFromCBasicType(CBasicType.Long)).Return(4);
     platform.Stub(p => p.GetByteSizeFromCBasicType(CBasicType.LongLong)).Return(8);
     platform.Stub(p => p.GetByteSizeFromCBasicType(CBasicType.Double)).Return(8);
     platform.Stub(p => p.PointerType).Return(PrimitiveType.Pointer32);
     this.program = new Program { Platform = platform };
 }
Ejemplo n.º 10
0
 protected Program RewriteProgram32(string sourceFilename, Address addrBase)
 {
     sc = new ServiceContainer();
     sc.AddService<IConfigurationService>(new FakeDecompilerConfiguration());
     sc.AddService<IFileSystemService>(new FileSystemServiceImpl());
     sc.AddService<DecompilerEventListener>(new FakeDecompilerEventListener());
     var ldr = new Loader(sc);
     var arch = new X86ArchitectureFlat32();
     program = ldr.AssembleExecutable(
         FileUnitTester.MapTestPath(sourceFilename),
         new X86TextAssembler(sc, arch) { Platform = new DefaultPlatform(sc, arch) },
         addrBase);
     return RewriteProgram();
 }
Ejemplo n.º 11
0
 public void Setup()
 {
     this.mr = new MockRepository();
     this.sc = new ServiceContainer();
     this.eventListener = mr.Stub<DecompilerEventListener>();
     this.sc.AddService(typeof(DecompilerEventListener), this.eventListener);
     var arch = mr.Stub<IProcessorArchitecture>();
     var platform = mr.Stub<IPlatform>();
     arch.Stub(a => a.WordWidth).Return(PrimitiveType.Word32);
     platform.Stub(p => p.Architecture).Return(arch);
     platform.Stub(p => p.GetByteSizeFromCBasicType(CBasicType.Long)).Return(4);
     platform.Stub(p => p.GetByteSizeFromCBasicType(CBasicType.Double)).Return(8);
     platform.Stub(p => p.PointerType).Return(PrimitiveType.Pointer32);
     this.program = new Program { Platform = platform };
 }
Ejemplo n.º 12
0
        public void Setup()
        {
            mr = new MockRepository();
            program = new Program();
            proc = new Procedure("testProc", new Frame(PrimitiveType.Word32));
            block = proc.AddBlock("l00100000");
            trace = new RtlTrace(0x00100000);
            r0 = new Identifier("r0", PrimitiveType.Word32, new RegisterStorage("r0", 0, 0, PrimitiveType.Word32));
            r1 = new Identifier("r1", PrimitiveType.Word32, new RegisterStorage("r1", 1, 0, PrimitiveType.Word32));
            r2 = new Identifier("r2", PrimitiveType.Word32, new RegisterStorage("r2", 2, 0, PrimitiveType.Word32));
            grf = proc.Frame.EnsureFlagGroup(Registers.eflags, 3, "SCZ", PrimitiveType.Byte);

            scanner = mr.StrictMock<IScanner>();
            arch = mr.DynamicMock<IProcessorArchitecture>();
            arch.Stub(s => s.PointerType).Return(PrimitiveType.Pointer32);
            program.Architecture = arch;
            arch.Replay();
            program.Platform = new DefaultPlatform(null, arch);
            arch.BackToRecord();
        }
Ejemplo n.º 13
0
        public void HSC_ARM32_Calls()
        {
            var image = CreateImage(Address.Ptr32(0x1000),
                0xE1A0F00E,     // mov r15,r14 (return)
                0xEBFFFFFD,
                0xEBFFFFFC);
            prog = new Program
            {
                Image = image,
                Architecture = new Arm32ProcessorArchitecture(),
            };
            var host = mr.Stub<IRewriterHost>();
            mr.ReplayAll();

            var hsc = new HeuristicScanner(prog, host);
            var linAddrs = hsc.FindCallOpcodes(new Address[] {
                Address.Ptr32(0x1000),
            }).ToList();

            Assert.AreEqual(2, linAddrs.Count);
            Assert.IsTrue(linAddrs.Contains(Address.Ptr32(0x1004)));
            Assert.IsTrue(linAddrs.Contains(Address.Ptr32(0x1008)));
        }
Ejemplo n.º 14
0
        protected Program RewriteProgramMsdos(string sourceFilename, Address addrBase)
		{
            var cfgSvc = MockRepository.GenerateStub<IConfigurationService>();
            var env = MockRepository.GenerateStub<OperatingEnvironment>();
            var tlSvc = MockRepository.GenerateStub<ITypeLibraryLoaderService>();
            cfgSvc.Stub(c => c.GetEnvironment("ms-dos")).Return(env);
            cfgSvc.Stub(c => c.GetSignatureFiles()).Return(new List<SignatureFile>());
            env.Stub(e => e.TypeLibraries).Return(new List<ITypeLibraryElement>());
            env.Stub(e => e.CharacteristicsLibraries).Return(new List<ITypeLibraryElement>());
            sc = new ServiceContainer();
            sc.AddService<IConfigurationService>(cfgSvc);
            sc.AddService<DecompilerHost>(new FakeDecompilerHost());
            sc.AddService<DecompilerEventListener>(new FakeDecompilerEventListener());
            sc.AddService<IFileSystemService>(new FileSystemServiceImpl());
            sc.AddService<ITypeLibraryLoaderService>(tlSvc);
            var ldr = new Loader(sc);
            var arch = new X86ArchitectureReal();

            program = ldr.AssembleExecutable(
                FileUnitTester.MapTestPath(sourceFilename),
                new X86TextAssembler(sc, arch) { Platform = new MsdosPlatform(sc, arch) },
                addrBase);
            return RewriteProgram();
		}
Ejemplo n.º 15
0
        public void HSC_ARM32_Calls()
        {
            var mem = CreateMemoryArea(Address.Ptr32(0x1000),
                0xE1A0F00E,     // mov r15,r14 (return)
                0xEBFFFFFD,
                0xEBFFFFFC);
            this.segment = new ImageSegment(".text", mem, AccessMode.ReadExecute);
            var imageMap = new SegmentMap(
                mem.BaseAddress,
                segment);
            program = new Program
            {
                SegmentMap = imageMap,
                Architecture = new Arm32ProcessorArchitecture(),
            };
            var host = mr.Stub<IRewriterHost>();
            mr.ReplayAll();

            var hsc = new HeuristicScanner(program, host, eventListener);
            var linAddrs = hsc.FindCallOpcodes(segment.MemoryArea, new Address[] {
                Address.Ptr32(0x1000),
            }).ToList();

            Assert.AreEqual(2, linAddrs.Count);
            Assert.IsTrue(linAddrs.Contains(Address.Ptr32(0x1004)));
            Assert.IsTrue(linAddrs.Contains(Address.Ptr32(0x1008)));
        }
Ejemplo n.º 16
0
 private void WriteString(Program program, uint uiAddr, string str)
 {
     var imgW = program.CreateImageWriter(Address.Ptr32(uiAddr));
     imgW.WriteString(str, Encoding.ASCII);
 }
Ejemplo n.º 17
0
        protected override void RunTest(Program program, TextWriter fut)
        {
            InitProgram(program);
            IImportResolver importResolver = null;
            var eventListener = new FakeDecompilerEventListener();
            DataFlowAnalysis dfa = new DataFlowAnalysis(
                program,
                importResolver,
                eventListener);
            dfa.UntangleProcedures();

            foreach (Procedure proc in program.Procedures.Values)
            {
                SsaTransform sst = new SsaTransform(
                    dfa.ProgramDataFlow,
                    proc,
                    importResolver,
                    proc.CreateBlockDominatorGraph(),
                    new HashSet<RegisterStorage>());
                SsaState ssa = sst.SsaState;

                var icrw = new IndirectCallRewriter(
                    program,
                    ssa,
                    eventListener);
                icrw.Rewrite();

                ssa.Write(fut);
                proc.Write(false, fut);
                fut.WriteLine();
            }
        }
Ejemplo n.º 18
0
 private void InitProgram(Program program)
 {
     SetCSignatures(program);
     SetTypedefs(program);
 }
Ejemplo n.º 19
0
 private void SetTypedefs(Program program)
 {
     foreach (var de in types)
     {
         program.EnvironmentMetadata.Types.Add(de);
     }
 }
Ejemplo n.º 20
0
		private void RunTest(string sourceFile, string outputFile)
		{
            Program program;
            using (var rdr = new StreamReader(FileUnitTester.MapTestPath(sourceFile)))
            {
                program = asm.Assemble(Address.SegPtr(0x0C00, 0), rdr);
            }
			using (FileUnitTester fut = new FileUnitTester(outputFile))
			{
				Dumper dump = new Dumper(asm.Architecture);
                var mem = program.SegmentMap.Segments.Values.First().MemoryArea;
                var formatter = new TextFormatter(fut.TextWriter);
				dump.DumpData(program.SegmentMap, mem.BaseAddress, mem.Bytes.Length, formatter);
				fut.TextWriter.WriteLine();
				dump.ShowAddresses = true;
				dump.ShowCodeBytes = true;
				dump.DumpAssembler(program.SegmentMap, mem.BaseAddress, mem.EndAddress, formatter);

				fut.AssertFilesEqual();
			}	
		}
Ejemplo n.º 21
0
 public GlobalCallRewriter(Program program, ProgramDataFlow mpprocflow, DecompilerEventListener eventListener) : base(program)
 {
     this.mpprocflow = mpprocflow;
 }
Ejemplo n.º 22
0
 protected Program RewriteX86_32Fragment(string asmFragment, Address addrBase)
 {
     sc = new ServiceContainer();
     sc.AddService<DecompilerEventListener>(new FakeDecompilerEventListener());
     var asm = new X86TextAssembler(sc, new X86ArchitectureFlat32());
     program = asm.AssembleFragment(addrBase, asmFragment);
     program.Platform = new DefaultPlatform(null, program.Architecture);
     program.EntryPoints.Add(
         addrBase,
         new ImageSymbol(addrBase));
     return RewriteProgram();
 }
Ejemplo n.º 23
0
 public void SudSaveProgramOptions()
 {
     var program = new Program();
     program.User.Heuristics.Add("shingle");
     
     var pSaver = new ProjectSaver(sc);
     var file = pSaver.VisitProgram("foo.proj", program);
     var ip = (DecompilerInput_v3)file;
     Assert.IsTrue(ip.User.Heuristics.Any(h => h.Name == "shingle"));
 }
Ejemplo n.º 24
0
 public void MemOperandTest()
 {
     Program prog = new Program();
     var lr = asm.AssembleFragment(
         Address.SegPtr(0xC00, 0),
         @"	.i86
     mov word ptr [bx+2],3
     mov byte ptr [bx+4],3
     mov dword ptr [bx+6],3
     add word ptr [bx+2],3
     add byte ptr [bx+4],3
     ");
     Assert.IsTrue(Compare(lr.Image.Bytes, new byte []
     {
         0xC7, 0x47, 0x02, 0x03, 0x00,
         0xC6, 0x47, 0x04, 0x03,
         0x66, 0xC7, 0x47, 0x06, 0x03, 0x00, 0x00, 0x00,
         0x83, 0x47, 0x02, 0x03,
         0x80, 0x47, 0x04, 0x03
     }));
 }
Ejemplo n.º 25
0
 private void Given_VaScanner(IProcessorArchitecture arch)
 {
     var platform = new DefaultPlatform(null, arch);
     var segmentMap = CreateSegmentMap(0, 128);
     this.program = new Program(segmentMap, arch, platform);
     this.vafs = CreateVaScanner(program);
 }
Ejemplo n.º 26
0
 private VarargsFormatScanner CreateVaScanner(Program program)
 {
     this.state = program.Architecture.CreateProcessorState();
     var frame = program.Architecture.CreateFrame();
     return new VarargsFormatScanner(program, frame, state, sc);
 }
Ejemplo n.º 27
0
		protected Procedure DoRewrite(string code)
		{
            program = asm.AssembleFragment(baseAddress, code);
			DoRewriteCore();
			return program.Procedures.Values[0];
		}
Ejemplo n.º 28
0
        public void SudSavePlatformOptions_Dictionary()
        {
            var platform = new TestPlatform
            {
                Test_Options = new Dictionary<string, object>
                {
                    {
                        "Names", new Dictionary<string, object> {
                            {  "Adam", "30" },
                            {  "Bob", "10" },
                            {  "Cecil", "120" }
                        }
                    },
                    { "Name2", "Sue" },
                }
            };
            var program = new Program
            {
                Platform = platform
            };
            var sw = new StringWriter();
            When_SaveToTextWriter(program, sw);
            var sExp =
@"<?xml version=""1.0"" encoding=""utf-16""?>
<project xmlns=""http://schemata.jklnet.org/Reko/v3"">
  <input>
    <user>
      <platform>
        <dict key=""Names"">
          <item key=""Adam"">30</item>
          <item key=""Bob"">10</item>
          <item key=""Cecil"">120</item>
        </dict>
        <item key=""Name2"">Sue</item>
      </platform>
    </user>
  </input>
</project>";
            if (sw.ToString() != sExp)
                Debug.Print("{0}", sw.ToString());
            Assert.AreEqual(sExp, sw.ToString());
        }
Ejemplo n.º 29
0
 private void When_SaveToTextWriter(Program program, TextWriter sw)
 {
     var saver = new ProjectSaver(sc);
     var sProj = new Project_v3
     {
         Inputs = { saver.VisitProgram("foo.exe", program) }
     };
     var writer = new FilteringXmlWriter(sw);
     writer.Formatting = System.Xml.Formatting.Indented;
     XmlSerializer ser = SerializedLibrary.CreateSerializer_v3(typeof(Project_v3));
     ser.Serialize(writer, sProj);
 }
Ejemplo n.º 30
0
 private void SetCSignatures(Program program)
 {
     foreach (var addr in program.Procedures.Keys)
     {
         program.User.Procedures.Add(
             addr,
             new Procedure_v1
             {
                 CSignature = this.CSignature
             });
     }
 }
Ejemplo n.º 31
0
 private void AssembleFragment(string asmSrc)
 {
     lr = asm.AssembleFragment(Address.SegPtr(0x0C00, 0), asmSrc);
     mem = lr.SegmentMap.Segments.Values.First().MemoryArea;
 }
Ejemplo n.º 32
0
		public void AsCarryInstructions()
		{
            Program program;
            using (var rdr = new StreamReader(FileUnitTester.MapTestPath("Fragments/carryinsts.asm")))
            {
			    program = asm.Assemble(Address.SegPtr(0xBAC, 0), rdr);
            }
			using (FileUnitTester fut = new FileUnitTester("Intel/AsCarryInstructions.txt"))
			{
				Dumper dump = new Dumper(arch);
                var mem = program.SegmentMap.Segments.Values.First().MemoryArea;
				dump.DumpData(program.SegmentMap, mem.BaseAddress, mem.Length, new TextFormatter(fut.TextWriter));
				fut.AssertFilesEqual();
			}
		}