rustifying

This commit is contained in:
Dominik Maier 2020-11-22 04:49:00 +01:00
parent cd49cdfbea
commit 57b4d1d84f
2 changed files with 54 additions and 63 deletions

View File

@ -22,7 +22,7 @@ unsafe fn llmp_test_clientloop(client: *mut llmp_client, _data: *mut c_void) ->
size_of::<u32>(), size_of::<u32>(),
); );
(*llmp_message).tag = TAG_SIMPLE_U32_V1; (*llmp_message).tag = TAG_SIMPLE_U32_V1;
llmp_client_send(client, llmp_message); llmp_client_send(client, llmp_message).unwrap();
thread::sleep(time::Duration::from_millis(100)); thread::sleep(time::Duration::from_millis(100));
} }
@ -68,7 +68,7 @@ unsafe fn test_adder_clientloop(client: *mut llmp_client, _data: *mut c_void) ->
size_of::<u32>(), size_of::<u32>(),
); );
(*llmp_message).tag = TAG_MATH_RESULT_V1; (*llmp_message).tag = TAG_MATH_RESULT_V1;
llmp_client_send(client, llmp_message); llmp_client_send(client, llmp_message).unwrap();
last_result = current_result; last_result = current_result;
} }

View File

@ -264,14 +264,11 @@ pub unsafe fn llmp_msg_in_page(page: *mut llmp_page, msg: *mut llmp_message) ->
/* allign to LLMP_ALIGNNMENT bytes */ /* allign to LLMP_ALIGNNMENT bytes */
#[inline] #[inline]
unsafe fn llmp_align(to_align: c_ulong) -> c_ulong { unsafe fn llmp_align(to_align: c_ulong) -> c_ulong {
if 64 as c_int == 0 as c_int if 64 as c_int == 0 as c_int || to_align.wrapping_rem(64 as c_ulong) == 0 as c_int as c_ulong {
|| to_align.wrapping_rem(64 as c_ulong) == 0 as c_int as c_ulong
{
return to_align; return to_align;
} }
return to_align.wrapping_add( return to_align
(64 as c_ulong).wrapping_sub(to_align.wrapping_rem(64 as c_int as c_ulong)), .wrapping_add((64 as c_ulong).wrapping_sub(to_align.wrapping_rem(64 as c_int as c_ulong)));
);
} }
/* In case we don't have enough space, make sure the next page will be large /* In case we don't have enough space, make sure the next page will be large
enough. For now, we want to have at least enough space to store 2 of the enough. For now, we want to have at least enough space to store 2 of the
@ -279,13 +276,12 @@ largest messages we encountered. */
#[inline] #[inline]
unsafe fn new_map_size(max_alloc: c_ulong) -> c_ulong { unsafe fn new_map_size(max_alloc: c_ulong) -> c_ulong {
return next_pow2({ return next_pow2({
let mut _a: c_ulong = let mut _a: c_ulong = max_alloc
max_alloc .wrapping_mul(2 as c_ulong)
.wrapping_mul(2 as c_ulong) .wrapping_add(llmp_align(
.wrapping_add(llmp_align( (::std::mem::size_of::<llmp_message>() as c_ulong)
(::std::mem::size_of::<llmp_message>() as c_ulong) .wrapping_add(::std::mem::size_of::<llmp_payload_new_page>() as c_ulong),
.wrapping_add(::std::mem::size_of::<llmp_payload_new_page>() as c_ulong), ));
));
let mut _b: c_ulong = ((1 as c_int) << 28 as c_int) as c_ulong; let mut _b: c_ulong = ((1 as c_int) << 28 as c_int) as c_ulong;
if _a > _b { if _a > _b {
_a _a
@ -298,10 +294,7 @@ unsafe fn new_map_size(max_alloc: c_ulong) -> c_ulong {
* llmp_page->messages */ * llmp_page->messages */
unsafe fn _llmp_page_init(mut page: *mut llmp_page, sender: u32, size: c_ulong) { unsafe fn _llmp_page_init(mut page: *mut llmp_page, sender: u32, size: c_ulong) {
(*page).sender = sender; (*page).sender = sender;
::std::ptr::write_volatile( ::std::ptr::write_volatile(&mut (*page).current_msg_id as *mut c_ulong, 0 as c_ulong);
&mut (*page).current_msg_id as *mut c_ulong,
0 as c_ulong,
);
(*page).max_alloc_size = 0 as c_ulong; (*page).max_alloc_size = 0 as c_ulong;
(*page).c_ulongotal = size; (*page).c_ulongotal = size;
(*page).size_used = 0 as c_ulong; (*page).size_used = 0 as c_ulong;
@ -343,8 +336,7 @@ pub unsafe fn llmp_recv_blocking(
) -> *mut llmp_message { ) -> *mut llmp_message {
let mut current_msg_id: u32 = 0 as u32; let mut current_msg_id: u32 = 0 as u32;
if !last_msg.is_null() { if !last_msg.is_null() {
if (*last_msg).tag == 0xaf1e0f1 as c_uint if (*last_msg).tag == 0xaf1e0f1 as c_uint && llmp_msg_in_page(page, last_msg) as c_int != 0
&& llmp_msg_in_page(page, last_msg) as c_int != 0
{ {
panic!("BUG: full page passed to await_message_blocking or reset failed"); panic!("BUG: full page passed to await_message_blocking or reset failed");
} }
@ -512,7 +504,7 @@ pub unsafe fn llmp_alloc_next(
After commiting, the msg shall no longer be altered! After commiting, the msg shall no longer be altered!
It will be read by the consuming threads (broker->clients or client->broker) It will be read by the consuming threads (broker->clients or client->broker)
*/ */
pub unsafe fn llmp_send(page: *mut llmp_page, msg: *mut llmp_message) -> bool { pub unsafe fn llmp_send(page: *mut llmp_page, msg: *mut llmp_message) -> Result<(), AflError> {
if (*msg).tag == 0xdeadaf as c_uint { if (*msg).tag == 0xdeadaf as c_uint {
panic!(format!( panic!(format!(
"No tag set on message with id {}", "No tag set on message with id {}",
@ -520,7 +512,10 @@ pub unsafe fn llmp_send(page: *mut llmp_page, msg: *mut llmp_message) -> bool {
)); ));
} }
if msg.is_null() || !llmp_msg_in_page(page, msg) { if msg.is_null() || !llmp_msg_in_page(page, msg) {
return 0 as c_int != 0; return Err(AflError::Unknown(format!(
"Llmp Message {:?} is null or not in current page",
msg
)));
} }
compiler_fence(Ordering::SeqCst); compiler_fence(Ordering::SeqCst);
::std::ptr::write_volatile( ::std::ptr::write_volatile(
@ -529,8 +524,9 @@ pub unsafe fn llmp_send(page: *mut llmp_page, msg: *mut llmp_message) -> bool {
); );
compiler_fence(Ordering::SeqCst); compiler_fence(Ordering::SeqCst);
return 1 as c_int != 0; return Ok(());
} }
#[inline] #[inline]
unsafe fn _llmp_broker_current_broadcast_map( unsafe fn _llmp_broker_current_broadcast_map(
broker_state: *mut llmp_broker_state, broker_state: *mut llmp_broker_state,
@ -620,11 +616,14 @@ unsafe fn llmp_handle_out_eop(
// We never sent a msg on the new buf */ // We never sent a msg on the new buf */
*last_msg_p = 0 as *mut llmp_message; *last_msg_p = 0 as *mut llmp_message;
/* Send the last msg on the old buf */ /* Send the last msg on the old buf */
if !llmp_send(old_map, out) { match llmp_send(old_map, out) {
afl_free(maps as *mut c_void); Err(_e) => {
return 0 as *mut afl_shmem; afl_free(maps as *mut c_void);
println!("Error sending message");
0 as *mut afl_shmem
}
Ok(_) => maps,
} }
return maps;
} }
/* no more space left! We'll have to start a new page */ /* no more space left! We'll have to start a new page */
pub unsafe fn llmp_broker_handle_out_eop(mut broker: *mut llmp_broker_state) -> AflRet { pub unsafe fn llmp_broker_handle_out_eop(mut broker: *mut llmp_broker_state) -> AflRet {
@ -702,10 +701,8 @@ unsafe fn llmp_broker_register_client(
return 0 as *mut llmp_broker_client_metadata; return 0 as *mut llmp_broker_client_metadata;
} }
(*(*client).client_state).id = (*broker).llmp_client_count as u32; (*(*client).client_state).id = (*broker).llmp_client_count as u32;
(*client).cur_client_map = calloc( (*client).cur_client_map =
1 as c_ulong, calloc(1 as c_ulong, ::std::mem::size_of::<afl_shmem>() as c_ulong) as *mut afl_shmem;
::std::mem::size_of::<afl_shmem>() as c_ulong,
) as *mut afl_shmem;
if (*client).cur_client_map.is_null() { if (*client).cur_client_map.is_null() {
return 0 as *mut llmp_broker_client_metadata; return 0 as *mut llmp_broker_client_metadata;
} }
@ -862,12 +859,11 @@ unsafe fn llmp_broker_handle_new_msgs(
/* We need to replace the message ID with our own */ /* We need to replace the message ID with our own */
let out_page: *mut llmp_page = let out_page: *mut llmp_page =
shmem2page(_llmp_broker_current_broadcast_map(broker)); shmem2page(_llmp_broker_current_broadcast_map(broker));
(*out).message_id = (*out_page) (*out).message_id = (*out_page).current_msg_id.wrapping_add(1 as c_ulong) as u32;
.current_msg_id match llmp_send(out_page, out) {
.wrapping_add(1 as c_ulong) as u32; Err(e) => panic!(format!("Error sending msg: {:?}", e)),
if !llmp_send(out_page, out) { _ => (),
panic!("Error sending msg"); };
}
(*broker).last_msg_sent = out (*broker).last_msg_sent = out
} }
} }
@ -938,21 +934,19 @@ pub unsafe fn llmp_broker_launch_client(
) -> bool { ) -> bool {
if clientdata < (*broker).llmp_clients if clientdata < (*broker).llmp_clients
|| clientdata || clientdata
> &mut *(*broker).llmp_clients.offset( > &mut *(*broker)
(*broker) .llmp_clients
.llmp_client_count .offset((*broker).llmp_client_count.wrapping_sub(1 as c_ulong) as isize)
.wrapping_sub(1 as c_ulong) as isize, as *mut llmp_broker_client_metadata
) as *mut llmp_broker_client_metadata
{ {
println!( println!(
"[!] WARNING: Illegal client specified at ptr {:?} (instead of {:?} to {:?})", "[!] WARNING: Illegal client specified at ptr {:?} (instead of {:?} to {:?})",
clientdata, clientdata,
(*broker).llmp_clients, (*broker).llmp_clients,
&mut *(*broker).llmp_clients.offset( &mut *(*broker)
(*broker) .llmp_clients
.llmp_client_count .offset((*broker).llmp_client_count.wrapping_sub(1 as c_ulong) as isize,)
.wrapping_sub(1 as c_ulong) as isize, as *mut llmp_broker_client_metadata,
) as *mut llmp_broker_client_metadata,
); );
return 0 as c_int != 0; return 0 as c_int != 0;
} }
@ -1023,9 +1017,7 @@ unsafe fn llmp_client_prune_old_pages(mut client: *mut llmp_client) {
.map; .map;
/* look for pages that are save_to_unmap, then unmap them. */ /* look for pages that are save_to_unmap, then unmap them. */
while (*(*client).out_maps.offset(0 as isize)).map != current_map while (*(*client).out_maps.offset(0 as isize)).map != current_map
&& (*shmem2page(&mut *(*client).out_maps.offset(0 as isize))).save_to_unmap && (*shmem2page(&mut *(*client).out_maps.offset(0 as isize))).save_to_unmap as c_int != 0
as c_int
!= 0
{ {
/* This page is save to unmap. The broker already reads or read it. */ /* This page is save to unmap. The broker already reads or read it. */
afl_shmem_deinit(&mut *(*client).out_maps.offset(0 as isize)); afl_shmem_deinit(&mut *(*client).out_maps.offset(0 as isize));
@ -1225,17 +1217,18 @@ pub unsafe fn llmp_client_cancel(client: *mut llmp_client, mut msg: *mut llmp_me
) as c_ulong; ) as c_ulong;
} }
/* Commits a msg to the client's out ringbuf */ /* Commits a msg to the client's out ringbuf */
pub unsafe fn llmp_client_send(mut client_state: *mut llmp_client, msg: *mut llmp_message) -> bool { pub unsafe fn llmp_client_send(
mut client_state: *mut llmp_client,
msg: *mut llmp_message,
) -> Result<(), AflError> {
let page: *mut llmp_page = shmem2page( let page: *mut llmp_page = shmem2page(
&mut *(*client_state).out_maps.offset( &mut *(*client_state)
(*client_state) .out_maps
.out_map_count .offset((*client_state).out_map_count.wrapping_sub(1) as isize),
.wrapping_sub(1 as c_ulong) as isize,
),
); );
let ret: bool = llmp_send(page, msg); llmp_send(page, msg)?;
(*client_state).last_msg_sent = msg; (*client_state).last_msg_sent = msg;
return ret; Ok(())
} }
/* Creates a new, unconnected, client state */ /* Creates a new, unconnected, client state */
@ -1244,10 +1237,8 @@ pub unsafe fn llmp_client_new_unconnected() -> *mut llmp_client {
1 as c_ulong, 1 as c_ulong,
::std::mem::size_of::<llmp_client>() as c_ulong, ::std::mem::size_of::<llmp_client>() as c_ulong,
) as *mut llmp_client; ) as *mut llmp_client;
(*client_state).current_broadcast_map = calloc( (*client_state).current_broadcast_map =
1 as c_ulong, calloc(1 as c_ulong, ::std::mem::size_of::<afl_shmem>() as c_ulong) as *mut afl_shmem;
::std::mem::size_of::<afl_shmem>() as c_ulong,
) as *mut afl_shmem;
if (*client_state).current_broadcast_map.is_null() { if (*client_state).current_broadcast_map.is_null() {
return 0 as *mut llmp_client; return 0 as *mut llmp_client;
} }