Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Respect alignment for zero-sized types stored in the world #6618

Closed
wants to merge 4 commits into from

Conversation

JoJoJet
Copy link
Member

@JoJoJet JoJoJet commented Nov 14, 2022

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.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events P-Unsound A bug that results in undefined compiler behavior labels Nov 14, 2022
@alice-i-cecile
Copy link
Member

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 alice-i-cecile added this to the 0.9.1 milestone Nov 14, 2022
@alice-i-cecile alice-i-cecile added the C-Bug An unexpected or incorrect behavior label Nov 14, 2022
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Nov 14, 2022
@JoJoJet
Copy link
Member Author

JoJoJet commented Nov 14, 2022

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.

@alice-i-cecile
Copy link
Member

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+

bors bot pushed a commit that referenced this pull request Nov 14, 2022
# 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.
@bors bors bot changed the title Respect alignment for zero-sized types stored in the world [Merged by Bors] - Respect alignment for zero-sized types stored in the world Nov 14, 2022
@bors bors bot closed this Nov 14, 2022
cart pushed a commit that referenced this pull request Nov 30, 2022
# 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.
@JoJoJet JoJoJet deleted the aligned-dangle branch December 6, 2022 04:49
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
…e#6618)

# Objective

Fixes bevyengine#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.
@JoJoJet
Copy link
Member Author

JoJoJet commented Mar 24, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Bug An unexpected or incorrect behavior P-Unsound A bug that results in undefined compiler behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BlobVec does not respect alignment for zero-sized types
4 participants