xref: /freebsd/contrib/llvm-project/compiler-rt/lib/dfsan/dfsan.cpp (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 //===-- dfsan.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of DataFlowSanitizer.
10 //
11 // DataFlowSanitizer runtime.  This file defines the public interface to
12 // DataFlowSanitizer as well as the definition of certain runtime functions
13 // called automatically by the compiler (specifically the instrumentation pass
14 // in llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp).
15 //
16 // The public interface is defined in include/sanitizer/dfsan_interface.h whose
17 // functions are prefixed dfsan_ while the compiler interface functions are
18 // prefixed __dfsan_.
19 //===----------------------------------------------------------------------===//
20 
21 #include "dfsan/dfsan.h"
22 
23 #include "dfsan/dfsan_chained_origin_depot.h"
24 #include "dfsan/dfsan_flags.h"
25 #include "dfsan/dfsan_origin.h"
26 #include "dfsan/dfsan_thread.h"
27 #include "sanitizer_common/sanitizer_atomic.h"
28 #include "sanitizer_common/sanitizer_common.h"
29 #include "sanitizer_common/sanitizer_file.h"
30 #include "sanitizer_common/sanitizer_flag_parser.h"
31 #include "sanitizer_common/sanitizer_flags.h"
32 #include "sanitizer_common/sanitizer_internal_defs.h"
33 #include "sanitizer_common/sanitizer_libc.h"
34 #include "sanitizer_common/sanitizer_report_decorator.h"
35 #include "sanitizer_common/sanitizer_stacktrace.h"
36 
37 using namespace __dfsan;
38 
39 Flags __dfsan::flags_data;
40 
41 // The size of TLS variables. These constants must be kept in sync with the ones
42 // in DataFlowSanitizer.cpp.
43 static const int kDFsanArgTlsSize = 800;
44 static const int kDFsanRetvalTlsSize = 800;
45 static const int kDFsanArgOriginTlsSize = 800;
46 
47 SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL u64
48     __dfsan_retval_tls[kDFsanRetvalTlsSize / sizeof(u64)];
49 SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL u32 __dfsan_retval_origin_tls;
50 SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL u64
51     __dfsan_arg_tls[kDFsanArgTlsSize / sizeof(u64)];
52 SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL u32
53     __dfsan_arg_origin_tls[kDFsanArgOriginTlsSize / sizeof(u32)];
54 
55 // Instrumented code may set this value in terms of -dfsan-track-origins.
56 // * undefined or 0: do not track origins.
57 // * 1: track origins at memory store operations.
58 // * 2: track origins at memory load and store operations.
59 //      TODO: track callsites.
60 extern "C" SANITIZER_WEAK_ATTRIBUTE const int __dfsan_track_origins;
61 
62 extern "C" SANITIZER_INTERFACE_ATTRIBUTE int dfsan_get_track_origins() {
63   return &__dfsan_track_origins ? __dfsan_track_origins : 0;
64 }
65 
66 // On Linux/x86_64, memory is laid out as follows:
67 //
68 //  +--------------------+ 0x800000000000 (top of memory)
69 //  |    application 3   |
70 //  +--------------------+ 0x700000000000
71 //  |      invalid       |
72 //  +--------------------+ 0x610000000000
73 //  |      origin 1      |
74 //  +--------------------+ 0x600000000000
75 //  |    application 2   |
76 //  +--------------------+ 0x510000000000
77 //  |      shadow 1      |
78 //  +--------------------+ 0x500000000000
79 //  |      invalid       |
80 //  +--------------------+ 0x400000000000
81 //  |      origin 3      |
82 //  +--------------------+ 0x300000000000
83 //  |      shadow 3      |
84 //  +--------------------+ 0x200000000000
85 //  |      origin 2      |
86 //  +--------------------+ 0x110000000000
87 //  |      invalid       |
88 //  +--------------------+ 0x100000000000
89 //  |      shadow 2      |
90 //  +--------------------+ 0x010000000000
91 //  |    application 1   |
92 //  +--------------------+ 0x000000000000
93 //
94 //  MEM_TO_SHADOW(mem) = mem ^ 0x500000000000
95 //  SHADOW_TO_ORIGIN(shadow) = shadow + 0x100000000000
96 
97 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
98 dfsan_label __dfsan_union_load(const dfsan_label *ls, uptr n) {
99   dfsan_label label = ls[0];
100   for (uptr i = 1; i != n; ++i)
101     label |= ls[i];
102   return label;
103 }
104 
105 // Return the union of all the n labels from addr at the high 32 bit, and the
106 // origin of the first taint byte at the low 32 bit.
107 extern "C" SANITIZER_INTERFACE_ATTRIBUTE u64
108 __dfsan_load_label_and_origin(const void *addr, uptr n) {
109   dfsan_label label = 0;
110   u64 ret = 0;
111   uptr p = (uptr)addr;
112   dfsan_label *s = shadow_for((void *)p);
113   for (uptr i = 0; i < n; ++i) {
114     dfsan_label l = s[i];
115     if (!l)
116       continue;
117     label |= l;
118     if (!ret)
119       ret = *(dfsan_origin *)origin_for((void *)(p + i));
120   }
121   return ret | (u64)label << 32;
122 }
123 
124 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
125 void __dfsan_unimplemented(char *fname) {
126   if (flags().warn_unimplemented)
127     Report("WARNING: DataFlowSanitizer: call to uninstrumented function %s\n",
128            fname);
129 }
130 
131 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_wrapper_extern_weak_null(
132     const void *addr, char *fname) {
133   if (!addr)
134     Report(
135         "ERROR: DataFlowSanitizer: dfsan generated wrapper calling null "
136         "extern_weak function %s\nIf this only happens with dfsan, the "
137         "dfsan instrumentation pass may be accidentally optimizing out a "
138         "null check\n",
139         fname);
140 }
141 
142 // Use '-mllvm -dfsan-debug-nonzero-labels' and break on this function
143 // to try to figure out where labels are being introduced in a nominally
144 // label-free program.
145 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_nonzero_label() {
146   if (flags().warn_nonzero_labels)
147     Report("WARNING: DataFlowSanitizer: saw nonzero label\n");
148 }
149 
150 // Indirect call to an uninstrumented vararg function. We don't have a way of
151 // handling these at the moment.
152 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
153 __dfsan_vararg_wrapper(const char *fname) {
154   Report("FATAL: DataFlowSanitizer: unsupported indirect call to vararg "
155          "function %s\n", fname);
156   Die();
157 }
158 
159 // Resolves the union of two labels.
160 SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
161 dfsan_union(dfsan_label l1, dfsan_label l2) {
162   return l1 | l2;
163 }
164 
165 static const uptr kOriginAlign = sizeof(dfsan_origin);
166 static const uptr kOriginAlignMask = ~(kOriginAlign - 1UL);
167 
168 static uptr OriginAlignUp(uptr u) {
169   return (u + kOriginAlign - 1) & kOriginAlignMask;
170 }
171 
172 static uptr OriginAlignDown(uptr u) { return u & kOriginAlignMask; }
173 
174 // Return the origin of the first taint byte in the size bytes from the address
175 // addr.
176 static dfsan_origin GetOriginIfTainted(uptr addr, uptr size) {
177   for (uptr i = 0; i < size; ++i, ++addr) {
178     dfsan_label *s = shadow_for((void *)addr);
179 
180     if (*s) {
181       // Validate address region.
182       CHECK(MEM_IS_SHADOW(s));
183       return *(dfsan_origin *)origin_for((void *)addr);
184     }
185   }
186   return 0;
187 }
188 
189 // For platforms which support slow unwinder only, we need to restrict the store
190 // context size to 1, basically only storing the current pc, because the slow
191 // unwinder which is based on libunwind is not async signal safe and causes
192 // random freezes in forking applications as well as in signal handlers.
193 // DFSan supports only Linux. So we do not restrict the store context size.
194 #define GET_STORE_STACK_TRACE_PC_BP(pc, bp) \
195   BufferedStackTrace stack;                 \
196   stack.Unwind(pc, bp, nullptr, true, flags().store_context_size);
197 
198 #define PRINT_CALLER_STACK_TRACE        \
199   {                                     \
200     GET_CALLER_PC_BP_SP;                \
201     (void)sp;                           \
202     GET_STORE_STACK_TRACE_PC_BP(pc, bp) \
203     stack.Print();                      \
204   }
205 
206 // Return a chain with the previous ID id and the current stack.
207 // from_init = true if this is the first chain of an origin tracking path.
208 static u32 ChainOrigin(u32 id, StackTrace *stack, bool from_init = false) {
209   // StackDepot is not async signal safe. Do not create new chains in a signal
210   // handler.
211   DFsanThread *t = GetCurrentThread();
212   if (t && t->InSignalHandler())
213     return id;
214 
215   // As an optimization the origin of an application byte is updated only when
216   // its shadow is non-zero. Because we are only interested in the origins of
217   // taint labels, it does not matter what origin a zero label has. This reduces
218   // memory write cost. MSan does similar optimization. The following invariant
219   // may not hold because of some bugs. We check the invariant to help debug.
220   if (!from_init && id == 0 && flags().check_origin_invariant) {
221     Printf("  DFSan found invalid origin invariant\n");
222     PRINT_CALLER_STACK_TRACE
223   }
224 
225   Origin o = Origin::FromRawId(id);
226   stack->tag = StackTrace::TAG_UNKNOWN;
227   Origin chained = Origin::CreateChainedOrigin(o, stack);
228   return chained.raw_id();
229 }
230 
231 static void ChainAndWriteOriginIfTainted(uptr src, uptr size, uptr dst,
232                                          StackTrace *stack) {
233   dfsan_origin o = GetOriginIfTainted(src, size);
234   if (o) {
235     o = ChainOrigin(o, stack);
236     *(dfsan_origin *)origin_for((void *)dst) = o;
237   }
238 }
239 
240 // Copy the origins of the size bytes from src to dst. The source and target
241 // memory ranges cannot be overlapped. This is used by memcpy. stack records the
242 // stack trace of the memcpy. When dst and src are not 4-byte aligned properly,
243 // origins at the unaligned address boundaries may be overwritten because four
244 // contiguous bytes share the same origin.
245 static void CopyOrigin(const void *dst, const void *src, uptr size,
246                        StackTrace *stack) {
247   uptr d = (uptr)dst;
248   uptr beg = OriginAlignDown(d);
249   // Copy left unaligned origin if that memory is tainted.
250   if (beg < d) {
251     ChainAndWriteOriginIfTainted((uptr)src, beg + kOriginAlign - d, beg, stack);
252     beg += kOriginAlign;
253   }
254 
255   uptr end = OriginAlignDown(d + size);
256   // If both ends fall into the same 4-byte slot, we are done.
257   if (end < beg)
258     return;
259 
260   // Copy right unaligned origin if that memory is tainted.
261   if (end < d + size)
262     ChainAndWriteOriginIfTainted((uptr)src + (end - d), (d + size) - end, end,
263                                  stack);
264 
265   if (beg >= end)
266     return;
267 
268   // Align src up.
269   uptr src_a = OriginAlignUp((uptr)src);
270   dfsan_origin *src_o = origin_for((void *)src_a);
271   u32 *src_s = (u32 *)shadow_for((void *)src_a);
272   dfsan_origin *src_end = origin_for((void *)(src_a + (end - beg)));
273   dfsan_origin *dst_o = origin_for((void *)beg);
274   dfsan_origin last_src_o = 0;
275   dfsan_origin last_dst_o = 0;
276   for (; src_o < src_end; ++src_o, ++src_s, ++dst_o) {
277     if (!*src_s)
278       continue;
279     if (*src_o != last_src_o) {
280       last_src_o = *src_o;
281       last_dst_o = ChainOrigin(last_src_o, stack);
282     }
283     *dst_o = last_dst_o;
284   }
285 }
286 
287 // Copy the origins of the size bytes from src to dst. The source and target
288 // memory ranges may be overlapped. So the copy is done in a reverse order.
289 // This is used by memmove. stack records the stack trace of the memmove.
290 static void ReverseCopyOrigin(const void *dst, const void *src, uptr size,
291                               StackTrace *stack) {
292   uptr d = (uptr)dst;
293   uptr end = OriginAlignDown(d + size);
294 
295   // Copy right unaligned origin if that memory is tainted.
296   if (end < d + size)
297     ChainAndWriteOriginIfTainted((uptr)src + (end - d), (d + size) - end, end,
298                                  stack);
299 
300   uptr beg = OriginAlignDown(d);
301 
302   if (beg + kOriginAlign < end) {
303     // Align src up.
304     uptr src_a = OriginAlignUp((uptr)src);
305     void *src_end = (void *)(src_a + end - beg - kOriginAlign);
306     dfsan_origin *src_end_o = origin_for(src_end);
307     u32 *src_end_s = (u32 *)shadow_for(src_end);
308     dfsan_origin *src_begin_o = origin_for((void *)src_a);
309     dfsan_origin *dst = origin_for((void *)(end - kOriginAlign));
310     dfsan_origin last_src_o = 0;
311     dfsan_origin last_dst_o = 0;
312     for (; src_end_o >= src_begin_o; --src_end_o, --src_end_s, --dst) {
313       if (!*src_end_s)
314         continue;
315       if (*src_end_o != last_src_o) {
316         last_src_o = *src_end_o;
317         last_dst_o = ChainOrigin(last_src_o, stack);
318       }
319       *dst = last_dst_o;
320     }
321   }
322 
323   // Copy left unaligned origin if that memory is tainted.
324   if (beg < d)
325     ChainAndWriteOriginIfTainted((uptr)src, beg + kOriginAlign - d, beg, stack);
326 }
327 
328 // Copy or move the origins of the len bytes from src to dst. The source and
329 // target memory ranges may or may not be overlapped. This is used by memory
330 // transfer operations. stack records the stack trace of the memory transfer
331 // operation.
332 static void MoveOrigin(const void *dst, const void *src, uptr size,
333                        StackTrace *stack) {
334   // Validate address regions.
335   if (!MEM_IS_SHADOW(shadow_for(dst)) ||
336       !MEM_IS_SHADOW(shadow_for((void *)((uptr)dst + size))) ||
337       !MEM_IS_SHADOW(shadow_for(src)) ||
338       !MEM_IS_SHADOW(shadow_for((void *)((uptr)src + size)))) {
339     CHECK(false);
340     return;
341   }
342   // If destination origin range overlaps with source origin range, move
343   // origins by copying origins in a reverse order; otherwise, copy origins in
344   // a normal order. The orders of origin transfer are consistent with the
345   // orders of how memcpy and memmove transfer user data.
346   uptr src_aligned_beg = OriginAlignDown((uptr)src);
347   uptr src_aligned_end = OriginAlignDown((uptr)src + size);
348   uptr dst_aligned_beg = OriginAlignDown((uptr)dst);
349   if (dst_aligned_beg < src_aligned_end && dst_aligned_beg >= src_aligned_beg)
350     return ReverseCopyOrigin(dst, src, size, stack);
351   return CopyOrigin(dst, src, size, stack);
352 }
353 
354 // Set the size bytes from the addres dst to be the origin value.
355 static void SetOrigin(const void *dst, uptr size, u32 origin) {
356   if (size == 0)
357     return;
358 
359   // Origin mapping is 4 bytes per 4 bytes of application memory.
360   // Here we extend the range such that its left and right bounds are both
361   // 4 byte aligned.
362   uptr x = unaligned_origin_for((uptr)dst);
363   uptr beg = OriginAlignDown(x);
364   uptr end = OriginAlignUp(x + size);  // align up.
365   u64 origin64 = ((u64)origin << 32) | origin;
366   // This is like memset, but the value is 32-bit. We unroll by 2 to write
367   // 64 bits at once. May want to unroll further to get 128-bit stores.
368   if (beg & 7ULL) {
369     if (*(u32 *)beg != origin)
370       *(u32 *)beg = origin;
371     beg += 4;
372   }
373   for (uptr addr = beg; addr < (end & ~7UL); addr += 8) {
374     if (*(u64 *)addr == origin64)
375       continue;
376     *(u64 *)addr = origin64;
377   }
378   if (end & 7ULL)
379     if (*(u32 *)(end - kOriginAlign) != origin)
380       *(u32 *)(end - kOriginAlign) = origin;
381 }
382 
383 #define RET_CHAIN_ORIGIN(id)           \
384   GET_CALLER_PC_BP_SP;                 \
385   (void)sp;                            \
386   GET_STORE_STACK_TRACE_PC_BP(pc, bp); \
387   return ChainOrigin(id, &stack);
388 
389 // Return a new origin chain with the previous ID id and the current stack
390 // trace.
391 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_origin
392 __dfsan_chain_origin(dfsan_origin id) {
393   RET_CHAIN_ORIGIN(id)
394 }
395 
396 // Return a new origin chain with the previous ID id and the current stack
397 // trace if the label is tainted.
398 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_origin
399 __dfsan_chain_origin_if_tainted(dfsan_label label, dfsan_origin id) {
400   if (!label)
401     return id;
402   RET_CHAIN_ORIGIN(id)
403 }
404 
405 // Copy or move the origins of the len bytes from src to dst.
406 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_mem_origin_transfer(
407     const void *dst, const void *src, uptr len) {
408   if (src == dst)
409     return;
410   GET_CALLER_PC_BP;
411   GET_STORE_STACK_TRACE_PC_BP(pc, bp);
412   MoveOrigin(dst, src, len, &stack);
413 }
414 
415 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void dfsan_mem_origin_transfer(
416     const void *dst, const void *src, uptr len) {
417   __dfsan_mem_origin_transfer(dst, src, len);
418 }
419 
420 static void CopyShadow(void *dst, const void *src, uptr len) {
421   internal_memcpy((void *)__dfsan::shadow_for(dst),
422                   (const void *)__dfsan::shadow_for(src),
423                   len * sizeof(dfsan_label));
424 }
425 
426 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void dfsan_mem_shadow_transfer(
427     void *dst, const void *src, uptr len) {
428   CopyShadow(dst, src, len);
429 }
430 
431 // Copy shadow and origins of the len bytes from src to dst.
432 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
433 __dfsan_mem_shadow_origin_transfer(void *dst, const void *src, uptr size) {
434   if (src == dst)
435     return;
436   CopyShadow(dst, src, size);
437   if (dfsan_get_track_origins()) {
438     // Duplicating code instead of calling __dfsan_mem_origin_transfer
439     // so that the getting the caller stack frame works correctly.
440     GET_CALLER_PC_BP;
441     GET_STORE_STACK_TRACE_PC_BP(pc, bp);
442     MoveOrigin(dst, src, size, &stack);
443   }
444 }
445 
446 // Copy shadow and origins as per __atomic_compare_exchange.
447 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
448 __dfsan_mem_shadow_origin_conditional_exchange(u8 condition, void *target,
449                                                void *expected,
450                                                const void *desired, uptr size) {
451   void *dst;
452   const void *src;
453   // condition is result of native call to __atomic_compare_exchange
454   if (condition) {
455     // Copy desired into target
456     dst = target;
457     src = desired;
458   } else {
459     // Copy target into expected
460     dst = expected;
461     src = target;
462   }
463   if (src == dst)
464     return;
465   CopyShadow(dst, src, size);
466   if (dfsan_get_track_origins()) {
467     // Duplicating code instead of calling __dfsan_mem_origin_transfer
468     // so that the getting the caller stack frame works correctly.
469     GET_CALLER_PC_BP;
470     GET_STORE_STACK_TRACE_PC_BP(pc, bp);
471     MoveOrigin(dst, src, size, &stack);
472   }
473 }
474 
475 namespace __dfsan {
476 
477 bool dfsan_inited = false;
478 bool dfsan_init_is_running = false;
479 
480 void dfsan_copy_memory(void *dst, const void *src, uptr size) {
481   internal_memcpy(dst, src, size);
482   dfsan_mem_shadow_transfer(dst, src, size);
483   if (dfsan_get_track_origins())
484     dfsan_mem_origin_transfer(dst, src, size);
485 }
486 
487 // Releases the pages within the origin address range.
488 static void ReleaseOrigins(void *addr, uptr size) {
489   const uptr beg_origin_addr = (uptr)__dfsan::origin_for(addr);
490   const void *end_addr = (void *)((uptr)addr + size);
491   const uptr end_origin_addr = (uptr)__dfsan::origin_for(end_addr);
492 
493   if (end_origin_addr - beg_origin_addr <
494       common_flags()->clear_shadow_mmap_threshold)
495     return;
496 
497   const uptr page_size = GetPageSizeCached();
498   const uptr beg_aligned = RoundUpTo(beg_origin_addr, page_size);
499   const uptr end_aligned = RoundDownTo(end_origin_addr, page_size);
500 
501   if (!MmapFixedSuperNoReserve(beg_aligned, end_aligned - beg_aligned))
502     Die();
503 }
504 
505 static void WriteZeroShadowInRange(uptr beg, uptr end) {
506   // Don't write the label if it is already the value we need it to be.
507   // In a program where most addresses are not labeled, it is common that
508   // a page of shadow memory is entirely zeroed.  The Linux copy-on-write
509   // implementation will share all of the zeroed pages, making a copy of a
510   // page when any value is written.  The un-sharing will happen even if
511   // the value written does not change the value in memory.  Avoiding the
512   // write when both |label| and |*labelp| are zero dramatically reduces
513   // the amount of real memory used by large programs.
514   if (!mem_is_zero((const char *)beg, end - beg))
515     internal_memset((void *)beg, 0, end - beg);
516 }
517 
518 // Releases the pages within the shadow address range, and sets
519 // the shadow addresses not on the pages to be 0.
520 static void ReleaseOrClearShadows(void *addr, uptr size) {
521   const uptr beg_shadow_addr = (uptr)__dfsan::shadow_for(addr);
522   const void *end_addr = (void *)((uptr)addr + size);
523   const uptr end_shadow_addr = (uptr)__dfsan::shadow_for(end_addr);
524 
525   if (end_shadow_addr - beg_shadow_addr <
526       common_flags()->clear_shadow_mmap_threshold) {
527     WriteZeroShadowInRange(beg_shadow_addr, end_shadow_addr);
528     return;
529   }
530 
531   const uptr page_size = GetPageSizeCached();
532   const uptr beg_aligned = RoundUpTo(beg_shadow_addr, page_size);
533   const uptr end_aligned = RoundDownTo(end_shadow_addr, page_size);
534 
535   if (beg_aligned >= end_aligned) {
536     WriteZeroShadowInRange(beg_shadow_addr, end_shadow_addr);
537   } else {
538     if (beg_aligned != beg_shadow_addr)
539       WriteZeroShadowInRange(beg_shadow_addr, beg_aligned);
540     if (end_aligned != end_shadow_addr)
541       WriteZeroShadowInRange(end_aligned, end_shadow_addr);
542     if (!MmapFixedSuperNoReserve(beg_aligned, end_aligned - beg_aligned))
543       Die();
544   }
545 }
546 
547 void SetShadow(dfsan_label label, void *addr, uptr size, dfsan_origin origin) {
548   if (0 != label) {
549     const uptr beg_shadow_addr = (uptr)__dfsan::shadow_for(addr);
550     internal_memset((void *)beg_shadow_addr, label, size);
551     if (dfsan_get_track_origins())
552       SetOrigin(addr, size, origin);
553     return;
554   }
555 
556   if (dfsan_get_track_origins())
557     ReleaseOrigins(addr, size);
558 
559   ReleaseOrClearShadows(addr, size);
560 }
561 
562 }  // namespace __dfsan
563 
564 // If the label s is tainted, set the size bytes from the address p to be a new
565 // origin chain with the previous ID o and the current stack trace. This is
566 // used by instrumentation to reduce code size when too much code is inserted.
567 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_maybe_store_origin(
568     dfsan_label s, void *p, uptr size, dfsan_origin o) {
569   if (UNLIKELY(s)) {
570     GET_CALLER_PC_BP_SP;
571     (void)sp;
572     GET_STORE_STACK_TRACE_PC_BP(pc, bp);
573     SetOrigin(p, size, ChainOrigin(o, &stack));
574   }
575 }
576 
577 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_set_label(
578     dfsan_label label, dfsan_origin origin, void *addr, uptr size) {
579   __dfsan::SetShadow(label, addr, size, origin);
580 }
581 
582 SANITIZER_INTERFACE_ATTRIBUTE
583 void dfsan_set_label(dfsan_label label, void *addr, uptr size) {
584   dfsan_origin init_origin = 0;
585   if (label && dfsan_get_track_origins()) {
586     GET_CALLER_PC_BP;
587     GET_STORE_STACK_TRACE_PC_BP(pc, bp);
588     init_origin = ChainOrigin(0, &stack, true);
589   }
590   __dfsan::SetShadow(label, addr, size, init_origin);
591 }
592 
593 SANITIZER_INTERFACE_ATTRIBUTE
594 void dfsan_add_label(dfsan_label label, void *addr, uptr size) {
595   if (0 == label)
596     return;
597 
598   if (dfsan_get_track_origins()) {
599     GET_CALLER_PC_BP;
600     GET_STORE_STACK_TRACE_PC_BP(pc, bp);
601     dfsan_origin init_origin = ChainOrigin(0, &stack, true);
602     SetOrigin(addr, size, init_origin);
603   }
604 
605   for (dfsan_label *labelp = shadow_for(addr); size != 0; --size, ++labelp)
606     *labelp |= label;
607 }
608 
609 // Unlike the other dfsan interface functions the behavior of this function
610 // depends on the label of one of its arguments.  Hence it is implemented as a
611 // custom function.
612 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
613 __dfsw_dfsan_get_label(long data, dfsan_label data_label,
614                        dfsan_label *ret_label) {
615   *ret_label = 0;
616   return data_label;
617 }
618 
619 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label __dfso_dfsan_get_label(
620     long data, dfsan_label data_label, dfsan_label *ret_label,
621     dfsan_origin data_origin, dfsan_origin *ret_origin) {
622   *ret_label = 0;
623   *ret_origin = 0;
624   return data_label;
625 }
626 
627 // This function is used if dfsan_get_origin is called when origin tracking is
628 // off.
629 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_origin __dfsw_dfsan_get_origin(
630     long data, dfsan_label data_label, dfsan_label *ret_label) {
631   *ret_label = 0;
632   return 0;
633 }
634 
635 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_origin __dfso_dfsan_get_origin(
636     long data, dfsan_label data_label, dfsan_label *ret_label,
637     dfsan_origin data_origin, dfsan_origin *ret_origin) {
638   *ret_label = 0;
639   *ret_origin = 0;
640   return data_origin;
641 }
642 
643 SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
644 dfsan_read_label(const void *addr, uptr size) {
645   if (size == 0)
646     return 0;
647   return __dfsan_union_load(shadow_for(addr), size);
648 }
649 
650 SANITIZER_INTERFACE_ATTRIBUTE dfsan_origin
651 dfsan_read_origin_of_first_taint(const void *addr, uptr size) {
652   return GetOriginIfTainted((uptr)addr, size);
653 }
654 
655 SANITIZER_INTERFACE_ATTRIBUTE void dfsan_set_label_origin(dfsan_label label,
656                                                           dfsan_origin origin,
657                                                           void *addr,
658                                                           uptr size) {
659   __dfsan_set_label(label, origin, addr, size);
660 }
661 
662 extern "C" SANITIZER_INTERFACE_ATTRIBUTE int
663 dfsan_has_label(dfsan_label label, dfsan_label elem) {
664   return (label & elem) == elem;
665 }
666 
667 namespace __dfsan {
668 
669 typedef void (*dfsan_conditional_callback_t)(dfsan_label label,
670                                              dfsan_origin origin);
671 static dfsan_conditional_callback_t conditional_callback = nullptr;
672 static dfsan_label labels_in_signal_conditional = 0;
673 
674 static void ConditionalCallback(dfsan_label label, dfsan_origin origin) {
675   // Programs have many branches. For efficiency the conditional sink callback
676   // handler needs to ignore as many as possible as early as possible.
677   if (label == 0) {
678     return;
679   }
680   if (conditional_callback == nullptr) {
681     return;
682   }
683 
684   // This initial ConditionalCallback handler needs to be in here in dfsan
685   // runtime (rather than being an entirely user implemented hook) so that it
686   // has access to dfsan thread information.
687   DFsanThread *t = GetCurrentThread();
688   // A callback operation which does useful work (like record the flow) will
689   // likely be too long executed in a signal handler.
690   if (t && t->InSignalHandler()) {
691     // Record set of labels used in signal handler for completeness.
692     labels_in_signal_conditional |= label;
693     return;
694   }
695 
696   conditional_callback(label, origin);
697 }
698 
699 }  // namespace __dfsan
700 
701 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
702 __dfsan_conditional_callback_origin(dfsan_label label, dfsan_origin origin) {
703   __dfsan::ConditionalCallback(label, origin);
704 }
705 
706 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_conditional_callback(
707     dfsan_label label) {
708   __dfsan::ConditionalCallback(label, 0);
709 }
710 
711 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void dfsan_set_conditional_callback(
712     __dfsan::dfsan_conditional_callback_t callback) {
713   __dfsan::conditional_callback = callback;
714 }
715 
716 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
717 dfsan_get_labels_in_signal_conditional() {
718   return __dfsan::labels_in_signal_conditional;
719 }
720 
721 namespace __dfsan {
722 
723 typedef void (*dfsan_reaches_function_callback_t)(dfsan_label label,
724                                                   dfsan_origin origin,
725                                                   const char *file,
726                                                   unsigned int line,
727                                                   const char *function);
728 static dfsan_reaches_function_callback_t reaches_function_callback = nullptr;
729 static dfsan_label labels_in_signal_reaches_function = 0;
730 
731 static void ReachesFunctionCallback(dfsan_label label, dfsan_origin origin,
732                                     const char *file, unsigned int line,
733                                     const char *function) {
734   if (label == 0) {
735     return;
736   }
737   if (reaches_function_callback == nullptr) {
738     return;
739   }
740 
741   // This initial ReachesFunctionCallback handler needs to be in here in dfsan
742   // runtime (rather than being an entirely user implemented hook) so that it
743   // has access to dfsan thread information.
744   DFsanThread *t = GetCurrentThread();
745   // A callback operation which does useful work (like record the flow) will
746   // likely be too long executed in a signal handler.
747   if (t && t->InSignalHandler()) {
748     // Record set of labels used in signal handler for completeness.
749     labels_in_signal_reaches_function |= label;
750     return;
751   }
752 
753   reaches_function_callback(label, origin, file, line, function);
754 }
755 
756 }  // namespace __dfsan
757 
758 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
759 __dfsan_reaches_function_callback_origin(dfsan_label label, dfsan_origin origin,
760                                          const char *file, unsigned int line,
761                                          const char *function) {
762   __dfsan::ReachesFunctionCallback(label, origin, file, line, function);
763 }
764 
765 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
766 __dfsan_reaches_function_callback(dfsan_label label, const char *file,
767                                   unsigned int line, const char *function) {
768   __dfsan::ReachesFunctionCallback(label, 0, file, line, function);
769 }
770 
771 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
772 dfsan_set_reaches_function_callback(
773     __dfsan::dfsan_reaches_function_callback_t callback) {
774   __dfsan::reaches_function_callback = callback;
775 }
776 
777 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
778 dfsan_get_labels_in_signal_reaches_function() {
779   return __dfsan::labels_in_signal_reaches_function;
780 }
781 
782 class Decorator : public __sanitizer::SanitizerCommonDecorator {
783  public:
784   Decorator() : SanitizerCommonDecorator() {}
785   const char *Origin() const { return Magenta(); }
786 };
787 
788 namespace {
789 
790 void PrintNoOriginTrackingWarning() {
791   Decorator d;
792   Printf(
793       "  %sDFSan: origin tracking is not enabled. Did you specify the "
794       "-dfsan-track-origins=1 option?%s\n",
795       d.Warning(), d.Default());
796 }
797 
798 void PrintNoTaintWarning(const void *address) {
799   Decorator d;
800   Printf("  %sDFSan: no tainted value at %x%s\n", d.Warning(), address,
801          d.Default());
802 }
803 
804 void PrintInvalidOriginWarning(dfsan_label label, const void *address) {
805   Decorator d;
806   Printf(
807       "  %sTaint value 0x%x (at %p) has invalid origin tracking. This can "
808       "be a DFSan bug.%s\n",
809       d.Warning(), label, address, d.Default());
810 }
811 
812 void PrintInvalidOriginIdWarning(dfsan_origin origin) {
813   Decorator d;
814   Printf(
815       "  %sOrigin Id %d has invalid origin tracking. This can "
816       "be a DFSan bug.%s\n",
817       d.Warning(), origin, d.Default());
818 }
819 
820 bool PrintOriginTraceFramesToStr(Origin o, InternalScopedString *out) {
821   Decorator d;
822   bool found = false;
823 
824   while (o.isChainedOrigin()) {
825     StackTrace stack;
826     dfsan_origin origin_id = o.raw_id();
827     o = o.getNextChainedOrigin(&stack);
828     if (o.isChainedOrigin())
829       out->append(
830           "  %sOrigin value: 0x%x, Taint value was stored to memory at%s\n",
831           d.Origin(), origin_id, d.Default());
832     else
833       out->append("  %sOrigin value: 0x%x, Taint value was created at%s\n",
834                   d.Origin(), origin_id, d.Default());
835 
836     // Includes a trailing newline, so no need to add it again.
837     stack.PrintTo(out);
838     found = true;
839   }
840 
841   return found;
842 }
843 
844 bool PrintOriginTraceToStr(const void *addr, const char *description,
845                            InternalScopedString *out) {
846   CHECK(out);
847   CHECK(dfsan_get_track_origins());
848   Decorator d;
849 
850   const dfsan_label label = *__dfsan::shadow_for(addr);
851   CHECK(label);
852 
853   const dfsan_origin origin = *__dfsan::origin_for(addr);
854 
855   out->append("  %sTaint value 0x%x (at %p) origin tracking (%s)%s\n",
856               d.Origin(), label, addr, description ? description : "",
857               d.Default());
858 
859   Origin o = Origin::FromRawId(origin);
860   return PrintOriginTraceFramesToStr(o, out);
861 }
862 
863 }  // namespace
864 
865 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void dfsan_print_origin_trace(
866     const void *addr, const char *description) {
867   if (!dfsan_get_track_origins()) {
868     PrintNoOriginTrackingWarning();
869     return;
870   }
871 
872   const dfsan_label label = *__dfsan::shadow_for(addr);
873   if (!label) {
874     PrintNoTaintWarning(addr);
875     return;
876   }
877 
878   InternalScopedString trace;
879   bool success = PrintOriginTraceToStr(addr, description, &trace);
880 
881   if (trace.length())
882     Printf("%s", trace.data());
883 
884   if (!success)
885     PrintInvalidOriginWarning(label, addr);
886 }
887 
888 extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr
889 dfsan_sprint_origin_trace(const void *addr, const char *description,
890                           char *out_buf, uptr out_buf_size) {
891   CHECK(out_buf);
892 
893   if (!dfsan_get_track_origins()) {
894     PrintNoOriginTrackingWarning();
895     return 0;
896   }
897 
898   const dfsan_label label = *__dfsan::shadow_for(addr);
899   if (!label) {
900     PrintNoTaintWarning(addr);
901     return 0;
902   }
903 
904   InternalScopedString trace;
905   bool success = PrintOriginTraceToStr(addr, description, &trace);
906 
907   if (!success) {
908     PrintInvalidOriginWarning(label, addr);
909     return 0;
910   }
911 
912   if (out_buf_size) {
913     internal_strncpy(out_buf, trace.data(), out_buf_size - 1);
914     out_buf[out_buf_size - 1] = '\0';
915   }
916 
917   return trace.length();
918 }
919 
920 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void dfsan_print_origin_id_trace(
921     dfsan_origin origin) {
922   if (!dfsan_get_track_origins()) {
923     PrintNoOriginTrackingWarning();
924     return;
925   }
926   Origin o = Origin::FromRawId(origin);
927 
928   InternalScopedString trace;
929   bool success = PrintOriginTraceFramesToStr(o, &trace);
930 
931   if (trace.length())
932     Printf("%s", trace.data());
933 
934   if (!success)
935     PrintInvalidOriginIdWarning(origin);
936 }
937 
938 extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr dfsan_sprint_origin_id_trace(
939     dfsan_origin origin, char *out_buf, uptr out_buf_size) {
940   CHECK(out_buf);
941 
942   if (!dfsan_get_track_origins()) {
943     PrintNoOriginTrackingWarning();
944     return 0;
945   }
946   Origin o = Origin::FromRawId(origin);
947 
948   InternalScopedString trace;
949   bool success = PrintOriginTraceFramesToStr(o, &trace);
950 
951   if (!success) {
952     PrintInvalidOriginIdWarning(origin);
953     return 0;
954   }
955 
956   if (out_buf_size) {
957     internal_strncpy(out_buf, trace.data(), out_buf_size - 1);
958     out_buf[out_buf_size - 1] = '\0';
959   }
960 
961   return trace.length();
962 }
963 
964 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_origin
965 dfsan_get_init_origin(const void *addr) {
966   if (!dfsan_get_track_origins())
967     return 0;
968 
969   const dfsan_label label = *__dfsan::shadow_for(addr);
970   if (!label)
971     return 0;
972 
973   const dfsan_origin origin = *__dfsan::origin_for(addr);
974 
975   Origin o = Origin::FromRawId(origin);
976   dfsan_origin origin_id = o.raw_id();
977   while (o.isChainedOrigin()) {
978     StackTrace stack;
979     origin_id = o.raw_id();
980     o = o.getNextChainedOrigin(&stack);
981   }
982   return origin_id;
983 }
984 
985 void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
986                                                  void *context,
987                                                  bool request_fast,
988                                                  u32 max_depth) {
989   using namespace __dfsan;
990   DFsanThread *t = GetCurrentThread();
991   if (!t || !StackTrace::WillUseFastUnwind(request_fast)) {
992     return Unwind(max_depth, pc, bp, context, 0, 0, false);
993   }
994   Unwind(max_depth, pc, bp, nullptr, t->stack_top(), t->stack_bottom(), true);
995 }
996 
997 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_print_stack_trace() {
998   GET_CALLER_PC_BP;
999   GET_STORE_STACK_TRACE_PC_BP(pc, bp);
1000   stack.Print();
1001 }
1002 
1003 extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr
1004 dfsan_sprint_stack_trace(char *out_buf, uptr out_buf_size) {
1005   CHECK(out_buf);
1006   GET_CALLER_PC_BP;
1007   GET_STORE_STACK_TRACE_PC_BP(pc, bp);
1008   return stack.PrintTo(out_buf, out_buf_size);
1009 }
1010 
1011 void Flags::SetDefaults() {
1012 #define DFSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
1013 #include "dfsan_flags.inc"
1014 #undef DFSAN_FLAG
1015 }
1016 
1017 static void RegisterDfsanFlags(FlagParser *parser, Flags *f) {
1018 #define DFSAN_FLAG(Type, Name, DefaultValue, Description) \
1019   RegisterFlag(parser, #Name, Description, &f->Name);
1020 #include "dfsan_flags.inc"
1021 #undef DFSAN_FLAG
1022 }
1023 
1024 static void InitializeFlags() {
1025   SetCommonFlagsDefaults();
1026   {
1027     CommonFlags cf;
1028     cf.CopyFrom(*common_flags());
1029     cf.intercept_tls_get_addr = true;
1030     OverrideCommonFlags(cf);
1031   }
1032   flags().SetDefaults();
1033 
1034   FlagParser parser;
1035   RegisterCommonFlags(&parser);
1036   RegisterDfsanFlags(&parser, &flags());
1037   parser.ParseStringFromEnv("DFSAN_OPTIONS");
1038   InitializeCommonFlags();
1039   if (Verbosity()) ReportUnrecognizedFlags();
1040   if (common_flags()->help) parser.PrintFlagDescriptions();
1041 }
1042 
1043 SANITIZER_INTERFACE_ATTRIBUTE
1044 void dfsan_clear_arg_tls(uptr offset, uptr size) {
1045   internal_memset((void *)((uptr)__dfsan_arg_tls + offset), 0, size);
1046 }
1047 
1048 SANITIZER_INTERFACE_ATTRIBUTE
1049 void dfsan_clear_thread_local_state() {
1050   internal_memset(__dfsan_arg_tls, 0, sizeof(__dfsan_arg_tls));
1051   internal_memset(__dfsan_retval_tls, 0, sizeof(__dfsan_retval_tls));
1052 
1053   if (dfsan_get_track_origins()) {
1054     internal_memset(__dfsan_arg_origin_tls, 0, sizeof(__dfsan_arg_origin_tls));
1055     internal_memset(&__dfsan_retval_origin_tls, 0,
1056                     sizeof(__dfsan_retval_origin_tls));
1057   }
1058 }
1059 
1060 SANITIZER_INTERFACE_ATTRIBUTE
1061 void dfsan_set_arg_tls(uptr offset, dfsan_label label) {
1062   // 2x to match ShadowTLSAlignment.
1063   // ShadowTLSAlignment should probably be changed.
1064   // TODO: Consider reducing ShadowTLSAlignment to 1.
1065   // Aligning to 2 bytes is probably a remnant of fast16 mode.
1066   ((dfsan_label *)__dfsan_arg_tls)[offset * 2] = label;
1067 }
1068 
1069 SANITIZER_INTERFACE_ATTRIBUTE
1070 void dfsan_set_arg_origin_tls(uptr offset, dfsan_origin o) {
1071   __dfsan_arg_origin_tls[offset] = o;
1072 }
1073 
1074 extern "C" void dfsan_flush() {
1075   const uptr maxVirtualAddress = GetMaxUserVirtualAddress();
1076   for (unsigned i = 0; i < kMemoryLayoutSize; ++i) {
1077     uptr start = kMemoryLayout[i].start;
1078     uptr end = kMemoryLayout[i].end;
1079     uptr size = end - start;
1080     MappingDesc::Type type = kMemoryLayout[i].type;
1081 
1082     if (type != MappingDesc::SHADOW && type != MappingDesc::ORIGIN)
1083       continue;
1084 
1085     // Check if the segment should be mapped based on platform constraints.
1086     if (start >= maxVirtualAddress)
1087       continue;
1088 
1089     if (!MmapFixedSuperNoReserve(start, size, kMemoryLayout[i].name)) {
1090       Printf("FATAL: DataFlowSanitizer: failed to clear memory region\n");
1091       Die();
1092     }
1093   }
1094   __dfsan::labels_in_signal_conditional = 0;
1095   __dfsan::labels_in_signal_reaches_function = 0;
1096 }
1097 
1098 // TODO: CheckMemoryLayoutSanity is based on msan.
1099 // Consider refactoring these into a shared implementation.
1100 static void CheckMemoryLayoutSanity() {
1101   uptr prev_end = 0;
1102   for (unsigned i = 0; i < kMemoryLayoutSize; ++i) {
1103     uptr start = kMemoryLayout[i].start;
1104     uptr end = kMemoryLayout[i].end;
1105     MappingDesc::Type type = kMemoryLayout[i].type;
1106     CHECK_LT(start, end);
1107     CHECK_EQ(prev_end, start);
1108     CHECK(addr_is_type(start, type));
1109     CHECK(addr_is_type((start + end) / 2, type));
1110     CHECK(addr_is_type(end - 1, type));
1111     if (type == MappingDesc::APP) {
1112       uptr addr = start;
1113       CHECK(MEM_IS_SHADOW(MEM_TO_SHADOW(addr)));
1114       CHECK(MEM_IS_ORIGIN(MEM_TO_ORIGIN(addr)));
1115       CHECK_EQ(MEM_TO_ORIGIN(addr), SHADOW_TO_ORIGIN(MEM_TO_SHADOW(addr)));
1116 
1117       addr = (start + end) / 2;
1118       CHECK(MEM_IS_SHADOW(MEM_TO_SHADOW(addr)));
1119       CHECK(MEM_IS_ORIGIN(MEM_TO_ORIGIN(addr)));
1120       CHECK_EQ(MEM_TO_ORIGIN(addr), SHADOW_TO_ORIGIN(MEM_TO_SHADOW(addr)));
1121 
1122       addr = end - 1;
1123       CHECK(MEM_IS_SHADOW(MEM_TO_SHADOW(addr)));
1124       CHECK(MEM_IS_ORIGIN(MEM_TO_ORIGIN(addr)));
1125       CHECK_EQ(MEM_TO_ORIGIN(addr), SHADOW_TO_ORIGIN(MEM_TO_SHADOW(addr)));
1126     }
1127     prev_end = end;
1128   }
1129 }
1130 
1131 // TODO: CheckMemoryRangeAvailability is based on msan.
1132 // Consider refactoring these into a shared implementation.
1133 static bool CheckMemoryRangeAvailability(uptr beg, uptr size) {
1134   if (size > 0) {
1135     uptr end = beg + size - 1;
1136     if (!MemoryRangeIsAvailable(beg, end)) {
1137       Printf("FATAL: Memory range %p - %p is not available.\n", beg, end);
1138       return false;
1139     }
1140   }
1141   return true;
1142 }
1143 
1144 // TODO: ProtectMemoryRange is based on msan.
1145 // Consider refactoring these into a shared implementation.
1146 static bool ProtectMemoryRange(uptr beg, uptr size, const char *name) {
1147   if (size > 0) {
1148     void *addr = MmapFixedNoAccess(beg, size, name);
1149     if (beg == 0 && addr) {
1150       // Depending on the kernel configuration, we may not be able to protect
1151       // the page at address zero.
1152       uptr gap = 16 * GetPageSizeCached();
1153       beg += gap;
1154       size -= gap;
1155       addr = MmapFixedNoAccess(beg, size, name);
1156     }
1157     if ((uptr)addr != beg) {
1158       uptr end = beg + size - 1;
1159       Printf("FATAL: Cannot protect memory range %p - %p (%s).\n", beg, end,
1160              name);
1161       return false;
1162     }
1163   }
1164   return true;
1165 }
1166 
1167 // TODO: InitShadow is based on msan.
1168 // Consider refactoring these into a shared implementation.
1169 bool InitShadow(bool init_origins) {
1170   // Let user know mapping parameters first.
1171   VPrintf(1, "dfsan_init %p\n", (void *)&__dfsan::dfsan_init);
1172   for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
1173     VPrintf(1, "%s: %zx - %zx\n", kMemoryLayout[i].name, kMemoryLayout[i].start,
1174             kMemoryLayout[i].end - 1);
1175 
1176   CheckMemoryLayoutSanity();
1177 
1178   if (!MEM_IS_APP(&__dfsan::dfsan_init)) {
1179     Printf("FATAL: Code %p is out of application range. Non-PIE build?\n",
1180            (uptr)&__dfsan::dfsan_init);
1181     return false;
1182   }
1183 
1184   const uptr maxVirtualAddress = GetMaxUserVirtualAddress();
1185 
1186   for (unsigned i = 0; i < kMemoryLayoutSize; ++i) {
1187     uptr start = kMemoryLayout[i].start;
1188     uptr end = kMemoryLayout[i].end;
1189     uptr size = end - start;
1190     MappingDesc::Type type = kMemoryLayout[i].type;
1191 
1192     // Check if the segment should be mapped based on platform constraints.
1193     if (start >= maxVirtualAddress)
1194       continue;
1195 
1196     bool map = type == MappingDesc::SHADOW ||
1197                (init_origins && type == MappingDesc::ORIGIN);
1198     bool protect = type == MappingDesc::INVALID ||
1199                    (!init_origins && type == MappingDesc::ORIGIN);
1200     CHECK(!(map && protect));
1201     if (!map && !protect)
1202       CHECK(type == MappingDesc::APP);
1203     if (map) {
1204       if (!CheckMemoryRangeAvailability(start, size))
1205         return false;
1206       if (!MmapFixedSuperNoReserve(start, size, kMemoryLayout[i].name))
1207         return false;
1208       if (common_flags()->use_madv_dontdump)
1209         DontDumpShadowMemory(start, size);
1210     }
1211     if (protect) {
1212       if (!CheckMemoryRangeAvailability(start, size))
1213         return false;
1214       if (!ProtectMemoryRange(start, size, kMemoryLayout[i].name))
1215         return false;
1216     }
1217   }
1218 
1219   return true;
1220 }
1221 
1222 static void DFsanInit(int argc, char **argv, char **envp) {
1223   CHECK(!dfsan_init_is_running);
1224   if (dfsan_inited)
1225     return;
1226   dfsan_init_is_running = true;
1227   SanitizerToolName = "DataflowSanitizer";
1228 
1229   AvoidCVE_2016_2143();
1230 
1231   InitializeFlags();
1232 
1233   CheckASLR();
1234 
1235   InitShadow(dfsan_get_track_origins());
1236 
1237   initialize_interceptors();
1238 
1239   // Set up threads
1240   DFsanTSDInit(DFsanTSDDtor);
1241 
1242   dfsan_allocator_init();
1243 
1244   DFsanThread *main_thread = DFsanThread::Create(nullptr, nullptr);
1245   SetCurrentThread(main_thread);
1246   main_thread->Init();
1247 
1248   dfsan_init_is_running = false;
1249   dfsan_inited = true;
1250 }
1251 
1252 namespace __dfsan {
1253 
1254 void dfsan_init() { DFsanInit(0, nullptr, nullptr); }
1255 
1256 }  // namespace __dfsan
1257 
1258 #if SANITIZER_CAN_USE_PREINIT_ARRAY
1259 __attribute__((section(".preinit_array"),
1260                used)) static void (*dfsan_init_ptr)(int, char **,
1261                                                     char **) = DFsanInit;
1262 #endif
1263