pub mod bytes;
pub use bytes::BytesInput;
pub mod value;
pub use value::ValueInput;
pub mod encoded;
pub use encoded::*;
pub mod gramatron;
pub use gramatron::*;
pub mod generalized;
pub use generalized::*;
pub mod bytessub;
pub use bytessub::BytesSubInput;
#[cfg(feature = "multipart_inputs")]
pub mod multi;
#[cfg(feature = "multipart_inputs")]
pub use multi::*;
#[cfg(feature = "multipart_inputs")]
pub mod list;
#[cfg(feature = "multipart_inputs")]
pub use list::*;
#[cfg(feature = "nautilus")]
pub mod nautilus;
use alloc::{
boxed::Box,
string::String,
vec::{Drain, Splice, Vec},
};
use core::{
clone::Clone,
fmt::Debug,
hash::Hash,
marker::PhantomData,
ops::{DerefMut, RangeBounds},
};
#[cfg(feature = "std")]
use std::{fs::File, io::Read, path::Path};
#[cfg(feature = "std")]
use libafl_bolts::fs::write_file_atomic;
use libafl_bolts::{
Error, HasLen, generic_hash_std,
ownedref::{OwnedMutSlice, OwnedSlice},
subrange::{SubRangeMutSlice, SubRangeSlice},
};
#[cfg(feature = "nautilus")]
pub use nautilus::*;
use serde::{Deserialize, Serialize};
use crate::corpus::CorpusId;
#[cfg(not(feature = "std"))]
pub trait Input: Clone + Serialize + serde::de::DeserializeOwned + Debug + Hash {
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, _id: Option<CorpusId>) -> String {
format!("{:016x}", generic_hash_std(self))
}
}
#[cfg(feature = "std")]
pub trait Input: Clone + Serialize + serde::de::DeserializeOwned + Debug + Hash {
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![];
file.read_to_end(&mut bytes)?;
Ok(postcard::from_bytes(&bytes)?)
}
fn generate_name(&self, _id: Option<CorpusId>) -> String {
format!("{:016x}", generic_hash_std(self))
}
}
pub trait InputConverter {
type From;
type To;
fn convert(&mut self, input: Self::From) -> Result<Self::To, Error>;
}
pub trait ToTargetBytes<I> {
fn to_target_bytes<'a>(&mut self, input: &'a I) -> OwnedSlice<'a, u8>;
}
#[derive(Debug)]
pub struct TargetBytesInputConverter<I, T> {
to_bytes_converter: T,
phantom: PhantomData<I>,
}
impl<I, T> InputConverter for TargetBytesInputConverter<I, T>
where
T: ToTargetBytes<I>,
{
type From = I;
type To = BytesInput;
fn convert(&mut self, input: Self::From) -> Result<Self::To, Error> {
Ok(BytesInput::new(
self.to_bytes_converter.to_target_bytes(&input).to_vec(),
))
}
}
impl<I, T> TargetBytesInputConverter<I, T> {
pub fn new(to_target_bytes_converter: T) -> Self {
Self {
to_bytes_converter: to_target_bytes_converter,
phantom: PhantomData,
}
}
}
impl<I, T> From<T> for TargetBytesInputConverter<I, T> {
fn from(to_bytes_converter: T) -> Self {
Self::new(to_bytes_converter)
}
}
#[macro_export]
macro_rules! none_input_converter {
() => {
None::<$crate::inputs::ClosureInputConverter<_, _>>
};
}
#[derive(Copy, Clone, Serialize, Deserialize, Debug, Default, Hash)]
pub struct NopInput {}
impl NopInput {
#[must_use]
pub fn new() -> Self {
Self {}
}
}
impl Input for NopInput {}
impl HasTargetBytes for NopInput {
fn target_bytes(&self) -> OwnedSlice<'_, u8> {
OwnedSlice::from(vec![0])
}
}
impl HasLen for NopInput {
fn len(&self) -> usize {
0
}
}
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)
}
}
pub trait HasTargetBytes {
fn target_bytes(&self) -> OwnedSlice<'_, u8>;
}
pub trait HasMutatorBytes: HasLen {
fn mutator_bytes(&self) -> &[u8];
fn mutator_bytes_mut(&mut self) -> &mut [u8];
fn sub_bytes<R>(&self, range: R) -> SubRangeSlice<'_, u8>
where
R: RangeBounds<usize>,
{
SubRangeSlice::new(OwnedSlice::from(self.mutator_bytes()), range)
}
fn sub_bytes_mut<R>(&mut self, range: R) -> SubRangeMutSlice<'_, u8>
where
R: RangeBounds<usize>,
{
SubRangeMutSlice::new(OwnedMutSlice::from(self.mutator_bytes_mut()), range)
}
fn sub_input<R>(&mut self, range: R) -> BytesSubInput<'_, Self>
where
R: RangeBounds<usize>,
{
BytesSubInput::new(self, range)
}
}
impl HasMutatorBytes for Vec<u8> {
fn mutator_bytes(&self) -> &[u8] {
self.as_ref()
}
fn mutator_bytes_mut(&mut self) -> &mut [u8] {
self.as_mut()
}
}
#[deprecated(since = "0.15.0", note = "Use &mut Vec<u8> directly")]
pub type MutVecInput<'a> = &'a mut Vec<u8>;
impl HasMutatorBytes for &'_ mut Vec<u8> {
fn mutator_bytes(&self) -> &[u8] {
self
}
fn mutator_bytes_mut(&mut self) -> &mut [u8] {
self
}
}
pub trait ResizableMutator<T> {
fn resize(&mut self, new_len: usize, value: T);
fn extend<'a, I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
where
T: 'a;
fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter>
where
R: RangeBounds<usize>,
I: IntoIterator<Item = T>;
fn drain<R>(&mut self, range: R) -> Drain<'_, T>
where
R: RangeBounds<usize>;
}
impl<T> ResizableMutator<T> for Vec<T>
where
T: Copy + 'static,
{
fn resize(&mut self, new_len: usize, value: T) {
<Vec<T>>::resize(self, new_len, value);
}
fn extend<'a, I: IntoIterator<Item = &'a T>>(&mut self, iter: I) {
<Vec<T> as Extend<I::Item>>::extend(self, iter);
}
fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter>
where
R: RangeBounds<usize>,
I: IntoIterator<Item = T>,
{
<Vec<T>>::splice(self, range, replace_with)
}
fn drain<R>(&mut self, range: R) -> Drain<'_, T>
where
R: RangeBounds<usize>,
{
<Vec<T>>::drain(self, range)
}
}
impl ResizableMutator<u8> for &mut Vec<u8> {
fn resize(&mut self, new_len: usize, value: u8) {
self.deref_mut().resize(new_len, value);
}
fn extend<'b, I: IntoIterator<Item = &'b u8>>(&mut self, iter: I) {
<Vec<u8> as Extend<I::Item>>::extend(self, iter);
}
fn splice<R, I>(&mut self, range: R, replace_with: I) -> Splice<'_, I::IntoIter>
where
R: RangeBounds<usize>,
I: IntoIterator<Item = u8>,
{
self.deref_mut().splice::<R, I>(range, replace_with)
}
fn drain<R>(&mut self, range: R) -> Drain<'_, u8>
where
R: RangeBounds<usize>,
{
self.deref_mut().drain(range)
}
}
#[derive(Debug, Copy, Clone, Default)]
pub struct NopToTargetBytes;
impl NopToTargetBytes {
#[must_use]
pub fn new() -> Self {
Self
}
}
impl<I> ToTargetBytes<I> for NopToTargetBytes
where
I: HasTargetBytes + Debug,
{
fn to_target_bytes<'a>(&mut self, input: &'a I) -> OwnedSlice<'a, u8> {
input.target_bytes()
}
}