1. 14 6月, 2015 2 次提交
    • R
      refactor malloc's expand_heap to share with __simple_malloc · e3bc22f1
      Rich Felker 提交于
      this extends the brk/stack collision protection added to full malloc
      in commit 276904c2 to also protect the
      __simple_malloc function used in static-linked programs that don't
      reference the free function.
      
      it also extends support for using mmap when brk fails, which full
      malloc got in commit 54463033, to
      __simple_malloc.
      
      since __simple_malloc may expand the heap by arbitrarily large
      increments, the stack collision detection is enhanced to detect
      interval overlap rather than just proximity of a single address to the
      stack. code size is increased a bit, but this is partly offset by the
      sharing of code between the two malloc implementations, which due to
      linking semantics, both get linked in a program that needs the full
      malloc with realloc/free support.
      e3bc22f1
    • R
      remove cancellation points in stdio · 4ef9b828
      Rich Felker 提交于
      commit 58165923 added these optional
      cancellation points on the basis that cancellable stdio could be
      useful, to unblock threads stuck on stdio operations that will never
      complete. however, the only way to ensure that cancellation can
      achieve this is to violate the rules for side effects when
      cancellation is acted upon, discarding knowledge of any partial data
      transfer already completed. our implementation exhibited this behavior
      and was thus non-conforming.
      
      in addition to improving correctness, removing these cancellation
      points moderately reduces code size, and should significantly improve
      performance on i386, where sysenter/syscall instructions can be used
      instead of "int $128" for non-cancellable syscalls.
      4ef9b828
  2. 13 6月, 2015 3 次提交
    • R
      fix idiom for setting stdio stream orientation to wide · 536c6d5a
      Rich Felker 提交于
      the old idiom, f->mode |= f->mode+1, was adapted from the idiom for
      setting byte orientation, f->mode |= f->mode-1, but the adaptation was
      incorrect. unless the stream was alreasdy set byte-oriented, this code
      incremented f->mode each time it was executed, which would eventually
      lead to overflow. it could be fixed by changing it to f->mode |= 1,
      but upcoming changes will require slightly more work at the time of
      wide orientation, so it makes sense to just call fwide. as an
      optimization in the single-character functions, fwide is only called
      if the stream is not already wide-oriented.
      536c6d5a
    • R
      add printing of null %s arguments as "(null)" in wide printf · f8f565df
      Rich Felker 提交于
      this is undefined, but supported in our implementation of the normal
      printf, so for consistency the wide variant should support it too.
      f8f565df
    • R
      add %m support to wide printf · f9e25d81
      Rich Felker 提交于
      f9e25d81
  3. 11 6月, 2015 1 次提交
  4. 10 6月, 2015 3 次提交
    • R
      implement arch-generic version of __unmapself · c30cbcb0
      Rich Felker 提交于
      this can be used to put off writing an asm version of __unmapself for
      new archs, or as a permanent solution on archs where it's not
      practical or even possible to run momentarily with no stack.
      
      the concept here is simple: the caller takes a lock on a global shared
      stack and uses it to make the munmap and exit syscalls. the only trick
      is unlocking, which must be done after the thread exits, and this is
      achieved by using the set_tid_address syscall to have the kernel zero
      and futex-wake the lock word as part of the exit syscall.
      c30cbcb0
    • R
      in malloc, refuse to use brk if it grows into stack · 276904c2
      Rich Felker 提交于
      the linux/nommu fdpic ELF loader sets up the brk range to overlap
      entirely with the main thread's stack (but growing from opposite
      ends), so that the resulting failure mode for malloc is not to return
      a null pointer but to start returning pointers to memory that overlaps
      with the caller's stack. needless to say this extremely dangerous and
      makes brk unusable.
      
      since it's non-trivial to detect execution environments that might be
      affected by this kernel bug, and since the severity of the bug makes
      any sort of detection that might yield false-negatives unsafe, we
      instead check the proximity of the brk to the stack pointer each time
      the brk is to be expanded. both the main thread's stack (where the
      real known risk lies) and the calling thread's stack are checked. an
      arbitrary gap distance of 8 MB is imposed, chosen to be larger than
      linux default main-thread stack reservation sizes and larger than any
      reasonable stack configuration on nommu.
      
      the effeciveness of this patch relies on an assumption that the amount
      by which the brk is being grown is smaller than the gap limit, which
      is always true for malloc's use of brk. reliance on this assumption is
      why the check is being done in malloc-specific code and not in __brk.
      276904c2
    • R
      fix spurious errors from pwd/grp functions when nscd backend is absent · bd1eacea
      Rich Felker 提交于
      for several pwd/grp functions, the only way the caller can distinguish
      between a successful negative result ("no such user/group") and an
      internal error is by clearing errno before the call and checking errno
      afterwards. the nscd backend support code correctly simulated a
      not-found response on systems where such a backend is not running, but
      failed to restore errno.
      
      this commit also fixed an outdated/incorrect comment.
      bd1eacea
  5. 08 6月, 2015 1 次提交
    • R
      fix regression in pre-v7 arm on kernels with kuser helper removed · 75ce4503
      Rich Felker 提交于
      the arm atomics/TLS runtime selection code is called from
      __set_thread_area and depends on having libc.auxv and __hwcap
      available. commit 71f099cb moved the
      first call to __set_thread_area to the top of dynamic linking stage 3,
      before this data is made available, causing the runtime detection code
      to always see __hwcap as zero and thereby select the atomics/TLS
      implementations based on kuser helper.
      
      upcoming work on superh will use similar runtime detection.
      
      ideally this early-init code should be cleanly refactored and shared
      between the dynamic linker and static-linked startup.
      75ce4503
  6. 07 6月, 2015 6 次提交
  7. 05 6月, 2015 1 次提交
  8. 04 6月, 2015 1 次提交
    • R
      fix dynamic linker regression processing R_*_NONE type relocations · b6a6cd70
      Rich Felker 提交于
      commit f3ddd173 inadvertently removed
      the early check for "none" type relocations, causing the address
      dso->base+0 to be dereferenced to obtain an addend. shared libraries,
      (including libc.so) and PIE executables were unaffected, since their
      base addresses are the actual address of their mappings and are
      readable. non-PIE main executables, however, have a base address of 0
      because their load addresses are absolute and not offset at load time.
      
      in practice none-type relocations do not arise with toolchains that
      are in use except on mips, and on mips it's moderately rare for a
      non-PIE executable to have a relocation table, since the mips-specific
      got processing serves in its place for most purposes.
      b6a6cd70
  9. 29 5月, 2015 1 次提交
    • R
      fix failure of ungetc and ungetwc to work on files in eof status · 2b4fcfda
      Rich Felker 提交于
      these functions were written to handle clearing eof status, but failed
      to account for the __toread function's handling of eof. with this
      patch applied, __toread still returns EOF when the file is in eof
      status, so that read operations will fail, but it also sets up valid
      buffer pointers for read mode, which are set to the end of the buffer
      rather than the beginning in order to make the whole buffer available
      to ungetc/ungetwc.
      
      minor changes to __uflow were needed since it's now possible to have
      non-zero buffer pointers while in eof status. as made, these changes
      remove a 'fast path' bypassing the function call to __toread, which
      could be reintroduced with slightly different logic, but since
      ordinary files have a syscall in f->read, optimizing the code path
      does not seem worthwhile.
      
      the __stdio_read function is also updated not to zero the read buffer
      pointers on eof/error. while not necessary for correctness, this
      change avoids the overhead of calling __toread in ungetc after
      reaching eof, and it also reduces code size and increases consistency
      with the fmemopen read operation which does not zero the pointers.
      2b4fcfda
  10. 28 5月, 2015 1 次提交
    • R
      implement fail-safe static locales for newlocale · aeeac9ca
      Rich Felker 提交于
      this frees applications which need to make temporary use of the C
      locale (via uselocale) from the possibility that newlocale might fail.
      
      the C.UTF-8 locale is also provided as a static locale. presently they
      behave the same, but this may change in the future.
      aeeac9ca
  11. 27 5月, 2015 3 次提交
    • R
      rename internal locale file handling locale maps · 11858d31
      Rich Felker 提交于
      since the __setlocalecat function was removed, the filename
      __setlocalecat.c no longer made sense.
      11858d31
    • R
      overhaul locale internals to treat categories roughly uniformly · 61a3364d
      Rich Felker 提交于
      previously, LC_MESSAGES was treated specially as the only category
      which could be set to a locale name without a definition file, in
      order to facilitate gettext message translations when no libc locale
      was available. LC_NUMERIC was completely un-settable, and LC_CTYPE
      stored a flag intended to be used for a possible future byte-based C
      locale, instead of storing a __locale_map pointer like the other
      categories use.
      
      this patch changes all categories to be represented by pointers to
      __locale_map structures, and allows locale names without definition
      files to be treated as valid locales with trivial definition when used
      in any category. outwardly visible functional changes should be minor,
      limited mainly to the strings read back from setlocale and the way
      gettext handles translations in categories other than LC_MESSAGES.
      
      various internal refactoring has also been performed, and improvements
      in const correctness have been made.
      61a3364d
    • R
      replace atomics with locks in locale-setting code · 63c188ec
      Rich Felker 提交于
      this is part of a general program of removing direct use of atomics
      where they are not necessary to meet correctness or performance needs,
      but in this case it's also an optimization. only the global locale
      needs synchronization; allocated locales referenced with locale_t
      handles are immutable during their lifetimes, and using atomics to
      initialize them increases their cost of setup.
      63c188ec
  12. 26 5月, 2015 3 次提交
    • R
      reprocess all libc/ldso symbolic relocations in dynamic linking stage 3 · 9bbddf73
      Rich Felker 提交于
      commit f3ddd173 introduced early
      relocations and subsequent reprocessing as part of the dynamic linker
      bootstrap overhaul, to allow use of arbitrary libc functions before
      the main application and libraries are loaded, but only reprocessed
      GOT/PLT relocation types.
      
      commit c093e2e8 added reprocessing of
      non-GOT/PLT relocations to fix an actual regression that was observed
      on powerpc, but only for RELA format tables with out-of-line addends.
      REL table (inline addends at the relocation address) reprocessing is
      trickier because the first relocation pass clobbers the addends.
      
      this patch extends symbolic relocation reprocessing for libc/ldso to
      support all relocation types, whether REL or RELA format tables are
      used. it is believed not to alter behavior on any existing archs for
      the current dynamic linker and libc code. the motivations for this
      change are consistency and future-proofing. it ensures that behavior
      does not differ depending on whether REL or RELA tables are used,
      which could lead to undetected arch-specific bugs. it also ensures
      that, if in the future code depending on additional relocation types
      is added to libc.so, either at the source level or as part of the
      compiler runtime that gets pulled in (for example, soft-float with TLS
      for fenv), the new code will work properly.
      
      the implementation concept is simple: stage 2 of the dynamic linker
      counts the number of symbolic relocations in the libc/ldso REL table
      and allocates a VLA to save their addends into; stage 3 then uses the
      saved addends in place of the inline ones which were clobbered. for
      stack safety, a hard limit (currently 4k) is imposed on the number of
      such addends; this should be a couple orders of magnitude larger than
      the actual need. this number is not a runtime variable that could
      break fail-safety; it is constant for a given libc.so build.
      9bbddf73
    • R
      move call to dynamic linker stage-3 into stage-2 function · 768b82c6
      Rich Felker 提交于
      this move eliminates a duplicate "by-hand" symbol lookup loop from the
      stage-1 code and replaces it with a call to find_sym, which can be
      used once we're in stage 2. it reduces the size of the stage 1 code,
      which is helpful because stage 1 will become the crt start file for
      static-PIE executables, and it will allow stage 3 to access stage 2's
      automatic storage, which will be important in an upcoming commit.
      768b82c6
    • R
      mark mips cancellable syscall code as code · 7b75c487
      Rich Felker 提交于
      otherwise disassemblers treat it as data.
      7b75c487
  13. 25 5月, 2015 2 次提交
    • R
      simplify/shrink relocation processing in dynamic linker stage 1 · 0e0e4942
      Rich Felker 提交于
      the outer-loop approach made sense when we were also processing
      DT_JMPREL, which might be in REL or RELA form, to avoid major code
      duplication. commit 09db855b removed
      processing of DT_JMPREL, and in the remaining two tables, the format
      (REL or RELA) is known by the name of the table. simply writing two
      versions of the loop results in smaller and simpler code.
      0e0e4942
    • R
      remove processing of DT_JMPREL from dynamic linker stage 1 bootstrap · 09db855b
      Rich Felker 提交于
      the DT_JMPREL relocation table necessarily consists entirely of
      JMP_SLOT (REL_PLT in internal nomenclature) relocations, which are
      symbolic; they cannot be resolved in stage 1, so there is no point in
      processing them.
      09db855b
  14. 22 5月, 2015 2 次提交
  15. 19 5月, 2015 2 次提交
    • R
      reprocess libc/ldso RELA relocations in stage 3 of dynamic linking · c093e2e8
      Rich Felker 提交于
      this fixes a regression on powerpc that was introduced in commit
      f3ddd173. global data accesses on
      powerpc seem to be using a translation-unit-local GOT filled via
      R_PPC_ADDR32 relocations rather than R_PPC_GLOB_DAT. being a non-GOT
      relocation type, these were not reprocessed after adding the main
      application and its libraries to the chain, causing libc code not to
      see copy relocations in the main program, and therefore to use the
      pre-copy-relocation addresses for global data objects (like environ).
      
      the motivation for the dynamic linker only reprocessing GOT/PLT
      relocation types in stage 3 is that these types always have a zero
      addend, making them safe to process again even if the storage for the
      addend has been clobbered. other relocation types which can be used
      for address constants in initialized data objects may have non-zero
      addends which will be clobbered during the first pass of relocation
      processing if they're stored inline (REL form) rather than out-of-line
      (RELA form).
      
      powerpc generally uses only RELA, so this patch is sufficient to fix
      the regression in practice, but is not fully general, and would not
      suffice if an alternate toolchain generated REL for powerpc.
      c093e2e8
    • R
      fix null pointer dereference in dcngettext under specific conditions · 43e9f652
      Rich Felker 提交于
      if setlocale has not been called, the current locale's messages_name
      may be a null pointer. the code path where it's assumed to be non-null
      was only reachable if bindtextdomain had already been called, which is
      normally not done in programs which do not call setlocale, so the
      omitted check went unnoticed.
      
      patch from Void Linux, with description rewritten.
      43e9f652
  16. 16 5月, 2015 2 次提交
    • 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
    • R
      in i386 __set_thread_area, don't assume %gs register is initially zero · 707d7c30
      Rich Felker 提交于
      commit f630df09 added logic to handle
      the case where __set_thread_area is called more than once by reusing
      the GDT slot already in the %gs register, and only setting up a new
      GDT slot when %gs is zero. this created a hidden assumption that %gs
      is zero when a new process image starts, which is true in practice on
      Linux, but does not seem to be documented ABI, and fails to hold under
      qemu app-level emulation.
      
      while it would in theory be possible to zero %gs in the entry point
      code, this code is shared between static and dynamic binaries, and
      dynamic binaries must not clobber the value of %gs already setup by
      the dynamic linker.
      
      the alternative solution implemented in this commit simply uses global
      data to store the GDT index that's selected. __set_thread_area should
      only be called in the initial thread anyway (subsequent threads get
      their thread pointer setup by __clone), but even if it were called by
      another thread, it would simply read and write back the same GDT index
      that was already assigned to the initial thread, and thus (in the x86
      memory model) there is no data race.
      707d7c30
  17. 07 5月, 2015 2 次提交
    • R
      fix futimes legacy function with null tv pointer · ece0c48a
      Rich Felker 提交于
      a null pointer is valid here and indicates that the current time
      should be used. based on patch by Felix Janda, simplified.
      ece0c48a
    • 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
  18. 02 5月, 2015 4 次提交