Limit rayon to one thread during fuzzing

This should enable more deterministic execution.
This commit is contained in:
Nick Fitzgerald
2020-02-28 16:32:06 -08:00
parent 99a37a7e61
commit 4866fa0e6a
6 changed files with 40 additions and 3 deletions

View File

@@ -4,3 +4,29 @@
pub mod generators;
pub mod oracles;
/// One time start up initialization for fuzzing:
///
/// * Enables `env_logger`.
///
/// * Restricts `rayon` to a single thread in its thread pool, for more
/// deterministic executions.
///
/// If a fuzz target is taking raw input bytes from the fuzzer, it is fine to
/// call this function in the fuzz target's oracle or in the fuzz target
/// itself. However, if the fuzz target takes an `Arbitrary` type, and the
/// `Arbitrary` implementation is not derived and does interesting things, then
/// the `Arbitrary` implementation should call this function, since it runs
/// before the fuzz target itself.
pub(crate) fn init_fuzzing() {
static INIT: std::sync::Once = std::sync::Once::new();
INIT.call_once(|| {
let _ = env_logger::try_init();
rayon::ThreadPoolBuilder::new()
.num_threads(1)
.build_global()
.expect("should only initialize the rayon thread pool once!");
})
}