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; } }
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(); }
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(); }
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; } }
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); }
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(); }
public void Setup() { arch = new X86ArchitectureFlat32(); var services = new ServiceContainer(); services.AddService<IFileSystemService>(new FileSystemServiceImpl()); asm = new X86Assembler(services, new DefaultPlatform(services, arch), loadAddress, new List<ImageSymbol>()); }
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(); }
public void Fstsw_Setup() { arch = new X86ArchitectureFlat32(); asm = new X86Assembler(null, new DefaultPlatform(null, new X86ArchitectureFlat32()), Address.Ptr32(0x10000), new List<ImageSymbol>()); Procedure proc = new Procedure("test", arch.CreateFrame()); orw = new OperandRewriter32(arch, proc.Frame, null); emitter = new ProcedureBuilder(); }
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(); }; }
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(); }; }
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(); }
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"); }
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); }
private X86Rewriter CreateRewriter32(X86Assembler m) { state = new X86State(arch32); return new X86Rewriter(arch32, host, state, m.GetImage().Image.CreateLeReader(0), new Frame(arch32.WordWidth)); }
private X86Assembler Create32bitAssembler() { arch = arch32; baseAddr = baseAddr32; var asm = new X86Assembler(arch, baseAddr32, new List<EntryPoint>()); host = new RewriterHost(asm.ImportReferences); return asm; }
public abstract void Build(X86Assembler m);
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(); }
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); }
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); }
public new void Setup() { base.Setup(); m = new X86Assembler(sc, new MsdosPlatform(sc, new X86ArchitectureReal()), Address.SegPtr(0x100, 0x0100), new List<EntryPoint>()); }
private X86Assembler Create32bitAssembler() { arch = arch32; baseAddr = baseAddr32; var asm = new X86Assembler(sc, new DefaultPlatform(sc, arch), baseAddr32, new List<ImageSymbol>()); host = new RewriterHost(asm.ImportReferences); return asm; }
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); }
public new void Setup() { base.Setup(); m = new X86Assembler(new IntelArchitecture(ProcessorMode.Real), Address.SegPtr(0x100, 0x0100), new List<EntryPoint>()); }
private X86Assembler Create16bitAssembler() { arch = arch16; baseAddr = baseAddr16; var asm = new X86Assembler(sc, new MsdosPlatform(sc, arch), baseAddr16, new List<ImageSymbol>()); host = new RewriterHost(asm.ImportReferences); return asm; }
public void Setup() { arch = new X86ArchitectureFlat32(); asm = new X86Assembler(arch, loadAddress, new List<EntryPoint>()); }
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)); }
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); }
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); }