Wait, how does placing ZST statics work again?

未关闭
#546 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
活跃
技术栈
rust
领域
compilers

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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

主要语言
没有语言数据
星标
760
派生
68
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/unsafe-code-guidelines 的其他 Issue

查看 rust-lang/unsafe-code-guidelines 的全部 Issue

相似的 Issue

更多 Compilers Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。