Skip to content

Commit c247ff5

Browse files
committed
Rework features
1 parent 69043b0 commit c247ff5

File tree

19 files changed

+108
-155
lines changed

19 files changed

+108
-155
lines changed

cargo-test-fuzz/src/bin/cargo_test_fuzz/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ mod tests {
2525
#[allow(clippy::unwrap_used)]
2626
cargo_test_fuzz(&[
2727
"--features",
28-
&("test-fuzz/".to_owned() + test_fuzz::serde_format().as_feature()),
28+
&("test-fuzz/".to_owned() + test_fuzz::serde_format::as_feature()),
2929
"--no-run",
3030
"--no-instrumentation",
3131
"target",

cargo-test-fuzz/tests/fuzz_generic.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ const TIMEOUT: &str = "60";
1010
fn fuzz_foo_qwerty() {
1111
// smoelius: When `bincode` is enabled, `cargo-afl` fails because "the program crashed with one
1212
// of the test cases provided."
13-
// smoelius: `to_string` is used here because `SerdeFormat` won't necessarily contain the
14-
// `Bincode` variant.
15-
if serde_format().to_string() == "Bincode" {
16-
fuzz("test_foo_qwerty", 2);
17-
} else {
18-
fuzz("test_foo_qwerty", 1);
19-
};
13+
fuzz(
14+
"test_foo_qwerty",
15+
if serde_format::serializes_variant_names() {
16+
1
17+
} else {
18+
2
19+
},
20+
);
2021
}
2122

2223
#[cfg_attr(dylint_lib = "general", allow(non_thread_safe_call_in_test))]

docs/crates.dot

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ digraph {
22
"cargo-test-fuzz" -> "internal"
33
"cargo-test-fuzz" -> "test-fuzz"
44
"cargo-test-fuzz" -> "testing"
5-
"macro" -> "internal"
65
"macro-generated-code" -> "test-fuzz"
76
"runtime" -> "internal"
87
"test-fuzz" -> "internal"

internal/Cargo.toml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,24 @@ authors = ["Samuel E. Moelius III <[email protected]>"]
99
license = "AGPL-3.0 WITH mif-exception"
1010
repository = "https://github.com/trailofbits/test-fuzz"
1111

12+
# smoelius: https://github.com/rust-lang/cargo/issues/1839
13+
# Because of the above issue, the crate for the default format (bincode) must be included regardless
14+
# of whether it is selected. A test-fuzz test (`link`) verifies that the crate's code is not linked
15+
# in when another format is selected.
16+
1217
[dependencies]
1318
cargo_metadata = "0.18"
14-
proc-macro2 = "1.0"
15-
quote = "1.0"
1619
serde = "1.0"
17-
strum_macros = "0.25"
20+
21+
bincode = "1.3"
22+
cbor4ii = { version = "0.3", features = ["serde1", "use_std"], optional = true }
23+
serde_cbor = { version = "0.11", optional = true }
1824

1925
[features]
2026
__auto_concretize = []
2127
__serde_bincode = []
22-
__serde_cbor = []
23-
__serde_cbor4ii = []
28+
__serde_cbor = ["serde_cbor"]
29+
__serde_cbor4ii = ["cbor4ii"]
30+
31+
[package.metadata.cargo-udeps.ignore]
32+
normal = ["bincode"]

internal/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ pub use auto_concretize::enabled as auto_concretize_enabled;
33

44
pub mod dirs;
55

6-
mod serde_format;
7-
pub use serde_format::*;
6+
pub mod serde_format;

internal/src/serde_format.rs

Lines changed: 68 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,87 @@
1-
#![allow(clippy::use_self)]
1+
use serde::{de::DeserializeOwned, Serialize};
2+
use std::io::Read;
23

3-
use proc_macro2::{Ident, Span, TokenStream};
4-
use quote::{quote, ToTokens, TokenStreamExt};
5-
use strum_macros::Display;
6-
7-
#[derive(Copy, Clone, Debug, Display, Eq, PartialEq)]
8-
pub enum SerdeFormat {
9-
#[cfg(any(serde_default, feature = "__serde_bincode"))]
10-
Bincode,
11-
#[cfg(feature = "__serde_cbor")]
12-
Cbor,
13-
#[cfg(feature = "__serde_cbor4ii")]
14-
Cbor4ii,
15-
}
4+
#[cfg(any(serde_default, feature = "__serde_bincode"))]
5+
const BYTE_LIMIT: u64 = 1024 * 1024 * 1024;
166

177
#[allow(clippy::vec_init_then_push)]
188
#[must_use]
19-
pub fn serde_format() -> SerdeFormat {
9+
pub fn as_feature() -> &'static str {
2010
let mut formats = vec![];
11+
2112
#[cfg(any(serde_default, feature = "__serde_bincode"))]
22-
formats.push(SerdeFormat::Bincode);
13+
formats.push("serde_bincode");
14+
2315
#[cfg(feature = "__serde_cbor")]
24-
formats.push(SerdeFormat::Cbor);
16+
formats.push("serde_cbor");
17+
2518
#[cfg(feature = "__serde_cbor4ii")]
26-
formats.push(SerdeFormat::Cbor4ii);
19+
formats.push("serde_cbor4ii");
20+
2721
assert!(
2822
formats.len() <= 1,
2923
"{}",
3024
"Multiple serde formats selected: {formats:?}"
3125
);
26+
3227
formats.pop().expect("No serde format selected")
3328
}
3429

35-
impl SerdeFormat {
36-
#[must_use]
37-
pub fn as_feature(self) -> &'static str {
38-
match self {
39-
#[cfg(any(serde_default, feature = "__serde_bincode"))]
40-
Self::Bincode => "serde_bincode",
41-
#[cfg(feature = "__serde_cbor")]
42-
Self::Cbor => "serde_cbor",
43-
#[cfg(feature = "__serde_cbor4ii")]
44-
Self::Cbor4ii => "serde_cbor4ii",
45-
}
46-
}
30+
#[must_use]
31+
pub fn serializes_variant_names() -> bool {
32+
#[cfg(any(serde_default, feature = "__serde_bincode"))]
33+
return false;
34+
35+
#[cfg(feature = "__serde_cbor")]
36+
return true;
37+
38+
#[cfg(feature = "__serde_cbor4ii")]
39+
return true;
40+
}
41+
42+
pub fn serialize<T: Serialize>(args: &T) -> Vec<u8> {
43+
#[cfg(any(serde_default, feature = "__serde_bincode"))]
44+
return {
45+
use bincode::Options;
46+
// smoelius: From
47+
// https://github.com/bincode-org/bincode/blob/c44b5e364e7084cdbabf9f94b63a3c7f32b8fb68/src/lib.rs#L102-L103 :
48+
// /// **Warning:** the default configuration used by [`bincode::serialize`] is not
49+
// /// the same as that used by the `DefaultOptions` struct. ...
50+
// The point is that `bincode::serialize(..)` and `bincode::options().serialize(..)` use
51+
// different encodings, even though the latter uses "default" options.
52+
bincode::options()
53+
.with_limit(BYTE_LIMIT)
54+
.serialize(args)
55+
.unwrap()
56+
};
57+
58+
#[cfg(feature = "__serde_cbor")]
59+
return serde_cbor::to_vec(args).unwrap();
60+
61+
#[cfg(feature = "__serde_cbor4ii")]
62+
return {
63+
let mut data = Vec::new();
64+
cbor4ii::serde::to_writer(&mut data, args).unwrap();
65+
data
66+
};
4767
}
4868

49-
impl ToTokens for SerdeFormat {
50-
fn to_tokens(&self, tokens: &mut TokenStream) {
51-
let ident = Ident::new(&self.to_string(), Span::call_site());
52-
tokens.append_all(quote! {
53-
test_fuzz::SerdeFormat::#ident
54-
});
55-
}
69+
pub fn deserialize<T: DeserializeOwned, R: Read>(reader: R) -> Option<T> {
70+
#[cfg(any(serde_default, feature = "__serde_bincode"))]
71+
return {
72+
use bincode::Options;
73+
bincode::options()
74+
.with_limit(BYTE_LIMIT)
75+
.deserialize_from(reader)
76+
.ok()
77+
};
78+
79+
#[cfg(feature = "__serde_cbor")]
80+
return serde_cbor::from_reader(reader).ok();
81+
82+
#[cfg(feature = "__serde_cbor4ii")]
83+
return {
84+
let reader = std::io::BufReader::new(reader);
85+
cbor4ii::serde::from_reader(reader).ok()
86+
};
5687
}

macro/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@ subprocess = "0.2"
2323
syn = { version = "2.0", features = ["full", "parsing", "visit", "visit-mut"] }
2424
toolchain_find = "0.4"
2525

26-
internal = { path = "../internal", package = "test-fuzz-internal", version = "=4.0.1" }
26+
internal = { path = "../internal", package = "test-fuzz-internal", version = "=4.0.1", optional = true }
2727

2828
[features]
29-
__auto_concretize = []
29+
__auto_concretize = ["internal"]
3030
__persistent = []
31-
__serde_bincode = ["internal/__serde_bincode"]
32-
__serde_cbor = ["internal/__serde_cbor"]
33-
__serde_cbor4ii = ["internal/__serde_cbor4ii"]

macro/build.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

macro/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#![cfg_attr(feature = "__auto_concretize", feature(proc_macro_span))]
33

44
use darling::{ast::NestedMeta, FromMeta};
5-
use internal::serde_format;
65
use itertools::MultiUnzip;
76
use once_cell::sync::Lazy;
87
use proc_macro::TokenStream;
@@ -453,7 +452,6 @@ fn map_method_or_fn(
453452
)
454453
};
455454

456-
let serde_format = serde_format();
457455
let write_concretizations = quote! {
458456
let impl_concretization = [
459457
#(#impl_ty_names),*
@@ -628,7 +626,7 @@ fn map_method_or_fn(
628626
let args = Args(
629627
#(#args_is),*
630628
);
631-
test_fuzz::runtime::write_args(#serde_format, &args);
629+
test_fuzz::runtime::write_args(&args);
632630
}
633631

634632
struct UsingReader<R>(R);
@@ -639,7 +637,7 @@ fn map_method_or_fn(
639637
struct Args #ty_generics (
640638
#(#pub_arg_tys),*
641639
) #args_where_clause;
642-
let args = test_fuzz::runtime::read_args::<Args #ty_generics_as_turbofish, _>(#serde_format, reader);
640+
let args = test_fuzz::runtime::read_args::<Args #ty_generics_as_turbofish, _>(reader);
643641
args.map(|args| #mod_ident :: Args(
644642
#(#args_is),*
645643
))

runtime/Cargo.toml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,10 @@ authors = ["Samuel E. Moelius III <[email protected]>"]
99
license = "AGPL-3.0 WITH mif-exception"
1010
repository = "https://github.com/trailofbits/test-fuzz"
1111

12-
# smoelius: https://github.com/rust-lang/cargo/issues/1839
13-
# Because of the above issue, the crate for the default format (bincode) must be included regardless
14-
# of whether it is selected. A test-fuzz test (`link`) verifies that the crate's code is not linked
15-
# in when another format is selected.
16-
1712
[dependencies]
18-
bincode = "1.3"
19-
cbor4ii = { version = "0.3", features = ["serde1", "use_std"], optional = true }
2013
hex = "0.4"
2114
num-traits = "0.2"
2215
serde = { version = "1.0", features = ["derive"] }
23-
serde_cbor = { version = "0.11", optional = true }
2416
sha-1 = "0.10"
2517

2618
internal = { path = "../internal", package = "test-fuzz-internal", version = "=4.0.1" }
27-
28-
[features]
29-
__serde_bincode = []
30-
__serde_cbor = ["serde_cbor"]
31-
__serde_cbor4ii = ["cbor4ii"]
32-
33-
[package.metadata.cargo-udeps.ignore]
34-
normal = ["bincode"]

0 commit comments

Comments
 (0)