Is there a way to read a volatile value from an unaligned address on stable Rust?

Viewed 230

I want to fetch ACPI's XSDT. Getting the table needs volatile access. Also, the header or the entries may not be well-aligned because the header size is 36 bytes, while the required alignment is 8 bytes. So, I want a function that reads a volatile value from an unaligned address. read_volatile requires a well-aligned address. Rust also provides unaligned_volatile_load, but I don't want to use any nightly features because their behaviors frequently change. Is there a way to do this on stable Rust?

1 Answers

Since the header is only 36 bytes, you can use std::ptr::read_volatile to copy it with minimal overhead into a [u8; 36]. From there you can use std::ptr::read_unaligned which does not require nightly.

If you are mostly concerned with a nightly build changing you could also choose a specific nightly build and stick to that so it is at least consistent, e.g.:

rustup install nightly-2021-08-09
Related