returning_works encounters UB, will fail in Rust 1.83
I am filing this issue because this project's tests failed in a crater run for this PR: https://github.com/rust-lang/rust/pull/130251 which should land in Rust 1.83.
The problem is that this crate can (under some conditions I do not understand but are encountered by returning_works
) apply a negative offset to a null pointer via ptr::offset
, which would cause the pointer to wrap around the address space. The standard library is getting a debug assertion for such wrapping offsets. I'm pretty sure that this is just a missing check if the pointer is null in the Drop
impl for Mapping
. Nothing too wild happens currently because this just tries to munmap
an address range that isn't mapped and that is well-defined. But we already tell LLVM that ptr::offset
does not wrap, and in this test it does, so surprise optimizations may ensue.
In any case, Miri can also be used to detect this bug today:
╰ ➤ RUSTFLAGS=-Awarnings cargo +nightly miri test returning_works
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.00s
Running unittests src/lib.rs (target/miri/x86_64-unknown-linux-gnu/debug/deps/apu_pcengines_hal-b14fa427df7385eb)
running 1 test
test tests::returning_works ... error: Undefined Behavior: out-of-bounds pointer arithmetic: expected a pointer to the end of 5376 bytes of memory, but got a null pointer
--> src/lib.rs:213:17
|
213 | self.gpio.offset((FCH_GPIO_OFFSET as isize) * -1),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ out-of-bounds pointer arithmetic: expected a pointer to the end of 5376 bytes of memory, but got a null pointer