t_posix_memalign.c revision 1.6 1 /* $NetBSD: t_posix_memalign.c,v 1.6 2023/07/04 15:06:36 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34 The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_posix_memalign.c,v 1.6 2023/07/04 15:06:36 riastradh Exp $");
36
37 #include <atf-c.h>
38
39 #include <errno.h>
40 #include <stdbool.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #define rounddown(x, n) (((x) / (n)) * (n))
47
48 ATF_TC(posix_memalign_basic);
49 ATF_TC_HEAD(posix_memalign_basic, tc)
50 {
51 atf_tc_set_md_var(tc, "descr", "Checks posix_memalign(3)");
52 }
53 ATF_TC_BODY(posix_memalign_basic, tc)
54 {
55 enum { maxaligntest = 16384 };
56 static const size_t align[] = {
57 0, 1, 2, 3, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
58 8192, maxaligntest,
59 };
60 static const size_t size[] = {
61 0, 1, 2, 3, 4, 10, 100, 10000, 16384, 32768, 65536,
62 rounddown(SIZE_MAX, maxaligntest),
63 };
64 size_t i, j;
65
66 for (i = 0; i < __arraycount(align); i++) {
67 for (j = 0; j < __arraycount(size); j++) {
68 void *p = (void *)0x1;
69 const int ret = posix_memalign(&p, align[i], size[j]);
70
71 if (align[i] == 0 ||
72 (align[i] & (align[i] - 1)) != 0 ||
73 align[i] < sizeof(void *)) {
74 ATF_CHECK_EQ_MSG(ret, EINVAL,
75 "posix_memalign(&p, %zu, %zu): %s",
76 align[i], size[j], strerror(ret));
77 continue;
78 }
79 if (size[j] == rounddown(SIZE_MAX, maxaligntest) &&
80 ret != EINVAL) {
81 /*
82 * If obscenely large alignment isn't
83 * rejected as EINVAL, we can't
84 * allocate that much memory anyway.
85 */
86 ATF_CHECK_EQ_MSG(ret, ENOMEM,
87 "posix_memalign(&p, %zu, %zu): %s",
88 align[i], size[j], strerror(ret));
89 continue;
90 }
91
92 /*
93 * Allocation should fail only if the alignment
94 * isn't supported, in which case it will fail
95 * with EINVAL. No standard criterion for what
96 * alignments are supported, so just stop here
97 * on EINVAL.
98 */
99 if (ret == EINVAL)
100 continue;
101
102 ATF_CHECK_EQ_MSG(ret, 0,
103 "posix_memalign(&p, %zu, %zu): %s",
104 align[i], size[j], strerror(ret));
105 ATF_CHECK_EQ_MSG((intptr_t)p & (align[i] - 1), 0,
106 "posix_memalign(&p, %zu, %zu): %p",
107 align[i], size[j], p);
108
109 if (size[j] != 0) {
110 if (p == NULL) {
111 atf_tc_fail_nonfatal(
112 "%s:%d:"
113 "posix_memalign(&p, %zu, %zu):"
114 " %p",
115 __FILE__, __LINE__,
116 align[i], size[j], p);
117 }
118 } else {
119 /*
120 * No guarantees about whether
121 * zero-size allocation yields null
122 * pointer or something else.
123 */
124 }
125
126 free(p);
127 }
128 }
129 }
130
131
132 ATF_TC(aligned_alloc_basic);
133 ATF_TC_HEAD(aligned_alloc_basic, tc)
134 {
135 atf_tc_set_md_var(tc, "descr", "Checks aligned_alloc(3)");
136 }
137 ATF_TC_BODY(aligned_alloc_basic, tc)
138 {
139 enum { maxaligntest = 16384 };
140 static const size_t align[] = {
141 0, 1, 2, 3, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
142 8192, maxaligntest,
143 };
144 static const size_t size[] = {
145 0, 1, 2, 3, 4, 10, 100, 10000, 16384, 32768, 65536,
146 rounddown(SIZE_MAX, maxaligntest),
147 };
148 size_t i, j;
149
150 for (i = 0; i < __arraycount(align); i++) {
151 for (j = 0; j < __arraycount(size); j++) {
152 void *const p = aligned_alloc(align[i], size[j]);
153
154 /*
155 * C11, 6.2.8 Alignment of objects, paragraph
156 * 4, p. 48:
157 *
158 * Every valid alignment value shall be a
159 * nonnegative integral power of two.
160 *
161 * C11, 7.22.3.1 The aligned_alloc function,
162 * paragraph 2, p. 348:
163 *
164 * The value of alignment shall be a valid
165 * alignment supported by the
166 * implementation and the value of size
167 * shall be an integral multiple of
168 * alignment.
169 *
170 * Setting errno to EINVAL is a NetBSD
171 * extension. The last clause appears to rule
172 * out aligned_alloc(n, 0) for any n, but it's
173 * not clear.
174 */
175 if (align[i] == 0 ||
176 (align[i] & (align[i] - 1)) != 0 ||
177 (size[j] != 0 && size[j] % align[i] != 0)) {
178 if (p != NULL) {
179 ATF_CHECK_EQ_MSG(p, NULL,
180 "aligned_alloc(%zu, %zu): %p",
181 align[i], size[j], p);
182 continue;
183 }
184 ATF_CHECK_EQ_MSG(errno, EINVAL,
185 "aligned_alloc(%zu, %zu): %s",
186 align[i], size[j], strerror(errno));
187 continue;
188 }
189
190 if (size[j] == rounddown(SIZE_MAX, maxaligntest)) {
191 ATF_CHECK_EQ_MSG(p, NULL,
192 "aligned_alloc(%zu, %zu): %p, %s",
193 align[i], size[j], p, strerror(errno));
194 switch (errno) {
195 case EINVAL:
196 case ENOMEM:
197 break;
198 default:
199 atf_tc_fail_nonfatal(
200 "%s:%d:"
201 " aligned_alloc(%zu, %zu): %s",
202 __FILE__, __LINE__,
203 align[i], size[j],
204 strerror(errno));
205 break;
206 }
207 continue;
208 }
209
210 /*
211 * Allocation should fail only if the alignment
212 * isn't supported, in which case it will fail
213 * with EINVAL. No standard criterion for what
214 * alignments are supported, so just stop here
215 * on EINVAL.
216 */
217 if (p == NULL && errno == EINVAL)
218 continue;
219
220 ATF_CHECK_EQ_MSG((intptr_t)p & (align[i] - 1), 0,
221 "aligned_alloc(%zu, %zu): %p",
222 align[i], size[j], p);
223 if (size[j] != 0) {
224 if (p == NULL) {
225 atf_tc_fail_nonfatal(
226 "%s:%d:"
227 " aligned_alloc(&p, %zu, %zu):"
228 " %p, %s",
229 __FILE__, __LINE__,
230 align[i], size[j], p,
231 strerror(errno));
232 }
233 } else {
234 /*
235 * No guarantees about whether
236 * zero-size allocation yields null
237 * pointer or something else.
238 */
239 }
240
241 free(p);
242 }
243 }
244 }
245
246
247 ATF_TP_ADD_TCS(tp)
248 {
249
250 ATF_TP_ADD_TC(tp, posix_memalign_basic);
251 ATF_TP_ADD_TC(tp, aligned_alloc_basic);
252
253 return atf_no_error();
254 }
255