This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new 79165a14f6b fs/mmap: ignore the file descriptor for anonymous mappings
79165a14f6b is described below
commit 79165a14f6bc2db8c9e87e1b76f60bf210099901
Author: jingfei <[email protected]>
AuthorDate: Fri Nov 27 12:12:11 2026 +0800
fs/mmap: ignore the file descriptor for anonymous mappings
MAP_ANONYMOUS does not use a backing file, but mmap still looks up
any descriptor other than -1 before reaching the anonymous mapping
path. Invalid descriptors therefore fail with EBADF instead of
allocating memory or reporting an invalid mapping argument.
Skip file_get for anonymous mappings and document that fd is ignored.
File-backed mappings retain their descriptor and access checks.
Signed-off-by: jingfei <[email protected]>
---
Documentation/reference/user/10_filesystem.rst | 15 +++++++++------
fs/mmap/fs_mmap.c | 3 ++-
2 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/Documentation/reference/user/10_filesystem.rst
b/Documentation/reference/user/10_filesystem.rst
index 2b3916ec377..2aadc755aa3 100644
--- a/Documentation/reference/user/10_filesystem.rst
+++ b/Documentation/reference/user/10_filesystem.rst
@@ -599,12 +599,13 @@ are two conditions where ``mmap()`` can be supported:
:param flags: See the ``MAP_*`` definitions in ``sys/mman.h``.
- - ``MAP_SHARED`` - Required
- - ``MAP_PRIVATE`` - Will cause an error
+ - ``MAP_SHARED`` - Either this or ``MAP_PRIVATE`` is required
+ - ``MAP_PRIVATE`` - Either this or ``MAP_SHARED`` is required
- ``MAP_FIXED`` - Will cause an error
- ``MAP_FILE`` - Ignored
- - ``MAP_ANONYMOUS`` - Will cause an error
- - ``MAP_ANON`` - Will cause an error
+ - ``MAP_ANONYMOUS`` - Allocate memory without a backing file;
+ requires ``CONFIG_FS_ANONMAP``
+ - ``MAP_ANON`` - Alias for ``MAP_ANONYMOUS``
- ``MAP_GROWSDOWN`` - Ignored
- ``MAP_DENYWRITE`` - Will cause an error
- ``MAP_EXECUTABLE`` - Ignored
@@ -613,7 +614,8 @@ are two conditions where ``mmap()`` can be supported:
- ``MAP_POPULATE`` - Ignored
- ``AP_NONBLOCK`` - Ignored
- :param fd: file descriptor of the backing file -- required.
+ :param fd: File descriptor of the backing file. Ignored when
+ ``MAP_ANONYMOUS`` is set; use -1 for portability.
:param offset: The offset into the file to map.
:return:
@@ -624,7 +626,8 @@ are two conditions where ``mmap()`` can be supported:
- ``ENOSYS`` - Returned if any of the unsupported ``mmap()`` features
are attempted.
- - ``EBADF`` - ``fd`` is not a valid file descriptor.
+ - ``EBADF`` - ``fd`` is not a valid file descriptor and
+ ``MAP_ANONYMOUS`` is not set.
- ``EINVAL`` - Length is 0. flags contained neither ``MAP_PRIVATE`` or
``MAP_SHARED``, or contained both of these values.
- ``ENODEV`` - The underlying file-system of the specified file does
diff --git a/fs/mmap/fs_mmap.c b/fs/mmap/fs_mmap.c
index 384d9a19f4a..14db0a17620 100644
--- a/fs/mmap/fs_mmap.c
+++ b/fs/mmap/fs_mmap.c
@@ -293,7 +293,8 @@ FAR void *mmap(FAR void *start, size_t length, int prot,
int flags,
FAR void *mapped = NULL;
int ret;
- if (fd != -1 && (ret = file_get(fd, &filep)) < 0)
+ if ((flags & MAP_ANONYMOUS) == 0 && fd != -1 &&
+ (ret = file_get(fd, &filep)) < 0)
{
ferr("ERROR: fd:%d referred file is not valid\n", fd);
goto errout;