moved std sharedmem to mod

This commit is contained in:
Dominik Maier 2021-01-06 04:43:40 +01:00
parent db5183d43f
commit 8b4896df28
3 changed files with 207 additions and 204 deletions

View File

@ -1,63 +1,16 @@
//! A generic sharememory region to be used by any functions (queues or feedbacks
// too.)
#[cfg(feature = "std")]
pub use shmem::AflShmem;
use alloc::string::{String, ToString};
use core::fmt::Debug;
#[cfg(feature = "std")]
use core::{mem::size_of, slice};
#[cfg(feature = "std")]
use libc::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, c_void};
#[cfg(feature = "std")]
use std::{env, ffi::CStr};
use std::env;
use crate::AflError;
extern "C" {
#[cfg(feature = "std")]
fn snprintf(_: *mut c_char, _: c_ulong, _: *const c_char, _: ...) -> c_int;
#[cfg(feature = "std")]
fn strncpy(_: *mut c_char, _: *const c_char, _: c_ulong) -> *mut c_char;
#[cfg(feature = "std")]
fn shmctl(__shmid: c_int, __cmd: c_int, __buf: *mut shmid_ds) -> c_int;
#[cfg(feature = "std")]
fn shmget(__key: c_int, __size: c_ulong, __shmflg: c_int) -> c_int;
#[cfg(feature = "std")]
fn shmat(__shmid: c_int, __shmaddr: *const c_void, __shmflg: c_int) -> *mut c_void;
}
#[cfg(feature = "std")]
#[derive(Copy, Clone)]
#[repr(C)]
struct ipc_perm {
pub __key: c_int,
pub uid: c_uint,
pub gid: c_uint,
pub cuid: c_uint,
pub cgid: c_uint,
pub mode: c_ushort,
pub __pad1: c_ushort,
pub __seq: c_ushort,
pub __pad2: c_ushort,
pub __glibc_reserved1: c_ulong,
pub __glibc_reserved2: c_ulong,
}
#[cfg(feature = "std")]
#[derive(Copy, Clone)]
#[repr(C)]
struct shmid_ds {
pub shm_perm: ipc_perm,
pub shm_segsz: c_ulong,
pub shm_atime: c_long,
pub shm_dtime: c_long,
pub shm_ctime: c_long,
pub shm_cpid: c_int,
pub shm_lpid: c_int,
pub shm_nattch: c_ulong,
pub __glibc_reserved4: c_ulong,
pub __glibc_reserved5: c_ulong,
}
/// A Shared map
pub trait ShMem: Sized + Debug {
/// Creates a new map with the given size
@ -119,16 +72,70 @@ pub trait ShMem: Sized + Debug {
}
#[cfg(feature = "std")]
#[derive(Clone, Debug)]
pub struct AflShmem {
pub mod shmem {
use core::{mem::size_of, slice};
use libc::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, c_void};
use std::ffi::CStr;
use crate::AflError;
use super::ShMem;
extern "C" {
#[cfg(feature = "std")]
fn snprintf(_: *mut c_char, _: c_ulong, _: *const c_char, _: ...) -> c_int;
#[cfg(feature = "std")]
fn strncpy(_: *mut c_char, _: *const c_char, _: c_ulong) -> *mut c_char;
#[cfg(feature = "std")]
fn shmctl(__shmid: c_int, __cmd: c_int, __buf: *mut shmid_ds) -> c_int;
#[cfg(feature = "std")]
fn shmget(__key: c_int, __size: c_ulong, __shmflg: c_int) -> c_int;
#[cfg(feature = "std")]
fn shmat(__shmid: c_int, __shmaddr: *const c_void, __shmflg: c_int) -> *mut c_void;
}
#[derive(Copy, Clone)]
#[repr(C)]
struct ipc_perm {
pub __key: c_int,
pub uid: c_uint,
pub gid: c_uint,
pub cuid: c_uint,
pub cgid: c_uint,
pub mode: c_ushort,
pub __pad1: c_ushort,
pub __seq: c_ushort,
pub __pad2: c_ushort,
pub __glibc_reserved1: c_ulong,
pub __glibc_reserved2: c_ulong,
}
#[derive(Copy, Clone)]
#[repr(C)]
struct shmid_ds {
pub shm_perm: ipc_perm,
pub shm_segsz: c_ulong,
pub shm_atime: c_long,
pub shm_dtime: c_long,
pub shm_ctime: c_long,
pub shm_cpid: c_int,
pub shm_lpid: c_int,
pub shm_nattch: c_ulong,
pub __glibc_reserved4: c_ulong,
pub __glibc_reserved5: c_ulong,
}
/// The default Sharedmap impl for unix using shmctl & shmget
#[derive(Clone, Debug)]
pub struct AflShmem {
pub shm_str: [u8; 20],
pub shm_id: c_int,
pub map: *mut u8,
pub map_size: usize,
}
}
#[cfg(feature = "std")]
impl ShMem for AflShmem {
impl ShMem for AflShmem {
fn existing_from_shm_slice(
map_str_bytes: &[u8; 20],
map_size: usize,
@ -154,31 +161,28 @@ impl ShMem for AflShmem {
fn map_mut(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.map, self.map_size) }
}
}
}
#[cfg(feature = "std")]
/// Deinit sharedmaps on drop
impl Drop for AflShmem {
/// Deinit sharedmaps on drop
impl Drop for AflShmem {
fn drop(&mut self) {
unsafe {
afl_shmem_deinit(self);
}
}
}
}
#[cfg(feature = "std")]
/// Create an uninitialized shmap
const fn afl_shmem_unitialized() -> AflShmem {
/// Create an uninitialized shmap
const fn afl_shmem_unitialized() -> AflShmem {
AflShmem {
shm_str: [0; 20],
shm_id: -1,
map: 0 as *mut c_uchar,
map_size: 0,
}
}
}
#[cfg(feature = "std")]
impl AflShmem {
impl AflShmem {
pub fn from_str(shm_str: &CStr, map_size: usize) -> Result<Self, AflError> {
let mut ret = afl_shmem_unitialized();
let map = unsafe { afl_shmem_by_str(&mut ret, shm_str, map_size) };
@ -204,11 +208,10 @@ impl AflShmem {
)))
}
}
}
}
#[cfg(feature = "std")]
/// Deinitialize this shmem instance
unsafe fn afl_shmem_deinit(shm: *mut AflShmem) {
/// Deinitialize this shmem instance
unsafe fn afl_shmem_deinit(shm: *mut AflShmem) {
if shm.is_null() || (*shm).map.is_null() {
/* Serialized map id */
// Not set or not initialized;
@ -217,12 +220,11 @@ unsafe fn afl_shmem_deinit(shm: *mut AflShmem) {
(*shm).shm_str[0 as usize] = '\u{0}' as u8;
shmctl((*shm).shm_id, 0 as c_int, 0 as *mut shmid_ds);
(*shm).map = 0 as *mut c_uchar;
}
}
#[cfg(feature = "std")]
/// Functions to create Shared memory region, for observation channels and
/// opening inputs and stuff.
unsafe fn afl_shmem_init(shm: *mut AflShmem, map_size: usize) -> *mut c_uchar {
/// Functions to create Shared memory region, for observation channels and
/// opening inputs and stuff.
unsafe fn afl_shmem_init(shm: *mut AflShmem, map_size: usize) -> *mut c_uchar {
(*shm).map_size = map_size;
(*shm).map = 0 as *mut c_uchar;
(*shm).shm_id = shmget(
@ -251,11 +253,14 @@ unsafe fn afl_shmem_init(shm: *mut AflShmem, map_size: usize) -> *mut c_uchar {
return 0 as *mut c_uchar;
}
return (*shm).map;
}
}
#[cfg(feature = "std")]
/// Uses a shmap id string to open a shared map
unsafe fn afl_shmem_by_str(shm: *mut AflShmem, shm_str: &CStr, map_size: usize) -> *mut c_uchar {
/// Uses a shmap id string to open a shared map
unsafe fn afl_shmem_by_str(
shm: *mut AflShmem,
shm_str: &CStr,
map_size: usize,
) -> *mut c_uchar {
if shm.is_null() || shm_str.to_bytes().len() == 0 || map_size == 0 {
return 0 as *mut c_uchar;
}
@ -279,6 +284,7 @@ unsafe fn afl_shmem_by_str(shm: *mut AflShmem, shm_str: &CStr, map_size: usize)
return 0 as *mut c_uchar;
}
return (*shm).map;
}
}
#[cfg(test)]

View File

@ -1,4 +1,5 @@
pub mod inmemory;
pub use inmemory::InMemoryExecutor;
#[cfg(feature = "runtime")]
pub mod runtime;
@ -113,11 +114,9 @@ where
mod test {
use core::marker::PhantomData;
use crate::executors::Executor;
use super::{Executor, NopExecutor};
use crate::inputs::BytesInput;
use super::NopExecutor;
#[test]
fn nop_executor() {
let empty_input = BytesInput::new(vec![]);

View File

@ -304,16 +304,14 @@ where
#[cfg(test)]
mod tests {
use crate::inputs::BytesInput;
use crate::mutators::scheduled::StdScheduledMutator;
use crate::utils::{Rand, XKCDRand};
use crate::{
corpus::{Corpus, InMemoryCorpus, Testcase},
inputs::BytesInput,
inputs::HasBytesVec,
mutators::scheduled::{mutation_splice, StdScheduledMutator},
utils::{Rand, XKCDRand},
};
use super::mutation_splice;
#[test]
fn test_mut_splice() {
// With the current impl, seed of 1 will result in a split at pos 2.