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_stacktrace.h"
35 
36 using namespace __dfsan;
37 
38 typedef atomic_uint16_t atomic_dfsan_label;
39 static const dfsan_label kInitializingLabel = -1;
40 
41 static const uptr kNumLabels = 1 << (sizeof(dfsan_label) * 8);
42 
43 static atomic_dfsan_label __dfsan_last_label;
44 static dfsan_label_info __dfsan_label_info[kNumLabels];
45 
46 Flags __dfsan::flags_data;
47 
48 // The size of TLS variables. These constants must be kept in sync with the ones
49 // in DataFlowSanitizer.cpp.
50 static const int kDFsanArgTlsSize = 800;
51 static const int kDFsanRetvalTlsSize = 800;
52 
53 SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL u64
54     __dfsan_retval_tls[kDFsanRetvalTlsSize / sizeof(u64)];
55 SANITIZER_INTERFACE_ATTRIBUTE THREADLOCAL u64
56     __dfsan_arg_tls[kDFsanArgTlsSize / sizeof(u64)];
57 
58 SANITIZER_INTERFACE_ATTRIBUTE uptr __dfsan_shadow_ptr_mask;
59 
60 // On Linux/x86_64, memory is laid out as follows:
61 //
62 // +--------------------+ 0x800000000000 (top of memory)
63 // | application memory |
64 // +--------------------+ 0x700000008000 (kAppAddr)
65 // |                    |
66 // |       unused       |
67 // |                    |
68 // +--------------------+ 0x300200000000 (kUnusedAddr)
69 // |    union table     |
70 // +--------------------+ 0x300000000000 (kUnionTableAddr)
71 // |       origin       |
72 // +--------------------+ 0x200000000000 (kOriginAddr)
73 // |   shadow memory    |
74 // +--------------------+ 0x000000010000 (kShadowAddr)
75 // | reserved by kernel |
76 // +--------------------+ 0x000000000000
77 //
78 // To derive a shadow memory address from an application memory address,
79 // bits 44-46 are cleared to bring the address into the range
80 // [0x000000008000,0x100000000000).  Then the address is shifted left by 1 to
81 // account for the double byte representation of shadow labels and move the
82 // address into the shadow memory range.  See the function shadow_for below.
83 
84 // On Linux/MIPS64, memory is laid out as follows:
85 //
86 // +--------------------+ 0x10000000000 (top of memory)
87 // | application memory |
88 // +--------------------+ 0xF000008000 (kAppAddr)
89 // |                    |
90 // |       unused       |
91 // |                    |
92 // +--------------------+ 0x2200000000 (kUnusedAddr)
93 // |    union table     |
94 // +--------------------+ 0x2000000000 (kUnionTableAddr)
95 // |   shadow memory    |
96 // +--------------------+ 0x0000010000 (kShadowAddr)
97 // | reserved by kernel |
98 // +--------------------+ 0x0000000000
99 
100 // On Linux/AArch64 (39-bit VMA), memory is laid out as follow:
101 //
102 // +--------------------+ 0x8000000000 (top of memory)
103 // | application memory |
104 // +--------------------+ 0x7000008000 (kAppAddr)
105 // |                    |
106 // |       unused       |
107 // |                    |
108 // +--------------------+ 0x1200000000 (kUnusedAddr)
109 // |    union table     |
110 // +--------------------+ 0x1000000000 (kUnionTableAddr)
111 // |   shadow memory    |
112 // +--------------------+ 0x0000010000 (kShadowAddr)
113 // | reserved by kernel |
114 // +--------------------+ 0x0000000000
115 
116 // On Linux/AArch64 (42-bit VMA), memory is laid out as follow:
117 //
118 // +--------------------+ 0x40000000000 (top of memory)
119 // | application memory |
120 // +--------------------+ 0x3ff00008000 (kAppAddr)
121 // |                    |
122 // |       unused       |
123 // |                    |
124 // +--------------------+ 0x1200000000 (kUnusedAddr)
125 // |    union table     |
126 // +--------------------+ 0x8000000000 (kUnionTableAddr)
127 // |   shadow memory    |
128 // +--------------------+ 0x0000010000 (kShadowAddr)
129 // | reserved by kernel |
130 // +--------------------+ 0x0000000000
131 
132 // On Linux/AArch64 (48-bit VMA), memory is laid out as follow:
133 //
134 // +--------------------+ 0x1000000000000 (top of memory)
135 // | application memory |
136 // +--------------------+ 0xffff00008000 (kAppAddr)
137 // |       unused       |
138 // +--------------------+ 0xaaaab0000000 (top of PIE address)
139 // | application PIE    |
140 // +--------------------+ 0xaaaaa0000000 (top of PIE address)
141 // |                    |
142 // |       unused       |
143 // |                    |
144 // +--------------------+ 0x1200000000 (kUnusedAddr)
145 // |    union table     |
146 // +--------------------+ 0x8000000000 (kUnionTableAddr)
147 // |   shadow memory    |
148 // +--------------------+ 0x0000010000 (kShadowAddr)
149 // | reserved by kernel |
150 // +--------------------+ 0x0000000000
151 
152 typedef atomic_dfsan_label dfsan_union_table_t[kNumLabels][kNumLabels];
153 
154 #ifdef DFSAN_RUNTIME_VMA
155 // Runtime detected VMA size.
156 int __dfsan::vmaSize;
157 #endif
158 
159 static uptr UnusedAddr() {
160   return UnionTableAddr() + sizeof(dfsan_union_table_t);
161 }
162 
163 static atomic_dfsan_label *union_table(dfsan_label l1, dfsan_label l2) {
164   return &(*(dfsan_union_table_t *) UnionTableAddr())[l1][l2];
165 }
166 
167 // Checks we do not run out of labels.
168 static void dfsan_check_label(dfsan_label label) {
169   if (label == kInitializingLabel) {
170     Report("FATAL: DataFlowSanitizer: out of labels\n");
171     Die();
172   }
173 }
174 
175 // Resolves the union of two unequal labels.  Nonequality is a precondition for
176 // this function (the instrumentation pass inlines the equality test).
177 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
178 dfsan_label __dfsan_union(dfsan_label l1, dfsan_label l2) {
179   DCHECK_NE(l1, l2);
180 
181   if (l1 == 0)
182     return l2;
183   if (l2 == 0)
184     return l1;
185 
186   // If no labels have been created, yet l1 and l2 are non-zero, we are using
187   // fast16labels mode.
188   if (atomic_load(&__dfsan_last_label, memory_order_relaxed) == 0)
189     return l1 | l2;
190 
191   if (l1 > l2)
192     Swap(l1, l2);
193 
194   atomic_dfsan_label *table_ent = union_table(l1, l2);
195   // We need to deal with the case where two threads concurrently request
196   // a union of the same pair of labels.  If the table entry is uninitialized,
197   // (i.e. 0) use a compare-exchange to set the entry to kInitializingLabel
198   // (i.e. -1) to mark that we are initializing it.
199   dfsan_label label = 0;
200   if (atomic_compare_exchange_strong(table_ent, &label, kInitializingLabel,
201                                      memory_order_acquire)) {
202     // Check whether l2 subsumes l1.  We don't need to check whether l1
203     // subsumes l2 because we are guaranteed here that l1 < l2, and (at least
204     // in the cases we are interested in) a label may only subsume labels
205     // created earlier (i.e. with a lower numerical value).
206     if (__dfsan_label_info[l2].l1 == l1 ||
207         __dfsan_label_info[l2].l2 == l1) {
208       label = l2;
209     } else {
210       label =
211         atomic_fetch_add(&__dfsan_last_label, 1, memory_order_relaxed) + 1;
212       dfsan_check_label(label);
213       __dfsan_label_info[label].l1 = l1;
214       __dfsan_label_info[label].l2 = l2;
215     }
216     atomic_store(table_ent, label, memory_order_release);
217   } else if (label == kInitializingLabel) {
218     // Another thread is initializing the entry.  Wait until it is finished.
219     do {
220       internal_sched_yield();
221       label = atomic_load(table_ent, memory_order_acquire);
222     } while (label == kInitializingLabel);
223   }
224   return label;
225 }
226 
227 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
228 dfsan_label __dfsan_union_load(const dfsan_label *ls, uptr n) {
229   dfsan_label label = ls[0];
230   for (uptr i = 1; i != n; ++i) {
231     dfsan_label next_label = ls[i];
232     if (label != next_label)
233       label = __dfsan_union(label, next_label);
234   }
235   return label;
236 }
237 
238 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
239 dfsan_label __dfsan_union_load_fast16labels(const dfsan_label *ls, uptr n) {
240   dfsan_label label = ls[0];
241   for (uptr i = 1; i != n; ++i)
242     label |= ls[i];
243   return label;
244 }
245 
246 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
247 void __dfsan_unimplemented(char *fname) {
248   if (flags().warn_unimplemented)
249     Report("WARNING: DataFlowSanitizer: call to uninstrumented function %s\n",
250            fname);
251 }
252 
253 // Use '-mllvm -dfsan-debug-nonzero-labels' and break on this function
254 // to try to figure out where labels are being introduced in a nominally
255 // label-free program.
256 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_nonzero_label() {
257   if (flags().warn_nonzero_labels)
258     Report("WARNING: DataFlowSanitizer: saw nonzero label\n");
259 }
260 
261 // Indirect call to an uninstrumented vararg function. We don't have a way of
262 // handling these at the moment.
263 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
264 __dfsan_vararg_wrapper(const char *fname) {
265   Report("FATAL: DataFlowSanitizer: unsupported indirect call to vararg "
266          "function %s\n", fname);
267   Die();
268 }
269 
270 // Like __dfsan_union, but for use from the client or custom functions.  Hence
271 // the equality comparison is done here before calling __dfsan_union.
272 SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
273 dfsan_union(dfsan_label l1, dfsan_label l2) {
274   if (l1 == l2)
275     return l1;
276   return __dfsan_union(l1, l2);
277 }
278 
279 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
280 dfsan_label dfsan_create_label(const char *desc, void *userdata) {
281   dfsan_label label =
282       atomic_fetch_add(&__dfsan_last_label, 1, memory_order_relaxed) + 1;
283   dfsan_check_label(label);
284   __dfsan_label_info[label].l1 = __dfsan_label_info[label].l2 = 0;
285   __dfsan_label_info[label].desc = desc;
286   __dfsan_label_info[label].userdata = userdata;
287   return label;
288 }
289 
290 // For platforms which support slow unwinder only, we need to restrict the store
291 // context size to 1, basically only storing the current pc, because the slow
292 // unwinder which is based on libunwind is not async signal safe and causes
293 // random freezes in forking applications as well as in signal handlers.
294 // DFSan supports only Linux. So we do not restrict the store context size.
295 #define GET_STORE_STACK_TRACE_PC_BP(pc, bp) \
296   BufferedStackTrace stack;                 \
297   stack.Unwind(pc, bp, nullptr, true, flags().store_context_size);
298 
299 #define PRINT_CALLER_STACK_TRACE        \
300   {                                     \
301     GET_CALLER_PC_BP_SP;                \
302     (void)sp;                           \
303     GET_STORE_STACK_TRACE_PC_BP(pc, bp) \
304     stack.Print();                      \
305   }
306 
307 /*
308 static u32 ChainOrigin(u32 id, StackTrace *stack, bool from_init = false) {
309   // StackDepot is not async signal safe. Do not create new chains in a signal
310   // handler.
311   DFsanThread *t = GetCurrentThread();
312   if (t && t->InSignalHandler())
313     return id;
314 
315   // As an optimization the origin of an application byte is updated only when
316   // its shadow is non-zero. Because we are only interested in the origins of
317   // taint labels, it does not matter what origin a zero label has. This reduces
318   // memory write cost. MSan does similar optimization. The following invariant
319   // may not hold because of some bugs. We check the invariant to help debug.
320   if (!from_init && id == 0 && flags().check_origin_invariant) {
321     Printf("  DFSan found invalid origin invariant\n");
322     PRINT_CALLER_STACK_TRACE
323   }
324 
325   Origin o = Origin::FromRawId(id);
326   stack->tag = StackTrace::TAG_UNKNOWN;
327   Origin chained = Origin::CreateChainedOrigin(o, stack);
328   return chained.raw_id();
329 }
330 */
331 
332 static void WriteShadowIfDifferent(dfsan_label label, uptr shadow_addr,
333                                    uptr size) {
334   dfsan_label *labelp = (dfsan_label *)shadow_addr;
335   for (; size != 0; --size, ++labelp) {
336     // Don't write the label if it is already the value we need it to be.
337     // In a program where most addresses are not labeled, it is common that
338     // a page of shadow memory is entirely zeroed.  The Linux copy-on-write
339     // implementation will share all of the zeroed pages, making a copy of a
340     // page when any value is written.  The un-sharing will happen even if
341     // the value written does not change the value in memory.  Avoiding the
342     // write when both |label| and |*labelp| are zero dramatically reduces
343     // the amount of real memory used by large programs.
344     if (label == *labelp)
345       continue;
346 
347     *labelp = label;
348   }
349 }
350 
351 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __dfsan_set_label(
352     dfsan_label label, void *addr, uptr size) {
353   const uptr beg_shadow_addr = (uptr)__dfsan::shadow_for(addr);
354 
355   if (0 != label) {
356     WriteShadowIfDifferent(label, beg_shadow_addr, size);
357     return;
358   }
359 
360   // If label is 0, releases the pages within the shadow address range, and sets
361   // the shadow addresses not on the pages to be 0.
362   const void *end_addr = (void *)((uptr)addr + size);
363   const uptr end_shadow_addr = (uptr)__dfsan::shadow_for(end_addr);
364   const uptr page_size = GetPageSizeCached();
365   const uptr beg_aligned = RoundUpTo(beg_shadow_addr, page_size);
366   const uptr end_aligned = RoundDownTo(end_shadow_addr, page_size);
367 
368   // dfsan_set_label can be called from the following cases
369   // 1) mapped ranges by new/delete and malloc/free. This case has shadow memory
370   // size > 100k, and happens less frequently.
371   // 2) zero-filling internal data structures by utility libraries. This case
372   // has shadow memory size < 32k, and happens more often.
373   // Set kNumPagesThreshold to be 8 to avoid releasing small pages.
374   const int kNumPagesThreshold = 8;
375   if (beg_aligned + kNumPagesThreshold * page_size >= end_aligned)
376     return WriteShadowIfDifferent(label, beg_shadow_addr, size);
377 
378   WriteShadowIfDifferent(label, beg_shadow_addr, beg_aligned - beg_shadow_addr);
379   ReleaseMemoryPagesToOS(beg_aligned, end_aligned);
380   WriteShadowIfDifferent(label, end_aligned, end_shadow_addr - end_aligned);
381 }
382 
383 SANITIZER_INTERFACE_ATTRIBUTE
384 void dfsan_set_label(dfsan_label label, void *addr, uptr size) {
385   __dfsan_set_label(label, addr, size);
386 }
387 
388 SANITIZER_INTERFACE_ATTRIBUTE
389 void dfsan_add_label(dfsan_label label, void *addr, uptr size) {
390   for (dfsan_label *labelp = shadow_for(addr); size != 0; --size, ++labelp)
391     if (*labelp != label)
392       *labelp = __dfsan_union(*labelp, label);
393 }
394 
395 // Unlike the other dfsan interface functions the behavior of this function
396 // depends on the label of one of its arguments.  Hence it is implemented as a
397 // custom function.
398 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
399 __dfsw_dfsan_get_label(long data, dfsan_label data_label,
400                        dfsan_label *ret_label) {
401   *ret_label = 0;
402   return data_label;
403 }
404 
405 SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
406 dfsan_read_label(const void *addr, uptr size) {
407   if (size == 0)
408     return 0;
409   return __dfsan_union_load(shadow_for(addr), size);
410 }
411 
412 extern "C" SANITIZER_INTERFACE_ATTRIBUTE
413 const struct dfsan_label_info *dfsan_get_label_info(dfsan_label label) {
414   return &__dfsan_label_info[label];
415 }
416 
417 extern "C" SANITIZER_INTERFACE_ATTRIBUTE int
418 dfsan_has_label(dfsan_label label, dfsan_label elem) {
419   if (label == elem)
420     return true;
421   const dfsan_label_info *info = dfsan_get_label_info(label);
422   if (info->l1 != 0) {
423     return dfsan_has_label(info->l1, elem) || dfsan_has_label(info->l2, elem);
424   } else {
425     return false;
426   }
427 }
428 
429 extern "C" SANITIZER_INTERFACE_ATTRIBUTE dfsan_label
430 dfsan_has_label_with_desc(dfsan_label label, const char *desc) {
431   const dfsan_label_info *info = dfsan_get_label_info(label);
432   if (info->l1 != 0) {
433     return dfsan_has_label_with_desc(info->l1, desc) ||
434            dfsan_has_label_with_desc(info->l2, desc);
435   } else {
436     return internal_strcmp(desc, info->desc) == 0;
437   }
438 }
439 
440 extern "C" SANITIZER_INTERFACE_ATTRIBUTE uptr
441 dfsan_get_label_count(void) {
442   dfsan_label max_label_allocated =
443       atomic_load(&__dfsan_last_label, memory_order_relaxed);
444 
445   return static_cast<uptr>(max_label_allocated);
446 }
447 
448 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void
449 dfsan_dump_labels(int fd) {
450   dfsan_label last_label =
451       atomic_load(&__dfsan_last_label, memory_order_relaxed);
452   for (uptr l = 1; l <= last_label; ++l) {
453     char buf[64];
454     internal_snprintf(buf, sizeof(buf), "%u %u %u ", l,
455                       __dfsan_label_info[l].l1, __dfsan_label_info[l].l2);
456     WriteToFile(fd, buf, internal_strlen(buf));
457     if (__dfsan_label_info[l].l1 == 0 && __dfsan_label_info[l].desc) {
458       WriteToFile(fd, __dfsan_label_info[l].desc,
459                   internal_strlen(__dfsan_label_info[l].desc));
460     }
461     WriteToFile(fd, "\n", 1);
462   }
463 }
464 
465 #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \
466   BufferedStackTrace stack;                 \
467   stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal);
468 
469 void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
470                                                  void *context,
471                                                  bool request_fast,
472                                                  u32 max_depth) {
473   using namespace __dfsan;
474   DFsanThread *t = GetCurrentThread();
475   if (!t || !StackTrace::WillUseFastUnwind(request_fast)) {
476     return Unwind(max_depth, pc, bp, context, 0, 0, false);
477   }
478   Unwind(max_depth, pc, bp, nullptr, t->stack_top(), t->stack_bottom(), true);
479 }
480 
481 extern "C" SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_print_stack_trace() {
482   GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME());
483   stack.Print();
484 }
485 
486 void Flags::SetDefaults() {
487 #define DFSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
488 #include "dfsan_flags.inc"
489 #undef DFSAN_FLAG
490 }
491 
492 static void RegisterDfsanFlags(FlagParser *parser, Flags *f) {
493 #define DFSAN_FLAG(Type, Name, DefaultValue, Description) \
494   RegisterFlag(parser, #Name, Description, &f->Name);
495 #include "dfsan_flags.inc"
496 #undef DFSAN_FLAG
497 }
498 
499 static void InitializeFlags() {
500   SetCommonFlagsDefaults();
501   flags().SetDefaults();
502 
503   FlagParser parser;
504   RegisterCommonFlags(&parser);
505   RegisterDfsanFlags(&parser, &flags());
506   parser.ParseStringFromEnv("DFSAN_OPTIONS");
507   InitializeCommonFlags();
508   if (Verbosity()) ReportUnrecognizedFlags();
509   if (common_flags()->help) parser.PrintFlagDescriptions();
510 }
511 
512 SANITIZER_INTERFACE_ATTRIBUTE
513 void dfsan_clear_arg_tls(uptr offset, uptr size) {
514   internal_memset((void *)((uptr)__dfsan_arg_tls + offset), 0, size);
515 }
516 
517 SANITIZER_INTERFACE_ATTRIBUTE
518 void dfsan_clear_thread_local_state() {
519   internal_memset(__dfsan_arg_tls, 0, sizeof(__dfsan_arg_tls));
520   internal_memset(__dfsan_retval_tls, 0, sizeof(__dfsan_retval_tls));
521 }
522 
523 static void InitializePlatformEarly() {
524   AvoidCVE_2016_2143();
525 #ifdef DFSAN_RUNTIME_VMA
526   __dfsan::vmaSize =
527     (MostSignificantSetBitIndex(GET_CURRENT_FRAME()) + 1);
528   if (__dfsan::vmaSize == 39 || __dfsan::vmaSize == 42 ||
529       __dfsan::vmaSize == 48) {
530     __dfsan_shadow_ptr_mask = ShadowMask();
531   } else {
532     Printf("FATAL: DataFlowSanitizer: unsupported VMA range\n");
533     Printf("FATAL: Found %d - Supported 39, 42, and 48\n", __dfsan::vmaSize);
534     Die();
535   }
536 #endif
537 }
538 
539 static void dfsan_fini() {
540   if (internal_strcmp(flags().dump_labels_at_exit, "") != 0) {
541     fd_t fd = OpenFile(flags().dump_labels_at_exit, WrOnly);
542     if (fd == kInvalidFd) {
543       Report("WARNING: DataFlowSanitizer: unable to open output file %s\n",
544              flags().dump_labels_at_exit);
545       return;
546     }
547 
548     Report("INFO: DataFlowSanitizer: dumping labels to %s\n",
549            flags().dump_labels_at_exit);
550     dfsan_dump_labels(fd);
551     CloseFile(fd);
552   }
553 }
554 
555 extern "C" void dfsan_flush() {
556   if (!MmapFixedSuperNoReserve(ShadowAddr(), UnusedAddr() - ShadowAddr()))
557     Die();
558 }
559 
560 static void dfsan_init(int argc, char **argv, char **envp) {
561   InitializeFlags();
562 
563   ::InitializePlatformEarly();
564 
565   dfsan_flush();
566   if (common_flags()->use_madv_dontdump)
567     DontDumpShadowMemory(ShadowAddr(), UnusedAddr() - ShadowAddr());
568 
569   // Protect the region of memory we don't use, to preserve the one-to-one
570   // mapping from application to shadow memory. But if ASLR is disabled, Linux
571   // will load our executable in the middle of our unused region. This mostly
572   // works so long as the program doesn't use too much memory. We support this
573   // case by disabling memory protection when ASLR is disabled.
574   uptr init_addr = (uptr)&dfsan_init;
575   if (!(init_addr >= UnusedAddr() && init_addr < AppAddr()))
576     MmapFixedNoAccess(UnusedAddr(), AppAddr() - UnusedAddr());
577 
578   InitializeInterceptors();
579 
580   // Register the fini callback to run when the program terminates successfully
581   // or it is killed by the runtime.
582   Atexit(dfsan_fini);
583   AddDieCallback(dfsan_fini);
584 
585   // Set up threads
586   DFsanTSDInit(DFsanTSDDtor);
587   DFsanThread *main_thread = DFsanThread::Create(nullptr, nullptr, nullptr);
588   SetCurrentThread(main_thread);
589   main_thread->ThreadStart();
590 
591   __dfsan_label_info[kInitializingLabel].desc = "<init label>";
592 }
593 
594 #if SANITIZER_CAN_USE_PREINIT_ARRAY
595 __attribute__((section(".preinit_array"), used))
596 static void (*dfsan_init_ptr)(int, char **, char **) = dfsan_init;
597 #endif
598