Skip to content

Commit

Permalink
Merge pull request #526 from Chia-Network/extend-fuzzer
Browse files Browse the repository at this point in the history
extend serializer fuzzer
  • Loading branch information
arvidn authored Jan 3, 2025
2 parents 709ecc7 + cc44935 commit 68d87a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 12 additions & 0 deletions fuzz/fuzz_targets/node_eq.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
15 changes: 12 additions & 3 deletions fuzz/fuzz_targets/serializer.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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]| {
Expand Down

0 comments on commit 68d87a2

Please sign in to comment.