bevy/crates
SpecificProtagonist 13e89a1678
Fix EntityMeta.spawned_or_despawned unsoundness (#19350)
# Objective

#19047 added an `MaybeUninit` field to `EntityMeta`, but did not
guarantee that it will be initialized before access:

```rust
let mut world = World::new();
let id = world.entities().reserve_entity();
world.flush();
world.entity(id);
```

<details>
<summary>Miri Error</summary>

```
error: Undefined Behavior: using uninitialized data, but this operation requires initialized memory
    --> /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/entity/mod.rs:1121:26
     |
1121 |                 unsafe { meta.spawned_or_despawned.assume_init() }
     |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
     |
     = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
     = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
     = note: BACKTRACE:
     = note: inside closure at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/entity/mod.rs:1121:26: 1121:65
     = note: inside `std::option::Option::<&bevy_ecs::entity::EntityMeta>::map::<bevy_ecs::entity::SpawnedOrDespawned, {closure@bevy_ecs::entity::Entities::entity_get_spawned_or_despawned::{closure#1}}>` at /home/vj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:1144:29: 1144:33
     = note: inside `bevy_ecs::entity::Entities::entity_get_spawned_or_despawned` at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/entity/mod.rs:1112:9: 1122:15
     = note: inside closure at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/entity/mod.rs:1094:13: 1094:57
     = note: inside `bevy_ecs::change_detection::MaybeLocation::<std::option::Option<&std::panic::Location<'_>>>::new_with_flattened::<{closure@bevy_ecs::entity::Entities::entity_get_spawned_or_despawned_by::{closure#0}}>` at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/change_detection.rs:1371:20: 1371:24
     = note: inside `bevy_ecs::entity::Entities::entity_get_spawned_or_despawned_by` at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/entity/mod.rs:1093:9: 1096:11
     = note: inside `bevy_ecs::entity::Entities::entity_does_not_exist_error_details` at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/entity/mod.rs:1163:23: 1163:70
     = note: inside `bevy_ecs::entity::EntityDoesNotExistError::new` at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/entity/mod.rs:1182:22: 1182:74
     = note: inside `bevy_ecs::world::unsafe_world_cell::UnsafeWorldCell::<'_>::get_entity` at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/world/unsafe_world_cell.rs:368:20: 368:73
     = note: inside `<bevy_ecs::entity::Entity as bevy_ecs::world::WorldEntityFetch>::fetch_ref` at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/world/entity_fetch.rs:207:21: 207:42
     = note: inside `bevy_ecs::world::World::get_entity::<bevy_ecs::entity::Entity>` at /home/vj/workspace/rust/bevy/crates/bevy_ecs/src/world/mod.rs:911:18: 911:42
note: inside `main`
    --> src/main.rs:12:15
     |
12   |     world.entity(id);
     |
```

</details>

## Solution

- remove the existing `MaybeUninit` in `EntityMeta.spawned_or_despawned`
- initialize during flush. This is not needed for soundness, but not
doing this means we can't return a sensible location/tick for flushed
entities.

## Testing

Test via the snippet above (also added equivalent test).

---------

Co-authored-by: urben1680 <55257931+urben1680@users.noreply.github.com>
2025-05-27 22:45:07 +00:00
..
bevy_a11y Bump accesskit to 0.19 and accesskit_winit to 0.27 (#19160) 2025-05-26 17:48:36 +00:00
bevy_animation Adopt consistent FooSystems naming convention for system sets (#18900) 2025-05-06 15:18:03 +00:00
bevy_anti_aliasing Remove Shader weak_handles from bevy_anti_aliasing. (#19391) 2025-05-27 03:14:55 +00:00
bevy_app Mention in .add_observer() docs that first parameter must be a Trigger (#19315) 2025-05-26 20:06:08 +00:00
bevy_asset Remove apostrophes in possessive its (#19244) 2025-05-26 19:53:14 +00:00
bevy_audio Remove apostrophes in possessive its (#19244) 2025-05-26 19:53:14 +00:00
bevy_color Implemented Alpha for f32. (#18653) 2025-05-06 00:00:17 +00:00
bevy_core_pipeline Remove Shader weak_handles from bevy_core_pipeline (except two). (#19395) 2025-05-27 22:32:27 +00:00
bevy_derive Remove upcasting methods + Cleanup interned label code (#18984) 2025-05-26 15:38:12 +00:00
bevy_dev_tools Improve visibility of debug picking node (#18990) 2025-05-26 15:39:49 +00:00
bevy_diagnostic Rename Timer::finished and Timer::paused to is_finished and is_paused (#19386) 2025-05-27 22:24:18 +00:00
bevy_dylib
bevy_ecs Fix EntityMeta.spawned_or_despawned unsoundness (#19350) 2025-05-27 22:45:07 +00:00
bevy_encase_derive
bevy_gilrs refactor(utils): move SyncCell and SyncUnsafeCell to bevy_platform (#19305) 2025-05-27 04:57:26 +00:00
bevy_gizmos Remove Shader weak_handles from bevy_gizmos. (#19394) 2025-05-27 22:32:32 +00:00
bevy_gltf Make sure that serde_json::Map::into_values exists (#19229) 2025-05-26 19:38:28 +00:00
bevy_image Optional explicit compressed image format support (#19190) 2025-05-26 18:00:33 +00:00
bevy_input Simplify bevy_utils Features (#19090) 2025-05-24 01:46:11 +00:00
bevy_input_focus Navigate through TabGroups in order. (#19218) 2025-05-26 19:32:10 +00:00
bevy_internal Simplify bevy_utils Features (#19090) 2025-05-24 01:46:11 +00:00
bevy_log refactor(utils): move SyncCell and SyncUnsafeCell to bevy_platform (#19305) 2025-05-27 04:57:26 +00:00
bevy_macro_utils Better macro errors for get_struct_fields (#17639) 2025-05-26 16:57:03 +00:00
bevy_math Add a Dir4 to resolve #17983 (#19223) 2025-05-26 19:35:07 +00:00
bevy_mesh
bevy_mikktspace
bevy_pbr Remove Shader weak_handles from bevy_pbr (excluding meshlets). (#19365) 2025-05-27 22:32:47 +00:00
bevy_picking don't filter dragged entity out of DragEnter events (#19179) 2025-05-26 17:56:54 +00:00
bevy_platform refactor(utils): move SyncCell and SyncUnsafeCell to bevy_platform (#19305) 2025-05-27 04:57:26 +00:00
bevy_ptr
bevy_reflect Make sure that serde_json::Map::into_values exists (#19229) 2025-05-26 19:38:28 +00:00
bevy_remote Make sure that serde_json::Map::into_values exists (#19229) 2025-05-26 19:38:28 +00:00
bevy_render Remove ArchetypeComponentId and archetype_component_access (#19143) 2025-05-27 19:04:32 +00:00
bevy_scene Make entity generation a new type and remove identifier (#19121) 2025-05-08 04:03:05 +00:00
bevy_sprite Remove Shader weak_handles from bevy_sprite. (#19392) 2025-05-27 04:01:34 +00:00
bevy_state Enable state scoped entities by default (#19354) 2025-05-26 20:26:41 +00:00
bevy_tasks
bevy_text Remove apostrophes in possessive its (#19244) 2025-05-26 19:53:14 +00:00
bevy_time Rename Timer::finished and Timer::paused to is_finished and is_paused (#19386) 2025-05-27 22:24:18 +00:00
bevy_transform Simplify bevy_utils Features (#19090) 2025-05-24 01:46:11 +00:00
bevy_ui Remove Shader weak_handles from bevy_ui. (#19393) 2025-05-27 22:32:40 +00:00
bevy_utils refactor(utils): move SyncCell and SyncUnsafeCell to bevy_platform (#19305) 2025-05-27 04:57:26 +00:00
bevy_window Allow unfocused window creation (#19237) 2025-05-26 20:05:33 +00:00
bevy_winit Allow unfocused window creation (#19237) 2025-05-26 20:05:33 +00:00