Skip to content

Commit e49c718

Browse files
domenukkriesentoaster
authored andcommitted
Actually make ConstMapObserver work, introduce nonnull_raw_mut macro (AFLplusplus#2687)
* Actually make ConstMapObserver work * fixes * does that work? * mas
1 parent 3854a2e commit e49c718

File tree

9 files changed

+45
-27
lines changed

9 files changed

+45
-27
lines changed

fuzzers/baby/backtrace_baby_fuzzers/c_code_with_fork_executor/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ pub fn main() {
4747
};
4848
// Create an observation channel using the signals map
4949
let observer = unsafe {
50-
ConstMapObserver::<u8, 3>::from_mut_ptr(
50+
ConstMapObserver::from_mut_ptr(
5151
"signals",
52-
NonNull::new(map_ptr).expect("map ptr is null."),
52+
NonNull::new(map_ptr)
53+
.expect("map ptr is null.")
54+
.cast::<[u8; 3]>(),
5355
)
5456
};
5557
// Create a stacktrace observer

fuzzers/baby/backtrace_baby_fuzzers/c_code_with_inprocess_executor/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ pub fn main() {
3636
};
3737
// Create an observation channel using the signals map
3838
let observer = unsafe {
39-
ConstMapObserver::<u8, 3>::from_mut_ptr(
39+
ConstMapObserver::from_mut_ptr(
4040
"signals",
41-
NonNull::new(array_ptr).expect("map ptr is null"),
41+
NonNull::new(array_ptr)
42+
.expect("map ptr is null")
43+
.cast::<[u8; 3]>(),
4244
)
4345
};
4446
// Create a stacktrace observer

fuzzers/binary_only/fuzzbench_fork_qemu/src/fuzzer.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,11 @@ fn fuzz(
159159

160160
// Create an observation channel using the coverage map
161161
let mut edges_observer = unsafe {
162-
HitcountsMapObserver::new(ConstMapObserver::<_, EDGES_MAP_DEFAULT_SIZE>::from_mut_ptr(
162+
HitcountsMapObserver::new(ConstMapObserver::from_mut_ptr(
163163
"edges",
164-
NonNull::new(edges.as_mut_ptr()).expect("map ptr is null."),
164+
NonNull::new(edges.as_mut_ptr())
165+
.expect("map ptr is null.")
166+
.cast::<[u8; EDGES_MAP_DEFAULT_SIZE]>(),
165167
))
166168
.track_indices()
167169
};

fuzzers/binary_only/qemu_cmin/src/fuzzer.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,11 @@ pub fn fuzz() -> Result<(), Error> {
162162
unsafe { EDGES_MAP_PTR = edges.as_mut_ptr() };
163163

164164
let mut edges_observer = unsafe {
165-
HitcountsMapObserver::new(ConstMapObserver::<_, EDGES_MAP_DEFAULT_SIZE>::from_mut_ptr(
165+
HitcountsMapObserver::new(ConstMapObserver::from_mut_ptr(
166166
"edges",
167-
NonNull::new(edges.as_mut_ptr()).expect("The edge map pointer is null."),
167+
NonNull::new(edges.as_mut_ptr())
168+
.expect("The edge map pointer is null.")
169+
.cast::<[u8; EDGES_MAP_DEFAULT_SIZE]>(),
168170
))
169171
};
170172

fuzzers/structure_aware/baby_fuzzer_multi/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2021"
1010

1111
[features]
1212
default = ["std"]
13-
tui = []
13+
tui = ["libafl/tui_monitor"]
1414
std = []
1515

1616
[profile.dev]
@@ -24,9 +24,6 @@ opt-level = 3
2424
debug = true
2525

2626
[dependencies]
27-
libafl = { path = "../../../libafl", features = [
28-
"multipart_inputs",
29-
"tui_monitor",
30-
] }
27+
libafl = { path = "../../../libafl", features = ["multipart_inputs"] }
3128
libafl_bolts = { path = "../../../libafl_bolts" }
3229
log = { version = "0.4.22", features = ["release_max_level_info"] }

fuzzers/structure_aware/baby_fuzzer_multi/src/main.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use std::path::PathBuf;
12
#[cfg(windows)]
23
use std::ptr::write_volatile;
3-
use std::{path::PathBuf, ptr::write};
44

55
#[cfg(feature = "tui")]
66
use libafl::monitors::tui::TuiMonitor;
@@ -15,25 +15,24 @@ use libafl::{
1515
fuzzer::{Fuzzer, StdFuzzer},
1616
inputs::{BytesInput, HasTargetBytes, MultipartInput},
1717
mutators::{havoc_mutations::havoc_mutations, scheduled::StdScheduledMutator},
18-
observers::StdMapObserver,
18+
observers::ConstMapObserver,
1919
schedulers::QueueScheduler,
2020
stages::mutational::StdMutationalStage,
2121
state::StdState,
2222
Evaluator,
2323
};
24-
use libafl_bolts::{rands::StdRand, tuples::tuple_list, AsSlice};
24+
use libafl_bolts::{nonnull_raw_mut, rands::StdRand, tuples::tuple_list, AsSlice};
2525

2626
/// Coverage map with explicit assignments due to the lack of instrumentation
2727
static mut SIGNALS: [u8; 128] = [0; 128];
28-
static mut SIGNALS_PTR: *mut u8 = unsafe { SIGNALS.as_mut_ptr() };
28+
static mut SIGNALS_PTR: *mut [u8; 128] = &raw mut SIGNALS;
2929

3030
/// "Coverage" map for count, just to help things along
3131
static mut LAST_COUNT: [usize; 1] = [usize::MAX];
32-
static mut LAST_COUNT_PTR: *mut usize = unsafe { LAST_COUNT.as_mut_ptr() };
3332

3433
/// Assign a signal to the signals map
3534
fn signals_set(idx: usize) {
36-
unsafe { write(SIGNALS_PTR.add(idx), 1) };
35+
unsafe { (*SIGNALS_PTR)[idx] = 1 };
3736
}
3837

3938
/// Assign a count to the count "map"
@@ -83,9 +82,9 @@ pub fn main() {
8382

8483
// Create an observation channel using the signals map
8584
let signals_observer =
86-
unsafe { StdMapObserver::from_mut_ptr("signals", SIGNALS_PTR, SIGNALS.len()) };
85+
unsafe { ConstMapObserver::from_mut_ptr("signals", nonnull_raw_mut!(SIGNALS)) };
8786
let mut count_observer =
88-
unsafe { StdMapObserver::from_mut_ptr("count", LAST_COUNT_PTR, LAST_COUNT.len()) };
87+
unsafe { ConstMapObserver::from_mut_ptr("count", nonnull_raw_mut!(LAST_COUNT)) };
8988
*count_observer.initial_mut() = usize::MAX; // we are minimising!
9089

9190
// Feedback to rate the interestingness of an input

libafl/src/observers/map/const_map.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,16 @@ where
199199
/// # Safety
200200
/// Will dereference the `map_ptr` with up to len elements.
201201
#[must_use]
202-
pub unsafe fn from_mut_ptr(name: &'static str, map_ptr: NonNull<T>) -> Self {
202+
pub unsafe fn from_mut_ptr(name: &'static str, map_ptr: NonNull<[T; N]>) -> Self {
203203
ConstMapObserver {
204204
map: OwnedMutSizedSlice::from_raw_mut(map_ptr),
205205
name: Cow::from(name),
206206
initial: T::default(),
207207
}
208208
}
209+
210+
/// Gets the initial value for this map, mutably
211+
pub fn initial_mut(&mut self) -> &mut T {
212+
&mut self.initial
213+
}
209214
}

libafl_bolts/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,18 @@ macro_rules! nonzero {
11221122
};
11231123
}
11241124

1125+
/// Get a [`core::ptr::NonNull`] to a global static mut (or similar).
1126+
///
1127+
/// The same as [`core::ptr::addr_of_mut`] or `&raw mut`, but wrapped in said [`NonNull`](core::ptr::NonNull).
1128+
#[macro_export]
1129+
macro_rules! nonnull_raw_mut {
1130+
($val:expr) => {
1131+
// # Safety
1132+
// The pointer to a value will never be null (unless we're on an archaic OS in a CTF challenge).
1133+
unsafe { core::ptr::NonNull::new(&raw mut $val).unwrap_unchecked() }
1134+
};
1135+
}
1136+
11251137
#[cfg(feature = "python")]
11261138
#[allow(missing_docs)]
11271139
pub mod pybind {

libafl_bolts/src/ownedref.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -944,12 +944,9 @@ impl<'a, T: 'a + Sized, const N: usize> OwnedMutSizedSlice<'a, T, N> {
944944
/// The pointer must be valid and point to a map of the size `size_of<T>() * N`
945945
/// The content will be dereferenced in subsequent operations.
946946
#[must_use]
947-
pub unsafe fn from_raw_mut(ptr: NonNull<T>) -> OwnedMutSizedSlice<'a, T, N> {
947+
pub unsafe fn from_raw_mut(ptr: NonNull<[T; N]>) -> OwnedMutSizedSlice<'a, T, N> {
948948
Self {
949-
inner: OwnedMutSizedSliceInner::RefRaw(
950-
ptr.as_ptr() as *mut [T; N],
951-
UnsafeMarker::new(),
952-
),
949+
inner: OwnedMutSizedSliceInner::RefRaw(ptr.as_ptr(), UnsafeMarker::new()),
953950
}
954951
}
955952

0 commit comments

Comments
 (0)