From e7ed5be9a2fce9f844adc5c3a3b0fbc259c2bcaa Mon Sep 17 00:00:00 2001 From: bitwave Date: Mon, 6 Sep 2021 19:13:45 +0200 Subject: [PATCH] Use external, custom time function for no_std environments (#281) * Use external, custom time function for no_std environments * fixup! Use external, custom time function for no_std environments * fixup! Use external, custom time function for no_std environments --- fuzzers/baby_no_std/src/main.rs | 8 ++++++++ libafl/src/bolts/mod.rs | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/fuzzers/baby_no_std/src/main.rs b/fuzzers/baby_no_std/src/main.rs index b705ad76fe..80ffebc092 100644 --- a/fuzzers/baby_no_std/src/main.rs +++ b/fuzzers/baby_no_std/src/main.rs @@ -46,6 +46,14 @@ fn signals_set(idx: usize) { unsafe { SIGNALS[idx] = 1 }; } +/// Provide custom time in no_std environment +/// Use a time provider of your choice +#[no_mangle] +pub extern "C" fn external_current_millis() -> u64 { + // TODO: use "real" time here + 1000 +} + #[allow(clippy::similar_names)] pub fn main() { // The closure that we want to fuzz diff --git a/libafl/src/bolts/mod.rs b/libafl/src/bolts/mod.rs index e30ba00d12..a78d9817f0 100644 --- a/libafl/src/bolts/mod.rs +++ b/libafl/src/bolts/mod.rs @@ -34,13 +34,22 @@ pub fn current_time() -> time::Duration { SystemTime::now().duration_since(UNIX_EPOCH).unwrap() } +/// external defined function in case of no_std +/// +/// Define your own `external_current_millis()` function via `extern "C"` +/// which is linked into the binary and called from here. +#[cfg(not(feature = "std"))] +extern "C" { + #[no_mangle] + fn external_current_millis() -> u64; +} + /// Current time (fixed fallback for no_std) #[cfg(not(feature = "std"))] #[inline] pub fn current_time() -> time::Duration { - // We may not have a rt clock available. - // TODO: Make it somehow plugin-able - time::Duration::from_millis(1) + let millis = unsafe { external_current_millis() }; + time::Duration::from_millis(millis) } /// Gets current nanoseconds since [`UNIX_EPOCH`]