msan.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. //===-- msan.h --------------------------------------------------*- C++ -*-===//
  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 MemorySanitizer.
  10. //
  11. // Private MSan header.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef MSAN_H
  14. #define MSAN_H
  15. #include "sanitizer_common/sanitizer_flags.h"
  16. #include "sanitizer_common/sanitizer_internal_defs.h"
  17. #include "sanitizer_common/sanitizer_stacktrace.h"
  18. #include "msan_interface_internal.h"
  19. #include "msan_flags.h"
  20. #include "ubsan/ubsan_platform.h"
  21. #ifndef MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
  22. # define MSAN_REPLACE_OPERATORS_NEW_AND_DELETE 1
  23. #endif
  24. #ifndef MSAN_CONTAINS_UBSAN
  25. # define MSAN_CONTAINS_UBSAN CAN_SANITIZE_UB
  26. #endif
  27. struct MappingDesc {
  28. uptr start;
  29. uptr end;
  30. enum Type {
  31. INVALID, APP, SHADOW, ORIGIN
  32. } type;
  33. const char *name;
  34. };
  35. #if SANITIZER_LINUX && defined(__mips64)
  36. // MIPS64 maps:
  37. // - 0x0000000000-0x0200000000: Program own segments
  38. // - 0xa200000000-0xc000000000: PIE program segments
  39. // - 0xe200000000-0xffffffffff: libraries segments.
  40. const MappingDesc kMemoryLayout[] = {
  41. {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "app-1"},
  42. {0x000200000000ULL, 0x002200000000ULL, MappingDesc::INVALID, "invalid"},
  43. {0x002200000000ULL, 0x004000000000ULL, MappingDesc::SHADOW, "shadow-2"},
  44. {0x004000000000ULL, 0x004200000000ULL, MappingDesc::INVALID, "invalid"},
  45. {0x004200000000ULL, 0x006000000000ULL, MappingDesc::ORIGIN, "origin-2"},
  46. {0x006000000000ULL, 0x006200000000ULL, MappingDesc::INVALID, "invalid"},
  47. {0x006200000000ULL, 0x008000000000ULL, MappingDesc::SHADOW, "shadow-3"},
  48. {0x008000000000ULL, 0x008200000000ULL, MappingDesc::SHADOW, "shadow-1"},
  49. {0x008200000000ULL, 0x00a000000000ULL, MappingDesc::ORIGIN, "origin-3"},
  50. {0x00a000000000ULL, 0x00a200000000ULL, MappingDesc::ORIGIN, "origin-1"},
  51. {0x00a200000000ULL, 0x00c000000000ULL, MappingDesc::APP, "app-2"},
  52. {0x00c000000000ULL, 0x00e200000000ULL, MappingDesc::INVALID, "invalid"},
  53. {0x00e200000000ULL, 0x00ffffffffffULL, MappingDesc::APP, "app-3"}};
  54. #define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x8000000000ULL)
  55. #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x2000000000ULL)
  56. #elif SANITIZER_LINUX && defined(__aarch64__)
  57. // The mapping describes both 39-bits, 42-bits, and 48-bits VMA. AArch64
  58. // maps:
  59. // - 0x0000000000000-0x0000010000000: 39/42/48-bits program own segments
  60. // - 0x0005500000000-0x0005600000000: 39-bits PIE program segments
  61. // - 0x0007f80000000-0x0007fffffffff: 39-bits libraries segments
  62. // - 0x002aa00000000-0x002ab00000000: 42-bits PIE program segments
  63. // - 0x003ff00000000-0x003ffffffffff: 42-bits libraries segments
  64. // - 0x0aaaaa0000000-0x0aaab00000000: 48-bits PIE program segments
  65. // - 0xffff000000000-0x1000000000000: 48-bits libraries segments
  66. // It is fragmented in multiples segments to increase the memory available
  67. // on 42-bits (12.21% of total VMA available for 42-bits and 13.28 for
  68. // 39 bits). The 48-bits segments only cover the usual PIE/default segments
  69. // plus some more segments (262144GB total, 0.39% total VMA).
  70. const MappingDesc kMemoryLayout[] = {
  71. {0x00000000000ULL, 0x01000000000ULL, MappingDesc::INVALID, "invalid"},
  72. {0x01000000000ULL, 0x02000000000ULL, MappingDesc::SHADOW, "shadow-2"},
  73. {0x02000000000ULL, 0x03000000000ULL, MappingDesc::ORIGIN, "origin-2"},
  74. {0x03000000000ULL, 0x04000000000ULL, MappingDesc::SHADOW, "shadow-1"},
  75. {0x04000000000ULL, 0x05000000000ULL, MappingDesc::ORIGIN, "origin-1"},
  76. {0x05000000000ULL, 0x06000000000ULL, MappingDesc::APP, "app-1"},
  77. {0x06000000000ULL, 0x07000000000ULL, MappingDesc::INVALID, "invalid"},
  78. {0x07000000000ULL, 0x08000000000ULL, MappingDesc::APP, "app-2"},
  79. {0x08000000000ULL, 0x09000000000ULL, MappingDesc::INVALID, "invalid"},
  80. // The mappings below are used only for 42-bits VMA.
  81. {0x09000000000ULL, 0x0A000000000ULL, MappingDesc::SHADOW, "shadow-3"},
  82. {0x0A000000000ULL, 0x0B000000000ULL, MappingDesc::ORIGIN, "origin-3"},
  83. {0x0B000000000ULL, 0x0F000000000ULL, MappingDesc::INVALID, "invalid"},
  84. {0x0F000000000ULL, 0x10000000000ULL, MappingDesc::APP, "app-3"},
  85. {0x10000000000ULL, 0x11000000000ULL, MappingDesc::INVALID, "invalid"},
  86. {0x11000000000ULL, 0x12000000000ULL, MappingDesc::APP, "app-4"},
  87. {0x12000000000ULL, 0x17000000000ULL, MappingDesc::INVALID, "invalid"},
  88. {0x17000000000ULL, 0x18000000000ULL, MappingDesc::SHADOW, "shadow-4"},
  89. {0x18000000000ULL, 0x19000000000ULL, MappingDesc::ORIGIN, "origin-4"},
  90. {0x19000000000ULL, 0x20000000000ULL, MappingDesc::INVALID, "invalid"},
  91. {0x20000000000ULL, 0x21000000000ULL, MappingDesc::APP, "app-5"},
  92. {0x21000000000ULL, 0x26000000000ULL, MappingDesc::INVALID, "invalid"},
  93. {0x26000000000ULL, 0x27000000000ULL, MappingDesc::SHADOW, "shadow-5"},
  94. {0x27000000000ULL, 0x28000000000ULL, MappingDesc::ORIGIN, "origin-5"},
  95. {0x28000000000ULL, 0x29000000000ULL, MappingDesc::SHADOW, "shadow-7"},
  96. {0x29000000000ULL, 0x2A000000000ULL, MappingDesc::ORIGIN, "origin-7"},
  97. {0x2A000000000ULL, 0x2B000000000ULL, MappingDesc::APP, "app-6"},
  98. {0x2B000000000ULL, 0x2C000000000ULL, MappingDesc::INVALID, "invalid"},
  99. {0x2C000000000ULL, 0x2D000000000ULL, MappingDesc::SHADOW, "shadow-6"},
  100. {0x2D000000000ULL, 0x2E000000000ULL, MappingDesc::ORIGIN, "origin-6"},
  101. {0x2E000000000ULL, 0x2F000000000ULL, MappingDesc::APP, "app-7"},
  102. {0x2F000000000ULL, 0x39000000000ULL, MappingDesc::INVALID, "invalid"},
  103. {0x39000000000ULL, 0x3A000000000ULL, MappingDesc::SHADOW, "shadow-9"},
  104. {0x3A000000000ULL, 0x3B000000000ULL, MappingDesc::ORIGIN, "origin-9"},
  105. {0x3B000000000ULL, 0x3C000000000ULL, MappingDesc::APP, "app-8"},
  106. {0x3C000000000ULL, 0x3D000000000ULL, MappingDesc::INVALID, "invalid"},
  107. {0x3D000000000ULL, 0x3E000000000ULL, MappingDesc::SHADOW, "shadow-8"},
  108. {0x3E000000000ULL, 0x3F000000000ULL, MappingDesc::ORIGIN, "origin-8"},
  109. {0x3F000000000ULL, 0x40000000000ULL, MappingDesc::APP, "app-9"},
  110. // The mappings below are used only for 48-bits VMA.
  111. // TODO(unknown): 48-bit mapping ony covers the usual PIE, non-PIE
  112. // segments and some more segments totalizing 262144GB of VMA (which cover
  113. // only 0.32% of all 48-bit VMA). Memory availability can be increase by
  114. // adding multiple application segments like 39 and 42 mapping.
  115. {0x0040000000000ULL, 0x0041000000000ULL, MappingDesc::INVALID, "invalid"},
  116. {0x0041000000000ULL, 0x0042000000000ULL, MappingDesc::APP, "app-10"},
  117. {0x0042000000000ULL, 0x0047000000000ULL, MappingDesc::INVALID, "invalid"},
  118. {0x0047000000000ULL, 0x0048000000000ULL, MappingDesc::SHADOW, "shadow-10"},
  119. {0x0048000000000ULL, 0x0049000000000ULL, MappingDesc::ORIGIN, "origin-10"},
  120. {0x0049000000000ULL, 0x0050000000000ULL, MappingDesc::INVALID, "invalid"},
  121. {0x0050000000000ULL, 0x0051000000000ULL, MappingDesc::APP, "app-11"},
  122. {0x0051000000000ULL, 0x0056000000000ULL, MappingDesc::INVALID, "invalid"},
  123. {0x0056000000000ULL, 0x0057000000000ULL, MappingDesc::SHADOW, "shadow-11"},
  124. {0x0057000000000ULL, 0x0058000000000ULL, MappingDesc::ORIGIN, "origin-11"},
  125. {0x0058000000000ULL, 0x0059000000000ULL, MappingDesc::APP, "app-12"},
  126. {0x0059000000000ULL, 0x005E000000000ULL, MappingDesc::INVALID, "invalid"},
  127. {0x005E000000000ULL, 0x005F000000000ULL, MappingDesc::SHADOW, "shadow-12"},
  128. {0x005F000000000ULL, 0x0060000000000ULL, MappingDesc::ORIGIN, "origin-12"},
  129. {0x0060000000000ULL, 0x0061000000000ULL, MappingDesc::INVALID, "invalid"},
  130. {0x0061000000000ULL, 0x0062000000000ULL, MappingDesc::APP, "app-13"},
  131. {0x0062000000000ULL, 0x0067000000000ULL, MappingDesc::INVALID, "invalid"},
  132. {0x0067000000000ULL, 0x0068000000000ULL, MappingDesc::SHADOW, "shadow-13"},
  133. {0x0068000000000ULL, 0x0069000000000ULL, MappingDesc::ORIGIN, "origin-13"},
  134. {0x0069000000000ULL, 0x0AAAAA0000000ULL, MappingDesc::INVALID, "invalid"},
  135. {0x0AAAAA0000000ULL, 0x0AAAB00000000ULL, MappingDesc::APP, "app-14"},
  136. {0x0AAAB00000000ULL, 0x0AACAA0000000ULL, MappingDesc::INVALID, "invalid"},
  137. {0x0AACAA0000000ULL, 0x0AACB00000000ULL, MappingDesc::SHADOW, "shadow-14"},
  138. {0x0AACB00000000ULL, 0x0AADAA0000000ULL, MappingDesc::INVALID, "invalid"},
  139. {0x0AADAA0000000ULL, 0x0AADB00000000ULL, MappingDesc::ORIGIN, "origin-14"},
  140. {0x0AADB00000000ULL, 0x0FF9F00000000ULL, MappingDesc::INVALID, "invalid"},
  141. {0x0FF9F00000000ULL, 0x0FFA000000000ULL, MappingDesc::SHADOW, "shadow-15"},
  142. {0x0FFA000000000ULL, 0x0FFAF00000000ULL, MappingDesc::INVALID, "invalid"},
  143. {0x0FFAF00000000ULL, 0x0FFB000000000ULL, MappingDesc::ORIGIN, "origin-15"},
  144. {0x0FFB000000000ULL, 0x0FFFF00000000ULL, MappingDesc::INVALID, "invalid"},
  145. {0x0FFFF00000000ULL, 0x1000000000000ULL, MappingDesc::APP, "app-15"},
  146. };
  147. # define MEM_TO_SHADOW(mem) ((uptr)mem ^ 0x6000000000ULL)
  148. # define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x1000000000ULL)
  149. #elif SANITIZER_LINUX && SANITIZER_PPC64
  150. const MappingDesc kMemoryLayout[] = {
  151. {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "low memory"},
  152. {0x000200000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
  153. {0x080000000000ULL, 0x180200000000ULL, MappingDesc::SHADOW, "shadow"},
  154. {0x180200000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
  155. {0x1C0000000000ULL, 0x2C0200000000ULL, MappingDesc::ORIGIN, "origin"},
  156. {0x2C0200000000ULL, 0x300000000000ULL, MappingDesc::INVALID, "invalid"},
  157. {0x300000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
  158. // Various kernels use different low end ranges but we can combine them into one
  159. // big range. They also use different high end ranges but we can map them all to
  160. // one range.
  161. // Maps low and high app ranges to contiguous space with zero base:
  162. // Low: 0000 0000 0000 - 0001 ffff ffff -> 1000 0000 0000 - 1001 ffff ffff
  163. // High: 3000 0000 0000 - 3fff ffff ffff -> 0000 0000 0000 - 0fff ffff ffff
  164. // High: 4000 0000 0000 - 4fff ffff ffff -> 0000 0000 0000 - 0fff ffff ffff
  165. // High: 7000 0000 0000 - 7fff ffff ffff -> 0000 0000 0000 - 0fff ffff ffff
  166. #define LINEARIZE_MEM(mem) \
  167. (((uptr)(mem) & ~0xE00000000000ULL) ^ 0x100000000000ULL)
  168. #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x080000000000ULL)
  169. #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
  170. #elif SANITIZER_LINUX && SANITIZER_S390_64
  171. const MappingDesc kMemoryLayout[] = {
  172. {0x000000000000ULL, 0x040000000000ULL, MappingDesc::APP, "low memory"},
  173. {0x040000000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
  174. {0x080000000000ULL, 0x180000000000ULL, MappingDesc::SHADOW, "shadow"},
  175. {0x180000000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
  176. {0x1C0000000000ULL, 0x2C0000000000ULL, MappingDesc::ORIGIN, "origin"},
  177. {0x2C0000000000ULL, 0x440000000000ULL, MappingDesc::INVALID, "invalid"},
  178. {0x440000000000ULL, 0x500000000000ULL, MappingDesc::APP, "high memory"}};
  179. #define MEM_TO_SHADOW(mem) \
  180. ((((uptr)(mem)) & ~0xC00000000000ULL) + 0x080000000000ULL)
  181. #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
  182. #elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64
  183. // Low memory: main binary, MAP_32BIT mappings and modules
  184. // High memory: heap, modules and main thread stack
  185. const MappingDesc kMemoryLayout[] = {
  186. {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "low memory"},
  187. {0x010000000000ULL, 0x100000000000ULL, MappingDesc::INVALID, "invalid"},
  188. {0x100000000000ULL, 0x310000000000ULL, MappingDesc::SHADOW, "shadow"},
  189. {0x310000000000ULL, 0x380000000000ULL, MappingDesc::INVALID, "invalid"},
  190. {0x380000000000ULL, 0x590000000000ULL, MappingDesc::ORIGIN, "origin"},
  191. {0x590000000000ULL, 0x600000000000ULL, MappingDesc::INVALID, "invalid"},
  192. {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
  193. // Maps low and high app ranges to contiguous space with zero base:
  194. // Low: 0000 0000 0000 - 00ff ffff ffff -> 2000 0000 0000 - 20ff ffff ffff
  195. // High: 6000 0000 0000 - 7fff ffff ffff -> 0000 0000 0000 - 1fff ffff ffff
  196. #define LINEARIZE_MEM(mem) \
  197. (((uptr)(mem) & ~0xc00000000000ULL) ^ 0x200000000000ULL)
  198. #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x100000000000ULL)
  199. #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x280000000000)
  200. #elif SANITIZER_NETBSD || (SANITIZER_LINUX && SANITIZER_WORDSIZE == 64)
  201. #ifdef MSAN_LINUX_X86_64_OLD_MAPPING
  202. // Requires PIE binary and ASLR enabled.
  203. // Main thread stack and DSOs at 0x7f0000000000 (sometimes 0x7e0000000000).
  204. // Heap at 0x600000000000.
  205. const MappingDesc kMemoryLayout[] = {
  206. {0x000000000000ULL, 0x200000000000ULL, MappingDesc::INVALID, "invalid"},
  207. {0x200000000000ULL, 0x400000000000ULL, MappingDesc::SHADOW, "shadow"},
  208. {0x400000000000ULL, 0x600000000000ULL, MappingDesc::ORIGIN, "origin"},
  209. {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app"}};
  210. #define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x400000000000ULL)
  211. #define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x200000000000ULL)
  212. #else // MSAN_LINUX_X86_64_OLD_MAPPING
  213. // All of the following configurations are supported.
  214. // ASLR disabled: main executable and DSOs at 0x555550000000
  215. // PIE and ASLR: main executable and DSOs at 0x7f0000000000
  216. // non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
  217. // Heap at 0x700000000000.
  218. const MappingDesc kMemoryLayout[] = {
  219. {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
  220. {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
  221. {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
  222. {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
  223. {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
  224. {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
  225. {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
  226. {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
  227. {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
  228. {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
  229. {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
  230. {0x700000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
  231. #define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
  232. #define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
  233. #endif // MSAN_LINUX_X86_64_OLD_MAPPING
  234. #else
  235. #error "Unsupported platform"
  236. #endif
  237. const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
  238. #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
  239. #ifndef __clang__
  240. __attribute__((optimize("unroll-loops")))
  241. #endif
  242. inline bool addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
  243. // It is critical for performance that this loop is unrolled (because then it is
  244. // simplified into just a few constant comparisons).
  245. #ifdef __clang__
  246. #pragma unroll
  247. #endif
  248. for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
  249. if (kMemoryLayout[i].type == mapping_type &&
  250. addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
  251. return true;
  252. return false;
  253. }
  254. #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
  255. #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
  256. #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
  257. // These constants must be kept in sync with the ones in MemorySanitizer.cpp.
  258. const int kMsanParamTlsSize = 800;
  259. const int kMsanRetvalTlsSize = 800;
  260. namespace __msan {
  261. extern int msan_inited;
  262. extern bool msan_init_is_running;
  263. extern int msan_report_count;
  264. bool ProtectRange(uptr beg, uptr end);
  265. bool InitShadow(bool init_origins);
  266. char *GetProcSelfMaps();
  267. void InitializeInterceptors();
  268. void MsanAllocatorInit();
  269. void MsanDeallocate(StackTrace *stack, void *ptr);
  270. void *msan_malloc(uptr size, StackTrace *stack);
  271. void *msan_calloc(uptr nmemb, uptr size, StackTrace *stack);
  272. void *msan_realloc(void *ptr, uptr size, StackTrace *stack);
  273. void *msan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack);
  274. void *msan_valloc(uptr size, StackTrace *stack);
  275. void *msan_pvalloc(uptr size, StackTrace *stack);
  276. void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack);
  277. void *msan_memalign(uptr alignment, uptr size, StackTrace *stack);
  278. int msan_posix_memalign(void **memptr, uptr alignment, uptr size,
  279. StackTrace *stack);
  280. void InstallTrapHandler();
  281. void InstallAtExitHandler();
  282. const char *GetStackOriginDescr(u32 id, uptr *pc);
  283. void EnterSymbolizer();
  284. void ExitSymbolizer();
  285. bool IsInSymbolizer();
  286. struct SymbolizerScope {
  287. SymbolizerScope() { EnterSymbolizer(); }
  288. ~SymbolizerScope() { ExitSymbolizer(); }
  289. };
  290. void PrintWarning(uptr pc, uptr bp);
  291. void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin);
  292. // Unpoison first n function arguments.
  293. void UnpoisonParam(uptr n);
  294. void UnpoisonThreadLocalState();
  295. // Returns a "chained" origin id, pointing to the given stack trace followed by
  296. // the previous origin id.
  297. u32 ChainOrigin(u32 id, StackTrace *stack);
  298. const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1;
  299. #define GET_MALLOC_STACK_TRACE \
  300. BufferedStackTrace stack; \
  301. if (__msan_get_track_origins() && msan_inited) \
  302. stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \
  303. nullptr, common_flags()->fast_unwind_on_malloc, \
  304. common_flags()->malloc_context_size)
  305. // For platforms which support slow unwinder only, we restrict the store context
  306. // size to 1, basically only storing the current pc. We do this because the slow
  307. // unwinder which is based on libunwind is not async signal safe and causes
  308. // random freezes in forking applications as well as in signal handlers.
  309. #define GET_STORE_STACK_TRACE_PC_BP(pc, bp) \
  310. BufferedStackTrace stack; \
  311. if (__msan_get_track_origins() > 1 && msan_inited) { \
  312. int size = flags()->store_context_size; \
  313. if (!SANITIZER_CAN_FAST_UNWIND) \
  314. size = Min(size, 1); \
  315. stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_malloc, size);\
  316. }
  317. #define GET_STORE_STACK_TRACE \
  318. GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
  319. #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \
  320. BufferedStackTrace stack; \
  321. if (msan_inited) { \
  322. stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal); \
  323. }
  324. class ScopedThreadLocalStateBackup {
  325. public:
  326. ScopedThreadLocalStateBackup() { Backup(); }
  327. ~ScopedThreadLocalStateBackup() { Restore(); }
  328. void Backup();
  329. void Restore();
  330. private:
  331. u64 va_arg_overflow_size_tls;
  332. };
  333. void MsanTSDInit(void (*destructor)(void *tsd));
  334. void *MsanTSDGet();
  335. void MsanTSDSet(void *tsd);
  336. void MsanTSDDtor(void *tsd);
  337. } // namespace __msan
  338. #define MSAN_MALLOC_HOOK(ptr, size) \
  339. do { \
  340. if (&__sanitizer_malloc_hook) { \
  341. UnpoisonParam(2); \
  342. __sanitizer_malloc_hook(ptr, size); \
  343. } \
  344. RunMallocHooks(ptr, size); \
  345. } while (false)
  346. #define MSAN_FREE_HOOK(ptr) \
  347. do { \
  348. if (&__sanitizer_free_hook) { \
  349. UnpoisonParam(1); \
  350. __sanitizer_free_hook(ptr); \
  351. } \
  352. RunFreeHooks(ptr); \
  353. } while (false)
  354. #endif // MSAN_H