No_Std_Time: no_std-friendly timing and timestamping
The no_std_time crate provides no_std-friendly timing and high-performance timestamping utilities. It is a core component of the LibAFL fuzzing framework.
This crate offers two main pieces of functionality:
- A way to get the current system time as a
Duration, even inno_stdenvironments. - A high-performance time counter using CPU-specific instructions (
rdtsc, etc.) for fast measurements.
Usage
Getting the Current Time
You can get the current time using the current_time() function.
With the std feature enabled (the default), this works out of the box:
use current_time;
let time = current_time;
println!;
In a no_std environment, you must provide an implementation for external_current_millis that returns the milliseconds since the UNIX epoch.
// In your no_std binary:
pub extern "C"
High-Performance Timestamping
For high-performance measurements, read_time_counter() provides access to fast, low-level CPU cycle counters. This is useful for profiling and performance-critical code.
use read_time_counter;
let start = read_time_counter;
// ... do some work ...
let end = read_time_counter;
let elapsed = end - start;
println!;
This function is optimized for the following architectures:
x86andx86_64(usingrdtsc)aarch64(usingcntvct_el0)arm(using the performance monitor unit)riscv32andriscv64(usingrdcycle)
On other architectures, it falls back to a system-time-based implementation.
Formatting Durations
The crate includes a utility to format a Duration into a human-readable string. This requires the alloc feature.
use Duration;
use format_duration;
let duration = from_secs;
let formatted = format_duration;
assert_eq!;
Features
std(default): Enables functionality that depends on the standard library, such as getting the system time automatically.alloc(default): Enables features that require heap allocation, such asformat_duration.
The LibAFL Project
The LibAFL project is part of AFLplusplus and maintained by
- Andrea Fioraldi andrea@aflplus.plus
- Dominik Maier dominik@aflplus.plus
- s1341 github@shmarya.net
- Dongjia Zhang toka@aflplus.plus
- Addison Crump me@addisoncrump.info
Contributing
For bugs, feel free to open issues or contact us directly. Thank you for your support. <3
Even though we will gladly assist you in finishing up your PR, try to
- keep all the crates compiling with stable rust (hide the eventual non-stable code under
cfgs.) - run
cargo nightly fmton your code before pushing - check the output of
cargo clippy --allor./clippy.sh - run
cargo build --no-default-featuresto check forno_stdcompatibility (and possibly add#[cfg(feature = "std")]) to hide parts of your code.
Some parts in this list may sound hard, but don't be afraid to open a PR if you cannot fix them by yourself. We will gladly assist.