-
Notifications
You must be signed in to change notification settings - Fork 710
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
Document or remove some uses of unsafe
#1657
Conversation
Codecov Report
@@ Coverage Diff @@
## main #1657 +/- ##
=======================================
Coverage 92.09% 92.09%
=======================================
Files 132 132
Lines 18869 18915 +46
Branches 196 196
=======================================
+ Hits 17377 17420 +43
- Misses 1455 1458 +3
Partials 37 37
... and 4 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
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.
In general I am open to doing this but I think if we're going to use zerocopy we should really get more use out of it than this.
I will follow up on this when I am done with the BoringSSL merge.
@@ -12,8 +12,14 @@ impl PublicExponent { | |||
|
|||
// TODO: Use `NonZeroU64::new(...).unwrap()` when `feature(const_panic)` is | |||
// stable. | |||
pub(super) const _3: Self = Self(unsafe { NonZeroU64::new_unchecked(3) }); | |||
pub(super) const _65537: Self = Self(unsafe { NonZeroU64::new_unchecked(65537) }); | |||
pub(super) const _3: Self = Self(match NonZeroU64::new(3) { |
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.
Remove the TODO above this.
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.
I can do that, but it might be good to keep it since .unwrap()
would still be preferable to a match. Lmk either way.
@@ -29,7 +35,10 @@ impl PublicExponent { | |||
// | |||
// TODO: Use `NonZeroU64::new(...).unwrap()` when `feature(const_panic)` is | |||
// stable. | |||
const MAX: Self = Self(unsafe { NonZeroU64::new_unchecked((1u64 << 33) - 1) }); | |||
const MAX: Self = Self(match NonZeroU64::new((1u64 << 33) - 1) { |
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.
Ditto: Remove the TODO.
@@ -25,7 +27,7 @@ pub trait FromByteArray<T> { | |||
|
|||
macro_rules! define_endian { | |||
($endian:ident) => { | |||
#[derive(Clone, Copy)] | |||
#[derive(Clone, Copy, FromZeroes, FromBytes, AsBytes)] |
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.
I think a lot of what's in endian.rs likely duplicates the zerocopy functionality that is being derived here. I suspect we could get rid of even more of the unsafe
by removing that duplication.
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 idea. We've got the byteorder module which provides analogous types, although they don't provide alignment guarantees, which can pessimize code.
We've got an open issue to support versions of those types with platform-native alignment, but I haven't had time to work on it yet. If that's the thing that would unblock this, I'd be happy to prioritize it.
a466b53
to
4056fb9
Compare
MSRV: I think bumping the MSRV to 1.61 right now would be fine, but it would be good to understand the MSRV policy going forward. Ideally in other projects I've wanted to stick to a Tokio-like policy of at least 6 months, but also generally be fairly conservative unless there are major benefits to be had (beyond small syntactic conveniences). I suppose the main qualm I have here is the dependencies that would be brought in. Right now rustls has fairly minimal dependencies:
Bringing in the macros from zerocopy-derive would add syn, quote, and proc-macro2. On the one hand, that's quite a bit of code to compile -- on the other hand, they're bound to be part of downstream's dependency graphs already. @joshlf have you ever contemplated writing some token stream processors without syn and quote? This seems like the kind of dependency where it might be worth it. @joshlf it looks like you were active within the Safe Transmute project. What happened with that? It seems to have petered out, why? This really seems like something that core could/should provide. |
zerocopy's
It's still in progress. There haven't been a ton of development cycles available to spend on it, but there have been some. I wouldn't expect it to land soon, but I would still expect it to land. |
Unfortunately, I made a foobar with this PR and merged it unintendedly as-is. I reverted the merge in #1661. (I meant to merge the latest BoringSSL merge.) In any case, I think we need to try to using proc_macros and adding syn, etc. as dependencies. I am open to the idea of taking byteorder and related as dependencies but I would prefer to not do it right now until we have a clearer story for how we're going to manage dependencies w.r.t. the FIPS validation. So I think if we could avoid adding I would like to split this PR up into multiple ones that address the individual issues:
|
Submitted #1664 Also submitted one safety comment in #1665
As best I can tell, it's just hard to review. I took a stab at cleaning it up, but I realized that |
Let's have a chat first. I am planning to do "deeper surgery" anyway to make all these union and union- like things much clearer throughout the codebase. Briefly: we trying to model enums where each variant implements a particular trait. This allows us to simulate support for returning |
Hey @briansmith, I'm happy to have this merged if it's in a state you're comfortable with, but I also know you were hesitant about taking another dependency. I'd be happy to make any changes or chat about the overall approach.
I can also remove the
derive
feature fromzerocopy
if you'd prefer; that's the bulk of the compilation time (zerocopy itself without the derive is a fairly small library). I'm not sure if compilation time is your primary concern, but I know it is for some crate authors.