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

未关闭 适合新手
#3,764 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
84/100
Issue 类型
文档
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
documentation

调研方向

阅读 zerocopy/src/pointer/transmute.rs:396-403 中的 InvariantsEq 实现和 SAFETY 注释,然后检查 zerocopy/src/wrappers.rs:202-211 中的 Unalign::try_deref。更新安全性论证,说明对齐共存前提或相关的 layout 属性,同时保持 sound 实现不变。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Rust
星标
2.6k
派生
194
平均合并
18 小时 15 分钟
30 天内合并 PR
14

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

google/zerocopy 的其他 Issue

查看 google/zerocopy 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。