Debian Codegraph @

tests/misalign.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2007 David Gibson & Adam Litke, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <signal.h>
25 #include <sys/mman.h>
26 #include <sys/utsname.h>
27
28 #include <hugetlbfs.h>
29
30 #include "hugetests.h"
31
32 /*
33 * Test rationale:
34 *
35 * Just as normal mmap()s can't have an address, length or offset
36 * which is not page aligned, so hugepage mmap()s can't have an
37 * address, length or offset with is not hugepage aligned.
38 *
39 * However, from time to time when the various mmap() /
40 * get_unmapped_area() paths are updated, somebody misses one of the
41 * necessary checks for the hugepage paths. This testcase ensures
42 * that attempted hugepage mappings with parameters which are not
43 * correctly hugepage aligned are rejected.
44 *
45 * However starting with 3.10-rc1, length passed in mmap() doesn't need
46 * to be aligned because commit af73e4d9506d3b797509f3c030e7dcd554f7d9c4
47 * added ALIGN() to kernel side, in mmap_pgoff(), when mapping huge page
48 * files.
49 */
50 int main(int argc, char *argv[])
51 {
52 long page_size, hpage_size;
53 int fd;
54 void *p, *q;
55 int err;
56 struct utsname buf;
57
58 test_init(argc, argv);
59
60 if (uname(&buf) != 0)
61 FAIL("uname failed %s", strerror(errno));
62
63 page_size = getpagesize();
64 hpage_size = check_hugepagesize();
65
66 fd = hugetlbfs_unlinked_fd();
67 if (fd < 0)
68 FAIL("hugetlbfs_unlinked_fd()");
69
70 /* First see what an ok mapping looks like, as a basis for our
71 * bad addresses and so forth */
72 p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
73 if (p == MAP_FAILED)
74 FAIL("mmap() without hint failed: %s", strerror(errno));
75 if (((unsigned long)p % hpage_size) != 0)
76 FAIL("mmap() without hint at misaligned address");
77
78 verbose_printf("Mapped at %p, length 0x%lx\n", p, hpage_size);
79
80 err = munmap(p, hpage_size);
81 if (err != 0)
82 FAIL("munmap() without hint failed: %s", strerror(errno));
83
84 /* 1) Try a misaligned hint address */
85 q = mmap(p + page_size, hpage_size, PROT_READ|PROT_WRITE,
86 MAP_PRIVATE, fd, 0);
87 if (q == MAP_FAILED)
88 /* Bad hint shouldn't fail, just ignore the hint */
89 FAIL("mmap() with hint failed: %s", strerror(errno));
90 if (((unsigned long)q % hpage_size) != 0)
91 FAIL("mmap() with hint at misaligned address");
92
93 err = munmap(q, hpage_size);
94 if (err != 0)
95 FAIL("munmap() with hint failed: %s", strerror(errno));
96
97 /* 2) Try a misaligned address with MAP_FIXED */
98 q = mmap(p + page_size, hpage_size, PROT_READ|PROT_WRITE,
99 MAP_PRIVATE|MAP_FIXED, fd, 0);
100 if (q != MAP_FAILED)
101 FAIL("mmap() MAP_FIXED at misaligned address succeeded");
102
103 /* 3) Try a misaligned length */
104 q = mmap(NULL, page_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
105
106 if (test_compare_kver(buf.release, "3.10.0") < 0) {
107 if (q != MAP_FAILED)
108 FAIL("mmap() with misaligned length 0x%lx succeeded",
109 page_size);
110 } else {
111 if (q == MAP_FAILED)
112 FAIL("mmap() with misaligned length 0x%lx failed",
113 page_size);
114 }
115
116 /* 4) Try a misaligned length with MAP_FIXED */
117 q = mmap(p, page_size, PROT_READ|PROT_WRITE,
118 MAP_PRIVATE|MAP_FIXED, fd, 0);
119
120 if (test_compare_kver(buf.release, "3.10.0") < 0) {
121 if (q != MAP_FAILED)
122 FAIL("mmap() MAP_FIXED with misaligned length 0x%lx "
123 "succeeded", page_size);
124 } else {
125 if (q == MAP_FAILED)
126 FAIL("mmap() MAP_FIXED with misaligned length 0x%lx "
127 "failed", page_size);
128 }
129
130 /* 5) Try a misaligned offset */
131 q = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE,
132 MAP_PRIVATE, fd, page_size);
133 if (q != MAP_FAILED)
134 FAIL("mmap() with misaligned offset 0x%lx succeeded",
135 page_size);
136
137 /* 6) Try a misaligned offset with MAP_FIXED*/
138 q = mmap(p, hpage_size, PROT_READ|PROT_WRITE,
139 MAP_PRIVATE|MAP_FIXED, fd, page_size);
140 if (q != MAP_FAILED)
141 FAIL("mmap() MAP_FIXED with misaligned offset 0x%lx succeeded",
142 page_size);
143
144 PASS();
145 }