Skip to content

Commit 60ed625

Browse files
committed
fix for high optimisation levels
1 parent ab30eac commit 60ed625

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

libafl_targets/src/libfuzzer/observers.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use core::{ffi::c_void, fmt::Debug};
2-
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
2+
use std::{
3+
ptr::{read_volatile, write_volatile},
4+
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
5+
};
36

47
use libafl::{
58
bolts::tuples::Named,
@@ -20,29 +23,29 @@ extern "C" {
2023
}
2124

2225
static OOMED: AtomicBool = AtomicBool::new(false);
23-
static mut RSS_MAX: usize = 2 << 30; // 2GB, which is a typical default
26+
static RSS_MAX: AtomicUsize = AtomicUsize::new(2 << 30); // 2GB, which is the default
2427

2528
static MALLOC_SIZE: AtomicUsize = AtomicUsize::new(0);
2629

27-
extern "C" fn oom_malloc_hook(ptr: *const c_void, size: usize) {
30+
pub extern "C" fn oom_malloc_hook(ptr: *const c_void, size: usize) {
2831
let size = match unsafe { libafl_check_malloc_size(ptr) } {
2932
0 => size,
3033
real => real,
3134
};
3235

3336
let total = MALLOC_SIZE.fetch_add(size, Ordering::Relaxed) + size;
34-
if total > unsafe { RSS_MAX } && !OOMED.load(Ordering::Relaxed) {
37+
if total > RSS_MAX.load(Ordering::Relaxed) && !OOMED.load(Ordering::Relaxed) {
3538
OOMED.store(true, Ordering::Relaxed);
3639
unsafe {
3740
// we need to kill the process in a way that immediately triggers the crash handler
3841
let null = core::ptr::null_mut();
39-
*null = 0;
42+
write_volatile(null, 0);
4043
panic!("We somehow didn't crash on a null pointer write. Strange...");
4144
}
4245
}
4346
}
4447

45-
extern "C" fn oom_free_hook(ptr: *const c_void) {
48+
pub extern "C" fn oom_free_hook(ptr: *const c_void) {
4649
let size = unsafe { libafl_check_malloc_size(ptr) };
4750
if MALLOC_SIZE
4851
.fetch_sub(size, Ordering::Relaxed)
@@ -63,8 +66,12 @@ pub struct OOMObserver {
6366
impl OOMObserver {
6467
pub fn new(rss_max: usize) -> Self {
6568
unsafe {
66-
RSS_MAX = rss_max;
67-
__sanitizer_install_malloc_and_free_hooks(Some(oom_malloc_hook), Some(oom_free_hook));
69+
RSS_MAX.store(rss_max, Ordering::Relaxed);
70+
if __sanitizer_install_malloc_and_free_hooks(Some(oom_malloc_hook), Some(oom_free_hook))
71+
== 0
72+
{
73+
panic!("Could not install malloc and free hooks");
74+
}
6875
}
6976
Self { oomed: false }
7077
}

0 commit comments

Comments
 (0)