JunyuanShen943 opened a new pull request, #22217:
URL: https://github.com/apache/nuttx/pull/22217
## Summary
* Why: `mmap()` of a tmpfs file with a length that extends past EOF fails
with `EINVAL`, because `tmpfs_mmap()` only accepts
`offset + length <= size`. POSIX allows such a mapping; the part past
EOF reads as zeros. This is the normal way to map the last partial
page of a file.
* How:
- `mm/map: add mm_map_exist()`: a query whether any mapping of the
task group overlaps an address range. `mm_map_find()` only matches
a range that lies fully inside one entry, so it cannot answer this.
- `fs/tmpfs`: when the mapping extends past EOF, make the backing
buffer cover it (growing the allocation if needed) and zero the
region past EOF, keeping the logical file size unchanged. Growing
the allocation may move the buffer, so it is refused with `ENOMEM`
while the object is already mapped. The file lock is dropped around
`mm_map_exist()` and before `mm_map_add()` to keep the mm_map -> file
lock order used by `munmap()`.
## Impact
* New feature or changed behavior: YES, tmpfs `mmap()` past EOF now
succeeds instead of failing with `EINVAL`.
* User adaptation needed: NO.
* Build: NO.
* Hardware: NO.
* Documentation: NO.
* Security: NO.
* Compatibility: NO. New `mm_map_exist()` in `include/nuttx/mm/map.h`.
Limitations (unchanged, BUILD_FLAT has no MMU): an access past the mapped
pages cannot raise `SIGBUS`; the overlap check only sees mappings of the
calling task group.
## Testing
I confirm that changes are verified on local setup and works as intended:
* Build Host: Ubuntu 22.04 x86_64, gcc 13.1.0
* Target: sim:nsh with `CONFIG_FS_TMPFS=y`
A small local test program (not part of this PR) on a tmpfs mount:
write 100 bytes, `mmap(4096, PROT_READ | PROT_WRITE, MAP_SHARED)`, check
the bytes past EOF, `fstat()`, write through the mapping and `pread()`,
then try a second, larger mapping while the first one is still mapped.
Testing logs before change (master):
```
nsh> mount -t tmpfs /tmp
nsh> tmpfsmap /tmp/mapfile
FAIL: mmap 4096 bytes MAP_SHARED of a 100 byte file (errno=22)
tmpfsmap: FAILED (1 failures)
```
Testing logs after change:
```
nsh> mount -t tmpfs /tmp
nsh> tmpfsmap /tmp/mapfile
ok: mmap 4096 bytes MAP_SHARED of a 100 byte file (errno=0)
ok: bytes past EOF read as zero (0 non-zero)
ok: file data visible in mapping
ok: file size unchanged after mmap (100)
ok: write through MAP_SHARED is seen by read()
second larger mmap while mapped: rejected (errno=12)
tmpfsmap: PASSED (0 failures)
```
Also: `tools/checkpatch.sh -m` clean, sim:nsh (+ `CONFIG_FS_TMPFS`) make
and CMake builds pass, ostest exits with status 0. Verified on sim only.
## PR verification Self-Check
* [x] This PR introduces only one functional change.
* [x] I have updated all required description fields above.
* [x] My PR adheres to Contributing Guidelines and Documentation (git commit
title and message, coding standard, etc).
* [ ] My PR is still work in progress (not ready for review).
* [x] My PR is ready for review and can be safely merged into a codebase.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]