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 {
36439352acSDimitry Andric     INVALID = 1,
37439352acSDimitry Andric     ALLOCATOR = 2,
38439352acSDimitry Andric     APP = 4,
39439352acSDimitry Andric     SHADOW = 8,
40439352acSDimitry Andric     ORIGIN = 16,
410b57cec5SDimitry Andric   } type;
420b57cec5SDimitry Andric   const char *name;
430b57cec5SDimitry Andric };
440b57cec5SDimitry Andric 
45439352acSDimitry Andric // Note: MappingDesc::ALLOCATOR entries are only used to check for memory
46439352acSDimitry Andric // layout compatibility. The actual allocation settings are in
47439352acSDimitry Andric // msan_allocator.cpp, which need to be kept in sync.
480b57cec5SDimitry Andric #if SANITIZER_LINUX && defined(__mips64)
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric // MIPS64 maps:
510b57cec5SDimitry Andric // - 0x0000000000-0x0200000000: Program own segments
520b57cec5SDimitry Andric // - 0xa200000000-0xc000000000: PIE program segments
530b57cec5SDimitry Andric // - 0xe200000000-0xffffffffff: libraries segments.
540b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
550b57cec5SDimitry Andric     {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "app-1"},
560b57cec5SDimitry Andric     {0x000200000000ULL, 0x002200000000ULL, MappingDesc::INVALID, "invalid"},
570b57cec5SDimitry Andric     {0x002200000000ULL, 0x004000000000ULL, MappingDesc::SHADOW, "shadow-2"},
580b57cec5SDimitry Andric     {0x004000000000ULL, 0x004200000000ULL, MappingDesc::INVALID, "invalid"},
590b57cec5SDimitry Andric     {0x004200000000ULL, 0x006000000000ULL, MappingDesc::ORIGIN, "origin-2"},
600b57cec5SDimitry Andric     {0x006000000000ULL, 0x006200000000ULL, MappingDesc::INVALID, "invalid"},
610b57cec5SDimitry Andric     {0x006200000000ULL, 0x008000000000ULL, MappingDesc::SHADOW, "shadow-3"},
620b57cec5SDimitry Andric     {0x008000000000ULL, 0x008200000000ULL, MappingDesc::SHADOW, "shadow-1"},
630b57cec5SDimitry Andric     {0x008200000000ULL, 0x00a000000000ULL, MappingDesc::ORIGIN, "origin-3"},
640b57cec5SDimitry Andric     {0x00a000000000ULL, 0x00a200000000ULL, MappingDesc::ORIGIN, "origin-1"},
650b57cec5SDimitry Andric     {0x00a200000000ULL, 0x00c000000000ULL, MappingDesc::APP, "app-2"},
660b57cec5SDimitry Andric     {0x00c000000000ULL, 0x00e200000000ULL, MappingDesc::INVALID, "invalid"},
670b57cec5SDimitry Andric     {0x00e200000000ULL, 0x00ffffffffffULL, MappingDesc::APP, "app-3"}};
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric #define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x8000000000ULL)
700b57cec5SDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x2000000000ULL)
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric #elif SANITIZER_LINUX && defined(__aarch64__)
730b57cec5SDimitry Andric 
74bdd1243dSDimitry Andric // The mapping assumes 48-bit VMA. AArch64 maps:
75bdd1243dSDimitry Andric // - 0x0000000000000-0x0100000000000: 39/42/48-bits program own segments
76bdd1243dSDimitry Andric // - 0x0a00000000000-0x0b00000000000: 48-bits PIE program segments
77bdd1243dSDimitry Andric //   Ideally, this would extend to 0x0c00000000000 (2^45 bytes - the
78bdd1243dSDimitry Andric //   maximum ASLR region for 48-bit VMA) but it is too hard to fit in
79bdd1243dSDimitry Andric //   the larger app/shadow/origin regions.
80bdd1243dSDimitry Andric // - 0x0e00000000000-0x1000000000000: 48-bits libraries segments
810b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
82bdd1243dSDimitry Andric     {0X0000000000000, 0X0100000000000, MappingDesc::APP, "app-10-13"},
83bdd1243dSDimitry Andric     {0X0100000000000, 0X0200000000000, MappingDesc::SHADOW, "shadow-14"},
84bdd1243dSDimitry Andric     {0X0200000000000, 0X0300000000000, MappingDesc::INVALID, "invalid"},
85bdd1243dSDimitry Andric     {0X0300000000000, 0X0400000000000, MappingDesc::ORIGIN, "origin-14"},
86bdd1243dSDimitry Andric     {0X0400000000000, 0X0600000000000, MappingDesc::SHADOW, "shadow-15"},
87bdd1243dSDimitry Andric     {0X0600000000000, 0X0800000000000, MappingDesc::ORIGIN, "origin-15"},
88bdd1243dSDimitry Andric     {0X0800000000000, 0X0A00000000000, MappingDesc::INVALID, "invalid"},
89bdd1243dSDimitry Andric     {0X0A00000000000, 0X0B00000000000, MappingDesc::APP, "app-14"},
90bdd1243dSDimitry Andric     {0X0B00000000000, 0X0C00000000000, MappingDesc::SHADOW, "shadow-10-13"},
91bdd1243dSDimitry Andric     {0X0C00000000000, 0X0D00000000000, MappingDesc::INVALID, "invalid"},
92bdd1243dSDimitry Andric     {0X0D00000000000, 0X0E00000000000, MappingDesc::ORIGIN, "origin-10-13"},
93439352acSDimitry Andric     {0x0E00000000000, 0x0E40000000000, MappingDesc::ALLOCATOR, "allocator"},
94439352acSDimitry Andric     {0X0E40000000000, 0X1000000000000, MappingDesc::APP, "app-15"},
950b57cec5SDimitry Andric };
96bdd1243dSDimitry Andric # define MEM_TO_SHADOW(mem) ((uptr)mem ^ 0xB00000000000ULL)
97bdd1243dSDimitry Andric # define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x200000000000ULL)
980b57cec5SDimitry Andric 
9906c3fb27SDimitry Andric #elif SANITIZER_LINUX && SANITIZER_LOONGARCH64
10006c3fb27SDimitry Andric // LoongArch64 maps:
10106c3fb27SDimitry Andric // - 0x000000000000-0x010000000000: Program own segments
10206c3fb27SDimitry Andric // - 0x555500000000-0x555600000000: PIE program segments
10306c3fb27SDimitry Andric // - 0x7fff00000000-0x7fffffffffff: libraries segments.
10406c3fb27SDimitry Andric const MappingDesc kMemoryLayout[] = {
10506c3fb27SDimitry Andric     {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
10606c3fb27SDimitry Andric     {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
10706c3fb27SDimitry Andric     {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
10806c3fb27SDimitry Andric     {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
10906c3fb27SDimitry Andric     {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
11006c3fb27SDimitry Andric     {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
11106c3fb27SDimitry Andric     {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
11206c3fb27SDimitry Andric     {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
11306c3fb27SDimitry Andric     {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
11406c3fb27SDimitry Andric     {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
11506c3fb27SDimitry Andric     {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
116439352acSDimitry Andric     {0x700000000000ULL, 0x740000000000ULL, MappingDesc::ALLOCATOR, "allocator"},
117439352acSDimitry Andric     {0x740000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
11806c3fb27SDimitry Andric #  define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
11906c3fb27SDimitry Andric #  define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x100000000000ULL)
12006c3fb27SDimitry Andric 
1210b57cec5SDimitry Andric #elif SANITIZER_LINUX && SANITIZER_PPC64
1220b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
1230b57cec5SDimitry Andric     {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "low memory"},
1240b57cec5SDimitry Andric     {0x000200000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
1250b57cec5SDimitry Andric     {0x080000000000ULL, 0x180200000000ULL, MappingDesc::SHADOW, "shadow"},
1260b57cec5SDimitry Andric     {0x180200000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
1270b57cec5SDimitry Andric     {0x1C0000000000ULL, 0x2C0200000000ULL, MappingDesc::ORIGIN, "origin"},
1280b57cec5SDimitry Andric     {0x2C0200000000ULL, 0x300000000000ULL, MappingDesc::INVALID, "invalid"},
129439352acSDimitry Andric     {0x300000000000ULL, 0x320000000000ULL, MappingDesc::ALLOCATOR, "allocator"},
130439352acSDimitry Andric     {0x320000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric // Various kernels use different low end ranges but we can combine them into one
1330b57cec5SDimitry Andric // big range. They also use different high end ranges but we can map them all to
1340b57cec5SDimitry Andric // one range.
1350b57cec5SDimitry Andric // Maps low and high app ranges to contiguous space with zero base:
1360b57cec5SDimitry Andric //   Low:  0000 0000 0000 - 0001 ffff ffff  ->  1000 0000 0000 - 1001 ffff ffff
1370b57cec5SDimitry Andric //   High: 3000 0000 0000 - 3fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
1380b57cec5SDimitry Andric //   High: 4000 0000 0000 - 4fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
1390b57cec5SDimitry Andric //   High: 7000 0000 0000 - 7fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
1400b57cec5SDimitry Andric #define LINEARIZE_MEM(mem) \
1410b57cec5SDimitry Andric   (((uptr)(mem) & ~0xE00000000000ULL) ^ 0x100000000000ULL)
1420b57cec5SDimitry Andric #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x080000000000ULL)
1430b57cec5SDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
1440b57cec5SDimitry Andric 
1455ffd83dbSDimitry Andric #elif SANITIZER_LINUX && SANITIZER_S390_64
1465ffd83dbSDimitry Andric const MappingDesc kMemoryLayout[] = {
1475ffd83dbSDimitry Andric     {0x000000000000ULL, 0x040000000000ULL, MappingDesc::APP, "low memory"},
1485ffd83dbSDimitry Andric     {0x040000000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
1495ffd83dbSDimitry Andric     {0x080000000000ULL, 0x180000000000ULL, MappingDesc::SHADOW, "shadow"},
1505ffd83dbSDimitry Andric     {0x180000000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
1515ffd83dbSDimitry Andric     {0x1C0000000000ULL, 0x2C0000000000ULL, MappingDesc::ORIGIN, "origin"},
1525ffd83dbSDimitry Andric     {0x2C0000000000ULL, 0x440000000000ULL, MappingDesc::INVALID, "invalid"},
153439352acSDimitry Andric     {0x440000000000ULL, 0x460000000000ULL, MappingDesc::ALLOCATOR, "allocator"},
154439352acSDimitry Andric     {0x460000000000ULL, 0x500000000000ULL, MappingDesc::APP, "high memory"}};
1555ffd83dbSDimitry Andric 
1565ffd83dbSDimitry Andric #define MEM_TO_SHADOW(mem) \
1575ffd83dbSDimitry Andric   ((((uptr)(mem)) & ~0xC00000000000ULL) + 0x080000000000ULL)
1585ffd83dbSDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
1595ffd83dbSDimitry Andric 
160fcaf7f86SDimitry Andric #elif SANITIZER_FREEBSD && defined(__aarch64__)
161fcaf7f86SDimitry Andric 
162fcaf7f86SDimitry Andric // Low memory: main binary, MAP_32BIT mappings and modules
163fcaf7f86SDimitry Andric // High memory: heap, modules and main thread stack
164fcaf7f86SDimitry Andric const MappingDesc kMemoryLayout[] = {
165fcaf7f86SDimitry Andric     {0x000000000000ULL, 0x020000000000ULL, MappingDesc::APP, "low memory"},
166fcaf7f86SDimitry Andric     {0x020000000000ULL, 0x200000000000ULL, MappingDesc::INVALID, "invalid"},
167fcaf7f86SDimitry Andric     {0x200000000000ULL, 0x620000000000ULL, MappingDesc::SHADOW, "shadow"},
168fcaf7f86SDimitry Andric     {0x620000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
169fcaf7f86SDimitry Andric     {0x700000000000ULL, 0xb20000000000ULL, MappingDesc::ORIGIN, "origin"},
170fcaf7f86SDimitry Andric     {0xb20000000000ULL, 0xc00000000000ULL, MappingDesc::INVALID, "invalid"},
171fcaf7f86SDimitry Andric     {0xc00000000000ULL, 0x1000000000000ULL, MappingDesc::APP, "high memory"}};
172fcaf7f86SDimitry Andric 
173fcaf7f86SDimitry Andric // Maps low and high app ranges to contiguous space with zero base:
174fcaf7f86SDimitry Andric //   Low:  0000 0000 0000 - 01ff ffff ffff -> 4000 0000 0000 - 41ff ffff ffff
175fcaf7f86SDimitry Andric //   High: c000 0000 0000 - ffff ffff ffff -> 0000 0000 0000 - 3fff ffff ffff
176fcaf7f86SDimitry Andric #define LINEARIZE_MEM(mem) \
177fcaf7f86SDimitry Andric   (((uptr)(mem) & ~0x1800000000000ULL) ^ 0x400000000000ULL)
178fcaf7f86SDimitry Andric #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x200000000000ULL)
179fcaf7f86SDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x500000000000)
180fcaf7f86SDimitry Andric 
1810b57cec5SDimitry Andric #elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric // Low memory: main binary, MAP_32BIT mappings and modules
1840b57cec5SDimitry Andric // High memory: heap, modules and main thread stack
1850b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
1860b57cec5SDimitry Andric     {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "low memory"},
1870b57cec5SDimitry Andric     {0x010000000000ULL, 0x100000000000ULL, MappingDesc::INVALID, "invalid"},
1880b57cec5SDimitry Andric     {0x100000000000ULL, 0x310000000000ULL, MappingDesc::SHADOW, "shadow"},
1890b57cec5SDimitry Andric     {0x310000000000ULL, 0x380000000000ULL, MappingDesc::INVALID, "invalid"},
1900b57cec5SDimitry Andric     {0x380000000000ULL, 0x590000000000ULL, MappingDesc::ORIGIN, "origin"},
1910b57cec5SDimitry Andric     {0x590000000000ULL, 0x600000000000ULL, MappingDesc::INVALID, "invalid"},
1920b57cec5SDimitry Andric     {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric // Maps low and high app ranges to contiguous space with zero base:
1950b57cec5SDimitry Andric //   Low:  0000 0000 0000 - 00ff ffff ffff  ->  2000 0000 0000 - 20ff ffff ffff
1960b57cec5SDimitry Andric //   High: 6000 0000 0000 - 7fff ffff ffff  ->  0000 0000 0000 - 1fff ffff ffff
1970b57cec5SDimitry Andric #define LINEARIZE_MEM(mem) \
1980b57cec5SDimitry Andric   (((uptr)(mem) & ~0xc00000000000ULL) ^ 0x200000000000ULL)
1990b57cec5SDimitry Andric #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x100000000000ULL)
2000b57cec5SDimitry Andric #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x280000000000)
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric #elif SANITIZER_NETBSD || (SANITIZER_LINUX && SANITIZER_WORDSIZE == 64)
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric // All of the following configurations are supported.
2050b57cec5SDimitry Andric // ASLR disabled: main executable and DSOs at 0x555550000000
2060b57cec5SDimitry Andric // PIE and ASLR: main executable and DSOs at 0x7f0000000000
2070b57cec5SDimitry Andric // non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
2080b57cec5SDimitry Andric // Heap at 0x700000000000.
2090b57cec5SDimitry Andric const MappingDesc kMemoryLayout[] = {
2100b57cec5SDimitry Andric     {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
2110b57cec5SDimitry Andric     {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
2120b57cec5SDimitry Andric     {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
2130b57cec5SDimitry Andric     {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
2140b57cec5SDimitry Andric     {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
2150b57cec5SDimitry Andric     {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
2160b57cec5SDimitry Andric     {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
2170b57cec5SDimitry Andric     {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
2180b57cec5SDimitry Andric     {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
2190b57cec5SDimitry Andric     {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
2200b57cec5SDimitry Andric     {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
221439352acSDimitry Andric     {0x700000000000ULL, 0x740000000000ULL, MappingDesc::ALLOCATOR, "allocator"},
222439352acSDimitry Andric     {0x740000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
2230b57cec5SDimitry Andric #define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
2240b57cec5SDimitry Andric #define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric #else
2270b57cec5SDimitry Andric #error "Unsupported platform"
2280b57cec5SDimitry Andric #endif
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric #ifndef __clang__
2350b57cec5SDimitry Andric __attribute__((optimize("unroll-loops")))
2360b57cec5SDimitry Andric #endif
237439352acSDimitry Andric inline bool
addr_is_type(uptr addr,int mapping_types)238439352acSDimitry Andric addr_is_type(uptr addr, int mapping_types) {
2390b57cec5SDimitry Andric // It is critical for performance that this loop is unrolled (because then it is
2400b57cec5SDimitry Andric // simplified into just a few constant comparisons).
2410b57cec5SDimitry Andric #ifdef __clang__
2420b57cec5SDimitry Andric #pragma unroll
2430b57cec5SDimitry Andric #endif
2440b57cec5SDimitry Andric   for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
245439352acSDimitry Andric     if ((kMemoryLayout[i].type & mapping_types) &&
2460b57cec5SDimitry Andric         addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
2470b57cec5SDimitry Andric       return true;
2480b57cec5SDimitry Andric   return false;
2490b57cec5SDimitry Andric }
2500b57cec5SDimitry Andric 
251439352acSDimitry Andric #define MEM_IS_APP(mem) \
252439352acSDimitry Andric   (addr_is_type((uptr)(mem), MappingDesc::APP | MappingDesc::ALLOCATOR))
2530b57cec5SDimitry Andric #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
2540b57cec5SDimitry Andric #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
2550b57cec5SDimitry Andric 
25668d75effSDimitry Andric // These constants must be kept in sync with the ones in MemorySanitizer.cpp.
2570b57cec5SDimitry Andric const int kMsanParamTlsSize = 800;
2580b57cec5SDimitry Andric const int kMsanRetvalTlsSize = 800;
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric namespace __msan {
2610b57cec5SDimitry Andric extern int msan_inited;
2620b57cec5SDimitry Andric extern bool msan_init_is_running;
2630b57cec5SDimitry Andric extern int msan_report_count;
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric bool ProtectRange(uptr beg, uptr end);
266439352acSDimitry Andric bool InitShadowWithReExec(bool init_origins);
2670b57cec5SDimitry Andric char *GetProcSelfMaps();
2680b57cec5SDimitry Andric void InitializeInterceptors();
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric void MsanAllocatorInit();
2711db9f3b2SDimitry Andric void MsanDeallocate(BufferedStackTrace *stack, void *ptr);
2720b57cec5SDimitry Andric 
2731db9f3b2SDimitry Andric void *msan_malloc(uptr size, BufferedStackTrace *stack);
2741db9f3b2SDimitry Andric void *msan_calloc(uptr nmemb, uptr size, BufferedStackTrace *stack);
2751db9f3b2SDimitry Andric void *msan_realloc(void *ptr, uptr size, BufferedStackTrace *stack);
2761db9f3b2SDimitry Andric void *msan_reallocarray(void *ptr, uptr nmemb, uptr size,
2771db9f3b2SDimitry Andric                         BufferedStackTrace *stack);
2781db9f3b2SDimitry Andric void *msan_valloc(uptr size, BufferedStackTrace *stack);
2791db9f3b2SDimitry Andric void *msan_pvalloc(uptr size, BufferedStackTrace *stack);
2801db9f3b2SDimitry Andric void *msan_aligned_alloc(uptr alignment, uptr size, BufferedStackTrace *stack);
2811db9f3b2SDimitry Andric void *msan_memalign(uptr alignment, uptr size, BufferedStackTrace *stack);
2820b57cec5SDimitry Andric int msan_posix_memalign(void **memptr, uptr alignment, uptr size,
2831db9f3b2SDimitry Andric                         BufferedStackTrace *stack);
2840b57cec5SDimitry Andric 
2850b57cec5SDimitry Andric void InstallTrapHandler();
2860b57cec5SDimitry Andric void InstallAtExitHandler();
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric const char *GetStackOriginDescr(u32 id, uptr *pc);
2890b57cec5SDimitry Andric 
29081ad6265SDimitry Andric bool IsInSymbolizerOrUnwider();
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric void PrintWarning(uptr pc, uptr bp);
2930b57cec5SDimitry Andric void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin);
2940b57cec5SDimitry Andric 
2950b57cec5SDimitry Andric // Unpoison first n function arguments.
2960b57cec5SDimitry Andric void UnpoisonParam(uptr n);
2970b57cec5SDimitry Andric void UnpoisonThreadLocalState();
2980b57cec5SDimitry Andric 
2990b57cec5SDimitry Andric // Returns a "chained" origin id, pointing to the given stack trace followed by
3000b57cec5SDimitry Andric // the previous origin id.
3010b57cec5SDimitry Andric u32 ChainOrigin(u32 id, StackTrace *stack);
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1;
304bdd1243dSDimitry Andric const int STACK_TRACE_TAG_FIELDS = STACK_TRACE_TAG_POISON + 1;
305bdd1243dSDimitry Andric const int STACK_TRACE_TAG_VPTR = STACK_TRACE_TAG_FIELDS + 1;
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric #define GET_MALLOC_STACK_TRACE                                             \
30806c3fb27SDimitry Andric   UNINITIALIZED BufferedStackTrace stack;                                  \
30906c3fb27SDimitry Andric   if (__msan_get_track_origins() && msan_inited) {                         \
31006c3fb27SDimitry Andric     stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, \
31106c3fb27SDimitry Andric                  common_flags()->fast_unwind_on_malloc,                    \
31206c3fb27SDimitry Andric                  common_flags()->malloc_context_size);                     \
31306c3fb27SDimitry Andric   }
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric // For platforms which support slow unwinder only, we restrict the store context
3160b57cec5SDimitry Andric // size to 1, basically only storing the current pc. We do this because the slow
3170b57cec5SDimitry Andric // unwinder which is based on libunwind is not async signal safe and causes
3180b57cec5SDimitry Andric // random freezes in forking applications as well as in signal handlers.
3190b57cec5SDimitry Andric #define GET_STORE_STACK_TRACE_PC_BP(pc, bp)                              \
32006c3fb27SDimitry Andric   UNINITIALIZED BufferedStackTrace stack;                                \
3210b57cec5SDimitry Andric   if (__msan_get_track_origins() > 1 && msan_inited) {                   \
3220b57cec5SDimitry Andric     int size = flags()->store_context_size;                              \
3230b57cec5SDimitry Andric     if (!SANITIZER_CAN_FAST_UNWIND)                                      \
3240b57cec5SDimitry Andric       size = Min(size, 1);                                               \
32506c3fb27SDimitry Andric     stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_malloc, \
32606c3fb27SDimitry Andric                  size);                                                  \
3270b57cec5SDimitry Andric   }
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric #define GET_STORE_STACK_TRACE \
3300b57cec5SDimitry Andric   GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
3310b57cec5SDimitry Andric 
3320b57cec5SDimitry Andric #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp)                              \
33306c3fb27SDimitry Andric   UNINITIALIZED BufferedStackTrace stack;                                \
33468d75effSDimitry Andric   if (msan_inited) {                                                     \
33568d75effSDimitry Andric     stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal); \
33668d75effSDimitry Andric   }
3370b57cec5SDimitry Andric 
3381db9f3b2SDimitry Andric #define GET_FATAL_STACK_TRACE \
3391db9f3b2SDimitry Andric   GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
3401db9f3b2SDimitry Andric 
3411db9f3b2SDimitry Andric // Unwind the stack for fatal error, as the parameter `stack` is
3421db9f3b2SDimitry Andric // empty without origins.
3431db9f3b2SDimitry Andric #define GET_FATAL_STACK_TRACE_IF_EMPTY(STACK)                                 \
3441db9f3b2SDimitry Andric   if (msan_inited && (STACK)->size == 0) {                                    \
3451db9f3b2SDimitry Andric     (STACK)->Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), nullptr, \
3461db9f3b2SDimitry Andric                     common_flags()->fast_unwind_on_fatal);                    \
3471db9f3b2SDimitry Andric   }
3481db9f3b2SDimitry Andric 
3490b57cec5SDimitry Andric class ScopedThreadLocalStateBackup {
3500b57cec5SDimitry Andric  public:
ScopedThreadLocalStateBackup()3510b57cec5SDimitry Andric   ScopedThreadLocalStateBackup() { Backup(); }
~ScopedThreadLocalStateBackup()3520b57cec5SDimitry Andric   ~ScopedThreadLocalStateBackup() { Restore(); }
3530b57cec5SDimitry Andric   void Backup();
3540b57cec5SDimitry Andric   void Restore();
3550b57cec5SDimitry Andric  private:
3560b57cec5SDimitry Andric   u64 va_arg_overflow_size_tls;
3570b57cec5SDimitry Andric };
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric void MsanTSDInit(void (*destructor)(void *tsd));
3600b57cec5SDimitry Andric void *MsanTSDGet();
3610b57cec5SDimitry Andric void MsanTSDSet(void *tsd);
3620b57cec5SDimitry Andric void MsanTSDDtor(void *tsd);
3630b57cec5SDimitry Andric 
3645f757f3fSDimitry Andric void InstallAtForkHandler();
3655f757f3fSDimitry Andric 
3660b57cec5SDimitry Andric }  // namespace __msan
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric #endif  // MSAN_H
369