Skip to content

Commit 68d87a2

Browse files
authored
Merge pull request #526 from Chia-Network/extend-fuzzer
extend serializer fuzzer
2 parents 709ecc7 + cc44935 commit 68d87a2

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

fuzz/fuzz_targets/node_eq.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use clvmr::{Allocator, NodePtr, SExp};
2+
3+
/// compare two CLVM trees. Returns true if they are identical, false otherwise
4+
pub fn node_eq(allocator: &Allocator, s1: NodePtr, s2: NodePtr) -> bool {
5+
match (allocator.sexp(s1), allocator.sexp(s2)) {
6+
(SExp::Pair(s1a, s1b), SExp::Pair(s2a, s2b)) => {
7+
node_eq(allocator, s1a, s2a) && node_eq(allocator, s1b, s2b)
8+
}
9+
(SExp::Atom, SExp::Atom) => allocator.atom_eq(s1, s2),
10+
_ => false,
11+
}
12+
}

fuzz/fuzz_targets/serializer.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#![no_main]
22

33
mod fuzzing_utils;
4+
mod node_eq;
45

56
use clvmr::allocator::Allocator;
6-
use clvmr::serde::{node_to_bytes_backrefs, Serializer};
7+
use clvmr::serde::{node_from_bytes_backrefs, node_to_bytes_backrefs, Serializer};
8+
use node_eq::node_eq;
79

810
use libfuzzer_sys::fuzz_target;
911

@@ -22,9 +24,16 @@ fn do_fuzz(data: &[u8], short_atoms: bool) {
2224
assert!(done);
2325
let b2 = ser.into_inner();
2426

25-
if b1 != b2 {
26-
panic!("b1 and b2 do not match");
27+
{
28+
// make sure both serializations are valid, and can be parsed to produce
29+
// the same tree
30+
let b1 = node_from_bytes_backrefs(&mut allocator, &b1).unwrap();
31+
let b2 = node_from_bytes_backrefs(&mut allocator, &b2).unwrap();
32+
assert!(node_eq(&allocator, b1, program));
33+
assert!(node_eq(&allocator, b1, b2));
2734
}
35+
36+
assert_eq!(b1, b2);
2837
}
2938

3039
fuzz_target!(|data: &[u8]| {

0 commit comments

Comments
 (0)