1use fNET::*;
2
3pub struct Rng(pub u64);
4impl Rng {
5 pub fn next(&mut self) -> u64 {
6 self.0 ^= self.0 << 13;
7 self.0 ^= self.0 >> 7;
8 self.0 ^= self.0 << 17;
9 self.0
10 }
11}
12
13pub fn run(cases: usize, seed: u64) -> usize {
15 let mut rng = Rng(seed.max(1));
16 let descriptors = [
17 OpCodeInfo::custom(0xf780, 2, "blob", OperandKind::InlineData),
18 OpCodeInfo::custom(0xf781, 2, "short-blob", OperandKind::ShortInlineData),
19 OpCodeInfo::custom(0xfe70, 2, "switch", OperandKind::InlineSwitch),
20 OpCodeInfo::custom(0x24, 1, "wide", OperandKind::InlineBytes(16)),
21 ];
22 let table = ExtensionTable::new(&descriptors).unwrap();
23 let mut total = 0;
24 for index in 0..cases {
25 let mut data = Vec::with_capacity(512);
26 let count = rng.next() as usize % 256;
27 match index % 5 {
28 0 => data.extend((0..count).map(|_| rng.next() as u8)),
29 1 => {
30 for _ in 0..1 + count % 16 {
31 let info = ALL_CODES[rng.next() as usize % ALL_CODES.len()].op_code();
32 append(&mut data, info, &mut rng);
33 }
34 }
35 2 => {
36 for _ in 0..1 + count % 32 {
37 let prefix = [
38 Code::Unaligned,
39 Code::Volatile,
40 Code::Constrained,
41 Code::Readonly,
42 Code::No,
43 Code::Tail,
44 ][rng.next() as usize % 6];
45 append(&mut data, prefix.op_code(), &mut rng);
46 }
47 append(&mut data, Code::Callvirt.op_code(), &mut rng);
48 data.push(0x2a);
49 }
50 3 => {
51 let info = &descriptors[rng.next() as usize % descriptors.len()];
52 append(&mut data, info, &mut rng);
53 data.push(0x2a);
54 }
55 _ => {
56 let fixture = include_bytes!("../../tests/data/runtime-corpus.bin");
57 let start = rng.next() as usize % (fixture.len() - 256);
58 data.extend_from_slice(&fixture[start..start + count]);
59 for _ in 0..count % 8 {
60 let slot = rng.next() as usize % data.len().max(1);
61 if let Some(value) = data.get_mut(slot) {
62 *value ^= rng.next() as u8;
63 }
64 }
65 }
66 }
67 if index % 3 == 0 && !data.is_empty() {
68 data.truncate(rng.next() as usize % data.len());
69 }
70 let options = if index % 3 == 0 {
71 DecoderOptions::ECMA335
72 } else {
73 DecoderOptions {
74 features: FeatureSet::ALL,
75 decode_reserved: index % 2 == 0,
76 }
77 };
78 let dialect = [
79 MonoDialect::Modern,
80 MonoDialect::Legacy,
81 MonoDialect::July2002,
82 MonoDialect::Early2002,
83 MonoDialect::May2004,
84 ][(index / 5) % 5];
85 let extensions = if index % 2 == 0 {
86 table
87 } else {
88 ExtensionTable::EMPTY
89 };
90 total += exercise(&data, rng.next(), options, dialect, extensions);
91 }
92 total
93}
94
95fn append(data: &mut Vec<u8>, info: &OpCodeInfo, rng: &mut Rng) {
96 data.extend_from_slice(&info.encoding_bytes()[..usize::from(info.size)]);
97 let size = match info.operand {
98 OperandKind::InlineSwitch => {
99 let count = rng.next() as u32 % 32;
100 data.extend(count.to_le_bytes());
101 count as usize * 4
102 }
103 OperandKind::InlineData => {
104 let count = rng.next() as u32 % 32;
105 data.extend(if rng.next() % 8 == 0 { u32::MAX } else { count }.to_le_bytes());
106 count as usize
107 }
108 OperandKind::ShortInlineData => {
109 let count = rng.next() as u8 % 32;
110 data.push(count);
111 usize::from(count)
112 }
113 operand => operand.fixed_size().unwrap(),
114 };
115 data.extend((0..size).map(|_| rng.next() as u8));
116}
117
118pub fn exercise(
119 data: &[u8],
120 ip: u64,
121 options: DecoderOptions,
122 dialect: MonoDialect,
123 table: ExtensionTable<'_>,
124) -> usize {
125 let mut decoder = Decoder::with_extensions(data, ip, options, table).with_mono_dialect(dialect);
126 let mut count = 0;
127 while decoder.can_decode() {
128 let before = decoder.position();
129 let mut cloned = decoder.clone();
130 let instruction = decoder.decode();
131 let mut into = Instruction::default();
132 cloned.decode_into(&mut into);
133 assert_eq!(instruction, into);
134 assert!(!instruction.is_empty());
135 assert_eq!(instruction.offset(), before);
136 assert_eq!(instruction.ip(), ip.wrapping_add(before as u64));
137 assert_eq!(instruction.bytes(), &data[before..decoder.position()]);
138 assert_eq!(instruction.next_ip(), decoder.ip());
139 assert_eq!(instruction.error(), decoder.last_error());
140 match instruction.operand() {
141 Operand::Branch {
142 displacement,
143 target,
144 } => assert_eq!(
145 target,
146 instruction
147 .next_ip()
148 .wrapping_add(displacement as i64 as u64)
149 ),
150 Operand::Switch(targets) => {
151 assert_eq!(targets.iter().len(), targets.len());
152 for (slot, target) in targets.into_iter().enumerate() {
153 assert_eq!(targets.get(slot), Some(target));
154 }
155 assert_eq!(targets.get(targets.len()), None);
156 }
157 Operand::Bytes(payload) => assert!(instruction.bytes().ends_with(payload)),
158 _ => {}
159 }
160 let mut storage = [0u8; 64];
161 let mut sink = BufSink::new(&mut storage);
162 let _ = FmtFormatter::new().format(&instruction, &mut sink);
163 count += 1;
164 }
165 assert_eq!(decoder.position(), data.len());
166 assert_eq!(decoder.next(), None);
167 assert!(decoder.decode().is_empty());
168 let mut consumed = 0;
169 for group in Decoder::with_extensions(data, ip, options, table)
170 .with_mono_dialect(dialect)
171 .into_prefixed()
172 {
173 assert!(!group.is_empty());
174 assert_eq!(group.offset(), consumed);
175 assert_eq!(group.bytes(), &data[consumed..consumed + group.len()]);
176 assert_eq!(
177 group.prefix_bytes().len() + group.instruction.len(),
178 group.len()
179 );
180 let prefixes: usize = group
181 .prefixes()
182 .map(|prefix| {
183 assert!(prefix.is_prefix());
184 prefix.len()
185 })
186 .sum();
187 assert_eq!(prefixes, group.prefix_bytes().len());
188 let _ = group.validate_prefixes(PrefixValidationOptions::Ecma335);
189 let _ = group.validate_prefixes(PrefixValidationOptions::Runtime);
190 consumed += group.len();
191 }
192 assert_eq!(consumed, data.len());
193 count
194}