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

OwningRefMut::as_owner and as_owner_mut are unsound #61

Open
comex opened this issue Mar 22, 2020 · 0 comments
Open

OwningRefMut::as_owner and as_owner_mut are unsound #61

comex opened this issue Mar 22, 2020 · 0 comments

Comments

@comex
Copy link

comex commented Mar 22, 2020

No fancy Stacked Borrows stuff in this one, just normal memory unsafety. These methods of OwningRefMut:

    /// A reference to the underlying owner.
    pub fn as_owner(&self) -> &O {
        &self.owner
    }

    /// A mutable reference to the underlying owner.
    pub fn as_owner_mut(&mut self) -> &mut O {
        &mut self.owner
    }

...are both unsound, because they can be used to access the owner object while the associated reference thinks it has unique access to it. In particular, it can be used to modify that data and invalidate the reference. (Doing so is straightforward with as_owner_mut, and is still possible with as_owner with some interior mutability shenanigans.)

Here is a simple test case for both methods. Clone the repo and run either

  • RUSTFLAGS="-Zsanitizer=address" cargo test as_owner_mut_is_unsound
  • RUSTFLAGS="-Zsanitizer=address" cargo test as_owner_is_unsound

and you should get a heap-use-after-free error.

This doesn't affect the non-Mut OwningRef; OwningRef::as_owner is sound.

steffahn added a commit to steffahn/owning-ref-rs that referenced this issue Jan 18, 2021
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

No branches or pull requests

1 participant