From f3f9141def4a6281452b087a6b57a2c2c026ffb7 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sun, 23 Jun 2024 17:15:39 -0700 Subject: add fuzz target for larger write helpers --- fuzz/.gitignore | 3 + fuzz/Cargo.toml | 25 +++++++ fuzz/fuzz_targets/write_helpers_are_correct.rs | 96 ++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 fuzz/.gitignore create mode 100644 fuzz/Cargo.toml create mode 100644 fuzz/fuzz_targets/write_helpers_are_correct.rs diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 0000000..a092511 --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,3 @@ +target +corpus +artifacts diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 0000000..67ffa43 --- /dev/null +++ b/fuzz/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "yaxpeax-arch-fuzz" +version = "0.0.0" +authors = ["Automatically generated"] +publish = false +edition = "2018" + +[package.metadata] +cargo-fuzz = true + +[dependencies] +libfuzzer-sys = "0.4" + +[dependencies.yaxpeax-arch] +path = ".." + +# Prevent this from interfering with workspaces +[workspace] +members = ["."] + +[[bin]] +name = "write_helpers_are_correct" +path = "fuzz_targets/write_helpers_are_correct.rs" +test = false +doc = false diff --git a/fuzz/fuzz_targets/write_helpers_are_correct.rs b/fuzz/fuzz_targets/write_helpers_are_correct.rs new file mode 100644 index 0000000..41e27bd --- /dev/null +++ b/fuzz/fuzz_targets/write_helpers_are_correct.rs @@ -0,0 +1,96 @@ +#![no_main] +use libfuzzer_sys::fuzz_target; +use yaxpeax_arch::display::DisplaySink; + +use std::convert::TryInto; + +fuzz_target!(|data: &[u8]| { + let mut buf = String::new(); + match data.len() { + 1 => { + let i = data[0]; + + buf.clear(); + buf.write_u8(i).expect("write succeeds"); + assert_eq!(buf, format!("{:x}", i)); + + buf.clear(); + buf.write_prefixed_u8(i).expect("write succeeds"); + assert_eq!(buf, format!("0x{:x}", i)); + + let expected = if (i as i8) < 0 { + format!("-0x{:x}", (i as i8).unsigned_abs()) + } else { + format!("0x{:x}", i) + }; + + buf.clear(); + buf.write_prefixed_i8(i as i8).expect("write succeeds"); + assert_eq!(buf, expected); + }, + 2 => { + let i: u16 = u16::from_le_bytes(data.try_into().expect("checked the size is right")); + + buf.clear(); + buf.write_u16(i).expect("write succeeds"); + assert_eq!(buf, format!("{:x}", i)); + + buf.clear(); + buf.write_prefixed_u16(i).expect("write succeeds"); + assert_eq!(buf, format!("0x{:x}", i)); + + let expected = if (i as i16) < 0 { + format!("-0x{:x}", (i as i16).unsigned_abs()) + } else { + format!("0x{:x}", i) + }; + + buf.clear(); + buf.write_prefixed_i16(i as i16).expect("write succeeds"); + assert_eq!(buf, expected); + } + 4 => { + let i: u32 = u32::from_le_bytes(data.try_into().expect("checked the size is right")); + + buf.clear(); + buf.write_u32(i).expect("write succeeds"); + assert_eq!(buf, format!("{:x}", i)); + + buf.clear(); + buf.write_prefixed_u32(i).expect("write succeeds"); + assert_eq!(buf, format!("0x{:x}", i)); + + let expected = if (i as i32) < 0 { + format!("-0x{:x}", (i as i32).unsigned_abs()) + } else { + format!("0x{:x}", i) + }; + + buf.clear(); + buf.write_prefixed_i32(i as i32).expect("write succeeds"); + assert_eq!(buf, expected); + }, + 8 => { + let i: u64 = u64::from_le_bytes(data.try_into().expect("checked the size is right")); + + buf.clear(); + buf.write_u64(i).expect("write succeeds"); + assert_eq!(buf, format!("{:x}", i)); + + buf.clear(); + buf.write_prefixed_u64(i).expect("write succeeds"); + assert_eq!(buf, format!("0x{:x}", i)); + + let expected = if (i as i64) < 0 { + format!("-0x{:x}", (i as i64).unsigned_abs()) + } else { + format!("0x{:x}", i) + }; + + buf.clear(); + buf.write_prefixed_i64(i as i64).expect("write succeeds"); + assert_eq!(buf, expected); + }, + _ => {} + } +}); -- cgit v1.1