-
Notifications
You must be signed in to change notification settings - Fork 121
feat(io): fix ancillary API to avoid UB #737
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
Merged
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0f1c13b
feat(io): new AncillaryBuf::build() API
fantix 034429c
fix(io): redo new AncillaryBuilder API
fantix 8e75b2d
Merge remote-tracking branch 'origin/master' into refactor-cmsg-3
fantix eff182a
fix(io): address CR issues
fantix 1b1af11
Merge remote-tracking branch 'origin/master' into refactor-cmsg-3
fantix f67e03c
fix(io): fix API to avoid UB
fantix fedc0bf
fix(io): gate libc::in_pktinfo for Linux/Android
fantix 88fa58d
Merge remote-tracking branch 'origin/master' into refactor-cmsg-3
fantix 6358127
fix(io): replace with Pod, fix hirarchy and docs
fantix 66d6220
fix(io,quic): fix decode_data(), propagate error
fantix 8264129
fix(quic): unsafe union access on Windows
fantix dcbab5e
Merge branch 'master' into refactor-cmsg-3
fantix 6dd08dd
chore(quic): use helpful panic message
fantix aabe62e
fix(io): enable bytemuck:min_const_generics
fantix 2f8132f
Merge branch 'master' into refactor-cmsg-3
fantix 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| use std::mem::MaybeUninit; | ||
|
|
||
| use super::{AncillaryData, CodecError, copy_from_bytes, copy_to_bytes}; | ||
|
|
||
| /// Marker trait to enable automatic `AncillaryData` implementation via | ||
| /// bytemuck. | ||
| /// | ||
| /// Types that implement both [`bytemuck::NoUninit`] and this trait will | ||
| /// automatically implement [`AncillaryData`] using a simple byte-wise | ||
| /// encoding/decoding. | ||
| /// | ||
| /// # Safety | ||
| /// | ||
| /// This trait should only be implemented for types where a simple byte-wise | ||
| /// copy is a valid encoding/decoding strategy. The type must also implement | ||
| /// `bytemuck::NoUninit` to ensure it has no uninitialized bytes. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// ``` | ||
| /// use compio_io::ancillary::BytemuckMarker; | ||
| /// | ||
| /// #[derive(Clone, Copy)] | ||
| /// #[repr(C)] | ||
| /// struct MyType { | ||
| /// value: u32, | ||
| /// } | ||
| /// | ||
| /// unsafe impl bytemuck::NoUninit for MyType {} | ||
| /// impl BytemuckMarker for MyType {} | ||
| /// | ||
| /// // Now MyType automatically implements AncillaryData | ||
| /// ``` | ||
| pub trait BytemuckMarker {} | ||
|
Berrysoft marked this conversation as resolved.
Outdated
fantix marked this conversation as resolved.
Outdated
|
||
|
|
||
| impl<T> AncillaryData for T | ||
| where | ||
| T: bytemuck::NoUninit + BytemuckMarker, | ||
| { | ||
| fn encode(&self, buffer: &mut [MaybeUninit<u8>]) -> Result<(), CodecError> { | ||
| unsafe { copy_to_bytes(self, buffer) } | ||
| } | ||
|
|
||
| fn decode(buffer: &[u8]) -> Result<Self, CodecError> { | ||
| unsafe { copy_from_bytes(buffer) } | ||
| } | ||
| } | ||
|
|
||
| macro_rules! impl_bytemuck_marker { | ||
| ($($t:ty),* $(,)?) => { | ||
| $( | ||
| impl BytemuckMarker for $t {} | ||
| )* | ||
| }; | ||
| } | ||
|
|
||
| impl_bytemuck_marker!( | ||
| (), | ||
| bool, | ||
| char, | ||
| u8, | ||
| u16, | ||
| u32, | ||
| u64, | ||
| u128, | ||
| usize, | ||
| i8, | ||
| i16, | ||
| i32, | ||
| i64, | ||
| i128, | ||
| isize, | ||
| f32, | ||
| f64, | ||
| ); | ||
|
|
||
| macro_rules! impl_bytemuck_marker_for_array { | ||
| ($($N:expr),* $(,)?) => { | ||
| $( | ||
| impl<T> BytemuckMarker for [T; $N] where T: BytemuckMarker {} | ||
| )* | ||
| }; | ||
| } | ||
|
|
||
| impl_bytemuck_marker_for_array!( | ||
| 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, | ||
| 27, 28, 29, 30, 31, 32, 48, 64, 96, 128, 256, 512, | ||
| ); | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.