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
commit 4ad04ca8f291e37d83a8283c3d7ad09119079286 Author: dengwenqi <[email protected]> AuthorDate: Thu Nov 12 16:13:26 2026 +0800 fs/shm: round the shm buffer up to a page for mmap() Without an MMU, shmfs_mmap() returns the object buffer directly and rejects a mapping longer than the object, since it would run into the neighbouring heap memory. That also rejects the usual POSIX sequence of ftruncate(fd, size) followed by mmap() of a whole page, sysconf(_SC_PAGESIZE), when size is not a page multiple: mmap() fails with EINVAL. Allocate the buffer of a FLAT or PROTECTED build in whole pages, and allow a mapping up to the page-rounded size. Signed-off-by: dengwenqi <[email protected]> Signed-off-by: jingfei <[email protected]> --- fs/shm/shmfs.c | 7 +++++-- fs/shm/shmfs_alloc.c | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/fs/shm/shmfs.c b/fs/shm/shmfs.c index bdc32a99a20..b736ee7f776 100644 --- a/fs/shm/shmfs.c +++ b/fs/shm/shmfs.c @@ -29,8 +29,10 @@ #include <errno.h> #include <stdbool.h> #include <string.h> +#include <unistd.h> #include <sys/param.h> +#include <nuttx/nuttx.h> #include <nuttx/fs/ioctl.h> #include <nuttx/mm/map.h> #include <nuttx/arch.h> @@ -418,10 +420,11 @@ static int shmfs_mmap(FAR struct file *filep, { #ifndef CONFIG_BUILD_KERNEL /* Without an MMU mapping the object address is returned directly, so - * a mapping longer than the object would expose adjacent heap memory. + * a mapping longer than the page-rounded backing would expose + * adjacent heap memory. */ - if (entry->length > object->length) + if (entry->length > ALIGN_UP(object->length, sysconf(_SC_PAGESIZE))) { inode_release(filep->f_inode); return -EINVAL; diff --git a/fs/shm/shmfs_alloc.c b/fs/shm/shmfs_alloc.c index 0133f85a168..7417262ab21 100644 --- a/fs/shm/shmfs_alloc.c +++ b/fs/shm/shmfs_alloc.c @@ -27,6 +27,7 @@ #include <nuttx/config.h> #include <stdbool.h> +#include <unistd.h> #include <nuttx/arch.h> #include <nuttx/cache.h> @@ -52,7 +53,12 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length) */ size_t hdr_size = sizeof(struct shmfs_object_s); - size_t alloc_size = length; + + /* mmap() hands out this buffer directly: round it up to a page so that + * a page-sized mapping of a smaller object stays inside it. + */ + + size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE)); #ifdef CONFIG_FS_SHMFS_NO_ALIGN size_t cachesize = 0; #else @@ -82,7 +88,7 @@ FAR struct shmfs_object_s *shmfs_alloc_object(size_t length) * memory in user heap */ - size_t alloc_size = length; + size_t alloc_size = ALIGN_UP(length, sysconf(_SC_PAGESIZE)); object = fs_heap_zalloc(sizeof(struct shmfs_object_s)); if (object)
