What assumptions can safe-interface functions make about `&Cell`s of different types aliasing?

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

还没有人认领这个 Issue。

评估

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

调研方向

Start with the f1 and f2 examples in the issue, then read the discussion in issue #145329 about Cell<Rc> aliasing. Determine which aliasing assumptions safe-interface functions may rely on and whether they support a sound Cell::get_cloned(); done means a clearly stated rule that distinguishes the allowed and unsound cases.

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

描述

Background: Suppose we have two functions, each of which potentially uses unsafe code internally but presents a safe interface. f returns a value of some type T, and g takes a T as its argument. We want to prove that g(f()) is sound (i.e. no undefined behaviour), which means that anything that g assumes is true about its argument for its safety proof must be ensured by f, and that in turn depends on what g's safety assumptions are. As such, some explicit choice needs to be made for "the safety assumptions that a function with a safe interface is allowed to assume" – making these less stringent makes g harder to write, making them more stringent makes f harder to write.

My question is about what explicit choice should be made for these "safe-interface safety assumptions" with respect to aliasing &Cells. It's clear that some combinations must be disallowed, because they could lead to UB in safe code. For example, this (safe interface and internally safe) function has undefined behaviour if its arguments alias:

fn f1(x: &Cell<&'static usize>, y: &Cell<*const usize>) -> usize {
    y.set(core::ptr::null());
    *x.get()
}

A similar problem can happen with a single argument, if it's a cell that aliases its own contents (this is UB if the &'static Cell<usize> inside x is an alias for x itself, which can happen if the memory location *x holds a pointer to itself):

fn f2(x: &Cell<&'static Cell<usize>>) -> usize {
    x.get().set(0);
    x.get().get()
}

On the other hand, there are some similar cases which can't (in current stable Rust), as far as I know, lead to undefined behaviour in code that uses no unsafe internally. For example, a &Cell<Singleton<&'static Cell<usize>>> (where Singleton<T> is a non-Copy transparent public-field wrapper around T but can only be constructed once per program execution) probably can't be directly exploited in safe Rust because the only way to observe the value inside the Singleton would be to move the Singleton out of the cell, but it can't be moved out because there's no way to construct a replacement Singleton to swap/replace it with.

It would be possible to say that the explicit choice for the safe-interface safety assumption about aliasing Cells would be "anything that can't be used to cause undefined behaviour without unsafe is allowed". But this would reject some useful code; in particular I'm thinking of https://github.com/rust-lang/rust/issues/145329 which wants to be able to make a safety assumption that an &Cell<Rc<T>> doesn't alias its own reference count (which is a Cell<usize> accessible via a pointer contained in the Rc). But in stable Rust, without using unsafe internally, such a Cell<Rc<T>> doesn't seem to be able to lead to undefined behaviour in code that does not have a T nor Rc<T> and is unable to construct one (because without access to an Rc<T>, you don't have anything to move into the Cell, and thus can't use any of the methods of &Cell except swap and as_ptr which don't help). So it would be nice if the safe-interface safety assumption were a little stronger, making it possible to write a sound Cell::get_cloned().

主要语言
没有语言数据
星标
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 摘要。