pub mod bytes;
pub use bytes::BytesInput;
pub mod encoded;
pub use encoded::*;
pub mod gramatron;
pub use gramatron::*;
pub mod generalized;
pub use generalized::*;
#[cfg(feature = "multipart_inputs")]
pub mod multi;
#[cfg(feature = "multipart_inputs")]
pub use multi::*;
#[cfg(feature = "nautilus")]
pub mod nautilus;
use alloc::{
boxed::Box,
string::{String, ToString},
vec::Vec,
};
use core::{clone::Clone, fmt::Debug, marker::PhantomData};
#[cfg(feature = "std")]
use std::{fs::File, hash::Hash, io::Read, path::Path};
#[cfg(feature = "std")]
use libafl_bolts::fs::write_file_atomic;
use libafl_bolts::{ownedref::OwnedSlice, Error};
#[cfg(feature = "nautilus")]
pub use nautilus::*;
use serde::{Deserialize, Serialize};
#[cfg(not(feature = "std"))]
pub trait Input: Clone + Serialize + serde::de::DeserializeOwned + Debug {
fn to_file<P>(&self, _path: P) -> Result<(), Error> {
Err(Error::not_implemented("Not supported in no_std"))
}
fn from_file<P>(_path: P) -> Result<Self, Error> {
Err(Error::not_implemented("Not supprted in no_std"))
}
fn generate_name(&self, idx: usize) -> String;
fn wrapped_as_testcase(&mut self) {}
}
#[cfg(feature = "std")]
pub trait Input: Clone + Serialize + serde::de::DeserializeOwned + Debug {
fn to_file<P>(&self, path: P) -> Result<(), Error>
where
P: AsRef<Path>,
{
write_file_atomic(path, &postcard::to_allocvec(self)?)
}
fn from_file<P>(path: P) -> Result<Self, Error>
where
P: AsRef<Path>,
{
let mut file = File::open(path)?;
let mut bytes: Vec<u8> = vec![];
file.read_to_end(&mut bytes)?;
Ok(postcard::from_bytes(&bytes)?)
}
fn generate_name(&self, idx: usize) -> String;
fn wrapped_as_testcase(&mut self) {}
}
pub trait InputConverter: Debug {
type From: Input;
type To: Input;
fn convert(&mut self, input: Self::From) -> Result<Self::To, Error>;
}
#[macro_export]
macro_rules! none_input_converter {
() => {
None::<$crate::inputs::ClosureInputConverter<_, _>>
};
}
#[derive(Copy, Clone, Serialize, Deserialize, Debug, Hash)]
pub struct NopInput {}
impl Input for NopInput {
fn generate_name(&self, _idx: usize) -> String {
"nop-input".to_string()
}
}
impl HasTargetBytes for NopInput {
fn target_bytes(&self) -> OwnedSlice<u8> {
OwnedSlice::from(vec![0])
}
}
pub trait HasTargetBytes {
fn target_bytes(&self) -> OwnedSlice<u8>;
}
pub trait HasBytesVec {
fn bytes(&self) -> &[u8];
fn bytes_mut(&mut self) -> &mut Vec<u8>;
}
pub trait UsesInput {
type Input: Input;
}
#[derive(Debug)]
pub struct NopInputConverter<I> {
phantom: PhantomData<I>,
}
impl<I> Default for NopInputConverter<I> {
fn default() -> Self {
Self {
phantom: PhantomData,
}
}
}
impl<I> InputConverter for NopInputConverter<I>
where
I: Input,
{
type From = I;
type To = I;
fn convert(&mut self, input: Self::From) -> Result<Self::To, Error> {
Ok(input)
}
}
pub struct ClosureInputConverter<F, T>
where
F: Input,
T: Input,
{
convert_cb: Box<dyn FnMut(F) -> Result<T, Error>>,
}
impl<F, T> Debug for ClosureInputConverter<F, T>
where
F: Input,
T: Input,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("ClosureInputConverter")
.finish_non_exhaustive()
}
}
impl<F, T> ClosureInputConverter<F, T>
where
F: Input,
T: Input,
{
#[must_use]
pub fn new(convert_cb: Box<dyn FnMut(F) -> Result<T, Error>>) -> Self {
Self { convert_cb }
}
}
impl<F, T> InputConverter for ClosureInputConverter<F, T>
where
F: Input,
T: Input,
{
type From = F;
type To = T;
fn convert(&mut self, input: Self::From) -> Result<Self::To, Error> {
(self.convert_cb)(input)
}
}