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 +++
printf
calls access out of bounds memory, as you haven't provided an argument for%p
format. Ifmmap
returns-1
, you should look aterrno
and see what the error is first. – Pawel Veselovmmap
may change the value oferrno
, so you need to look at it immediately after eachmmap
call, something like:uint8_t *first = mmap(...); printf("first: %p %s\n", (void *)first, strerror(errno));
(repeat for second). – zwolmmap
failing, not the segfault) on Linux 5.8 and I have absolutely no idea why.errno
isEINVAL
. As best I can tell from the manpage,MAP_SHARED_VALIDATE | MAP_ANONYMOUS
should be a valid combination of flags. – zwolMAP_SHARED_VALIDATE
? The man page says "since Linux 4.15". – Ian Abbott