|
| 1 | +#![no_main] |
| 2 | + |
| 3 | +mod make_tree; |
| 4 | +mod node_eq; |
| 5 | + |
| 6 | +use clvmr::allocator::{Allocator, NodePtr, SExp}; |
| 7 | +use clvmr::serde::node_from_bytes_backrefs; |
| 8 | +use clvmr::serde::write_atom::write_atom; |
| 9 | +use clvmr::serde::ReadCacheLookup; |
| 10 | +use clvmr::serde::{serialized_length, treehash, ObjectCache}; |
| 11 | +use std::io; |
| 12 | +use std::io::Cursor; |
| 13 | +use std::io::Write; |
| 14 | + |
| 15 | +use node_eq::node_eq; |
| 16 | + |
| 17 | +use libfuzzer_sys::fuzz_target; |
| 18 | + |
| 19 | +const BACK_REFERENCE: u8 = 0xfe; |
| 20 | +const CONS_BOX_MARKER: u8 = 0xff; |
| 21 | + |
| 22 | +#[derive(PartialEq, Eq)] |
| 23 | +enum ReadOp { |
| 24 | + Parse, |
| 25 | + Cons, |
| 26 | +} |
| 27 | + |
| 28 | +// make sure back-references returned by ReadCacheLookup are smaller than the |
| 29 | +// node they reference |
| 30 | +pub fn compare_back_references(allocator: &Allocator, node: NodePtr) -> io::Result<Vec<u8>> { |
| 31 | + let mut f = Cursor::new(Vec::new()); |
| 32 | + |
| 33 | + let mut read_op_stack: Vec<ReadOp> = vec![ReadOp::Parse]; |
| 34 | + let mut write_stack: Vec<NodePtr> = vec![node]; |
| 35 | + |
| 36 | + let mut read_cache_lookup = ReadCacheLookup::new(); |
| 37 | + |
| 38 | + let mut thc = ObjectCache::new(treehash); |
| 39 | + let mut slc = ObjectCache::new(serialized_length); |
| 40 | + |
| 41 | + while let Some(node_to_write) = write_stack.pop() { |
| 42 | + let op = read_op_stack.pop(); |
| 43 | + assert!(op == Some(ReadOp::Parse)); |
| 44 | + |
| 45 | + let node_serialized_length = *slc |
| 46 | + .get_or_calculate(allocator, &node_to_write, None) |
| 47 | + .expect("couldn't calculate serialized length"); |
| 48 | + let node_tree_hash = thc |
| 49 | + .get_or_calculate(allocator, &node_to_write, None) |
| 50 | + .expect("can't get treehash"); |
| 51 | + |
| 52 | + let result1 = read_cache_lookup.find_path(node_tree_hash, node_serialized_length); |
| 53 | + match result1 { |
| 54 | + Some(path) => { |
| 55 | + f.write_all(&[BACK_REFERENCE])?; |
| 56 | + write_atom(&mut f, &path)?; |
| 57 | + read_cache_lookup.push(*node_tree_hash); |
| 58 | + { |
| 59 | + // make sure the path is never encoded as more bytes than |
| 60 | + // the node we're referencing |
| 61 | + use std::io::Write; |
| 62 | + let mut temp = Cursor::new(Vec::<u8>::new()); |
| 63 | + temp.write_all(&[BACK_REFERENCE])?; |
| 64 | + write_atom(&mut temp, &path)?; |
| 65 | + let temp = temp.into_inner(); |
| 66 | + assert!(temp.len() < node_serialized_length as usize); |
| 67 | + } |
| 68 | + } |
| 69 | + None => match allocator.sexp(node_to_write) { |
| 70 | + SExp::Pair(left, right) => { |
| 71 | + f.write_all(&[CONS_BOX_MARKER])?; |
| 72 | + write_stack.push(right); |
| 73 | + write_stack.push(left); |
| 74 | + read_op_stack.push(ReadOp::Cons); |
| 75 | + read_op_stack.push(ReadOp::Parse); |
| 76 | + read_op_stack.push(ReadOp::Parse); |
| 77 | + } |
| 78 | + SExp::Atom => { |
| 79 | + let atom = allocator.atom(node_to_write); |
| 80 | + write_atom(&mut f, atom.as_ref())?; |
| 81 | + read_cache_lookup.push(*node_tree_hash); |
| 82 | + } |
| 83 | + }, |
| 84 | + } |
| 85 | + while let Some(ReadOp::Cons) = read_op_stack.last() { |
| 86 | + read_op_stack.pop(); |
| 87 | + read_cache_lookup.pop2_and_cons(); |
| 88 | + } |
| 89 | + } |
| 90 | + Ok(f.into_inner()) |
| 91 | +} |
| 92 | + |
| 93 | +// serializing with the regular compressed serializer should yield the same |
| 94 | +// result as using the incremental one (as long as it's in a single add() call). |
| 95 | +fuzz_target!(|data: &[u8]| { |
| 96 | + let mut unstructured = arbitrary::Unstructured::new(data); |
| 97 | + let mut allocator = Allocator::new(); |
| 98 | + let (program, _) = make_tree::make_tree(&mut allocator, &mut unstructured); |
| 99 | + |
| 100 | + let b1 = compare_back_references(&allocator, program).unwrap(); |
| 101 | + let b2 = node_from_bytes_backrefs(&mut allocator, &b1).unwrap(); |
| 102 | + assert!(node_eq(&allocator, b2, program)); |
| 103 | +}); |
0 commit comments