|
| 1 | +use std::fmt::{self, Write as _}; |
| 2 | +use std::fs; |
| 3 | +use std::io::Write; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +use num_traits::ToPrimitive; |
| 7 | +use rustc_apfloat::{Float, Round}; |
| 8 | + |
| 9 | +use crate::{Args, Error, FloatRepr, Op, decode_eval_check, for_each_repr, round_to_u8}; |
| 10 | + |
| 11 | +const ROUND_ALL: &[Round] = &[ |
| 12 | + Round::NearestTiesToEven, |
| 13 | + Round::TowardPositive, |
| 14 | + Round::TowardNegative, |
| 15 | + Round::TowardZero, |
| 16 | + Round::NearestTiesToAway, |
| 17 | +]; |
| 18 | + |
| 19 | +/// Create baseline inputs for the fuzzer to start with. It will start applying mutations to these |
| 20 | +/// inputs. |
| 21 | +/// |
| 22 | +/// This creates the cartesian product of the following: |
| 23 | +/// |
| 24 | +/// * All float types |
| 25 | +/// * All operations |
| 26 | +/// * All rounding modes |
| 27 | +/// * A small list of possible inputs, applied to each argument. |
| 28 | +/// |
| 29 | +/// This creates a _lot_ of files, so running `cmin` after helps give the fuzzer less input to |
| 30 | +/// work with. |
| 31 | +pub fn generate(dir: &Path) { |
| 32 | + fs::create_dir_all(&dir).unwrap(); |
| 33 | + let mut total = 0; |
| 34 | + |
| 35 | + for_each_repr!(for F in all_reprs!() { |
| 36 | + let count = gen_for_f::<F>(dir); |
| 37 | + total += count; |
| 38 | + }); |
| 39 | + |
| 40 | + eprintln!("wrote {total} total files to `{}`", dir.display()); |
| 41 | + eprintln!("note that it is recommended to run `cargo afl cmin` (`just gen` handles this)"); |
| 42 | +} |
| 43 | + |
| 44 | +fn gen_for_f<F: FloatRepr>(dir: &Path) -> u64 { |
| 45 | + let mut buf = Vec::new(); |
| 46 | + let mut name = String::new(); |
| 47 | + |
| 48 | + // There is no `ONE` constant, so this works. |
| 49 | + let one = (F::RustcApFloat::SMALLEST / F::RustcApFloat::SMALLEST).value; |
| 50 | + let inputs = [ |
| 51 | + F::RustcApFloat::ZERO, |
| 52 | + F::RustcApFloat::INFINITY, |
| 53 | + -F::RustcApFloat::ZERO, |
| 54 | + -F::RustcApFloat::INFINITY, |
| 55 | + F::RustcApFloat::qnan(None), |
| 56 | + F::RustcApFloat::snan(None), |
| 57 | + F::RustcApFloat::largest(), |
| 58 | + F::RustcApFloat::SMALLEST, |
| 59 | + F::RustcApFloat::smallest_normalized(), |
| 60 | + one, |
| 61 | + ]; |
| 62 | + let mut count = 0; |
| 63 | + let flt_name = F::short_lowercase_name(); |
| 64 | + |
| 65 | + // We don't need to test anything here, just use `cli_args` for config. |
| 66 | + let mut cli_args = Args::default(); |
| 67 | + cli_args.ignore_cxx = true; |
| 68 | + cli_args.ignore_hard = true; |
| 69 | + |
| 70 | + for op in Op::ALL.iter().copied() { |
| 71 | + for rm in ROUND_ALL.iter().copied() { |
| 72 | + let mut write_one = |a: F, b: F, c: F, input_desc: fmt::Arguments| { |
| 73 | + buf.clear(); |
| 74 | + name.clear(); |
| 75 | + |
| 76 | + write!(name, "{flt_name}-{op:?}-{rm:?}-{input_desc}").unwrap(); |
| 77 | + |
| 78 | + buf.push(F::KIND.to_u8().unwrap()); |
| 79 | + buf.push(op.to_u8().unwrap()); |
| 80 | + buf.push(round_to_u8(rm)); |
| 81 | + |
| 82 | + for arg in [a, b, c].iter().take(op.airity() as usize) { |
| 83 | + arg.write_as_le_bytes_into(&mut buf); |
| 84 | + } |
| 85 | + |
| 86 | + // Verify that the created input parses correctly. We don't need to do the |
| 87 | + // evaluation check, running the fuzzer will handle that. |
| 88 | + match decode_eval_check(&buf, &cli_args, false) { |
| 89 | + Ok(()) | Err(Error::Check(_)) => (), |
| 90 | + Err(Error::Decode(e)) => panic!("error decoding: {e}"), |
| 91 | + } |
| 92 | + |
| 93 | + let mut f = fs::OpenOptions::new() |
| 94 | + .create(true) |
| 95 | + .write(true) |
| 96 | + .truncate(true) |
| 97 | + .open(dir.join(&name)) |
| 98 | + .unwrap(); |
| 99 | + f.write_all(&mut buf).unwrap(); |
| 100 | + count += 1; |
| 101 | + }; |
| 102 | + |
| 103 | + let airity = op.airity() as u8; |
| 104 | + for (ai, a) in inputs.iter().enumerate() { |
| 105 | + if airity == 1 { |
| 106 | + write_one( |
| 107 | + F::from_ap(*a), |
| 108 | + F::from_bits_u128(0), |
| 109 | + F::from_bits_u128(0), |
| 110 | + format_args!("{ai}"), |
| 111 | + ) |
| 112 | + } else { |
| 113 | + for (bi, b) in inputs.iter().enumerate() { |
| 114 | + if airity == 2 { |
| 115 | + write_one( |
| 116 | + F::from_ap(*a), |
| 117 | + F::from_ap(*b), |
| 118 | + F::from_bits_u128(0), |
| 119 | + format_args!("{ai}-{bi}"), |
| 120 | + ) |
| 121 | + } else { |
| 122 | + assert_eq!(airity, 3); |
| 123 | + for (ci, c) in inputs.iter().enumerate() { |
| 124 | + write_one( |
| 125 | + F::from_ap(*a), |
| 126 | + F::from_ap(*b), |
| 127 | + F::from_ap(*c), |
| 128 | + format_args!("{ai}-{bi}-{ci}"), |
| 129 | + ) |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + eprintln!("{flt_name}: wrote {count} files"); |
| 139 | + count |
| 140 | +} |
0 commit comments