コード例 #1
0
        public void IsValidAddressSizeTest()
        {
            Assert.IsTrue(X86Architecture.IsValidAddressSize(CpuType.AmdBulldozer, DataSize.Bit64));
            Assert.IsFalse(X86Architecture.IsValidAddressSize(CpuType.IntelPentium4, DataSize.Bit64));

            Assert.IsTrue(X86Architecture.IsValidAddressSize(null, DataSize.Bit64));
            Assert.IsFalse(X86Architecture.IsValidAddressSize(null, DataSize.Bit256));
        }
コード例 #2
0
        public void ConstructorTest()
        {
            var arch = new X86Architecture();

            Assert.AreEqual(CpuType.IntelSandyBridge, arch.CpuType);
            Assert.AreEqual(DataSize.Bit32, arch.AddressSize);
            Assert.AreEqual(DataSize.Bit32, arch.OperandSize);
            Assert.AreEqual(CpuType.IntelSandyBridge.Features, arch.Features);
        }
コード例 #3
0
        public void ConstructorTest()
        {
            var arch = new X86Architecture();

            Assert.AreEqual(CpuType.IntelSandyBridge, arch.CpuType);
            Assert.AreEqual(DataSize.Bit32, arch.AddressSize);
            Assert.AreEqual(DataSize.Bit32, arch.OperandSize);
            Assert.AreEqual(CpuType.IntelSandyBridge.Features, arch.Features);
        }
コード例 #4
0
        public void NameTest()
        {
            X86Architecture arch;

            arch = new X86Architecture(CpuType.IntelNehalem);
            Assert.AreEqual("Intel codename Nehalem (x86-64)", arch.Name);

            arch = new X86Architecture(CpuFeatures.Privileged);
            Assert.AreEqual("x86-64 architecture", arch.Name);
        }
コード例 #5
0
        public void ConstructorTest_CpuFeatures()
        {
            var features = CpuFeatures.PclMulQdq | CpuFeatures.Privileged;

            var arch = new X86Architecture(features);

            Assert.AreEqual(null, arch.CpuType);
            Assert.AreEqual(DataSize.Bit32, arch.AddressSize);
            Assert.AreEqual(DataSize.Bit32, arch.OperandSize);
            Assert.AreEqual(features, arch.Features);
        }
コード例 #6
0
        public void ConstructorTest_CpuType()
        {
            var type = CpuType.AmdBulldozer;

            var arch = new X86Architecture(type);

            Assert.AreEqual(type, arch.CpuType);
            Assert.AreEqual(DataSize.Bit32, arch.AddressSize);
            Assert.AreEqual(DataSize.Bit32, arch.OperandSize);
            Assert.AreEqual(type.Features, arch.Features);
        }
コード例 #7
0
        private static ControlFlowGraph <Instruction> ConstructStaticFlowGraph(byte[] rawCode, long entrypoint)
        {
            var architecture        = new X86Architecture();
            var instructionProvider = new X86DecoderInstructionProvider(architecture, rawCode, 32);

            var cfgBuilder = new StaticFlowGraphBuilder <Instruction>(
                instructionProvider,
                new X86StaticSuccessorResolver());

            return(cfgBuilder.ConstructFlowGraph(entrypoint));
        }
コード例 #8
0
        public void ConstructorTest_CpuFeatures()
        {
            var features = CpuFeatures.PclMulQdq | CpuFeatures.Privileged;

            var arch = new X86Architecture(features);

            Assert.AreEqual(null, arch.CpuType);
            Assert.AreEqual(DataSize.Bit32, arch.AddressSize);
            Assert.AreEqual(DataSize.Bit32, arch.OperandSize);
            Assert.AreEqual(features, arch.Features);
        }
コード例 #9
0
        public void CreateContextTest()
        {
            BinObjectFileFormat format = new BinObjectFileFormat();
            var           arch         = new X86Architecture();
            BinObjectFile objectFile   = (BinObjectFile)format.CreateObjectFile(arch, "test");
            var           context      = arch.CreateContext(objectFile);

            Assert.IsNotNull(context);
            Assert.IsInstanceOf <Context>(context);
            Assert.AreEqual(objectFile, context.Representation);
        }
コード例 #10
0
        public void Do()
        {
            BinObjectFileFormat format = new BinObjectFileFormat();
            var           arch         = new X86Architecture(CpuType.AmdBulldozer, DataSize.Bit32);
            BinObjectFile objectFile   = (BinObjectFile)format.CreateObjectFile(arch, "helloworld");

            Section textSection = objectFile.Sections.AddNew(SectionType.Program);

            var text = textSection.Contents;

            text.Add(new Label("main"));
            text.Add(new Mov(Register.EDX, new Reference("len")));
            text.Add(new Mov(Register.ECX, new Reference("str")));
            text.Add(new Mov(Register.EBX, 1));
            text.Add(new Mov(Register.EAX, 4));
            text.Add(new Int(0x80));

            text.Add(new Mov(Register.EBX, 0));
            text.Add(new Mov(Register.EAX, 1));
            text.Add(new Int(0x80));

            Section dataSection = objectFile.Sections.AddNew(SectionType.Data);
            var     data        = dataSection.Contents;

            data.Add(new Label("str"));
            data.Add(new DeclareString("Hello World\n"));

            data.Add(new Define("len", (context) =>
            {
                Symbol strSymbol = context.SymbolTable["str"];
                return(new SimpleExpression(context.Address - strSymbol.Value));
            }));

            byte[] result   = Assemble(objectFile);
            byte[] expected = new byte[] {
                0xBA, 0x0C, 0x00, 0x00, 0x00,                           // mov EDX, len
                0xB9, 0x30, 0x00, 0x00, 0x00,                           // mov ECX, str
                0xBB, 0x01, 0x00, 0x00, 0x00,                           // mov EBX, 1
                0xB8, 0x04, 0x00, 0x00, 0x00,                           // mov EAX, 4
                0xCD, 0x80,                                             // int 0x80

                0xBB, 0x00, 0x00, 0x00, 0x00,                           // mov EBX, 0
                0xB8, 0x01, 0x00, 0x00, 0x00,                           // mov EAX, 1
                0xCD, 0x80,                                             // int 0x80

                // Padding
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                // "Hello World\n"
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x0A
            };
            Assert.AreEqual(expected, result);
        }
コード例 #11
0
        public void ConstructorTest_CpuType_CpuFeatures_DataSize()
        {
            var type     = CpuType.IntelPenryn;
            var size     = DataSize.Bit64;
            var features = CpuFeatures.PclMulQdq | CpuFeatures.Privileged;

            var arch = new X86Architecture(type, features, size);

            Assert.AreEqual(type, arch.CpuType);
            Assert.AreEqual(size, arch.AddressSize);
            Assert.AreEqual(size, arch.OperandSize);
            Assert.AreEqual(type.Features | features, arch.Features);
        }
コード例 #12
0
        public void Do()
        {
            BinObjectFileFormat format = new BinObjectFileFormat();
            var arch = new X86Architecture(CpuType.AmdBulldozer, DataSize.Bit32);
            BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "helloworld");

            Section textSection = objectFile.Sections.AddNew(SectionType.Program);

            var text = textSection.Contents;
            text.Add(new Label("main"));
            text.Add(new Mov(Register.EDX, new Reference("len")));
            text.Add(new Mov(Register.ECX, new Reference("str")));
            text.Add(new Mov(Register.EBX, 1));
            text.Add(new Mov(Register.EAX, 4));
            text.Add(new Int(0x80));

            text.Add(new Mov(Register.EBX, 0));
            text.Add(new Mov(Register.EAX, 1));
            text.Add(new Int(0x80));

            Section dataSection = objectFile.Sections.AddNew(SectionType.Data);
            var data = dataSection.Contents;
            data.Add(new Label("str"));
            data.Add(new DeclareString("Hello World\n"));

            data.Add(new Define("len", (context) =>
                {
                    Symbol strSymbol = context.SymbolTable["str"];
                    return new SimpleExpression(context.Address - strSymbol.Value);
                }));

            byte[] result = Assemble(objectFile);
            byte[] expected = new byte[]{
                0xBA, 0x0C, 0x00, 0x00, 0x00,		// mov EDX, len
                0xB9, 0x30, 0x00, 0x00, 0x00,		// mov ECX, str
                0xBB, 0x01, 0x00, 0x00, 0x00,		// mov EBX, 1
                0xB8, 0x04, 0x00, 0x00, 0x00,		// mov EAX, 4
                0xCD, 0x80,							// int 0x80

                0xBB, 0x00, 0x00, 0x00, 0x00,		// mov EBX, 0
                0xB8, 0x01, 0x00, 0x00, 0x00,		// mov EAX, 1
                0xCD, 0x80,							// int 0x80

                // Padding
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                // "Hello World\n"
                0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x0A
            };
            Assert.AreEqual(expected, result);
        }
コード例 #13
0
        public static ControlFlowGraph <Instruction> ReadNativeFunction(string loc, uint fileOffset, bool is32Bit)
        {
            using var fs = File.OpenRead(loc);
            fs.Position  = fileOffset;

            var architecture        = new X86Architecture();
            var instructionProvider = new X86DecoderInstructionProvider(architecture, fs, is32Bit ? 32 : 64);
            var cfgBuilder          = new StaticFlowGraphBuilder <Instruction>(
                instructionProvider,
                new X86StaticSuccessorResolver());

            // pass in a file offset, since we're working on a file on disk. would pass rva (and base addr in provider
            // ctor) for in-memory.
            ControlFlowGraph <Instruction> graph = cfgBuilder.ConstructFlowGraph(fileOffset);

            return(graph);
        }
コード例 #14
0
        /// <summary>
        /// Assembles the given instruction.
        /// </summary>
        /// <param name="instruction">The <see cref="X86Instruction"/> to assemble.</param>
        /// <param name="mode">The mode in which to assemble.</param>
        /// <returns>The bytes representing the assembled instruction.</returns>
        /// <exception cref="AssemblerException">
        /// An assembler exception occurred.
        /// </exception>
        private byte[] Assemble(X86Instruction instruction, DataSize mode)
        {
            byte[] actual = null;
            BinObjectFileFormat format = new BinObjectFileFormat();
            var           arch         = new X86Architecture(CpuType.AmdBulldozer, mode);
            BinObjectFile objectFile   = (BinObjectFile)format.CreateObjectFile(arch, "test");
            Section       textSection  = objectFile.Sections.AddNew(SectionType.Program);
            var           text         = textSection.Contents;

            text.Add(instruction);

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    objectFile.Format.CreateAssembler(objectFile).Assemble(writer);
                    actual = ms.ToArray();
                }
            }

            return(actual);
        }
コード例 #15
0
        public void Do2()
        {
            BinObjectFileFormat format = new BinObjectFileFormat();
            var           arch         = new X86Architecture(CpuType.AmdBulldozer, DataSize.Bit32);
            BinObjectFile objectFile   = (BinObjectFile)format.CreateObjectFile(arch, "helloworld");

            Section textSection = objectFile.Sections.AddNew(SectionType.Program);
            var     text        = textSection.Contents;

            text.Add(new Label("main"));
            text.Add(new Mov(Register.EDX, new Reference("len")));
            text.Add(new Mov(Register.ECX, new Reference("str")));
            text.Add(new Mov(Register.EBX, 1));
            text.Add(new Mov(Register.EAX, 4));
            text.Add(new Int(0x80));

            text.Add(new Mov(Register.EBX, 0));
            text.Add(new Mov(Register.EAX, 1));
            text.Add(new Int(0x80));

            Section dataSection = objectFile.Sections.AddNew(SectionType.Data);
            var     data        = dataSection.Contents;

            data.Add(new Label("str"));
            data.Add(new DeclareString("Hello World\n"));

            data.Add(new Define("len", (context) =>
            {
                Symbol strSymbol = context.SymbolTable["str"];
                return(new SimpleExpression(context.Address - strSymbol.Value));
            }));

            using (FileStream fs = File.Create("helloworld.bin"))
                using (BinaryWriter writer = new BinaryWriter(fs))
                    objectFile.Format.CreateAssembler(objectFile).Assemble(writer);
        }
コード例 #16
0
        /// <summary>
        /// Assembles the given instruction.
        /// </summary>
        /// <param name="instruction">The <see cref="X86Instruction"/> instance to test.</param>
        /// <param name="nasmInstruction">The NASM string representation of the same instruction.</param>
        /// <param name="mode">The mode (16-bit, 32-bit or 64-bit) to use.</param>
        /// <returns>A (expected, actual) tuple.</returns>
        private Tuple <byte[], byte[]> AssembleInstruction(X86Instruction instruction, string nasmInstruction, DataSize mode)
        {
            #region Contract
            if (!Enum.IsDefined(typeof(DataSize), mode))
            {
                throw new InvalidEnumArgumentException("mode", (int)mode, typeof(DataSize));
            }
            if (mode != DataSize.Bit16 && mode != DataSize.Bit32 && mode != DataSize.Bit64)
            {
                throw new ArgumentException(null, "mode");
            }
            #endregion

            // Assemble the NASM instruction.
            byte[] expected = null;
            if (nasmInstruction != null)
            {
                StringBuilder sb = new StringBuilder();
                switch (mode)
                {
                case DataSize.Bit16:
                    sb.AppendLine("[BITS 16]");
                    break;

                case DataSize.Bit32:
                    sb.AppendLine("[BITS 32]");
                    break;

                case DataSize.Bit64:
                    sb.AppendLine("[BITS 64]");
                    break;

                default:
                    throw new NotSupportedException();
                }
                sb.AppendLine(nasmInstruction);
                string feedback;
                expected = RunAssembler(sb.ToString(), out feedback);
                if (feedback != null && feedback.Length > 0)
                {
                    Console.WriteLine("Assembler feedback:");
                    Console.WriteLine(feedback);
                }
            }

            // Assemble the SharpAssembler instruction.
            byte[] actual = null;
            if (instruction != null)
            {
                BinObjectFileFormat format = new BinObjectFileFormat();
                var           arch         = new X86Architecture(CpuType.AmdBulldozer, mode);
                BinObjectFile objectFile   = (BinObjectFile)format.CreateObjectFile(arch, "test");
                Section       textSection  = objectFile.Sections.AddNew(SectionType.Program);
                var           text         = textSection.Contents;

                text.Add(instruction);

                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (BinaryWriter writer = new BinaryWriter(ms))
                        {
                            objectFile.Format.CreateAssembler(objectFile).Assemble(writer);
                            actual = ms.ToArray();
                        }
                    }
                }
                catch (AssemblerException ex)
                {
                    Console.WriteLine(ex);
                    actual = null;
                }
            }

            return(new Tuple <byte[], byte[]>(expected, actual));
        }
コード例 #17
0
        /// <summary>
        /// Assembles the given instruction.
        /// </summary>
        /// <param name="instruction">The <see cref="X86Instruction"/> to assemble.</param>
        /// <param name="mode">The mode in which to assemble.</param>
        /// <returns>The bytes representing the assembled instruction.</returns>
        /// <exception cref="AssemblerException">
        /// An assembler exception occurred.
        /// </exception>
        private byte[] Assemble(X86Instruction instruction, DataSize mode)
        {
            byte[] actual = null;
            BinObjectFileFormat format = new BinObjectFileFormat();
            var arch = new X86Architecture(CpuType.AmdBulldozer, mode);
            BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "test");
            Section textSection = objectFile.Sections.AddNew(SectionType.Program);
            var text = textSection.Contents;

            text.Add(instruction);

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    objectFile.Format.CreateAssembler(objectFile).Assemble(writer);
                    actual = ms.ToArray();
                }
            }

            return actual;
        }
コード例 #18
0
 public void CreateContextTest()
 {
     BinObjectFileFormat format = new BinObjectFileFormat();
     var arch = new X86Architecture();
     BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "test");
     var context = arch.CreateContext(objectFile);
     Assert.IsNotNull(context);
     Assert.IsInstanceOf<Context>(context);
     Assert.AreEqual(objectFile, context.Representation);
 }
コード例 #19
0
        /// <summary>
        /// Assembles the given instruction.
        /// </summary>
        /// <param name="instruction">The <see cref="X86Instruction"/> instance to test.</param>
        /// <param name="nasmInstruction">The NASM string representation of the same instruction.</param>
        /// <param name="mode">The mode (16-bit, 32-bit or 64-bit) to use.</param>
        /// <returns>A (expected, actual) tuple.</returns>
        private Tuple<byte[], byte[]> AssembleInstruction(X86Instruction instruction, string nasmInstruction, DataSize mode)
        {
            #region Contract
            if (!Enum.IsDefined(typeof(DataSize), mode))
                throw new InvalidEnumArgumentException("mode", (int)mode, typeof(DataSize));
            if (mode != DataSize.Bit16 && mode != DataSize.Bit32 && mode != DataSize.Bit64)
                throw new ArgumentException(null, "mode");
            #endregion

            // Assemble the NASM instruction.
            byte[] expected = null;
            if (nasmInstruction != null)
            {
                StringBuilder sb = new StringBuilder();
                switch (mode)
                {
                    case DataSize.Bit16:
                        sb.AppendLine("[BITS 16]");
                        break;
                    case DataSize.Bit32:
                        sb.AppendLine("[BITS 32]");
                        break;
                    case DataSize.Bit64:
                        sb.AppendLine("[BITS 64]");
                        break;
                    default:
                        throw new NotSupportedException();
                }
                sb.AppendLine(nasmInstruction);
                string feedback;
                expected = RunAssembler(sb.ToString(), out feedback);
                if (feedback != null && feedback.Length > 0)
                {
                    Console.WriteLine("Assembler feedback:");
                    Console.WriteLine(feedback);
                }
            }

            // Assemble the SharpAssembler instruction.
            byte[] actual = null;
            if (instruction != null)
            {
                BinObjectFileFormat format = new BinObjectFileFormat();
                var arch = new X86Architecture(CpuType.AmdBulldozer, mode);
                BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "test");
                Section textSection = objectFile.Sections.AddNew(SectionType.Program);
                var text = textSection.Contents;

                text.Add(instruction);

                try
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (BinaryWriter writer = new BinaryWriter(ms))
                        {
                            objectFile.Format.CreateAssembler(objectFile).Assemble(writer);
                            actual = ms.ToArray();
                        }
                    }
                }
                catch (AssemblerException ex)
                {
                    Console.WriteLine(ex);
                    actual = null;
                }
            }

            return new Tuple<byte[], byte[]>(expected, actual);
        }
コード例 #20
0
        public void NameTest()
        {
            X86Architecture arch;

            arch = new X86Architecture(CpuType.IntelNehalem);
            Assert.AreEqual("Intel codename Nehalem (x86-64)", arch.Name);

            arch = new X86Architecture(CpuFeatures.Privileged);
            Assert.AreEqual("x86-64 architecture", arch.Name);
        }
コード例 #21
0
            /// <summary>
            /// Checks whether the instruction variant is supported in the specified architecture (depending 64-bit
            /// mode or CPU features).
            /// </summary>
            /// <param name="architecture">The architecture.</param>
            /// <returns><see langword="true"/> when the instruction variant is valid for the
            /// specified architecture; otherwise, <see langword="false"/>.</returns>
            public bool IsValid(X86Architecture architecture)
            {
                #region Contract
                Contract.Requires<ArgumentNullException>(architecture != null);
                #endregion

                if ((architecture.Features & requiredFeatures) != requiredFeatures)
                    return false;
                // TODO: Implement.
                //if (architecture.AddressSize
                throw new NotImplementedException();
            }
コード例 #22
0
        public void Do2()
        {
            BinObjectFileFormat format = new BinObjectFileFormat();
            var arch = new X86Architecture(CpuType.AmdBulldozer, DataSize.Bit32);
            BinObjectFile objectFile = (BinObjectFile)format.CreateObjectFile(arch, "helloworld");

            Section textSection = objectFile.Sections.AddNew(SectionType.Program);
            var text = textSection.Contents;
            text.Add(new Label("main"));
            text.Add(new Mov(Register.EDX, new Reference("len")));
            text.Add(new Mov(Register.ECX, new Reference("str")));
            text.Add(new Mov(Register.EBX, 1));
            text.Add(new Mov(Register.EAX, 4));
            text.Add(new Int(0x80));

            text.Add(new Mov(Register.EBX, 0));
            text.Add(new Mov(Register.EAX, 1));
            text.Add(new Int(0x80));

            Section dataSection = objectFile.Sections.AddNew(SectionType.Data);
            var data = dataSection.Contents;
            data.Add(new Label("str"));
            data.Add(new DeclareString("Hello World\n"));

            data.Add(new Define("len", (context) =>
            {
                Symbol strSymbol = context.SymbolTable["str"];
                return new SimpleExpression(context.Address - strSymbol.Value);
            }));

            using (FileStream fs = File.Create("helloworld.bin"))
                using (BinaryWriter writer = new BinaryWriter(fs))
                    objectFile.Format.CreateAssembler(objectFile).Assemble(writer);
        }
コード例 #23
0
        public void ConstructorTest_CpuType_CpuFeatures_DataSize()
        {
            var type = CpuType.IntelPenryn;
            var size = DataSize.Bit64;
            var features = CpuFeatures.PclMulQdq | CpuFeatures.Privileged;

            var arch = new X86Architecture(type, features, size);

            Assert.AreEqual(type, arch.CpuType);
            Assert.AreEqual(size, arch.AddressSize);
            Assert.AreEqual(size, arch.OperandSize);
            Assert.AreEqual(type.Features | features, arch.Features);
        }
コード例 #24
0
 public X86StateTransitionResolverTest()
 {
     _architecture = new X86Architecture();
 }
コード例 #25
0
        public void ConstructorTest_CpuType()
        {
            var type = CpuType.AmdBulldozer;

            var arch = new X86Architecture(type);

            Assert.AreEqual(type, arch.CpuType);
            Assert.AreEqual(DataSize.Bit32, arch.AddressSize);
            Assert.AreEqual(DataSize.Bit32, arch.OperandSize);
            Assert.AreEqual(type.Features, arch.Features);
        }