Commit Graph

4068 Commits

Author SHA1 Message Date
Rich Felker
04e18b61df fix regression in setlocale for LC_ALL with per-category setting
commit d88e5dfa8b inadvertently changed
the argument pased to __get_locale from part (the current ;-delimited
component) to name (the full string).
2018-11-02 20:40:24 -04:00
Rich Felker
79f653c6bc fix failure to flush stderr when fflush(0) is called
commit ddc947eda3 fixed the
corresponding bug for exit which was introduced when commit
0b80a7b040 added support for
caller-provided buffers, making it possible for stderr to be a
buffered stream.
2018-11-02 12:52:56 -04:00
Rich Felker
4a08603026 fix deadlock and buffered data loss race in fclose
fflush(NULL) and __stdio_exit lock individual FILEs while holding the
open file list lock to walk the list. since fclose first locked the
FILE to be closed, then the ofl lock, it could deadlock with these
functions.

also, because fclose removed the FILE to be closed from the open file
list before flushing and closing it, a concurrent fclose or exit could
complete successfully before fclose flushed the FILE it was closing,
resulting in data loss.

reorder the body of fclose to first flush and close the file, then
remove it from the open file list only after unlocking it. this
creates a window where consumers of the open file list can see dead
FILE objects, but in the absence of undefined behavior on the part of
the application, such objects will be in an inactive-buffer state and
processing them will have no side effects.

__unlist_locked_file is also moved so that it's performed only for
non-permanent files. this change is not necessary, but preserves
consistency (and thereby provides safety/hardening) in the case where
an application uses one of the standard streams after closing it while
holding an explicit lock on it. such usage is of course undefined
behavior.
2018-11-02 12:31:19 -04:00
Alexander Monakov
00bd3b7d30 __libc_start_main: slightly simplify stage2 pointer setup
Use "+r" in the asm instead of implementing a non-transparent copy by
applying "0" constraint to the source value. Introduce a typedef for
the function type to avoid spelling it out twice.
2018-11-02 12:13:53 -04:00
Rich Felker
0239cd0681 remove commented-out debug printf from strstr
this was leftover from before the initial commit.
2018-11-02 12:04:41 -04:00
Rich Felker
8f5a820d14 fix spuriously slow check in twoway strstr/memmem cores
mem0 && mem && ... is redundant since mem can only be nonzero when
mem0 is nonzero.
2018-11-02 12:02:09 -04:00
Rich Felker
1b52863e24 don't omit setting errno in internal __map_file function
a caller needs the reason for open (or fstat, albeit unlikely) failure
if it's going to make decisions about continuing a path search or
similar.
2018-10-22 21:37:50 -04:00
Rich Felker
5af1f5942b make the default locale (& a variant) failure-free cases for newlocale
commit aeeac9ca54 introduced fail-safe
invariants that creating a locale_t object for the C locale or C.UTF-8
locale will always succeed. extend the guarantee to also cover the
following:

- newlocale(LC_ALL_MASK, "", 0)
- newlocale(LC_ALL_MASK-LC_CTYPE_MASK, "C", 0)

provided that the LANG/LC_* environment variables have not been
changed by the program. these usages are idiomatic for getting the
default locale, and for getting a locale that behaves as the C locale
except for honoring the default locale's character encoding.
2018-10-22 00:22:33 -04:00
Rich Felker
74e704006a simplify newlocale and allow failure for explicit locale names
unify the code paths for allocated and non-allocated locale objects,
always using a tmp object. this is necessary to avoid clobbering the
base locale object too soon if we allow for the possibility that
looking up an explicitly requested locale name may fail, and makes the
code simpler and cleaner anyway.

eliminate the complex and fragile logic for checking whether one of
the non-allocated locale objects can be used for the result, and
instead just memcmp against each of them.
2018-10-22 00:20:10 -04:00
Rich Felker
6753fb68b8 remove volatile qualification from category pointers in __locale_struct
commit 63c188ec42 missed making this
change when switching from atomics to locking for modification of the
global locale, leaving access to locale structures unnecessarily
burdened with the restrictions of volatile.

the volatile qualification was originally added in commit
56fbaa3bbe.
2018-10-20 22:44:34 -04:00
Rich Felker
d88e5dfa8b adapt setlocale to support possibility of failure
introduce a new LOC_MAP_FAILED sentinel for errors, since null
pointers for a category's locale map indicate the C locale. at this
time, __get_locale does not fail, so there should be no functional
change by this commit.
2018-10-20 21:54:20 -04:00
Rich Felker
8084d6ab57 adjust types in FILE struct to make line buffering check less expensive
the choice of signed char for lbf was a theoretically space-saving
hack that was not helping, and was unwantedly expensive. while
comparing bytes against a byte-sized member sounds easy, the trick
here was that the byte to be compared was unsigned while the lbf
member was signed, making it possible to set lbf negative to disable
line buffering. however, this imposed a requirement to promote both
operands, zero-extending one and sign-extending the other, in order to
compare them.

to fix this, repurpose the waiters count slot (unused since commit
c21f750727). while we're at it, switch
mode (orientation) from signed char to int as well. this makes no
semantic difference (its only possible values are -1, 0, and 1) but it
might help on archs where byte access is awkward.
2018-10-18 13:59:17 -04:00
Rich Felker
d8870dcf38 optimize internal putc_unlocked macro used in putc
to check whether flush due to line buffering is needed, the int-type
character argument must be truncated to unsigned char for comparison.
if the original value is subsequently passed to __overflow, it must be
preserved, adding to register pressure. since it doesn't matter,
truncate all uses so the original value is no longer live.
2018-10-18 13:41:25 -04:00
Rich Felker
a21a6092cf fix wrong result for putc variants due to operator precedence
the internal putc_unlocked macro was wrongly returning a meaningless
boolean result rather than the written character or EOF.

bug was found by reading (very surprising) asm.
2018-10-18 13:36:43 -04:00
Rich Felker
9dd1912256 further optimize getc/putc when locking is needed
check whether the lock is free before loading the calling thread's
tid. if so, just use a dummy tid value that cannot compare equal to
any actual thread id (because it's one bit wider). this also avoids
the need to save the tid and pass it to locking_getc or locking_putc,
reducing register pressure.

this change might slightly hurt the case where the caller already
holds the lock, but it does not affect the single-threaded case, and
may significantly improve the multi-threaded case, especially on archs
where loading the thread pointer is disproportionately expensive like
early mips and arm ISA levels. but even on i386 it helps, at least on
some machines; I measured roughly a 10-15% improvement.
2018-10-18 13:09:33 -04:00
Rich Felker
7eda27d025 use prototype for function pointer in static link libc init barrier
this is not needed for correctness, but doesn't hurt, and in some
cases the compiler may pessimize the call assuming the callee might be
variadic when it lacks a prototype.
2018-10-18 11:44:49 -04:00
Rich Felker
ba0d83e822 fix error in constraints for static link libc init barrier
commit 4390383b32 inadvertently used "r"
instead of "0" for the input constraint, which only happened to work
for the configuration I tested it on because it usually makes sense
for the compiler to choose the same input and output register.
2018-10-18 11:41:47 -04:00
Rich Felker
ab5e1e3408 fix build regression due to missing file for putc changes
commit d664061adb inadvertently omitted
the new file putc.h.
2018-10-18 10:44:32 -04:00
Rich Felker
d8f2efa708 bypass indirection through pointer objects to access stdin/out/err
by ABI, the public stdin/out/err macros use extern pointer objects,
and this is necessary to avoid copy relocations that would be
expensive and make the size of the FILE structure part of the ABI.
however, internally it makes sense to access the underlying FILE
objects directly. this avoids both an indirection through the GOT to
find the address of the stdin/out/err pointer objects (which can't be
computed PC-relative because they may have been moved to the main
program by copy relocations) and an indirection through the resulting
pointer object.

in most places this is just a minor optimization, but in the case of
getchar and putchar (and the unlocked versions thereof), ipa constant
propagation makes all accesses to members of stdin/out PC-relative or
GOT-relative, possibly reducing register pressure as well.
2018-10-18 00:32:20 -04:00
Rich Felker
d664061adb optimize hot paths of putc with manual shrink-wrapping
this is the analog of commit dd8f02b7dc,
but for putc.
2018-10-17 23:38:29 -04:00
Rich Felker
dd8f02b7dc optimize hot paths of getc with manual shrink-wrapping
with these changes, in a program that has not created any threads
besides the main thread and that has not called f[try]lockfile, getc
performs indistinguishably from getc_unlocked. this was measured on
several i386 and x86_64 models, and should hold on other archs too
simply by the properties of the code generation.

the case where the caller already holds the lock (via flockfile) is
improved significantly as well (40-60% reduction in time on machines
tested) and the case where locking is needed is improved somewhat
(roughly 10%).

the key technique used here is forcing the non-hot path out-of-line
and enabling it to be a tail call. a static noinline function
(conditional on __GNUC__) is used rather than the extern hiddens used
elsewhere for this purpose, so that the compiler can choose
non-default calling conventions, making it possible to tail-call to a
callee that takes more arguments than the caller on archs where
arguments are passed on the stack or must have space reserved on the
stack for spilling the. the tid could just be reloaded via the thread
pointer in locking_getc, but that would be ridiculously expensive on
some archs where thread pointer load requires a trap or syscall.
2018-10-17 23:16:35 -04:00
Rich Felker
7136836e14 document and make explicit desired noinline property for __init_libc
on multiple occasions I've started to flatten/inline the code in
__init_libc, only to rediscover the reason it was not inlined: GCC
fails to deallocate its stack (and now, with the changes in commit
4390383b32, fails to produce a tail call
to the stage 2 function; see PR #87639) before calling main if it was
inlined.

document this with a comment and use an explicit noinline attribute if
__GNUC__ is defined so that even with CFLAGS that heavily favor
inlining it won't get inlined.
2018-10-17 22:28:51 -04:00
Rich Felker
4390383b32 impose barrier between thread pointer setup and use for static linking
this is the analog of commit 1c84c99913
for static linking. unlike with dynamic linking, we don't have
symbolic lookup to use as a barrier. use a dummy (target-agnostic)
degenerate inline asm fragment instead. this technique has precedent
in commit 05ac345f89 where it's used for
explicit_bzero. if it proves problematic in any way, loading the
address of the stage 2 function from a pointer object whose address
leaks to kernelspace during thread pointer init could be used as an
even stronger barrier.
2018-10-17 22:20:01 -04:00
Rich Felker
a4a3e4dbc0 make thread-pointer-loading asm non-volatile
this will allow the compiler to cache and reuse the result, meaning we
no longer have to take care not to load it more than once for the sake
of archs where the load may be expensive.

depends on commit 1c84c99913 for
correctness, since otherwise the compiler could hoist loads during
stage 3 of dynamic linking before the initial thread-pointer setup.
2018-10-16 14:11:46 -04:00
Rich Felker
7f01a734fe remove ancient clang workaround from powerpc pthread_arch.h asm
versions of clang all the way back to 3.1 lack the bug this was
purportedly working around.
2018-10-16 14:11:46 -04:00
Rich Felker
bf453d6839 restore attribute((const)) to pthread_self and errno location decls
revert commit a603a75a72.

as a result of commit 1c84c99913 this is
now safe, assuming an interpretation of the somewhat-underspecified
attribute((const)) consistent with real-world usage.
2018-10-16 14:10:27 -04:00
Rich Felker
1c84c99913 add new stage 2b to dynamic linker bootstrap for thread pointer
commit a603a75a72 removed attribute
const from __errno_location and pthread_self, and the same reasoning
forced arch definitions of __pthread_self to use volatile asm,
significantly impacting code generation and imposing manual caching of
pointers where the impact might be noticable.

reorder the thread pointer setup and place it across a strong barrier
(symbolic function lookup) so that there is no assumed ordering
between the initialization and the accesses to the thread pointer in
stage 3.
2018-10-16 13:50:28 -04:00
Rich Felker
2085378a4f move stdio locking MAYBE_WAITERS definition to stdio_impl.h
don't repeat definition in two places.
2018-10-16 13:50:27 -04:00
Rich Felker
b36c37f6fa fix misleading placement of statement on same line as for loop in ldso
the placement triggered -Wmisleading-indentation warnings if enabled,
and was gratuitously confusing to anyone reading the code.
2018-10-15 15:31:02 -04:00
Szabolcs Nagy
e901613888 x86_64: add single instruction fma
fma is only available on recent x86_64 cpus and it is much faster than
a software fma, so this should be done with a runtime check, however
that requires more changes, this patch just adds the code so it can be
tested when musl is compiled with -mfma or -mfma4.
2018-10-15 14:45:28 -04:00
Szabolcs Nagy
7396ef0a05 arm: add single instruction fma
vfma is available in the vfpv4 fpu and above, the ACLE standard feature
test for double precision hardware fma support is
  __ARM_FEATURE_FMA && __ARM_FP&8
we need further checks to work around clang bugs (fixed in clang >=7.0)
  && !__SOFTFP__
because __ARM_FP is defined even with -mfloat-abi=soft
  && !BROKEN_VFP_ASM
to disable the single precision code when inline asm handling is broken.

For runtime selection the HWCAP_ARM_VFPv4 hwcap flag can be used, but
that requires further work.
2018-10-15 14:42:46 -04:00
Szabolcs Nagy
7c5f3bb955 powerpc: add single instruction fabs, fabsf, fma, fmaf, sqrt, sqrtf
These are only available on hard float target and sqrt is not available
in the base ISA, so further check is used.
2018-10-15 14:41:59 -04:00
Szabolcs Nagy
1da534ada8 s390x: add single instruction fma and fmaf
These are available in the s390x baseline isa -march=z900.
2018-10-15 14:41:43 -04:00
Rich Felker
481006fd88 allow escaped path-separator slashes in glob
previously (before and after rewrite), spurious escaping of path
separators as \/ was not treated the same as /, but rather got split
as an unpaired \ at the end of the fnmatch pattern and an unescaped /,
resulting in a mismatch/error.

for the case of \/ as part of the maximal literal prefix, remove the
explicit rejection of it and move the handling of / below escape
processing.

for the case of \/ after a proper glob pattern, it's hard to parse the
pattern, so don't. instead cheat and count repetitions of \ prior to
the already-found / character. if there are an odd number, the last is
escaping the /, so back up the split position by one. now the
char clobbered by null termination is variable, so save it and restore
as needed.
2018-10-13 01:28:07 -04:00
Rich Felker
d44b07fc90 rewrite core of the glob implementation for correctness & optimization
this code has been long overdue for a rewrite, but the immediate cause
that necessitated it was total failure to see past unreadable path
components. for example, A/B/* would fail to match anything, even
though it should succeed, when both A and A/B are searchable but only
A/B is readable. this problem both was caught in conformance testing,
and impacted users.

the old glob implementation insisted on searching the listing of each
path component for a match, even if the next component was a literal.
it also used considerable stack space, up to length of the pattern,
per recursion level, and relied on an artificial bound of the pattern
length by PATH_MAX, which was incorrect because a pattern can be much
longer than PATH_MAX while having matches shorter (for example, with
necessarily long bracket expressions, or with redundancy).

in the new implementation, each level of recursion starts by consuming
the maximal literal (possibly escaped-literal) path prefix remaining
in the pattern, and only opening a directory to read when there is a
proper glob pattern in the next path component. it then recurses into
each matching entry. the top-level glob function provided automatic
storage (up to PATH_MAX) for construction of candidate/result strings,
and allocates a duplicate of the pattern that can be modified in-place
with temporary null-termination to pass to fnmatch. this allocation is
not a big deal since glob already has to perform allocation, and has
to link free to clean up if it experiences an allocation failure or
other error after some results have already been allocated.

care is taken to use the d_type field from iterated dirents when
possible; stat is called only when there are literal path components
past the last proper-glob component, or when needed to disambiguate
symlinks for the purpose of GLOB_MARK.

one peculiarity with the new implementation is the manner in which the
error handling callback will be called. if attempting to match */B/C/D
where a directory A exists that is inaccessible, the error reported
will be a stat error for A/B/C/D rather than (previous and wrong
implementation) an opendir error for A, or (likely on other
implementations) a stat error for A/B. such behavior does not seem to
be non-conforming, but if it turns out to be undesirable for any
reason, backtracking could be done on error to report the first
component producing it.

also, redundant slashes are no longer normalized, but preserved as
they appear in the pattern; this is probably more correct, and falls
out naturally from the algorithm used. since trailing slashes (which
force all matches to be directories) are preserved as well, the
behavior of GLOB_MARK has been adjusted not to append an additional
slash to results that already end in slash.
2018-10-12 23:31:27 -04:00
Rich Felker
37cd167639 fix dlsym of thread-local symbols on archs with DTP_OFFSET!=0
commit 6ba5517a46 modified
__tls_get_addr to offset the address by +DTP_OFFSET (0x8000 on
powerpc, mips, etc.) and adjusted the result of DTPREL relocations by
-DTP_OFFSET to compensate, but missed changing the argument setup for
calls to __tls_get_addr from dlsym.
2018-10-12 12:31:36 -04:00
Rich Felker
b6d701a475 combine arch ABI's DTP_OFFSET into DTV pointers
as explained in commit 6ba5517a46, some
archs use an offset (typicaly -0x8000) with their DTPOFF relocations,
which __tls_get_addr needs to invert. on affected archs, which lack
direct support for large immediates, this can cost multiple extra
instructions in the hot path. instead, incorporate the DTP_OFFSET into
the DTV entries. this means they are no longer valid pointers, so
store them as an array of uintptr_t rather than void *; this also
makes it easier to access slot 0 as a valid slot count.

commit e75b16cf93 left behind cruft in
two places, __reset_tls and __tls_get_new, from back when it was
possible to have uninitialized gap slots indicated by a null pointer
in the DTV. since the concept of null pointer is no longer meaningful
with an offset applied, remove this cruft.

presently there are no archs with both TLSDESC and nonzero DTP_OFFSET,
but the dynamic TLSDESC relocation code is also updated to apply an
inverted offset to its offset field, so that the offset DTV would not
impose a runtime cost in TLSDESC resolver functions.
2018-10-12 00:39:56 -04:00
Rich Felker
09a805a623 fix redundant computations of strlen in glob append function
len was already passed as an argument, so don't use strcat, and use
memcpy instead of strcpy.
2018-10-11 14:27:15 -04:00
Rich Felker
e2552581bc fix invalid substitute of [1] for flexible array member in glob 2018-10-11 14:23:14 -04:00
Szabolcs Nagy
7b384c42b7 fix fesetround error checking
Rounding modes are not bit flags, but arbitrary non-negative integers.
2018-10-10 16:15:51 -04:00
Rich Felker
b3389bbfb5 fix build regression on armhf in tlsdesc asm
when invoking the assembler, arm gcc does not always pass the right
flags to enable use of vfp instruction mnemonics. for C code it
produces, it emits the .fpu directive, but this does not help when
building asm source files, which tlsdesc needs to be. to fix, use an
explicit directive here.

commit 0beb9dfbec introduced this
regression. it has not appeared in any release.
2018-10-09 10:59:39 -04:00
Rich Felker
d1395c43c0 allow freeaddrinfo of arbitrary sublists of addrinfo list
the specification for freeaddrinfo allows it to be used to free
"arbitrary sublists" of the list returned by getaddrinfo. it's not
clearly stated how such sublists come into existence, but the
interpretation seems to be that the application can edit the ai_next
pointers to cut off a portion of the list and then free it.

actual freeing of individual list slots is contrary to the design of
our getaddrinfo implementation, which has no failure paths after
making a single allocation, so that light callers can avoid linking
realloc/free. freeing individual slots is also incompatible with
sharing the string for ai_canonname, which the current implementation
does despite no requirement that it be present except on the first
result. so, rather than actually freeing individual slots, provide a
way to find the start of the allocated array, and reference-count it,
freeing the memory all at once after the last slot has been freed.

since the language in the spec is "arbitrary sublists", no provision
for handling other constructs like multiple lists glued together,
circular links, etc. is made. presumably passing such a construct to
freeaddrinfo produces undefined behavior.
2018-10-04 20:27:17 -04:00
Rich Felker
7bf773a8f9 inline cp15 thread pointer load in arm dynamic TLSDESC asm when possible
the indirect function call is a significant portion of the code path
for the dynamic case, and most users are probably building for ISA
levels where it can be omitted.

we could drop at least one register save/restore (lr) with this
change, and possibly another (ip) with some clever shuffling, but it's
not clear whether there's a way to do it that's not more expensive, or
whether avoiding the save/restore would have any practical effect, so
in the interest of avoiding complexity it's omitted for now.
2018-10-01 19:39:24 -04:00
Rich Felker
0beb9dfbec add TLSDESC support for 32-bit arm
unlike other asm where the baseline ISA is used, these functions are
hot paths and use ISA-level specializations.

call-clobbered vfp registers are saved before calling __tls_get_new,
since there is no guarantee it won't use them. while setjmp/longjmp
have to use hwcap to decide whether to the fpu is in use, since
application code could be using vfp registers even if libc was
compiled as pure softfloat, __tls_get_new is part of libc and can be
assumed not to have access to vfp registers if tlsdesc.S does not.
thus it suffices just to check the predefined preprocessor macros. the
check for __ARM_PCS_VFP is redundant; !__SOFTFP__ must always be true
if the target ISA level includes fpu instructions/registers.
2018-10-01 18:37:02 -04:00
Rich Felker
4d0a82170a fix aliasing-based undefined behavior in string functions
use the GNU C may_alias attribute if available, and fallback to naive
byte-by-byte loops if __GNUC__ is not defined.

this patch has been written to minimize changes so that history
remains reviewable; it does not attempt to bring the affected code
into a more consistent or elegant form.
2018-09-26 14:39:10 -04:00
Rich Felker
8cd738bbee optimize nop case of wmemmove 2018-09-23 02:51:01 -04:00
Rich Felker
82c41e9232 fix undefined pointer comparison in wmemmove 2018-09-23 02:48:25 -04:00
Rich Felker
debadaa238 fix undefined pointer comparison in memmove
the comparison must take place in the address space model as an
integer type, since comparing pointers that are not pointing into the
same array is undefined.

the subsequent d<s comparison however is valid, because it's only
reached in the case where the source and dest overlap, in which case
they are necessarily pointing to parts of the same array.

to make the comparison, use an unsigned range check for dist(s,d)>=n,
algebraically !(-n<s-d<n). subtracting n yields !(-2*n<s-d-n<0), which
mapped into unsigned modular arithmetic is !(-2*n<s-d-n) or rather
-2*n>=s-d-n.
2018-09-23 00:03:08 -04:00
Szabolcs Nagy
c50985d5c8 new tsearch implementation
Rewrote the AVL tree implementation:

- It is now non-recursive with fixed stack usage (large enough for
worst case tree height). twalk and tdestroy are still recursive as
that's smaller/simpler.

- Moved unrelated interfaces into separate translation units.

- The node structure is changed to use indexed children instead of
left/right pointers, this simplifies the balancing logic.

- Using void * pointers instead of struct node * in various places,
because this better fits the api (node address is passed in a void**
argument, so it is tempting to incorrectly cast it to struct node **).

- As a further performance improvement the rebalancing now stops
when it is not needed (subtree height is unchanged). Otherwise
the behaviour should be the same as before (checked over generated
random inputs that the resulting tree shape is equivalent).

- Removed the old copyright notice (including prng related one: it
should be licensed under the same terms as the rest of the project).

.text size of pic tsearch + tfind + tdelete + twalk:

   x86_64 i386 aarch64  arm mips powerpc ppc64le  sh4 m68k s390x
old   941  899    1220 1068 1852    1400    1600 1008 1008  1488
new   857  881    1040  976 1564    1192    1360  736  820  1408
2018-09-20 17:57:47 -04:00
Szabolcs Nagy
5ef60206ff add arm and sh bits/ptrace.h
These should have been added in commit
df6d9450ea
that added target specific PTRACE_ macros, but somehow got missed.
2018-09-20 17:56:29 -04:00