Exemplo n.º 1
0
        private void RunTest(AssemblerFragment fragment, string sExp)
        {
            Address addrBase=  Address.SegPtr(0xC00, 0);
            X86Assembler asm = new X86Assembler(new IntelArchitecture(ProcessorMode.Real), addrBase, new List<EntryPoint>());
            fragment.Build(asm);
            Program lr = asm.GetImage();

            X86Disassembler dasm = new X86Disassembler(
                ProcessorMode.Real,
                lr.Image.CreateLeReader(lr.Image.BaseAddress),
                PrimitiveType.Word16,
                PrimitiveType.Word16,
                false);
            StringBuilder sb = new StringBuilder();
            try
            {
                foreach (var instr in dasm)
                {
                    sb.AppendFormat("{0}\t{1}", instr.Address, instr);
                    sb.AppendLine();
                }
                Assert.AreEqual(sExp, sb.ToString());
            }
            catch
            {
                Console.WriteLine(sb.ToString());
                throw;
            }
        }
Exemplo n.º 2
0
 private void RunTest(AssemblerFragment fragment, string sExp)
 {
     Address addrBase=  Address.SegPtr(0xC00, 0);
     X86Assembler asm = new X86Assembler(sc, new MsdosPlatform(sc, new X86ArchitectureReal()), addrBase, new List<ImageSymbol>());
     fragment.Build(asm);
     Program lr = asm.GetImage();
     var mem = lr.SegmentMap.Segments.Values.First().MemoryArea;
     X86Disassembler dasm = new X86Disassembler(
         ProcessorMode.Real,
         mem.CreateLeReader(mem.BaseAddress),
         PrimitiveType.Word16,
         PrimitiveType.Word16,
         false);
     StringBuilder sb = new StringBuilder();
     try
     {
         foreach (var instr in dasm)
         {
             sb.AppendFormat("{0}\t{1}", instr.Address, instr);
             sb.AppendLine();
         }
         Assert.AreEqual(sExp, sb.ToString());
     }
     catch
     {
         Console.WriteLine(sb.ToString());
         throw;
     }
 }
Exemplo n.º 3
0
        public Program Assemble(Address addr, TextReader rdr)
        {
            addrBase = addr;
            lexer = new Lexer(rdr);

            asm = new X86Assembler(services, new MsdosPlatform(services, arch), addrBase, entryPoints);
            asm.Platform = Platform;

            // Assemblers are strongly line-oriented.

            while (lexer.PeekToken() != Token.EOFile)
            {
                try {
                    ProcessLine();
                }
                catch (Exception ex)
                {
                    Debug.Print("Error on line {0}: {1}", lexer.LineNumber, ex.Message);
                    throw;
                }
            }

            asm.ReportUnresolvedSymbols();
            addrStart = addrBase;
            return asm.GetImage();
        }
Exemplo n.º 4
0
        private void BuildTest(Address addrBase, IPlatform platform , Action<X86Assembler> asmProg)
        {
            var sc = new ServiceContainer();
            sc.AddService<DecompilerEventListener>(new FakeDecompilerEventListener());
            sc.AddService<DecompilerHost>(new FakeDecompilerHost());
            sc.AddService<IFileSystemService>(new FileSystemServiceImpl());
            var entryPoints = new List<EntryPoint>();
            var asm = new X86Assembler(sc, platform, 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),
                sc);
            scanner.EnqueueEntryPoint(new EntryPoint(addrBase, arch.CreateProcessorState()));
            scanner.ScanImage();
        }
Exemplo n.º 5
0
        private void Given_Code(Action<X86Assembler> coder)
        {
            var asm = new X86Assembler(sc, new DefaultPlatform(sc, arch), Address.Ptr32(0x00100000), new List<ImageSymbol>());
            coder(asm);
            var program = asm.GetImage();
            this.segmentMap = program.SegmentMap;

            Given_Platform();

            var win32 = new Win32Emulator(program.SegmentMap, platform, importReferences);
            
            emu = new X86Emulator(arch, program.SegmentMap, win32);
            emu.InstructionPointer = program.ImageMap.BaseAddress;
            emu.WriteRegister(Registers.esp, (uint)program.ImageMap.BaseAddress.ToLinear() + 0x0FFC);
            emu.ExceptionRaised += delegate { throw new Exception(); };
        }
Exemplo n.º 6
0
        private void Given_Code(Action<X86Assembler> coder)
        {
            var asm = new X86Assembler(arch, Address.Ptr32(0x00100000), new List<EntryPoint>());
            coder(asm);
            var program = asm.GetImage();
            this.image = program.Image;

            Given_Platform();

            var win32 = new Win32Emulator(image, platform, importReferences);
            
            emu = new X86Emulator(arch, program.Image, win32);
            emu.InstructionPointer = program.Image.BaseAddress;
            emu.WriteRegister(Registers.esp, (uint)program.Image.BaseAddress.ToLinear() + 0x0FFC);
            emu.ExceptionRaised += delegate { throw new Exception(); };
        }
Exemplo n.º 7
0
        private void BuildTest(Address addrBase, IPlatform platform , Action<X86Assembler> asmProg)
        {
            var sc = new ServiceContainer();
            var eventListener = new FakeDecompilerEventListener();
            sc.AddService<DecompilerEventListener>(eventListener);
            sc.AddService<DecompilerHost>(new FakeDecompilerHost());
            sc.AddService<IFileSystemService>(new FileSystemServiceImpl());
            var entryPoints = new List<ImageSymbol>();
            var asm = new X86Assembler(sc, platform, addrBase, entryPoints);
            asmProg(asm);

            program = asm.GetImage();
            var project = new Project { Programs = { program } };
            scanner = new Scanner(
                program,
                new ImportResolver(project, program, eventListener),
                sc);
            scanner.EnqueueImageSymbol(new ImageSymbol(addrBase), true);
            scanner.ScanImage();
        }
Exemplo n.º 8
0
        public void Scanner_CallGraphTree()
        {
            Program prog = new Program();
            var addr = Address.SegPtr(0xC00, 0);
            var m = new X86Assembler(sc, new DefaultPlatform(sc, new X86ArchitectureReal()), 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), sc);
            EntryPoint ep = new EntryPoint(addr, prog.Architecture.CreateProcessorState());
            scan.EnqueueEntryPoint(ep);
            scan.ScanImage();

            Assert.AreEqual(4, prog.Procedures.Count);
        }
Exemplo n.º 9
0
 private void BuildX86RealTest(Action<X86Assembler> test)
 {
     var addr = Address.SegPtr(0x0C00, 0);
     var m = new X86Assembler(sc, new FakePlatform(null, new X86ArchitectureReal()), addr, new List<EntryPoint>());
     test(m);
     var lr = m.GetImage();
     program = new Program(
         lr.Image,
         lr.ImageMap,
         lr.Architecture,
         lr.Platform);
     scan = CreateScanner(program);
     EntryPoint ep = new EntryPoint(addr, program.Architecture.CreateProcessorState());
     scan.EnqueueEntryPoint(ep);
 }
Exemplo n.º 10
0
 private void BuildTest(IntelArchitecture arch, Address addr, IPlatform platform, Action<X86Assembler> m)
 {
     proc = new Procedure("test", arch.CreateFrame());
     block = proc.AddBlock("testblock");
     this.state = arch.CreateProcessorState();
     var asm = new X86Assembler(sc, new DefaultPlatform(sc, arch), addr, new List<ImageSymbol>());
     scanner = mr.StrictMock<IScanner>();
     scanner.Stub(s => s.Services).Return(sc);
     m(asm);
     lr = asm.GetImage();
     host = new RewriterHost(
         asm.ImportReferences,
         new Dictionary<string, FunctionType>
         {
             {
                 "GetDC",
                 new FunctionType(
                     new Identifier("", new Pointer(VoidType.Instance, 4), new RegisterStorage("eax", 0, 0, PrimitiveType.Word32)),
                     new [] {
                         new Identifier("arg",
                             new TypeReference(
                                 "HWND",
                                 new Pointer(VoidType.Instance, 4)),
                             new StackArgumentStorage(4, new TypeReference(
                                 "HWND",
                                 new Pointer(VoidType.Instance, 4))))
                     })
                 {
                     StackDelta = 4,
                 }
             }
        },
        new Dictionary<string, DataType>());
     var rw = arch.CreateRewriter(
         lr.SegmentMap.Segments.Values.First().MemoryArea.CreateLeReader(addr), 
         this.state, 
         proc.Frame,
         host);
     this.program = new Program
     {
         Architecture = arch,
         SegmentMap = lr.SegmentMap,
         ImageMap = lr.ImageMap,
         Platform = platform,
     };
     using (mr.Record())
     {
         scanner.Stub(x => x.FindContainingBlock(Arg<Address>.Is.Anything)).Return(block);
         scanner.Stub(x => x.GetTrace(null, null, null)).IgnoreArguments().Return(rw);
         scanner.Stub(x => x.Services).Return(sc);
     }
     wi = new BlockWorkitem(scanner, program, state, addr);
 }
Exemplo n.º 11
0
 private X86Rewriter CreateRewriter32(X86Assembler m)
 {
     state = new X86State(arch32);
     return new X86Rewriter(arch32, host, state, m.GetImage().Image.CreateLeReader(0), new Frame(arch32.WordWidth));
 }
Exemplo n.º 12
0
        public Program Assemble(Address addr, TextReader rdr)
        {
            addrBase = addr;
            lexer = new Lexer(rdr);

            asm = new X86Assembler(arch, addrBase, entryPoints);
            asm.Platform = Platform;

            // Assemblers are strongly line-oriented.

            while (lexer.PeekToken() != Token.EOFile)
            {
                ProcessLine();
            }

            asm.ReportUnresolvedSymbols();
            addrStart = addrBase;
            return asm.GetImage();
        }
Exemplo n.º 13
0
        private void BuildTest(IntelArchitecture arch, Address addr, IPlatform platform, Action<X86Assembler> m)
        {
            this.arch = new IntelArchitecture(ProcessorMode.Protected32);
            proc = new Procedure("test", arch.CreateFrame());
            block = proc.AddBlock("testblock");
            this.state = arch.CreateProcessorState();
            var asm = new X86Assembler(sc, new DefaultPlatform(sc, arch), addr, new List<EntryPoint>());
            scanner = mr.StrictMock<IScanner>();
            m(asm);
            lr = asm.GetImage();
            host = new RewriterHost(asm.ImportReferences,
                new Dictionary<string, ProcedureSignature>
                {
                {
                    "GetDC", 
                    new ProcedureSignature(
                        new Identifier("", new Pointer(VoidType.Instance, 4), new RegisterStorage("eax", 0, 0, PrimitiveType.Word32)),
                        new Identifier("arg", 
                            new TypeReference(
                                "HWND",
                                new Pointer(VoidType.Instance, 4)),
                            new StackArgumentStorage(0, new TypeReference(
                                "HWND",
                                new Pointer(VoidType.Instance, 4)))))
                                {
                                    StackDelta = 4,
}
                }
              });
            var rw = arch.CreateRewriter(lr.Image.CreateLeReader(addr), this.state, proc.Frame, host);
            var prog = new Program
            {
                Architecture = arch,
                Image = lr.Image,
                ImageMap = lr.ImageMap,
                Platform = platform,
            };
            using (mr.Record())
            {
                scanner.Stub(x => x.FindContainingBlock(Arg<Address>.Is.Anything)).Return(block);
                scanner.Stub(x => x.GetTrace(null, null, null)).IgnoreArguments().Return(rw);
            }
            wi = new BlockWorkitem(scanner, prog, state, addr);
        }
Exemplo n.º 14
0
        public void Scanner_CallGraphTree()
        {
            program = new Program();
            var addr = Address.SegPtr(0xC00, 0);
            var m = new X86Assembler(sc, new DefaultPlatform(sc, new X86ArchitectureReal()), addr, new List<ImageSymbol>());
            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");

            program = m.GetImage();
            program.Platform = new FakePlatform(null, arch);
            Given_Project();

            var scan = new Scanner(
                program, 
                new ImportResolver(project, program, eventListener),
                sc);
            var sym = new ImageSymbol(addr);
            scan.EnqueueImageSymbol(sym, true);
            scan.ScanImage();

            Assert.AreEqual(4, program.Procedures.Count);
        }
Exemplo n.º 15
0
 private void BuildX86RealTest(Action<X86Assembler> test)
 {
     var addr = Address.SegPtr(0x0C00, 0);
     var m = new X86Assembler(sc, new FakePlatform(null, new X86ArchitectureReal()), addr, new List<ImageSymbol>());
     test(m);
     this.program = m.GetImage();
     this.scan = this.CreateScanner(this.program);
     var sym = new ImageSymbol(addr);
     scan.EnqueueImageSymbol(sym, true);
 }
Exemplo n.º 16
0
 private X86Rewriter CreateRewriter32(X86Assembler m)
 {
     state = new X86State(arch32);
     return new X86Rewriter(
         arch32, 
         host, 
         state, 
         m.GetImage().SegmentMap.Segments.Values.First().MemoryArea.CreateLeReader(0),
         new Frame(arch32.WordWidth));
 }