Skip to content

Commit

Permalink
Fuzz for compatibility with bincode v1 (#498)
Browse files Browse the repository at this point in the history
* Fuzz for compatibility with bincode v1

* Make AllTypes recursive

* Revert round trip test (add recursion to it too)

* Update fuzzer lockfile

* Adjust compat fuzzer to be stricter

* data doesn't need to be a &&[u8]
  • Loading branch information
5225225 authored Feb 7, 2022
1 parent 2e16e13 commit 3127ff5
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 9 deletions.
72 changes: 69 additions & 3 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
bincodev1 = {package = "bincode", version = "1.3.3"}
serde = { version = "1.0.135", features = ["derive"] }

[dependencies.bincode]
path = ".."
Expand All @@ -23,3 +25,9 @@ name = "roundtrip"
path = "fuzz_targets/roundtrip.rs"
test = false
doc = false

[[bin]]
name = "compat"
path = "fuzz_targets/compat.rs"
test = false
doc = false
55 changes: 55 additions & 0 deletions fuzz/fuzz_targets/compat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::ffi::CString;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use std::num::{NonZeroI128, NonZeroI32, NonZeroU128, NonZeroU32};
use std::path::PathBuf;
use std::time::{Duration, SystemTime};

#[derive(bincode::Decode, bincode::Encode, PartialEq, Debug, serde::Serialize, serde::Deserialize, Eq, PartialOrd, Ord)]
enum AllTypes {
BTreeMap(BTreeMap<u8, AllTypes>),
BTreeSet(BTreeSet<AllTypes>),
VecDeque(VecDeque<AllTypes>),
Vec(Vec<u8>),
String(String),
Box(Box<u8>),
BoxSlice(Box<[u8]>),
CString(CString),
SystemTime(SystemTime),
Duration(Duration),
PathBuf(PathBuf),
IpAddr(IpAddr),
Ipv4Addr(Ipv4Addr),
Ipv6Addr(Ipv6Addr),
SocketAddr(SocketAddr),
SocketAddrV4(SocketAddrV4),
SocketAddrV6(SocketAddrV6),
NonZeroU32(NonZeroU32),
NonZeroI32(NonZeroI32),
NonZeroU128(NonZeroU128),
NonZeroI128(NonZeroI128),
I128(i128),
I8(i8),
U128(u128),
U8(u8),
// Cow(Cow<'static, [u8]>), Blocked, see comment on decode
}

fuzz_target!(|data: &[u8]| {
let config = bincode::config::legacy().with_limit::<1024>();
#[allow(deprecated)]
let mut configv1 = bincodev1::config();
configv1.limit(1024);
let bincode_v1: Result<AllTypes, _> = configv1.deserialize(data);
let bincode_v2: Result<(AllTypes, _), _> = bincode::decode_from_slice(data, config);

if bincode_v1.as_ref().ok() != bincode_v2.as_ref().ok().map(|x| &x.0) {
println!("Bytes: {:?}", data);
println!("Bincode V1: {:?}", bincode_v1);
println!("Bincode V2: {:?}", bincode_v2);
panic!("failed equality check");
}
});
12 changes: 6 additions & 6 deletions fuzz/fuzz_targets/roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ enum AllTypes {
BTreeMap(BTreeMap<u8, u8>),
HashMap(HashMap<u8, u8>),
BTreeSet(BTreeSet<u8>),
VecDeque(VecDeque<u8>),
Vec(Vec<u8>),
VecDeque(VecDeque<AllTypes>),
Vec(Vec<AllTypes>),
String(String),
Box(Box<u8>),
BoxSlice(Box<[u8]>),
Rc(Rc<u8>),
Arc(Arc<u8>),
Box(Box<AllTypes>),
BoxSlice(Box<[AllTypes]>),
Rc(Rc<AllTypes>),
Arc(Arc<AllTypes>),
CString(CString),
SystemTime(SystemTime),
Duration(Duration),
Expand Down

0 comments on commit 3127ff5

Please sign in to comment.