From f324c60b0229853ba38b4b0bc7a56148895af0bd Mon Sep 17 00:00:00 2001 From: s1341 Date: Mon, 20 May 2024 09:34:31 +0300 Subject: [PATCH] Add testcase sampling rate (#2226) * fuzzer: Add with_sampling_rate * Format * Fix clippy --- libafl/src/fuzzer/mod.rs | 28 ++++++++++++++++++++++++++++ libafl_frida/src/asan/hook_funcs.rs | 6 +++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/libafl/src/fuzzer/mod.rs b/libafl/src/fuzzer/mod.rs index 09fc4ff144..bcd1fd660b 100644 --- a/libafl/src/fuzzer/mod.rs +++ b/libafl/src/fuzzer/mod.rs @@ -290,6 +290,8 @@ where scheduler: CS, feedback: F, objective: OF, + num_testcases: u64, + testcase_sampling_rate: Option, phantom: PhantomData, } @@ -470,6 +472,13 @@ where let idx = state.corpus_mut().add(testcase)?; self.scheduler_mut().on_add(state, idx)?; + self.num_testcases += 1; + let send_events = if let Some(sampling_rate) = self.testcase_sampling_rate { + send_events && self.num_testcases % sampling_rate == 0 + } else { + send_events + }; + if send_events { // TODO set None for fast targets let observers_buf = if manager.configuration() == EventConfig::AlwaysUnique { @@ -770,6 +779,25 @@ where scheduler, feedback, objective, + num_testcases: 0, + testcase_sampling_rate: None, + phantom: PhantomData, + } + } + + /// Create a new `StdFuzzer` with a specified `TestCase` sampling rate + pub fn with_sampling_rate( + scheduler: CS, + feedback: F, + objective: OF, + sampling_rate: u64, + ) -> Self { + Self { + scheduler, + feedback, + objective, + num_testcases: 0, + testcase_sampling_rate: Some(sampling_rate), phantom: PhantomData, } } diff --git a/libafl_frida/src/asan/hook_funcs.rs b/libafl_frida/src/asan/hook_funcs.rs index 6dccfe3df9..e3a1786aaf 100644 --- a/libafl_frida/src/asan/hook_funcs.rs +++ b/libafl_frida/src/asan/hook_funcs.rs @@ -2302,7 +2302,7 @@ impl AsanRuntime { Backtrace::new(), ))); } - original(s, p4, n) + original(s, p4, n); } #[cfg(target_vendor = "apple")] @@ -2332,7 +2332,7 @@ impl AsanRuntime { Backtrace::new(), ))); } - original(s, p8, n) + original(s, p8, n); } #[cfg(target_vendor = "apple")] @@ -2362,6 +2362,6 @@ impl AsanRuntime { Backtrace::new(), ))); } - original(s, p16, n) + original(s, p16, n); } }