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