moved llmp to examples

This commit is contained in:
Dominik Maier 2020-12-31 03:19:31 +01:00
parent 752bc86203
commit e1268fa200
6 changed files with 36 additions and 73 deletions

View File

@ -13,6 +13,8 @@ fxhash = "0.2.1" # yet another hash
xxhash-rust = { version = "0.8.0", features = ["const_xxh3", "xxh3"] } # xxh3 hashing for rust xxhash-rust = { version = "0.8.0", features = ["const_xxh3", "xxh3"] } # xxh3 hashing for rust
serde_json = "1.0.60" serde_json = "1.0.60"
num_cpus = "1.0" # cpu count, for llmp example
[[bench]] [[bench]]
name = "rand_speeds" name = "rand_speeds"
harness = false harness = false
@ -30,6 +32,10 @@ debug = true
default = ["std"] default = ["std"]
std = [] std = []
[[example]]
name = "llmp_test"
path = "./examples/llmp_test/main.rs"
[dependencies] [dependencies]
tuple_list = "0.1.2" tuple_list = "0.1.2"
hashbrown = { version = "0.9", features = ["serde"] } # A faster hashmap, nostd compatible hashbrown = { version = "0.9", features = ["serde"] } # A faster hashmap, nostd compatible

View File

@ -6,18 +6,20 @@ use std::thread;
use std::time; use std::time;
use afl::events::llmp; use afl::events::llmp;
use afl::events::shmem::AflShmem;
use afl::AflError;
const TAG_SIMPLE_U32_V1: u32 = 0x51300321; const TAG_SIMPLE_U32_V1: u32 = 0x51300321;
const TAG_MATH_RESULT_V1: u32 = 0x77474331; const TAG_MATH_RESULT_V1: u32 = 0x77474331;
fn adder_loop(port: u16) -> ! { fn adder_loop(port: u16) -> ! {
let mut client = llmp::LlmpClient::create_attach_to_tcp(port).unwrap(); let mut client = llmp::LlmpClient::<AflShmem>::create_attach_to_tcp(port).unwrap();
let mut last_result: u32 = 0; let mut last_result: u32 = 0;
let mut current_result: u32 = 0; let mut current_result: u32 = 0;
loop { loop {
let mut msg_counter = 0; let mut msg_counter = 0;
loop { loop {
let (tag, buf) = match client.recv_buf().unwrap() { let (_sender, tag, buf) = match client.recv_buf().unwrap() {
None => break, None => break,
Some(msg) => msg, Some(msg) => msg,
}; };
@ -47,29 +49,30 @@ fn adder_loop(port: u16) -> ! {
} }
} }
unsafe fn broker_message_hook( fn broker_message_hook(
client_id: u32, client_id: u32,
message: *mut llmp::LlmpMsg, tag: llmp::Tag,
) -> llmp::LlmpMsgHookResult { message: &[u8],
match (*message).tag { ) -> Result<llmp::LlmpMsgHookResult, AflError> {
match tag {
TAG_SIMPLE_U32_V1 => { TAG_SIMPLE_U32_V1 => {
println!( println!(
"Client {:?} sent message: {:?}", "Client {:?} sent message: {:?}",
client_id, client_id,
u32::from_le_bytes((*message).as_slice_unsafe().try_into().unwrap()) u32::from_le_bytes(message.try_into().unwrap())
); );
llmp::LlmpMsgHookResult::ForwardToClients Ok(llmp::LlmpMsgHookResult::ForwardToClients)
} }
TAG_MATH_RESULT_V1 => { TAG_MATH_RESULT_V1 => {
println!( println!(
"Adder Client has this current result: {:?}", "Adder Client has this current result: {:?}",
u32::from_le_bytes((*message).as_slice_unsafe().try_into().unwrap()) u32::from_le_bytes(message.try_into().unwrap())
); );
llmp::LlmpMsgHookResult::Handled Ok(llmp::LlmpMsgHookResult::Handled)
} }
_ => { _ => {
println!("Unknwon message id received!"); println!("Unknwon message id received!");
llmp::LlmpMsgHookResult::ForwardToClients Ok(llmp::LlmpMsgHookResult::ForwardToClients)
} }
} }
} }
@ -89,13 +92,16 @@ fn main() {
match mode.as_str() { match mode.as_str() {
"broker" => { "broker" => {
let mut broker: llmp::LlmpBroker = llmp::LlmpBroker::new().unwrap(); let mut broker = llmp::LlmpBroker::<AflShmem>::new().unwrap();
broker.launch_tcp_listener(port).unwrap(); broker
broker.add_message_hook(broker_message_hook); .launch_tcp_listener(
broker.loop_forever(Some(Duration::from_millis(5))) std::net::TcpListener::bind(format!("127.0.0.1:{}", port)).unwrap(),
)
.unwrap();
broker.loop_forever(&mut broker_message_hook, Some(Duration::from_millis(5)))
} }
"ctr" => { "ctr" => {
let mut client = llmp::LlmpClient::create_attach_to_tcp(port).unwrap(); let mut client = llmp::LlmpClient::<AflShmem>::create_attach_to_tcp(port).unwrap();
let mut counter: u32 = 0; let mut counter: u32 = 0;
loop { loop {
counter = counter.wrapping_add(1); counter = counter.wrapping_add(1);

View File

@ -1,11 +0,0 @@
[package]
name = "llmp_test"
version = "0.1.0"
authors = ["Dominik Maier <domenukk@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num_cpus = "1.0"
afl = { path = "../" }

View File

@ -313,22 +313,21 @@ where
} }
} }
/*
#[cfg(feature = "std")] #[cfg(feature = "std")]
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::observers::{Observer, StdMapObserver}; use crate::observers::StdMapObserver;
use crate::tuples::Named;
static mut MAP: [u32; 4] = [0; 4]; static mut MAP: [u32; 4] = [0; 4];
#[test] #[test]
fn test_observer_serde() { fn test_observer_serde() {
let o: Box<dyn Observer> = let obv = StdMapObserver::new("test", unsafe { &mut MAP });
Box::new(StdMapObserver::<u32>::new("test", unsafe { &mut MAP })); let vec = postcard::to_allocvec(&obv).unwrap();
let s = serde_json::to_string(&o).unwrap(); println!("{:?}", vec);
println!("{}", s); let obv2: StdMapObserver<u32> = postcard::from_bytes(&vec).unwrap();
let d: Box<dyn Observer> = serde_json::from_str(&s).unwrap(); assert_eq!(obv.name(), obv2.name());
assert_eq!(d.name(), o.name());
} }
} }
*/

View File

@ -1,14 +0,0 @@
[package]
name = "afl_derives"
version = "0.1.0"
authors = ["Andrea Fioraldi <andreafioraldi@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
syn = "0.15"
quote = "0.6"
[lib]
proc-macro = true

View File

@ -1,23 +0,0 @@
#[macro_use]
extern crate quote;
#[macro_use]
extern crate syn;
extern crate proc_macro;
use proc_macro::TokenStream;
use syn::DeriveInput;
#[proc_macro_derive(MyMacro)]
pub fn my_macro(input: TokenStream) -> TokenStream {
// Parse the input tokens into a syntax tree
let input = parse_macro_input!(input as DeriveInput);
// Build the output, possibly using quasi-quotation
let expanded = quote! {
// ...
};
// Hand the output tokens back to the compiler
TokenStream::from(expanded)
}