Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential unsound casting from volatile_register::RW to the wrapped type #485

Closed
shinmao opened this issue Sep 26, 2023 · 2 comments · Fixed by #493
Closed

Potential unsound casting from volatile_register::RW to the wrapped type #485

shinmao opened this issue Sep 26, 2023 · 2 comments · Fixed by #493

Comments

@shinmao
Copy link

shinmao commented Sep 26, 2023

Hi, I am wondering whether the casting here is unsound:

pub fn vect_active() -> VectActive {
let icsr =
unsafe { ptr::read_volatile(&(*SCB::PTR).icsr as *const _ as *const u32) } & 0x1FF;

Based on the definition, the wrapper struct volatile_register::RW (let me abbreviate it as vr here) didn't have stable layout, and rustc would preserve the right to insert padding or reorder the structure. vr<u32> should be declared as transparent; otherwise, casting it to u32 could lead to UB such as uninitialized memory exposure to ptr::read_volatile.

@adamgreig
Copy link
Member

Yep, you're right. In practice this is very unlikely to lead to any issues but in principle volatile_register should add a repr to the structs. There's an open PR to do so here: rust-embedded/volatile-register#9 but it's not merged yet.

@Dirbaio
Copy link
Member

Dirbaio commented Oct 20, 2023

volatile-register v0.2.2 is now out, it adds #[repr(transparent)] so the pointer cast is now sound.

@newAM newAM linked a pull request Nov 4, 2023 that will close this issue
github-merge-queue bot pushed a commit that referenced this issue Nov 4, 2023
cortex-m: update to volatile-register 0.2.2, fixes #485
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants