Example #1
0
        Decoder CreateDecoder(int codeSize, string hexBytes)
        {
            Decoder decoder;
            var     codeReader = new ByteArrayCodeReader(hexBytes);

            switch (codeSize)
            {
            case 16:
                decoder = Decoder.Create16(codeReader);
                decoder.InstructionPointer = DecoderConstants.DEFAULT_IP16;
                break;

            case 32:
                decoder = Decoder.Create32(codeReader);
                decoder.InstructionPointer = DecoderConstants.DEFAULT_IP32;
                break;

            case 64:
                decoder = Decoder.Create64(codeReader);
                decoder.InstructionPointer = DecoderConstants.DEFAULT_IP64;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(codeSize));
            }

            Assert.Equal(codeSize, decoder.Bitness);
            return(decoder);
        }
Example #2
0
        void Decode_multiple_instrs_with_one_instance()
        {
            var reader16      = new DecodeMultipleCodeReader();
            var reader32      = new DecodeMultipleCodeReader();
            var reader64      = new DecodeMultipleCodeReader();
            var decoderDict16 = new Dictionary <DecoderOptions, Decoder>();
            var decoderDict32 = new Dictionary <DecoderOptions, Decoder>();
            var decoderDict64 = new Dictionary <DecoderOptions, Decoder>();

            foreach (var info in DecoderTestUtils.GetDecoderTests(needHexBytes: true, includeOtherTests: false))
            {
                var     data    = HexUtils.ToByteArray(info.HexBytes);
                var     decoder = Decoder.Create(info.Bitness, new ByteArrayCodeReader(data), info.Options);
                Decoder decoderAll;
                switch (info.Bitness)
                {
                case 16:
                    decoder.InstructionPointer = DecoderConstants.DEFAULT_IP16;
                    reader16.SetArray(data);
                    if (!decoderDict16.TryGetValue(info.Options, out decoderAll))
                    {
                        decoderDict16.Add(info.Options, decoderAll = Decoder.Create16(reader16, info.Options));
                    }
                    break;

                case 32:
                    decoder.InstructionPointer = DecoderConstants.DEFAULT_IP32;
                    reader32.SetArray(data);
                    if (!decoderDict32.TryGetValue(info.Options, out decoderAll))
                    {
                        decoderDict32.Add(info.Options, decoderAll = Decoder.Create32(reader32, info.Options));
                    }
                    break;

                case 64:
                    decoder.InstructionPointer = DecoderConstants.DEFAULT_IP64;
                    reader64.SetArray(data);
                    if (!decoderDict64.TryGetValue(info.Options, out decoderAll))
                    {
                        decoderDict64.Add(info.Options, decoderAll = Decoder.Create64(reader64, info.Options));
                    }
                    break;

                default:
                    throw new InvalidOperationException();
                }
                decoderAll.InstructionPointer = decoder.InstructionPointer;
                var instr1 = decoder.Decode();
                var instr2 = decoderAll.Decode();
                Assert.Equal(info.Code, instr1.Code);
                Assert.True(Instruction.TEST_BitByBitEquals(instr1, instr2));
            }
        }
Example #3
0
        static Instruction[] GetInstructions()
        {
            var decoder = Decoder.Create64(new ByteArrayCodeReader(testCode));
            var endRip  = decoder.InstructionPointer + (uint)testCode.Length;
            var list    = new List <Instruction>();

            while (decoder.InstructionPointer < endRip)
            {
                list.Add(decoder.Decode());
            }
            return(list.ToArray());
        }
Example #4
0
        void Encode_MOV_CR8_in_64bit_mode_does_not_add_LOCK(string hexBytes, Code code, string encodedBytes)
        {
            var decoder = Decoder.Create64(new ByteArrayCodeReader(hexBytes));

            decoder.Decode(out var instr);
            Assert.Equal(code, instr.Code);
            var writer  = new CodeWriterImpl();
            var encoder = decoder.CreateEncoder(writer);

            encoder.Encode(ref instr, 0);
            var expectedBytes = HexUtils.ToByteArray(encodedBytes);
            var actualBytes   = writer.ToArray();

            Assert.Equal(expectedBytes, actualBytes);
        }
Example #5
0
        void Decode_multiple_instrs_with_one_instance()
        {
            var reader16     = new DecodeMultipleCodeReader();
            var reader32     = new DecodeMultipleCodeReader();
            var reader64     = new DecodeMultipleCodeReader();
            var decoderAll16 = Decoder.Create16(reader16);
            var decoderAll32 = Decoder.Create32(reader32);
            var decoderAll64 = Decoder.Create64(reader64);

            foreach (var info in DecoderTestUtils.GetDecoderTests(needHexBytes: true, includeOtherTests: false))
            {
                var     data    = HexUtils.ToByteArray(info.HexBytes);
                var     decoder = Decoder.Create(info.Bitness, new ByteArrayCodeReader(data));
                Decoder decoderAll;
                switch (info.Bitness)
                {
                case 16:
                    decoder.InstructionPointer = DecoderConstants.DEFAULT_IP16;
                    reader16.SetArray(data);
                    decoderAll = decoderAll16;
                    break;

                case 32:
                    decoder.InstructionPointer = DecoderConstants.DEFAULT_IP32;
                    reader32.SetArray(data);
                    decoderAll = decoderAll32;
                    break;

                case 64:
                    decoder.InstructionPointer = DecoderConstants.DEFAULT_IP64;
                    reader64.SetArray(data);
                    decoderAll = decoderAll64;
                    break;

                default:
                    throw new InvalidOperationException();
                }
                decoderAll.InstructionPointer = decoder.InstructionPointer;
                var instr1 = decoder.Decode();
                var instr2 = decoderAll.Decode();
                Assert.Equal(info.Code, instr1.Code);
                Assert.True(Instruction.TEST_BitByBitEquals(ref instr1, ref instr2));
            }
        }