Skip to content

Commit

Permalink
Run the echo fuzzer in the CI
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvestre committed Nov 30, 2023
1 parent 66e0835 commit 045dd57
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/fuzzing.yml
Expand Up @@ -42,6 +42,7 @@ jobs:
- { name: fuzz_date, should_pass: false }
- { name: fuzz_expr, should_pass: true }
- { name: fuzz_printf, should_pass: false }
- { name: fuzz_echo, should_pass: false }
- { name: fuzz_parse_glob, should_pass: true }
- { name: fuzz_parse_size, should_pass: true }
- { name: fuzz_parse_time, should_pass: true }
Expand Down
21 changes: 8 additions & 13 deletions fuzz/fuzz_targets/fuzz_echo.rs
@@ -1,6 +1,6 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use uu_echo::uumain; // Changed from uu_printf to uu_echo
use uu_echo::uumain;

use rand::prelude::SliceRandom;
use rand::Rng;
Expand All @@ -12,7 +12,7 @@ use crate::fuzz_common::{
compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd,
};

static CMD_PATH: &str = "/usr/bin/echo"; // Changed from "printf" to "echo"
static CMD_PATH: &str = "/usr/bin/echo";

fn generate_echo() -> String {
let mut rng = rand::thread_rng();
Expand All @@ -22,7 +22,6 @@ fn generate_echo() -> String {
let include_n = rng.gen_bool(0.1); // 10% chance
let include_e = rng.gen_bool(0.1); // 10% chance
let include_E = rng.gen_bool(0.1); // 10% chance
// --help and --version are typically not included in fuzzing as they don't change output format

if include_n {
echo_str.push_str("-n ");
Expand All @@ -41,29 +40,25 @@ fn generate_echo() -> String {
if include_e {
// Add a 10% chance of including an escape sequence
if rng.gen_bool(0.1) {
echo_str.push_str(&generate_escape_sequence(&mut rng)); // This function should handle echo-specific sequences
echo_str.push_str(&generate_escape_sequence(&mut rng));
}
}

echo_str
}

// You should also modify the generate_escape_sequence function to include echo-specific sequences
fn generate_escape_sequence(rng: &mut impl Rng) -> String {
let escape_sequences = [
"\\\\", "\\a", "\\b", "\\c", "\\e", "\\f", "\\n", "\\r", "\\t", "\\v",
"\\0NNN", // You can randomly generate NNN
"\\xHH", // You can randomly generate HH
// ... other sequences
"\\\\", "\\a", "\\b", "\\c", "\\e", "\\f", "\\n", "\\r", "\\t", "\\v", "\\0NNN", "\\xHH",
];
escape_sequences.choose(rng).unwrap().to_string()
}

fuzz_target!(|_data: &[u8]| {
let echo_input = generate_echo(); // Changed from generate_printf to generate_echo
let mut args = vec![OsString::from("echo")]; // Changed from "printf" to "echo"
let echo_input = generate_echo();
let mut args = vec![OsString::from("echo")];
args.extend(echo_input.split_whitespace().map(OsString::from));
let rust_result = generate_and_run_uumain(&args, uumain); // uumain function from uu_echo
let rust_result = generate_and_run_uumain(&args, uumain);

let gnu_result = match run_gnu_cmd(CMD_PATH, &args[1..], false) {
Ok(result) => result,
Expand All @@ -88,6 +83,6 @@ fuzz_target!(|_data: &[u8]| {
&gnu_result.stderr,
rust_result.exit_code,
gnu_result.exit_code,
false, // Set to true if you want to fail on stderr diff
true,
);
});

0 comments on commit 045dd57

Please sign in to comment.