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
10static unsigned char GlobalBuf[4096];
11static int ShutDownThread;
12static 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.
16static 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
26int main() {
27 pthread_t Thread;
28 pthread_create(newthread: &Thread, NULL, start_routine: 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(th: Thread, NULL);
36 return 0;
37}
38

source code of compiler-rt/test/dfsan/threaded_flush.c