xref: /llvm-project/compiler-rt/test/dfsan/threaded_flush.c (revision 975327a609e55ad9c53bfeee63443128ce20006c)
1 // Tests that doing dfsan_flush() while another thread is executing doesn't
2 // segfault.
3 // RUN: %clang_dfsan %s -o %t && %run %t
4 
5 #include <assert.h>
6 #include <pthread.h>
7 #include <sanitizer/dfsan_interface.h>
8 #include <stdlib.h>
9 
10 static unsigned char GlobalBuf[4096];
11 static int ShutDownThread;
12 static int StartFlush;
13 
14 // Access GlobalBuf continuously, causing its shadow to be touched as well.
15 // When main() calls dfsan_flush(), no segfault should be triggered.
accessGlobalInBackground(void * Arg)16 static void *accessGlobalInBackground(void *Arg) {
17   __atomic_store_n(&StartFlush, 1, __ATOMIC_RELEASE);
18 
19   while (!__atomic_load_n(&ShutDownThread, __ATOMIC_ACQUIRE))
20     for (unsigned I = 0; I < sizeof(GlobalBuf); ++I)
21       ++GlobalBuf[I];
22 
23   return NULL;
24 }
25 
main()26 int main() {
27   pthread_t Thread;
28   pthread_create(&Thread, NULL, accessGlobalInBackground, NULL);
29   while (!__atomic_load_n(&StartFlush, __ATOMIC_ACQUIRE))
30     ; // Spin
31 
32   dfsan_flush();
33 
34   __atomic_store_n(&ShutDownThread, 1, __ATOMIC_RELEASE);
35   pthread_join(Thread, NULL);
36   return 0;
37 }
38