Skip to content

EntityRef as WorldQuery is unsound. #9422

Closed

Description

@joseph-gio

Bevy version

Main branch.

What you did

    #[test]
    fn unsound_disjoint_mutable_access() {
        fn unsound_system(
            mut query: Query<(Entity, &mut TestComponent)>,
            refs: Query<super::EntityRef, Without<TestComponent>>,
        ) {
            let (id, mut c) = query.iter_mut().next().unwrap();
            let world = refs.iter().next().unwrap().world();

            // We now have mutable and shared access to the same component at the same time.
            let c2 = world.entity(id).get::<TestComponent>().unwrap();
            *c = TestComponent(1);
            println!("{c2:?}");
        }

        let mut world = World::new();
        world.spawn(TestComponent(0));
        world.spawn_empty();

        let mut schedule = Schedule::new();
        schedule.add_systems(unsound_system);

        schedule.run(&mut world);
    }

What went wrong

This should panic at runtime since the accesses of the two system params conflict. Instead, it runs and allows aliased mutable access.

Activity

  1. added
    C-BugAn unexpected or incorrect behavior
    S-Needs-TriageThis issue needs to be labelled
    A-ECSEntities, components, systems, and events
    I-UnsoundA bug that results in undefined compiler behavior
    and removed
    S-Needs-TriageThis issue needs to be labelled
    on Aug 11, 2023
  2. added
    P-HighThis is particularly urgent, and deserves immediate attention
    on Aug 12, 2023
  3. james7132 commented on Aug 12, 2023

    @james7132
    Member

    EntityRef::world was supposed to be removed in #6960 to make this impossible. Apparently it wasn't. Probably the result of a bad merge or rebase.

  4. joseph-gio commented on Aug 16, 2023

    @joseph-gio
    MemberAuthor

    Ah, looks like it snuck back in during this merge: 0c91c16

  5. added a commit that references this issue on Aug 29, 2023
    bc8bf34
  6. added a commit that references this issue on Jan 9, 2024
    77d0070
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ECSEntities, components, systems, and eventsC-BugAn unexpected or incorrect behaviorI-UnsoundA bug that results in undefined compiler behaviorP-HighThis is particularly urgent, and deserves immediate attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions