Home | History | Annotate | Line # | Download | only in stdlib
t_posix_memalign.c revision 1.5.10.1
      1  1.5.10.1    martin /*	$NetBSD: t_posix_memalign.c,v 1.5.10.1 2024/08/24 16:24:04 martin 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.5.10.1    martin __RCSID("$NetBSD: t_posix_memalign.c,v 1.5.10.1 2024/08/24 16:24:04 martin 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.5.10.1    martin #define	rounddown(x, n)	(((x) / (n)) * (n))
     47  1.5.10.1    martin 
     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.5.10.1    martin 	enum { maxaligntest = 16384 };
     56       1.4      nros 	static const size_t align[] = {
     57  1.5.10.1    martin 		0, 1, 2, 3, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
     58  1.5.10.1    martin 		8192, maxaligntest,
     59  1.5.10.1    martin 	};
     60  1.5.10.1    martin 	static const size_t size[] = {
     61  1.5.10.1    martin 		0, 1, 2, 3, 4, 10, 100, 10000, 16384, 32768, 65536,
     62  1.5.10.1    martin 		rounddown(SIZE_MAX, maxaligntest),
     63       1.1  pgoyette 	};
     64  1.5.10.1    martin 	size_t i, j;
     65       1.1  pgoyette 
     66  1.5.10.1    martin 	for (i = 0; i < __arraycount(align); i++) {
     67  1.5.10.1    martin 		for (j = 0; j < __arraycount(size); j++) {
     68  1.5.10.1    martin 			void *p = (void *)0x1;
     69  1.5.10.1    martin 			const int ret = posix_memalign(&p, align[i], size[j]);
     70  1.5.10.1    martin 
     71  1.5.10.1    martin 			if (align[i] == 0 ||
     72  1.5.10.1    martin 			    (align[i] & (align[i] - 1)) != 0 ||
     73  1.5.10.1    martin 			    align[i] < sizeof(void *)) {
     74  1.5.10.1    martin 				ATF_CHECK_EQ_MSG(ret, EINVAL,
     75  1.5.10.1    martin 				    "posix_memalign(&p, %zu, %zu): %s",
     76  1.5.10.1    martin 				    align[i], size[j], strerror(ret));
     77  1.5.10.1    martin 				continue;
     78  1.5.10.1    martin 			}
     79  1.5.10.1    martin 			if (size[j] == rounddown(SIZE_MAX, maxaligntest) &&
     80  1.5.10.1    martin 			    ret != EINVAL) {
     81  1.5.10.1    martin 				/*
     82  1.5.10.1    martin 				 * If obscenely large alignment isn't
     83  1.5.10.1    martin 				 * rejected as EINVAL, we can't
     84  1.5.10.1    martin 				 * allocate that much memory anyway.
     85  1.5.10.1    martin 				 */
     86  1.5.10.1    martin 				ATF_CHECK_EQ_MSG(ret, ENOMEM,
     87  1.5.10.1    martin 				    "posix_memalign(&p, %zu, %zu): %s",
     88  1.5.10.1    martin 				    align[i], size[j], strerror(ret));
     89  1.5.10.1    martin 				continue;
     90  1.5.10.1    martin 			}
     91  1.5.10.1    martin 
     92  1.5.10.1    martin 			/*
     93  1.5.10.1    martin 			 * Allocation should fail only if the alignment
     94  1.5.10.1    martin 			 * isn't supported, in which case it will fail
     95  1.5.10.1    martin 			 * with EINVAL.  No standard criterion for what
     96  1.5.10.1    martin 			 * alignments are supported, so just stop here
     97  1.5.10.1    martin 			 * on EINVAL.
     98  1.5.10.1    martin 			 */
     99  1.5.10.1    martin 			if (ret == EINVAL)
    100  1.5.10.1    martin 				continue;
    101  1.5.10.1    martin 
    102  1.5.10.1    martin 			ATF_CHECK_EQ_MSG(ret, 0,
    103  1.5.10.1    martin 			    "posix_memalign(&p, %zu, %zu): %s",
    104  1.5.10.1    martin 			    align[i], size[j], strerror(ret));
    105  1.5.10.1    martin 			ATF_CHECK_EQ_MSG((intptr_t)p & (align[i] - 1), 0,
    106  1.5.10.1    martin 			    "posix_memalign(&p, %zu, %zu): %p",
    107  1.5.10.1    martin 			    align[i], size[j], p);
    108  1.5.10.1    martin 
    109  1.5.10.1    martin 			if (size[j] != 0) {
    110  1.5.10.1    martin 				if (p == NULL) {
    111  1.5.10.1    martin 					atf_tc_fail_nonfatal(
    112  1.5.10.1    martin 					    "%s:%d:"
    113  1.5.10.1    martin 					    "posix_memalign(&p, %zu, %zu):"
    114  1.5.10.1    martin 					    " %p",
    115  1.5.10.1    martin 					    __FILE__, __LINE__,
    116  1.5.10.1    martin 					    align[i], size[j], p);
    117  1.5.10.1    martin 				}
    118  1.5.10.1    martin 			} else {
    119  1.5.10.1    martin 				/*
    120  1.5.10.1    martin 				 * No guarantees about whether
    121  1.5.10.1    martin 				 * zero-size allocation yields null
    122  1.5.10.1    martin 				 * pointer or something else.
    123  1.5.10.1    martin 				 */
    124  1.5.10.1    martin 			}
    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.5.10.1    martin 	enum { maxaligntest = 16384 };
    140       1.3      nros 	static const size_t align[] = {
    141  1.5.10.1    martin 		0, 1, 2, 3, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096,
    142  1.5.10.1    martin 		8192, maxaligntest,
    143  1.5.10.1    martin 	};
    144  1.5.10.1    martin 	static const size_t size[] = {
    145  1.5.10.1    martin 		0, 1, 2, 3, 4, 10, 100, 10000, 16384, 32768, 65536,
    146  1.5.10.1    martin 		rounddown(SIZE_MAX, maxaligntest),
    147       1.3      nros 	};
    148  1.5.10.1    martin 	size_t i, j;
    149  1.5.10.1    martin 
    150  1.5.10.1    martin 	for (i = 0; i < __arraycount(align); i++) {
    151  1.5.10.1    martin 		for (j = 0; j < __arraycount(size); j++) {
    152  1.5.10.1    martin 			void *const p = aligned_alloc(align[i], size[j]);
    153  1.5.10.1    martin 
    154  1.5.10.1    martin 			/*
    155  1.5.10.1    martin 			 * C17, 6.2.8 Alignment of objects, paragraph
    156  1.5.10.1    martin 			 * 4, p. 37:
    157  1.5.10.1    martin 			 *
    158  1.5.10.1    martin 			 *	Every valid alignment value shall be a
    159  1.5.10.1    martin 			 *	nonnegative integral power of two.
    160  1.5.10.1    martin 			 *
    161  1.5.10.1    martin 			 * C17, 7.22.3.1 The aligned_alloc function,
    162  1.5.10.1    martin 			 * paragraph 2, p. 348:
    163  1.5.10.1    martin 			 *
    164  1.5.10.1    martin 			 *	If the value of alignment is not a
    165  1.5.10.1    martin 			 *	valid alignment supported by the
    166  1.5.10.1    martin 			 *	implementation the function shall fail
    167  1.5.10.1    martin 			 *	by returning a null pointer.
    168  1.5.10.1    martin 			 *
    169  1.5.10.1    martin 			 * Setting errno to EINVAL is a NetBSD
    170  1.5.10.1    martin 			 * extension.  The last clause appears to rule
    171  1.5.10.1    martin 			 * out aligned_alloc(n, 0) for any n, but it's
    172  1.5.10.1    martin 			 * not clear.
    173  1.5.10.1    martin 			 */
    174  1.5.10.1    martin 			if (align[i] == 0 ||
    175  1.5.10.1    martin 			    (align[i] & (align[i] - 1)) != 0) {
    176  1.5.10.1    martin 				if (p != NULL) {
    177  1.5.10.1    martin 					ATF_CHECK_EQ_MSG(p, NULL,
    178  1.5.10.1    martin 					    "aligned_alloc(%zu, %zu): %p",
    179  1.5.10.1    martin 					    align[i], size[j], p);
    180  1.5.10.1    martin 					continue;
    181  1.5.10.1    martin 				}
    182  1.5.10.1    martin 				ATF_CHECK_EQ_MSG(errno, EINVAL,
    183  1.5.10.1    martin 				    "aligned_alloc(%zu, %zu): %s",
    184  1.5.10.1    martin 				    align[i], size[j], strerror(errno));
    185  1.5.10.1    martin 				continue;
    186  1.5.10.1    martin 			}
    187       1.3      nros 
    188  1.5.10.1    martin 			if (size[j] == rounddown(SIZE_MAX, maxaligntest)) {
    189  1.5.10.1    martin 				ATF_CHECK_EQ_MSG(p, NULL,
    190  1.5.10.1    martin 				    "aligned_alloc(%zu, %zu): %p, %s",
    191  1.5.10.1    martin 				    align[i], size[j], p, strerror(errno));
    192  1.5.10.1    martin 				switch (errno) {
    193  1.5.10.1    martin 				case EINVAL:
    194  1.5.10.1    martin 				case ENOMEM:
    195  1.5.10.1    martin 					break;
    196  1.5.10.1    martin 				default:
    197  1.5.10.1    martin 					atf_tc_fail_nonfatal(
    198  1.5.10.1    martin 					    "%s:%d:"
    199  1.5.10.1    martin 					    " aligned_alloc(%zu, %zu): %s",
    200  1.5.10.1    martin 					    __FILE__, __LINE__,
    201  1.5.10.1    martin 					    align[i], size[j],
    202  1.5.10.1    martin 					    strerror(errno));
    203  1.5.10.1    martin 					break;
    204  1.5.10.1    martin 				}
    205  1.5.10.1    martin 				continue;
    206  1.5.10.1    martin 			}
    207  1.5.10.1    martin 
    208  1.5.10.1    martin 			/*
    209  1.5.10.1    martin 			 * Allocation should fail only if the alignment
    210  1.5.10.1    martin 			 * isn't supported, in which case it will fail
    211  1.5.10.1    martin 			 * with EINVAL.  No standard criterion for what
    212  1.5.10.1    martin 			 * alignments are supported, so just stop here
    213  1.5.10.1    martin 			 * on EINVAL.
    214  1.5.10.1    martin 			 */
    215  1.5.10.1    martin 			if (p == NULL && errno == EINVAL)
    216  1.5.10.1    martin 				continue;
    217  1.5.10.1    martin 
    218  1.5.10.1    martin 			ATF_CHECK_EQ_MSG((intptr_t)p & (align[i] - 1), 0,
    219  1.5.10.1    martin 			    "aligned_alloc(%zu, %zu): %p",
    220  1.5.10.1    martin 			    align[i], size[j], p);
    221  1.5.10.1    martin 			if (size[j] != 0) {
    222  1.5.10.1    martin 				if (p == NULL) {
    223  1.5.10.1    martin 					atf_tc_fail_nonfatal(
    224  1.5.10.1    martin 					    "%s:%d:"
    225  1.5.10.1    martin 					    " aligned_alloc(&p, %zu, %zu):"
    226  1.5.10.1    martin 					    " %p, %s",
    227  1.5.10.1    martin 					    __FILE__, __LINE__,
    228  1.5.10.1    martin 					    align[i], size[j], p,
    229  1.5.10.1    martin 					    strerror(errno));
    230  1.5.10.1    martin 				}
    231  1.5.10.1    martin 			} else {
    232  1.5.10.1    martin 				/*
    233  1.5.10.1    martin 				 * No guarantees about whether
    234  1.5.10.1    martin 				 * zero-size allocation yields null
    235  1.5.10.1    martin 				 * pointer or something else.
    236  1.5.10.1    martin 				 */
    237  1.5.10.1    martin 			}
    238       1.3      nros 
    239       1.3      nros 			free(p);
    240       1.3      nros 		}
    241       1.3      nros 	}
    242       1.3      nros }
    243       1.3      nros 
    244       1.3      nros 
    245       1.1  pgoyette ATF_TP_ADD_TCS(tp)
    246       1.1  pgoyette {
    247  1.5.10.1    martin 
    248       1.2    jruoho 	ATF_TP_ADD_TC(tp, posix_memalign_basic);
    249  1.5.10.1    martin 	ATF_TP_ADD_TC(tp, aligned_alloc_basic);
    250  1.5.10.1    martin 
    251       1.1  pgoyette 	return atf_no_error();
    252       1.1  pgoyette }
    253