Skip to content

UnsafePinned: also include the effects of UnsafeCell #140638

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

Merged
merged 2 commits into from
Jun 5, 2025

Conversation

RalfJung
Copy link
Member

@RalfJung RalfJung commented May 4, 2025

This tackles #137750 by including an UnsafeCell in UnsafePinned, thus imbuing it with all the usual properties of interior mutability (no noalias nor dereferenceable on shared refs, special treatment by Miri's aliasing model). The soundness issue is not fixed yet because coroutine lowering does not use UnsafePinned.

The RFC said that UnsafePinned would not permit mutability on shared references, but since then, #137750 has demonstrated that this is not tenable. In the face of those examples, I propose that we do the "obvious" thing and permit shared mutable state inside UnsafePinned. This seems loosely consistent with the fact that we allow going from Pin<&mut T> to &T (where the former can be aliased with other pointers that perform mutation, and hence the same goes for the latter) -- but the as_ref example shows that we in fact would need to add this UnsafeCell even if we didn't have a safe conversion to &T, since for the compiler and Miri, &T and Pin<&T> are basically the same type.

To make this possible, I had to remove the Copy and Clone impls for UnsafePinned.

Tracking issue: #125735
Cc @rust-lang/lang @rust-lang/opsem @Sky9x
I don't think this needs FCP since the type is still unstable -- we'll finally decide whether we like this approach when UnsafePinned is moved towards stabilization (IOW, this PR is reversible). However, I'd still like to make sure that the lang team is okay with the direction I am proposing here.

@rustbot
Copy link
Collaborator

rustbot commented May 4, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels May 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 4, 2025

The Miri subtree was changed

cc @rust-lang/miri

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@Sky9x Sky9x left a comment

Choose a reason for hiding this comment

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

getting rid of those Copy/Clone impls is nice

@RalfJung RalfJung added the I-lang-nominated Nominated for discussion during a lang team meeting. label May 4, 2025
@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from b08491e to 66ca017 Compare May 4, 2025 16:17
@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from 66ca017 to bc8609c Compare May 4, 2025 21:16
@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from bc8609c to d87cf5a Compare May 5, 2025 06:45
@rust-log-analyzer

This comment has been minimized.

@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from d87cf5a to 405c954 Compare May 5, 2025 11:30
@traviscross
Copy link
Contributor

@rustbot labels -I-lang-nominated +I-lang-radar

We talked about this in the lang call today with 2/5 present. We felt it indeed made sense, given what we've found, for &UnsafePinned<T> to also be special.

@rustbot rustbot added I-lang-radar Items that are on lang's radar and will need eventual work or consideration. and removed I-lang-nominated Nominated for discussion during a lang team meeting. labels May 8, 2025
@RalfJung
Copy link
Member Author

@WaffleLapkin @tgross35 could one of you review this?

@RalfJung
Copy link
Member Author

After 3 weeks without a review, I'll reassign.
r? @tgross35
(or maybe @WaffleLapkin ?)

@rustbot rustbot assigned tgross35 and unassigned thomcc May 23, 2025
@RalfJung RalfJung force-pushed the unsafe-pinned-shared-aliased branch from 405c954 to 259d6f4 Compare May 28, 2025 08:56
@workingjubilee workingjubilee reopened this Jun 4, 2025
@workingjubilee
Copy link
Member

Soundness fix. Well, pre-fix. Code looks fine to me. Approving.

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Jun 4, 2025

📌 Commit 259d6f4 has been approved by workingjubilee

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 4, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jun 5, 2025
…sed, r=workingjubilee

UnsafePinned: also include the effects of UnsafeCell

This tackles rust-lang#137750 by including an `UnsafeCell` in `UnsafePinned`, thus imbuing it with all the usual properties of interior mutability (no `noalias` nor `dereferenceable` on shared refs, special treatment by Miri's aliasing model). The soundness issue is not fixed yet because coroutine lowering does not use `UnsafePinned`.

The RFC said that `UnsafePinned` would not permit mutability on shared references, but since then, rust-lang#137750 has demonstrated that this is not tenable. In the face of those examples, I propose that we do the "obvious" thing and permit shared mutable state inside `UnsafePinned`. This seems loosely consistent with the fact that we allow going from `Pin<&mut T>` to `&T` (where the former can be aliased with other pointers that perform mutation, and hence the same goes for the latter) -- but the `as_ref` example shows that we in fact would need to add this `UnsafeCell` even if we didn't have a safe conversion to `&T`, since for the compiler and Miri, `&T` and `Pin<&T>` are basically the same type.

To make this possible, I had to remove the `Copy` and `Clone` impls for `UnsafePinned`.

Tracking issue: rust-lang#125735
Cc `@rust-lang/lang` `@rust-lang/opsem`  `@Sky9x`
I don't think this needs FCP since the type is still unstable -- we'll finally decide whether we like this approach when `UnsafePinned` is moved towards stabilization (IOW, this PR is reversible). However, I'd still like to make sure that the lang team is okay with the direction I am proposing here.
bors added a commit that referenced this pull request Jun 5, 2025
Rollup of 8 pull requests

Successful merges:

 - #140638 (UnsafePinned: also include the effects of UnsafeCell)
 - #141272 (modularize the config module bootstrap)
 - #141777 (Do not run PGO/BOLT in x64 Linux alt builds)
 - #141870 (Fix broken link to rustc_type_ir module in serialization docs)
 - #141938 (update rust offload bootstrap)
 - #141962 (rustc-dev-guide subtree update)
 - #141965 (`tests/ui`: A New Order [3/N])
 - #141970 (implement new `x` flag: `--skip-std-check-if-no-download-rustc`)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Jun 5, 2025
Rollup of 6 pull requests

Successful merges:

 - #140638 (UnsafePinned: also include the effects of UnsafeCell)
 - #141777 (Do not run PGO/BOLT in x64 Linux alt builds)
 - #141938 (update rust offload bootstrap)
 - #141962 (rustc-dev-guide subtree update)
 - #141965 (`tests/ui`: A New Order [3/N])
 - #141970 (implement new `x` flag: `--skip-std-check-if-no-download-rustc`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 2d7ebd3 into rust-lang:master Jun 5, 2025
16 checks passed
@rustbot rustbot added this to the 1.89.0 milestone Jun 5, 2025
rust-timer added a commit that referenced this pull request Jun 5, 2025
Rollup merge of #140638 - RalfJung:unsafe-pinned-shared-aliased, r=workingjubilee

UnsafePinned: also include the effects of UnsafeCell

This tackles #137750 by including an `UnsafeCell` in `UnsafePinned`, thus imbuing it with all the usual properties of interior mutability (no `noalias` nor `dereferenceable` on shared refs, special treatment by Miri's aliasing model). The soundness issue is not fixed yet because coroutine lowering does not use `UnsafePinned`.

The RFC said that `UnsafePinned` would not permit mutability on shared references, but since then, #137750 has demonstrated that this is not tenable. In the face of those examples, I propose that we do the "obvious" thing and permit shared mutable state inside `UnsafePinned`. This seems loosely consistent with the fact that we allow going from `Pin<&mut T>` to `&T` (where the former can be aliased with other pointers that perform mutation, and hence the same goes for the latter) -- but the `as_ref` example shows that we in fact would need to add this `UnsafeCell` even if we didn't have a safe conversion to `&T`, since for the compiler and Miri, `&T` and `Pin<&T>` are basically the same type.

To make this possible, I had to remove the `Copy` and `Clone` impls for `UnsafePinned`.

Tracking issue: #125735
Cc ``@rust-lang/lang`` ``@rust-lang/opsem``  ``@Sky9x``
I don't think this needs FCP since the type is still unstable -- we'll finally decide whether we like this approach when `UnsafePinned` is moved towards stabilization (IOW, this PR is reversible). However, I'd still like to make sure that the lang team is okay with the direction I am proposing here.
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Jun 6, 2025
Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#140638 (UnsafePinned: also include the effects of UnsafeCell)
 - rust-lang/rust#141777 (Do not run PGO/BOLT in x64 Linux alt builds)
 - rust-lang/rust#141938 (update rust offload bootstrap)
 - rust-lang/rust#141962 (rustc-dev-guide subtree update)
 - rust-lang/rust#141965 (`tests/ui`: A New Order [3/N])
 - rust-lang/rust#141970 (implement new `x` flag: `--skip-std-check-if-no-download-rustc`)

r? `@ghost`
`@rustbot` modify labels: rollup
@RalfJung RalfJung deleted the unsafe-pinned-shared-aliased branch June 6, 2025 06:51
@onestacked
Copy link
Contributor

@RalfJung This PR missed changing the comment and doctest on UnsafePinned::get()

/// Note that `&UnsafePinned<T>` is read-only if `&T` is read-only. This means that if there is
/// mutation of the `T`, future reads from the `*const T` returned here are UB! Use
/// [`UnsafeCell`] if you also need interior mutability.
///
/// [`UnsafeCell`]: crate::cell::UnsafeCell
///
/// ```rust,no_run
/// #![feature(unsafe_pinned)]
/// use std::pin::UnsafePinned;
///
/// unsafe {
/// let mut x = UnsafePinned::new(0);
/// let ptr = x.get(); // read-only pointer, assumes immutability
/// x.get_mut_unchecked().write(1);
/// ptr.read(); // UB!
/// }
/// ```

@RalfJung
Copy link
Member Author

RalfJung commented Jun 7, 2025

Good point, I filed a follow-up at #142162.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-lang-radar Items that are on lang's radar and will need eventual work or consideration. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants