musl
5 years agofix mistake/cruft in 1.1.20 release notes
Rich Felker [Tue, 4 Sep 2018 23:27:15 +0000 (19:27 -0400)]
fix mistake/cruft in 1.1.20 release notes

this fix was intended to be amended in before release, but somehow got
missed.

5 years agoimplement fexecve in terms of execveat when it exists
Joseph C. Sible [Sun, 2 Sep 2018 17:42:26 +0000 (13:42 -0400)]
implement fexecve in terms of execveat when it exists

This lets fexecve work even when /proc isn't mounted.

5 years agorelease 1.1.20
Rich Felker [Tue, 4 Sep 2018 17:11:44 +0000 (13:11 -0400)]
release 1.1.20

5 years agofix stack-based oob memory clobber in resolver's result sorting
Rich Felker [Sun, 2 Sep 2018 21:08:43 +0000 (17:08 -0400)]
fix stack-based oob memory clobber in resolver's result sorting

commit 4f35eb7591031a1e5ef9828f9304361f282f28b9 introduced this bug.
it is not present in any released versions. inadvertent use of the &
operator on an array into which we're indexing produced arithmetic on
the wrong-type pointer, with undefined behavior.

5 years agoconsistently use _NSIG/8 idiom for kernel sigset size in sigaction
Rich Felker [Sat, 1 Sep 2018 18:14:32 +0000 (14:14 -0400)]
consistently use _NSIG/8 idiom for kernel sigset size in sigaction

this code in sigaction was the only place where sizeof was being
applied to the kernel sigaction's mask member to get the size argument
to pass to the kernel. everywhere else, _NSIG/8 is used for this
purpose.

5 years agoalways terminate by SIGABRT when abort is called
Rich Felker [Sat, 1 Sep 2018 05:54:44 +0000 (01:54 -0400)]
always terminate by SIGABRT when abort is called

Linux makes this surprisingly difficult, but it can be done. the trick
here is using the fact that we control the implementation of sigaction
to prevent changing the disposition of SIGABRT to anything but SIG_DFL
after abort has tried and failed to terminate the process simply by
calling raise(SIGABRT).

5 years agooptimize raise not to make a syscall for getting tid
Rich Felker [Sat, 1 Sep 2018 05:46:44 +0000 (01:46 -0400)]
optimize raise not to make a syscall for getting tid

assuming signals are blocked, which they are here, the tid in the
thread structure is always valid and cannot change out from under us.

5 years agoprevent perror from clobbering stderr's orientation
Rich Felker [Thu, 30 Aug 2018 04:08:16 +0000 (00:08 -0400)]
prevent perror from clobbering stderr's orientation

this requirement is specified by POSIX.

5 years agoprevent psignal/psiginfo from clobbering stderr orientation, errno
Rich Felker [Thu, 30 Aug 2018 04:00:22 +0000 (00:00 -0400)]
prevent psignal/psiginfo from clobbering stderr orientation, errno

these functions are specified to write to stderr but not set its
orientation, presumably so that they can be used in programs operating
stderr in wide mode. also, they are not allowed to clobber errno on
success. save and restore to meet the requirement.

psiginfo is reduced to a think wrapper around psignal, since it
already behaved the same. if we want to add more detailed siginfo
printing at some point this will need refactoring.

5 years agomake vfprintf set stream orientation even for zero-length output
Rich Felker [Thu, 30 Aug 2018 03:53:45 +0000 (23:53 -0400)]
make vfprintf set stream orientation even for zero-length output

if no output is produced, no underlying fwrite will ever be called,
but byte-oriented printf functions are still required to set the
orientation of the stream to byte-oriented. call __towrite explicitly
if the FILE is not already in write mode.

5 years agore-fix vfprintf temporary buffer logic
Rich Felker [Thu, 30 Aug 2018 03:45:43 +0000 (23:45 -0400)]
re-fix vfprintf temporary buffer logic

commit b5a8b28915aad17b6f49ccacd6d3fef3890844d1 setup the write buffer
bound pointers for the temporary buffer manually to fix a buffer
overflow issue, but in doing so, caused vfprintf on unbuffered files
never to call __towrite, thereby failing to set the stream orientation
to byte-oriented, failing to clear any prior read mode, and failing to
produce an error when the stream is not writable.

revert the inline setup of the bounds pointers and instead zero them,
so that the underlying fwrite code will call __towrite to set them up.

5 years agofix missing flush of stderr at exit if it was put in buffered mode
Rich Felker [Thu, 30 Aug 2018 03:40:49 +0000 (23:40 -0400)]
fix missing flush of stderr at exit if it was put in buffered mode

commit 0b80a7b0404b6e49b0b724e3e3fe0ed5af3b08ef added the ability to
set application-provided stdio FILE buffers, adding the possibility
that stderr might be buffered at exit time, but __stdio_exit did not
have code to flush it.

this regression was not present in any release.

5 years agofix async thread cancellation on sh-fdpic
Rich Felker [Wed, 29 Aug 2018 18:01:46 +0000 (14:01 -0400)]
fix async thread cancellation on sh-fdpic

if __cp_cancel was reached via __syscall_cp, r12 will necessarily
still contain a GOT pointer (for libc.so or for the static-linked main
program) valid for entering __cancel. however, in the case of async
cancellation, r12 may contain any scratch value; it's not necessarily
even a valid GOT pointer for the code that was interrupted.

unlike in commit 0ec49dab6794166d67fae4764ce7fdea42ea6103 where the
corresponding issue was fixed for powerpc64, there is fundamentally no
way for fdpic code to recompute its GOT pointer. so a new mechanism is
introduced for cancel_handler to write a GOT register value into the
interrupted context on archs where it is needed.

5 years agofix async thread cancellation on powerpc64
Rich Felker [Wed, 29 Aug 2018 16:48:42 +0000 (12:48 -0400)]
fix async thread cancellation on powerpc64

entering the local entry point for __cancel from __cp_cancel is valid
if __cp_cancel was reached from __syscall_cp, since both are in libc
and share the same TOC pointer, but it is not valid if __cp_cancel was
reached when cancel_handler rewrote the program counter for
asynchronous cancellation of code outside libc.

to ensure __cancel is entered with a valid TOC pointer, recompute the
correct value in a PC-relative manner before jumping.

5 years agovfwprintf: honor field width with 'c' format type
A. Wilcox [Thu, 23 Aug 2018 13:06:12 +0000 (08:06 -0500)]
vfwprintf: honor field width with 'c' format type

5 years agofix several values reported by sysconf
Rich Felker [Wed, 29 Aug 2018 01:22:48 +0000 (21:22 -0400)]
fix several values reported by sysconf

- REALTIME_SIGNALS is supposed to be version-valued
- DELAYTIMER_MAX was wrongly using the min allowed max
- unavailable compilation environments wrongly used 0 instead of -1

5 years agofix return value of system on failure to spawn child process
Rich Felker [Wed, 29 Aug 2018 00:39:26 +0000 (20:39 -0400)]
fix return value of system on failure to spawn child process

the value 0x7f00 (as if by _exit(127)) is specified only for the case
where the child is created but then fails to exec the shell, since
traditional fork+exec implementations do not admit reporting an error
via errno in this case without additional machinery. it's unclear
whether an implementation not subject to this failure mode needs to
emulate it; one could read the standard as requiring that. if so,
additional code will need to be added to map posix_spawn errors into
the form system is expected to return. but for now, returning -1 to
indicate an error is significantly better behavior than always
reporting failures as if the shell failed to exec after fork.

5 years agoset stream orientations in open_[w]memstream
Rich Felker [Tue, 28 Aug 2018 23:22:13 +0000 (19:22 -0400)]
set stream orientations in open_[w]memstream

fundamentally there is no good reason these functions need to set an
orientation (morally it should be possible to write a wchar_t[] memory
stream using byte functions, or a char[] memory stream using wide
functions), but it's a part of the specification that they do. aside
from being able to inspect the orientation with fwide, failure to set
the orientation in open_wmemstream is observable if the locale changes
between open_wmemstream and the first operation on the stream; this is
because the encoding rule (locale) for the stream is required to be
bound at the time the stream becomes wide-oriented.

for open_wmemstream, call fwide to avoid duplicating the logic for
binding the encoding rule. for open_memstream it suffices just to set
the mode field in the FILE struct.

5 years agomake fmemopen's w+ mode truncate the buffer
Rich Felker [Tue, 28 Aug 2018 23:16:40 +0000 (19:16 -0400)]
make fmemopen's w+ mode truncate the buffer

the w+ mode is specified to "truncate the buffer contents". like most
of fmemopen, exactly what this means is underspecified. mode w and w+
of course implicitly 'truncate' the buffer if a write from the initial
position is flushed, so in order for this part of the text about w+
not to be spurious, it should be interpreted as requiring something
else, and the obvious reasonable interpretation is that the truncation
is immediately visible if you attempt to read from the stream or the
buffer before writing/flushing.

this interpretation agrees with reported conformance test failures.

5 years agoset errno when fileno is called on a FILE with no underlying fd
Rich Felker [Tue, 28 Aug 2018 22:40:15 +0000 (18:40 -0400)]
set errno when fileno is called on a FILE with no underlying fd

this is a POSIX requirement.

also remove the gratuitous locking shenanigans and simply access f->fd
under control of the lock. there is no advantage to not doing so, and
it made the correctness non-obvious at best.

5 years agoreject invalid arguments to pthread_barrierattr_setpshared
Rich Felker [Tue, 28 Aug 2018 22:12:17 +0000 (18:12 -0400)]
reject invalid arguments to pthread_barrierattr_setpshared

this is a POSIX requirement.

5 years agorewrite __aeabi_read_tp in asm
Szabolcs Nagy [Fri, 24 Aug 2018 23:11:59 +0000 (23:11 +0000)]
rewrite __aeabi_read_tp in asm

__aeabi_read_tp used to call c code, but that was incorrect as the
arm runtime abi specifies special pcs for this function: it is only
allowed to clobber r0, ip, lr and cpsr.

maintainer's note: the old code explicitly saved and restored all
general-purpose registers which are call-clobbered in the normal
calling convention, so it's unlikely that any real-world compilers
produced code that could break. however theoretically they could have
chosen to use floating point registers, in which case the caller's
values of those registers would be clobbered.

5 years agofix dubious char signedness check in limits.h
Rich Felker [Tue, 28 Aug 2018 17:54:50 +0000 (13:54 -0400)]
fix dubious char signedness check in limits.h

commit 201995f382cc698ae19289623cc06a70048ffe7b introduced a hack
utilizing the signedness of character constants at the preprocessor
level to avoid depending on the gcc-specific __CHAR_UNSIGNED__ predef.
while this trick works on gcc and presumably other compilers being
used, it's not clear that the behavior it depends on is actually
conforming. C11 6.4.4.4 ¶10 defines character constants as having type
int, and 6.10.1 ¶4 defines preprocessor #if arithmetic to take place
in intmax_t or uintmax_t, depending on the signedness of the integer
operand types, and it is specified that "this includes interpreting
character constants".

if character literals had type char and just promoted to int, it would
be clear that when char is unsigned they should behave as uintmax_t at
the preprocessor level. however, as written the text of the standard
seems to require that character constants always behave as intmax_t,
corresponding to int, at the preprocessor level.

since there is a good deal of ambiguity about the correct behavior and
a risk that compilers will disagree or that an interpretation may
mandate a change in the behavior, do not rely on it for defining
CHAR_MIN and CHAR_MAX correctly. instead, use the signedness of the
value (as opposed to the type) of '\xff', which will be positive if
and only if plain char is unsigned. this behavior is clearly
specified, and the specific case '\xff' is even used in an example,
under 6.4.4.4 of the standard.

5 years agofix deadlock in async thread self-cancellation
Rich Felker [Tue, 28 Aug 2018 16:45:44 +0000 (12:45 -0400)]
fix deadlock in async thread self-cancellation

with async cancellation enabled, pthread_cancel(pthread_self())
deadlocked due to pthread_kill holding killlock which is needed by
pthread_exit.

this could be solved by making pthread_kill block signals around the
critical section, at least when the target thread is itself, but the
issue only arises for cancellation, and otherwise would just be
imposing unnecessary cost.

instead just have pthread_cancel explicitly check for async
self-cancellation and call pthread_exit(PTHREAD_CANCELED) directly
rather than going through the signal machinery.

5 years agotime: fix incorrect DST offset when using POSIX timezones without DST
A. Wilcox [Mon, 27 Aug 2018 23:17:20 +0000 (18:17 -0500)]
time: fix incorrect DST offset when using POSIX timezones without DST

This manifests itself in mktime if tm_isdst = 1 and the current TZ= is
a POSIX timezone specification. mktime would see that tm_isdst was set
to 0 by __secs_to_zone, and subtract 'oppoff' (dst_off) - gmtoff from
the resultant time. This meant that mktime returned a time that was
exactly double the GMT offset of the desired timezone when tm_isdst
was = 1.

5 years agofix tls access on arm targets before armv6k
Szabolcs Nagy [Thu, 23 Aug 2018 10:57:34 +0000 (10:57 +0000)]
fix tls access on arm targets before armv6k

commit 610c5a8524c3d6cd3ac5a5f1231422e7648a3791 changed the thread
pointer setup so tp points at the end of the pthread struct on arm,
but failed to update __aeabi_read_tp so it was off by 8.

this broke tls access in code that is compiled with -mtp=soft, which
is the default when target arch is pre armv6k or thumb1.

maintainer's note: no release versions are affected.

5 years agofix missing strerror text for EMULTIHOP
Rich Felker [Thu, 23 Aug 2018 19:56:52 +0000 (15:56 -0400)]
fix missing strerror text for EMULTIHOP

this is an obsolete error code from RFS, an obsolete predecessor of
NFS. POSIX documents it only as "Reserved", but maintains the
requirement that it be defined. as long as it is defined, it needs a
string for strerror to produce; the one chosen matches glibc and
documentation from other language runtimes I could find.

5 years agofix printf precision specifier for hex floats on non-ld80 archs
Rich Felker [Thu, 23 Aug 2018 19:24:03 +0000 (15:24 -0400)]
fix printf precision specifier for hex floats on non-ld80 archs

the code to perform rounding to the desired precision wrongly assumed
the long double mantissa was an integral number of nibbles (hex
digits) in length. this is true for 80-bit extended precision (64-bit
mantissa) but not for double (53) or quad (113).

scale the rounding value by 1<<(LDBL_MANT_DIG%4) to compensate.

5 years agogetopt: update optarg and optind correctly on missing argument
Rich Felker [Wed, 22 Aug 2018 23:29:56 +0000 (19:29 -0400)]
getopt: update optarg and optind correctly on missing argument

the text of the specification for getopt's handling of options that
require an argument, which requires updating optarg and optind, does
not exclude the error case where the end of the argument list has been
reached. in that case, it is expected that optarg be assigned
argv[argc] (normally null) and optind be incremented by 2, resulting
in a value of argc+1.

5 years agofix FP_ILOGB0 and FP_ILOGBNAN definitions to be valid for use in #if
Rich Felker [Wed, 22 Aug 2018 23:11:58 +0000 (19:11 -0400)]
fix FP_ILOGB0 and FP_ILOGBNAN definitions to be valid for use in #if

commit 98c9af500125df41fdb46d7e384b00982d72493a wrongly claimed they
do not need to be valid for such usage, but the last sentence of C11
7.1.4 ¶1 imposes a broad requirement that all macros specified as
integer constant expressions also need to be valid for #if.

simply write the value out explicitly. there is no value here in
pretending that the width of int will vary.

5 years agoremove erroneous SYMLINK_MAX definition from limits.h, pathconf
Rich Felker [Tue, 21 Aug 2018 00:20:31 +0000 (20:20 -0400)]
remove erroneous SYMLINK_MAX definition from limits.h, pathconf

POSIX requires the symlink function to fail with ENAMETOOLONG if the
link contents to be written exceed SYMLINK_MAX in length, but neither
Linux nor our syscall wrapper code enforce this. the value 255 for
SYMLINK_MAX is not meaningful and does not seem to have been motivated
by anything except perhaps a wrong assumption that a definition was
mandatory. it has been present (though moving through bits to
top-level limits.h) since the beginning of the project history.

[f]pathconf is entitled to return -1 as the limit for conf names for
which there is no hard limit, with the usual POSIX note that an
indefinite limit does not imply an infinite limit. in principle we
might should report a limit for filesystems that impose one, but such
functionality is not currently present for any of the pathconf limits,
and adding it is beyond the scope of fixing the incorrect limit.

5 years agomips archs: fix runaway execution if start fn passed to clone returns
Segev Finer [Wed, 15 Aug 2018 17:46:18 +0000 (20:46 +0300)]
mips archs: fix runaway execution if start fn passed to clone returns

Call SYS_exit on return from fn in __clone. This is the expected
behavior of this function. Without this the child task will crash on
return from fn, since it will return to nowhere.

5 years agofix pthread_create return value with PTHREAD_EXPLICIT_SCHED
Rich Felker [Thu, 16 Aug 2018 16:16:36 +0000 (12:16 -0400)]
fix pthread_create return value with PTHREAD_EXPLICIT_SCHED

due to moved code, commit b8742f32602add243ee2ce74d804015463726899
inadvertently used the return value of __clone, rather than the return
value of SYS_sched_setscheduler in the new thread, to check whether it
needed to report failure. since a successful __clone returns the tid
of the new thread, which is never zero, this caused pthread_create
always to return with an invalid error number in the code path for
PTHREAD_EXPLICIT_SCHED.

this regression was not present in any releases.

5 years agofix sign of strftime %z output with offsets <1 hour west of UTC
Rich Felker [Wed, 8 Aug 2018 02:15:04 +0000 (22:15 -0400)]
fix sign of strftime %z output with offsets <1 hour west of UTC

the sign character produced came from the sign of tm_gmtoff/3600 as an
integer division, which is zero for negative offsets smaller in
magnitude than 3600. instead of printing the hours and minutes as
separate fields, print them as a single value of the form
hours*100+minutes, which naturally has the correct sign.

5 years agofix musl-gcc wrapper to be compatible with default-pie gcc toolchains
Rich Felker [Thu, 2 Aug 2018 23:15:48 +0000 (19:15 -0400)]
fix musl-gcc wrapper to be compatible with default-pie gcc toolchains

the specfile for the wrapper was written assuming output is pie only
if -pie appears on the command line. recent (and older patched)
versions of gcc can be configured to produce pie output by default,
adn when used with such a toolchain, the wrapper linked the wrong
startfiles (crt*) containing pic-incompatible code.

rather than trying to figure out gcc's default, simply always use the
pic-compatible start files.

5 years agomake pthread_attr_init honor defaults set by pthread_setattr_default_np
Rich Felker [Fri, 27 Jul 2018 16:05:00 +0000 (12:05 -0400)]
make pthread_attr_init honor defaults set by pthread_setattr_default_np

this fixes a major gap in the intended functionality of
pthread_setattr_default_np. if application/library code creating a
thread does not pass a null attribute pointer to pthread_create, but
sets up an attribute object to change other properties while leaving
the stack alone, the created thread will get a stack with size
DEFAULT_STACK_SIZE. this makes pthread_setattr_default_np useless for
working around stack overflow issues in such applications, and leaves
a major risk of regression if previously-working code switches from
using a null attribute pointer to an attribute object.

this change aligns the behavior more closely with the glibc
pthread_setattr_default_np functionality too, albeit via a different
mechanism. glibc encodes "default" specially in the attribute object
and reads the actual default at thread creation time. with this
commit, we now copy the current default into the attribute object at
pthread_attr_init time, so that applications that query the properties
of the attribute object will see the right values.

5 years agobsearch: simplify and optimize
Fangrui Song [Sun, 22 Jul 2018 00:34:00 +0000 (17:34 -0700)]
bsearch: simplify and optimize

maintainer's note: the key observation here is that the compared
element is the first slot of the second ceil(half) of the array, and
thus can be removed for further comparison when it does not match, so
that we descend into the second ceil(half)-1 rather than ceil(half)
elements. this change ensures that nel strictly decreases with each
iteration, so that the case of != but nel==1 does not need to be
special-cased anymore.

5 years agomove inclusion of linux headers for kd.h, soundcard.h, vt.h to bits
midipix [Sun, 15 Jul 2018 02:51:22 +0000 (22:51 -0400)]
move inclusion of linux headers for kd.h, soundcard.h, vt.h to bits

maintainer's note: while musl does not use the linux kernel headers,
it does provide these three sys/* headers which do nothing but include
the corresponding linux/* headers, since the sys/* versions are the
ones documented for application use (and they arguably provide
interfaces that are not linux-specific but common to other unices).
these headers should probably not be provided by libc (rather by a
separate package), but as long as they are, use the bits header
framework as an aid to out-of-tree ports of musl for non-linux systems
that want to implement them in some other way.

5 years agoremove inclusion guard hacks for sys/kd.h
midipix [Sun, 15 Jul 2018 02:49:06 +0000 (22:49 -0400)]
remove inclusion guard hacks for sys/kd.h

maintainer's note: at some point, probably long before linux separated
the uapi headers, it was the case, or at least I believed it was the
case, that linux/types.h was unsafe to include from userspace. thus,
the inclusion guard macro _LINUX_TYPES_H was defined in sys/kd.h to
prevent linux/kd.h from including linux/types.h (which it spuriously
includes but does not use). as far as I can tell, whatever problem
this was meant to solve does not seem to have been present for a long
time, and the hack was not done correctly anyway, so removing it is
the right thing to do.

5 years agoremove spurious declaration of __getdents from readdir.c
Rich Felker [Wed, 18 Jul 2018 17:35:35 +0000 (13:35 -0400)]
remove spurious declaration of __getdents from readdir.c

5 years agofix regression in alignment of dirent structs produced by readdir
Rich Felker [Wed, 18 Jul 2018 17:25:48 +0000 (13:25 -0400)]
fix regression in alignment of dirent structs produced by readdir

commit 32482f61da7650ff10741bd5aedd66bbc3ea165b reduced the number of
int members before the dirent buf from 4 to 3, thereby misaligning it
mod sizeof(off_t), producing invalid accesses on any arch where
alignof(off_t)==sizeof(off_t).

rather than re-adding wasted padding, reorder the struct to meet the
requirement and add a comment and static assertion to prevent this
from getting broken again.

5 years agoadd support for arch-specific ptrace command macros
Szabolcs Nagy [Sun, 8 Jul 2018 13:16:54 +0000 (15:16 +0200)]
add support for arch-specific ptrace command macros

sys/ptrace.h is target specific, use bits/ptrace.h to add target
specific macro definitions.

these macros are kept in the generic sys/ptrace.h even though some
targets don't support them:

PTRACE_GETREGS
PTRACE_SETREGS
PTRACE_GETFPREGS
PTRACE_SETFPREGS
PTRACE_GETFPXREGS
PTRACE_SETFPXREGS

so no macro definition got removed in this patch on any target. only
s390x has a numerically conflicting macro definition (PTRACE_SINGLEBLOCK).

the PT_ aliases follow glibc headers, otherwise the definitions come
from linux uapi headers except ones that are skipped in glibc and
there is no real kernel support (s390x PTRACE_*_AREA) or need special
type definitions (mips PTRACE_*_WATCH_*) or only relevant for linux
2.4 compatibility (PTRACE_OLDSETOPTIONS).

5 years agosys/ptrace.h: add missing PTRACE_EVENT_STOP
Szabolcs Nagy [Sun, 8 Jul 2018 13:15:06 +0000 (15:15 +0200)]
sys/ptrace.h: add missing PTRACE_EVENT_STOP

new in linux v3.1 commit 3544d72a0e10d0aa1c1bd59ed77a53a59cdc12f7
changed in linux v3.4 commit 5cdf389aee90109e2e3d88085dea4dd5508a3be7

A tracer recieves this event in the waitpid status of a PTRACED_SEIZED
process.

5 years agouchar.h: define char16_t and char32_t for old c++
Szabolcs Nagy [Sun, 8 Jul 2018 13:19:03 +0000 (15:19 +0200)]
uchar.h: define char16_t and char32_t for old c++

including uchar.h in c++ code is only well defined in c++11 onwards
where char16_t and char32_t type definitions must be hidden since they
are keywords.  however some c++ code compiled for older c++ standard
include uchar.h too and they need the typedefs, this fix makes such
code work.

5 years agoblock dlopen of libraries with initial-exec refs to dynamic TLS
Rich Felker [Mon, 16 Jul 2018 16:32:57 +0000 (12:32 -0400)]
block dlopen of libraries with initial-exec refs to dynamic TLS

previously, this operation succeeded, and the relocation results
worked for access from new threads created after dlopen, but produced
invalid accesses (and possibly clobbered other memory) from threads
that already existed.

the way the check is written, it still permits dlopen of libraries
containing initial-exec references to static TLS (TLS in the main
program or in a dynamic library loaded at startup).

5 years agofix inefficient choice of tlsdesc function due to off-by-one
Rich Felker [Mon, 16 Jul 2018 16:29:14 +0000 (12:29 -0400)]
fix inefficient choice of tlsdesc function due to off-by-one

tls_id is one-based, whereas [static_]tls_cnt is a count, so
comparison for checking that a given tls_id is dynamic rather than
static needs to use strict inequality.

5 years agoimplement getaddrinfo's AI_ADDRCONFIG flag
Rich Felker [Sun, 15 Jul 2018 00:20:19 +0000 (20:20 -0400)]
implement getaddrinfo's AI_ADDRCONFIG flag

this flag is notoriously under-/mis-specified, and in the past it was
implemented as a nop, essentially considering the absence of a
loopback interface with 127.0.0.1 and ::1 addresses an unsupported
configuration. however, common real-world container environments omit
IPv6 support (even for the network-namespaced loopback interface), and
some kernels omit IPv6 support entirely. future systems on the other
hand might omit IPv4 entirely.

treat these as supported configurations and suppress results of the
unconfigured/unsupported address families when AI_ADDRCONFIG is
requested. use routability of the loopback address to make the
determination; unlike other implementations, we do not exclude
loopback from the "an address is configured" condition, since there is
no basis in the specification for such exclusion. obtaining a result
with AI_ADDRCONFIG does not imply routability of the result, and
applications must still be able to cope with unroutable results even
if they pass AI_ADDRCONFIG.

5 years agofix writes outside buffer by ungetc after setvbuf
Rich Felker [Sat, 14 Jul 2018 01:56:27 +0000 (21:56 -0400)]
fix writes outside buffer by ungetc after setvbuf

commit 0b80a7b0404b6e49b0b724e3e3fe0ed5af3b08ef, which added non-stub
setvbuf, applied the UNGET pushback adjustment to the size of the
buffer passed in, but inadvertently omitted offsetting the start by
the same amount, thereby allowing unget to clobber up to 8 bytes
before the start of the buffer. this bug was introduced in the present
release cycle; no releases are affected.

5 years agoadd ST_RELATIME to statvfs.h
Rich Felker [Fri, 13 Jul 2018 00:41:30 +0000 (20:41 -0400)]
add ST_RELATIME to statvfs.h

5 years agoresolver: don't depend on v4mapped ipv6 to probe routability of v4 addrs
Rich Felker [Wed, 11 Jul 2018 19:03:34 +0000 (15:03 -0400)]
resolver: don't depend on v4mapped ipv6 to probe routability of v4 addrs

to produce sorted results roughly corresponding to RFC 3484/6724,
__lookup_name computes routability and choice of source address via
dummy UDP connect operations (which do not produce any packets). since
at the logical level, the properties fed into the sort key are
computed on ipv6 addresses, the code was written to use the v4mapped
ipv6 form of ipv4 addresses and share a common code path for them all.
however, on kernels where ipv6 support has been completely omitted,
this causes ipv4 to appear equally unroutable as ipv6, thereby putting
unreachable ipv6 addresses before ipv4 addresses in the results.

instead, use only ipv4 sockets to compute routability for ipv4
addresses. some gratuitous conversion back and forth is left so that
the logic is not affected by these changes. it may be possible to
simplify the ipv4 case considerably, thereby reducing code size and
complexity.

5 years agooptimize explicit_bzero for size
Alexander Monakov [Thu, 28 Jun 2018 17:57:29 +0000 (20:57 +0300)]
optimize explicit_bzero for size

Avoid saving/restoring the incoming argument by reusing memset return
value.

5 years agoavoid spurious dso matches by dladdr outside bounds of load segments
Rich Felker [Thu, 28 Jun 2018 16:20:58 +0000 (12:20 -0400)]
avoid spurious dso matches by dladdr outside bounds of load segments

since slack space at the beginning and/or end of writable load maps is
donated to malloc, the application could obtain valid pointers in
these ranges which dladdr would erroneously identify as part of the
shared object whose mapping they came from.

instead of checking the queried address against the mapping base and
length, check it against the load segments from the program headers,
and only match the dso if it lies within the bounds of one of them.

as a shortcut, if the address does match the range of the mapping but
not any of the load segments, we know it cannot match any other dso
and can immediately return failure.

5 years agomake dladdr consistently produce the first symbol in presence of aliases
Rich Felker [Thu, 28 Jun 2018 16:07:51 +0000 (12:07 -0400)]
make dladdr consistently produce the first symbol in presence of aliases

the early-exit condition for the symbol match loop on exact matches
caused dladdr to produce the first match for an exact match, but the
last match for an inexact match. in the interest of consistency,
require a strictly-closer match to replace an already-found one.

5 years agofix symtab-order-dependent spurious matches in dladdr
Rich Felker [Thu, 28 Jun 2018 15:51:43 +0000 (11:51 -0400)]
fix symtab-order-dependent spurious matches in dladdr

commit 8b8fb7f03721c42445f982582f462144ab60a1a0 added logic to prevent
matching a symbol with no recorded size (closest-match) when there is
an intervening symbol whose size was recorded, but it only worked when
the intervening symbol was encountered later in the search.

instead of rejecting symbols where addr falls outside their recorded
size during the closest-match search, accept them to find the true
closest-match, then reject such a result only once the search has
finished.

5 years agocorrectly handle non-matching symbols in dladdr
Rich Felker [Wed, 27 Jun 2018 19:32:09 +0000 (15:32 -0400)]
correctly handle non-matching symbols in dladdr

based on patch by Axel Siebenborn, with fixes discussed on the mailing
list after submission and and rebased around the UB fix in commit
e829695fcc880f8578c2b964ea2d090f0016c9d7.

avoid spurious symbol matches by dladdr beyond symbol size. for
symbols with a size recorded, only match if the queried address lies
within the address range determined by the symbol address and size.
for symbols with no size recorded, the old closest-match behavior is
kept, as long as there is no intervening symbol with a recorded size.

the case where no symbol is matched, but the address does lie within
the memory range of a shared object, is specified as success. fix the
return value and produce a valid (with null dli_sname and dli_saddr)
Dl_info structure.

5 years agoavoid using undefined pointer arithmetic in dladdr
Rich Felker [Wed, 27 Jun 2018 19:29:12 +0000 (15:29 -0400)]
avoid using undefined pointer arithmetic in dladdr

5 years agofix missing timeout argument to futex syscall in __futexwait
Patrick Oppenlander [Fri, 1 Jun 2018 00:49:20 +0000 (10:49 +1000)]
fix missing timeout argument to futex syscall in __futexwait

5 years agoadd explicit_bzero implementation
David Carlier [Fri, 15 Jun 2018 13:30:09 +0000 (13:30 +0000)]
add explicit_bzero implementation

maintainer's note: past sentiment was that, despite being imperfect
and unable to force clearing of all possible copies of sensitive data
(e.g. in registers, register spills, signal contexts left on the
stack, etc.) this function would be added if major implementations
agreed on it, which has happened -- several BSDs and glibc all include
it.

5 years agoinet_ntop: do not compress single zeros in IPv6
Arthur Jones [Tue, 5 Jun 2018 23:51:27 +0000 (16:51 -0700)]
inet_ntop: do not compress single zeros in IPv6

maintainer's note: this change is for conformance with RFC 5952,
4.2.2, which explicitly forbids use of :: to shorten a single 16-bit 0
field when producing the canonical text representation for an IPv6
address. fixes a test failure reported by Philip Homburg, who also
submitted a patch, but this fix is simpler and should produce smaller
code.

5 years agostrftime: fix underlying format string in %z format
Daniel Sabogal [Mon, 11 Jun 2018 17:15:15 +0000 (13:15 -0400)]
strftime: fix underlying format string in %z format

the expression (tm->__tm_gmtoff)/3600 has type long. use %+.2ld instead.

5 years agoresolver: omit final dot (root/suppress-search) in canonical name
Rich Felker [Tue, 26 Jun 2018 20:17:05 +0000 (16:17 -0400)]
resolver: omit final dot (root/suppress-search) in canonical name

if a final dot was included in the queried host name to anchor it to
the dns root/suppress search domains, and the result was not a CNAME,
the returned canonical name included the final dot. this was not
consistent with other implementations, confused some applications, and
does not seem desirable.

POSIX specifies returning a pointer to, or to a copy of, the input
nodename, when the canonical name is not available, but does not
attempt to specify what constitutes "not available". in the case of
search, we already have an implementation-defined "availability" of a
canonical name as the fully-qualified name resulting from search, so
defining it similarly in the no-search case seems reasonable in
addition to being consistent with other implementations.

as a bonus, fix the case where more than one trailing dot is included,
since otherwise the changes made here would wrongly cause lookups with
two trailing dots to succeed. previously this case resulted in
malformed dns queries and produced EAI_AGAIN after a timeout. now it
fails immediately with EAI_NONAME.

5 years agofix regression in powerpc[64] SO_PEERSEC definition
Rich Felker [Tue, 26 Jun 2018 20:28:49 +0000 (16:28 -0400)]
fix regression in powerpc[64] SO_PEERSEC definition

commit 587f5a53bc3a68d80b239ba515d583df690a96df moved the definition
of SO_PEERSEC to bits/socket.h for archs where the SO_* macros differ
from their standard values, but failed to add copies of the generic
definition for powerpc and powerpc64.

5 years agofix value of SO_PEERSEC on mips archs
Rich Felker [Tue, 26 Jun 2018 19:55:29 +0000 (15:55 -0400)]
fix value of SO_PEERSEC on mips archs

adapted from patch by Matthias Schiffer.

5 years agoadd m68k reg.h and user.h
Rich Felker [Tue, 26 Jun 2018 16:30:23 +0000 (12:30 -0400)]
add m68k reg.h and user.h

5 years agofix dynamic linker mapping/clearing bss in first/only LOAD segment
Rich Felker [Tue, 26 Jun 2018 16:15:13 +0000 (12:15 -0400)]
fix dynamic linker mapping/clearing bss in first/only LOAD segment

writable load segments can have size-in-memory larger than their size
in the ELF file, representing bss or equivalent. the initial partial
page has to be zero-filled, and additional anonymous pages have to be
mapped such that accesses don't failt with SIGBUS.

map_library skips redundant MAP_FIXED mapping of the initial
(lowest-address) segment when processing LOAD segments since it was
already mapped when reserving the virtual address range, but in doing
so, inadvertently also skipped the code to fill/map bss. typical
executable and library files have two or more LOAD segments, and the
first one is text/rodata (non-writable) and thus has no bss, but it is
syntactically valid for an ELF program/library to put its writable
segment first, or to have only one segment (everything writable). the
binutils bfd-based linker has been observed to create such programs in
the presence of unusual sections or linker scripts.

fix by moving only the mmap_fixed operation under the conditional
rather than skipping the remainder of the loop body. add a check to
avoid bss processing in the case where the segment is not writable;
this should not happen, but if it does, the change would be a crashing
regression without this check.

5 years agoadd memfd_create syscall wrapper
Szabolcs Nagy [Tue, 19 Jun 2018 20:28:03 +0000 (20:28 +0000)]
add memfd_create syscall wrapper

memfd_create was added in linux v3.17 and glibc has api for it.

5 years agoadd mlock2 linux syscall wrapper
Szabolcs Nagy [Sat, 28 Apr 2018 17:25:41 +0000 (17:25 +0000)]
add mlock2 linux syscall wrapper

mlock2 syscall was added in linux v4.4 and glibc has api for it.
It falls back to mlock in case of flags==0, so that case works
even on older kernels.

MLOCK_ONFAULT is moved under _GNU_SOURCE following glibc.

5 years agofix m68k float.h long double exponent range
Rich Felker [Wed, 20 Jun 2018 19:28:49 +0000 (15:28 -0400)]
fix m68k float.h long double exponent range

unlike the x86 variant, the m68k ld80 format allows (biased) exponent
zero with mantissa msb set, thereby extending the normal range.

5 years agowork around broken kernel struct ipc_perm on some big endian archs
Rich Felker [Wed, 20 Jun 2018 04:07:09 +0000 (00:07 -0400)]
work around broken kernel struct ipc_perm on some big endian archs

the mode member of struct ipc_perm is specified by POSIX to have type
mode_t, which is uniformly defined as unsigned int. however, Linux
defines it with type __kernel_mode_t, and defines __kernel_mode_t as
unsigned short on some archs. since there is a subsequent padding
field, treating it as a 32-bit unsigned int works on little endian
archs, but the order is backwards on big endian archs with the
erroneous definition.

since multiple archs are affected, remedy the situation with fixup
code in the affected functions (shmctl, semctl, and msgctl) rather
than repeating the same shims in syscall_arch.h for every affected
arch.

5 years agos390x: add kexec_file_load syscall number from linux v4.17
Szabolcs Nagy [Sat, 9 Jun 2018 23:03:48 +0000 (23:03 +0000)]
s390x: add kexec_file_load syscall number from linux v4.17

new in linux commit 71406883fd35794d573b3085433c41d0a3bf6c21

5 years agomips: add HWCAP_ flags from linux v4.17
Szabolcs Nagy [Sat, 9 Jun 2018 22:58:19 +0000 (22:58 +0000)]
mips: add HWCAP_ flags from linux v4.17

new in linux commit 256211f2b0b251e532d1899b115e374feb16fa7a

5 years agoaarch64: add HWCAP_ flags from linux v4.17
Szabolcs Nagy [Sat, 9 Jun 2018 22:51:08 +0000 (22:51 +0000)]
aarch64: add HWCAP_ flags from linux v4.17

hwcaps for armv8.4, new in linux commit
7206dc93a58fb76421c4411eefa3c003337bcb2d

5 years agoadd speculation control prctls from linux v4.17
Szabolcs Nagy [Sat, 9 Jun 2018 21:23:54 +0000 (21:23 +0000)]
add speculation control prctls from linux v4.17

PR_{SET,GET}_SPECULATION_CTRL controls speculation related vulnerability
mitigations, new in commits
b617cfc858161140d69cc0b5cc211996b557a1c7
356e4bfff2c5489e016fdb925adbf12a1e3950ee

5 years agoadd ETH_P_PREAUTH ethertype from linux v4.17
Szabolcs Nagy [Sat, 9 Jun 2018 21:20:45 +0000 (21:20 +0000)]
add ETH_P_PREAUTH ethertype from linux v4.17

added in linux commit 4fe0de5b143762d327bfaf1d7be7c5b58041a18c

5 years agoadd TCP_NLA_* from linux v4.17
Szabolcs Nagy [Sat, 9 Jun 2018 21:15:13 +0000 (21:15 +0000)]
add TCP_NLA_* from linux v4.17

new and missing netlink attributes types for SCM_TIMESTAMPING_OPT_STATS,
new ones were added in commits
7156d194a0772f733865267e7207e0b08f81b02b
be631892948060f44b1ceee3132be1266932071e
87ecc95d81d951b0984f2eb9c5c118cb68d0dce8

5 years agoadd {MSG,SEM,SHM}_STAT_ANY from linux v4.17
Szabolcs Nagy [Sat, 9 Jun 2018 21:06:58 +0000 (21:06 +0000)]
add {MSG,SEM,SHM}_STAT_ANY from linux v4.17

introduced to stat ipc objects without permission checks since the
info is available in /proc/sysvipc anyway, new in linux commits
23c8cec8cf679b10997a512abb1e86f0cedc42ba
a280d6dc77eb6002f269d58cd47c7c7e69b617b6
c21a6970ae727839a2f300cd8dd957de0d0238c3

5 years agoadd MAP_FIXED_NOREPLACE from linux v4.17
Szabolcs Nagy [Sat, 9 Jun 2018 20:39:35 +0000 (20:39 +0000)]
add MAP_FIXED_NOREPLACE from linux v4.17

to map at a fixed address without unmapping underlying mappings
(fails with EEXIST unlike MAP_FIXED), new in linux commits
4ed28639519c7bad5f518e70b3284c6e0763e650 and
a4ff8e8620d3f4f50ac4b41e8067b7d395056843.

5 years agopowerpc: add pkey syscall numbers from linux v4.16
Szabolcs Nagy [Sat, 28 Apr 2018 16:36:40 +0000 (16:36 +0000)]
powerpc: add pkey syscall numbers from linux v4.16

add pkey_mprotect, pkey_alloc, pkey_free syscall numbers,
new in linux commits 3350eb2ea127978319ced883523d828046af4045
and 9499ec1b5e82321829e1c1510bcc37edc20b6f38

5 years agoaarch64: add HWCAP_ASIMDFHM from linux v4.16
Szabolcs Nagy [Sat, 28 Apr 2018 16:29:55 +0000 (16:29 +0000)]
aarch64: add HWCAP_ASIMDFHM from linux v4.16

armv8.4 fp mul instructions.
added in commit 3b3b681097fae73b7f5dcdd42db6cfdf32943d4c

5 years agosys/ptrace.h: add PTRACE_SECCOMP_GET_METADATA from linux v4.16
Szabolcs Nagy [Sat, 28 Apr 2018 16:23:23 +0000 (16:23 +0000)]
sys/ptrace.h: add PTRACE_SECCOMP_GET_METADATA from linux v4.16

to get seccomp state for checkpoint restore.
added in linux commit 26500475ac1b499d8636ff281311d633909f5d20

struct tag follows the glibc api and ptrace_peeksiginfo_args
got changed too accordingly.

5 years agonetinet/if_ether.h: add ETH_TLEN from linux v4.16
Szabolcs Nagy [Sat, 28 Apr 2018 16:16:12 +0000 (16:16 +0000)]
netinet/if_ether.h: add ETH_TLEN from linux v4.16

octets in ethernet type field
added in linux commit 4bbb3e0e8239f9079bf1fe20b3c0cb598714ae61

5 years agonetinet/if_ether.h: add ETH_P_ERSPAN2 from linux v4.16
Szabolcs Nagy [Sat, 28 Apr 2018 16:10:53 +0000 (16:10 +0000)]
netinet/if_ether.h: add ETH_P_ERSPAN2 from linux v4.16

protocol number for erspan v2 support
added in linux commit f551c91de262ba36b20c3ac19538afb4f4507441

5 years agosys/epoll.h: add EPOLLNVAL from linux v4.16
Szabolcs Nagy [Sat, 28 Apr 2018 15:52:06 +0000 (15:52 +0000)]
sys/epoll.h: add EPOLLNVAL from linux v4.16

added to uapi in commit 65aaf87b3aa2d049c6b9fd85221858a895df3393
used since commit a9a08845e9acbd224e4ee466f5c1275ed50054e8,
which renamed POLL* to EPOLL* in the kernel.

5 years agoadd m68k port
Rich Felker [Thu, 14 Jun 2018 18:26:30 +0000 (14:26 -0400)]
add m68k port

three ABIs are supported: the default with 68881 80-bit fpu format and
results returned in floating point registers, softfloat-only with the
same format, and coldfire fpu with IEEE single/double only. only the
first is tested at all, and only under qemu which has fpu emulation
bugs.

basic functionality smoke tests have been performed for the most
common arch-specific breakage via libc-test and qemu user-level
emulation. some sysvipc failures remain, but are shared with other big
endian archs and will be fixed separately.

5 years agoadd support for m68k 80-bit long double variant
Rich Felker [Thu, 14 Jun 2018 15:00:58 +0000 (11:00 -0400)]
add support for m68k 80-bit long double variant

since x86 and m68k are the only archs with 80-bit long double and each
has mandatory endianness, select the variant via endianness.
differences are minor: apparently just byte order and representation
of infinities. the m68k format is not well-documented anywhere I could
find, so if other differences are found they may require additional
changes later.

5 years agoadd missing m68k relocation types in elf.h
Rich Felker [Tue, 12 Jun 2018 21:02:21 +0000 (17:02 -0400)]
add missing m68k relocation types in elf.h

6 years agofix TLS layout of TLS variant I when there is a gap above TP
Szabolcs Nagy [Fri, 1 Jun 2018 23:52:01 +0000 (01:52 +0200)]
fix TLS layout of TLS variant I when there is a gap above TP

In TLS variant I the TLS is above TP (or above a fixed offset from TP)
but on some targets there is a reserved gap above TP before TLS starts.

This matters for the local-exec tls access model when the offsets of
TLS variables from the TP are hard coded by the linker into the
executable, so the libc must compute these offsets the same way as the
linker.  The tls offset of the main module has to be

alignup(GAP_ABOVE_TP, main_tls_align).

If there is no TLS in the main module then the gap can be ignored
since musl does not use it and the tls access models of shared
libraries are not affected.

The previous setup only worked if (tls_align & -GAP_ABOVE_TP) == 0
(i.e. TLS did not require large alignment) because the gap was
treated as a fixed offset from TP.  Now the TP points at the end
of the pthread struct (which is aligned) and there is a gap above
it (which may also need alignment).

The fix required changing TP_ADJ and __pthread_self on affected
targets (aarch64, arm and sh) and in the tlsdesc asm the offset to
access the dtv changed too.

6 years agofix output size handling for multi-unicode-char big5-hkscs characters
Rich Felker [Sat, 2 Jun 2018 02:05:48 +0000 (22:05 -0400)]
fix output size handling for multi-unicode-char big5-hkscs characters

since this iconv implementation's output is stateless, it's necessary
to know before writing anything to the output buffer whether the
conversion of the current input character will fit.

previously we used a hard-coded table of the output size needed for
each supported output encoding, but failed to update the table when
adding support for conversion to jis-based encodings and again when
adding separate encoding identifiers for implicit-endianness utf-16/32
and ucs-2/4 variants, resulting in out-of-bound table reads and
incorrect size checks. no buffer overflow was possible, but the
affected characters could be converted incorrectly, and iconv could
potentially produce an incorrect return value as a result.

remove the hard-coded table, and instead perform the recursive iconv
conversion to a temporary buffer, measuring the output size and
transferring it to the actual output buffer only if the whole
converted result fits.

6 years agofix iconv mapping of big5-hkscs characters that map to two unicode chars
Rich Felker [Sat, 2 Jun 2018 01:50:17 +0000 (21:50 -0400)]
fix iconv mapping of big5-hkscs characters that map to two unicode chars

this case is handled with a recursive call to iconv using a
specially-constructed conversion descriptor. the constant 0 was used
as the offset for utf-8, since utf-8 appears first in the charmaps
table, but the offset used needs to point into the charmap entry, past
the name/aliases at the beginning, to the byte identifying the
encoding. as a result of this error, junk was produced.

instead, call find_charmap so we don't have to hard-code a nontrivial
offset. with this change, the code has been tested and found to work
in the case of converting the affected hkscs characters to utf-8.

6 years agofix iconv conversion to UTF-32 with implicit (big) endianness
Will Dietz [Thu, 3 May 2018 18:44:53 +0000 (13:44 -0500)]
fix iconv conversion to UTF-32 with implicit (big) endianness

maintainer's notes:

commit 95c6044e2ae85846330814c4ac5ebf4102dbe02c split UTF-32 and
UTF-32BE but neglected to add a case for the former as a destination
encoding, resulting in it wrongly being handled by the default case.
the intent was that the value of the macro be chosen to encode "big
endian" in the low bits, so that no code would be needed, but this was
botched; instead, handle it the way UCS2 is handled.

6 years agofix iconv buffer overflow converting to legacy JIS-based encodings
Will Dietz [Tue, 1 May 2018 19:16:44 +0000 (14:16 -0500)]
fix iconv buffer overflow converting to legacy JIS-based encodings

maintainer's notes:

commit a223dbd27ae36fe53f9f67f86caf685b729593fc added the reverse
conversions to JIS-based encodings, but omitted the check for remining
buffer space in the case where the next character to be written was
single-byte, allowing conversion to continue past the end of the
destination buffer.

6 years agomake linking of thread-start with explicit scheduling conditional
Rich Felker [Wed, 9 May 2018 04:33:54 +0000 (00:33 -0400)]
make linking of thread-start with explicit scheduling conditional

the wrapper start function that performs scheduling operations is
unreachable if pthread_attr_setinheritsched is never called, so move
it there rather than the pthread_create source file, saving some code
size for static-linked programs.

6 years agoimprove design of thread-start with explicit scheduling attributes
Rich Felker [Wed, 9 May 2018 04:14:13 +0000 (00:14 -0400)]
improve design of thread-start with explicit scheduling attributes

eliminate the awkward startlock mechanism and corresponding fields of
the pthread structure that were only used at startup.

instead of having pthread_create perform the scheduling operations and
having the new thread wait for them to be completed, start the new
thread with a wrapper start function that performs its own scheduling,
sending the result code back via a futex. this way the new thread can
use storage from the calling thread's stack rather than permanent
fields in the pthread structure.

6 years agoclean up and reduce size of internal pthread structure
Rich Felker [Tue, 8 May 2018 02:39:07 +0000 (22:39 -0400)]
clean up and reduce size of internal pthread structure

over time the pthread structure has accumulated a lot of cruft taking
up size. this commit removes unused fields and packs booleans and
other small data more efficiently. changes which would also require
changing code are not included at this time.

non-volatile booleans are packed as unsigned char bitfield members.

the canceldisable and cancelasync fields need volatile qualification
due to how they're accessed from the cancellation signal handler and
cancellable syscalls called from signal handlers. since volatile
bitfield semantics are not clearly defined, discrete char objects are
used instead.

the pid field is completely removed; it has been unused since commit
83dc6eb087633abcf5608ad651d3b525ca2ec35e.

the tid field's type is changed to int because its use is as a value
in futexes, which are defined as plain int. it has no conceptual
relationship to pid_t. also, its position is not ABI.

startlock is reduced to a length-1 array. the second element was
presumably intended as a waiter count, but it was never used and made
no sense, since there is at most one waiter.

6 years agoimprove joinable/detached thread state handling
Rich Felker [Sun, 6 May 2018 01:33:58 +0000 (21:33 -0400)]
improve joinable/detached thread state handling

previously, some accesses to the detached state (from pthread_join and
pthread_getattr_np) were unsynchronized; they were harmless in
programs with well-defined behavior, but ugly. other accesses (in
pthread_exit and pthread_detach) were synchronized by a poorly named
"exitlock", with an ad-hoc trylock operation on it open-coded in
pthread_detach, whose only purpose was establishing protocol for which
thread is responsible for deallocation of detached-thread resources.

instead, use an atomic detach_state and unify it with the futex used
to wait for thread exit. this eliminates 2 members from the pthread
structure, gets rid of the hackish lock usage, and makes rigorous the
trap added in commit 80bf5952551c002cf12d96deb145629765272db0 for
catching attempts to join detached threads. it should also make
attempt to detach an already-detached thread reliably trap.

6 years agoimprove pthread_exit synchronization with functions targeting tid
Rich Felker [Fri, 4 May 2018 18:26:31 +0000 (14:26 -0400)]
improve pthread_exit synchronization with functions targeting tid

if the last thread exited via pthread_exit, the logic that marked it
dead did not account for the possibility of it targeting itself via
atexit handlers. for example, an atexit handler calling
pthread_kill(pthread_self(), SIGKILL) would return success
(previously, ESRCH) rather than causing termination via the signal.

move the release of killlock after the determination is made whether
the exiting thread is the last thread. in the case where it's not,
move the release all the way to the end of the function. this way we
can clear the tid rather than spending storage on a dedicated
dead-flag. clearing the tid is also preferable in that it hardens
against inadvertent use of the value after the thread has terminated
but before it is joined.

6 years agoremove incorrect ESRCH error from pthread_kill
Rich Felker [Fri, 4 May 2018 17:18:51 +0000 (13:18 -0400)]
remove incorrect ESRCH error from pthread_kill

posix documents in the rationale and future directions for
pthread_kill that, since the lifetime of the thread id for a joinable
thread lasts until it is joined, ESRCH is not a correct error for
pthread_kill to produce when the target thread has exited but not yet
been joined, and that conforming applications cannot attempt to detect
this state. future versions of the standard may explicitly require
that ESRCH not be returned for this case.

6 years agouse a dedicated futex object for pthread_join instead of tid field
Rich Felker [Wed, 2 May 2018 16:13:43 +0000 (12:13 -0400)]
use a dedicated futex object for pthread_join instead of tid field

the tid field in the pthread structure is not volatile, and really
shouldn't be, so as not to limit the compiler's ability to reorder,
merge, or split loads in code paths that may be relevant to
performance (like controlling lock ownership).

however, use of objects which are not volatile or atomic with futex
wait is inherently broken, since the compiler is free to transform a
single load into multiple loads, thereby using a different value for
the controlling expression of the loop and the value passed to the
futex syscall, leading the syscall to block instead of returning.

reportedly glibc's pthread_join was actually affected by an equivalent
issue in glibc on s390.

add a separate, dedicated join_futex object for pthread_join to use.

6 years agooptimize sigisemptyset
Rich Felker [Tue, 1 May 2018 20:56:02 +0000 (16:56 -0400)]
optimize sigisemptyset

the static const zero set ended up getting put in bss instead of
rodata, wasting writable memory, and the call to memcmp was
size-inefficient. generally for nonstandard extension functions we try
to avoid poking at any internals directly, but the way the zero set
was setup was arguably already doing so.

6 years agoavoid excessive stack usage in getcwd
Rich Felker [Tue, 1 May 2018 18:46:59 +0000 (14:46 -0400)]
avoid excessive stack usage in getcwd

to support the GNU extension of allocating a buffer for getcwd's
result when a null pointer is passed without incurring a link
dependency on free, we use a PATH_MAX-sized buffer on the stack and
only duplicate it to allocated storage after the operation succeeds.
unfortunately this imposed excessive stack usage on all callers,
including those not making use of the GNU extension.

instead, use a VLA to make stack allocation conditional.