1 /* 2 * Copyright (c) 2002, Intel Corporation. All rights reserved. 3 * This file is licensed under the GPL license. For the full content 4 * of this license, see the COPYING file at the top level of this 5 * source tree. 6 * 7 * The munmap() function shall remove any mappings for those 8 * entire pages containing any part of the address space of 9 * the process starting at addr and continuing for len bytes. 10 * Further references to these pages shall result in the 11 * generation of a SIGSEGV signal to the process. 12 * 13 * Test Step: 14 * 1. map a file into memory; 15 * 2. unmap; 16 * 3. Try to reference the unmapped memory, outside the 17 * mapped file, test whether SIGSEGV is triggered. 18 */ 19 20 #define _XOPEN_SOURCE 600 21 22 #include <sys/mman.h> 23 #include <sys/types.h> 24 #include <sys/stat.h> 25 #include <sys/wait.h> 26 #include <errno.h> 27 #include <fcntl.h> 28 #include <pthread.h> 29 #include <signal.h> 30 #include <stdio.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <unistd.h> 34 #include "posixtest.h" 35 36 #define TNAME "munmap/1-1.c" 37 38 void sigsegv_handler(int signum) 39 { 40 printf("Got SIGSEGV\n"); 41 printf("Test PASSED\n"); 42 exit(PTS_PASS); 43 } 44 45 int main(void) 46 { 47 char tmpfname[256]; 48 long file_size; 49 50 void *pa = NULL; 51 void *addr = NULL; 52 size_t len; 53 int flag; 54 int fd; 55 off_t off = 0; 56 int prot; 57 58 int page_size; 59 60 char *ch1; 61 62 struct sigaction sa; 63 64 sigfillset(&sa.sa_mask); 65 sa.sa_handler = sigsegv_handler; 66 sigaction(SIGSEGV, &sa, NULL); 67 68 page_size = sysconf(_SC_PAGE_SIZE); 69 file_size = 2 * page_size; 70 71 /* We hope to map 2 pages */ 72 len = page_size + 1; 73 74 /* Create tmp file */ 75 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_munmap_1_1_%d", 76 getpid()); 77 unlink(tmpfname); 78 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR); 79 if (fd == -1) { 80 printf(TNAME " Error at open(): %s\n", strerror(errno)); 81 exit(PTS_UNRESOLVED); 82 } 83 unlink(tmpfname); 84 85 if (ftruncate(fd, file_size) == -1) { 86 printf("Error at ftruncate: %s\n", strerror(errno)); 87 exit(PTS_UNRESOLVED); 88 } 89 90 flag = MAP_SHARED; 91 prot = PROT_READ | PROT_WRITE; 92 pa = mmap(addr, len, prot, flag, fd, off); 93 if (pa == MAP_FAILED) { 94 printf("Test UNRESOLVED: " TNAME " Error at mmap: %s\n", 95 strerror(errno)); 96 exit(PTS_UNRESOLVED); 97 } 98 99 /* ch1 is outside the mapped object, but in the mapped file */ 100 ch1 = pa + len + 1; 101 *ch1 = 'b'; 102 103 close(fd); 104 if (munmap(pa, len) == -1) { 105 printf("Test FAILED: " TNAME " Error at munmap: %s\n", 106 strerror(errno)); 107 exit(PTS_FAIL); 108 } 109 110 /* Try to reference the unmapped area, should trigger SIGSEGV */ 111 *ch1 = 'a'; 112 113 /* If reach this point, test fail */ 114 printf("Test FAILED: Did not trigger SIGSEGV\n"); 115 return PTS_FAIL; 116 } 117