aboutsummaryrefslogtreecommitdiff
path: root/fuzz
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2024-06-23 17:15:39 -0700
committeriximeow <me@iximeow.net>2024-06-23 17:15:39 -0700
commitf3f9141def4a6281452b087a6b57a2c2c026ffb7 (patch)
tree92e19ec199d2557860ee6153cc4f92b89ec47cd1 /fuzz
parent9402dc008deaf993a3ac969f56af772cc545b8e8 (diff)
add fuzz target for larger write helpers
Diffstat (limited to 'fuzz')
-rw-r--r--fuzz/.gitignore3
-rw-r--r--fuzz/Cargo.toml25
-rw-r--r--fuzz/fuzz_targets/write_helpers_are_correct.rs96
3 files changed, 124 insertions, 0 deletions
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);
+ },
+ _ => {}
+ }
+});