Skip to content

fix(vfs):调整 lseek 行为 —— 禁止 procfs SEEK_END,目录 SEEK_END 返回 MAX_LFS_FILESIZE#1344

Merged
fslongjin merged 6 commits intoDragonOS-Community:masterfrom
kaleidoscope416:fix_sys_lseek
Nov 6, 2025
Merged

fix(vfs):调整 lseek 行为 —— 禁止 procfs SEEK_END,目录 SEEK_END 返回 MAX_LFS_FILESIZE#1344
fslongjin merged 6 commits intoDragonOS-Community:masterfrom
kaleidoscope416:fix_sys_lseek

Conversation

@kaleidoscope416
Copy link
Contributor

@kaleidoscope416 kaleidoscope416 commented Nov 5, 2025

vfs: 调整 lseek 行为 —— 禁止 procfs SEEK_END,目录 SEEK_END 返回 MAX_LFS_FILESIZE

  • 在 lseek 中加入对 procfs 私有数据的检查:若为 procfs 的伪文件,拒绝 SEEK_END 和无效的 seek 模式,返回 EINVAL。
  • 对目录类型在 SEEK_END 的处理采用常见 Linux 语义:返回 MAX_LFS_FILESIZE (0x7fffffffffffffff)。测试允许返回该值或 EINVAL,选择返回 MAX_LFS_FILESIZE 以通过现有测试。
  • 保持 SEEK_CUR 按当前 offset 计算位置(使用 self.offset.load(Ordering::SeqCst))。
  • 目的:使 DragonOS 在 procfs/目录与 lseek 相关行为上与 Linux 更一致,修复测试套件对这些情形的期望。
  • 按照 POSIX / Linux lseek 语义,对于目标偏移为负的情况应返回 EINVAL。
  • 将lseek加入whitelist并添加屏蔽测试

注意:

  • 该变更为局部修复;更完善的方式是对不同文件类型(procfs、设备、目录等)在 inode/drivers 层实现或分发 llseek,或做更大的 FileDescription 重构以统一文件描述符语义。
  • 建议在合并后运行 lseek 测试用例与 procfs 相关测试以验证行为。

Note

Refines lseek to reject procfs SEEK_END, return MAX_LFS_FILESIZE for directory SEEK_END, treat negative results as EINVAL, and enables gVisor lseek_test with targeted blocklists.

  • VFS (kernel/src/filesystem/vfs/file.rs):
    • Procfs: disallow SEEK_END and Invalid in lseek, return EINVAL.
    • Directories: SEEK_END returns MAX_LFS_FILESIZE.
    • Negative resulting positions now return EINVAL.
  • Tests:
    • Enable lseek_test in user/apps/tests/syscall/gvisor/whitelist.txt.
    • Add blocklisted cases in user/apps/tests/syscall/gvisor/blocklists/lseek_test: LseekTest.ProcStatTwice, LseekTest.EtcPasswdDup, LseekTest.SeekDataAndSeekHole.

Written by Cursor Bugbot for commit f3374a0. This will update automatically on new commits. Configure here.

Copilot AI review requested due to automatic review settings November 5, 2025 13:19
@github-actions github-actions bot added the ambiguous The title of PR/issue doesn't match the format label Nov 5, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for the lseek syscall test from gvisor, with implementation improvements to handle edge cases for procfs files and directories.

  • Added lseek_test to the gvisor test whitelist with a corresponding blocklist for unsupported test cases
  • Enhanced lseek implementation to handle procfs files and directories correctly
  • Fixed error code from EOVERFLOW to EINVAL for negative seek positions
  • Removed extraneous blank lines in syscall handler code

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
user/apps/tests/syscall/gvisor/whitelist.txt Enabled lseek_test in the test suite
user/apps/tests/syscall/gvisor/blocklists/lseek_test Created blocklist for 3 unsupported lseek test cases
kernel/src/filesystem/vfs/syscall/sys_lseek.rs Removed unnecessary blank lines for code cleanup
kernel/src/filesystem/vfs/file.rs Added procfs and directory-specific lseek handling, fixed error code for negative positions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +369 to +372
crate::driver::base::block::SeekFrom::SeekEnd(_) => {
return Err(SystemError::EINVAL);
}
crate::driver::base::block::SeekFrom::Invalid => {
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the imported SeekFrom type directly instead of the fully qualified path crate::driver::base::block::SeekFrom. The type is already imported at line 11, so you can simplify to SeekFrom::SeekEnd(_) and SeekFrom::Invalid.

Suggested change
crate::driver::base::block::SeekFrom::SeekEnd(_) => {
return Err(SystemError::EINVAL);
}
crate::driver::base::block::SeekFrom::Invalid => {
SeekFrom::SeekEnd(_) => {
return Err(SystemError::EINVAL);
}
SeekFrom::Invalid => {

Copilot uses AI. Check for mistakes.
if FileType::Dir == file_type {
// 对目录,返回 Linux 常见语义:允许 SEEK_END 并返回 MAX_LFS_FILESIZE。
// 测试接受 MAX_LFS_FILESIZE 或 EINVAL,但为通过当前测试选择返回 MAX_LFS_FILESIZE。
const MAX_LFS_FILESIZE: i64 = 0x7fffffffffffffff;
Copy link

Copilot AI Nov 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic number 0x7fffffffffffffff should use a more explicit representation. Consider using i64::MAX instead of the hex literal for better readability and maintainability.

Suggested change
const MAX_LFS_FILESIZE: i64 = 0x7fffffffffffffff;
const MAX_LFS_FILESIZE: i64 = i64::MAX;

Copilot uses AI. Check for mistakes.
Comment on lines +368 to +375
match origin {
SeekFrom::SeekEnd(_) => {
return Err(SystemError::EINVAL);
}
SeekFrom::Invalid => {
return Err(SystemError::EINVAL);
}
_ => {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SeekFrom::SeekEnd(_) | SeekFrom::Invalid

可以这样合并测试条件。不然太啰嗦了

if FileType::Dir == file_type {
// 对目录,返回 Linux 常见语义:允许 SEEK_END 并返回 MAX_LFS_FILESIZE。
// 测试接受 MAX_LFS_FILESIZE 或 EINVAL,但为通过当前测试选择返回 MAX_LFS_FILESIZE。
const MAX_LFS_FILESIZE: i64 = i64::MAX;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在全局声明。

@fslongjin
Copy link
Member

还有,需要修改pr标题

@kaleidoscope416 kaleidoscope416 changed the title Fix sys lseek fix(vfs):调整 lseek 行为 —— 禁止 procfs SEEK_END,目录 SEEK_END 返回 MAX_LFS_FILESIZE Nov 6, 2025
@github-actions github-actions bot added Bug fix A bug is fixed in this pull request and removed ambiguous The title of PR/issue doesn't match the format labels Nov 6, 2025
@github-actions github-actions bot added the test Unitest/User space test label Nov 6, 2025
@fslongjin fslongjin merged commit cb6df6d into DragonOS-Community:master Nov 6, 2025
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug fix A bug is fixed in this pull request test Unitest/User space test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants