Moved to no_std preamble (#643)

* Moved to no_std preamble

* fixed use

* no_std targets

* derive no_std

* fix yml

* ci

* alf

* gitignore

* fix python build

* import cleanup

* nostd

* linux fix
This commit is contained in:
Dominik Maier 2022-05-27 01:04:29 +02:00 committed by GitHub
parent 5887d1a7b7
commit 763ed9a3e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 109 additions and 14 deletions

View File

@ -38,6 +38,10 @@ jobs:
run: cargo test
- name: Test libafl no_std
run: cd libafl && cargo test --no-default-features
- name: Test libafl_targets no_std
run: cd libafl_targets && cargo test --no-default-features
ubuntu:
@ -171,7 +175,7 @@ jobs:
run: cd ./fuzzers/baby_no_std && cargo +nightly run || test $? -ne 0 || exit 1
- name: no_std tests
run: cd ./libafl && cargo test --no-default-features
- name: armv6m-none-eabi (32 bit no_std) clippy
- name: libafl armv6m-none-eabi (32 bit no_std) clippy
run: cd ./libafl && cargo clippy --target thumbv6m-none-eabi --no-default-features
build-docker:
runs-on: ubuntu-latest

2
.gitignore vendored
View File

@ -42,3 +42,5 @@ a
forkserver_test
__pycache__
*.lafl_lock
*atomic_file_testfile*

View File

@ -63,13 +63,17 @@
//! }
//!```
#[cfg(feature = "frida_cli")]
use alloc::boxed::Box;
use alloc::{
string::{String, ToString},
vec::Vec,
};
use clap::{Command, CommandFactory, Parser};
use serde::{Deserialize, Serialize};
#[cfg(feature = "frida_cli")]
use std::error;
use std::net::SocketAddr;
use std::path::PathBuf;
use std::time::Duration;
use std::{net::SocketAddr, path::PathBuf, time::Duration};
use super::os::Cores;
use crate::Error;
@ -367,6 +371,8 @@ pub fn parse_args() -> FuzzerOptions {
))]
mod tests {
use super::*;
#[cfg(feature = "frida_cli")]
use alloc::string::String;
/// pass a standard option and `--` followed by some options that `FuzzerOptions` doesn't know
/// about; expect the standard option to work normally, and everything after `--` to be

View File

@ -9,6 +9,9 @@ use std::{
#[cfg(unix)]
use std::os::unix::prelude::{AsRawFd, RawFd};
#[cfg(feature = "std")]
use alloc::borrow::ToOwned;
use crate::Error;
/// The default filename to use to deliver testcases to the target

View File

@ -24,6 +24,8 @@ use crate::{
Error,
};
#[cfg(feature = "std")]
use alloc::string::ToString;
use core::fmt::{self, Debug, Formatter};
#[cfg(feature = "std")]
use core::marker::PhantomData;

View File

@ -73,6 +73,8 @@ use core::{
};
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use alloc::string::ToString;
#[cfg(feature = "std")]
use std::{
env,

View File

@ -12,6 +12,12 @@ use crate::{
},
Error,
};
#[cfg(feature = "std")]
use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::{mem::ManuallyDrop, ptr::addr_of};
use hashbrown::HashMap;
use serde::{Deserialize, Serialize};

View File

@ -539,6 +539,7 @@ pub mod unix_shmem {
#[cfg(all(unix, feature = "std", not(target_os = "android")))]
mod default {
use alloc::string::ToString;
use core::{ptr, slice};
use libc::{
c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, close, ftruncate, mmap, munmap,
@ -919,6 +920,7 @@ pub mod unix_shmem {
/// Module containing `ashmem` shared memory support, commonly used on Android.
#[cfg(all(unix, feature = "std"))]
pub mod ashmem {
use alloc::string::ToString;
use core::{ptr, slice};
use libc::{
c_uint, c_ulong, c_void, close, ioctl, mmap, open, MAP_SHARED, O_RDWR, PROT_READ,
@ -1152,6 +1154,7 @@ pub mod win32_shmem {
Error,
};
use alloc::string::String;
use core::{
ffi::c_void,
fmt::{self, Debug, Formatter},

View File

@ -1,6 +1,7 @@
//! Stores and restores state when a client needs to relaunch.
//! Uses a [`ShMem`] up to a threshold, then write to disk.
use ahash::AHasher;
use alloc::string::{String, ToString};
use core::{hash::Hasher, marker::PhantomData, mem::size_of, ptr, slice};
use serde::{de::DeserializeOwned, Serialize};
use std::{
@ -241,6 +242,10 @@ where
#[cfg(test)]
mod tests {
use alloc::{
string::{String, ToString},
vec::Vec,
};
use serial_test::serial;
use crate::bolts::{

View File

@ -140,6 +140,7 @@ pub mod pybind {
use crate::corpus::pybind::PythonCorpus;
use crate::corpus::CachedOnDiskCorpus;
use crate::inputs::BytesInput;
use alloc::string::String;
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

View File

@ -211,6 +211,7 @@ pub mod pybind {
use crate::corpus::pybind::PythonCorpus;
use crate::corpus::OnDiskCorpus;
use crate::inputs::BytesInput;
use alloc::string::String;
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

View File

@ -354,6 +354,7 @@ pub mod pybind {
use crate::bolts::ownedref::OwnedPtrMut;
use crate::inputs::{BytesInput, HasBytesVec};
use crate::pybind::PythonMetadata;
use alloc::{boxed::Box, vec::Vec};
use pyo3::prelude::*;
use pyo3::types::PyDict;

View File

@ -4,6 +4,8 @@ use core::{
marker::PhantomData,
};
#[cfg(feature = "std")]
use alloc::{borrow::ToOwned, string::String, vec::Vec};
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
#[cfg(feature = "std")]
@ -696,6 +698,8 @@ mod tests {
#[test]
#[cfg(unix)]
fn test_parse_afl_cmdline() {
use alloc::string::ToString;
let mut mgr = SimpleEventManager::<BytesInput, _>::new(SimpleMonitor::new(|status| {
println!("{}", status);
}));

View File

@ -27,6 +27,7 @@ use crate::{
Error,
};
use alloc::{borrow::ToOwned, string::ToString, vec::Vec};
use nix::{
sys::{
select::{pselect, FdSet},

View File

@ -18,6 +18,8 @@ use core::{
};
use alloc::boxed::Box;
#[cfg(all(unix, feature = "std"))]
use alloc::vec::Vec;
#[cfg(all(feature = "std", unix))]
use std::intrinsics::transmute;
@ -507,6 +509,8 @@ pub fn inprocess_get_input<'a, I>() -> Option<&'a I> {
#[cfg(unix)]
mod unix_signal_handler {
use alloc::vec::Vec;
#[cfg(feature = "std")]
use alloc::{boxed::Box, string::String};
use core::mem::transmute;
use libc::siginfo_t;
#[cfg(feature = "std")]
@ -854,6 +858,9 @@ mod unix_signal_handler {
#[cfg(all(windows, feature = "std"))]
mod windows_exception_handler {
#[cfg(feature = "std")]
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
use core::ffi::c_void;
use core::{mem::transmute, ptr};
@ -1550,6 +1557,7 @@ where
/// signal handlers and `panic_hooks` for the child process
#[cfg(all(feature = "std", unix))]
pub mod child_signal_handlers {
use alloc::boxed::Box;
use libc::siginfo_t;
use std::panic;
@ -1683,6 +1691,7 @@ pub mod pybind {
use crate::inputs::{BytesInput, HasBytesVec};
use crate::observers::pybind::PythonObserversTuple;
use crate::state::pybind::{PythonStdState, PythonStdStateWrapper};
use alloc::boxed::Box;
use pyo3::prelude::*;
use pyo3::types::PyBytes;

View File

@ -3,6 +3,8 @@
//! This feedback should be used in combination with another feedback as this feedback always considers testcases
//! to be not interesting.
//! Requires a [`ConcolicObserver`] to observe the concolic trace.
use alloc::{borrow::ToOwned, string::String};
use crate::{
bolts::tuples::Named,
corpus::Testcase,

View File

@ -1,4 +1,5 @@
//! Nautilus grammar mutator, see <https://github.com/nautilus-fuzz/nautilus>
use alloc::string::String;
use core::fmt::Debug;
use grammartec::{chunkstore::ChunkStore, context::Context};
use serde::{Deserialize, Serialize};

View File

@ -2,6 +2,7 @@
use std::{fmt::Debug, marker::PhantomData};
use alloc::string::{String, ToString};
use hashbrown::HashSet;
use serde::{Deserialize, Serialize};

View File

@ -682,6 +682,7 @@ pub mod pybind {
use crate::schedulers::QueueScheduler;
use crate::stages::pybind::PythonStagesTuple;
use crate::state::pybind::{PythonStdState, PythonStdStateWrapper};
use alloc::{boxed::Box, vec::Vec};
use pyo3::prelude::*;
/// `StdFuzzer` with fixed generics

View File

@ -134,6 +134,7 @@ pub mod pybind {
use crate::inputs::{BytesInput, HasBytesVec};
use crate::state::pybind::{PythonStdState, PythonStdStateWrapper};
use crate::Error;
use alloc::vec::Vec;
use pyo3::prelude::*;
#[derive(Clone, Debug)]

View File

@ -1,6 +1,9 @@
//! Generators for the [`Nautilus`](https://github.com/RUB-SysSec/nautilus) grammar fuzzer
use crate::{generators::Generator, inputs::nautilus::NautilusInput, Error};
use alloc::{string::String, vec::Vec};
use alloc::{
string::{String, ToString},
vec::Vec,
};
use core::fmt::Debug;
use grammartec::context::Context;
use std::{fs, io::BufReader, path::Path};

View File

@ -5,6 +5,8 @@
use ahash::AHasher;
use core::hash::Hasher;
#[cfg(feature = "std")]
use alloc::string::ToString;
use alloc::{borrow::ToOwned, rc::Rc, string::String, vec::Vec};
#[cfg(feature = "std")]
use core::str::from_utf8;
@ -256,6 +258,8 @@ impl EncodedInput {
#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
use alloc::borrow::ToOwned;
use crate::inputs::encoded::{
InputDecoder, InputEncoder, NaiveTokenizer, TokenInputEncoderDecoder,
};

View File

@ -4,7 +4,7 @@
//use ahash::AHasher;
//use core::hash::Hasher;
use alloc::{rc::Rc, string::String};
use alloc::{rc::Rc, string::String, vec::Vec};
use core::{cell::RefCell, convert::From};
use serde::{Deserialize, Serialize};

View File

@ -3,7 +3,7 @@ Welcome to `LibAFL`
*/
#![allow(incomplete_features)]
#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]
// For `type_eq`
#![cfg_attr(unstable_feature, feature(specialization))]
// For `type_id` and owned things
@ -69,6 +69,9 @@ Welcome to `LibAFL`
)
)]
#[cfg(feature = "std")]
#[macro_use]
extern crate std;
#[macro_use]
pub extern crate alloc;
#[macro_use]
@ -573,6 +576,7 @@ pub mod pybind {
macro_rules! impl_serde_pyobjectwrapper {
($struct_name:ident, $inner:tt) => {
const _: () = {
use alloc::vec::Vec;
use pyo3::prelude::*;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

View File

@ -822,6 +822,7 @@ pub mod pybind {
use pyo3::types::PyUnicode;
use super::ClientStats;
use alloc::{boxed::Box, string::String, vec::Vec};
use core::time::Duration;
// TODO create a PyObjectFnMut to pass, track stabilization of https://github.com/rust-lang/rust/issues/29625

View File

@ -1,5 +1,6 @@
use super::{current_time, format_duration_hms, Duration, String, TimedStats, TuiContext};
use alloc::vec::Vec;
use tui::{
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},

View File

@ -170,16 +170,16 @@ impl Tokens {
}
let pos_quote = match line.find('\"') {
Some(x) => x,
None => return Err(Error::illegal_argument("Illegal line: ".to_owned() + line)),
None => return Err(Error::illegal_argument(format!("Illegal line: {}", line))),
};
if line.chars().nth(line.len() - 1) != Some('"') {
return Err(Error::illegal_argument("Illegal line: ".to_owned() + line));
return Err(Error::illegal_argument(format!("Illegal line: {}", line)));
}
// extract item
let item = match line.get(pos_quote + 1..line.len() - 1) {
Some(x) => x,
None => return Err(Error::illegal_argument("Illegal line: ".to_owned() + line)),
None => return Err(Error::illegal_argument(format!("Illegal line: {}", line))),
};
if item.is_empty() {
continue;
@ -189,9 +189,10 @@ impl Tokens {
let token: Vec<u8> = match str_decode(item) {
Ok(val) => val,
Err(_) => {
return Err(Error::illegal_argument(
"Illegal line (hex decoding): ".to_owned() + line,
))
return Err(Error::illegal_argument(format!(
"Illegal line (hex decoding): {}",
line
)))
}
};

View File

@ -1,4 +1,5 @@
use crate::observers::concolic::{serialization_format::MessageFileReader, SymExpr, SymExprRef};
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
/// A metadata holding a buffer of a concolic trace.

View File

@ -4,6 +4,8 @@ use core::{
num::NonZeroUsize,
};
#[cfg(feature = "std")]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

View File

@ -5,6 +5,7 @@ use crate::{
Observer,
},
};
use alloc::string::String;
use serde::{Deserialize, Serialize};
/// A standard [`ConcolicObserver`] observer, observing constraints written into a memory buffer.

View File

@ -383,6 +383,8 @@ impl<W: Write + Seek> MessageFileWriter<W> {
mod serialization_tests {
use std::io::Cursor;
use alloc::vec::Vec;
use super::{MessageFileReader, MessageFileWriter, SymExpr};
/// This test intends to ensure that the serialization format can efficiently encode the required information.

View File

@ -1557,6 +1557,7 @@ pub mod pybind {
macro_rules! define_python_map_observer {
($struct_name1:ident, $py_name1:tt, $struct_name2:ident, $py_name2:tt, $struct_name_trait:ident, $py_name_trait:tt, $datatype:ty, $wrapper_name: ident) => {
#[pyclass(unsendable, name = $py_name1)]
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Serialize, Deserialize, Debug, Clone)]
@ -1628,7 +1629,7 @@ pub mod pybind {
fn new(name: String, map: Vec<$datatype>) -> Self {
Self {
//TODO: Not leak memory
inner: OwnedMapObserver::new(Box::leak(name.into_boxed_str()), map),
inner: OwnedMapObserver::new(alloc::boxed::Box::leak(name.into_boxed_str()), map),
}
}

View File

@ -8,6 +8,7 @@ use crate::{
Error,
};
use alloc::string::{String, ToString};
use backtrace::Backtrace;
use regex::Regex;
use serde::{Deserialize, Serialize};

View File

@ -2,6 +2,8 @@
//! The executor must explicitely support these observers.
//! For example, they are supported on the [`crate::executors::CommandExecutor`].
use alloc::string::String;
use crate::{bolts::tuples::Named, observers::Observer};
/// An observer that captures stdout of a target.

View File

@ -4,6 +4,10 @@
use core::marker::PhantomData;
use alloc::string::String;
#[cfg(feature = "concolic_mutation")]
use alloc::{borrow::ToOwned, string::ToString, vec::Vec};
use crate::{
corpus::Corpus,
executors::{Executor, HasObservers},

View File

@ -268,6 +268,7 @@ pub mod pybind {
use crate::stages::{Stage, StagesTuple};
use crate::state::pybind::{PythonStdState, PythonStdStateWrapper};
use crate::Error;
use alloc::vec::Vec;
use pyo3::prelude::*;
#[derive(Clone, Debug)]

View File

@ -668,6 +668,7 @@ pub mod pybind {
use crate::state::{
HasCorpus, HasExecutions, HasMaxSize, HasMetadata, HasRand, HasSolutions, StdState,
};
use alloc::{boxed::Box, vec::Vec};
use pyo3::prelude::*;
use pyo3::types::PyDict;
use std::path::PathBuf;

View File

@ -1,5 +1,6 @@
//! Derives for `LibAFL`
#![no_std]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(clippy::all)]
#![deny(clippy::pedantic)]

View File

@ -4,6 +4,7 @@
use core::fmt::{self, Debug, Formatter};
use alloc::string::{String, ToString};
use libafl::{
bolts::{ownedref::OwnedRefMut, tuples::Named},
executors::ExitKind,

View File

@ -2,6 +2,7 @@
//! writing basic-block trace files to be read by coverage analysis tools, such as [Lighthouse](https://github.com/gaasedelen/lighthouse),
//! [bncov](https://github.com/ForAllSecure/bncov), [dragondance](https://github.com/0ffffffffh/dragondance), etc.
use alloc::{string::String, vec::Vec};
use core::ptr::addr_of;
use libafl::Error;
use rangemap::RangeMap;

View File

@ -1,6 +1,7 @@
//! `libafl_targets` contains runtime code, injected in the target itself during compilation.
//!
//!
#![no_std]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
@ -58,6 +59,9 @@
)
)]
#[cfg(feature = "std")]
#[macro_use]
extern crate std;
#[allow(unused_imports)]
#[macro_use]
extern crate alloc;

View File

@ -2,6 +2,8 @@
//! This makes `LibAFL` interoperable with harnesses written for other fuzzers like `Libfuzzer` and [`AFLplusplus`](aflplus.plus).
//! We will interact with a C++ target, so use external c functionality
use alloc::{string::String, vec::Vec};
extern "C" {
/// int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
fn LLVMFuzzerTestOneInput(data: *const u8, size: usize) -> i32;