Debian Codegraph @

tests/truncate_above_4GB.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 David Gibson & Adam Litke, IBM Corporation.
4 * Copyright (C) 2006 Hugh Dickins <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #define _LARGEFILE64_SOURCE
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <sys/mman.h>
29
30 #include <hugetlbfs.h>
31
32 #include "hugetests.h"
33
34 /*
35 * Test rationale:
36 *
37 * At one stage, a misconversion of hugetlb_vmtruncate_list to a
38 * prio_tree meant that on 32-bit machines, truncates at or above 4GB
39 * could truncate lower pages, resulting in BUG_ON()s.
40 *
41 * WARNING: The offsets and addresses used within are specifically
42 * calculated to trigger the bug as it existed. Don't mess with them
43 * unless you *really* know what you're doing.
44 *
45 * The kernel bug in question was fixed with commit
46 * 856fc29505556cf263f3dcda2533cf3766c14ab6.
47 */
48 #define FOURGIG ((off64_t)0x100000000ULL)
49
50 static void sigbus_handler_fail(int signum, siginfo_t *si, void *uc)
51 {
52 FAIL("Unexpected SIGBUS");
53 }
54
55 static void sigbus_handler_pass(int signum, siginfo_t *si, void *uc)
56 {
57 PASS();
58 }
59
60 int main(int argc, char *argv[])
61 {
62 int page_size;
63 long hpage_size;
64 long long buggy_offset, truncate_point;
65 int fd;
66 void *p, *q;
67 volatile unsigned int *pi, *qi;
68 int err;
69 struct sigaction sa_fail = {
70 .sa_sigaction = sigbus_handler_fail,
71 .sa_flags = SA_SIGINFO,
72 };
73 struct sigaction sa_pass = {
74 .sa_sigaction = sigbus_handler_pass,
75 .sa_flags = SA_SIGINFO,
76 };
77
78 test_init(argc, argv);
79
80 page_size = getpagesize();
81 hpage_size = check_hugepagesize();
82 truncate_point = FOURGIG;
83
84 if (hpage_size > truncate_point)
85 CONFIG("Huge page size is too large");
86
87 if (truncate_point % hpage_size > 0)
88 CONFIG("Truncation point is not aligned to huge page size");
89
90 check_free_huge_pages(3);
91
92 fd = hugetlbfs_unlinked_fd();
93 if (fd < 0)
94 FAIL("hugetlbfs_unlinked_fd()");
95
96 buggy_offset = truncate_point / (hpage_size / page_size);
97 buggy_offset = ALIGN(buggy_offset, hpage_size);
98
99 verbose_printf("Mapping 3 hpages at offset 0x%llx...", truncate_point);
100 /* First get arena of three hpages size, at file offset 4GB */
101 q = mmap64(NULL, 3*hpage_size, PROT_READ|PROT_WRITE,
102 MAP_PRIVATE, fd, truncate_point);
103 if (q == MAP_FAILED)
104 FAIL("mmap() offset 4GB: %s", strerror(errno));
105 verbose_printf("mapped at %p\n", q);
106 qi = q;
107 /* Touch the high page */
108 *qi = 0;
109
110 /* This part of the test makes the problem more obvious, but
111 * is not essential. It can't be done on segmented powerpc, where
112 * segment restrictions prohibit us from performing such a
113 * mapping, so skip it there. Similarly, ia64's address space
114 * restrictions prevent this. */
115 #if (defined(__powerpc__) && defined(PPC_NO_SEGMENTS)) \
116 || !defined(__powerpc__) && !defined(__powerpc64__) \
117 && !defined(__ia64__)
118 /* Replace middle hpage by tinypage mapping to trigger
119 * nr_ptes BUG */
120 verbose_printf("Replacing map at %p-%p...", q + hpage_size,
121 q + hpage_size + hpage_size-1);
122 p = mmap64(q + hpage_size, hpage_size, PROT_READ|PROT_WRITE,
123 MAP_FIXED|MAP_PRIVATE|MAP_ANON, -1, 0);
124 if (p != q + hpage_size)
125 FAIL("mmap() before low hpage");
126 verbose_printf("done\n");
127 pi = p;
128 /* Touch one page to allocate its page table */
129 *pi = 0;
130 #endif
131
132 /* Replace top hpage by hpage mapping at confusing file offset */
133 verbose_printf("Replacing map at %p with map from offset 0x%llx...",
134 q + 2*hpage_size, buggy_offset);
135 p = mmap64(q + 2*hpage_size, hpage_size, PROT_READ|PROT_WRITE,
136 MAP_FIXED|MAP_PRIVATE, fd, buggy_offset);
137 if (p != q + 2*hpage_size)
138 FAIL("mmap() buggy offset 0x%llx", buggy_offset);
139 verbose_printf("done\n");
140 pi = p;
141 /* Touch the low page with something non-zero */
142 *pi = 1;
143
144 verbose_printf("Truncating at 0x%llx...", truncate_point);
145 err = ftruncate64(fd, truncate_point);
146 if (err)
147 FAIL("ftruncate(): %s", strerror(errno));
148 verbose_printf("done\n");
149
150 err = sigaction(SIGBUS, &sa_fail, NULL);
151 if (err)
152 FAIL("sigaction() fail: %s", strerror(errno));
153
154 if (*pi != 1)
155 FAIL("Data 1 has changed to %u", *pi);
156
157 err = sigaction(SIGBUS, &sa_pass, NULL);
158 if (err)
159 FAIL("sigaction() pass: %s", strerror(errno));
160
161 *qi;
162
163 /* Should have SIGBUSed above */
164 FAIL("Didn't SIGBUS on truncated page.");
165 }