Skip to content

pin!() is unsound due to coercions #153438

Closed

Description

@theemathas

View all comments

Related to #153425

Edit: See #153438 (comment) for an easier unsoundness demonstration

Old version of the code that demonstrates unsoundness

The following code causes UB. In my testing, it prints a random-looking number.

use std::ops::{Deref, DerefMut};
use std::pin::{Pin, pin};
use std::task::{Context, Poll, Waker};

struct Thing<'a, T> {
    data: Option<T>,
    out: &'a mut Option<T>,
}
impl<T> Deref for Thing<'_, T> {
    type Target = T;
    fn deref(&self) -> &T {
        self.data.as_ref().unwrap()
    }
}
impl<T> DerefMut for Thing<'_, T> {
    fn deref_mut(&mut self) -> &mut T {
        self.data.as_mut().unwrap()
    }
}
impl<T> Drop for Thing<'_, T> {
    fn drop(&mut self) {
        *self.out = self.data.take();
    }
}

fn wrong_pin<T>(data: T, callback: impl FnOnce(Pin<&mut T>)) -> T {
    let mut storage = None::<T>;
    {
        let thing = Thing {
            data: Some(data),
            out: &mut storage,
        };
        let pinned: Pin<&mut T> = pin!(thing);
        callback(pinned);
    }
    storage.unwrap()
}

// The above function violates the Pin guarantee.
// Code below exploits this to actually cause UB.

struct PendingOnce(bool);
impl Future for PendingOnce {
    type Output = ();
    fn poll(mut self: Pin<&mut Self>, _: &mut Context) -> Poll<()> {
        if self.0 {
            Poll::Ready(())
        } else {
            self.0 = true;
            Poll::Pending
        }
    }
}

fn main() {
    let mut ctx = Context::from_waker(Waker::noop());
    let future = async {
        let x = Box::new(1);
        let y = &x;
        PendingOnce(false).await;
        println!("{y}");
    };
    let future = wrong_pin(future, |pinned_future| {
        let _ = pinned_future.poll(&mut ctx);
    });
    let _ = pin!(future).poll(&mut ctx);
}

The issue is that the pin!(thing) macro call moves the entire Thing into an inaccessible variable. Then, instead of pinning Thing, it deref-coerces the &mut Thing<T> into a &mut T and pins that instead. Then, the Drop impl can later access the &mut T, breaking the Pin invariant.

This presumably regressed in #139114, which changed pin!() to use super let. This uses a block expression in the syntax, which then presumably runs into the bug in #23014, causing the strange coercion behavior. Edit: see #153438 (comment)

The code compiled since 1.88.0. It didn't compile in 1.87.0.

Meta

Reproducible on the playground with version 1.96.0-nightly (2026-03-04 b90dc1e597db0bbc0cab)

Activity

  1. added
    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
    T-langRelevant to the language team
    I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness
    C-bugCategory: This is a bug.
    A-coercionsArea: implicit and explicit `expr as Type` coercions
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    on Mar 5, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Mar 5, 2026
  3. added
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Mar 5, 2026
  4. Noratrieb commented on Mar 5, 2026

    @Noratrieb
    Member

    @dianne you'll love this

  5. theemathas commented on Mar 5, 2026

    @theemathas
    ContributorAuthor

    I think I initially misdiagnosed what changed that caused the unsoundness.

    Here's the body of the pin! macro in version 1.87.0:

    $crate::pin::Pin::<&mut _> { __pointer: &mut { $value } }

    The important part here is &mut { $value }. The bug in #23014 causes deref coercions to not happen on expressions like &mut { expr }. That is, the pin!() macro was previously relying on the bug in order for the macro to be sound.

    The current implementation does not have the &mut { expr } code pattern that prevented a deref coercion, so a deref coercion could happen, causing the unsoundness.

  6. 46 remaining items

  7. added a commit that references this issue on Jul 15, 2026
  8. added a commit that references this issue on Jul 20, 2026
  9. removed
    proposed-final-comment-periodProposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off.
    disposition-closeThis PR / issue is in PFCP or FCP with a disposition to close it.
    on Jul 20, 2026
  10. added a commit that references this issue on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coercionsArea: implicit and explicit `expr as Type` coercionsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-pinArea: PinC-bugCategory: This is a bug.F-super_letit's super, let's go!I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-typesRelevant to the types team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions