1. 08 10月, 2012 1 次提交
    • R
      clean up and refactor program initialization · 0a96a37f
      Rich Felker 提交于
      the code in __libc_start_main is now responsible for parsing auxv,
      rather than duplicating the parsing all over the place. this should
      shave off a few cycles and some code size. __init_libc is left as an
      external-linkage function despite the fact that it could be static, to
      prevent it from being inlined and permanently wasting stack space when
      main is called.
      
      a few other minor changes are included, like eliminating per-thread
      ssp canaries (they were likely broken when combined with certain
      dlopen usages, and completely unnecessary) and some other unnecessary
      checks. since this code gets linked into every program, it should be
      as small and simple as possible.
      0a96a37f
  2. 05 10月, 2012 2 次提交
    • R
      support for TLS in dynamic-loaded (dlopen) modules · dcd60371
      Rich Felker 提交于
      unlike other implementations, this one reserves memory for new TLS in
      all pre-existing threads at dlopen-time, and dlopen will fail with no
      resources consumed and no new libraries loaded if memory is not
      available. memory is not immediately distributed to running threads;
      that would be too complex and too costly. instead, assurances are made
      that threads needing the new TLS can obtain it in an async-signal-safe
      way from a buffer belonging to the dynamic linker/new module (via
      atomic fetch-and-add based allocator).
      
      I've re-appropriated the lock that was previously used for __synccall
      (synchronizing set*id() syscalls between threads) as a general
      pthread_create lock. it's a "backwards" rwlock where the "read"
      operation is safe atomic modification of the live thread count, which
      multiple threads can perform at the same time, and the "write"
      operation is making sure the count does not increase during an
      operation that depends on it remaining bounded (__synccall or dlopen).
      in static-linked programs that don't use __synccall, this lock is a
      no-op and has no cost.
      dcd60371
    • R
      TLS (GNU/C11 thread-local storage) support for static-linked programs · 8431d797
      Rich Felker 提交于
      the design for TLS in dynamic-linked programs is mostly complete too,
      but I have not yet implemented it. cost is nonzero but still low for
      programs which do not use TLS and/or do not use threads (a few hundred
      bytes of new code, plus dependency on memcpy). i believe it can be
      made smaller at some point by merging __init_tls and __init_security
      into __libc_start_main and avoiding duplicate auxv-parsing code.
      
      at the same time, I've also slightly changed the logic pthread_create
      uses to allocate guard pages to ensure that guard pages are not
      counted towards commit charge.
      8431d797
  3. 07 9月, 2012 2 次提交
    • R
      further use of _Noreturn, for non-plain-C functions · 0c05bd3a
      Rich Felker 提交于
      note that POSIX does not specify these functions as _Noreturn, because
      POSIX is aligned with C99, not the new C11 standard. when POSIX is
      eventually updated to C11, it will almost surely give these functions
      the _Noreturn attribute. for now, the actual _Noreturn keyword is not
      used anyway when compiling with a c99 compiler, which is what POSIX
      requires; the GCC __attribute__ is used instead if it's available,
      however.
      
      in a few places, I've added infinite for loops at the end of _Noreturn
      functions to silence compiler warnings. presumably
      __buildin_unreachable could achieve the same thing, but it would only
      work on newer GCCs and would not be portable. the loops should have
      near-zero code size cost anyway.
      
      like the previous _Noreturn commit, this one is based on patches
      contributed by philomath.
      0c05bd3a
    • R
      use restrict everywhere it's required by c99 and/or posix 2008 · 400c5e5c
      Rich Felker 提交于
      to deal with the fact that the public headers may be used with pre-c99
      compilers, __restrict is used in place of restrict, and defined
      appropriately for any supported compiler. we also avoid the form
      [restrict] since older versions of gcc rejected it due to a bug in the
      original c99 standard, and instead use the form *restrict.
      400c5e5c
  4. 10 8月, 2012 1 次提交
    • R
      fix (hopefully) all hard-coded 8's for kernel sigset_t size · 2f437040
      Rich Felker 提交于
      some minor changes to how hard-coded sets for thread-related purposes
      are handled were also needed, since the old object sizes were not
      necessarily sufficient. things have gotten a bit ugly in this area,
      and i think a cleanup is in order at some point, but for now the goal
      is just to get the code working on all supported archs including mips,
      which was badly broken by linux rejecting syscalls with the wrong
      sigset_t size.
      2f437040
  5. 12 7月, 2012 2 次提交
    • R
      fix several locks that weren't updated right for new futex-based __lock · bbbe87e3
      Rich Felker 提交于
      these could have caused memory corruption due to invalid accesses to
      the next field. all should be fixed now; I found the errors with fgrep
      -r '__lock(&', which is bogus since the argument should be an array.
      bbbe87e3
    • R
      fix potential race condition in detached threads · 92f8396b
      Rich Felker 提交于
      after the thread unmaps its own stack/thread structure, the kernel,
      performing child tid clear and futex wake, could clobber a new mapping
      made at the same location as the just-removed thread's tid field.
      disable kernel clearing of child tid to prevent this.
      92f8396b
  6. 10 6月, 2012 1 次提交
    • R
      add pthread_attr_setstack interface (and get) · 819006a8
      Rich Felker 提交于
      i originally omitted these (optional, per POSIX) interfaces because i
      considered them backwards implementation details. however, someone
      later brought to my attention a fairly legitimate use case: allocating
      thread stacks in memory that's setup for sharing and/or fast transfer
      between CPU and GPU so that the thread can move data to a GPU directly
      from automatic-storage buffers without having to go through additional
      buffer copies.
      
      perhaps there are other situations in which these interfaces are
      useful too.
      819006a8
  7. 03 6月, 2012 1 次提交
  8. 24 5月, 2012 1 次提交
  9. 05 5月, 2012 1 次提交
    • R
      make pthread stacks non-executable · 7e4d7946
      Rich Felker 提交于
      this change is necessary or pthread_create will always fail on
      security-hardened kernels. i considered first trying to make the stack
      executable and simply retrying without execute permissions when the
      first try fails, but (1) this would incur a serious performance
      penalty on hardened systems, and (2) having the stack be executable is
      just a bad idea from a security standpoint.
      
      if there is real-world "GNU C" code that uses nested functions with
      threads, and it can't be fixed, we'll have to consider other ways of
      solving the problem, but for now this seems like the best fix.
      7e4d7946
  10. 04 5月, 2012 1 次提交
    • R
      overhaul SSP support to use a real canary · 58aa5f45
      Rich Felker 提交于
      pthread structure has been adjusted to match the glibc/GCC abi for
      where the canary is stored on i386 and x86_64. it will need variants
      for other archs to provide the added security of the canary's entropy,
      but even without that it still works as well as the old "minimal" ssp
      support. eventually such changes will be made anyway, since they are
      also needed for GCC/C11 thread-local storage support (not yet
      implemented).
      
      care is taken not to attempt initializing the thread pointer unless
      the program actually uses SSP (by reference to __stack_chk_fail).
      58aa5f45
  11. 28 2月, 2012 1 次提交
  12. 10 2月, 2012 1 次提交
    • R
      small fix for new pthread cleanup stuff · 2230218c
      Rich Felker 提交于
      even if pthread_create/exit code is not linked, run flag needs to be
      checked and cleanup function potentially run on pop. thus, move the
      code to the module that's always linked when pthread_cleanup_push/pop
      is used.
      2230218c
  13. 09 2月, 2012 1 次提交
    • R
      replace bad cancellation cleanup abi with a sane one · afc35d5e
      Rich Felker 提交于
      the old abi was intended to duplicate glibc's abi at the expense of
      being ugly and slow, but it turns out glib was not even using that abi
      except on non-gcc-compatible compilers (which it doesn't even support)
      and was instead using an exceptions-in-c/unwind-based approach whose
      abi we could not duplicate anyway without nasty dwarf2/unwind
      integration.
      
      the new abi is copied from a very old glibc abi, which seems to still
      be supported/present in current glibc. it avoids all unwinding,
      whether by sjlj or exceptions, and merely maintains a linked list of
      cleanup functions to be called from the context of pthread_exit. i've
      made some care to ensure that longjmp out of a cleanup function should
      work, even though it is not required to.
      
      this change breaks abi compatibility with programs which were using
      pthread cancellation, which is unfortunate, but that's why i'm making
      the change now rather than later. considering that most pthread
      features have not been usable until recently anyway, i don't see it as
      a major issue at this point.
      afc35d5e
  14. 28 9月, 2011 1 次提交
  15. 18 9月, 2011 1 次提交
    • R
      overhaul clone syscall wrapping · 3f72cdac
      Rich Felker 提交于
      several things are changed. first, i have removed the old __uniclone
      function signature and replaced it with the "standard" linux
      __clone/clone signature. this was necessary to expose clone to
      applications anyway, and it makes it easier to port __clone to new
      archs, since it's now testable independently of pthread_create.
      
      secondly, i have removed all references to the ugly ldt descriptor
      structure (i386 only) from the c code and pthread structure. in places
      where it is needed, it is now created on the stack just when it's
      needed, in assembly code. thus, the i386 __clone function takes the
      desired thread pointer as its argument, rather than an ldt descriptor
      pointer, just like on all other sane archs. this should not affect
      applications since there is really no way an application can use clone
      with threads/tls in a way that doesn't horribly conflict with and
      clobber the underlying implementation's use. applications are expected
      to use clone only for creating actual processes, possibly with new
      namespace features and whatnot.
      3f72cdac
  16. 12 8月, 2011 1 次提交
    • R
      pthread and synccall cleanup, new __synccall_wait op · 407d9330
      Rich Felker 提交于
      fix up clone signature to match the actual behavior. the new
      __syncall_wait function allows a __synccall callback to wait for other
      threads to continue without returning, so that it can resume action
      after the caller finishes. this interface could be made significantly
      more general/powerful with minimal effort, but i'll wait to do that
      until it's actually useful for something.
      407d9330
  17. 04 8月, 2011 3 次提交
    • R
      further debloat cancellation handlers · 5f37fc13
      Rich Felker 提交于
      cleanup push and pop are also no-ops if pthread_exit is not reachable.
      this can make a big difference for library code which needs to protect
      itself against cancellation, but which is unlikely to actually be used
      in programs with threads/cancellation.
      5f37fc13
    • R
      missed detail in cancellation bloat fix · 56385dd5
      Rich Felker 提交于
      56385dd5
    • R
      fix static linking dependency bloat with cancellation · 730bee72
      Rich Felker 提交于
      previously, pthread_cleanup_push/pop were pulling in all of
      pthread_create due to dependency on the __pthread_unwind_next
      function. this was not needed, as cancellation cleanup handlers can
      never be called unless pthread_exit or pthread_cancel is reachable.
      730bee72
  18. 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
  19. 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
  20. 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
  21. 20 4月, 2011 1 次提交
  22. 18 4月, 2011 3 次提交
  23. 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
  24. 15 4月, 2011 1 次提交
  25. 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
  26. 07 4月, 2011 3 次提交