#include #include #include #include #include int main(void) { void *addr; size_t size, page_size; page_size = getpagesize(); printf("Page size : %i\n", (int)page_size); size = 6 * (int)page_size; addr = mmap((caddr_t)0, size, (PROT_READ | PROT_WRITE), (MAP_PRIVATE | MAP_ANONYMOUS), 0, 0); if (addr == MAP_FAILED) { printf("mmap() failed !\n"); return 1; } printf("Address : %X\n", (int)addr); if (memset(addr, 0, size) != addr) { printf("memset() failed !\n"); return 2; } if (mprotect((caddr_t)addr, size, (PROT_NONE)) != 0) { printf("mprotect() (1) failed : %s\n", strerror(errno)); return 3; } if (mprotect((caddr_t)addr, size, (PROT_READ)) != 0) { printf("mprotect() (2) failed : %s\n", strerror(errno)); return 3; } if (mprotect((caddr_t)addr, size, (PROT_READ | PROT_WRITE)) != 0) { printf("mprotect() (3) failed : %s\n", strerror(errno)); return 3; } if (memset(addr, 1, size) != addr) { printf("memset() failed !\n"); return 4; } return 0; }