bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Respect alignment for zero-sized types stored in the world

Open joseph-gio opened this issue 3 years ago • 1 comments

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_align to bevy_ptr, which creates a well-aligned dangling pointer to a type whose alignment is not known at compile time.

joseph-gio avatar Nov 14 '22 16:11 joseph-gio

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.

alice-i-cecile avatar Nov 14 '22 16:11 alice-i-cecile

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.

joseph-gio avatar Nov 14 '22 19:11 joseph-gio

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+

alice-i-cecile avatar Nov 14 '22 21:11 alice-i-cecile

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.

joseph-gio avatar Mar 24 '23 21:03 joseph-gio