Skip to content

Commit

Permalink
Support for bolts::cpu::read_time_counter on arm64 (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Sep 27, 2022
1 parent be0ae3a commit 279bb77
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions libafl/src/bolts/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Architecture agnostic processor features

#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
#[cfg(target_arch = "aarch64")]
use core::arch::asm;

#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")))]
use crate::bolts::current_nanos;

// TODO: Add more architectures, using C code, see
Expand All @@ -26,13 +29,28 @@ pub fn read_time_counter() -> u64 {
}
}

/// Read a timestamp for measurements
///
/// Fetches the counter-virtual count register
/// as we do not need to remove the cntvct_el2 offset.
#[cfg(target_arch = "aarch64")]
#[must_use]
pub fn read_time_counter() -> u64 {
let v: u64 = 0;
unsafe {
// TODO pushing a change in core::arch::aarch64 ?
asm!("mrs {v}, cntvct_el0", v = out(reg) _);
}
v
}

/// Read a timestamp for measurements.
///
/// This function is a wrapper around different ways to get a timestamp, fast.
/// In this way, an experiment only has to
/// change this implementation rather than every instead of [`read_time_counter`]
/// On unsupported architectures, it's falling back to normal system time, in millis.
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86")))]
#[cfg(not(any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64")))]
#[must_use]
pub fn read_time_counter() -> u64 {
current_nanos()
Expand Down

0 comments on commit 279bb77

Please sign in to comment.