From cc449351ae015f947c81271106b990387dcd9d8a Mon Sep 17 00:00:00 2001 From: arvidn Date: Fri, 3 Jan 2025 16:26:42 +0100 Subject: [PATCH] extend serializer fuzzer, to ensure the serialized form is valid and round-trips correctly --- fuzz/fuzz_targets/node_eq.rs | 12 ++++++++++++ fuzz/fuzz_targets/serializer.rs | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 fuzz/fuzz_targets/node_eq.rs diff --git a/fuzz/fuzz_targets/node_eq.rs b/fuzz/fuzz_targets/node_eq.rs new file mode 100644 index 00000000..a4c87a2b --- /dev/null +++ b/fuzz/fuzz_targets/node_eq.rs @@ -0,0 +1,12 @@ +use clvmr::{Allocator, NodePtr, SExp}; + +/// compare two CLVM trees. Returns true if they are identical, false otherwise +pub fn node_eq(allocator: &Allocator, s1: NodePtr, s2: NodePtr) -> bool { + match (allocator.sexp(s1), allocator.sexp(s2)) { + (SExp::Pair(s1a, s1b), SExp::Pair(s2a, s2b)) => { + node_eq(allocator, s1a, s2a) && node_eq(allocator, s1b, s2b) + } + (SExp::Atom, SExp::Atom) => allocator.atom_eq(s1, s2), + _ => false, + } +} diff --git a/fuzz/fuzz_targets/serializer.rs b/fuzz/fuzz_targets/serializer.rs index aedba3d4..2e58a2e6 100644 --- a/fuzz/fuzz_targets/serializer.rs +++ b/fuzz/fuzz_targets/serializer.rs @@ -1,9 +1,11 @@ #![no_main] mod fuzzing_utils; +mod node_eq; use clvmr::allocator::Allocator; -use clvmr::serde::{node_to_bytes_backrefs, Serializer}; +use clvmr::serde::{node_from_bytes_backrefs, node_to_bytes_backrefs, Serializer}; +use node_eq::node_eq; use libfuzzer_sys::fuzz_target; @@ -22,9 +24,16 @@ fn do_fuzz(data: &[u8], short_atoms: bool) { assert!(done); let b2 = ser.into_inner(); - if b1 != b2 { - panic!("b1 and b2 do not match"); + { + // make sure both serializations are valid, and can be parsed to produce + // the same tree + let b1 = node_from_bytes_backrefs(&mut allocator, &b1).unwrap(); + let b2 = node_from_bytes_backrefs(&mut allocator, &b2).unwrap(); + assert!(node_eq(&allocator, b1, program)); + assert!(node_eq(&allocator, b1, b2)); } + + assert_eq!(b1, b2); } fuzz_target!(|data: &[u8]| {