xref: /aosp_15_r20/external/compiler-rt/lib/dfsan/dfsan.cc (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- dfsan.cc ----------------------------------------------------------===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is a part of DataFlowSanitizer.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // DataFlowSanitizer runtime.  This file defines the public interface to
13*7c3d14c8STreehugger Robot // DataFlowSanitizer as well as the definition of certain runtime functions
14*7c3d14c8STreehugger Robot // called automatically by the compiler (specifically the instrumentation pass
15*7c3d14c8STreehugger Robot // in llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp).
16*7c3d14c8STreehugger Robot //
17*7c3d14c8STreehugger Robot // The public interface is defined in include/sanitizer/dfsan_interface.h whose
18*7c3d14c8STreehugger Robot // functions are prefixed dfsan_ while the compiler interface functions are
19*7c3d14c8STreehugger Robot // prefixed __dfsan_.
20*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
21*7c3d14c8STreehugger Robot 
22*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_atomic.h"
23*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_common.h"
24*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_flags.h"
25*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_flag_parser.h"
26*7c3d14c8STreehugger Robot #include "sanitizer_common/sanitizer_libc.h"
27*7c3d14c8STreehugger Robot 
28*7c3d14c8STreehugger Robot #include "dfsan/dfsan.h"
29*7c3d14c8STreehugger Robot 
30*7c3d14c8STreehugger Robot using namespace __dfsan;
31*7c3d14c8STreehugger Robot 
32*7c3d14c8STreehugger Robot typedef atomic_uint16_t atomic_dfsan_label;
33*7c3d14c8STreehugger Robot static const dfsan_label kInitializingLabel = -1;
34*7c3d14c8STreehugger Robot 
35*7c3d14c8STreehugger Robot static const uptr kNumLabels = 1 << (sizeof(dfsan_label) * 8);
36*7c3d14c8STreehugger Robot 
37*7c3d14c8STreehugger Robot static atomic_dfsan_label __dfsan_last_label;
38*7c3d14c8STreehugger Robot static dfsan_label_info __dfsan_label_info[kNumLabels];
39*7c3d14c8STreehugger Robot 
40*7c3d14c8STreehugger Robot Flags __dfsan::flags_data;
41*7c3d14c8STreehugger Robot 
42*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL dfsan_label __dfsan_retval_tls;
43*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL dfsan_label __dfsan_arg_tls[64];
44*7c3d14c8STreehugger Robot 
45*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE uptr __dfsan_shadow_ptr_mask;
46*7c3d14c8STreehugger Robot 
47*7c3d14c8STreehugger Robot // On Linux/x86_64, memory is laid out as follows:
48*7c3d14c8STreehugger Robot //
49*7c3d14c8STreehugger Robot // +--------------------+ 0x800000000000 (top of memory)
50*7c3d14c8STreehugger Robot // | application memory |
51*7c3d14c8STreehugger Robot // +--------------------+ 0x700000008000 (kAppAddr)
52*7c3d14c8STreehugger Robot // |                    |
53*7c3d14c8STreehugger Robot // |       unused       |
54*7c3d14c8STreehugger Robot // |                    |
55*7c3d14c8STreehugger Robot // +--------------------+ 0x200200000000 (kUnusedAddr)
56*7c3d14c8STreehugger Robot // |    union table     |
57*7c3d14c8STreehugger Robot // +--------------------+ 0x200000000000 (kUnionTableAddr)
58*7c3d14c8STreehugger Robot // |   shadow memory    |
59*7c3d14c8STreehugger Robot // +--------------------+ 0x000000010000 (kShadowAddr)
60*7c3d14c8STreehugger Robot // | reserved by kernel |
61*7c3d14c8STreehugger Robot // +--------------------+ 0x000000000000
62*7c3d14c8STreehugger Robot //
63*7c3d14c8STreehugger Robot // To derive a shadow memory address from an application memory address,
64*7c3d14c8STreehugger Robot // bits 44-46 are cleared to bring the address into the range
65*7c3d14c8STreehugger Robot // [0x000000008000,0x100000000000).  Then the address is shifted left by 1 to
66*7c3d14c8STreehugger Robot // account for the double byte representation of shadow labels and move the
67*7c3d14c8STreehugger Robot // address into the shadow memory range.  See the function shadow_for below.
68*7c3d14c8STreehugger Robot 
69*7c3d14c8STreehugger Robot // On Linux/MIPS64, memory is laid out as follows:
70*7c3d14c8STreehugger Robot //
71*7c3d14c8STreehugger Robot // +--------------------+ 0x10000000000 (top of memory)
72*7c3d14c8STreehugger Robot // | application memory |
73*7c3d14c8STreehugger Robot // +--------------------+ 0xF000008000 (kAppAddr)
74*7c3d14c8STreehugger Robot // |                    |
75*7c3d14c8STreehugger Robot // |       unused       |
76*7c3d14c8STreehugger Robot // |                    |
77*7c3d14c8STreehugger Robot // +--------------------+ 0x2200000000 (kUnusedAddr)
78*7c3d14c8STreehugger Robot // |    union table     |
79*7c3d14c8STreehugger Robot // +--------------------+ 0x2000000000 (kUnionTableAddr)
80*7c3d14c8STreehugger Robot // |   shadow memory    |
81*7c3d14c8STreehugger Robot // +--------------------+ 0x0000010000 (kShadowAddr)
82*7c3d14c8STreehugger Robot // | reserved by kernel |
83*7c3d14c8STreehugger Robot // +--------------------+ 0x0000000000
84*7c3d14c8STreehugger Robot 
85*7c3d14c8STreehugger Robot // On Linux/AArch64 (39-bit VMA), memory is laid out as follow:
86*7c3d14c8STreehugger Robot //
87*7c3d14c8STreehugger Robot // +--------------------+ 0x8000000000 (top of memory)
88*7c3d14c8STreehugger Robot // | application memory |
89*7c3d14c8STreehugger Robot // +--------------------+ 0x7000008000 (kAppAddr)
90*7c3d14c8STreehugger Robot // |                    |
91*7c3d14c8STreehugger Robot // |       unused       |
92*7c3d14c8STreehugger Robot // |                    |
93*7c3d14c8STreehugger Robot // +--------------------+ 0x1200000000 (kUnusedAddr)
94*7c3d14c8STreehugger Robot // |    union table     |
95*7c3d14c8STreehugger Robot // +--------------------+ 0x1000000000 (kUnionTableAddr)
96*7c3d14c8STreehugger Robot // |   shadow memory    |
97*7c3d14c8STreehugger Robot // +--------------------+ 0x0000010000 (kShadowAddr)
98*7c3d14c8STreehugger Robot // | reserved by kernel |
99*7c3d14c8STreehugger Robot // +--------------------+ 0x0000000000
100*7c3d14c8STreehugger Robot 
101*7c3d14c8STreehugger Robot // On Linux/AArch64 (42-bit VMA), memory is laid out as follow:
102*7c3d14c8STreehugger Robot //
103*7c3d14c8STreehugger Robot // +--------------------+ 0x40000000000 (top of memory)
104*7c3d14c8STreehugger Robot // | application memory |
105*7c3d14c8STreehugger Robot // +--------------------+ 0x3ff00008000 (kAppAddr)
106*7c3d14c8STreehugger Robot // |                    |
107*7c3d14c8STreehugger Robot // |       unused       |
108*7c3d14c8STreehugger Robot // |                    |
109*7c3d14c8STreehugger Robot // +--------------------+ 0x1200000000 (kUnusedAddr)
110*7c3d14c8STreehugger Robot // |    union table     |
111*7c3d14c8STreehugger Robot // +--------------------+ 0x8000000000 (kUnionTableAddr)
112*7c3d14c8STreehugger Robot // |   shadow memory    |
113*7c3d14c8STreehugger Robot // +--------------------+ 0x0000010000 (kShadowAddr)
114*7c3d14c8STreehugger Robot // | reserved by kernel |
115*7c3d14c8STreehugger Robot // +--------------------+ 0x0000000000
116*7c3d14c8STreehugger Robot 
117*7c3d14c8STreehugger Robot typedef atomic_dfsan_label dfsan_union_table_t[kNumLabels][kNumLabels];
118*7c3d14c8STreehugger Robot 
119*7c3d14c8STreehugger Robot #ifdef DFSAN_RUNTIME_VMA
120*7c3d14c8STreehugger Robot // Runtime detected VMA size.
121*7c3d14c8STreehugger Robot int __dfsan::vmaSize;
122*7c3d14c8STreehugger Robot #endif
123*7c3d14c8STreehugger Robot 
UnusedAddr()124*7c3d14c8STreehugger Robot static uptr UnusedAddr() {
125*7c3d14c8STreehugger Robot   return MappingArchImpl<MAPPING_UNION_TABLE_ADDR>()
126*7c3d14c8STreehugger Robot          + sizeof(dfsan_union_table_t);
127*7c3d14c8STreehugger Robot }
128*7c3d14c8STreehugger Robot 
union_table(dfsan_label l1,dfsan_label l2)129*7c3d14c8STreehugger Robot static atomic_dfsan_label *union_table(dfsan_label l1, dfsan_label l2) {
130*7c3d14c8STreehugger Robot   return &(*(dfsan_union_table_t *) UnionTableAddr())[l1][l2];
131*7c3d14c8STreehugger Robot }
132*7c3d14c8STreehugger Robot 
133*7c3d14c8STreehugger Robot // Checks we do not run out of labels.
dfsan_check_label(dfsan_label label)134*7c3d14c8STreehugger Robot static void dfsan_check_label(dfsan_label label) {
135*7c3d14c8STreehugger Robot   if (label == kInitializingLabel) {
136*7c3d14c8STreehugger Robot     Report("FATAL: DataFlowSanitizer: out of labels\n");
137*7c3d14c8STreehugger Robot     Die();
138*7c3d14c8STreehugger Robot   }
139*7c3d14c8STreehugger Robot }
140*7c3d14c8STreehugger Robot 
141*7c3d14c8STreehugger Robot // Resolves the union of two unequal labels.  Nonequality is a precondition for
142*7c3d14c8STreehugger Robot // this function (the instrumentation pass inlines the equality test).
143*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE
__dfsan_union(dfsan_label l1,dfsan_label l2)144*7c3d14c8STreehugger Robot dfsan_label __dfsan_union(dfsan_label l1, dfsan_label l2) {
145*7c3d14c8STreehugger Robot   DCHECK_NE(l1, l2);
146*7c3d14c8STreehugger Robot 
147*7c3d14c8STreehugger Robot   if (l1 == 0)
148*7c3d14c8STreehugger Robot     return l2;
149*7c3d14c8STreehugger Robot   if (l2 == 0)
150*7c3d14c8STreehugger Robot     return l1;
151*7c3d14c8STreehugger Robot 
152*7c3d14c8STreehugger Robot   if (l1 > l2)
153*7c3d14c8STreehugger Robot     Swap(l1, l2);
154*7c3d14c8STreehugger Robot 
155*7c3d14c8STreehugger Robot   atomic_dfsan_label *table_ent = union_table(l1, l2);
156*7c3d14c8STreehugger Robot   // We need to deal with the case where two threads concurrently request
157*7c3d14c8STreehugger Robot   // a union of the same pair of labels.  If the table entry is uninitialized,
158*7c3d14c8STreehugger Robot   // (i.e. 0) use a compare-exchange to set the entry to kInitializingLabel
159*7c3d14c8STreehugger Robot   // (i.e. -1) to mark that we are initializing it.
160*7c3d14c8STreehugger Robot   dfsan_label label = 0;
161*7c3d14c8STreehugger Robot   if (atomic_compare_exchange_strong(table_ent, &label, kInitializingLabel,
162*7c3d14c8STreehugger Robot                                      memory_order_acquire)) {
163*7c3d14c8STreehugger Robot     // Check whether l2 subsumes l1.  We don't need to check whether l1
164*7c3d14c8STreehugger Robot     // subsumes l2 because we are guaranteed here that l1 < l2, and (at least
165*7c3d14c8STreehugger Robot     // in the cases we are interested in) a label may only subsume labels
166*7c3d14c8STreehugger Robot     // created earlier (i.e. with a lower numerical value).
167*7c3d14c8STreehugger Robot     if (__dfsan_label_info[l2].l1 == l1 ||
168*7c3d14c8STreehugger Robot         __dfsan_label_info[l2].l2 == l1) {
169*7c3d14c8STreehugger Robot       label = l2;
170*7c3d14c8STreehugger Robot     } else {
171*7c3d14c8STreehugger Robot       label =
172*7c3d14c8STreehugger Robot         atomic_fetch_add(&__dfsan_last_label, 1, memory_order_relaxed) + 1;
173*7c3d14c8STreehugger Robot       dfsan_check_label(label);
174*7c3d14c8STreehugger Robot       __dfsan_label_info[label].l1 = l1;
175*7c3d14c8STreehugger Robot       __dfsan_label_info[label].l2 = l2;
176*7c3d14c8STreehugger Robot     }
177*7c3d14c8STreehugger Robot     atomic_store(table_ent, label, memory_order_release);
178*7c3d14c8STreehugger Robot   } else if (label == kInitializingLabel) {
179*7c3d14c8STreehugger Robot     // Another thread is initializing the entry.  Wait until it is finished.
180*7c3d14c8STreehugger Robot     do {
181*7c3d14c8STreehugger Robot       internal_sched_yield();
182*7c3d14c8STreehugger Robot       label = atomic_load(table_ent, memory_order_acquire);
183*7c3d14c8STreehugger Robot     } while (label == kInitializingLabel);
184*7c3d14c8STreehugger Robot   }
185*7c3d14c8STreehugger Robot   return label;
186*7c3d14c8STreehugger Robot }
187*7c3d14c8STreehugger Robot 
188*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE
__dfsan_union_load(const dfsan_label * ls,uptr n)189*7c3d14c8STreehugger Robot dfsan_label __dfsan_union_load(const dfsan_label *ls, uptr n) {
190*7c3d14c8STreehugger Robot   dfsan_label label = ls[0];
191*7c3d14c8STreehugger Robot   for (uptr i = 1; i != n; ++i) {
192*7c3d14c8STreehugger Robot     dfsan_label next_label = ls[i];
193*7c3d14c8STreehugger Robot     if (label != next_label)
194*7c3d14c8STreehugger Robot       label = __dfsan_union(label, next_label);
195*7c3d14c8STreehugger Robot   }
196*7c3d14c8STreehugger Robot   return label;
197*7c3d14c8STreehugger Robot }
198*7c3d14c8STreehugger Robot 
199*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE
__dfsan_unimplemented(char * fname)200*7c3d14c8STreehugger Robot void __dfsan_unimplemented(char *fname) {
201*7c3d14c8STreehugger Robot   if (flags().warn_unimplemented)
202*7c3d14c8STreehugger Robot     Report("WARNING: DataFlowSanitizer: call to uninstrumented function %s\n",
203*7c3d14c8STreehugger Robot            fname);
204*7c3d14c8STreehugger Robot }
205*7c3d14c8STreehugger Robot 
206*7c3d14c8STreehugger Robot // Use '-mllvm -dfsan-debug-nonzero-labels' and break on this function
207*7c3d14c8STreehugger Robot // to try to figure out where labels are being introduced in a nominally
208*7c3d14c8STreehugger Robot // label-free program.
__dfsan_nonzero_label()209*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_nonzero_label() {
210*7c3d14c8STreehugger Robot   if (flags().warn_nonzero_labels)
211*7c3d14c8STreehugger Robot     Report("WARNING: DataFlowSanitizer: saw nonzero label\n");
212*7c3d14c8STreehugger Robot }
213*7c3d14c8STreehugger Robot 
214*7c3d14c8STreehugger Robot // Indirect call to an uninstrumented vararg function. We don't have a way of
215*7c3d14c8STreehugger Robot // handling these at the moment.
216*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
__dfsan_vararg_wrapper(const char * fname)217*7c3d14c8STreehugger Robot __dfsan_vararg_wrapper(const char *fname) {
218*7c3d14c8STreehugger Robot   Report("FATAL: DataFlowSanitizer: unsupported indirect call to vararg "
219*7c3d14c8STreehugger Robot          "function %s\n", fname);
220*7c3d14c8STreehugger Robot   Die();
221*7c3d14c8STreehugger Robot }
222*7c3d14c8STreehugger Robot 
223*7c3d14c8STreehugger Robot // Like __dfsan_union, but for use from the client or custom functions.  Hence
224*7c3d14c8STreehugger Robot // the equality comparison is done here before calling __dfsan_union.
225*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
dfsan_union(dfsan_label l1,dfsan_label l2)226*7c3d14c8STreehugger Robot dfsan_union(dfsan_label l1, dfsan_label l2) {
227*7c3d14c8STreehugger Robot   if (l1 == l2)
228*7c3d14c8STreehugger Robot     return l1;
229*7c3d14c8STreehugger Robot   return __dfsan_union(l1, l2);
230*7c3d14c8STreehugger Robot }
231*7c3d14c8STreehugger Robot 
232*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE
dfsan_create_label(const char * desc,void * userdata)233*7c3d14c8STreehugger Robot dfsan_label dfsan_create_label(const char *desc, void *userdata) {
234*7c3d14c8STreehugger Robot   dfsan_label label =
235*7c3d14c8STreehugger Robot     atomic_fetch_add(&__dfsan_last_label, 1, memory_order_relaxed) + 1;
236*7c3d14c8STreehugger Robot   dfsan_check_label(label);
237*7c3d14c8STreehugger Robot   __dfsan_label_info[label].l1 = __dfsan_label_info[label].l2 = 0;
238*7c3d14c8STreehugger Robot   __dfsan_label_info[label].desc = desc;
239*7c3d14c8STreehugger Robot   __dfsan_label_info[label].userdata = userdata;
240*7c3d14c8STreehugger Robot   return label;
241*7c3d14c8STreehugger Robot }
242*7c3d14c8STreehugger Robot 
243*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE
__dfsan_set_label(dfsan_label label,void * addr,uptr size)244*7c3d14c8STreehugger Robot void __dfsan_set_label(dfsan_label label, void *addr, uptr size) {
245*7c3d14c8STreehugger Robot   for (dfsan_label *labelp = shadow_for(addr); size != 0; --size, ++labelp) {
246*7c3d14c8STreehugger Robot     // Don't write the label if it is already the value we need it to be.
247*7c3d14c8STreehugger Robot     // In a program where most addresses are not labeled, it is common that
248*7c3d14c8STreehugger Robot     // a page of shadow memory is entirely zeroed.  The Linux copy-on-write
249*7c3d14c8STreehugger Robot     // implementation will share all of the zeroed pages, making a copy of a
250*7c3d14c8STreehugger Robot     // page when any value is written.  The un-sharing will happen even if
251*7c3d14c8STreehugger Robot     // the value written does not change the value in memory.  Avoiding the
252*7c3d14c8STreehugger Robot     // write when both |label| and |*labelp| are zero dramatically reduces
253*7c3d14c8STreehugger Robot     // the amount of real memory used by large programs.
254*7c3d14c8STreehugger Robot     if (label == *labelp)
255*7c3d14c8STreehugger Robot       continue;
256*7c3d14c8STreehugger Robot 
257*7c3d14c8STreehugger Robot     *labelp = label;
258*7c3d14c8STreehugger Robot   }
259*7c3d14c8STreehugger Robot }
260*7c3d14c8STreehugger Robot 
261*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
dfsan_set_label(dfsan_label label,void * addr,uptr size)262*7c3d14c8STreehugger Robot void dfsan_set_label(dfsan_label label, void *addr, uptr size) {
263*7c3d14c8STreehugger Robot   __dfsan_set_label(label, addr, size);
264*7c3d14c8STreehugger Robot }
265*7c3d14c8STreehugger Robot 
266*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE
dfsan_add_label(dfsan_label label,void * addr,uptr size)267*7c3d14c8STreehugger Robot void dfsan_add_label(dfsan_label label, void *addr, uptr size) {
268*7c3d14c8STreehugger Robot   for (dfsan_label *labelp = shadow_for(addr); size != 0; --size, ++labelp)
269*7c3d14c8STreehugger Robot     if (*labelp != label)
270*7c3d14c8STreehugger Robot       *labelp = __dfsan_union(*labelp, label);
271*7c3d14c8STreehugger Robot }
272*7c3d14c8STreehugger Robot 
273*7c3d14c8STreehugger Robot // Unlike the other dfsan interface functions the behavior of this function
274*7c3d14c8STreehugger Robot // depends on the label of one of its arguments.  Hence it is implemented as a
275*7c3d14c8STreehugger Robot // custom function.
276*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
__dfsw_dfsan_get_label(long data,dfsan_label data_label,dfsan_label * ret_label)277*7c3d14c8STreehugger Robot __dfsw_dfsan_get_label(long data, dfsan_label data_label,
278*7c3d14c8STreehugger Robot                        dfsan_label *ret_label) {
279*7c3d14c8STreehugger Robot   *ret_label = 0;
280*7c3d14c8STreehugger Robot   return data_label;
281*7c3d14c8STreehugger Robot }
282*7c3d14c8STreehugger Robot 
283*7c3d14c8STreehugger Robot SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
dfsan_read_label(const void * addr,uptr size)284*7c3d14c8STreehugger Robot dfsan_read_label(const void *addr, uptr size) {
285*7c3d14c8STreehugger Robot   if (size == 0)
286*7c3d14c8STreehugger Robot     return 0;
287*7c3d14c8STreehugger Robot   return __dfsan_union_load(shadow_for(addr), size);
288*7c3d14c8STreehugger Robot }
289*7c3d14c8STreehugger Robot 
290*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE
dfsan_get_label_info(dfsan_label label)291*7c3d14c8STreehugger Robot const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label) {
292*7c3d14c8STreehugger Robot   return &__dfsan_label_info[label];
293*7c3d14c8STreehugger Robot }
294*7c3d14c8STreehugger Robot 
295*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE int
dfsan_has_label(dfsan_label label,dfsan_label elem)296*7c3d14c8STreehugger Robot dfsan_has_label(dfsan_label label, dfsan_label elem) {
297*7c3d14c8STreehugger Robot   if (label == elem)
298*7c3d14c8STreehugger Robot     return true;
299*7c3d14c8STreehugger Robot   const dfsan_label_info *info = dfsan_get_label_info(label);
300*7c3d14c8STreehugger Robot   if (info->l1 != 0) {
301*7c3d14c8STreehugger Robot     return dfsan_has_label(info->l1, elem) || dfsan_has_label(info->l2, elem);
302*7c3d14c8STreehugger Robot   } else {
303*7c3d14c8STreehugger Robot     return false;
304*7c3d14c8STreehugger Robot   }
305*7c3d14c8STreehugger Robot }
306*7c3d14c8STreehugger Robot 
307*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
dfsan_has_label_with_desc(dfsan_label label,const char * desc)308*7c3d14c8STreehugger Robot dfsan_has_label_with_desc(dfsan_label label, const char *desc) {
309*7c3d14c8STreehugger Robot   const dfsan_label_info *info = dfsan_get_label_info(label);
310*7c3d14c8STreehugger Robot   if (info->l1 != 0) {
311*7c3d14c8STreehugger Robot     return dfsan_has_label_with_desc(info->l1, desc) ||
312*7c3d14c8STreehugger Robot            dfsan_has_label_with_desc(info->l2, desc);
313*7c3d14c8STreehugger Robot   } else {
314*7c3d14c8STreehugger Robot     return internal_strcmp(desc, info->desc) == 0;
315*7c3d14c8STreehugger Robot   }
316*7c3d14c8STreehugger Robot }
317*7c3d14c8STreehugger Robot 
318*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr
dfsan_get_label_count(void)319*7c3d14c8STreehugger Robot dfsan_get_label_count(void) {
320*7c3d14c8STreehugger Robot   dfsan_label max_label_allocated =
321*7c3d14c8STreehugger Robot       atomic_load(&__dfsan_last_label, memory_order_relaxed);
322*7c3d14c8STreehugger Robot 
323*7c3d14c8STreehugger Robot   return static_cast<uptr>(max_label_allocated);
324*7c3d14c8STreehugger Robot }
325*7c3d14c8STreehugger Robot 
326*7c3d14c8STreehugger Robot extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
dfsan_dump_labels(int fd)327*7c3d14c8STreehugger Robot dfsan_dump_labels(int fd) {
328*7c3d14c8STreehugger Robot   dfsan_label last_label =
329*7c3d14c8STreehugger Robot       atomic_load(&__dfsan_last_label, memory_order_relaxed);
330*7c3d14c8STreehugger Robot 
331*7c3d14c8STreehugger Robot   for (uptr l = 1; l <= last_label; ++l) {
332*7c3d14c8STreehugger Robot     char buf[64];
333*7c3d14c8STreehugger Robot     internal_snprintf(buf, sizeof(buf), "%u %u %u ", l,
334*7c3d14c8STreehugger Robot                       __dfsan_label_info[l].l1, __dfsan_label_info[l].l2);
335*7c3d14c8STreehugger Robot     WriteToFile(fd, buf, internal_strlen(buf));
336*7c3d14c8STreehugger Robot     if (__dfsan_label_info[l].l1 == 0 && __dfsan_label_info[l].desc) {
337*7c3d14c8STreehugger Robot       WriteToFile(fd, __dfsan_label_info[l].desc,
338*7c3d14c8STreehugger Robot                   internal_strlen(__dfsan_label_info[l].desc));
339*7c3d14c8STreehugger Robot     }
340*7c3d14c8STreehugger Robot     WriteToFile(fd, "\n", 1);
341*7c3d14c8STreehugger Robot   }
342*7c3d14c8STreehugger Robot }
343*7c3d14c8STreehugger Robot 
SetDefaults()344*7c3d14c8STreehugger Robot void Flags::SetDefaults() {
345*7c3d14c8STreehugger Robot #define DFSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
346*7c3d14c8STreehugger Robot #include "dfsan_flags.inc"
347*7c3d14c8STreehugger Robot #undef DFSAN_FLAG
348*7c3d14c8STreehugger Robot }
349*7c3d14c8STreehugger Robot 
RegisterDfsanFlags(FlagParser * parser,Flags * f)350*7c3d14c8STreehugger Robot static void RegisterDfsanFlags(FlagParser *parser, Flags *f) {
351*7c3d14c8STreehugger Robot #define DFSAN_FLAG(Type, Name, DefaultValue, Description) \
352*7c3d14c8STreehugger Robot   RegisterFlag(parser, #Name, Description, &f->Name);
353*7c3d14c8STreehugger Robot #include "dfsan_flags.inc"
354*7c3d14c8STreehugger Robot #undef DFSAN_FLAG
355*7c3d14c8STreehugger Robot }
356*7c3d14c8STreehugger Robot 
InitializeFlags()357*7c3d14c8STreehugger Robot static void InitializeFlags() {
358*7c3d14c8STreehugger Robot   SetCommonFlagsDefaults();
359*7c3d14c8STreehugger Robot   flags().SetDefaults();
360*7c3d14c8STreehugger Robot 
361*7c3d14c8STreehugger Robot   FlagParser parser;
362*7c3d14c8STreehugger Robot   RegisterCommonFlags(&parser);
363*7c3d14c8STreehugger Robot   RegisterDfsanFlags(&parser, &flags());
364*7c3d14c8STreehugger Robot   parser.ParseString(GetEnv("DFSAN_OPTIONS"));
365*7c3d14c8STreehugger Robot   InitializeCommonFlags();
366*7c3d14c8STreehugger Robot   if (Verbosity()) ReportUnrecognizedFlags();
367*7c3d14c8STreehugger Robot   if (common_flags()->help) parser.PrintFlagDescriptions();
368*7c3d14c8STreehugger Robot }
369*7c3d14c8STreehugger Robot 
InitializePlatformEarly()370*7c3d14c8STreehugger Robot static void InitializePlatformEarly() {
371*7c3d14c8STreehugger Robot   AvoidCVE_2016_2143();
372*7c3d14c8STreehugger Robot #ifdef DFSAN_RUNTIME_VMA
373*7c3d14c8STreehugger Robot   __dfsan::vmaSize =
374*7c3d14c8STreehugger Robot     (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1);
375*7c3d14c8STreehugger Robot   if (__dfsan::vmaSize == 39 || __dfsan::vmaSize == 42) {
376*7c3d14c8STreehugger Robot     __dfsan_shadow_ptr_mask = ShadowMask();
377*7c3d14c8STreehugger Robot   } else {
378*7c3d14c8STreehugger Robot     Printf("FATAL: DataFlowSanitizer: unsupported VMA range\n");
379*7c3d14c8STreehugger Robot     Printf("FATAL: Found %d - Supported 39 and 42\n", __dfsan::vmaSize);
380*7c3d14c8STreehugger Robot     Die();
381*7c3d14c8STreehugger Robot   }
382*7c3d14c8STreehugger Robot #endif
383*7c3d14c8STreehugger Robot }
384*7c3d14c8STreehugger Robot 
dfsan_fini()385*7c3d14c8STreehugger Robot static void dfsan_fini() {
386*7c3d14c8STreehugger Robot   if (internal_strcmp(flags().dump_labels_at_exit, "") != 0) {
387*7c3d14c8STreehugger Robot     fd_t fd = OpenFile(flags().dump_labels_at_exit, WrOnly);
388*7c3d14c8STreehugger Robot     if (fd == kInvalidFd) {
389*7c3d14c8STreehugger Robot       Report("WARNING: DataFlowSanitizer: unable to open output file %s\n",
390*7c3d14c8STreehugger Robot              flags().dump_labels_at_exit);
391*7c3d14c8STreehugger Robot       return;
392*7c3d14c8STreehugger Robot     }
393*7c3d14c8STreehugger Robot 
394*7c3d14c8STreehugger Robot     Report("INFO: DataFlowSanitizer: dumping labels to %s\n",
395*7c3d14c8STreehugger Robot            flags().dump_labels_at_exit);
396*7c3d14c8STreehugger Robot     dfsan_dump_labels(fd);
397*7c3d14c8STreehugger Robot     CloseFile(fd);
398*7c3d14c8STreehugger Robot   }
399*7c3d14c8STreehugger Robot }
400*7c3d14c8STreehugger Robot 
dfsan_init(int argc,char ** argv,char ** envp)401*7c3d14c8STreehugger Robot static void dfsan_init(int argc, char **argv, char **envp) {
402*7c3d14c8STreehugger Robot   InitializeFlags();
403*7c3d14c8STreehugger Robot 
404*7c3d14c8STreehugger Robot   InitializePlatformEarly();
405*7c3d14c8STreehugger Robot 
406*7c3d14c8STreehugger Robot   MmapFixedNoReserve(ShadowAddr(), UnusedAddr() - ShadowAddr());
407*7c3d14c8STreehugger Robot 
408*7c3d14c8STreehugger Robot   // Protect the region of memory we don't use, to preserve the one-to-one
409*7c3d14c8STreehugger Robot   // mapping from application to shadow memory. But if ASLR is disabled, Linux
410*7c3d14c8STreehugger Robot   // will load our executable in the middle of our unused region. This mostly
411*7c3d14c8STreehugger Robot   // works so long as the program doesn't use too much memory. We support this
412*7c3d14c8STreehugger Robot   // case by disabling memory protection when ASLR is disabled.
413*7c3d14c8STreehugger Robot   uptr init_addr = (uptr)&dfsan_init;
414*7c3d14c8STreehugger Robot   if (!(init_addr >= UnusedAddr() && init_addr < AppAddr()))
415*7c3d14c8STreehugger Robot     MmapFixedNoAccess(UnusedAddr(), AppAddr() - UnusedAddr());
416*7c3d14c8STreehugger Robot 
417*7c3d14c8STreehugger Robot   InitializeInterceptors();
418*7c3d14c8STreehugger Robot 
419*7c3d14c8STreehugger Robot   // Register the fini callback to run when the program terminates successfully
420*7c3d14c8STreehugger Robot   // or it is killed by the runtime.
421*7c3d14c8STreehugger Robot   Atexit(dfsan_fini);
422*7c3d14c8STreehugger Robot   AddDieCallback(dfsan_fini);
423*7c3d14c8STreehugger Robot 
424*7c3d14c8STreehugger Robot   __dfsan_label_info[kInitializingLabel].desc = "<init label>";
425*7c3d14c8STreehugger Robot }
426*7c3d14c8STreehugger Robot 
427*7c3d14c8STreehugger Robot #if SANITIZER_CAN_USE_PREINIT_ARRAY
428*7c3d14c8STreehugger Robot __attribute__((section(".preinit_array"), used))
429*7c3d14c8STreehugger Robot static void (*dfsan_init_ptr)(int, char **, char **) = dfsan_init;
430*7c3d14c8STreehugger Robot #endif
431