Debian Codegraph @

tests/heap-overflow.c

raw symbols SCIP
1 /*
2 * Test heap overflow for libhugetlbfs.
3 * Copyright 2008 Cray Inc. All rights reserved.
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
8 * of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
18 */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <sys/wait.h>
26 #include <hugetlbfs.h>
27 #include "hugetests.h"
28
29 long oc_pool = -1;
30 long hpagesize;
31
32 void cleanup(void)
33 {
34 if (oc_pool > 0)
35 restore_overcommit_pages(hpagesize, oc_pool);
36 }
37
38 int main(int argc, char **argv)
39 {
40 int freepages;
41 long size1, size2;
42 void *p1, *p2;
43 int st, pid, rv;
44 unsigned long long mapping_size;
45
46 test_init(argc, argv);
47
48 if (!getenv("HUGETLB_MORECORE"))
49 CONFIG("Must have HUGETLB_MORECORE=yes");
50
51 hpagesize = check_hugepagesize();
52
53 /* Must be root because this test modifies the overcommit pool */
54 check_must_be_root();
55
56 oc_pool = read_nr_overcommit(hpagesize);
57 if (oc_pool > 0)
58 set_nr_overcommit_hugepages(hpagesize, 0);
59
60 freepages = get_huge_page_counter(hpagesize, HUGEPAGES_FREE);
61 if (freepages < 3)
62 CONFIG("Must have at least 3 free hugepages");
63
64 /*
65 * Allocation 1: one hugepage. Due to malloc overhead, morecore
66 * will probably mmap two hugepages.
67 */
68 size1 = hpagesize;
69 p1 = malloc(size1);
70 if (!p1)
71 FAIL("Couldn't malloc %ld bytes", size1);
72 mapping_size = get_mapping_page_size(p1);
73 if (mapping_size != hpagesize)
74 FAIL("First allocation %p not on hugepages", p1);
75
76 /*
77 * Allocation 2: all free hugepages to ensure we exhaust the free pool.
78 */
79 size2 = freepages * hpagesize;
80 p2 = malloc(size2);
81 if (!p2)
82 FAIL("Couldn't malloc %ld bytes", size2);
83 mapping_size = get_mapping_page_size(p1);
84 st = (mapping_size == hpagesize);
85 verbose_printf("Second allocation %p huge? %s\n", p2, st < 0 ? "??" :
86 (st ? "yes" : "no"));
87
88 /*
89 * Touch the pages in a child process. Kernel sends a SIGKILL if
90 * we run out of hugepages.
91 */
92 pid = fork();
93 if (pid < 0)
94 FAIL("fork: %s", strerror(errno));
95
96 if (pid == 0) {
97 memset(p1, 0, size1);
98 memset(p2, 0, size2);
99 exit(0);
100 }
101
102 rv = waitpid(pid, &st, 0);
103 if (rv < 0)
104 FAIL("waitpid: %s\n", strerror(errno));
105 if (WIFSIGNALED(st))
106 FAIL("Child killed by signal %d touching malloc'ed memory",
107 WTERMSIG(st));
108
109 PASS();
110 }