RT-Thread / RT-Thread/rt-thread

[Bug] rt-smart用户态任务切换时可能会导致user_ctx.ctx被错误清除

Open
#11,105 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
12.2k
Forks
5.4k
Avg merge
4d 12h
Merged PRs (30d)
40

Description

RT-Thread Version

5.3.0

Hardware Type/Architectures

qemu aarch64

Develop Toolchain

GCC

Describe the bug
  1. 准备一个while(1)的用户态程序 test.elf。
  2. 进入ash。先后台运行此程序, ./test.elf &
  3. 再启动另一个程序,例如 ls 。 就会触发异常。

异常发生在arch_set_thread_context 函数获取判断 thread(ash)->user_ctx.ctx 是否为空时。

错误发生的流程如下:

  1. 在启动新程序 "ls"时,通过系统调用fork进入内核准备启动新进程,current进程是“ash”

  2. 在内核启动新进程的过程中上下文被切到后台进程“test.elf”中执行,current进程是“test.elf”

  3. 在执行“test.elf”时进入os counter中断,此时“test.elf”时间片使用完,内核将current进程切回“ash”,此时在中断返回时会执行RESTORE_USER_CTX,由于此中断是从“test.elf”这个用户模式进来的,所以会执行user_ctx的restore,在lwp_uthread_ctx_restore函数中将“ash”的user_ctx.ctx清空。然后切回之前的“fork”

  4. 系统回到之前的fork中继续创建新进程,在执行到arch_set_thread_context时发现user_ctx.ctx的值为空。

Other additional context
Image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the sequence on qemu aarch64 with the background test.elf, ash, and a subsequent ls, then trace arch_set_thread_context through RESTORE_USER_CTX and lwp_uthread_ctx_restore during the described fork and timer-interrupt switch. Done means launching ls no longer triggers the exception and ash's user_ctx.ctx is not incorrectly cleared.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.