Skip to content

Commit

Permalink
fix ub
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRawMeatball committed Aug 1, 2022
1 parent becb9e7 commit b02a1d7
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
25 changes: 4 additions & 21 deletions crates/bevy_ecs/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -655,14 +640,12 @@ impl Entities {
#[repr(C)]
pub struct EntityMeta {
pub generation: u32,
pub _padding: MaybeUninit<u32>,
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
Expand Down
38 changes: 33 additions & 5 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit b02a1d7

Please sign in to comment.