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
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions crates/bevy_ecs/src/storage/blob_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ impl BlobVec {
capacity: usize,
) -> BlobVec {
if item_layout.size() == 0 {
let align = NonZeroUsize::new(item_layout.align()).expect("alignment must be > 0");
BlobVec {
swap_scratch: NonNull::dangling(),
data: NonNull::dangling(),
data: bevy_ptr::dangling_with_align(align),
capacity: usize::MAX,
len: 0,
item_layout,
Expand Down Expand Up @@ -405,7 +406,8 @@ const fn padding_needed_for(layout: &Layout, align: usize) -> usize {

#[cfg(test)]
mod tests {
use crate::ptr::OwningPtr;
use crate as bevy_ecs; // required for derive macros
use crate::{component::Component, ptr::OwningPtr, world::World};

use super::BlobVec;
use std::{alloc::Layout, cell::RefCell, rc::Rc};
Expand Down Expand Up @@ -544,4 +546,26 @@ mod tests {
// SAFETY: drop is able to drop a value of its `item_layout`
let _ = unsafe { BlobVec::new(item_layout, Some(drop), 0) };
}

#[test]
fn aligned_zst() {
JoJoJet marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Component)]
#[repr(align(32))]
struct Zst;

let mut world = World::default();
world.spawn(Zst);
world.spawn(Zst);
world.spawn(Zst);
world.spawn_empty();

let mut count = 0;

let mut q = world.query::<&Zst>();
for &Zst in q.iter(&world) {
count += 1;
}

assert_eq!(count, 3);
}
}
12 changes: 11 additions & 1 deletion crates/bevy_ptr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#![no_std]
#![warn(missing_docs)]

use core::{cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, ptr::NonNull};
use core::{
cell::UnsafeCell, marker::PhantomData, mem::MaybeUninit, num::NonZeroUsize, ptr::NonNull,
};

/// Type-erased borrow of some unknown type chosen when constructing this type.
///
Expand Down Expand Up @@ -243,6 +245,14 @@ impl<'a, T> From<&'a [T]> for ThinSlicePtr<'a, T> {
}
}

/// Creates a dangling pointer with specified alignment.
/// See [`NonNull::dangling`].
pub fn dangling_with_align(align: NonZeroUsize) -> NonNull<u8> {
// SAFETY: The pointer will not be null, since it was created
// from the address of a `NonZeroUsize`.
unsafe { NonNull::new_unchecked(align.get() as *mut u8) }
}

mod private {
use core::cell::UnsafeCell;

Expand Down