From a013ad60856a74699bb6bd76ae80d418e76a46aa Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Sun, 19 Nov 2023 22:58:30 +0000 Subject: [PATCH] bolts for haiku update (#1673) core_affinity using available_parallelism which also support this platform. further tests disabling. --- libafl_bolts/examples/llmp_test/main.rs | 28 ++++++++++++------------- libafl_bolts/src/core_affinity.rs | 20 +++++++++++++++++- libafl_bolts/src/llmp.rs | 2 +- libafl_bolts/src/minibsod.rs | 16 ++++++++++++++ libafl_bolts/src/shmem.rs | 2 +- 5 files changed, 51 insertions(+), 17 deletions(-) diff --git a/libafl_bolts/examples/llmp_test/main.rs b/libafl_bolts/examples/llmp_test/main.rs index a5265aebf8..5c193c436c 100644 --- a/libafl_bolts/examples/llmp_test/main.rs +++ b/libafl_bolts/examples/llmp_test/main.rs @@ -3,37 +3,37 @@ This shows how llmp can be used directly, without libafl abstractions */ extern crate alloc; -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] use core::time::Duration; -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] use std::{num::NonZeroUsize, thread, time}; -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] use libafl_bolts::{ llmp::{self, Tag}, shmem::{ShMemProvider, StdShMemProvider}, ClientId, Error, SimpleStderrLogger, }; -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] const _TAG_SIMPLE_U32_V1: Tag = Tag(0x5130_0321); -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] const _TAG_MATH_RESULT_V1: Tag = Tag(0x7747_4331); -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] const _TAG_1MEG_V1: Tag = Tag(0xB111_1161); /// The time the broker will wait for things to happen before printing a message -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] const BROKER_TIMEOUT: Duration = Duration::from_secs(10); /// How long the broker may sleep between forwarding a new chunk of sent messages -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] const SLEEP_BETWEEN_FORWARDS: Duration = Duration::from_millis(5); -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] static LOGGER: SimpleStderrLogger = SimpleStderrLogger::new(); -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] fn adder_loop(port: u16) -> Result<(), Box> { let shmem_provider = StdShMemProvider::new()?; let mut client = llmp::LlmpClient::create_attach_to_tcp(shmem_provider, port)?; @@ -71,7 +71,7 @@ fn adder_loop(port: u16) -> Result<(), Box> { } } -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] fn large_msg_loop(port: u16) -> Result<(), Box> { let mut client = llmp::LlmpClient::create_attach_to_tcp(StdShMemProvider::new()?, port)?; @@ -91,7 +91,7 @@ fn large_msg_loop(port: u16) -> Result<(), Box> { } #[allow(clippy::unnecessary_wraps)] -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] fn broker_message_hook( msg_or_timeout: Option<(ClientId, llmp::Tag, llmp::Flags, &[u8])>, ) -> Result { @@ -126,12 +126,12 @@ fn broker_message_hook( } } -#[cfg(not(any(unix, windows)))] +#[cfg(target_os = "haiku")] fn main() { eprintln!("LLMP example is currently not supported on no_std. Implement ShMem for no_std."); } -#[cfg(any(unix, windows))] +#[cfg(not(target_os = "haiku"))] fn main() -> Result<(), Box> { /* The main node has a broker, and a few worker threads */ diff --git a/libafl_bolts/src/core_affinity.rs b/libafl_bolts/src/core_affinity.rs index 336e5d140e..86ba77331a 100644 --- a/libafl_bolts/src/core_affinity.rs +++ b/libafl_bolts/src/core_affinity.rs @@ -353,7 +353,7 @@ mod linux { #[allow(clippy::unnecessary_wraps)] #[inline] fn get_core_ids_helper() -> Result, Error> { - Ok(Vec::new()) + haiku::get_core_ids() } #[cfg(target_os = "haiku")] @@ -363,6 +363,21 @@ 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::core_affinity::{CoreId, Error}; + + #[allow(clippy::unnecessary_wraps)] + pub fn get_core_ids() -> Result, Error> { + Ok((0..(usize::from(available_parallelism()?))) + .map(CoreId) + .collect::>()) + } +} + // Windows Section #[cfg(target_os = "windows")] @@ -735,6 +750,7 @@ mod freebsd { unsafe { mem::zeroed::() } } + // FIXME: unstable for now on freebsd #[cfg(test)] mod tests { use libc::CPU_ISSET; @@ -742,11 +758,13 @@ mod freebsd { use super::*; #[test] + #[ignore] fn test_freebsd_get_affinity_mask() { get_affinity_mask().unwrap(); } #[test] + #[ignore] fn test_freebsd_set_for_current() { let ids = get_core_ids().unwrap(); diff --git a/libafl_bolts/src/llmp.rs b/libafl_bolts/src/llmp.rs index c42ada0eef..ef22424cd9 100644 --- a/libafl_bolts/src/llmp.rs +++ b/libafl_bolts/src/llmp.rs @@ -3173,7 +3173,7 @@ where } #[cfg(test)] -#[cfg(all(unix, feature = "std"))] +#[cfg(all(unix, feature = "std", not(target_os = "haiku")))] mod tests { use std::{thread::sleep, time::Duration}; diff --git a/libafl_bolts/src/minibsod.rs b/libafl_bolts/src/minibsod.rs index 61a0329073..5b05f1812f 100644 --- a/libafl_bolts/src/minibsod.rs +++ b/libafl_bolts/src/minibsod.rs @@ -654,6 +654,21 @@ fn write_crash( Ok(()) } +#[cfg(all(target_os = "haiku", target_arch = "x86_64"))] +fn write_crash( + writer: &mut BufWriter, + signal: Signal, + ucontext: &ucontext_t, +) -> Result<(), std::io::Error> { + writeln!( + writer, + "Received signal {} at {:#016x}", + signal, ucontext.uc_mcontext.rip + )?; + + Ok(()) +} + #[cfg(not(any( target_vendor = "apple", target_os = "linux", @@ -662,6 +677,7 @@ fn write_crash( target_os = "dragonfly", target_os = "openbsd", target_os = "netbsd", + target_os = "haiku", any(target_os = "solaris", target_os = "illumos"), )))] fn write_crash( diff --git a/libafl_bolts/src/shmem.rs b/libafl_bolts/src/shmem.rs index a519e0fbb6..d4b67f02c4 100644 --- a/libafl_bolts/src/shmem.rs +++ b/libafl_bolts/src/shmem.rs @@ -1537,7 +1537,7 @@ impl std::io::Seek for ShMemCursor { } } -#[cfg(feature = "std")] +#[cfg(all(feature = "std", not(target_os = "haiku")))] #[cfg(test)] mod tests { use serial_test::serial;