-
Notifications
You must be signed in to change notification settings - Fork 94
Possible UB from Box<T> aliasing #74
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
Comments
Related comment #73 (comment) |
Thanks! Just for reference I'll add my reply to that to remind myself in the future:
|
What do you mean? Once you use a And since having |
This partially addresses #74. There is still a violation in implementing `ShallowCopy` for `Box`, but that one we'll tackle separately.
@danielhenrymantilla My concern specifically is that it makes it very unergonomic to work with boxed values in evmap, since you cannot simply use a value that contains |
This is trying to address the unsoundness that arises from the current version of `ShallowCopy` (see #74). In the process, it also deals with the fact that casting between `Inner<.., T, ..>` and `Inner<.., ManuallyDrop<T>, ..>` is likely not sound (rust-lang/unsafe-code-guidelines#35 (comment)). It does not yet work.
This is trying to address the unsoundness that arises from the current version of `ShallowCopy` (see #74). In the process, it also deals with the fact that casting between `Inner<.., T, ..>` and `Inner<.., ManuallyDrop<T>, ..>` is likely not sound (rust-lang/unsafe-code-guidelines#35 (comment)). It does not yet work.
This is trying to address the unsoundness that arises from the current version of `ShallowCopy` (see #74). In the process, it also deals with the fact that casting between `Inner<.., T, ..>` and `Inner<.., ManuallyDrop<T>, ..>` is likely not sound (rust-lang/unsafe-code-guidelines#35 (comment)). It does not yet work.
Quick update: I believe that the use in |
@jonhoo I think I misunderstood your
I interpreted it as " But if you are worried about a general |
This is trying to address the unsoundness that arises from the current version of `ShallowCopy` (see #74). In the process, it also deals with the fact that casting between `Inner<.., T, ..>` and `Inner<.., ManuallyDrop<T>, ..>` is likely not sound (rust-lang/unsafe-code-guidelines#35 (comment)). It does not yet work.
@danielhenrymantilla Yes, I agree with you, I think |
This implementation gets rid of the unsound `ShallowCopy` trait (#74 & rust-lang/unsafe-code-guidelines#35 (comment)), and replaces it with a wrapper type around aliased values. The core mechanism is that the wrapper type holds a `MaybeUninit<T>`, and aliases it by doing a `ptr::read` of the whole `MaybeUninit<T>` to alias. It then takes care to only give out `&T`s, which is allowed to alias. To drop, the implementation sets a thread-local that the wrapper uses to determine if it should truly drop the inner `T` (i.e., to indicate that this is the last copy, and the `T` is no longer aliased). The removal of `ShallowCopy` makes some of the API nicer, and obviates the need for `evmap-derive`. Unfortunately the introduction of the wrapper also complicates certain bounds which now need to know about the wrapping. Overall though, an ergonomic win. The implementation passes all tests, and I _think_ it is sound (if you think it's not, please let me know!). The one bit that I'm not sure about is the thread-local if a user nests `evmap`s (since they'll share that thread local). Note that this does _not_ take care of #78. Fixes #74. Fixes #55 since `ShallowCopy` is no longer neeeded.
This implementation gets rid of the unsound `ShallowCopy` trait (#74 & rust-lang/unsafe-code-guidelines#35 (comment)), and replaces it with a wrapper type around aliased values. The core mechanism is that the wrapper type holds a `MaybeUninit<T>`, and aliases it by doing a `ptr::read` of the whole `MaybeUninit<T>` to alias. It then takes care to only give out `&T`s, which is allowed to alias. To drop, the implementation casts between two different generic arguments of the new `Aliased` type, where one type causes the `Aliased<T>` to drop the inner `T` and the other does not. The documentation goes into more detail. The resulting cast _should_ be safe (unlike the old `ManuallyDrop` cast). The removal of `ShallowCopy` makes some of the API nicer by removing trait bounds, and obviates the need for `evmap-derive`. While I was going through, I also took the liberty of tidying up the external API of `evmap` a bit. The implementation passes all tests, and I _think_ it is sound (if you think it's not, please let me know!). Note that this does _not_ take care of #78. Fixes #74. Fixes #55 since `ShallowCopy` is no longer neeeded. Also fixes #72 for the same reason.
While watching yesterday's live stream Box shallow copying was mentioned, and me and a couple viewers asked about aliasing
I asked in the UFC zulip stream about it
Quoting RalfJ:
Example which triggers UB according to MIRI (As provided by bjorn3 in the zulip topic)
Even just making a shared reference instead of actually reading the Copy value also triggers UB according to MIRI
Changing it to make the shallow copy the same way as the Box<T> shallow copy impl seems to change nothing: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=6e7b08eb28d6032fbdb41028225f4dde
The text was updated successfully, but these errors were encountered: