1 /* 2 * Copyright 2023, Trung Nguyen, trungnt282910@gmail.com. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include <fcntl.h> 7 #include <stdlib.h> 8 #include <stdio.h> 9 #include <sys/mman.h> 10 #include <unistd.h> 11 12 int 13 main() 14 { 15 char tmpfile[] = "/tmp/mmap_fixed_test_XXXXXX"; 16 int fd = mkstemp(tmpfile); 17 18 if (fd < 0) { 19 printf("cannot create temporary file.\n"); 20 return -1; 21 } 22 23 unlink(tmpfile); 24 ftruncate(fd, 4096); 25 26 // Should not crash the kernel (#18422) 27 void* addr = mmap(NULL, 4096, PROT_NONE, MAP_SHARED, fd, 0); 28 void* addr1 = mmap(addr, 4096, PROT_NONE, MAP_SHARED | MAP_FIXED, fd, 0); 29 30 if (addr == MAP_FAILED || addr1 == MAP_FAILED) { 31 printf("mmap failed.\n"); 32 return -1; 33 } 34 35 if (addr != addr1) { 36 printf("MAP_FIXED did not return same address.\n"); 37 return -1; 38 } 39 40 return 0; 41 } 42