linux/rust/pin-init/internal/src
Gary Guo 68bf102226 rust: pin-init: fix incorrect accessor reference lifetime
When a field has been initialized, `init!`/`pin_init!` create a reference
or pinned reference to the field so it can be accessed later during the
initialization of other fields. However, the reference it created is
incorrectly `&'static` rather than just the scope of the initializer.

This means that you can do

    init!(Foo {
        a: 1,
        _: {
            let b: &'static u32 = a;
        }
    })

which is unsound.

This is caused by `&mut (*#slot).#ident`, which actually allows arbitrary
lifetime, so this is effectively `'static`. Somewhat ironically, the safety
justification of creating the accessor is.. "SAFETY: TODO".

Fix it by adding `let_binding` method on `DropGuard` to shorten lifetime.
This results in exactly what we want for these accessors. The safety and
invariant comments of `DropGuard` have been reworked; instead of reasoning
about what caller can do with the guard, express it in a way that the
ownership is transferred to the guard and `forget` takes it back, so the
unsafe operations within the `DropGuard` can be more easily justified.

Fixes: 42415d163e ("rust: pin-init: add references to previously initialized fields")
Cc: stable@vger.kernel.org
Signed-off-by: Gary Guo <gary@garyguo.net>
Link: https://patch.msgid.link/20260427-pin-init-fix-v3-2-496a699674dd@garyguo.net
[ Reworded for missing word. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2026-04-30 22:43:32 +02:00
..
diagnostics.rs rust: pin-init: rewrite derive(Zeroable) and derive(MaybeZeroable) using syn 2026-01-17 10:51:42 +01:00
init.rs rust: pin-init: fix incorrect accessor reference lifetime 2026-04-30 22:43:32 +02:00
lib.rs rust: pin-init: build: simplify use of nightly features 2026-03-25 10:56:16 +01:00
pin_data.rs rust: pin-init: add ?Sized bounds to traits in #[pin_data] macro 2026-01-17 10:51:42 +01:00
pinned_drop.rs rust: pin-init: rewrite the #[pinned_drop] attribute macro using syn 2026-01-17 10:51:42 +01:00
zeroable.rs rust: pin-init: rewrite derive(Zeroable) and derive(MaybeZeroable) using syn 2026-01-17 10:51:42 +01:00