Subject: Re 47429:allocating JIT code buffer failed: Permission denied

From: Jeffrey Walton <[email-masked]>
Date: Fri, 26 Mar 2021 19:34:24 -0400

Dammit, check for the proper return value...

% cat mmap-test.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
int main(int argc, char* argv[])
{
    size_t len = 10;
    int prot = PROT_EXEC | PROT_READ | PROT_WRITE;
    int flags = MAP_PRIVATE | MAP_ANONYMOUS;

    void *p = mmap (NULL, len, prot, flags, -1, 0);
    int err = errno;

    if (p != (void*) -1) {
        printf("p is good\n");
        munmap(p, len);
    }
    else {
        printf("p is bad (%d)\n", err);
    }

    return 0;
}

% clang -Wall mmap-test.c -o mmap-test.exe
% ./mmap-test.exe
p is bad (13)

That is the permission denied.

Next, avoid W+X:

% cat mmap-test.c
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mman.h>
int main(int argc, char* argv[])
{
    size_t len = 10;
    int prot = /*PROT_EXEC |*/ PROT_READ | PROT_WRITE;
    int flags = MAP_PRIVATE | MAP_ANONYMOUS;

    void *p = mmap (NULL, len, prot, flags, -1, 0);
    int err = errno;

    if (p != (void*) -1) {
        printf("p is good\n");
        munmap(p, len);
    }
    else {
        printf("p is bad (%d)\n", err);
    }

    return 0;
}

It looks like W^X is the culprit.

Jeff