Possible to interleave `&mut` and `&` safely?

Aperta
#562 8 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
rust

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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:

  1. Some code runs in the thread which writes a &mut reference to the stack that also refers to the stack.
  2. The thread yields control, which hands control over to the scheduler
  3. The scheduler obtains a &Node<Thread> reference to the currently-running thread and reads its id field

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 a NonNull<u32> and dereference it? Specifically, is it problematic that the original NonNull<Node<Thread>> refers to the whole Thread, including the stack?
  • Is there any way to provide a non-raw-pointer API which provides access to the inner T in a Node<T> which would play nicely with this use case?
Lingua principale
Nessun dato sulla lingua
Stelle
760
Fork
68
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/unsafe-code-guidelines

Tutte le issue di rust-lang/unsafe-code-guidelines

Issue simili

Altre issue su Operating Systems

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.