Debian Codegraph @

tests/stack_grow_into_huge.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
20 #define _GNU_SOURCE
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/mman.h>
26 #include <sys/resource.h>
27 #include <sys/wait.h>
28
29 #include <hugetlbfs.h>
30 #include "hugetests.h"
31
32 /*
33 * Test rationale:
34 *
35 * On PowerPC, the address space is divided into segments. These segments can
36 * contain either huge pages or normal pages, but not both. All segments are
37 * initially set up to map normal pages. When a huge page mapping is created
38 * within a set of empty segments, they are "enabled" for huge pages at that
39 * time. Once enabled for huge pages, they can not be used again for normal
40 * pages for the remaining lifetime of the process.
41 *
42 * If the segment immediately preceeding the segment containing the stack is
43 * converted to huge pages and the stack is made to grow into the this
44 * preceeding segment, some kernels may attempt to map normal pages into the
45 * huge page-only segment -- resulting in bugs.
46 *
47 * The kernel bug in question was fixed by commit
48 * 0d59a01bc461bbab4017ff449b8401151ef44cf6.
49 */
50
51 #ifdef __LP64__
52 #define STACK_ALLOCATION_SIZE (256*1024*1024)
53 #else
54 #define STACK_ALLOCATION_SIZE (16*1024*1024)
55 #endif
56
57 void do_child(void *stop_address)
58 {
59 struct rlimit r;
60 volatile int *x;
61
62 /* corefile from this process is not interesting and limiting
63 * its size can save a lot of time. '1' is a special value,
64 * that will also abort dumping via pipe, which by default
65 * sets limit to RLIM_INFINITY. */
66 r.rlim_cur = 1;
67 r.rlim_max = 1;
68 setrlimit(RLIMIT_CORE, &r);
69
70 do {
71 x = alloca(STACK_ALLOCATION_SIZE);
72 *x = 1;
73 } while ((void *)x >= stop_address);
74 }
75
76 int main(int argc, char *argv[])
77 {
78 int fd, pid, s, ret;
79 struct rlimit r;
80 char *b;
81 long hpage_size = gethugepagesize();
82 void *stack_address, *mmap_address, *heap_address;
83
84 test_init(argc, argv);
85
86 ret = getrlimit(RLIMIT_STACK, &r);
87 if (ret)
88 CONFIG("getrlimit failed: %s", strerror(errno));
89
90 if (r.rlim_cur != RLIM_INFINITY)
91 CONFIG("Stack rlimit must be 'unlimited'");
92
93 fd = hugetlbfs_unlinked_fd();
94 if (fd < 0)
95 CONFIG("Couldn't get hugepage fd");
96
97 stack_address = alloca(0);
98 heap_address = sbrk(0);
99
100 /*
101 * paranoia: start mapping two hugepages below the start of the stack,
102 * in case the alignment would cause us to map over something if we
103 * only used a gap of one hugepage.
104 */
105 mmap_address = PALIGN(stack_address - 2 * hpage_size, hpage_size);
106
107 do {
108 b = mmap(mmap_address, hpage_size, PROT_READ|PROT_WRITE,
109 MAP_FIXED|MAP_SHARED, fd, 0);
110 mmap_address -= hpage_size;
111 /*
112 * if we get all the way down to the heap, stop trying
113 */
114 if (mmap_address <= heap_address)
115 break;
116 } while (b == MAP_FAILED);
117
118 if (b == MAP_FAILED)
119 FAIL("mmap: %s", strerror(errno));
120
121 if ((pid = fork()) < 0)
122 FAIL("fork: %s", strerror(errno));
123
124 if (pid == 0) {
125 do_child(mmap_address);
126 exit(0);
127 }
128
129 ret = waitpid(pid, &s, 0);
130 if (ret == -1)
131 FAIL("waitpid: %s", strerror(errno));
132
133 /*
134 * The child grows its stack until a failure occurs. We expect
135 * this to result in a SIGSEGV. If any other signal is
136 * delivered (ie. SIGTRAP) or no signal is sent at all, we
137 * determine the kernel has not behaved correctly and trigger a
138 * test failure.
139 */
140 if (WIFSIGNALED(s)) {
141 int sig = WTERMSIG(s);
142
143 if (sig == SIGSEGV) {
144 PASS();
145 } else {
146 FAIL("Got unexpected signal: %s", strsignal(sig));
147 }
148 }
149 FAIL("Child not signalled");
150 }