Home | History | Annotate | Download | only in mmap
      1 /*
      2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
      3  * Copyright (c) 2012, Cyril Hrubis <chrubis (at) 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 #include <sys/vfs.h>
     43 #include "posixtest.h"
     44 
     45 #define TYPE_TMPFS_MAGIC	0x01021994
     46 
     47 int main(void)
     48 {
     49 	char tmpfname[256];
     50 	long page_size;
     51 
     52 	char *pa;
     53 	size_t len;
     54 	int fd;
     55 
     56 	pid_t child;
     57 	int i, exit_val;
     58 
     59 	struct statfs buf;
     60 
     61 	page_size = sysconf(_SC_PAGE_SIZE);
     62 
     63 	/* mmap will create a partial page */
     64 	len = page_size / 2;
     65 
     66 	if (statfs("/tmp", &buf)) {
     67 		printf("Error at statfs(): %s\n", strerror(errno));
     68 		return PTS_UNRESOLVED;
     69 	}
     70 
     71 	if (buf.f_type == TYPE_TMPFS_MAGIC) {
     72 		printf("From mmap(2) manpage, skip known bug on tmpfs\n");
     73 		return PTS_UNTESTED;
     74 	}
     75 
     76 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_5_%d", getpid());
     77 	child = fork();
     78 	switch (child) {
     79 	case 0:
     80 		/* Create shared object */
     81 		unlink(tmpfname);
     82 		fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
     83 			  S_IRUSR | S_IWUSR);
     84 		if (fd == -1) {
     85 			printf("Error at open(): %s\n", strerror(errno));
     86 			return PTS_UNRESOLVED;
     87 		}
     88 		if (ftruncate(fd, len) == -1) {
     89 			printf("Error at ftruncate(): %s\n", strerror(errno));
     90 			return PTS_UNRESOLVED;
     91 		}
     92 
     93 		pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
     94 		if (pa == MAP_FAILED) {
     95 			printf("Error at mmap(): %s\n", strerror(errno));
     96 			return PTS_FAIL;
     97 		}
     98 
     99 		/* Check the patial page is ZERO filled */
    100 		for (i = len; i < page_size; i++)
    101 			if (pa[i] != 0) {
    102 				printf("Test FAILED: The partial page at the "
    103 				       "end of an object is not zero-filled\n");
    104 				return PTS_FAIL;
    105 			}
    106 
    107 		/* Write the partial page */
    108 		pa[len + 1] = 'b';
    109 		msync(pa, len, MS_SYNC);
    110 		munmap(pa, len);
    111 		close(fd);
    112 		return PTS_PASS;
    113 	case -1:
    114 		printf("Error at fork(): %s\n", strerror(errno));
    115 		return PTS_UNRESOLVED;
    116 	default:
    117 		break;
    118 	}
    119 
    120 	wait(&exit_val);
    121 	if (!(WIFEXITED(exit_val) && (WEXITSTATUS(exit_val) == PTS_PASS))) {
    122 		unlink(tmpfname);
    123 
    124 		if (WIFEXITED(exit_val))
    125 			return WEXITSTATUS(exit_val);
    126 
    127 		printf("Child exited abnormally\n");
    128 		return PTS_UNRESOLVED;
    129 	}
    130 
    131 	fd = open(tmpfname, O_RDWR, 0);
    132 	unlink(tmpfname);
    133 
    134 	pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    135 	if (pa == MAP_FAILED) {
    136 		printf("Error at 2nd mmap(): %s\n", strerror(errno));
    137 		return PTS_FAIL;
    138 	}
    139 
    140 	if (pa[len + 1] == 'b') {
    141 		printf("Test FAILED: Modification of the partial page "
    142 		       "at the end of an object is written out\n");
    143 		return PTS_FAIL;
    144 	}
    145 	close(fd);
    146 	munmap(pa, len);
    147 
    148 	printf("Test PASSED\n");
    149 	return PTS_PASS;
    150 }
    151