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

Aperta Adatta ai principianti
#3,764 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

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 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.

Lingua principale
Rust
Stelle
2.6k
Fork
194
Merge medio
18h 15m
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google/zerocopy

Tutte le issue di google/zerocopy

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.