RT-Thread / RT-Thread/rt-thread
[Bug] sys_dup2在启用RT_USING_DFS_V2时会导致dfs_file_set_fpos报错
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
master
Hardware Type/Architectures
all
Develop Toolchain
Other
Describe the bug
在启用RT_USING_DFS_V2情况下,假设设备uart0作为串口终端(shell console),正在调用dfs_file_read()读取数据时候,或者在serial_fops_read()函数被rt_wqueue_wait挂起的时候。
如果此时调用sys_dup2(fd, 0)函数,将会关闭uart0并更新dfs_fdtable,也就是说在dfs_file_read执行过程中file指针被修改了!!!
fdt->fds[0] = fdt->fds[fd];
具体解释见下面的dfs_file_read()函数
/* fpos lock */
off_t pos = dfs_file_get_fpos(file);
ret = rw_verify_area(file, &pos, len);
if (ret > 0)
{
len = ret;
if (dfs_is_mounted(file->vnode->mnt) == 0)
{
#ifdef RT_USING_PAGECACHE
if (file->vnode->aspace && !(file->flags & O_DIRECT))
{
ret = dfs_aspace_read(file, buf, len, &pos);
}
else
#endif
{
///////////////////////////////////////////////////////////////////////////
// 就是跑到这里的时候,sys_dup2被调用了,那么file指针对应的内容被更改了,且被关闭。
///////////////////////////////////////////////////////////////////////////
ret = file->fops->read(file, buf, len, &pos);
}
}
else
{
ret = -EINVAL;
}
}
/* fpos unlock */
///////////////////////////////////////////////////////////////////////////
// 由于file指针对应的内容被更改了,因此dfs_file_set_fpos中rt_take_sem将会奔溃
///////////////////////////////////////////////////////////////////////////
dfs_file_set_fpos(file, pos);
Other additional context
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing sys_dup2 and dfs_file_read in components/dfs/dfs_v2/src/dfs_file.c, then inspect serial_fops_read and dfs_file_set_fpos. Reproduce the RT_USING_DFS_V2 case while uart0 is blocked in a read, and verify that concurrent descriptor duplication no longer causes dfs_file_set_fpos to crash or use an invalid file state.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100