1 //===-- msan.h --------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of MemorySanitizer.
12 // Private MSan header.
13 //===----------------------------------------------------------------------===//
18 #include "sanitizer_common/sanitizer_flags.h"
19 #include "sanitizer_common/sanitizer_internal_defs.h"
20 #include "sanitizer_common/sanitizer_stacktrace.h"
21 #include "msan_interface_internal.h"
22 #include "msan_flags.h"
23 #include "ubsan/ubsan_platform.h"
25 #ifndef MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
26 # define MSAN_REPLACE_OPERATORS_NEW_AND_DELETE 1
29 #ifndef MSAN_CONTAINS_UBSAN
30 # define MSAN_CONTAINS_UBSAN CAN_SANITIZE_UB
37 INVALID, APP, SHADOW, ORIGIN
43 #if SANITIZER_LINUX && defined(__mips64)
45 // Everything is above 0x00e000000000.
46 const MappingDesc kMemoryLayout[] = {
47 {0x000000000000ULL, 0x00a000000000ULL, MappingDesc::INVALID, "invalid"},
48 {0x00a000000000ULL, 0x00c000000000ULL, MappingDesc::SHADOW, "shadow"},
49 {0x00c000000000ULL, 0x00e000000000ULL, MappingDesc::ORIGIN, "origin"},
50 {0x00e000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app"}};
52 #define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x4000000000ULL)
53 #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x002000000000)
55 #elif SANITIZER_LINUX && defined(__powerpc64__)
57 const MappingDesc kMemoryLayout[] = {
58 {0x000000000000ULL, 0x000100000000ULL, MappingDesc::APP, "low memory"},
59 {0x000100000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
60 {0x080000000000ULL, 0x180100000000ULL, MappingDesc::SHADOW, "shadow"},
61 {0x180100000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
62 {0x1C0000000000ULL, 0x2C0100000000ULL, MappingDesc::ORIGIN, "origin"},
63 {0x2C0100000000ULL, 0x300000000000ULL, MappingDesc::INVALID, "invalid"},
64 {0x300000000000ULL, 0x400000000000ULL, MappingDesc::APP, "high memory"}};
66 // Maps low and high app ranges to contiguous space with zero base:
67 // Low: 0000 0000 0000 - 0000 ffff ffff -> 1000 0000 0000 - 1000 ffff ffff
68 // High: 3000 0000 0000 - 3fff ffff ffff -> 0000 0000 0000 - 0fff ffff ffff
69 #define LINEARIZE_MEM(mem) \
70 (((uptr)(mem) & ~0x200000000000ULL) ^ 0x100000000000ULL)
71 #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x080000000000ULL)
72 #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
74 #elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64
76 // Low memory: main binary, MAP_32BIT mappings and modules
77 // High memory: heap, modules and main thread stack
78 const MappingDesc kMemoryLayout[] = {
79 {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "low memory"},
80 {0x010000000000ULL, 0x100000000000ULL, MappingDesc::INVALID, "invalid"},
81 {0x100000000000ULL, 0x310000000000ULL, MappingDesc::SHADOW, "shadow"},
82 {0x310000000000ULL, 0x380000000000ULL, MappingDesc::INVALID, "invalid"},
83 {0x380000000000ULL, 0x590000000000ULL, MappingDesc::ORIGIN, "origin"},
84 {0x590000000000ULL, 0x600000000000ULL, MappingDesc::INVALID, "invalid"},
85 {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
87 // Maps low and high app ranges to contiguous space with zero base:
88 // Low: 0000 0000 0000 - 00ff ffff ffff -> 2000 0000 0000 - 20ff ffff ffff
89 // High: 6000 0000 0000 - 7fff ffff ffff -> 0000 0000 0000 - 1fff ffff ffff
90 #define LINEARIZE_MEM(mem) \
91 (((uptr)(mem) & ~0xc00000000000ULL) ^ 0x200000000000ULL)
92 #define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x100000000000ULL)
93 #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x280000000000)
95 #elif SANITIZER_LINUX && SANITIZER_WORDSIZE == 64
97 // Requries PIE binary and ASLR enabled.
98 // Main thread stack and DSOs at 0x7f0000000000 (sometimes 0x7e0000000000).
99 // Heap at 0x600000000000.
100 const MappingDesc kMemoryLayout[] = {
101 {0x000000000000ULL, 0x200000000000ULL, MappingDesc::INVALID, "invalid"},
102 {0x200000000000ULL, 0x400000000000ULL, MappingDesc::SHADOW, "shadow"},
103 {0x400000000000ULL, 0x600000000000ULL, MappingDesc::ORIGIN, "origin"},
104 {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app"}};
106 #define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x400000000000ULL)
107 #define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x200000000000ULL)
110 #error "Unsupported platform"
113 const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
115 #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
118 __attribute__((optimize("unroll-loops")))
120 inline bool addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
121 // It is critical for performance that this loop is unrolled (because then it is
122 // simplified into just a few constant comparisons).
126 for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
127 if (kMemoryLayout[i].type == mapping_type &&
128 addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
133 #define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
134 #define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
135 #define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
137 // These constants must be kept in sync with the ones in MemorySanitizer.cc.
138 const int kMsanParamTlsSize = 800;
139 const int kMsanRetvalTlsSize = 800;
142 extern int msan_inited;
143 extern bool msan_init_is_running;
144 extern int msan_report_count;
146 bool ProtectRange(uptr beg, uptr end);
147 bool InitShadow(bool init_origins);
148 char *GetProcSelfMaps();
149 void InitializeInterceptors();
151 void MsanAllocatorThreadFinish();
152 void *MsanCalloc(StackTrace *stack, uptr nmemb, uptr size);
153 void *MsanReallocate(StackTrace *stack, void *oldp, uptr size,
154 uptr alignment, bool zeroise);
155 void MsanDeallocate(StackTrace *stack, void *ptr);
156 void InstallTrapHandler();
157 void InstallAtExitHandler();
159 const char *GetStackOriginDescr(u32 id, uptr *pc);
161 void EnterSymbolizer();
162 void ExitSymbolizer();
163 bool IsInSymbolizer();
165 struct SymbolizerScope {
166 SymbolizerScope() { EnterSymbolizer(); }
167 ~SymbolizerScope() { ExitSymbolizer(); }
171 void PrintWarning(uptr pc, uptr bp);
172 void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin);
174 void GetStackTrace(BufferedStackTrace *stack, uptr max_s, uptr pc, uptr bp,
175 bool request_fast_unwind);
177 void ReportUMR(StackTrace *stack, u32 origin);
178 void ReportExpectedUMRNotFound(StackTrace *stack);
180 void ReportAtExitStatistics();
181 void DescribeMemoryRange(const void *x, uptr size);
182 void ReportUMRInsideAddressRange(const char *what, const void *start, uptr size,
185 // Unpoison first n function arguments.
186 void UnpoisonParam(uptr n);
187 void UnpoisonThreadLocalState();
189 // Returns a "chained" origin id, pointing to the given stack trace followed by
190 // the previous origin id.
191 u32 ChainOrigin(u32 id, StackTrace *stack);
193 const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1;
195 #define GET_MALLOC_STACK_TRACE \
196 BufferedStackTrace stack; \
197 if (__msan_get_track_origins() && msan_inited) \
198 GetStackTrace(&stack, common_flags()->malloc_context_size, \
199 StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \
200 common_flags()->fast_unwind_on_malloc)
202 #define GET_STORE_STACK_TRACE_PC_BP(pc, bp) \
203 BufferedStackTrace stack; \
204 if (__msan_get_track_origins() > 1 && msan_inited) \
205 GetStackTrace(&stack, flags()->store_context_size, pc, bp, \
206 common_flags()->fast_unwind_on_malloc)
208 #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp) \
209 BufferedStackTrace stack; \
211 GetStackTrace(&stack, kStackTraceMax, pc, bp, \
212 common_flags()->fast_unwind_on_fatal)
214 #define GET_STORE_STACK_TRACE \
215 GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
217 class ScopedThreadLocalStateBackup {
219 ScopedThreadLocalStateBackup() { Backup(); }
220 ~ScopedThreadLocalStateBackup() { Restore(); }
224 u64 va_arg_overflow_size_tls;
227 extern void (*death_callback)(void);
229 void MsanTSDInit(void (*destructor)(void *tsd));
231 void MsanTSDSet(void *tsd);
232 void MsanTSDDtor(void *tsd);
234 } // namespace __msan
236 #define MSAN_MALLOC_HOOK(ptr, size) \
237 if (&__sanitizer_malloc_hook) __sanitizer_malloc_hook(ptr, size)
238 #define MSAN_FREE_HOOK(ptr) \
239 if (&__sanitizer_free_hook) __sanitizer_free_hook(ptr)