0

I read that MAP_SHARED_VALIDATE will return EOPNOTSUPP and mmap() would fail when there are unknown flags but, when I use the LEGACY_MAP_MASK (All are valid flags), the mmap( ) is failing.

#define LEGACY_MAP_MASK (MAP_SHARED \
        | MAP_PRIVATE \
        | MAP_FIXED\
        | MAP_ANONYMOUS \
        | MAP_DENYWRITE \
        | MAP_EXECUTABLE \
        | MAP_UNINITIALIZED \
        | MAP_GROWSDOWN \
        | MAP_LOCKED \
        | MAP_NORESERVE \
        | MAP_POPULATE \
        | MAP_NONBLOCK \
        | MAP_STACK \
        | MAP_HUGETLB \
        | MAP_32BIT \
        | MAP_HUGE_2MB \

static int fd_file;
static void *mapped_address;

#define TEST_FILE "sample"
#define TEST_FILE_SIZE 1024
#define TEST_FILE_MODE 0600

fd_file = open(TEST_FILE, O_CREAT | O_RDWR, TEST_FILE_MODE);
mapped_address = mmap(NULL, TEST_FILE_SIZE, PROT_READ | PROT_WRITE, LEGACY_MAP_MASK |                  MAP_SHARED_VALIDATE, fd_file, 0);
if (mapped_address == MAP_FAILED)
  printf("Oops mmap() failed");

Error:

mmap( ) failed with the unexpected error: EINVAL (22)

====================================================================

Edit: After the response from the comment:

#define LEGACY_MAP_MASK (MAP_SHARED \
        | MAP_PRIVATE \
        | MAP_DENYWRITE \
        | MAP_EXECUTABLE \
        | MAP_UNINITIALIZED \
        | MAP_LOCKED \
        | MAP_NORESERVE \
        | MAP_POPULATE \
        | MAP_NONBLOCK \
        | MAP_STACK \
        | MAP_32BIT \
        | MAP_HUGE_2MB \
        | MAP_HUGE_1GB)
mapped_address = mmap(NULL, TEST_FILE_SIZE, PROT_READ | PROT_WRITE, LEGACY_MAP_MASK | MAP_SHARED_VALIDATE, fd_file, 0);
mmap( ) is successful,
Paul
  • 331
  • 2
  • 7
  • @Some programmer dude, Could you please help me with this. – Paul Sep 15 '20 at 06:08
  • @ 12431234123412341234123, Could you please tell me your views about it. – Paul Sep 15 '20 at 06:18
  • 1
    I'm not surprised that you're getting `EINVAL`, given that you're passing mutiple conflicting flags. You need to pick exactly one of `MAP_SHARED`, `MAP_PRIVATE`, `MAP_SHARED_VALIDATE`. – Hasturkun Sep 15 '20 at 09:18
  • @Hasturkun , Could you please tell how the mmap ( ) is successful even after using MAP_SHARED and MAP_PRIVATE. – Paul Sep 16 '20 at 07:28
  • No, and I don't really see what you've changed. In any case, passing more than one of these flags breaks your contract with the kernel (ORing them together will probably just give you `MAP_SHARED_VALIDATE`, but don't do that). Any one of your flags could be causing failure in combination. If you want to know which, I suggest you either read the kernel source, or apply Delta Debugging to your list of flags. – Hasturkun Sep 16 '20 at 10:55

0 Answers0