# Rust 1.20 caused pinning to become incorrect

**URL:** <https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695>\
**Category:** Unsafe Code Guidelines\
**Created:** [February 2, 2018, 5:08pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695 "2018-02-02T17:08:21Z")\
**Posts on this page:** 16\
**Page:** 2

<div class="post-metadata">

**Author:** ![asajeffrey](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/asajeffrey/32/3002_2.png) [@asajeffrey](https://internals.rust-lang.org/u/asajeffrey)\
**Post date:** [February 3, 2018, 5:25pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/21 "2018-02-03T17:25:13Z")

</div>

I guess this is an issue of what the implicit contract for `Drop` is. Is it expected to drop all of its owned data? Even in the presence of drop code panic? Is it allowed to panic? Is it allowed to abort? Is there any contract at all?

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [February 3, 2018, 7:31pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/22 "2018-02-03T19:31:46Z")

</div>

> [@asajeffrey](#):
>
> The only thing I’d ask for is please can we give ourselves some wiggle room and define two invariants for rely-guarantee reasoning (code can rely on R and should guarantee G, where G implies R). That way there’s some room for strengthening R and weakening G. If we just have one invariant (i.e. R=G) we’re stuck never being able to change them!

This is a great point. However, rely-guarantee is (AFAIK) terminology from concurrency research and describes modular (thread-per-thread) verification of concurrent interactions. I'm not actually aware of any research for type system invariants that provide "wiggle room". Somehow, this would have to make a difference between the types that are _produced_ by an unsafely implemented library (where it has to show the stronger invariant) and types that are _consumed_ (where it can only rely on the weaker invariant). But given this is all higher-order and we can both produce and consume closures, I will only believe this really works once I see it worked out. 🙂

So, while I agree with the goal, I actually have no idea how to formally do that. I know I should write this down as a future research project though 😃

> [@asajeffrey](#):
>
> I think my main point still stands, which is that the contextual definition of correctness is brittle, and that this is an interesting example of it producing problems “in the wild”.

It's not just brittle. It's anti-modular: Two libraries can be correct independently but break in combination. Just imagine `VecDeque` not being in libstd; then `VecDeque` and `Pin` form such a pair.

So, I'd say the contextual definition is plain broken.

> [@asajeffrey](#):
>
> @RalfJung: this might be a way to tie the contextual definition of correctness back to rely-guarantee reasoning, if the contracts are themselves stated using rely-guarantee.

Could you elaborate? I don't follow. This doesn't help with the anti-modularity, right?

> [@asajeffrey](#):
>
> I guess this is an issue of what the implicit contract for Drop is. Is it expected to drop all of its owned data? Even in the presence of drop code panic? Is it allowed to panic? Is it allowed to abort? Is there any contract at all?

I agree with what has been said above that ever sine the leakpocalypse, the contract is something like "`drop` is called on a best-effort basis; you can't ever rely on it for safety" (EDIT: except if it's all your code. You can rely on your own stack objects getting dropped). But probably this has not been documented clearly enough.

---

<div class="post-metadata">

**Author:** ![arielb1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arielb1/32/3044_2.png) [@arielb1](https://internals.rust-lang.org/u/arielb1)\
**Post date:** [February 3, 2018, 9:30pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/23 "2018-02-03T21:30:27Z")

</div>

Pinning is unsafe in the presence of arena-allocated `Rc` data anyway, which is something we want to allow one day:

```rust
struct Cyclic<A, T> {
    cycle: RefCell<Option<Rc<Cyclic<T, A>, A>>,
    data: T
}

fn foo(arena: &ArenaAllocator) {
    let rc1 = arena <- Cyclic {
        cycle: Default::default(),
        data: Pin::new(),
    };
    
    // make a cycle    
    *rc1.cycle.borrow_mut() = Some(rc1.clone());
    rc1.data.pin();

    // no destructors are run, but the arena (and its containing Rc) is eventually free-ed.
}

```

---

<div class="post-metadata">

**Author:** ![asajeffrey](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/asajeffrey/32/3002_2.png) [@asajeffrey](https://internals.rust-lang.org/u/asajeffrey)\
**Post date:** [February 4, 2018, 5:15pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/24 "2018-02-04T17:15:58Z")

</div>

Arena-allocated data doesn’t get its destructor called? That’s annoying. It would be nice if there were a way to call the destructors for data that was marked `may_dangle` or some similar marker for drop code that is safe in the presence of cycles. But that’s a different topic!

---

<div class="post-metadata">

**Author:** ![asajeffrey](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/asajeffrey/32/3002_2.png) [@asajeffrey](https://internals.rust-lang.org/u/asajeffrey)\
**Post date:** [February 4, 2018, 5:30pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/25 "2018-02-04T17:30:47Z")

</div>

Yes, more research is needed!

We might need some kind of Kleisli semantics future world trick, that is you don’t just show rely/guarantee for the current R and G, but for all future R’ and G’. This is the usual trick for getting monotonicity in the presence of contravariance (in particular for higher-order functions).

For contextual correctness, I was wondering if there’s a way to interpret the defn “for all safe C . C[P] has no UB” as something like “for all safe C . for all implementations I . (I satisfies appropriate contracts) implies (C[P] linked against I has no UB)”. There is lots of handwaving here, but this might be a way to recover a defn of correctness based on invariants (assuming that’s how the contracts for I are defined).

---

<div class="post-metadata">

**Author:** ![atagunov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/atagunov/32/5877_2.png) [@atagunov](https://internals.rust-lang.org/u/atagunov)\
**Post date:** [February 5, 2018, 2:42am UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/26 "2018-02-05T02:42:16Z")

</div>

> [@asajeffrey](#):
>
> Arena-allocated data doesn't get its destructor called?

guess in the example

- the counter in `Rc` never reaches `0` because of the reference loop
- so `drop` is never called on the `Pin` instance
- yet the memory it occupies is free-ed when `ArenaAllocator` itself is free-ed

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [February 5, 2018, 10:41am UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/27 "2018-02-05T10:41:28Z")

</div>

> [@asajeffrey](#):
>
> For contextual correctness, I was wondering if there’s a way to interpret the defn “for all safe C . C[P] has no UB” as something like “for all safe C . for all implementations I . (I satisfies appropriate contracts) implies (C[P] linked against I has no UB)”. There is lots of handwaving here, but this might be a way to recover a defn of correctness based on invariants (assuming that’s how the contracts for I are defined).

This sounds a lot like the logical relation-based approach we are using in RustBelt. However, on its own, this does not provide any wiggle room.

Btw, do you have an example of a guarantee that you'd like unsafe code to _provide_, but not to _rely on_? Some example where you think we currently still have wiggle room and should keep it?

---

<div class="post-metadata">

**Author:** ![asajeffrey](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/asajeffrey/32/3002_2.png) [@asajeffrey](https://internals.rust-lang.org/u/asajeffrey)\
**Post date:** [February 5, 2018, 3:45pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/28 "2018-02-05T15:45:19Z")

</div>

An example would be something where the contract hasn’t been settled yet, perhaps `may_dangle` would be a good example?

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [February 5, 2018, 4:54pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/29 "2018-02-05T16:54:11Z")

</div>

`may_dangle` seems so little understood, I don’t even know one precise definition of it – let alone _two_ (a lower and an upper bound).

---

<div class="post-metadata">

**Author:** ![asajeffrey](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/asajeffrey/32/3002_2.png) [@asajeffrey](https://internals.rust-lang.org/u/asajeffrey)\
**Post date:** [February 5, 2018, 7:11pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/30 "2018-02-05T19:11:42Z")

</div>

The rely is nothing at all 🙂

But this does bring up an interesting issue, which is that the cases where wiggle room is useful are probably cases where there’s nothing sensible to say formally because the design process is still underway.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [February 5, 2018, 9:07pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/31 "2018-02-05T21:07:55Z")

</div>

> [@asajeffrey](#):
>
> The rely is nothing at all 🙂

So `Vec::drop` is unsound for relying on something? 😉

More seriously though, I do feel like there could or should also be wiggle room in the better understood parts of the type system. I mean, we have picked a concrete invariant for our research -- but the consequences of this are far-reaching: For every possible unsafe abstraction someone could come up with, it now either _is_ or _is not_ sound (at least if you believe in the excluded middle 😛 ), and we have no idea where this dividing line lies. I would not be surprised at all if at some point down the road, someone comes up with something that's compatible with all the unsafe code that's widely used, desirable, but is not compatible with our invariant.

---

<div class="post-metadata">

**Author:** ![arielb1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arielb1/32/3044_2.png) [@arielb1](https://internals.rust-lang.org/u/arielb1)\
**Post date:** [February 5, 2018, 10:04pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/32 "2018-02-05T22:04:56Z")

</div>

> [@atagunov](#):
>
> guess in the example
> 
> - the counter in `Rc` never reaches 0 because of the reference loop
> - so `drop` is never called on the `Pin` instance
> - yet the memory it occupies is free-ed when `ArenaAllocator` itself is free-ed

That's what I meant. An arena-allocated `Rc` (this is not a full "free at once" arena, but rather an "compartment arena" where data can be free-ed while the arena is being used) can't call destructors for leaked contents of the arena when the arena exits because destroying a reference cycle would be unsound, so reference cycles are still leaked. However, the memory _owned by the arena_ could still be free-ed.

This is basically a by-design use of allocators, and therefore is unlikely to be removed.

> `may_dangle` seems so little understood, I don’t even know one precise definition of it – let alone two (a lower and an upper bound).

From my POV (see e.g. [https://github.com/rust-lang/rfcs/pull/1327#issuecomment-149366279](https://github.com/rust-lang/rfcs/pull/1327#issuecomment-149366279) and the newer [nll-rfc/0000-nonlexical-lifetimes.md at 30db8883d63b102bf8544e425a21f0d3ebe27ab3 · nikomatsakis/nll-rfc · GitHub](https://github.com/nikomatsakis/nll-rfc/blob/30db8883d63b102bf8544e425a21f0d3ebe27ab3/0000-nonlexical-lifetimes.md#layer-3-accomodating-dropck)), the main problem with `may_dangle` is that it basically makes calling functions unsafe.

The _semantics_ are pretty clear - it removes the implicit assumption that type and lifetime parameters are active when they are in scope, and also the similar WF assumption that `&'a mut T` requires that `T: 'a`. That's a problem for safety, because any function based on that type parameter assumes that these lifetimes are in scope, which means calling it is illegal - so you basically have to prove that your destructor is safe "from first principles", as if the lifetime system did not exist.

I'll note that in a destructor impl, there's still the "data-type is destruction safe" bound - the compiler guarantees that the fields of your type are destruction-safe, which means you can safely call destructors on the types of subfields (and that ends up calling a `Drop` impl, then you know the lifetime obligations for that impl are met, and you're back in the "modular world" again).

There's just no compiler infrastructure for proving that sort of thing, as that would basically require duplicating the type-checker, so you're on your own when it comes to the proofs.

The "josephine invariant" ([https://github.com/rust-lang/rust/issues/34761#issuecomment-362855806](https://github.com/rust-lang/rust/issues/34761#issuecomment-362855806)) was never intended to hold - in fact, as I demonstrated, planned-to-be-implemented allocator support would break it.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [February 6, 2018, 5:09pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/33 "2018-02-06T17:09:12Z")

</div>

> [@arielb1](#):
>
> There’s just no compiler infrastructure for proving that sort of thing, as that would basically require duplicating the type-checker, so you’re on your own when it comes to the proofs.

Just to be clear, "this sort of thing" is referring to a more explicit treatment of the currently implicit bounds and having the compiler check those at every function call? I can't see any fundamental reason why this would need a second type system -- it's "just" a more flexible and expressive version of the current type system. However, I'd not be surprised if actually adapting rustc to properly do these checks would be lots of work, given how deeply well-formedness is currently (from what I gather) engraved into the compiler.

---

<div class="post-metadata">

**Author:** ![arielb1](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/arielb1/32/3044_2.png) [@arielb1](https://internals.rust-lang.org/u/arielb1)\
**Post date:** [February 6, 2018, 6:58pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/34 "2018-02-06T18:58:45Z")

</div>

> [@RalfJung](#):
>
> Just to be clear, “this sort of thing” is referring to a more explicit treatment of the currently implicit bounds and having the compiler check those at every function call?

The problem with removing the implicit lifetime bounds isn't so much technical - the compiler already handles these bounds fairly modularly - but is rather an "ecosystem split" problem: there is no way in the surface language to control the implicit lifetime bounds, and adding such a way would force "trivial" functions to make a decision: which type parameters do we force to be alive?

There's also no way to require "destructor-validity-for" except for destructors, but that's a more desirable thing which we probably want someday.

---

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [February 7, 2018, 7:30pm UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/35 "2018-02-07T19:30:24Z")

</div>

> [@arielb1](#):
>
> is rather an “ecosystem split” problem: there is no way in the surface language to control the implicit lifetime bounds, and adding such a way would force “trivial” functions to make a decision: which type parameters do we force to be alive?

Yeah, that seems pretty much necessary -- essentially this is an instance of [More implicit bounds (?Sized, ?DynSized, ?Move) · Issue #2255 · rust-lang/rfcs · GitHub](https://github.com/rust-lang/rfcs/issues/2255)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:29am UTC](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695/36 "2019-03-25T08:29:37Z")

</div>

Closed on March 25, 2019, 8:29am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/rust-1-20-caused-pinning-to-become-incorrect/6695.md?page=1)
