-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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] - Fix unsoundness in EntityMut::world_scope
#7387
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ce4a192
add a test case for `EntityMut` unsoundness
JoJoJet 3412c82
catch the panic in the test
JoJoJet f134b43
use archetype moves instead of despawning
JoJoJet bb59e54
fix unsoundness
JoJoJet b85453e
rename `on_drop` -> `cleanup_guard`
JoJoJet 0aff619
be overly cautious
JoJoJet f8ff83a
fix a stacked borrows failure
JoJoJet 33cc770
Merge remote-tracking branch 'upstream/main' into world-scope-unsound
JoJoJet ecb90f7
update a comment
JoJoJet dfb9934
add a message to a test panic
JoJoJet 3cb2ab0
Add a link to this PR on the regression test
JoJoJet 41369f3
catch panics using only safe code
JoJoJet feecc66
Revert "catch panics using only safe code"
JoJoJet ed2bc62
use a custom guard type to to perform the cleanup
JoJoJet 708abcc
fix a typo
JoJoJet c082001
make the regression test more rigorous
JoJoJet 75f507f
minor correction to the docs for `EntityMut::update_location`
JoJoJet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -564,14 +564,29 @@ impl<'w> EntityMut<'w> { | |
/// # assert_eq!(new_r.0, 1); | ||
/// ``` | ||
pub fn world_scope<U>(&mut self, f: impl FnOnce(&mut World) -> U) -> U { | ||
let val = f(self.world); | ||
self.update_location(); | ||
val | ||
struct Guard<'w, 'a> { | ||
entity_mut: &'a mut EntityMut<'w>, | ||
} | ||
|
||
impl Drop for Guard<'_, '_> { | ||
#[inline] | ||
fn drop(&mut self) { | ||
self.entity_mut.update_location(); | ||
} | ||
} | ||
|
||
// When `guard` is dropped at the end of this scope, | ||
// it will update the cached `EntityLocation` for this instance. | ||
// This will run even in case the closure `f` unwinds. | ||
let guard = Guard { entity_mut: self }; | ||
f(guard.entity_mut.world) | ||
} | ||
|
||
/// Updates the internal entity location to match the current location in the internal | ||
/// [`World`]. This is only needed if the user called [`EntityMut::world`], which enables the | ||
/// location to change. | ||
/// [`World`]. | ||
/// | ||
/// This is *only* required when using the unsafe function [`EntityMut::world_mut`], | ||
/// which enables the location to change. | ||
pub fn update_location(&mut self) { | ||
self.location = self.world.entities().get(self.entity).unwrap(); | ||
} | ||
|
@@ -856,6 +871,8 @@ pub(crate) unsafe fn take_component<'a>( | |
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::panic::AssertUnwindSafe; | ||
|
||
use crate as bevy_ecs; | ||
use crate::component::ComponentId; | ||
use crate::prelude::*; // for the `#[derive(Component)]` | ||
|
@@ -947,4 +964,28 @@ mod tests { | |
assert!(entity.get_by_id(invalid_component_id).is_none()); | ||
assert!(entity.get_mut_by_id(invalid_component_id).is_none()); | ||
} | ||
|
||
// regression test for https://github.com/bevyengine/bevy/pull/7387 | ||
#[test] | ||
JoJoJet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fn entity_mut_world_scope_panic() { | ||
let mut world = World::new(); | ||
|
||
let mut entity = world.spawn_empty(); | ||
let old_location = entity.location(); | ||
let id = entity.id(); | ||
let res = std::panic::catch_unwind(AssertUnwindSafe(|| { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would move the variable |
||
entity.world_scope(|w| { | ||
// Change the entity's `EntityLocation`, which invalidates the original `EntityMut`. | ||
// This will get updated at the end of the scope. | ||
w.entity_mut(id).insert(TestComponent(0)); | ||
|
||
// Ensure that the entity location still gets updated even in case of a panic. | ||
panic!("this should get caught by the outer scope") | ||
}); | ||
})); | ||
assert!(res.is_err()); | ||
|
||
// Ensure that the location has been properly updated. | ||
assert!(entity.location() != old_location); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we document that
world_scope()
callsupdate_location()
? This was missing in the original code too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a conscious choice not to mention unsafe implementation details in the docs of a safe method
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair. But on the other hand if I see the
update_location()
on docs.rs then I will immediately ask myself "should I use that afterworld_scope()
?", and safety doesn't answer my question; if anything I'd be tempted to think I do need it since it's unsafe butworld_scope()
is not.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs for
EntityMut::update_location
say that it only needs to be called if you are usingEntityMut::world_mut
. Perhaps it could be phrased differently to make that more clear?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then that should be fine.