Possible to interleave `&mut` and `&` safely?
还没有人认领这个 Issue。
评估
- 难度
- 5/5
- 预计耗时
- 一周以上
- 新手友好度
- 25/100
- Issue 类型
- 功能
- 描述清晰度
- 需要澄清
- 活跃度
- 停滞
- 技术栈
- rust
调研方向
Start with the Node and Thread examples in the issue, then examine the proposed UnsafeCell and raw-pointer approaches in light of Rust's aliasing rules. Done means resolving both pointer-arithmetic and non-raw-pointer API questions, with the resulting guidance recorded in the issue.
由索引模型根据 Issue 内容生成。
描述
I'm working on a linked list implementation for an embedded kernel. We have the following Thread structure, which will be a member of a list:
struct Node<T> {
next: Option<NonNull<Node<T>>>,
prev: Option<NonNull<Node<T>>>,
t: T,
}
struct Thread {
stack: [u8; STACK_SIZE],
id: u32,
...
}
The stack is the thread's stack, which will be used to hold arbitrary Rust values, which might include &mut references to other values in the stack (i.e., it might be self-referential). (While pinning is obviously a concern, that's not what this issue is about.)
In addition to self-references in the stack, we also need to be able to inspect other fields in Thread from inside the scheduler.
Here's an example - possibly problematic - sequence of events:
- Some code runs in the thread which writes a
&mutreference to the stack that also refers to the stack. - The thread yields control, which hands control over to the scheduler
- The scheduler obtains a
&Node<Thread>reference to the currently-running thread and reads itsidfield
While, in (3), we haven't read data from stack, I presume that it's still considered UB to have a &Node<Thread> while other &mut references exist which point into the stack, which overlaps with the referent of the &Node<Thread>?
My first thought was to just wrap the whole thing in UnsafeCell so that the Node API doesn't provide &Threads, but instead provides &UnsafeCell<Thread>s. But IIUC that doesn't solve the problem - it's still illegal to have &UnsafeCell<T> and &mut T overlap.
My next thought was to just use raw pointers and never synthesize & references.
Concretely, my questions are the following. All of them are in the context of &mut references existing which refer to stack.
- Given a
NonNull<Node<Thread>>, is it sound to perform pointer arithmetic to obtain aNonNull<u32>and dereference it? Specifically, is it problematic that the originalNonNull<Node<Thread>>refers to the wholeThread, including thestack? - Is there any way to provide a non-raw-pointer API which provides access to the inner
Tin aNode<T>which would play nicely with this use case?
- 主要语言
- 没有语言数据
- 星标
- 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
-
taskmanager: a failed v1alpha1 backup leaves the partial backup behind可能已有人在做 @arshil42 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
[Cache] Downloads without symlinks fail on Windows for snapshot paths of 223-259 chars since #5354未关闭
难度 2/5 1-3 小时 新手友好度 78/100
huggingface/huggingface_hub#5481 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
danielmiessler/LifeOS#3023 ·
维护者通常 5 天内回复
-
难度 2/5 1-3 小时 新手友好度 86/100
维护者通常 1 天内回复