]> git.ipfire.org Git - thirdparty/glibc.git/log
thirdparty/glibc.git
2 months agomath: Fix UB in ldbl-128ibm setpayload azanella/ubsan-undef
Adhemerval Zanella [Thu, 8 May 2025 12:21:55 +0000 (12:21 +0000)] 
math: Fix UB in ldbl-128ibm setpayload

2 months agomath: Fix UB in ldbl-128ibm llroundl
Adhemerval Zanella [Thu, 8 May 2025 12:21:06 +0000 (12:21 +0000)] 
math: Fix UB in ldbl-128ibm llroundl

2 months agomath: Fix UB in ldbl-128ibm lrintl
Adhemerval Zanella [Thu, 8 May 2025 12:20:56 +0000 (12:20 +0000)] 
math: Fix UB in ldbl-128ibm lrintl

2 months agomath: Fix UB in ldbl-128ibm llrintl
Adhemerval Zanella [Thu, 8 May 2025 12:20:21 +0000 (12:20 +0000)] 
math: Fix UB in ldbl-128ibm llrintl

2 months agomath: Fix UB in ldbl-128 __ieee754_rem_pio2l
Adhemerval Zanella [Wed, 7 May 2025 16:04:23 +0000 (16:04 +0000)] 
math: Fix UB in ldbl-128 __ieee754_rem_pio2l

UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-128ibm/e_rem_pio2l.c:254:27 left shift of 325455441821696 by 23 cannot be represented in type 'long int'

2 months agomath: Fix UB in roundevenf_finite
Adhemerval Zanella [Wed, 7 May 2025 15:59:59 +0000 (15:59 +0000)] 
math: Fix UB in roundevenf_finite

On powerpc64le tanf tests show:

UBSAN: Undefined behaviour in ../sysdeps/ieee754/flt-32/math_config.h:105:11 passing zero to __builtin_ctz()

2 months agomath: Fix UB in ldbl-128ibm ilogb
Adhemerval Zanella [Wed, 7 May 2025 13:37:15 +0000 (13:37 +0000)] 
math: Fix UB in ldbl-128ibm ilogb

UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-128ibm/e_ilogbl.c:45:37 left shift of 4611686018427387904 by 1 cannot be represented in type 'long int'

2 months agomath: Fix UB in flt-32 rint
Adhemerval Zanella [Tue, 6 May 2025 13:46:34 +0000 (13:46 +0000)] 
math: Fix UB in flt-32 rint

UBSAN: Undefined behaviour in ../sysdeps/ieee754/flt-32/s_rintf.c:46:4 left shift of 1 by 31 cannot be represented in type 'int'

2 months agomath: Fix UB in flt-32 nearbyint
Adhemerval Zanella [Tue, 6 May 2025 13:43:36 +0000 (13:43 +0000)] 
math: Fix UB in flt-32 nearbyint

UBSAN: Undefined behaviour in ../sysdeps/ieee754/flt-32/s_nearbyintf.c:53:4 left shift of 1 by 31 cannot be represented in type 'int'

2 months agomath: Fix UB on dbl-64 rint
Adhemerval Zanella [Tue, 6 May 2025 13:38:31 +0000 (13:38 +0000)] 
math: Fix UB on dbl-64 rint

UBSAN: Undefined behaviour in ../sysdeps/ieee754/dbl-64/s_rint.c:53:4 left shift of 1 by 63 cannot be represented in type 'long long int'

2 months agomath: Fix UB in dbl-64 lrint
Adhemerval Zanella [Tue, 6 May 2025 11:43:19 +0000 (11:43 +0000)] 
math: Fix UB in dbl-64 lrint

UBSAN: Undefined behaviour in ../sysdeps/ieee754/dbl-64/s_lrint.c:99:30 left shift of 1048576 by 11 cannot be represented in type 'long int'

2 months agomath: Fix UB in ldbl-128 setpayload
Adhemerval Zanella [Mon, 5 May 2025 15:28:25 +0000 (12:28 -0300)] 
math: Fix UB in ldbl-128 setpayload

UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_setpayloadl_main.c:47:34 shift exponent 16431 is too large for 64-bit type 'long long unsigned int'

2 months agomath: Fix UB in flt-32 and dbl-64 significand
Adhemerval Zanella [Mon, 5 May 2025 15:22:54 +0000 (12:22 -0300)] 
math: Fix UB in flt-32 and dbl-64 significand

On x86_64:

$ math/test-float-significand
testing float (without inline functions)
UBSAN: Undefined behaviour in ./s_significand_template.c:31:45 negation of 2147483648 cannot be represented in type 'int'

2 months agomath: Fix UB in ldbl-96 sinl
Adhemerval Zanella [Mon, 5 May 2025 15:22:37 +0000 (12:22 -0300)] 
math: Fix UB in ldbl-96 sinl

2 months agomath: Fix UB in ldbl-128 powl
Adhemerval Zanella [Mon, 5 May 2025 13:48:40 +0000 (10:48 -0300)] 
math: Fix UB in ldbl-128 powl

testing _Float128 (without inline functions)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/e_powl.c:439:11 left shift of 4294953849 by 16 cannot be represented in type 'int'

2 months agomath: Fix UB in ldbl-128 roundl
Adhemerval Zanella [Mon, 5 May 2025 13:45:15 +0000 (10:45 -0300)] 
math: Fix UB in ldbl-128 roundl

$ math/test-float128-cospi
testing _Float128 (without inline functions)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_roundl.c:75:30 left shift of 1 by 63 cannot be represented in type 'long long int'

2 months agomath: Fix UB in __ieee754_rem_pio2l
Adhemerval Zanella [Mon, 5 May 2025 13:37:57 +0000 (10:37 -0300)] 
math: Fix UB in __ieee754_rem_pio2l

2 months agomath: Fix UB in ldbl-128 rintl
Adhemerval Zanella [Mon, 5 May 2025 13:37:35 +0000 (10:37 -0300)] 
math: Fix UB in ldbl-128 rintl

UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_rintl.c:60:4 left shift of 1 by 63 cannot be represented in type 'long int'

2 months agomath: Fix UB in ldbl-128 nearbyintl
Adhemerval Zanella [Mon, 5 May 2025 12:45:48 +0000 (09:45 -0300)] 
math: Fix UB in ldbl-128 nearbyintl

UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_nearbyintl.c:61:4 left shift of 1 by 63 cannot be represented in type 'long int'

2 months agomath: Fix UB in lroundl
Adhemerval Zanella [Mon, 5 May 2025 12:42:07 +0000 (09:42 -0300)] 
math: Fix UB in lroundl

UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_lroundl.c:72:32 left shift of 562949953421312 by 14 cannot be represented in type 'long int'

2 months agomath: Fix UB in ldbl-128 lrintl
Adhemerval Zanella [Mon, 5 May 2025 12:39:07 +0000 (09:39 -0300)] 
math: Fix UB in ldbl-128 lrintl

$ math/test-float128-llrint

UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_llrintl.c:83:31 left shift of 281474976710656 by 15 cannot be represented in type 'long long int'
Aborted

UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_lrintl.c:111:30 left shift of 281474976710656 by 15 cannot be represented in type 'long int'
Aborted

2 months agomath: Fix UB in ldbl-128 llrintl
Adhemerval Zanella [Mon, 5 May 2025 12:36:51 +0000 (09:36 -0300)] 
math: Fix UB in ldbl-128 llrintl

testing _Float128 (without inline functions)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_llrintl.c:83:31 left shift of 281474976710656 by 15 cannot be represented in type 'long long int'

2 months agomath: Fix UB on ldbl-128 fmodl
Adhemerval Zanella [Mon, 5 May 2025 12:22:21 +0000 (09:22 -0300)] 
math: Fix UB on ldbl-128 fmodl

2 months agomath: Fix UB in ldbl-128 llroundl
Adhemerval Zanella [Fri, 2 May 2025 18:41:00 +0000 (15:41 -0300)] 
math: Fix UB in ldbl-128 llroundl

Building with --enable-ubasn triggers:

$ math/test-float128-llround
testing _Float128 (without inline functions)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_llroundl.c:65:37 left shift of 562949953421312 by 14 cannot be represented in type 'long long int'
Aborted

2 months agomath: Fix UB in ldbl-96 setayloadl
Adhemerval Zanella [Fri, 2 May 2025 18:36:50 +0000 (15:36 -0300)] 
math: Fix UB in ldbl-96 setayloadl

The code can shift the 1ULL for value larger than 32 depending of
the exponent value.  Building with ubsan triggers:

$ math/test-ldouble-setpayload
testing long double (without inline functions)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-96/s_setpayloadl_main.c:48:32 shift exponent 16414 is too large for 32-bit type 'unsigned int'

2 months agomath: Fix UB on ldbl-96 remquol
Adhemerval Zanella [Fri, 2 May 2025 18:15:45 +0000 (15:15 -0300)] 
math: Fix UB on ldbl-96 remquol

Building with --enable-ubasn triggers:

$ math/test-ldouble-pow
testing long double (without inline functions)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-96/s_roundl.c:75:28 left shift of 1 by 31 cannot be represented in type 'int'

2 months agomath: Fix UB on lroundl
Adhemerval Zanella [Fri, 2 May 2025 17:59:13 +0000 (14:59 -0300)] 
math: Fix UB on lroundl

Building with --enable-ubasn triggers:

$ math/test-ldouble-pow
testing long double (without inline functions)
UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-96/s_roundl.c:75:28 left shift of 1 by 31 cannot be represented in type 'int'
Aborted

2 months agomath: Fix UB on llroundl
Adhemerval Zanella [Fri, 2 May 2025 17:46:40 +0000 (14:46 -0300)] 
math: Fix UB on llroundl

Building with --enable-ubasn triggers:

UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-96/s_llroundl.c:70:25 left shift of 4294967296 by 31 cannot be represented in type 'long long int'

The right shift is undefined if value overflow, but code is assuming
an arithmetic shift.

2 months agox86: Fix UB in isnanl
Adhemerval Zanella [Fri, 2 May 2025 12:57:47 +0000 (09:57 -0300)] 
x86: Fix UB in isnanl

Building with --enable-ubasn triggers:

$ math/test-ldouble-roundeven
UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-96/s_roundl.c:75:28 left shift of 1 by 31 cannot be represented in type 'int'

Also adds the inputs that triggers it on isnan testcase.

2 months agox86: Fix UB in isinfl
Adhemerval Zanella [Thu, 1 May 2025 21:14:37 +0000 (18:14 -0300)] 
x86: Fix UB in isinfl

Building with --enable-ubasn triggers:

$ math/test-ldouble-isinf
UBSAN: Undefined behaviour in ../sysdeps/x86/fpu/s_isinfl.c:25:8 negation of 2147483648 cannot be represented in type 'int'

Also move the isinfl implementation to sysdeps/x86 and remove the
sysdeps/x86_64 version.

2 months agogmon: Fix UB in sprofil
Adhemerval Zanella [Fri, 25 Apr 2025 18:27:08 +0000 (15:27 -0300)] 
gmon: Fix UB in sprofil

UBSAN: Undefined behaviour in ../sysdeps/posix/sprofil.c:272:16 variable length array bound evaluates to non-positive value 0

Move the VLA to after the porfile stop condition (where size might
be zero).

2 months agogmon: Fix UB in tst-sprofil.c
Adhemerval Zanella [Fri, 25 Apr 2025 18:26:35 +0000 (15:26 -0300)] 
gmon: Fix UB in tst-sprofil.c

UBSAN: Undefined behaviour in tst-sprofil.c:140:6 unsigned integer overflow: 2432902008176640000 + 7812407968270641256 cannot be represened in type 'long int'

Use unsigned types for the fibonacci.

2 months agostring: Remove UB on ffs/ffsll
Adhemerval Zanella [Fri, 25 Apr 2025 17:28:22 +0000 (14:28 -0300)] 
string: Remove UB on ffs/ffsll

Building with ubsan on 32 bit architecture, tst-ffs shows:

ffsll(0x4000000000000000) as expected 63
UBSAN: Undefined behaviour in ffsll.c:37:34 negation of 9223372036854775808 cannot be represented in type 'long long int'

Since the idea is to isolate the least significant bit, use unsigned
types.

2 months agostring: Fix UB on test-strrchr
Adhemerval Zanella [Fri, 25 Apr 2025 16:04:33 +0000 (13:04 -0300)] 
string: Fix UB on test-strrchr

UBSAN: Undefined behaviour in test-strrchr.c:100:35 unsigned integer overflow: 1804289383 * 846930886 cannot be represened in type 'long int'

Use int64_t operations instead.

2 months agosysvipc: Fix UB on time64 time support
Adhemerval Zanella [Fri, 25 Apr 2025 13:57:32 +0000 (13:57 +0000)] 
sysvipc: Fix UB on time64 time support

Building with ubsan on 32 bit architecture without 64 bit time_t
as default, it shows:

UBSAN: Undefined behaviour in ../sysdeps/unix/sysv/linux/msgctl.c:180:45 left shift of 3935167480 by 32 cannot be represented in type 'long long int'

Add a new macro, IPC_HILO, to handle this transparently by using
unsigned shifts.

2 months agonptl: xfail tst-cleanup2 when building with ubsan
Adhemerval Zanella [Fri, 25 Apr 2025 12:45:11 +0000 (09:45 -0300)] 
nptl: xfail tst-cleanup2 when building with ubsan

The test explicit uses UB (sprintf (NULL, ...) to triggers a SIGSEGV
from libc and check with the pthread cleanup are correctly called.

With ubsan enable, it triggers:

UBSAN: Undefined behaviour in Xprintf_buffer_write.c:39:7 null pointer passed as argument 1, nonnull attribute declared at unknown:0:0

Which is a memcpy call with NULL argument.

2 months agostdio: Fix test-printf-ldbl-compat.c build with ubsan
Adhemerval Zanella [Thu, 24 Apr 2025 20:15:39 +0000 (17:15 -0300)] 
stdio: Fix test-printf-ldbl-compat.c build with ubsan

On powercp64le with --enable-ubsan the build fails with:

In file included from ../include/bits/stdio2.h:1,
                 from ../libio/stdio.h:967,
                 from ../include/stdio.h:14,
                 from ../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-ldbl-compat.c:21,
                 from ../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-ieee128.c:1:
In function ‘vfprintf’,
    inlined from ‘do_test_call_varg’ at ../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-ldbl-compat.c:56:3:
../libio/bits/stdio2.h:166:10: error: null format string [-Werror=format-overflow=]
  166 |   return __vfprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt, __ap);
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘vsnprintf’,
    inlined from ‘do_test_call_varg’ at ../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-ldbl-compat.c:68:3:
../libio/bits/stdio2.h:100:10: error: null format string [-Werror=format-truncation=]
  100 |   return __builtin___vsnprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  101 |                                     __glibc_objsize (__s), __fmt, __ap);
      |                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘fprintf’,
    inlined from ‘do_test_call_rarg.constprop’ at ../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-ldbl-compat.c:104:3:
../libio/bits/stdio2.h:111:10: error: null format string [-Werror=format-overflow=]
  111 |   return __fprintf_chk (__stream, __USE_FORTIFY_LEVEL - 1, __fmt,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  112 |                         __va_arg_pack ());
      |                         ~~~~~~~~~~~~~~~~~
In function ‘printf’,
    inlined from ‘do_test_call_rarg.constprop’ at ../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-ldbl-compat.c:108:3:
../libio/bits/stdio2.h:118:10: error: null format string [-Werror=format-overflow=]
  118 |   return __printf_chk (__USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ());
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In function ‘snprintf’,
    inlined from ‘do_test_call_rarg.constprop’ at ../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-ldbl-compat.c:112:3:
../libio/bits/stdio2.h:68:10: error: null format string [-Werror=format-truncation=]
   68 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   69 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   70 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
cc1: all warnings being treated as errors

2 months agostdio: Fix test-printf-chk-ldbl-compat build with ubsan
Adhemerval Zanella [Thu, 24 Apr 2025 20:14:52 +0000 (17:14 -0300)] 
stdio: Fix test-printf-chk-ldbl-compat build with ubsan

On powercp64le with --enable-ubsan the build fails with:

../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-chk-ldbl-compat.c: In function ‘do_test_call_varg’:
../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-chk-ldbl-compat.c:60:3: error: null format string [-Werror=format-overflow=]
   60 |   __vfprintf_chk (stream, 1, format, args);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-chk-ldbl-compat.c:66:3: error: null format string [-Werror=format-overflow=]
   66 |   __vprintf_chk (1, format, args);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-chk-ldbl-compat.c:72:3: error: null format string [-Werror=format-truncation=]
   72 |   __vsnprintf_chk (string, 79, 1, 127, format, args);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-chk-ldbl-compat.c: In function ‘do_test_call_rarg.constprop’:
../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-chk-ldbl-compat.c:111:3: error: null format string [-Werror=format-overflow=]
  111 |   __fprintf_chk (stdout, 1, format, ld, d);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-chk-ldbl-compat.c:115:3: error: null format string [-Werror=format-overflow=]
  115 |   __printf_chk (1, format, ld, d);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../sysdeps/ieee754/ldbl-128ibm-compat/test-printf-chk-ldbl-compat.c:119:3: error: null format string [-Werror=format-truncation=]
  119 |   __snprintf_chk (string, 79, 1, 127, format, ld, d);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

2 months agostdio-common: Fix UB on stdio-common/vfprintf-process-arg.c
Adhemerval Zanella [Thu, 24 Apr 2025 12:41:11 +0000 (09:41 -0300)] 
stdio-common: Fix UB on stdio-common/vfprintf-process-arg.c

On i686 debug/tst-sprintf-fortify-unchecked triggers:

UBSAN: Undefined behaviour in vfprintf-process-arg.c:41:57 negation of 9223372036854775808 cannot be represented in type 'long long int'

2 months agonss: Fix tst-nss-hash UB
Adhemerval Zanella [Wed, 23 Apr 2025 19:42:28 +0000 (16:42 -0300)] 
nss: Fix tst-nss-hash UB

UBSAN: Undefined behaviour in tst-nss-hash.c:49:8 variable length array bound evaluates to non-positive value 0

2 months agonss: Fix UB on test-netdb
Adhemerval Zanella [Wed, 23 Apr 2025 19:40:57 +0000 (16:40 -0300)] 
nss: Fix UB on test-netdb

The gethostname is defined as not accepting NULL arguments, so
use a minimal size for initial call.

2 months agostdlib: Fix tst-makecontext3 UB
Adhemerval Zanella [Wed, 23 Apr 2025 19:28:24 +0000 (16:28 -0300)] 
stdlib: Fix tst-makecontext3 UB

UBSAN: Undefined behaviour in tst-makecontext3.c:164:40 left shift of 1794341827 by 1 cannot be represented in type 'int'

2 months agolinux: Fix UB on tst-sched-setattr.c
Adhemerval Zanella [Wed, 23 Apr 2025 19:23:36 +0000 (16:23 -0300)] 
linux: Fix UB on tst-sched-setattr.c

UBSAN: Undefined behaviour in ../sysdeps/unix/sysv/linux/tst-sched_setattr.c:86:5 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0

2 months agostring: Fix UB on tesf-ffs
Adhemerval Zanella [Wed, 23 Apr 2025 19:20:28 +0000 (16:20 -0300)] 
string: Fix UB on tesf-ffs

UBSAN: Undefined behaviour in test-ffs.c:48:3 left shift of 1 by 31 cannot be represented in type 'int'

2 months agostdlib: Fix UB in tst-limits.c
Adhemerval Zanella [Wed, 23 Apr 2025 15:37:16 +0000 (12:37 -0300)] 
stdlib: Fix UB in tst-limits.c

UBSAN: Undefined behaviour in tst-limits.c:14:16 left shift of 1 by 63 cannot be represented in type 'long long int'

2 months agostdlib: Adapt tst-environ to ubsan
Adhemerval Zanella [Wed, 23 Apr 2025 14:44:24 +0000 (11:44 -0300)] 
stdlib: Adapt tst-environ to ubsan

The test explicits checks for passing a null argument to a
function with argument nonnull attribute.

2 months agostdlib: Fix UB on strtod FIX
Adhemerval Zanella [Wed, 23 Apr 2025 13:35:49 +0000 (10:35 -0300)] 
stdlib: Fix UB on strtod FIX

2 months agomath: Rewrite ldbl-128 isnan
Adhemerval Zanella [Tue, 22 Apr 2025 20:01:48 +0000 (17:01 -0300)] 
math: Rewrite ldbl-128 isnan

With ubsan, test-float128-cabs triggers:

UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/s_isnanl.c:29:30 negation of 9223372036854775808 cannot be represented in type 'long int'

Rewrite with simplified version.

2 months agomath: Fix UB in float128 atan2
Adhemerval Zanella [Tue, 22 Apr 2025 18:59:02 +0000 (15:59 -0300)] 
math: Fix UB in float128 atan2

UBSAN: Undefined behaviour in ../sysdeps/ieee754/float128/../ldbl-128/e_atan2l.c:68:9 unsigned integer overflow: 9223372036854775808 - 4611404543450677248 cannot be represened in type 'long int'

2 months agomath: Fix UB in test-canonical-ldbl-96.c
Adhemerval Zanella [Tue, 22 Apr 2025 18:57:10 +0000 (15:57 -0300)] 
math: Fix UB in test-canonical-ldbl-96.c

UBSAN: Undefined behaviour in ../sysdeps/ieee754/ldbl-96/test-canonical-ldbl-96.c:97:7 left shift of 1 by 31 cannot be represented in type 'int'

2 months agosunrpc: Fix UB on xdr_hyper
Adhemerval Zanella [Tue, 22 Apr 2025 17:55:38 +0000 (14:55 -0300)] 
sunrpc: Fix UB on xdr_hyper

ubsan triggers:

UBSAN: Undefined behaviour in xdr.c:262:28 left shift of 18446744073709551615 by 32 cannot be represented in type 'long int'

Fix by using unsigned type cast for left shift.

2 months agostring: Fix UB on gneric strncmp
Adhemerval Zanella [Tue, 22 Apr 2025 17:35:39 +0000 (14:35 -0300)] 
string: Fix UB on gneric strncmp

Building with ubsan it triggers:

UBSAN: Undefined behaviour in ../string/strncmp.c:103:12 shift exponent 32 is too large for 32-bit type 'int'

Use an unsigned constant literal for MERGE, since op_t is defined
as unsigned.

2 months agostring: Fix UB on index_first/index_last
Adhemerval Zanella [Tue, 22 Apr 2025 17:34:27 +0000 (14:34 -0300)] 
string: Fix UB on index_first/index_last

Building with ubsan the test-strcnmp triggers:

UBSAN: Undefined behaviour in ../sysdeps/generic/string-fzi.h:39:12 passing zero to __builtin_ctz()

Use stdbit.h functions instead of ctl/clz.

2 months agostring: Fix UB on generic strcmp
Adhemerval Zanella [Tue, 22 Apr 2025 17:30:06 +0000 (14:30 -0300)] 
string: Fix UB on generic strcmp

Building with ubsan it trigger:

UBSAN: Undefined behaviour in ../string/strcmp.c:92:12 shift exponent 32 is too large for 32-bit type 'int'

Use an unsigned constant literal for MERGE, since op_t is defined
as unsigned.

2 months agostring: Fix UB on gneric stpcpy
Adhemerval Zanella [Tue, 22 Apr 2025 17:28:35 +0000 (14:28 -0300)] 
string: Fix UB on gneric stpcpy

Building with ubsan it trigger:

UBSAN: Undefined behaviour in ../string/stpcpy.c:91:12 shift exponent 56 is too large for 32-bit type 'int'

Use an unsigned constant literal for MERGE, since op_t is defined
as unsigned.

2 months agostdlib: Fix UB on strtod
Adhemerval Zanella [Tue, 22 Apr 2025 17:20:12 +0000 (14:20 -0300)] 
stdlib: Fix UB on strtod

With glibc built with ubsan it triggers:

UBSAN: Undefined behaviour in strtod_l.c:1580:8 shift exponent 41 is too large for 32-bit type 'int'

Use the correct constant literal definition based on mp_limb_t size.

2 months agostdlib: Fix UB on erand48/jrand48
Adhemerval Zanella [Tue, 22 Apr 2025 17:06:33 +0000 (14:06 -0300)] 
stdlib: Fix UB on erand48/jrand48

With glibc built with ubsan it triggers:

UBSAN: Undefined behaviour in jrand48_r.c:29:34 left shift of 41612 by 16 cannot be represented in type 'int'
UBSAN: Undefined behaviour in erand48_r.c:39:45 left shift of 3972 by 20 cannot be represented in type 'int'

Fix by casting to uint32_t for the shift operation.

2 months agostdlib: Fix __libc_message_impl iovec size
Adhemerval Zanella [Tue, 22 Apr 2025 17:01:05 +0000 (14:01 -0300)] 
stdlib: Fix __libc_message_impl iovec size

The ubsan triggers an array item access error with stdlib/tst-bz20544.

2 months agolocale: Fix UB on VLA allocation
Adhemerval Zanella [Tue, 22 Apr 2025 13:50:13 +0000 (10:50 -0300)] 
locale: Fix UB on VLA allocation

Both level 2 and level 3 sizes can be zero, which triggers a 0-size
VLA.  Reorganize the code to allocate the VLA iff sizes are positive.

2 months agolibio: Fix UB __libio_codecvt_length
Adhemerval Zanella [Tue, 22 Apr 2025 12:02:54 +0000 (09:02 -0300)] 
libio: Fix UB __libio_codecvt_length

To avoid a 0 size VLA.

2 months agoiconv: Fix UB on iconv/tst-translit-mchar
Adhemerval Zanella [Mon, 21 Apr 2025 20:51:37 +0000 (17:51 -0300)] 
iconv: Fix UB on iconv/tst-translit-mchar

Building with ubsan, the test triggers:

UBSAN: Undefined behaviour in programs/locfile.c:598:3 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0

The obstack_grow is only define for size > 0.

2 months agoiconv: Fix UB on find_derivation
Adhemerval Zanella [Mon, 21 Apr 2025 20:49:31 +0000 (17:49 -0300)] 
iconv: Fix UB on find_derivation

The cost addition might overflow since the default value is
LONG_INT.  Use wrap addition instead.

2 months agomath: Fix UB in setayloadf
Adhemerval Zanella [Mon, 21 Apr 2025 18:15:07 +0000 (15:15 -0300)] 
math: Fix UB in setayloadf

The code can shift the 1U for value larger than 31 depending of
the exponent value.  Add a check prior the shift.

2 months agomath: Fix UB in setayload
Adhemerval Zanella [Mon, 21 Apr 2025 18:12:21 +0000 (15:12 -0300)] 
math: Fix UB in setayload

The code can shift the 1ULL for value larger than 63 depending of
the exponent value.  Add a check prior the shift.

2 months agomath: Remove UB from float128 ilogbf
Adhemerval Zanella [Mon, 21 Apr 2025 17:43:49 +0000 (14:43 -0300)] 
math: Remove UB from float128 ilogbf

The subnormal exponent calculation invokes UB by left shifting the
high or lower work.  Use unsigned values and stdc_leading_zeros
instead.

2 months agopowerpc: Use generic ilogb/ilogbf and refactor ilogbf128
Adhemerval Zanella [Tue, 29 Apr 2025 16:30:04 +0000 (13:30 -0300)] 
powerpc: Use generic ilogb/ilogbf and refactor ilogbf128

The powerpc64 leverages the use of xsxexpdp and xsxexpqp for
for both ilogb/ilogbf for float, double, and float128 types.
However with the new generic ilogb/ilogbf, this is not really
a gain anymore.

On POWER9 with gcc-13, the xsxexpdp/xsxexpqp shows:

$ ./benchtests/bench-ilogb
  "ilogb": {
   "subnormal": {
    "duration": 5.08829e+08,
    "iterations": 4.4588e+07,
    "max": 18.761,
    "min": 6.7005,
    "mean": 11.4118
   },
   "normal": {
    "duration": 5.04674e+08,
    "iterations": 9.9596e+07,
    "max": 7.386,
    "min": 5.0505,
    "mean": 5.06722
   }
$ ./benchtests/bench-ilogbf
  "ilogbf": {
   "subnormal": {
    "duration": 5.04918e+08,
    "iterations": 9.8732e+07,
    "max": 7.1595,
    "min": 5.0475,
    "mean": 5.11402
   },
   "normal": {
    "duration": 5.04971e+08,
    "iterations": 9.8744e+07,
    "max": 7.771,
    "min": 5.048,
    "mean": 5.11394
   }
  }

While the new generic implementation shows:

$ ./benchtests/bench-ilogb
  "ilogb": {
   "subnormal": {
    "duration": 5.05389e+08,
    "iterations": 9.2644e+07,
    "max": 11.0355,
    "min": 5.4255,
    "mean": 5.45517
   },
   "normal": {
    "duration": 5.04667e+08,
    "iterations": 1.02388e+08,
    "max": 9.758,
    "min": 4.8945,
    "mean": 4.92897
   }
  }[azanella@cfarm135 powerpc64le-linux-gnu-power9-gcc13]$ ./benchtests/bench-ilogbf
  "ilogbf": {
   "subnormal": {
    "duration": 5.05409e+08,
    "iterations": 9.238e+07,
    "max": 7.69,
    "min": 5.442,
    "mean": 5.47098
   },
   "normal": {
    "duration": 5.0456e+08,
    "iterations": 1.02012e+08,
    "max": 6.84,
    "min": 4.922,
    "mean": 4.94609
   }
  }

The xsxexpdp/xsxexpqp also adds some extra code size overhead since it
uses the generic ilogb/ilogbf for 0/inf/NaN handling.  It is still kept
for float128, and this patch also optimizes it to avoid need to call
extra generic symbol to handle not number inputs.

On same hardware (POWER9/gcc-13) it shows the improvement:

* master

  "ilogbf128": {
   "subnormal": {
    "duration": 5.09608e+08,
    "iterations": 3.3092e+07,
    "max": 28.845,
    "min": 6.824,
    "mean": 15.3997
   },
   "normal": {
    "duration": 5.05148e+08,
    "iterations": 9.1692e+07,
    "max": 7.744,
    "min": 5.377,
    "mean": 5.50918
   }
  }

* patch:

  "ilogbf128": {
   "subnormal": {
    "duration": 5.0586e+08,
    "iterations": 8.388e+07,
    "max": 7.3295,
    "min": 5.952,
    "mean": 6.03076
   },
   "normal": {
    "duration": 5.04783e+08,
    "iterations": 9.6608e+07,
    "max": 8.9255,
    "min": 5.185,
    "mean": 5.22507
   }
  }

Checked on powerpc64le-linux-gnu and powerpc64le-linux-gnu targetting
POWER8 and with --disable-multi-arch on POWER9.

2 months agomath: Remove i386 ilogb/ilogbf/llogb/llogbf
Adhemerval Zanella [Tue, 29 Apr 2025 16:30:03 +0000 (13:30 -0300)] 
math: Remove i386 ilogb/ilogbf/llogb/llogbf

The new float and double implementation does not required an
extra function call and error handling uses math_err function,
which results in better performance on i386 as well.

With gcc-14 on AMD AMD Ryzen 9 5900X, master shows:

$ ./benchtests/bench-ilogb
  "ilogb": {
   "subnormal": {
    "duration": 3.68863e+09,
    "iterations": 1.72228e+08,
    "max": 89.2995,
    "min": 21.016,
    "mean": 21.4171
   },
   "normal": {
    "duration": 3.68878e+09,
    "iterations": 1.72948e+08,
    "max": 78.6065,
    "min": 21.127,
    "mean": 21.3288
   }
  }
$ ./benchtests/bench-ilogbf
  "ilogbf": {
   "subnormal": {
    "duration": 3.68835e+09,
    "iterations": 1.66716e+08,
    "max": 46.953,
    "min": 21.793,
    "mean": 22.1236
   },
   "normal": {
    "duration": 3.68784e+09,
    "iterations": 1.66168e+08,
    "max": 46.9715,
    "min": 21.904,
    "mean": 22.1935
   }
  }

While with this patch:

$ ./benchtests/bench-ilogb
  "ilogb": {
   "subnormal": {
    "duration": 3.68134e+09,
    "iterations": 4.17516e+08,
    "max": 32.5045,
    "min": 8.3245,
    "mean": 8.81723
   },
   "normal": {
    "duration": 3.6677e+09,
    "iterations": 6.79468e+08,
    "max": 50.9305,
    "min": 5.3465,
    "mean": 5.3979
   }
}
$ ./benchtests/bench-ilogbf
  "ilogbf": {
   "subnormal": {
    "duration": 3.67553e+09,
    "iterations": 5.11032e+08,
    "max": 35.927,
    "min": 7.0485,
    "mean": 7.19237
   },
   "normal": {
    "duration": 3.66877e+09,
    "iterations": 6.556e+08,
    "max": 26.3625,
    "min": 5.5315,
    "mean": 5.59605
   }
 }

Checked on i686-linux-gnu.

2 months agomath: Optimize float ilogb/llogb
Adhemerval Zanella [Tue, 29 Apr 2025 16:30:02 +0000 (13:30 -0300)] 
math: Optimize float ilogb/llogb

It removes the wrapper by moving the error/EDOM handling to an
out-of-line implementation (__math_invalidf_i/__math_invalidf_li).
Also, __glibc_unlikely is used on errors case since it helps
code generation on recent gcc.

The code now builds to with gcc-14 on aarch64:

0000000000000000 <__ilogbf>:
   0:   1e260000        fmov    w0, s0
   4:   d3577801        ubfx    x1, x0, #23, #8
   8:   340000e1        cbz     w1, 24 <__ilogbf+0x24>
   c:   5101fc20        sub     w0, w1, #0x7f
  10:   7103fc3f        cmp     w1, #0xff
  14:   54000040        b.eq    1c <__ilogbf+0x1c>  // b.none
  18:   d65f03c0        ret
  1c:   12b00000        mov     w0, #0x7fffffff                 // #2147483647
  20:   14000000        b       0 <__math_invalidf_i>
  24:   53175800        lsl     w0, w0, #9
  28:   340000a0        cbz     w0, 3c <__ilogbf+0x3c>
  2c:   5ac01000        clz     w0, w0
  30:   12800fc1        mov     w1, #0xffffff81                 // #-127
  34:   4b000020        sub     w0, w1, w0
  38:   d65f03c0        ret
  3c:   320107e0        mov     w0, #0x80000001                 // #-2147483647
  40:   14000000        b       0 <__math_invalidf_i>

Some ABI requires additional adjustments:

  * i386 and m68k requires to use the template version, since
    both provide __ieee754_ilogb implementatations.

  * loongarch uses a custom implementation as well.

  * powerpc64le also has a custom implementation for POWER9, which
    is also used for float and float128 version.  The generic
    e_ilogb.c implementation is moved on powerpc to keep the
    current code as-is.

Checked on aarch64-linux-gnu and x86_64-linux-gnu.

2 months agomath: Remove UB and optimize double ilogbf
Adhemerval Zanella [Tue, 29 Apr 2025 16:30:01 +0000 (13:30 -0300)] 
math: Remove UB and optimize double ilogbf

The subnormal exponent calculation invokes UB by left shifting the
signed expoenent to find the first leading bit.

The patch reimplements ilogb using the math_config.h macros and
uses the new stdbit.h function to simplify the subnormal handling.

On aarch64 it generates better code:

* master:

0000000000000000 <__ieee754_ilogbf>:
   0:   1e260000        fmov    w0, s0
   4:   12007801        and     w1, w0, #0x7fffffff
   8:   72091c1f        tst     w0, #0x7f800000
   c:   54000141        b.ne    34 <__ieee754_ilogbf+0x34>  // b.any
  10:   34000201        cbz     w1, 50 <__ieee754_ilogbf+0x50>
  14:   53185c21        lsl     w1, w1, #8
  18:   12800fa0        mov     w0, #0xffffff82                 // #-126
  1c:   d503201f        nop
  20:   531f7821        lsl     w1, w1, #1
  24:   51000400        sub     w0, w0, #0x1
  28:   7100003f        cmp     w1, #0x0
  2c:   54ffffac        b.gt    20 <__ieee754_ilogbf+0x20>
  30:   d65f03c0        ret
  34:   13177c20        asr     w0, w1, #23
  38:   12b01002        mov     w2, #0x7f7fffff                 // #2139095039
  3c:   5101fc00        sub     w0, w0, #0x7f
  40:   6b02003f        cmp     w1, w2
  44:   12b00001        mov     w1, #0x7fffffff                 // #2147483647
  48:   1a819000        csel    w0, w0, w1, ls  // ls = plast
  4c:   d65f03c0        ret
  50:   320107e0        mov     w0, #0x80000001                 // #-2147483647
  54:   d65f03c0        ret

* patch:

0000000000000000 <__ieee754_ilogbf>:
   0:   1e260001        fmov    w1, s0
   4:   d3577820        ubfx    x0, x1, #23, #8
   8:   350000e0        cbnz    w0, 24 <__ieee754_ilogbf+0x24>
   c:   53175821        lsl     w1, w1, #9
  10:   34000141        cbz     w1, 38 <__ieee754_ilogbf+0x38>
  14:   5ac01021        clz     w1, w1
  18:   12800fc0        mov     w0, #0xffffff81                 // #-127
  1c:   4b010000        sub     w0, w0, w1
  20:   d65f03c0        ret
  24:   7103fc1f        cmp     w0, #0xff
  28:   5101fc00        sub     w0, w0, #0x7f
  2c:   12b00001        mov     w1, #0x7fffffff                 // #2147483647
  30:   1a811000        csel    w0, w0, w1, ne  // ne = any
  34:   d65f03c0        ret
  38:   320107e0        mov     w0, #0x80000001                 // #-2147483647
  3c:   d65f03c0        ret

Other architecture with support for stdc_leading_zeros and/or
__builtin_clzll should have similar improvements.

Checked on aarch64-linux-gnu and x86_64-linux-gnu.

2 months agomath: Optimize double ilogb/llogb
Adhemerval Zanella [Tue, 29 Apr 2025 16:30:00 +0000 (13:30 -0300)] 
math: Optimize double ilogb/llogb

It removes the wrapper by moving the error/EDOM handling to an
out-of-line implementation (__math_invalid_i/__math_invalid_li).
Also, __glibc_unlikely is used on errors case since it helps
code generation on recent gcc.

The code now builds to with gcc-14 on aarch64:

0000000000000000 <__ilogb>:
   0:   9e660000        fmov    x0, d0
   4:   d374f801        ubfx    x1, x0, #52, #11
   8:   340000e1        cbz     w1, 24 <__ilogb+0x24>
   c:   510ffc20        sub     w0, w1, #0x3ff
  10:   711ffc3f        cmp     w1, #0x7ff
  14:   54000040        b.eq    1c <__ilogb+0x1c>  // b.none
  18:   d65f03c0        ret
  1c:   12b00000        mov     w0, #0x7fffffff                 // #2147483647
  20:   14000000        b       0 <__math_invalid_i>
  24:   d374cc00        lsl     x0, x0, #12
  28:   b40000a0        cbz     x0, 3c <__ilogb+0x3c>
  2c:   dac01000        clz     x0, x0
  30:   12807fc1        mov     w1, #0xfffffc01                 // #-1023
  34:   4b000020        sub     w0, w1, w0
  38:   d65f03c0        ret
  3c:   320107e0        mov     w0, #0x80000001                 // #-2147483647
  40:   14000000        b       0 <__math_invalid_i>

Some ABI requires additional adjustments:

  * i386 and m68k requires to use the template version, since
    both provide __ieee754_ilogb implementatations.

  * loongarch uses a custom implementation as well.

  * powerpc64le also has a custom implementation for POWER9, which
    is also used for float and float128 version.  The generic
    e_ilogb.c implementation is moved on powerpc to keep the
    current code as-is.

Checked on aarch64-linux-gnu and x86_64-linux-gnu.

2 months agomath: Remove UB and optimize double ilogb
Adhemerval Zanella [Tue, 29 Apr 2025 16:29:59 +0000 (13:29 -0300)] 
math: Remove UB and optimize double ilogb

The subnormal exponent calculation invokes UB by left shifting the
signed expoenent to find the first leading bit.  The implementation
also uses 32 bits operations, which generates suboptimal code in
64 bits architectures.

The patch reimplements ilogb using the math_config.h macros and
uses the new stdbit function to simplify the subnormal handling.

On aarch64 it generates better code:

* master:

0000000000000000 <__ieee754_ilogb>:
   0:   9e660000        fmov    x0, d0
   4:   d360fc02        lsr     x2, x0, #32
   8:   d360f801        ubfx    x1, x0, #32, #31
   c:   f26c285f        tst     x2, #0x7ff00000
  10:   540001a1        b.ne    44 <__ieee754_ilogb+0x44>  // b.any
  14:   2a000022        orr     w2, w1, w0
  18:   34000322        cbz     w2, 7c <__ieee754_ilogb+0x7c>
  1c:   35000221        cbnz    w1, 60 <__ieee754_ilogb+0x60>
  20:   2a0003e1        mov     w1, w0
  24:   7100001f        cmp     w0, #0x0
  28:   12808240        mov     w0, #0xfffffbed                 // #-1043
  2c:   540000ad        b.le    40 <__ieee754_ilogb+0x40>
  30:   531f7821        lsl     w1, w1, #1
  34:   51000400        sub     w0, w0, #0x1
  38:   7100003f        cmp     w1, #0x0
  3c:   54ffffac        b.gt    30 <__ieee754_ilogb+0x30>
  40:   d65f03c0        ret
  44:   13147c20        asr     w0, w1, #20
  48:   12b00202        mov     w2, #0x7fefffff                 // #2146435071
  4c:   510ffc00        sub     w0, w0, #0x3ff
  50:   6b02003f        cmp     w1, w2
  54:   12b00001        mov     w1, #0x7fffffff                 // #2147483647
  58:   1a819000        csel    w0, w0, w1, ls  // ls = plast
  5c:   d65f03c0        ret
  60:   53155021        lsl     w1, w1, #11
  64:   12807fa0        mov     w0, #0xfffffc02                 // #-1022
  68:   531f7821        lsl     w1, w1, #1
  6c:   51000400        sub     w0, w0, #0x1
  70:   7100003f        cmp     w1, #0x0
  74:   54ffffac        b.gt    68 <__ieee754_ilogb+0x68>
  78:   d65f03c0        ret
  7c:   320107e0        mov     w0, #0x80000001                 // #-2147483647
  80:   d65f03c0        ret

* patch:

0000000000000000 <__ieee754_ilogb>:
   0:   9e660001        fmov    x1, d0
   4:   d374f820        ubfx    x0, x1, #52, #11
   8:   350000e0        cbnz    w0, 24 <__ieee754_ilogb+0x24>
   c:   d374cc21        lsl     x1, x1, #12
  10:   b4000141        cbz     x1, 38 <__ieee754_ilogb+0x38>
  14:   dac01021        clz     x1, x1
  18:   12807fc0        mov     w0, #0xfffffc01                 // #-1023
  1c:   4b010000        sub     w0, w0, w1
  20:   d65f03c0        ret
  24:   711ffc1f        cmp     w0, #0x7ff
  28:   510ffc00        sub     w0, w0, #0x3ff
  2c:   12b00001        mov     w1, #0x7fffffff                 // #2147483647
  30:   1a811000        csel    w0, w0, w1, ne  // ne = any
  34:   d65f03c0        ret
  38:   320107e0        mov     w0, #0x80000001                 // #-2147483647
  3c:   d65f03c0        ret

Other architecture with support for stdc_leading_zeros and/or
__builtin_clzll should have similar improvements.

Checked on aarch64-linux-gnu and x86_64-linux-gnu.

2 months agomath: Fix UB in expm1
Adhemerval Zanella [Fri, 18 Apr 2025 14:47:42 +0000 (11:47 -0300)] 
math: Fix UB in expm1

Building with ubsan triggers:

UBSAN: Undefined behaviour in ../sysdeps/ieee754/dbl-64/s_expm1.c:242:4 left shift of 4294967271 by 20 cannot be represented in type 'int'

Since at the time k is always positive, just cast to uint32_t.

2 months agoaarch64: Fix UB in ifunc resolvers
Adhemerval Zanella [Fri, 18 Apr 2025 13:43:36 +0000 (10:43 -0300)] 
aarch64: Fix UB in ifunc resolvers

When building with ubsan the ifunc resolvers triggers:

UBSAN: Undefined behaviour in ../sysdeps/aarch64/multiarch/memchr.c:34:1 left shift of 255 by 24 cannot be represented in type 'int'

The midr is defined as uint64_t, so use UINT64_C to define the masks
as well.

2 months agomalloc: Fix UB in malloc-debug
Adhemerval Zanella [Fri, 18 Apr 2025 13:08:21 +0000 (10:08 -0300)] 
malloc: Fix UB in malloc-debug

Multiple tests fail when malloc-debug is built with ubsan:

UBSAN: Undefined behaviour in malloc-debug.c:231:24 applying non-zero offset to a NULL pointer

The main issue is it tries to apply DUMPED_MAIN_ARENA_CHUNK or
for mem2chunk for NULL pointers.

2 months agostdio: Fix UB on snprintf
Adhemerval Zanella [Fri, 18 Apr 2025 13:03:56 +0000 (10:03 -0300)] 
stdio: Fix UB on snprintf

The elf/tst-dl-printf-static test when built with ubsan triggers:

UBSAN: Undefined behaviour in vfprintf-process-arg.c:58:36 negation of 9223372036854775808 cannot be represented in type 'long int'

2 months agox86: Fix UB in x86_cpu_present/x86_cpu_active
Adhemerval Zanella [Fri, 18 Apr 2025 12:52:04 +0000 (09:52 -0300)] 
x86: Fix UB in x86_cpu_present/x86_cpu_active

The elf/tst-cpu-features-supports (and other tests that check for
CPU features) triggers the following issue with ubsan:

UBSAN: Undefined behaviour in ../sysdeps/x86/sys/platform/x86.h:59:42 left shift of 1 by 31 cannot be represented in type 'int'

The active_array is unsigned, so use an unsigned constant as well.

2 months agox86_64: Fix UB on plt rewrite
Adhemerval Zanella [Fri, 18 Apr 2025 12:49:59 +0000 (09:49 -0300)] 
x86_64: Fix UB on plt rewrite

The elf/tst-plt-rewrite2 and elf/tst-plt-rewrite2 triggers the
failures with ubsan:

UBSAN: Undefined behaviour in ../sysdeps/x86_64/dl-machine.h:637:39 store to misaligned address 0x00007adb50230021 for type 'uint32_t'

2 months agoelf: Fix UB on _dl_early_allocate
Adhemerval Zanella [Fri, 18 Apr 2025 12:45:40 +0000 (09:45 -0300)] 
elf: Fix UB on _dl_early_allocate

The ubsan triggers on elf/tst-tls-allocation-failure-static-patched:

UBSAN: Undefined behaviour in ../sysdeps/unix/sysv/linux/dl-early_allocate.c:58:16 pointer index expression with base 0x0000555578792000 overflowed  to 0x8000555578792cc0

The function is called with a size larger than PTRDIFF_MAX, and
the addition than overflow.  Fix it by limiting the size up to
PTRDIFF_MAX, like all other malloc functions.

2 months agoelf: Fix UB on _dl_map_object_from_fd
Adhemerval Zanella [Mon, 21 Apr 2025 20:27:54 +0000 (17:27 -0300)] 
elf: Fix UB on _dl_map_object_from_fd

On 32-bit architecture ubsan triggers:

UBSAN: Undefined behaviour in dl-load.c:1345:54 pointer index expression with base 0x00612508 overflowed  to 0xf7c3a508

Use explicit uintptr_t operation instead.

2 months agoargp: Fix shift bug
Adhemerval Zanella [Thu, 17 Apr 2025 20:07:51 +0000 (17:07 -0300)] 
argp: Fix shift bug

From gnulib commits 06094e390b0 and 88033d3779362a.

2 months agolocale: Fix UB on add_locale_uint32_array
Adhemerval Zanella [Fri, 18 Apr 2025 12:29:21 +0000 (09:29 -0300)] 
locale: Fix UB on add_locale_uint32_array

The ubsan triggers:

UBSAN: Undefined behaviour in programs/locfile.c:644:3 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0

The obstack_grow is only required if there is extra elements to be
inserted (n_elems > 0).

2 months agolocale: Fix UB in elem_hash
Adhemerval Zanella [Fri, 18 Apr 2025 12:40:51 +0000 (09:40 -0300)] 
locale: Fix UB in elem_hash

The ubsan triggers:

UBSAN: Undefined behaviour in ./elem-hash.h:27:14 left shift of 360447856 by 3 cannot be represented in type 'int'

Using unsigned shift here zero fill like signed.

2 months agolocalte: Fix UB on collate_finish
Adhemerval Zanella [Thu, 17 Apr 2025 21:08:01 +0000 (18:08 -0300)] 
localte: Fix UB on collate_finish

The ubsan triggers:

UBSAN: Undefined behaviour in programs/ld-collate.c:1557:7 variable length array bound evaluates to non-positive value 0

nrules is guaranteed to be at most sizeof (((struct element_t *)
0)->used_in_level) * 8, so use it instead.

2 months agolocale: Fix UB on insert_weights
Adhemerval Zanella [Fri, 18 Apr 2025 12:27:29 +0000 (09:27 -0300)] 
locale: Fix UB on insert_weights

The ubsan triggers:

UBSAN: Undefined behaviour in programs/ld-collate.c:862:5 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0,

The memcpy is only requires if current 'weights' is nonnull, so
check it before calling it.

2 months agolocate: Fix UB on memcpy call
Adhemerval Zanella [Thu, 17 Apr 2025 21:03:16 +0000 (18:03 -0300)] 
locate: Fix UB on memcpy call

The ubsan triggers:

UBSAN: Undefined behaviour in programs/charmap.c:908:2 null pointer passed as argument 2, nonnull attribute declared at unknown:0:0

This is not an isseu since size is always '0' in this case.

2 months agoelf: Adjust DT_EXTRATAGIDX to avoid undefined shifts
Richard Henderson [Thu, 17 Apr 2025 20:58:35 +0000 (17:58 -0300)] 
elf: Adjust DT_EXTRATAGIDX to avoid undefined shifts

When building with --enable-ubsan, the relocation code triggers:

UBSAN: Undefined behaviour in get-dynamic-info.h:56:30 left shift of 1879047925 by 1 cannot be represented in type 'int'

Originally from
https://sourceware.org/pipermail/libc-alpha/2015-August/063015.html.

2 months agolocale: Fix --enable-ubsan build failure on some ABIs
Adhemerval Zanella [Wed, 30 Apr 2025 17:24:20 +0000 (14:24 -0300)] 
locale: Fix --enable-ubsan build failure on some ABIs

On mips, arc, powerpc, and s390 gcc 14 triggers the warning

In function ‘charmap_new_char’,
    inlined from ‘parse_charmap.isra’ at ../locale/programs/charmap.c:570:6:
../locale/programs/charmap.c:1017:32: error: ‘strncmp’ specified bound [21474836494294967295] exceeds maximum object size 2147483647 [-Werror=stringop-overread]
 1017 |   if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0)
      |                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../locale/programs/charmap.c:1017:32: error: ‘strncmp’ specified bound [21474836494294967295] exceeds maximum object size 2147483647 [-Werror=stringop-overread]
cc1: all warnings being treated as errors

So move the case to an special function and disable the sanitizer.

2 months agoriscv: Fix --enable-ubsan build failure on riscv
Adhemerval Zanella [Wed, 30 Apr 2025 17:25:28 +0000 (14:25 -0300)] 
riscv: Fix --enable-ubsan build failure on riscv

With ubsan enable, libc.so fails to build with:

[...]linkobj/libc_pic.a(setcontext.os): in function `__start_context':
[...]sysdeps/unix/sysv/linux/riscv/setcontext.S:111:(.text+0xc0): relocation
truncated to fit: R_RISCV_JAL against symbol `__GI_exit' defined in .text section
in [...]/linkobj/libc_pic.a(exit.os)

Using 'call' instead of 'j' works regardless whether UBSAN.

2 months agoubsan: Add initial support for -fsanitize=undefined
Adhemerval Zanella [Wed, 16 Apr 2025 12:32:19 +0000 (09:32 -0300)] 
ubsan: Add initial support for -fsanitize=undefined

It is enabled through a new configure flag, --enable-ubsan, and
should be used for debugging and/or testing.  Not all ubsan handlers
are implemented, only those generated/required by glibc libraries,
programs, and tests.  Some extra handlers might be needed in future
C++ tests, and __ubsan_handle_dynamic_type_cache_miss also needs a
proper implementation.

The ubsan handlers are exported from ld.so since they are used on
all libraries and tests.  This might interfere with ubsan from
compiler runtime (when programs are built with libubsan in shared
mode), and this is completely untested and/or not supported at the
moment.

There is no support for the UBSAN_OPTIONS environment variable,
although some options are supported through glibc.ubsan tunables.
Currently, glibc.ubsan.halt_on_errors can be used to avoid
the process halt when any UB handler is issued.

Using -fsanitize=undefined enables some extra compiler checks that
are not easily enabled through the libc-diag.h macro.  For instance
on iconv/iconvconfig.c, gcc 14.2.1 shows:

In file included from ../include/bits/string_fortified.h:1,
                 from ../string/string.h:548,
                 from ../include/string.h:60,
                 from iconvconfig.c:32:
In function ‘strcpy’,
    inlined from ‘write_output’ at iconvconfig.c:1033:7,
    inlined from ‘main’ at iconvconfig.c:340:14:
../string/bits/string_fortified.h:81:10: error: ‘__builtin_memcpy’ offset [0, 7] is out of the bounds [0, 0] [-Werror=array-bounds=]
   81 |   return __builtin___strcpy_chk (__dest, __src, __glibc_objsize (__dest));
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../string/bits/string_fortified.h:81:10: error: ‘__builtin_memcpy’ offset [0, 7] is out of the bounds [0, 0] [-Werror=array-bounds=]
cc1: all warnings being treated as errors

Some extra code adjustments are required to fix such cases.

This preliminary support is still incomplete:

  * Not all targets are supported, nor have I checked the test suitei
    on all successful targets.  Also, I only checked with limited gcc
    versions (only gcc 14.2.1 and for some targets 15.0.0).

    Currently --enable-ubsan builds on Linux for aarch64, arm, hppa,
    i686, powerpc64, microblaze, mips64, loongarch64, sparc, s390x, and
    x86_64.

  * The instrumentation is disabled on rltd.c, although it is enabled
    on other loaders functions.

  * A lot of test cases show failures due to UB.

Also, gcc-14 triggers an ICE building math routines.  gcc-15
works correctly.

2 months agonss: remove undefined behavior and optimize getaddrinfo
Collin Funk [Mon, 5 May 2025 02:31:34 +0000 (19:31 -0700)] 
nss: remove undefined behavior and optimize getaddrinfo

On x86-64 and compiling with -O2 using stdc_leading_zeros compiles to
the bsr instruction.  The fls function removed by this patch is inlined
but still loops while checking each bit individually.

* nss/getaddrinfo.c: Include <stdbit.h>.
(fls): Remove function.  This function contains a left shift of 31 on an
'int' which is undefined.
(rfc3484_sort): Use stdc_leading_zeros instead of fls.

Signed-off-by: Collin Funk <collin.funk1@gmail.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
2 months agopowerpc: Remove POWER7 strncasecmp optimization
Adhemerval Zanella [Fri, 2 May 2025 12:07:36 +0000 (09:07 -0300)] 
powerpc: Remove POWER7 strncasecmp optimization

These routines are not extensively used (gnulib documentation even
recommend use a replacement [1]), and there is already a POWER8
version that uses proper vectorized instructions.

[1] https://www.gnu.org/software/gnulib/manual/gnulib.html#C-strings

Checked with a build for some powerpc variations.
Reviewed-by: Peter Bergner <bergner@linux.ibm.com>
2 months agomanual: add more pthread functions
DJ Delorie [Sat, 3 May 2025 00:51:18 +0000 (20:51 -0400)] 
manual: add more pthread functions

Add stubs and partial docs for many undocumented pthreads functions.
While neither exhaustive nor complete, gives minimal usage docs
for many functions and expands the pthreads chapters, making it
easier to continue improving this section in the future.

Reviewed-by: Collin Funk <collin.funk1@gmail.com>
2 months agoS390: Add new s390 platform z17.
Stefan Liebler [Tue, 29 Apr 2025 11:28:58 +0000 (13:28 +0200)] 
S390: Add new s390 platform z17.

The glibc-hwcaps subdirectories are extended by "z17".  Libraries are loaded if
the z17 facility bits are active:
- Miscellaneous-instruction-extensions facility 4
- Vector-enhancements-facility 3
- Vector-Packed-Decimal-Enhancement Facility 3
- CPU: Concurrent-Functions Facility

tst-glibc-hwcaps.c is extended in order to test z17 via new marker6.
In case of running on a z17 with a kernel not recognizing z17 yet,
AT_PLATFORM will be z900 but vector-bit in AT_HWCAP is set.  This situation
is now recognized and this testcase does not fail.

A fatal glibc error is dumped if glibc was build with architecture
level set for z17, but run on an older machine (See dl-hwcap-check.h).
Note, you might get an SIGILL before this check if you don't use:
configure --with-rtld-early-cflags=-march=<older-machine>

ld.so --list-diagnostics now also dumps information about s390.cpu_features.

Independent from z17, the s390x kernel won't introduce new HWCAP-Bits if there
is no special handling needed in kernel itself.  For z17, we don't have new
HWCAP flags, but have to check the facility bits retrieved by
stfle-instruction.

Instead of storing all the stfle-bits (currently four 64bit values) in the
cpu_features struct, we now only store those bits, which are needed within
glibc itself.  Note that we have this list twice, one with original values and
the other one which can be filtered with GLIBC_TUNABLES=glibc.cpu.hwcaps.
Those new fields are stored in so far reserved space in cpu_features struct.
Thus processes started in between the update of glibc package and we e.g. have
a new ld.so and an old libc.so, won't crash. The glibc internal ifunc-resolvers
would not select the best optimized variant.

The users of stfle-bits are also updated:
- parsing of GLIBC_TUNABLES=glibc.cpu.hwcaps
- glibc internal ifunc-resolvers
- __libc_ifunc_impl_list
- sysconf

2 months agoCorrect test descriptors in libm-test-pown.inc
Joseph Myers [Thu, 1 May 2025 22:28:59 +0000 (22:28 +0000)] 
Correct test descriptors in libm-test-pown.inc

While working on implementing compoundn, I noticed that
libm-test-pown.inc was wrongly using TEST_ff_f and AUTO_TESTS_ff_f
when the actual types involved meant fL_f should be used instead of
ff_f; fix to use the correct descriptor strings for pown.  (These
strings affect how gen-libm-test.py generates a C file in some cases.
The structure type test_fL_f_data for expected results and the use of
RUN_TEST_LOOP_fL_f in the ALL_RM_TEST call were already correct.)

Tested for x86_64.  The generated libm-test-pown.c was actually
unchanged, but the old descriptor strings were still logically
incorrect.

2 months agomalloc: Inline tcache_try_malloc
Wilco Dijkstra [Wed, 16 Apr 2025 12:21:56 +0000 (12:21 +0000)] 
malloc: Inline tcache_try_malloc

Inline tcache_try_malloc into calloc since it is the only caller.  Also fix
usize2tidx and use it in __libc_malloc, __libc_calloc and _mid_memalign.
The result is simpler, cleaner code.

Reviewed-by: DJ Delorie <dj@redhat.com>
3 months agomath: Fix UB on sinpif (BZ 32925)
Adhemerval Zanella [Fri, 25 Apr 2025 20:54:29 +0000 (17:54 -0300)] 
math: Fix UB on sinpif (BZ 32925)

The left shift overflows for 'int', use uint32_t instead.  It syncs
with CORE-MATH commit bbfabd99.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
3 months agomath: Fix UB on erfcf (BZ 32924)
Adhemerval Zanella [Fri, 25 Apr 2025 20:54:28 +0000 (17:54 -0300)] 
math: Fix UB on erfcf (BZ 32924)

The left shift overflows for 'int', use uint64_t instead.  It syncs
with CORE-MATH commit d0a2be200cbc1344d800d9ef0ebee9ad67dd3ad8.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
3 months agomath: Fix UB on cospif (BZ 32923)
Adhemerval Zanella [Fri, 25 Apr 2025 20:54:27 +0000 (17:54 -0300)] 
math: Fix UB on cospif (BZ 32923)

The left shift overflows for 'int', use uint32_t instead.  It syncs
with CORE-MATH commit bbfabd993a71b049c210b0febfd06d18369fadc1.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
3 months agomath: Fix UB on cbrtf (BZ 32922)
Adhemerval Zanella [Fri, 25 Apr 2025 20:54:26 +0000 (17:54 -0300)] 
math: Fix UB on cbrtf (BZ 32922)

The left shift overflows for 'int64_t', use unsigned instead.  It syncs
with CORE-MATH commit f7c7408d1749ec2859ea249495af699359ae559b.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>
3 months agomath: Fix UB on sinhf (BZ 32921)
Adhemerval Zanella [Fri, 25 Apr 2025 20:54:25 +0000 (17:54 -0300)] 
math: Fix UB on sinhf (BZ 32921)

The left shift overflows for 'int', use uint64_t instead.  It syncs
with CORE-MATH commit bbfabd99.

Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu.
Reviewed-by: Carlos O'Donell <carlos@redhat.com>