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

simplex() is unsafe and can cause segfault #10

Open
SteveLauC opened this issue Oct 24, 2024 · 0 comments
Open

simplex() is unsafe and can cause segfault #10

SteveLauC opened this issue Oct 24, 2024 · 0 comments

Comments

@SteveLauC
Copy link
Owner

For a type that implements unsafe trait Split, when being split into 2 read/write halves, Monoio uses UnsafeCell internally to acquire mutable access to the type instance, this is the reason why trait Split is unsafe. It would be only safe if your type can support read/write at the same time.

SimplexStream is basically a bytes::BytesMut, its read cursor is stored as a pointer, and appending to it can trigger re-allocation, which will invalidate the stored pointer, then a segfault would happen if such an invalidated pointer is accessed. So SimplexStream should not implement the unsafe trait Split.

Tokio's split() and read/write halves do not use UnsafeCell, instead, it uses std::sync::Mutex to ensure read and write to the type instance won't happen at the same time, this explains why it can blindly do the split operation to anything that has AsyncRead + AsyncWrite implemented:

pub fn split<T>(stream: T) -> ReadHalf<T>, WriteHalf<T>
where
    T: AsyncRead + AsyncWrite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant