1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//! [<img alt="github" src="https://img.shields.io/badge/github-udoprog/musli-8da0cb?style=for-the-badge&logo=github" height="20">](https://github.com/udoprog/musli)
//! [<img alt="crates.io" src="https://img.shields.io/crates/v/musli-tests.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/musli-tests)
//! [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-musli--tests-66c2a5?style=for-the-badge&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K" height="20">](https://docs.rs/musli-tests)
//!
//! Helper utilities for ensuring that musli is safe to use and behaves as expected.
//!
//! This does include a fairly sophisticated benchmarking suite.
//!
//! <br>
//!
//! ## Benchmarking
//!
//! Now this includes the `extra` category, which requires `model_minimal`. This
//! is not exactly a fair comparison, because it compares serialization formats
//! which were designed to model data that others are not capable off.
//!
//! The exact tradeoff can be seen by comparing the feature `model_full` with
//! `model_minimal`.
//!
//! ```sh
//! cargo bench --no-default-features --features musli,full,extra,model_minimal,simdutf8
//! ```
//!
//! To run benchmarks for serialization suites which *do* support all features, do:
//!
//! ```sh
//! cargo bench --features full
//! ```
//!
//! <br>
//!
//! ## Fuzz testing
//!
//! This comes with the `fuzz` binary which can do the following things.
//!
//! Run serialization for a long time against a lot of random data. Both with
//! and without miri. When run with miri, the size of the datasets is
//! drastically reduced.
//!
//! ```sh
//! cargo run --bin fuzz
//! cargo +nightly miri run --bin fuzz
//! ```
//!
//! Run deserialization against randomly generated bytes. Both with and without
//! miri. When run with miri, the size of the datasets is drastically reduced.
//!
//! ```sh
//! cargo run --bin fuzz -- --random
//! cargo +nightly miri run --bin fuzz -- --random
//! ```
//!
//! > **Note** you can add the running of optional crates by enabling its
//! > corresponding feature, such as `--features bincode`.
//!
//! <br>
//!
//! ## Potential security issues discovered by this crate
//!
//! * `dlhn`: Allocating and initializing large arrays based on untrusted input
//!   (DoS): [dlhn#11](https://github.com/otake84/dlhn/issues/11).
//!
//! <br>
//!
//! ## Size comparison
//!
//! To perform a one-off size comparison:
//!
//! ```sh
//! cargo run --bin fuzz -- --size
//! ```
//!
//! This will *for now* just take the first generated field of a given type,
//! serialize it, and print out its size. In the future it will perform an
//! average over the entire set with more statistics.

#![no_std]

extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

const RNG_SEED: u64 = 2718281828459045235;

#[macro_export]
macro_rules! miri {
    ($($(#[$($meta:meta)*])* const $ident:ident: $value_ty:ty = $range:expr, $miri:expr;)*) => {
        $(
            $(#[$($meta)*])*
            #[cfg(miri)]
            const $ident: $value_ty = $miri;
            $(#[$($meta)*])*
            #[cfg(not(miri))]
            const $ident: $value_ty = $range;
        )*
    }
}

// Defines denies feature combinations.
//
// * Negative features are not supported in cargo, and feature blocking
//   everything is too complex. model_map for example also depends on std.
// * Benchmarks for these must be explicitly run, because they only include a
//   subset of available data, we wouldn't be doing an apples-to-apples
//   comparison if we allowed only a model subset to be compared against a
//   serialization which supports a superset. If you do want to make this
//   comparison, you can enable `model_minimal`.
macro_rules! deny {
    ($base:literal $(, $feature:literal)*) => {
        $(
            #[cfg(all(feature = $base, feature = $feature))]
            compile_error!(concat!($base, ": does not support feature: ", $feature));
        )*
    }
}

deny!("rkyv", "model_tuple", "model_map_string_key", "model_usize");
deny!("dlhn", "model_map", "model_128");
deny!("bitcode", "model_128");

mod mode;
pub mod models;
pub mod utils;

#[cfg(feature = "musli-wire")]
pub mod wire {
    pub use musli_wire::*;
}

#[cfg(feature = "musli-storage")]
pub mod storage {
    pub use musli_storage::*;
}

#[cfg(feature = "musli-descriptive")]
pub mod s {
    pub use musli_descriptive::*;
}

/// Roundtrip the given expression through all supported formats.
#[cfg(all(
    feature = "musli-wire",
    feature = "musli-storage",
    feature = "musli-descriptive"
))]
#[macro_export]
macro_rules! rt {
    ($($tt:tt)*) => {{
        let a = ::musli_wire::rt!($($tt)*);
        let b = ::musli_storage::rt!($($tt)*);
        let c = ::musli_descriptive::rt!($($tt)*);
        assert_eq!(a, b);
        assert_eq!(a, c);
        a
    }};
}

#[macro_export]
macro_rules! musli_zerocopy_call {
    ($call:path) => {
    };

    ($call:path, prim, $ty:ty, $size_hint:expr $(, $tt:tt)*) => {
        $call!(musli_value, musli_value_buf, prim, $ty, $size_hint);
        $crate::musli_zerocopy_call!($call $(, $tt)*);
    };

    // Ignore others.
    ($call:path, $name:ident, $ty:ty, $size_hint:expr $(, $tt:tt)*) => {
        $crate::musli_zerocopy_call!($call $(, $tt)*);
    };
}

/// Call the given macro with the existing feature matrix.
#[macro_export]
macro_rules! feature_matrix {
    ($call:path $(, $($tt:tt)*)?) => {
        #[cfg(feature = "serde_json")]
        $call!(serde_json, serde_json_buf $(, $($tt)*)*);
        #[cfg(feature = "bincode")]
        $call!(serde_bincode, serde_bincode_buf $(, $($tt)*)*);
        #[cfg(feature = "rmp-serde")]
        $call!(serde_rmp, serde_rmp_buf $(, $($tt)*)*);
        #[cfg(feature = "musli-json")]
        $call!(musli_json, musli_json_buf $(, $($tt)*)*);
        #[cfg(feature = "musli-wire")]
        $call!(musli_wire, musli_wire_buf $(, $($tt)*)*);
        #[cfg(feature = "musli-descriptive")]
        $call!(musli_descriptive, musli_descriptive_buf $(, $($tt)*)*);
        #[cfg(feature = "musli-storage")]
        $call!(musli_storage, musli_storage_buf $(, $($tt)*)*);
        #[cfg(feature = "musli-storage")]
        $call!(musli_storage_packed, musli_storage_packed_buf $(, $($tt)*)*);
        #[cfg(feature = "musli-value")]
        $call!(musli_value, musli_value_buf $(, $($tt)*)*);
        #[cfg(feature = "musli-zerocopy")]
        $call!(musli_zerocopy, musli_zerocopy_buf $(, $($tt)*)*);
        #[cfg(all(feature = "dlhn", not(any(model_128, model_all))))]
        $call!(serde_dlhn, serde_dlhn_buf $(, $($tt)*)*);
        #[cfg(feature = "serde_cbor")]
        $call!(serde_cbor, serde_cbor_buf $(, $($tt)*)*);
        #[cfg(feature = "bitcode")]
        $call!(serde_bitcode, serde_bitcode_buf $(, $($tt)*)*);
        #[cfg(feature = "bitcode")]
        $call!(derive_bitcode, derive_bitcode_buf $(, $($tt)*)*);
        #[cfg(feature = "rkyv")]
        $call!(rkyv, rkyv_buf $(, $($tt)*)*);
        #[cfg(feature = "postcard")]
        $call!(postcard, postcard_buf $(, $($tt)*)*);
    };
}

/// Only expand `$block` if the given test is supported by this framework.
#[macro_export]
macro_rules! if_supported {
    (musli_zerocopy, lg, $block:block) => {};
    (musli_zerocopy, allocated, $block:block) => {};
    (musli_zerocopy, medium_enum, $block:block) => {};
    ($framework:ident, $test:ident, $block:block) => {
        $block
    };
}

#[macro_export]
macro_rules! types {
    ($call:path $(, $($tt:tt)*)?) => {
        $call! {
            $($($tt)*,)?
            prim, Primitives, PRIMITIVES, 1000,
            lg, LargeStruct, LARGE_STRUCTS, 10000,
            allocated, Allocated, ALLOCATED, 5000,
            medium_enum, MediumEnum, MEDIUM_ENUMS, 1000
        };
    }
}

/// Build common RNG.
pub fn rng() -> rand::prelude::StdRng {
    use rand::prelude::*;
    rand::prelude::StdRng::seed_from_u64(RNG_SEED)
}