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