-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Replace transmute with bounded manual Send
impl
#2830
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
Open
djkoloski
wants to merge
1
commit into
hyperium:master
Choose a base branch
from
djkoloski:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -5,7 +5,6 @@ use http::HeaderMap; | |
use pin_project_lite::pin_project; | ||
use std::error::Error as StdError; | ||
use std::io::{self, Cursor, IoSlice}; | ||
use std::mem; | ||
use std::task::Context; | ||
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; | ||
use tracing::{debug, trace, warn}; | ||
|
@@ -409,15 +408,14 @@ fn h2_to_io_error(e: h2::Error) -> io::Error { | |
} | ||
} | ||
|
||
struct UpgradedSendStream<B>(SendStream<SendBuf<Neutered<B>>>); | ||
struct UpgradedSendStream<B>(SendStream<SendBuf<B>>); | ||
|
||
impl<B> UpgradedSendStream<B> | ||
where | ||
B: Buf, | ||
{ | ||
unsafe fn new(inner: SendStream<SendBuf<B>>) -> Self { | ||
assert_eq!(mem::size_of::<B>(), mem::size_of::<Neutered<B>>()); | ||
Self(mem::transmute(inner)) | ||
Self(inner) | ||
} | ||
|
||
fn reserve_capacity(&mut self, cnt: usize) { | ||
|
@@ -442,30 +440,24 @@ where | |
} | ||
|
||
unsafe fn as_inner_unchecked(&mut self) -> &mut SendStream<SendBuf<B>> { | ||
&mut *(&mut self.0 as *mut _ as *mut _) | ||
&mut self.0 | ||
} | ||
} | ||
|
||
#[repr(transparent)] | ||
struct Neutered<B> { | ||
_inner: B, | ||
impossible: Impossible, | ||
} | ||
|
||
enum Impossible {} | ||
|
||
unsafe impl<B> Send for Neutered<B> {} | ||
const _: () = { | ||
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. Why use an unnamed const rather than a private module? 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. Private modules need names, |
||
#[repr(transparent)] | ||
struct Sendable<B>(B); | ||
|
||
impl<B> Buf for Neutered<B> { | ||
fn remaining(&self) -> usize { | ||
match self.impossible {} | ||
} | ||
unsafe impl<B> Send for Sendable<B> {} | ||
|
||
fn chunk(&self) -> &[u8] { | ||
match self.impossible {} | ||
} | ||
|
||
fn advance(&mut self, _cnt: usize) { | ||
match self.impossible {} | ||
} | ||
} | ||
// Because `B` can't be sent through an `UpgradedSendStream`, we want to implement `Send` even if | ||
// `B` is not. We can do this by only implementing `Send` on `UpgradedSendStream<B>` if | ||
// `SendStream<StreamBuf<Sendable<B>>>` is also `Send`. This is better than unconditionally | ||
// implementing it because in that case `UpgradedSendStream` would be `Send` even when | ||
// `SendStream<SendBuf<B>>` is not (for some `B: Send`). This could be the case if `SendStream` or | ||
// `SendBuf` stopped being `Send` (even for sendable `B`). | ||
unsafe impl<B> Send for UpgradedSendStream<B> | ||
where | ||
SendStream<SendBuf<Sendable<B>>>: Send, | ||
{} | ||
}; |
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.
I don't like this. The whole point of
Neutered<B>
is that you can't create one, whereas here someone can use the field directly and send aB
over the channel.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.
My impression was that
new
/as_inner_unchecked
were meant to act as guards against sending aB
. For example, the safety conditions fornew
would be "This must be the only instance of the stream" andas_inner_unchecked
would be "No messages must be sent through the returned stream".It's a little unclear to me what the desired API is since this is all private. If the goal is to make the stream impossible to use incorrectly, even in private contexts, then
as_inner_unchecked
shouldn't exist. Even then, someone with private access could access the inner field and do the transmute by hand, so I don't think it's feasible to prevent misuse in private contexts.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.
Yes they are, but as it doesn't live in its own module, the rest of the code in that module is free to do
.0
and access theSendBuf<B>
without the unsafety ofas_inner_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.
(Hit cmd-enter too early, sorry)
The goal is to make the stream impossible to use incorrectly in safe 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.
Perhaps we should move
UpgradedSendStream
into a separate module and make it public. That way we can use visibility to prevent users from accessing.0
while providing an API without usingtransmute
under the hood.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 attempted this and found that
write
was being called from theAsyncWrite
impl forH2Upgraded
. Should these uses additionally haveas_inner_unchecked
for some reason? Or am I misunderstanding why the original implementationNeutered
the stream?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.
There is no
H2Upgraded
in this patch, did you mean another type?The
Neutered<_>
thing is to show that we are not using that variantSendBuf::Buf
there, onlySendBuf::Cursor
.