1. 14 10月, 2012 1 次提交
    • R
      workaround broken hidden-visibility handling in pcc · 36be5284
      Rich Felker 提交于
      with this change, pcc-built musl libc.so seems to work correctly. the
      problem is that pcc generates GOT lookups for external-linkage symbols
      even if they are hidden, rather than using GOT-relative addressing.
      the entire reason we're using hidden visibility on the __libc object
      is to make it accessible prior to relocations -- not to mention
      inexpensive to access. unfortunately, the workaround makes it even
      more expensive on pcc.
      
      when the pcc issue is fixed, an appropriate version test should be
      added so new pcc can use the much more efficient variant.
      36be5284
  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. 27 7月, 2012 1 次提交
  4. 01 6月, 2012 1 次提交
    • R
      enable LARGEFILE64 aliases · a5412976
      Rich Felker 提交于
      these will NOT be used when compiling with -D_LARGEFILE64_SOURCE on
      musl; instead, they exist in the hopes of eventually being able to run
      some glibc-linked apps with musl sitting in place of glibc.
      
      also remove the (apparently incorrect) fcntl alias.
      a5412976
  5. 23 5月, 2012 1 次提交
    • R
      remove everything related to forkall · 0c29adfe
      Rich Felker 提交于
      i made a best attempt, but the intended semantics of this function are
      fundamentally contradictory. there is no consistent way to handle
      ownership of locks when forking a multi-threaded process. the code
      could have worked by accident for programs that only used normal
      mutexes and nothing else (since they don't actually store or care
      about their owner), but that's about it. broken-by-design interfaces
      that aren't even in glibc (only solaris) don't belong in musl.
      0c29adfe
  6. 25 4月, 2012 1 次提交
    • R
      ditch the priority inheritance locks; use malloc's version of lock · 4750cf42
      Rich Felker 提交于
      i did some testing trying to switch malloc to use the new internal
      lock with priority inheritance, and my malloc contention test got
      20-100 times slower. if priority inheritance futexes are this slow,
      it's simply too high a price to pay for avoiding priority inversion.
      maybe we can consider them somewhere down the road once the kernel
      folks get their act together on this (and perferably don't link it to
      glibc's inefficient lock API)...
      
      as such, i've switch __lock to use malloc's implementation of
      lightweight locks, and updated all the users of the code to use an
      array with a waiter count for their locks. this should give optimal
      performance in the vast majority of cases, and it's simple.
      
      malloc is still using its own internal copy of the lock code because
      it seems to yield measurably better performance with -O3 when it's
      inlined (20% or more difference in the contention stress test).
      4750cf42
  7. 24 4月, 2012 1 次提交
    • R
      new internal locking primitive; drop spinlocks · f34d0ea5
      Rich Felker 提交于
      we use priority inheritance futexes if possible so that the library
      cannot hit internal priority inversion deadlocks in the presence of
      realtime priority scheduling (full support to be added later).
      f34d0ea5
  8. 25 2月, 2012 1 次提交
  9. 24 2月, 2012 1 次提交
    • R
      cleanup and work around visibility bug in gcc 3 that affects x86_64 · bae2e52b
      Rich Felker 提交于
      in gcc 3, the visibility attribute must be placed on both the
      declaration and on the definition. if it's omitted from the
      definition, the compiler fails to emit the ".hidden" directive in the
      assembly, and the linker will either generate textrels (if supported,
      such as on i386) or refuse to link (on targets where certain types of
      textrels are forbidden or impossible without further assumptions about
      memory layout, such as on x86_64).
      
      this patch also unifies the decision about when to use visibility into
      libc.h and makes the visibility in the utf-8 state machine tables
      based on libc.h rather than a duplicate test.
      bae2e52b
  10. 23 8月, 2011 1 次提交
    • R
      security hardening: ensure suid programs have valid stdin/out/err · df0b5a49
      Rich Felker 提交于
      this behavior (opening fds 0-2 for a suid program) is explicitly
      allowed (but not required) by POSIX to protect badly-written suid
      programs from clobbering files they later open.
      
      this commit does add some cost in startup code, but the availability
      of auxv and the security flag will be useful elsewhere in the future.
      in particular auxv is needed for static-linked vdso support, which is
      still waiting to be committed (sorry nik!)
      df0b5a49
  11. 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
  12. 07 8月, 2011 2 次提交
  13. 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
  14. 21 4月, 2011 1 次提交
    • R
      fix minor bugs due to incorrect threaded-predicate semantics · 870cc679
      Rich Felker 提交于
      some functions that should have been testing whether pthread_self()
      had been called and initialized the thread pointer were instead
      testing whether pthread_create() had been called and actually made the
      program "threaded". while it's unlikely any mismatch would occur in
      real-world problems, this could have introduced subtle bugs. now, we
      store the address of the main thread's thread descriptor in the libc
      structure and use its presence as a flag that the thread register is
      initialized. note that after fork, the calling thread (not necessarily
      the original main thread) is the new main thread.
      870cc679
  15. 18 4月, 2011 2 次提交
    • R
      clean up handling of thread/nothread mode, locking · 9080cc15
      Rich Felker 提交于
      9080cc15
    • R
      optimize cancellation enable/disable code · ebf82447
      Rich Felker 提交于
      the goal is to be able to use pthread_setcancelstate internally in
      the implementation, whenever a function might want to use functions
      which are cancellation points but avoid becoming a cancellation point
      itself. i could have just used a separate internal function for
      temporarily inhibiting cancellation, but the solution in this commit
      is better because (1) it's one less implementation-specific detail in
      functions that need to use it, and (2) application code can also get
      the same benefit.
      
      previously, pthread_setcancelstate dependend on pthread_self, which
      would pull in unwanted thread setup overhead for non-threaded
      programs. now, it temporarily stores the state in the global libc
      struct if threads have not been initialized, and later moves it if
      needed. this way we can instead use __pthread_self, which has no
      dependencies and assumes that the thread register is already valid.
      ebf82447
  16. 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
  17. 07 4月, 2011 1 次提交
    • R
      move rsyscall out of pthread_create module · b2486a89
      Rich Felker 提交于
      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.
      b2486a89
  18. 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
  19. 04 4月, 2011 1 次提交
  20. 03 4月, 2011 1 次提交
  21. 02 4月, 2011 1 次提交
    • R
      reorganize the __libc structure for threaded performance issues · 952987a0
      Rich Felker 提交于
      we want to keep atomically updated fields (locks and thread count) and
      really anything writable far away from frequently-needed function
      pointers. stuff some rarely-needed function pointers in between to
      pad, hopefully up to a cache line boundary.
      952987a0
  22. 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
  23. 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
  24. 13 3月, 2011 1 次提交
  25. 25 2月, 2011 1 次提交
    • R
      various changes in preparation for dynamic linking support · 41d51836
      Rich Felker 提交于
      prefer using visibility=hidden for __libc internal data, rather than
      an accessor function, if the compiler has visibility.
      
      optimize with -O3 for PIC targets (shared library). without heavy
      inlining, reloading the GOT register in small functions kills
      performance. 20-30% size increase for a single libc.so is not a big
      deal, compared to comparaible size increase in every static binaries.
      
      use -Bsymbolic-functions, not -Bsymbolic. global variables are subject
      to COPY relocations, and thus binding their addresses in the library
      at link time will cause library functions to read the wrong (original)
      copies instead of the copies made in the main program's bss section.
      
      add entry point, _start, for dynamic linker.
      41d51836
  26. 21 2月, 2011 1 次提交
    • R
      use an accessor function for __libc data pointer when compiled as PIC · d89c9e8a
      Rich Felker 提交于
      prior to this change, a large portion of libc was unusable prior to
      relocation by the dynamic linker, due to dependence on the global data
      in the __libc structure and the need to obtain its address through the
      GOT. with this patch, the accessor function __libc_loc is now able to
      obtain the address of __libc via PC-relative addressing without using
      the GOT. this means the majority of libc functionality is now
      accessible right away.
      
      naturally, the above statements all depend on having an architecture
      where PC-relative addressing and jumps/calls are feasible, and a
      compiler that generates the appropriate code.
      d89c9e8a
  27. 19 2月, 2011 1 次提交
    • R
      add pthread_atfork interface · e9417fff
      Rich Felker 提交于
      note that this presently does not handle consistency of the libc's own
      global state during forking. as per POSIX 2008, if the parent process
      was threaded, the child process may only call async-signal-safe
      functions until one of the exec-family functions is called, so the
      current behavior is believed to be conformant even if non-ideal. it
      may be improved at some later time.
      e9417fff
  28. 12 2月, 2011 1 次提交