llvm-project
59 строк · 1.6 Кб
1// 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
3//
4// 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
18struct arg_struct {19size_t index;20dfsan_origin origin;21};22
23static void *ThreadFn(void *arg) {24if (((arg_struct *)arg)->index % 2) {25int i = 10;26dfsan_set_label(8, (void *)&i, sizeof(i));27atomic_i.store(i, std::memory_order_relaxed);28return 0;29}30int j = atomic_i.load();31assert(dfsan_get_label(j) == 0 || dfsan_get_label(j) == 2);32#ifdef ORIGIN_TRACKING33if (dfsan_get_label(j) == 2)34assert(dfsan_get_init_origin(&j) == ((arg_struct *)arg)->origin);35#endif36return 0;37}
38
39int main(void) {40int i = 10;41dfsan_set_label(2, (void *)&i, sizeof(i));42#ifdef ORIGIN_TRACKING43dfsan_origin origin = dfsan_get_origin(i);44#endif45atomic_i.store(i, std::memory_order_relaxed);46const int kNumThreads = 24;47pthread_t t[kNumThreads];48arg_struct args[kNumThreads];49for (int i = 0; i < kNumThreads; ++i) {50args[i].index = i;51#ifdef ORIGIN_TRACKING52args[i].origin = origin;53#endif54pthread_create(&t[i], 0, ThreadFn, (void *)(args + i));55}56for (int i = 0; i < kNumThreads; ++i)57pthread_join(t[i], 0);58return 0;59}
60