aboutsummaryrefslogtreecommitdiff
path: root/fuzz/fuzz_targets/reuse_does_not_change_behavior.rs
blob: e7c13a52bfaae6e90eebad0b1ee1768e8608ec05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#![no_main]
#[macro_use] extern crate libfuzzer_sys;
extern crate yaxpeax_x86;

use yaxpeax_arch::{Decoder, LengthedInstruction};

fuzz_target!(|data: &[u8]| {
    let x86_64_decoder = yaxpeax_x86::long_mode::InstDecoder::default();
    let x86_32_decoder = yaxpeax_x86::protected_mode::InstDecoder::default();
    let x86_16_decoder = yaxpeax_x86::real_mode::InstDecoder::default();

    {
        use yaxpeax_x86::long_mode::{DisplayStyle, InstructionTextBuffer};
        let decoder = x86_64_decoder;

        if let Ok(mut inst) = decoder.decode_slice(data) {
            let remainder_slice = &data[inst.len().to_const() as usize..];

            let mut initial_inst_buf = InstructionTextBuffer::new();
            initial_inst_buf.format_inst(&inst.display_with(DisplayStyle::Intel)).expect("can format");

            let mut reader = yaxpeax_arch::U8Reader::new(data);
            decoder.decode_into(&mut inst, &mut reader).expect("can redecode");

            let mut re_inst_buf = InstructionTextBuffer::new();
            re_inst_buf.format_inst(&inst.display_with(DisplayStyle::Intel)).expect("can format");

            assert_eq!(initial_inst_buf.text_str(), re_inst_buf.text_str());

            let first_next_inst = decoder.decode_slice(remainder_slice);
            let mut rest_reader = yaxpeax_arch::U8Reader::new(remainder_slice);
            let rest_res = decoder.decode_into(&mut inst, &mut rest_reader);
            match (first_next_inst, rest_res) {
                (Ok(first_next_inst), Ok(())) => {
                    assert_eq!(first_next_inst, inst, "on second pass of {:x?}", remainder_slice);
                }
                (Err(left), Err(right)) => {
                    assert_eq!(left, right, "on second pass of {:x?}", remainder_slice);
                }
                other => {
                    panic!("mismatched results when reusing instruction: {:?}", other);
                }
            }
        };
    }

    {
        use yaxpeax_x86::protected_mode::{DisplayStyle, InstructionTextBuffer};
        let decoder = x86_32_decoder;

        if let Ok(mut inst) = decoder.decode_slice(data) {
            let remainder_slice = &data[inst.len().to_const() as usize..];

            let mut initial_inst_buf = InstructionTextBuffer::new();
            initial_inst_buf.format_inst(&inst.display_with(DisplayStyle::Intel)).expect("can format");

            let mut reader = yaxpeax_arch::U8Reader::new(data);
            decoder.decode_into(&mut inst, &mut reader).expect("can redecode");

            let mut re_inst_buf = InstructionTextBuffer::new();
            re_inst_buf.format_inst(&inst.display_with(DisplayStyle::Intel)).expect("can format");

            assert_eq!(initial_inst_buf.text_str(), re_inst_buf.text_str());

            let first_next_inst = decoder.decode_slice(remainder_slice);
            let mut rest_reader = yaxpeax_arch::U8Reader::new(remainder_slice);
            let rest_res = decoder.decode_into(&mut inst, &mut rest_reader);
            match (first_next_inst, rest_res) {
                (Ok(first_next_inst), Ok(())) => {
                    assert_eq!(first_next_inst, inst, "on second pass of {:x?}", remainder_slice);
                }
                (Err(left), Err(right)) => {
                    assert_eq!(left, right, "on second pass of {:x?}", remainder_slice);
                }
                other => {
                    panic!("mismatched results when reusing instruction: {:?}", other);
                }
            }
        };
    }

    {
        use yaxpeax_x86::real_mode::{DisplayStyle, InstructionTextBuffer};
        let decoder = x86_16_decoder;

        if let Ok(mut inst) = decoder.decode_slice(data) {
            let remainder_slice = &data[inst.len().to_const() as usize..];

            let mut initial_inst_buf = InstructionTextBuffer::new();
            initial_inst_buf.format_inst(&inst.display_with(DisplayStyle::Intel)).expect("can format");

            let mut reader = yaxpeax_arch::U8Reader::new(data);
            decoder.decode_into(&mut inst, &mut reader).expect("can redecode");

            let mut re_inst_buf = InstructionTextBuffer::new();
            re_inst_buf.format_inst(&inst.display_with(DisplayStyle::Intel)).expect("can format");

            assert_eq!(initial_inst_buf.text_str(), re_inst_buf.text_str());

            let first_next_inst = decoder.decode_slice(remainder_slice);
            let mut rest_reader = yaxpeax_arch::U8Reader::new(remainder_slice);
            let rest_res = decoder.decode_into(&mut inst, &mut rest_reader);
            match (first_next_inst, rest_res) {
                (Ok(first_next_inst), Ok(())) => {
                    assert_eq!(first_next_inst, inst, "on second pass of {:x?}", remainder_slice);
                }
                (Err(left), Err(right)) => {
                    assert_eq!(left, right, "on second pass of {:x?}", remainder_slice);
                }
                other => {
                    panic!("mismatched results when reusing instruction: {:?}", other);
                }
            }
        };
    }
});