Skip to content

Commit 96b8d08

Browse files
committed
fuzz: Move main toward the top of the file
1 parent ee2f66d commit 96b8d08

1 file changed

Lines changed: 56 additions & 56 deletions

File tree

fuzz/src/main.rs

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,62 @@ enum Commands {
7878
},
7979
}
8080

81+
fn main() {
82+
let cli_args = Args::parse();
83+
84+
if let Some(cmd) = &cli_args.command {
85+
match cmd {
86+
Commands::Check { file } => {
87+
let mut buf = Vec::new();
88+
let reader: &mut dyn Read = match file {
89+
Some(fname) if fname == "-" => &mut io::stdin(),
90+
Some(fname) => &mut fs::File::open(fname).unwrap(),
91+
None => &mut io::stdin(),
92+
};
93+
reader.read_to_end(&mut buf).unwrap();
94+
match decode_eval_check(&buf, &cli_args, false) {
95+
Ok(()) => (),
96+
Err(Error::Decode(e)) => println!("error: {e} (no panic raised)"),
97+
Err(Error::Check(e)) => panic!("check error: {e}"),
98+
}
99+
}
100+
Commands::Decode { files } => run_decode_subcmd(files, &cli_args),
101+
Commands::Bruteforce { .. } => exhaustive::run_for_all_floats(&cli_args),
102+
}
103+
return;
104+
}
105+
106+
// HACK(eddyb) `#[cfg(fuzzing)] {...}` used instead of `if cfg!(fuzzing) {...}`
107+
// because the latter can still cause the `afl` crate to be linked, and it
108+
// depends on native libraries that are only available under `cargo afl ...`.
109+
#[cfg(fuzzing)]
110+
if true {
111+
// FIXME(eddyb) make the first argument (panic hook choice) a CLI toggle.
112+
afl::fuzz(true, |buf| {
113+
// Discard decoding errors
114+
decode_eval_check(&buf, &cli_args, false).unwrap();
115+
});
116+
117+
return;
118+
}
119+
120+
// FIXME(eddyb) add better docs for all of this.
121+
// FIXME(eddyb) add `seed` subcommand using `FuzzOp::encode_into`, and a set
122+
// of basic examples, e.g. every `FuzzOp` variant with `0.0` for all inputs
123+
// (and/or maybe testcases from known and/or fixed bugs, too).
124+
Args::command().print_long_help().unwrap();
125+
eprintln!(
126+
"\n\
127+
To fuzz `rustc_apfloat`, you must use `cargo afl`:\n\
128+
- `cargo install afl`\n\
129+
- build with `cargo afl build -p rustc_apfloat-fuzz --release`\n\
130+
- seed with `mkdir fuzz/in-foo && echo > fuzz/in-foo/empty`\n\
131+
- run with `cargo afl fuzz -i fuzz/in-foo -o fuzz/out-foo target/release/rustc_apfloat-fuzz`\n\
132+
"
133+
);
134+
std::process::exit(1);
135+
}
136+
81137
/// Trait implemented for types that describe a floating-point format supported
82138
/// by `rustc_apfloat`, but which themselves only carry the binary representation
83139
/// (instead of `rustc_apfloat` types or native/hardware floating-point types).
@@ -302,62 +358,6 @@ float_reprs! {
302358

303359
pub(crate) use for_each_repr;
304360

305-
fn main() {
306-
let cli_args = Args::parse();
307-
308-
if let Some(cmd) = &cli_args.command {
309-
match cmd {
310-
Commands::Check { file } => {
311-
let mut buf = Vec::new();
312-
let reader: &mut dyn Read = match file {
313-
Some(fname) if fname == "-" => &mut io::stdin(),
314-
Some(fname) => &mut fs::File::open(fname).unwrap(),
315-
None => &mut io::stdin(),
316-
};
317-
reader.read_to_end(&mut buf).unwrap();
318-
match decode_eval_check(&buf, &cli_args, false) {
319-
Ok(()) => (),
320-
Err(Error::Decode(e)) => println!("error: {e} (no panic raised)"),
321-
Err(Error::Check(e)) => panic!("check error: {e}"),
322-
}
323-
}
324-
Commands::Decode { files } => run_decode_subcmd(files, &cli_args),
325-
Commands::Bruteforce { .. } => exhaustive::run_for_all_floats(&cli_args),
326-
}
327-
return;
328-
}
329-
330-
// HACK(eddyb) `#[cfg(fuzzing)] {...}` used instead of `if cfg!(fuzzing) {...}`
331-
// because the latter can still cause the `afl` crate to be linked, and it
332-
// depends on native libraries that are only available under `cargo afl ...`.
333-
#[cfg(fuzzing)]
334-
if true {
335-
// FIXME(eddyb) make the first argument (panic hook choice) a CLI toggle.
336-
afl::fuzz(true, |buf| {
337-
// Discard decoding errors
338-
decode_eval_check(&buf, &cli_args, false).unwrap();
339-
});
340-
341-
return;
342-
}
343-
344-
// FIXME(eddyb) add better docs for all of this.
345-
// FIXME(eddyb) add `seed` subcommand using `FuzzOp::encode_into`, and a set
346-
// of basic examples, e.g. every `FuzzOp` variant with `0.0` for all inputs
347-
// (and/or maybe testcases from known and/or fixed bugs, too).
348-
Args::command().print_long_help().unwrap();
349-
eprintln!(
350-
"\n\
351-
To fuzz `rustc_apfloat`, you must use `cargo afl`:\n\
352-
- `cargo install afl`\n\
353-
- build with `cargo afl build -p rustc_apfloat-fuzz --release`\n\
354-
- seed with `mkdir fuzz/in-foo && echo > fuzz/in-foo/empty`\n\
355-
- run with `cargo afl fuzz -i fuzz/in-foo -o fuzz/out-foo target/release/rustc_apfloat-fuzz`\n\
356-
"
357-
);
358-
std::process::exit(1);
359-
}
360-
361361
/// Errors from improperly formed inputs that cause an exit from the fuzzer but do not raise
362362
/// a test failing error.
363363
#[derive(Clone, Copy, Debug)]

0 commit comments

Comments
 (0)