summaryrefslogtreecommitdiff
path: root/lib/libthr/thread/thr_sig.c
Commit message (Collapse)AuthorAgeFilesLines
* Use sigfastblock(2) for masking signals in libthr.Konstantin Belousov2020-02-091-6/+61
| | | | | | | | | | | | | | | | | Ensure proper handshake to transfer sigfastblock(2) blocking word ownership from rtld to libthr. Unfortunately sigfastblock(2) is not enough to stop intercepting signals in libthr, because critical sections must ensure more than just signal blocking. Tested by: pho Disscussed with: cem, emaste, jilles Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D12773 Notes: svn path=/head/; revision=357698
* Avoid conflicts with libc symbols in libthr jump table.Konstantin Belousov2019-07-311-2/+3
| | | | | | | | | | | | | | | | | | | In some corner cases of static linking and unexpected libraries order on the linker command line, libc symbol might preempt the same libthr symbol, in which case libthr jump table points back to libc causing either infinite recursion or loop. Handle all of such symbols by using private libthr names for them, ensuring that the right pointers are installed into the table. In collaboration with: arichardson PR: 239475 Tested by: pho MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D21088 Notes: svn path=/head/; revision=350481
* lib: further adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-261-1/+3
| | | | | | | | | | | | | | | Mainly focus on files that use BSD 2-Clause license, however the tool I was using mis-identified many licenses so this was mostly a manual - error prone - task. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. Notes: svn path=/head/; revision=326219
* libthr: fix style in previous commitEric van Gyzen2017-05-261-1/+1
| | | | | | | | | | | I intended to add this to the previous commit. Reviewed by: kib MFC after: 3 days Sponsored by: Dell EMC Notes: svn path=/head/; revision=318953
* libthr: prevent setcontext() from masking SIGTHREric van Gyzen2017-05-261-1/+1
| | | | | | | | | | | | | | | | | __thr_setcontext() mistakenly tested for the presence of SIGCANCEL in its local ucontext_t instead of the parameter. Therefore, if a thread calls setcontext() with a context whose signal mask contains SIGTHR (a.k.a. SIGCANCEL), that signal will be blocked, preventing the thread from being cancelled or suspended. Reported by: gcc 6.1 via RISC-V tinderbox Reviewed by: kib MFC after: 3 days Sponsored by: Dell EMC Differential Revision: https://reviews.freebsd.org/D10933 Notes: svn path=/head/; revision=318952
* libthr: fix warnings at WARNS=6Eric van Gyzen2017-05-191-1/+1
| | | | | | | | | | | | | | | | | | | Fix warnings about the following when WARNS=6 (which I will commit soon): - casting away const - no previous 'extern' declaration for non-static variable - others as explained by #pragmas and comments - unused parameters The last is the only functional change. Reviewed by: kib MFC after: 3 days Sponsored by: Dell EMC Differential Revision: https://reviews.freebsd.org/D10808 Notes: svn path=/head/; revision=318539
* Do not leak THR_FLAGS_SUSPENDED from the previous suspend/resumeKonstantin Belousov2016-05-051-3/+1
| | | | | | | | | | | | | | | | | cycle. The flag currently is cleared by the resumed thread. If next suspend request comes before the thread was able to clean the flag, in which case suspender skip the thread. Instead, clear the THR_FLAGS_SUSPEND flag in resume_common(), we do not care how much code was executed in the resumed thread when the pthread_resume_*np(s) functions returned. PR: 209233 Reported by: Lawrence Esswood <le277@cam.ac.uk> MFC after: 1 week Notes: svn path=/head/; revision=299114
* Use __FBSDID() for .c files from lib/libthr/thread.Konstantin Belousov2016-04-081-2/+3
| | | | | | | Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=297706
* Pre-resolve symbols required for the deferred signal processing. ThisKonstantin Belousov2015-08-101-1/+2
| | | | | | | | | | | | | | | | avoids recursion into rtld when leaving libthr critical section for the deferred signal delivery. For the same reason, use syscall(2) instead of referencing __sys_sigreturn(2). Syscall() is already pre-resolved for fork() interceptor. Tested by: Andre Meiser <ortadur@web.de> Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=286582
* Fix typo in comment.Konstantin Belousov2015-06-141-2/+2
| | | | | | | MFC after: 3 days Notes: svn path=/head/; revision=284385
* Fix bug in r276630. Do not allow pthread_sigmask() to block SIGCANCEL.Konstantin Belousov2015-01-211-1/+2
| | | | | | | | | Reported and tested by: royger Sponsored by: The FreeBSD Foundation MFC after: 3 days Notes: svn path=/head/; revision=277488
* Reduce the size of the interposing table and amount ofKonstantin Belousov2015-01-111-17/+0
| | | | | | | | | | | | | | | | | | | | cancellation-handling code in the libthr. Translate some syscalls into their more generic counterpart, and remove translated syscalls from the table. List of the affected syscalls: creat, open -> openat raise -> thr_kill sleep, usleep -> nanosleep pause -> sigsuspend wait, wait3, waitpid -> wait4 Suggested and reviewed by: jilles (previous version) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=277032
* Fix known issues which blow up the process after dlopen("libthr.so")Konstantin Belousov2015-01-031-75/+95
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | (or loading a dso linked to libthr.so into process which was not linked against threading library). - Remove libthr interposers of the libc functions, including __error(). Instead, functions calls are indirected through the interposing table, similar to how pthread stubs in libc are already done. Libc by default points either to syscall trampolines or to existing libc implementations. On libthr load, libthr rewrites the pointers to the cancellable implementations already in libthr. The interposition table is separate from pthreads stubs indirection table to not pull pthreads stubs into static binaries. - Postpone the malloc(3) internal mutexes initialization until libthr is loaded. This avoids recursion between calloc(3) and static pthread_mutex_t initialization. - Reinstall signal handlers with wrapper on libthr load. The _rtld_is_dlopened(3) is used to avoid useless calls to sigaction(2) when libthr is statically referenced from the main binary. In the process, fix openat(2), swapcontext(2) and setcontext(2) interposing. The libc symbols were exported at different versions than libthr interposers. Export both libc and libthr versions from libc now, with default set to the higher version from libthr. Remove unused and disconnected swapcontext(3) userspace implementation from libc/gen. No objections from: deischen Tested by: pho, antoine (exp-run) (previous versions) Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=276630
* If check_deferred_signal() execution needs binding of PLT symbol,Konstantin Belousov2013-11-231-2/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | unlocking the rtld bind lock results in the processing of ast and recursing into the check_deferred_signal(). Nested execution of check_deferred_signal() delivers the signal to user code and clears si_signo. On return, top-level check_deferred_signal() frame continues delivering the same signal one more time, but now with zero si_signo. Fix this by adding a flag to indicate that deferred delivery is running, so check_deferred_signal() should avoid doing anything. Since user signal handler is allowed to modify the passed machine context to make return from the signal handler to cause arbitrary jump, or do longjmp(). For this case, also clear the flag in thr_sighandler(), since kernel signal delivery means that nested delivery code should not run right now. Reported by: Vitaly Magerya <vmagerya@gmail.com> Reviewed by: davidxu, jilles Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=258499
* Since the cause of the problems with the __fillcontextx() wasKonstantin Belousov2013-06-031-15/+7
| | | | | | | | | | | | identified, unify the code of check_deferred_signal() for all architectures, making the variant under #ifdef x86 common. Tested by: marius (sparc64) Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Notes: svn path=/head/; revision=251284
* The getcontext() from the __fillcontextx() call in theKonstantin Belousov2013-05-281-2/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | check_deferred_signal() returns twice, since handle_signal() emulates the return from the normal signal handler by sigreturn(2)ing the passed context. Second return is performed on the destroyed stack frame, because __fillcontextx() has already returned. This causes undefined and bad behaviour, usually the victim thread gets SIGSEGV. Avoid nested frame and the need to return from it by doing direct call to getcontext() in the check_deferred_signal() and using a new private libc helper __fillcontextx2() to complement the context with the extended CPU state if the deferred signal is still present. The __fillcontextx() is now unused, but is kept to allow older libthr.so to be used with the new libc. Mark __fillcontextx() as returning twice [1]. Reported by: pgj Pointy hat to: kib Discussed with: dim Tested by: pgj, dim Suggested by: jilles [1] MFC after: 1 week Notes: svn path=/head/; revision=251047
* Partially apply the capitalization of the heading word of the sequenceKonstantin Belousov2013-05-271-3/+3
| | | | | | | | | | and fix typo. Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=251040
* Fix return value for setcontext and swapcontext.David Xu2013-05-091-4/+8
| | | | Notes: svn path=/head/; revision=250402
* Remove extra code for SA_RESETHAND, it is not needed because kernel hasDavid Xu2013-04-281-7/+0
| | | | | | | already done this. Notes: svn path=/head/; revision=250013
* Remove debug code.David Xu2013-04-181-1/+0
| | | | Notes: svn path=/head/; revision=249607
* Avoid copying memory if SIGCANCEL is not masked.David Xu2013-04-181-4/+14
| | | | Notes: svn path=/head/; revision=249606
* Revert revision 249323, the PR/177624 is confusing, that bug is causedDavid Xu2013-04-181-1/+10
| | | | | | | | by using buggy getcontext/setcontext on same stack, while swapcontext normally works on different stack, there is no such a problem. Notes: svn path=/head/; revision=249604
* swapcontext wrapper can not be implemented in C, the stack pointer saved inDavid Xu2013-04-101-10/+1
| | | | | | | | | the context becomes invalid when the function returns, same as setjmp, it must be implemented in assemble language, see discussions in PR misc/177624. Notes: svn path=/head/; revision=249323
* In suspend_common(), don't wait for a thread which is in creation, becauseDavid Xu2012-08-271-1/+2
| | | | | | | | | | pthread_suspend_all_np() may have already suspended its parent thread. Add locking code in pthread_suspend_all_np() to only allow one thread to suspend other threads, this eliminates a deadlock where two or more threads try to suspend each others. Notes: svn path=/head/; revision=239718
* libthr: In the atfork handlers for signals, do not skip the last signal.Jilles Tjoelker2012-03-261-3/+3
| | | | | | | | | | | _SIG_MAXSIG works a bit unexpectedly: signals 1 till _SIG_MAXSIG are valid, both bounds inclusive. Reviewed by: davidxu MFC after: 1 week Notes: svn path=/head/; revision=233516
* Use getcontextx(3) internal API instead of getcontext(2) to provideKonstantin Belousov2012-01-211-4/+13
| | | | | | | | | | | | | | | | the signal handlers with the context information in the deferrred case. Only enable the use of getcontextx(3) in the deferred signal delivery code on amd64 and i386. Sparc64 seems to have some undetermined issues with interaction of alloca(3) and signal delivery. Tested by: flo (who also provided sparc64 harware access for me), pho Discussed with: marius MFC after: 1 month Notes: svn path=/head/; revision=230430
* Fix a typo.David Xu2011-01-111-1/+1
| | | | | | | Submitted by: avg Notes: svn path=/head/; revision=217253
* Return previous sigaction correctly.David Xu2010-10-291-1/+4
| | | | | | | Submitted by: avg Notes: svn path=/head/; revision=214506
* Remove local variable 'first', instead check signal number in memory,David Xu2010-10-291-4/+1
| | | | | | | | because the variable can be in register, second checking the variable may still return true, however this is unexpected. Notes: svn path=/head/; revision=214500
* If we are at cancellation point, always work as deferred mode despiteDavid Xu2010-09-211-34/+32
| | | | | | | | | whether asynchronous mode is turned on or not, this always gives us a chance to decide whether thread should be canceled or not in cancellation points. Notes: svn path=/head/; revision=212952
* Because atfork lock is held while forking, a thread cancellation triggeredDavid Xu2010-09-191-5/+1
| | | | | | | by atfork handler is unsafe, use intenal flag no_cancel to disable it. Notes: svn path=/head/; revision=212841
* Because POSIX does not allow EINTR to be returned from sigwait(),David Xu2010-09-101-6/+8
| | | | | | | | | | | add a wrapper for it in libc and rework the code in libthr, the system call still can return EINTR, we keep this feature. Discussed on: thread Reviewed by: jilles Notes: svn path=/head/; revision=212405
* Fix off-by-one error in function _thr_sigact_unload, also disable theDavid Xu2010-09-061-3/+5
| | | | | | | | function, it seems some gnome application tends to crash if we unregister sigaction automatically. Notes: svn path=/head/; revision=212245
* Remove incorrect comments, also make sure signal isDavid Xu2010-09-011-5/+4
| | | | | | | disabled when unregistering sigaction. Notes: svn path=/head/; revision=212095
* Add signal handler wrapper, the reason to add it becauses there areDavid Xu2010-09-011-99/+419
| | | | | | | | | | | | | | | | | | | | | | | | | | some cases we want to improve: 1) if a thread signal got a signal while in cancellation point, it is possible the TDP_WAKEUP may be eaten by signal handler if the handler called some interruptibly system calls. 2) In signal handler, we want to disable cancellation. 3) When thread holding some low level locks, it is better to disable signal, those code need not to worry reentrancy, sigprocmask system call is avoided because it is a bit expensive. The signal handler wrapper works in this way: 1) libthr installs its signal handler if user code invokes sigaction to install its handler, the user handler is recorded in internal array. 2) when a signal is delivered, libthr's signal handler is invoke, libthr checks if thread holds some low level lock or is in critical region, if it is true, the signal is buffered, and all signals are masked, once the thread leaves critical region, correct signal mask is restored and buffered signal is processed. 3) before user signal handler is invoked, cancellation is temporarily disabled, after user signal handler is returned, cancellation state is restored, and pending cancellation is rescheduled. Notes: svn path=/head/; revision=212076
* Add wrapper for setcontext() and swapcontext(), the wrappersDavid Xu2010-08-241-14/+45
| | | | | | | unblock SIGCANCEL which is needed by thread cancellation. Notes: svn path=/head/; revision=211737
* Reduce redundant code.David Xu2010-08-201-76/+21
| | | | | | | Submitted by: kib Notes: svn path=/head/; revision=211526
* In current implementation, thread cancellation is done in signal handler,David Xu2010-08-201-14/+64
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | which does not know what is the state of interrupted system call, for example, open() system call opened a file and the thread is still cancelled, result is descriptor leak, there are other problems which can cause resource leak or undeterminable side effect when a thread is cancelled. However, this is no longer true in new implementation. In defering mode, a thread is canceled if cancellation request is pending and later the thread enters a cancellation point, otherwise, a later pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and causes target thread to abort system call, userland code in libthr then checks cancellation state, and cancels the thread if needed. For example, the cancellation point open(), the thread may be canceled at start, but later, if it opened a file descriptor, it is not canceled, this avoids file handle leak. Another example is read(), a thread may be canceled at start of the function, but later, if it read some bytes from a socket, the thread is not canceled, the caller then can decide if it should still enable cancelling or disable it and continue reading data until it thinks it has read all bytes of a packet, and keeps a protocol stream in health state, if user ignores partly reading of a packet without disabling cancellation, then second iteration of read loop cause the thread to be cancelled. An exception is that the close() cancellation point always closes a file handle despite whether the thread is cancelled or not. The old mechanism is still kept, for a functions which is not so easily to fix a cancellation problem, the rough mechanism is used. Reviewed by: kib@ Notes: svn path=/head/; revision=211524
* Use _SIG_VALID instead of expanded form of the macro.Konstantin Belousov2010-07-121-1/+1
| | | | | | | | Submitted by: Garrett Cooper <yanegomi gmail com> MFC after: 1 week Notes: svn path=/head/; revision=209933
* Use UMTX_OP_WAIT_UINT_PRIVATE and UMTX_OP_WAKE_PRIVATE to saveDavid Xu2008-04-291-3/+3
| | | | | | | time in kernel(avoid VM lookup). Notes: svn path=/head/; revision=178647
* - Copy signal mask out before THR_UNLOCK(), because THR_UNLOCK() may callDavid Xu2008-03-181-0/+3
| | | | | | | | | | | _thr_suspend_check() which messes sigmask saved in thread structure. - Don't suspend a thread has force_exit set. - In pthread_exit(), if there is a suspension flag set, wake up waiting- thread after setting PS_DEAD, this causes waiting-thread to break loop in suspend_common(). Notes: svn path=/head/; revision=177337
* Increase and decrease in_sigcancel_handler accordingly to avoid possibleDavid Xu2008-03-051-2/+2
| | | | | | | error caused by nested SIGCANCEL stack, it is a bit complex. Notes: svn path=/head/; revision=176818
* If a new thread is created, it inherits current thread's signal masks,David Xu2008-03-041-0/+2
| | | | | | | | | | | however if current thread is executing cancellation handler, signal SIGCANCEL may have already been blocked, this is unexpected, unblock the signal in new thread if this happens. MFC after: 1 week Notes: svn path=/head/; revision=176784
* Add some function prototypes.David Xu2007-11-211-0/+5
| | | | Notes: svn path=/head/; revision=173802
* Remove umtx_t definition, use type long directly, add wrapper functionDavid Xu2007-11-211-1/+2
| | | | | | | | _thr_umtx_wait_uint() for umtx operation UMTX_OP_WAIT_UINT, use the function in semaphore operations, this fixed compiler warnings. Notes: svn path=/head/; revision=173801
* test cancel_pending to save a thr_wake call in some specical cases.David Xu2006-12-061-1/+1
| | | | Notes: svn path=/head/; revision=164929
* Use kernel provided userspace condition variable to implement pthreadDavid Xu2006-12-041-0/+2
| | | | | | | condition variable. Notes: svn path=/head/; revision=164877
* Eliminate atomic operations in thread cancellation functions, it shouldDavid Xu2006-11-241-18/+11
| | | | | | | reduce overheads of cancellation points. Notes: svn path=/head/; revision=164583
* Replace internal usage of struct umtx with umutex which can supportsDavid Xu2006-09-061-4/+4
| | | | | | | real-time if we want, no functionality is changed. Notes: svn path=/head/; revision=162061
* 1. Don't override underscore version of aio_suspend(), system(),David Xu2006-07-251-1/+107
| | | | | | | | | | | | | wait(), waitpid() and usleep(), they are internal versions and should not be cancellation points. 2. Make wait3() as a cancellation point. 3. Move raise() and pause() into file thr_sig.c. 4. Add functions _sigsuspend, _sigwait, _sigtimedwait and _sigwaitinfo, remove SIGCANCEL bit in wait-set for those functions, the signal is used internally to implement thread cancellation. Notes: svn path=/head/; revision=160662