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

未关闭
#807 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
活跃
技术栈
rust

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Rust
星标
282
派生
12
平均合并
2 天 9 小时
30 天内合并 PR
28

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tier4/awkernel 的其他 Issue

查看 tier4/awkernel 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。