Flawed `InvariantsEq` safety proof for `Unalign` incorrectly assumes `try_deref` is total
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 84/100
- Tipo di issue
- Documentazione
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- rust
- Ambito
- documentation
Direzione di ricerca
Leggi le implementazioni di InvariantsEq e il commento SAFETY in zerocopy/src/pointer/transmute.rs:396-403, quindi esamina Unalign::try_deref in zerocopy/src/wrappers.rs:202-211. Aggiorna la motivazione di sicurezza per indicare la precondizione di coesistenza allineata o le proprietà di layout pertinenti, lasciando invariata l’implementazione sound.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
- Lingua principale
- Rust
- Stelle
- 2.6k
- Fork
- 194
- Merge medio
- 18h 15m
- PR unite (30g)
- 14
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di google/zerocopy
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
I maintainer di solito rispondono entro 1 giorno
-
Features to support optional defmt for the byteorder typesForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertacustomer-request
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
google/zerocopy#3914 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Misaligned pointer dereference in `Ref` due to incomplete `ByteSlice` move-stability contractAperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
google/zerocopy#3766 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
google/zerocopy#3495 · 8 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di google/zerocopy
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 5 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
eandualem/agent-interface#290 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
I maintainer di solito rispondono entro 1 giorno
-
benchmark-review
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno