I am trying to run the code example in this answer https://stackoverflow.com/a/38974980 on macOS 13, and the program gives segmentation fault.
In fact, let's simplify the program linked above to
#include<sys/mman.h>
#include<stddef.h>
int main(){
size_t size = 100;
char* buf = (char*) mmap(NULL, (size_t) size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
*buf = 'a';
return 0;
}
And even this simple program gives segmentation fault on macOS. The mmap does not return null pointer, but the pointer cannot be accessed. Removing PROT_EXEC
fixes the problem, but the whole point is to allocate executable memory. Why is it not working? How could I fix it?