Validity of Non-matched Patterns

Aperta
#587 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Documentazione
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
compilers

Direzione di ricerca

Start with the foo() pattern and bar() entry point in the issue, focusing on whether a failed discriminant check can avoid reading invalid fields or padding. Done means establishing and documenting the validity rules for non-matching patterns, including the proposed union-of-struct option representation.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

I was wondering what the rules are on binding pattern validity, namely when a pattern does not succesfully match.

The main example I can think of are custom ADTs (ADTs using union-of-struct definitions, rather than enum)

Using that example, the specific question I want to ask whether the following code (with bar() as the entry point) is guaranteed to be well-defined (or if it is UB, or might be UB depending on match lowering):

#[repr(C)]
pub union FakeEnum {
     pub fake_variant: FakeVariant,
     pub other_variant: OtherVariant,
     // other fields
}

pub struct OtherVariant {
    pub discriminant: u32,
    pub blank: u32,
}

pub struct FakeVariant {
     pub discriminant: u32,
     pub value: NonZeroU32,
}


pub unsafe fn foo(x: FakeEnum) {
     unsafe {
           match x {
               FakeEnum{fake_variant: val @ FakeVariant{discriminant: 0, ..}} => println!("{}", val.value),
               _ => {}
     }
}

pub fn bar() {
     foo(FakeEnum{other_variant: OtherVariant{discriminant: 1, blank: 0})
}

Because FakeVariant is matched by binding, one might expect that it eagerly requires validity (which causes UB on reading 0 bytes for value). However, since when discriminant is checked, the pattern match fails, possibly preventing value from being read at all.

For context of this, in the OS I'm working on, there is a concept of an "Option" (not in the optional sense like std::option::Option, but in the sense of "A list of options"), which roughly follows the union-of-struct pattern for two main reasons:

  • The "discriminant" of the option is actually a UUID (#[repr(Uuid)] does not exist in Rust), and
  • There is no fixed list of the set of options that may be passed to the kernel (explicit functionality exists for "If you don't know what this option is, ignore it").

(There is also a header of common fields that carries more data than just the discriminant, notably a flags field).

It would be nice, for ergonomics reasons (and to make correctness easier) if I could write matches that check if the type matches an expected value, and if so, bind the whole union field, which would use a form similar to the above. However, I worry about validity, considering that while all option types of a given group have a fixed upper size (32 or 64 bytes for the data after the header is common), there is no guarantee that any given option type uses the whole payload (and thus might have tail padding, or interior padding after the header).

Lingua principale
Nessun dato sulla lingua
Stelle
760
Fork
68
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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 rust-lang/unsafe-code-guidelines

Tutte le issue di rust-lang/unsafe-code-guidelines

Issue simili

Altre issue su Compilers

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.