• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
3  * Copyright (c) 2012, Cyril Hrubis <chrubis@suse.cz>
4  *
5  * This file is licensed under the GPL license.  For the full content
6  * of this license, see the COPYING file at the top level of this
7  * source tree.
8  *
9  * Implementation performs mapping operations over whole pages.
10  * Thus, while the argument len
11  * need not meet a size or alignment constraint,
12  * the implementation shall include, in any mapping
13  * operation, any partial page specified by the range [pa,pa+len).
14  * The system shall always zero-fill any partial page at the end of an object.
15  * Further, the system shall never write out any modified portions of
16  * the last page of an object which are beyond its end.
17  *
18  * Test Steps:
19  * 1. Create a process, in this process:
20  *    a. map a file  with size of 1/2 * page_size,
21  *       set len = 1/2 * page_size
22  *    b. Read the partial page beyond the object size.
23  *       Make sure the partial page is zero-filled;
24  *    c. Modify a byte in the partial page, then un-map the and close the
25  *       file descriptor.
26  * 2. Wait for the child proces to exit, then
27  *    Map the file again,
28  *    read the byte from the position modified at step 1-c and check.
29  */
30 
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/mman.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <errno.h>
42 #ifdef	__linux__
43 #include <sys/vfs.h>
44 #endif
45 #include "posixtest.h"
46 
47 #define TYPE_TMPFS_MAGIC	0x01021994
48 
main(void)49 int main(void)
50 {
51 	char tmpfname[256];
52 	long page_size;
53 
54 	char *pa;
55 	size_t len;
56 	int fd;
57 
58 	pid_t child;
59 	int i, exit_val;
60 
61 	page_size = sysconf(_SC_PAGE_SIZE);
62 
63 	/* mmap will create a partial page */
64 	len = page_size / 2;
65 
66 #ifdef	__linux__
67 	struct statfs buf;
68 
69 	if (statfs("/tmp", &buf)) {
70 		printf("Error at statfs(): %s\n", strerror(errno));
71 		return PTS_UNRESOLVED;
72 	}
73 
74 	if (buf.f_type == TYPE_TMPFS_MAGIC) {
75 		printf("From mmap(2) manpage, skip known bug on tmpfs\n");
76 		return PTS_UNTESTED;
77 	}
78 #endif
79 
80 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_5_%d", getpid());
81 	child = fork();
82 	switch (child) {
83 	case 0:
84 		/* Create shared object */
85 		unlink(tmpfname);
86 		fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
87 			  S_IRUSR | S_IWUSR);
88 		if (fd == -1) {
89 			printf("Error at open(): %s\n", strerror(errno));
90 			return PTS_UNRESOLVED;
91 		}
92 		if (ftruncate(fd, len) == -1) {
93 			printf("Error at ftruncate(): %s\n", strerror(errno));
94 			return PTS_UNRESOLVED;
95 		}
96 
97 		pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
98 		if (pa == MAP_FAILED) {
99 			printf("Error at mmap(): %s\n", strerror(errno));
100 			return PTS_FAIL;
101 		}
102 
103 		/* Check the patial page is ZERO filled */
104 		for (i = len; i < page_size; i++)
105 			if (pa[i] != 0) {
106 				printf("Test FAILED: The partial page at the "
107 				       "end of an object is not zero-filled\n");
108 				return PTS_FAIL;
109 			}
110 
111 		/* Write the partial page */
112 		pa[len + 1] = 'b';
113 		msync(pa, len, MS_SYNC);
114 		munmap(pa, len);
115 		close(fd);
116 		return PTS_PASS;
117 	case -1:
118 		printf("Error at fork(): %s\n", strerror(errno));
119 		return PTS_UNRESOLVED;
120 	default:
121 		break;
122 	}
123 
124 	wait(&exit_val);
125 	if (!(WIFEXITED(exit_val) && (WEXITSTATUS(exit_val) == PTS_PASS))) {
126 		unlink(tmpfname);
127 
128 		if (WIFEXITED(exit_val))
129 			return WEXITSTATUS(exit_val);
130 
131 		printf("Child exited abnormally\n");
132 		return PTS_UNRESOLVED;
133 	}
134 
135 	fd = open(tmpfname, O_RDWR, 0);
136 	unlink(tmpfname);
137 
138 	pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
139 	if (pa == MAP_FAILED) {
140 		printf("Error at 2nd mmap(): %s\n", strerror(errno));
141 		return PTS_FAIL;
142 	}
143 
144 	if (pa[len + 1] == 'b') {
145 		printf("Test FAILED: Modification of the partial page "
146 		       "at the end of an object is written out\n");
147 		return PTS_FAIL;
148 	}
149 	close(fd);
150 	munmap(pa, len);
151 
152 	printf("Test PASSED\n");
153 	return PTS_PASS;
154 }
155