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