This source file includes following definitions.
- get_errno
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include <stdlib.h>
23
24 #include <errno.h>
25 #include <string.h>
26 #include <unistd.h>
27 #if defined __linux__
28 # include <fcntl.h>
29 # include <stdint.h>
30 # include <string.h>
31 # include <sys/mman.h>
32 #endif
33
34 #include "macros.h"
35
36
37
38
39
40 static int
41 get_errno (void)
42 {
43 volatile int err = errno;
44 return err;
45 }
46
47 static int (* volatile get_errno_func) (void) = get_errno;
48
49 int
50 main ()
51 {
52
53 {
54 errno = 1789;
55 free (NULL);
56 ASSERT_NO_STDIO (get_errno_func () == 1789);
57 }
58 {
59 #define N 10000
60 void * volatile ptrs[N];
61 size_t i;
62 for (i = 0; i < N; i++)
63 ptrs[i] = malloc (15);
64 for (i = 0; i < N; i++)
65 {
66 errno = 1789;
67 free (ptrs[i]);
68 ASSERT_NO_STDIO (get_errno_func () == 1789);
69 }
70 #undef N
71 }
72 {
73 #define N 1000
74 void * volatile ptrs[N];
75 size_t i;
76 for (i = 0; i < N; i++)
77 ptrs[i] = malloc (729);
78 for (i = 0; i < N; i++)
79 {
80 errno = 1789;
81 free (ptrs[i]);
82 ASSERT_NO_STDIO (get_errno_func () == 1789);
83 }
84 #undef N
85 }
86 {
87 #define N 10
88 void * volatile ptrs[N];
89 size_t i;
90 for (i = 0; i < N; i++)
91 ptrs[i] = malloc (5318153);
92 for (i = 0; i < N; i++)
93 {
94 errno = 1789;
95 free (ptrs[i]);
96 ASSERT_NO_STDIO (get_errno_func () == 1789);
97 }
98 #undef N
99 }
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 #if defined __linux__ && !(__GLIBC__ == 2 && __GLIBC_MINOR__ < 15)
115 if (open ("/proc/sys/vm/max_map_count", O_RDONLY) >= 0)
116 {
117
118 size_t pagesize = getpagesize ();
119 void *firstpage_backup = malloc (pagesize);
120 void *lastpage_backup = malloc (pagesize);
121
122
123
124 void *bumper_region =
125 mmap (NULL, 0x1000000, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
126
127 int fd = open ("test-free", O_RDONLY);
128
129 if (firstpage_backup != NULL && lastpage_backup != NULL
130 && bumper_region != (void *)(-1)
131 && fd >= 0)
132 {
133
134 size_t big_size = 0x1000000;
135 void * volatile ptr = malloc (big_size - 0x100);
136 char *ptr_aligned = (char *) ((uintptr_t) ptr & ~(pagesize - 1));
137
138
139
140
141 memcpy (firstpage_backup, ptr_aligned, pagesize);
142 memcpy (lastpage_backup, ptr_aligned + big_size - pagesize, pagesize);
143 if (mmap (ptr_aligned - pagesize, pagesize + big_size + pagesize,
144 PROT_READ | PROT_WRITE,
145 MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0)
146 != (void *)(-1))
147 {
148 memcpy (ptr_aligned, firstpage_backup, pagesize);
149 memcpy (ptr_aligned + big_size - pagesize, lastpage_backup, pagesize);
150
151
152
153
154 size_t i;
155 for (i = 0; i < 65536; i++)
156 if (mmap (NULL, pagesize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0)
157 == (void *)(-1))
158 break;
159
160
161
162 errno = 1789;
163
164
165
166
167 free (ptr);
168 ASSERT_NO_STDIO (get_errno_func () == 1789);
169 }
170 }
171 }
172 #endif
173
174 return 0;
175 }