oweals/musl.git
4 years agorelease 1.1.24 v1.1.24
Rich Felker [Sun, 13 Oct 2019 21:58:27 +0000 (17:58 -0400)]
release 1.1.24

4 years agomath: fix signed int left shift ub in sqrt
Szabolcs Nagy [Sun, 13 Oct 2019 14:54:31 +0000 (14:54 +0000)]
math: fix signed int left shift ub in sqrt

Both sqrt and sqrtf shifted the signed exponent as signed int to adjust
the bit representation of the result. There are signed right shifts too
in the code but those are implementation defined and are expected to
compile to arithmetic shift on supported compilers and targets.

4 years agofix aliasing-based undefined behavior in mbsrtowcs
Rich Felker [Sun, 13 Oct 2019 21:21:36 +0000 (17:21 -0400)]
fix aliasing-based undefined behavior in mbsrtowcs

mbsrtowcs contains "vectorized" loops to quickly step over bytes
without the high bit set; these have undefined behavior by virtue of
aliasing uint32_t over top of char data for the accesses.

commit 4d0a82170a25464c39522d7190b9fe302045ddb2 fixed the
corresponding usage in string functions by using the may_alias
attribute conditional on __GNUC__ and disabled the vectorized code in
its absence. do the same for mbsrtowcs.

4 years agoadd Arm to the copyright file
Szabolcs Nagy [Wed, 2 Oct 2019 22:07:49 +0000 (22:07 +0000)]
add Arm to the copyright file

Several math functions are now from the ARM optimized-routines repo
licensed under standard MIT terms and copyrighted by Arm Limited,
so mention this in the COPYRIGHT too.

4 years agoreintroduce riscv64 struct sigcontext
Rich Felker [Wed, 2 Oct 2019 13:28:03 +0000 (09:28 -0400)]
reintroduce riscv64 struct sigcontext

commit ab3eb89a8b83353cdaab12ed017a67a7730f90e9 removed it as part of
correcting the mcontext_t definition, but there is still code using
struct sigcontext and expecting the member names present in it, most
notably libgcc_eh. almost all such usage is incorrect, but bring back
struct sigcontext at least for now so as not to introduce regressions.

4 years agofix riscv64 elf_fpregset_t type and member names mismatch
Rich Felker [Mon, 30 Sep 2019 03:45:47 +0000 (23:45 -0400)]
fix riscv64 elf_fpregset_t type and member names mismatch

in order for sys/procfs.h (provided by sys/user.h) to be useful, it
needs to match the API its consumers (gdb, etc.) expect, including the
member names established by glibc.

this partly reverts commit 29e8737f81ccc9fbadcf61a75318aa3d0516aafa,
which partly reverted d493206de7df4db07ad34f24701539ba0a6ed38c,
eliminating struct user_fpregs_struct which seems to have had no
precedent and using union __riscv_mc_fp_state for elf_fpregset_t. this
requires indirect inclusion of signal.h to make union
__riscv_mc_fp_state visible, but being that these are nonstandard
"junk" headers with no official restrictions on what they can pull in,
that's no big deal.

split off and expanded from patch by Khem Raj.

4 years agofix riscv64 signal.h namespace violations and ucontext API mismatches
Rich Felker [Sun, 29 Sep 2019 22:20:40 +0000 (18:20 -0400)]
fix riscv64 signal.h namespace violations and ucontext API mismatches

the top-level mcontext_t member names were namespace-violating in
standards profiles before, and nested-level member names (some of them
single-letter) were egregiously bad namespace impositions even in
non-strict profiles. moreover, they mismatched those used in the
public API first defined in glibc, breaking any code making use of
them.

unlike most archs, the public API used in glibc for riscv mcontext_t
members was designed to be namespace-safe, so we can and should expose
the members regardless of feature test macros. only the typedefs for
greg_t, gregset_t, and fpregset_t need to be protected behind FTMs.

the struct tags for mcontext_t and ucontext_t are also changed. for
mcontext_t this is necessary to make the common definition across
profiles namespace-safe. for ucontext_t, it's just a matter of
matching the tag from the glibc-defined API.

these changes are split off and expanded from a patch by Khem Raj.

4 years agoremove remaining traces of __tls_get_new
Szabolcs Nagy [Sun, 29 Sep 2019 12:25:39 +0000 (12:25 +0000)]
remove remaining traces of __tls_get_new

Some declarations of __tls_get_new were left in the code, even
though the definition got removed in

  commit 9d44b6460ab603487dab4d916342d9ba4467e6b9
  install dynamic tls synchronously at dlopen, streamline access

this can make the build fail with

  ld: lib/libc.so: hidden symbol `__tls_get_new' isn't defined

when libc.so is linked without --gc-sections, because a .hidden
declaration in asm code creates a reference even if the symbol
is not actually used.

4 years agomath: optimize lrint on 32bit targets
Szabolcs Nagy [Mon, 16 Sep 2019 20:33:11 +0000 (20:33 +0000)]
math: optimize lrint on 32bit targets

lrint in (LONG_MAX, 1/DBL_EPSILON) and in (-1/DBL_EPSILON, LONG_MIN)
is not trivial: rounding to int may be inexact, but the conversion to
int may overflow and then the inexact flag must not be raised. (the
overflow threshold is rounding mode dependent).

this matters on 32bit targets (without single instruction lrint or
rint), so the common case (when there is no overflow) is optimized by
inlining the lrint logic, otherwise the old code is kept as a fallback.

on my laptop an i486 lrint call is asm:10ns, old c:30ns, new c:21ns
on a smaller arm core: old c:71ns, new c:34ns
on a bigger arm core: old c:27ns, new c:19ns

4 years agoclean up mips (32-bit, o32) syscall asm constraints
Rich Felker [Fri, 27 Sep 2019 13:57:54 +0000 (09:57 -0400)]
clean up mips (32-bit, o32) syscall asm constraints

analogous to commit ddc7c4f936c7a90781072f10dbaa122007e939d0 for
mips64 and n32, remove the hack to load the syscall number into $2 via
asm, and use a constraint to let the compiler load it instead.

now, only $4, $5, and $6 are potential input-only registers. $2 is
always input and output, and $7 is both when it's an argument,
otherwise output-only. previously, $7 was treated as an input (with a
"1" constraint matching its output position) even when it was not an
input, which was arguably undefined behavior (asm input from
indeterminate value). this is corrected.

as before, $8, $9, and $10 are conditionally input-output registers
for 5-, 6-, and 7-argument syscalls. their role in input is carrying
in the values that will be stored on the stack for arguments 5-7.
their role in output is carrying back whatever the kernel has
clobbered them with, so that the compiler cannot assume they still
contain the input values.

4 years agodocument mips r6 in INSTALL file
Rich Felker [Fri, 27 Sep 2019 04:22:48 +0000 (00:22 -0400)]
document mips r6 in INSTALL file

4 years agofix mips setjmp/longjmp fpu state on r6, related issues
Rich Felker [Fri, 27 Sep 2019 03:46:09 +0000 (23:46 -0400)]
fix mips setjmp/longjmp fpu state on r6, related issues

mips32 has two fpu register file variants: FR=0 with 32 32-bit
registers, where pairs of neighboring even/odd registers are used to
represent doubles, and FR=1 with 32 64-bit registers, each of which
can store a single or double.

up through r5 (our "mips" arch), the supported ABI uses FR=0, but
modern compilers generate "fpxx" model code that can safely operate
with either model. r6, which is an incompatible but similar ISA, drops
FR=0 and only provides the FR=1 model. as such, setjmp and longjmp,
which depended on being able to save and restore call-saved doubles by
storing and loading their 32-bit halves, were completely broken in the
presence of floating point code on mips r6.

to fix this, use the s.d and l.d mnemonics to store and load fpu
registers. these expand to the existing swc1 and lwc1 instructions for
pairs of 32-bit fpu registers on mips1, but on mips2 and later they
translate directly to the 64-bit sdc1 and ldc1.

with FR=0, sdc1 and ldc1 behave just like the pairs of swc1 and lwc1
instructions they replace, storing or loading the even/odd pair of fpu
registers that can be treated as separate single-precision floats or
as a unit representing a double. but with FR=1, they store/load
individual 64-bit registers. this yields the ABI-correct behavior on
mips r6, and should make linking of pre-r6 (plain "mips") code with
"fp64" model code workable, although this is and will likely remain
unsupported usage.

in addition to the mips r6 problem this change fixes, reportedly
clang's internal assembler refuses to assemble swc1 and lwc1
instructions for odd register indices when building for "fpxx" model
(the default). this caused setjmp and longjmp not to build. by using
the s.d and l.d forms, this problem is avoided too.

as a bonus, code size is reduced everywhere but mips1.

4 years agofix mips r6 syscall clobber lists not to include hi/lo registers
Rich Felker [Thu, 26 Sep 2019 23:14:36 +0000 (19:14 -0400)]
fix mips r6 syscall clobber lists not to include hi/lo registers

mips r6 (an incompatible isa from traditional mips) removes the hi and
lo registers used for mul/div results. older gcc versions accepted
them in the clobber list for asm, but their presence is incorrect and
breaks on later versions.

in the process of fixing this, the clobber list for 32-bit mips
syscalls has been deduplicated via a macro like on mips64 and n32.

4 years agoarm: fix setjmp and longjmp asm for armv8-a
Szabolcs Nagy [Wed, 25 Sep 2019 18:34:25 +0000 (19:34 +0100)]
arm: fix setjmp and longjmp asm for armv8-a

armv8 removed the coprocessor instructions other than cp14, so
on an armv8 system the related hwcaps should never be set.

new llvm complains about the use of coprocessor instructions in
armv8-a mode (even though they are never executed at runtime),
so ifdef them out when musl is built for armv8.

4 years agofix data race in timer_create with SIGEV_THREAD notification
Rich Felker [Thu, 26 Sep 2019 01:49:53 +0000 (21:49 -0400)]
fix data race in timer_create with SIGEV_THREAD notification

in the timer thread start function, self->timer_id was accessed
without synchronization; the timer thread could fail to see the store
from the calling thread, resulting in timer_delete failing to delete
the correct kernel-level timer.

this fix is based on a patch by changdiankang, but with the load moved
to after receiving the timer_delete signal rather than just after the
start barrier, so as not to retain the possibility of data race with
timer_delete.

4 years agocorrect the operand specifiers in the riscv64 CAS routines
Palmer Dabbelt [Wed, 25 Sep 2019 03:30:15 +0000 (20:30 -0700)]
correct the operand specifiers in the riscv64 CAS routines

The operand sepcifiers in a_cas and a_cas_p for riscv64 were incorrect:
there's a backwards branch in the routine, so despite tmp being written
at the end of the assembly fragment it cannot be allocated in one of the
input registers because the input values may be needed for another trip
around the loop.

For code that follows the guaranteed forward progress requirements, the
backwards branch is rarely taken: SiFive's hardware only fails a store
conditional on execptional cases (ie, instruction cache misses inside
the loop), and until recently a bug in QEMU allowed back-to-back
store conditionals to succeed.  The bug has been fixed in the latest
QEMU release, but it turns out that the fix caused this latent bug in
musl to manifest.

4 years agoharden thread start with failed scheduling against broken __clone
Rich Felker [Fri, 13 Sep 2019 18:17:36 +0000 (14:17 -0400)]
harden thread start with failed scheduling against broken __clone

commit 8a544ee3a2a75af278145b09531177cab4939b41 introduced a
dependency of the failure path for explicit scheduling at thread
creation on __clone's handling of the start function returning, which
should result in SYS_exit.

as noted in commit 05870abeaac0588fb9115cfd11f96880a0af2108, the arm
version of __clone was broken in this case. in the past, the mips
version was also broken; it was fixed in commit
8b2b61e0001281be0dcd3dedc899bf187172fecb.

since this code path is pretty much entirely untested (previously only
reachable in applications that call the public clone() and return from
the start function) and consists of fragile per-arch asm, don't assume
it works, at least not until it's been thoroughly tested. instead make
the SYS_exit syscall from the start function's failure path.

4 years agofix %lf in wprintf
Brion Vibber [Thu, 12 Sep 2019 05:43:34 +0000 (22:43 -0700)]
fix %lf in wprintf

commit cc3a4466605fe8dfc31f3b75779110ac93055bc1 fixed this for printf
but neglected to fix wprintf.

Previously, %lf caused a failure to output.

4 years agofix arm __tlsdesc_dynamic when built as thumb code without __ARM_ARCH>=5
Rich Felker [Wed, 11 Sep 2019 19:40:26 +0000 (15:40 -0400)]
fix arm __tlsdesc_dynamic when built as thumb code without __ARM_ARCH>=5

we don't actually support building asm source files as thumb1, but
it's possible that the condition __ARM_ARCH>=5 would be false on old
compilers that did not define __ARM_ARCH at all. avoiding that would
require enumerating all of the possible __ARM_ARCH_*__ macros for
testing.

as noted in commit 05870abeaac0588fb9115cfd11f96880a0af2108, mov lr,pc
is not valid for saving a return address when in thumb mode. since
this code is a hot path (dynamic TLS access), don't do the out-of-line
bl->bx chaining to save the return value; instead, use the fact that
this file is preprocessed asm to add the missing thumb bit with an add
in place of the mov.

the change here does not affect builds for ISA levels new enough to
have a thread pointer read instruction, or for armv5 and later as long
as the compiler properly defines __ARM_ARCH, or for any build as arm
(not thumb) code. it's likely that it makes no difference whatsoever
to any present-day practical build environments, but nonetheless now
it's safe.

as an alternative, we could just assume __thumb__ implies availability
of blx since we don't support building asm source files as thumb1. I
didn't do that in order to avoid having a wrong assumption here if
that ever changes.

4 years agofix arm __a_barrier_oldkuser when built as thumb
Rich Felker [Wed, 11 Sep 2019 17:21:28 +0000 (13:21 -0400)]
fix arm __a_barrier_oldkuser when built as thumb

as noted in commit 05870abeaac0588fb9115cfd11f96880a0af2108, mov lr,pc
is not a valid method for saving the return address in code that might
be built as thumb.

this one is unlikely to matter, since any ISA level that has thumb2
should also have native implementations of atomics that don't involve
kuser_helper, and the affected code is only used on very old kernels
to begin with.

4 years agofix code path where child function returns in arm __clone built as thumb
Rich Felker [Wed, 11 Sep 2019 17:13:57 +0000 (13:13 -0400)]
fix code path where child function returns in arm __clone built as thumb

mov lr,pc is not a valid way to save the return address in thumb mode
since it omits the thumb bit. use a chain of bl and bx to emulate blx.
this could be avoided by converting to a .S file with preprocessor
conditions to use blx if available, but the time cost here is
dominated by the syscall anyway.

while making this change, also remove the remnants of support for
pre-bx ISA levels. commit 9f290a49bf9ee247d540d3c83875288a7991699c
removed the hack from the parent code paths, but left the unnecessary
code in the child. keeping it would require rewriting two code paths
rather than one, and is useless for reasons described in that commit.

4 years agoaarch64: add HWCAP2 flags from linux v5.2
Szabolcs Nagy [Tue, 20 Aug 2019 10:20:03 +0000 (10:20 +0000)]
aarch64: add HWCAP2 flags from linux v5.2

AT_HWCAP2 flags, see

  linux commit 671db581815faf17cbedd7fcbc48823a247d90b1
  arm64: Expose DC CVADP to userspace

  linux commit 06a916feca2b262ab0c1a2aeb68882f4b1108a07
  arm64: Expose SVE2 features for userspace

4 years agoadd new syscall numbers from linux v5.2
Szabolcs Nagy [Mon, 12 Aug 2019 18:48:35 +0000 (18:48 +0000)]
add new syscall numbers from linux v5.2

new mount api syscalls were added, same numers on all targets, see

  linux commit a07b20004793d8926f78d63eb5980559f7813404
  vfs: syscall: Add open_tree(2) to reference or clone a mount

  linux commit 2db154b3ea8e14b04fee23e3fdfd5e9d17fbc6ae
  vfs: syscall: Add move_mount(2) to move mounts around

  linux commit 24dcb3d90a1f67fe08c68a004af37df059d74005
  vfs: syscall: Add fsopen() to prepare for superblock creation

  linux commit ecdab150fddb42fe6a739335257949220033b782
  vfs: syscall: Add fsconfig() for configuring and managing a context

  linux commit 93766fbd2696c2c4453dd8e1070977e9cd4e6b6d
  vfs: syscall: Add fsmount() to create a mount for a superblock

  linux commit cf3cba4a429be43e5527a3f78859b1bfd9ebc5fb
  vfs: syscall: Add fspick() to select a superblock for reconfiguration

  linux commit 9c8ad7a2ff0bfe58f019ec0abc1fb965114dde7d
  uapi, x86: Fix the syscall numbering of the mount API syscalls [ver #2]

  linux commit d8076bdb56af5e5918376cd1573a6b0007fc1a89
  uapi: Wire up the mount API syscalls on non-x86 arches [ver #2]

4 years agofcntl.h: add AT_RECURSIVE from linux v5.2
Szabolcs Nagy [Mon, 12 Aug 2019 18:30:00 +0000 (18:30 +0000)]
fcntl.h: add AT_RECURSIVE from linux v5.2

apply open_tree with OPEN_TREE_CLONE call to the entire subtree, see

  linux commit a07b20004793d8926f78d63eb5980559f7813404
  vfs: syscall: Add open_tree(2) to reference or clone a mount

4 years agofcntl.h: add AT_STATX_ statx sync flag definitions
Szabolcs Nagy [Mon, 12 Aug 2019 18:21:47 +0000 (18:21 +0000)]
fcntl.h: add AT_STATX_ statx sync flag definitions

see

  linux commit a528d35e8bfcc521d7cb70aaf03e1bd296c8493f
  statx: Add a system call to make enhanced file info available

these are linux specific and not reserved names for fcntl.h so they
are under _BSD_SOURCE|_GNU_SOURCE.

4 years agosched.h: add CLONE_PIDFD from linux v5.2
Szabolcs Nagy [Mon, 12 Aug 2019 17:57:42 +0000 (17:57 +0000)]
sched.h: add CLONE_PIDFD from linux v5.2

when set a pidfd is stored in parent_tidptr, see

  linux commit b3e5838252665ee4cfa76b82bdf1198dca81e5be
  clone: add CLONE_PIDFD

4 years agonetinet/if_ether.h: add ETH_P_DSA_8021Q from linux v5.2
Szabolcs Nagy [Mon, 12 Aug 2019 17:37:48 +0000 (17:37 +0000)]
netinet/if_ether.h: add ETH_P_DSA_8021Q from linux v5.2

ethertype for fake VLAN header for DSA, see

  linux commit bf5bc3ce8a8f32a0d45b6820ede8f9fc3e9c23df
  ether: Add dedicated Ethertype for pseudo-802.1Q DSA tagging

4 years agohonor __WCHAR_TYPE__ on archs with legacy long definition of wchar_t
Rich Felker [Sun, 8 Sep 2019 21:33:48 +0000 (17:33 -0400)]
honor __WCHAR_TYPE__ on archs with legacy long definition of wchar_t

historically, a number of 32-bit archs used long rather than int for
wchar_t, for no good reason. GCC still uses the historical types, but
clang replaced them all with int, and it seems PCC uses int too.
mismatching the compiler's type for wchar_t is not an option due to
wide string literals.

note that the mismatch does not affect C++ ABI since wchar_t is its
own builtin type/keyword in C++, distinct from both int and long, not
a typedef.

i386 already worked around this by honoring __WCHAR_TYPE__ if defined
by the compiler, and only using the official legacy ABI type if not.
add the same to the other affected archs.

it might make sense at some point to switch to using int as the
default if __WCHAR_TYPE__ is not defined, if the expectations is that
new compilers will treat int as the correct choice, but it's unlikely
that the case where __WCHAR_TYPE__ is undefined will ever be used
anyway. I actually wanted to move the definition of wchar_t to the
top-level shared alltypes.h.in, using __WCHAR_TYPE__ and falling back
to int if not defined, but that can't be done without assuming all
compilers define __WCHAR_TYPE__ thanks to some pathological archs
where the ABI has wchar_t as an unsigned type.

4 years agosynchronously clean up pthread_create failure due to scheduling errors
Rich Felker [Fri, 6 Sep 2019 20:17:44 +0000 (16:17 -0400)]
synchronously clean up pthread_create failure due to scheduling errors

previously, when pthread_create failed due to inability to set
explicit scheduling according to the requested attributes, the nascent
thread was detached and made responsible for its own cleanup via the
standard pthread_exit code path. this left it consuming resources
potentially well after pthread_create returned, in a way that the
application could not see or mitigate, and unnecessarily exposed its
existence to the rest of the implementation via the global thread
list.

instead, attempt explicit scheduling early and reuse the failure path
for __clone failure if it fails. the nascent thread's exit futex is
not needed for unlocking the thread list, since the thread calling
pthread_create holds the thread list lock the whole time, so it can be
repurposed to ensure the thread has finished exiting. no pthread_exit
is needed, and freeing the stack, if needed, can happen just as it
would if __clone failed.

4 years agoset explicit scheduling for new thread from calling thread, not self
Rich Felker [Fri, 6 Sep 2019 19:26:44 +0000 (15:26 -0400)]
set explicit scheduling for new thread from calling thread, not self

if setting scheduling properties succeeds, the new thread may end up
with lower priority than the caller, and may be unable to continue
running due to another intermediate-priority thread. this produces a
priority inversion situation for the thread calling pthread_create,
since it cannot return until the new thread reports success.

originally, the parent was responsible for setting the new thread's
priority; commits b8742f32602add243ee2ce74d804015463726899 and
40bae2d32fd6f3ffea437fa745ad38a1fe77b27e changed it as part of
trimming down the pthread structure. since then, commit
04335d9260c076cf4d9264bd93dd3b06c237a639 partly reversed the changes,
but did not switch responsibilities back. do that now.

4 years agofix unsynchronized decrement of thread count on pthread_create error
Rich Felker [Fri, 6 Sep 2019 19:52:00 +0000 (15:52 -0400)]
fix unsynchronized decrement of thread count on pthread_create error

commit 8f11e6127fe93093f81a52b15bb1537edc3fc8af wrongly documented
that all changes to libc.threads_minus_1 were guarded by the thread
list lock, but the decrement for failed SYS_clone took place after the
thread list lock was released.

4 years agoadd public declaration for optreset under appropriate feature profiles
Rich Felker [Fri, 30 Aug 2019 21:48:47 +0000 (17:48 -0400)]
add public declaration for optreset under appropriate feature profiles

commit 030e52639248ac8417a4934298caa78c21a228d1 added optreset, a BSD
extension to getopt duplicating the functionality (also an extension)
of setting optind to 0, but failed to provide a public declaration for
it. according to the BSD documentation and headers, the application is
not supposed to need to provide its own declaration.

4 years agoadd posix_spawn [f]chdir file actions
Rich Felker [Fri, 30 Aug 2019 20:21:36 +0000 (16:21 -0400)]
add posix_spawn [f]chdir file actions

these are presently extensions, thus named with _np to match glibc and
other implementations that provide them; however they are likely to be
standardized in the future without the _np suffix as a result of
Austin Group issue 1208. if so, both names will be kept as aliases.

4 years agoadd copy_file_range system call wrapper
Árni Dagur [Mon, 19 Aug 2019 23:41:14 +0000 (23:41 +0000)]
add copy_file_range system call wrapper

4 years agofix clash between sys/user.h and kernel ptrace.h on powerpc[64], sh
Rich Felker [Mon, 19 Aug 2019 03:41:17 +0000 (23:41 -0400)]
fix clash between sys/user.h and kernel ptrace.h on powerpc[64], sh

due to historical accident/sloppiness in glibc, the powerpc,
powerpc64, and sh versions of struct user, defined by sys/user.h, used
struct pt_regs from the kernel asm/ptrace.h for their regs member.
this made it impossible to define the type in an API-compatible manner
without either including asm/ptrace.h like glibc does (contrary to our
policy of not depending on kernel headers), or clashing with
asm/ptrace.h's definition of struct pt_regs if both headers are
included (which is almost always the case in software using
sys/user.h).

for a long time I viewed this problem as having no reasonable fix. I
even explored the possibility of having the powerpc[64] and sh
versions of user.h just include the kernel header (breaking with
policy), but that looked like it might introduce new clashes with
sys/ptrace.h. and it would also bring in a lot of additional cruft
that makes no sense for sys/user.h to expose. glibc goes out of its
way to suppress some of that with #undef, possibly leading to
different problems. this is a rabbit-hole that should be explored no
further.

as it turns out, however, nothing actually uses struct user
sufficiently to care about the type of the regs member; most software
including sys/user.h does not even use struct user at all. so, the
problem can be fixed just by doing away with the insistence on strict
glibc API compatibility for the struct tag of the regs member.

rather than renaming the tag, which might lead to the new name
entering use as API, simply use an untagged structure inside struct
user with the same members/layout as struct pt_regs.

for sh, struct pt_dspregs is just removed entirely since it was not
used.

4 years agofix external dummy_lock symbol inadvertently introduced in sigaction
Rich Felker [Sun, 18 Aug 2019 02:31:40 +0000 (22:31 -0400)]
fix external dummy_lock symbol inadvertently introduced in sigaction

commit 9b14ad541068d4f7d0be9bcd1ff4c70090d868d3 introduced this
namespace violation.

4 years agoremove sporadic server members from struct sched_param
Rich Felker [Thu, 15 Aug 2019 00:50:42 +0000 (20:50 -0400)]
remove sporadic server members from struct sched_param

these members are associated with an unsupported option group. with
time_t changing size on 32-bit archs, all interfaces taking struct
sched_param arguments would need redirection and compat shims in order
to be able to continue offering these members, for no benefit. just
convert them to reserved space instead.

4 years agore-add ELF gregs and fpregs types to riscv64 user.h
Khem Raj [Wed, 14 Aug 2019 01:07:16 +0000 (18:07 -0700)]
re-add ELF gregs and fpregs types to riscv64 user.h

d493206de7df4db07ad34f24701539ba0a6ed38c deleted all the content of
user.h, but sys/procfs.h expects this from sys/user.h
threfore we retain the non conflicting parts

4 years agofix regression whereby main thread didn't get TLS relocations
Rich Felker [Wed, 14 Aug 2019 01:53:30 +0000 (21:53 -0400)]
fix regression whereby main thread didn't get TLS relocations

commit ffab43602b5900c86b7040abdda8ccf6cdec95f5 broke this by moving
relocations after not only the allocation of storage for the main
thread's static TLS, but after the copying of the TLS image. thus,
relocation results were not reflected in the main thread's copy. this
could be fixed by calling __reset_tls after relocations, but instead
split the allocation and installation before/after relocations so that
there's not a redundant copy.

due to commit 71af5309874269bcc9e4b84ea716fab33d888c1d, updating of
static_tls_cnt needs to be kept with allocation of static TLS, before
relocations, rather than after installation.

4 years agofix accidentlly-external cmp symbol introduced with catgets
Rich Felker [Tue, 13 Aug 2019 14:19:09 +0000 (10:19 -0400)]
fix accidentlly-external cmp symbol introduced with catgets

commit 7590203c486d9002522019045d34ee3dee0a66f5 omitted static here.

4 years agomake relocation time symbol lookup and dlsym consistent
Szabolcs Nagy [Sat, 10 Aug 2019 23:14:40 +0000 (23:14 +0000)]
make relocation time symbol lookup and dlsym consistent

Using common code path for all symbol lookups fixes three dlsym issues:

- st_shndx of STT_TLS symbols were not checked and thus an undefined
  tls symbol reference could be incorrectly treated as a definition
  (the sysv hash lookup returns undefined symbols, gnu does not, so should
  be rare in practice).

- symbol binding was not checked so a hidden symbol may be returned
  (in principle STB_LOCAL symbols may appear in the dynamic symbol table
  for hidden symbols, but linkers most likely don't produce it).

- mips specific behaviour was not applied (ARCH_SYM_REJECT_UND) so
  undefined symbols may be returned on mips.

always_inline is used to avoid relocation performance regression, the
code generation for find_sym should not be affected.

4 years agoldso: correct condition for local symbol handling in do_relocs
Rich Felker [Mon, 12 Aug 2019 22:19:38 +0000 (18:19 -0400)]
ldso: correct condition for local symbol handling in do_relocs

commit 7a9669e977e5f750cf72ccbd2614f8b72ce02c4c added use of the
symbol reference as the definition, in place of performing a lookup,
for STT_SECTION symbol references that were first found used in FDPIC.
such references may happen in certain other cases, such as
local-dynamic TLS and with relocation types that require a symbol but
that are being used for non-symbolic purposes, like the powerpc
unaligned address relocations.

in all such cases I'm aware of, the symbol referenced is a section
symbol (STT_SECTION); however, the important semantic property is not
its being a section, but rather its binding local (STB_LOCAL). check
the latter instead of the former for greater generality and semantic
correctness.

4 years agoadd support for powerpc/powerpc64 unaligned relocations
Samuel Holland [Sun, 30 Jun 2019 12:39:20 +0000 (07:39 -0500)]
add support for powerpc/powerpc64 unaligned relocations

R_PPC_UADDR32 (R_PPC64_UADDR64) has the same meaning as R_PPC_ADDR32
(R_PPC64_ADDR64), except that its address need not be aligned. For
powerpc64, BFD ld(1) will automatically convert between ADDR<->UADDR
relocations when the address is/isn't at its native alignment. This
will happen if, for example, there is a pointer in a packed struct.

gold and lld do not currently generate R_PPC64_UADDR64, but pass
through misaligned R_PPC64_ADDR64 relocations from object files,
possibly relaxing them to misaligned R_PPC64_RELATIVE. In both cases
(relaxed or not) this violates the PSABI, which defines the relevant
field type as "a 64-bit field occupying 8 bytes, the alignment of
which is 8 bytes unless otherwise specified."

All three linkers violate the PSABI on 32-bit powerpc, where the only
difference is that the field is 32 bits wide, aligned to 4 bytes.

Currently musl fails to load executables linked by BFD ld containing
R_PPC64_UADDR64, with the error "unsupported relocation type 43".
This change provides compatibility with BFD ld on powerpc64, and any
static linker on either architecture that starts following the PSABI
more closely.

4 years agoldso: remove redundant runtime checks in static TLS logic
Rich Felker [Sun, 11 Aug 2019 15:57:38 +0000 (11:57 -0400)]
ldso: remove redundant runtime checks in static TLS logic

as a result of commit ffab43602b5900c86b7040abdda8ccf6cdec95f5,
static_tls_cnt is now valid during relocations at program startup, so
it's no longer necessary to condition the check against static_tls_cnt
on this being a runtime (dlopen) relocation.

4 years agoldso: fix calloc misuse allocating initial tls
Rich Felker [Sun, 11 Aug 2019 15:48:06 +0000 (11:48 -0400)]
ldso: fix calloc misuse allocating initial tls

this is analogous to commit 2f1f51ae7b2d78247568e7fdb8462f3c19e469a4,
and should have been caught at the same time since it was right next
to the code moved in that commit. between final stage 3 reloc_all and
the jump to the main program's entry point, it is not valid to call
any functions which may be interposed by the application; doing so
results in execution of application code before ctors have run, and on
fdpic archs, before the main program's fdpic self-fixups have taken
place, which will produce runaway wrong execution.

4 years agoadd secure_getenv function
Petr Vaněk [Tue, 28 May 2019 20:47:48 +0000 (22:47 +0200)]
add secure_getenv function

This function is a GNU extension introduced in glibc 2.17.

4 years agoin clock_getres, check for null pointer before storing result
Rich Felker [Thu, 8 Aug 2019 01:35:28 +0000 (21:35 -0400)]
in clock_getres, check for null pointer before storing result

POSIX allows a null pointer, in which case the function only checks
the validity of the clock id argument.

4 years agoremove spurious null check in clock_settime
Rich Felker [Thu, 8 Aug 2019 01:31:41 +0000 (21:31 -0400)]
remove spurious null check in clock_settime

at the point of this check, the pointer has already been dereferenced.
clock_settime is not defined for null pointer arguments.

4 years agofix regression in recvmmsg with no timeout
Rich Felker [Thu, 8 Aug 2019 01:28:37 +0000 (21:28 -0400)]
fix regression in recvmmsg with no timeout

somewhat analogous to commit d0b547dfb5f7678cab6bc39dd736ed6454357ca4,
but here the omission of the null timeout check was in the time64
syscall code path. this code is not yet used except on x32.

4 years agoadd non-stub implementation of catgets localization functions
Rich Felker [Thu, 8 Aug 2019 01:15:53 +0000 (21:15 -0400)]
add non-stub implementation of catgets localization functions

these accept the netbsd/openbsd message catalog file format,
consisting of a sorted list of set headers and a sorted list of
message headers for each set, admitting trivial binary search for
lookups.

the gnu format was not chosen because it's unusably bad. it does not
admit efficient (log time or better) lookups; rather, it requires
linear search or hash table lookups, and the hash function is awful:
it's literally set_id*msg_id.

4 years agofix regression in select with no timeout
Rich Felker [Wed, 7 Aug 2019 06:57:53 +0000 (02:57 -0400)]
fix regression in select with no timeout

commit 722a1ae3351a03ab25010dbebd492eced664853b inadvertently passed a
copy of {s,us} to the syscall even if the timeout argument tv was
null, thereby causing immediate timeout (polling) in place of
unlimited timeout. only archs using SYS_select were affected.

4 years agofix failure of glob to match broken symlinks under some conditions
Rich Felker [Thu, 11 Jul 2019 20:55:17 +0000 (16:55 -0400)]
fix failure of glob to match broken symlinks under some conditions

when the pattern ended with one or more literal path components, or
when the GLOB_MARK flag was passed to request that glob flag directory
results and the type obtained by readdir was unknown or inconclusive
(symlink), the stat function was called to evaluate existence and/or
determine type. however, stat fails with ENOENT for broken symlinks,
and this caused the match to be omitted from the results.

instead, use stat only for the unknown/inconclusive cases with
GLOB_MARK, and otherwise, or if stat fails, use lstat existence still
needs to be determined. this minimizes the number of costly syscalls,
performing both only in the case where GLOB_MARK is in use and there
is a final literal path component which is a broken symlink.

based on/simplified from patch by James Y Knight.

4 years agoremove riscv64 bits/user.h contents
Rich Felker [Tue, 6 Aug 2019 16:50:38 +0000 (12:50 -0400)]
remove riscv64 bits/user.h contents

the contents conflicted with asm/ptrace.h. glibc does not provide
anything in user.h for riscv, so software cannot be depending on it.

simplified from patch submitted by Baruch Siach.

4 years agofix risc64 conflict with kernel headers
Baruch Siach [Tue, 6 Aug 2019 05:51:13 +0000 (08:51 +0300)]
fix risc64 conflict with kernel headers

Rename user registers struct definitions to avoid conflict with the
asm/ptrace.h kernel header that defines the same structs. Use the
__riscv_mc prefix as glibc does.

4 years agoin arm cancellation point asm, don't unnecessarily preserve link register
Patrick Oppenlander [Thu, 1 Aug 2019 04:34:59 +0000 (14:34 +1000)]
in arm cancellation point asm, don't unnecessarily preserve link register

The only reason we needed to preserve the link register was because we
were using a branch-link instruction to branch to __cp_cancel.
Replacing this with a branch means we can avoid the save/restore as
the link register is no longer modified.

4 years agoglob: implement GLOB_TILDE and GLOB_TILDE_CHECK
Ismael Luceno [Thu, 25 Jul 2019 21:50:48 +0000 (23:50 +0200)]
glob: implement GLOB_TILDE and GLOB_TILDE_CHECK

4 years agouse setitimer function rather than syscall to implement alarm
Rich Felker [Mon, 5 Aug 2019 23:55:42 +0000 (19:55 -0400)]
use setitimer function rather than syscall to implement alarm

otherwise alarm will break on 32-bit archs when time_t is changed to
64-bit. a second itimerval object is introduced for retrieving the old
value, since the setitimer function has restrict-qualified arguments.

4 years agofix build regression in i386 asm for atan2, atan2f
Rich Felker [Mon, 5 Aug 2019 23:57:07 +0000 (19:57 -0400)]
fix build regression in i386 asm for atan2, atan2f

commit f3ed8bfe8a82af1870ddc8696ed4cc1d5aa6b441 inadvertently removed
labels that were still needed.

4 years agofix x87 stack imbalance in corner cases of i386 math asm
Rich Felker [Mon, 5 Aug 2019 22:41:47 +0000 (18:41 -0400)]
fix x87 stack imbalance in corner cases of i386 math asm

commit 31c5fb80b9eae86f801be4f46025bc6532a554c5 introduced underflow
code paths for the i386 math asm, along with checks on the fpu status
word to skip the underflow-generation instructions if the underflow
flag was already raised. unfortunately, at least one such path, in
log1p, returned with 2 items on the x87 stack rather than just 1 item
for the return value. this is a violation of the ABI's calling
convention, and could cause subsequent floating point code to produce
NANs due to x87 stack overflow. if floating point results are used in
flow control, this can lead to runaway wrong code execution.

rather than reviewing each "underflow already raised" code path for
correctness, remove them all. they're likely slower than just
performing the underflow code unconditionally, and significantly more
complex.

all of this code should be ripped out and replaced by C source files
with inline asm. doing so would preclude this kind of error by having
the compiler perform all x87 stack register allocation and stack
manipulation, and would produce comparable or better code. however
such a change is a much larger project.

4 years agofix regression in clock_gettime on 32-bit archs without vdso
Rich Felker [Mon, 5 Aug 2019 16:01:13 +0000 (12:01 -0400)]
fix regression in clock_gettime on 32-bit archs without vdso

commit 72f50245d018af0c31b38dec83c557a4e5dd1ea8 broke this by creating
a code path where r is uninitialized.

4 years agoupdate riscv64 syscall numbers to linux v5.1
Rich Felker [Sat, 3 Aug 2019 22:43:36 +0000 (18:43 -0400)]
update riscv64 syscall numbers to linux v5.1

commit f3f96f2daa4d00f0e38489fb465cd0244b531abe added these for the
rest of the archs, but the patch it corresponded to missed riscv64
since riscv64 was not yet upstream at the time. this caused commit
dfc81828f7ab41da08f744c44117a1bb20a05749 to break riscv64 build, due
to a wrong assumption that SYS_statx was unconditionally defined.

4 years agoclock_gettime: add support for 32-bit vdso with 64-bit time_t
Rich Felker [Fri, 2 Aug 2019 18:04:45 +0000 (14:04 -0400)]
clock_gettime: add support for 32-bit vdso with 64-bit time_t

this fixes a major upcoming performance regression introduced by
commit 72f50245d018af0c31b38dec83c557a4e5dd1ea8, whereby 32-bit archs
would lose vdso clock_gettime after switching to 64-bit time_t, unless
the kernel supports time64 and provides a time64 version of the vdso
function. this would incur not just one but two syscalls: first, the
failed time64 syscall, then the fallback time32 one.

overflow of the 32-bit result is detected and triggers a revert to
syscalls. normally, on a system that's not Y2038-ready, this would
still overflow, but if the process has been migrated to a
time64-capable kernel or if the kernel has been hot-patched to add
time64 syscalls, it may conceivably work.

4 years agomove IPC_STAT definition to a new bits/ipcstat.h file
Rich Felker [Thu, 1 Aug 2019 00:35:37 +0000 (20:35 -0400)]
move IPC_STAT definition to a new bits/ipcstat.h file

otherwise, 32-bit archs that could otherwise share the generic
bits/ipc.h would need to duplicate the struct ipc_perm definition,
obscuring the fact that it's the same. sysvipc is not widely used and
these headers are not commonly included, so there is no performance
gain to be had by limiting the number of indirectly included files
here.

files with the existing time32 definition of IPC_STAT are added to all
current 32-bit archs now, so that when it's changed the change will
show up as a change rather than addition of a new file where it's less
obvious that the value is changing vs the generic one that was used
before.

4 years agofix missing declarations for pthread_join extensions in source file
Rich Felker [Wed, 31 Jul 2019 21:18:21 +0000 (17:18 -0400)]
fix missing declarations for pthread_join extensions in source file

per policy, define the feature test macro to get declarations for the
pthread_tryjoin_np and pthread_timedjoin_np functions. in the past
this has been only for checking; with 32-bit archs getting 64-bit
time_t it will also be necessary for symbols to get redirected
correctly.

4 years agoallow archs to define IPC_STAT, propagate time64 bit to other macros
Rich Felker [Wed, 31 Jul 2019 05:17:53 +0000 (01:17 -0400)]
allow archs to define IPC_STAT, propagate time64 bit to other macros

to make use of {sem,shm,msg}ctl IPC_STAT functionality to provide
64-bit time_t on 32-bit archs, IPC_STAT and related macros must be
defined with bit 8 (0x100) set. allow archs to define IPC_STAT in
bits/ipc.h, and define the other macros in terms of it so that they
all get the same value of the time64 bit.

4 years agoclock_gettime: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Wed, 31 Jul 2019 04:26:16 +0000 (00:26 -0400)]
clock_gettime: add time64 syscall support, decouple 32-bit time_t

the time64 syscall has to be used if time_t is 64-bit, since there's
no way of knowing before making a syscall whether the result will fit
in 32 bits, and the 32-bit syscalls do not report overflow as an
error.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the result is now read from the kernel
through long[2] array, then copied into the timespec, to remove the
assumption that time_t is the same as long.

vdso clock_gettime is still used in place of a syscall if available.
32-bit archs with 64-bit time_t must use the time64 version of the
vdso function; if it's not available, performance will significantly
suffer. support for both vdso functions could be added, but would
break the ability to move a long-lived process from a pre-time64
kernel to one that can outlast Y2038 with checkpoint/resume, at least
without added hacks to identify that the 32-bit function is no longer
usable and stop using it (e.g. by seeing negative tv_sec). this
possibility may be explored in future work on the function.

4 years agoclock_adjtime: add time64 support, decouple 32-bit time_t, fix x32
Rich Felker [Wed, 31 Jul 2019 03:51:45 +0000 (23:51 -0400)]
clock_adjtime: add time64 support, decouple 32-bit time_t, fix x32

the 64-bit/time64 version of the syscall is not API-compatible with
the userspace timex structure definition; fields specified as long
have type long long. so when using the time64 syscall, we have to
convert the entire structure. this was always the case for x32 as
well, but went unnoticed, meaning that clock_adjtime just passed junk
to the kernel on x32. it should be fixed now.

for the fallback case, we avoid encoding any assumptions about the new
location of the time member or naming of the legacy slots by accessing
them through a union of the kernel type and the new userspace type.
the only assumption is that the non-time members live at the same
offsets as in the (non-time64, long-based) kernel timex struct. this
property saves us from having to convert the whole thing, and avoids a
lot of additional work in compat shims.

the new code is statically unreachable for now except on x32, where it
fixes major brokenness. it is permanently unreachable on 64-bit.

4 years agoioctl: add fallback for new time64 SIOCGSTAMP[NS]
Rich Felker [Wed, 31 Jul 2019 03:48:25 +0000 (23:48 -0400)]
ioctl: add fallback for new time64 SIOCGSTAMP[NS]

without this, the SIOCGSTAMP and SIOCGSTAMPNS ioctl commands, for
obtaining timestamps, would stop working on pre-5.1 kernels after
time_t is switched to 64-bit and their values are changed to the new
time64 versions.

new code is written such that it's statically unreachable on 64-bit
archs, and on existing 32-bit archs until the macro values are changed
to activate 64-bit time_t.

4 years agoget/setsockopt: add fallback for new time64 SO_RCVTIMEO/SO_SNDTIMEO
Rich Felker [Wed, 31 Jul 2019 02:11:39 +0000 (22:11 -0400)]
get/setsockopt: add fallback for new time64 SO_RCVTIMEO/SO_SNDTIMEO

without this, the SO_RCVTIMEO and SO_SNDTIMEO socket options would
stop working on pre-5.1 kernels after time_t is switched to 64-bit and
their values are changed to the new time64 versions.

new code is written such that it's statically unreachable on 64-bit
archs, and on existing 32-bit archs until the macro values are changed
to activate 64-bit time_t.

4 years agomake __socketcall analogous to __syscall, error-returning
Rich Felker [Tue, 30 Jul 2019 21:51:16 +0000 (17:51 -0400)]
make __socketcall analogous to __syscall, error-returning

the __socketcall and __socketcall_cp macros are remnants from a really
old version of the syscall-mechanism infrastructure, and don't follow
the pattern that the "__" version of the macro returns the raw negated
error number rather than setting errno and returning -1.

for time64 purposes, some socket syscalls will need to operate on the
error value rather than returning immediately, so fix this up so they
can use it.

4 years agosysvipc: overhaul {sem,shm,msg}ctl for time64
Rich Felker [Tue, 30 Jul 2019 18:51:53 +0000 (14:51 -0400)]
sysvipc: overhaul {sem,shm,msg}ctl for time64

being "ctl" functions that take command numbers, these will be handled
like ioctl/sockopt/etc., using new command numbers for the time64
variants with an "IPC_TIME64" bit added to their values. to obtain
such a reserved bit, we reuse the IPC_64 bit, 0x100, which served only
as part of the libc-to-kernel interface, not as a public interface of
the libc functions.

using new command numbers avoids the need for compat shims (in ABIs
doing time64 through symbol redirection and compat shims) and, by
virtue of having a fixed time64 bit for all commands, we can ensure
that libc can perform the appropriate translations, even if the
application is using new commands from a newer version of the libc
headers than the libc available at runtime.

for the vast majority of 32-bit archs, the kernel {sem,shm,msq}id64_ds
definitions left padding space intended for expanding their time_t
fields to 64 bits in-place, and it would have been really nice to be
able to do time64 support that way. however the padding was almost
always in little-endian order (except on powerpc, and for msqid_ds
only on mips, where it matched the arch's byte order), and more
importantly, the alignment was overlooked. in semid_ds and msqid_ds,
the time_t members were not suitably aligned to be expanded to 64-bit,
due to the ipc_perm header consisting of 9 32-bit words -- except on
powerpc where ipc_perm contains an extra padding word. in shmid_ds,
the time_t members were suitably aligned, except that mips
(accidentally?) omitted the padding for them alltogether.

as a result, we're stuck with adding new time_t fields on the end of
the structures, and assembling the 32-bit lo/hi parts (or 16-bit hi
parts, for mips shmid_ds, which lacked sufficient reserved space for
full 32-bit hi parts) to fill them in.

all of the functional changes here are conditional on the IPC_TIME64
macro having a nonzero definition, which will only happen when
IPC_STAT is redefined for 32-bit archs, and on time_t being larger
than long, so for now the new code is all dead code.

4 years agofix semctl with SEM_STAT_ANY
Rich Felker [Wed, 31 Jul 2019 21:25:40 +0000 (17:25 -0400)]
fix semctl with SEM_STAT_ANY

due to the variadic signature, semctl needs to be made aware of any
new commands that take arguments. this was overlooked when commit
af55070eae5438476f921d827b7ae49e8141c3fe added SEM_STAT_ANY.

4 years agoremove gratuitously-different arch-specific bits/ipc.h files
Rich Felker [Tue, 30 Jul 2019 18:16:12 +0000 (14:16 -0400)]
remove gratuitously-different arch-specific bits/ipc.h files

these differ from generic only in using endian-matched padding with a
short __ipc_perm_seq field in place of the int field in generic. this
is not a documented public interface anyway, and the original intent
was to use int here. some ports just inadvertently slipped in the
kernel short+padding form.

4 years agoremove arch-specific bits/ipc.h that are identical to generic
Rich Felker [Tue, 30 Jul 2019 18:12:58 +0000 (14:12 -0400)]
remove arch-specific bits/ipc.h that are identical to generic

previously these differed from generic because they needed their own
definitions of IPC_64. now that it's no longer in public header,
they're identical.

4 years agomove IPC_64 from public bits/ipc.h to syscall_arch.h
Rich Felker [Tue, 30 Jul 2019 17:07:55 +0000 (13:07 -0400)]
move IPC_64 from public bits/ipc.h to syscall_arch.h

the definition of the IPC_64 macro controls the interface between libc
and the kernel through syscalls; it's not a public API. the meaning is
rather obscure. long ago, Linux's sysvipc *id_ds structures used
16-bit uids/gids and wrong types for a few other fields. this was in
the libc5 era, before glibc. the IPC_64 flag (64 is a misnomer; it's
more like 32) tells the kernel to use the modern[-ish] versions of the
structures.

the definition of IPC_64 has nothing to do with whether the arch is
32- or 64-bit. rather, due to either historical accident or
intentional obnoxiousness, the kernel only accepts and masks off the
0x100 IPC_64 flag conditional on CONFIG_ARCH_WANT_IPC_PARSE_VERSION,
i.e. for archs that want to provide, or that accidentally provided,
both. for archs which don't define this option, no masking is
performed and commands with the 0x100 bit set will fail as invalid. so
ultimately, the definition is just a matter of matching an arbitrary
switch defined per-arch in the kernel.

4 years agoselect: overhaul for time64
Rich Felker [Tue, 30 Jul 2019 01:08:43 +0000 (21:08 -0400)]
select: overhaul for time64

major changes are made alongside adding time64 syscall support to
account for issues found during research. select historically accepts
non-normalized (tv_usec not restricted to less than 1000000) timeouts,
and the kernel normalizes them, but the normalization code is buggy
and subject to integer overflows. since normalization is needed anyway
when using SYS_pselect6 or SYS_pselect6_time64 as the backend, simply
do it up-front to eliminate both code path complexity and the
possibility of kernel bugs.

as a side effect, select no longer updates the caller's timeout
timeval with the remaining time. previously, archs that used
SYS_select updated it and archs that used SYS_pselect6 didn't. this
change may turn out to be controversial and may need revisiting, but
in any case the old behavior was not strictly conforming.

POSIX allows modification of the timeout "upon successful completion",
but the Linux syscall modifies it upon unsuccessful completion (EINTR)
as well (and presumably each time the syscall stops and restarts
before it's known whether completion will be successful). it's
possible that this language does not reflect the actual intent of the
standard, since other historical implementations probably behaved like
Linux, but that should be clarified if there's a desire to bring the
old behavior back. regardless, programs that are depending on this are
not correct and are already broken on some archs we support.

4 years agorecvmmsg: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Tue, 30 Jul 2019 01:03:01 +0000 (21:03 -0400)]
recvmmsg: add time64 syscall support, decouple 32-bit time_t

the time64 syscall is used only if the timeout does not fit in 32
bits. after preprocessing, the code is unchanged on 64-bit archs. for
32-bit archs, the timeout now goes through an intermediate copy,
meaning that the caller does not get back the updated timeout. this is
based on my reading of the documentation, which does not document the
updating as a contract you can rely on, and mentions that the whole
recvmmsg timeout mechanism is buggy and unlikely to be useful. if it
turns out that there's interest in making the remaining time
officially available to callers, such functionality could be added
back later.

4 years agosetitimer, getitimer: decouple time_t from long
Rich Felker [Tue, 30 Jul 2019 00:57:05 +0000 (20:57 -0400)]
setitimer, getitimer: decouple time_t from long

these functions have no new time64 syscall, so the existence of a
time64 syscall cannot be used as the condition for the new code.
instead, assume the syscall takes timevals as longs, which is true
everywhere but x32, and interface with the kernel through long[4]
objects.

rather than adding new hacks to special-case x32 here, just add
x32-specific source files since a trivial syscall wrapper suffices
there.

the new code paths added in this commit are statically unreachable on
all current archs, but will become reachable when 32-bit archs get
64-bit time_t.

4 years agoremove duplicates of new generic bits/msg.h
Rich Felker [Mon, 29 Jul 2019 22:27:17 +0000 (18:27 -0400)]
remove duplicates of new generic bits/msg.h

4 years agouse 64-bit msqid_ds layout in the generic version of bits/msg.h
Rich Felker [Mon, 29 Jul 2019 22:23:27 +0000 (18:23 -0400)]
use 64-bit msqid_ds layout in the generic version of bits/msg.h

this layout is more common already than the old generic, and should
become even more common in the future with new archs added and with
64-bit time_t on 32-bit archs.

4 years agoduplicate generic bits/msg.h for each arch using it, in prep to change
Rich Felker [Mon, 29 Jul 2019 22:22:33 +0000 (18:22 -0400)]
duplicate generic bits/msg.h for each arch using it, in prep to change

4 years agoremove duplicates of new generic bits/sem.h
Rich Felker [Mon, 29 Jul 2019 22:17:43 +0000 (18:17 -0400)]
remove duplicates of new generic bits/sem.h

some of these were not exact duplicates, but had gratuitously
different naming for padding, or omitted the endian checks because the
arch is fixed-endian.

4 years agouse 64-bit semid_ds layout in the generic version of bits/sem.h
Rich Felker [Mon, 29 Jul 2019 22:12:05 +0000 (18:12 -0400)]
use 64-bit semid_ds layout in the generic version of bits/sem.h

this layout is slightly less common than the old generic one, but only
because x86_64 and x32 wrongly (according to comments in the kernel
headers) copied the i386 padding. for future archs, and with 64-bit
time_t on 32-bit archs, the new layout here will become the most
common, and it makes sense to treat it as the generic.

4 years agocollapse out byte order conditions in bits/sem.h for fixed-endian archs
Rich Felker [Mon, 29 Jul 2019 22:09:55 +0000 (18:09 -0400)]
collapse out byte order conditions in bits/sem.h for fixed-endian archs

having preprocessor conditionals on byte order in the bits headers for
fixed-endian archs is confusing at best. remove them.

4 years agoduplicate generic bits/sem.h for each arch using it, in prep to change
Rich Felker [Mon, 29 Jul 2019 22:07:23 +0000 (18:07 -0400)]
duplicate generic bits/sem.h for each arch using it, in prep to change

4 years agoextricate bits/sem.h from x32 time_t hack
Rich Felker [Mon, 29 Jul 2019 22:03:41 +0000 (18:03 -0400)]
extricate bits/sem.h from x32 time_t hack

various padding fields in the generic bits/sem.h were defined in terms
of time_t as a cheap hack standing in for "kernel long", to allow x32
to use the generic version of the file. this was a really bad idea, as
it ended up getting copied into lots of arch-specific versions of the
bits file, and is a blocker to changing time_t to 64-bit on 32-bit
archs.

this commit adds an x32-specific version of the header, and changes
padding type back from time_t to long (currently the same type on all
archs but x32) in the generic header and all the others the hack got
copied into.

4 years agoremove trailing newlines from various versions of bits/shm.h
Rich Felker [Mon, 29 Jul 2019 19:54:38 +0000 (15:54 -0400)]
remove trailing newlines from various versions of bits/shm.h

4 years agoremove duplicates of new generic bits/shm.h
Rich Felker [Mon, 29 Jul 2019 19:52:20 +0000 (15:52 -0400)]
remove duplicates of new generic bits/shm.h

4 years agouse 64-bit shmid_ds layout in the generic version of bits/shm.h
Rich Felker [Mon, 29 Jul 2019 19:44:58 +0000 (15:44 -0400)]
use 64-bit shmid_ds layout in the generic version of bits/shm.h

this layout is more common already than the old generic, and should
become even more common in the future with new archs added and with
64-bit time_t on 32-bit archs.

the duplicate arch-specific copies are not removed yet in this commit,
so as to assist git tooling in copy/rename tracking.

4 years agoduplicate generic bits/shm.h for each arch using it, in prep to change
Rich Felker [Mon, 29 Jul 2019 19:39:24 +0000 (15:39 -0400)]
duplicate generic bits/shm.h for each arch using it, in prep to change

there are more archs sharing the generic 64-bit version of the struct,
which is uniform and much more reasonable, than sharing the current
"generic" one, and depending on how time64 sysvipc is done for 32-bit
archs, even more may be sharing the "64-bit version" in the future.

so, duplicate the current generic to all archs using it (arm, i386,
m68k, microblaze, or1k) so that the generic can be changed freely.

this is recorded as its own commit mainly as a hint to git tooling, to
assist in copy/move tracking.

4 years agotimerfd: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Mon, 29 Jul 2019 03:53:38 +0000 (23:53 -0400)]
timerfd: add time64 syscall support, decouple 32-bit time_t

the changes here are semantically and structurally identical to those
made to timer_settime and timer_gettime for time64 support.

4 years agosched_rr_get_interval: don't assume time_t is 32-bit on 32-bit archs
Rich Felker [Mon, 29 Jul 2019 03:26:38 +0000 (23:26 -0400)]
sched_rr_get_interval: don't assume time_t is 32-bit on 32-bit archs

as with clock_getres, the time64 syscall for this is not necessary or
useful, this time since scheduling timeslices are not on the order 68
years. if there's a 32-bit syscall, use it and expand the result into
timespec; otherwise there is only one syscall and it does the right
thing to store to timespec directly.

on 64-bit archs, there is no change to the code after preprocessing.

4 years agoclock_getres: don't assume time_t is 32-bit on 32-bit archs
Rich Felker [Mon, 29 Jul 2019 02:53:10 +0000 (22:53 -0400)]
clock_getres: don't assume time_t is 32-bit on 32-bit archs

the time64 syscall for this is not necessary or useful, since clock
resolution is generally better than 68-year granularity. if there's a
32-bit syscall, use it and expand the result into timespec; otherwise
there is only one syscall and it does the right thing to store to
timespec directly.

on 64-bit archs, there is no change to the code after preprocessing.

4 years agotimer_gettime: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Mon, 29 Jul 2019 02:46:19 +0000 (22:46 -0400)]
timer_gettime: add time64 syscall support, decouple 32-bit time_t

the time64 syscall has to be used if time_t is 64-bit, since there's
no way of knowing before making a syscall whether the result will fit
in 32 bits, and the 32-bit syscalls do not report overflow as an
error.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the result is now read from the kernel
through long[4] array, then copied into the timespec, to remove the
assumption that time_t is the same as long.

4 years agoremove x32 syscall timespec fixup hacks
Rich Felker [Sun, 28 Jul 2019 23:29:28 +0000 (19:29 -0400)]
remove x32 syscall timespec fixup hacks

the x32 syscall interfaces treat timespec's tv_nsec member as 64-bit
despite the API type being long and long being 32-bit in the ABI. this
is no problem for syscalls that store timespecs to userspace as
results, but caused uninitialized padding to be misinterpreted as the
high bits in syscalls that take timespecs as input.

since the beginning of the port, we've dealt with this situation with
hacks in syscall_arch.h, and injected between __syscall_cp_c and
__syscall_cp_asm, to special-case the syscall numbers that involve
timespecs as inputs and copy them to a form suitable to pass to the
kernel.

commit 40aa18d55ab763e69ad16d0cf1cebea708ffde47 set the stage for
removal of these hacks by letting us treat the "normal" x32 syscalls
dealing with timespec as if they're x32's "time64" syscalls,
effectively making x32 ax "time64-only 32-bit arch" like riscv32 will
be when it's added. since then, all users of syscalls that x32's
syscall_arch.h had hacks for have been updated to use time64 syscalls,
so the hacks can be removed.

there are still at least a few other timespec-related syscalls broken
on x32, which were overlooked when the x32 hacks were done or added
later. these include at least recvmmsg, adjtimex/clock_adjtime, and
timerfd_settime, and they will be fixed independently later on.

4 years agoutimensat: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 22:51:20 +0000 (18:51 -0400)]
utimensat: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if either of the requested times does not fit in 32 bits. care is
taken to normalize the inputs to account for UTIME_NOW or UTIME_OMIT
in tv_nsec, in which case tv_sec should be ignored. this is needed not
only to avoid spurious time64 syscalls that might waste time failing
with ENOSYS, but also to accurately decide whether fallback is
possible.

if the requested time cannot be represented, the function fails with
ENOTSUP, defined in general as "The implementation does not support
the requested feature or value". neither the time64 syscall, nor this
error, can happen on current 32-bit archs where time_t is a 32-bit
type, and both are statically unreachable.

on 64-bit archs, there are only superficial changes to the
SYS_futimesat fallback path, which has been modified to pass long[4]
instead of struct timeval[2] to the kernel, making it suitable for use
on 32-bit archs even once time_t is changed to 64-bit. for 32-bit
archs, the call to SYS_utimensat has also been changed to copy the
timespecs through an array of long[4] rather than passing the
timespec[2] in-place.

4 years agoclock_settime: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 22:43:51 +0000 (18:43 -0400)]
clock_settime: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested time does not fit in 32 bits. on current 32-bit
archs where time_t is a 32-bit type, this makes it statically
unreachable.

if the time64 syscall is needed because the requested time does not
fit in 32 bits, we define this as an error ENOTSUP, for "The
implementation does not support the requested feature or value".

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the time is moved through an intermediate
copy to remove the assumption that time_t is a 32-bit type.

4 years agotimer_settime: add support for time64 syscall, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 22:38:28 +0000 (18:38 -0400)]
timer_settime: add support for time64 syscall, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
if either component of the itimerspec does not fit in 32 bits, or if
time_t is 64-bit and the caller requested the old value, in which case
there's a possibility that the old value might not fit in 32 bits. on
current 32-bit archs where time_t is a 32-bit type, this makes it
statically unreachable.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the time is moved through an intermediate
copy to remove the assumption that time_t is a 32-bit type.

4 years agopselect, ppoll: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 22:15:22 +0000 (18:15 -0400)]
pselect, ppoll: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested timeout length does not fit in 32 bits. on current
32-bit archs where time_t is a 32-bit type, this makes it statically
unreachable.

on 64-bit archs, there are only superficial changes to the code after
preprocessing. both before and after these changes, these functions
copied their timeout arguments to avoid letting the kernel clobber the
caller's copies. now, the copying also serves to change the type from
userspace timespec to a pair of longs, which makes a difference only
in the 32-bit fallback case, not on 64-bit.

4 years agofutex wait operations: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 21:44:51 +0000 (17:44 -0400)]
futex wait operations: add time64 syscall support, decouple 32-bit time_t

thanks to the original factorization using the __timedwait function,
there are no FUTEX_WAIT calls anywhere else, giving us a single point
of change to make nearly all the timed thread primitives time64-ready.
the one exception is the FUTEX_LOCK_PI command for PI mutex timedlock.
I haven't tried to make these two points share code, since they have
different fallbacks (no non-private fallback needed for PI since PI
was added later) and FUTEX_LOCK_PI isn't a cancellation point (thus
allowing the whole code path to inline into pthread_mutex_timedlock).

as for other changes in this series, the time64 syscall is used only
if it's the only one defined for the arch, or if the requested timeout
does not fit in 32 bits. on current 32-bit archs where time_t is a
32-bit type, this makes it statically unreachable.

on 64-bit archs, there are only superficial changes to the code after
preprocessing. on current 32-bit archs, the time is passed via an
intermediate copy to remove the assumption that time_t is a 32-bit
type.