Exemplo n.º 1
0
        void DefaultArgs()
        {
            const int   bitness = 64;
            const ulong origRip = 0x123456789ABCDE00;
            const ulong newRip  = 0x8000000000000000;

            var originalData = new byte[] {
                /*0000*/ 0xB0, 0x00,                   // mov al,0
                /*0002*/ 0xEB, 0x09,                   // jmp short 123456789ABCDE0Dh
                /*0004*/ 0xB0, 0x01,                   // mov al,1
                /*0006*/ 0xE9, 0x03, 0x00, 0x00, 0x00, // jmp near ptr 123456789ABCDE0Eh
                /*000B*/ 0xB0, 0x02,                   // mov al,2
            };
            var  instructions = BlockEncoderTest.Decode(bitness, origRip, originalData, DecoderOptions.None);
            var  codeWriter   = new CodeWriterImpl();
            bool b            = BlockEncoder.TryEncode(bitness, new InstructionBlock(codeWriter, instructions, newRip), out var errorMessage, out var result);

            Assert.True(b);
            Assert.Null(errorMessage);
            Assert.Equal(newRip, result.RIP);
            Assert.Equal(0x28, codeWriter.ToArray().Length);
            Assert.Null(result.RelocInfos);
            Assert.NotNull(result.NewInstructionOffsets);
            Assert.True(result.NewInstructionOffsets.Length == 0);
            Assert.NotNull(result.ConstantOffsets);
            Assert.True(result.ConstantOffsets.Length == 0);
        }
Exemplo n.º 2
0
        void DefaultArgs()
        {
            const int   bitness = 64;
            const ulong origRip = 0x123456789ABCDE00;
            const ulong newRip  = 0x8000000000000000;

            var originalData = new byte[] {
                /*0000*/ 0xB0, 0x00,                   // mov al,0
                /*0002*/ 0xEB, 0x09,                   // jmp short 123456789ABCDE0Dh
                /*0004*/ 0xB0, 0x01,                   // mov al,1
                /*0006*/ 0xE9, 0x03, 0x00, 0x00, 0x00, // jmp near ptr 123456789ABCDE0Eh
                /*000B*/ 0xB0, 0x02,                   // mov al,2
            };
            var instructions = BlockEncoderTest.Decode(bitness, origRip, originalData, DecoderOptions.None);
            var codeWriter   = new CodeWriterImpl();
            var errorMessage = BlockEncoder.Encode(bitness, new InstructionBlock(codeWriter, instructions, newRip));

            Assert.Null(errorMessage);
            Assert.Equal(0x28, codeWriter.ToArray().Length);
        }
Exemplo n.º 3
0
        void VerifyResultArrays(BlockEncoderOptions options)
        {
            const int   bitness  = 64;
            const ulong origRip1 = 0x123456789ABCDE00;
            const ulong origRip2 = 0x223456789ABCDE00;
            const ulong newRip1  = 0x8000000000000000;
            const ulong newRip2  = 0x9000000000000000;

            {
                var  instructions1 = BlockEncoderTest.Decode(bitness, origRip1, new byte[] { 0xE9, 0x56, 0x78, 0xA5, 0x5A }, DecoderOptions.None);
                var  codeWriter1   = new CodeWriterImpl();
                bool b             = BlockEncoder.TryEncode(bitness, new InstructionBlock(codeWriter1, instructions1, newRip1), out var errorMessage, out var result, options);
                Assert.True(b);
                Assert.Null(errorMessage);
                Assert.Equal(newRip1, result.RIP);
                if ((options & BlockEncoderOptions.ReturnRelocInfos) != 0)
                {
                    Assert.NotNull(result.RelocInfos);
                    Assert.True(result.RelocInfos.Count == 1);
                }
                else
                {
                    Assert.Null(result.RelocInfos);
                }
                if ((options & BlockEncoderOptions.ReturnNewInstructionOffsets) != 0)
                {
                    Assert.NotNull(result.NewInstructionOffsets);
                    Assert.True(result.NewInstructionOffsets.Length == 1);
                }
                else
                {
                    Assert.NotNull(result.NewInstructionOffsets);
                    Assert.True(result.NewInstructionOffsets.Length == 0);
                }
                if ((options & BlockEncoderOptions.ReturnConstantOffsets) != 0)
                {
                    Assert.NotNull(result.ConstantOffsets);
                    Assert.True(result.ConstantOffsets.Length == 1);
                }
                else
                {
                    Assert.NotNull(result.ConstantOffsets);
                    Assert.True(result.ConstantOffsets.Length == 0);
                }
            }
            {
                var  instructions1 = BlockEncoderTest.Decode(bitness, origRip1, new byte[] { 0xE9, 0x56, 0x78, 0xA5, 0x5A }, DecoderOptions.None);
                var  instructions2 = BlockEncoderTest.Decode(bitness, origRip2, new byte[] { 0x90, 0xE9, 0x56, 0x78, 0xA5, 0x5A }, DecoderOptions.None);
                var  codeWriter1   = new CodeWriterImpl();
                var  codeWriter2   = new CodeWriterImpl();
                var  block1        = new InstructionBlock(codeWriter1, instructions1, newRip1);
                var  block2        = new InstructionBlock(codeWriter2, instructions2, newRip2);
                bool b             = BlockEncoder.TryEncode(bitness, new[] { block1, block2 }, out var errorMessage, out var resultArray, options);
                Assert.True(b);
                Assert.Null(errorMessage);
                Assert.NotNull(resultArray);
                Assert.Equal(2, resultArray.Length);
                Assert.Equal(newRip1, resultArray[0].RIP);
                Assert.Equal(newRip2, resultArray[1].RIP);
                if ((options & BlockEncoderOptions.ReturnRelocInfos) != 0)
                {
                    Assert.NotNull(resultArray[0].RelocInfos);
                    Assert.NotNull(resultArray[1].RelocInfos);
                    Assert.True(resultArray[0].RelocInfos.Count == 1);
                    Assert.True(resultArray[1].RelocInfos.Count == 1);
                }
                else
                {
                    Assert.Null(resultArray[0].RelocInfos);
                    Assert.Null(resultArray[1].RelocInfos);
                }
                if ((options & BlockEncoderOptions.ReturnNewInstructionOffsets) != 0)
                {
                    Assert.NotNull(resultArray[0].NewInstructionOffsets);
                    Assert.NotNull(resultArray[1].NewInstructionOffsets);
                    Assert.True(resultArray[0].NewInstructionOffsets.Length == 1);
                    Assert.True(resultArray[1].NewInstructionOffsets.Length == 2);
                }
                else
                {
                    Assert.NotNull(resultArray[0].NewInstructionOffsets);
                    Assert.True(resultArray[0].NewInstructionOffsets.Length == 0);
                    Assert.NotNull(resultArray[1].NewInstructionOffsets);
                    Assert.True(resultArray[1].NewInstructionOffsets.Length == 0);
                }
                if ((options & BlockEncoderOptions.ReturnConstantOffsets) != 0)
                {
                    Assert.NotNull(resultArray[0].ConstantOffsets);
                    Assert.NotNull(resultArray[1].ConstantOffsets);
                    Assert.True(resultArray[0].ConstantOffsets.Length == 1);
                    Assert.True(resultArray[1].ConstantOffsets.Length == 2);
                }
                else
                {
                    Assert.NotNull(resultArray[0].ConstantOffsets);
                    Assert.True(resultArray[0].ConstantOffsets.Length == 0);
                    Assert.NotNull(resultArray[1].ConstantOffsets);
                    Assert.True(resultArray[1].ConstantOffsets.Length == 0);
                }
            }
        }