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
This commit is contained in:
bitwave 2021-09-06 19:13:45 +02:00 committed by GitHub
parent d8ef1dd90a
commit e7ed5be9a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 3 deletions

View File

@ -46,6 +46,14 @@ fn signals_set(idx: usize) {
unsafe { SIGNALS[idx] = 1 }; 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)] #[allow(clippy::similar_names)]
pub fn main() { pub fn main() {
// The closure that we want to fuzz // The closure that we want to fuzz

View File

@ -34,13 +34,22 @@ pub fn current_time() -> time::Duration {
SystemTime::now().duration_since(UNIX_EPOCH).unwrap() 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) /// Current time (fixed fallback for no_std)
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
#[inline] #[inline]
pub fn current_time() -> time::Duration { pub fn current_time() -> time::Duration {
// We may not have a rt clock available. let millis = unsafe { external_current_millis() };
// TODO: Make it somehow plugin-able time::Duration::from_millis(millis)
time::Duration::from_millis(1)
} }
/// Gets current nanoseconds since [`UNIX_EPOCH`] /// Gets current nanoseconds since [`UNIX_EPOCH`]