Flawed `InvariantsEq` safety proof for `Unalign` incorrectly assumes `try_deref` is total

Đang mở Phù hợp với người mới
#3,764 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

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 obtain let t = u.try_deref().unwrap() because try_deref doesn't consume self, 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:

  1. The InvariantsEq contract only constrains referents where both &T and &Unalign<T> can validly exist at the same time, and the existence of a valid &T already implies the referent address is aligned to align_of::<T>() (in which case u.try_deref() does succeed).
  2. Structurally, Unalign<T> is a single-field #[repr(C, packed)] wrapper around T, so it has identical size, bit validity, and UnsafeCell byte ranges as T.
  3. Downstream pointer transmutations (Ptr::transmute) preserve the Unaligned alignment invariant state, requiring an explicit try_into_aligned() check before materializing &T.
Affected Files
  • zerocopy/src/pointer/transmute.rs
  • zerocopy/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

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của google/zerocopy

Tất cả issue của google/zerocopy

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.