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 8700e41134e mm/anonmap: page-align anonymous mmap
8700e41134e is described below

commit 8700e41134e4e6cfdbab7c1b4c9599ac2bb4bdc1
Author: Xiang Xiao <[email protected]>
AuthorDate: Fri Nov 13 17:47:20 2026 +0800

    mm/anonmap: page-align anonymous mmap
    
    Anonymous mappings come from the heap, which does not guarantee page
    alignment, while user space is entitled to rely on mmap() returning
    page-aligned memory (the libfuse node slab allocator masks the node
    pointer to a page boundary to find its slab header).  Align the
    allocation to getpagesize().
    
    Co-authored-by: GitHub Copilot <[email protected]>
    Signed-off-by: Xiang Xiao <[email protected]>
    Signed-off-by: v-liuchenxu3 <[email protected]>
---
 fs/mmap/fs_anonmap.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/fs/mmap/fs_anonmap.c b/fs/mmap/fs_anonmap.c
index a78f4f42b9b..0c983d1880d 100644
--- a/fs/mmap/fs_anonmap.c
+++ b/fs/mmap/fs_anonmap.c
@@ -33,6 +33,8 @@
 #include <nuttx/sched.h>
 #include <assert.h>
 #include <nuttx/debug.h>
+#include <string.h>
+#include <unistd.h>
 
 #include "sched/sched.h"
 #include "fs_heap.h"
@@ -238,14 +240,22 @@ int map_anonymous(FAR struct mm_map_entry_s *entry, bool 
kernel)
     }
 #endif
 
+  /* The mapping is page aligned (matching the getpagesize() value) since
+   * some callers rely on the POSIX guarantee that mmap() returns
+   * page-aligned memory.
+   */
+
   entry->vaddr = kernel ?
-    fs_heap_zalloc(entry->length) : kumm_zalloc(entry->length);
+    fs_heap_memalign(getpagesize(), entry->length) :
+    kumm_memalign(getpagesize(), entry->length);
   if (entry->vaddr == NULL)
     {
-      ferr("ERROR: kumm_alloc() failed, enable DEBUG_MM for info!\n");
+      ferr("ERROR: memalign failed, enable DEBUG_MM for info!\n");
       return -ENOMEM;
     }
 
+  memset(entry->vaddr, 0, entry->length);
+
   entry->munmap = unmap_anonymous;
   entry->priv.i = kernel;
 

Reply via email to