source: tests/heap.cfa@ 013b028

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 013b028 was 505450a, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

abort when memory check fails

  • Property mode set to 100644
File size: 15.2 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// heap.cfa --
8//
9// Author : Peter A. Buhr
10// Created On : Tue Nov 6 17:54:56 2018
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Aug 9 08:05:16 2020
13// Update Count : 57
14//
15
16#include <thread.hfa>
17#include <kernel.hfa> // processor
18#include <stdlib.hfa> // *allocs
19#include <malloc.h> // malloc_*
20
21// #include <time.hfa>
22// #define __CFA_DEFAULT_PREEMPTION__ 1000`us
23// //#define __CFA_DEFAULT_PREEMPTION__ 0
24
25// Duration default_preemption() {
26// return __CFA_DEFAULT_PREEMPTION__;
27// }
28
29#define __U_DEFAULT_MMAP_START__ (512 * 1024 + 1)
30size_t default_mmap_start() __attribute__(( weak )) {
31 return __U_DEFAULT_MMAP_START__;
32} // default_mmap_start
33
34thread Worker {
35}; // Worker
36
37void main( Worker & ) {
38 enum { NoOfAllocs = 5000, NoOfMmaps = 10 };
39 char * locns[NoOfAllocs];
40 size_t amount;
41 enum { limit = 64 * 1024 }; // check alignments up to here
42
43 // check alloc/free
44
45 for ( j; 40 ) {
46 for ( i; NoOfAllocs ) {
47 locns[i] = alloc( i );
48 //sout | (void *)locns[i];
49 for ( k; i ) locns[i][k] = '\345';
50 } // for
51 //sout | (char *)sbrk(0) - start | " bytes";
52
53 for ( i; NoOfAllocs ) {
54 //sout | (void *)locns[i];
55 for ( k; i ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage1" );
56 free( locns[i] );
57 } // for
58 //sout | (char *)sbrk(0) - start | " bytes";
59
60 for ( i; NoOfAllocs ) {
61 locns[i] = alloc( i );
62 //sout | (void *)locns[i];
63 for ( k; i ) locns[i][k] = '\345';
64 } // for
65 for ( i; NoOfAllocs - 1 -~= 0 ) {
66 //sout | (void *)locns[i];
67 for ( k; i ) if ( locns[i][k] != '\345' ) abort( "new/delete corrupt storage2" );
68 free( locns[i] );
69 } // for
70 } // for
71
72 // check malloc/free (sbrk)
73
74 for ( i; NoOfAllocs ) {
75 size_t s = (i + 1) * 20;
76 char * area = (char *)malloc( s );
77 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
78 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
79 free( area );
80 } // for
81
82 for ( i; NoOfAllocs ) {
83 size_t s = i + 1; // +1 to make initialization simpler
84 locns[i] = (char *)malloc( s );
85 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
86 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
87 } // for
88 for ( i; NoOfAllocs ) {
89 size_t s = i + 1;
90 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
91 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" );
92 free( locns[i] );
93 } // for
94
95 // check malloc/free (mmap)
96
97 for ( i; NoOfMmaps ) {
98 size_t s = i + default_mmap_start(); // cross over point
99 char * area = (char *)malloc( s );
100 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
101 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
102 free( area );
103 } // for
104
105 for ( i; NoOfMmaps ) {
106 size_t s = i + default_mmap_start(); // cross over point
107 locns[i] = (char *)malloc( s );
108 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
109 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
110 } // for
111 for ( i; NoOfMmaps ) {
112 size_t s = i + default_mmap_start(); // cross over point
113 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
114 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "malloc/free corrupt storage" );
115 free( locns[i] );
116 } // for
117
118 // check calloc/free (sbrk)
119
120 for ( i; NoOfAllocs ) {
121 size_t s = (i + 1) * 20;
122 char * area = (char *)calloc( 5, s );
123 if ( area[0] != '\0' || area[s - 1] != '\0' ||
124 area[malloc_size( area ) - 1] != '\0' ||
125 ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage1" );
126 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
127 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
128 free( area );
129 } // for
130
131 for ( i; NoOfAllocs ) {
132 size_t s = i + 1;
133 locns[i] = (char *)calloc( 5, s );
134 if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' ||
135 locns[i][malloc_size( locns[i] ) - 1] != '\0' ||
136 ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage2" );
137 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
138 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
139 } // for
140 for ( i; NoOfAllocs ) {
141 size_t s = i + 1;
142 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
143 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage3" );
144 free( locns[i] );
145 } // for
146
147 // check calloc/free (mmap)
148
149 for ( i; NoOfMmaps ) {
150 size_t s = i + default_mmap_start(); // cross over point
151 char * area = (char *)calloc( 1, s );
152 if ( area[0] != '\0' || area[s - 1] != '\0' ) abort( "calloc/free corrupt storage4.1" );
153 if ( area[malloc_size( area ) - 1] != '\0' ) abort( "calloc/free corrupt storage4.2" );
154 if ( ! malloc_zero_fill( area ) ) abort( "calloc/free corrupt storage4.3" );
155 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last
156 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
157 free( area );
158 } // for
159
160 for ( i; NoOfMmaps ) {
161 size_t s = i + default_mmap_start(); // cross over point
162 locns[i] = (char *)calloc( 1, s );
163 if ( locns[i][0] != '\0' || locns[i][s - 1] != '\0' ||
164 locns[i][malloc_size( locns[i] ) - 1] != '\0' ||
165 ! malloc_zero_fill( locns[i] ) ) abort( "calloc/free corrupt storage5" );
166 locns[i][0] = '\345'; locns[i][s - 1] = '\345'; // fill first/last
167 locns[i][malloc_usable_size( locns[i] ) - 1] = '\345'; // fill ultimate byte
168 } // for
169 for ( i; NoOfMmaps ) {
170 size_t s = i + default_mmap_start(); // cross over point
171 if ( locns[i][0] != '\345' || locns[i][s - 1] != '\345' ||
172 locns[i][malloc_usable_size( locns[i] ) - 1] != '\345' ) abort( "calloc/free corrupt storage6" );
173 free( locns[i] );
174 } // for
175
176 // check memalign/free (sbrk)
177
178 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
179 //sout | alignments[a];
180 for ( s; 1 ~ NoOfAllocs ) { // allocation of size 0 can return null
181 char * area = (char *)memalign( a, s );
182 //sout | i | area;
183 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
184 abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, s, area );
185 } // if
186 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte
187 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
188 free( area );
189 } // for
190 } // for
191
192 // check memalign/free (mmap)
193
194 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
195 //sout | alignments[a];
196 for ( i; 1 ~ NoOfMmaps ) {
197 size_t s = i + default_mmap_start(); // cross over point
198 char * area = (char *)memalign( a, s );
199 //sout | i | area;
200 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
201 abort( "memalign/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)s, area );
202 } // if
203 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte
204 area[malloc_usable_size( area ) - 1] = '\345'; // fill ultimate byte
205 free( area );
206 } // for
207 } // for
208
209 // check calloc/realloc/free (sbrk)
210
211 for ( i; 1 ~ 10_000 ~ 12 ) {
212 // initial N byte allocation
213 char * area = (char *)calloc( 5, i );
214 if ( area[0] != '\0' || area[i - 1] != '\0' ||
215 area[malloc_size( area ) - 1] != '\0' ||
216 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage1" );
217
218 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
219 for ( s; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request
220 area = (char *)realloc( area, s ); // attempt to reuse storage
221 if ( area[0] != '\0' || area[s - 1] != '\0' ||
222 area[malloc_size( area ) - 1] != '\0' ||
223 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage2" );
224 } // for
225 free( area );
226 } // for
227
228 // check calloc/realloc/free (mmap)
229
230 for ( i; 1 ~ 10_000 ~ 12 ) {
231 // initial N byte allocation
232 size_t s = i + default_mmap_start(); // cross over point
233 char * area = (char *)calloc( 1, s );
234 if ( area[0] != '\0' || area[s - 1] != '\0' ||
235 area[malloc_size( area ) - 1] != '\0' ||
236 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage3" );
237
238 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
239 for ( r; i ~ 256 * 1024 ~ 26 ) { // start at initial memory request
240 area = (char *)realloc( area, r ); // attempt to reuse storage
241 if ( area[0] != '\0' || area[r - 1] != '\0' ||
242 area[malloc_size( area ) - 1] != '\0' ||
243 ! malloc_zero_fill( area ) ) abort( "calloc/realloc/free corrupt storage4" );
244 } // for
245 free( area );
246 } // for
247
248 // check memalign/realloc/free
249
250 amount = 2;
251 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
252 // initial N byte allocation
253 char * area = (char *)memalign( a, amount ); // aligned N-byte allocation
254 //sout | alignments[a] | area;
255 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
256 abort( "memalign/realloc/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
257 } // if
258 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
259
260 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
261 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
262 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" );
263 area = (char *)realloc( area, s ); // attempt to reuse storage
264 //sout | i | area;
265 if ( (size_t)area % a != 0 ) { // check for initial alignment
266 abort( "memalign/realloc/free bad alignment %p", area );
267 } // if
268 area[s - 1] = '\345'; // fill last byte
269 } // for
270 free( area );
271 } // for
272
273 // check cmemalign/free
274
275 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
276 //sout | alignments[a];
277 for ( s; 1 ~ limit ) { // allocation of size 0 can return null
278 char * area = (char *)cmemalign( a, 1, s );
279 //sout | i | area;
280 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
281 abort( "cmemalign/free bad alignment : cmemalign(%d,%d) = %p", (int)a, s, area );
282 } // if
283 if ( area[0] != '\0' || area[s - 1] != '\0' ||
284 area[malloc_size( area ) - 1] != '\0' ||
285 ! malloc_zero_fill( area ) ) abort( "cmemalign/free corrupt storage" );
286 area[0] = '\345'; area[s - 1] = '\345'; // fill first/last byte
287 free( area );
288 } // for
289 } // for
290
291 // check cmemalign/realloc/free
292
293 amount = 2;
294 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
295 // initial N byte allocation
296 char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation
297 //sout | alignments[a] | area;
298 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
299 abort( "cmemalign/realloc/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area );
300 } // if
301 if ( area[0] != '\0' || area[amount - 1] != '\0' ||
302 area[malloc_size( area ) - 1] != '\0' ||
303 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage1" );
304 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
305
306 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
307 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
308 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc/free corrupt storage2" );
309 area = (char *)realloc( area, s ); // attempt to reuse storage
310 //sout | i | area;
311 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
312 abort( "cmemalign/realloc/free bad alignment %p", area );
313 } // if
314 if ( area[0] != '\345' || area[s - 1] != '\0' ||
315 area[malloc_size( area ) - 1] != '\0' ||
316 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" );
317 area[s - 1] = '\345'; // fill last byte
318 } // for
319 free( area );
320 } // for
321
322 // check memalign/realloc with align/free
323
324 amount = 2;
325 for ( a; libAlign() ~= limit ~ a ) { // generate powers of 2
326 // initial N byte allocation
327 char * area = (char *)memalign( a, amount ); // aligned N-byte allocation
328 //sout | alignments[a] | area | endl;
329 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
330 abort( "memalign/realloc with align/free bad alignment : memalign(%d,%d) = %p", (int)a, (int)amount, area );
331 } // if
332 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
333
334 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
335 for ( s; amount ~ 256 * 1024 ) { // start at initial memory request
336 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "memalign/realloc/free corrupt storage" );
337 area = (char *)realloc( area, a * 2, s ); // attempt to reuse storage
338 //sout | i | area | endl;
339 if ( (size_t)area % a * 2 != 0 ) { // check for initial alignment
340 abort( "memalign/realloc with align/free bad alignment %p", area );
341 } // if
342 area[s - 1] = '\345'; // fill last byte
343 } // for
344 free( area );
345 } // for
346
347 // check cmemalign/realloc with align/free
348
349 amount = 2;
350 for ( size_t a = libAlign() + libAlign(); a <= limit; a += a ) { // generate powers of 2
351 // initial N byte allocation
352 char * area = (char *)cmemalign( a, 1, amount ); // aligned N-byte allocation
353 //sout | alignments[a] | area | endl;
354 if ( (size_t)area % a != 0 || malloc_alignment( area ) != a ) { // check for initial alignment
355 abort( "cmemalign/realloc with align/free bad alignment : cmemalign(%d,%d) = %p", (int)a, (int)amount, area );
356 } // if
357 if ( area[0] != '\0' || area[amount - 1] != '\0' ||
358 area[malloc_size( area ) - 1] != '\0' ||
359 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc with align/free corrupt storage1" );
360 area[0] = '\345'; area[amount - 2] = '\345'; // fill first/penultimate byte
361
362 // Do not start this loop index at 0 because realloc of 0 bytes frees the storage.
363 for ( int s = amount; s < 256 * 1024; s += 1 ) { // start at initial memory request
364 if ( area[0] != '\345' || area[s - 2] != '\345' ) abort( "cmemalign/realloc with align/free corrupt storage2" );
365 area = (char *)realloc( area, a * 2, s ); // attempt to reuse storage
366 //sout | i | area | endl;
367 if ( (size_t)area % a * 2 != 0 || malloc_alignment( area ) != a * 2 ) { // check for initial alignment
368 abort( "cmemalign/realloc with align/free bad alignment %p %zd %zd", area, malloc_alignment( area ), a * 2 );
369 } // if
370 if ( area[s - 1] != '\0' || area[s - 1] != '\0' ||
371 area[malloc_size( area ) - 1] != '\0' ||
372 ! malloc_zero_fill( area ) ) abort( "cmemalign/realloc/free corrupt storage3" );
373 area[s - 1] = '\345'; // fill last byte
374 } // for
375 free( area );
376 } // for
377
378 //sout | "worker" | thisTask() | "successful completion";
379} // Worker main
380
381int main() {
382 const unsigned int NoOfWorkers = 4;
383 {
384 processor processors[NoOfWorkers - 1] __attribute__(( unused )); // more than one processor
385 Worker workers[NoOfWorkers] __attribute__(( unused ));
386 }
387 // checkFreeOn();
388 // malloc_stats();
389}
390
391// Local Variables: //
392// tab-width: 4 //
393// compile-command: "cfa -nodebug -O2 heap.cfa" //
394// End: //
Note: See TracBrowser for help on using the repository browser.