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

This crate is currently unsound due to spurious reads #39

Closed
matthunz opened this issue Jun 6, 2023 · 1 comment · Fixed by #29
Closed

This crate is currently unsound due to spurious reads #39

matthunz opened this issue Jun 6, 2023 · 1 comment · Fixed by #29

Comments

@matthunz
Copy link

matthunz commented Jun 6, 2023

As pointed out in japaric/vcell#10, crates like this that use references to mmio are unsound.

I propose a new design that only stores raw pointers to prevent this behavior.
We could then initialize the cell using addr_of_mut to avoid any creation of references before that data is initialized.

pub struct VolatileCell<T> {
    ptr: *mut T,
}

impl<T> VolatileCell<T> {
    pub fn new(ptr: *mut T) -> Self {
        Self { ptr }
    }

    pub fn get(&self) -> T
    where
        T: Copy,
    {
        unsafe { self.ptr.read_volatile() }
    }

    pub fn get_mut(&self) -> T
    where
        T: Copy,
    {
        unsafe { self.ptr.read_volatile() }
    }
}
@phil-opp
Copy link
Member

Thanks for reporting! We are currently working on a new interface based on raw pointers in #29. It still requires some polishing, but I hope that it will be ready soon.

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.

2 participants