Closed 00xc closed 11 months ago
The
heap
object has a mutable reference toHEAP_MEM
, which gets invalidated when calculatingremaining_space
, as it does so through a mut pointer. Thus, usingheap
after using the pointer is technically undefined behavior under Rust's aliasing rules.
Wouldn't get the mutable reference in heap also get invalidated by the creation of a shared reference? Note that you can also create a pointer to the HEAP_MEM
global without creating a reference by using the addr_of!
macro.
Wouldn't get the mutable reference in heap also get invalidated by the creation of a shared reference?
Technically we are not creating any reference, right? Miri does not seem to complain either.
Note that you can also create a pointer to the
HEAP_MEM
global without creating a reference by using theaddr_of!
macro.
Yeah maybe this is cleaner, thanks!
Wouldn't get the mutable reference in heap also get invalidated by the creation of a shared reference?
Technically we are not creating any reference, right? Miri does not seem to complain either.
The call to slice::as_ptr
creates a reference to the slice. I'm not sure why Miri doesn't complain.
The chaos harness has a potential UB bug reported by Miri due to mutable pointer aliasing. The
heap
object has a mutable reference toHEAP_MEM
, which gets invalidated when calculatingremaining_space
, as it does so through a mut pointer. Thus, usingheap
after using the pointer is technically undefined behavior under Rust's aliasing rules.https://github.com/rust-osdev/linked-list-allocator/blob/3c9bafaf88918a77615e6692c6fffbc7ab3428b5/fuzz/fuzz_targets/chaos.rs#L36
https://github.com/rust-osdev/linked-list-allocator/blob/3c9bafaf88918a77615e6692c6fffbc7ab3428b5/fuzz/fuzz_targets/chaos.rs#L84-L87
https://github.com/rust-osdev/linked-list-allocator/blob/3c9bafaf88918a77615e6692c6fffbc7ab3428b5/fuzz/fuzz_targets/chaos.rs#L94
Fix this by taking a const pointer.
Note that it is very unlikely this caused any actual issues under the current state of the compiler.
This can be tested by running the following reproducer (a simplified version of the chaos harness) under Miri (
cargo +nightly miri run
).Reproducer
```rust use linked_list_allocator::Heap; use std::alloc::Layout; use std::ptr::NonNull; #[derive(Debug)] enum Action { // allocate a chunk with the size specified Alloc { size: u16, align_bit: u8 }, // free the pointer at the index specified Free { index: u8 }, // extend the heap by amount specified Extend { additional: u16 }, } use Action::*; const MAX_HEAP_SIZE: usize = 5000; static mut HEAP_MEM: [u8; MAX_HEAP_SIZE] = [0; MAX_HEAP_SIZE]; fn main() { let actions = vec![ Alloc { size: 25, align_bit: 1, }, Extend { additional: 255 }, ]; let size = 100; fuzz(size, actions); } fn fuzz(size: u16, actions: Vec