xref: /llvm-project/compiler-rt/test/lsan/TestCases/thread_context_crash.cpp (revision e6191923f5201d63c350cfc252f9c038a2051436)
1 // Check that concurent GetCurrentThread does not crash.
2 // RUN: %clangxx_lsan -O3 -pthread %s -o %t && %run %t 100
3 
4 // REQUIRES: lsan-standalone
5 
6 // For unknown reason linker can't resolve GetCurrentThread on
7 // https://ci.chromium.org/p/chromium/builders/try/mac_upload_clang.
8 // UNSUPPORTED: darwin
9 
10 #include <pthread.h>
11 #include <stdlib.h>
12 #include <vector>
13 
14 #include <sanitizer/common_interface_defs.h>
15 
16 namespace __lsan {
17 class ThreadContextLsanBase *GetCurrentThread();
18 }
19 
try_to_crash(void * args)20 void *try_to_crash(void *args) {
21   for (int i = 0; i < 100000; ++i)
22     __lsan::GetCurrentThread();
23   return nullptr;
24 }
25 
main(int argc,char ** argv)26 int main(int argc, char **argv) {
27   std::vector<pthread_t> threads;
28   for (int i = 0; i < atoi(argv[1]); ++i) {
29     threads.resize(10);
30     for (auto &thread : threads)
31       pthread_create(&thread, 0, try_to_crash, NULL);
32 
33     for (auto &thread : threads)
34       pthread_join(thread, nullptr);
35   }
36   return 0;
37 }
38