Ejemplo 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(
                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;
            }
        }
Ejemplo n.º 2
0
        public static void Switch(X86Assembler m)
        {
            m.Proc("foo");
            m.Push(m.cs);
            m.Pop(m.ds);
            m.Mov(m.bl, m.MemB(Registers.si, 0));
            m.Cmp(m.bl, 0x02);
            m.Ja("default");

            m.Label("test");
            m.Xor(m.bh, m.bh);
            m.Add(m.bx, m.bx);
            m.Jmp(m.MemW(Registers.bx, "jmptable"));

            m.Label("jmptable");
            m.Dw("one");
            m.Dw("two");
            m.Dw("three");

            m.Label("one");
            m.Mov(m.ax, 1);
            m.Ret();

            m.Label("two");
            m.Mov(m.ax, 2);
            m.Ret();

            m.Label("three");
            m.Mov(m.ax, 3);
            m.Ret();

            m.Label("default");
            m.Mov(m.ax, 0);
            m.Ret();
        }
Ejemplo n.º 3
0
 public void Fstsw_Setup()
 {
     arch = new IntelArchitecture(ProcessorMode.Protected32);
     asm = new X86Assembler(arch, Address.Ptr32(0x10000), new List<EntryPoint>());
     Procedure proc = new Procedure("test", arch.CreateFrame());
     orw = new OperandRewriter32(arch, proc.Frame, null);
     emitter = new ProcedureBuilder();
 }
Ejemplo n.º 4
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(); };
        }
Ejemplo n.º 5
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();
        }
Ejemplo n.º 6
0
        public override void Build(X86Assembler m)
        {
            m.i86();
            // A straight-forward factorial function + a driver program to ensure the return value 
            // is USE'd.

            m.Mov(cx, 0x100);
            m.Push(cx);
            m.Call("factorial");
            m.Add(Registers.sp, 2);
            m.Mov(m.WordPtr(0x0100), ax);
            m.Ret();

            m.Proc("factorial");
            m.Push(bp);
            m.Mov(bp, sp);

            m.Mov(ax, m.WordPtr(bp, 4));
            m.Dec(ax);
            m.Jz("base_case");

            m.Push(ax);
            m.Call("factorial");
            m.Inc(sp);
            m.Inc(sp);
            m.Mov(dx, m.WordPtr(bp, 4));
            m.Imul(dx);
            m.Jmp("done");

            m.Label("base_case");
            m.Mov(ax, 1);

            m.Label("done");
            m.Pop(bp);
            m.Ret();
            m.Endp("factorial");
        }
Ejemplo n.º 7
0
        public static void Switch32(X86Assembler m)
        {
            m.Proc("foo");
            m.Mov(m.eax, m.MemDw(Registers.esp, 4));
            m.Cmp(m.eax, 3);
            m.Ja("default");

            m.Xor(m.edx, m.edx);
            m.Mov(m.dl, m.MemB(Registers.eax, "bytes"));
            m.Jmp(m.MemDw(Registers.edx, 4, "jumps"));

            m.Label("bytes").Db(1, 0, 1, 2);
            m.Label("jumps").Dd("jump0", "jump1", "jump2");

            m.Label("jump0");
            m.Mov(m.eax, 0);
            m.Jmp("done");

            m.Label("jump1");
            m.Mov(m.eax, 1);
            m.Jmp("done");

            m.Label("jump2");
            m.Mov(m.eax, 2);
            m.Jmp("done");

            m.Label("default");
            m.Mov(m.eax, -1);

            m.Label("done");
            m.Mov(m.MemDw("dummy"), m.eax);
            m.Ret();

            m.Label("dummy").Dd(0);

        }
Ejemplo n.º 8
0
 public void Setup()
 {
     arch = new X86ArchitectureFlat32();
     asm = new X86Assembler(arch, loadAddress, new List<EntryPoint>());
 }
Ejemplo n.º 9
0
        private void BuildTest(IntelArchitecture arch, Address addr, Platform 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(arch, addr, new List<EntryPoint>());
            scanner = repository.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, 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 (repository.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);
        }
Ejemplo n.º 10
0
 private void BuildX86RealTest(Action<X86Assembler> test)
 {
     var addr = Address.SegPtr(0x0C00, 0);
     var m = new X86Assembler(new IntelArchitecture(ProcessorMode.Real), addr, new List<EntryPoint>());
     test(m);
     var lr = m.GetImage();
     program = new Program(
         lr.Image,
         lr.ImageMap,
         lr.Architecture,
         new FakePlatform(null, arch));
     scan = CreateScanner(program);
     EntryPoint ep = new EntryPoint(addr, program.Architecture.CreateProcessorState());
     scan.EnqueueEntryPoint(ep);
 }
Ejemplo 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);
        }
Ejemplo n.º 12
0
 private X86Assembler Create32bitAssembler()
 {
     arch = arch32;
     baseAddr = baseAddr32;
     var asm = new X86Assembler(arch, baseAddr32, new List<EntryPoint>());
     host = new RewriterHost(asm.ImportReferences);
     return asm;
 }
Ejemplo n.º 13
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));
 }
Ejemplo n.º 14
0
 public abstract void Build(X86Assembler m);