Debian Codegraph @

tests/task-size-overrun.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 _LARGEFILE64_SOURCE
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <errno.h>
28 #include <assert.h>
29
30 #include <hugetlbfs.h>
31
32 #include "hugetests.h"
33
34 #define MAPS_BUF_SZ 4096
35
36 static unsigned long find_last_mapped(void)
37 {
38 FILE *f;
39 char line[MAPS_BUF_SZ];
40 char *tmp;
41 unsigned long start, end, off, ino;
42 int ret;
43
44 f = fopen("/proc/self/maps", "r");
45 if (!f) {
46 ERROR("Failed to open /proc/self/maps: %s\n", strerror(errno));
47 return -1;
48 }
49
50 do {
51 tmp = fgets(line, MAPS_BUF_SZ, f);
52 } while (tmp);
53 fclose(f);
54
55 verbose_printf("Last map: %s", line);
56 ret = sscanf(line, "%lx-%lx %*s %lx %*s %ld %*s", &start, &end, &off, &ino);
57 if (ret == EOF)
58 FAIL("Couldn't parse /proc/self/maps line: %s: %s\n", line,
59 strerror(errno));
60 if (ret != 4)
61 FAIL("Couldn't parse /proc/self/maps line: %s\n", line);
62
63 verbose_printf("Last map at 0x%lx-0x%lx\n", start, end);
64 return end;
65 }
66
67 #define ALIGN_DOWN(x,a) ((x) & ~((a) - 1))
68
69 static unsigned long find_task_size(void)
70 {
71 unsigned long low, high; /* PFNs */
72 void *p;
73
74 low = find_last_mapped();
75 if (!low || ((low % getpagesize()) != 0))
76 FAIL("Bogus stack end address, 0x%lx!?", low);
77 low = low / getpagesize();
78
79 /* This sum should get us (2^(wordsize) - 2 pages) */
80 high = (unsigned long)(-2 * getpagesize()) / getpagesize();
81
82 verbose_printf("Binary searching for task size PFNs 0x%lx..0x%lx\n",
83 low, high);
84
85 while (high > low + 1) {
86 unsigned long pfn = (low + high) / 2;
87 unsigned long addr = pfn * getpagesize();
88
89 assert((pfn >= low) && (pfn <= high));
90
91 p = mmap64((void *)addr, getpagesize(), PROT_READ,
92 MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, -1, 0);
93 if (p == MAP_FAILED) {
94 verbose_printf("Map failed at 0x%lx (%s)\n",
95 addr, strerror(errno));
96 high = pfn;
97 } else {
98 verbose_printf("Map succeeded at 0x%lx\n", addr);
99 munmap(p, getpagesize());
100 low = pfn;
101 }
102 }
103
104 return low * getpagesize();
105 }
106
107 int main(int argc, char *argv[])
108 {
109 long hpage_size;
110 int fd;
111 void *p;
112 unsigned long task_size;
113 unsigned long straddle_addr;
114
115 test_init(argc, argv);
116
117 task_size = find_task_size();
118
119 verbose_printf("TASK_SIZE = 0x%lx\n", task_size);
120
121 hpage_size = check_hugepagesize();
122
123 fd = hugetlbfs_unlinked_fd();
124 if (fd < 0)
125 FAIL("hugetlbfs_unlinked_fd()");
126
127 straddle_addr = task_size - hpage_size;
128 straddle_addr = ALIGN(straddle_addr, hpage_size);
129
130 /* We first try to get the mapping without MAP_FIXED */
131 verbose_printf("Mapping without MAP_FIXED at %lx...", straddle_addr);
132 errno = 0;
133 p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
134 MAP_SHARED, fd, 0);
135 verbose_printf("%s\n", strerror(errno));
136 if (p == (void *)straddle_addr)
137 FAIL("Apparently suceeded in mapping across TASK_SIZE boundary");
138
139 verbose_printf("Mapping with MAP_FIXED at %lx...", straddle_addr);
140 errno = 0;
141 p = mmap((void *)straddle_addr, 2*hpage_size, PROT_READ|PROT_WRITE,
142 MAP_SHARED|MAP_FIXED, fd, 0);
143 verbose_printf("%s\n", strerror(errno));
144 if (p != MAP_FAILED)
145 FAIL("Apparently suceeded in mapping across TASK_SIZE boundary");
146
147 PASS();
148 }