| 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_printf::uumain; |
| 11 |
|
| 12 |
use rand::RngExt; |
| 13 |
use rand::seq::IndexedRandom; |
| 14 |
use std::env; |
| 15 |
use std::ffi::OsString; |
| 16 |
|
| 17 |
use uufuzz::CommandResult; |
| 18 |
use uufuzz::{compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd}; |
| 19 |
|
| 20 |
static CMD_PATH: &str = "printf"; |
| 21 |
|
| 22 |
fn generate_escape_sequence(rng: &mut impl RngExt) -> String { |
| 23 |
let escape_sequences = [ |
| 24 |
"\\\"", |
| 25 |
"\\\\", |
| 26 |
"\\a", |
| 27 |
"\\b", |
| 28 |
"\\c", |
| 29 |
"\\e", |
| 30 |
"\\f", |
| 31 |
"\\n", |
| 32 |
"\\r", |
| 33 |
"\\t", |
| 34 |
"\\v", |
| 35 |
"\\000", |
| 36 |
"\\x00", |
| 37 |
"\\u0000", |
| 38 |
"\\U00000000", |
| 39 |
"%%", |
| 40 |
]; |
| 41 |
escape_sequences.choose(rng).unwrap().to_string() |
| 42 |
} |
| 43 |
|
| 44 |
fn generate_printf() -> String { |
| 45 |
let mut rng = rand::rng(); |
| 46 |
let format_specifiers = ["%s", "%d", "%f", "%x", "%o", "%c", "%b", "%q"]; |
| 47 |
let mut printf_str = String::new(); |
| 48 |
// Add a 20% chance of generating an invalid format specifier |
| 49 |
if rng.random_bool(0.2) { |
| 50 |
printf_str.push_str("%z"); // Invalid format specifier |
| 51 |
} else { |
| 52 |
let specifier = *format_specifiers.choose(&mut rng).unwrap(); |
| 53 |
printf_str.push_str(specifier); |
| 54 |
|
| 55 |
// Add a 20% chance of introducing complex format strings |
| 56 |
if rng.random_bool(0.2) { |
| 57 |
printf_str.push_str(&format!(" %{}", rng.random_range(1..=1000))); |
| 58 |
} else { |
| 59 |
// Add a random string or number after the specifier |
| 60 |
if specifier == "%s" { |
| 61 |
printf_str.push_str(&format!( |
| 62 |
" {}", |
| 63 |
generate_random_string(rng.random_range(1..=10)) |
| 64 |
)); |
| 65 |
} else { |
| 66 |
printf_str.push_str(&format!(" {}", rng.random_range(1..=1000))); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// Add a 10% chance of including an escape sequence |
| 72 |
if rng.random_bool(0.1) { |
| 73 |
printf_str.push_str(&generate_escape_sequence(&mut rng)); |
| 74 |
} |
| 75 |
printf_str |
| 76 |
} |
| 77 |
|
| 78 |
fuzz_target!(|_data: &[u8]| { |
| 79 |
let printf_input = generate_printf(); |
| 80 |
let mut args = vec![OsString::from("printf")]; |
| 81 |
args.extend(printf_input.split_whitespace().map(OsString::from)); |
| 82 |
let rust_result = generate_and_run_uumain(&args, uumain, None); |
| 83 |
|
| 84 |
// TODO remove once uutils printf supports localization |
| 85 |
unsafe { |
| 86 |
env::set_var("LC_ALL", "C"); |
| 87 |
} |
| 88 |
let gnu_result = match run_gnu_cmd(CMD_PATH, &args[1..], false, None) { |
| 89 |
Ok(result) => result, |
| 90 |
Err(error_result) => { |
| 91 |
eprintln!("Failed to run GNU command:"); |
| 92 |
eprintln!("Stderr: {}", error_result.stderr); |
| 93 |
eprintln!("Exit Code: {}", error_result.exit_code); |
| 94 |
CommandResult { |
| 95 |
stdout: String::new(), |
| 96 |
stderr: error_result.stderr, |
| 97 |
exit_code: error_result.exit_code, |
| 98 |
} |
| 99 |
} |
| 100 |
}; |
| 101 |
|
| 102 |
compare_result( |
| 103 |
"printf", |
| 104 |
&format!("{:?}", &args[1..]), |
| 105 |
None, |
| 106 |
&rust_result, |
| 107 |
&gnu_result, |
| 108 |
false, // Set to true if you want to fail on stderr diff |
| 109 |
); |
| 110 |
}); |