1. 28 6月, 2016 1 次提交
    • R
      fix failure to obtain EOWNERDEAD status for process-shared robust mutexes · 384d103d
      Rich Felker 提交于
      Linux's documentation (robust-futex-ABI.txt) claims that, when a
      process dies with a futex on the robust list, bit 30 (0x40000000) is
      set to indicate the status. however, what actually happens is that
      bits 0-30 are replaced with the value 0x40000000, i.e. bits 0-29
      (containing the old owner tid) are cleared at the same time bit 30 is
      set.
      
      our userspace-side code for robust mutexes was written based on that
      documentation, assuming that kernel would never produce a futex value
      of 0x40000000, since the low (owner) bits would always be non-zero.
      commit d338b506 introduced this
      assumption explicitly while fixing another bug in how non-recoverable
      status for robust mutexes was tracked. presumably the tests conducted
      at that time only checked non-process-shared robust mutexes, which are
      handled in pthread_exit (which implemented the documented kernel
      protocol, not the actual one) rather than by the kernel.
      
      change pthread_exit robust list processing to match the kernel
      behavior, clearing bits 0-29 while setting bit 30, and use the value
      0x7fffffff instead of 0x40000000 to encode non-recoverable status. the
      choice of value here is arbitrary; any value with at least one of bits
      0-29 set should work just as well,
      384d103d
  2. 18 6月, 2015 1 次提交
  3. 16 6月, 2015 1 次提交
    • R
      refactor stdio open file list handling, move it out of global libc struct · 1b0cdc87
      Rich Felker 提交于
      functions which open in-memory FILE stream variants all shared a tail
      with __fdopen, adding the FILE structure to stdio's open file list.
      replacing this common tail with a function call reduces code size and
      duplication of logic. the list is also partially encapsulated now.
      
      function signatures were chosen to facilitate tail call optimization
      and reduce the need for additional accessor functions.
      
      with these changes, static linked programs that do not use stdio no
      longer have an open file list at all.
      1b0cdc87
  4. 16 5月, 2015 1 次提交
    • R
      eliminate costly tricks to avoid TLS access for current locale state · 68630b55
      Rich Felker 提交于
      the code being removed used atomics to track whether any threads might
      be using a locale other than the current global locale, and whether
      any threads might have abstract 8-bit (non-UTF-8) LC_CTYPE active, a
      feature which was never committed (still pending). the motivations
      were to support early execution prior to setup of the thread pointer,
      to partially support systems (ancient kernels) where thread pointer
      setup is not possible, and to avoid high performance cost on archs
      where accessing the thread pointer may be very slow.
      
      since commit 19a1fe67, the thread
      pointer is always available, so these hacks are no longer needed.
      removing them greatly simplifies the affected code.
      68630b55
  5. 07 5月, 2015 1 次提交
    • R
      fix stack protector crashes on x32 & powerpc due to misplaced TLS canary · 484194db
      Rich Felker 提交于
      i386, x86_64, x32, and powerpc all use TLS for stack protector canary
      values in the default stack protector ABI, but the location only
      matched the ABI on i386 and x86_64. on x32, the expected location for
      the canary contained the tid, thus producing spurious mismatches
      (resulting in process termination) upon fork. on powerpc, the expected
      location contained the stdio_locks list head, so returning from a
      function after calling flockfile produced spurious mismatches. in both
      cases, the random canary was not present, and a predictable value was
      used instead, making the stack protector hardening much less effective
      than it should be.
      
      in the current fix, the thread structure has been expanded to have
      canary fields at all three possible locations, and archs that use a
      non-default location must define a macro in pthread_arch.h to choose
      which location is used. for most archs (which lack TLS canary ABI) the
      choice does not matter.
      484194db
  6. 19 4月, 2015 1 次提交
    • R
      make dlerror state and message thread-local and dynamically-allocated · 01d42747
      Rich Felker 提交于
      this fixes truncation of error messages containing long pathnames or
      symbol names.
      
      the dlerror state was previously required by POSIX to be global. the
      resolution of bug 97 relaxed the requirements to allow thread-safe
      implementations of dlerror with thread-local state and message buffer.
      01d42747
  7. 14 4月, 2015 1 次提交
    • R
      remove remnants of support for running in no-thread-pointer mode · 19a1fe67
      Rich Felker 提交于
      since 1.1.0, musl has nominally required a thread pointer to be setup.
      most of the remaining code that was checking for its availability was
      doing so for the sake of being usable by the dynamic linker. as of
      commit 71f099cb, this is no longer
      necessary; the thread pointer is now valid before any libc code
      (outside of dynamic linker bootstrap functions) runs.
      
      this commit essentially concludes "phase 3" of the "transition path
      for removing lazy init of thread pointer" project that began during
      the 1.1.0 release cycle.
      19a1fe67
  8. 10 4月, 2015 4 次提交
    • R
      apply vmlock wait to __unmapself in pthread_exit · a2d30533
      Rich Felker 提交于
      a2d30533
    • R
      redesign and simplify vmlock system · f08ab9e6
      Rich Felker 提交于
      this global lock allows certain unlock-type primitives to exclude
      mmap/munmap operations which could change the identity of virtual
      addresses while references to them still exist.
      
      the original design mistakenly assumed mmap/munmap would conversely
      need to exclude the same operations which exclude mmap/munmap, so the
      vmlock was implemented as a sort of 'symmetric recursive rwlock'. this
      turned out to be unnecessary.
      
      commit 25d12fc0 already shortened the
      interval during which mmap/munmap held their side of the lock, but
      left the inappropriate lock design and some inefficiency.
      
      the new design uses a separate function, __vm_wait, which does not
      hold any lock itself and only waits for lock users which were already
      present when it was called to release the lock. this is sufficient
      because of the way operations that need to be excluded are sequenced:
      the "unlock-type" operations using the vmlock need only block
      mmap/munmap operations that are precipitated by (and thus sequenced
      after) the atomic-unlock they perform while holding the vmlock.
      
      this allows for a spectacular lack of synchronization in the __vm_wait
      function itself.
      f08ab9e6
    • R
      optimize out setting up robust list with kernel when not needed · 4e98cce1
      Rich Felker 提交于
      as a result of commit 12e1e324, kernel
      processing of the robust list is only needed for process-shared
      mutexes. previously the first attempt to lock any owner-tracked mutex
      resulted in robust list initialization and a set_robust_list syscall.
      this is no longer necessary, and since the kernel's record of the
      robust list must now be cleared at thread exit time for detached
      threads, optimizing it out is more worthwhile than before too.
      4e98cce1
    • R
      process robust list in pthread_exit to fix detached thread use-after-unmap · 12e1e324
      Rich Felker 提交于
      the robust list head lies in the thread structure, which is unmapped
      before exit for detached threads. this leaves the kernel unable to
      process the exiting thread's robust list, and with a dangling pointer
      which may happen to point to new unrelated data at the time the kernel
      processes it.
      
      userspace processing of the robust list was already needed for
      non-pshared robust mutexes in order to perform private futex wakes
      rather than the shared ones the kernel would do, but it was
      conditional on linking pthread_mutexattr_setrobust and did not bother
      processing the pshared mutexes in the list, which requires additional
      logic for the robust list pending slot in case pthread_exit is
      interrupted by asynchronous process termination.
      
      the new robust list processing code is linked unconditionally (inlined
      in pthread_exit), handles both private and shared mutexes, and also
      removes the kernel's reference to the robust list before unmapping and
      exit if the exiting thread is detached.
      12e1e324
  9. 17 2月, 2015 1 次提交
  10. 16 1月, 2015 1 次提交
    • R
      overhaul __synccall and fix AS-safety and other issues in set*id · 78a8ef47
      Rich Felker 提交于
      multi-threaded set*id and setrlimit use the internal __synccall
      function to work around the kernel's wrongful treatment of these
      process properties as thread-local. the old implementation of
      __synccall failed to be AS-safe, despite POSIX requiring setuid and
      setgid to be AS-safe, and was not rigorous in assuring that all
      threads were caught. in a worst case, threads late in the process of
      exiting could retain permissions after setuid reported success, in
      which case attacks to regain dropped permissions may have been
      possible under the right conditions.
      
      the new implementation of __synccall depends on the presence of
      /proc/self/task and will fail if it can't be opened, but is able to
      determine that it has caught all threads, and does not use any locks
      except its own. it thereby achieves AS-safety simply by blocking
      signals to preclude re-entry in the same thread.
      
      with this commit, all known conformance and safety issues in set*id
      functions should be fixed.
      78a8ef47
  11. 07 9月, 2014 2 次提交
    • R
      add C11 thread creation and related thread functions · 23614b0f
      Rich Felker 提交于
      based on patch by Jens Gustedt.
      
      the main difficulty here is handling the difference between start
      function signatures and thread return types for C11 threads versus
      POSIX threads. pointers to void are assumed to be able to represent
      faithfully all values of int. the function pointer for the thread
      start function is cast to an incorrect type for passing through
      pthread_create, but is cast back to its correct type before calling so
      that the behavior of the call is well-defined.
      
      changes to the existing threads implementation were kept minimal to
      reduce the risk of regressions, and duplication of code that carries
      implementation-specific assumptions was avoided for ease and safety of
      future maintenance.
      23614b0f
    • J
      use weak symbols for the POSIX functions that will be used by C threads · df7d0dfb
      Jens Gustedt 提交于
      The intent of this is to avoid name space pollution of the C threads
      implementation.
      
      This has two sides to it. First we have to provide symbols that wouldn't
      pollute the name space for the C threads implementation. Second we have
      to clean up some internal uses of POSIX functions such that they don't
      implicitly drag in such symbols.
      df7d0dfb
  12. 24 8月, 2014 1 次提交
    • R
      fix false ownership of stdio FILEs due to tid reuse · 5345c9b8
      Rich Felker 提交于
      this is analogous commit fffc5cda
      which fixed the corresponding issue for mutexes.
      
      the robust list can't be used here because the locks do not share a
      common layout with mutexes. at some point it may make sense to simply
      incorporate a mutex object into the FILE structure and use it, but
      that would be a much more invasive change, and it doesn't mesh well
      with the current design that uses a simpler code path for internal
      locking and pulls in the recursive-mutex-like code when the flockfile
      API is used explicitly.
      5345c9b8
  13. 23 8月, 2014 1 次提交
    • R
      fix use of uninitialized memory with application-provided thread stacks · a6293285
      Rich Felker 提交于
      the subsequent code in pthread_create and the code which copies TLS
      initialization images to the new thread's TLS space assume that the
      memory provided to them is zero-initialized, which is true when it's
      obtained by pthread_create using mmap. however, when the caller
      provides a stack using pthread_attr_setstack, pthread_create cannot
      make any assumptions about the contents. simply zero-filling the
      relevant memory in this case is the simplest and safest fix.
      a6293285
  14. 16 8月, 2014 1 次提交
    • R
      enable private futex for process-local robust mutexes · b092f1c5
      Rich Felker 提交于
      the kernel always uses non-private wake when walking the robust list
      when a thread or process exits, so it's not able to wake waiters
      listening with the private futex flag. this problem is solved by doing
      the equivalent in userspace as the last step of pthread_exit.
      
      care is taken to remove mutexes from the robust list before unlocking
      them so that the kernel will not attempt to access them again,
      possibly after another thread locks them. this removal code can treat
      the list as singly-linked, since no further code which would add or
      remove items is able to run at this point. moreover, the pending
      pointer is not needed since the mutexes being unlocked are all
      process-local; in the case of asynchronous process termination, they
      all cease to exist.
      
      since a process-local robust mutex cannot come into existence without
      a call to pthread_mutexattr_setrobust in the same process, the code
      for userspace robust list processing is put in that source file, and
      a weak alias to a dummy function is used to avoid pulling in this
      bloat as part of pthread_exit in static-linked programs.
      b092f1c5
  15. 17 7月, 2014 1 次提交
    • R
      work around constant folding bug 61144 in gcc 4.9.0 and 4.9.1 · a6adb2bc
      Rich Felker 提交于
      previously we detected this bug in configure and issued advice for a
      workaround, but this turned out not to work. since then gcc 4.9.0 has
      appeared in several distributions, and now 4.9.1 has been released
      without a fix despite this being a wrong code generation bug which is
      supposed to be a release-blocker, per gcc policy.
      
      since the scope of the bug seems to affect only data objects (rather
      than functions) whose definitions are overridable, and there are only
      a very small number of these in musl, I am just changing them from
      const to volatile for the time being. simply removing the const would
      be sufficient to make gcc 4.9.1 work (the non-const case was
      inadvertently fixed as part of another change in gcc), and this would
      also be sufficient with 4.9.0 if we forced -O0 on the affected files
      or on the whole build. however it's cleaner to just remove all the
      broken compiler detection and use volatile, which will ensure that
      they are never constant-folded. the quality of a non-broken compiler's
      output should not be affected except for the fact that these objects
      are no longer const and thus possibly add a few bytes to data/bss.
      
      this change can be reconsidered and possibly reverted at some point in
      the future when the broken gcc versions are no longer relevant.
      a6adb2bc
  16. 06 7月, 2014 1 次提交
    • R
      eliminate use of cached pid from thread structure · 83dc6eb0
      Rich Felker 提交于
      the main motivation for this change is to remove the assumption that
      the tid of the main thread is also the pid of the process. (the value
      returned by the set_tid_address syscall was used to fill both fields
      despite it semantically being the tid.) this is historically and
      presently true on linux and unlikely to change, but it conceivably
      could be false on other systems that otherwise reproduce the linux
      syscall api/abi.
      
      only a few parts of the code were actually still using the cached pid.
      in a couple places (aio and synccall) it was a minor optimization to
      avoid a syscall. caching could be reintroduced, but lazily as part of
      the public getpid function rather than at program startup, if it's
      deemed important for performance later. in other places (cancellation
      and pthread_kill) the pid was completely unnecessary; the tkill
      syscall can be used instead of tgkill. this is actually a rather
      subtle issue, since tgkill is supposedly a solution to race conditions
      that can affect use of tkill. however, as documented in the commit
      message for commit 7779dbd2, tgkill
      does not actually solve this race; it just limits it to happening
      within one process rather than between processes. we use a lock that
      avoids the race in pthread_kill, and the use in the cancellation
      signal handler is self-targeted and thus not subject to tid reuse
      races, so both are safe regardless of which syscall (tgkill or tkill)
      is used.
      83dc6eb0
  17. 03 7月, 2014 1 次提交
    • R
      add locale framework · 0bc03091
      Rich Felker 提交于
      this commit adds non-stub implementations of setlocale, duplocale,
      newlocale, and uselocale, along with the data structures and minimal
      code needed for representing the active locale on a per-thread basis
      and optimizing the common case where thread-local locale settings are
      not in use.
      
      at this point, the data structures only contain what is necessary to
      represent LC_CTYPE (a single flag) and LC_MESSAGES (a name for use in
      finding message translation files). representation for the other
      categories will be added later; the expectation is that a single
      pointer will suffice for each.
      
      for LC_CTYPE, the strings "C" and "POSIX" are treated as special; any
      other string is accepted and treated as "C.UTF-8". for other
      categories, any string is accepted after being truncated to a maximum
      supported length (currently 15 bytes). for LC_MESSAGES, the name is
      kept regardless of whether libc itself can use such a message
      translation locale, since applications using catgets or gettext should
      be able to use message locales libc is not aware of. for other
      categories, names which are not successfully loaded as locales (which,
      at present, means all names) are treated as aliases for "C". setlocale
      never fails.
      
      locale settings are not yet used anywhere, so this commit should have
      no visible effects except for the contents of the string returned by
      setlocale.
      0bc03091
  18. 10 6月, 2014 2 次提交
    • R
      simplify errno implementation · ac31bf27
      Rich Felker 提交于
      the motivation for the errno_ptr field in the thread structure, which
      this commit removes, was to allow the main thread's errno to keep its
      address when lazy thread pointer initialization was used. &errno was
      evaluated prior to setting up the thread pointer and stored in
      errno_ptr for the main thread; subsequently created threads would have
      errno_ptr pointing to their own errno_val in the thread structure.
      
      since lazy initialization was removed, there is no need for this extra
      level of indirection; __errno_location can simply return the address
      of the thread's errno_val directly. this does cause &errno to change,
      but the change happens before entry to application code, and thus is
      not observable.
      ac31bf27
    • R
      replace all remaining internal uses of pthread_self with __pthread_self · df15168c
      Rich Felker 提交于
      prior to version 1.1.0, the difference between pthread_self (the
      public function) and __pthread_self (the internal macro or inline
      function) was that the former would lazily initialize the thread
      pointer if it was not already initialized, whereas the latter would
      crash in this case. since lazy initialization is no longer supported,
      use of pthread_self no longer makes sense; it simply generates larger,
      slower code.
      df15168c
  19. 25 3月, 2014 2 次提交
    • R
      fix pointer type mismatch and misplacement of const · 689e0e6b
      Rich Felker 提交于
      689e0e6b
    • R
      always initialize thread pointer at program start · dab441ae
      Rich Felker 提交于
      this is the first step in an overhaul aimed at greatly simplifying and
      optimizing everything dealing with thread-local state.
      
      previously, the thread pointer was initialized lazily on first access,
      or at program startup if stack protector was in use, or at certain
      random places where inconsistent state could be reached if it were not
      initialized early. while believed to be fully correct, the logic was
      fragile and non-obvious.
      
      in the first phase of the thread pointer overhaul, support is retained
      (and in some cases improved) for systems/situation where loading the
      thread pointer fails, e.g. old kernels.
      
      some notes on specific changes:
      
      - the confusing use of libc.main_thread as an indicator that the
        thread pointer is initialized is eliminated in favor of an explicit
        has_thread_pointer predicate.
      
      - sigaction no longer needs to ensure that the thread pointer is
        initialized before installing a signal handler (this was needed to
        prevent a situation where the signal handler caused the thread
        pointer to be initialized and the subsequent sigreturn cleared it
        again) but it still needs to ensure that implementation-internal
        thread-related signals are not blocked.
      
      - pthread tsd initialization for the main thread is deferred in a new
        manner to minimize bloat in the static-linked __init_tp code.
      
      - pthread_setcancelstate no longer needs special handling for the
        situation before the thread pointer is initialized. it simply fails
        on systems that cannot support a thread pointer, which are
        non-conforming anyway.
      
      - pthread_cleanup_push/pop now check for missing thread pointer and
        nop themselves out in this case, so stdio no longer needs to avoid
        the cancellable path when the thread pointer is not available.
      
      a number of cases remain where certain interfaces may crash if the
      system does not support a thread pointer. at this point, these should
      be limited to pthread interfaces, and the number of such cases should
      be fewer than before.
      dab441ae
  20. 16 9月, 2013 2 次提交
  21. 15 9月, 2013 1 次提交
    • S
      support configurable page size on mips, powerpc and microblaze · b20760c0
      Szabolcs Nagy 提交于
      PAGE_SIZE was hardcoded to 4096, which is historically what most
      systems use, but on several archs it is a kernel config parameter,
      user space can only know it at execution time from the aux vector.
      
      PAGE_SIZE and PAGESIZE are not defined on archs where page size is
      a runtime parameter, applications should use sysconf(_SC_PAGE_SIZE)
      to query it. Internally libc code defines PAGE_SIZE to libc.page_size,
      which is set to aux[AT_PAGESZ] in __init_libc and early in __dynlink
      as well. (Note that libc.page_size can be accessed without GOT, ie.
      before relocations are done)
      
      Some fpathconf settings are hardcoded to 4096, these should be actually
      queried from the filesystem using statfs.
      b20760c0
  22. 27 4月, 2013 7 次提交
  23. 06 4月, 2013 1 次提交
  24. 01 4月, 2013 1 次提交
    • R
      implement pthread_getattr_np · 14a835b3
      Rich Felker 提交于
      this function is mainly (purely?) for obtaining stack address
      information, but we also provide the detach state since it's easy to
      do anyway.
      14a835b3
  25. 27 3月, 2013 1 次提交
    • R
      remove __SYSCALL_SSLEN arch macro in favor of using public _NSIG · ccc7b4c3
      Rich Felker 提交于
      the issue at hand is that many syscalls require as an argument the
      kernel-ABI size of sigset_t, intended to allow the kernel to switch to
      a larger sigset_t in the future. previously, each arch was defining
      this size in syscall_arch.h, which was redundant with the definition
      of _NSIG in bits/signal.h. as it's used in some not-quite-portable
      application code as well, _NSIG is much more likely to be recognized
      and understood immediately by someone reading the code, and it's also
      shorter and less cluttered.
      
      note that _NSIG is actually 65/129, not 64/128, but the division takes
      care of throwing away the off-by-one part.
      ccc7b4c3
  26. 02 2月, 2013 2 次提交