Respect alignment for zero-sized types stored in the world
Objective
Fixes #6615.
BlobVec does not respect alignment for zero-sized types, which results in UB whenever a ZST with alignment other than 1 is used in the world.
Solution
Add the fn bevy_ptr::dangling_with_align.
Changelog
- Added the function
dangling_with_aligntobevy_ptr, which creates a well-aligned dangling pointer to a type whose alignment is not known at compile time.
Just so I understand the context more: why would you want to mess with the alignment of ZSTs? Definitely agree that this is needed, but I'm trying to piece this together better.
Just so I understand the context more: why would you want to mess with the alignment of ZSTs? Definitely agree that this is needed, but I'm trying to piece this together better.
I'm not aware of any actual use cases for this kind of alignment fiddling. This PR is about correctness more than anything -- I would be surprised if this issue caused any problems in practice.
Merging: either we make a 0.9.1 release and it's in there, or we don't and there's no harm done by merging early.
bors r+
Pull request successfully merged into main.
Build succeeded:
- build-and-install-on-iOS
- build-android
- build (macos-latest)
- build (ubuntu-latest)
- build-wasm
- build (windows-latest)
- build-without-default-features (bevy)
- build-without-default-features (bevy_ecs)
- build-without-default-features (bevy_reflect)
- check-compiles
- check-doc
- check-missing-examples-in-docs
- ci
- markdownlint
- run-examples
- run-examples-on-wasm
- run-examples-on-windows-dx12
So apparently this actually can matter in practice: https://doc.rust-lang.org/reference/type-layout.html
According to this, [T; 0] can have non-zero alignment on some platforms, so it's actually not impossible to cause this bug by accident. Granted, you'd need some very abstracted generic code to make that happen.