Wait, how does placing ZST statics work again?

Đang mở
#546 8 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ính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
compilers

Hướng nghiên cứu

Start by reading the Rust reference PR #1657 and the linked unsafe-code-guidelines issues about ZST statics, then compare the address-range and byte-set interpretations described here. Done means reaching and documenting a decision about which rule should govern ZST static placement, including how it fits the LLVM and embedded-development constraints.

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

Mô tả

As revealed in https://github.com/rust-lang/reference/pull/1657 there is not an immediately shared understanding on ZST statics and where they should go. There are two major ways to interpret "ZST statics do not overlap with other statics". One is "statics are their address ranges" and the other is "statics are their byte sets". We need to pick one.

Other relevant-seeming issues:

Statics As Address Ranges

Expressed by @RalfJung as:

fn does_overlap(r1: Range<usize>, r2: Range<usize>) -> bool {
    !(r1.end <= r2.start || r2.end <= r1.start)
}
Pros
  1. very straightforward definition, which removes some nonintuitive scenarios
  2. still allows placing a static ZST: () at the start or end address of a given static ARRAY: [u8; N]
  3. stronger, which may allow proving things more easily (i.e. optimizations)
Cons
  1. induces more UB if people combine Rust with linker scripts to reposition statics in ways that are incompatible with this rule
  2. requires more R&D to develop a good alternative for people using linker control that doesn't cause UB left and right

Statics As Byte Sets

Elegantly phrased by @CAD97 as: "No byte of memory is contained in more than one static item."

This rule seems to be functionally equivalent to the other phrasing offered, which is "static items cannot alias other static items (using the definition of aliasing from the reference aliasing model)".

Pros
  1. potentially less UB for embedded developers
  2. makes the logic between "where a ZST allocation can go" and "where a ZST &mut () can go" more uniform
Cons
  1. this rule allows some unexpected scenarios
  2. seems like it may be unnecessarily weak and may prove incompatible with LLVM
  3. hard to say if the patterns this enables will actually be sound-in-practice, even if we allow this

Constraints

We have some preexisting concerns. Each is pushing us in the opposite direction:

  1. LLVM doesn't seem to actually handle the notion of a static ZST: () strictly nesting in another static ARRAY: [u8; N] well. This does imply we may be passing up a number of optimizations if we adopt the byte-set rule, suggesting that we should pick the address-based rule.
    • Note that currently, LLVM doesn't seem to handle the notion of static ZST: ()... at all, really[^1]
  2. Embedded developers (including kernel and driver developers) often need the ability to slice-and-dice the address space. Some of this is done using fixed-size statics, but another common way to do this is using statics that are relocated using linker control (flags or linker scripts), which then have their addresses extracted and then composed with a pointer of ambiguous (or, slightly more rarely, known-appropriate) provenance. Often, both. If ZSTs are allowed to be nested in other statics, this has better composition and enables this rule.

[^1]: according to https://github.com/rust-lang/reference/pull/1657#issuecomment-2464528692 and https://llvm.godbolt.org/z/6qTvx3qxd

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.