Use pointers instead of &self in Latch::set#1011
Merged
bors[bot] merged 2 commits intorayon-rs:masterfrom Jan 21, 2023
Merged
Conversation
This was referenced Jan 19, 2023
saethlin
reviewed
Jan 19, 2023
saethlin
reviewed
Jan 19, 2023
Comment on lines
383
to
413
| L: Latch, | ||
| { | ||
| #[inline] | ||
| fn set(&self) { | ||
| L::set(self); | ||
| unsafe fn set(this: *const Self) { | ||
| L::set(&**this); | ||
| } |
Contributor
There was a problem hiding this comment.
This impl makes me uncomfortable. I previously tried to implement this PR and I think I came up with almost exactly the same patch as you have here, but I wasn't sure what to do with this. Of course I couldn't come up with a case where this actually causes an issue, but implementing this for &Latch seems like an invitation to create the references we're trying to avoid.
Member
Author
There was a problem hiding this comment.
I added a new LatchRef to replace this -- let me know what you think!
Contributor
There was a problem hiding this comment.
That is very cool. I like it.
`Latch::set` can invalidate its own `&self`, because it releases the owning thread to continue execution, which may then invalidate the latch by deallocation, reuse, etc. We've known about this problem when it comes to accessing latch fields too late, but the possibly dangling reference was still a problem, like rust-lang/rust#55005. The result of that was rust-lang/rust#98017, omitting the LLVM attribute `dereferenceable` on references to `!Freeze` types -- those containing `UnsafeCell`. However, miri's Stacked Borrows implementation is finer- grained than that, only relaxing for the cell itself in the `!Freeze` type. For rayon, that solves the dangling reference in atomic calls, but remains a problem for other fields of a `Latch`. This easiest fix for rayon is to use a raw pointer instead of `&self`. We still end up with some temporary references for stuff like atomics, but those should be fine with the rules above.
d66bdc0 to
f880d02
Compare
SOF3
added a commit
to SOF3/dynec
that referenced
this pull request
Jan 21, 2023
SOF3
added a commit
to SOF3/dynec
that referenced
this pull request
Jan 21, 2023
Member
Author
|
bors r+ |
Contributor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Latch::setcan invalidate its own&self, because it releases theowning thread to continue execution, which may then invalidate the latch
by deallocation, reuse, etc. We've known about this problem when it
comes to accessing latch fields too late, but the possibly dangling
reference was still a problem, like rust-lang/rust#55005.
The result of that was rust-lang/rust#98017, omitting the LLVM attribute
dereferenceableon references to!Freezetypes -- those containingUnsafeCell. However, miri's Stacked Borrows implementation is finer-grained than that, only relaxing for the cell itself in the
!Freezetype. For rayon, that solves the dangling reference in atomic calls, but
remains a problem for other fields of a
Latch.This easiest fix for rayon is to use a raw pointer instead of
&self.We still end up with some temporary references for stuff like atomics,
but those should be fine with the rules above.