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