Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions os/StarryOS/kernel/src/syscall/mm/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,16 @@ pub fn sys_mprotect(addr: usize, length: usize, prot: u32) -> AxResult<isize> {
let mut aspace = aspace_arc.lock();
let length = align_up_4k(length);
let start_addr = VirtAddr::from(addr);
// man 2 mprotect: addresses without a mapping → ENOMEM.
if aspace.find_area(start_addr).is_none() {
// man 2 mprotect: if any page in [addr, addr+len) lacks a mapping → ENOMEM.
// Linux validates the whole range, not just the first page: an unmapped hole
// in the middle of `[mapped][hole][mapped]` must fail instead of silently
// protecting only the mapped fragments. `can_access_range` with an empty
// access mask is a pure contiguous-coverage check (every area's flags
// trivially contain the empty set), so it returns false on the first gap.
// We pre-check and leave the mapping untouched on failure, i.e. report
// ENOMEM atomically without any half-applied protection — the errno real
// programs test for.
if !aspace.can_access_range(start_addr, length, MappingFlags::empty()) {
Comment thread
ZR233 marked this conversation as resolved.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can_access_range + MappingFlags::empty() 是正确的选择:纯连续覆盖检查,contains(empty()) 恒为 true,等价于仅检查区间是否有空洞。逻辑简洁且与 POSIX/Linux 语义一致。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can_access_range(start_addr, length, MappingFlags::empty()) 逻辑正确:纯连续覆盖检查,contains(empty()) 恒为 true,遇到空洞即返回 false。检查在 protect() 前完成,失败路径映射不变。

return Err(AxError::NoMemory);
}
if permission_flags.contains(MmapProt::WRITE) {
Expand Down
Loading