10b57cec5SDimitry Andric //===-- msan.h --------------------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file is a part of MemorySanitizer.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // Private MSan header.
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef MSAN_H
150b57cec5SDimitry Andric #define MSAN_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_flags.h"
180b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_internal_defs.h"
190b57cec5SDimitry Andric #include "sanitizer_common/sanitizer_stacktrace.h"
200b57cec5SDimitry Andric #include "msan_interface_internal.h"
210b57cec5SDimitry Andric #include "msan_flags.h"
220b57cec5SDimitry Andric #include "ubsan/ubsan_platform.h"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric #ifndef MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
250b57cec5SDimitry Andric # define MSAN_REPLACE_OPERATORS_NEW_AND_DELETE 1
260b57cec5SDimitry Andric #endif
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #ifndef MSAN_CONTAINS_UBSAN
290b57cec5SDimitry Andric # define MSAN_CONTAINS_UBSAN CAN_SANITIZE_UB
300b57cec5SDimitry Andric #endif
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric struct MappingDesc {
330b57cec5SDimitry Andric   uptr start;
340b57cec5SDimitry Andric   uptr end;
350b57cec5SDimitry Andric   enum Type {
360b57cec5SDimitry Andric     INVALID, APP, SHADOW, ORIGIN
370b57cec5SDimitry Andric   } type;
380b57cec5SDimitry Andric   const char *name;
390b57cec5SDimitry Andric };
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric #if SANITIZER_LINUX && defined(__mips64)
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric // MIPS64 maps:
450b57cec5SDimitry Andric // - 0x0000000000-0x0200000000: Program own segments
460b57cec5SDimitry Andric // - 0xa200000000-0xc000000000: PIE program segments
470b57cec5SDimitry Andric // - 0xe200000000-0xffffffffff: libraries segments.
480b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
490b57cec5SDimitry Andric     {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "app-1"},
500b57cec5SDimitry Andric     {0x000200000000ULL, 0x002200000000ULL, MappingDesc::INVALID, "invalid"},
510b57cec5SDimitry Andric     {0x002200000000ULL, 0x004000000000ULL, MappingDesc::SHADOW, "shadow-2"},
520b57cec5SDimitry Andric     {0x004000000000ULL, 0x004200000000ULL, MappingDesc::INVALID, "invalid"},
530b57cec5SDimitry Andric     {0x004200000000ULL, 0x006000000000ULL, MappingDesc::ORIGIN, "origin-2"},
540b57cec5SDimitry Andric     {0x006000000000ULL, 0x006200000000ULL, MappingDesc::INVALID, "invalid"},
550b57cec5SDimitry Andric     {0x006200000000ULL, 0x008000000000ULL, MappingDesc::SHADOW, "shadow-3"},
560b57cec5SDimitry Andric     {0x008000000000ULL, 0x008200000000ULL, MappingDesc::SHADOW, "shadow-1"},
570b57cec5SDimitry Andric     {0x008200000000ULL, 0x00a000000000ULL, MappingDesc::ORIGIN, "origin-3"},
580b57cec5SDimitry Andric     {0x00a000000000ULL, 0x00a200000000ULL, MappingDesc::ORIGIN, "origin-1"},
590b57cec5SDimitry Andric     {0x00a200000000ULL, 0x00c000000000ULL, MappingDesc::APP, "app-2"},
600b57cec5SDimitry Andric     {0x00c000000000ULL, 0x00e200000000ULL, MappingDesc::INVALID, "invalid"},
610b57cec5SDimitry Andric     {0x00e200000000ULL, 0x00ffffffffffULL, MappingDesc::APP, "app-3"}};
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric #define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x8000000000ULL)
640b57cec5SDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x2000000000ULL)
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric #elif SANITIZER_LINUX && defined(__aarch64__)
670b57cec5SDimitry Andric 
68bdd1243dSDimitry Andric // The mapping assumes 48-bit VMA. AArch64 maps:
69bdd1243dSDimitry Andric // - 0x0000000000000-0x0100000000000: 39/42/48-bits program own segments
70bdd1243dSDimitry Andric // - 0x0a00000000000-0x0b00000000000: 48-bits PIE program segments
71bdd1243dSDimitry Andric //   Ideally, this would extend to 0x0c00000000000 (2^45 bytes - the
72bdd1243dSDimitry Andric //   maximum ASLR region for 48-bit VMA) but it is too hard to fit in
73bdd1243dSDimitry Andric //   the larger app/shadow/origin regions.
74bdd1243dSDimitry Andric // - 0x0e00000000000-0x1000000000000: 48-bits libraries segments
750b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
76bdd1243dSDimitry Andric     {0X0000000000000, 0X0100000000000, MappingDesc::APP, "app-10-13"},
77bdd1243dSDimitry Andric     {0X0100000000000, 0X0200000000000, MappingDesc::SHADOW, "shadow-14"},
78bdd1243dSDimitry Andric     {0X0200000000000, 0X0300000000000, MappingDesc::INVALID, "invalid"},
79bdd1243dSDimitry Andric     {0X0300000000000, 0X0400000000000, MappingDesc::ORIGIN, "origin-14"},
80bdd1243dSDimitry Andric     {0X0400000000000, 0X0600000000000, MappingDesc::SHADOW, "shadow-15"},
81bdd1243dSDimitry Andric     {0X0600000000000, 0X0800000000000, MappingDesc::ORIGIN, "origin-15"},
82bdd1243dSDimitry Andric     {0X0800000000000, 0X0A00000000000, MappingDesc::INVALID, "invalid"},
83bdd1243dSDimitry Andric     {0X0A00000000000, 0X0B00000000000, MappingDesc::APP, "app-14"},
84bdd1243dSDimitry Andric     {0X0B00000000000, 0X0C00000000000, MappingDesc::SHADOW, "shadow-10-13"},
85bdd1243dSDimitry Andric     {0X0C00000000000, 0X0D00000000000, MappingDesc::INVALID, "invalid"},
86bdd1243dSDimitry Andric     {0X0D00000000000, 0X0E00000000000, MappingDesc::ORIGIN, "origin-10-13"},
87bdd1243dSDimitry Andric     {0X0E00000000000, 0X1000000000000, MappingDesc::APP, "app-15"},
880b57cec5SDimitry Andric };
89bdd1243dSDimitry Andric # define MEM_TO_SHADOW(mem) ((uptr)mem ^ 0xB00000000000ULL)
90bdd1243dSDimitry Andric # define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x200000000000ULL)
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric #elif SANITIZER_LINUX && SANITIZER_PPC64
930b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
940b57cec5SDimitry Andric     {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "low memory"},
950b57cec5SDimitry Andric     {0x000200000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
960b57cec5SDimitry Andric     {0x080000000000ULL, 0x180200000000ULL, MappingDesc::SHADOW, "shadow"},
970b57cec5SDimitry Andric     {0x180200000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
980b57cec5SDimitry Andric     {0x1C0000000000ULL, 0x2C0200000000ULL, MappingDesc::ORIGIN, "origin"},
990b57cec5SDimitry Andric     {0x2C0200000000ULL, 0x300000000000ULL, MappingDesc::INVALID, "invalid"},
1000b57cec5SDimitry Andric     {0x300000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric // Various kernels use different low end ranges but we can combine them into one
1030b57cec5SDimitry Andric // big range. They also use different high end ranges but we can map them all to
1040b57cec5SDimitry Andric // one range.
1050b57cec5SDimitry Andric // Maps low and high app ranges to contiguous space with zero base:
1060b57cec5SDimitry Andric //   Low:  0000 0000 0000 - 0001 ffff ffff  ->  1000 0000 0000 - 1001 ffff ffff
1070b57cec5SDimitry Andric //   High: 3000 0000 0000 - 3fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
1080b57cec5SDimitry Andric //   High: 4000 0000 0000 - 4fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
1090b57cec5SDimitry Andric //   High: 7000 0000 0000 - 7fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
1100b57cec5SDimitry Andric #define LINEARIZE_MEM(mem) \
1110b57cec5SDimitry Andric   (((uptr)(mem) & ~0xE00000000000ULL) ^ 0x100000000000ULL)
1120b57cec5SDimitry Andric #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x080000000000ULL)
1130b57cec5SDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
1140b57cec5SDimitry Andric 
1155ffd83dbSDimitry Andric #elif SANITIZER_LINUX && SANITIZER_S390_64
1165ffd83dbSDimitry Andric const MappingDesc kMemoryLayout[] = {
1175ffd83dbSDimitry Andric     {0x000000000000ULL, 0x040000000000ULL, MappingDesc::APP, "low memory"},
1185ffd83dbSDimitry Andric     {0x040000000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
1195ffd83dbSDimitry Andric     {0x080000000000ULL, 0x180000000000ULL, MappingDesc::SHADOW, "shadow"},
1205ffd83dbSDimitry Andric     {0x180000000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
1215ffd83dbSDimitry Andric     {0x1C0000000000ULL, 0x2C0000000000ULL, MappingDesc::ORIGIN, "origin"},
1225ffd83dbSDimitry Andric     {0x2C0000000000ULL, 0x440000000000ULL, MappingDesc::INVALID, "invalid"},
1235ffd83dbSDimitry Andric     {0x440000000000ULL, 0x500000000000ULL, MappingDesc::APP, "high memory"}};
1245ffd83dbSDimitry Andric 
1255ffd83dbSDimitry Andric #define MEM_TO_SHADOW(mem) \
1265ffd83dbSDimitry Andric   ((((uptr)(mem)) & ~0xC00000000000ULL) + 0x080000000000ULL)
1275ffd83dbSDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
1285ffd83dbSDimitry Andric 
129fcaf7f86SDimitry Andric #elif SANITIZER_FREEBSD && defined(__aarch64__)
130fcaf7f86SDimitry Andric 
131fcaf7f86SDimitry Andric // Low memory: main binary, MAP_32BIT mappings and modules
132fcaf7f86SDimitry Andric // High memory: heap, modules and main thread stack
133fcaf7f86SDimitry Andric const MappingDesc kMemoryLayout[] = {
134fcaf7f86SDimitry Andric     {0x000000000000ULL, 0x020000000000ULL, MappingDesc::APP, "low memory"},
135fcaf7f86SDimitry Andric     {0x020000000000ULL, 0x200000000000ULL, MappingDesc::INVALID, "invalid"},
136fcaf7f86SDimitry Andric     {0x200000000000ULL, 0x620000000000ULL, MappingDesc::SHADOW, "shadow"},
137fcaf7f86SDimitry Andric     {0x620000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
138fcaf7f86SDimitry Andric     {0x700000000000ULL, 0xb20000000000ULL, MappingDesc::ORIGIN, "origin"},
139fcaf7f86SDimitry Andric     {0xb20000000000ULL, 0xc00000000000ULL, MappingDesc::INVALID, "invalid"},
140fcaf7f86SDimitry Andric     {0xc00000000000ULL, 0x1000000000000ULL, MappingDesc::APP, "high memory"}};
141fcaf7f86SDimitry Andric 
142fcaf7f86SDimitry Andric // Maps low and high app ranges to contiguous space with zero base:
143fcaf7f86SDimitry Andric //   Low:  0000 0000 0000 - 01ff ffff ffff -> 4000 0000 0000 - 41ff ffff ffff
144fcaf7f86SDimitry Andric //   High: c000 0000 0000 - ffff ffff ffff -> 0000 0000 0000 - 3fff ffff ffff
145fcaf7f86SDimitry Andric #define LINEARIZE_MEM(mem) \
146fcaf7f86SDimitry Andric   (((uptr)(mem) & ~0x1800000000000ULL) ^ 0x400000000000ULL)
147fcaf7f86SDimitry Andric #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x200000000000ULL)
148fcaf7f86SDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x500000000000)
149fcaf7f86SDimitry Andric 
1500b57cec5SDimitry Andric #elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric // Low memory: main binary, MAP_32BIT mappings and modules
1530b57cec5SDimitry Andric // High memory: heap, modules and main thread stack
1540b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
1550b57cec5SDimitry Andric     {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "low memory"},
1560b57cec5SDimitry Andric     {0x010000000000ULL, 0x100000000000ULL, MappingDesc::INVALID, "invalid"},
1570b57cec5SDimitry Andric     {0x100000000000ULL, 0x310000000000ULL, MappingDesc::SHADOW, "shadow"},
1580b57cec5SDimitry Andric     {0x310000000000ULL, 0x380000000000ULL, MappingDesc::INVALID, "invalid"},
1590b57cec5SDimitry Andric     {0x380000000000ULL, 0x590000000000ULL, MappingDesc::ORIGIN, "origin"},
1600b57cec5SDimitry Andric     {0x590000000000ULL, 0x600000000000ULL, MappingDesc::INVALID, "invalid"},
1610b57cec5SDimitry Andric     {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric // Maps low and high app ranges to contiguous space with zero base:
1640b57cec5SDimitry Andric //   Low:  0000 0000 0000 - 00ff ffff ffff  ->  2000 0000 0000 - 20ff ffff ffff
1650b57cec5SDimitry Andric //   High: 6000 0000 0000 - 7fff ffff ffff  ->  0000 0000 0000 - 1fff ffff ffff
1660b57cec5SDimitry Andric #define LINEARIZE_MEM(mem) \
1670b57cec5SDimitry Andric   (((uptr)(mem) & ~0xc00000000000ULL) ^ 0x200000000000ULL)
1680b57cec5SDimitry Andric #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x100000000000ULL)
1690b57cec5SDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x280000000000)
1700b57cec5SDimitry Andric 
1710b57cec5SDimitry Andric #elif SANITIZER_NETBSD || (SANITIZER_LINUX && SANITIZER_WORDSIZE == 64)
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric // All of the following configurations are supported.
1740b57cec5SDimitry Andric // ASLR disabled: main executable and DSOs at 0x555550000000
1750b57cec5SDimitry Andric // PIE and ASLR: main executable and DSOs at 0x7f0000000000
1760b57cec5SDimitry Andric // non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
1770b57cec5SDimitry Andric // Heap at 0x700000000000.
1780b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
1790b57cec5SDimitry Andric     {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
1800b57cec5SDimitry Andric     {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
1810b57cec5SDimitry Andric     {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
1820b57cec5SDimitry Andric     {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
1830b57cec5SDimitry Andric     {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
1840b57cec5SDimitry Andric     {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
1850b57cec5SDimitry Andric     {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
1860b57cec5SDimitry Andric     {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
1870b57cec5SDimitry Andric     {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
1880b57cec5SDimitry Andric     {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
1890b57cec5SDimitry Andric     {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
1900b57cec5SDimitry Andric     {0x700000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
1910b57cec5SDimitry Andric #define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
1920b57cec5SDimitry Andric #define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric #else
1950b57cec5SDimitry Andric #error "Unsupported platform"
1960b57cec5SDimitry Andric #endif
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric #ifndef __clang__
2030b57cec5SDimitry Andric __attribute__((optimize("unroll-loops")))
2040b57cec5SDimitry Andric #endif
2050b57cec5SDimitry Andric inline bool addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
2060b57cec5SDimitry Andric // It is critical for performance that this loop is unrolled (because then it is
2070b57cec5SDimitry Andric // simplified into just a few constant comparisons).
2080b57cec5SDimitry Andric #ifdef __clang__
2090b57cec5SDimitry Andric #pragma unroll
2100b57cec5SDimitry Andric #endif
2110b57cec5SDimitry Andric   for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
2120b57cec5SDimitry Andric     if (kMemoryLayout[i].type == mapping_type &&
2130b57cec5SDimitry Andric         addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
2140b57cec5SDimitry Andric       return true;
2150b57cec5SDimitry Andric   return false;
2160b57cec5SDimitry Andric }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
2190b57cec5SDimitry Andric #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
2200b57cec5SDimitry Andric #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
2210b57cec5SDimitry Andric 
22268d75effSDimitry Andric // These constants must be kept in sync with the ones in MemorySanitizer.cpp.
2230b57cec5SDimitry Andric const int kMsanParamTlsSize = 800;
2240b57cec5SDimitry Andric const int kMsanRetvalTlsSize = 800;
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric namespace __msan {
2270b57cec5SDimitry Andric extern int msan_inited;
2280b57cec5SDimitry Andric extern bool msan_init_is_running;
2290b57cec5SDimitry Andric extern int msan_report_count;
2300b57cec5SDimitry Andric 
2310b57cec5SDimitry Andric bool ProtectRange(uptr beg, uptr end);
2320b57cec5SDimitry Andric bool InitShadow(bool init_origins);
2330b57cec5SDimitry Andric char *GetProcSelfMaps();
2340b57cec5SDimitry Andric void InitializeInterceptors();
2350b57cec5SDimitry Andric 
2360b57cec5SDimitry Andric void MsanAllocatorInit();
2370b57cec5SDimitry Andric void MsanDeallocate(StackTrace *stack, void *ptr);
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric void *msan_malloc(uptr size, StackTrace *stack);
2400b57cec5SDimitry Andric void *msan_calloc(uptr nmemb, uptr size, StackTrace *stack);
2410b57cec5SDimitry Andric void *msan_realloc(void *ptr, uptr size, StackTrace *stack);
2420b57cec5SDimitry Andric void *msan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack);
2430b57cec5SDimitry Andric void *msan_valloc(uptr size, StackTrace *stack);
2440b57cec5SDimitry Andric void *msan_pvalloc(uptr size, StackTrace *stack);
2450b57cec5SDimitry Andric void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack);
2460b57cec5SDimitry Andric void *msan_memalign(uptr alignment, uptr size, StackTrace *stack);
2470b57cec5SDimitry Andric int msan_posix_memalign(void **memptr, uptr alignment, uptr size,
2480b57cec5SDimitry Andric                         StackTrace *stack);
2490b57cec5SDimitry Andric 
2500b57cec5SDimitry Andric void InstallTrapHandler();
2510b57cec5SDimitry Andric void InstallAtExitHandler();
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric const char *GetStackOriginDescr(u32 id, uptr *pc);
2540b57cec5SDimitry Andric 
25581ad6265SDimitry Andric bool IsInSymbolizerOrUnwider();
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric void PrintWarning(uptr pc, uptr bp);
2580b57cec5SDimitry Andric void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin);
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric // Unpoison first n function arguments.
2610b57cec5SDimitry Andric void UnpoisonParam(uptr n);
2620b57cec5SDimitry Andric void UnpoisonThreadLocalState();
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric // Returns a "chained" origin id, pointing to the given stack trace followed by
2650b57cec5SDimitry Andric // the previous origin id.
2660b57cec5SDimitry Andric u32 ChainOrigin(u32 id, StackTrace *stack);
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1;
269bdd1243dSDimitry Andric const int STACK_TRACE_TAG_FIELDS = STACK_TRACE_TAG_POISON + 1;
270bdd1243dSDimitry Andric const int STACK_TRACE_TAG_VPTR = STACK_TRACE_TAG_FIELDS + 1;
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric #define GET_MALLOC_STACK_TRACE                                            \
2730b57cec5SDimitry Andric   BufferedStackTrace stack;                                               \
2740b57cec5SDimitry Andric   if (__msan_get_track_origins() && msan_inited)                          \
2750b57cec5SDimitry Andric     stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(),         \
2760b57cec5SDimitry Andric                  nullptr, common_flags()->fast_unwind_on_malloc,          \
2770b57cec5SDimitry Andric                  common_flags()->malloc_context_size)
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric // For platforms which support slow unwinder only, we restrict the store context
2800b57cec5SDimitry Andric // size to 1, basically only storing the current pc. We do this because the slow
2810b57cec5SDimitry Andric // unwinder which is based on libunwind is not async signal safe and causes
2820b57cec5SDimitry Andric // random freezes in forking applications as well as in signal handlers.
2830b57cec5SDimitry Andric #define GET_STORE_STACK_TRACE_PC_BP(pc, bp)                                    \
2840b57cec5SDimitry Andric   BufferedStackTrace stack;                                                    \
2850b57cec5SDimitry Andric   if (__msan_get_track_origins() > 1 && msan_inited) {                         \
2860b57cec5SDimitry Andric     int size = flags()->store_context_size;                                    \
2870b57cec5SDimitry Andric     if (!SANITIZER_CAN_FAST_UNWIND)                                            \
2880b57cec5SDimitry Andric       size = Min(size, 1);                                                     \
2890b57cec5SDimitry Andric     stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_malloc, size);\
2900b57cec5SDimitry Andric   }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric #define GET_STORE_STACK_TRACE \
2930b57cec5SDimitry Andric   GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp)                              \
2960b57cec5SDimitry Andric   BufferedStackTrace stack;                                              \
29768d75effSDimitry Andric   if (msan_inited) {                                                     \
29868d75effSDimitry Andric     stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal); \
29968d75effSDimitry Andric   }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric class ScopedThreadLocalStateBackup {
3020b57cec5SDimitry Andric  public:
3030b57cec5SDimitry Andric   ScopedThreadLocalStateBackup() { Backup(); }
3040b57cec5SDimitry Andric   ~ScopedThreadLocalStateBackup() { Restore(); }
3050b57cec5SDimitry Andric   void Backup();
3060b57cec5SDimitry Andric   void Restore();
3070b57cec5SDimitry Andric  private:
3080b57cec5SDimitry Andric   u64 va_arg_overflow_size_tls;
3090b57cec5SDimitry Andric };
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric void MsanTSDInit(void (*destructor)(void *tsd));
3120b57cec5SDimitry Andric void *MsanTSDGet();
3130b57cec5SDimitry Andric void MsanTSDSet(void *tsd);
3140b57cec5SDimitry Andric void MsanTSDDtor(void *tsd);
3150b57cec5SDimitry Andric 
3160b57cec5SDimitry Andric }  // namespace __msan
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric #endif  // MSAN_H
319