xref: /openbsd-src/regress/lib/libc/malloc/malloc_general/malloc_general.c (revision e603c72f713dd59b67030a9b97ec661800da159e)
1 /*	$OpenBSD	*/
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
28 size(void)
29 {
30 	int p = arc4random_uniform(13) + 3;
31 	return arc4random_uniform(1 << p);
32 }
33 
34 struct { void *p; size_t sz; } a[N];
35 
36 extern char *malloc_options;
37 
38 void
39 fill(u_char *p, size_t sz)
40 {
41 	size_t i;
42 
43 	for (i = 0; i < sz; i++)
44 		p[i] = i % 256;
45 }
46 
47 void
48 check(u_char *p, size_t sz)
49 {
50 	size_t i;
51 
52 	for (i = 0; i < sz; i++)
53 		if (p[i] != i % 256)
54 			errx(1, "check");
55 }
56 
57 int
58 main(int argc, char *argv[])
59 {
60 	int count, p, r, i;
61 	void * q;
62 	size_t sz;
63 	char mo[20];
64 
65 	if (argc == 1)
66 		errx(1, "usage: malloc_options");
67 
68 	/* first reset flags that might be set by env or sysctl */
69 	strlcpy(mo, "cfgju", sizeof(mo));
70 	strlcat(mo, argv[1], sizeof(mo));
71 	malloc_options = mo;
72 
73 	for (count = 0; count < 800000; count++) {
74 		if (count % 10000 == 0) {
75 			printf(".");
76 			fflush(stdout);
77 		}
78 		p = arc4random_uniform(2);
79 		i = arc4random_uniform(N);
80 		switch (p) {
81 		case 0:
82 			if (a[i].p) {
83 #ifdef VERBOSE
84 				printf("F %p\n", a[i].p);
85 #endif
86 				if (a[i].p)
87 					check(a[i].p, a[i].sz);
88 				free(a[i].p);
89 				a[i].p = NULL;
90 			}
91 			sz = size();
92 #ifdef VERBOSE
93 			printf("M %zu=", sz);
94 #endif
95 			r = arc4random_uniform(2);
96 			a[i].p = r == 0 ? malloc_conceal(sz) : malloc(sz);
97 			a[i].sz = sz;
98 #ifdef VERBOSE
99 			printf("%p\n", a[i].p);
100 #endif
101 			if (a[i].p)
102 				fill(a[i].p, sz);
103 			break;
104 		case 1:
105 			sz = size();
106 #ifdef VERBOSE
107 			printf("R %p %zu=", a[i].p, sz);
108 #endif
109 			q = realloc(a[i].p, sz);
110 #ifdef VERBOSE
111 			printf("%p\n", q);
112 #endif
113 			if (a[i].p && q)
114 				check(q, a[i].sz < sz ? a[i].sz : sz);
115 			if (q) {
116 				a[i].p = q;
117 				a[i].sz = sz;
118 				fill(a[i].p, sz);
119 			}
120 			break;
121 		}
122 	}
123 	for (i = 0; i < N; i++) {
124 		if (a[i].p)
125 			check(a[i].p, a[i].sz);
126 		r = arc4random_uniform(2);
127 		if (r)
128 			free(a[i].p);
129 		else
130 			freezero(a[i].p, a[i].sz);
131 	}
132 	printf("\n");
133 	return 0;
134 }
135