Validity of Non-matched Patterns

Đang mở
#587 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tài liệu
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
rust
Lĩnh vực
compilers

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Không có dữ liệu ngôn ngữ
Star
760
Fork
68
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của rust-lang/unsafe-code-guidelines

Tất cả issue của rust-lang/unsafe-code-guidelines

Issue tương tự

Thêm issue về Compilers

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.