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

Prefer &self for COM interface traits #1511

Merged
merged 2 commits into from
Feb 7, 2022
Merged

Prefer &self for COM interface traits #1511

merged 2 commits into from
Feb 7, 2022

Conversation

kennykerr
Copy link
Collaborator

@kennykerr kennykerr commented Feb 7, 2022

The "implement_vector" test illustrates one way to mutate within an implementation using a RwLock.

Fixes #1497

@kennykerr kennykerr merged commit 93d3566 into master Feb 7, 2022
@kennykerr kennykerr deleted the impl_self branch February 7, 2022 23:45
Ok(self.0.as_mut_ptr())
fn Buffer(&self) -> Result<*mut u8> {
unsafe {
let writer: &mut Self = &mut *(self as *const _ as *mut Self);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of cast is unsound- because self is a shared reference, producing a unique reference from it has immediate undefined behavior. (The same thing applies to the into_impl and implement_data_object tests.)

For this to work you will need some kind of interior mutability to make *mut access legal at all, and also some care to avoid invalidating (in the language-lawyer, avoiding-miscompilation sense) the *mut u8s you hand out if Buffer gets called multiple times. For reference, the current candidate for the actual rules here is Stacked Borrows, and you can test compliance with those rules using Miri.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this is just to let the tests pass without writing a full RwLock implementation as I did for the vector test.

Is there a simple way to write this test that is not UB even if it is wildly unsafe? IBufferByteAccess is wildly unsafe to begin with. I'd just prefer not to have to write each test with something like RwLock if I can help it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes- the lowest level form of interior mutability is UnsafeCell, which directly gives you a *mut to its contents.

For example, this passes under Miri (Tools > Miri in the upper right): https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=abb63fe3c9e3a30f9d274f6111bdd6d9

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, will update the tests to use UnsafeCell.

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 this pull request may close these issues.

COM interface traits using &mut self may not be sound
2 participants