1. 10 4月, 2015 3 次提交
    • 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
  2. 17 2月, 2015 1 次提交
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 16 9月, 2013 2 次提交
  14. 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
  15. 27 4月, 2013 7 次提交
  16. 06 4月, 2013 1 次提交
  17. 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
  18. 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
  19. 02 2月, 2013 3 次提交
    • R
      fix stale locks left behind when pthread_create fails · 72768ea9
      Rich Felker 提交于
      this bug seems to have been around a long time.
      72768ea9
    • R
      if pthread_create fails, it must not attempt mmap if there is no mapping · 077549e0
      Rich Felker 提交于
      this bug was introduced when support for application-provided stacks
      was originally added.
      077549e0
    • R
      pthread stack treatment overhaul for application-provided stacks, etc. · d5142642
      Rich Felker 提交于
      the main goal of these changes is to address the case where an
      application provides a stack of size N, but TLS has size M that's a
      significant portion of the size N (or even larger than N), thus giving
      the application less stack space than it expected or no stack at all!
      
      the new strategy pthread_create now uses is to only put TLS on the
      application-provided stack if TLS is smaller than 1/8 of the stack
      size or 2k, whichever is smaller. this ensures that the application
      always has "close enough" to what it requested, and the threshold is
      chosen heuristically to make sure "sane" amounts of TLS still end up
      in the application-provided stack.
      
      if TLS does not fit the above criteria, pthread_create uses mmap to
      obtain space for TLS, but still uses the application-provided stack
      for actual call frame stack. this is to avoid wasting memory, and for
      the sake of supporting ugly hacks like garbage collection based on
      assumptions that the implementation will use the provided stack range.
      
      in order for the above heuristics to ever succeed, the amount of TLS
      space wasted on POSIX TSD (pthread_key_create based) needed to be
      reduced. otherwise, these changes would preclude any use of
      pthread_create without mmap, which would have serious memory usage and
      performance costs for applications trying to create huge numbers of
      threads using pre-allocated stack space. the new value of
      PTHREAD_KEYS_MAX is the minimum allowed by POSIX, 128. this should
      still be plenty more than real-world applications need, especially now
      that C11/gcc-style TLS is now supported in musl, and most apps and
      libraries choose to use that instead of POSIX TSD when available.
      
      at the same time, PTHREAD_STACK_MIN has been decreased. it was
      originally set to PAGE_SIZE back when there was no support for TLS or
      application-provided stacks, and requests smaller than a whole page
      did not make sense. now, there are two good reasons to support
      requests smaller than a page: (1) applications could provide
      pre-allocated stacks smaller than a page, and (2) with smaller stack
      sizes, stack+TLS+TSD can all fit in one page, making it possible for
      applications which need huge numbers of threads with minimal stack
      needs to allocate exactly one page per thread. the new value of
      PTHREAD_STACK_MIN, 2k, is aligned with the minimum size for
      sigaltstack.
      d5142642
  20. 12 11月, 2012 1 次提交
    • R
      add support for thread scheduling (POSIX TPS option) · 1e21e78b
      Rich Felker 提交于
      linux's sched_* syscalls actually implement the TPS (thread
      scheduling) functionality, not the PS (process scheduling)
      functionality which the sched_* functions are supposed to have.
      omitting support for the PS option (and having the sched_* interfaces
      fail with ENOSYS rather than omitting them, since some broken software
      assumes they exist) seems to be the only conforming way to do this on
      linux.
      1e21e78b
  21. 09 11月, 2012 1 次提交
    • R
      clean up sloppy nested inclusion from pthread_impl.h · efd4d87a
      Rich Felker 提交于
      this mirrors the stdio_impl.h cleanup. one header which is not
      strictly needed, errno.h, is left in pthread_impl.h, because since
      pthread functions return their error codes rather than using errno,
      nearly every single pthread function needs the errno constants.
      
      in a few places, rather than bringing in string.h to use memset, the
      memset was replaced by direct assignment. this seems to generate much
      better code anyway, and makes many functions which were previously
      non-leaf functions into leaf functions (possibly eliminating a great
      deal of bloat on some platforms where non-leaf functions require ugly
      prologue and/or epilogue).
      efd4d87a
  22. 16 10月, 2012 1 次提交
    • R
      add support for TLS variant I, presently needed for arm and mips · 9ec4283b
      Rich Felker 提交于
      despite documentation that makes it sound a lot different, the only
      ABI-constraint difference between TLS variants II and I seems to be
      that variant II stores the initial TLS segment immediately below the
      thread pointer (i.e. the thread pointer points to the end of it) and
      variant I stores the initial TLS segment above the thread pointer,
      requiring the thread descriptor to be stored below. the actual value
      stored in the thread pointer register also tends to have per-arch
      random offsets applied to it for silly micro-optimization purposes.
      
      with these changes applied, TLS should be basically working on all
      supported archs except microblaze. I'm still working on getting the
      necessary information and a working toolchain that can build TLS
      binaries for microblaze, but in theory, static-linked programs with
      TLS and dynamic-linked programs where only the main executable uses
      TLS should already work on microblaze.
      
      alignment constraints have not yet been heavily tested, so it's
      possible that this code does not always align TLS segments correctly
      on archs that need TLS variant I.
      9ec4283b
  23. 15 10月, 2012 1 次提交
  24. 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
  25. 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