Skip to content

Harden byte storage aliasing and regeneration - #821

Merged
frankmcsherry merged 6 commits into
TimelyDataflow:masterfrom
frankmcsherry:bytes-safety-fixes
Sep 22, 2026
Merged

frankmcsherry merged 6 commits into
TimelyDataflow:masterfrom
frankmcsherry:bytes-safety-fixes

Conversation

@frankmcsherry

Copy link
Copy Markdown
Member

Custom backing objects can return different slices from Deref and DerefMut, invalidate their allocation before panicking during regeneration, or store bytes inline where Arc bookkeeping 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 in UnsafeCell.

A failed downcast now returns None with an empty view while retaining the backing object for a retry with the correct type.
The erased owner preserves the constructor's Send bound, and the documentation explains the ownership and synchronization invariants and clarifies that BytesMut itself is neither Send nor Sync.

Validation: cargo test -p timely_bytes and cargo check -p timely_communication pass.
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.

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.
@frankmcsherry
frankmcsherry merged commit 9efd010 into TimelyDataflow:master Sep 22, 2026
9 checks passed
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

Successfully merging this pull request may close these issues.

1 participant