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

Fix drop order of JoinedCell fields #21

Merged
merged 3 commits into from
Oct 5, 2021

Conversation

steffahn
Copy link
Contributor

@steffahn steffahn commented Oct 4, 2021

Closes #20

@Voultapher
Copy link
Owner

Let me know if you agree with 2ca0660

@Voultapher
Copy link
Owner

Also do you think switching to ManuallyDrop would be worth it? We have pointers anyway, so the use seems limited to me. And I suspect this simple pointer based code is quite fast to compile.

@steffahn
Copy link
Contributor Author

steffahn commented Oct 4, 2021

2ca0660 Seems like a valid approach as well.

I’m not sure if it’d be superflous, but I’d feel even better if the code used addr_of_mut!((*ptr).field) instead of the &mut (*ptr).field. I’m just being paranoid that &mut (*ptr).field might require that the whole *ptr is supposed to be valid, even though a different field is already dropped. (Probably not, but I’m not sure and the change wouldn’t hurt anyway I guess?)

@steffahn
Copy link
Contributor Author

steffahn commented Oct 4, 2021

Note that even without 2ca0660, drop order of fields is clearly specified.

But with the addr_of_mut, I like it even better than the previous drop_in_place(joined_ptr.as_ptr());, because in my mind that one could also try to get mutable access to the whole *joined_ptr.as_ptr() first, which would invalidate the shared reference to owner that dependent might contain.

In other words: I’m not quite sure if it makes a difference but if any version is sound then the current one with drop_in_place(addr_of_mut!((*joined_ptr.as_ptr()).dependent)); drop_in_place(addr_of_mut!((*joined_ptr.as_ptr()).owner)); should be sound, too.

@steffahn
Copy link
Contributor Author

steffahn commented Oct 4, 2021

I, too, don’t really think using ManuallyDrop adds anything useful.

@steffahn
Copy link
Contributor Author

steffahn commented Oct 4, 2021

Squashed the commits a bit.

@steffahn
Copy link
Contributor Author

steffahn commented Oct 4, 2021

Added another commit with more changes. I’m more confident that these operations are sound with addr_of_mut than before. In particular, the UnsafeSelfCell::borrow_mut seemed problematic because it returned a mutable reference to the whole JoinedCell even though owner must not be borrowed mutably while dependent can hold a reference to it.

@steffahn
Copy link
Contributor Author

steffahn commented Oct 4, 2021

Another usage of addr_of_mut added. I’m not sure if &mut (*self.joined_ptr.as_ptr()).owner is allowed when the dependent field contains uninitialized data. Better safe than sorry.

@steffahn
Copy link
Contributor Author

steffahn commented Oct 4, 2021

Did some more reading, turns out the addr_of[_mut] usage is unnecessary in most cases, but 2 things were legitimate AFAICT:

  • the UnsafeSelfCell::borrow_mut was problematic, creating an intermediate mutable reference to the whole JoinedCell
  • the
    let owner_ptr: *mut $Owner = core::ptr::addr_of_mut!((*joined_ptr.as_ptr()).owner);
    let dependent_ptr: *mut $Dependent = core::ptr::addr_of_mut!((*joined_ptr.as_ptr()).dependent);
    are important, because previously they would create intermediate references to uninitialized memory.

@steffahn
Copy link
Contributor Author

steffahn commented Oct 4, 2021

Reduced usage of addr_of[_mut] to those cases now :-), squashed again

Copy link
Owner

@Voultapher Voultapher left a comment

Choose a reason for hiding this comment

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

Thanks, lgtm.

src/unsafe_self_cell.rs Show resolved Hide resolved
@Voultapher Voultapher merged commit ccead0d into Voultapher:main Oct 5, 2021
@steffahn steffahn deleted the fix_drop_order branch October 5, 2021 16:47
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.

Incorrect drop order for JoinedCell
2 participants