]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/msan/msan.h
Update compiler-rt to 3.7.0 release. This also includes the sanitizer
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / msan / msan.h
1 //===-- msan.h --------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of MemorySanitizer.
11 //
12 // Private MSan header.
13 //===----------------------------------------------------------------------===//
14
15 #ifndef MSAN_H
16 #define MSAN_H
17
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"
24
25 #ifndef MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
26 # define MSAN_REPLACE_OPERATORS_NEW_AND_DELETE 1
27 #endif
28
29 #ifndef MSAN_CONTAINS_UBSAN
30 # define MSAN_CONTAINS_UBSAN CAN_SANITIZE_UB
31 #endif
32
33 struct MappingDesc {
34   uptr start;
35   uptr end;
36   enum Type {
37     INVALID, APP, SHADOW, ORIGIN
38   } type;
39   const char *name;
40 };
41
42
43 #if SANITIZER_LINUX && defined(__mips64)
44
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"}};
51
52 #define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x4000000000ULL)
53 #define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x002000000000)
54
55 #elif SANITIZER_LINUX && defined(__powerpc64__)
56
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"}};
65
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)
73
74 #elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64
75
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"}};
86
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)
94
95 #elif SANITIZER_LINUX && SANITIZER_WORDSIZE == 64
96
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"}};
105
106 #define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x400000000000ULL)
107 #define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x200000000000ULL)
108
109 #else
110 #error "Unsupported platform"
111 #endif
112
113 const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
114
115 #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
116
117 #ifndef __clang__
118 __attribute__((optimize("unroll-loops")))
119 #endif
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).
123 #ifdef __clang__
124 #pragma unroll
125 #endif
126   for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
127     if (kMemoryLayout[i].type == mapping_type &&
128         addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
129       return true;
130   return false;
131 }
132
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)
136
137 // These constants must be kept in sync with the ones in MemorySanitizer.cc.
138 const int kMsanParamTlsSize = 800;
139 const int kMsanRetvalTlsSize = 800;
140
141 namespace __msan {
142 extern int msan_inited;
143 extern bool msan_init_is_running;
144 extern int msan_report_count;
145
146 bool ProtectRange(uptr beg, uptr end);
147 bool InitShadow(bool init_origins);
148 char *GetProcSelfMaps();
149 void InitializeInterceptors();
150
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();
158
159 const char *GetStackOriginDescr(u32 id, uptr *pc);
160
161 void EnterSymbolizer();
162 void ExitSymbolizer();
163 bool IsInSymbolizer();
164
165 struct SymbolizerScope {
166   SymbolizerScope() { EnterSymbolizer(); }
167   ~SymbolizerScope() { ExitSymbolizer(); }
168 };
169
170 void MsanDie();
171 void PrintWarning(uptr pc, uptr bp);
172 void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin);
173
174 void GetStackTrace(BufferedStackTrace *stack, uptr max_s, uptr pc, uptr bp,
175                    bool request_fast_unwind);
176
177 void ReportUMR(StackTrace *stack, u32 origin);
178 void ReportExpectedUMRNotFound(StackTrace *stack);
179 void ReportStats();
180 void ReportAtExitStatistics();
181 void DescribeMemoryRange(const void *x, uptr size);
182 void ReportUMRInsideAddressRange(const char *what, const void *start, uptr size,
183                                  uptr offset);
184
185 // Unpoison first n function arguments.
186 void UnpoisonParam(uptr n);
187 void UnpoisonThreadLocalState();
188
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);
192
193 const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1;
194
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)
201
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)
207
208 #define GET_FATAL_STACK_TRACE_PC_BP(pc, bp)                                    \
209   BufferedStackTrace stack;                                                    \
210   if (msan_inited)                                                             \
211   GetStackTrace(&stack, kStackTraceMax, pc, bp,                                \
212                 common_flags()->fast_unwind_on_fatal)
213
214 #define GET_STORE_STACK_TRACE \
215   GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
216
217 class ScopedThreadLocalStateBackup {
218  public:
219   ScopedThreadLocalStateBackup() { Backup(); }
220   ~ScopedThreadLocalStateBackup() { Restore(); }
221   void Backup();
222   void Restore();
223  private:
224   u64 va_arg_overflow_size_tls;
225 };
226
227 extern void (*death_callback)(void);
228
229 void MsanTSDInit(void (*destructor)(void *tsd));
230 void *MsanTSDGet();
231 void MsanTSDSet(void *tsd);
232 void MsanTSDDtor(void *tsd);
233
234 }  // namespace __msan
235
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)
240
241 #endif  // MSAN_H