#![doc = include_str!("../README.md")]
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
#![no_std]
#![cfg_attr(not(test), warn(
missing_debug_implementations,
missing_docs,
//trivial_casts,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
//unused_results
))]
#![cfg_attr(test, deny(
missing_debug_implementations,
missing_docs,
//trivial_casts,
trivial_numeric_casts,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
unused_must_use,
//unused_results
))]
#![cfg_attr(
test,
deny(
bad_style,
dead_code,
improper_ctypes,
non_shorthand_field_patterns,
no_mangle_generic_items,
overflowing_literals,
path_statements,
patterns_in_fns_without_body,
unconditional_recursion,
unused,
unused_allocation,
unused_comparisons,
unused_parens,
while_true
)
)]
#[macro_use]
extern crate std;
#[doc(hidden)]
pub extern crate alloc;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use libafl_core::Error;
use serde::{Deserialize, Serialize};
pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
get_core_ids_helper()
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[repr(transparent)]
pub struct CoreId(
pub usize,
);
impl CoreId {
pub fn set_affinity(&self) -> Result<(), Error> {
match set_for_current_helper(*self) {
Ok(()) | Err(Error::Unsupported(_, _)) => Ok(()),
Err(e) => Err(e),
}
}
pub fn set_affinity_forced(&self) -> Result<(), Error> {
set_for_current_helper(*self)
}
}
impl From<usize> for CoreId {
fn from(id: usize) -> Self {
CoreId(id)
}
}
impl From<CoreId> for usize {
fn from(core_id: CoreId) -> usize {
core_id.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
pub struct Cores {
pub cmdline: String,
pub ids: Vec<CoreId>,
}
impl Cores {
pub fn all() -> Result<Self, Error> {
Self::from_cmdline("all")
}
pub fn trim(&mut self, count: usize) -> Result<(), Error> {
if count > self.ids.len() {
return Err(Error::illegal_argument(format!(
"Core trim value {count} is larger than number of chosen cores of {}",
self.ids.len()
)));
}
self.ids.resize(count, CoreId(0));
Ok(())
}
pub fn from_cmdline(args: &str) -> Result<Self, Error> {
let mut cores: Vec<CoreId> = vec![];
if args == "all" {
let num_cores = get_core_ids()?.len();
for x in 0..num_cores {
cores.push(x.into());
}
} else {
let core_args: Vec<&str> = args.split(',').collect();
for csv in core_args {
let core_range: Vec<&str> = csv.split('-').collect();
if core_range.len() == 1 {
cores.push(core_range[0].parse::<usize>()?.into());
} else if core_range.len() == 2 {
for x in core_range[0].parse::<usize>()?..=(core_range[1].parse::<usize>()?) {
cores.push(x.into());
}
}
}
}
if cores.is_empty() {
return Err(Error::illegal_argument(format!(
"No cores specified! parsed: {args}"
)));
}
Ok(Self {
cmdline: args.to_string(),
ids: cores,
})
}
#[must_use]
pub fn contains(&self, core_id: CoreId) -> bool {
self.ids.contains(&core_id)
}
#[must_use]
pub fn position(&self, core_id: CoreId) -> Option<usize> {
self.ids
.iter()
.position(|&cur_core_id| cur_core_id == core_id)
}
}
impl From<&[usize]> for Cores {
fn from(cores: &[usize]) -> Self {
let cmdline = cores
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join(",");
let ids = cores.iter().map(|x| (*x).into()).collect();
Self { cmdline, ids }
}
}
impl From<Vec<usize>> for Cores {
fn from(cores: Vec<usize>) -> Self {
Self::from(cores.as_slice())
}
}
impl TryFrom<&str> for Cores {
type Error = Error;
fn try_from(cores: &str) -> Result<Self, Self::Error> {
Self::from_cmdline(cores)
}
}
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd"
))]
#[inline]
fn get_core_ids_helper() -> Result<Vec<CoreId>, Error> {
linux::get_core_ids()
}
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd"
))]
#[inline]
fn set_for_current_helper(core_id: CoreId) -> Result<(), Error> {
linux::set_for_current(core_id)
}
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd"
))]
mod linux {
use alloc::{string::ToString, vec::Vec};
use core::mem::{size_of, zeroed};
#[cfg(not(target_os = "freebsd"))]
use libc::cpu_set_t;
#[cfg(target_os = "freebsd")]
use libc::cpuset_t as cpu_set_t;
#[cfg(not(target_os = "dragonfly"))]
use libc::{CPU_ISSET, CPU_SET, CPU_SETSIZE, sched_getaffinity, sched_setaffinity};
#[cfg(target_os = "dragonfly")]
use libc::{CPU_ISSET, CPU_SET, sched_getaffinity, sched_setaffinity};
#[cfg(target_os = "dragonfly")]
const CPU_SETSIZE: libc::c_int = 256;
use libafl_core::Error;
use super::CoreId;
#[allow(trivial_numeric_casts)]
pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
let full_set = get_affinity_mask()?;
let mut core_ids: Vec<CoreId> = Vec::new();
for i in 0..CPU_SETSIZE as usize {
if unsafe { CPU_ISSET(i, &full_set) } {
core_ids.push(CoreId(i));
}
}
Ok(core_ids)
}
pub fn set_for_current(core_id: CoreId) -> Result<(), Error> {
let mut set = new_cpu_set();
unsafe { CPU_SET(core_id.0, &mut set) };
let result = unsafe {
sched_setaffinity(
0, size_of::<cpu_set_t>(),
&raw const set,
)
};
if result < 0 {
Err(Error::unknown("Failed to set_for_current"))
} else {
Ok(())
}
}
fn get_affinity_mask() -> Result<cpu_set_t, Error> {
let mut set = new_cpu_set();
let result = unsafe {
sched_getaffinity(
0, size_of::<cpu_set_t>(),
&raw mut set,
)
};
if result == 0 {
Ok(set)
} else {
Err(Error::unknown(
"Failed to retrieve affinity using sched_getaffinity".to_string(),
))
}
}
fn new_cpu_set() -> cpu_set_t {
unsafe { zeroed::<cpu_set_t>() }
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg_attr(miri, ignore)]
fn test_linux_get_affinity_mask() {
get_affinity_mask().unwrap();
}
#[test]
#[cfg_attr(miri, ignore)]
fn test_linux_set_for_current() {
let ids = get_core_ids().unwrap();
assert!(!ids.is_empty());
ids[0].set_affinity().unwrap();
let mut core_mask = new_cpu_set();
unsafe { CPU_SET(ids[0].0, &mut core_mask) };
let new_mask = get_affinity_mask().unwrap();
let mut is_equal = true;
for i in 0..CPU_SETSIZE as usize {
let is_set1 = unsafe { CPU_ISSET(i, &core_mask) };
let is_set2 = unsafe { CPU_ISSET(i, &new_mask) };
if is_set1 != is_set2 {
is_equal = false;
}
}
assert!(is_equal);
}
}
}
#[cfg(target_os = "haiku")]
#[expect(clippy::unnecessary_wraps)]
#[inline]
fn get_core_ids_helper() -> Result<Vec<CoreId>, Error> {
haiku::get_core_ids()
}
#[cfg(target_os = "haiku")]
#[expect(clippy::unnecessary_wraps)]
#[inline]
fn set_for_current_helper(_core_id: CoreId) -> Result<(), Error> {
Ok(())
}
#[cfg(target_os = "haiku")]
mod haiku {
use alloc::vec::Vec;
use std::thread::available_parallelism;
use crate::{CoreId, Error};
#[expect(clippy::unnecessary_wraps)]
pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
Ok((0..(usize::from(available_parallelism()?)))
.map(CoreId)
.collect::<Vec<_>>())
}
}
#[cfg(target_os = "windows")]
#[inline]
fn get_core_ids_helper() -> Result<Vec<CoreId>, Error> {
windows::get_core_ids()
}
#[cfg(target_os = "windows")]
#[inline]
fn set_for_current_helper(core_id: CoreId) -> Result<(), Error> {
windows::set_for_current(core_id)
}
#[cfg(target_os = "windows")]
mod windows {
use alloc::vec::Vec;
use windows::Win32::System::{
SystemInformation::GROUP_AFFINITY,
Threading::{GetCurrentThread, SetThreadGroupAffinity},
};
use crate::{CoreId, Error};
pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
let mut core_ids: Vec<CoreId> = Vec::new();
match get_num_logical_cpus_ex_windows() {
Some(total_cores) => {
for i in 0..total_cores {
core_ids.push(CoreId(i));
}
Ok(core_ids)
}
None => Err(Error::unknown("Unable to get logical CPUs count!")),
}
}
pub fn set_for_current(id: CoreId) -> Result<(), Error> {
let id: usize = id.into();
let mut cpu_group = 0;
let mut cpu_id = id;
let total_cores = get_num_logical_cpus_ex_windows().unwrap();
if id >= 64 {
cpu_group = total_cores / 64;
cpu_id = id - (cpu_group * 64);
}
let mask: usize = 1 << cpu_id;
unsafe {
let ga = GROUP_AFFINITY {
Mask: mask,
Group: cpu_group as u16,
Reserved: [0; 3],
};
let mut outga = GROUP_AFFINITY::default();
let result =
SetThreadGroupAffinity(GetCurrentThread(), &raw const ga, Some(&raw mut outga));
if result.0 == 0 {
Err(Error::unknown("Failed to set_for_current"))
} else {
Ok(())
}
}
}
#[expect(clippy::cast_ptr_alignment)]
pub fn get_num_logical_cpus_ex_windows() -> Option<usize> {
use core::{ptr, slice};
#[expect(non_upper_case_globals)]
const RelationProcessorCore: u32 = 0;
#[repr(C)]
#[allow(non_camel_case_types)] struct GROUP_AFFINITY {
mask: usize,
group: u16,
reserved: [u16; 3],
}
#[repr(C)]
#[allow(non_camel_case_types)] struct PROCESSOR_RELATIONSHIP {
flags: u8,
efficiency_class: u8,
reserved: [u8; 20],
group_count: u16,
group_mask_tenative: [GROUP_AFFINITY; 1],
}
#[repr(C)]
#[allow(non_camel_case_types)] struct SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX {
relationship: u32,
size: u32,
processor: PROCESSOR_RELATIONSHIP,
}
unsafe extern "system" {
fn GetLogicalProcessorInformationEx(
relationship: u32,
data: *mut u8,
length: &mut u32,
) -> bool;
}
let mut needed_size = 0;
unsafe {
GetLogicalProcessorInformationEx(
RelationProcessorCore,
ptr::null_mut(),
&mut needed_size,
);
}
if needed_size == 0 {
return None;
}
let mut buffer: Vec<u8> = vec![0_u8; needed_size as usize];
unsafe {
let result: bool = GetLogicalProcessorInformationEx(
RelationProcessorCore,
buffer.as_mut_ptr(),
&mut needed_size,
);
if !result {
return None;
}
}
let mut n_logical_procs: usize = 0;
let mut byte_offset: usize = 0;
while byte_offset < needed_size as usize {
unsafe {
let part_ptr_raw: *const u8 = buffer.as_ptr().add(byte_offset);
let part_ptr: *const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX =
part_ptr_raw as *const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX;
let part: &SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX = &*part_ptr;
if part.relationship == RelationProcessorCore {
let groupmasks_slice: &[GROUP_AFFINITY] = slice::from_raw_parts(
part.processor.group_mask_tenative.as_ptr(),
part.processor.group_count as usize,
);
let n_local_procs: usize = groupmasks_slice
.iter()
.map(|g| g.mask.count_ones() as usize)
.sum::<usize>();
n_logical_procs += n_local_procs;
}
byte_offset += part.size as usize;
}
}
Some(n_logical_procs)
}
}
#[cfg(target_vendor = "apple")]
#[inline]
fn get_core_ids_helper() -> Result<Vec<CoreId>, Error> {
apple::get_core_ids()
}
#[cfg(target_vendor = "apple")]
#[inline]
fn set_for_current_helper(core_id: CoreId) -> Result<(), Error> {
apple::set_for_current(core_id)
}
#[cfg(target_vendor = "apple")]
mod apple {
use alloc::vec::Vec;
use std::thread::available_parallelism;
use libafl_core::Error;
#[cfg(target_arch = "x86_64")]
use libc::{
KERN_SUCCESS, THREAD_AFFINITY_POLICY, THREAD_AFFINITY_POLICY_COUNT, integer_t,
kern_return_t, mach_msg_type_number_t, pthread_mach_thread_np, pthread_self,
thread_policy_flavor_t, thread_policy_t, thread_t,
};
#[cfg(all(target_arch = "aarch64", not(miri)))]
use libc::{pthread_set_qos_class_self_np, qos_class_t::QOS_CLASS_USER_INITIATED};
use super::CoreId;
#[cfg(target_arch = "x86_64")]
#[repr(C)]
struct thread_affinity_policy_data_t {
affinity_tag: integer_t,
}
#[cfg(target_arch = "x86_64")]
#[link(name = "System", kind = "framework")]
unsafe extern "C" {
fn thread_policy_set(
thread: thread_t,
flavor: thread_policy_flavor_t,
policy_info: thread_policy_t,
count: mach_msg_type_number_t,
) -> kern_return_t;
}
pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
Ok((0..(usize::from(available_parallelism()?)))
.map(CoreId)
.collect::<Vec<_>>())
}
#[cfg(target_arch = "x86_64")]
pub fn set_for_current(core_id: CoreId) -> Result<(), Error> {
let mut info = thread_affinity_policy_data_t {
affinity_tag: core_id.0.try_into().unwrap(),
};
unsafe {
let result = thread_policy_set(
pthread_mach_thread_np(pthread_self()),
THREAD_AFFINITY_POLICY as _,
&raw mut info as thread_policy_t,
THREAD_AFFINITY_POLICY_COUNT,
);
if result == KERN_SUCCESS {
Ok(())
} else {
Err(Error::unknown(format!(
"Failed to set_for_current {result:?}"
)))
}
}
}
#[cfg(target_arch = "aarch64")]
#[expect(clippy::unnecessary_wraps)]
pub fn set_for_current(_core_id: CoreId) -> Result<(), Error> {
#[cfg(not(miri))]
unsafe {
let _result = pthread_set_qos_class_self_np(QOS_CLASS_USER_INITIATED, 0);
}
Ok(())
}
}
#[cfg(target_os = "netbsd")]
#[inline]
fn get_core_ids_helper() -> Result<Vec<CoreId>, Error> {
netbsd::get_core_ids()
}
#[cfg(target_os = "netbsd")]
#[inline]
fn set_for_current_helper(core_id: CoreId) -> Result<(), Error> {
netbsd::set_for_current(core_id)
}
#[cfg(target_os = "netbsd")]
mod netbsd {
use alloc::vec::Vec;
use std::thread::available_parallelism;
use libafl_core::Error;
use libc::{
_cpuset, _cpuset_create, _cpuset_destroy, _cpuset_set, _cpuset_size, pthread_self,
pthread_setaffinity_np,
};
use super::CoreId;
#[expect(trivial_numeric_casts)]
pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
Ok((0..(usize::from(available_parallelism()?)))
.map(CoreId)
.collect::<Vec<_>>())
}
pub fn set_for_current(core_id: CoreId) -> Result<(), Error> {
let set = new_cpuset();
unsafe { _cpuset_set(core_id.0 as u64, set) };
let result = unsafe {
pthread_setaffinity_np(
pthread_self(), _cpuset_size(set),
set,
)
};
unsafe { _cpuset_destroy(set) };
if result < 0 {
Err(Error::unknown("Failed to set_for_current"))
} else {
Ok(())
}
}
fn new_cpuset() -> *mut _cpuset {
unsafe { _cpuset_create() }
}
}
#[cfg(target_os = "openbsd")]
#[inline]
fn get_core_ids_helper() -> Result<Vec<CoreId>, Error> {
openbsd::get_core_ids()
}
#[cfg(target_os = "openbsd")]
#[expect(clippy::unnecessary_wraps)]
#[inline]
fn set_for_current_helper(_: CoreId) -> Result<(), Error> {
Ok(()) }
#[cfg(target_os = "openbsd")]
mod openbsd {
use alloc::vec::Vec;
use std::thread::available_parallelism;
use libafl_core::Error;
use super::CoreId;
#[expect(trivial_numeric_casts)]
pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
Ok((0..(usize::from(available_parallelism()?)))
.map(CoreId)
.collect::<Vec<_>>())
}
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
#[inline]
fn get_core_ids_helper() -> Result<Vec<CoreId>, Error> {
solaris::get_core_ids()
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
#[inline]
fn set_for_current_helper(core_id: CoreId) -> Result<(), Error> {
solaris::set_for_current(core_id)
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
mod solaris {
use alloc::vec::Vec;
use std::thread::available_parallelism;
use libafl_core::Error;
use super::CoreId;
#[expect(clippy::unnecessary_wraps)]
pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
Ok((0..(usize::from(available_parallelism()?)))
.map(CoreId)
.collect::<Vec<_>>())
}
pub fn set_for_current(core_id: CoreId) -> Result<(), Error> {
let result = unsafe {
libc::processor_bind(
libc::P_PID,
libc::getpid(),
core_id.0.try_into().unwrap(),
std::ptr::null_mut(),
)
};
if result < 0 {
Err(Error::unknown("Failed to processor_bind"))
} else {
Ok(())
}
}
}
#[cfg(test)]
mod tests {
use std::thread::available_parallelism;
use super::*;
#[test]
#[cfg_attr(any(miri, target_os = "freebsd"), ignore)]
fn test_get_core_ids() {
let set = get_core_ids().unwrap();
assert_eq!(set.len(), usize::from(available_parallelism().unwrap()));
}
#[test]
#[cfg_attr(miri, ignore)]
fn test_set_affinity() {
let ids = get_core_ids().unwrap();
assert!(!ids.is_empty());
ids[0].set_affinity().unwrap();
}
}