diff options
Diffstat (limited to 'lib/libc')
139 files changed, 5685 insertions, 836 deletions
diff --git a/lib/libc/Makefile b/lib/libc/Makefile index d0c254e33396..34474cfa9fe4 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -109,7 +109,6 @@ NOASM= .include "${LIBC_SRCTOP}/inet/Makefile.inc" .include "${LIBC_SRCTOP}/isc/Makefile.inc" .include "${LIBC_SRCTOP}/locale/Makefile.inc" -.include "${LIBC_SRCTOP}/md/Makefile.inc" .include "${LIBC_SRCTOP}/nameser/Makefile.inc" .include "${LIBC_SRCTOP}/net/Makefile.inc" .include "${LIBC_SRCTOP}/nls/Makefile.inc" @@ -119,6 +118,7 @@ NOASM= .endif .include "${LIBC_SRCTOP}/regex/Makefile.inc" .include "${LIBC_SRCTOP}/resolv/Makefile.inc" +.include "${LIBC_SRCTOP}/stdbit/Makefile.inc" .include "${LIBC_SRCTOP}/stdio/Makefile.inc" .include "${LIBC_SRCTOP}/stdlib/Makefile.inc" .include "${LIBC_SRCTOP}/stdtime/Makefile.inc" diff --git a/lib/libc/Versions.def b/lib/libc/Versions.def index 184e107d225a..1c7b34bef35b 100644 --- a/lib/libc/Versions.def +++ b/lib/libc/Versions.def @@ -42,6 +42,10 @@ FBSD_1.7 { FBSD_1.8 { } FBSD_1.7; +# This version was first added to 16.0-current. +FBSD_1.9 { +} FBSD_1.8; + # This is our private namespace. Any global interfaces that are # strictly for use only by other FreeBSD applications and libraries # are listed here. We use a separate namespace so we can write @@ -49,4 +53,4 @@ FBSD_1.8 { # # Please do NOT increment the version of this namespace. FBSDprivate_1.0 { -} FBSD_1.8; +} FBSD_1.9; diff --git a/lib/libc/aarch64/string/timingsafe_memcmp.S b/lib/libc/aarch64/string/timingsafe_memcmp.S index 28fdd911a387..4cc10a152aa8 100644 --- a/lib/libc/aarch64/string/timingsafe_memcmp.S +++ b/lib/libc/aarch64/string/timingsafe_memcmp.S @@ -114,4 +114,4 @@ ENTRY(timingsafe_memcmp) csetm w0, lo csinc w0, w0, wzr, ls ret -END(timingsafe_bcmp) +END(timingsafe_memcmp) diff --git a/lib/libc/arm/gen/arm_drain_writebuf.2 b/lib/libc/arm/gen/arm_drain_writebuf.2 index 613c35d39ef2..7fa9eb460114 100644 --- a/lib/libc/arm/gen/arm_drain_writebuf.2 +++ b/lib/libc/arm/gen/arm_drain_writebuf.2 @@ -42,10 +42,7 @@ Not all hardware supports buffered writes; on such systems the .Nm function is a no-op. .Pp -On ARMv5 systems, this executes a cp15 coprocessor -.Dq drain write buffer -operation. -On ARMv6 and ARMv7 systems, this executes a +This executes a .Dq DSB SY synchronization barrier, followed by an L2 cache drain on systems where the DSB does not include L2 automatically. diff --git a/lib/libc/db/mpool/mpool.c b/lib/libc/db/mpool/mpool.c index 9dab032134bc..1bab66d73baf 100644 --- a/lib/libc/db/mpool/mpool.c +++ b/lib/libc/db/mpool/mpool.c @@ -455,7 +455,7 @@ mpool_stat(MPOOL *mp) (void)fprintf(stderr, "%lu pages in the file\n", mp->npages); (void)fprintf(stderr, - "page size %lu, cacheing %lu pages of %lu page max cache\n", + "page size %lu, caching %lu pages of %lu page max cache\n", mp->pagesize, mp->curcache, mp->maxcache); (void)fprintf(stderr, "%lu page puts, %lu page gets, %lu page new\n", mp->pageput, mp->pageget, mp->pagenew); diff --git a/lib/libc/gdtoa/_hdtoa.c b/lib/libc/gdtoa/_hdtoa.c index 8ae739acf0db..9c42630cd918 100644 --- a/lib/libc/gdtoa/_hdtoa.c +++ b/lib/libc/gdtoa/_hdtoa.c @@ -40,6 +40,7 @@ #define DBL_ADJ (DBL_MAX_EXP - 2) #define SIGFIGS ((DBL_MANT_DIG + 3) / 4 + 1) +#define MAX_HEX_DIGITS ((DBL_MANT_DIG + 3 - 1) / 4 + 1) static const float one[] = { 1.0f, -1.0f }; @@ -111,7 +112,7 @@ __hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign, s0 = rv_alloc(bufsize); /* Round to the desired number of digits. */ - if (SIGFIGS > ndigits && ndigits > 0) { + if (MAX_HEX_DIGITS > ndigits && ndigits > 0) { float redux = one[u.bits.sign]; int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG; u.bits.exp = offset; diff --git a/lib/libc/gdtoa/_hldtoa.c b/lib/libc/gdtoa/_hldtoa.c index 965d2349d103..5f10d12c5c09 100644 --- a/lib/libc/gdtoa/_hldtoa.c +++ b/lib/libc/gdtoa/_hldtoa.c @@ -65,6 +65,7 @@ typedef uint32_t manl_t; #define LDBL_ADJ (LDBL_MAX_EXP - 2) #define SIGFIGS ((LDBL_MANT_DIG + 3) / 4 + 1) +#define MAX_HEX_DIGITS ((LDBL_MANT_DIG + 3 - 1) / 4 + 1) static const float one[] = { 1.0f, -1.0f }; @@ -125,7 +126,7 @@ __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, s0 = rv_alloc(bufsize); /* Round to the desired number of digits. */ - if (SIGFIGS > ndigits && ndigits > 0) { + if (MAX_HEX_DIGITS > ndigits && ndigits > 0) { float redux = one[u.bits.sign]; int offset = 4 * ndigits + LDBL_MAX_EXP - 4 - LDBL_MANT_DIG; #ifdef __i386__ diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index 26f638568efc..494b65bc5cc1 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -193,7 +193,6 @@ FBSD_1.0 { __isinff; __isinfl; isatty; - initgroups; jrand48; lcong48; ldexp; @@ -462,6 +461,7 @@ FBSD_1.8 { fdscandir_b; fts_open_b; glob_b; + initgroups; inotify_add_watch; inotify_init; inotify_init1; diff --git a/lib/libc/gen/_rand48.c b/lib/libc/gen/_rand48.c index 990e2c86949b..114c1595b33d 100644 --- a/lib/libc/gen/_rand48.c +++ b/lib/libc/gen/_rand48.c @@ -13,34 +13,6 @@ #include "rand48.h" -unsigned short _rand48_seed[3] = { - RAND48_SEED_0, - RAND48_SEED_1, - RAND48_SEED_2 -}; -unsigned short _rand48_mult[3] = { - RAND48_MULT_0, - RAND48_MULT_1, - RAND48_MULT_2 -}; -unsigned short _rand48_add = RAND48_ADD; - -void -_dorand48(unsigned short xseed[3]) -{ - unsigned long accu; - unsigned short temp[2]; - - accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] + - (unsigned long) _rand48_add; - temp[0] = (unsigned short) accu; /* lower 16 bits */ - accu >>= sizeof(unsigned short) * 8; - accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] + - (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0]; - temp[1] = (unsigned short) accu; /* middle 16 bits */ - accu >>= sizeof(unsigned short) * 8; - accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0]; - xseed[0] = temp[0]; - xseed[1] = temp[1]; - xseed[2] = (unsigned short) accu; -} +uint48 _rand48_seed = RAND48_SEED; +uint48 _rand48_mult = RAND48_MULT; +uint48 _rand48_add = RAND48_ADD; diff --git a/lib/libc/gen/drand48.c b/lib/libc/gen/drand48.c index cec04a6a2425..f7f43ff20468 100644 --- a/lib/libc/gen/drand48.c +++ b/lib/libc/gen/drand48.c @@ -13,10 +13,10 @@ #include "rand48.h" -extern unsigned short _rand48_seed[3]; - double drand48(void) { - return erand48(_rand48_seed); + ERAND48_BEGIN; + _DORAND48(_rand48_seed); + ERAND48_END(_rand48_seed); } diff --git a/lib/libc/gen/erand48.c b/lib/libc/gen/erand48.c index 286904c27839..38d4774a9fe6 100644 --- a/lib/libc/gen/erand48.c +++ b/lib/libc/gen/erand48.c @@ -16,8 +16,9 @@ double erand48(unsigned short xseed[3]) { - _dorand48(xseed); - return ldexp((double) xseed[0], -48) + - ldexp((double) xseed[1], -32) + - ldexp((double) xseed[2], -16); + uint48 tmp; + + ERAND48_BEGIN; + DORAND48(tmp, xseed); + ERAND48_END(tmp); } diff --git a/lib/libc/gen/fts-compat.c b/lib/libc/gen/fts-compat.c index f87cabf085f7..62a1e0a81f62 100644 --- a/lib/libc/gen/fts-compat.c +++ b/lib/libc/gen/fts-compat.c @@ -44,9 +44,9 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> -#include "gen-compat.h" #include "fts-compat.h" #include "un-namespace.h" +#include "gen-compat.h" #include "gen-private.h" diff --git a/lib/libc/gen/fts-compat11.c b/lib/libc/gen/fts-compat11.c index 0351ce5ac690..5abb378f5f08 100644 --- a/lib/libc/gen/fts-compat11.c +++ b/lib/libc/gen/fts-compat11.c @@ -43,9 +43,9 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> -#include "gen-compat.h" #include "fts-compat11.h" #include "un-namespace.h" +#include "gen-compat.h" #include "gen-private.h" diff --git a/lib/libc/gen/fts.3 b/lib/libc/gen/fts.3 index ee558b892c8c..da304e59ee72 100644 --- a/lib/libc/gen/fts.3 +++ b/lib/libc/gen/fts.3 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd June 30, 2025 +.Dd October 6, 2025 .Dt FTS 3 .Os .Sh NAME @@ -69,14 +69,15 @@ on a file hierarchy, which is then supplied to the other .Nm functions. -The function +The .Fn fts_read -returns a pointer to a structure describing one of the files in the file -hierarchy. -The function +function returns a pointer to a structure describing one of the files +in the file hierarchy. +The .Fn fts_children -returns a pointer to a linked list of structures, each of which describes -one of the files contained in a directory in the hierarchy. +function returns a pointer to a linked list of structures, each of +which describes one of the files contained in a directory in the +hierarchy. In general, directories are visited two distinguishable times; in pre-order (before any of their descendants are visited) and in post-order (after all of their descendants have been visited). @@ -376,7 +377,44 @@ The .Fa fts_name field is always .Dv NUL Ns -terminated . -.Sh FTS_OPEN +.Ss Thread Safety +The +.Nm +functions can safely be used in multi-threaded programs provided no +two threads access the same +.Vt FTS +or +.Vt FTSENT +structure simultaneously. +However, unless the +.Dv FTS_NOCHDIR +flag was passed to +.Fn fts_open +or +.Fn fts_open_b , +calls to +.Fn fts_read +and +.Fn fts_children +may change the current working directory, which will affect all +threads. +Conversely, changing the current working directory either during or +between calls to +.Fn fts_read +or +.Fn fts_children +(even in a single-thread program) may cause +.Nm +to malfunction unless the +.Dv FTS_NOCHDIR +flag was passed to +.Fn fts_open +or +.Fn fts_open_b +and all paths in +.Va path_argv +were absolute. +.Ss Fn fts_open The .Fn fts_open function takes a pointer to an array of character pointers naming one @@ -507,10 +545,10 @@ from descending into directories that have a different device number than the file from which the descent began. .El .Pp -The argument -.Fn compar -specifies a user-defined function which may be used to order the traversal -of the hierarchy. +The +.Fa compar +argument points to a user-defined function which may be used to order +the traversal of the hierarchy. It takes two pointers to pointers to .Vt FTSENT @@ -545,7 +583,7 @@ the directory traversal order is in the order listed in .Fa path_argv for the root paths, and in the order listed in the directory for everything else. -.Sh FTS_OPEN_B +.Ss Fn fts_open_b The .Fn fts_open_b function is identical to @@ -554,7 +592,7 @@ except that it takes a block pointer instead of a function pointer. The block is copied before .Fn fts_open_b returns, so the original can safely go out of scope or be released. -.Sh FTS_READ +.Ss Fn fts_read The .Fn fts_read function returns a pointer to an @@ -588,6 +626,15 @@ structure is returned, and .Va errno may or may not have been set (see .Fa fts_info ) . +Note that +.Fn fts_read +will not set +.Va errno +to 0 if called again with the same +.Fa ftsp +argument after the +.Dv FTS_STOP +flag has been set or the end of the stream has been reached. .Pp The .Vt FTSENT @@ -602,10 +649,10 @@ directory, in which case they will not be overwritten until after a call to .Fn fts_read after the .Vt FTSENT -structure has been returned by the function +structure has been returned by the .Fn fts_read -in post-order. -.Sh FTS_CHILDREN +function in post-order. +.Ss Fn fts_children The .Fn fts_children function returns a pointer to an @@ -679,11 +726,11 @@ and .Fa fts_namelen fields. .El -.Sh FTS_SET -The function +.Ss Fn fts_set +The .Fn fts_set -allows the user application to determine further processing for the -file +function allows the user application to determine further processing +for the file .Fa f of the stream .Fa ftsp . @@ -749,7 +796,40 @@ The file may be one of those most recently returned by either or .Fn fts_read . .El -.Sh FTS_CLOSE +.Ss Fn fts_set_clientptr , Fn fts_get_clientptr +The +.Fn fts_set_clientptr +function sets the client data pointer for the stream +.Fa ftsp +to +.Fa clientdata . +The +.Fn fts_get_clientptr +function returns the client data pointer associated with +.Fa ftsp . +This can be used to pass per-stream data to the comparison function. +.Pp +For performance reasons, +.Fn fts_get_clientptr +may be shadowed by a preprocessor macro. +.Ss Fn fts_get_stream +The +.Fn fts_get_stream +function returns the +.Nm +stream associated with the file entry +.Fa f . +A typical use for this would be for a comparison function to first call +.Fn fts_get_stream +on one of its arguments, then call +.Fn fts_get_clientptr +to obtain the client data pointer, which in turn points to information +necessary to correctly order the two entries. +.Pp +For performance reasons, +.Fn fts_get_stream +may be shadowed by a preprocessor macro. +.Ss Fn fts_close The .Fn fts_close function closes a file hierarchy stream @@ -760,6 +840,75 @@ or .Fn fts_open_b was called to open .Fa ftsp . +.Sh RETURN VALUES +The +.Fn fts_open +and +.Fn fts_open_b +functions return a pointer to the new +.Nm +stream on success and +.Dv NULL +on failure. +.Pp +The +.Fn fts_read +function returns a pointer to the next file entry on success, or if an +error occurs that relates specifically to that file entry. +On reaching the end of the file hierarchy, it returns +.Dv NULL +and sets the external variable +.Va errno +to 0. +On failure, it returns +.Dv NULL +and sets +.Va errno +to an appropriate non-zero value. +If called again after the +.Dv FTS_STOP +flag has been set or the end of the stream has been reached, +.Fn fts_read +returns +.Dv NULL +and leaves +.Va errno +untouched. +.Pp +The +.Fn fts_children +function returns a pointer to a linked list of file entries on +success. +On reaching the end of the file hierarchy, it returns +.Dv NULL +and sets the external variable +.Va errno +to 0. +On failure, it returns +.Dv NULL +and sets +.Va errno +to an appropriate non-zero value. +.Pp +The +.Fn fts_set +function returns 0 on success and \-1 if its +.Fa instr +argument is invalid. +.Pp +The +.Fn fts_get_clientptr +function returns the client data pointer associated with its argument, +or +.Dv NULL +if none has been set. +.Pp +The +.Fn fts_get_stream +function returns a pointer to the +.Nm +stream associated with its argument. +.Pp The .Fn fts_close function @@ -816,7 +965,7 @@ functions may fail and set as follows: .Bl -tag -width Er .It Bq Er EINVAL -The options were invalid, or the list were empty. +The options were invalid, or the list was empty. .El .Sh SEE ALSO .Xr find 1 , diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c index cce959ba836a..4aa386d777cd 100644 --- a/lib/libc/gen/fts.c +++ b/lib/libc/gen/fts.c @@ -106,7 +106,6 @@ struct _fts_private { * This assumption only holds for UFS-like filesystems that implement * links and directories this way, so we must punt for others. */ - static const char *ufslike_filesystems[] = { "ufs", "zfs", @@ -679,7 +678,6 @@ fts_children(FTS *sp, int instr) void * (fts_get_clientptr)(FTS *sp) { - return (fts_get_clientptr(sp)); } @@ -696,7 +694,6 @@ FTS * void fts_set_clientptr(FTS *sp, void *clientptr) { - sp->fts_clientptr = clientptr; } diff --git a/lib/libc/gen/gen-compat.h b/lib/libc/gen/gen-compat.h index 08e80ede6b6e..19b9addb4321 100644 --- a/lib/libc/gen/gen-compat.h +++ b/lib/libc/gen/gen-compat.h @@ -40,16 +40,50 @@ struct freebsd11_statfs; struct freebsd11_dirent *freebsd11_readdir(DIR *); int freebsd11_readdir_r(DIR *, struct freebsd11_dirent *, struct freebsd11_dirent **); -int freebsd11_stat(const char *, struct freebsd11_stat *); -int freebsd11_lstat(const char *, struct freebsd11_stat *); -int freebsd11_fstat(int, struct freebsd11_stat *); -int freebsd11_fstatat(int, const char *, struct freebsd11_stat *, int); -int freebsd11_statfs(const char *, struct freebsd11_statfs *); -int freebsd11_getfsstat(struct freebsd11_statfs *, long, int); int freebsd11_getmntinfo(struct freebsd11_statfs **, int); char *freebsd11_devname(__uint32_t dev, __mode_t type); -char *freebsd11_devname_r(__uint32_t dev, __mode_t type, char *buf, int len); +char *freebsd11_devname_r(__uint32_t dev, __mode_t type, char *buf, + int len); + +/* + * We want freebsd11_fstat in C source to result in resolution to + * - fstat@FBSD_1.0 for libc.so (but we do not need the _definition_ + * of this fstat, it is provided by libsys.so which we want to use). + * - freebsd11_fstat for libc.a (since if we make it fstat@FBSD_1.0 + * for libc.a, then final linkage into static object ignores version + * and would reference fstat, which is the current syscall, not the + * compat syscall). libc.a provides the freebsd11_fstat implementation. + * Note that freebsd11_fstat from libc.a is not used for anything, but + * we make it correct nonetheless, just in case it would. + * This is arranged by COMPAT_SYSCALL, and libc can just use freebsd11_fstat. + */ +#ifdef PIC +#define COMPAT_SYSCALL(rtype, fun, args, sym, ver) \ + rtype fun args; __sym_compat(sym, fun, ver); +#else +#define COMPAT_SYSCALL(rtype, fun, args, sym, ver) \ + rtype fun args; +#endif + +COMPAT_SYSCALL(int, freebsd11_stat, (const char *, struct freebsd11_stat *), + stat, FBSD_1.0); +COMPAT_SYSCALL(int, freebsd11_lstat, (const char *, struct freebsd11_stat *), + lstat, FBSD_1.0); +COMPAT_SYSCALL(int, freebsd11_fstat, (int, struct freebsd11_stat *), + fstat, FBSD_1.0); +COMPAT_SYSCALL(int, freebsd11_fstatat, (int, const char *, + struct freebsd11_stat *, int), fstatat, FBSD_1.1); + +COMPAT_SYSCALL(int, freebsd11_statfs, (const char *, + struct freebsd11_statfs *), statfs, FBSD_1.0); +COMPAT_SYSCALL(int, freebsd11_getfsstat, (struct freebsd11_statfs *, long, + int), getfsstat, FBSD_1.0); + +COMPAT_SYSCALL(int, freebsd14_setgroups, (int gidsize, const __gid_t *gidset), + setgroups, FBSD_1.0); + +#undef COMPAT_SYSCALL #endif /* _GEN_COMPAT_H_ */ diff --git a/lib/libc/gen/getgrouplist.3 b/lib/libc/gen/getgrouplist.3 index e9a980f99751..9e05ff7e7a29 100644 --- a/lib/libc/gen/getgrouplist.3 +++ b/lib/libc/gen/getgrouplist.3 @@ -1,5 +1,13 @@ +.\"- +.\" SPDX-License-Identifier: BSD-3-Clause +.\" .\" Copyright (c) 1991, 1993 .\" The Regents of the University of California. All rights reserved. +.\" Copyright (c) 2025 The FreeBSD Foundation +.\" +.\" Portions of this documentation were written by Olivier Certner +.\" <olce@FreeBSD.org> at Kumacom SARL under sponsorship from the FreeBSD +.\" Foundation. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -25,12 +33,12 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd October 26, 2014 +.Dd October 9, 2025 .Dt GETGROUPLIST 3 .Os .Sh NAME .Nm getgrouplist -.Nd calculate group access list +.Nd produce a user's effective group list .Sh LIBRARY .Lb libc .Sh SYNOPSIS @@ -40,36 +48,44 @@ .Sh DESCRIPTION The .Fn getgrouplist -function reads through the group file and calculates -the group access list for the user specified in -.Fa name . -The +function retrieves from the group database the supplementary groups for the user +specified in +.Fa name +and returns the effective group list, whose first group is the value of +.Fa basegid +and the others are the supplementary groups. .Fa basegid -is automatically included in the groups list. -Typically this value is given as -the group number from the password file. +typically is the user's initial numerical group ID from the password database. .Pp -The resulting group list is returned in the array pointed to by +The effective group list is returned in the array pointed to by .Fa groups . -The caller specifies the size of the +The caller specifies the length of the .Fa groups array in the integer pointed to by -.Fa ngroups ; -the actual number of groups found is returned in +.Fa ngroups . +The number of groups of the effective group list, which may be greater than the +.Fa groups +array's length, is returned through .Fa ngroups . .Sh RETURN VALUES The .Fn getgrouplist -function -returns 0 on success and \-1 if the size of the group list is too small to -hold all the user's groups. -Here, the group array will be filled with as many groups as will fit. +function returns 0 on success and \-1 if the length of the group list is too +small to hold all the user's groups. +In the latter case, the +.Fa groups +array is filled with as many groups as possible from the start of the effective +group list, and the length pointed to by +.Fa ngroups +is set to the full length of the latter, thus to a value strictly greater than +before the call. .Sh FILES .Bl -tag -width /etc/group -compact .It Pa /etc/group group membership list .El .Sh SEE ALSO +.Xr setcred 2 , .Xr setgroups 2 , .Xr initgroups 3 .Sh HISTORY diff --git a/lib/libc/gen/getgrouplist.c b/lib/libc/gen/getgrouplist.c index 5bd06bc5121f..9c57b7031336 100644 --- a/lib/libc/gen/getgrouplist.c +++ b/lib/libc/gen/getgrouplist.c @@ -29,13 +29,8 @@ * SUCH DAMAGE. */ -/* - * get credential - */ #include <sys/types.h> -#include <grp.h> -#include <string.h> #include <unistd.h> #include <ssp/ssp.h> @@ -46,4 +41,3 @@ __ssp_real(getgrouplist)(const char *uname, gid_t agroup, gid_t *groups, int *gr { return __getgroupmembership(uname, agroup, groups, *grpcnt, grpcnt); } - diff --git a/lib/libc/gen/getvfsbyname.3 b/lib/libc/gen/getvfsbyname.3 index 23036429b27e..61fd48624fbd 100644 --- a/lib/libc/gen/getvfsbyname.3 +++ b/lib/libc/gen/getvfsbyname.3 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd August 16, 2018 +.Dd October 28, 2025 .Dt GETVFSBYNAME 3 .Os .Sh NAME @@ -102,6 +102,7 @@ argument specifies a file system that is unknown or not configured in the kernel. .El .Sh SEE ALSO +.Xr lsvfs 1 , .Xr jail 2 , .Xr mount 2 , .Xr sysctl 3 , diff --git a/lib/libc/gen/initgroups.3 b/lib/libc/gen/initgroups.3 index 03bd07494fc9..74133e7d7048 100644 --- a/lib/libc/gen/initgroups.3 +++ b/lib/libc/gen/initgroups.3 @@ -1,5 +1,13 @@ +.\"- +.\" SPDX-License-Identifier: BSD-3-Clause +.\" .\" Copyright (c) 1983, 1991, 1993 .\" The Regents of the University of California. All rights reserved. +.\" Copyright (c) 2025 The FreeBSD Foundation +.\" +.\" Portions of this documentation were written by Olivier Certner +.\" <olce@FreeBSD.org> at Kumacom SARL under sponsorship from the FreeBSD +.\" Foundation. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions @@ -25,12 +33,12 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd October 26, 2014 +.Dd October 9, 2025 .Dt INITGROUPS 3 .Os .Sh NAME .Nm initgroups -.Nd initialize group access list +.Nd initialize supplementary groups as per the group database .Sh LIBRARY .Lb libc .Sh SYNOPSIS @@ -40,19 +48,18 @@ .Sh DESCRIPTION The .Fn initgroups -function -uses the -.Xr getgrouplist 3 -function to calculate the group access list for the user -specified in +function initializes the current process' supplementary groups as prescribed by +its arguments and the system's group database. +.Pp +It first uses the +.Fn getgrouplist +function to compute a list of groups containing the passed +.Fa basegid , +which typically is the user's initial numerical group ID from the password +database, and the supplementary groups in the group database for the user named .Fa name . -This group list is then setup for the current process using -.Xr setgroups 2 . -The -.Fa basegid -is automatically included in the groups list. -Typically this value is given as -the group number from the password file. +It then installs this list as the current process' supplementary groups using +.Fn setgroups . .Sh RETURN VALUES .Rv -std initgroups .Sh ERRORS @@ -60,9 +67,9 @@ The .Fn initgroups function may fail and set .Va errno -for any of the errors specified for the library function -.Xr setgroups 2 . -It may also return: +to any of the errors specified for the +.Xr setgroups 2 +system call, or to: .Bl -tag -width Er .It Bq Er ENOMEM The @@ -77,3 +84,67 @@ The .Fn initgroups function appeared in .Bx 4.2 . +.Pp +The +.Fn initgroups +function changed semantics in +.Fx 15 , +following that of +.Xr setgroups 2 +in the same release. +Before that, it would also set the effective group ID to +.Fa basegid , +and would not include the latter in the supplementary groups except before +.Fx 8 . +Its current behavior in these respects is known to be compatible with that of +the following systems up to the specified versions that are current at time of +this writing: +.Bl -dash -width "-" -compact +.It +Linux (up to 6.6) with the GNU libc (up to 2.42) +.It +.Nx 1.1 and greater (up to 10) +.It +.Ox (up to 7.7) +.It +Systems based on illumos (up to August 2025 sources) +.El +.Sh SECURITY CONSIDERATIONS +As +.Fa basegid +is typically the user's initial numerical group ID, to which the current +process' effective group ID is generally initialized, processes using functions +to change their effective group ID +.Pq via Xr setgid 2 or similar +or that are spawned from executables with the set-group-ID mode bit set will not +be able to relinquish the access rights deriving from being a member of +.Fa basegid , +as these functions do not change the supplementary groups. +.Pp +This behavior is generally desirable in order to paper over the difference of +treatment between the effective group and supplementary ones in this situation, +as they are all in the end indiscriminately used in traditional UNIX +discretionary access checks. +It blends well with the practice of allocating each user its own private group, +as processes launched from a set-group-ID executable keep the same user and +consistently stay also in the same user's group. +Finally, it was also chosen for compatibility with other systems +.Po +see the +.Sx HISTORY +section +.Pc . +.Pp +This convention of including +.Fa basegid +in the supplementary groups is however only enforced by the +.Fn initgroups +function, and not by the +.Xr setgroups 2 +system call, so applications expressly wanting to include in the supplementary +groups only those specified by the group database can themselves call +.Fn getgrouplist +and then +.Fn setgroups +on the result with the first element skipped +.Pq see Xr getgrouplist 3 . diff --git a/lib/libc/gen/initgroups.c b/lib/libc/gen/initgroups.c index b6697dd7ed8f..a1a7d92250e2 100644 --- a/lib/libc/gen/initgroups.c +++ b/lib/libc/gen/initgroups.c @@ -3,6 +3,11 @@ * * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. + * Copyright (c) 2025 The FreeBSD Foundation + * + * Portions of this software were developed by Olivier Certner + * <olce@FreeBSD.org> at Kumacom SARL under sponsorship from the FreeBSD + * Foundation. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -29,34 +34,52 @@ * SUCH DAMAGE. */ -#include <sys/param.h> +/* For __sym_compat(). */ +#include <sys/cdefs.h> -#include "namespace.h" -#include <err.h> -#include "un-namespace.h" #include <errno.h> -#include <stdio.h> #include <stdlib.h> #include <unistd.h> -int -initgroups(const char *uname, gid_t agroup) +/* For freebsd14_setgroups(). */ +#include "gen-compat.h" + +static int +initgroups_impl(const char *uname, gid_t agroup, + int (*setgroups)(int, const gid_t *)) { - int ngroups, ret; - long ngroups_max; gid_t *groups; + long ngroups_max; + int ngroups, ret; /* - * Provide space for one group more than possible to allow - * setgroups to fail and set errno. + * Provide space for one group more than possible to allow setgroups() + * to fail and set 'errno' in case we get back more than {NGROUPS_MAX} + + * 1 groups. */ ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2; - if ((groups = malloc(sizeof(*groups) * ngroups_max)) == NULL) - return (ENOMEM); + groups = malloc(sizeof(*groups) * ngroups_max); + if (groups == NULL) + return (-1); /* malloc() set 'errno'. */ ngroups = (int)ngroups_max; - getgrouplist(uname, agroup, groups, &ngroups); - ret = setgroups(ngroups, groups); + (void)getgrouplist(uname, agroup, groups, &ngroups); + ret = (*setgroups)(ngroups, groups); + free(groups); - return (ret); + return (ret); /* setgroups() set 'errno'. */ } + +int +initgroups(const char *uname, gid_t agroup) +{ + return (initgroups_impl(uname, agroup, setgroups)); +} + +int +freebsd14_initgroups(const char *uname, gid_t agroup) +{ + return (initgroups_impl(uname, agroup, freebsd14_setgroups)); +} + +__sym_compat(initgroups, freebsd14_initgroups, FBSD_1.0); diff --git a/lib/libc/gen/jrand48.c b/lib/libc/gen/jrand48.c index 0a9f780a9e5c..93442439d49e 100644 --- a/lib/libc/gen/jrand48.c +++ b/lib/libc/gen/jrand48.c @@ -11,14 +11,13 @@ * to anyone/anything when using this software. */ -#include <stdint.h> - #include "rand48.h" long jrand48(unsigned short xseed[3]) { + uint48 tmp; - _dorand48(xseed); - return ((int32_t)(((uint32_t)xseed[2] << 16) | (uint32_t)xseed[1])); + DORAND48(tmp, xseed); + return ((int)((tmp >> 16) & 0xffffffff)); } diff --git a/lib/libc/gen/lcong48.c b/lib/libc/gen/lcong48.c index f13826b3d3f3..871b2110ed94 100644 --- a/lib/libc/gen/lcong48.c +++ b/lib/libc/gen/lcong48.c @@ -13,18 +13,10 @@ #include "rand48.h" -extern unsigned short _rand48_seed[3]; -extern unsigned short _rand48_mult[3]; -extern unsigned short _rand48_add; - void lcong48(unsigned short p[7]) { - _rand48_seed[0] = p[0]; - _rand48_seed[1] = p[1]; - _rand48_seed[2] = p[2]; - _rand48_mult[0] = p[3]; - _rand48_mult[1] = p[4]; - _rand48_mult[2] = p[5]; + LOADRAND48(_rand48_seed, &p[0]); + LOADRAND48(_rand48_mult, &p[3]); _rand48_add = p[6]; } diff --git a/lib/libc/gen/lrand48.c b/lib/libc/gen/lrand48.c index a3d0111cf4d5..cc07044b8af9 100644 --- a/lib/libc/gen/lrand48.c +++ b/lib/libc/gen/lrand48.c @@ -13,11 +13,9 @@ #include "rand48.h" -extern unsigned short _rand48_seed[3]; - long lrand48(void) { - _dorand48(_rand48_seed); - return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1); + _DORAND48(_rand48_seed); + return (_rand48_seed >> 17) & 0x7fffffff; } diff --git a/lib/libc/gen/mrand48.c b/lib/libc/gen/mrand48.c index 15b0bfb1bd6e..f9128a6d4188 100644 --- a/lib/libc/gen/mrand48.c +++ b/lib/libc/gen/mrand48.c @@ -15,13 +15,9 @@ #include "rand48.h" -extern unsigned short _rand48_seed[3]; - long mrand48(void) { - - _dorand48(_rand48_seed); - return ((int32_t)(((uint32_t)_rand48_seed[2] << 16) | - (uint32_t)_rand48_seed[1])); + _DORAND48(_rand48_seed); + return ((int)((_rand48_seed >> 16) & 0xffffffff)); } diff --git a/lib/libc/gen/nrand48.c b/lib/libc/gen/nrand48.c index 6c54065e7e0f..f6f4e231105c 100644 --- a/lib/libc/gen/nrand48.c +++ b/lib/libc/gen/nrand48.c @@ -16,6 +16,8 @@ long nrand48(unsigned short xseed[3]) { - _dorand48(xseed); - return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); + uint48 tmp; + + DORAND48(tmp, xseed); + return ((tmp >> 17) & 0x7fffffff); } diff --git a/lib/libc/gen/psignal.3 b/lib/libc/gen/psignal.3 index 098b7b02a9b9..bf6a99b4b113 100644 --- a/lib/libc/gen/psignal.3 +++ b/lib/libc/gen/psignal.3 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd May 10, 2025 +.Dd September 23, 2025 .Dt PSIGNAL 3 .Os .Sh NAME @@ -141,6 +141,13 @@ The name in can be either the name of the signal, with or without the .Dq SIG prefix, or a decimal number. +.Sh RETURN VALUES +The +.Fn sig2str +and +.Fn str2sig +return 0 on success and -1 on translation failure. +In the latter case the memory to store the translation result is left intact. .Sh SEE ALSO .Xr sigaction 2 , .Xr perror 3 , diff --git a/lib/libc/gen/rand48.3 b/lib/libc/gen/rand48.3 index 1e47c843058e..3ea649354270 100644 --- a/lib/libc/gen/rand48.3 +++ b/lib/libc/gen/rand48.3 @@ -9,7 +9,7 @@ .\" of any kind. I shall in no event be liable for anything that happens .\" to anyone/anything when using this software. .\" -.Dd September 4, 2012 +.Dd September 11, 2025 .Dt RAND48 3 .Os .Sh NAME @@ -183,5 +183,8 @@ generator calls. .Xr arc4random 3 , .Xr rand 3 , .Xr random 3 +.Sh STANDARDS +The functions described in this page are expected to conform to +.St -p1003.1-2008 . .Sh AUTHORS .An Martin Birgmeier diff --git a/lib/libc/gen/rand48.h b/lib/libc/gen/rand48.h index 9861e99683cb..d3326e851491 100644 --- a/lib/libc/gen/rand48.h +++ b/lib/libc/gen/rand48.h @@ -14,10 +14,11 @@ #ifndef _RAND48_H_ #define _RAND48_H_ +#include <sys/types.h> #include <math.h> #include <stdlib.h> -void _dorand48(unsigned short[3]); +#include "fpmath.h" #define RAND48_SEED_0 (0x330e) #define RAND48_SEED_1 (0xabcd) @@ -27,4 +28,62 @@ void _dorand48(unsigned short[3]); #define RAND48_MULT_2 (0x0005) #define RAND48_ADD (0x000b) +typedef uint64_t uint48; + +extern uint48 _rand48_seed; +extern uint48 _rand48_mult; +extern uint48 _rand48_add; + +#define TOUINT48(x, y, z) \ + ((uint48)(x) + (((uint48)(y)) << 16) + (((uint48)(z)) << 32)) + +#define RAND48_SEED TOUINT48(RAND48_SEED_0, RAND48_SEED_1, RAND48_SEED_2) +#define RAND48_MULT TOUINT48(RAND48_MULT_0, RAND48_MULT_1, RAND48_MULT_2) + +#define LOADRAND48(l, x) do { \ + (l) = TOUINT48((x)[0], (x)[1], (x)[2]); \ +} while (0) + +#define STORERAND48(l, x) do { \ + (x)[0] = (unsigned short)(l); \ + (x)[1] = (unsigned short)((l) >> 16); \ + (x)[2] = (unsigned short)((l) >> 32); \ +} while (0) + +#define _DORAND48(l) do { \ + (l) = (l) * _rand48_mult + _rand48_add; \ +} while (0) + +#define DORAND48(l, x) do { \ + LOADRAND48(l, x); \ + _DORAND48(l); \ + STORERAND48(l, x); \ +} while (0) + +#define ERAND48_BEGIN \ + union { \ + union IEEEd2bits ieee; \ + uint64_t u64; \ + } u; \ + int s + +/* + * Optimization for speed: assume doubles are IEEE 754 and use bit fiddling + * rather than converting to double. Specifically, clamp the result to 48 bits + * and convert to a double in [0.0, 1.0) via division by 2^48. Normalize by + * shifting the most significant bit into the implicit one position and + * adjusting the exponent accordingly. The store to the exponent field + * overwrites the implicit one. + */ +#define ERAND48_END(x) do { \ + u.u64 = ((x) & 0xffffffffffffULL); \ + if (u.u64 == 0) \ + return (0.0); \ + u.u64 <<= 5; \ + for (s = 0; !(u.u64 & (1LL << 52)); s++, u.u64 <<= 1) \ + ; \ + u.ieee.bits.exp = 1022 - s; \ + return (u.ieee.d); \ +} while (0) + #endif /* _RAND48_H_ */ diff --git a/lib/libc/gen/seed48.c b/lib/libc/gen/seed48.c index 258c4bac3c9f..f57656ce1121 100644 --- a/lib/libc/gen/seed48.c +++ b/lib/libc/gen/seed48.c @@ -13,24 +13,14 @@ #include "rand48.h" -extern unsigned short _rand48_seed[3]; -extern unsigned short _rand48_mult[3]; -extern unsigned short _rand48_add; - unsigned short * seed48(unsigned short xseed[3]) { static unsigned short sseed[3]; - sseed[0] = _rand48_seed[0]; - sseed[1] = _rand48_seed[1]; - sseed[2] = _rand48_seed[2]; - _rand48_seed[0] = xseed[0]; - _rand48_seed[1] = xseed[1]; - _rand48_seed[2] = xseed[2]; - _rand48_mult[0] = RAND48_MULT_0; - _rand48_mult[1] = RAND48_MULT_1; - _rand48_mult[2] = RAND48_MULT_2; + STORERAND48(_rand48_seed, sseed); + LOADRAND48(_rand48_seed, xseed); + _rand48_mult = RAND48_MULT; _rand48_add = RAND48_ADD; - return sseed; + return (sseed); } diff --git a/lib/libc/gen/srand48.c b/lib/libc/gen/srand48.c index fd369a094c51..4b82ece72db8 100644 --- a/lib/libc/gen/srand48.c +++ b/lib/libc/gen/srand48.c @@ -13,18 +13,11 @@ #include "rand48.h" -extern unsigned short _rand48_seed[3]; -extern unsigned short _rand48_mult[3]; -extern unsigned short _rand48_add; - void srand48(long seed) { - _rand48_seed[0] = RAND48_SEED_0; - _rand48_seed[1] = (unsigned short) seed; - _rand48_seed[2] = (unsigned short) (seed >> 16); - _rand48_mult[0] = RAND48_MULT_0; - _rand48_mult[1] = RAND48_MULT_1; - _rand48_mult[2] = RAND48_MULT_2; + _rand48_seed = TOUINT48(RAND48_SEED_0, (unsigned short)seed, + (unsigned short)(seed >> 16)); + _rand48_mult = RAND48_MULT; _rand48_add = RAND48_ADD; } diff --git a/lib/libc/gen/sysconf.3 b/lib/libc/gen/sysconf.3 index e38357b898a7..290ef0dc158c 100644 --- a/lib/libc/gen/sysconf.3 +++ b/lib/libc/gen/sysconf.3 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd April 26, 2013 +.Dd August 30, 2025 .Dt SYSCONF 3 .Os .Sh NAME @@ -77,7 +77,9 @@ The maximum number of supplemental groups. .It Li _SC_NPROCESSORS_CONF The number of processors configured. .It Li _SC_NPROCESSORS_ONLN -The number of processors currently online. +The number of processors currently online, taking into account current jail +restrictions to report only the number of processors that are usable to the +process. .It Li _SC_OPEN_MAX One more than the maximum value the system may assign to a new file descriptor. .It Li _SC_PAGESIZE diff --git a/lib/libc/gen/sysconf.c b/lib/libc/gen/sysconf.c index 66562d0e29f0..87aedc07c110 100644 --- a/lib/libc/gen/sysconf.c +++ b/lib/libc/gen/sysconf.c @@ -51,7 +51,7 @@ #include "un-namespace.h" #include "../stdlib/atexit.h" -#include "tzdir.h" /* from ../../../contrib/tzcode/stdtime */ +#include "tzdir.h" /* from ../../../contrib/tzcode */ #include "libc_private.h" #define _PATH_ZONEINFO TZDIR /* from tzfile.h */ @@ -72,6 +72,7 @@ long sysconf(int name) { struct rlimit rl; + cpuset_t cpus; size_t len; int mib[2], sverrno, value; long lvalue, defaultresult; @@ -581,8 +582,21 @@ yesno: return (_POSIX_IPV6); #endif - case _SC_NPROCESSORS_CONF: case _SC_NPROCESSORS_ONLN: + /* + * Consult our root set first, because our CPU availability + * may not match the total number of CPUs available on the + * system and we may have a non-uniform layout even within + * userland. In particular, each jail has a root set that can + * be constrained by its parent and processes within the jail + * cannot widen beyond those constraints, so to those processes + * it makes sense to claim the more limited count. + */ + if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1, + sizeof(cpus), &cpus) == 0) + return (CPU_COUNT(&cpus)); + /* FALLTHROUGH */ + case _SC_NPROCESSORS_CONF: if (_elf_aux_info(AT_NCPUS, &value, sizeof(value)) == 0) return ((long)value); mib[0] = CTL_HW; diff --git a/lib/libc/include/compat.h b/lib/libc/include/compat.h index 97f22607ddd7..630ffe7daae3 100644 --- a/lib/libc/include/compat.h +++ b/lib/libc/include/compat.h @@ -80,4 +80,3 @@ __sym_compat(setgroups, freebsd14_setgroups, FBSD_1.0); #undef __weak_reference #endif /* __LIBC_COMPAT_H__ */ - diff --git a/lib/libc/include/port_before.h b/lib/libc/include/port_before.h index cfc43c53f157..aa2cd394104a 100644 --- a/lib/libc/include/port_before.h +++ b/lib/libc/include/port_before.h @@ -5,7 +5,6 @@ #define _LIBC 1 #define DO_PTHREADS 1 #define USE_POLL 1 -#define HAVE_MD5 1 #define ISC_SOCKLEN_T socklen_t #define ISC_FORMAT_PRINTF(fmt, args) \ diff --git a/lib/libc/inet/inet_net_ntop.c b/lib/libc/inet/inet_net_ntop.c index 9d98dbb5ca99..30dd5c0571f2 100644 --- a/lib/libc/inet/inet_net_ntop.c +++ b/lib/libc/inet/inet_net_ntop.c @@ -1,20 +1,23 @@ -/*- - * SPDX-License-Identifier: ISC +/* $OpenBSD: inet_net_ntop.c,v 1.9 2019/07/03 03:24:04 deraadt Exp $ */ + +/* + * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org> + * Copyright (c) 1996 by Internet Software Consortium. * - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. + * SPDX-License-Identifier: ISC * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. */ #include "port_before.h" @@ -31,18 +34,10 @@ #include "port_after.h" -#ifdef SPRINTF_CHAR -# define SPRINTF(x) strlen(sprintf/**/x) -#else -# define SPRINTF(x) ((size_t)sprintf x) -#endif - -static char * inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, - size_t size); -static char * inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, - size_t size); +static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t); +static char *inet_net_ntop_ipv6(const u_char *, int, char *, size_t); -/*% +/* * char * * inet_net_ntop(af, src, bits, dst, size) * convert network number from network to presentation format. @@ -66,7 +61,7 @@ inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size) } } -/*% +/* * static char * * inet_net_ntop_ipv4(src, bits, dst, size) * convert IPv4 network number from network to presentation format. @@ -83,53 +78,63 @@ static char * inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) { char *odst = dst; - char *t; u_int m; int b; + char *ep; + int advance; + + ep = dst + size; + if (ep <= dst) + goto emsgsize; if (bits < 0 || bits > 32) { errno = EINVAL; return (NULL); } - if (bits == 0) { - if (size < sizeof "0") + if (ep - dst < sizeof "0") goto emsgsize; *dst++ = '0'; - size--; *dst = '\0'; } /* Format whole octets. */ for (b = bits / 8; b > 0; b--) { - if (size <= sizeof "255.") + if (ep - dst < sizeof "255.") + goto emsgsize; + advance = snprintf(dst, ep - dst, "%u", *src++); + if (advance <= 0 || advance >= ep - dst) goto emsgsize; - t = dst; - dst += SPRINTF((dst, "%u", *src++)); + dst += advance; if (b > 1) { + if (dst + 1 >= ep) + goto emsgsize; *dst++ = '.'; *dst = '\0'; } - size -= (size_t)(dst - t); } /* Format partial octet. */ b = bits % 8; if (b > 0) { - if (size <= sizeof ".255") + if (ep - dst < sizeof ".255") goto emsgsize; - t = dst; if (dst != odst) *dst++ = '.'; m = ((1 << b) - 1) << (8 - b); - dst += SPRINTF((dst, "%u", *src & m)); - size -= (size_t)(dst - t); + advance = snprintf(dst, ep - dst, "%u", *src & m); + if (advance <= 0 || advance >= ep - dst) + goto emsgsize; + dst += advance; } /* Format CIDR /width. */ - if (size <= sizeof "/32") + if (ep - dst < sizeof "/32") + goto emsgsize; + advance = snprintf(dst, ep - dst, "/%u", bits); + if (advance <= 0 || advance >= ep - dst) goto emsgsize; - dst += SPRINTF((dst, "/%u", bits)); + dst += advance; return (odst); emsgsize: @@ -137,132 +142,27 @@ inet_net_ntop_ipv4(const u_char *src, int bits, char *dst, size_t size) return (NULL); } -/*% - * static char * - * inet_net_ntop_ipv6(src, bits, fakebits, dst, size) - * convert IPv6 network number from network to presentation format. - * generates CIDR style result always. Picks the shortest representation - * unless the IP is really IPv4. - * always prints specified number of bits (bits). - * return: - * pointer to dst, or NULL if an error occurred (check errno). - * note: - * network byte order assumed. this means 192.5.5.240/28 has - * 0b11110000 in its fourth octet. - * author: - * Vadim Kogan (UCB), June 2001 - * Original version (IPv4) by Paul Vixie (ISC), July 1996 - */ - static char * -inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) { - u_int m; - int b; - int p; - int zero_s, zero_l, tmp_zero_s, tmp_zero_l; - int i; - int is_ipv4 = 0; - unsigned char inbuf[16]; - char outbuf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255/128")]; - char *cp; - int words; - u_char *s; +inet_net_ntop_ipv6(const u_char *src, int bits, char *dst, size_t size) +{ + int ret; + char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255:255:255:255/128")]; if (bits < 0 || bits > 128) { errno = EINVAL; return (NULL); } - cp = outbuf; - - if (bits == 0) { - *cp++ = ':'; - *cp++ = ':'; - *cp = '\0'; - } else { - /* Copy src to private buffer. Zero host part. */ - p = (bits + 7) / 8; - memcpy(inbuf, src, p); - memset(inbuf + p, 0, 16 - p); - b = bits % 8; - if (b != 0) { - m = ~0 << (8 - b); - inbuf[p-1] &= m; - } - - s = inbuf; - - /* how many words need to be displayed in output */ - words = (bits + 15) / 16; - if (words == 1) - words = 2; - - /* Find the longest substring of zero's */ - zero_s = zero_l = tmp_zero_s = tmp_zero_l = 0; - for (i = 0; i < (words * 2); i += 2) { - if ((s[i] | s[i+1]) == 0) { - if (tmp_zero_l == 0) - tmp_zero_s = i / 2; - tmp_zero_l++; - } else { - if (tmp_zero_l && zero_l < tmp_zero_l) { - zero_s = tmp_zero_s; - zero_l = tmp_zero_l; - tmp_zero_l = 0; - } - } - } - - if (tmp_zero_l && zero_l < tmp_zero_l) { - zero_s = tmp_zero_s; - zero_l = tmp_zero_l; - } - - if (zero_l != words && zero_s == 0 && ((zero_l == 6) || - ((zero_l == 5 && s[10] == 0xff && s[11] == 0xff) || - ((zero_l == 7 && s[14] != 0 && s[15] != 1))))) - is_ipv4 = 1; - - /* Format whole words. */ - for (p = 0; p < words; p++) { - if (zero_l != 0 && p >= zero_s && p < zero_s + zero_l) { - /* Time to skip some zeros */ - if (p == zero_s) - *cp++ = ':'; - if (p == words - 1) - *cp++ = ':'; - s++; - s++; - continue; - } + if (inet_ntop(AF_INET6, src, buf, size) == NULL) + return (NULL); - if (is_ipv4 && p > 5 ) { - *cp++ = (p == 6) ? ':' : '.'; - cp += SPRINTF((cp, "%u", *s++)); - /* we can potentially drop the last octet */ - if (p != 7 || bits > 120) { - *cp++ = '.'; - cp += SPRINTF((cp, "%u", *s++)); - } - } else { - if (cp != outbuf) - *cp++ = ':'; - cp += SPRINTF((cp, "%x", *s * 256 + s[1])); - s += 2; - } - } + ret = snprintf(dst, size, "%s/%d", buf, bits); + if (ret < 0 || ret >= size) { + errno = EMSGSIZE; + return (NULL); } - /* Format CIDR /width. */ - sprintf(cp, "/%u", bits); - if (strlen(outbuf) + 1 > size) - goto emsgsize; - strcpy(dst, outbuf); return (dst); - -emsgsize: - errno = EMSGSIZE; - return (NULL); } /* @@ -271,5 +171,3 @@ emsgsize: */ #undef inet_net_ntop __weak_reference(__inet_net_ntop, inet_net_ntop); - -/*! \file */ diff --git a/lib/libc/inet/inet_net_pton.c b/lib/libc/inet/inet_net_pton.c index d566a0e1d3c3..14c88eb72014 100644 --- a/lib/libc/inet/inet_net_pton.c +++ b/lib/libc/inet/inet_net_pton.c @@ -1,20 +1,23 @@ -/*- - * SPDX-License-Identifier: ISC +/* $OpenBSD: inet_net_pton.c,v 1.14 2022/12/27 17:10:06 jmc Exp $ */ + +/* + * Copyright (c) 2012 by Gilles Chehade <gilles@openbsd.org> + * Copyright (c) 1996,1999 by Internet Software Consortium. * - * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") - * Copyright (C) 1996, 1998, 1999, 2001, 2003 Internet Software Consortium. + * SPDX-License-Identifier: ISC * - * Permission to use, copy, modify, and/or distribute this software for any + * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE - * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. */ #include "port_before.h" @@ -22,7 +25,6 @@ #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> -#include <arpa/nameser.h> #include <arpa/inet.h> #include <assert.h> @@ -34,13 +36,37 @@ #include "port_after.h" -#ifdef SPRINTF_CHAR -# define SPRINTF(x) strlen(sprintf/**/x) -#else -# define SPRINTF(x) ((size_t)sprintf x) -#endif +static int inet_net_pton_ipv4(const char *, u_char *, size_t); +static int inet_net_pton_ipv6(const char *, u_char *, size_t); -/*% +/* + * static int + * inet_net_pton(af, src, dst, size) + * convert network number from presentation to network format. + * accepts hex octets, hex strings, decimal octets, and /CIDR. + * "size" is in bytes and describes "dst". + * return: + * number of bits, either imputed classfully or specified with /CIDR, + * or -1 if some failure occurred (check errno). ENOENT means it was + * not a valid network specification. + * author: + * Paul Vixie (ISC), June 1996 + */ +int +inet_net_pton(int af, const char *src, void *dst, size_t size) +{ + switch (af) { + case AF_INET: + return (inet_net_pton_ipv4(src, dst, size)); + case AF_INET6: + return (inet_net_pton_ipv6(src, dst, size)); + default: + errno = EAFNOSUPPORT; + return (-1); + } +} + +/* * static int * inet_net_pton_ipv4(src, dst, size) * convert IPv4 network number from presentation to network format. @@ -57,22 +83,24 @@ * Paul Vixie (ISC), June 1996 */ static int -inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) { - static const char xdigits[] = "0123456789abcdef"; - static const char digits[] = "0123456789"; - int n, ch, tmp = 0, dirty, bits; +inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) +{ + static const char + xdigits[] = "0123456789abcdef", + digits[] = "0123456789"; + int n, ch, tmp, dirty, bits; const u_char *odst = dst; - ch = *src++; + ch = (unsigned char)*src++; if (ch == '0' && (src[0] == 'x' || src[0] == 'X') - && isascii((unsigned char)(src[1])) - && isxdigit((unsigned char)(src[1]))) { + && isascii((unsigned char)src[1]) && isxdigit((unsigned char)src[1])) { /* Hexadecimal: Eat nybble string. */ - if (size <= 0U) + if (size == 0) goto emsgsize; - dirty = 0; - src++; /*%< skip x or X. */ - while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) { + tmp = 0, dirty = 0; + src++; /* skip x or X. */ + while ((ch = (unsigned char)*src++) != '\0' && + isascii(ch) && isxdigit(ch)) { if (isupper(ch)) ch = tolower(ch); n = strchr(xdigits, ch) - xdigits; @@ -82,14 +110,14 @@ inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) { else tmp = (tmp << 4) | n; if (++dirty == 2) { - if (size-- <= 0U) + if (size-- == 0) goto emsgsize; *dst++ = (u_char) tmp; dirty = 0; } } - if (dirty) { /*%< Odd trailing nybble? */ - if (size-- <= 0U) + if (dirty) { /* Odd trailing nybble? */ + if (size-- == 0) goto emsgsize; *dst++ = (u_char) (tmp << 4); } @@ -104,16 +132,16 @@ inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) { tmp += n; if (tmp > 255) goto enoent; - } while ((ch = *src++) != '\0' && + } while ((ch = (unsigned char)*src++) != '\0' && isascii(ch) && isdigit(ch)); - if (size-- <= 0U) + if (size-- == 0) goto emsgsize; *dst++ = (u_char) tmp; if (ch == '\0' || ch == '/') break; if (ch != '.') goto enoent; - ch = *src++; + ch = (unsigned char)*src++; if (!isascii(ch) || !isdigit(ch)) goto enoent; } @@ -121,10 +149,10 @@ inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) { goto enoent; bits = -1; - if (ch == '/' && isascii((unsigned char)(src[0])) && - isdigit((unsigned char)(src[0])) && dst > odst) { + if (ch == '/' && isascii((unsigned char)src[0]) && + isdigit((unsigned char)src[0]) && dst > odst) { /* CIDR width specifier. Nothing can follow it. */ - ch = *src++; /*%< Skip over the /. */ + ch = (unsigned char)*src++; /* Skip over the /. */ bits = 0; do { n = strchr(digits, ch) - digits; @@ -132,8 +160,9 @@ inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) { bits *= 10; bits += n; if (bits > 32) - goto enoent; - } while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch)); + goto emsgsize; + } while ((ch = (unsigned char)*src++) != '\0' && + isascii(ch) && isdigit(ch)); if (ch != '\0') goto enoent; } @@ -147,29 +176,23 @@ inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) { goto enoent; /* If no CIDR spec was given, infer width from net class. */ if (bits == -1) { - if (*odst >= 240) /*%< Class E */ + if (*odst >= 240) /* Class E */ bits = 32; - else if (*odst >= 224) /*%< Class D */ - bits = 8; - else if (*odst >= 192) /*%< Class C */ + else if (*odst >= 224) /* Class D */ + bits = 4; + else if (*odst >= 192) /* Class C */ bits = 24; - else if (*odst >= 128) /*%< Class B */ + else if (*odst >= 128) /* Class B */ bits = 16; - else /*%< Class A */ + else /* Class A */ bits = 8; /* If imputed mask is narrower than specified octets, widen. */ if (bits < ((dst - odst) * 8)) bits = (dst - odst) * 8; - /* - * If there are no additional bits specified for a class D - * address adjust bits to 4. - */ - if (bits == 8 && *odst == 224) - bits = 4; } /* Extend network to cover the actual mask. */ while (bits > ((dst - odst) * 8)) { - if (size-- <= 0U) + if (size-- == 0) goto emsgsize; *dst++ = '\0'; } @@ -184,222 +207,48 @@ inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) { return (-1); } -static int -getbits(const char *src, int *bitsp) { - static const char digits[] = "0123456789"; - int n; - int val; - char ch; - - val = 0; - n = 0; - while ((ch = *src++) != '\0') { - const char *pch; - - pch = strchr(digits, ch); - if (pch != NULL) { - if (n++ != 0 && val == 0) /*%< no leading zeros */ - return (0); - val *= 10; - val += (pch - digits); - if (val > 128) /*%< range */ - return (0); - continue; - } - return (0); - } - if (n == 0) - return (0); - *bitsp = val; - return (1); -} static int -getv4(const char *src, u_char *dst, int *bitsp) { - static const char digits[] = "0123456789"; - u_char *odst = dst; - int n; - u_int val; - char ch; - - val = 0; - n = 0; - while ((ch = *src++) != '\0') { - const char *pch; +inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) +{ + struct in6_addr in6; + int ret; + int bits; + size_t bytes; + char buf[INET6_ADDRSTRLEN + sizeof("/128")]; + char *sep; + const char *errstr; - pch = strchr(digits, ch); - if (pch != NULL) { - if (n++ != 0 && val == 0) /*%< no leading zeros */ - return (0); - val *= 10; - val += (pch - digits); - if (val > 255) /*%< range */ - return (0); - continue; - } - if (ch == '.' || ch == '/') { - if (dst - odst > 3) /*%< too many octets? */ - return (0); - *dst++ = val; - if (ch == '/') - return (getbits(src, bitsp)); - val = 0; - n = 0; - continue; - } - return (0); + if (strlcpy(buf, src, sizeof buf) >= sizeof buf) { + errno = EMSGSIZE; + return (-1); } - if (n == 0) - return (0); - if (dst - odst > 3) /*%< too many octets? */ - return (0); - *dst++ = val; - return (1); -} -static int -inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) { - static const char xdigits_l[] = "0123456789abcdef", - xdigits_u[] = "0123456789ABCDEF"; - u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp; - const char *xdigits, *curtok; - int ch, saw_xdigit; - u_int val; - int digits; - int bits; - size_t bytes; - int words; - int ipv4; + sep = strchr(buf, '/'); + if (sep != NULL) + *sep++ = '\0'; - memset((tp = tmp), '\0', NS_IN6ADDRSZ); - endp = tp + NS_IN6ADDRSZ; - colonp = NULL; - /* Leading :: requires some special handling. */ - if (*src == ':') - if (*++src != ':') - goto enoent; - curtok = src; - saw_xdigit = 0; - val = 0; - digits = 0; - bits = -1; - ipv4 = 0; - while ((ch = *src++) != '\0') { - const char *pch; + ret = inet_pton(AF_INET6, buf, &in6); + if (ret != 1) + return (-1); - if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) - pch = strchr((xdigits = xdigits_u), ch); - if (pch != NULL) { - val <<= 4; - val |= (pch - xdigits); - if (++digits > 4) - goto enoent; - saw_xdigit = 1; - continue; - } - if (ch == ':') { - curtok = src; - if (!saw_xdigit) { - if (colonp) - goto enoent; - colonp = tp; - continue; - } else if (*src == '\0') - goto enoent; - if (tp + NS_INT16SZ > endp) - return (0); - *tp++ = (u_char) (val >> 8) & 0xff; - *tp++ = (u_char) val & 0xff; - saw_xdigit = 0; - digits = 0; - val = 0; - continue; - } - if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) && - getv4(curtok, tp, &bits) > 0) { - tp += NS_INADDRSZ; - saw_xdigit = 0; - ipv4 = 1; - break; /*%< '\\0' was seen by inet_pton4(). */ - } - if (ch == '/' && getbits(src, &bits) > 0) - break; - goto enoent; - } - if (saw_xdigit) { - if (tp + NS_INT16SZ > endp) - goto enoent; - *tp++ = (u_char) (val >> 8) & 0xff; - *tp++ = (u_char) val & 0xff; - } - if (bits == -1) + if (sep == NULL) bits = 128; - - words = (bits + 15) / 16; - if (words < 2) - words = 2; - if (ipv4) - words = 8; - endp = tmp + 2 * words; - - if (colonp != NULL) { - /* - * Since some memmove()'s erroneously fail to handle - * overlapping regions, we'll do the shift by hand. - */ - const int n = tp - colonp; - int i; - - if (tp == endp) - goto enoent; - for (i = 1; i <= n; i++) { - endp[- i] = colonp[n - i]; - colonp[n - i] = 0; + else { + bits = strtonum(sep, 0, 128, &errstr); + if (errstr) { + errno = EINVAL; + return (-1); } - tp = endp; } - if (tp != endp) - goto enoent; bytes = (bits + 7) / 8; - if (bytes > size) - goto emsgsize; - memcpy(dst, tmp, bytes); - return (bits); - - enoent: - errno = ENOENT; - return (-1); - - emsgsize: - errno = EMSGSIZE; - return (-1); -} - -/*% - * int - * inet_net_pton(af, src, dst, size) - * convert network number from presentation to network format. - * accepts hex octets, hex strings, decimal octets, and /CIDR. - * "size" is in bytes and describes "dst". - * return: - * number of bits, either imputed classfully or specified with /CIDR, - * or -1 if some failure occurred (check errno). ENOENT means it was - * not a valid network specification. - * author: - * Paul Vixie (ISC), June 1996 - */ -int -inet_net_pton(int af, const char *src, void *dst, size_t size) { - switch (af) { - case AF_INET: - return (inet_net_pton_ipv4(src, dst, size)); - case AF_INET6: - return (inet_net_pton_ipv6(src, dst, size)); - default: - errno = EAFNOSUPPORT; + if (bytes > size) { + errno = EMSGSIZE; return (-1); } + memcpy(dst, &in6.s6_addr, bytes); + return (bits); } /* @@ -408,5 +257,3 @@ inet_net_pton(int af, const char *src, void *dst, size_t size) { */ #undef inet_net_pton __weak_reference(__inet_net_pton, inet_net_pton); - -/*! \file */ diff --git a/lib/libc/locale/Makefile.inc b/lib/libc/locale/Makefile.inc index 33caafc5c10a..127f8fc67abc 100644 --- a/lib/libc/locale/Makefile.inc +++ b/lib/libc/locale/Makefile.inc @@ -46,7 +46,7 @@ MAN+= btowc.3 \ wctrans.3 wctype.3 wcwidth.3 \ duplocale.3 freelocale.3 newlocale.3 querylocale.3 uselocale.3 xlocale.3 -MAN+= big5.5 euc.5 gb18030.5 gb2312.5 gbk.5 mskanji.5 utf8.5 +MANNODEV+= big5.5 euc.5 gb18030.5 gb2312.5 gbk.5 mskanji.5 utf8.5 MLINKS+=btowc.3 wctob.3 MLINKS+=digittoint.3 digittoint_l.3 diff --git a/lib/libc/locale/xlocale_private.h b/lib/libc/locale/xlocale_private.h index ef1a8687a376..4cb323d5fa4c 100644 --- a/lib/libc/locale/xlocale_private.h +++ b/lib/libc/locale/xlocale_private.h @@ -200,11 +200,9 @@ extern _Thread_local locale_t __thread_locale; */ static inline locale_t __get_locale(void) { - - if (!__has_thread_locale) { + if (!__has_thread_locale || __thread_locale == NULL) return (&__xlocale_global_locale); - } - return (__thread_locale ? __thread_locale : &__xlocale_global_locale); + return (__thread_locale); } /** diff --git a/lib/libc/md/Makefile.inc b/lib/libc/md/Makefile.inc deleted file mode 100644 index 82c5f0670485..000000000000 --- a/lib/libc/md/Makefile.inc +++ /dev/null @@ -1,3 +0,0 @@ -.PATH: ${SRCTOP}/sys/kern - -SRCS+= md5c.c diff --git a/lib/libc/nls/Makefile.inc b/lib/libc/nls/Makefile.inc index f26e04c187a5..c211026aba72 100644 --- a/lib/libc/nls/Makefile.inc +++ b/lib/libc/nls/Makefile.inc @@ -12,6 +12,11 @@ MAN+= catclose.3 catgets.3 catopen.3 # for translators. NLSNAME= libc +# We don't want libc's NLS catalogues to be installed in the clibs package. +# Put them in locales instead, since anyone interested in NLS will have +# that installed. +NLSPACKAGE= locales + NLS+= be_BY.UTF-8 NLS+= ca_ES.ISO8859-1 NLS+= de_DE.ISO8859-1 diff --git a/lib/libc/posix1e/Makefile.inc b/lib/libc/posix1e/Makefile.inc index 934998cdd092..48f6c1ddf884 100644 --- a/lib/libc/posix1e/Makefile.inc +++ b/lib/libc/posix1e/Makefile.inc @@ -84,7 +84,6 @@ MAN+= acl.3 \ acl_valid.3 \ extattr.3 \ mac.3 \ - mac.conf.5 \ mac_free.3 \ mac_is_present.3 \ mac_get.3 \ @@ -134,4 +133,6 @@ MLINKS+=acl_create_entry.3 acl_create_entry_np.3\ mac_text.3 mac_from_text.3 \ mac_text.3 mac_to_text.3 +MANNODEV+= mac.conf.5 + CLEANFILES+= subr_acl_nfs4.c diff --git a/lib/libc/posix1e/mac_free.3 b/lib/libc/posix1e/mac_free.3 index 4ed68b70f3a3..6674ca2e9094 100644 --- a/lib/libc/posix1e/mac_free.3 +++ b/lib/libc/posix1e/mac_free.3 @@ -31,7 +31,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd September 21, 2023 +.Dd October 26, 2025 .Dt MAC_FREE 3 .Os .Sh NAME @@ -85,7 +85,7 @@ is a complex structure in the implementation, .Fn mac_free is specific to -.Vt mac_3 , +.Vt mac_t , and must not be used to free the character strings returned from .Fn mac_to_text . Doing so may result in undefined behavior. diff --git a/lib/libc/posix1e/mac_text.3 b/lib/libc/posix1e/mac_text.3 index 29c1aacca485..7633f4b0da64 100644 --- a/lib/libc/posix1e/mac_text.3 +++ b/lib/libc/posix1e/mac_text.3 @@ -28,7 +28,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd September 21, 2023 +.Dd October 26, 2025 .Dt MAC_TEXT 3 .Os .Sh NAME @@ -52,14 +52,16 @@ into the internal policy label format and places it in .Fa *mac , which must later be freed with -.Xr free 3 . +.Xr mac_free 3 . .Pp The .Fn mac_to_text function allocates storage for .Fa *text , which will be set to the text representation of -.Fa label . +.Fa label +and must later be freed with +.Xr free 3 . .Pp Refer to .Xr maclabel 7 diff --git a/lib/libc/regex/Makefile.inc b/lib/libc/regex/Makefile.inc index 89468f1317f6..e3417a3d9983 100644 --- a/lib/libc/regex/Makefile.inc +++ b/lib/libc/regex/Makefile.inc @@ -9,9 +9,9 @@ SYM_MAPS+=${LIBC_SRCTOP}/regex/Symbol.map # manpages only included in libc version .if ${LIB} == "c" -MAN+= regex.3 -MAN+= re_format.7 +MAN+= regex.3 +MLINKS+= regex.3 regcomp.3 regex.3 regexec.3 regex.3 regerror.3 +MLINKS+= regexec.3 regfree.3 -MLINKS+=regex.3 regcomp.3 regex.3 regexec.3 regex.3 regerror.3 -MLINKS+=regexec.3 regfree.3 +MANNODEV+= re_format.7 .endif diff --git a/lib/libc/resolv/Symbol.map b/lib/libc/resolv/Symbol.map index 6b9c43298fb5..26daecbe2eff 100644 --- a/lib/libc/resolv/Symbol.map +++ b/lib/libc/resolv/Symbol.map @@ -103,6 +103,5 @@ FBSD_1.0 { }; FBSD_1.4 { - __res_rndinit; __res_nrandomid; }; diff --git a/lib/libc/resolv/res_init.c b/lib/libc/resolv/res_init.c index 71ab2dcb7038..5a2fce013c8c 100644 --- a/lib/libc/resolv/res_init.c +++ b/lib/libc/resolv/res_init.c @@ -86,19 +86,6 @@ #include <unistd.h> #include <netdb.h> -#ifndef HAVE_MD5 -# include "../dst/md5.h" -#else -# ifdef SOLARIS2 -# include <sys/md5.h> -# elif _LIBC -# include <md5.h> -# endif -#endif -#ifndef _MD5_H_ -# define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */ -#endif - #include "un-namespace.h" #include "port_after.h" @@ -184,8 +171,6 @@ __res_vinit(res_state statp, int preinit) { statp->options = RES_DEFAULT; } - statp->_rnd = malloc(16); - res_rndinit(statp); statp->id = res_nrandomid(statp); memset(u, 0, sizeof(u)); @@ -733,48 +718,18 @@ net_mask(struct in_addr in) /*!< XXX - should really use system's version of th } #endif -static u_char srnd[16]; - void -res_rndinit(res_state statp) +freebsd15_res_rndinit(res_state statp) { - struct timeval now; - u_int32_t u32; - u_int16_t u16; - u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd; - - gettimeofday(&now, NULL); - u32 = now.tv_sec; - memcpy(rnd, &u32, 4); - u32 = now.tv_usec; - memcpy(rnd + 4, &u32, 4); - u32 += now.tv_sec; - memcpy(rnd + 8, &u32, 4); - u16 = getpid(); - memcpy(rnd + 12, &u16, 2); + (void)statp; } +__sym_compat(__res_rndinit, freebsd15_res_rndinit, FBSD_1.4); u_int res_nrandomid(res_state statp) { - struct timeval now; - u_int16_t u16; - MD5_CTX ctx; - u_char *rnd = statp->_rnd == NULL ? srnd : statp->_rnd; - - gettimeofday(&now, NULL); - u16 = (u_int16_t) (now.tv_sec ^ now.tv_usec); - memcpy(rnd + 14, &u16, 2); -#ifndef HAVE_MD5 - MD5_Init(&ctx); - MD5_Update(&ctx, rnd, 16); - MD5_Final(rnd, &ctx); -#else - MD5Init(&ctx); - MD5Update(&ctx, rnd, 16); - MD5Final(rnd, &ctx); -#endif - memcpy(&u16, rnd + 14, 2); - return ((u_int) u16); + (void) statp; + + return ((u_int)(arc4random() & 0xffff)); } /*% @@ -808,10 +763,6 @@ res_ndestroy(res_state statp) { free(statp->_u._ext.ext); statp->_u._ext.ext = NULL; } - if (statp->_rnd != NULL) { - free(statp->_rnd); - statp->_rnd = NULL; - } statp->options &= ~RES_INIT; } diff --git a/lib/libc/riscv/string/Makefile.inc b/lib/libc/riscv/string/Makefile.inc new file mode 100644 index 000000000000..6dae6b2cb62d --- /dev/null +++ b/lib/libc/riscv/string/Makefile.inc @@ -0,0 +1,10 @@ +MDSRCS+= \ + bcopy.c \ + bzero.c \ + memchr.S \ + memcpy.S \ + memset.S \ + strlen.S \ + strnlen.S \ + strchrnul.S \ + strrchr.S diff --git a/lib/libc/riscv/string/bcopy.c b/lib/libc/riscv/string/bcopy.c new file mode 100644 index 000000000000..0dee529fb9df --- /dev/null +++ b/lib/libc/riscv/string/bcopy.c @@ -0,0 +1,14 @@ +/*- + * Public domain. + */ + +#include <string.h> + +#undef bcopy /* _FORTIFY_SOURCE */ + +void +bcopy(const void *src, void *dst, size_t len) +{ + + memmove(dst, src, len); +} diff --git a/lib/libc/riscv/string/bzero.c b/lib/libc/riscv/string/bzero.c new file mode 100644 index 000000000000..d82f3061865b --- /dev/null +++ b/lib/libc/riscv/string/bzero.c @@ -0,0 +1,14 @@ +/*- + * Public domain. + */ + +#include <string.h> + +#undef bzero /* _FORTIFY_SOURCE */ + +void +bzero(void *b, size_t len) +{ + + memset(b, 0, len); +} diff --git a/lib/libc/riscv/string/memchr.S b/lib/libc/riscv/string/memchr.S new file mode 100644 index 000000000000..e6e04bfae96a --- /dev/null +++ b/lib/libc/riscv/string/memchr.S @@ -0,0 +1,188 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic <strajabot@FreeBSD.org> + */ + +#include <machine/asm.h> + +/* + * a0 - const void *b + * a1 - int c + * a2 - size_t len + */ +ENTRY(memchr) + /* + * a0 - const char *ptr + * a1 - char cccccccc[8] + * a2 - char iter[8] + * a3 - uint8_t *end + * a4 - uint64_t *end_align + * a5 - uint64_t *end_unroll + */ + + beqz a2, .Lno_match + + /* c = (uint8_t) c */ + andi a1, a1, 0xFF + + /* + * t0 = 0x0101010101010101 + * t1 = 0x8080808080808080 + * t2 = b << 3 + * cccccccc = (uint8_t)c * t0 + * end = b + len; + * ptr = b & ~0b111 + */ + add a3, a0, a2 + li t0, 0x01010101 + sltu t2, a0, a3 + slli t1, t0, 32 + neg t2, t2 + or t0, t0, t1 + and a3, a3, t2 + slli t1, t0, 7 + slli t2, a0, 3 + and a0, a0, ~0b111 + mul a1, t0, a1 + + ld a2, (a0) + + /* + * mask_start = REP8_0x01 ^ (REP8_0x01 << t2) + * iter = iter ^ cccccccc + * iter = iter | mask_start + */ + sll t2, t0, t2 + xor a2, a2, a1 + xor t2, t2, t0 + or a2, a2, t2 + + /* has_zero(iter) + * end_align = (end + 7) & ~0b111; + */ + addi a4, a3, 7 + not t2, a2 + sub a2, a2, t0 + and t2, t2, t1 + andi a4, a4, ~0b111 + and a2, a2, t2 + + /* ptr = ptr + 8 */ + addi a0, a0, 8 + + bnez a2, .Lfind_zero + + /* if(ptr == end_align) */ + beq a0, a4, .Lno_match + + /* end_unroll = end_align & ~0b1111 */ + andi a5, a4, ~0b1111 + + /* + * Instead of branching to check if `ptr` is 16-byte aligned: + * - Probe the next 8 bytes for `c` + * - Align `ptr` down to the nearest 16-byte boundary + * + * If `ptr` was already 16-byte aligned, those 8 bytes will be + * checked again inside the unrolled loop. + * + * This removes an unpredictable branch and improves performance. + */ + + ld a2, (a0) + xor a2, a2, a1 + + not t2, a2 + sub a2, a2, t0 + and t2, t2, t1 + and a2, a2, t2 + + addi a0, a0, 8 + + bnez a2, .Lfind_zero + + andi a0, a0, ~0b1111 + + /* while(ptr != end_unroll) */ + beq a0, a5, .Lskip_loop +.Lloop: + ld a2, (a0) + ld t3, 8(a0) + + xor a2, a2, a1 + xor t3, t3, a1 + + not t2, a2 + not t4, t3 + sub a2, a2, t0 + sub t3, t3, t0 + and t2, t2, t1 + and t4, t4, t1 + and a2, a2, t2 + and t3, t3, t4 + + addi a0, a0, 8 + + bnez a2, .Lfind_zero + + /* move into iter for find_zero */ + mv a2, t3 + + addi a0, a0, 8 + + bnez a2, .Lfind_zero + + bne a0, a5, .Lloop +.Lskip_loop: + + /* there might be one 8byte left */ + beq a0, a4, .Lno_match + + ld a2, (a0) + xor a2, a2, a1 + + not t2, a2 + sub a2, a2, t0 + and t2, t2, t1 + and a2, a2, t2 + + addi a0, a0, 8 + + beqz a2, .Lno_match + +.Lfind_zero: + /* + * ptr = ptr - 8 + * t1 = 0x0001020304050607 + * iter = iter & (-iter) + * iter = iter >> 7 + * iter = iter * t1 + * iter = iter >> 56 + */ + li t1, 0x10203000 + neg t0, a2 + slli t1, t1, 4 + and a2, a2, t0 + addi t1, t1, 0x405 + srli a2, a2, 7 + slli t1, t1, 16 + addi a0, a0, -8 + addi t1, t1, 0x607 + mul a2, a2, t1 + srli a2, a2, 56 + + /* left = end - ptr */ + sub t0, a3, a0 + + /* return iter < left ? ptr + iter : NULL */ + sltu t1, a2, t0 + neg t1, t1 + add a0, a0, a2 + and a0, a0, t1 + ret + +.Lno_match: + li a0, 0 + ret +END(memchr) diff --git a/lib/libc/riscv/string/memcpy.S b/lib/libc/riscv/string/memcpy.S new file mode 100644 index 000000000000..7536514df777 --- /dev/null +++ b/lib/libc/riscv/string/memcpy.S @@ -0,0 +1,217 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic <strajabot@FreeBSD.org> + */ + +#include <machine/asm.h> + +/* + * a0 - void* dst + * a1 - const void* src + * a2 - size_t len + */ +ENTRY(memcpy) + beqz a2, .Lreturn + + /* diff = (dstv - srcv) & 0b111 */ + sub t0, a0, a1 + andi t0, t0, 0b111 + + sltiu t1, a2, 8 + + /* we never change a0, because memcpy returns the original dst */ + mv a3, a0 + + /* len < 8 */ + bnez t1, .Lend + + /* t1 = (-dst) & 0b111 */ + neg t1, a0 + andi t1, t1, 0b111 + + sub a2, a2, t1 + + la t2, .Lduff_start + slli t3, t1, 3 + sub t2, t2, t3 + jr t2 + lb t3, 6(a1) + sb t3, 6(a3) + lb t3, 5(a1) + sb t3, 5(a3) + lb t3, 4(a1) + sb t3, 4(a3) + lb t3, 3(a1) + sb t3, 3(a3) + lb t3, 2(a1) + sb t3, 2(a3) + lb t3, 1(a1) + sb t3, 1(a3) + lb t3, 0(a1) + sb t3, 0(a3) +.Lduff_start: + + add a1, a1, t1 + add a3, a3, t1 + + beqz a2, .Lreturn + + beqz t0, .Lmemcpy8 + + /* + * a4 - size_t right_shift + * a5 - size_t left_shift + * a6 - size_t whole (number of dword stores) + */ + + /* right_shift = (src % 0b111) * 8; */ + andi a4, a1, 0b111 + slli a4, a4, 3 + + /* left_shift = 64 - right_shift */ + neg a5, a4 + + /* whole = len / 8 */ + srli a6, a2, 3 + + /* len = len % 8 */ + andi a2, a2, 0b111 + + /* t0 - uint64_t* ptr */ + + /* ptr = src & ~0b111 */ + andi t0, a1, ~0b111 + + /* src += whole * 8 */ + slli t1, a6, 3 + add a1, a1, t1 + + /* + * t1 - uint64_t low + * t2 - uint64_t high + */ + + /* low = *ptr++ */ + ld t1, (t0) + addi t0, t0, 8 + + /* low >>= right_shift */ + srl t1, t1, a4 + + beqz a6, .Llmain_skip +.Llmain: + /* high = *ptr++ */ + ld t2, (t0) + addi t0, t0, 8 + + /* whole-- */ + addi a6, a6, -1 + + /* temp = (high << left_shift) | low */ + sll t3, t2, a5 + or t3, t3, t1 + + /* low = high >> right_shift */ + srl t1, t2, a4 + + /* *dst++ = temp */ + sd t3, (a3) + addi a3, a3, 8 + + bnez a6, .Llmain + +.Llmain_skip: + +.Lend: + la t1, .Lduff_end + slli t2, a2, 3 + sub t1, t1, t2 + jr t1 + lb t2, 6(a1) + sb t2, 6(a3) + lb t2, 5(a1) + sb t2, 5(a3) + lb t2, 4(a1) + sb t2, 4(a3) + lb t2, 3(a1) + sb t2, 3(a3) + lb t2, 2(a1) + sb t2, 2(a3) + lb t2, 1(a1) + sb t2, 1(a3) + lb t2, 0(a1) + sb t2, 0(a3) +.Lduff_end: + +.Lreturn: + ret + +/* exectued when dst - src is multiple of 8 + * a0 - void* dst + * a1 - const void* src + * a2 - size_t len + */ +.Lmemcpy8: + + beqz a2, .Lreturn + + slti t0, a2, 128 + bnez t0, .Llmain8_64_skip + + /* a4 - uint64_t* end_unroll */ + + /* end_unroll = dst + len / 64 * 64 */ + andi t0, a2, ~0b111111 + add a4, a3, t0 + + /* len = len % 64 */ + andi a2, a2, 0b111111 + +.Llmain8_64: + ld t0, 0(a1) + ld t1, 8(a1) + ld t2, 16(a1) + ld t3, 24(a1) + sd t0, 0(a3) + sd t1, 8(a3) + sd t2, 16(a3) + sd t3, 24(a3) + ld t0, 32(a1) + ld t1, 40(a1) + ld t2, 48(a1) + ld t3, 56(a1) + sd t0, 32(a3) + sd t1, 40(a3) + sd t2, 48(a3) + sd t3, 56(a3) + addi a3, a3, 64 + addi a1, a1, 64 + bne a3, a4, .Llmain8_64 +.Llmain8_64_skip: + + beqz a2, .Lreturn + + /* a4 - uint64_t* end_align */ + + /* end_align = (dst + len) & ~0b111 */ + add a4, a3, a2 + andi a4, a4, ~0b111 + + /* len = len % 8 */ + andi a2, a2, 0b111 + + beq a3, a4, .Llmain8_skip +.Llmain8: + ld t0, (a1) + sd t0, (a3) + addi a3, a3, 8 + addi a1, a1, 8 + bne a3, a4, .Llmain8 +.Llmain8_skip: + + la t1, .Lduff_end + slli t2, a2, 3 + sub t1, t1, t2 + jr t1 +END(memcpy) diff --git a/lib/libc/riscv/string/memset.S b/lib/libc/riscv/string/memset.S new file mode 100644 index 000000000000..ca435dfdd5c1 --- /dev/null +++ b/lib/libc/riscv/string/memset.S @@ -0,0 +1,95 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic <strajabot@FreeBSD.org> + */ + +#include <machine/asm.h> + +/* + * register a0 - void *dest + * register a1 - int c + * register a2 - size_t len + */ +ENTRY(memset) + andi a1, a1, 0xFF + + sltiu t1, a2, 8 + mv t0, a0 + bnez t1, .Lend + + li t1, 0x0101010101010101 + mul a1, a1, t1 + + andi t1, a0, 0b111 + andi t0, a0, ~0b111 + + beqz t1, .Lloop_store_64 + + la t2, .Lduff_start + slli t3, t1, 2 + add t2, t2, t3 + jr -4(t2) +.Lduff_start: + sb a1, 1(t0) + sb a1, 2(t0) + sb a1, 3(t0) + sb a1, 4(t0) + sb a1, 5(t0) + sb a1, 6(t0) + sb a1, 7(t0) + + /* a3 = a3 -(8-a) <=> a3 = a3 + (a-8) */ + addi t1, t1, -8 + add a2, a2, t1 + addi t0, t0, 8 + +.Lloop_store_64: + slti t1, a2, 64 + bnez t1, .Lstore_rest + sd a1, 0(t0) + sd a1, 8(t0) + sd a1, 16(t0) + sd a1, 24(t0) + sd a1, 32(t0) + sd a1, 40(t0) + sd a1, 48(t0) + sd a1, 56(t0) + addi a2, a2, -64 + addi t0, t0, 64 + j .Lloop_store_64 + +.Lstore_rest: + la t2, .Lduff_rest + andi t3, a2, ~0b111 + srli t4, t3, 1 + sub t2, t2, t4 + jr t2 + sd a1, 56(t0) + sd a1, 48(t0) + sd a1, 40(t0) + sd a1, 32(t0) + sd a1, 24(t0) + sd a1, 16(t0) + sd a1, 8(t0) + sd a1, 0(t0) +.Lduff_rest: + add t0, t0, t3 + sub a2, a2, t3 + +.Lend: + slli a2, a2, 2 + la t2, .Lduff_end + sub t2, t2, a2 + jr t2 + sb a1, 6(t0) + sb a1, 5(t0) + sb a1, 4(t0) + sb a1, 3(t0) + sb a1, 2(t0) + sb a1, 1(t0) + sb a1, (t0) +.Lduff_end: + ret +END(memset) + diff --git a/lib/libc/riscv/string/strchrnul.S b/lib/libc/riscv/string/strchrnul.S new file mode 100644 index 000000000000..8abba71c4199 --- /dev/null +++ b/lib/libc/riscv/string/strchrnul.S @@ -0,0 +1,116 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic <strajabot@FreeBSD.org> + */ + +#include <machine/asm.h> + + .weak strchrnul + .set strchrnul, __strchrnul + +/* + * a0 - const char *str + * a1 - int c; + */ +ENTRY(__strchrnul) + /* + * a0 - const char *ptr; + * a1 - char cccccccc[8]; + * a2 - char iter[8]; + * a3 - char mask_end + */ + + /* int to char */ + andi a1, a1, 0xFF + + /* t0 = 0x0101010101010101 */ + li t0, 0x01010101 + slli t1, t0, 32 + or t0, t0, t1 + + /* t1 = 0x8080808080808080 */ + slli t1, t0, 7 + + /* spread char across bytes */ + mul a1, a1, t0 + + /* align_offset */ + andi t2, a0, 0b111 + + /* align pointer */ + andi a0, a0, ~0b111 + + /* if pointer is aligned skip to loop */ + beqz t2, .Lloop + + ld a2, (a0) + + /* mask_start calculation */ + slli t2, t2, 3 + neg t2, t2 + srl t2, t0, t2 + + /* fill bytes before start with non-zero */ + or a3, a2, t2 + + xor a2, a2, a1 + or a2, a2, t2 + + /* has_zero for \0 */ + not t3, a3 + not t2, a2 + sub a3, a3, t0 + sub a2, a2, t0 + and a3, a3, t3 + and a2, a2, t2 + and a3, a3, t1 + and a2, a2, t1 + + + /* if \0 or c was found, exit */ + or a2, a2, a3 + addi a0, a0, 8 + bnez a2, .Lfind_char + + +.Lloop: + ld a2, (a0) + + /* has_zero for both \0 or c */ + xor a3, a2, a1 + + not t2, a2 + not t3, a3 + sub a2, a2, t0 + sub a3, a3, t0 + and a2, a2, t2 + and a3, a3, t3 + and a2, a2, t1 + and a3, a3, t1 + + /* if \0 or c was found, exit */ + or a2, a2, a3 + addi a0, a0, 8 + beqz a2, .Lloop + +.Lfind_char: + addi a0, a0, -8 + + /* isolate lowest set bit */ + neg t0, a2 + and a2, a2, t0 + + li t0, 0x0001020304050607 + srli a2, a2, 7 + + /* lowest set bit is 2^(8*k) + * multiplying by it shifts the idx array in t0 by k bytes to the left */ + mul a2, a2, t0 + + /* highest byte contains idx of first zero */ + srli a2, a2, 56 + + add a0, a0, a2 + ret +END(__strchrnul) diff --git a/lib/libc/riscv/string/strlen.S b/lib/libc/riscv/string/strlen.S new file mode 100644 index 000000000000..3beb160f2e6f --- /dev/null +++ b/lib/libc/riscv/string/strlen.S @@ -0,0 +1,77 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic <strajabot@FreeBSD.org> + */ + +#include <machine/asm.h> + +/* + * https://graphics.stanford.edu/~seander/bithacks.html#ZeroInWord + * uses haszero(v) (((v) - 0x01010101UL) & ~(v) & 0x80808080UL) + * which evalutates > 0 when there is zero in v + * + * register a0 - char *s + */ +ENTRY(strlen) + /* + * register a0 - char *str_start + * register a1 - char *str_ptr + * register a2 - char[8] iter + */ + + /* load constants for haszero */ + li t0, 0x0101010101010101 + slli t1, t0, 7 # 0x8080808080808080, avoid li + + /* check alignment of str_start */ + andi a1, a0, ~0b111 + ld a2, (a1) + beq a1, a0, .Lhas_zero + + /* fill bytes before str_start with non-zero */ + slli t2, a0, 3 + addi t3, t2, -64 + neg t3, t3 + srl t3, t0, t3 + or a2, a2, t3 + + /* unrolled iteration of haszero */ + not t2, a2 + sub a2, a2, t0 + and a2, a2, t2 + and a2, a2, t1 + + bnez a2, .Lfind_zero + +.Lloop_has_zero: + ld a2, 8(a1) + addi a1, a1, 8 # move ptr to next 8byte +.Lhas_zero: + not t2, a2 + sub a2, a2, t0 + and a2, a2, t2 + and a2, a2, t1 + + beqz a2, .Lloop_has_zero + +.Lfind_zero: + /* use (iter & -iter) to isolate lowest set bit */ + sub a3, zero, a2 #a3 = -iter + and t1, a2, a3 #t1 = (iter & -iter) + + li t0, 0x0001020304050607 + srli t1, t1, 7 + /* + * lowest set bit is 2^(8*k) + * multiplying by it shifts the idx array in t0 by k bytes to the left + */ + mul t1, t1, t0 + /* highest byte contains idx of first zero */ + srli t1, t1, 56 + + add a1, a1, t1 + sub a0, a1, a0 + ret +END(strlen) + diff --git a/lib/libc/riscv/string/strnlen.S b/lib/libc/riscv/string/strnlen.S new file mode 100644 index 000000000000..c0fd959548ff --- /dev/null +++ b/lib/libc/riscv/string/strnlen.S @@ -0,0 +1,143 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic <strajabot@FreeBSD.org> + */ + +#include <machine/asm.h> + +/* + * a0 - const char *s + * a1 - size_t maxlen; + */ +ENTRY(strnlen) + /* + * a0 - const char *s; + * a1 - size_t maxlen; + * a2 - uint64_t *ptr; + * a3 - char iter[8]; + * a4 - uint64_t *end_align; + * a5 - uint64_t *end_unroll; + */ + + beqz a1, .Lnot_found + + /* ptr = s & ~0b111 */ + /* t0 = 0x0101010101010101 */ + /* t1 = 0x8080808080808080 */ + /* end_align = (s + maxlen + 7) & ~0b111 */ + /* mask_start = t0 >> ((-s.value) << 3) */ + add a4, a0, a1 + li t0, 0x01010101 + addi a4, a4, 7 + slli t1, t0, 32 + neg t2, a0 + andi a4, a4, ~0b111 + or t0, t0, t1 + slli t2, t2, 3 + andi a2, a0, ~0b111 + slli t1, t0, 7 + srl t2, t0, t2 + + /* if pointer is aligned skip to loop */ + beq a0, a2, .Lskip_start + + /* iter = *ptr */ + ld a3, (a2) + + /* iter = iter | mask_start */ + or a3, a3, t2 + + /* has_zero */ + not t2, a3 + sub a3, a3, t0 + and t2, t2, t1 + and a3, a3, t2 + + addi a2, a2, 8 + bnez a3, .Lfind_zero + +.Lskip_start: + /* end_unroll */ + sub t2, a4, a2 + andi t2, t2, ~0b1111 + add a5, a2, t2 + + /* while (ptr != end_unroll) */ + beq a2, a5, .Lskip_loop +.Lloop: + ld a3, (a2) + ld a6, 8(a2) + + /* has_zero */ + not t2, a3 + not t3, a6 + sub a3, a3, t0 + sub a6, a6, t0 + and t2, t2, t1 + and t3, t3, t1 + and a3, a3, t2 + and a6, a6, t3 + + addi a2, a2, 8 + bnez a3, .Lfind_zero + + mv a3, a6 + + addi a2, a2, 8 + bnez a3, .Lfind_zero + + bne a2, a5, .Lloop + +.Lskip_loop: + + beq a2, a4, .Lnot_found + + ld a3, (a2) + + /* has_zero */ + not t2, a3 + sub a3, a3, t0 + and t2, t2, t1 + and a3, a3, t2 + + + addi a2, a2, 8 + beqz a3, .Lnot_found + +.Lfind_zero: + + /* move ptr back */ + addi a2, a2, -8 + + /* isolate lowest set bit */ + neg t0, a3 + and a3, a3, t0 + + li t0, 0x0001020304050607 + srli a3, a3, 7 + + /* lowest set bit is 2^(8*k) + * multiplying by it shifts the idx array in t0 by k bytes to the left */ + mul a3, a3, t0 + + /* highest byte contains idx of first zero */ + srli a3, a3, 56 + + /* zero_idx */ + sub a2, a2, a0 + add a2, a2, a3 + + /* min(zero_idx, maxlen) */ + sub a2, a2, a1 + srai t1, a2, 63 + and a2, a2, t1 + add a0, a1, a2 + + ret + +.Lnot_found: + mv a0, a1 + ret + +END(strnlen) diff --git a/lib/libc/riscv/string/strrchr.S b/lib/libc/riscv/string/strrchr.S new file mode 100644 index 000000000000..e922a692e77f --- /dev/null +++ b/lib/libc/riscv/string/strrchr.S @@ -0,0 +1,127 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2024 Strahinja Stanisic <strajabot@FreeBSD.org> + */ + +#include <machine/asm.h> + + .weak rindex + .set rindex, strrchr + +/* + * a0 - const char *s + * a1 - int c + */ +ENTRY(strrchr) + /* + * a0 - const char *ptr_align + * a1 - temporary + * a2 - temporary + * a3 - temporary + * a4 - temporary + * a5 - const char[8] cccccccc + * a6 - const uint64_t *save_align + * a7 - const uint64_t save_iter + * t0 - const uintr64_t REP8_0X01 + * t1 - const uintr64_t REP8_0X80 + */ + + /* + * save_align = 0 + * save_iter = 0xFFFFFFFFFFFFFF00 + * REP8_0X01 = 0x0101010101010101 + * cccccccc = (char)c * REP8_0X01 + * REP8_0X80 = (REP8_0X80 << 7) << ((str % 8) * 8) + * ptr_align = str - str % 8 + */ + li t0, 0x01010101 + li a6, 0 + slli a2, a0, 3 + slli t1, t0, 32 + li a7, 0xFFFFFFFFFFFFFF00 + or t0, t0, t1 + andi a1, a1, 0xFF + slli t1, t0, 7 + andi a0, a0, ~0b111 + mul a5, a1, t0 + sll t1, t1, a2 + +.Lloop: /* do { */ + ld a1, 0(a0) /* a1 -> data = *ptr_align */ + not a3, a1 /* a3 -> nhz = ~data */ + xor a2, a1, a5 /* a2 -> iter = data ^ cccccccc */ + sub a1, a1, t0 /* a1 -> hz = data - REP8_0X01 */ + not a4, a2 /* a4 -> nhc = ~iter */ + and a1, a1, a3 /* hz = hz & nhz */ + sub a3, a2, t0 /* a3 -> hc = iter - REP8_0X01 */ + and a1, a1, t1 /* hz = hz & REP8_0X80 */ + and a3, a3, a4 /* hc = hc & nhc */ + addi a4, a1, -1 /* a4 -> mask_end = hz - 1 */ + and a3, a3, t1 /* hc = hc & REP8_0X80 */ + xor a4, a4, a1 /* mask_end = mask_end ^ hz */ + addi a0, a0, 8 /* ptr_align = ptr_align + 8 */ + and a3, a3, a4 /* hc = hc & mask_end */ + slli t1, t0, 7 /* REP8_0X80 = REP8_0X01 << 7 */ + not a4, a4 /* mask_end = ~mask_end */ + + beqz a3, .Lskip_save /* if(!hc) goto skip_save */ + or a2, a2, a4 /* iter = iter | mask_end */ + addi a6, a0, -8 /* save_align = ptr_align - 8 */ + mv a7, a2 /* save_iter = iter */ + +.Lskip_save: + beqz a1, .Lloop /* } while(!hz) */ + +.Lfind_char: + /* + * a1 -> iter = save_iter + * a2 -> mask_iter = 0xFF00000000000000 + * a3 -> match_off = 7 + */ + li a2, 0xFF + mv a1, a7 + slli a2, a2, 56 + li a3, 7 + + and a0, a1, a2 + srli a2, a2, 8 + beqz a0, .Lret + + addi a3, a3, -1 + and a0, a1, a2 + srli a2, a2, 8 + beqz a0, .Lret + + addi a3, a3, -1 + and a0, a1, a2 + srli a2, a2, 8 + beqz a0, .Lret + + addi a3, a3, -1 + and a0, a1, a2 + srli a2, a2, 8 + beqz a0, .Lret + + addi a3, a3, -1 + and a0, a1, a2 + srli a2, a2, 8 + beqz a0, .Lret + + addi a3, a3, -1 + and a0, a1, a2 + srli a2, a2, 8 + beqz a0, .Lret + + addi a3, a3, -1 + and a0, a1, a2 + srli a2, a2, 8 + beqz a0, .Lret + + addi a3, a3, -1 + +.Lret: + /* return save_align + match_offset */ + add a0, a6, a3 + ret +END(strrchr) diff --git a/lib/libc/rpc/Makefile.inc b/lib/libc/rpc/Makefile.inc index 87963d10eec1..c22fac2c0e16 100644 --- a/lib/libc/rpc/Makefile.inc +++ b/lib/libc/rpc/Makefile.inc @@ -42,12 +42,12 @@ crypt_xdr.c: ${RPCDIR}/crypt.x crypt.h crypt.h: ${RPCDIR}/crypt.x ${RPCGEN} -h -o ${.TARGET} ${RPCDIR}/crypt.x + MAN+= bindresvport.3 des_crypt.3 getnetconfig.3 getnetpath.3 getrpcent.3 \ getrpcport.3 rpc.3 rpc_soc.3 rpc_clnt_auth.3 rpc_clnt_calls.3 \ rpc_clnt_create.3 rpc_svc_calls.3 rpc_svc_create.3 rpc_svc_err.3 \ rpc_svc_reg.3 rpc_xdr.3 rpcbind.3 publickey.3 rpc_secure.3 \ rtime.3 -MAN+= rpc.5 netconfig.5 MLINKS+= bindresvport.3 bindresvport_sa.3 \ des_crypt.3 ecb_crypt.3 \ des_crypt.3 cbc_crypt.3 \ @@ -177,3 +177,5 @@ MLINKS+= bindresvport.3 bindresvport_sa.3 \ rpc_soc.3 xdr_authunix_parms.3 \ rpc_soc.3 xdr_pmap.3 \ rpc_soc.3 xdr_pmaplist.3 + +MANNODEV+= rpc.5 netconfig.5 diff --git a/lib/libc/rpc/rpc_generic.c b/lib/libc/rpc/rpc_generic.c index 0e563f6a5996..8019f2d8f236 100644 --- a/lib/libc/rpc/rpc_generic.c +++ b/lib/libc/rpc/rpc_generic.c @@ -610,6 +610,10 @@ __rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf) return NULL; break; #endif + case AF_NETLINK: + if (asprintf(&ret, "%s", (char *)nbuf->buf) < 0) + return NULL; + break; case AF_LOCAL: sun = nbuf->buf; if (asprintf(&ret, "%.*s", (int)(sun->sun_len - diff --git a/lib/libc/stdbit/Makefile.inc b/lib/libc/stdbit/Makefile.inc new file mode 100644 index 000000000000..fceccb12b589 --- /dev/null +++ b/lib/libc/stdbit/Makefile.inc @@ -0,0 +1,34 @@ +# sources for <stdbit.h> +.PATH: ${LIBC_SRCTOP}/stdbit + +SRCS+= stdc_bit_ceil.c \ + stdc_bit_floor.c \ + stdc_bit_width.c \ + stdc_count_ones.c \ + stdc_count_zeros.c \ + stdc_first_leading_one.c \ + stdc_first_leading_zero.c \ + stdc_first_trailing_one.c \ + stdc_first_trailing_zero.c \ + stdc_has_single_bit.c \ + stdc_leading_ones.c \ + stdc_leading_zeros.c \ + stdc_trailing_ones.c \ + stdc_trailing_zeros.c + +SYM_MAPS+=${LIBC_SRCTOP}/stdbit/Symbol.map + +MAN+= stdc_bit_ceil.3 \ + stdc_bit_floor.3 \ + stdc_bit_width.3 \ + stdc_count_ones.3 \ + stdc_count_zeros.3 \ + stdc_first_leading_one.3 \ + stdc_first_leading_zero.3 \ + stdc_first_trailing_one.3 \ + stdc_first_trailing_zero.3 \ + stdc_has_single_bit.3 \ + stdc_leading_ones.3 \ + stdc_leading_zeros.3 \ + stdc_trailing_ones.3 \ + stdc_trailing_zeros.3 diff --git a/lib/libc/stdbit/Symbol.map b/lib/libc/stdbit/Symbol.map new file mode 100644 index 000000000000..7014d6984c07 --- /dev/null +++ b/lib/libc/stdbit/Symbol.map @@ -0,0 +1,85 @@ +FBSD_1.9 { + stdc_leading_zeros_uc; + stdc_leading_zeros_us; + stdc_leading_zeros_ui; + stdc_leading_zeros_ul; + stdc_leading_zeros_ull; + + stdc_leading_ones_uc; + stdc_leading_ones_us; + stdc_leading_ones_ui; + stdc_leading_ones_ul; + stdc_leading_ones_ull; + + stdc_trailing_zeros_uc; + stdc_trailing_zeros_us; + stdc_trailing_zeros_ui; + stdc_trailing_zeros_ul; + stdc_trailing_zeros_ull; + + stdc_trailing_ones_uc; + stdc_trailing_ones_us; + stdc_trailing_ones_ui; + stdc_trailing_ones_ul; + stdc_trailing_ones_ull; + + stdc_first_leading_zero_uc; + stdc_first_leading_zero_us; + stdc_first_leading_zero_ui; + stdc_first_leading_zero_ul; + stdc_first_leading_zero_ull; + + stdc_first_leading_one_uc; + stdc_first_leading_one_us; + stdc_first_leading_one_ui; + stdc_first_leading_one_ul; + stdc_first_leading_one_ull; + + stdc_first_trailing_zero_uc; + stdc_first_trailing_zero_us; + stdc_first_trailing_zero_ui; + stdc_first_trailing_zero_ul; + stdc_first_trailing_zero_ull; + + stdc_first_trailing_one_uc; + stdc_first_trailing_one_us; + stdc_first_trailing_one_ui; + stdc_first_trailing_one_ul; + stdc_first_trailing_one_ull; + + stdc_count_zeros_uc; + stdc_count_zeros_us; + stdc_count_zeros_ui; + stdc_count_zeros_ul; + stdc_count_zeros_ull; + + stdc_count_ones_uc; + stdc_count_ones_us; + stdc_count_ones_ui; + stdc_count_ones_ul; + stdc_count_ones_ull; + + stdc_has_single_bit_uc; + stdc_has_single_bit_us; + stdc_has_single_bit_ui; + stdc_has_single_bit_ul; + stdc_has_single_bit_ull; + + stdc_bit_width_uc; + stdc_bit_width_us; + stdc_bit_width_ui; + stdc_bit_width_ul; + stdc_bit_width_ull; + + stdc_bit_floor_uc; + stdc_bit_floor_us; + stdc_bit_floor_ui; + stdc_bit_floor_ul; + stdc_bit_floor_ull; + + stdc_bit_ceil_uc; + stdc_bit_ceil_us; + stdc_bit_ceil_ui; + stdc_bit_ceil_ul; + stdc_bit_ceil_ull; +}; diff --git a/lib/libc/stdbit/stdc_bit_ceil.3 b/lib/libc/stdbit/stdc_bit_ceil.3 new file mode 100644 index 000000000000..470b255f471e --- /dev/null +++ b/lib/libc/stdbit/stdc_bit_ceil.3 @@ -0,0 +1,81 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_BIT_CEIL 3 +.Os +.Sh NAME +.Nm stdc_bit_ceil +.Nd round up to the next power of\~2 +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned char +.Fn stdc_bit_ceil_uc "unsigned char value" +.Ft unsigned short +.Fn stdc_bit_ceil_us "unsigned short value" +.Ft unsigned int +.Fn stdc_bit_ceil_ui "unsigned int value" +.Ft unsigned long +.Fn stdc_bit_ceil_ul "unsigned long value" +.Ft unsigned long long +.Fn stdc_bit_ceil_ull "unsigned long long value" +.Ft typeof Ns Pq Em value +.Fn stdc_bit_ceil "value" +.Sh DESCRIPTION +The +.Nm stdc_bit_ceil_ Ns Em type Ns () +family of functions rounds +.Fa value +up to the next power of\~2, where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_bit_ceil "value" +type-generic macro picks the appropriate +.Nm stdc_bit_ceil_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns +.Fa value +round up to the next power of\~2. +If this value is not representable, +0\~is returned. +.Sh SEE ALSO +.Xr stdbit 3 , +.Xr stdc_bit_floor 3 +.Sh STANDARDS +The +.Nm stdc_bit_ceil_ Ns Em type Ns () +family of functions and the +.Fn stdc_bit_ceil +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_bit_ceil_ Ns Em type Ns () +family of functions and the +.Fn stdc_bit_ceil +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_bit_ceil.c b/lib/libc/stdbit/stdc_bit_ceil.c new file mode 100644 index 000000000000..2dfd7bbcbea2 --- /dev/null +++ b/lib/libc/stdbit/stdc_bit_ceil.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <assert.h> +#include <limits.h> +#include <stdbit.h> + +/* Ensure we don't shift 1U out of range. */ +static_assert(UCHAR_WIDTH < UINT_WIDTH, + "stdc_bit_ceil_uc needs UCHAR_WIDTH < UINT_WIDTH"); + +unsigned char +stdc_bit_ceil_uc(unsigned char x) +{ + if (x <= 1) + return (1); + + return (1U << (UINT_WIDTH - __builtin_clz(x - 1))); +} + +/* Ensure we don't shift 1U out of range. */ +static_assert(USHRT_WIDTH < UINT_WIDTH, + "stdc_bit_ceil_us needs USHRT_WIDTH < UINT_WIDTH"); + +unsigned short +stdc_bit_ceil_us(unsigned short x) +{ + if (x <= 1) + return (1); + + return (1U << (UINT_WIDTH - __builtin_clz(x - 1))); +} + +unsigned int +stdc_bit_ceil_ui(unsigned int x) +{ + if (x <= 1) + return (1); + + if (x > UINT_MAX/2 + 1) + return (0); + + return (1U << (UINT_WIDTH - __builtin_clz(x - 1))); +} + +unsigned long +stdc_bit_ceil_ul(unsigned long x) +{ + if (x <= 1) + return (1); + + if (x > ULONG_MAX/2 + 1) + return (0); + + return (1UL << (ULONG_WIDTH - __builtin_clzl(x - 1))); +} + +unsigned long long +stdc_bit_ceil_ull(unsigned long long x) +{ + if (x <= 1) + return (1); + + if (x > ULLONG_MAX/2 + 1) + return (0); + + return (1ULL << (ULLONG_WIDTH - __builtin_clzll(x - 1))); +} diff --git a/lib/libc/stdbit/stdc_bit_floor.3 b/lib/libc/stdbit/stdc_bit_floor.3 new file mode 100644 index 000000000000..3e90c34f070f --- /dev/null +++ b/lib/libc/stdbit/stdc_bit_floor.3 @@ -0,0 +1,83 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_BIT_FLOOR 3 +.Os +.Sh NAME +.Nm stdc_bit_floor +.Nd round down to the previous power of\~2 +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned char +.Fn stdc_bit_floor_uc "unsigned char value" +.Ft unsigned short +.Fn stdc_bit_floor_us "unsigned short value" +.Ft unsigned int +.Fn stdc_bit_floor_ui "unsigned int value" +.Ft unsigned long +.Fn stdc_bit_floor_ul "unsigned long value" +.Ft unsigned long long +.Fn stdc_bit_floor_ull "unsigned long long value" +.Ft typeof Ns Pq Em value +.Fn stdc_bit_floor "value" +.Sh DESCRIPTION +The +.Nm stdc_bit_floor_ Ns Em type Ns () +family of functions rounds +.Fa value +down to the previous power of\~2, where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_bit_floor "value" +type-generic macro picks the appropriate +.Nm stdc_bit_floor_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns +.Fa value +round down to the previous power of\~2. +If +.Fa value +is equal to zero, +zero is returned. +.Sh SEE ALSO +.Xr stdbit 3 , +.Xr stdc_bit_ceil 3 +.Sh STANDARDS +The +.Nm stdc_bit_floor_ Ns Em type Ns () +family of functions and the +.Fn stdc_bit_floor +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_bit_floor_ Ns Em type Ns () +family of functions and the +.Fn stdc_bit_floor +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_bit_floor.c b/lib/libc/stdbit/stdc_bit_floor.c new file mode 100644 index 000000000000..0a491d6ec7b3 --- /dev/null +++ b/lib/libc/stdbit/stdc_bit_floor.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <limits.h> +#include <stdbit.h> + +unsigned char +stdc_bit_floor_uc(unsigned char x) +{ + if (x == 0) + return (0); + + return (1U << (UINT_WIDTH - __builtin_clz(x) - 1)); +} + +unsigned short +stdc_bit_floor_us(unsigned short x) +{ + if (x == 0) + return (0); + + return (1U << (UINT_WIDTH - __builtin_clz(x) - 1)); +} + +unsigned int +stdc_bit_floor_ui(unsigned int x) +{ + if (x == 0) + return (0); + + return (1U << (UINT_WIDTH - __builtin_clz(x) - 1)); +} + +unsigned long +stdc_bit_floor_ul(unsigned long x) +{ + if (x == 0) + return (0); + + return (1UL << (ULONG_WIDTH - __builtin_clzl(x) - 1)); +} + +unsigned long long +stdc_bit_floor_ull(unsigned long long x) +{ + if (x == 0) + return (0); + + return (1ULL << (ULLONG_WIDTH - __builtin_clzll(x) - 1)); +} diff --git a/lib/libc/stdbit/stdc_bit_width.3 b/lib/libc/stdbit/stdc_bit_width.3 new file mode 100644 index 000000000000..8d59886818d3 --- /dev/null +++ b/lib/libc/stdbit/stdc_bit_width.3 @@ -0,0 +1,104 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_BIT_WIDTH 3 +.Os +.Sh NAME +.Nm stdc_bit_width +.Nd find the first trailing one in an intege +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_bit_width_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_bit_width_us "unsigned short value" +.Ft unsigned int +.Fn stdc_bit_width_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_bit_width_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_bit_width_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_bit_width "value" +.Sh DESCRIPTION +The +.Nm stdc_bit_width_ Ns Em type Ns () +family of functions returns the number of bits needed to represent +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_bit_width "value" +type-generic macro picks the appropriate +.Nm stdc_bit_width_ Ns Em type Ns () +function based on the type of +.Fa value . +.Pp +Functions +.Fn stdc_bit_width_ui , +.Fn stdc_bit_width_ul , +and +.Fn stdc_bit_width_ull +are identical to +.Bx 4.3 +functions +.Xr fls 3 , +.Xr flsl 3 , +and +.Xr flsll 3 +respectively, except for operating on unsigned instead of signed values. +.Sh RETURN VALUES +Returns the least number of bits needed to represent +.Fa value . +If +.Fa value +is zero, the return value is zero. +Otherwise it is +.EQ +1 + \(lf log sub 2 italic value \(rf . +.EN +.Sh SEE ALSO +.Xr bit_fls 3 , +.Xr fls 3 , +.Xr flsl 3 , +.Xr flsll 3 , +.Xr stdbit 3 , +.Xr stdc_count_leading_zeros 3 , +.Xr stdc_first_leading_one 3 +.Sh STANDARDS +The +.Nm stdc_bit_width_ Ns Em type Ns () +family of functions and the +.Fn stdc_bit_width +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_bit_width_ Ns Em type Ns () +family of functions and the +.Fn stdc_bit_width +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_bit_width.c b/lib/libc/stdbit/stdc_bit_width.c new file mode 100644 index 000000000000..e248fae48e9f --- /dev/null +++ b/lib/libc/stdbit/stdc_bit_width.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <limits.h> +#include <stdbit.h> + +unsigned int +stdc_bit_width_uc(unsigned char x) +{ + if (x == 0) + return (0); + + return (UINT_WIDTH - __builtin_clz(x)); +} + +unsigned int +stdc_bit_width_us(unsigned short x) +{ + if (x == 0) + return (0); + + return (UINT_WIDTH - __builtin_clz(x)); +} + +unsigned int +stdc_bit_width_ui(unsigned int x) +{ + if (x == 0) + return (0); + + return (UINT_WIDTH - __builtin_clz(x)); +} + +unsigned int +stdc_bit_width_ul(unsigned long x) +{ + if (x == 0) + return (0); + + return (ULONG_WIDTH - __builtin_clzl(x)); +} + +unsigned int +stdc_bit_width_ull(unsigned long long x) +{ + if (x == 0) + return (0); + + return (ULLONG_WIDTH - __builtin_clzll(x)); +} diff --git a/lib/libc/stdbit/stdc_count_ones.3 b/lib/libc/stdbit/stdc_count_ones.3 new file mode 100644 index 000000000000..6de92216c24b --- /dev/null +++ b/lib/libc/stdbit/stdc_count_ones.3 @@ -0,0 +1,85 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_COUNT_ONES 3 +.Os +.Sh NAME +.Nm stdc_count_ones +.Nd count the ones in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_count_ones_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_count_ones_us "unsigned short value" +.Ft unsigned int +.Fn stdc_count_ones_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_count_ones_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_count_ones_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_count_ones "value" +.Sh DESCRIPTION +The +.Nm stdc_count_ones_ Ns Em type Ns () +family of functions returns the number of bits set to\~1 in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +This is also known as the +.Dq population count +(popcount) or +.Dq Hamming weight +operation. +The +.Fn stdc_count_ones "value" +type-generic macro picks the appropriate +.Nm stdc_count_ones_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the number of bits set to\~1 in +.Fa value . +.Sh SEE ALSO +.Xr bit_count 3 , +.Xr stdbit 3 , +.Xr stdc_count_zeros 3 , +.Xr stdc_has_single_bit 3 +.Sh STANDARDS +The +.Nm stdc_count_ones_ Ns Em type Ns () +family of functions and the +.Fn stdc_count_ones +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_count_ones_ Ns Em type Ns () +family of functions and the +.Fn stdc_count_ones +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_count_ones.c b/lib/libc/stdbit/stdc_count_ones.c new file mode 100644 index 000000000000..c5198dca84df --- /dev/null +++ b/lib/libc/stdbit/stdc_count_ones.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <limits.h> +#include <stdbit.h> + +unsigned int +stdc_count_ones_uc(unsigned char x) +{ + return (__builtin_popcount(x)); +} + +unsigned int +stdc_count_ones_us(unsigned short x) +{ + return (__builtin_popcount(x)); +} + +unsigned int +stdc_count_ones_ui(unsigned int x) +{ + return (__builtin_popcount(x)); +} + +unsigned int +stdc_count_ones_ul(unsigned long x) +{ + return (__builtin_popcountl(x)); +} + +unsigned int +stdc_count_ones_ull(unsigned long long x) +{ + return (__builtin_popcountll(x)); +} diff --git a/lib/libc/stdbit/stdc_count_zeros.3 b/lib/libc/stdbit/stdc_count_zeros.3 new file mode 100644 index 000000000000..762ba62d3793 --- /dev/null +++ b/lib/libc/stdbit/stdc_count_zeros.3 @@ -0,0 +1,84 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_COUNT_ZEROS 3 +.Os +.Sh NAME +.Nm stdc_count_zeros +.Nd count the zeros in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_count_zeros_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_count_zeros_us "unsigned short value" +.Ft unsigned int +.Fn stdc_count_zeros_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_count_zeros_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_count_zeros_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_count_zeros "value" +.Sh DESCRIPTION +The +.Nm stdc_count_zeros_ Ns Em type Ns () +family of functions returns the number of bits set to\~0 in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +This is also known as the +.Dq population count +(popcount) or +.Dq Hamming weight +of the complement of +.Fa value . +The +.Fn stdc_count_zeros "value" +type-generic macro picks the appropriate +.Nm stdc_count_zeros_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the number of bits set to\~0 in +.Fa value . +.Sh SEE ALSO +.Xr stdbit 3 , +.Xr stdc_count_ones 3 +.Sh STANDARDS +The +.Nm stdc_count_zeros_ Ns Em type Ns () +family of functions and the +.Fn stdc_count_zeros +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_count_zeros_ Ns Em type Ns () +family of functions and the +.Fn stdc_count_zeros +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_count_zeros.c b/lib/libc/stdbit/stdc_count_zeros.c new file mode 100644 index 000000000000..35db965afcb5 --- /dev/null +++ b/lib/libc/stdbit/stdc_count_zeros.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <limits.h> +#include <stdbit.h> + +unsigned int +stdc_count_zeros_uc(unsigned char x) +{ + return (__builtin_popcount(x ^ UCHAR_MAX)); +} + +unsigned int +stdc_count_zeros_us(unsigned short x) +{ + return (__builtin_popcount(x ^ USHRT_MAX)); +} + +unsigned int +stdc_count_zeros_ui(unsigned int x) +{ + return (__builtin_popcount(~x)); +} + +unsigned int +stdc_count_zeros_ul(unsigned long x) +{ + return (__builtin_popcountl(~x)); +} + +unsigned int +stdc_count_zeros_ull(unsigned long long x) +{ + return (__builtin_popcountll(~x)); +} diff --git a/lib/libc/stdbit/stdc_first_leading_one.3 b/lib/libc/stdbit/stdc_first_leading_one.3 new file mode 100644 index 000000000000..48f62d997043 --- /dev/null +++ b/lib/libc/stdbit/stdc_first_leading_one.3 @@ -0,0 +1,93 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_FIRST_LEADING_ONE 3 +.Os +.Sh NAME +.Nm stdc_first_leading_one +.Nd find the first leading one in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_first_leading_one_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_first_leading_one_us "unsigned short value" +.Ft unsigned int +.Fn stdc_first_leading_one_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_first_leading_one_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_first_leading_one_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_first_leading_one "value" +.Sh DESCRIPTION +The +.Nm stdc_first_leading_one_ Ns Em type Ns () +family of functions returns the index of the most significant set bit in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_first_leading_one "value" +type-generic macro picks the appropriate +.Nm stdc_first_leading_one_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the index of the most significant set bit in +.Fa value . +The bits are numbered such that the most significant bit has number\~1, +and the least significant bit has number +.Ms w +where +.Ms w +is the number of bits in the type of +.Fa value . +If no bits are set in +.Fa value +(i.\^e.\& +.Fa value +is zero), zero is returned. +.Sh SEE ALSO +.Xr fls 3 , +.Xr stdbit 3 , +.Xr stdc_leading_zeros 3 , +.Xr stdc_first_leading_zero 3 , +.Xr stdc_first_trailing_one 3 +.Sh STANDARDS +The +.Nm stdc_first_leading_one_ Ns Em type Ns () +family of functions and the +.Fn stdc_first_leading_one +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_first_leading_one_ Ns Em type Ns () +family of functions and the +.Fn stdc_first_leading_one +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_first_leading_one.c b/lib/libc/stdbit/stdc_first_leading_one.c new file mode 100644 index 000000000000..de436856569f --- /dev/null +++ b/lib/libc/stdbit/stdc_first_leading_one.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <limits.h> +#include <stdbit.h> + +unsigned int +stdc_first_leading_one_uc(unsigned char x) +{ + const int offset = UINT_WIDTH - UCHAR_WIDTH; + + if (x == 0) + return (0); + + return (__builtin_clz(x << offset) + 1); +} + +unsigned int +stdc_first_leading_one_us(unsigned short x) +{ + const int offset = UINT_WIDTH - USHRT_WIDTH; + + if (x == 0) + return (0); + + return (__builtin_clz(x << offset) + 1); +} + +unsigned int +stdc_first_leading_one_ui(unsigned int x) +{ + if (x == 0) + return (0); + + return (__builtin_clz(x) + 1); +} + +unsigned int +stdc_first_leading_one_ul(unsigned long x) +{ + if (x == 0) + return (0); + + return (__builtin_clzl(x) + 1); +} + +unsigned int +stdc_first_leading_one_ull(unsigned long long x) +{ + if (x == 0) + return (0); + + return (__builtin_clzll(x) + 1); +} diff --git a/lib/libc/stdbit/stdc_first_leading_zero.3 b/lib/libc/stdbit/stdc_first_leading_zero.3 new file mode 100644 index 000000000000..317b6263acb0 --- /dev/null +++ b/lib/libc/stdbit/stdc_first_leading_zero.3 @@ -0,0 +1,92 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_FIRST_LEADING_ZERO 3 +.Os +.Sh NAME +.Nm stdc_first_leading_zero +.Nd find the first leading zero in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_first_leading_zero_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_first_leading_zero_us "unsigned short value" +.Ft unsigned int +.Fn stdc_first_leading_zero_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_first_leading_zero_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_first_leading_zero_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_first_leading_zero "value" +.Sh DESCRIPTION +The +.Nm stdc_first_leading_zero_ Ns Em type Ns () +family of functions returns the index of the most significant clear bit in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_first_leading_zero "value" +type-generic macro picks the appropriate +.Nm stdc_first_leading_zero_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the index of the most significant clear bit in +.Fa value . +The bits are numbered such that the most significant bit has number\~1, +and the least significant bit has number +.Ms w +where +.Ms w +is the number of bits in the type of +.Fa value . +If no bits are clear in +.Fa value +(i.\^e.\& +.Fa value +is the bitwise complement of zero), zero is returned. +.Sh SEE ALSO +.Xr stdbit 3 , +.Xr stdc_leading_ones 3 , +.Xr stdc_first_leading_ones 3 , +.Xr stdc_first_trailing_zero 3 +.Sh STANDARDS +The +.Nm stdc_first_leading_zero_ Ns Em type Ns () +family of functions and the +.Fn stdc_first_leading_zero +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_first_leading_zero_ Ns Em type Ns () +family of functions and the +.Fn stdc_first_leading_zero +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_first_leading_zero.c b/lib/libc/stdbit/stdc_first_leading_zero.c new file mode 100644 index 000000000000..e6609b51b648 --- /dev/null +++ b/lib/libc/stdbit/stdc_first_leading_zero.c @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <limits.h> +#include <stdbit.h> + +unsigned int +stdc_first_leading_zero_uc(unsigned char x) +{ + const int offset = UINT_WIDTH - UCHAR_WIDTH; + + if (x == UCHAR_MAX) + return (0); + + return (__builtin_clz(~(unsigned int)x << offset) + 1); +} + +unsigned int +stdc_first_leading_zero_us(unsigned short x) +{ + const int offset = UINT_WIDTH - USHRT_WIDTH; + + if (x == USHRT_MAX) + return (0); + + return (__builtin_clz(~(unsigned int)x << offset) + 1); +} + +unsigned int +stdc_first_leading_zero_ui(unsigned int x) +{ + if (x == ~0U) + return (0); + + return (__builtin_clz(~x) + 1); +} + +unsigned int +stdc_first_leading_zero_ul(unsigned long x) +{ + if (x == ~0UL) + return (0); + + return (__builtin_clzl(~x) + 1); +} + +unsigned int +stdc_first_leading_zero_ull(unsigned long long x) +{ + if (x == ~0ULL) + return (0); + + return (__builtin_clzll(~x) + 1); +} diff --git a/lib/libc/stdbit/stdc_first_trailing_one.3 b/lib/libc/stdbit/stdc_first_trailing_one.3 new file mode 100644 index 000000000000..0b590ac39732 --- /dev/null +++ b/lib/libc/stdbit/stdc_first_trailing_one.3 @@ -0,0 +1,110 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_FIRST_TRAILING_ONE 3 +.Os +.Sh NAME +.Nm stdc_first_trailing_one +.Nd find the first trailing one in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_first_trailing_one_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_first_trailing_one_us "unsigned short value" +.Ft unsigned int +.Fn stdc_first_trailing_one_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_first_trailing_one_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_first_trailing_one_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_first_trailing_one "value" +.Sh DESCRIPTION +The +.Nm stdc_first_trailing_one_ Ns Em type Ns () +family of functions returns the index of the least significant set bit in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_first_trailing_one "value" +type-generic macro picks the appropriate +.Nm stdc_first_trailing_one_ Ns Em type Ns () +function based on the type of +.Fa value . +.Pp +Functions +.Fn stdc_first_trailing_one_ui , +.Fn stdc_first_trailing_one_ul , +and +.Fn stdc_first_trailing_one_ull +are identical to +.Bx 4.3 +functions +.Xr ffs 3 , +.Xr ffsl 3 , +and +.Xr ffsll 3 +respectively, except for operating on unsigned instead of signed values. +.Sh RETURN VALUES +Returns the index of the least significant set bit in +.Fa value . +The bits are numbered such that the least significant bit has number\~1, +and the most significant bit has number +.Ms w +where +.Ms w +is the number of bits in the type of +.Fa value . +If no bits are set in +.Fa value +(i.\^e.\& +.Fa value +is zero), one is returned. +.Sh SEE ALSO +.Xr bit_ffs 3 , +.Xr ffs 3 , +.Xr ffsl 3 , +.Xr ffsll 3 , +.Xr stdbit 3 , +.Xr stdc_trailing_zeros 3 , +.Xr stdc_first_trailing_zero 3 , +.Xr stdc_first_leading_one 3 +.Sh STANDARDS +The +.Nm stdc_first_trailing_one_ Ns Em type Ns () +family of functions and the +.Fn stdc_first_trailing_one +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_first_trailing_one_ Ns Em type Ns () +family of functions and the +.Fn stdc_first_trailing_one +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_first_trailing_one.c b/lib/libc/stdbit/stdc_first_trailing_one.c new file mode 100644 index 000000000000..22972758d25d --- /dev/null +++ b/lib/libc/stdbit/stdc_first_trailing_one.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <stdbit.h> + +unsigned int +stdc_first_trailing_one_uc(unsigned char x) +{ + if (x == 0) + return (0); + + return (__builtin_ctz(x) + 1); +} + +unsigned int +stdc_first_trailing_one_us(unsigned short x) +{ + if (x == 0) + return (0); + + return (__builtin_ctz(x) + 1); +} + +unsigned int +stdc_first_trailing_one_ui(unsigned int x) +{ + if (x == 0) + return (0); + + return (__builtin_ctz(x) + 1); +} + +unsigned int +stdc_first_trailing_one_ul(unsigned long x) +{ + if (x == 0) + return (0); + + return (__builtin_ctzl(x) + 1); +} + +unsigned int +stdc_first_trailing_one_ull(unsigned long long x) +{ + if (x == 0) + return (0); + + return (__builtin_ctzll(x) + 1); +} diff --git a/lib/libc/stdbit/stdc_first_trailing_zero.3 b/lib/libc/stdbit/stdc_first_trailing_zero.3 new file mode 100644 index 000000000000..bc4f7982d765 --- /dev/null +++ b/lib/libc/stdbit/stdc_first_trailing_zero.3 @@ -0,0 +1,93 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_FIRST_TRAILING_ZERO 3 +.Os +.Sh NAME +.Nm stdc_first_trailing_zero +.Nd find the first trailing zero in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_first_trailing_zero_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_first_trailing_zero_us "unsigned short value" +.Ft unsigned int +.Fn stdc_first_trailing_zero_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_first_trailing_zero_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_first_trailing_zero_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_first_trailing_zero "value" +.Sh DESCRIPTION +The +.Nm stdc_first_trailing_zero_ Ns Em type Ns () +family of functions returns the index of the least significant clear bit in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_first_trailing_zero "value" +type-generic macro picks the appropriate +.Nm stdc_first_trailing_zero_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the index of the least significant clear bit in +.Fa value . +The bits are numbered such that the least significant bit has number\~1, +and the most significant bit has number +.Ms w +where +.Ms w +is the number of bits in the type of +.Fa value . +If no bits are clear in +.Fa value +(i.\^e.\& +.Fa value +is the bitwise complement of zero), zero is returned. +.Sh SEE ALSO +.Xr bit_ffc 3 , +.Xr stdbit 3 , +.Xr stdc_trailing_ones 3 , +.Xr stdc_first_trailing_ones 3 , +.Xr stdc_first_leading_zero 3 +.Sh STANDARDS +The +.Nm stdc_first_trailing_zero_ Ns Em type Ns () +family of functions and the +.Fn stdc_first_trailing_zero +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_first_trailing_zero_ Ns Em type Ns () +family of functions and the +.Fn stdc_first_trailing_zero +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_first_trailing_zero.c b/lib/libc/stdbit/stdc_first_trailing_zero.c new file mode 100644 index 000000000000..27395c6977c3 --- /dev/null +++ b/lib/libc/stdbit/stdc_first_trailing_zero.c @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <limits.h> +#include <stdbit.h> + +unsigned int +stdc_first_trailing_zero_uc(unsigned char x) +{ + if (x == UCHAR_MAX) + return (0); + + return (__builtin_ctz(~x) + 1); +} + +unsigned int +stdc_first_trailing_zero_us(unsigned short x) +{ + if (x == USHRT_MAX) + return (0); + + return (__builtin_ctz(~x) + 1); +} + +unsigned int +stdc_first_trailing_zero_ui(unsigned int x) +{ + if (x == ~0U) + return (0); + + return (__builtin_ctz(~x) + 1); +} + +unsigned int +stdc_first_trailing_zero_ul(unsigned long x) +{ + if (x == ~0UL) + return (0); + + return (__builtin_ctzl(~x) + 1); +} + +unsigned int +stdc_first_trailing_zero_ull(unsigned long long x) +{ + if (x == ~0ULL) + return (0); + + return (__builtin_ctzll(~x) + 1); +} diff --git a/lib/libc/stdbit/stdc_has_single_bit.3 b/lib/libc/stdbit/stdc_has_single_bit.3 new file mode 100644 index 000000000000..8f82c552216a --- /dev/null +++ b/lib/libc/stdbit/stdc_has_single_bit.3 @@ -0,0 +1,98 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_HAS_SINGLE_BIT 3 +.Os +.Sh NAME +.Nm stdc_has_single_bit +.Nd check for single bit set +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft bool +.Fn stdc_has_single_bit_uc "unsigned char value" +.Ft bool +.Fn stdc_has_single_bit_us "unsigned short value" +.Ft bool +.Fn stdc_has_single_bit_ui "unsigned int value" +.Ft bool +.Fn stdc_has_single_bit_ul "unsigned long value" +.Ft bool +.Fn stdc_has_single_bit_ull "unsigned long long value" +.Ft bool +.Fn stdc_has_single_bit "value" +.Sh DESCRIPTION +The +.Nm stdc_has_single_bit_ Ns Em type Ns () +family of functions checks if there is exactly one bit +set in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_has_single_bit "value" +type-generic macro picks the appropriate +.Nm stdc_has_single_bit_ Ns Em type Ns () +function based on the type of +.Fa value . +.Pp +The functions in this family behave similarly to the +.Fn powerof2 "value" +macro of +.In sys/param.h , +but differ when +.Fa value +is zero: while +.Fn powerof2 +considers zero to be a power of two, +.Fn stdc_has_single_bit +does not. +.Sh RETURN VALUES +Returns +.Sy true +if exactly one bit is set in +.Fa value , +otherwise returns +.Sy false . +I.\^e. the function determines if +.Fa value +is a power of two. +.Sh SEE ALSO +.Xr stdbit 3 , +.Xr stdc_count_ones 3 +.Sh STANDARDS +The +.Nm stdc_has_single_bit_ Ns Em type Ns () +family of functions and the +.Fn stdc_has_single_bit +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_has_single_bit_ Ns Em type Ns () +family of functions and the +.Fn stdc_has_single_bit +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_has_single_bit.c b/lib/libc/stdbit/stdc_has_single_bit.c new file mode 100644 index 000000000000..e5f676265551 --- /dev/null +++ b/lib/libc/stdbit/stdc_has_single_bit.c @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <stdbit.h> +#include <stdbool.h> + +bool +stdc_has_single_bit_uc(unsigned char x) +{ + return (x != 0 && (x & x - 1) == 0); +} + +bool +stdc_has_single_bit_us(unsigned short x) +{ + return (x != 0 && (x & x - 1) == 0); +} + +bool +stdc_has_single_bit_ui(unsigned int x) +{ + return (x != 0 && (x & x - 1) == 0); +} + +bool +stdc_has_single_bit_ul(unsigned long x) +{ + return (x != 0 && (x & x - 1) == 0); +} + +bool +stdc_has_single_bit_ull(unsigned long long x) +{ + return (x != 0 && (x & x - 1) == 0); +} diff --git a/lib/libc/stdbit/stdc_leading_ones.3 b/lib/libc/stdbit/stdc_leading_ones.3 new file mode 100644 index 000000000000..6cf7989b73ea --- /dev/null +++ b/lib/libc/stdbit/stdc_leading_ones.3 @@ -0,0 +1,86 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_LEADING_ONES 3 +.Os +.Sh NAME +.Nm stdc_leading_ones +.Nd find the number of leading ones in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_leading_ones_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_leading_ones_us "unsigned short value" +.Ft unsigned int +.Fn stdc_leading_ones_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_leading_ones_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_leading_ones_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_leading_ones "value" +.Sh DESCRIPTION +The +.Nm stdc_leading_ones_ Ns Em type Ns () +family of functions returns the number of leading ones in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_leading_ones "value" +type-generic macro picks the appropriate +.Nm stdc_leading_ones_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the number of leading ones in +.Fa value . +If +.Fa value +is all ones, +the total number of bits in the type of +.Fa value +is returned. +.Sh SEE ALSO +.Xr stdbit 3 , +.Xr stdc_leading_zeros 3 , +.Xr stdc_trailing_ones 3 , +.Xr stdc_first_leading_zero 3 +.Sh STANDARDS +The +.Nm stdc_leading_ones_ Ns Em type Ns () +family of functions and the +.Fn stdc_leading_ones +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_leading_ones_ Ns Em type Ns () +family of functions and the +.Fn stdc_leading_ones +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_leading_ones.c b/lib/libc/stdbit/stdc_leading_ones.c new file mode 100644 index 000000000000..79d3ec7f2065 --- /dev/null +++ b/lib/libc/stdbit/stdc_leading_ones.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <assert.h> +#include <limits.h> +#include <stdbit.h> + +/* Avoid triggering undefined behavior if x == 0. */ +static_assert(UCHAR_WIDTH < UINT_WIDTH, + "stdc_leading_ones_uc needs UCHAR_WIDTH < UINT_WIDTH"); + +unsigned int +stdc_leading_ones_uc(unsigned char x) +{ + const int offset = UINT_WIDTH - UCHAR_WIDTH; + + return (__builtin_clz(~(x << offset))); +} + +/* Avoid triggering undefined behavior if x == 0. */ +static_assert(USHRT_WIDTH < UINT_WIDTH, + "stdc_leading_ones_us needs USHRT_WIDTH < UINT_WIDTH"); + +unsigned int +stdc_leading_ones_us(unsigned short x) +{ + const int offset = UINT_WIDTH - USHRT_WIDTH; + + return (__builtin_clz(~(x << offset))); +} + +unsigned int +stdc_leading_ones_ui(unsigned int x) +{ + if (x == ~0U) + return (UINT_WIDTH); + + return (__builtin_clz(~x)); +} + +unsigned int +stdc_leading_ones_ul(unsigned long x) +{ + if (x == ~0UL) + return (ULONG_WIDTH); + + return (__builtin_clzl(~x)); +} + +unsigned int +stdc_leading_ones_ull(unsigned long long x) +{ + if (x == ~0ULL) + return (ULLONG_WIDTH); + + return (__builtin_clzll(~x)); +} diff --git a/lib/libc/stdbit/stdc_leading_zeros.3 b/lib/libc/stdbit/stdc_leading_zeros.3 new file mode 100644 index 000000000000..7a7a203ac78c --- /dev/null +++ b/lib/libc/stdbit/stdc_leading_zeros.3 @@ -0,0 +1,86 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_LEADING_ZEROS 3 +.Os +.Sh NAME +.Nm stdc_leading_zeros +.Nd find the number of leading zeros in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_leading_zeros_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_leading_zeros_us "unsigned short value" +.Ft unsigned int +.Fn stdc_leading_zeros_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_leading_zeros_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_leading_zeros_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_leading_zeros "value" +.Sh DESCRIPTION +The +.Nm stdc_leading_zeros_ Ns Em type Ns () +family of functions returns the number of leading zeros in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_leading_zeros "value" +type-generic macro picks the appropriate +.Nm stdc_leading_zeros_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the number of leading zeros in +.Fa value . +If +.Fa value +is all zeros, +the total number of bits in the type of +.Fa value +is returned. +.Sh SEE ALSO +.Xr stdbit 3 , +.Xr stdc_leading_ones 3 , +.Xr stdc_trailing_zeros 3 , +.Xr stdc_first_leading_one 3 +.Sh STANDARDS +The +.Nm stdc_leading_zeros_ Ns Em type Ns () +family of functions and the +.Fn stdc_leading_zeros +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_leading_zeros_ Ns Em type Ns () +family of functions and the +.Fn stdc_leading_zeros +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_leading_zeros.c b/lib/libc/stdbit/stdc_leading_zeros.c new file mode 100644 index 000000000000..2fdf64ec93d4 --- /dev/null +++ b/lib/libc/stdbit/stdc_leading_zeros.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <assert.h> +#include <limits.h> +#include <stdbit.h> + +/* Offset must be greater than zero. */ +static_assert(UCHAR_WIDTH < UINT_WIDTH, + "stdc_leading_zeros_uc needs UCHAR_WIDTH < UINT_WIDTH"); + +unsigned int +stdc_leading_zeros_uc(unsigned char x) +{ + const int offset = UINT_WIDTH - UCHAR_WIDTH; + + return (__builtin_clz((x << offset) + (1U << (offset - 1)))); +} + +/* Offset must be greater than zero. */ +static_assert(USHRT_WIDTH < UINT_WIDTH, + "stdc_leading_zeros_us needs USHRT_WIDTH < UINT_WIDTH"); + +unsigned int +stdc_leading_zeros_us(unsigned short x) +{ + const int offset = UINT_WIDTH - USHRT_WIDTH; + + return (__builtin_clz((x << offset) + (1U << (offset - 1)))); +} + +unsigned int +stdc_leading_zeros_ui(unsigned int x) +{ + if (x == 0) + return (UINT_WIDTH); + + return (__builtin_clz(x)); +} + +unsigned int +stdc_leading_zeros_ul(unsigned long x) +{ + if (x == 0) + return (ULONG_WIDTH); + + return (__builtin_clzl(x)); +} + +unsigned int +stdc_leading_zeros_ull(unsigned long long x) +{ + if (x == 0) + return (ULLONG_WIDTH); + + return (__builtin_clzll(x)); +} diff --git a/lib/libc/stdbit/stdc_trailing_ones.3 b/lib/libc/stdbit/stdc_trailing_ones.3 new file mode 100644 index 000000000000..96277506fc17 --- /dev/null +++ b/lib/libc/stdbit/stdc_trailing_ones.3 @@ -0,0 +1,86 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_TRAILING_ONES 3 +.Os +.Sh NAME +.Nm stdc_trailing_ones +.Nd find the number of trailing ones in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_trailing_ones_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_trailing_ones_us "unsigned short value" +.Ft unsigned int +.Fn stdc_trailing_ones_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_trailing_ones_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_trailing_ones_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_trailing_ones "value" +.Sh DESCRIPTION +The +.Nm stdc_trailing_ones_ Ns Em type Ns () +family of functions returns the number of trailing ones in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_trailing_ones "value" +type-generic macro picks the appropriate +.Nm stdc_trailing_ones_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the number of trailing ones in +.Fa value . +If +.Fa value +is all ones, +the total number of bits in the type of +.Fa value +is returned. +.Sh SEE ALSO +.Xr stdbit 3 , +.Xr stdc_leading_ones 3 , +.Xr stdc_trailing_zeros 3 , +.Xr stdc_first_trailing_zero 3 +.Sh STANDARDS +The +.Nm stdc_trailing_ones_ Ns Em type Ns () +family of functions and the +.Fn stdc_trailing_ones +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_trailing_ones_ Ns Em type Ns () +family of functions and the +.Fn stdc_trailing_ones +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_trailing_ones.c b/lib/libc/stdbit/stdc_trailing_ones.c new file mode 100644 index 000000000000..c1a0e6b991da --- /dev/null +++ b/lib/libc/stdbit/stdc_trailing_ones.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <assert.h> +#include <limits.h> +#include <stdbit.h> + +/* Avoid triggering undefined behavior if x == ~0. */ +static_assert(UCHAR_WIDTH < UINT_WIDTH, + "stdc_trailing_ones_uc needs UCHAR_WIDTH < UINT_WIDTH"); + +unsigned int +stdc_trailing_ones_uc(unsigned char x) +{ + return (__builtin_ctz(~x)); +} + +/* Avoid triggering undefined behavior if x == ~0. */ +static_assert(USHRT_WIDTH < UINT_WIDTH, + "stdc_trailing_ones_uc needs USHRT_WIDTH < UINT_WIDTH"); + +unsigned int +stdc_trailing_ones_us(unsigned short x) +{ + return (__builtin_ctz(~x)); +} + +unsigned int +stdc_trailing_ones_ui(unsigned int x) +{ + if (x == ~0U) + return (UINT_WIDTH); + + return (__builtin_ctz(~x)); +} + +unsigned int +stdc_trailing_ones_ul(unsigned long x) +{ + if (x == ~0UL) + return (ULONG_WIDTH); + + return (__builtin_ctzl(~x)); +} + +unsigned int +stdc_trailing_ones_ull(unsigned long long x) +{ + if (x == ~0ULL) + return (ULLONG_WIDTH); + + return (__builtin_ctzll(~x)); +} diff --git a/lib/libc/stdbit/stdc_trailing_zeros.3 b/lib/libc/stdbit/stdc_trailing_zeros.3 new file mode 100644 index 000000000000..4bc675590ee7 --- /dev/null +++ b/lib/libc/stdbit/stdc_trailing_zeros.3 @@ -0,0 +1,87 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 9, 2025 +.Dt STDC_TRAILING_ZEROS 3 +.Os +.Sh NAME +.Nm stdc_trailing_zeros +.Nd find the number of trailing zeros in an integer +.Sh SYNOPSIS +.Lb libc +.In stdbit.h +.Ft unsigned int +.Fn stdc_trailing_zeros_uc "unsigned char value" +.Ft unsigned int +.Fn stdc_trailing_zeros_us "unsigned short value" +.Ft unsigned int +.Fn stdc_trailing_zeros_ui "unsigned int value" +.Ft unsigned int +.Fn stdc_trailing_zeros_ul "unsigned long value" +.Ft unsigned int +.Fn stdc_trailing_zeros_ull "unsigned long long value" +.Ft unsigned int +.Fn stdc_trailing_zeros "value" +.Sh DESCRIPTION +The +.Nm stdc_trailing_zeros_ Ns Em type Ns () +family of functions returns the number of trailing zeros in +.Fa value , +where +.Fa value +is of type +.Va unsigned char , +.Va unsigned short , +.Va unsigned int , +.Va unsigned long , +or +.Va unsigned long long +for +.Em type +being +.Sy uc , +.Sy us , +.Sy ui , +.Sy ul , +or +.Sy ull +respectively. +The +.Fn stdc_trailing_zeros "value" +type-generic macro picks the appropriate +.Nm stdc_trailing_zeros_ Ns Em type Ns () +function based on the type of +.Fa value . +.Sh RETURN VALUES +Returns the number of trailing zeros in +.Fa value . +If +.Fa value +is all zeros, +the total number of bits in the type of +.Fa value +is returned. +.Sh SEE ALSO +.Xr ffs 3 , +.Xr stdbit 3 , +.Xr stdc_leading_zeros 3 , +.Xr stdc_trailing_ones 3 , +.Xr stdc_first_trailing_one 3 +.Sh STANDARDS +The +.Nm stdc_trailing_zeros_ Ns Em type Ns () +family of functions and the +.Fn stdc_trailing_zeros +type-generic macro conform to +.St -isoC-2023 . +.Sh HISTORY +The +.Nm stdc_trailing_zeros_ Ns Em type Ns () +family of functions and the +.Fn stdc_trailing_zeros +type-generic macro were added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdbit/stdc_trailing_zeros.c b/lib/libc/stdbit/stdc_trailing_zeros.c new file mode 100644 index 000000000000..6213f62cffdb --- /dev/null +++ b/lib/libc/stdbit/stdc_trailing_zeros.c @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <assert.h> +#include <limits.h> +#include <stdbit.h> + +/* Ensure we do not shift 1U out of range. */ +static_assert(UCHAR_WIDTH < UINT_WIDTH, + "stdc_trailing_zeros_uc needs UCHAR_WIDTH < UINT_WIDTH"); + +unsigned int +stdc_trailing_zeros_uc(unsigned char x) +{ + return (__builtin_ctz(x | 1U << UCHAR_WIDTH)); +} + +/* Ensure we do not shift 1U out of range. */ +static_assert(USHRT_WIDTH < UINT_WIDTH, + "stdc_trailing_zeros_uc needs USHRT_WIDTH < UINT_WIDTH"); + +unsigned int +stdc_trailing_zeros_us(unsigned short x) +{ + return (__builtin_ctz(x | 1U << USHRT_WIDTH)); +} + +unsigned int +stdc_trailing_zeros_ui(unsigned int x) +{ + if (x == 0U) + return (UINT_WIDTH); + + return (__builtin_ctz(x)); +} + +unsigned int +stdc_trailing_zeros_ul(unsigned long x) +{ + if (x == 0UL) + return (ULONG_WIDTH); + + return (__builtin_ctzl(x)); +} + +unsigned int +stdc_trailing_zeros_ull(unsigned long long x) +{ + if (x == 0ULL) + return (ULLONG_WIDTH); + + return (__builtin_ctzll(x)); +} diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index ca199a669be1..b878a7625e9f 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -7,10 +7,10 @@ MISRCS+=C99_Exit.c a64l.c abort.c abs.c atexit.c atof.c atoi.c atol.c atoll.c \ div.c exit.c getenv.c getopt.c getopt_long.c \ getsubopt.c hcreate.c hcreate_r.c hdestroy_r.c heapsort.c heapsort_b.c \ hsearch_r.c imaxabs.c imaxdiv.c \ - insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c \ + insque.c l64a.c labs.c ldiv.c llabs.c lldiv.c lsearch.c memalignment.c \ merge.c mergesort_b.c ptsname.c qsort.c qsort_r.c qsort_r_compat.c \ qsort_s.c quick_exit.c radixsort.c rand.c \ - random.c reallocarray.c reallocf.c realpath.c remque.c \ + random.c reallocarray.c reallocf.c realpath.c recallocarray.c remque.c \ set_constraint_handler_s.c strfmon.c strtoimax.c \ strtol.c strtold.c strtoll.c strtoq.c strtoul.c strtonum.c strtoull.c \ strtoumax.c strtouq.c system.c tdelete.c tfind.c tsearch.c twalk.c @@ -35,7 +35,7 @@ MAN+= a64l.3 abort.3 abs.3 atexit.3 atof.3 \ atoi.3 atol.3 at_quick_exit.3 bsearch.3 \ div.3 exit.3 getenv.3 getopt.3 getopt_long.3 getsubopt.3 \ hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 llabs.3 lldiv.3 \ - lsearch.3 memory.3 ptsname.3 qsort.3 \ + lsearch.3 memalignment.3 memory.3 ptsname.3 qsort.3 \ quick_exit.3 \ radixsort.3 rand.3 random.3 reallocarray.3 reallocf.3 realpath.3 \ set_constraint_handler_s.3 \ @@ -76,6 +76,7 @@ MLINKS+=random.3 initstate.3 \ random.3 srandom.3 \ random.3 srandomdev.3 MLINKS+=radixsort.3 sradixsort.3 +MLINKS+=reallocarray.3 recallocarray.3 MLINKS+=set_constraint_handler_s.3 abort_handler_s.3 MLINKS+=set_constraint_handler_s.3 ignore_handler_s.3 MLINKS+=strfmon.3 strfmon_l.3 diff --git a/lib/libc/stdlib/Symbol.map b/lib/libc/stdlib/Symbol.map index 2b79ca2ece8b..8b7e97c3cbdc 100644 --- a/lib/libc/stdlib/Symbol.map +++ b/lib/libc/stdlib/Symbol.map @@ -131,6 +131,11 @@ FBSD_1.8 { getenv_r; }; +FBSD_1.9 { + memalignment; + recallocarray; +}; + FBSDprivate_1.0 { __system; _system; diff --git a/lib/libc/stdlib/exit.3 b/lib/libc/stdlib/exit.3 index bfb14c5c9f83..40010e678e5f 100644 --- a/lib/libc/stdlib/exit.3 +++ b/lib/libc/stdlib/exit.3 @@ -104,7 +104,7 @@ may be used to provide more information to the parent process. .Pp The complete .Fa status -value is avaliable as +value is available as .Va si_status member of the .Vt siginfo_t diff --git a/lib/libc/stdlib/malloc/jemalloc/jemalloc.3 b/lib/libc/stdlib/malloc/jemalloc/jemalloc.3 index a4ea3e1f54a9..1c99352a6ae2 100644 --- a/lib/libc/stdlib/malloc/jemalloc/jemalloc.3 +++ b/lib/libc/stdlib/malloc/jemalloc/jemalloc.3 @@ -1,13 +1,13 @@ '\" t .\" Title: JEMALLOC .\" Author: Jason Evans -.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/> -.\" Date: 11/10/2019 +.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/> +.\" Date: 05/06/2022 .\" Manual: User Manual -.\" Source: jemalloc 5.2.1-0-gea6b3e973b477b8061e0076bb257dbd7f3faa756 +.\" Source: jemalloc 5.3.0-0-g54eaed1d8b56b1aa528be3bdd1877e59c56fa90c .\" Language: English .\" -.TH "JEMALLOC" "3" "11/10/2019" "jemalloc 5.2.1-0-gea6b3e973b47" "User Manual" +.TH "JEMALLOC" "3" "05/06/2022" "jemalloc 5.3.0-0-g54eaed1d8b56" "User Manual" .\" ----------------------------------------------------------------- .\" * Define some portability stuff .\" ----------------------------------------------------------------- @@ -31,7 +31,7 @@ jemalloc \- general purpose memory allocation functions .SH "LIBRARY" .PP -This manual describes jemalloc 5\&.2\&.1\-0\-gea6b3e973b477b8061e0076bb257dbd7f3faa756\&. More information can be found at the +This manual describes jemalloc 5\&.3\&.0\-0\-g54eaed1d8b56b1aa528be3bdd1877e59c56fa90c\&. More information can be found at the \m[blue]\fBjemalloc website\fR\m[]\&\s-2\u[1]\d\s+2\&. .PP The following configuration options are enabled in libc\*(Aqs built\-in jemalloc: @@ -603,7 +603,7 @@ T} :T{ 8 KiB T}:T{ -[40 KiB, 48 KiB, 54 KiB, 64 KiB] +[40 KiB, 48 KiB, 56 KiB, 64 KiB] T} :T{ 16 KiB @@ -848,6 +848,11 @@ in these cases\&. This option is disabled by default unless is specified during configuration, in which case it is enabled by default\&. .RE .PP +opt\&.cache_oblivious (\fBbool\fR) r\- +.RS 4 +Enable / Disable cache\-oblivious large allocation alignment, for large requests with no alignment constraints\&. If this feature is disabled, all large allocations are page\-aligned as an implementation artifact, which can severely harm CPU cache utilization\&. However, the cache\-oblivious layout comes at the cost of one extra page per large allocation, which in the most extreme case increases physical memory usage for the 16 KiB size class to 20 KiB\&. This option is enabled by default\&. +.RE +.PP opt\&.metadata_thp (\fBconst char *\fR) r\- .RS 4 Controls whether to allow jemalloc to use transparent huge page (THP) for internal metadata (see @@ -859,6 +864,11 @@ uses no THP initially, but may begin to do so when metadata usage reaches certai \(lqdisabled\(rq\&. .RE .PP +opt\&.trust_madvise (\fBbool\fR) r\- +.RS 4 +If true, do not perform runtime check for MADV_DONTNEED, to check that it actually zeros pages\&. The default is disabled on Linux and enabled elsewhere\&. +.RE +.PP opt\&.retain (\fBbool\fR) r\- .RS 4 If true, retain unused virtual memory for later reuse rather than discarding it by calling @@ -990,6 +1000,28 @@ is enabled\&. The default is \(lq\(rq\&. .RE .PP +opt\&.stats_interval (\fBint64_t\fR) r\- +.RS 4 +Average interval between statistics outputs, as measured in bytes of allocation activity\&. The actual interval may be sporadic because decentralized event counters are used to avoid synchronization bottlenecks\&. The output may be triggered on any thread, which then calls +malloc_stats_print()\&. +opt\&.stats_interval_opts +can be combined to specify output options\&. By default, interval\-triggered stats output is disabled (encoded as \-1)\&. +.RE +.PP +opt\&.stats_interval_opts (\fBconst char *\fR) r\- +.RS 4 +Options (the +\fIopts\fR +string) to pass to the +malloc_stats_print() +for interval based statistics printing (enabled through +opt\&.stats_interval)\&. See available options in +malloc_stats_print()\&. Has no effect unless +opt\&.stats_interval +is enabled\&. The default is +\(lq\(rq\&. +.RE +.PP opt\&.junk (\fBconst char *\fR) r\- [\fB\-\-enable\-fill\fR] .RS 4 Junk filling\&. If set to @@ -1046,13 +1078,13 @@ This option is disabled by default\&. opt\&.tcache (\fBbool\fR) r\- .RS 4 Thread\-specific caching (tcache) enabled/disabled\&. When there are multiple threads, each thread uses a tcache for objects up to a certain size\&. Thread\-specific caching allows many allocations to be satisfied without performing any thread synchronization, at the cost of increased memory use\&. See the -opt\&.lg_tcache_max +opt\&.tcache_max option for related tuning information\&. This option is enabled by default\&. .RE .PP -opt\&.lg_tcache_max (\fBsize_t\fR) r\- +opt\&.tcache_max (\fBsize_t\fR) r\- .RS 4 -Maximum size class (log base 2) to cache in the thread\-specific cache (tcache)\&. At a minimum, all small size classes are cached, and at a maximum all large size classes are cached\&. The default maximum is 32 KiB (2^15)\&. +Maximum size class to cache in the thread\-specific cache (tcache)\&. At a minimum, the first size class is cached; and at a maximum, size classes up to 8 MiB can be cached\&. The default maximum is 32 KiB (2^15)\&. As a convenience, this may also be set by specifying lg_tcache_max, which will be taken to be the base\-2 logarithm of the setting of tcache_max\&. .RE .PP opt\&.thp (\fBconst char *\fR) r\- @@ -1091,7 +1123,8 @@ for heap profile format documentation\&. opt\&.prof_prefix (\fBconst char *\fR) r\- [\fB\-\-enable\-prof\fR] .RS 4 Filename prefix for profile dumps\&. If the prefix is set to the empty string, no automatic dumps will occur; this is primarily useful for disabling the automatic final heap dump (which also disables leak reporting, if enabled)\&. The default prefix is -jeprof\&. +jeprof\&. This prefix value can be overridden by +prof\&.prefix\&. .RE .PP opt\&.prof_active (\fBbool\fR) r\- [\fB\-\-enable\-prof\fR] @@ -1129,7 +1162,9 @@ Average interval (log base 2) between memory profile dumps, as measured in bytes <prefix> is controlled by the opt\&.prof_prefix -option\&. By default, interval\-triggered profile dumping is disabled (encoded as \-1)\&. +and +prof\&.prefix +options\&. By default, interval\-triggered profile dumping is disabled (encoded as \-1)\&. .RE .PP opt\&.prof_gdump (\fBbool\fR) r\- [\fB\-\-enable\-prof\fR] @@ -1147,7 +1182,9 @@ function to dump final memory usage to a file named according to the pattern <prefix> is controlled by the opt\&.prof_prefix -option\&. Note that +and +prof\&.prefix +options\&. Note that atexit() may allocate memory during application initialization and then deadlock internally when jemalloc in turn calls atexit(), so this option is not universally usable (though the application can register its own @@ -1161,7 +1198,46 @@ Leak reporting enabled/disabled\&. If enabled, use an \fBatexit\fR(3) function to report memory leaks detected by allocation sampling\&. See the opt\&.prof -option for information on analyzing heap profile output\&. This option is disabled by default\&. +option for information on analyzing heap profile output\&. Works only when combined with +opt\&.prof_final, otherwise does nothing\&. This option is disabled by default\&. +.RE +.PP +opt\&.prof_leak_error (\fBbool\fR) r\- [\fB\-\-enable\-prof\fR] +.RS 4 +Similar to +opt\&.prof_leak, but makes the process exit with error code 1 if a memory leak is detected\&. This option supersedes +opt\&.prof_leak, meaning that if both are specified, this option takes precedence\&. When enabled, also enables +opt\&.prof_leak\&. Works only when combined with +opt\&.prof_final, otherwise does nothing\&. This option is disabled by default\&. +.RE +.PP +opt\&.zero_realloc (\fBconst char *\fR) r\- +.RS 4 +Determines the behavior of +realloc() +when passed a value of zero for the new size\&. +\(lqalloc\(rq +treats this as an allocation of size zero (and returns a non\-null result except in case of resource exhaustion)\&. +\(lqfree\(rq +treats this as a deallocation of the pointer, and returns +\fBNULL\fR +without setting +\fIerrno\fR\&. +\(lqabort\(rq +aborts the process if zero is passed\&. The default is +\(lqfree\(rq +on Linux and Windows, and +\(lqalloc\(rq +elsewhere\&. +.sp +There is considerable divergence of behaviors across implementations in handling this case\&. Many have the behavior of +\(lqfree\(rq\&. This can introduce security vulnerabilities, since a +\fBNULL\fR +return value indicates failure, and the continued validity of the passed\-in pointer (per POSIX and C11)\&. +\(lqalloc\(rq +is safe, but can cause leaks in programs that expect the common behavior\&. Programs intended to be portable and leak\-free cannot assume either behavior, and must therefore never call realloc with a size of 0\&. The +\(lqabort\(rq +option enables these testing this behavior\&. .RE .PP thread\&.arena (\fBunsigned\fR) rw @@ -1182,7 +1258,7 @@ Get a pointer to the the value that is returned by the thread\&.allocated mallctl\&. This is useful for avoiding the overhead of repeated mallctl*() -calls\&. +calls\&. Note that the underlying counter should not be modified by the application\&. .RE .PP thread\&.deallocated (\fBuint64_t\fR) r\- [\fB\-\-enable\-stats\fR] @@ -1196,7 +1272,23 @@ Get a pointer to the the value that is returned by the thread\&.deallocated mallctl\&. This is useful for avoiding the overhead of repeated mallctl*() -calls\&. +calls\&. Note that the underlying counter should not be modified by the application\&. +.RE +.PP +thread\&.peak\&.read (\fBuint64_t\fR) r\- [\fB\-\-enable\-stats\fR] +.RS 4 +Get an approximation of the maximum value of the difference between the number of bytes allocated and the number of bytes deallocated by the calling thread since the last call to +thread\&.peak\&.reset, or since the thread\*(Aqs creation if it has not called +thread\&.peak\&.reset\&. No guarantees are made about the quality of the approximation, but jemalloc currently endeavors to maintain accuracy to within one hundred kilobytes\&. +.RE +.PP +thread\&.peak\&.reset (\fBvoid\fR) \-\- [\fB\-\-enable\-stats\fR] +.RS 4 +Resets the counter for net bytes allocated in the calling thread to zero\&. This affects subsequent calls to +thread\&.peak\&.read, but not the values returned by +thread\&.allocated +or +thread\&.deallocated\&. .RE .PP thread\&.tcache\&.enabled (\fBbool\fR) rw @@ -1224,11 +1316,27 @@ Control whether sampling is currently active for the calling thread\&. This is a prof\&.active; both must be active for the calling thread to sample\&. This flag is enabled by default\&. .RE .PP +thread\&.idle (\fBvoid\fR) \-\- +.RS 4 +Hints to jemalloc that the calling thread will be idle for some nontrivial period of time (say, on the order of seconds), and that doing some cleanup operations may be beneficial\&. There are no guarantees as to what specific operations will be performed; currently this flushes the caller\*(Aqs tcache and may (according to some heuristic) purge its associated arena\&. +.sp +This is not intended to be a general\-purpose background activity mechanism, and threads should not wake up multiple times solely to call it\&. Rather, a thread waiting for a task should do a timed wait first, call +thread\&.idle +if no task appears in the timeout interval, and then do an untimed wait\&. For such a background activity mechanism, see +background_thread\&. +.RE +.PP tcache\&.create (\fBunsigned\fR) r\- .RS 4 Create an explicit thread\-specific cache (tcache) and return an identifier that can be passed to the \fBMALLOCX_TCACHE(\fR\fB\fItc\fR\fR\fB)\fR macro to explicitly use the specified cache rather than the automatically managed one that is used by default\&. Each explicit cache can be used by only one thread at a time; the application must assure that this constraint holds\&. +.sp +If the amount of space supplied for storing the thread\-specific cache identifier does not equal +sizeof(\fBunsigned\fR), no thread\-specific cache will be created, no data will be written to the space pointed by +\fIoldp\fR, and +\fI*oldlenp\fR +will be set to 0\&. .RE .PP tcache\&.flush (\fBunsigned\fR) \-w @@ -1634,6 +1742,12 @@ Maximum size supported by this large size class\&. arenas\&.create (\fBunsigned\fR, \fBextent_hooks_t *\fR) rw .RS 4 Explicitly create a new arena outside the range of automatically managed arenas, with optionally specified extent hooks, and return the new arena index\&. +.sp +If the amount of space supplied for storing the arena index does not equal +sizeof(\fBunsigned\fR), no arena will be created, no data will be written to the space pointed by +\fIoldp\fR, and +\fI*oldlenp\fR +will be set to 0\&. .RE .PP arenas\&.lookup (\fBunsigned\fR, \fBvoid*\fR) rw @@ -1666,7 +1780,16 @@ Dump a memory profile to the specified file, or if NULL is specified, to a file <prefix> is controlled by the opt\&.prof_prefix -option\&. +and +prof\&.prefix +options\&. +.RE +.PP +prof\&.prefix (\fBconst char *\fR) \-w [\fB\-\-enable\-prof\fR] +.RS 4 +Set the filename prefix for profile dumps\&. See +opt\&.prof_prefix +for the default setting\&. This can be useful to differentiate profile dumps such as from forked processes\&. .RE .PP prof\&.gdump (\fBbool\fR) rw [\fB\-\-enable\-prof\fR] @@ -1676,7 +1799,9 @@ When enabled, trigger a memory profile dump every time the total virtual memory <prefix> is controlled by the opt\&.prof_prefix -option\&. +and +prof\&.prefix +options\&. .RE .PP prof\&.reset (\fBsize_t\fR) \-w [\fB\-\-enable\-prof\fR] @@ -1752,6 +1877,18 @@ for details)\&. Retained memory is excluded from mapped memory statistics, e\&.g stats\&.mapped\&. .RE .PP +stats\&.zero_reallocs (\fBsize_t\fR) r\- [\fB\-\-enable\-stats\fR] +.RS 4 +Number of times that the +realloc() +was called with a non\-\fBNULL\fR +pointer argument and a +\fB0\fR +size argument\&. This is a fundamentally unsafe pattern in portable programs; see +opt\&.zero_realloc +for details\&. +.RE +.PP stats\&.background_thread\&.num_threads (\fBsize_t\fR) r\- [\fB\-\-enable\-stats\fR] .RS 4 Number of @@ -1825,6 +1962,26 @@ is one of the counters in mutex profiling counters\&. .RE .PP +stats\&.mutexes\&.prof_thds_data\&.{counter} (\fBcounter specific type\fR) r\- [\fB\-\-enable\-stats\fR] +.RS 4 +Statistics on +\fIprof\fR +threads data mutex (global scope; profiling related)\&. +{counter} +is one of the counters in +mutex profiling counters\&. +.RE +.PP +stats\&.mutexes\&.prof_dump\&.{counter} (\fBcounter specific type\fR) r\- [\fB\-\-enable\-stats\fR] +.RS 4 +Statistics on +\fIprof\fR +dumping mutex (global scope; profiling related)\&. +{counter} +is one of the counters in +mutex profiling counters\&. +.RE +.PP stats\&.mutexes\&.reset (\fBvoid\fR) \-\- [\fB\-\-enable\-stats\fR] .RS 4 Reset all mutex profile statistics, including global mutexes, arena mutexes and bin mutexes\&. @@ -2242,7 +2399,7 @@ heap_v2/524288 [\&.\&.\&.] @ 0x5f86da8 0x5f5a1dc [\&.\&.\&.] 0x29e4d4e 0xa200316 0xabb2988 [\&.\&.\&.] t*: 13: 6688 [0: 0] - t3: 12: 6496 [0: ] + t3: 12: 6496 [0: 0] t99: 1: 192 [0: 0] [\&.\&.\&.] @@ -2264,9 +2421,9 @@ to indicate descriptions of the corresponding fields\&. <heap_profile_format_version>/<mean_sample_interval> <aggregate>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>] [\&.\&.\&.] - <thread_3_aggregate>: <curobjs>: <curbytes>[<cumobjs>: <cumbytes>] + <thread_3_aggregate>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>] [\&.\&.\&.] - <thread_99_aggregate>: <curobjs>: <curbytes>[<cumobjs>: <cumbytes>] + <thread_99_aggregate>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>] [\&.\&.\&.] @ <top_frame> <frame> [\&.\&.\&.] <frame> <frame> <frame> [\&.\&.\&.] <backtrace_aggregate>: <curobjs>: <curbytes> [<cumobjs>: <cumbytes>] @@ -2432,7 +2589,8 @@ is not \fInewlen\fR is too large or too small\&. Alternatively, \fI*oldlenp\fR -is too large or too small; in this case as much data as possible are read despite the error\&. +is too large or too small; when it happens, except for a very few cases explicitly documented otherwise, as much data as possible are read despite the error, with the amount of data read being recorded in +\fI*oldlenp\fR\&. .RE .PP ENOENT diff --git a/lib/libc/stdlib/memalignment.3 b/lib/libc/stdlib/memalignment.3 new file mode 100644 index 000000000000..4a2269a82c81 --- /dev/null +++ b/lib/libc/stdlib/memalignment.3 @@ -0,0 +1,53 @@ +.\" +.\" Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> +.\" +.\" SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd November 10, 2025 +.Dt MEMALIGNMENT 3 +.Os +.Sh NAME +.Nm memalignment +.Nd find the memory alignment of an object +.Sh SYNOPSIS +.Lb libc +.In stdlib.h +.Ft size_t +.Fn memalignment "const void *ptr" +.Sh DESCRIPTION +The +.Fn memalignment +function determines the alignment of the object pointed to by +.Fa ptr . +This alignment is a power of\~2, and may be larger than the range +supported by the +.Sy alignof +operator. +The value returned can be compared to the result of +.Sy alignof , +and if it is greater or equal, the alignment requirement of the operand +is satisfied. +.Sh RETURN VALUES +Returns the alignment of +.Fa ptr +as a power of\~2. +If +.Fa ptr +is a null pointer, an alignment of zero is returned. +An alignment of zero indicates that the tested pointer cannot be used to +access an object of any type. +.Sh SEE ALSO +.Xr aligned_alloc 3 , +.Xr posix_memalign 3 +.Sh STANDARDS +The +.Fn memalignment +function conforms to +.St -isoC-2023 . +.Sh HISTORY +The +.Fn memalignment +function was added in +.Fx 15.1. +.Sh AUTHOR +.An Robert Clausecker Aq Mt fuz@FreeBSD.org diff --git a/lib/libc/stdlib/memalignment.c b/lib/libc/stdlib/memalignment.c new file mode 100644 index 000000000000..771ddc2f5253 --- /dev/null +++ b/lib/libc/stdlib/memalignment.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <stdint.h> +#include <stdlib.h> + +size_t +memalignment(const void *p) +{ + uintptr_t align; + + if (p == NULL) + return (0); + + align = (uintptr_t)p; + align &= -align; + +#if UINTPTR_MAX > SIZE_MAX + /* if alignment overflows size_t, return maximum possible */ + if (align > SIZE_MAX) + align = SIZE_MAX - SIZE_MAX/2; +#endif + + return (align); +} diff --git a/lib/libc/stdlib/reallocarray.3 b/lib/libc/stdlib/reallocarray.3 index 80035c67a497..9a2ab5c7a840 100644 --- a/lib/libc/stdlib/reallocarray.3 +++ b/lib/libc/stdlib/reallocarray.3 @@ -26,7 +26,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd May 1, 2015 +.Dd October 2, 2025 .Dt REALLOCARRAY 3 .Os .Sh NAME @@ -38,6 +38,8 @@ .In stdlib.h .Ft void * .Fn reallocarray "void *ptr" "size_t nmemb" "size_t size" +.Ft void * +.Fn recallocarray "void *ptr" "size_t oldnmeb" "size_t nmemb" size_t size" .Sh DESCRIPTION The .Fn reallocarray @@ -52,6 +54,33 @@ and checks for integer overflow in the calculation .Fa nmemb * .Fa size . +.Pp +The +.Fn recallocarray +function is similar to the +.Fn reallocarray +function +except it ensures newly allocated memory is cleared similar to +.Fn calloc . +If +.Fa ptr +is +.Dv NULL , +.Fa oldnmemb +is ignored and the call is equivalent to +.Fn calloc . +If +.Fa ptr +is not +.Dv NULL , +.Fa oldnmemb +must be a value such that +.Fa oldnmemb +* +.Fa size +is the size of the earlier allocation that returned +.Fa ptr , +otherwise the behaviour is undefined. .Sh RETURN VALUES The .Fn reallocarray @@ -142,3 +171,9 @@ function first appeared in .Ox 5.6 and .Fx 11.0 . +The +.Fn recallocarray +function first appeared in +.Ox 6.1 +and +.Fx 16.0 . diff --git a/lib/libc/stdlib/reallocarray.c b/lib/libc/stdlib/reallocarray.c index 0868804486cc..3632734c84de 100644 --- a/lib/libc/stdlib/reallocarray.c +++ b/lib/libc/stdlib/reallocarray.c @@ -17,23 +17,19 @@ #include <sys/types.h> #include <errno.h> +#include <stdckdint.h> #include <stdint.h> #include <stdlib.h> -/* - * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX - * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW - */ -#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) - void * reallocarray(void *optr, size_t nmemb, size_t size) { + size_t nbytes; - if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && - nmemb > 0 && SIZE_MAX / nmemb < size) { + if (ckd_mul(&nbytes, nmemb, size)) { errno = ENOMEM; return (NULL); } - return (realloc(optr, size * nmemb)); + + return (realloc(optr, nbytes)); } diff --git a/lib/libc/stdlib/realpath.3 b/lib/libc/stdlib/realpath.3 index 065ba312c2ef..76f40249963b 100644 --- a/lib/libc/stdlib/realpath.3 +++ b/lib/libc/stdlib/realpath.3 @@ -28,7 +28,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd May 11, 2012 +.Dd October 10, 2025 .Dt REALPATH 3 .Os .Sh NAME @@ -108,11 +108,11 @@ and .Xr getcwd 3 . .Sh SEE ALSO .Xr getcwd 3 -.\" .Sh STANDARDS -.\" The -.\" .Fn realpath -.\" function conforms to -.\" .St -p1003.1-2001 . +.Sh STANDARDS +The +.Fn realpath +function conforms to +.St -p1003.1-2001 . .Sh HISTORY The .Fn realpath diff --git a/lib/libc/stdlib/realpath.c b/lib/libc/stdlib/realpath.c index 4c52b73319ab..18f29e95ee6b 100644 --- a/lib/libc/stdlib/realpath.c +++ b/lib/libc/stdlib/realpath.c @@ -49,7 +49,7 @@ realpath1(const char *path, char *resolved) { struct stat sb; char *p, *q; - size_t left_len, resolved_len, next_token_len; + size_t left_len, prev_len, resolved_len, next_token_len; unsigned symlinks; ssize_t slen; char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; @@ -98,6 +98,7 @@ realpath1(const char *path, char *resolved) left_len = 0; } + prev_len = resolved_len; if (resolved[resolved_len - 1] != '/') { if (resolved_len + 1 >= PATH_MAX) { errno = ENAMETOOLONG; @@ -133,8 +134,17 @@ realpath1(const char *path, char *resolved) errno = ENAMETOOLONG; return (NULL); } - if (lstat(resolved, &sb) != 0) + if (lstat(resolved, &sb) != 0) { + /* + * EACCES means the parent directory is not + * readable, while ENOTDIR means the parent + * directory is not a directory. Rewind the path + * to correctly indicate where the error lies. + */ + if (errno == EACCES || errno == ENOTDIR) + resolved[prev_len] = '\0'; return (NULL); + } if (S_ISLNK(sb.st_mode)) { if (symlinks++ > MAXSYMLINKS) { errno = ELOOP; diff --git a/lib/libc/stdlib/recallocarray.c b/lib/libc/stdlib/recallocarray.c new file mode 100644 index 000000000000..cbf1fb2470cf --- /dev/null +++ b/lib/libc/stdlib/recallocarray.c @@ -0,0 +1,73 @@ +/* $OpenBSD: recallocarray.c,v 1.1 2017/03/06 18:44:21 otto Exp $ */ +/* + * Copyright (c) 2008, 2017 Otto Moerbeek <otto@drijf.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <errno.h> +#include <stdckdint.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <unistd.h> + +void *recallocarray(void *, size_t, size_t, size_t); + +void * +recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size) +{ + size_t oldsize, newsize; + void *newptr; + + if (ptr == NULL) + return calloc(newnmemb, size); + + if (ckd_mul(&newsize, newnmemb, size)) { + errno = ENOMEM; + return NULL; + } + + if (ckd_mul(&oldsize, oldnmemb, size)) { + errno = EINVAL; + return NULL; + } + + /* + * Don't bother too much if we're shrinking just a bit, + * we do not shrink for series of small steps, oh well. + */ + if (newsize <= oldsize) { + size_t d = oldsize - newsize; + + if (d < oldsize / 2 && d < (size_t)getpagesize()) { + memset((char *)ptr + newsize, 0, d); + return ptr; + } + } + + newptr = malloc(newsize); + if (newptr == NULL) + return NULL; + + if (newsize > oldsize) { + memcpy(newptr, ptr, oldsize); + memset((char *)newptr + oldsize, 0, newsize - oldsize); + } else + memcpy(newptr, ptr, newsize); + + explicit_bzero(ptr, oldsize); + free(ptr); + + return newptr; +} diff --git a/lib/libc/stdlib/strfmon.3 b/lib/libc/stdlib/strfmon.3 index 20cc560d401d..d537d9d05fa7 100644 --- a/lib/libc/stdlib/strfmon.3 +++ b/lib/libc/stdlib/strfmon.3 @@ -22,7 +22,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd December 6, 2023 +.Dd November 24, 2025 .Dt STRFMON 3 .Os .Sh NAME @@ -182,6 +182,16 @@ function will fail if: Conversion stopped due to lack of space in the buffer. .It Bq Er EINVAL The format string is invalid. +.It Bq Er EINVAL +The +.Cm + +flag was included in a conversion specification and the locale's +.Va positive_sign +and +.Va negative_sign +values would both be returned by +.Xr localeconv 3 +as empty strings. .It Bq Er ENOMEM Not enough memory for temporary buffers. .El diff --git a/lib/libc/stdlib/strfmon.c b/lib/libc/stdlib/strfmon.c index 68a36a6d5567..611ac45b9c82 100644 --- a/lib/libc/stdlib/strfmon.c +++ b/lib/libc/stdlib/strfmon.c @@ -106,7 +106,7 @@ vstrfmon_l(char *__restrict s, size_t maxsize, locale_t loc, const char *__restrict format, va_list ap) { char *dst; /* output destination pointer */ - const char *fmt; /* current format poistion pointer */ + const char *fmt; /* current format position pointer */ struct lconv *lc; /* pointer to lconv structure */ char *asciivalue; /* formatted double pointer */ @@ -171,7 +171,9 @@ literal: flags &= ~(NEED_GROUPING); continue; case '+': /* use locale defined signs */ - if (flags & SIGN_POSN_USED) + if ((flags & SIGN_POSN_USED) || + ((lc->positive_sign[0] == '\0') && + (lc->negative_sign[0] == '\0'))) goto format_error; flags |= (SIGN_POSN_USED | LOCALE_POSN); continue; @@ -455,7 +457,7 @@ __setup_vars(int flags, char *cs_precedes, char *sep_by_space, char *sign_posn, if (*sep_by_space == CHAR_MAX) *sep_by_space = 0; if (*sign_posn == CHAR_MAX) - *sign_posn = 0; + *sign_posn = 1; } static int diff --git a/lib/libc/stdtime/Makefile.inc b/lib/libc/stdtime/Makefile.inc index 647cbe6f40ba..1baa39a6c0a6 100644 --- a/lib/libc/stdtime/Makefile.inc +++ b/lib/libc/stdtime/Makefile.inc @@ -32,4 +32,5 @@ MLINKS+=strftime.3 strftime_l.3 MLINKS+=strptime.3 strptime_l.3 MLINKS+=time2posix.3 posix2time.3 MLINKS+=tzset.3 daylight.3 \ - tzset.3 timezone.3 + tzset.3 timezone.3 \ + tzset.3 tzname.3 diff --git a/lib/libc/stdtime/ctime.3 b/lib/libc/stdtime/ctime.3 index 96b7f775535a..6384e8bd959b 100644 --- a/lib/libc/stdtime/ctime.3 +++ b/lib/libc/stdtime/ctime.3 @@ -27,7 +27,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd March 26, 2024 +.Dd September 23, 2025 .Dt CTIME 3 .Os .Sh NAME @@ -41,6 +41,8 @@ .Nm localtime , .Nm localtime_r , .Nm mktime , +.Nm offtime , +.Nm offtime_r , .Nm timegm .Nd transform binary date and time values .Sh LIBRARY @@ -68,14 +70,19 @@ .Fn localtime_r "const time_t *clock" "struct tm *result" .Ft time_t .Fn mktime "struct tm *tm" +.Ft struct tm * +.Fn offtime "const time_t *clock" "long offset" +.Ft struct tm * +.Fn offtime_r "const time_t *clock" "long offset" "struct tm *result" .Ft time_t .Fn timegm "struct tm *tm" .Sh DESCRIPTION The .Fn ctime , .Fn gmtime , +.Fn localtime , and -.Fn localtime +.Fn offtime functions all take as argument a pointer to a time value representing the time in seconds since the Epoch (00:00:00 UTC on January 1, 1970; see @@ -123,6 +130,18 @@ adjustment, and returns a pointer to a .Vt struct tm . .Pp The +.Fn offtime +function similarly converts the time value with a time zone adjustment +corresponding to the provided +.Fa offset , +which is expressed in seconds, with positive values indicating a time +zone ahead of UTC (east of the Prime Meridian). +It does not call +.Xr tzset 3 +or modify +.Va tzname . +.Pp +The .Fn ctime function adjusts the time value for the current time zone in the same manner as @@ -155,13 +174,15 @@ except the caller must provide the output buffer .Fa buf , which must be at least 26 characters long, to store the result in. The -.Fn localtime_r +.Fn localtime_r , +.Fn gmtime_r , and -.Fn gmtime_r +.Fn offtime_r functions provide the same functionality as -.Fn localtime +.Fn localtime , +.Fn gmtime , and -.Fn gmtime +.Fn offtime respectively, except the caller must provide the output buffer .Fa result . .Pp @@ -368,6 +389,12 @@ and .Fn localtime_r functions have been available since .Fx 8.0 . +The +.Fn offtime +and +.Fn offtime_r +functions were added in +.Fx 15.0 . .Sh BUGS Except for .Fn difftime , diff --git a/lib/libc/stdtime/strptime.3 b/lib/libc/stdtime/strptime.3 index 7df73d2d080a..9456fa757b85 100644 --- a/lib/libc/stdtime/strptime.3 +++ b/lib/libc/stdtime/strptime.3 @@ -171,7 +171,7 @@ is taken as noon. The .Fa %Z format specifier only accepts time zone abbreviations of the local time zone, -or the value "GMT". +and the values "GMT", "UTC", or "Z". This limitation is because of ambiguity due to of the over loading of time zone abbreviations. One such example is diff --git a/lib/libc/stdtime/strptime.c b/lib/libc/stdtime/strptime.c index 5f1293c7a267..375e49146639 100644 --- a/lib/libc/stdtime/strptime.c +++ b/lib/libc/stdtime/strptime.c @@ -546,7 +546,8 @@ label: zonestr[cp - buf] = '\0'; tzset(); if (0 == strcmp(zonestr, "GMT") || - 0 == strcmp(zonestr, "UTC")) { + 0 == strcmp(zonestr, "UTC") || + 0 == strcmp(zonestr, "Z")) { *GMTp = 1; } else if (0 == strcmp(zonestr, tzname[0])) { tm->tm_isdst = 0; diff --git a/lib/libc/stdtime/tzset.3 b/lib/libc/stdtime/tzset.3 index 94ccbec9aba7..33e6a556306d 100644 --- a/lib/libc/stdtime/tzset.3 +++ b/lib/libc/stdtime/tzset.3 @@ -28,7 +28,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd December 25, 2023 +.Dd September 30, 2025 .Dt TZSET 3 .Os .Sh NAME @@ -58,8 +58,8 @@ specifies how this is done. .Pp If .Ev TZ -does not appear in the environment, the best available approximation to -local wall clock time, as specified by the +does not appear in the environment, the best available approximation +to local wall clock time, as specified by the .Xr tzfile 5 Ns -format file .Pa /etc/localtime @@ -68,9 +68,7 @@ is used. If .Ev TZ appears in the environment but its value is a null string, Coordinated -Universal Time -.Pq Tn UTC -is used (without leap second correction). +Universal Time (UTC) is used (without leap second correction). .Pp If .Ev TZ @@ -81,13 +79,12 @@ the rest of its value is used as a pathname of a file from which to read the time conversion information. If the first character of the pathname is a slash .Pq Ql / -it is used as -an absolute pathname; otherwise, it is used as a pathname relative to -the system time conversion information directory. +it is used as an absolute pathname; otherwise, it is used as a +pathname relative to the system time conversion information directory. .Pp -If its value does not begin with a colon, it is first used as the pathname -of a file (as described above) from which to read the time conversion -information. +If its value does not begin with a colon, it is first used as the +pathname of a file (as described above) from which to read the time +conversion information. If that file cannot be read, the value is then interpreted as a direct specification (the format is described below) of the time conversion information. @@ -96,24 +93,23 @@ If the .Ev TZ environment variable does not specify a .Xr tzfile 5 Ns -format -file and cannot be interpreted as a direct specification, -.Tn UTC -is used. +file and cannot be interpreted as a direct specification, UTC is used. .Pp After the first call to .Nm tzset , the .Vt timezone -variable is set to the difference, in seconds, between Coordinated Universal -Time (UTC) and the local time. +variable is set to the difference, in seconds, between Coordinated +Universal Time (UTC) and the local time. The .Vt daylight variable is set to 0 if the local timezone is observing standard time. -It is set to 1 if the local timezone ever observes an alternate time, such as summer time. +It is set to 1 if the local timezone ever observes an alternate time, +such as summer time. The first element of the .Vt tzname -array is the timezone name of standard time, while the second element is the -name of the altnerative time zone. +array is the timezone name of standard time, while the second element +is the name of the altnerative time zone. .Sh SPECIFICATION FORMAT When .Ev TZ @@ -151,8 +147,7 @@ minus .Pq Ql \- , plus .Pq Ql + , -and -.Tn ASCII +and ASCII .Dv NUL are allowed. .It Em offset @@ -294,10 +289,8 @@ by the file .Em posixrules in the system time conversion information directory are used, with the -standard and summer time offsets from -.Tn UTC -replaced by those specified by -the +standard and summer time offsets from UTC replaced by those specified +by the .Em offset values in .Ev TZ . @@ -315,20 +308,14 @@ local time zone file .It Pa /usr/share/zoneinfo time zone directory .It Pa /usr/share/zoneinfo/posixrules -rules for -.Tn POSIX Ns -style -.Tn TZ Ns 's +rules for POSIX-style TZs .It Pa /usr/share/zoneinfo/Etc/GMT -for -.Tn UTC -leap seconds +for UTC leap seconds .El .Pp If the file .Pa /usr/share/zoneinfo/UTC -does not exist, -.Tn UTC -leap seconds are loaded from +does not exist, UTC leap seconds are loaded from .Pa /usr/share/zoneinfo/posixrules . .Sh SEE ALSO .Xr date 1 , diff --git a/lib/libc/string/ffs.3 b/lib/libc/string/ffs.3 index 2a5adb01c737..14a64126e692 100644 --- a/lib/libc/string/ffs.3 +++ b/lib/libc/string/ffs.3 @@ -27,7 +27,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd October 17, 2015 +.Dd October 25, 2025 .Dt FFS 3 .Os .Sh NAME @@ -79,7 +79,20 @@ A return value of zero from any of these functions means that the argument was zero. .Sh SEE ALSO .Xr bitstring 3 , +.Xr stdc_first_trailing_one 3 , +.Xr stdc_trailing_zeros 3 , .Xr bitset 9 +.Sh STANDARDS +The +.Fn ffs +function conforms to +.St -p1003.1-2008 . +The +.Fn ffsl +and +.Fn ffsll +functions conform to +.St -p1003.1-2024 . .Sh HISTORY The .Fn ffs diff --git a/lib/libc/tests/Makefile b/lib/libc/tests/Makefile index 975c895770ee..ae8e0e937676 100644 --- a/lib/libc/tests/Makefile +++ b/lib/libc/tests/Makefile @@ -14,6 +14,7 @@ TESTS_SUBDIRS+= resolv TESTS_SUBDIRS+= rpc TESTS_SUBDIRS+= secure TESTS_SUBDIRS+= setjmp +TESTS_SUBDIRS+= stdbit TESTS_SUBDIRS+= stdio TESTS_SUBDIRS+= stdlib TESTS_SUBDIRS+= stdtime diff --git a/lib/libc/tests/gen/Makefile b/lib/libc/tests/gen/Makefile index 8c2151105209..054b1b426b3d 100644 --- a/lib/libc/tests/gen/Makefile +++ b/lib/libc/tests/gen/Makefile @@ -19,6 +19,7 @@ ATF_TESTS_C+= glob2_test .if ${COMPILER_FEATURES:Mblocks} ATF_TESTS_C+= glob_blocks_test .endif +ATF_TESTS_C+= limits_test ATF_TESTS_C+= makecontext_test ATF_TESTS_C+= opendir_test ATF_TESTS_C+= popen_test diff --git a/lib/libc/tests/gen/limits_test.c b/lib/libc/tests/gen/limits_test.c new file mode 100644 index 000000000000..b4e8bf3178f1 --- /dev/null +++ b/lib/libc/tests/gen/limits_test.c @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: FreeBSD-2-Clause + */ + +#include <assert.h> +#include <limits.h> +#include <signal.h> +#include <stdint.h> +#include <wchar.h> + +#include <atf-c.h> + +/* if this file builds, the unit tests have passed */ +#define CHECK_STYPE(type, TYPE) \ + static_assert(sizeof(type) * CHAR_BIT == TYPE ## _WIDTH, \ + __XSTRING(TYPE) "_WIDTH wrongly defined"); \ + static_assert((1ULL << (TYPE ## _WIDTH - 1)) - 1 == TYPE ## _MAX, \ + __XSTRING(TYPE) "_MAX wrongly defined"); \ + static_assert(TYPE ## _MIN == -TYPE ## _MAX - 1, \ + __XSTRING(TYPE) "_MIN wrongly defined") +#define CHECK_UTYPE(type, TYPE) \ + static_assert(sizeof(type) * CHAR_BIT == TYPE ## _WIDTH, \ + __XSTRING(TYPE) "_WIDTH wrongly defined"); \ + static_assert((type)~0ULL == TYPE ## _MAX, \ + __XSTRING(TYPE) "_MAX wrongly defined"); + +/* primitive types */ +#ifdef __CHAR_UNSIGNED__ +CHECK_UTYPE(char, CHAR); +#else +CHECK_STYPE(char, CHAR); +#endif + +CHECK_STYPE(signed char, SCHAR); +CHECK_STYPE(short, SHRT); +CHECK_STYPE(int, INT); +CHECK_STYPE(long, LONG); +CHECK_STYPE(long long, LLONG); + +CHECK_UTYPE(unsigned char, UCHAR); +CHECK_UTYPE(unsigned short, USHRT); +CHECK_UTYPE(unsigned int, UINT); +CHECK_UTYPE(unsigned long, ULONG); +CHECK_UTYPE(unsigned long long, ULLONG); + +/* fixed-width types */ +CHECK_STYPE(int8_t, INT8); +CHECK_STYPE(int16_t, INT16); +CHECK_STYPE(int32_t, INT32); +CHECK_STYPE(int64_t, INT64); + +CHECK_UTYPE(uint8_t, UINT8); +CHECK_UTYPE(uint16_t, UINT16); +CHECK_UTYPE(uint32_t, UINT32); +CHECK_UTYPE(uint64_t, UINT64); + +CHECK_STYPE(int_least8_t, INT_LEAST8); +CHECK_STYPE(int_least16_t, INT_LEAST16); +CHECK_STYPE(int_least32_t, INT_LEAST32); +CHECK_STYPE(int_least64_t, INT_LEAST64); + +CHECK_UTYPE(uint_least8_t, UINT_LEAST8); +CHECK_UTYPE(uint_least16_t, UINT_LEAST16); +CHECK_UTYPE(uint_least32_t, UINT_LEAST32); +CHECK_UTYPE(uint_least64_t, UINT_LEAST64); + +CHECK_STYPE(int_fast8_t, INT_FAST8); +CHECK_STYPE(int_fast16_t, INT_FAST16); +CHECK_STYPE(int_fast32_t, INT_FAST32); +CHECK_STYPE(int_fast64_t, INT_FAST64); + +CHECK_UTYPE(uint_fast8_t, UINT_FAST8); +CHECK_UTYPE(uint_fast16_t, UINT_FAST16); +CHECK_UTYPE(uint_fast32_t, UINT_FAST32); +CHECK_UTYPE(uint_fast64_t, UINT_FAST64); + +/* other types */ +#if WCHAR_MIN == 0 +CHECK_UTYPE(wchar_t, WCHAR); +#else +CHECK_STYPE(wchar_t, WCHAR); +#endif +CHECK_STYPE(intmax_t, INTMAX); +CHECK_STYPE(intptr_t, INTPTR); +CHECK_STYPE(ptrdiff_t, PTRDIFF); +CHECK_STYPE(wint_t, WINT); +CHECK_STYPE(sig_atomic_t, SIG_ATOMIC); + +CHECK_UTYPE(uintmax_t, UINTMAX); +CHECK_UTYPE(uintptr_t, UINTPTR); +CHECK_UTYPE(size_t, SIZE); + +/* dummy */ +ATF_TP_ADD_TCS(tp) +{ + (void)tp; + + return (atf_no_error()); +} diff --git a/lib/libc/tests/gen/realpath2_test.c b/lib/libc/tests/gen/realpath2_test.c index f89dd99cbb72..431df8721ae0 100644 --- a/lib/libc/tests/gen/realpath2_test.c +++ b/lib/libc/tests/gen/realpath2_test.c @@ -1,6 +1,7 @@ /* * Copyright (c) 2017 Jan Kokemüller * All rights reserved. + * Copyright (c) 2025 Klara, Inc. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -25,6 +26,8 @@ */ #include <sys/param.h> +#include <sys/stat.h> + #include <errno.h> #include <fcntl.h> #include <stdio.h> @@ -34,6 +37,31 @@ #include <atf-c.h> +ATF_TC(realpath_null); +ATF_TC_HEAD(realpath_null, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test null input"); +} +ATF_TC_BODY(realpath_null, tc) +{ + ATF_REQUIRE_ERRNO(EINVAL, realpath(NULL, NULL) == NULL); +} + +ATF_TC(realpath_empty); +ATF_TC_HEAD(realpath_empty, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test empty input"); +} +ATF_TC_BODY(realpath_empty, tc) +{ + char resb[PATH_MAX] = ""; + + ATF_REQUIRE_EQ(0, mkdir("foo", 0755)); + ATF_REQUIRE_EQ(0, chdir("foo")); + ATF_REQUIRE_ERRNO(ENOENT, realpath("", resb) == NULL); + ATF_REQUIRE_STREQ("", resb); +} + ATF_TC(realpath_buffer_overflow); ATF_TC_HEAD(realpath_buffer_overflow, tc) { @@ -44,16 +72,11 @@ ATF_TC_HEAD(realpath_buffer_overflow, tc) ATF_TC_BODY(realpath_buffer_overflow, tc) { - char path[MAXPATHLEN] = { 0 }; - char resb[MAXPATHLEN] = { 0 }; - size_t i; + char path[PATH_MAX] = ""; + char resb[PATH_MAX] = ""; - path[0] = 'a'; + memset(path, 'a', sizeof(path) - 1); path[1] = '/'; - for (i = 2; i < sizeof(path) - 1; ++i) { - path[i] = 'a'; - } - ATF_REQUIRE(realpath(path, resb) == NULL); } @@ -66,9 +89,9 @@ ATF_TC_HEAD(realpath_empty_symlink, tc) ATF_TC_BODY(realpath_empty_symlink, tc) { - char path[MAXPATHLEN] = { 0 }; - char slnk[MAXPATHLEN] = { 0 }; - char resb[MAXPATHLEN] = { 0 }; + char path[PATH_MAX] = ""; + char slnk[PATH_MAX] = ""; + char resb[PATH_MAX] = ""; int fd; (void)strlcat(slnk, "empty_symlink", sizeof(slnk)); @@ -89,11 +112,70 @@ ATF_TC_BODY(realpath_empty_symlink, tc) ATF_REQUIRE(unlink(slnk) == 0); } -ATF_TP_ADD_TCS(tp) +ATF_TC(realpath_partial); +ATF_TC_HEAD(realpath_partial, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test that failure leaves a partial result"); + atf_tc_set_md_var(tc, "require.user", "unprivileged"); +} + +ATF_TC_BODY(realpath_partial, tc) { + char resb[PATH_MAX] = ""; + size_t len; + + /* scenario 1: missing directory */ + ATF_REQUIRE_EQ(0, mkdir("foo", 0755)); + ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 8 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo/bar", resb + len - 8); + + /* scenario 2: dead link 1 */ + ATF_REQUIRE_EQ(0, symlink("nix", "foo/bar")); + ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 8 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo/nix", resb + len - 8); + + /* scenario 3: missing file */ + ATF_REQUIRE_EQ(0, unlink("foo/bar")); + ATF_REQUIRE_EQ(0, mkdir("foo/bar", 0755)); + ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 12 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo/bar/baz", resb + len - 12); + + /* scenario 4: dead link 2 */ + ATF_REQUIRE_EQ(0, symlink("nix", "foo/bar/baz")); + ATF_REQUIRE_ERRNO(ENOENT, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 12 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo/bar/nix", resb + len - 12); + + /* scenario 5: unreadable directory */ + ATF_REQUIRE_EQ(0, chmod("foo", 000)); + ATF_REQUIRE_ERRNO(EACCES, realpath("foo/bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 4 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/foo", resb + len - 4); + + /* scenario 6: not a directory */ + ATF_REQUIRE_EQ(0, close(creat("bar", 0644))); + ATF_REQUIRE_ERRNO(ENOTDIR, realpath("bar/baz", resb) == NULL); + len = strnlen(resb, sizeof(resb)); + ATF_REQUIRE(len > 4 && len < sizeof(resb)); + ATF_REQUIRE_STREQ("/bar", resb + len - 4); +} +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, realpath_null); + ATF_TP_ADD_TC(tp, realpath_empty); ATF_TP_ADD_TC(tp, realpath_buffer_overflow); ATF_TP_ADD_TC(tp, realpath_empty_symlink); + ATF_TP_ADD_TC(tp, realpath_partial); return atf_no_error(); } diff --git a/lib/libc/tests/net/Makefile b/lib/libc/tests/net/Makefile index 24cff61e8d24..ec0668633508 100644 --- a/lib/libc/tests/net/Makefile +++ b/lib/libc/tests/net/Makefile @@ -4,8 +4,10 @@ ATF_TESTS_C+= ether_test ATF_TESTS_C+= eui64_aton_test ATF_TESTS_C+= eui64_ntoa_test ATF_TESTS_CXX+= link_addr_test +ATF_TESTS_CXX+= inet_net_test CXXSTD.link_addr_test= c++20 +CXXSTD.inet_net_test= c++20 CFLAGS+= -I${.CURDIR} diff --git a/lib/libc/tests/net/inet_net_test.cc b/lib/libc/tests/net/inet_net_test.cc new file mode 100644 index 000000000000..60b60b152eca --- /dev/null +++ b/lib/libc/tests/net/inet_net_test.cc @@ -0,0 +1,333 @@ +/* + * SPDX-License-Identifier: ISC + * + * Copyright (c) 2025 Lexi Winter <ivy@FreeBSD.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Tests for inet_net_pton() and inet_net_ntop(). + */ + +#include <sys/types.h> +#include <sys/socket.h> + +#include <netinet/in.h> +#include <arpa/inet.h> + +#include <ranges> +#include <string> +#include <vector> + +#include <atf-c++.hpp> + +using namespace std::literals; + +/* + * inet_net_ntop() and inet_net_pton() for IPv4. + */ +ATF_TEST_CASE_WITHOUT_HEAD(inet_net_inet4) +ATF_TEST_CASE_BODY(inet_net_inet4) +{ + /* + * Define a list of addresses we want to check. Each address is passed + * to inet_net_pton() to convert it to an in_addr, then we convert the + * in_addr back to a string and compare it with the expected value. We + * want to test over-long prefixes here (such as 10.0.0.1/8), so we also + * specify what the result is expected to be. + */ + + struct test_addr { + std::string input; + int bits; + std::string output; + }; + + auto test_addrs = std::vector<test_addr>{ + // Simple prefixes that fall on octet boundaries. + { "10.0.0.0/8", 8, "10/8" }, + { "10.1.0.0/16", 16, "10.1/16" }, + { "10.1.2.0/24", 24, "10.1.2/24" }, + { "10.1.2.3/32", 32, "10.1.2.3/32" }, + + // Simple prefixes with the short-form address. + { "10/8", 8, "10/8" }, + { "10.1/16", 16, "10.1/16" }, + { "10.1.2/24", 24, "10.1.2/24" }, + + // A prefix that doesn't fall on an octet boundary. + { "10.1.64/18", 18, "10.1.64/18" }, + + // An overlong prefix with bits that aren't part of the prefix. + { "10.0.0.1/8", 8, "10/8" }, + }; + + for (auto const &addr: test_addrs) { + /* + * Convert the input string to an in_addr + bits, and make + * sure the result produces the number of bits we expected. + */ + + auto in = in_addr{}; + auto bits = inet_net_pton(AF_INET, addr.input.c_str(), + &in, sizeof(in)); + ATF_REQUIRE(bits != -1); + ATF_REQUIRE_EQ(bits, addr.bits); + + /* + * Convert the in_addr back to a string + */ + + /* + * XXX: Should there be a constant for the size of the result + * buffer? For now, use ADDRSTRLEN + 3 ("/32") + 1 (NUL). + * + * Fill the buffer with 'Z', so we can check the result was + * properly terminated. + */ + auto strbuf = std::vector<char>(INET_ADDRSTRLEN + 3 + 1, 'Z'); + auto ret = inet_net_ntop(AF_INET, &in, bits, + strbuf.data(), strbuf.size()); + ATF_REQUIRE(ret != NULL); + ATF_REQUIRE_EQ(ret, strbuf.data()); + + /* Make sure the result was NUL-terminated and find the NUL */ + ATF_REQUIRE(strbuf.size() >= 1); + auto end = std::ranges::find(strbuf, '\0'); + ATF_REQUIRE(end != strbuf.end()); + + /* + * Check the result matches what we expect. Use a temporary + * string here instead of std::ranges::equal because this + * means ATF can print the mismatch. + */ + auto str = std::string(std::ranges::begin(strbuf), end); + ATF_REQUIRE_EQ(str, addr.output); + } +} + +/* + * inet_net_ntop() and inet_net_pton() for IPv6. + */ +ATF_TEST_CASE_WITHOUT_HEAD(inet_net_inet6) +ATF_TEST_CASE_BODY(inet_net_inet6) +{ + /* + * Define a list of addresses we want to check. Each address is + * passed to inet_net_pton() to convert it to an in6_addr, then we + * convert the in6_addr back to a string and compare it with the + * expected value. We want to test over-long prefixes here (such + * as 2001:db8::1/32), so we also specify what the result is + * expected to be. + */ + + struct test_addr { + std::string input; + int bits; + std::string output; + }; + + auto test_addrs = std::vector<test_addr>{ + // A prefix with a trailing :: + { "2001:db8::/32", 32, "2001:db8::/32" }, + + // A prefix with a leading ::. Note that the output is + // different from the input because inet_ntop() renders + // this prefix with an IPv4 suffix for legacy reasons. + { "::ffff:0:0/96", 96, "::ffff:0.0.0.0/96" }, + + // The same prefix but with the IPv4 legacy form as input. + { "::ffff:0.0.0.0/96", 96, "::ffff:0.0.0.0/96" }, + + // A prefix with an infix ::. + { "2001:db8::1/128", 128, "2001:db8::1/128" }, + + // A prefix with bits set which are outside the prefix; + // these should be silently ignored. + { "2001:db8:1:1:1:1:1:1/32", 32, "2001:db8::/32" }, + + // As above but with infix ::. + { "2001:db8::1/32", 32, "2001:db8::/32" }, + + // A prefix with only ::, commonly used to represent the + // entire address space. + { "::/0", 0, "::/0" }, + + // A single address with no ::. + { "2001:db8:1:1:1:1:1:1/128", 128, "2001:db8:1:1:1:1:1:1/128" }, + + // A prefix with no ::. + { "2001:db8:1:1:0:0:0:0/64", 64, "2001:db8:1:1::/64" }, + + // A prefix which isn't on a 16-bit boundary. + { "2001:db8:c000::/56", 56, "2001:db8:c000::/56" }, + + // A prefix which isn't on a nibble boundary. + { "2001:db8:c100::/57", 57, "2001:db8:c100::/57" }, + + // An address without a prefix length, which should be treated + // as a /128. + { "2001:db8::", 128, "2001:db8::/128" }, + { "2001:db8::1", 128, "2001:db8::1/128" }, + + // Test vectors provided in PR bin/289198. + { "fe80::1/64", 64, "fe80::/64" }, + { "fe80::f000:74ff:fe54:bed2/64", + 64, "fe80::/64" }, + { "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64", + 64, "ffff:ffff:ffff:ffff::/64" }, + { "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, + "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128" }, + }; + + for (auto const &addr: test_addrs) { + /* + * Convert the input string to an in6_addr + bits, and make + * sure the result produces the number of bits we expected. + */ + + auto in6 = in6_addr{}; + errno = 0; + auto bits = inet_net_pton(AF_INET6, addr.input.c_str(), + &in6, sizeof(in6)); + ATF_REQUIRE(bits != -1); + ATF_REQUIRE_EQ(bits, addr.bits); + + /* + * Convert the in6_addr back to a string + */ + + /* + * XXX: Should there be a constant for the size of the result + * buffer? For now, use ADDRSTRLEN + 4 ("/128") + 1 (NUL). + * + * Fill the buffer with 'Z', so we can check the result was + * properly terminated. + */ + auto strbuf = std::vector<char>(INET6_ADDRSTRLEN + 4 + 1, 'Z'); + auto ret = inet_net_ntop(AF_INET6, &in6, bits, + strbuf.data(), strbuf.size()); + ATF_REQUIRE(ret != NULL); + ATF_REQUIRE_EQ(ret, strbuf.data()); + + /* Make sure the result was NUL-terminated and find the NUL */ + ATF_REQUIRE(strbuf.size() >= 1); + auto end = std::ranges::find(strbuf, '\0'); + ATF_REQUIRE(end != strbuf.end()); + + /* + * Check the result matches what we expect. Use a temporary + * string here instead of std::ranges::equal because this + * means ATF can print the mismatch. + */ + auto str = std::string(std::ranges::begin(strbuf), end); + ATF_REQUIRE_EQ(str, addr.output); + } +} + +ATF_TEST_CASE_WITHOUT_HEAD(inet_net_pton_invalid) +ATF_TEST_CASE_BODY(inet_net_pton_invalid) +{ + auto ret = int{}; + auto addr4 = in_addr{}; + auto str4 = "10.0.0.0"s; + auto addr6 = in6_addr{}; + auto str6 = "2001:db8::"s; + + /* Passing an address which is too short should be an error */ + ret = inet_net_pton(AF_INET6, str6.c_str(), &addr6, sizeof(addr6) - 1); + ATF_REQUIRE_EQ(ret, -1); + + ret = inet_net_pton(AF_INET, str4.c_str(), &addr4, sizeof(addr4) - 1); + ATF_REQUIRE_EQ(ret, -1); + + /* Test some generally invalid addresses. */ + auto invalid4 = std::vector<std::string>{ + // Prefix length too big + "10.0.0.0/33", + // Prefix length is negative + "10.0.0.0/-1", + // Prefix length is not a number + "10.0.0.0/foo", + // Input is not a network prefix + "this is not an IP address", + }; + + for (auto const &addr: invalid4) { + auto ret = inet_net_pton(AF_INET, addr.c_str(), &addr4, + sizeof(addr4)); + ATF_REQUIRE_EQ(ret, -1); + } + + auto invalid6 = std::vector<std::string>{ + // Prefix length too big + "2001:db8::/129", + // Prefix length is negative + "2001:db8::/-1", + // Prefix length is not a number + "2001:db8::/foo", + // Input is not a network prefix + "this is not an IP address", + }; + + for (auto const &addr: invalid6) { + auto ret = inet_net_pton(AF_INET6, addr.c_str(), &addr6, + sizeof(addr6)); + ATF_REQUIRE_EQ(ret, -1); + } +} + +ATF_TEST_CASE_WITHOUT_HEAD(inet_net_ntop_invalid) +ATF_TEST_CASE_BODY(inet_net_ntop_invalid) +{ + auto addr4 = in_addr{}; + auto addr6 = in6_addr{}; + auto strbuf = std::vector<char>(INET6_ADDRSTRLEN + 4 + 1); + + /* + * Passing a buffer which is too small should not overrun the buffer. + * Test this by initialising the buffer to 'Z', and only providing + * part of it to the function. + */ + + std::ranges::fill(strbuf, 'Z'); + auto ret = inet_net_ntop(AF_INET6, &addr6, 128, strbuf.data(), 1); + ATF_REQUIRE_EQ(ret, nullptr); + ATF_REQUIRE_EQ(strbuf[1], 'Z'); + + std::ranges::fill(strbuf, 'Z'); + ret = inet_net_ntop(AF_INET, &addr4, 32, strbuf.data(), 1); + ATF_REQUIRE_EQ(ret, nullptr); + ATF_REQUIRE_EQ(strbuf[1], 'Z'); + + /* Check that invalid prefix lengths return an error */ + + ret = inet_net_ntop(AF_INET6, &addr6, 129, strbuf.data(), strbuf.size()); + ATF_REQUIRE_EQ(ret, nullptr); + ret = inet_net_ntop(AF_INET6, &addr6, -1, strbuf.data(), strbuf.size()); + ATF_REQUIRE_EQ(ret, nullptr); + + ret = inet_net_ntop(AF_INET, &addr4, 33, strbuf.data(), strbuf.size()); + ATF_REQUIRE_EQ(ret, nullptr); + ret = inet_net_ntop(AF_INET, &addr4, -1, strbuf.data(), strbuf.size()); + ATF_REQUIRE_EQ(ret, nullptr); +} + +ATF_INIT_TEST_CASES(tcs) +{ + ATF_ADD_TEST_CASE(tcs, inet_net_inet4); + ATF_ADD_TEST_CASE(tcs, inet_net_inet6); + ATF_ADD_TEST_CASE(tcs, inet_net_pton_invalid); + ATF_ADD_TEST_CASE(tcs, inet_net_ntop_invalid); +} diff --git a/lib/libc/tests/stdbit/Makefile b/lib/libc/tests/stdbit/Makefile new file mode 100644 index 000000000000..37450056007a --- /dev/null +++ b/lib/libc/tests/stdbit/Makefile @@ -0,0 +1,19 @@ +# ensure libc functions are tested, not clang's builtins +CFLAGS+= -fno-builtin + +ATF_TESTS_C+= stdc_bit_ceil_test \ + stdc_bit_floor_test \ + stdc_bit_width_test \ + stdc_count_ones_test \ + stdc_count_zeros_test \ + stdc_first_leading_one_test \ + stdc_first_leading_zero_test \ + stdc_first_trailing_one_test \ + stdc_first_trailing_zero_test \ + stdc_has_single_bit_test \ + stdc_leading_ones_test \ + stdc_leading_zeros_test \ + stdc_trailing_ones_test \ + stdc_trailing_zeros_test + +.include <bsd.test.mk> diff --git a/lib/libc/tests/stdbit/stdbit-test-framework.c b/lib/libc/tests/stdbit/stdbit-test-framework.c new file mode 100644 index 000000000000..368b38fb4745 --- /dev/null +++ b/lib/libc/tests/stdbit/stdbit-test-framework.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * Test framework for stdbit functions. + * Requires the following macros to be defined: + * + * FUNCSTEM -- name of the function without type suffix + * MKREFFUNC(name, type) -- macro to generate a reference + * implementation of the function as a static function + * named name with give argument type. + */ + +#include <sys/cdefs.h> +#include <atf-c.h> +#include <limits.h> +#include <stdbit.h> +#include <stdint.h> + +#define ATF_TC_WITHOUT_HEAD1(stem, suffix) ATF_TC_WITHOUT_HEAD2(__CONCAT(stem, suffix)) +#define ATF_TC_WITHOUT_HEAD2(case) ATF_TC_WITHOUT_HEAD(case) +#define ATF_TC_BODY1(stem, suffix, tc) ATF_TC_BODY2(__CONCAT(stem, suffix), tc) +#define ATF_TC_BODY2(case, tc) ATF_TC_BODY(case, tc) + +#define SUFFIX _uc +#define TYPE unsigned char +#define TYPE_WIDTH UCHAR_WIDTH +#include "stdbit-test-kernel.c" +#undef TYPE_WIDTH +#undef TYPE +#undef SUFFIX + +#define SUFFIX _us +#define TYPE unsigned short +#define TYPE_WIDTH USHRT_WIDTH +#include "stdbit-test-kernel.c" +#undef TYPE_WIDTH +#undef TYPE +#undef SUFFIX + +#define SUFFIX _ui +#define TYPE unsigned int +#define TYPE_WIDTH UINT_WIDTH +#include "stdbit-test-kernel.c" +#undef TYPE_WIDTH +#undef TYPE +#undef SUFFIX + +#define SUFFIX _ul +#define TYPE unsigned long +#define TYPE_WIDTH ULONG_WIDTH +#include "stdbit-test-kernel.c" +#undef TYPE_WIDTH +#undef TYPE +#undef SUFFIX + +#define SUFFIX _ull +#define TYPE unsigned long long +#define TYPE_WIDTH ULLONG_WIDTH +#include "stdbit-test-kernel.c" +#undef TYPE_WIDTH +#undef TYPE +#undef SUFFIX + +#define ADD_CASE(stem, suffix) ADD_CASE1(__CONCAT(stem, suffix)) +#define ADD_CASE1(case) ATF_TP_ADD_TC(tp, case) + +ATF_TP_ADD_TCS(tp) +{ + ADD_CASE(FUNCSTEM, _uc); + ADD_CASE(FUNCSTEM, _us); + ADD_CASE(FUNCSTEM, _ui); + ADD_CASE(FUNCSTEM, _ul); + ADD_CASE(FUNCSTEM, _ull); + + return (atf_no_error()); +} diff --git a/lib/libc/tests/stdbit/stdbit-test-kernel.c b/lib/libc/tests/stdbit/stdbit-test-kernel.c new file mode 100644 index 000000000000..d584e391ff6f --- /dev/null +++ b/lib/libc/tests/stdbit/stdbit-test-kernel.c @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * test kernel for stdbit functions. + * Requires the following macros to be defined: + * + * FUNCSTEM -- stem of the function name + * SUFFIX -- type suffic + * TYPE -- argument type + * MKREFFUNC(ref, type) -- reference function builder + */ + +#define FUNC __CONCAT(FUNCSTEM, SUFFIX) +#define REF __CONCAT(FUNCSTEM, __CONCAT(SUFFIX, _ref)) + +MKREFFUNC(REF, TYPE) + +ATF_TC_WITHOUT_HEAD1(FUNCSTEM, SUFFIX); +ATF_TC_BODY1(FUNCSTEM, SUFFIX, tc) +{ + uintmax_t has, want; + size_t i, j; + TYPE value; + + /* test all single-bit patterns */ + for (i = 0; i < TYPE_WIDTH; i++) { + value = (TYPE)1 << i; + has = FUNC(value); + want = REF(value); + ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)", + __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value); + } + + /* test all double-bit patterns */ + for (i = 0; i < TYPE_WIDTH; i++) { + for (j = 0; j < i; j++) { + value = (TYPE)1 << i | (TYPE)1 << j; + has = FUNC(value); + want = REF(value); + ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)", + __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value); + } + } + + /* test all barber-pole patterns */ + value = ~(TYPE)0; + for (i = 0; i < TYPE_WIDTH; i++) { + has = FUNC(value); + want = REF(value); + ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)", + __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value); + + value = ~value; + has = FUNC(value); + want = REF(value); + ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)", + __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value); + + value = ~value << 1; + } +} + +#undef REF +#undef FUNC diff --git a/lib/libc/tests/stdbit/stdc_bit_ceil_test.c b/lib/libc/tests/stdbit/stdc_bit_ceil_test.c new file mode 100644 index 000000000000..0495da55c5d9 --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_bit_ceil_test.c @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_bit_ceil +#define MKREFFUNC(name, type) \ + static type \ + name(type value) \ + { \ + type ceil = 1; \ + \ + while (ceil < value && ceil != 0) \ + ceil <<= 1; \ + \ + return (ceil); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_bit_floor_test.c b/lib/libc/tests/stdbit/stdc_bit_floor_test.c new file mode 100644 index 000000000000..a2c5b5f7d8ce --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_bit_floor_test.c @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_bit_floor +#define MKREFFUNC(name, type) \ + static type \ + name(type value) \ + { \ + type floor = 1; \ + \ + if (value == 0) \ + return (0); \ + \ + while (value != 1) { \ + floor <<= 1; \ + value >>= 1; \ + } \ + \ + return (floor); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_bit_width_test.c b/lib/libc/tests/stdbit/stdc_bit_width_test.c new file mode 100644 index 000000000000..bfcb2b3bd779 --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_bit_width_test.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_bit_width +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + unsigned width = 0; \ + \ + while (value != 0) { \ + value >>= 1; \ + width++; \ + } \ + \ + return (width); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_count_ones_test.c b/lib/libc/tests/stdbit/stdc_count_ones_test.c new file mode 100644 index 000000000000..9093edde495b --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_count_ones_test.c @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_count_ones +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + unsigned count = 0; \ + \ + while (value != 0) { \ + count += value & 1; \ + value >>= 1; \ + } \ + \ + return (count); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_count_zeros_test.c b/lib/libc/tests/stdbit/stdc_count_zeros_test.c new file mode 100644 index 000000000000..a82de6696a64 --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_count_zeros_test.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_count_zeros +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + unsigned count = 0; \ + \ + value = ~value; \ + while (value != 0) { \ + count += value & 1; \ + value >>= 1; \ + } \ + \ + return (count); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_first_leading_one_test.c b/lib/libc/tests/stdbit/stdc_first_leading_one_test.c new file mode 100644 index 000000000000..7312789262ea --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_first_leading_one_test.c @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_first_leading_one +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + type bit = 1; \ + unsigned pos = 1; \ + \ + if (value == 0) \ + return (0); \ + \ + while ((type)(bit << 1) != 0) \ + bit <<= 1; \ + \ + while ((bit & value) == 0) { \ + bit >>= 1; \ + pos++; \ + } \ + \ + return (pos); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_first_leading_zero_test.c b/lib/libc/tests/stdbit/stdc_first_leading_zero_test.c new file mode 100644 index 000000000000..df2a1f4b8673 --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_first_leading_zero_test.c @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_first_leading_zero +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + type bit = 1; \ + unsigned pos = 1; \ + \ + value = ~value; \ + if (value == 0) \ + return (0); \ + \ + while ((type)(bit << 1) != 0) \ + bit <<= 1; \ + \ + while ((bit & value) == 0) { \ + bit >>= 1; \ + pos++; \ + } \ + \ + return (pos); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_first_trailing_one_test.c b/lib/libc/tests/stdbit/stdc_first_trailing_one_test.c new file mode 100644 index 000000000000..43284786cd0e --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_first_trailing_one_test.c @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_first_trailing_one +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + type bit = 1; \ + unsigned pos = 1; \ + \ + if (value == 0) \ + return (0); \ + \ + while ((bit & value) == 0) { \ + bit <<= 1; \ + pos++; \ + } \ + \ + return (pos); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_first_trailing_zero_test.c b/lib/libc/tests/stdbit/stdc_first_trailing_zero_test.c new file mode 100644 index 000000000000..ab2a995dcc86 --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_first_trailing_zero_test.c @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_first_trailing_zero +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + type bit = 1; \ + unsigned pos = 1; \ + \ + value = ~value; \ + if (value == 0) \ + return (0); \ + \ + while ((bit & value) == 0) { \ + bit <<= 1; \ + pos++; \ + } \ + \ + return (pos); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_has_single_bit_test.c b/lib/libc/tests/stdbit/stdc_has_single_bit_test.c new file mode 100644 index 000000000000..3426deccd8d0 --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_has_single_bit_test.c @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_has_single_bit +#define MKREFFUNC(name, type) \ + static bool \ + name(type value) \ + { \ + type bit; \ + \ + for (bit = 1; bit != 0; bit <<= 1) \ + if (value == bit) \ + return (true); \ + \ + return (false); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_leading_ones_test.c b/lib/libc/tests/stdbit/stdc_leading_ones_test.c new file mode 100644 index 000000000000..616b3d5e6279 --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_leading_ones_test.c @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_leading_ones +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + type bit = 1; \ + unsigned count = 0; \ + \ + while ((type)(bit << 1) != 0) \ + bit <<= 1; \ + \ + while (bit != 0 && (bit & value) != 0) { \ + bit >>= 1; \ + count++; \ + } \ + \ + return (count); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_leading_zeros_test.c b/lib/libc/tests/stdbit/stdc_leading_zeros_test.c new file mode 100644 index 000000000000..7c9d26e86456 --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_leading_zeros_test.c @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_leading_zeros +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + type bit = 1; \ + unsigned count = 0; \ + \ + while ((type)(bit << 1) != 0) \ + bit <<= 1; \ + \ + while (bit != 0 && (bit & value) == 0) { \ + bit >>= 1; \ + count++; \ + } \ + \ + return (count); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_trailing_ones_test.c b/lib/libc/tests/stdbit/stdc_trailing_ones_test.c new file mode 100644 index 000000000000..9e261cc543de --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_trailing_ones_test.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_trailing_ones +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + type bit = 1; \ + unsigned count = 0; \ + \ + while (bit != 0 && (bit & value) != 0) { \ + bit <<= 1; \ + count++; \ + } \ + \ + return (count); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdbit/stdc_trailing_zeros_test.c b/lib/libc/tests/stdbit/stdc_trailing_zeros_test.c new file mode 100644 index 000000000000..81fe11c5c82a --- /dev/null +++ b/lib/libc/tests/stdbit/stdc_trailing_zeros_test.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#define FUNCSTEM stdc_trailing_zeros +#define MKREFFUNC(name, type) \ + static unsigned \ + name(type value) \ + { \ + type bit = 1; \ + unsigned count = 0; \ + \ + while (bit != 0 && (bit & value) == 0) { \ + bit <<= 1; \ + count++; \ + } \ + \ + return (count); \ + } + +#include "stdbit-test-framework.c" diff --git a/lib/libc/tests/stdio/printfloat_test.c b/lib/libc/tests/stdio/printfloat_test.c index 031859124163..4493fe1c15d3 100644 --- a/lib/libc/tests/stdio/printfloat_test.c +++ b/lib/libc/tests/stdio/printfloat_test.c @@ -398,6 +398,18 @@ ATF_TC_BODY(subnormal_float, tc) testfmt("-0X1P-149", "%A", negative); } +ATF_TC_WITHOUT_HEAD(hexadecimal_rounding_fullprec); +ATF_TC_BODY(hexadecimal_rounding_fullprec, tc) +{ + /* Double: %.13a with binary64 mantissa=53 */ + testfmt("0x1.1234567890bbbp+0", "%.13a", 0x1.1234567890bbbp+0); + +#if defined(__aarch64__) + /* On arm64, long double is IEEE binary128 (mantissa=113) */ + testfmt("0x1.3c0ca428c59fbbbbbbbbbbbbbbbbp+0", "%.28La", 0x1.3c0ca428c59fbbbbbbbbbbbbbbbbp+0L); +#endif +} + ATF_TP_ADD_TCS(tp) { @@ -414,6 +426,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, hexadecimal_rounding); ATF_TP_ADD_TC(tp, subnormal_double); ATF_TP_ADD_TC(tp, subnormal_float); + ATF_TP_ADD_TC(tp, hexadecimal_rounding_fullprec); return (atf_no_error()); } diff --git a/lib/libc/tests/stdlib/strfmon_test.c b/lib/libc/tests/stdlib/strfmon_test.c index 86f6256dba0b..c6c20bd26985 100644 --- a/lib/libc/tests/stdlib/strfmon_test.c +++ b/lib/libc/tests/stdlib/strfmon_test.c @@ -1,31 +1,14 @@ -/*- - * Copyright (C) 2018 Conrad Meyer <cem@FreeBSD.org> +/* + * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org> * All rights reserved. + * Copyright (c) 2022-2025 Jose Luis Duran <jlduran@FreeBSD.org> * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * SPDX-License-Identifier: BSD-2-Clause */ #include <sys/param.h> +#include <errno.h> #include <locale.h> #include <monetary.h> #include <stdio.h> @@ -208,6 +191,56 @@ ATF_TC_BODY(strfmon_international_currency_code, tc) } } +ATF_TC_WITHOUT_HEAD(strfmon_plus_or_parenthesis); +ATF_TC_BODY(strfmon_plus_or_parenthesis, tc) +{ + const struct { + const char *format; + const char *locale; + const char *expected; + } tests[] = { + { "%+n", "en_US.UTF-8", "[$123.45] [-$123.45]" }, + { "%+i", "en_US.UTF-8", "[USD123.45] [-USD123.45]" }, + { "%(n", "C", "[123.45] [(123.45)]" }, + { "%(i", "C", "[123.45] [(123.45)]" }, + { "%(n", "en_US.UTF-8", "[$123.45] [($123.45)]" }, + { "%(i", "en_US.UTF-8", "[USD123.45] [(USD123.45)]" }, + { "%n", "C", "[123.45] [-123.45]" }, + { "%i", "C", "[123.45] [-123.45]" }, + { "%n", "en_US.UTF-8", "[$123.45] [-$123.45]" }, + { "%i", "en_US.UTF-8", "[USD123.45] [-USD123.45]" }, + }; + size_t i; + char actual[100], format[50]; + + for (i = 0; i < nitems(tests); ++i) { + if (setlocale(LC_MONETARY, tests[i].locale) == NULL) + atf_tc_skip("unable to setlocale(): %s", + tests[i].locale); + + snprintf(format, sizeof(format), "[%s] [%s]", + tests[i].format, tests[i].format); + strfmon(actual, sizeof(actual) - 1, format, + 123.45, -123.45); + ATF_CHECK_STREQ_MSG(tests[i].expected, actual, + "[%s] %s", tests[i].format, tests[i].locale); + } + + /* + * The '+' flag was included in a conversion specification and + * the locale's positive_sign and negative_sign values would + * both be returned by localeconv() as empty strings. + */ + if (setlocale(LC_MONETARY, "C") == NULL) + atf_tc_skip("unable to setlocale(): %s", tests[i].locale); + + ATF_CHECK_ERRNO(EINVAL, strfmon(actual, sizeof(actual) - 1, + "[%+n] [%+n]", 123.45, -123.45)); + + ATF_CHECK_ERRNO(EINVAL, strfmon(actual, sizeof(actual) - 1, + "[%+i] [%+i]", 123.45, -123.45)); +} + ATF_TC(strfmon_l); ATF_TC_HEAD(strfmon_l, tc) { @@ -220,7 +253,7 @@ ATF_TC_BODY(strfmon_l, tc) const char *locale; const char *expected; } tests[] = { - { "C", "[ **1234.57 ] [ **1234.57 ]" }, + { "C", "[ **1234.57] [ **1234.57]" }, { "de_DE.UTF-8", "[ **1234,57 €] [ **1.234,57 EUR]" }, { "en_GB.UTF-8", "[ £**1234.57] [ GBP**1,234.57]" }, }; @@ -247,6 +280,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, strfmon_cs_precedes_0); ATF_TP_ADD_TC(tp, strfmon_cs_precedes_1); ATF_TP_ADD_TC(tp, strfmon_international_currency_code); + ATF_TP_ADD_TC(tp, strfmon_plus_or_parenthesis); ATF_TP_ADD_TC(tp, strfmon_l); return (atf_no_error()); } diff --git a/lib/libc/tests/stdtime/Makefile b/lib/libc/tests/stdtime/Makefile index 6b9068e1641b..590dea22da31 100644 --- a/lib/libc/tests/stdtime/Makefile +++ b/lib/libc/tests/stdtime/Makefile @@ -3,6 +3,7 @@ ATF_TESTS_C+= strptime_test ATF_TESTS_C+= detect_tz_changes_test +CFLAGS.detect_tz_changes_test+= -I${SRCTOP}/contrib/tzcode .if ${MK_DETECT_TZ_CHANGES} != "no" CFLAGS.detect_tz_changes_test+= -DDETECT_TZ_CHANGES .endif diff --git a/lib/libc/tests/stdtime/detect_tz_changes_test.c b/lib/libc/tests/stdtime/detect_tz_changes_test.c index 6648d8498cc5..06c31c9fbc3d 100644 --- a/lib/libc/tests/stdtime/detect_tz_changes_test.c +++ b/lib/libc/tests/stdtime/detect_tz_changes_test.c @@ -20,12 +20,16 @@ #include <time.h> #include <unistd.h> +#include "tzdir.h" + #include <atf-c.h> -static const struct tzcase { +struct tzcase { const char *tzfn; const char *expect; -} tzcases[] = { +}; + +static const struct tzcase tzcases[] = { /* * A handful of time zones and the expected result of * strftime("%z (%Z)", tm) when that time zone is active @@ -41,7 +45,8 @@ static const struct tzcase { { "UTC", "+0000 (UTC)" }, { 0 }, }; - +static const struct tzcase utc = { "UTC", "+0000 (UTC)" }; +static const struct tzcase invalid = { "invalid", "+0000 (-00)" }; static const time_t then = 1751328000; /* 2025-07-01 00:00:00 UTC */ static bool debugging; @@ -62,9 +67,9 @@ debug(const char *fmt, ...) static void change_tz(const char *tzn) { - static const char *zfn = "/usr/share/zoneinfo"; - static const char *tfn = "root/etc/.localtime"; - static const char *dfn = "root/etc/localtime"; + static const char *zfn = TZDIR; + static const char *tfn = "root" TZDEFAULT ".tmp"; + static const char *dfn = "root" TZDEFAULT; ssize_t clen; int zfd, sfd, dfd; @@ -96,6 +101,50 @@ test_tz(const char *expect) ATF_CHECK_STREQ(expect, buf); } +ATF_TC(tz_default); +ATF_TC_HEAD(tz_default, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test default zone"); + atf_tc_set_md_var(tc, "require.user", "root"); +} +ATF_TC_BODY(tz_default, tc) +{ + /* prepare chroot with no /etc/localtime */ + ATF_REQUIRE_EQ(0, mkdir("root", 0755)); + ATF_REQUIRE_EQ(0, mkdir("root/etc", 0755)); + /* enter chroot */ + ATF_REQUIRE_EQ(0, chroot("root")); + ATF_REQUIRE_EQ(0, chdir("/")); + /* check timezone */ + unsetenv("TZ"); + test_tz("+0000 (UTC)"); +} + +ATF_TC(tz_invalid_file); +ATF_TC_HEAD(tz_invalid_file, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test invalid zone file"); + atf_tc_set_md_var(tc, "require.user", "root"); +} +ATF_TC_BODY(tz_invalid_file, tc) +{ + static const char *dfn = "root/etc/localtime"; + int fd; + + /* prepare chroot with bogus /etc/localtime */ + ATF_REQUIRE_EQ(0, mkdir("root", 0755)); + ATF_REQUIRE_EQ(0, mkdir("root/etc", 0755)); + ATF_REQUIRE((fd = open(dfn, O_RDWR | O_CREAT, 0644)) >= 0); + ATF_REQUIRE_EQ(8, write(fd, "invalid\n", 8)); + ATF_REQUIRE_EQ(0, close(fd)); + /* enter chroot */ + ATF_REQUIRE_EQ(0, chroot("root")); + ATF_REQUIRE_EQ(0, chdir("/")); + /* check timezone */ + unsetenv("TZ"); + test_tz(invalid.expect); +} + ATF_TC(thin_jail); ATF_TC_HEAD(thin_jail, tc) { @@ -320,6 +369,30 @@ test_tz_env(const char *tzval, const char *expect) test_tz(expect); } +static void +tz_env_common(void) +{ + char path[MAXPATHLEN]; + const struct tzcase *tzcase = tzcases; + int len; + + /* relative path */ + for (tzcase = tzcases; tzcase->tzfn != NULL; tzcase++) + test_tz_env(tzcase->tzfn, tzcase->expect); + /* absolute path */ + for (tzcase = tzcases; tzcase->tzfn != NULL; tzcase++) { + len = snprintf(path, sizeof(path), "%s/%s", TZDIR, tzcase->tzfn); + ATF_REQUIRE(len > 0 && (size_t)len < sizeof(path)); + test_tz_env(path, tzcase->expect); + } + /* absolute path with additional slashes */ + for (tzcase = tzcases; tzcase->tzfn != NULL; tzcase++) { + len = snprintf(path, sizeof(path), "%s/////%s", TZDIR, tzcase->tzfn); + ATF_REQUIRE(len > 0 && (size_t)len < sizeof(path)); + test_tz_env(path, tzcase->expect); + } +} + ATF_TC(tz_env); ATF_TC_HEAD(tz_env, tc) { @@ -327,10 +400,22 @@ ATF_TC_HEAD(tz_env, tc) } ATF_TC_BODY(tz_env, tc) { - const struct tzcase *tzcase; + tz_env_common(); + /* escape from TZDIR is permitted when not setugid */ + test_tz_env("../zoneinfo/UTC", utc.expect); +} - for (tzcase = tzcases; tzcase->tzfn != NULL; tzcase++) - test_tz_env(tzcase->tzfn, tzcase->expect); + +ATF_TC(tz_invalid_env); +ATF_TC_HEAD(tz_invalid_env, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test invalid TZ value"); + atf_tc_set_md_var(tc, "require.user", "root"); +} +ATF_TC_BODY(tz_invalid_env, tc) +{ + test_tz_env("invalid", invalid.expect); + test_tz_env(":invalid", invalid.expect); } ATF_TC(setugid); @@ -367,23 +452,25 @@ ATF_TC_HEAD(tz_env_setugid, tc) } ATF_TC_BODY(tz_env_setugid, tc) { - const struct tzcase *tzcase = tzcases; - ATF_REQUIRE_EQ(0, seteuid(UID_NOBODY)); ATF_REQUIRE(issetugid()); - for (tzcase = tzcases; tzcase->tzfn != NULL; tzcase++) - test_tz_env(tzcase->tzfn, tzcase->expect); + tz_env_common(); + /* escape from TZDIR is not permitted when setugid */ + test_tz_env("../zoneinfo/UTC", invalid.expect); } ATF_TP_ADD_TCS(tp) { debugging = !getenv("__RUNNING_INSIDE_ATF_RUN") && isatty(STDERR_FILENO); + ATF_TP_ADD_TC(tp, tz_default); + ATF_TP_ADD_TC(tp, tz_invalid_file); ATF_TP_ADD_TC(tp, thin_jail); #ifdef DETECT_TZ_CHANGES ATF_TP_ADD_TC(tp, detect_tz_changes); #endif /* DETECT_TZ_CHANGES */ ATF_TP_ADD_TC(tp, tz_env); + ATF_TP_ADD_TC(tp, tz_invalid_env); ATF_TP_ADD_TC(tp, setugid); ATF_TP_ADD_TC(tp, tz_env_setugid); return (atf_no_error()); diff --git a/lib/libc/tests/string/memcmp_test.c b/lib/libc/tests/string/memcmp_test.c index 5286a0b994f3..fa2f498ccfaf 100644 --- a/lib/libc/tests/string/memcmp_test.c +++ b/lib/libc/tests/string/memcmp_test.c @@ -41,14 +41,14 @@ #endif /* - * On FreeBSD we demand that memcmp returns the difference between the - * characters at the first site of mismatch. However, ISO/IEC 9899:1990 - * only specifies that a number greater than, equal to, or less than - * zero shall be returned. If a unit test for this less strict - * behaviour is desired, define RES(x) to be (((x) > 0) - ((x) < 0)). + * On FreeBSD we previously demanded that memcmp returns the difference + * between the characters at the first site of mismatch. However, + * ISO/IEC 9899:1990 only specifies that a number greater than, equal + * to, or less than zero shall be returned. If a unit test for the + * more strict behaviour is desired, define RES(x) to be (x). */ #ifndef RES -#define RES(x) (x) +#define RES(x) (((x) > 0) - ((x) < 0)) #endif static int (*memcmp_fn)(const void *, const void *, size_t); diff --git a/lib/libc/tests/sys/cpuset_test.c b/lib/libc/tests/sys/cpuset_test.c index 53d6a8215bbc..c8ad225fadfc 100644 --- a/lib/libc/tests/sys/cpuset_test.c +++ b/lib/libc/tests/sys/cpuset_test.c @@ -34,8 +34,10 @@ #include <sys/uio.h> #include <sys/wait.h> +#include <assert.h> #include <errno.h> #include <stdio.h> +#include <stdlib.h> #include <unistd.h> #include <atf-c.h> @@ -107,6 +109,19 @@ skip_ltncpu(int ncpu, cpuset_t *mask) atf_tc_skip("Test requires %d or more cores.", ncpu); } +static void +skip_ltncpu_root(int ncpu, cpuset_t *mask) +{ + + CPU_ZERO(mask); + ATF_REQUIRE_EQ(0, cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, + -1, sizeof(*mask), mask)); + if (CPU_COUNT(mask) < ncpu) { + atf_tc_skip("Test requires cpuset root with %d or more cores.", + ncpu); + } +} + ATF_TC(newset); ATF_TC_HEAD(newset, tc) { @@ -234,9 +249,8 @@ ATF_TC_BODY(deadlk, tc) } static int -do_jail(int sock) +create_jail(void) { - struct jail_test_info info; struct iovec iov[2]; char *name; int error; @@ -250,8 +264,22 @@ do_jail(int sock) iov[1].iov_base = name; iov[1].iov_len = strlen(name) + 1; - if (jail_set(iov, 2, JAIL_CREATE | JAIL_ATTACH) < 0) + error = jail_set(iov, 2, JAIL_CREATE | JAIL_ATTACH); + free(name); + if (error < 0) return (FAILURE_JAIL); + return (0); +} + +static int +do_jail(int sock) +{ + struct jail_test_info info; + int error; + + error = create_jail(); + if (error != 0) + return (error); /* Record parameters, kick them over, then make a swift exit. */ CPU_ZERO(&info.jail_tidmask); @@ -641,6 +669,111 @@ ATF_TC_BODY(jail_attach_disjoint, tc) try_attach(jid, &smask); } +struct nproc_info { + long nproc_init; + long nproc_final; + long nproc_global; +}; + +ATF_TC(jail_nproc); +ATF_TC_HEAD(jail_nproc, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test that _SC_PROCESSORS_ONLN reflects jail cpuset constraints"); +} +ATF_TC_BODY(jail_nproc, tc) +{ + cpuset_t jmask; + struct nproc_info ninfo = { }; + int sockpair[2]; + cpusetid_t setid; + ssize_t readsz; + pid_t pid; + int fcpu, error, pfd, sock; + char okb = 0x7f, rcvb; + + skip_ltncpu_root(2, &jmask); + fcpu = CPU_FFS(&jmask) - 1; + + /* + * Just adjusting our affinity should not affect the number of + * processors considered online- we want to be sure that it's only + * adjusted if our jail's root set is. + */ + CPU_CLR(fcpu, &jmask); + error = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, -1, + sizeof(jmask), &jmask); + ATF_REQUIRE_EQ(0, error); + ATF_REQUIRE(sysconf(_SC_NPROCESSORS_ONLN) > CPU_COUNT(&jmask)); + + ATF_REQUIRE_EQ(0, socketpair(PF_UNIX, SOCK_STREAM, 0, sockpair)); + + /* We'll wait on the procdesc, too, so we can fail faster if it dies. */ + ATF_REQUIRE((pid = pdfork(&pfd, 0)) != -1); + + if (pid == 0) { + /* First child sets up the jail. */ + sock = sockpair[SP_CHILD]; + close(sockpair[SP_PARENT]); + + error = create_jail(); + if (error != 0) + _exit(error); + + ninfo.nproc_init = sysconf(_SC_NPROCESSORS_ONLN); + + /* Signal the parent that we're jailed. */ + readsz = write(sock, &okb, sizeof(okb)); + assert(readsz == sizeof(okb)); + + /* Wait for parent to adjust our mask and signal OK. */ + readsz = read(sock, &rcvb, sizeof(rcvb)); + assert(readsz == sizeof(rcvb)); + assert(rcvb == okb); + + ninfo.nproc_final = sysconf(_SC_NPROCESSORS_ONLN); + ninfo.nproc_global = sysconf(_SC_NPROCESSORS_CONF); + readsz = write(sock, &ninfo, sizeof(ninfo)); + assert(readsz == sizeof(ninfo)); + + _exit(0); + } + + close(sockpair[SP_CHILD]); + sock = sockpair[SP_PARENT]; + + /* Wait for signal that they are jailed. */ + readsz = read(sock, &rcvb, sizeof(rcvb)); + assert(readsz == sizeof(rcvb)); + assert(rcvb == okb); + + /* Grab the cpuset id and adjust it. */ + error = cpuset_getid(CPU_LEVEL_ROOT, CPU_WHICH_PID, pid, &setid); + ATF_REQUIRE_EQ(0, error); + error = cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_CPUSET, + setid, sizeof(jmask), &jmask); + ATF_REQUIRE_EQ(0, error); + + /* Signal OK to proceed. */ + readsz = write(sock, &okb, sizeof(okb)); + ATF_REQUIRE_EQ(sizeof(okb), readsz); + + /* Grab our final nproc info. */ + readsz = read(sock, &ninfo, sizeof(ninfo)); + ATF_REQUIRE_EQ(sizeof(ninfo), readsz); + + /* + * We set our own affinity to jmask, which is derived from *our* root + * set, at the beginning of the test. The jail would inherit from this + * set, so we just re-use that mask here to confirm that + * _SC_NPROCESSORS_ONLN did actually drop in response to us limiting the + * jail, and that its _SC_NPROCESSORS_CONF did not. + */ + ATF_REQUIRE_EQ(CPU_COUNT(&jmask) + 1, ninfo.nproc_init); + ATF_REQUIRE_EQ(CPU_COUNT(&jmask) + 1, ninfo.nproc_global); + ATF_REQUIRE_EQ(CPU_COUNT(&jmask), ninfo.nproc_final); +} + ATF_TC(badparent); ATF_TC_HEAD(badparent, tc) { @@ -686,6 +819,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, jail_attach_prevbase); ATF_TP_ADD_TC(tp, jail_attach_plain); ATF_TP_ADD_TC(tp, jail_attach_disjoint); + ATF_TP_ADD_TC(tp, jail_nproc); ATF_TP_ADD_TC(tp, badparent); return (atf_no_error()); } diff --git a/lib/libc/tests/tls/dso/Makefile b/lib/libc/tests/tls/dso/Makefile index 5efd8b29a6bd..783534ff7aae 100644 --- a/lib/libc/tests/tls/dso/Makefile +++ b/lib/libc/tests/tls/dso/Makefile @@ -6,6 +6,7 @@ SRCS= h_tls_dlopen.c MAN= PACKAGE= tests +NO_DEV_PACKAGE= LIBDIR= ${TESTSBASE}/lib/libc/tls SHLIB_MAJOR= 1 diff --git a/lib/libc/tests/tls_dso/Makefile b/lib/libc/tests/tls_dso/Makefile index 89296c643695..7cb8f98b431e 100644 --- a/lib/libc/tests/tls_dso/Makefile +++ b/lib/libc/tests/tls_dso/Makefile @@ -7,6 +7,7 @@ LIBDIR= ${TESTSBASE}/lib/libc/tls SHLIBDIR= ${TESTSBASE}/lib/libc/tls SHLIB_MAJOR= 1 PACKAGE= tests +NO_DEV_PACKAGE= WITHOUT_STATIC= WITHOUT_PROFILE= |
