1. 30 7月, 2011 2 次提交
    • R
      add proper fuxed-based locking for stdio · dba68bf9
      Rich Felker 提交于
      previously, stdio used spinlocks, which would be unacceptable if we
      ever add support for thread priorities, and which yielded
      pathologically bad performance if an application attempted to use
      flockfile on a key file as a major/primary locking mechanism.
      
      i had held off on making this change for fear that it would hurt
      performance in the non-threaded case, but actually support for
      recursive locking had already inflicted that cost. by having the
      internal locking functions store a flag indicating whether they need
      to perform unlocking, rather than using the actual recursive lock
      counter, i was able to combine the conditionals at unlock time,
      eliminating any additional cost, and also avoid a nasty corner case
      where a huge number of calls to ftrylockfile could cause deadlock
      later at the point of internal locking.
      
      this commit also fixes some issues with usage of pthread_self
      conflicting with __attribute__((const)) which resulted in crashes with
      some compiler versions/optimizations, mainly in flockfile prior to
      pthread_create.
      dba68bf9
    • R
      new attempt at making set*id() safe and robust · acb04806
      Rich Felker 提交于
      changing credentials in a multi-threaded program is extremely
      difficult on linux because it requires synchronizing the change
      between all threads, which have their own thread-local credentials on
      the kernel side. this is further complicated by the fact that changing
      the real uid can fail due to exceeding RLIMIT_NPROC, making it
      possible that the syscall will succeed in some threads but fail in
      others.
      
      the old __rsyscall approach being replaced was robust in that it would
      report failure if any one thread failed, but in this case, the program
      would be left in an inconsistent state where individual threads might
      have different uid. (this was not as bad as glibc, which would
      sometimes even fail to report the failure entirely!)
      
      the new approach being committed refuses to change real user id when
      it cannot temporarily set the rlimit to infinity. this is completely
      POSIX conformant since POSIX does not require an implementation to
      allow real-user-id changes for non-privileged processes whatsoever.
      still, setting the real uid can fail due to memory allocation in the
      kernel, but this can only happen if there is not already a cached
      object for the target user. thus, we forcibly serialize the syscalls
      attempts, and fail the entire operation on the first failure. this
      *should* lead to an all-or-nothing success/failure result, but it's
      still fragile and highly dependent on kernel developers not breaking
      things worse than they're already broken.
      
      ideally linux will eventually add a CLONE_USERCRED flag that would
      give POSIX conformant credential changes without any hacks from
      userspace, and all of this code would become redundant and could be
      removed ~10 years down the line when everyone has abandoned the old
      broken kernels. i'm not holding my breath...
      acb04806
  2. 14 6月, 2011 3 次提交
    • R
      fix race condition in pthread_kill · 7779dbd2
      Rich Felker 提交于
      if thread id was reused by the kernel between the time pthread_kill
      read it from the userspace pthread_t object and the time of the tgkill
      syscall, a signal could be sent to the wrong thread. the tgkill
      syscall was supposed to prevent this race (versus the old tkill
      syscall) but it can't; it can only help in the case where the tid is
      reused in a different process, but not when the tid is reused in the
      same process.
      
      the only solution i can see is an extra lock to prevent threads from
      exiting while another thread is trying to pthread_kill them. it should
      be very very cheap in the non-contended case.
      7779dbd2
    • R
      run dtors before taking the exit-lock in pthread exit · f58c8a0f
      Rich Felker 提交于
      previously a long-running dtor could cause pthread_detach to block.
      f58c8a0f
    • R
      minor locking optimizations · 6232b96f
      Rich Felker 提交于
      6232b96f
  3. 08 5月, 2011 3 次提交
    • R
      11e4b925
    • R
    • R
      overhaul implementation-internal signal protections · 99b8a25e
      Rich Felker 提交于
      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.
      99b8a25e
  4. 20 4月, 2011 1 次提交
  5. 18 4月, 2011 3 次提交
  6. 17 4月, 2011 1 次提交
    • R
      overhaul pthread cancellation · feee9890
      Rich Felker 提交于
      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.
      feee9890
  7. 15 4月, 2011 1 次提交
  8. 14 4月, 2011 1 次提交
    • R
      simplify cancellation point handling · 9beb6330
      Rich Felker 提交于
      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.
      9beb6330
  9. 07 4月, 2011 4 次提交
  10. 06 4月, 2011 1 次提交
    • R
      new framework to inhibit thread cancellation when needed · 729cb49f
      Rich Felker 提交于
      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.
      729cb49f
  11. 04 4月, 2011 5 次提交
    • R
      pthread_create need not set errno · 7fd39952
      Rich Felker 提交于
      7fd39952
    • R
      block all signals during rsyscall · 66def4e7
      Rich Felker 提交于
      otherwise a signal handler could see an inconsistent and nonconformant
      program state where different threads have different uids/gids.
      66def4e7
    • R
      fix race condition in rsyscall handler · 1ad049b7
      Rich Felker 提交于
      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.
      1ad049b7
    • R
      don't trust siginfo in rsyscall handler · c9b2d801
      Rich Felker 提交于
      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.
      c9b2d801
    • R
      simplify calling of timer signal handler · f01d3518
      Rich Felker 提交于
      f01d3518
  12. 03 4月, 2011 1 次提交
  13. 02 4月, 2011 2 次提交
  14. 30 3月, 2011 1 次提交
    • R
      major improvements to cancellation handling · bf619d82
      Rich Felker 提交于
      - 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.
      bf619d82
  15. 26 3月, 2011 1 次提交
    • R
      match glibc/lsb cancellation abi on i386 · ea343364
      Rich Felker 提交于
      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.
      ea343364
  16. 25 3月, 2011 1 次提交
    • R
      overhaul cancellation to fix resource leaks and dangerous behavior with signals · b470030f
      Rich Felker 提交于
      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...
      b470030f
  17. 20 3月, 2011 3 次提交
    • R
      global cleanup to use the new syscall interface · aa398f56
      Rich Felker 提交于
      aa398f56
    • R
      syscall overhaul part two - unify public and internal syscall interface · 685e40bb
      Rich Felker 提交于
      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.
      685e40bb
    • R
      overhaul syscall interface · d00ff295
      Rich Felker 提交于
      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.
      d00ff295
  18. 16 3月, 2011 1 次提交
  19. 13 3月, 2011 1 次提交
  20. 11 3月, 2011 2 次提交
  21. 20 2月, 2011 2 次提交