This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 31e01b0e50b0c23703463bdc2401ce78fe7b86d1
Author: shenjunyuan <[email protected]>
AuthorDate: Tue Nov 24 11:51:06 2026 +0800

    fs/tmpfs: allow mmap() past the end of the file
    
    tmpfs_mmap() rejected any mapping that extends past the end of the
    file with -EINVAL.  file_mmap_() only falls back to rammap() on
    -ENOTTY, so MAP_SHARED and read-only MAP_PRIVATE mappings that reach
    past EOF failed.
    
    Grow tfo_data to cover the mapping, keeping the file size unchanged,
    and share the buffer as before.  The buffer can only move while the
    file is not mapped; otherwise the mapping fails with -ENOMEM.  The
    part past EOF is zeroed when the first mapping is made, so a later
    mapping sees what an earlier one wrote there, and the extra memory is
    given back when the last mapping goes away.
    
    Signed-off-by: shenjunyuan <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 fs/tmpfs/fs_tmpfs.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 67 insertions(+), 3 deletions(-)

diff --git a/fs/tmpfs/fs_tmpfs.c b/fs/tmpfs/fs_tmpfs.c
index 4d9d26cabd9..49f1be74947 100644
--- a/fs/tmpfs/fs_tmpfs.c
+++ b/fs/tmpfs/fs_tmpfs.c
@@ -2177,6 +2177,27 @@ static off_t tmpfs_seek(FAR struct file *filep, off_t 
offset, int whence)
   return position;
 }
 
+/****************************************************************************
+ * Name: tmpfs_release_mapping
+ *
+ * Description:
+ *   Drop one mapping and its file reference.  Called with the file locked;
+ *   the lock is released on return.
+ *
+ ****************************************************************************/
+
+static void tmpfs_release_mapping(FAR struct tmpfs_file_s *tfo)
+{
+  /* Give back memory grown for a mapping past EOF */
+
+  if (--tfo->tfo_maps == 0)
+    {
+      tmpfs_realloc_file(tfo, tfo->tfo_size);
+    }
+
+  tmpfs_release_lockedfile(tfo);
+}
+
 static int tmpfs_unmap(FAR struct task_group_s *group,
                        FAR struct mm_map_entry_s *entry,
                        FAR void *start, size_t length)
@@ -2220,8 +2241,7 @@ static int tmpfs_unmap(FAR struct task_group_s *group,
         }
 
       DEBUGASSERT(tfo->tfo_maps > 0);
-      tfo->tfo_maps--;
-      tmpfs_release_lockedfile(tfo);
+      tmpfs_release_mapping(tfo);
     }
 
   /* No.. We have been asked to "unmap' only a portion of the memory
@@ -2268,6 +2288,8 @@ static int tmpfs_msync(FAR struct mm_map_entry_s *entry, 
FAR void *start,
 static int tmpfs_mmap(FAR struct file *filep, FAR struct mm_map_entry_s *map)
 {
   FAR struct tmpfs_file_s *tfo;
+  FAR uint8_t *newdata;
+  size_t mapend;
   int ret;
 
   DEBUGASSERT(filep->f_priv != NULL);
@@ -2295,7 +2317,7 @@ static int tmpfs_mmap(FAR struct file *filep, FAR struct 
mm_map_entry_s *map)
     }
 
   if (map->offset < 0 || map->offset >= tfo->tfo_size ||
-      map->length == 0 || map->offset + map->length > tfo->tfo_size)
+      map->length == 0 || map->length > SIZE_MAX - (size_t)map->offset)
     {
       ret = -EINVAL;
       goto errout_with_lock;
@@ -2309,6 +2331,42 @@ static int tmpfs_mmap(FAR struct file *filep, FAR struct 
mm_map_entry_s *map)
       goto errout_with_lock;
     }
 
+  /* A mapping may extend past the end of the file.  Back the whole range
+   * with tfo_data; the file size is unchanged.  The buffer can only grow
+   * while nothing else maps it.
+   */
+
+  mapend = map->offset + map->length;
+  if (mapend > tfo->tfo_alloc)
+    {
+      if (tfo->tfo_maps > 0)
+        {
+          ret = -ENOMEM;
+          goto errout_with_lock;
+        }
+
+      newdata = fs_heap_realloc(tfo->tfo_data, mapend);
+      if (newdata == NULL)
+        {
+          ret = -ENOMEM;
+          goto errout_with_lock;
+        }
+
+      tfo->tfo_data  = newdata;
+      tfo->tfo_alloc = mapend;
+    }
+
+  /* Zero past EOF only for the first mapping, so a later mapping sees what
+   * an earlier one wrote there.  tmpfs_realloc_file() keeps the tail zeroed
+   * while the file stays mapped.
+   */
+
+  if (tfo->tfo_maps == 0)
+    {
+      memset(tfo->tfo_data + tfo->tfo_size, 0,
+             tfo->tfo_alloc - tfo->tfo_size);
+    }
+
   map->vaddr = tfo->tfo_data + map->offset;
   map->priv.p = tfo;
   map->munmap = tmpfs_unmap;
@@ -2326,6 +2384,12 @@ static int tmpfs_mmap(FAR struct file *filep, FAR struct 
mm_map_entry_s *map)
 
       tmpfs_update_atime((FAR struct tmpfs_object_s *)tfo);
     }
+  else if (tfo->tfo_maps == 0)
+    {
+      /* Give back what was grown for this mapping */
+
+      tmpfs_realloc_file(tfo, tfo->tfo_size);
+    }
 
 errout_with_lock:
   tmpfs_unlock_file(tfo);

Reply via email to