From b02a1d741c02174269311fa36210e15c740ba048 Mon Sep 17 00:00:00 2001 From: TheRawMeatball Date: Mon, 1 Aug 2022 16:26:04 +0300 Subject: [PATCH] fix ub --- crates/bevy_ecs/src/entity/mod.rs | 25 ++++---------------- examples/hello_world.rs | 38 +++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/crates/bevy_ecs/src/entity/mod.rs b/crates/bevy_ecs/src/entity/mod.rs index b48fb3d4e6a5a..4a883ccff785e 100644 --- a/crates/bevy_ecs/src/entity/mod.rs +++ b/crates/bevy_ecs/src/entity/mod.rs @@ -42,8 +42,7 @@ pub use map_entities::*; use crate::{archetype::ArchetypeId, storage::SparseSetIndex}; use std::{ convert::TryFrom, - fmt, - mem::{self, MaybeUninit}, + fmt, mem, sync::atomic::{AtomicI64, Ordering}, }; @@ -611,23 +610,9 @@ impl Entities { let free_cursor = self.free_cursor.get_mut(); *free_cursor = 0; self.meta.reserve(count); - const DO_UB: bool = false; - if DO_UB { - // the EntityMeta struct only contains integers, and it is valid to have all bytes set to u8::MAX - self.meta.as_mut_ptr().write_bytes(u8::MAX, count); - } else { - self.meta.resize( - count, - EntityMeta { - generation: u32::MAX, - _padding: MaybeUninit::uninit(), - location: EntityLocation { - archetype_id: ArchetypeId::INVALID, - index: usize::MAX, // dummy value, to be filled in - }, - }, - ); - } + // the EntityMeta struct only contains integers, and it is valid to have all bytes set to u8::MAX + self.meta.as_mut_ptr().write_bytes(u8::MAX, count); + self.meta.set_len(count); self.len = count as u32; } @@ -655,14 +640,12 @@ impl Entities { #[repr(C)] pub struct EntityMeta { pub generation: u32, - pub _padding: MaybeUninit, pub location: EntityLocation, } impl EntityMeta { const EMPTY: EntityMeta = EntityMeta { generation: 0, - _padding: MaybeUninit::uninit(), location: EntityLocation { archetype_id: ArchetypeId::INVALID, index: usize::MAX, // dummy value, to be filled in diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 3f09f5c4c7ab9..aa7262e4f9522 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,9 +1,37 @@ -use bevy::prelude::*; +use bevy::{ + diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, + prelude::*, +}; -fn main() { - App::new().add_system(hello_world_system).run(); +#[derive(Component)] +pub struct Nothing; + +#[derive(Bundle)] +pub struct NoBundle { + nothing: Nothing, +} + +fn startup(mut commands: Commands) { + let mut entities = Vec::new(); + for _ in 0..40_000_000 { + entities.push(NoBundle { nothing: Nothing }); + } + + commands.spawn_batch(entities); } -fn hello_world_system() { - println!("hello world"); +fn main() { + App::new() + .insert_resource(WindowDescriptor { + width: 1270.0, + height: 720.0, + title: String::from("Bug"), + ..Default::default() + }) + .insert_resource(ClearColor(Color::rgb(0.211, 0.643, 0.949))) + .add_plugin(FrameTimeDiagnosticsPlugin::default()) + .add_plugin(LogDiagnosticsPlugin::default()) + .add_plugins(DefaultPlugins) + .add_startup_system(startup) + .run(); }