42 Commits

Author SHA1 Message Date
Rich Felker
6232b96f51 minor locking optimizations 2011-06-14 01:23:42 -04:00
Rich Felker
11e4b92556 optimize out useless default-attribute object in pthread_create 2011-05-07 23:39:48 -04:00
Rich Felker
4c4e22d781 optimize compound-literal sigset_t's not to contain useless hurd bits 2011-05-07 23:37:10 -04:00
Rich Felker
99b8a25e94 overhaul implementation-internal signal protections
the new approach relies on the fact that the only ways to create
sigset_t objects without invoking UB are to use the sig*set()
functions, or from the masks returned by sigprocmask, sigaction, etc.
or in the ucontext_t argument to a signal handler. thus, as long as
sigfillset and sigaddset avoid adding the "protected" signals, there
is no way the application will ever obtain a sigset_t including these
bits, and thus no need to add the overhead of checking/clearing them
when sigprocmask or sigaction is called.

note that the old code actually *failed* to remove the bits from
sa_mask when sigaction was called.

the new implementations are also significantly smaller, simpler, and
faster due to ignoring the useless "GNU HURD signals" 65-1024, which
are not used and, if there's any sanity in the world, never will be
used.
2011-05-07 23:23:58 -04:00
Rich Felker
a6054e3c94 move some more code out of pthread_create.c
this also de-uglifies the dummy function aliasing a bit.
2011-04-19 23:09:14 -04:00
Rich Felker
2afed79f15 pthread_exit is not supposed to affect cancellability
if the exit was caused by cancellation, __cancel has already set these
flags anyway.
2011-04-17 17:09:41 -04:00
Rich Felker
1ebde9c3a2 fix pthread_exit from cancellation handler
cancellation frames were not correctly popped, so this usage would not
only loop, but also reuse discarded and invalid parts of the stack.
2011-04-17 17:06:05 -04:00
Rich Felker
9080cc153c clean up handling of thread/nothread mode, locking 2011-04-17 16:53:54 -04:00
Rich Felker
feee98903c overhaul pthread cancellation
this patch improves the correctness, simplicity, and size of
cancellation-related code. modulo any small errors, it should now be
completely conformant, safe, and resource-leak free.

the notion of entering and exiting cancellation-point context has been
completely eliminated and replaced with alternative syscall assembly
code for cancellable syscalls. the assembly is responsible for setting
up execution context information (stack pointer and address of the
syscall instruction) which the cancellation signal handler can use to
determine whether the interrupted code was in a cancellable state.

these changes eliminate race conditions in the previous generation of
cancellation handling code (whereby a cancellation request received
just prior to the syscall would not be processed, leaving the syscall
to block, potentially indefinitely), and remedy an issue where
non-cancellable syscalls made from signal handlers became cancellable
if the signal handler interrupted a cancellation point.

x86_64 asm is untested and may need a second try to get it right.
2011-04-17 11:43:03 -04:00
Rich Felker
016a5dc192 use a separate signal from SIGCANCEL for SIGEV_THREAD timers
otherwise we cannot support an application's desire to use
asynchronous cancellation within the callback function. this change
also slightly debloats pthread_create.c.
2011-04-14 12:51:00 -04:00
Rich Felker
9beb6330c0 simplify cancellation point handling
we take advantage of the fact that unless self->cancelpt is 1,
cancellation cannot happen. so just increment it by 2 to temporarily
block cancellation. this drops pthread_create.o well under 1k.
2011-04-13 20:47:01 -04:00
Rich Felker
c2cd25bff8 consistency: change all remaining syscalls to use SYS_ rather than __NR_ prefix 2011-04-06 20:32:53 -04:00
Rich Felker
b2486a8922 move rsyscall out of pthread_create module
this is something of a tradeoff, as now set*id() functions, rather
than pthread_create, are what pull in the code overhead for dealing
with linux's refusal to implement proper POSIX thread-vs-process
semantics. my motivations are:

1. it's cleaner this way, especially cleaner to optimize out the
rsyscall locking overhead from pthread_create when it's not needed.
2. it's expected that only a tiny number of core system programs will
ever use set*id() functions, whereas many programs may want to use
threads, and making thread overhead tiny is an incentive for "light"
programs to try threads.
2011-04-06 20:27:07 -04:00
Rich Felker
74950b336d pthread exit stuff: don't bother setting errno when we won't check it. 2011-04-06 19:47:50 -04:00
Rich Felker
622804ece7 fix rsyscall handler: must not clobber errno from signal context 2011-04-06 19:46:46 -04:00
Rich Felker
729cb49f52 new framework to inhibit thread cancellation when needed
with these small changes, libc functions which need to call functions
which are cancellation points, but which themselves must not be
cancellation points, can use the CANCELPT_INHIBIT and CANCELPT_RESUME
macros to temporarily inhibit all cancellation.
2011-04-05 18:00:28 -04:00
Rich Felker
7fd3995282 pthread_create need not set errno 2011-04-03 16:15:15 -04:00
Rich Felker
66def4e776 block all signals during rsyscall
otherwise a signal handler could see an inconsistent and nonconformant
program state where different threads have different uids/gids.
2011-04-03 13:15:42 -04:00
Rich Felker
1ad049b7b6 fix race condition in rsyscall handler
the problem: there is a (single-instruction) race condition window
between a thread flagging itself dead and decrementing itself from the
thread count. if it receives the rsyscall signal at this exact moment,
the rsyscall caller will never succeed in signalling enough flags to
succeed, and will deadlock forever. in previous versions of musl, the
about-to-terminate thread masked all signals prior to decrementing
the thread count, but this cost a whole syscall just to account for
extremely rare races.

the solution is a huge hack: rather than blocking in the signal
handler if the thread is dead, modify the signal mask of the saved
context and return in order to prevent further signal handling by the
dead thread. this allows the dead thread to continue decrementing the
thread count (if it had not yet done so) and exiting, even while the
live part of the program blocks for rsyscall.
2011-04-03 13:03:18 -04:00
Rich Felker
c9b2d8016f don't trust siginfo in rsyscall handler
for some inexplicable reason, linux allows the sender of realtime
signals to spoof its identity. permission checks for sending signals
should limit the impact to same-user processes, but just to be safe,
we avoid trusting the siginfo structure and instead simply examine the
program state to see if we're in the middle of a legitimate rsyscall.
2011-04-03 12:20:51 -04:00
Rich Felker
f01d351842 simplify calling of timer signal handler 2011-04-03 12:03:58 -04:00
Rich Felker
fd80cfa00b omit pthread tsd dtor code if tsd is not used 2011-04-03 02:33:50 -04:00
Rich Felker
4ae5e811f8 simplify setting result on thread cancellation 2011-04-01 22:15:03 -04:00
Rich Felker
3df3d4f512 fix misspelled PTHREAD_CANCELED constant 2011-04-01 20:48:02 -04:00
Rich Felker
bf619d82c8 major improvements to cancellation handling
- there is no longer any risk of spoofing cancellation requests, since
  the cancel flag is set in pthread_cancel rather than in the signal
  handler.

- cancellation signal is no longer unblocked when running the
  cancellation handlers. instead, pthread_create will cause any new
  threads created from a cancellation handler to unblock their own
  cancellation signal.

- various tweaks in preparation for POSIX timer support.
2011-03-29 12:58:22 -04:00
Rich Felker
ea343364a7 match glibc/lsb cancellation abi on i386
glibc made the ridiculous choice to use pass-by-register calling
convention for these functions, which is impossible to duplicate
directly on non-gcc compilers. instead, we use ugly asm to wrap and
convert the calling convention. presumably this works with every
compiler anyone could potentially want to use.
2011-03-25 22:13:57 -04:00
Rich Felker
b470030f83 overhaul cancellation to fix resource leaks and dangerous behavior with signals
this commit addresses two issues:

1. a race condition, whereby a cancellation request occurring after a
syscall returned from kernelspace but before the subsequent
CANCELPT_END would cause cancellable resource-allocating syscalls
(like open) to leak resources.

2. signal handlers invoked while the thread was blocked at a
cancellation point behaved as if asynchronous cancellation mode wer in
effect, resulting in potentially dangerous state corruption if a
cancellation request occurs.

the glibc/nptl implementation of threads shares both of these issues.

with this commit, both are fixed. however, cancellation points
encountered in a signal handler will not be acted upon if the signal
was received while the thread was already at a cancellation point.
they will of course be acted upon after the signal handler returns, so
in real-world usage where signal handlers quickly return, it should
not be a problem. it's possible to solve this problem too by having
sigaction() wrap all signal handlers with a function that uses a
pthread_cleanup handler to catch cancellation, patch up the saved
context, and return into the cancellable function that will catch and
act upon the cancellation. however that would be a lot of complexity
for minimal if any benefit...
2011-03-24 14:18:00 -04:00
Rich Felker
aa398f56fa global cleanup to use the new syscall interface 2011-03-20 00:16:43 -04:00
Rich Felker
685e40bb09 syscall overhaul part two - unify public and internal syscall interface
with this patch, the syscallN() functions are no longer needed; a
variadic syscall() macro allows syscalls with anywhere from 0 to 6
arguments to be made with a single macro name. also, manually casting
each non-integer argument with (long) is no longer necessary; the
casts are hidden in the macros.

some source files which depended on being able to define the old macro
SYSCALL_RETURNS_ERRNO have been modified to directly use __syscall()
instead of syscall(). references to SYSCALL_SIGSET_SIZE and SYSCALL_LL
have also been changed.

x86_64 has not been tested, and may need a follow-up commit to fix any
minor bugs/oversights.
2011-03-19 21:36:10 -04:00
Rich Felker
d00ff2950e overhaul syscall interface
this commit shuffles around the location of syscall definitions so
that we can make a syscall() library function with both SYS_* and
__NR_* style syscall names available to user applications, provides
the syscall() library function, and optimizes the code that performs
the actual inline syscalls in the library itself.

previously on i386 when built as PIC (shared library), syscalls were
incurring bus lock (lock prefix) overhead at entry and exit, due to
the way the ebx register was being loaded (xchg instruction with a
memory operand). now the xchg takes place between two registers.

further cleanup to arch/$(ARCH)/syscall.h is planned.
2011-03-19 18:51:42 -04:00
Rich Felker
29fae65780 cut out a syscall on thread creation in the case where guard size is 0 2011-03-16 11:36:21 -04:00
Rich Felker
5eb0d33ec0 implement flockfile api, rework stdio locking 2011-03-12 21:55:45 -05:00
Rich Felker
5fcebcde6a optimize pthread termination in the non-detached case
we can avoid blocking signals by simply using a flag to mark that the
thread has exited and prevent it from getting counted in the rsyscall
signal-pingpong. this restores the original pthread create/join
throughput from before the sigprocmask call was added.
2011-03-10 18:31:37 -05:00
Rich Felker
52213f7341 security fix: check that cancel/rsyscall signal was sent by the process itself 2011-03-10 11:59:39 -05:00
Rich Felker
98e02144da use rt_sigprocmask, not legacy sigprocmask, syscall in pthread exit code 2011-02-19 15:21:05 -05:00
Rich Felker
19eb13b9a4 race condition fix: block all signals before decrementing thread count
the existence of a (kernelspace) thread must never have observable
effects after the thread count is decremented. if signals are not
blocked, it could end up handling the signal for rsyscall and
contributing towards the count of threads which have changed ids,
causing a thread to be missed. this could lead to one thread retaining
unwanted privilege level.

this change may also address other subtle race conditions in
application code that uses signals.
2011-02-19 11:04:36 -05:00
Rich Felker
fb11b6b85e make pthread_exit run dtors for last thread, wait to decrement thread count 2011-02-19 10:38:57 -05:00
Rich Felker
e882756311 reorganize pthread data structures and move the definitions to alltypes.h
this allows sys/types.h to provide the pthread types, as required by
POSIX. this design also facilitates forcing ABI-compatible sizes in
the arch-specific alltypes.h, while eliminating the need for
developers changing the internals of the pthread types to poke around
with arch-specific headers they may not be able to test.
2011-02-17 17:16:20 -05:00
Rich Felker
0b2006c8fe begin unifying clone/thread management interface in preparation for porting 2011-02-15 03:24:58 -05:00
Rich Felker
59666802fb make pthread_create return EAGAIN on resource failure, as required by POSIX 2011-02-15 02:20:21 -05:00
Rich Felker
1a9a2ff7b0 reorganize thread exit code, make pthread_exit call cancellation handlers (pt2) 2011-02-13 19:58:30 -05:00
Rich Felker
0b44a0315b initial check-in, version 0.5.0 2011-02-12 00:22:29 -05:00