From 2f465f6682d22abf81660af8c5bd10fcce5d988a Mon Sep 17 00:00:00 2001 From: misha Date: Sat, 26 Apr 2025 23:00:24 +0300 Subject: [PATCH] New fuzzer for strongly typed data structures --- fuzz/Cargo.toml | 7 +++ fuzz/fuzz_targets/strongly_typed.rs | 91 +++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 fuzz/fuzz_targets/strongly_typed.rs diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 5fe38b679..cd637622d 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -11,9 +11,16 @@ cargo-fuzz = true [dependencies] libfuzzer-sys = "0.4" serde_json = { path = ".." } +serde = { version = "1.0.194", features = ["derive"] } [[bin]] name = "from_slice" path = "fuzz_targets/from_slice.rs" test = false doc = false + +[[bin]] +name = "strongly_typed" +path = "fuzz_targets/strongly_typed.rs" +test = false +doc = false diff --git a/fuzz/fuzz_targets/strongly_typed.rs b/fuzz/fuzz_targets/strongly_typed.rs new file mode 100644 index 000000000..1b0e96992 --- /dev/null +++ b/fuzz/fuzz_targets/strongly_typed.rs @@ -0,0 +1,91 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use serde::{Deserialize, Serialize}; +use serde_json::{from_slice, from_value, to_string}; +use std::collections::HashMap; + +#[derive(Serialize, Deserialize)] +enum Enum { + A(u32), + B(i32, i32), + C { x: i32, y: i32 }, + U, +} + +#[derive(Serialize, Deserialize)] +struct WrappedInt(i32); + +#[derive(Serialize, Deserialize)] +struct Numbers { + a: Option, + b: Option, + c: Option, + d: Option, + e: Option, + f: Option, + g: Option, + h: Option, + i: Option, + j: Option, + k: Option, + l: Option, +} + +#[derive(Serialize, Deserialize)] +struct CommonTypes { + a: Option, + b: Option, + c: Option, +} + +#[derive(Serialize, Deserialize)] +struct Arrays { + a: Option>, + b: Option<[u8; 3]>, +} + +#[derive(Serialize, Deserialize)] +struct Maps { + a: Option>, + b: Option>, + c: Option>, + d: Option>, + e: Option>, + f: Option>, + g: Option>, + h: Option>, + i: Option>, + j: Option>, + k: Option>, + l: Option>, + o: Option>, +} + +#[derive(Serialize, Deserialize)] +struct Others { + a: Option<(f32, char, i8)>, + b: Option, + c: Option, + d: Option, + e: Option, +} + +#[derive(Deserialize, Serialize)] +struct Data { + a: Option, + b: Option, + c: Option, + d: Option, + e: Option, +} + +fuzz_target!(|data: &[u8]| { + if let Ok(d) = from_slice::(data) { + let _ = to_string(&d); + } + + if let Ok(value) = from_slice(data) { + let _ = from_value::(value); + } +});