blob: 22ee323c752f8640fffaf40314e2835726d3138a [file] [log] [blame]
George Balatsouras5b4dda52021-06-04 15:34:02 -07001// RUN: %clangxx_dfsan %s -fno-exceptions -o %t && %run %t
2// RUN: %clangxx_dfsan -DORIGIN_TRACKING -mllvm -dfsan-track-origins=1 %s -fno-exceptions -o %t && %run %t
Jianzhou Zhao4e67ae72021-03-10 06:31:56 +00003//
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +00004// Use -fno-exceptions to turn off exceptions to avoid instrumenting
5// __cxa_begin_catch, std::terminate and __gxx_personality_v0.
6//
7// TODO: Support builtin atomics. For example, https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
8// DFSan instrumentation pass cannot identify builtin callsites yet.
9
10#include <sanitizer/dfsan_interface.h>
11
12#include <assert.h>
13#include <atomic>
14#include <pthread.h>
15
16std::atomic<int> atomic_i{0};
17
Jianzhou Zhao4e67ae72021-03-10 06:31:56 +000018struct arg_struct {
19 size_t index;
20 dfsan_origin origin;
21};
22
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000023static void *ThreadFn(void *arg) {
Jianzhou Zhao4e67ae72021-03-10 06:31:56 +000024 if (((arg_struct *)arg)->index % 2) {
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000025 int i = 10;
26 dfsan_set_label(8, (void *)&i, sizeof(i));
27 atomic_i.store(i, std::memory_order_relaxed);
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000028 return 0;
29 }
30 int j = atomic_i.load();
31 assert(dfsan_get_label(j) == 0 || dfsan_get_label(j) == 2);
Jianzhou Zhao4e67ae72021-03-10 06:31:56 +000032#ifdef ORIGIN_TRACKING
33 if (dfsan_get_label(j) == 2)
34 assert(dfsan_get_init_origin(&j) == ((arg_struct *)arg)->origin);
35#endif
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000036 return 0;
37}
38
39int main(void) {
40 int i = 10;
41 dfsan_set_label(2, (void *)&i, sizeof(i));
Jianzhou Zhao4e67ae72021-03-10 06:31:56 +000042#ifdef ORIGIN_TRACKING
43 dfsan_origin origin = dfsan_get_origin(i);
44#endif
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000045 atomic_i.store(i, std::memory_order_relaxed);
46 const int kNumThreads = 24;
47 pthread_t t[kNumThreads];
Jianzhou Zhao4e67ae72021-03-10 06:31:56 +000048 arg_struct args[kNumThreads];
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000049 for (int i = 0; i < kNumThreads; ++i) {
Jianzhou Zhao4e67ae72021-03-10 06:31:56 +000050 args[i].index = i;
51#ifdef ORIGIN_TRACKING
52 args[i].origin = origin;
53#endif
54 pthread_create(&t[i], 0, ThreadFn, (void *)(args + i));
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000055 }
Jianzhou Zhao4e67ae72021-03-10 06:31:56 +000056 for (int i = 0; i < kNumThreads; ++i)
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000057 pthread_join(t[i], 0);
Jianzhou Zhaoc88fede2021-02-23 16:48:26 +000058 return 0;
59}