Does `MaybeDangling<&[mut] T>` make any requirements about the pointer's provenance?
还没有人认领这个 Issue。
评估
调研方向
Start with the concrete Rust example, the quoted RFC language, and the current aliasing-model candidates; compare the stated Miri behavior with the two proposed resolutions. Done means reaching consensus on whether MaybeDangling<&[mut] T> preserves provenance and permissions, then recording that decision so future model changes are noticed.
由索引模型根据 Issue 内容生成。
描述
Concretely, is this code well-defined?
fn main() {
// Under the current models, we do not forbid writing through
// `MaybeDangling<&i32>`. That's not yet finally decided, but meanwhile
// ensure we document this and notice when it changes.
unsafe {
let mutref = &mut 0;
write_through_shr(mem::transmute(mutref));
}
fn write_through_shr(x: MaybeDangling<&i32>) {
unsafe {
let y: *mut i32 = mem::transmute(x);
y.write(1);
}
}
}
This "launders" a mutable reference through a MaybeDangling<&i32>. Miri says this is okay since MaybeDangling suppresses retags, meaning the provenance returned by the transmute in write_through_shr is exactly the same as the one that was passed into the transmute in main -- it is a Tree Borrows tag that has write permission.
To me, this is a natural consequence of the RFC:
MaybeDangling is a way to "truncate" T to its by-value invariant, which changes nothing for most types, but means that references and boxes are allowed as long as their bit patterns are fine
The above code not being UB means that we can not add LLVM attributes like captures(readonly) to MaybeDangling<&i32> arguments.
@wafflelapkin was quite surprised by this, so let's make sure we have consensus on how to resolve this question.
If we wanted to make this code UB, I can imagine two ways of achieving that:
- We actually still do retags inside
MaybeDangling, but we make those retags behave differently so that they don't fail for dangling pointers. IMO this is a bad idea; I think Rust should have a type that entirely suppresses the implicit move constructor that comes with references and boxes (i.e., the retags), and I thinkMaybeDanglingis the most natural candidate for that type. - When checking whether a shared reference is valid, we require its tag to not have write permission (with some suitable exception for
UnsafeCell). This would make transmutes from mutable references to shared references UB, unless we somehow make it so that we only check this validity requirement after all retags (if any) have happened. But even then this would lead to the odd situation where transmuting&mut Tto&Tis fine but transmuting it toMaybeDangling<&T>is UB, which seems broken for a type that is intended to let unsafe code do more stuff and avoid UB.
I think the 2nd point is how many people already think shared references work, e.g. I think it is the reason for @nikic's surprise here: the assumption is that the type itself requires the reference to be read-only. But currently that's not what the aliasing model candidates say; they say that shared references become read-only when they get retagged, but without a retag we can't say anything about their permissions.
@rust-lang/opsem what do you think?
- 主要语言
- 没有语言数据
- 星标
- 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
-
难度 2/5 1-3 小时 新手友好度 68/100
leanprover/lean4#16876 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
namespace team:operations
难度 1/5 1 小时以内 新手友好度 88/100
EclipseFdn/open-vsx.org#16434 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 90/100
eclipse-tractusx/tractusx-starter-kit#31 ·
维护者通常 1 天内回复
-
redact: SendGrid pattern masks Python identifiers like SG.rebound_binding_fields (agents misread code as corrupted)可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭area/auth comp/agent P3 type/bug
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#161417 ·
维护者通常 1 天内回复