Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deserialization of enums and add fuzzer #4

Merged
merged 8 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "cbor-smol"
version = "0.4.0"
authors = ["Nicolas Stalder <[email protected]>"]
edition = "2018"
edition = "2021"
description = "Streamlined serde serializer/deserializer for CBOR"
repository = "https://github.com/nickray/cbor-smol"
readme = "README.md"
Expand Down
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
31 changes: 31 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "cbor-smol-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
arbitrary = { version = "1.2.3", features = ["derive"] }
serde = { version = "1.0.152", features = ["derive"] }
serde_bytes = "0.11.9"
serde_cbor = "0.11.2"

[dependencies.cbor-smol]
path = ".."

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[profile.release]
debug = 1

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

use arbitrary::{Arbitrary, Unstructured};
use libfuzzer_sys::fuzz_target;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Arbitrary, Serialize, Deserialize)]
enum AllEnums {
I8(i8),
U8(u8),
I16(i16),
U16(u16),
I32(i32),
U32(u32),
// Not implemented
// I64(i64),
U64(u64),
Struct(Struct),
Array([Struct; 4]),
Option(Option<Struct>),
Vec(Vec<Struct>),
Bytes(#[serde(with = "serde_bytes")] Vec<u8>),
String(String),
Tuple((Struct, Struct)),
TupleVariant(Struct, Struct),
TupleVariantBytes(Struct, Struct, #[serde(with = "serde_bytes")] Vec<u8>),
StructVariant {
x: Struct,
y: Struct,
},
StructVariantBytes {
x: Struct,
y: Struct,
#[serde(with = "serde_bytes")]
z: Vec<u8>,
},
}

#[derive(Debug, PartialEq, Arbitrary, Serialize, Deserialize)]
struct Struct {
a: Box<AllEnums>,
b: Box<AllEnums>,
}

/// Workaround https://github.com/rust-fuzz/arbitrary/issues/144
#[derive(Debug)]
struct Input<'i>(AllEnums, &'i [u8]);

impl<'i> Arbitrary<'i> for Input<'i> {
fn arbitrary(u: &mut Unstructured<'i>) -> Result<Self, arbitrary::Error> {
Ok(Self(AllEnums::arbitrary(u)?, Arbitrary::arbitrary(u)?))
}

fn arbitrary_take_rest(mut u: Unstructured<'i>) -> Result<Self, arbitrary::Error> {
Ok(Self(
AllEnums::arbitrary(&mut u)?,
Arbitrary::arbitrary_take_rest(u)?,
))
}
fn size_hint(_depth: usize) -> (usize, Option<usize>) {
(0, None)
}
}

fuzz_target!(|data: Input<'_>| {
let bytes = data.1;
let data = data.0;
let _res: Option<AllEnums> = cbor_smol::cbor_deserialize(&bytes).ok();
let mut buffer = vec![0; 1024 * 20];
let res = cbor_smol::cbor_serialize(&data, &mut buffer).unwrap();
cbor_smol::cbor_deserialize(&res)
.map(|b: AllEnums| {
assert_eq!(data, b);
})
.map_err(|err| {
let v: Result<serde_cbor::Value, _> = serde_cbor::from_slice(&res);
panic!(
"Failed to deserialize: {:?}\n\
input: {:#?}\n\
data: {:02x?}\n\
serde_cbor gives: {:#?}\n",
err, data, res, v
);
})
.ok();
});
19 changes: 19 additions & 0 deletions src/consts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pub const MAJOR_OFFSET: u8 = 5;

pub const MAJOR_POSINT: u8 = 0;
pub const MAJOR_NEGINT: u8 = 1;
pub const MAJOR_BYTES: u8 = 2;
pub const MAJOR_STR: u8 = 3;
pub const MAJOR_ARRAY: u8 = 4;
pub const MAJOR_MAP: u8 = 5;
pub const MAJOR_SIMPLE: u8 = 7;

pub const SIMPLE_FALSE: u8 = 20;
pub const SIMPLE_TRUE: u8 = 21;
pub const SIMPLE_NULL: u8 = 22;
// pub const SIMPLE_UNDEFINED: u8 = 23;

pub const VALUE_FALSE: u8 = (MAJOR_SIMPLE << MAJOR_OFFSET) | SIMPLE_FALSE;
pub const VALUE_TRUE: u8 = (MAJOR_SIMPLE << MAJOR_OFFSET) | SIMPLE_TRUE;
pub const VALUE_NULL: u8 = (MAJOR_SIMPLE << MAJOR_OFFSET) | SIMPLE_NULL;
// pub const VALUE_UNDEFINED: u8 = (MAJOR_SIMPLE << MAJOR_LEN) | SIMPLE_UNDEFINED;
Loading