about summary refs log tree commit diff
path: root/src/internal
Commit message (Collapse)AuthorAgeFilesLines
* combine arch ABI's DTP_OFFSET into DTV pointersRich Felker2018-10-121-2/+3
| | | | | | | | | | | | | | | | | | | | | | as explained in commit 6ba5517a460c6c438f64d69464fdfc3269a4c91a, 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 e75b16cf93ebbc1ce758d3ea6b2923e8b2457c68 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.
* increase default thread stack/guard sizeRich Felker2018-09-181-2/+2
| | | | | | | | | | | | stack size default is increased from 80k to 128k. this coincides with Linux's hard-coded default stack for the main thread (128k is initially committed; growth beyond that up to ulimit is contingent on additional allocation succeeding) and GNU ld's default PT_GNU_STACK size for FDPIC, at least on sh. guard size default is increased from 4k to 8k to reduce the risk of guard page jumping on overflow, since use of just over 4k of stack is common (PATH_MAX buffers, etc.).
* limit the configurable default stack/guard size for threadsRich Felker2018-09-181-2/+5
| | | | | | | | | | limit to 8MB/1MB, repectively. since the defaults cannot be reduced once increased, excessively large settings would lead to an unrecoverably broken state. this change is in preparation to allow defaults to be increased via program headers at the linker level. creation of threads that really need larger sizes needs to be done with an explicit attribute.
* fix deletion of pthread tsd keys that still have non-null values storedRich Felker2018-09-181-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | per POSIX, deletion of a key for which some threads still have values stored is permitted, and newly created keys must initially hold the null value in all threads. these properties were not met by our implementation; if a key was deleted with values left and a new key was created in the same slot, the old values were still visible. moreover, due to lack of any synchronization in pthread_key_delete, there was a TOCTOU race whereby a concurrent pthread_exit could attempt to call a null destructor pointer for the newly orphaned value. this commit introduces a solution based on __synccall, stopping the world to zero out the values for deleted keys, but only does so lazily when all key slots have been exhausted. pthread_key_delete is split off into a separate translation unit so that static-linked programs which only create keys but never delete them will not pull in the __synccall machinery. a global rwlock is added to synchronize creation and deletion of keys with dtor execution. since the dtor execution loop now has to release and retake the lock around its call to each dtor, checks are made not to call the nodtor dummy function for keys which lack a dtor.
* fix null pointer subtraction and comparison in stdioRich Felker2018-09-161-2/+2
| | | | | | | | | | | | | | | | | | | | morally, for null pointers a and b, a-b, a<b, and a>b should all be defined as 0; however, C does not define any of them. the stdio implementation makes heavy use of such pointer comparison and subtraction for buffer logic, and also uses null pos/base/end pointers to indicate that the FILE is not in the corresponding (read or write) mode ready for accesses through the buffer. all of the comparisons are fixed trivially by using != in place of the relational operators, since the opposite relation (e.g. pos>end) is logically impossible. the subtractions have been reviewed to check that they are conditional the stream being in the appropriate reading- or writing-through-buffer mode, with checks added where needed. in fgets and getdelim, the checks added should improve performance for unbuffered streams by avoiding a do-nothing call to memchr, and should be negligible for buffered streams.
* fix undefined behavior in strto* via FILE buffer pointer abuseRich Felker2018-09-152-8/+40
| | | | | | | | | | | | | | | | | | | | | | | | | | | | in order to produce FILE objects to pass to the intscan/floatscan backends without any (prohibitively costly) extra buffering layer, the strto* functions set the FILE's rend (read end) buffer pointer to an invalid value at the end of the address space, or SIZE_MAX/2 past the beginning of the string. this led to undefined behavior comparing and subtracting the end pointer with the buffer position pointer (rpos). the comparison issue is easily eliminated by using != instead of <. however the subtractions require nontrivial changes: previously, f->shcnt stored the count that would have been read if consuming the whole buffer, which required an end pointer for the buffer. the purpose for this was that it allowed reading it and adding rpos-rend at any time to get the actual count so far, and required no adjustment at the time of __shgetc (actual function call) since the call would only happen when reaching the end of the buffer. to get rid of the dependency on rend, instead offset shcnt by buf-rpos (start of buffer) at the time of last __shlim/__shgetc call. this makes for slightly more work in __shgetc the function, but for the inline macro it's still just as easy to compute the current count. since the scan helper interfaces used here are a big hack, comments are added to document their contracts and what's going on with their implementations.
* fix regression with compilers not incorporating C99 DR#289 resolutionRich Felker2018-09-131-1/+1
| | | | | | | | | as originally published, the C99 syntax only allowed static index parameter declarators when a gratuitous parameter name was included. gcc 3, which some projects use for bootstrapping, is a supported C99 compiler, but does not have the fix to the standard incorporated, so edit the affected declaration to conform to the earlier buggy C99 syntax.
* remove vis.h protected-visibility hackRich Felker2018-09-121-27/+0
| | | | | | | | | since commit dc2f368e565c37728b0d620380b849c3a1ddd78f this has been disabled by default, but was left available in case users unhappy with the resulting size or performance regressions wanted to try to make it work. now that we make widespread use of hidden visibility for internal interfaces, this no longer makes sense. if any costly calls remain they can be fixed with hidden aliases.
* split internal lock API out of libc.h, creating lock.hRich Felker2018-09-122-6/+9
| | | | | | | | | this further reduces the number of source files which need to include libc.h and thereby be potentially exposed to libc global state and internals. this will also facilitate further improvements like adding an inline fast-path, if we want to do so later.
* move misplaced __fork_handler declarationRich Felker2018-09-122-1/+1
| | | | | | | | pthread_atfork.c does not actually include pthread_impl.h and has no reason to, so it wasn't getting the declaration. move it to libc.h which is already included by both fork.c and pthread_atfork.c. this makes more sense anyway since the function has little to do with pthreads anyway aside from the name.
* remove spurious inclusion of libc.h for LFS64 ABI aliasesRich Felker2018-09-121-6/+0
| | | | | | the LFS64 macro was not self-documenting and barely saved any characters. simply use weak_alias directly so that it's clear what's being done, and doesn't depend on a header to provide a strange macro.
* reduce spurious inclusion of libc.hRich Felker2018-09-127-8/+7
| | | | | | | | | | | | | | | | | | | | | libc.h was intended to be a header for access to global libc state and related interfaces, but ended up included all over the place because it was the way to get the weak_alias macro. most of the inclusions removed here are places where weak_alias was needed. a few were recently introduced for hidden. some go all the way back to when libc.h defined CANCELPT_BEGIN and _END, and all (wrongly implemented) cancellation points had to include it. remaining spurious users are mostly callers of the LOCK/UNLOCK macros and files that use the LFS64 macro to define the awful *64 aliases. in a few places, new inclusion of libc.h is added because several internal headers no longer implicitly include libc.h. declarations for __lockfile and __unlockfile are moved from libc.h to stdio_impl.h so that the latter does not need libc.h. putting them in libc.h made no sense at all, since the macros in stdio_impl.h are needed to use them correctly anyway.
* remove unused __futex function and source fileRich Felker2018-09-121-2/+0
| | | | | | | the direct syscall or various thin and mostly-inline wrappers around it are used instead internally. at some point a public futex function should be added, but it's not yet clear what the signature should be, and in the mean time this file is not useful.
* declare and make hidden additional internal init/exit symbolsRich Felker2018-09-121-0/+4
|
* declare and make hidden additional internal stdio symbolsRich Felker2018-09-121-0/+5
|
* declare and make hidden more internal locale functionsRich Felker2018-09-121-0/+2
|
* move additional pthread internal declarations to pthread_impl.h, hideRich Felker2018-09-121-0/+15
| | | | these were overlooked for various reasons in earlier stages.
* apply hidden visibility to various remaining internal interfacesRich Felker2018-09-127-23/+23
|
* apply hidden visibility to sigreturn code fragmentsRich Felker2018-09-121-1/+3
| | | | | | | these were overlooked in the declarations overhaul work because they are not properly declared, and the current framework even allows their declared types to vary by arch. at some point this should be cleaned up, but I'm not sure what the right way would be.
* apply hidden visibility to pthread internalsRich Felker2018-09-121-11/+11
|
* apply hidden visibility to stdio internalsRich Felker2018-09-121-26/+26
|
* apply hidden visibility to internal math functionsRich Felker2018-09-121-24/+24
| | | | | | | | this makes significant differences to codegen on archs with an expensive PLT-calling ABI; on i386 and gcc 7.3 for example, the sin and sinf functions no longer touch call-saved registers or the stack except for pushing outgoing arguments. performance is likely improved too, but no measurements were taken.
* overhaul internally-public declarations using wrapper headersRich Felker2018-09-123-22/+6
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | commits leading up to this one have moved the vast majority of libc-internal interface declarations to appropriate internal headers, allowing them to be type-checked and setting the stage to limit their visibility. the ones that have not yet been moved are mostly namespace-protected aliases for standard/public interfaces, which exist to facilitate implementing plain C functions in terms of POSIX functionality, or C or POSIX functionality in terms of extensions that are not standardized. some don't quite fit this description, but are "internally public" interfacs between subsystems of libc. rather than create a number of newly-named headers to declare these functions, and having to add explicit include directives for them to every source file where they're needed, I have introduced a method of wrapping the corresponding public headers. parallel to the public headers in $(srcdir)/include, we now have wrappers in $(srcdir)/src/include that come earlier in the include path order. they include the public header they're wrapping, then add declarations for namespace-protected versions of the same interfaces and any "internally public" interfaces for the subsystem they correspond to. along these lines, the wrapper for features.h is now responsible for the definition of the hidden, weak, and weak_alias macros. this means source files will no longer need to include any special headers to access these features. over time, it is my expectation that the scope of what is "internally public" will expand, reducing the number of source files which need to include *_impl.h and related headers down to those which are actually implementing the corresponding subsystems, not just using them.
* declare __getopt_msg in stdio_impl.hRich Felker2018-09-121-0/+2
| | | | | | it's not ideal, but the function is essentially an extended stdio function specialized to getopt's needs. the only reason it exists is avoiding pulling printf code into every program using getopt.
* move __memalign declaration to malloc_impl.hRich Felker2018-09-121-0/+2
| | | | | | | | the malloc-implementation-private header is the only right place for this, because, being in the reserved namespace, __memalign is not interposable and thus not valid to use anywhere else. anything outside of the malloc implementation must call an appropriate-namespace public function (aligned_alloc or posix_memalign).
* make arch __set_thread_area backends hiddenRich Felker2018-09-121-1/+1
| | | | | | | | this is not a public interface, and does not even necessarily match the syscall on all archs that have a syscall by that name. on archs where it's implemented in C, no action on the source file is needed; the hidden declaration in pthread_arch.h suffices.
* make arch __clone backends hiddenRich Felker2018-09-121-1/+1
| | | | | these are not a public interface and are not intended to be callable from anywhere but the public clone function or other places in libc.
* move tlsdesc and internal dl function declarations to dynlink.hRich Felker2018-09-121-0/+10
|
* move __stdio_exit_needed to stdio_impl.hRich Felker2018-09-121-0/+2
| | | | this functions is glue for linking dependency logic.
* move __loc_is_allocated declaration to locale_impl.hRich Felker2018-09-121-0/+1
|
* move declarations of tls setup/access functions to pthread_impl.hRich Felker2018-09-121-0/+6
| | | | | it's already included in all places where these are needed, and aside from __tls_get_addr, they're all implementation internals.
* move lgamma-related internal declarations to libm.hRich Felker2018-09-121-0/+4
|
* move declarations for malloc internals to malloc_impl.hRich Felker2018-09-121-0/+4
|
* improve machinery for ldso to report libc versionRich Felker2018-09-122-6/+3
| | | | | | | | eliminate gratuitous glue function for reporting the version, which was probably leftover from the old dynamic linker design which lacked a clear barrier for when/how it could access global data. put the declaration for the data object that replaces it in libc.h where it can be type checked.
* make internal declarations for flockfile tracking functions checkableRich Felker2018-09-121-0/+5
| | | | | | | logically these belong to the intersection of the stdio and pthread subsystems, and either place the declarations could go (stdio_impl.h or pthread_impl.h) requires a forward declaration for one of the argument types.
* move and deduplicate declarations of __vdsosym to make it checkableRich Felker2018-09-121-0/+2
|
* move and deduplicate declarations of __procfdname to make it checkableRich Felker2018-09-122-0/+4
| | | | | | syscall.h was chosen as the header to declare it, since its intended usage is alongside syscalls as a fallback for operations the direct syscall does not support.
* remove leftover declarations for removed functions from pthread_impl.hRich Felker2018-09-051-4/+0
|
* define and use internal macros for hidden visibility, weak refsRich Felker2018-09-056-25/+23
| | | | | | | | | this cleans up what had become widespread direct inline use of "GNU C" style attributes directly in the source, and lowers the barrier to increased use of hidden visibility, which will be useful to recovering some of the efficiency lost when the protected visibility hack was dropped in commit dc2f368e565c37728b0d620380b849c3a1ddd78f, especially on archs where the PLT ABI is costly.
* fix missing timeout argument to futex syscall in __futexwaitPatrick Oppenlander2018-06-261-2/+2
|
* add m68k portRich Felker2018-06-191-0/+9
| | | | | | | | | | | | | 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.
* add support for m68k 80-bit long double variantRich Felker2018-06-141-0/+11
| | | | | | | | | 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.
* make linking of thread-start with explicit scheduling conditionalRich Felker2018-05-091-0/+8
| | | | | | | 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.
* improve design of thread-start with explicit scheduling attributesRich Felker2018-05-091-2/+0
| | | | | | | | | | | | 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.
* clean up and reduce size of internal pthread structureRich Felker2018-05-071-7/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | 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.
* improve joinable/detached thread state handlingRich Felker2018-05-051-3/+9
| | | | | | | | | | | | | | | | | 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.
* improve pthread_exit synchronization with functions targeting tidRich Felker2018-05-051-1/+0
| | | | | | | | | | | | | | | | 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.
* use a dedicated futex object for pthread_join instead of tid fieldRich Felker2018-05-021-0/+1
| | | | | | | | | | | | | | | | | | 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.
* reintroduce hardening against partially-replaced allocatorRich Felker2018-04-191-0/+3
| | | | | | | | | | | | | | commit 618b18c78e33acfe54a4434e91aa57b8e171df89 removed the previous detection and hardening since it was incorrect. commit 72141795d4edd17f88da192447395a48444afa10 already handled all that remained for hardening the static-linked case. in the dynamic-linked case, have the dynamic linker check whether malloc was replaced and make that information available. with these changes, the properties documented in commit c9f415d7ea2dace5bf77f6518b6afc36bb7a5732 are restored: if calloc is not provided, it will behave as malloc+memset, and any of the memalign-family functions not provided will fail with ENOMEM.
* return chunks split off by memalign using __bin_chunk instead of freeRich Felker2018-04-191-0/+3
| | | | | | | | | | | | | | | this change serves multiple purposes: 1. it ensures that static linking of memalign-family functions will pull in the system malloc implementation, thereby causing link errors if an attempt is made to link the system memalign functions with a replacement malloc (incomplete allocator replacement). 2. it eliminates calls to free that are unpaired with allocations, which are confusing when setting breakpoints or tracing execution. as a bonus, making __bin_chunk external may discourage aggressive and unnecessary inlining of it.