1 // RUN: %clang_dfsan -DLIB -c %s -o %t.lib.o && \
2 // RUN: %clang_dfsan       -c %s -o %t.o && \
3 // RUN: %clang_dfsan %t.lib.o %t.o -o %t.bin && \
4 // RUN: %run %t.bin
5 
6 // RUN: %clang_dfsan -mllvm -dfsan-args-abi -DLIB -c %s -o %t.lib.o && \
7 // RUN: %clang_dfsan -mllvm -dfsan-args-abi -c %s -o %t.o && \
8 // RUN: %clang_dfsan -mllvm -dfsan-args-abi %t.o %t.lib.o -o %t.bin && \
9 // RUN: %run %t.bin
10 
11 #include <sanitizer/dfsan_interface.h>
12 #include <assert.h>
13 
14 #ifdef LIB
15 // Compiling this file with and without LIB defined allows this file to be
16 // built as two separate translation units.  This ensures that the code
17 // can not be optimized in a way that removes behavior we wish to test.  For
18 // example, computing a value should cause labels to be allocated only if
19 // the computation is actually done.  Putting the computation here prevents
20 // the compiler from optimizing away the computation (and labeling) that
21 // tests wish to verify.
22 
add_in_separate_translation_unit(int a,int b)23 int add_in_separate_translation_unit(int a, int b) {
24   return a + b;
25 }
26 
multiply_in_separate_translation_unit(int a,int b)27 int multiply_in_separate_translation_unit(int a, int b) {
28   return a * b;
29 }
30 
31 #else
32 
33 int add_in_separate_translation_unit(int i, int j);
34 int multiply_in_separate_translation_unit(int i, int j);
35 
main(void)36 int main(void) {
37   size_t label_count;
38 
39   // No labels allocated yet.
40   label_count = dfsan_get_label_count();
41   assert(0 == label_count);
42 
43   int i = 1;
44   dfsan_label i_label = dfsan_create_label("i", 0);
45   dfsan_set_label(i_label, &i, sizeof(i));
46 
47   // One label allocated for i.
48   label_count = dfsan_get_label_count();
49   assert(1u == label_count);
50 
51   int j = 2;
52   dfsan_label j_label = dfsan_create_label("j", 0);
53   dfsan_set_label(j_label, &j, sizeof(j));
54 
55   // Check that a new label was allocated for j.
56   label_count = dfsan_get_label_count();
57   assert(2u == label_count);
58 
59   // Create a value that combines i and j.
60   int i_plus_j = add_in_separate_translation_unit(i, j);
61 
62   // Check that a label was created for the union of i and j.
63   label_count = dfsan_get_label_count();
64   assert(3u == label_count);
65 
66   // Combine i and j in a different way.  Check that the existing label is
67   // reused, and a new label is not created.
68   int j_times_i = multiply_in_separate_translation_unit(j, i);
69   label_count = dfsan_get_label_count();
70   assert(3u == label_count);
71   assert(dfsan_get_label(i_plus_j) == dfsan_get_label(j_times_i));
72 
73   return 0;
74 }
75 #endif  // #ifdef LIB
76