Skip to content
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] - remove potential ub in render_resource_wrapper #7279

Conversation

robtfm
Copy link
Contributor

@robtfm robtfm commented Jan 19, 2023

Objective

as noted by james, transmuting arcs may be UB.

we now store a *const () pointer internally, and only rely on ptr.cast::<()>().cast::<T>() == ptr.

as a happy side effect this removes the need for boxing the value, so todo: potentially use this for release mode as well

@robtfm robtfm added A-Rendering Drawing game state to the screen P-Unsound A bug that results in undefined compiler behavior labels Jan 19, 2023
crates/bevy_render/src/render_resource/resource_macros.rs Outdated Show resolved Hide resolved
pub struct $wrapper_type(Option<std::sync::Arc<Box<()>>>);
#[derive(Debug)]
// safety invariant: when self.0 is Some(ptr), it comes from `into_raw` of an Arc<$wgpu_type> with a strong ref.
pub struct $wrapper_type(Option<*const ()>);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*const () is Copy. Do you still need this to be an Option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, so that when we consume the arc in try_unwrap we don't then consume it again in Drop

Copy link
Member

@james7132 james7132 Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just forget or ManuallyDrop self in try_unwrap?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mentioned that on the original issue here. i can if you like, i think it would be functionally the same, but i feel it would make the safety invariant less clear to verify (that the arc is valid iff the pointer is Some).

are you looking to avoid using extra space for the option?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the extra space and the extra branch on all of these operations. If that's removed, we can probably use this implementation for the release mode too and avoid the need for a cfg block. (Provided it works out in the benchmarks/stress tests).

Not too big a deal if we avoid this. This will likely all go away once the upstream compiler issue is fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

option removed.

its not committed here but i've tested in release, i see no impact on many_foxes and the compile time for changing code in bevy_render drops by around 40% there as well. not sure what the best benchmark would be though. either way i guess that should probably be a separate pr.

crates/bevy_render/src/render_resource/resource_macros.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/render_resource/resource_macros.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/render_resource/resource_macros.rs Outdated Show resolved Hide resolved
crates/bevy_render/src/render_resource/resource_macros.rs Outdated Show resolved Hide resolved
robtfm and others added 2 commits January 20, 2023 01:43
Co-authored-by: James Liu <contact@jamessliu.com>
robtfm and others added 2 commits January 20, 2023 09:01
@james7132 james7132 self-requested a review January 22, 2023 10:07
@BoxyUwU
Copy link
Member

BoxyUwU commented Feb 3, 2023

have you verified that this still keeps the compile time wins? I am rather confused as to how this is doing anything

Copy link
Member

@BoxyUwU BoxyUwU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like a really bad tradeoff to me and we should remove this abstraction seeing as how it was implemented wrong and resulted in bevy being unsound for like 4+ months 🤷‍♀️

@robtfm
Copy link
Contributor Author

robtfm commented Feb 3, 2023

have you verified that this still keeps the compile time wins? I am rather confused as to how this is doing anything

yes it keeps the improvement. the rustc issue linked in the code and the discussion on #5950 talk about the why, but it may not even be that exact issue. will need to recheck after a compiler fix.

@BoxyUwU BoxyUwU added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Feb 5, 2023
@alice-i-cecile
Copy link
Member

This feels like a really bad tradeoff to me and we should remove this abstraction seeing as how it was implemented wrong and resulted in bevy being unsound for like 4+ months woman_shrugging

Seconding this sentiment. There's a lot of tricky complexity here. That said, fix seems fine and it's good to resolve this particular instance of UB.

bors r+

bors bot pushed a commit that referenced this pull request Feb 6, 2023
# Objective

[as noted](#5950 (comment)) by james, transmuting arcs may be UB.
 
we now store a `*const ()` pointer internally, and only rely on `ptr.cast::<()>().cast::<T>() == ptr`.

as a happy side effect this removes the need for boxing the value, so todo: potentially use this for release mode as well
@bors bors bot changed the title remove potential ub in render_resource_wrapper [Merged by Bors] - remove potential ub in render_resource_wrapper Feb 6, 2023
@bors bors bot closed this Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen P-Unsound A bug that results in undefined compiler behavior S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants