Harden byte storage aliasing and regeneration - #821
Merged
frankmcsherry merged 6 commits intoSep 22, 2026
Merged
Conversation
Call the backing object’s DerefMut implementation once in construction and regeneration. Derive both the pointer and length from that slice so custom dereference implementations cannot make them disagree.
A backing DerefMut implementation can invalidate the old slice and then panic. Reset the view before calling it so a caught panic leaves an empty buffer that still owns the backing object.
Keep the constructor’s Send requirement in the erased owner type. Clarify that BytesMut is neither Send nor Sync, while its immutable Bytes views are both.
Arc bookkeeping can form shared references covering bytes stored inline in the backing object. Wrap the owner in UnsafeCell so those references can coexist with writes to the disjoint mutable slice. Document the aliasing and synchronization argument.
Discard the old view before exposing the contents of UnsafeCell during regeneration. A failed downcast now leaves an empty view while retaining the owner for a retry with the correct type. Document this behavior and the ownership invariant, and address the pointer and wording nits.
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.
Custom backing objects can return different slices from
DerefandDerefMut, invalidate their allocation before panicking during regeneration, or store bytes inline whereArcbookkeeping forms shared references to them.These changes derive each pointer and length from one mutable slice, clear the old view before downcasting or invoking
DerefMut, and wrap the backing object inUnsafeCell.A failed downcast now returns
Nonewith an empty view while retaining the backing object for a retry with the correct type.The erased owner preserves the constructor's
Sendbound, and the documentation explains the ownership and synchronization invariants and clarifies thatBytesMutitself is neitherSendnorSync.Validation:
cargo test -p timely_bytesandcargo check -p timely_communicationpass.A temporary audit harness with 16 cases passes under both Stacked Borrows and Tree Borrows with strict provenance on nightly 2026-04-27.
The reported failed-downcast UB was not reproduced locally before the reset was moved; the change removes reliance on retaining the old pointer across that operation.
The harness is not included in this PR.