Repository navigation
refactor(buf): better IoBuf - #555
Merged
Merged
Conversation
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Member
Author
|
I'm thinking of removing the unsafe requirement for constructing What do you think @Berrysoft |
George-Miao
marked this pull request as draft
December 3, 2025 07:10
Member
|
It's OK. Actually I'm considering making the |
Member
|
Another reason we previously make it unsafe is that |
George-Miao
force-pushed
the
refactor/buf
branch
8 times, most recently
from
December 4, 2025 10:23
8beabb6 to
7430913
Compare
George-Miao
force-pushed
the
refactor/buf
branch
from
December 4, 2025 10:29
7430913 to
266ccb4
Compare
George-Miao
marked this pull request as ready for review
December 4, 2025 10:44
Berrysoft
reviewed
Dec 4, 2025
This was referenced Jan 28, 2026
Merged
Closed
Closed
fantix
pushed a commit
to fantix/compio
that referenced
this pull request
Mar 3, 2026
--------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Open
FalkWoldmann
pushed a commit
to FalkWoldmann/compio
that referenced
this pull request
Sep 20, 2026
…ompio-rs#555 Issue compio-rs#220 raised exactly this in 2024 and it was fixed by making IoBuf and IoBufMut unsafe traits. PR compio-rs#555 removed the marker as part of a refactor whose description promised to replace it with a single `unsafe fn buffer`, which never landed. The recommended fix is a restoration of a property the project already decided it wanted.
FalkWoldmann
added a commit
to FalkWoldmann/compio
that referenced
this pull request
Sep 21, 2026
Bug 2 -- unsafe code trusting safe trait methods to agree with each other -- has been fixed four times on this branch, once per call site, because the traits gave unsafe code nothing to rely on. This fixes the cause instead. `IoBuf`, `IoBufMut`, `IoVectoredBuf`, `IoVectoredBufMut` and `SetLen` are `unsafe trait`s again, each stating the obligations unsafe code already depended on: stability of the pointer and length across calls, containment of the initialized prefix within the buffer's extent, an initialized prefix, and idempotent vectored iteration. `IoVectoredBuf`'s idempotency was already written down as a "Note for implementors" -- unsafe code builds an iovec array from one traversal and resolves completions against another, so it was a safety obligation wearing a convention's clothes. This restores the state the crate was in from compio-rs#220 until compio-rs#555 removed the marker in favour of an `unsafe fn buffer` that never landed. BREAKING CHANGE: downstream implementors must write `unsafe impl` and uphold the documented obligations. Code that only uses buffers is unaffected. Two things the change turned up, both verified with Miri: - `BytesMut::as_uninit` built a `capacity()`-long slice from a pointer that resolves through `DerefMut` and so carries provenance for only `len()` bytes. Undefined behaviour whenever the buffer has spare capacity, which for a read buffer is always. Its SAFETY comment asserted the opposite as a "DEPENDENCY LEMMA". The pointer now comes from `spare_capacity_mut`, which `bytes` derives from its own owning pointer. - A first draft of `SetLen`'s contract required `as_init().len() == n` after `set_len(n)`. Three in-tree impls -- `[u8]`, `[u8; N]` and `MmapMut` -- are no-ops, because a fixed-size always-initialized buffer has no boundary to move. The contract now requires only what memory safety needs: that `as_init` never reports an uninitialized byte and never exceeds `as_uninit`. A guarantee the crate's own code violates is worse than none. The defensive measures added earlier all stay. They are no longer load-bearing -- a correct implementation cannot reach them -- but they keep a broken implementation merely wrong rather than unsafe. The adversarial test types are now marked as deliberately contract-violating. Two new guards: - Static assertions that each trait is still `unsafe`. compio-rs#555 dropped the marker and shipped because nothing failed; these fail `cargo check` with E0199. - A contract test that checks stability and containment empirically for every in-tree buffer type, including a partially filled `BytesMut` and a spilled `SmallVec`. It is what found the `BytesMut` bug.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR refactors compio-buf with following changes:
IoSlice{,Mut}. UseIoBuffer{,Mut}insteadIoBufonly requires singleunsafe fn buffer(&self) -> IoBufferandIoBufMutonly requiresfn uninit_len(&self) -> usize.IoSlice{,Mut}is being moved to driver, renamed toSysSliceand can be converted fromIoBuffer{,Mut}.IoVectoredBuf{,Mut}:IoVectoredBufnow only requiresunsafe fn iter_buffer(&self) -> impl Iterator<Item = IoBuffer>;IoVectoredBufMutnow only requiresfn uninit_len_of(&self, idx: usize) -> usize;which returnsuninit_lenof given index corresponding to index initer_buffer.