• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <sys/mman.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/wait.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <pthread.h>
28 #include <signal.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 
34 #include "posixtest.h"
35 #include "tempfile.h"
36 
37 #define TNAME "munmap/1-1.c"
38 
sigsegv_handler(int signum PTS_ATTRIBUTE_UNUSED)39 static void sigsegv_handler(int signum PTS_ATTRIBUTE_UNUSED)
40 {
41 	printf("Got SIGSEGV\n");
42 	printf("Test PASSED\n");
43 	exit(PTS_PASS);
44 }
45 
main(void)46 int main(void)
47 {
48 	char tmpfname[PATH_MAX];
49 	long file_size;
50 
51 	void *pa = NULL;
52 	void *addr = NULL;
53 	size_t len;
54 	int flag;
55 	int fd;
56 	off_t off = 0;
57 	int prot;
58 
59 	int page_size;
60 
61 	char *ch1;
62 
63 	struct sigaction sa;
64 
65 	sigfillset(&sa.sa_mask);
66 	sa.sa_handler = sigsegv_handler;
67 	sigaction(SIGSEGV, &sa, NULL);
68 
69 	page_size = sysconf(_SC_PAGE_SIZE);
70 	file_size = 2 * page_size;
71 
72 	/* We hope to map 2 pages */
73 	len = page_size + 1;
74 
75 	/* Create tmp file */
76 	PTS_GET_TMP_FILENAME(tmpfname, "pts_munmap_1_1");
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