Fix soundness: use raw pointers instead of erased lifetimes#64
Merged
somethingelseentirely merged 1 commit intomainfrom Mar 17, 2026
Merged
Fix soundness: use raw pointers instead of erased lifetimes#64somethingelseentirely merged 1 commit intomainfrom
somethingelseentirely merged 1 commit intomainfrom
Conversation
…el UB The `data` fields in `Bytes` and `View` stored `&'static [u8]` and `&'static T` references alongside their `Arc<dyn ByteOwner>` owner. Under both Stacked Borrows and Tree Borrows, passing these types by value to any function (including `std::mem::drop`) while holding the last strong reference caused undefined behavior: the reference's "protected" tag conflicted with the Arc deallocation of the owner. This changes the fields to raw pointers (`*const [u8]` and `*const T`), which do not receive protection under either borrow model. The `erase_lifetime` helper is removed as it is no longer needed. Also adds a Miri test suite (36 tests) covering all unsafe code paths: lifetime erasure, weak reference upgrades, try_unwrap_owner pointer reconstruction, view operations, and complex drop orderings. https://claude.ai/code/session_01Q8DdoMzpG97eRPDx6dTwhP
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.
Summary
This PR fixes undefined behavior in
BytesandViewby replacing lifetime-erased references (&'static [u8]and&'static T) with raw pointers (*const [u8]and*const T). The previous approach violated both Stacked Borrows and Tree Borrows whenBytes/Viewwere passed by value to functions (includingdrop) while holding the last strong reference to their owner.Key Changes
Changed internal storage:
Bytes::dataandView::datanow use raw pointers instead of lifetime-erased references!Send + !Syncby default, butunsafe impl Send/Syncis justified since theArc<dyn ByteOwner>guarantees thread-safe accessRemoved
erase_lifetimehelper: No longer needed with raw pointer storage; lifetime erasure now happens implicitly via pointer castingUpdated all data access paths:
get_data()now returns a raw pointer; callers dereference it withunsafe { &*self.data }set_data()accepts&[u8](not&'static) and stores it as a raw pointerdata_ptr()new method returns the raw pointer directly for internal useview_prefix,view_suffix, etc.) updated to work with raw pointersAdded comprehensive Miri test suite (
tests/miri.rs):try_unwrap_owner, view operationsscripts/miri.sh) that runs tests with Tree Borrows (the recommended borrow model)Updated documentation:
Box<T>(Tree Borrows handles it correctly)Implementation Details
The core insight is that when
Bytesis passed by value to a function and that function call is the last use of the struct, the borrow checker "protects" any&'staticreferences as function arguments. If theArcowner is dropped during that function (e.g., indrop), the protected reference becomes dangling—undefined behavior under both borrow models. Raw pointers bypass this protection mechanism while maintaining safety through theArcowner's lifetime guarantee.https://claude.ai/code/session_01Q8DdoMzpG97eRPDx6dTwhP