dfsan.cpp 36 KB

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