Skip to content

Apple aarch64 fixes #660

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

Merged
merged 10 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 31 additions & 71 deletions libafl/src/bolts/core_affinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! let handles = core_ids.into_iter().map(|id| {
//! thread::spawn(move || {
//! // Pin this thread to a single CPU core.
//! core_affinity::set_for_current(id);
//! id.set_affinity();
//! // Do more work after this.
//! })
//! }).collect::<Vec<_>>();
Expand All @@ -43,16 +43,6 @@ pub fn get_core_ids() -> Result<Vec<CoreId>, Error> {
get_core_ids_helper()
}

/// This function tries to pin the current
/// thread to the specified core.
///
/// # Arguments
///
/// * `core_id` - `ID` of the core to pin
pub fn set_for_current(core_id: CoreId) -> Result<(), Error> {
set_for_current_helper(core_id)
}

/// This represents a CPU core.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct CoreId {
Expand All @@ -62,8 +52,21 @@ pub struct CoreId {

impl CoreId {
/// Set the affinity of the current process to this [`CoreId`]
///
/// Note: This will *_not_* fail if the target platform does not support core affinity.
/// (only on error cases for supported platforms)
/// If you really need to fail for unsupported platforms (like `aarch64` on `macOS`), use [`CoreId::set_affinity_forced`] instead.
///
pub fn set_affinity(&self) -> Result<(), Error> {
set_for_current(*self)
match set_for_current_helper(*self) {
Ok(_) | Err(Error::Unsupported(_, _)) => Ok(()),
Err(e) => Err(e),
}
}

/// Set the affinity of the current process to this [`CoreId`]
pub fn set_affinity_forced(&self) -> Result<(), Error> {
set_for_current_helper(*self)
}
}

Expand Down Expand Up @@ -274,28 +277,20 @@ mod linux {

#[cfg(test)]
mod tests {
use num_cpus;

use super::*;

#[test]
fn test_linux_get_affinity_mask() {
get_affinity_mask().unwrap();
}

#[test]
fn test_linux_get_core_ids() {
let set = get_core_ids().unwrap();
assert_eq!(set.len(), num_cpus::get());
}

#[test]
fn test_linux_set_for_current() {
let ids = get_core_ids().unwrap();

assert!(!ids.is_empty());

set_for_current(ids[0]).unwrap();
ids[0].set_affinity().unwrap();

// Ensure that the system pinned the current thread
// to the specified core.
Expand Down Expand Up @@ -542,28 +537,6 @@ mod windows {

Some(n_logical_procs)
}

#[cfg(test)]
mod tests {
use num_cpus;

use super::*;

#[test]
fn test_apple_get_core_ids() {
let set = get_core_ids().unwrap();
assert_eq!(set.len(), num_cpus::get());
}

#[test]
fn test_apple_set_for_current() {
let ids = get_core_ids().unwrap();

assert!(!ids.is_empty());

set_for_current(ids[0]).unwrap();
}
}
}

// Apple Section
Expand All @@ -590,8 +563,8 @@ mod apple {
use alloc::vec::Vec;
use libc::{
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, THREAD_AFFINITY_POLICY,
THREAD_AFFINITY_POLICY_COUNT,
thread_policy_flavor_t, thread_policy_t, thread_t, KERN_NOT_SUPPORTED, KERN_SUCCESS,
THREAD_AFFINITY_POLICY, THREAD_AFFINITY_POLICY_COUNT,
};
use num_cpus;

Expand Down Expand Up @@ -632,9 +605,18 @@ mod apple {
);

// 0 == KERN_SUCCESS

if result == 0 {
if result == KERN_SUCCESS {
Ok(())
} else if result == KERN_NOT_SUPPORTED {
// 46 == KERN_NOT_SUPPORTED
// Seting a core affinity is not supported on aarch64 apple...
// We won't report this as an error to the user, a there's nothing they could do about it.
// Error codes, see <https://opensource.apple.com/source/xnu/xnu-792/osfmk/mach/kern_return.h>
//|| (cfg!(all(target_vendor = "apple", target_arch = "aarch64"))
// && result == KERN_NOT_SUPPORTED)
Err(Error::unsupported(
"Setting a core affinity is not supported on this platform (KERN_NOT_SUPPORTED)",
))
} else {
Err(Error::unknown(format!(
"Failed to set_for_current {:?}",
Expand All @@ -643,28 +625,6 @@ mod apple {
}
}
}

#[cfg(test)]
mod tests {
use num_cpus;

use super::*;

#[test]
fn test_windows_get_core_ids() {
let set = get_core_ids().unwrap();
assert_eq!(set.len(), num_cpus::get());
}

#[test]
fn test_windows_set_for_current() {
let ids = get_core_ids().unwrap();

assert!(!ids.is_empty());

set_for_current(ids[0]).unwrap();
}
}
}

#[cfg(test)]
Expand All @@ -684,11 +644,11 @@ mod tests {
}

#[test]
fn test_set_for_current() {
fn test_set_affinity() {
let ids = get_core_ids().unwrap();

assert!(!ids.is_empty());

set_for_current(ids[0]).unwrap();
ids[0].set_affinity().unwrap();
}
}
2 changes: 1 addition & 1 deletion libafl/src/bolts/llmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2745,7 +2745,7 @@ mod tests {

#[test]
#[serial]
pub fn llmp_connection() {
pub fn test_llmp_connection() {
#[allow(unused_variables)]
let shmem_provider = StdShMemProvider::new().unwrap();
let mut broker = match LlmpConnection::on_port(shmem_provider.clone(), 1337).unwrap() {
Expand Down
4 changes: 2 additions & 2 deletions libafl/src/bolts/minibsod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub fn dump_registers<W: Write>(
writer: &mut BufWriter<W>,
ucontext: &ucontext_t,
) -> Result<(), std::io::Error> {
let mcontext = unsafe { *ucontext.uc_mcontext };
let mcontext = unsafe { &*ucontext.uc_mcontext };
for reg in 0..29_u8 {
writeln!(
writer,
Expand Down Expand Up @@ -225,7 +225,7 @@ fn write_crash<W: Write>(
signal: Signal,
ucontext: &ucontext_t,
) -> Result<(), std::io::Error> {
let mcontext = unsafe { *ucontext.uc_mcontext };
let mcontext = unsafe { &*ucontext.uc_mcontext };
writeln!(
writer,
"Received signal {} at 0x{:016x}, fault address: 0x{:016x}",
Expand Down
23 changes: 23 additions & 0 deletions libafl/src/bolts/os/unix_shmem_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ where
res.id = id;
Ok(res)
}

fn new_shmem(&mut self, map_size: usize) -> Result<Self::ShMem, Error> {
let (server_fd, client_fd) = self.send_receive(ServedShMemRequest::NewMap(map_size))?;

Expand Down Expand Up @@ -718,3 +719,25 @@ where
}
}
}

/*
TODO: Fix test

#[cfg(test)]
mod tests {
use serial_test::serial;

use crate::bolts::{
os::unix_shmem_server::ServedShMemProvider,
shmem::{ShMem, ShMemProvider, UnixShMemProvider},
};

#[test]
#[serial]
fn test_shmem_server_connection() {
let mut sp = ServedShMemProvider::<UnixShMemProvider>::new().unwrap();
let map = sp.new_shmem(2 << 14).unwrap();
assert!(map.is_empty());
}
}
*/
Loading