1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright © 2020 Keith Packard
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials provided
16  *    with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #define _DEFAULT_SOURCE
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <string.h>
41 #include <malloc.h>
42 #include <stdlib.h>
43 #include <stdint.h>
44 
45 #ifndef __clang__
46 #pragma GCC diagnostic ignored "-Walloc-size-larger-than="
47 #pragma GCC diagnostic ignored "-Wanalyzer-use-of-uninitialized-value"
48 #endif
49 
50 int
main(void)51 main(void)
52 {
53         void *r, *q;
54 	int result = 0;
55 	int err, pow;
56 
57 	errno = 0;
58 	r = malloc(0);
59         printf("malloc(0): %p\n", r);
60 	if (errno != 0) {
61 		printf("malloc(0) failed: %s. got %p\n", strerror(errno), r);
62 		result++;
63 	}
64 	free(r);
65 
66 	r = memalign(128, 237);
67         printf("memalign(128, 237): %p\n", r);
68 	if ((uintptr_t) r & 127) {
69 		printf("memalign(128, 237) unaligned (%p)\n", r);
70 		result++;
71 	}
72 	free(r);
73 
74 	r = NULL;
75 	err = posix_memalign(&r, 128, 237);
76 	printf("posix_memalign(128, 237): %p, err=%d\n", r, err);
77 	if ((uintptr_t) r & 127) {
78 		printf("posix_memalign(128, 237) unaligned (%p)\n", r);
79 		err++;
80 	}
81 	free(r);
82 
83 	r = NULL;
84 	err = posix_memalign(&r, 0, 237);
85 	printf("posix_memalign(0, 237): %p, err=%d\n", r, err);
86 	if (err != EINVAL) {
87 		printf("posix_memalign(0, 237) should return EINVAL\n");
88 		err++;
89 	}
90 	free(r);
91 
92 	r = NULL;
93 	err = posix_memalign(&r, 129, 237);
94 	printf("posix_memalign(129, 237): %p, err=%d\n", r, err);
95 	if (err != EINVAL) {
96 		printf("posix_memalign(129, 237) should return EINVAL\n");
97 		err++;
98 	}
99 	free(r);
100 
101 	r = NULL;
102 	err = posix_memalign(&r, 128, PTRDIFF_MAX);
103 	printf("posix_memalign(128, PTRDIFF_MAX): %p, err=%d\n", r, err);
104 	if (err != ENOMEM) {
105 		printf("posix_memalign(128, PTRDIFF_MAX) should return ENOMEM\n");
106 		err++;
107 	}
108 	free(r);
109 
110 	errno = 0;
111 	r = malloc(PTRDIFF_MAX);
112         q = NULL;
113         if (r)
114                 q = malloc(PTRDIFF_MAX);
115         printf("malloc(PTRDIFF_MAX: %p %p\n", r, q);
116 	if ((r && q) || errno != ENOMEM) {
117                 printf("2*malloc(PTRDIFF_MAX) should have failed. got %p,%p error %s\n", r, q, strerror(errno));
118 		result++;
119 	}
120         free(r);
121         free(q);
122 
123 	errno = 0;
124 	r = malloc(SIZE_MAX);
125         printf("malloc(SIZE_MAX): %p\n", r);
126 	if (r || errno != ENOMEM) {
127 		printf("malloc(SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno));
128 		result++;
129 	}
130 
131 	errno = 0;
132 	r = calloc(1, SIZE_MAX);
133         printf("calloc(1, SIZE_MAX): %p\n", r);
134 	if (r || errno != ENOMEM) {
135 		printf("calloc(1, SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno));
136 		result++;
137 	}
138 
139 	errno = 0;
140 	r = reallocarray(NULL, 1, SIZE_MAX);
141         printf("reallocarray(NULL, 1, SIZE_MAX): %p\n", r);
142 	if (r || errno != ENOMEM) {
143 		printf("reallocarray(NULL, 1, SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno));
144 		result++;
145 	}
146 
147 	for (pow = 0; pow < 4; pow++) {
148 		errno = 0;
149 		r = calloc(SIZE_MAX >> pow, SIZE_MAX >> pow);
150                 printf("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n", pow, pow, r);
151 		if (r || errno != ENOMEM) {
152 			printf("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d) should have failed. got %p error %s\n", pow, pow, r, strerror(errno));
153 			result++;
154 		}
155                 free(r);
156 		r = reallocarray(NULL, SIZE_MAX >> pow, SIZE_MAX >> pow);
157                 printf("reallocarray(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n", pow, pow, r);
158 		if (r || errno != ENOMEM) {
159 			printf("reallocarray(NULL, SIZE_MAX >> %d, SIZE_MAX >> %d) should have failed. got %p error %s\n", pow, pow, r, strerror(errno));
160 			result++;
161 		}
162                 free(r);
163 	}
164 
165 	/* make sure realloc doesn't read past the source */
166 
167 	void *big = malloc(1024);
168         printf("big %p\n", big);
169 	if (big) {
170                 memset(big, '1', 1024);
171 		void *small = malloc(128);
172                 printf("small %p\n", small);
173 		if (small) {
174                         memset(small, '2', 128);
175                         (void) atoi(small);
176 			free(big);
177 			char *med = realloc(small, 1024);
178 			if (med) {
179                                 printf("med %p\n", med);
180 #ifdef _NANO_MALLOC
181 				int i;
182 				for (i = 128; i < 1024; i++)
183 					if (med[i] != 0) {
184 						printf("looks like realloc read past old at %d (saw %d)\n", i, med[i]);
185 						++result;
186 					}
187 #endif
188 				free(med);
189 				small = NULL;
190 			}
191 			free (small);
192 		}
193 	}
194 
195 	malloc_stats();
196 
197 	return result;
198 }
199