Debian Codegraph @

tests/truncate_sigbus_versus_oom.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 2005-2006 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
27 #include <hugetlbfs.h>
28
29 #include "hugetests.h"
30
31 /*
32 * Test rationale:
33 *
34 * Some kernel have a bug in the positioning of the test against
35 * i_size. This bug means that attempting to instantiate a page
36 * beyond the end of a hugepage file can result in an OOM and SIGKILL
37 * instead of the correct SIGBUS.
38 *
39 * This bug was fixed by commit ebed4bfc8da8df5b6b0bc4a5064a949f04683509.
40 */
41 static void sigbus_handler(int signum, siginfo_t *si, void *uc)
42 {
43 PASS();
44 }
45
46 int main(int argc, char *argv[])
47 {
48 long hpage_size;
49 int fd, fdx;
50 unsigned long totpages;
51 void *p, *q;
52 int i;
53 int err;
54 struct sigaction sa = {
55 .sa_sigaction = sigbus_handler,
56 .sa_flags = SA_SIGINFO,
57 };
58
59 test_init(argc, argv);
60
61 hpage_size = check_hugepagesize();
62 totpages = get_huge_page_counter(hpage_size, HUGEPAGES_TOTAL);
63
64 fd = hugetlbfs_unlinked_fd();
65 if (fd < 0)
66 FAIL("hugetlbfs_unlinked_fd()");
67
68 p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
69 if (p == MAP_FAILED)
70 FAIL("mmap(): %s", strerror(errno));
71 err = ftruncate(fd, 0);
72 if (err)
73 FAIL("ftruncate(): %s", strerror(errno));
74
75 /* Now slurp up all the available pages */
76 fdx = hugetlbfs_unlinked_fd();
77 if (fdx < 0)
78 FAIL("hugetlbfs_unlinked_fd() 2");
79
80 q = mmap(NULL, totpages * hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED,
81 fdx, 0);
82 if (q == MAP_FAILED)
83 FAIL("mmap() reserving all pages: %s", strerror(errno));
84
85 /* Touch the pages to ensure they're removed from the pool */
86 for (i = 0; i < totpages; i++) {
87 volatile char *x = (volatile char *)q + i*hpage_size;
88 *x = 0;
89 }
90
91 /* SIGBUS is what *should* happen */
92 err = sigaction(SIGBUS, &sa, NULL);
93 if (err)
94 FAIL("sigaction(): %s", strerror(errno));
95
96 *((volatile unsigned int *)p);
97
98 /* Should have SIGBUSed above, or (failed the test) with SIGKILL */
99 FAIL("Didn't SIGBUS or OOM");
100 }