xref: /openbsd-src/regress/lib/libc/malloc/malloc_general/malloc_general.c (revision af97903fb555373f4af4abf3f3808148e699bb6f)
1 /*	$OpenBSD: malloc_general.c,v 1.7 2022/01/09 07:18:50 otto Exp $	*/
2 /*
3  * Copyright (c) 2017 Otto Moerbeek <otto@drijf.net>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <err.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 
23 /* #define VERBOSE */
24 
25 #define N 1000
26 
27 size_t
size(void)28 size(void)
29 {
30 	int p = arc4random_uniform(17) + 3;
31 	return arc4random_uniform(1 << p);
32 }
33 
34 struct { void *p; size_t sz; } a[N];
35 
36 void
fill(u_char * p,size_t sz)37 fill(u_char *p, size_t sz)
38 {
39 	size_t i;
40 
41 	for (i = 0; i < sz; i++)
42 		p[i] = i % 256;
43 }
44 
45 void
check(u_char * p,size_t sz)46 check(u_char *p, size_t sz)
47 {
48 	size_t i;
49 
50 	for (i = 0; i < sz; i++)
51 		if (p[i] != i % 256)
52 			errx(1, "check");
53 }
54 
55 int
main(int argc,char * argv[])56 main(int argc, char *argv[])
57 {
58 	int count, p, r, i;
59 	void * q;
60 	size_t sz;
61 
62 	for (count = 0; count < 800000; count++) {
63 		if (count % 10000 == 0) {
64 			printf(".");
65 			fflush(stdout);
66 		}
67 		p = arc4random_uniform(2);
68 		i = arc4random_uniform(N);
69 		switch (p) {
70 		case 0:
71 			if (a[i].p) {
72 #ifdef VERBOSE
73 				printf("F %p\n", a[i].p);
74 #endif
75 				if (a[i].p)
76 					check(a[i].p, a[i].sz);
77 				free(a[i].p);
78 				a[i].p = NULL;
79 			}
80 			sz = size();
81 #ifdef VERBOSE
82 			printf("M %zu=", sz);
83 #endif
84 			r = arc4random_uniform(2);
85 			a[i].p = r == 0 ? malloc_conceal(sz) : malloc(sz);
86 			a[i].sz = sz;
87 #ifdef VERBOSE
88 			printf("%p\n", a[i].p);
89 #endif
90 			if (a[i].p)
91 				fill(a[i].p, sz);
92 			break;
93 		case 1:
94 			sz = size();
95 #ifdef VERBOSE
96 			printf("R %p %zu=", a[i].p, sz);
97 #endif
98 			q = realloc(a[i].p, sz);
99 #ifdef VERBOSE
100 			printf("%p\n", q);
101 #endif
102 			if (a[i].p && q)
103 				check(q, a[i].sz < sz ? a[i].sz : sz);
104 			if (q) {
105 				a[i].p = q;
106 				a[i].sz = sz;
107 				fill(a[i].p, sz);
108 			}
109 			break;
110 		}
111 	}
112 	for (i = 0; i < N; i++) {
113 		if (a[i].p)
114 			check(a[i].p, a[i].sz);
115 		r = arc4random_uniform(2);
116 		if (r)
117 			free(a[i].p);
118 		else
119 			freezero(a[i].p, a[i].sz);
120 	}
121 	printf("\n");
122 	return 0;
123 }
124