msan.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 assumes 48-bit VMA. AArch64 maps:
  58. // - 0x0000000000000-0x0100000000000: 39/42/48-bits program own segments
  59. // - 0x0a00000000000-0x0b00000000000: 48-bits PIE program segments
  60. // Ideally, this would extend to 0x0c00000000000 (2^45 bytes - the
  61. // maximum ASLR region for 48-bit VMA) but it is too hard to fit in
  62. // the larger app/shadow/origin regions.
  63. // - 0x0e00000000000-0x1000000000000: 48-bits libraries segments
  64. const MappingDesc kMemoryLayout[] = {
  65. {0X0000000000000, 0X0100000000000, MappingDesc::APP, "app-10-13"},
  66. {0X0100000000000, 0X0200000000000, MappingDesc::SHADOW, "shadow-14"},
  67. {0X0200000000000, 0X0300000000000, MappingDesc::INVALID, "invalid"},
  68. {0X0300000000000, 0X0400000000000, MappingDesc::ORIGIN, "origin-14"},
  69. {0X0400000000000, 0X0600000000000, MappingDesc::SHADOW, "shadow-15"},
  70. {0X0600000000000, 0X0800000000000, MappingDesc::ORIGIN, "origin-15"},
  71. {0X0800000000000, 0X0A00000000000, MappingDesc::INVALID, "invalid"},
  72. {0X0A00000000000, 0X0B00000000000, MappingDesc::APP, "app-14"},
  73. {0X0B00000000000, 0X0C00000000000, MappingDesc::SHADOW, "shadow-10-13"},
  74. {0X0C00000000000, 0X0D00000000000, MappingDesc::INVALID, "invalid"},
  75. {0X0D00000000000, 0X0E00000000000, MappingDesc::ORIGIN, "origin-10-13"},
  76. {0X0E00000000000, 0X1000000000000, MappingDesc::APP, "app-15"},
  77. };
  78. # define MEM_TO_SHADOW(mem) ((uptr)mem ^ 0xB00000000000ULL)
  79. # define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x200000000000ULL)
  80. #elif SANITIZER_LINUX && SANITIZER_PPC64
  81. const MappingDesc kMemoryLayout[] = {
  82. {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "low memory"},
  83. {0x000200000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
  84. {0x080000000000ULL, 0x180200000000ULL, MappingDesc::SHADOW, "shadow"},
  85. {0x180200000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
  86. {0x1C0000000000ULL, 0x2C0200000000ULL, MappingDesc::ORIGIN, "origin"},
  87. {0x2C0200000000ULL, 0x300000000000ULL, MappingDesc::INVALID, "invalid"},
  88. {0x300000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
  89. // Various kernels use different low end ranges but we can combine them into one
  90. // big range. They also use different high end ranges but we can map them all to
  91. // one range.
  92. // Maps low and high app ranges to contiguous space with zero base:
  93. // Low: 0000 0000 0000 - 0001 ffff ffff -> 1000 0000 0000 - 1001 ffff ffff
  94. // High: 3000 0000 0000 - 3fff ffff ffff -> 0000 0000 0000 - 0fff ffff ffff
  95. // High: 4000 0000 0000 - 4fff ffff ffff -> 0000 0000 0000 - 0fff ffff ffff
  96. // High: 7000 0000 0000 - 7fff ffff ffff -> 0000 0000 0000 - 0fff ffff ffff
  97. #define LINEARIZE_MEM(mem) \
  98. (((uptr)(mem) & ~0xE00000000000ULL) ^ 0x100000000000ULL)
  99. #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x080000000000ULL)
  100. #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
  101. #elif SANITIZER_LINUX && SANITIZER_S390_64
  102. const MappingDesc kMemoryLayout[] = {
  103. {0x000000000000ULL, 0x040000000000ULL, MappingDesc::APP, "low memory"},
  104. {0x040000000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
  105. {0x080000000000ULL, 0x180000000000ULL, MappingDesc::SHADOW, "shadow"},
  106. {0x180000000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
  107. {0x1C0000000000ULL, 0x2C0000000000ULL, MappingDesc::ORIGIN, "origin"},
  108. {0x2C0000000000ULL, 0x440000000000ULL, MappingDesc::INVALID, "invalid"},
  109. {0x440000000000ULL, 0x500000000000ULL, MappingDesc::APP, "high memory"}};
  110. #define MEM_TO_SHADOW(mem) \
  111. ((((uptr)(mem)) & ~0xC00000000000ULL) + 0x080000000000ULL)
  112. #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
  113. #elif SANITIZER_FREEBSD && defined(__aarch64__)
  114. // Low memory: main binary, MAP_32BIT mappings and modules
  115. // High memory: heap, modules and main thread stack
  116. const MappingDesc kMemoryLayout[] = {
  117. {0x000000000000ULL, 0x020000000000ULL, MappingDesc::APP, "low memory"},
  118. {0x020000000000ULL, 0x200000000000ULL, MappingDesc::INVALID, "invalid"},
  119. {0x200000000000ULL, 0x620000000000ULL, MappingDesc::SHADOW, "shadow"},
  120. {0x620000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
  121. {0x700000000000ULL, 0xb20000000000ULL, MappingDesc::ORIGIN, "origin"},
  122. {0xb20000000000ULL, 0xc00000000000ULL, MappingDesc::INVALID, "invalid"},
  123. {0xc00000000000ULL, 0x1000000000000ULL, MappingDesc::APP, "high memory"}};
  124. // Maps low and high app ranges to contiguous space with zero base:
  125. // Low: 0000 0000 0000 - 01ff ffff ffff -> 4000 0000 0000 - 41ff ffff ffff
  126. // High: c000 0000 0000 - ffff ffff ffff -> 0000 0000 0000 - 3fff ffff ffff
  127. #define LINEARIZE_MEM(mem) \
  128. (((uptr)(mem) & ~0x1800000000000ULL) ^ 0x400000000000ULL)
  129. #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x200000000000ULL)
  130. #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x500000000000)
  131. #elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64
  132. // Low memory: main binary, MAP_32BIT mappings and modules
  133. // High memory: heap, modules and main thread stack
  134. const MappingDesc kMemoryLayout[] = {
  135. {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "low memory"},
  136. {0x010000000000ULL, 0x100000000000ULL, MappingDesc::INVALID, "invalid"},
  137. {0x100000000000ULL, 0x310000000000ULL, MappingDesc::SHADOW, "shadow"},
  138. {0x310000000000ULL, 0x380000000000ULL, MappingDesc::INVALID, "invalid"},
  139. {0x380000000000ULL, 0x590000000000ULL, MappingDesc::ORIGIN, "origin"},
  140. {0x590000000000ULL, 0x600000000000ULL, MappingDesc::INVALID, "invalid"},
  141. {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
  142. // Maps low and high app ranges to contiguous space with zero base:
  143. // Low: 0000 0000 0000 - 00ff ffff ffff -> 2000 0000 0000 - 20ff ffff ffff
  144. // High: 6000 0000 0000 - 7fff ffff ffff -> 0000 0000 0000 - 1fff ffff ffff
  145. #define LINEARIZE_MEM(mem) \
  146. (((uptr)(mem) & ~0xc00000000000ULL) ^ 0x200000000000ULL)
  147. #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x100000000000ULL)
  148. #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x280000000000)
  149. #elif SANITIZER_NETBSD || (SANITIZER_LINUX && SANITIZER_WORDSIZE == 64)
  150. // All of the following configurations are supported.
  151. // ASLR disabled: main executable and DSOs at 0x555550000000
  152. // PIE and ASLR: main executable and DSOs at 0x7f0000000000
  153. // non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
  154. // Heap at 0x700000000000.
  155. const MappingDesc kMemoryLayout[] = {
  156. {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
  157. {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
  158. {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
  159. {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
  160. {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
  161. {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
  162. {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
  163. {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
  164. {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
  165. {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
  166. {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
  167. {0x700000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
  168. #define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
  169. #define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
  170. #else
  171. #error "Unsupported platform"
  172. #endif
  173. const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
  174. #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
  175. #ifndef __clang__
  176. __attribute__((optimize("unroll-loops")))
  177. #endif
  178. inline bool addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
  179. // It is critical for performance that this loop is unrolled (because then it is
  180. // simplified into just a few constant comparisons).
  181. #ifdef __clang__
  182. #pragma unroll
  183. #endif
  184. for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
  185. if (kMemoryLayout[i].type == mapping_type &&
  186. addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
  187. return true;
  188. return false;
  189. }
  190. #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
  191. #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
  192. #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
  193. // These constants must be kept in sync with the ones in MemorySanitizer.cpp.
  194. const int kMsanParamTlsSize = 800;
  195. const int kMsanRetvalTlsSize = 800;
  196. namespace __msan {
  197. extern int msan_inited;
  198. extern bool msan_init_is_running;
  199. extern int msan_report_count;
  200. bool ProtectRange(uptr beg, uptr end);
  201. bool InitShadow(bool init_origins);
  202. char *GetProcSelfMaps();
  203. void InitializeInterceptors();
  204. void MsanAllocatorInit();
  205. void MsanDeallocate(StackTrace *stack, void *ptr);
  206. void *msan_malloc(uptr size, StackTrace *stack);
  207. void *msan_calloc(uptr nmemb, uptr size, StackTrace *stack);
  208. void *msan_realloc(void *ptr, uptr size, StackTrace *stack);
  209. void *msan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack);
  210. void *msan_valloc(uptr size, StackTrace *stack);
  211. void *msan_pvalloc(uptr size, StackTrace *stack);
  212. void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack);
  213. void *msan_memalign(uptr alignment, uptr size, StackTrace *stack);
  214. int msan_posix_memalign(void **memptr, uptr alignment, uptr size,
  215. StackTrace *stack);
  216. void InstallTrapHandler();
  217. void InstallAtExitHandler();
  218. const char *GetStackOriginDescr(u32 id, uptr *pc);
  219. bool IsInSymbolizerOrUnwider();
  220. void PrintWarning(uptr pc, uptr bp);
  221. void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin);
  222. // Unpoison first n function arguments.
  223. void UnpoisonParam(uptr n);
  224. void UnpoisonThreadLocalState();
  225. // Returns a "chained" origin id, pointing to the given stack trace followed by
  226. // the previous origin id.
  227. u32 ChainOrigin(u32 id, StackTrace *stack);
  228. const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1;
  229. const int STACK_TRACE_TAG_FIELDS = STACK_TRACE_TAG_POISON + 1;
  230. const int STACK_TRACE_TAG_VPTR = STACK_TRACE_TAG_FIELDS + 1;
  231. #define GET_MALLOC_STACK_TRACE \
  232. BufferedStackTrace stack; \
  233. if (__msan_get_track_origins() && msan_inited) \
  234. stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \
  235. nullptr, common_flags()->fast_unwind_on_malloc, \
  236. common_flags()->malloc_context_size)
  237. // For platforms which support slow unwinder only, we restrict the store context
  238. // size to 1, basically only storing the current pc. We do this because the slow
  239. // unwinder which is based on libunwind is not async signal safe and causes
  240. // random freezes in forking applications as well as in signal handlers.
  241. #define GET_STORE_STACK_TRACE_PC_BP(pc, bp) \
  242. BufferedStackTrace stack; \
  243. if (__msan_get_track_origins() > 1 && msan_inited) { \
  244. int size = flags()->store_context_size; \
  245. if (!SANITIZER_CAN_FAST_UNWIND) \
  246. size = Min(size, 1); \
  247. stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_malloc, size);\
  248. }
  249. #define GET_STORE_STACK_TRACE \
  250. GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
  251. #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \
  252. BufferedStackTrace stack; \
  253. if (msan_inited) { \
  254. stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal); \
  255. }
  256. class ScopedThreadLocalStateBackup {
  257. public:
  258. ScopedThreadLocalStateBackup() { Backup(); }
  259. ~ScopedThreadLocalStateBackup() { Restore(); }
  260. void Backup();
  261. void Restore();
  262. private:
  263. u64 va_arg_overflow_size_tls;
  264. };
  265. void MsanTSDInit(void (*destructor)(void *tsd));
  266. void *MsanTSDGet();
  267. void MsanTSDSet(void *tsd);
  268. void MsanTSDDtor(void *tsd);
  269. } // namespace __msan
  270. #endif // MSAN_H