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  * If MAP_FIXED is set,
10  * mmap() may return MAP_FAILED and set errno to [EINVAL].
11  *
12  * [EINVAL] The addr argument (if MAP_FIXED was specified) or off is not a
13  * multiple of the page size as returned by sysconf(), or is considered invalid
14  * by the implementation.
15  *
16  * Test Steps:
17  * 1. Set 'addr' as an illegal address, which is not a multiple of page size;
18  * 2. Call mmap() and get EINVAL;
19  */
20 
21 #define _XOPEN_SOURCE 600
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <errno.h>
33 #include "posixtest.h"
34 
main(void)35 int main(void)
36 {
37 	char tmpfname[256];
38 	long page_size;
39 	long total_size;
40 
41 	void *illegal_addr;
42 	void *pa;
43 	size_t size;
44 	int fd, saved_errno;
45 
46 	page_size = sysconf(_SC_PAGE_SIZE);
47 	total_size = page_size;
48 	size = total_size;
49 
50 	/* Create tmp file */
51 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_9_1_%d", getpid());
52 	unlink(tmpfname);
53 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
54 	if (fd == -1) {
55 		printf("Error at open(): %s\n", strerror(errno));
56 		return PTS_UNRESOLVED;
57 	}
58 	unlink(tmpfname);
59 	if (ftruncate(fd, total_size) == -1) {
60 		printf("Error at ftruncate(): %s\n", strerror(errno));
61 		return PTS_UNRESOLVED;
62 	}
63 
64 	/* Map the file for the first time, to get a legal address, pa */
65 	pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
66 
67 	if ((unsigned long)pa % page_size) {
68 		printf("pa is not multiple of page_size\n");
69 		illegal_addr = pa;
70 	} else {
71 		printf("pa is a multiple of page_size\n");
72 		illegal_addr = pa + 1;
73 	}
74 
75 	munmap(pa, size);
76 
77 	/* Mmap again using the illegal address, setting MAP_FIXED */
78 	pa = mmap(illegal_addr, size, PROT_READ | PROT_WRITE, MAP_FIXED, fd, 0);
79 
80 	saved_errno = errno;
81 
82 	close(fd);
83 	munmap(pa, size);
84 
85 	if (pa == MAP_FAILED && saved_errno == EINVAL) {
86 		printf("Test PASSED\n");
87 		return PTS_PASS;
88 	}
89 
90 	printf("Test FAILED, mmap with MAP_FIXED did not get EINVAL"
91 	       " when 'addr' is illegal\n");
92 	return PTS_FAIL;
93 }
94