Skip to content

FindThreads: libc check does not work with tsan

Similar to #18994 (closed), which was fixed with d5331713, FindThreads breaks with thread sanitizer.

$ cat la.c 
#include <pthread.h>

void* test_func(void* data)
{
  return data;
}

int main(void)
{
  pthread_t thread;
  pthread_create(&thread, NULL, test_func, NULL);
  pthread_detach(thread);
  pthread_join(thread, NULL);
  pthread_atfork(NULL, NULL, NULL);
  pthread_exit(NULL);

  return 0;
}
$ gcc -fsanitize=address -o la.o la.c
/usr/bin/ld: /tmp/ccqnpSkK.o: undefined reference to symbol 'pthread_detach@@GLIBC_2.2.5'
/usr/bin/ld: /usr/lib64/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
$ gcc -fsanitize=undefined -o la.o la.c
/usr/bin/ld: /tmp/ccuAErPC.o: undefined reference to symbol 'pthread_join@@GLIBC_2.2.5'
/usr/bin/ld: /usr/lib64/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
$ gcc -fsanitize=thread -o la.o la.c
$ g++ -fsanitize=thread -o la.o la.c
$

Which... well, doesn't matter, right? If -fsanitize=thread is good enough to compile and link the test program it should also be good enough to test any other program, shouldn't it? Well, no.

Not sure of the details, but:

[ 92%] Linking CXX executable <TARGET>
cd <DIRECTORY> && /usr/bin/cmake -E cmake_link_script CMakeFiles/<TARGET>.dir/link.txt --verbose=1
/usr/bin/c++  -fsanitize=thread   CMakeFiles/<TARGET>.dir/src/doctest_main.cpp.o ../asio_beast_standalone/CMakeFiles/<OTHER_TARGET>.dir/src/<OTHER_TARGET_FILE>.cpp.o ../lib/<A_MODULE>/test/CMakeFiles/<MODULE_TEST>.dir/<A_MODULE>.cpp.o ../lib/<A_MODULE>/test/CMakeFiles/<MODULE_TEST>.dir/reply.cpp.o ../lib/<A_MODULE>/test/CMakeFiles/<MODULE_TEST>.dir/request.cpp.o ../lib/device_sources/ws_device_source/test/CMakeFiles/sk_tt_<A_MODULE>_ws_device_source_test.dir/session.cpp.o  -o <TARGET> utils/libsk_tt_<A_MODULE>_test_utils.a /usr/lib64/libssl.so /usr/lib64/libcrypto.so ../lib/<A_MODULE>/libsk_tt_<A_MODULE>_<A_MODULE>.a <DEPS_PATH>/lib/libspdlog.a <DEPS_PATH>/lib/libfmt.a <DEPS_PATH>/lib/libsk_tt_device_proto_<A_MODULE>.a <DEPS_PATH>/lib/<DEP_A>.a <DEPS_PATH>/lib/<DEP_B>.a 
/usr/bin/ld: ../asio_beast_standalone/CMakeFiles/<OTHER_TARGET>.dir/src/<OTHER_TARGET_FILE>.cpp.o: undefined reference to symbol 'pthread_condattr_setclock@@GLIBC_2.3.3'
/usr/bin/ld: /usr/lib64/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

Adding pthread_condattr_setclock to PTHREAD_C_CXX_TEST_SOURCE doesn't make a difference.

Edited by Cristian Morales Vega
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information