Fix baby_no_std (#846)

* Fixing baby_no_std

* Fixed warnings for no_std

* Fix aarch build, clippy

* oops nyx again

* Using CString from alloc
This commit is contained in:
Dominik Maier 2022-10-19 14:14:10 +02:00 committed by GitHub
parent e8b3d33bf4
commit 28ab5e224b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 19 deletions

View File

@ -20,5 +20,4 @@ static-alloc = "0.2.3"
[target.'cfg(unix)'.dependencies]
libc = "0.2"
cstr_core = "0.2.3"

View File

@ -0,0 +1,34 @@
[env]
FUZZER_NAME="fuzzer"
PROJECT_DIR = { script = ["pwd"] }
[tasks.unsupported]
script_runner="@shell"
script='''
echo "Cargo-make not integrated yet on this"
'''
# Fuzzer
[tasks.build]
command = "cargo"
args = ["build", "--release", "-Zbuild-std=core,alloc", "--target", "x86_64-unknown-linux-gnu"]
# Test
[tasks.test]
linux_alias = "test_unix"
mac_alias = "unsupported"
windows_alias = "unsupported"
[tasks.test_unix]
script='''
cargo run -Zbuild-std=core,alloc --target x86_64-unknown-linux-gnu || true
'''
dependencies = ["build"]
[tasks.build_aarch]
script = "cargo +nightly build -Zbuild-std=core,alloc --target aarch64-unknown-none -v --release"
# Clean
[tasks.clean]
command = "cargo"
args = ["clean"]

View File

@ -0,0 +1,5 @@
fn main() {
if std::env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or_default() == "unix" {
println!("cargo:rustc-link-lib=c");
};
}

View File

@ -0,0 +1 @@
nightly

View File

@ -1,14 +1,16 @@
#![no_std]
// Embedded targets: build with no_main
#![cfg_attr(not(any(windows, unix)), no_main)]
#![cfg_attr(not(any(windows)), no_main)]
// Embedded needs alloc error handlers which only work on nightly right now...
#![cfg_attr(not(any(windows, unix)), feature(default_alloc_error_handler))]
#[cfg(not(any(windows, unix)))]
use core::panic::PanicInfo;
#![cfg_attr(not(any(windows)), feature(default_alloc_error_handler))]
#[cfg(any(windows, unix))]
use cstr_core::CString;
extern crate alloc;
#[cfg(any(windows, unix))]
use alloc::ffi::CString;
#[cfg(not(any(windows)))]
use core::panic::PanicInfo;
use libafl::{
bolts::{current_nanos, rands::StdRand, tuples::tuple_list, AsSlice},
corpus::InMemoryCorpus,
@ -26,16 +28,22 @@ use libafl::{
state::StdState,
};
#[cfg(any(windows, unix))]
use libc::{c_char, printf};
use libc::{abort, printf};
use static_alloc::Bump;
#[global_allocator]
static A: Bump<[u8; 512 * 1024 * 1024]> = Bump::uninit();
#[cfg(not(any(windows, unix)))]
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
#[cfg(unix)]
unsafe {
abort();
}
#[cfg(not(unix))]
loop {
// On embedded, there's not much left to do.
}
}
/// Coverage map with explicit assignments due to the lack of instrumentation
@ -46,7 +54,7 @@ fn signals_set(idx: usize) {
unsafe { SIGNALS[idx] = 1 };
}
/// Provide custom time in no_std environment
/// Provide custom time in `no_std` environment
/// Use a time provider of your choice
#[no_mangle]
pub extern "C" fn external_current_millis() -> u64 {
@ -54,8 +62,12 @@ pub extern "C" fn external_current_millis() -> u64 {
1000
}
/// The main of this program.
/// # Panics
/// Will panic once the fuzzer finds the correct conditions.
#[allow(clippy::similar_names)]
pub fn main() {
#[no_mangle]
pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> isize {
// The closure that we want to fuzz
let mut harness = |input: &BytesInput| {
let target = input.target_bytes();
@ -65,6 +77,7 @@ pub fn main() {
signals_set(1);
if buf.len() > 1 && buf[1] == b'b' {
signals_set(2);
#[allow(clippy::manual_assert)]
if buf.len() > 2 && buf[2] == b'c' {
panic!("=)");
}
@ -104,10 +117,8 @@ pub fn main() {
// TODO: Print `s` here, if your target permits it.
#[cfg(any(windows, unix))]
unsafe {
printf(
b"%s\n\0".as_ptr() as *const c_char,
CString::new(s).unwrap().as_ptr() as *const c_char,
);
let s = CString::new(s).unwrap();
printf(b"%s\n\0".as_ptr().cast(), s.as_ptr());
}
});
@ -146,4 +157,6 @@ pub fn main() {
fuzzer
.fuzz_loop(&mut stages, &mut executor, &mut state, &mut mgr)
.expect("Error in the fuzzing loop");
0
}

View File

@ -13,7 +13,7 @@ categories = ["development-tools::testing", "emulators", "embedded", "os", "no-s
[features]
default = ["std", "derive", "llmp_compression", "rand_trait", "fork", "prelude"]
std = ["serde_json", "serde_json/std", "hostname", "nix", "serde/std", "bincode", "wait-timeout", "regex", "byteorder", "once_cell", "uuid", "tui_monitor", "ctor", "backtrace"] # print, env, launcher ... support
std = ["serde_json", "serde_json/std", "hostname", "nix", "serde/std", "bincode", "wait-timeout", "regex", "byteorder", "once_cell", "uuid", "tui_monitor", "ctor", "backtrace", "uds"] # print, env, launcher ... support
derive = ["libafl_derive"] # provide derive(SerdeAny) macro.
fork = [] # uses the fork() syscall to spawn children, instead of launching a new command, if supported by the OS (has no effect on Windows, no_std).
rand_trait = ["rand_core"] # If set, libafl's rand implementations will implement `rand::Rng`
@ -94,7 +94,7 @@ grammartec = { version = "0.2", optional = true }
[target.'cfg(unix)'.dependencies]
libc = "0.2" # For (*nix) libc
uds = "0.2.6"
uds = { version = "0.2.6", optional = true }
lock_api = "0.4.7"
[target.'cfg(windows)'.dependencies]

View File

@ -452,7 +452,7 @@ impl InProcessExecutorHandlerData {
self.in_target == 1
}
#[cfg(not(windows))]
#[cfg(unix)]
fn is_valid(&self) -> bool {
!self.current_input_ptr.is_null()
}