1. 07 Jun, 2018 1 commit
    • alloc: make memory allocators use function pointers · 9865cd16
      Currently, our memory allocators are being redirected to the correct
      implementation at compile time by simply using macros. In order to make
      them swappable at runtime, this commit reshuffles that by instead making
      use of a global "git_allocator" structure, whose pointers are set up to
      reference the allocator functions. Like this, it becomes easy to swap
      out allocators by simply setting these function pointers.
      
      In order to initialize a "git_allocator", our provided allocators
      "stdalloc" and "crtdbg" both provide an init function. This is being
      called to initialize a passed in allocator struct and set up its members
      correctly.
      
      No support is yet included to enable users of libgit2 to switch out the
      memory allocator at a global level.
      Patrick Steinhardt committed
  2. 04 May, 2018 1 commit
    • global: adjust init count under lock · 0933fdc5
      Our global initialization functions `git_libgit2_init()` and
      `git_libgit2_shutdown()` both adjust a global init counter to determine
      whether we are the first respectively last user of libgit2. On
      Unix-systems do not do so under lock, though, which opens the
      possibility of a race between these two functions:
      
          Thread 1                            Thread 2
                                  git__n_inits = 0;
          git_libgit2_init();
          git_atomic_inc(&git__n_inits);
          /* git__n_inits == 1 */
                                              git_libgit2_shutdown();
                                              if (git_atomic_dec(&git__n_inits) != 0)
                                                  /* git__n_inits == 0, no early exit here */
                                              pthread_mutex_lock(&_init_mutex);
                                              shutdown_common();
                                              pthread_mutex_unlock(&_init_mutex);
          pthread_mutex_lock(&_init_mutex);
          init_once();
          pthread_mutex_unlock(&_init_mutex);
      
      So we can end up in a situation where we try to shutdown shared data
      structures before they have been initialized.
      
      Fix the race by always locking `_init_mutex` before incrementing or
      decrementing `git__n_inits`.
      Patrick Steinhardt committed
  3. 11 Apr, 2018 1 commit
  4. 28 Feb, 2018 1 commit
    • curl: explicitly initialize and cleanup global curl state · 2022b004
      Our curl-based streams make use of the easy curl interface. This
      interface automatically initializes and de-initializes the global curl
      state by calling out to `curl_global_init` and `curl_global_cleanup`.
      Thus, all global state will be repeatedly re-initialized when creating
      multiple curl streams in succession. Despite being inefficient, this is
      not thread-safe due to `curl_global_init` being not thread-safe itself.
      Thus a multi-threaded programing handling multiple curl streams at the
      same time is inherently racy.
      
      Fix the issue by globally initializing and cleaning up curl's state.
      Patrick Steinhardt committed
  5. 23 Oct, 2017 1 commit
  6. 03 Jul, 2017 1 commit
    • Make sure to always include "common.h" first · 0c7f49dd
      Next to including several files, our "common.h" header also declares
      various macros which are then used throughout the project. As such, we
      have to make sure to always include this file first in all
      implementation files. Otherwise, we might encounter problems or even
      silent behavioural differences due to macros or defines not being
      defined as they should be. So in fact, our header and implementation
      files should make sure to always include "common.h" first.
      
      This commit does so by establishing a common include pattern. Header
      files inside of "src" will now always include "common.h" as its first
      other file, separated by a newline from all the other includes to make
      it stand out as special. There are two cases for the implementation
      files. If they do have a matching header file, they will always include
      this one first, leading to "common.h" being transitively included as
      first file. If they do not have a matching header file, they instead
      include "common.h" as first file themselves.
      
      This fixes the outlined problems and will become our standard practice
      for header and source files inside of the "src/" from now on.
      Patrick Steinhardt committed
  7. 02 May, 2017 1 commit
  8. 02 Nov, 2016 1 commit
    • global: reset global state on shutdown without threading · 038f0e1b
      When threading is not enabled for libgit2, we keep global state
      in a simple static variable. When libgit2 is shut down, we clean
      up the global state by freeing the global state's dynamically
      allocated memory. When libgit2 is built with threading, we
      additionally free the thread-local storage and thus completely
      remove the global state. In a non-threaded build, though, we
      simply leave the global state as-is, which may result in an error
      upon reinitializing libgit2.
      
      Fix the issue by zeroing out the variable on a shutdown, thus
      returning it to its initial state.
      Patrick Steinhardt committed
  9. 01 Nov, 2016 1 commit
    • global: synchronize initialization and shutdown with pthreads · 59c6c286
      When trying to initialize and tear down global data structures
      from different threads at once with `git_libgit2_init` and
      `git_libgit2_shutdown`, we race around initializing data. While
      we use `pthread_once` to assert that we only initilize data a
      single time, we actually reset the `pthread_once_t` on the last
      call to `git_libgit2_shutdown`. As resetting this variable is not
      synchronized with other threads trying to access it, this is
      actually racy when one thread tries to do a complete shutdown of
      libgit2 while another thread tries to initialize it.
      
      Fix the issue by creating a mutex which synchronizes `init_once`
      and the library shutdown.
      Patrick Steinhardt committed
  10. 04 Aug, 2016 1 commit
  11. 20 Jun, 2016 1 commit
  12. 07 Jun, 2016 1 commit
    • global: clean up crt only after freeing tls data · 432af52b
      The thread local storage is used to hold some global state that
      is dynamically allocated and should be freed upon exit. On
      Windows, we clean up the C run-time right after execution of
      registered shutdown callbacks and before cleaning up the TLS.
      
      When we clean up the CRT, we also cause it to analyze for memory
      leaks. As we did not free the TLS yet this will lead to false
      positives.
      
      Fix the issue by first freeing the TLS and cleaning up the CRT
      only afterwards.
      Patrick Steinhardt committed
  13. 01 Jun, 2016 1 commit
  14. 18 Mar, 2016 1 commit
  15. 17 Mar, 2016 1 commit
  16. 14 Mar, 2016 1 commit
  17. 03 Mar, 2016 1 commit
    • ssh: initialize libssh2 · 22f3d3aa
      We should have been doing this, but it initializes itself upon first
      use, which works as long as nobody's doing concurrent network
      operations. Initialize it on our init to make sure it's not getting
      initialized concurrently.
      Carlos Martín Nieto committed
  18. 19 Feb, 2016 1 commit
  19. 09 Feb, 2016 1 commit
  20. 08 Feb, 2016 1 commit
  21. 12 Nov, 2015 1 commit
  22. 28 Jul, 2015 1 commit
  23. 22 Jul, 2015 1 commit
  24. 29 Jun, 2015 1 commit
  25. 08 Jun, 2015 1 commit
  26. 23 Apr, 2015 1 commit
  27. 18 Apr, 2015 1 commit
  28. 17 Apr, 2015 3 commits
  29. 04 Mar, 2015 3 commits
  30. 23 Dec, 2014 1 commit
  31. 12 Dec, 2014 1 commit
  32. 05 Dec, 2014 1 commit
  33. 03 Dec, 2014 1 commit
  34. 17 Nov, 2014 1 commit
  35. 08 Nov, 2014 1 commit
  36. 03 Nov, 2014 1 commit