xref: /openbsd-src/regress/lib/libc/malloc/malloc_general/malloc_general.c (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
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, 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 			a[i].p = malloc(sz);
96 			a[i].sz = sz;
97 #ifdef VERBOSE
98 			printf("%p\n", a[i].p);
99 #endif
100 			if (a[i].p)
101 				fill(a[i].p, sz);
102 			break;
103 		case 1:
104 			sz = size();
105 #ifdef VERBOSE
106 			printf("R %p %zu=", a[i].p, sz);
107 #endif
108 			q = realloc(a[i].p, sz);
109 #ifdef VERBOSE
110 			printf("%p\n", q);
111 #endif
112 			if (a[i].p && q)
113 				check(q, a[i].sz < sz ? a[i].sz : sz);
114 			if (q) {
115 				a[i].p = q;
116 				a[i].sz = sz;
117 				fill(a[i].p, sz);
118 			}
119 			break;
120 		}
121 	}
122 	for (i = 0; i < N; i++) {
123 		if (a[i].p)
124 			check(a[i].p, a[i].sz);
125 		free(a[i].p);
126 	}
127 	printf("\n");
128 	return 0;
129 }
130