Esempio n. 1
0
        public void Impres_ProcedureByName()
        {
            var proj = new Project
            {
                MetadataFiles = 
                {
                    new MetadataFile
                    {
                         ModuleName = "foo",
                         TypeLibrary = new TypeLibrary 
                         {
                              ServicesByName =
                              {
                                  {
                                      "bar@4", 
                                      new SystemService 
                                      { 
                                          Name = "bar", Signature= new ProcedureSignature()
                                      }
                                  }
                              }
                         }
                    }
                }
            };

            var impres = new ImportResolver(proj);
            var ep = impres.ResolveProcedure("foo", "bar@4", new DefaultPlatform(services, arch));
            Assert.AreEqual("bar", ep.Name);
        }
Esempio n. 2
0
		protected Program RewriteFile(string relativePath, Address addrBase)
		{
            var services = new ServiceContainer();
            var config = new FakeDecompilerConfiguration();
            services.AddService<IConfigurationService>(config);
            ILoader ldr = new Loader(services);
            var program = ldr.AssembleExecutable(
                FileUnitTester.MapTestPath(relativePath),
                new X86TextAssembler(new IntelArchitecture(ProcessorMode.Real)),
                addrBase);
            program.Platform = new DefaultPlatform(services, program.Architecture);
            var ep = new EntryPoint(program.Image.BaseAddress, program.Architecture.CreateProcessorState());
            var project = new Project { Programs = { program } };
			var scan = new Scanner(
                program,
                new Dictionary<Address, ProcedureSignature>(),
                new ImportResolver(project),
                new FakeDecompilerEventListener());
			scan.EnqueueEntryPoint(ep);
			scan.ScanImage();

			var dfa = new DataFlowAnalysis(program, new FakeDecompilerEventListener());
			dfa.AnalyzeProgram();
            return program;
		}
		public void Setup()
		{
            mr = new MockRepository();
            form = new MainForm();
            sc = new ServiceContainer();
            loader = mr.StrictMock<ILoader>();
            dec = mr.StrictMock<IDecompiler>();
            sc = new ServiceContainer();
            uiSvc = new FakeShellUiService();
            host = mr.StrictMock<DecompilerHost>();
            memSvc = mr.StrictMock<ILowLevelViewService>();
            var image = new LoadedImage(Address.Ptr32(0x10000), new byte[1000]);
            var imageMap = image.CreateImageMap();
            var arch = mr.StrictMock<IProcessorArchitecture>();
            arch.Stub(a => a.CreateRegisterBitset()).Return(new BitSet(32));
            arch.Replay();
            var platform = mr.StrictMock<Platform>(null, arch);
            arch.BackToRecord();
            program = new Program(image, imageMap, arch, platform);
            project = new Project { Programs = { program } };

            browserSvc = mr.StrictMock<IProjectBrowserService>();

            sc.AddService<IDecompilerUIService>(uiSvc);
            sc.AddService(typeof(IDecompilerShellUiService), uiSvc);
            sc.AddService(typeof(IDecompilerService), new DecompilerService());
            sc.AddService(typeof(IWorkerDialogService), new FakeWorkerDialogService());
            sc.AddService(typeof(DecompilerEventListener), new FakeDecompilerEventListener());
            sc.AddService(typeof(IProjectBrowserService), browserSvc);
            sc.AddService(typeof(ILowLevelViewService), memSvc);
            sc.AddService<ILoader>(loader);

            i = new TestInitialPageInteractor(sc, dec);

		}
Esempio n. 4
0
 private void btnLoad_Click(object sender, EventArgs e)
 {
     var image = new LoadedImage(Address.Ptr32(0x12312300),new byte[0x1000]);
     var imageMap = image.CreateImageMap();
     var arch = new Decompiler.Arch.X86.X86ArchitectureFlat32();
     var program = new Core.Program(image, imageMap, arch, new DefaultPlatform(null, arch));
     var project = new Project { Programs = { program } };
     pbs.Load(project);
 }
		private Program AssembleFile(string sourceFile, Address addr)
		{
            var ldr = new Loader(new ServiceContainer());
            var arch = new X86ArchitectureReal();
            Program program = ldr.AssembleExecutable(
                 FileUnitTester.MapTestPath(sourceFile),
                 new X86TextAssembler(arch),
                addr);
            var project = new Project { Programs = { program } };
			var scan = new Scanner(program, new Dictionary<Address, ProcedureSignature>(), new ImportResolver(project), null);
			foreach (EntryPoint ep in program.EntryPoints)
			{
				scan.EnqueueEntryPoint(ep);
			}
			scan.ScanImage();
			return program;
		}
Esempio n. 6
0
        protected void RunHexTest(string hexFile, string outputFile)
        {
            var svc = new ServiceContainer();
            var cfg = new FakeDecompilerConfiguration();
            svc.AddService<IConfigurationService>(cfg);
            ILoader ldr = new Loader(svc);
            var imgLoader = new DchexLoader(FileUnitTester.MapTestPath( hexFile), svc, null);
            var img = imgLoader.Load(null);
            var program = new Program(img.Image, img.Image.CreateImageMap(), img.Architecture, img.Platform);
            var project = new Project { Programs = { program } };
            var ep = new EntryPoint(program.Image.BaseAddress, program.Architecture.CreateProcessorState());
            var scan = new Scanner(program, new Dictionary<Address, ProcedureSignature>(), new ImportResolver(project), new FakeDecompilerEventListener());
            scan.EnqueueEntryPoint(ep);
            scan.ScanImage();

            var dfa = new DataFlowAnalysis(program, new FakeDecompilerEventListener());
            dfa.AnalyzeProgram();
            RunTest(program, outputFile);
        }
Esempio n. 7
0
        private void BuildTest(Address addrBase, Platform platform , Action<X86Assembler> asmProg)
        {
            var entryPoints = new List<EntryPoint>();
            var asm = new X86Assembler(arch, addrBase, entryPoints);
            asmProg(asm);

            var lr = asm.GetImage();
            program = new Program(
                lr.Image,
                lr.ImageMap,
                arch,
                platform);
            var project = new Project { Programs = { program } };
            scanner = new Scanner(
                program,
                new Dictionary<Address, ProcedureSignature>(),
                new ImportResolver(project),
                new FakeDecompilerEventListener());
            scanner.EnqueueEntryPoint(new EntryPoint(addrBase, arch.CreateProcessorState()));
            scanner.ScanImage();
        }
Esempio n. 8
0
 public void Load(Project project)
 {
     tree.ContextMenu = Services.RequireService<IDecompilerShellUiService>().GetContextMenu(MenuIds.CtxBrowser);
     tree.Nodes.Clear();
     this.mpitemToDesigner = new Dictionary<object, TreeNodeDesigner>();
     if (project == null)
     {
         tree.ShowRootLines = false;
         tree.ShowNodeToolTips = false;
         tree.Nodes.Clear();
         tree.Nodes.Add(tree.CreateNode("(No project loaded)"));
     }
     else 
     {
         AddComponents(project.Programs);
         AddComponents(project.MetadataFiles);
         project.MetadataFiles.CollectionChanged += TypeLibraries_CollectionChanged;
         tree.ShowNodeToolTips = true;
         tree.ShowRootLines = true;
     }
     this.project = project;
 }
Esempio n. 9
0
		private void DecompileAssembler(string asmLabel, Address loadAddress)
		{
            var cfg = new DecompilerConfiguration();
            var asm = cfg.GetAssembler(asmLabel);
            var prog = asm.AssembleFragment(loadAddress, txtAssembler.Text + Environment.NewLine);
            var sc = new ServiceContainer();
            var loader = new Loader(sc);
            DecompilerDriver decomp = new DecompilerDriver(loader, host, sc);
            var proj = new Project {
                Programs = {
                    prog
                }
            };
			decomp.Project = proj;
            decomp.ScanPrograms();
            decomp.AnalyzeDataFlow();
            decomp.ReconstructTypes();
            decomp.StructureProgram();
            decomp.WriteDecompilerProducts();

			plcOutput.Text = host.DisassemblyWriter.ToString();
			plcDecompiled.Text = host.DecompiledCodeWriter.ToString();

		}
Esempio n. 10
0
        public void Cvp_SetProcedure()
        {
            codeViewer.CreateControl();
            var m = new ProcedureBuilder();
            m.Return();

            using (mr.Record())
            {
                var project = new Project { Programs = { new Program() } };
                decompiler.Stub(d => d.Project).Return(project);
                uiPreferencesSvc.SourceCodeFont = font;
            }

            codeViewer.DisplayProcedure(m.Procedure);

            string sExp =
                "void ProcedureBuilder()" + nl +
                "{" + nl +
                "ProcedureBuilder_entry:" + nl +
                "l1:" + nl +
                "    'return'" + nl +
                "ProcedureBuilder_exit:" + nl +
                "}" + nl;
            Assert.AreEqual(sExp, Flatten(codeViewer.TextView.Model));
        }
Esempio n. 11
0
        public void Scanner_CallGraphTree()
        {
            Program prog = new Program();
            var addr = Address.SegPtr(0xC00, 0);
            var m = new X86Assembler(new IntelArchitecture(ProcessorMode.Real), addr, new List<EntryPoint>());
            m.i86();

            m.Proc("main");
            m.Call("baz");
            m.Ret();
            m.Endp("main");

            m.Proc("foo");
            m.Ret();
            m.Endp("foo");

            m.Proc("bar");
            m.Ret();
            m.Endp("bar");

            m.Proc("baz");
            m.Call("foo");
            m.Call("bar");
            m.Jmp("foo");
            m.Endp("baz");

            var lr = m.GetImage();
            prog.Image = lr.Image;
            prog.ImageMap = lr.ImageMap;
            prog.Architecture = lr.Architecture;
            prog.Platform = new FakePlatform(null, arch);
            var proj = new Project { Programs = { prog } };
            var scan = new Scanner(prog, new Dictionary<Address, ProcedureSignature>(), new ImportResolver(proj), new FakeDecompilerEventListener());
            EntryPoint ep = new EntryPoint(addr, prog.Architecture.CreateProcessorState());
            scan.EnqueueEntryPoint(ep);
            scan.ScanImage();

            Assert.AreEqual(4, prog.Procedures.Count);
        }
Esempio n. 12
0
 private void RunTest(Address addrBase)
 {
     var project =     new Project { Programs = { program } };
     scanner = new Scanner(
         program,
         new Dictionary<Address, ProcedureSignature>(),
         new ImportResolver(project),
         listener);
     scanner.EnqueueEntryPoint(new EntryPoint(addrBase, arch.CreateProcessorState()));
     scanner.ScanImage();
 }
Esempio n. 13
0
		private void RunTest(string sourceFile, string outputFile)
		{
			Program program;
            var asm = new X86TextAssembler(new X86ArchitectureFlat32());
            using (StreamReader rdr = new StreamReader(FileUnitTester.MapTestPath(sourceFile)))
            {
                program = asm.Assemble(Address.Ptr32(0x10000000), rdr);
                program.Platform = win32;
            }
            foreach (var item in asm.ImportReferences)
            {
                program.ImportReferences.Add(item.Key, item.Value);
            }
            var project = new Project { Programs = { program } };
            Scanner scan = new Scanner(
                program,
                new Dictionary<Address, ProcedureSignature>(),
                new ImportResolver(project),
                new FakeDecompilerEventListener());
            foreach (var ep in asm.EntryPoints)
            {
                scan.EnqueueEntryPoint(ep);
            }
            scan.ScanImage();

			using (FileUnitTester fut = new FileUnitTester(outputFile))
			{
				foreach (Procedure proc in program.Procedures.Values)
				{
					proc.Write(true, fut.TextWriter);
					fut.TextWriter.WriteLine();
				}
				fut.AssertFilesEqual();
			}
		}
Esempio n. 14
0
		private void DecompileC()
		{
			string tmpName = Guid.NewGuid().ToString();
			string tmpDir = Server.MapPath("tmp");
			string cFile = Path.Combine(tmpDir, tmpName + ".c");
			string asmFile = Path.Combine(tmpDir, tmpName + ".asm");
			try
			{
				CopyCSourceToTempFile(txtAssembler.Text, cFile);
				if (CompileCFile(tmpDir, cFile))
				{
                    var sc = new ServiceContainer();
                    var ldr = new Loader(sc);
                    var cfg = new DecompilerConfiguration();
                    var asm = cfg.GetAssembler("x86-masm");
                    var program = asm.AssembleFragment(Address.Ptr32(0x10000000), txtAssembler.Text + Environment.NewLine);
					DecompilerDriver decomp = new DecompilerDriver(ldr, host, sc);
					var project = new Project
                    {
                        Programs = { program }
                    };
                    decomp.Project = project;
                    decomp.ScanPrograms();
                    decomp.AnalyzeDataFlow();
                    decomp.ReconstructTypes();
                    decomp.StructureProgram();
                    decomp.WriteDecompilerProducts();

					plcOutput.Text = host.DisassemblyWriter.ToString();
					plcDecompiled.Text = host.DecompiledCodeWriter.ToString();
				}
			} 
			finally
			{
				if (File.Exists(asmFile))
					File.Delete(asmFile);
			}
		}
Esempio n. 15
0
        private Program RewriteProgram()
        {
            var project = new Project { Programs = { program } };
            var scan = new Scanner(
                program,
                new Dictionary<Address, ProcedureSignature>(),
                new ImportResolver(project),
                new FakeDecompilerEventListener());
            foreach (EntryPoint ep in program.EntryPoints)
            {
                scan.EnqueueEntryPoint(ep);
            }
            scan.ScanImage();

            DecompilerEventListener eventListener = new FakeDecompilerEventListener();
            DataFlowAnalysis da = new DataFlowAnalysis(program, eventListener);
            da.AnalyzeProgram();

            return program;
        }
Esempio n. 16
0
 public ImportResolver(Project project)
 {
     if (project == null)
         throw new ArgumentNullException("project");
     this.project = project;
 }
Esempio n. 17
0
 public void SudSaveProject()
 {
     Project project = new Project
     {
         Programs = 
         {
             new Program
             {
                 Image = new LoadedImage(Address.SegPtr(0x1000, 0), new byte[100]),
                 DisassemblyFilename = "foo.asm",
                 IntermediateFilename = "foo.cod",
                 UserProcedures = new SortedList<Address,Procedure_v1> 
                 {
                     { 
                         Address.SegPtr(0x1000, 0x10), 
                         new Procedure_v1
                         {
                             Name = "foo",
                             Signature = new SerializedSignature
                             {
                                 ReturnValue = new Argument_v1 { Kind = new Register_v1("eax") },
                                 Arguments = new Argument_v1[]
                                 {
                                     new Argument_v1
                                     {
                                         Kind = new StackVariable_v1(),
                                         Type = new PrimitiveType_v1(Domain.SignedInt, 4)
                                     },
                                     new Argument_v1
                                     {
                                         Kind = new StackVariable_v1(),
                                         Type = new PrimitiveType_v1(Domain.SignedInt, 4)
                                     }
                                 }
                             }
                         }
                     }
                 },
                 UserGlobalData =
                 {
                     { 
                       Address.SegPtr(0x2000, 0) ,
                       new GlobalDataItem_v2 {
                            Address = Address.SegPtr(0x2000, 0).ToString(),
                            DataType = new StringType_v2 { 
                                Termination=StringType_v2.ZeroTermination, 
                                CharType = new PrimitiveType_v1 { Domain = Domain.Character, ByteSize = 1 }
                            }
                       }
                       }
                 }
             }
         }
     };
     using (FileUnitTester fut = new FileUnitTester("Core/SudSaveProject.txt"))
     {
         FilteringXmlWriter writer = new FilteringXmlWriter(fut.TextWriter);
         writer.Formatting = System.Xml.Formatting.Indented;
         XmlSerializer ser = SerializedLibrary.CreateSerializer_v2(typeof(Project_v2));
         Project_v2 ud = new ProjectSaver().Save(project);
         ser.Serialize(writer, ud);
         fut.AssertFilesEqual();
     }
 }
Esempio n. 18
0
 public void Sud_SaveMetadataReference()
 {
     var project = new Project
     {
         Programs =
         {
             new Program
             {
                 Filename = "c:\\test\\foo.exe",
             }
         },
         MetadataFiles =
         {
             new MetadataFile
             {
                 Filename = "c:\\test\\foo.def",
                 ModuleName = "foo.def",
             }
         }
     };
     var ps = new ProjectSaver();
     var sProject = ps.Save(project);
     Assert.AreEqual(1, project.MetadataFiles.Count);
     Assert.AreEqual("c:\\test\\foo.def", project.MetadataFiles[0].Filename);
     Assert.AreEqual("foo.def", project.MetadataFiles[0].ModuleName);
 }
Esempio n. 19
0
 public Project LoadProject(Project_v2 sp)
 {
     var typelibs = sp.Inputs.OfType<MetadataFile_v2>().Select(m => VisitMetadataFile(m));
     var programs = sp.Inputs.OfType<DecompilerInput_v2>().Select(s => VisitInputFile(s));
     var asm = sp.Inputs.OfType<AssemblerFile_v2>().Select(s => VisitAssemblerFile(s));
     var project = new Project();
     project.Programs.AddRange(programs);
     project.MetadataFiles.AddRange(typelibs);
     return project;
 }
Esempio n. 20
0
        public void Scanner_AddEntryPoint()
        {
            Given_Trace(new RtlTrace(0x12314) 
            {
                m => { m.Return(4, 0); }
            });
            Given_Program(Address.Ptr32(0x12314));
            var project = new Project { Programs = { program } };
            
            var sc = new Scanner(
                program,
                null,
                new ImportResolver(project),
                new FakeDecompilerEventListener());
            sc.EnqueueEntryPoint(
                new EntryPoint(
                    Address.Ptr32(0x12314),
                    arch.CreateProcessorState()));
            sc.ScanImage();

            Assert.AreEqual(1, program.Procedures.Count);
            Assert.AreEqual(0x12314, program.Procedures.Keys[0].Offset);
            Assert.IsTrue(program.CallGraph.EntryPoints.Contains(program.Procedures.Values[0]));
        }