Skip to content

Pad BlobVec item size to multiple of alignment #10127

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rj00a
Copy link
Contributor

@rj00a rj00a commented Oct 15, 2023

Objective

Some of the BlobVec code assumes the size of the contained item is a multiple of its alignment. However, std::alloc::Layout by itself does not enforce this, and there are no checks in BlobVec::new. (There is a debug assertion elsewhere, but it can be bypassed in release mode). Functions like BlobVec::get_unchecked will produce pointers that are not properly aligned.

This problem is potentially observable with World::init_component_with_descriptor.

Solution

  • Round up the size of the layout to a multiple of its alignment in BlobVec::new.
  • Add test to demonstrate the problem.

@github-actions
Copy link
Contributor

Welcome, new contributor!

Please make sure you've read our contributing guide and we look forward to reviewing your pull request shortly ✨

@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 Oct 15, 2023
@@ -440,6 +442,21 @@ fn repeat_layout(layout: &Layout, n: usize) -> Option<(Layout, usize)> {
}
}

// TODO: replace with `Layout::pad_to_align` if/when it stabilizes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a tracking issue for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The utility methods for Layout are part of alloc_layout_extra

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// TODO: replace with `Layout::pad_to_align` if/when it stabilizes
// TODO: replace with `Layout::pad_to_align` if/when it stabilizes (https://github.com/rust-lang/rust/issues/55724)

@alice-i-cecile alice-i-cecile added this to the 0.13 milestone Oct 15, 2023
@rj00a
Copy link
Contributor Author

rj00a commented Oct 15, 2023

Ok so simply changing the size of item_layout doesn't work because initialize_unchecked will copy more data from the src pointer than it's supposed to, hence the Miri error. Might want to track "item size" and "item size + padding" as two separate things. That, or place some restrictions on item_layout to avoid the situation.

@alice-i-cecile alice-i-cecile added the D-Complex Quite challenging from either a design or technical perspective. Ask for help! label Oct 15, 2023
@robojeb
Copy link
Contributor

robojeb commented Oct 16, 2023

Ok so simply changing the size of item_layout doesn't work because initialize_unchecked will copy more data from the src pointer than it's supposed to, hence the Miri error. Might want to track "item size" and "item size + padding" as two separate things. That, or place some restrictions on item_layout to avoid the situation.

It would make more sense to me to store cell_layout and item_size rather than item_layout + padding.

The common operations are going to be "Offset a pointer to find a cell in the array" and "Copy item_size bytes in/out of a cell". So it feels like less mental overhead to store those values.

Copy link
Contributor

@robojeb robojeb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does repeat_layout() need to recompute the padding anymore if self.item_layout is being modified in the constructor to be "Item Size + Padding" anyway?

@james7132 james7132 self-requested a review October 20, 2023 03:20
@alice-i-cecile alice-i-cecile removed this from the 0.13 milestone Jan 24, 2024
@BenjaminBrienen BenjaminBrienen added X-Contentious There are nontrivial implications that should be thought through D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed D-Complex Quite challenging from either a design or technical perspective. Ask for help! labels Jan 23, 2025
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 D-Straightforward Simple bug fixes and API improvements, docs, test and examples P-Unsound A bug that results in undefined compiler behavior S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged X-Contentious There are nontrivial implications that should be thought through
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants