From 09402100946e6892ebd1e75773eb390cdd7f28ab Mon Sep 17 00:00:00 2001 From: Paul Cristian Sarbu Date: Fri, 12 May 2023 13:14:26 +0200 Subject: memcheck: fix race in libgit2... ...caused by incorrectly setting and resetting the library internal state and the misuse of pthreads in libgit2. Normally, git_libgit2_init and git_libgit2_shutdown should span the life of a worker thread in order to be safely used. However, due to an incorrect implementation of libgit2's threadstate with pthreads, on unix systems there is a race condition. Until the use of pthread_key_t is corrected in libgit2, we need to apply a workaround by always ensuring that the main thread is the first thread reaching the GitContext constructor. --- test/TARGETS | 6 +++++- test/main.cpp | 10 ++++++++++ test/other_tools/git_operations/git_config_run.test.cpp | 2 +- test/utils/remote_execution/main-remote-execution.cpp | 8 ++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/TARGETS b/test/TARGETS index 9e9c20eb..8fb612ec 100644 --- a/test/TARGETS +++ b/test/TARGETS @@ -2,7 +2,11 @@ { "type": ["@", "rules", "CC", "library"] , "name": ["catch-main"] , "srcs": ["main.cpp"] - , "deps": [["@", "catch2", "", "catch2"], ["test/utils", "log_config"]] + , "deps": + [ ["@", "catch2", "", "catch2"] + , ["test/utils", "log_config"] + , ["src/buildtool/file_system", "git_context"] + ] , "stage": ["test"] } , "TESTS": diff --git a/test/main.cpp b/test/main.cpp index c93583f2..bb431de2 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -13,9 +13,19 @@ // limitations under the License. #include "catch2/catch_session.hpp" +#include "src/buildtool/file_system/git_context.hpp" #include "test/utils/logging/log_config.hpp" auto main(int argc, char* argv[]) -> int { ConfigureLogging(); + + /** + * The current implementation of libgit2 uses pthread_key_t incorrectly + * on POSIX systems to handle thread-specific data, which requires us to + * explicitly make sure the main thread is the first one to call + * git_libgit2_init. Future versions of libgit2 will hopefully fix this. + */ + GitContext::Create(); + return Catch::Session().run(argc, argv); } diff --git a/test/other_tools/git_operations/git_config_run.test.cpp b/test/other_tools/git_operations/git_config_run.test.cpp index 8ffd744a..956b8926 100644 --- a/test/other_tools/git_operations/git_config_run.test.cpp +++ b/test/other_tools/git_operations/git_config_run.test.cpp @@ -45,7 +45,7 @@ auto main(int argc, char* argv[]) -> int { ConfigureLogging(); // start a git context, needed to read in the config file - GitContext context{}; + GitContext::Create(); // handle args if (argc < 3) { diff --git a/test/utils/remote_execution/main-remote-execution.cpp b/test/utils/remote_execution/main-remote-execution.cpp index 7dd48d92..49b0b617 100644 --- a/test/utils/remote_execution/main-remote-execution.cpp +++ b/test/utils/remote_execution/main-remote-execution.cpp @@ -86,6 +86,14 @@ auto main(int argc, char* argv[]) -> int { return EXIT_FAILURE; } + /** + * The current implementation of libgit2 uses pthread_key_t incorrectly + * on POSIX systems to handle thread-specific data, which requires us to + * explicitly make sure the main thread is the first one to call + * git_libgit2_init. Future versions of libgit2 will hopefully fix this. + */ + GitContext::Create(); + int result = Catch::Session().run(argc, argv); // valgrind fails if we terminate before grpc's async shutdown threads exit -- cgit v1.2.3