summaryrefslogtreecommitdiff
path: root/src/global.c
Commit message (Collapse)AuthorAgeFilesLines
* global: move init callbacks into an arrayPatrick Steinhardt2019-01-021-14/+22
| | | | | | | | | | | | | | | | | We currently have an explicit callchain of all the initialization callbacks in our `init_common` function. This is perfectly fine, but requires us to manually keep track of how many shutdown callbacks there may be installed: to avoid allocations before libgit2 is fully initialized, we assume that every initializer may register at most one shutdown function. These shutdown functions are stored in a static array of size `MAX_SHUTDOWN_CB`, which then needs to be updated manually whenever a new initializer function is being added. The situation can be easily fixed: convert the callchain of init functions into an array and iterate over it to initialize all subsystems. This allows us to define the `git__shutdown_callbacks` array with the same size as the initializer array and rids us of the need to always update `MAX_SHUTDOWN_CB`.
* stream: provide generic registration APIEdward Thomson2018-11-281-2/+2
| | | | | | | | | Update the new stream registration API to be `git_stream_register` which takes a registration structure and a TLS boolean. This allows callers to register non-TLS streams as well as TLS streams. Provide `git_stream_register_tls` that takes just the init callback for backward compatibliity.
* http: remove cURLEdward Thomson2018-11-281-2/+0
| | | | | We previously used cURL to support HTTP proxies. Now that we've added this support natively, we can remove the curl dependency.
* tls: introduce a wrap functionEdward Thomson2018-11-281-0/+2
| | | | | | | | | | | Introduce `git_tls_stream_wrap` which will take an existing `stream` with an already connected socket and begin speaking TLS on top of it. This is useful if you've built a connection to a proxy server and you wish to begin CONNECT over it to tunnel a TLS connection. Also update the pluggable TLS stream layer so that it can accept a registration structure that provides an `init` and `wrap` function, instead of a single initialization function.
* alloc: make memory allocators use function pointersPatrick Steinhardt2018-06-071-1/+3
| | | | | | | | | | | | | | | | | 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.
* Merge pull request #4645 from pks-t/pks/racy-init-deinitPatrick Steinhardt2018-05-091-10/+11
|\ | | | | global: adjust init count under lock
| * global: adjust init count under lockPatrick Steinhardt2018-05-041-10/+11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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`.
* | mbedtls: add global initializationEtienne Samson2018-04-111-1/+3
| |
* | curl: explicitly initialize and cleanup global curl statePatrick Steinhardt2018-02-281-2/+4
|/ | | | | | | | | | | | | 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.
* stream: Gather streams to src/streamsEtienne Samson2017-10-231-1/+1
|
* Make sure to always include "common.h" firstPatrick Steinhardt2017-07-031-1/+2
| | | | | | | | | | | | | | | | | | | | | | 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.
* transports: ssh: clean up after libssh2 on exitPatrick Steinhardt2017-05-021-1/+1
| | | | | | | After calling `libssh2_init`, we need to clean up after the library by executing `libssh2_exit` as soon as we exit. Register a shutdown handler to do so which simply calls `libssh2_exit`. This fixes several memory leaks.
* global: reset global state on shutdown without threadingPatrick Steinhardt2016-11-021-1/+2
| | | | | | | | | | | | | | 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.
* global: synchronize initialization and shutdown with pthreadsPatrick Steinhardt2016-11-011-3/+17
| | | | | | | | | | | | | | | 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.
* mwindow: init mwindow files in git_libgit2_initethomson/mwindow_initEdward Thomson2016-08-041-2/+3
|
* threads: add platform-independent thread initialization functionPatrick Steinhardt2016-06-201-1/+1
|
* global: clean up crt only after freeing tls dataPatrick Steinhardt2016-06-071-5/+5
| | | | | | | | | | | | | | 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.
* win32: clean up unused warnings in DllMainEdward Thomson2016-06-011-0/+3
|
* Merge pull request #3699 from libgit2/cmn/win32-free-tlsEdward Thomson2016-03-181-0/+14
|\ | | | | win32: free thread-local data on thread exit
| * win32: free thread-local data on thread exitcmn/win32-free-tlsCarlos Martin Nieto2016-03-181-0/+14
| |
* | merge driver: correct global initializationEdward Thomson2016-03-171-0/+2
|/
* Setup better defaults for OpenSSL ciphersDirkjan Bussink2016-03-141-0/+2
| | | | | | | | | This ensures that when using OpenSSL a safe default set of ciphers is selected. This is done so that the client communicates securely and we don't accidentally enable unsafe ciphers like RC4, or even worse some old export ciphers. Implements the first part of https://github.com/libgit2/libgit2/issues/3682
* ssh: initialize libssh2cmn/init-libssh2Carlos Martín Nieto2016-03-031-1/+3
| | | | | | | 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.
* global: remove an unused variableCarlos Martín Nieto2016-02-191-8/+0
|
* global: make openssl registration like the restEdward Thomson2016-02-081-115/+6
|
* global: refactor setup and cleanupEdward Thomson2016-02-081-59/+70
| | | | | Move the common initialization and cleanup methods to reduce unnecessary duplication.
* settings: add a setter for a custom user-agentCarlos Martín Nieto2015-11-121-0/+5
|
* error: store the error messages in a reusable buffercmn/error-bufferCarlos Martín Nieto2015-07-281-6/+8
| | | | | | | Instead of allocating a brand new buffer for each error string we want to store, we can use a per-thread buffer to store the error string and re-use the underlying storage. We already use the buffer to format the string, so this mostly makes that more direct.
* Merge branch 'master' into fix-init-orderingfix-init-orderingjoshaber2015-07-221-2/+16
|\
| * Include stacktrace summary in memory leak output.Jeff Hostetler2015-06-291-2/+16
| |
* | Increment `git__n_inits` before doing `init_once`.joshaber2015-07-221-1/+1
|/ | | | Fixes #3318.
* global: Ensure we free our SSL context.Tim Hentenaar2015-06-081-0/+16
|
* Rename GIT_SSL to GIT_OPENSSLCarlos Martín Nieto2015-04-231-4/+4
| | | | | This is what it's meant all along, but now we actually have multiple implementations, it's clearer to use the name of the library.
* Rename routine to free TLS dataJeff Hostetler2015-04-181-2/+7
|
* Remove DllMain now that TLS data freed by threadsJeff Hostetler2015-04-171-8/+0
|
* Also fix leak of TLS data on main thread.Jeff Hostetler2015-04-171-4/+1
|
* Attempt to fix Windows TLS memory leak.Jeff Hostetler2015-04-171-5/+13
|
* libgit2_shutdown: free TLS data (win32)Edward Thomson2015-03-041-0/+11
| | | | Free TLS data on thread exit (win32)
* libgit2_shutdown: clear err message on shutdownEdward Thomson2015-03-041-9/+27
| | | | | | Clear the error message on git_libgit2_shutdown for all versions of the library (no threads and Win32 threads). Drop the giterr_clear in clar, as that shouldn't be necessary.
* Fix leak of TLS error message in shutdown (ptherad version)Leo Yang2015-03-041-3/+3
|
* global: include sys/openssl.h for GIT_EXPORT of fnEdward Thomson2014-12-231-0/+1
| | | | | | The openssl setup function needs to be GIT_EXPORT'ed, be sure to include the `sys/openssl.h` header so that it is appropriately decorated as an export function.
* Make the OpenSSL locking function warnings more severeCarlos Martín Nieto2014-12-121-1/+0
| | | | | Our git_openssl_set_locking() would ideally not exist. Make it clearer that we provide it as a last resort and you should prefer anything else.
* Merge pull request #2743 from ethomson/init_valCarlos Martín Nieto2014-12-051-14/+30
|\ | | | | init: return the number of initializations
| * init: return the number of initializationsEdward Thomson2014-12-041-14/+30
| |
* | Add missing else directiveStefan Widgren2014-12-031-0/+1
|/ | | | | Add missing else directive to fix compiler warning: control reaches end of non-void function
* Plug possible leak in the openssl locksUngureanu Marius2014-11-171-0/+6
|
* Rename git_threads_ to git_libgit2_Carlos Martín Nieto2014-11-081-10/+11
| | | | | | This describes their purpose better, as we now initialize ssl and some other global stuff in there. Calling the init function is not something which has been optional for a while now.
* Merge pull request #2676 from libgit2/cmn/threadingEdward Thomson2014-11-061-18/+24
|\ | | | | Threading and crypto libraries
| * ssl: separate locking init from general initCarlos Martín Nieto2014-11-011-18/+23
| | | | | | | | | | Extract the lock-setting functions into their own, as we cannot assume that it's ok for us to set this unconditionally.
| * ssl: clear the OpenSSL locking functionCarlos Martín Nieto2014-11-011-0/+1
| | | | | | | | | | We're freeing the memory which holds the locks so we must make sure that the locking function doesn't try to use it.