| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore parens |
| 7 |
|
| 8 |
#![no_main] |
| 9 |
use libfuzzer_sys::fuzz_target; |
| 10 |
use uu_wc::uumain; |
| 11 |
|
| 12 |
use rand::RngExt; |
| 13 |
use std::ffi::OsString; |
| 14 |
|
| 15 |
use uufuzz::{ |
| 16 |
CommandResult, compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd, |
| 17 |
}; |
| 18 |
static CMD_PATH: &str = "wc"; |
| 19 |
|
| 20 |
fn generate_wc_args() -> String { |
| 21 |
let mut rng = rand::rng(); |
| 22 |
let arg_count = rng.random_range(1..=6); |
| 23 |
let mut args = Vec::new(); |
| 24 |
|
| 25 |
for _ in 0..arg_count { |
| 26 |
// Introduce a chance to add invalid arguments |
| 27 |
if rng.random_bool(0.1) { |
| 28 |
args.push(generate_random_string(rng.random_range(1..=20))); |
| 29 |
} else { |
| 30 |
match rng.random_range(0..=5) { |
| 31 |
0 => args.push(String::from("-c")), |
| 32 |
1 => args.push(String::from("-m")), |
| 33 |
2 => args.push(String::from("-l")), |
| 34 |
3 => args.push(String::from("-L")), |
| 35 |
4 => args.push(String::from("-w")), |
| 36 |
// TODO |
| 37 |
5 => { |
| 38 |
args.push(String::from("--files0-from")); |
| 39 |
if rng.random_bool(0.5) { |
| 40 |
args.push(generate_random_string(50)); // Longer invalid file name |
| 41 |
} else { |
| 42 |
args.push(generate_random_string(5)); |
| 43 |
} |
| 44 |
} |
| 45 |
_ => (), |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
args.join(" ") |
| 51 |
} |
| 52 |
|
| 53 |
/// Function to generate a random string of lines, including invalid ones |
| 54 |
fn generate_random_lines(count: usize) -> String { |
| 55 |
let mut rng = rand::rng(); |
| 56 |
let mut lines = Vec::new(); |
| 57 |
|
| 58 |
for _ in 0..count { |
| 59 |
if rng.random_bool(0.1) { |
| 60 |
lines.push(generate_random_string(rng.random_range(1000..=5000))); // Very long invalid line |
| 61 |
} else { |
| 62 |
lines.push(generate_random_string(rng.random_range(1..=20))); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
lines.join("\n") |
| 67 |
} |
| 68 |
|
| 69 |
fuzz_target!(|_data: &[u8]| { |
| 70 |
let wc_args = generate_wc_args(); |
| 71 |
let mut args = vec![OsString::from("wc")]; |
| 72 |
args.extend(wc_args.split_whitespace().map(OsString::from)); |
| 73 |
|
| 74 |
let input_lines = generate_random_lines(10); |
| 75 |
|
| 76 |
let rust_result = generate_and_run_uumain(&args, uumain, Some(&input_lines)); |
| 77 |
let gnu_result = match run_gnu_cmd(CMD_PATH, &args[1..], false, Some(&input_lines)) { |
| 78 |
Ok(result) => result, |
| 79 |
Err(error_result) => { |
| 80 |
eprintln!("Failed to run GNU command:"); |
| 81 |
eprintln!("Stderr: {}", error_result.stderr); |
| 82 |
eprintln!("Exit Code: {}", error_result.exit_code); |
| 83 |
CommandResult { |
| 84 |
stdout: String::new(), |
| 85 |
stderr: error_result.stderr, |
| 86 |
exit_code: error_result.exit_code, |
| 87 |
} |
| 88 |
} |
| 89 |
}; |
| 90 |
|
| 91 |
compare_result( |
| 92 |
"wc", |
| 93 |
&format!("{:?}", &args[1..]), |
| 94 |
Some(&input_lines), |
| 95 |
&rust_result, |
| 96 |
&gnu_result, |
| 97 |
false, // Set to true if you want to fail on stderr diff |
| 98 |
); |
| 99 |
}); |