From a001f3055cd326e11b64eb9530a658d7a1af08fe Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Fri, 11 Dec 2020 14:11:21 +0100 Subject: [PATCH] moved llmp test: --- afl/src/events/llmp.rs | 60 +++++++++++++++++++++++++++--- afl/src/events/mod.rs | 42 ++------------------- afl/src/events/shmem_translated.rs | 2 +- afl/src/executors/inmemory.rs | 1 - 4 files changed, 60 insertions(+), 45 deletions(-) diff --git a/afl/src/events/llmp.rs b/afl/src/events/llmp.rs index 9e32558e32..d3ff38cc88 100644 --- a/afl/src/events/llmp.rs +++ b/afl/src/events/llmp.rs @@ -95,7 +95,7 @@ pub type LlmpMsgHookFn = unsafe fn(client_id: u32, msg: *mut LlmpMsg) -> LlmpMsg pub type Tag = u32; /// Sending end on a (unidirectional) sharedmap channel -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct LlmpSender { /// ID of this sender. Only used in the broker. pub id: u32, @@ -112,7 +112,7 @@ pub struct LlmpSender { } /// Receiving end on a (unidirectional) sharedmap channel -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct LlmpReceiver { pub id: u32, /// Pointer to the last meg this received @@ -122,7 +122,7 @@ pub struct LlmpReceiver { } /// Client side of LLMP -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct LlmpClient { /// Outgoing channel to the broker pub llmp_out: LlmpSender, @@ -131,14 +131,14 @@ pub struct LlmpClient { } /// A page wrapper -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct LlmpSharedMap { /// Shmem containg the actual (unsafe) page, /// shared between one LlmpSender and one LlmpReceiver shmem: AflShmem, } /// Message sent over the "wire" -#[derive(Copy, Clone)] +#[derive(Copy, Clone, Debug)] #[repr(C, packed)] pub struct LlmpMsg { /// A tag @@ -622,7 +622,9 @@ impl LlmpSender { /// Receiving end of an llmp channel impl LlmpReceiver { + // Never inline, to not get some strange effects /// Read next message. + #[inline(never)] unsafe fn recv(&mut self) -> Result, AflError> { /* DBG("recv %p %p\n", page, last_msg); */ compiler_fence(Ordering::SeqCst); @@ -1109,3 +1111,51 @@ impl LlmpClient { self.llmp_in.recv_buf_blocking() } } + +#[cfg(test)] +mod tests { + + use std::{thread::sleep, time::Duration}; + + use super::{ + LlmpConnection::{self, IsBroker, IsClient}, + Tag, + }; + + #[test] + pub fn llmp_connection() { + let mut broker = match LlmpConnection::on_port(1337).unwrap() { + IsClient { client: _ } => panic!("Could not bind to port as broker"), + IsBroker { + broker, + listener_thread: _, + } => broker, + }; + + // Add the first client (2nd, actually, because of the tcp listener client) + let mut client = match LlmpConnection::on_port(1337).unwrap() { + IsBroker { + broker: _, + listener_thread: _, + } => panic!("Second connect should be a client!"), + IsClient { client } => client, + }; + + // Give the (background) tcp thread a few millis to post the message + sleep(Duration::from_millis(100)); + broker.once().unwrap(); + + let tag: Tag = 0x1337; + let arr: [u8; 1] = [1u8]; + // Send stuff + client.send_buf(tag, &arr).unwrap(); + // Forward stuff to clients + broker.once().unwrap(); + let (tag2, arr2) = client.recv_buf_blocking().unwrap(); + assert_eq!(tag, tag2); + assert_eq!(arr[0], arr2[0]); + + // We want at least the tcp and sender clients. + assert_eq!(broker.llmp_clients.len(), 2); + } +} diff --git a/afl/src/events/mod.rs b/afl/src/events/mod.rs index 12dfdbbc5b..3c1752023a 100644 --- a/afl/src/events/mod.rs +++ b/afl/src/events/mod.rs @@ -173,6 +173,7 @@ where /// Return the number of processes events or an error fn process(&mut self, state: &mut State, corpus: &mut C) -> Result; + #[inline] fn on_recv(&self, _state: &mut State, _corpus: &mut C) -> Result<(), AflError> { // TODO: Better way to move out of testcase, or get ref //Ok(corpus.add(self.testcase.take().unwrap())) @@ -223,6 +224,8 @@ where //TODO: broker.log() #[cfg(feature = "std")] println!("{}[{}]: {}", sender_id, severity_level, message); + // Silence warnings for no_std + let (_, _, _) = (sender_id, severity_level, message); Ok(BrokerEventResult::Handled) } Event::None { phantom: _ } => Ok(BrokerEventResult::Handled), @@ -298,6 +301,7 @@ where W: Write, //CE: CustomEvent, { + #[inline] fn fire<'a>(&mut self, event: Event<'a, I>) -> Result<(), AflError> { match self.handle_in_broker(&event)? { BrokerEventResult::Forward => (), //self.handle_in_client(event, state, corpus)?, @@ -566,16 +570,12 @@ where #[cfg(test)] mod tests { - use std::{thread, time::Duration}; - use crate::events::Event; use crate::inputs::bytes::BytesInput; use crate::observers::observer_serde::NamedSerdeAnyMap; use crate::observers::{Observer, StdMapObserver}; use crate::serde_anymap::{Ptr, PtrMut}; - use super::llmp::{LlmpConnection, Tag}; - static mut MAP: [u32; 4] = [0; 4]; #[test] @@ -611,38 +611,4 @@ mod tests { _ => panic!("mistmatch".to_string()), }; } - - use crate::events::tests::LlmpConnection::{IsBroker, IsClient}; - - #[test] - pub fn llmp_connection() { - let mut broker = match LlmpConnection::on_port(1337).unwrap() { - IsClient { client } => panic!("Could not bind to port as broker"), - IsBroker { - broker, - listener_thread, - } => broker, - }; - let mut client = match LlmpConnection::on_port(1337).unwrap() { - IsBroker { - broker, - listener_thread, - } => panic!("Second connect should be a client!"), - IsClient { client } => client, - }; - // Add the first client (2nd, actually, because of the tcp listener client) - broker.once().unwrap(); - assert_eq!(broker.llmp_clients.len(), 2); - - let tag: Tag = 0x1337; - let arr: [u8; 1] = [1u8]; - // Send stuff - client.send_buf(tag, &arr).unwrap(); - // Forward stuff to clients - broker.once().unwrap(); - broker.once().unwrap(); - let (tag2, arr2) = client.recv_buf_blocking().unwrap(); - assert_eq!(tag, tag2); - assert_eq!(arr[0], arr2[0]); - } } diff --git a/afl/src/events/shmem_translated.rs b/afl/src/events/shmem_translated.rs index 60939f5b09..1a9a06e86b 100644 --- a/afl/src/events/shmem_translated.rs +++ b/afl/src/events/shmem_translated.rs @@ -58,7 +58,7 @@ const AFL_RET_SUCCESS: c_uint = 0; // A generic sharememory region to be used by any functions (queues or feedbacks // too.) -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct AflShmem { pub shm_str: [u8; 20], pub shm_id: c_int, diff --git a/afl/src/executors/inmemory.rs b/afl/src/executors/inmemory.rs index 0a4006763b..8ce1e4b0c6 100644 --- a/afl/src/executors/inmemory.rs +++ b/afl/src/executors/inmemory.rs @@ -169,7 +169,6 @@ mod tests { use crate::executors::inmemory::InMemoryExecutor; use crate::executors::{Executor, ExitKind}; use crate::inputs::{HasTargetBytes, Input, TargetBytes}; - use crate::AflError; use serde::{Deserialize, Serialize};