jingfei195887 opened a new pull request, #22476:
URL: https://github.com/apache/nuttx/pull/22476
## Summary
`mmap(..., MAP_PRIVATE | MAP_ANONYMOUS, -2, 0)` returns EBADF even though
anonymous mappings ignore the file descriptor. The outer mmap wrapper looks up
the descriptor before it reaches the anonymous mapping path.
Skip that lookup for MAP_ANONYMOUS and update the mmap reference.
File-backed mappings keep their descriptor and access checks.
## Impact
Anonymous mappings accept an ignored fd value and report mapping argument
errors without first checking fd. No API, configuration, build-system or
hardware changes. Use fd=-1 for portability.
## Testing
I confirm that changes are verified on local setup and work as intended:
- Linux x86_64, GCC 13.1.0; sim:nsh with assertions, FS_ANONMAP and FDCHECK
disabled/enabled.
- The local regression in apps/examples/anonfdcheck exercises private/shared
anonymous mappings with -1, -2, INT_MIN, an out-of-range positive fd, a closed
descriptor, and open read-only/write-only descriptors. It checks zero filling,
writable memory, complete unmapping and balanced mapping counts.
- Invalid flags, zero length and MAP_FIXED report their own errors.
File-backed mappings still reject invalid descriptors with EBADF and
incompatible permissions with EACCES. Open descriptors remain usable afterward.
- INT_MAX is included with FDCHECK disabled and in an additional
fixed-version FDCHECK run. It is excluded from the FDCHECK baseline because
arbitrary invalid tags can abort in fdcheck_restore.
Baseline (`0f1701da1d1`), FDCHECK off/on:
```text
ANONFD RESULT checks=82 failures=14
ANONFD RESULT checks=78 failures=12
```
Fixed version, FDCHECK off/on, then the extra tagged-fd run:
```text
ANONFD RESULT checks=122 failures=0
ANONFD RESULT checks=110 failures=0
ANONFD RESULT checks=122 failures=0
```
The baseline skips data/unmap checks after mmap fails, so it executes fewer
assertions. Each baseline/branch pair uses the same configuration and test
source.
<details>
<summary>Minimal reproducer (FS_ANONMAP enabled)</summary>
```c
#include <sys/mman.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
size_t len = getpagesize();
void *ptr = mmap(NULL, len, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -2, 0);
if (ptr == MAP_FAILED)
{
printf("mmap failed: errno=%d\n", errno);
return 1;
}
return munmap(ptr, len) != 0;
}
```
</details>
No hardware runtime, protected/kernel runtime, SMP stress or
allocator-failure injection was performed.
Protected and kernel builds also pass with `CONFIG_FS_ANONMAP` enabled:
```text
qemu-armv7r:pnsh build PASS
qemu-armv8a:knsh build PASS
```
The final Make/CMake/OSTest/checkpatch gate passes on `ffbcfd27662d`, based
on `0f1701da1d1`. Upstream advanced by four commits during final verification;
those commits modify only ivshmem/PCI driver paths, disjoint from the files in
this change.
Small configuration (`sim:nsh`, optimized, `DEFAULT_SMALL`, `FS_ANONMAP` and
`FS_TMPFS`, no regression application), same effective configuration on
baseline and branch:
Baseline:
```text
text data bss dec hex filename
566293 2800 12896 581989 8e165 nuttx
```
Branch:
```text
text data bss dec hex filename
566293 2800 12896 581989 8e165 nuttx
```
Text, data and bss are unchanged.
## 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 the Contributing Guidelines and Documentation.
- [x] Commit title, sign-off and coding-style checks pass.
- [ ] 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]