Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory does not get allocated with the MAP_ANONYMOUS and MAP_SHARED_VALIDATE flag in mmap()

Tags:

c

linux

unix

mmap

I am trying to allocate memory blocks with MAP_ANONYMOUS flag, but it is not creating any memory blocks along with MAP_SHARED_VALIDATE flag, but MAP_ANONYMOUS with MAP_PRIVATE or MAP_SHARED flags creates the blocks of memory. Could someone explain why this happens.

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include <unistd.h>

int main(void)
{
  size_t size = getpagesize();

  errno = 0;
  void *first = mmap(0, size, PROT_READ|PROT_WRITE,
                     MAP_ANONYMOUS|MAP_SHARED_VALIDATE, -1, 0);
  printf("first: %p %s\n", first, strerror(errno));

  errno = 0;
  void *second = mmap(0, size, PROT_READ|PROT_WRITE,
                      MAP_ANONYMOUS|MAP_SHARED, -1, 0);
  printf("second: %p %s\n", second, strerror(errno));

  return 0;
}

has been observed to print, on both Linux 4.19 and Linux 5.8,

first: 0xffffffffffffffff Invalid argument
second: 0x7f56b274d000 Success

The flags seem to be getting passed down to the kernel accurately...

$ strace -e trace=mmap ./a.out 2>&1 | tail -n5
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED_VALIDATE|MAP_ANONYMOUS, -1, 0) = -1 EINVAL (Invalid argument)
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANONYMOUS, -1, 0) = 0x7fd3145bb000
first: 0xffffffffffffffff Invalid argument
second: 0x7fd3145bb000 Success
+++ exited with 0 +++
like image 404
Paul Avatar asked Mar 02 '23 23:03

Paul


1 Answers

Looking at do_mmap in linux/mm/mmap.c (kernel version 5.9), MAP_SHARED_VALIDATE only seems to be supported for file-backed mappings (see the if (file) and else sections). I do not know if that is a bug or if it is intentional.

EDIT: I have submitted a bug report.

like image 143
Ian Abbott Avatar answered Mar 05 '23 14:03

Ian Abbott