Flawed `InvariantsEq` safety proof for `Unalign` incorrectly assumes `try_deref` is total
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 84/100
- Issue 类型
- 文档
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- rust
调研方向
阅读 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 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.
- 主要语言
- Rust
- 星标
- 2.6k
- 派生
- 194
- 平均合并
- 18 小时 15 分钟
- 30 天内合并 PR
- 14
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
google/zerocopy 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
维护者通常 1 天内回复
-
Features to support optional defmt for the byteorder types可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭customer-request
难度 2/5 1-3 小时 新手友好度 72/100
google/zerocopy#3914 · 1 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 88/100
google/zerocopy#3766 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
google/zerocopy#3495 · 8 条评论 ·
维护者通常 1 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 5 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
eandualem/agent-interface#290 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
benchmark-review
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复