Data race on `LOGGER.buf_console` between `set_buf_console` and the logger

Aperta
#807 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Start with awkernel_lib/src/logger.rs and trace set_buf_console alongside Logger::enabled, Logger::log, and Logger::flush; then check the buffered logger setup in applications/awkernel_services/src/buffered_logger.rs. Verify how console registration and logging can overlap across CPUs, including the set_raw_console path. Done when the shared logger state is accessed without a data race and the boot-time raw console behavior is understood.

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

Descrizione

Summary

awkernel_lib::logger::set_buf_console writes LOGGER.buf_console without synchronization while other CPUs read it through log::info! and the other logging macros. This is a data race, which is undefined behavior in Rust.

This was found during the review of #799.

Details

LOGGER is a static mut in awkernel_lib/src/logger.rs:

static mut LOGGER: Logger = Logger {
    raw_console: None,
    buf_console: None,
};
  • Writer: set_buf_console runs (*ptr).buf_console = Some(logger) with no lock and no atomic operation. The buffered logger task (applications/awkernel_services/src/buffered_logger.rs) calls it, so it runs after all CPUs have started to execute tasks.
  • Readers: Logger::enabled, Logger::log, and Logger::flush read self.buf_console on every CPU each time a logging macro is called, also with no synchronization.

Possible effect

Option<&'static dyn Log> is two words: a data pointer and a vtable pointer. The write can be two stores. A CPU that reads between them can get the new data pointer with the old vtable pointer, and log.log(record) then calls through an invalid vtable.

The window is a few instructions and the write happens once at boot, so this is unlikely to occur in practice. I have not reproduced it.

Proposed fix

buf_console is written once and never changed afterward, so a separate AtomicBool that publishes it is enough:

  1. set_buf_console writes the pointer, and then sets the flag with Ordering::Release.
  2. Logger loads the flag with Ordering::Acquire, and reads buf_console only when the flag is set.

With this, no CPU reads the pointer while it is written.

Related

set_raw_console writes raw_console in the same way. It is called from console::register_console, which appears to run during early boot before the other CPUs start. Whether it can run concurrently with logging on another CPU should be checked as part of this issue.

Lingua principale
Rust
Stelle
282
Fork
12
Merge medio
2g 9h
PR unite (30g)
28

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 tier4/awkernel

Tutte le issue di tier4/awkernel

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.