Skip to content

Commit a5d8f2d

Browse files
committed
fuzz: remove potential undefined behavior in chaos harness
The chaos harness has a potential UB bug reported by Miri due to mutable pointer aliasing. The `heap` object has a mutable reference to `HEAP_MEM`, which gets invalidated when calculating `remaining_space`, as it does so through a mut pointer. Thus, using `heap` after using the pointer is technically undefined behavior under Rust's aliasing rules. Fix this by creating a const pointer via the `addr_of!()` macro. Note that it is very unlikely this caused any actual issues under the current state of the compiler. Signed-off-by: Carlos López <[email protected]>
1 parent 3c9bafa commit a5d8f2d

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

fuzz/fuzz_targets/chaos.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use arbitrary::Arbitrary;
33
use libfuzzer_sys::fuzz_target;
44
use linked_list_allocator::Heap;
55
use std::alloc::Layout;
6-
use std::ptr::NonNull;
6+
use std::ptr::{addr_of, NonNull};
77

88
#[derive(Debug, Arbitrary)]
99
enum Action {
@@ -81,8 +81,8 @@ fn fuzz(size: u16, actions: Vec<Action>) {
8181
Extend { additional } =>
8282
// safety: new heap size never exceeds MAX_HEAP_SIZE
8383
unsafe {
84-
let remaining_space = HEAP_MEM
85-
.as_mut_ptr()
84+
let remaining_space = addr_of!(HEAP_MEM)
85+
.cast::<u8>()
8686
.add(MAX_HEAP_SIZE)
8787
.offset_from(heap.top());
8888
assert!(remaining_space >= 0);

0 commit comments

Comments
 (0)