Skip to content

remove #[disable_initialized_field_access]#111

Open
BennoLossin wants to merge 2 commits intomainfrom
dev/unsound-fix-packed
Open

remove #[disable_initialized_field_access]#111
BennoLossin wants to merge 2 commits intomainfrom
dev/unsound-fix-packed

Conversation

@BennoLossin
Copy link
Member

Gary noticed [1] that the initializer macros as well as the [Pin]Init traits cannot support packed struct, since they use operations that require aligned pointers. This means that any code using packed structs and pin-init is unsound.

Thus remove the #[disable_initialized_field_access] attribute from init!, which is the only safe way to create an initializer of a packed struct.

In the future, we can add support for packed structs by changing the trait infrastructure to include UnalignedInit or hopefully another mechanism.

Reported-by: Gary Guo gary@garyguo.net
Link: https://rust-for-linux.zulipchat.com/#narrow/channel/561532-pin-init/topic/initialized.20field.20accessor.20detection/with/576210658 [1]
Fixes: db96c51 ("add references to previously initialized fields")

@BennoLossin BennoLossin requested a review from nbdd0121 February 27, 2026 19:25
@nbdd0121
Copy link
Member

The commit that this fixes should be the one that introduced this feature instead? Arguably creating reference is the fix, not the fixed commit.

@BennoLossin BennoLossin force-pushed the dev/unsound-fix-packed branch from a4f72d5 to a00f29b Compare February 28, 2026 08:22
@BennoLossin
Copy link
Member Author

Yeah you're right, fixed it. Both my kernel tests & the intel kernel CI also reported no issues, so I'll send this to the list as well.

Gary noticed [1] that the initializer macros as well as the `[Pin]Init`
traits cannot support unaligned fields, since they use operations that
require aligned pointers. This means that any code using structs with
unaligned fields in pin-init is unsound.

By default, the `init!` macro generates references to initialized
fields, which makes the compiler check that those fields are aligned.
However, we added the `#[disable_initialized_field_access]` attribute to
avoid this behavior in 71988db ("internal: init: add escape hatch
for referencing initialized fields"). Thus remove the
`#[disable_initialized_field_access]` attribute from `init!`, which is
the only safe way to create an initializer handling unaligned fields.

If support for in-place initializing structs with unaligned fields is
required in the future, we could figure out a solution. This is tracked
in [2].

Reported-by: Gary Guo <gary@garyguo.net>
Link: https://rust-for-linux.zulipchat.com/#narrow/channel/561532-pin-init/topic/initialized.20field.20accessor.20detection/with/576210658 [1]
Link: #112 [2]
Fixes: 71988db ("internal: init: add escape hatch for referencing initialized fields")
Signed-off-by: Benno Lossin <lossin@kernel.org>
@BennoLossin BennoLossin force-pushed the dev/unsound-fix-packed branch from 9f3773a to 2f6d697 Compare March 2, 2026 13:41
The functions `[Pin]Init::__[pinned_]init` and `ptr::write` called from
the `init!` macro require the passed pointer to be aligned. This fact is
ensured by the creation of field accessors to previously initialized
fields.

Since we missed this very important fact from the beginning [1],
document it in the code.

Link: https://rust-for-linux.zulipchat.com/#narrow/channel/561532-pin-init/topic/initialized.20field.20accessor.20detection/with/576210658 [1]
Signed-off-by: Benno Lossin <lossin@kernel.org>
@BennoLossin BennoLossin force-pushed the dev/unsound-fix-packed branch from 2f6d697 to 14ea16f Compare March 2, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants