Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bolts::cpu::read_time_counter arm64 implementation proposal. #790

Merged
merged 1 commit into from
Sep 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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