-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Forbid unsafe in most crates in the engine #12684
Conversation
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.
Definitely a good change. Ideally, Bevy would contain zero unsafe
code. While that may be impossible, it's still worth minimising the risk.
@@ -1,3 +1,5 @@ | |||
#![allow(unsafe_code)] |
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.
😂 No targeted allows here.
.map(|(i, section)| { | ||
// SAFETY: we exited early earlier in this function if | ||
// one of the fonts was missing. | ||
let font = unsafe { fonts.get(§ion.style.font).unwrap_unchecked() }; |
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.
Good choice, this is the sort of needless unsafe I was hoping to clean up.
@james7132 @JMS55 @rodolphito Meshlets added new unsafe code that will need to be allowed to get this mergeable. |
@JMS55 is there a reason why PersistentGpuBufferable is an unsafe trait? Both of it's functions are not used in an unsafe context without existing safety checks. |
Iirc it's because there's no checks for it, but the data has to be 4 byte aligned (or whatever wgpu wants), and it needs to accurately report the size in one function that then gets written in the second. |
If that's the case, the APIs on wgpu's end need to be unsafe, which I don't think is the case. As far as CPU-side memory safety goes, this looks safe to me, even if the implementation of the trait is wrong. |
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.
Looks good! What's your reason for forbid
-ing many of the crates when they already inherit deny
from the workspace?
Forbid can't be locally allowed. In crates where we really really shouldn't need unsafe, it's a better choice. |
Merging is blocked on #12728 |
Allowing it for now and using #12743 to address the soundness issues. |
Objective
Resolves #3824.
unsafe
code should be the exception, not the norm in Rust. It's obviously needed for various use cases as it's interfacing with platforms and essentially running the borrow checker at runtime in the ECS, but the touted benefits of Bevy is that we are able to heavily leverage Rust's safety, and we should be holding ourselves accountable to that by minimizing our unsafe footprint.Solution
Deny
unsafe_code
workspace wide. Add explicit exceptions for the following crates, and forbid it in almost all of the others.Several uses of unsafe were rewritten, as they did not need to be using them:
Option::unchecked
could be rewritten as a normal for loop and match instead of an iterator.