I am running the C program below and getting different results when executing it on RHEL 5.11 versus RHEL 6.8. On 5.11, the output is:
Page size is 4096
Memory allocated by memalign() at 0x12791000
mprotect success
While on 6.8, the output is:
Page size is 4096
Memory allocated by memalign() at 0xea6000
mprotect failed: Permission denied
Does anyone have any I idea why this is happening? Is there a kernel parameter that I need to set?
Here is the C program:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <malloc.h>
#include <sys/mman.h>
static void domp( void *, long );
static void domp( void *pmem, long psz ) {
if (mprotect( pmem, psz, PROT_READ | PROT_WRITE | PROT_EXEC ) == -1) {
perror( "mprotect failed" );
} else {
printf( "mprotect success\n" );
}
}
int main( int argc, char *argv[] ) {
long lpsz;
void *lmem;
lpsz = sysconf(_SC_PAGESIZE);
printf( "Page size is %ld\n", lpsz );
if ((lmem = memalign( lpsz, lpsz )) == NULL) {
perror( "memalign failed" );
exit (-1);
}
printf( "Memory allocated by memalign() at %p\n", lmem );
domp( lmem, lpsz );
return 0;
}