-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #67039 - xfix:manually-implement-pin-traits, r=nikomats…
…akis Use deref target in Pin trait implementations Using deref target instead of pointer itself avoids providing access to `&Rc<T>` for malicious implementations, which would allow calling `Rc::get_mut`. This is a breaking change necessary due to unsoundness, however the impact of it should be minimal. This only fixes the issue with malicious `PartialEq` implementations, other `Pin` soundness issues are still here. See <https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73> for more details.
- Loading branch information
Showing
3 changed files
with
81 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Pin's PartialEq implementation allowed to access the pointer allowing for | ||
// unsoundness by using Rc::get_mut to move value within Rc. | ||
// See https://internals.rust-lang.org/t/unsoundness-in-pin/11311/73 for more details. | ||
|
||
use std::ops::Deref; | ||
use std::pin::Pin; | ||
use std::rc::Rc; | ||
|
||
struct Apple; | ||
|
||
impl Deref for Apple { | ||
type Target = Apple; | ||
fn deref(&self) -> &Apple { | ||
&Apple | ||
} | ||
} | ||
|
||
impl PartialEq<Rc<Apple>> for Apple { | ||
fn eq(&self, _rc: &Rc<Apple>) -> bool { | ||
unreachable!() | ||
} | ||
} | ||
|
||
fn main() { | ||
let _ = Pin::new(Apple) == Rc::pin(Apple); | ||
//~^ ERROR type mismatch resolving | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/ui/issues/issue-67039-unsound-pin-partialeq.stderr
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error[E0271]: type mismatch resolving `<std::rc::Rc<Apple> as std::ops::Deref>::Target == std::rc::Rc<Apple>` | ||
--> $DIR/issue-67039-unsound-pin-partialeq.rs:25:29 | ||
| | ||
LL | let _ = Pin::new(Apple) == Rc::pin(Apple); | ||
| ^^ expected struct `Apple`, found struct `std::rc::Rc` | ||
| | ||
= note: expected type `Apple` | ||
found struct `std::rc::Rc<Apple>` | ||
= note: required because of the requirements on the impl of `std::cmp::PartialEq<std::pin::Pin<std::rc::Rc<Apple>>>` for `std::pin::Pin<Apple>` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0271`. |