c2rust icon indicating copy to clipboard operation
c2rust copied to clipboard

Reads to volatiles are not mapped to `read_volatile`

Open chrysn opened this issue 5 years ago • 0 comments

The RIOT-OS code base I'm translating as part of the efforts around https://github.com/rust-lang/rust-bindgen/issues/1344 contains a function like

 static inline kernel_pid_t thread_getpid(void)
 {
     extern volatile kernel_pid_t sched_active_pid;
 
     return sched_active_pid;
 }

My previous manual translation of this was unsafe { ::core::ptr::read_volatile(&bindgen_generated_symbols::sched_active_pid) }. When translating with c2rust transpile comile_commands.json --preserve-unused-functions --emit-modules --emit-no-std (using #291), I get

#[inline]
pub unsafe extern "C" fn thread_getpid() -> kernel_pid_t {
    extern "C" {
        #[link_name = "sched_active_pid"]
        static mut sched_active_pid_0: kernel_pid_t;
    }
    return sched_active_pid;
}

which, in my understanding, allows the compiler to mush together multiple calls in a single function. That's not particularly bad for get_pid (which won't change in code that the Rust compiler will see as contiguous), but other functions will depend on the read to be volatile.

(I'm not fuly sure whether that's a bug in C2Rust, me expecting things it can't know or just the lack of a flag, but after looking through the docs and existing issues, I'm hoping for the former).

chrysn avatar Sep 03 '20 16:09 chrysn