From c3275bbc3f692be6faac4cb73f1fa2cbcea5eedc Mon Sep 17 00:00:00 2001 From: Dominik Maier Date: Sun, 15 Nov 2020 03:22:07 +0100 Subject: [PATCH] compiles with no_std --- src/corpus/mod.rs | 1 - src/corpus/testcase.rs | 8 +++++++- src/engines/mod.rs | 5 ++++- src/executors/inmemory.rs | 29 ++++++++++++++++++++++++----- src/executors/mod.rs | 4 +++- src/feedbacks/mod.rs | 5 ++--- src/inputs/bytes.rs | 1 - src/inputs/mod.rs | 5 ++--- src/lib.rs | 1 + src/mutators/mod.rs | 1 - src/mutators/scheduled.rs | 1 - src/observers/mod.rs | 1 - src/stages/mod.rs | 1 - src/stages/mutational.rs | 1 - src/utils.rs | 1 - 15 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/corpus/mod.rs b/src/corpus/mod.rs index 476a825870..6fceac9c49 100644 --- a/src/corpus/mod.rs +++ b/src/corpus/mod.rs @@ -1,4 +1,3 @@ -extern crate alloc; pub mod testcase; pub use testcase::{Testcase, TestcaseMetadata}; diff --git a/src/corpus/testcase.rs b/src/corpus/testcase.rs index 450a3ab9c2..0caf475c04 100644 --- a/src/corpus/testcase.rs +++ b/src/corpus/testcase.rs @@ -1,12 +1,16 @@ -extern crate alloc; use crate::inputs::Input; use crate::AflError; +use alloc::boxed::Box; use alloc::rc::Rc; +use alloc::string::String; use core::cell::RefCell; use core::convert::Into; +use core::default::Default; +use core::option::Option; use hashbrown::HashMap; + #[cfg(feature = "std")] use std::fs::File; #[cfg(feature = "std")] @@ -39,6 +43,7 @@ where } */ +#[cfg(feature = "std")] pub enum FileBackedTestcase { /// A testcase on disk, not yet loaded Stored { filename: P }, @@ -51,6 +56,7 @@ pub enum FileBackedTestcase { Dirty { input: I, filename: P }, } +#[cfg(feature = "std")] impl FileBackedTestcase where I: Input, diff --git a/src/engines/mod.rs b/src/engines/mod.rs index 52749d1e54..5f714c94dc 100644 --- a/src/engines/mod.rs +++ b/src/engines/mod.rs @@ -1,5 +1,7 @@ //! The engine is the core piece of every good fuzzer -extern crate alloc; + +use alloc::boxed::Box; +use alloc::vec::Vec; use crate::corpus::Corpus; use crate::feedbacks::Feedback; @@ -83,6 +85,7 @@ where #[cfg(test)] mod tests { + use alloc::boxed::Box; use alloc::rc::Rc; use core::cell::RefCell; diff --git a/src/executors/inmemory.rs b/src/executors/inmemory.rs index bf3c590f31..d702abb2dc 100644 --- a/src/executors/inmemory.rs +++ b/src/executors/inmemory.rs @@ -1,9 +1,11 @@ extern crate alloc; +use alloc::boxed::Box; use alloc::rc::Rc; +use alloc::vec::Vec; use core::cell::RefCell; -use std::os::raw::c_void; -use std::ptr; +use core::ffi::c_void; +use core::ptr; use crate::executors::{Executor, ExitKind}; use crate::feedbacks::Feedback; @@ -89,6 +91,7 @@ where I: Input, { pub fn new(harness_fn: HarnessFunction) -> Self { + #[cfg(feature = "std")] unsafe { os_signals::setup_crash_handlers::(); } @@ -100,6 +103,7 @@ where } } +#[cfg(feature = "std")] #[cfg(unix)] pub mod unix_signals { @@ -130,9 +134,13 @@ pub mod unix_signals { ); } } - // TODO: LLMP + + #[cfg(feature = "std")] println!("Child crashed!"); + #[cfg(feature = "std")] let _ = stdout().flush(); + + // TODO: LLMP } pub extern "C" fn libaflrs_executor_inmem_handle_timeout( @@ -183,13 +191,18 @@ pub mod unix_signals { } } +#[cfg(feature = "std")] #[cfg(unix)] use unix_signals as os_signals; +#[cfg(feature = "std")] #[cfg(not(unix))] compile_error!("InMemoryExecutor not yet supported on this OS"); #[cfg(test)] -mod tests { + mod tests { + + extern crate alloc; + use alloc::boxed::Box; use crate::executors::inmemory::InMemoryExecutor; use crate::executors::{Executor, ExitKind}; use crate::inputs::Input; @@ -218,8 +231,14 @@ mod tests { } } + #[cfg(feature = "std")] fn test_harness_fn_nop(_executor: &dyn Executor, buf: &[u8]) -> ExitKind { - println! {"Fake exec with buf of len {}", buf.len()}; + println!{"Fake exec with buf of len {}", buf.len()}; + ExitKind::Ok + } + + #[cfg(not(feature = "std"))] + fn test_harness_fn_nop(_executor: &dyn Executor, _buf: &[u8]) -> ExitKind { ExitKind::Ok } diff --git a/src/executors/mod.rs b/src/executors/mod.rs index a539acd372..5f0933d7c7 100644 --- a/src/executors/mod.rs +++ b/src/executors/mod.rs @@ -1,6 +1,8 @@ -extern crate alloc; pub mod inmemory; +use alloc::boxed::Box; +use alloc::vec::Vec; + use crate::corpus::TestcaseMetadata; use crate::feedbacks::Feedback; use crate::inputs::Input; diff --git a/src/feedbacks/mod.rs b/src/feedbacks/mod.rs index 33d3395550..4a73c399d2 100644 --- a/src/feedbacks/mod.rs +++ b/src/feedbacks/mod.rs @@ -1,10 +1,9 @@ -extern crate alloc; extern crate num; +use alloc::boxed::Box; +use alloc::vec::Vec; use core::cell::RefCell; use core::marker::PhantomData; - -use alloc::vec::*; use num::Integer; use crate::corpus::TestcaseMetadata; diff --git a/src/inputs/bytes.rs b/src/inputs/bytes.rs index f518c98099..5c3b0a4131 100644 --- a/src/inputs/bytes.rs +++ b/src/inputs/bytes.rs @@ -1,4 +1,3 @@ -extern crate alloc; use alloc::borrow::ToOwned; use alloc::rc::Rc; diff --git a/src/inputs/mod.rs b/src/inputs/mod.rs index f291264ac2..bbdf3d71ce 100644 --- a/src/inputs/mod.rs +++ b/src/inputs/mod.rs @@ -1,4 +1,3 @@ -extern crate alloc; pub mod bytes; pub use bytes::BytesInput; @@ -30,7 +29,7 @@ pub trait Input: Clone { #[cfg(not(feature = "std"))] /// Write this input to the file - fn to_file

(&self, string: P) -> Result<(), AflError> + fn to_file

(&self, _path: P) -> Result<(), AflError> where { Err(AflError::NotImplemented("Not suppored in no_std".into())) } @@ -49,7 +48,7 @@ where { /// Write this input to the file #[cfg(not(feature = "std"))] - fn from_file

(string: P) -> Result + fn from_file

(_path: P) -> Result where { Err(AflError::NotImplemented("Not suppored in no_std".into())) } diff --git a/src/lib.rs b/src/lib.rs index af0253b448..6f5e6e1a0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] + #[macro_use] extern crate alloc; diff --git a/src/mutators/mod.rs b/src/mutators/mod.rs index 978c792326..ba0582c98f 100644 --- a/src/mutators/mod.rs +++ b/src/mutators/mod.rs @@ -1,4 +1,3 @@ -extern crate alloc; pub mod scheduled; pub use scheduled::ComposedByMutations; diff --git a/src/mutators/scheduled.rs b/src/mutators/scheduled.rs index 8c59575175..1e93ff787d 100644 --- a/src/mutators/scheduled.rs +++ b/src/mutators/scheduled.rs @@ -1,4 +1,3 @@ -extern crate alloc; use crate::inputs::{HasBytesVec, Input}; use crate::mutators::Corpus; diff --git a/src/observers/mod.rs b/src/observers/mod.rs index b27ed5aaf7..7f4917dd18 100644 --- a/src/observers/mod.rs +++ b/src/observers/mod.rs @@ -1,4 +1,3 @@ -extern crate alloc; extern crate num; use core::slice::from_raw_parts_mut; diff --git a/src/stages/mod.rs b/src/stages/mod.rs index e021f346ae..092680bfea 100644 --- a/src/stages/mod.rs +++ b/src/stages/mod.rs @@ -1,4 +1,3 @@ -extern crate alloc; pub mod mutational; pub use mutational::DefaultMutationalStage; diff --git a/src/stages/mutational.rs b/src/stages/mutational.rs index a38555b61b..fe78bd6f9b 100644 --- a/src/stages/mutational.rs +++ b/src/stages/mutational.rs @@ -1,4 +1,3 @@ -extern crate alloc; use alloc::rc::Rc; use core::cell::RefCell; diff --git a/src/utils.rs b/src/utils.rs index 553664c13f..3de7d72cb9 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,4 @@ //! Utility functions for AFL -extern crate alloc; use alloc::rc::Rc; use core::cell::RefCell;