Commit Graph

4675 Commits

Author SHA1 Message Date
Érico Nogueira b76f37fd56 add qsort_r and make qsort a wrapper around it
we make qsort a wrapper by providing a wrapper_cmp function that uses
the extra argument as a function pointer. should be optimized to a tail
call on most architectures, as long as it's built with
-fomit-frame-pointer, so the performance impact should be minimal.

to keep the git history clean, for now qsort_r is implemented in qsort.c
and qsort is implemented in qsort_nr.c.  qsort.c also received a few
trivial cleanups, including replacing (*cmp)() calls with cmp().
qsort_nr.c contains only wrapper_cmp and qsort as a qsort_r wrapper
itself.
2021-09-23 20:09:22 -04:00
Rich Felker 7be59733d7 add SPE FPU support to powerpc-sf
When the soft-float ABI for PowerPC was added in commit
5a92dd95c7, with Freescale cpus using
the alternative SPE FPU as the main use case, it was noted that we
could probably support hard float on them, but that it would involve
determining some difficult ABI constraints. This commit is the
completion of that work.

The Power-Arch-32 ABI supplement defines the ABI profiles, and indeed
ATR-SPE is built on ATR-SOFT-FLOAT. But setjmp/longjmp compatibility
are problematic for the same reason they're problematic on ARM, where
optional float-related parts of the register file are "call-saved if
present". This requires testing __hwcap, which is now done.

In keeping with the existing powerpc-sf subarch definition, which did
not have fenv, the fenv macros are not defined for SPE and the SPEFSCR
control register is left (and assumed to start in) the default mode.
2021-09-23 19:11:46 -04:00
Rich Felker e3e7189c11 fix undefined behavior in getdelim via null pointer arithmetic and memcpy
both passing a null pointer to memcpy with length 0, and adding 0 to a
null pointer, are undefined. in some sense this is 'benign' UB, but
having it precludes use of tooling that strictly traps on UB. there
may be better ways to fix it, but conditioning the operations which
are intended to be no-ops in the k==0 case on k being nonzero is a
simple and safe solution.
2021-09-11 21:21:43 -04:00
Rich Felker b713b8b2e4 fix excessively slow TLS performance on some mips models
commit 6d99ad91e8 introduced this
regression as part of a larger change, based on an incorrect
assumption that rdhwr being part of the mips r2 ISA level meant that
the TLS register, known in the mips documentation as UserLocal, was
unconditionally present on chips providing this ISA level and would
not need trap-and-emulate. this turns out to be false.

based on research by Stanislav Kljuhhin and Abilio Marques, who
reported the problem as a performance regression on certain routers
using OpenWRT vs older uclibc-based versions, it turns out the mips
manuals document the UserLocal register as a feature that might or
might not be implemented or enabled, reflected by a cpu capability bit
in the CONFIG3 register, and that Linux checks for this and has to
explicitly enable it on models that have it.

thus, it's indeed possible that r2+ chips can lack the feature,
bringing us back to the situation where Linux only has a fast
trap-and-emulate path for the case where the destination register is
$3. so, always read the thread pointer through $3. this may incur a
gratuitous move to the desired final register on chips where it's not
needed, but it really doesn't matter.
2021-08-12 18:07:44 -04:00
Érico Nogueira 3eed6a6f0a fix error checking in pthread_getname_np
len is unsigned and can never be smaller than 0. though unlikely, an
error in read() would have lead to an out of bounds write to name.

Reported-by: Michael Forney <mforney@mforney.org>
2021-08-06 11:26:15 -04:00
Rich Felker 3f701faace fix libc-internal signal blocking on mips archs
due to historical reasons, the mips signal set has 128 bits rather
than 64 like on every other arch. this was special-cased correctly, at
least for 32-bit mips, at one time, but was inadvertently broken in
commit 7c440977db, and seems never to
have been right on mips64/n32.

as consequenct of this bug, applications making use of high realtime
signal numbers on mips may have been able to execute application code
in contexts where doing so was unsafe.
2021-07-29 23:24:58 -04:00
Rich Felker 0fbd7d6683 fix broken struct shmid_ds on powerpc (32-bit)
the kernel structure has padding of the shm_segsz member up to 64
bits, as well as 2 unused longs at the end. somehow that was
overlooked when the powerpc port was added, and it has been broken
ever since; applications compiled with the wrong definition do not
correctly see the shm_segsz, shm_cpid, and shm_lpid members.

fixing the definition just by adding the missing padding would break
the ABI size of the structure as well as the position of the time64
shm_atime and shm_dtime members we added at the end. instead, just
move one of the unused padding members from the original end (before
time64) of the structure to the position of the missing padding. this
preserves size and preserves correct behavior of any compiled code
that was already working. programs affected by the wrong definition
need to be recompiled with the correct one.
2021-07-06 21:12:02 -04:00
Szabolcs Nagy 4f3d346bff math: fix fmaf not to depend on FE_TOWARDZERO 2021-07-06 00:29:57 -04:00
Rich Felker 937822abb6 fix TZ parsing logic for identifying POSIX-form strings
previously, the contents of the TZ variable were considered a
candidate for a file/path name only if they began with a colon or
contained a slash before any comma. the latter was very sloppy logic
to avoid treating any valid POSIX TZ string as a file name, but it
also triggered on values that are not valid POSIX TZ strings,
including 3-letter timezone names without any offset.

instead, only treat the TZ variable as POSIX form if it begins with a
nonzero standard time name followed by +, -, or a digit.

also, special case GMT and UTC to always be treated as POSIX form
(with implicit zero offset) so that a stray file by the same name
cannot break software that depends on setting TZ=GMT or TZ=UTC.
2021-06-23 17:30:10 -04:00
Khem Raj 1f0c7cb1cc riscv: rename __NR_fstatat __NR_newfstatat
on riscv64 this syscall is called __NR_newfstatat
this helps the name match kernel UAPI for external
programs
2021-06-05 11:47:16 -04:00
Michael Forney d8cb888db9 remove return with expression in void function 2021-04-27 19:31:48 -04:00
Érico Nogueira b7a130e0b9 remove unnecessary cast for map_library return
the function already returns (void *)
2021-04-20 15:40:27 -04:00
Érico Rolim bd3b9c4ca5 add pthread_getname_np function
based on the pthread_setname_np implementation
2021-04-20 15:34:30 -04:00
Rich Felker e1a51185ce fix popen not to leak pipes from one child to another
POSIX places an obscure requirement on popen which is like a limited
version of close-on-exec:

    "The popen() function shall ensure that any streams from previous
    popen() calls that remain open in the parent process are closed in
    the new child process."

if the POSIX-future 'e' mode flag is passed, producing a pipe FILE
with FD_CLOEXEC on the underlying pipe, this requirement is
automatically satisfied. however, for applications which use multiple
concurrent popen pipes but don't request close-on-exec, fd leaks from
earlier popen calls to later ones could produce deadlock situations
where processes are waiting for a pipe EOF that will never happen.

to fix this, iterate through all open FILEs and add close actions for
those obtained from popen. this requires holding a lock on the open
file list across the posix_spawn call so that additional popen FILEs
are not created after the list is traversed. note that it's still
possible for another popen call to start and create its pipe while the
lock is held, but such pipes are created with O_CLOEXEC and only drop
close-on-exec status (when 'e' flag is omitted) under control of the
lock.
2021-04-20 14:55:10 -04:00
Rich Felker e74acd59a5 remove spurious lock in popen
the newly allocated FILE * has not yet leaked to the application and
is only visible to stdio internals until popen returns. since we do
not change any fields of the structure observed by libc internals,
only the pipe_pid member, locking is not necessary.
2021-04-20 14:52:08 -04:00
Érico Nogueira 9a40e842df define __STDC_UTF_{16,32}__ macros
these macros are used to indicate that the implementation uses,
respectively, utf-16 and utf-32 encoding for char16_t and char32_t.
2021-04-19 09:49:20 -04:00
Rich Felker aad50fcd79 fix regression in dl_iterate_phdr reporting of modules with no TLS
__tls_get_addr should not be called with an invalid TLS module id of
0. in practice it probably "works", returning the DTV length as if it
were a pointer, and the callback should probably not inspect
dlpi_tls_data in this case, but it's likely that some real-world
callbacks use a check on dlpi_tls_data being non-null, rather than on
dlpi_tls_modid being nonzero, to conclude that the module has TLS.
2021-04-16 10:20:46 -04:00
Joakim Sindholt 0ea78a6421 nscd: fall back gracefully on kernels without AF_UNIX support 2021-04-16 10:17:42 -04:00
Dominic Chen 95a540e176 mallocng/aligned_alloc: check for malloc failure
With mallocng, calling posix_memalign() or aligned_alloc() will
SIGSEGV if the internal malloc() call returns NULL. This does not
occur with oldmalloc, which explicitly checks for allocation failure.
2021-04-16 10:17:25 -04:00
Rich Felker 2c00f95c1a make epoll_[p]wait a cancellation point
this is a Linux-specific function and not covered by POSIX's
requirements for which interfaces are cancellation points, but glibc
makes it one and existing software relies on it being one.

at some point a review for similar functions that should be made
cancellation points should be done.
2021-04-03 21:16:41 -04:00
Rich Felker 521b4d27a0 fix dl_iterate_phdr dlpi_tls_data reporting to match spec
dl_iterate_phdr was wrongly reporting the address of the DSO's PT_TLS
image rather than the calling thread's instance of the TLS. the man
page, which is essentially normative for a nonstandard function of
this sort, clearly specifies the latter. it does not clarify where
exactly within/relative-to the image the pointer should point, but the
reasonable thing to do is match the ABI's DTP offset, and this seems
to be what other implementations do.
2021-03-26 13:35:41 -04:00
Rich Felker 122002f0dd remove no-longer-needed special case handling in popen
popen was special-casing the possibility (only possible when the
parent closed stdin and/or stdout) that the child's end of the pipe
was already on the final desired fd number, in which case there was no
way to get rid of its close-on-exec flag in the child. commit
6fc6ca1a32 made this unnecessary by
implementing the POSIX-future requirement that dup2 file actions with
equal source and destination fd values remove the close-on-exec flag.
2021-03-15 10:29:20 -04:00
Rich Felker 8ef9d46f4d use internal malloc for posix_spawn file actions objects
this makes it possible to perform actions on file actions objects with
a libc-internal lock held without creating lock order relationships
that are silently imposed on an application-provided malloc.
2021-03-15 10:21:29 -04:00
Rich Felker cfdfd5ea3c don't fail to map library/executable with zero-length segment maps
reportedly the GNU linker can emit such segments, causing spurious
failure to load due to mmap with a length of zero producing EINVAL.
no action is required for such a load map (it's effectively a nop in
the program headers table) so just treat it as always successful.
2021-03-05 11:13:02 -05:00
Érico Rolim e48e99c112 suppress isascii() macro for C++
analogous to commit a60457c84a.
2021-02-25 16:35:54 -05:00
Rich Felker b129cd8690 guard against compilers failing to handle setjmp specially by default
since 4.1, gcc has had the __returns_twice__ attribute and has
required functions which return twice to carry it; however it's always
applied it automatically to known setjmp-like function names. clang
however does not do this reliably, at least not with -ffreestanding
and possibly under other conditions, resulting in silent emission of
wrong code.

since the symbol name setjmp is in no way special (setjmp is specified
as a macro that could expand to use any implementation-specific symbol
name or names), a compiler is justified not to do anything special
without further hints, and it's reasonable to do what we can to
provide such hints.

gcc 4.0.x and earlier do not recognize the attribute, so make use
conditional on __GNUC__ macros. clang and other gcc-like compilers
report (and have always reported) a later "GNUC" version so the
preprocessor conditional should function as desired for them as too.

undefine the internal macro after use so that nothing abuses it as a
public feature.
2021-02-22 15:52:21 -05:00
Szabolcs Nagy 3309e2d7a1 aarch64/bits/mman.h: add PROT_MTE from linux v5.10
see

  linux commit 9f3419315f3cdc41a7318e4d50ba18a592b30c8c
  arm64: mte: Add PROT_MTE support to mmap() and mprotect()
2021-02-15 09:16:06 -05:00
Szabolcs Nagy 44331150c1 aarch64/bits/hwcap.h: add HWCAP2_MTE from linux v5.10
see

  linux commit 3b714d24ef173f81c78af16f73dcc9b40428c803
  arm64: mte: CPU feature detection and initial sysreg configuration
2021-02-15 09:16:06 -05:00
Szabolcs Nagy 42aa19a0fe add aarch64/bits/mman.h with PROT_BTI from linux v5.8
this was missing, see

  linux commit 8ef8f360cf30be12382f89ff48a57fbbd9b31c14
  arm64: Basic Branch Target Identification support
2021-02-15 09:16:06 -05:00
Szabolcs Nagy b7554b5e12 aarch64/bits/hwcap.h: add HWCAP2_BTI from linux v5.8
hwcap for BTI was missing, see

  linux commit 8ef8f360cf30be12382f89ff48a57fbbd9b31c14
  arm64: Basic Branch Target Identification support
2021-02-15 09:16:06 -05:00
Szabolcs Nagy 87b8f14811 signal.h: add MTE specific SIGSEGV codes from linux v5.10
add synchronouse and asynchronous tag check failure codes, see

  linux commit 74f1082487feb90bbf880af14beb8e29c3030c9f
  arm64: mte: Add specific SIGSEGV codes
2021-02-15 09:16:06 -05:00
Szabolcs Nagy 19239cde94 sys/prctl.h: add MTE related constants from linux v5.10
these are for the aarch64 MTE (memory tagging extension), see

  linux commit 1c101da8b971a36695319dce7a24711dc567a0dd
  arm64: mte: Allow user control of the tag check mode via prctl()

  linux commit af5ce95282dc99d08a27a407a02c763dde1c5558
  arm64: mte: Allow user control of the generated random tags via prctl()
2021-02-15 09:16:06 -05:00
Szabolcs Nagy 8b29f02370 elf.h: add NT_ARM_TAGGED_ADDR_CTRL from linux v5.10
see

  linux commit 2200aa7154cb7ef76bac93e98326883ba64bfa2e
  arm64: mte: ptrace: Add NT_ARM_TAGGED_ADDR_CTRL regset
2021-02-15 09:16:06 -05:00
Szabolcs Nagy d7210f0c12 sys/mman.h: add MAP_HUGE_16KB from linux v5.10
see

  linux commit e47168f3d1b14af5281cf50c59561d59d28201f9
  powerpc/8xx: Support 16k hugepages with 4k pages
2021-02-15 09:16:06 -05:00
Szabolcs Nagy a7456524d7 sys/mount.h: add MS_NOSYMFOLLOW from linux v5.10
path resolution does not follow symlinks on nosymfollow mounts (but
readlink still does), see

  linux commit dab741e0e02bd3c4f5e2e97be74b39df2523fc6e
  Add a "nosymfollow" mount option.
2021-02-15 09:16:06 -05:00
Szabolcs Nagy 54ca1cc7f1 sys/membarrier.h: add new constants from linux v5.10
can cause rseq restart on another cpu to synchronize with global
memory access from rseq critical sections, see

  linux commit 2a36ab717e8fe678d98f81c14a0b124712719840
  rseq/membarrier: Add MEMBARRIER_CMD_PRIVATE_EXPEDITED_RSEQ
2021-02-15 09:16:06 -05:00
Szabolcs Nagy fd285f9dec bits/syscall.h: add process_madvise from linux v5.10
mainly added to linux to allow a central process management service in
android to give MADV_COLD|PAGEOUT hints for other processes, see

  linux commit ecb8ac8b1f146915aa6b96449b66dd48984caacc
  mm/madvise: introduce process_madvise() syscall: an external memory
  hinting API
2021-02-15 09:15:54 -05:00
Rich Felker 49b6df3d9f fix error return value for cuserid
the historical function was specified to return an empty string in the
caller-provided buffer, not a null pointer, to indicate error when the
argument is non-null. only when the argument is null should it return
a null pointer on error.
2021-02-13 14:03:23 -05:00
Rich Felker cc577d0e05 fix misuse of getpwuid_r in cuserid
getpwuid_r can return 0 but without a result in the case where there
was no error but no record exists. in that case cuserid was treating
it as success and copying junk out of pw.pw_name to the output buffer.
2021-02-13 13:59:44 -05:00
Rich Felker a75283d777 cuserid: don't return truncated results
checking the length also drops the need to pull in snprintf.
2021-02-13 13:59:09 -05:00
Sören Tempel ef137da642 cuserid: support invocation with a null pointer argument
this function was removed from the standard in 2001 but appeared in
SUSv2 with an obligation to support calls with a null pointer
argument, using a static buffer.
2021-02-13 13:40:22 -05:00
Khem Raj e5d2823631 riscv64: define ELF_NFPREG
ELF_NFPREG is used by some userspace applications like gdb
2021-02-12 22:16:38 -05:00
Szabolcs Nagy 964104f9f0 math: fix expm1f overflow threshold
the threshold was wrong so expm1f overflowed to inf a bit too early
and on most targets uint32_t compare is faster than float compare so
use that.

this also fixes sinhf incorrectly returning nan for some values where
the internal expm1f overflowed.
2021-02-10 14:06:50 -05:00
Szabolcs Nagy c4c38e6364 math: fix acoshf for negative inputs
on some negative inputs (e.g. -0x1.1e6ae8p+5) acoshf failed to return
nan. ensure that negative inputs result nan without introducing new
branches. this was tried before in

  commit 101e601285
  math: fix acoshf on negative values

but that fix was wrong. there are 3 formulas used:

  log1p(x-1 + sqrt((x-1)*(x-1)+2*(x-1)))
  log(2*x - 1/(x+sqrt(x*x-1)))
  log(x) + 0.693147180559945309417232121458176568

the first fails on large negative inputs (may compute log1p(0) or
log1p(inf)), the second one fails on some mid range or large negative
inputs (may compute log(large) or log(inf)) and the last one fails on
-0 (returns -inf).
2021-02-10 14:06:36 -05:00
Érico Rolim 074932c84d fix possible fd leak via missing O_CLOEXEC in pthread_setname_np
the omission of the flag here seems to have been an oversight when the
function was added in 8fb28b0b3e
2021-01-30 17:29:55 -05:00
Rich Felker 9b77aaca86 oldmalloc: preserve errno across free
as an outcome of Austin Group issue #385, future versions of the
standard will require free not to alter the value of errno. save and
restore it individually around the calls to madvise and munmap so that
the cost is not imposed on calls to free that do not result in any
syscall.
2021-01-30 17:28:08 -05:00
Rich Felker 98b9df994c fix build regression in oldmalloc
commit 8d37958d58 inadvertently broke
oldmalloc by having it implement __libc_malloc rather than
__libc_malloc_impl.
2021-01-30 17:26:34 -05:00
Rich Felker 2010df0d64 preserve errno across free
as an outcome of Austin Group issue #385, future versions of the
standard will require free not to alter the value of errno. save and
restore it individually around the calls to madvise and munmap so that
the cost is not imposed on calls to free that do not result in any
syscall.
2021-01-30 17:14:20 -05:00
Rich Felker 9afed99c22 fix inconsistent signature of __libc_start_main
commit 7586360bad removed the unused
arguments from the definition of __libc_start_main, making it
incompatible with the declaration at the point of call, which still
passed 6 arguments. calls with mismatched function type have undefined
behavior, breaking LTO and any other tooling that checks for function
signature mismatch.

removing the extra arguments from the point of call (crt1) is not an
option for fixing this, since that would be a change in ABI surface
between application and libc.

adding back the extra arguments requires some care. on archs that pass
arguments on the stack or that reserve argument spill space for the
callee on the stack, it imposes an ABI requirement on the caller to
provide such space. the modern crt1.c entry point provides such space,
but originally there was arch-specific asm for the call to
__libc_start_main. the last of this asm was removed in commit
6fef8cafbd, and manual review of the
code removed and its prior history was performed to check that all
archs/variants passed the legacy init/fini/ldso_fini arguments.
2021-01-30 16:42:26 -05:00
Rich Felker dd5b638471 fail posix_spawn file_actions operations with negative fds
these functions are specified to fail with EBADF on negative fd
arguments. apart from close, they are also specified to fail if the
value exceeds OPEN_MAX, but as written it is not clear that this
imposes any requirement when OPEN_MAX is not defined, and it's
undesirable to impose a dynamic limit (via setrlimit) here since the
limit at the time of posix_spawn may be different from the limit at
the time of setting up the file actions. this may require revisiting
later.
2021-01-30 16:09:22 -05:00