What assumptions can safe-interface functions make about `&Cell`s of different types aliasing?
还没有人认领这个 Issue。
评估
调研方向
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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
rust-lang/unsafe-code-guidelines 的其他 Issue
-
I-opsem-nominated
难度 4/5 3-5 天 新手友好度 35/100
rust-lang/unsafe-code-guidelines#634 · 5 条评论 ·
-
难度 5/5 一周以上 新手友好度 15/100
-
难度 5/5 一周以上 新手友好度 12/100
rust-lang/unsafe-code-guidelines#632 · 8 条评论 · 1 个 reaction ·
-
难度 5/5 一周以上 新手友好度 10/100
rust-lang/unsafe-code-guidelines#631 · 7 条评论 ·
-
I-opsem-nominated
难度 5/5 一周以上 新手友好度 20/100
rust-lang/unsafe-code-guidelines#630 · 1 个 reaction ·
查看 rust-lang/unsafe-code-guidelines 的全部 Issue
相似的 Issue
-
bug core-lang
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
Call-argument syntax diagnostic needs example spacing and removal of confusing trailing sentence未关闭
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
mw00/project-maya#1368 ·
维护者通常 2 天内回复
-
test(bind-json): legacy Solidity fixture requires an unavailable solc on Linux ARM64可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
foundry-rs/foundry#18828 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复