Flawed `InvariantsEq` safety proof for `Unalign` incorrectly assumes `try_deref` is total
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 84/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- rust
- Lĩnh vực
- documentation
Hướng nghiên cứu
Đọc các triển khai của InvariantsEq và comment SAFETY tại zerocopy/src/pointer/transmute.rs:396-403, sau đó kiểm tra Unalign::try_deref trong zerocopy/src/wrappers.rs:202-211. Cập nhật phần giải thích về tính an toàn để nêu điều kiện tiên quyết về sự cùng tồn tại được căn chỉnh hoặc các thuộc tính layout liên quan, giữ nguyên triển khai sound.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Agent-discovered bug, and agent-authored writeup.
Summary
In zerocopy/src/pointer/transmute.rs, the InvariantsEq trait denotes that two types have identical safety invariants: specifically, that it is sound for safe code to operate on a &T and a &Self pointing to the same referent at the same time.
The // SAFETY: comment for the InvariantsEq<T> for Unalign<T> implementation (zerocopy/src/pointer/transmute.rs:396-403) justifies this contract as follows:
Given
u: &Unalign<T>, it is already possible to obtainlet t = u.try_deref().unwrap()becausetry_derefdoesn't consumeself, and thus it must be sound for these two references to exist at the same time.
However, Unalign::<T>::try_deref (zerocopy/src/wrappers.rs:202-211) is a partial function that checks alignment via inner.try_into_aligned() and returns Err(AlignmentError) when self is not aligned to align_of::<T>(). On a valid, unaligned &Unalign<T> reference (e.g., at offset 1 inside a #[repr(C, packed)] struct where align_of::<T>() > 1), u.try_deref().unwrap() panics rather than producing a &T.
The implementation itself is sound—no Undefined Behavior is reachable—because:
- The
InvariantsEqcontract only constrains referents where both&Tand&Unalign<T>can validly exist at the same time, and the existence of a valid&Talready implies the referent address is aligned toalign_of::<T>()(in which caseu.try_deref()does succeed). - Structurally,
Unalign<T>is a single-field#[repr(C, packed)]wrapper aroundT, so it has identical size, bit validity, andUnsafeCellbyte ranges asT. - Downstream pointer transmutations (
Ptr::transmute) preserve theUnalignedalignment invariant state, requiring an explicittry_into_aligned()check before materializing&T.
Affected Files
zerocopy/src/pointer/transmute.rszerocopy/src/wrappers.rs
Suggested Fix
Update the // SAFETY: comment on unsafe impl<T> InvariantsEq<T> for Unalign<T> and unsafe impl<T> InvariantsEq<Unalign<T>> for T at zerocopy/src/pointer/transmute.rs:396-403 to note that the InvariantsEq precondition assumes a referent at which both &T and &Unalign<T> can coexist (which implies the referent is aligned for T, making u.try_deref().unwrap() succeed in that precondition domain), or justify the implementation directly from Unalign<T>'s #[repr(C, packed)] single-field layout having identical size, bit validity, and UnsafeCell coverage to T.
- Ngôn ngữ chính
- Rust
- Star
- 2.6k
- Fork
- 194
- Merge trung bình
- 18 giờ 15 phút
- Pull request đã merge (30 ngày)
- 14
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của google/zerocopy
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Features to support optional defmt for the byteorder typesCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mởcustomer-request
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
google/zerocopy#3914 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Misaligned pointer dereference in `Ref` due to incomplete `ByteSlice` move-stability contractĐang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
google/zerocopy#3766 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
google/zerocopy#3495 · 8 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của google/zerocopy
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 5 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
eandualem/agent-interface#290 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
benchmark-review
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày