fs/mmap: Ensure anonymous pages are initialized to zero

According to the mmap(2) specification, anonymous pages should be initialized to zero unless the MAP_UNINITIALIZED is specified.

Signed-off-by: ouyangxiangzhen <ouyangxiangzhen@xiaomi.com>
This commit is contained in:
ouyangxiangzhen 2024-05-22 15:44:34 +08:00 committed by Xiang Xiao
parent 53c7612faf
commit 0451ead2c5
2 changed files with 14 additions and 1 deletions

View File

@ -108,6 +108,17 @@ static int file_mmap_(FAR struct file *filep, FAR void *start,
if ((flags & MAP_ANONYMOUS) != 0) if ((flags & MAP_ANONYMOUS) != 0)
{ {
ret = map_anonymous(&entry, kernel); ret = map_anonymous(&entry, kernel);
/* According to the mmap(2) specification, anonymous pages should be
* initialized to zero unless the MAP_UNINITIALIZED is specified.
*/
if ((ret == OK) && (flags & MAP_UNINITIALIZED) == 0)
{
DEBUGASSERT(entry.vaddr != NULL);
memset(entry.vaddr, 0, entry.length);
}
goto out; goto out;
} }

View File

@ -56,7 +56,7 @@
#define MAP_ANONYMOUS (1 << 4) /* Bit 4: The mapping is not backed by any file */ #define MAP_ANONYMOUS (1 << 4) /* Bit 4: The mapping is not backed by any file */
#define MAP_ANON MAP_ANONYMOUS /* Alias */ #define MAP_ANON MAP_ANONYMOUS /* Alias */
/* These are Linux-specific (none are implemented). */ /* These are Linux-specific (most are not implemented). */
#define MAP_GROWSDOWN (1 << 5) /* Bit 5: Used to stack allocations */ #define MAP_GROWSDOWN (1 << 5) /* Bit 5: Used to stack allocations */
#define MAP_DENYWRITE (1 << 6) /* Bit 6: Do not permit writes to file */ #define MAP_DENYWRITE (1 << 6) /* Bit 6: Do not permit writes to file */
@ -66,6 +66,8 @@
#define MAP_POPULATE (1 << 10) /* Bit 10: populate (prefault) page tables */ #define MAP_POPULATE (1 << 10) /* Bit 10: populate (prefault) page tables */
#define MAP_NONBLOCK (1 << 11) /* Bit 11: Do not block on IO */ #define MAP_NONBLOCK (1 << 11) /* Bit 11: Do not block on IO */
#define MAP_UNINITIALIZED (1 << 26) /* Bit 26: Do not clear the anonymous pages */
/* Failure return */ /* Failure return */
#define MAP_FAILED ((FAR void*)-1) #define MAP_FAILED ((FAR void*)-1)