msan.h revision 360784
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
14#ifndef MSAN_H
15#define MSAN_H
16
17#include "sanitizer_common/sanitizer_flags.h"
18#include "sanitizer_common/sanitizer_internal_defs.h"
19#include "sanitizer_common/sanitizer_stacktrace.h"
20#include "msan_interface_internal.h"
21#include "msan_flags.h"
22#include "ubsan/ubsan_platform.h"
23
24#ifndef MSAN_REPLACE_OPERATORS_NEW_AND_DELETE
25# define MSAN_REPLACE_OPERATORS_NEW_AND_DELETE 1
26#endif
27
28#ifndef MSAN_CONTAINS_UBSAN
29# define MSAN_CONTAINS_UBSAN CAN_SANITIZE_UB
30#endif
31
32struct MappingDesc {
33  uptr start;
34  uptr end;
35  enum Type {
36    INVALID, APP, SHADOW, ORIGIN
37  } type;
38  const char *name;
39};
40
41
42#if SANITIZER_LINUX && defined(__mips64)
43
44// MIPS64 maps:
45// - 0x0000000000-0x0200000000: Program own segments
46// - 0xa200000000-0xc000000000: PIE program segments
47// - 0xe200000000-0xffffffffff: libraries segments.
48const MappingDesc kMemoryLayout[] = {
49    {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "app-1"},
50    {0x000200000000ULL, 0x002200000000ULL, MappingDesc::INVALID, "invalid"},
51    {0x002200000000ULL, 0x004000000000ULL, MappingDesc::SHADOW, "shadow-2"},
52    {0x004000000000ULL, 0x004200000000ULL, MappingDesc::INVALID, "invalid"},
53    {0x004200000000ULL, 0x006000000000ULL, MappingDesc::ORIGIN, "origin-2"},
54    {0x006000000000ULL, 0x006200000000ULL, MappingDesc::INVALID, "invalid"},
55    {0x006200000000ULL, 0x008000000000ULL, MappingDesc::SHADOW, "shadow-3"},
56    {0x008000000000ULL, 0x008200000000ULL, MappingDesc::SHADOW, "shadow-1"},
57    {0x008200000000ULL, 0x00a000000000ULL, MappingDesc::ORIGIN, "origin-3"},
58    {0x00a000000000ULL, 0x00a200000000ULL, MappingDesc::ORIGIN, "origin-1"},
59    {0x00a200000000ULL, 0x00c000000000ULL, MappingDesc::APP, "app-2"},
60    {0x00c000000000ULL, 0x00e200000000ULL, MappingDesc::INVALID, "invalid"},
61    {0x00e200000000ULL, 0x00ffffffffffULL, MappingDesc::APP, "app-3"}};
62
63#define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x8000000000ULL)
64#define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x2000000000ULL)
65
66#elif SANITIZER_LINUX && defined(__aarch64__)
67
68// The mapping describes both 39-bits, 42-bits, and 48-bits VMA.  AArch64
69// maps:
70// - 0x0000000000000-0x0000010000000: 39/42/48-bits program own segments
71// - 0x0005500000000-0x0005600000000: 39-bits PIE program segments
72// - 0x0007f80000000-0x0007fffffffff: 39-bits libraries segments
73// - 0x002aa00000000-0x002ab00000000: 42-bits PIE program segments
74// - 0x003ff00000000-0x003ffffffffff: 42-bits libraries segments
75// - 0x0aaaaa0000000-0x0aaab00000000: 48-bits PIE program segments
76// - 0xffff000000000-0x1000000000000: 48-bits libraries segments
77// It is fragmented in multiples segments to increase the memory available
78// on 42-bits (12.21% of total VMA available for 42-bits and 13.28 for
79// 39 bits). The 48-bits segments only cover the usual PIE/default segments
80// plus some more segments (262144GB total, 0.39% total VMA).
81const MappingDesc kMemoryLayout[] = {
82    {0x00000000000ULL, 0x01000000000ULL, MappingDesc::INVALID, "invalid"},
83    {0x01000000000ULL, 0x02000000000ULL, MappingDesc::SHADOW, "shadow-2"},
84    {0x02000000000ULL, 0x03000000000ULL, MappingDesc::ORIGIN, "origin-2"},
85    {0x03000000000ULL, 0x04000000000ULL, MappingDesc::SHADOW, "shadow-1"},
86    {0x04000000000ULL, 0x05000000000ULL, MappingDesc::ORIGIN, "origin-1"},
87    {0x05000000000ULL, 0x06000000000ULL, MappingDesc::APP, "app-1"},
88    {0x06000000000ULL, 0x07000000000ULL, MappingDesc::INVALID, "invalid"},
89    {0x07000000000ULL, 0x08000000000ULL, MappingDesc::APP, "app-2"},
90    {0x08000000000ULL, 0x09000000000ULL, MappingDesc::INVALID, "invalid"},
91    // The mappings below are used only for 42-bits VMA.
92    {0x09000000000ULL, 0x0A000000000ULL, MappingDesc::SHADOW, "shadow-3"},
93    {0x0A000000000ULL, 0x0B000000000ULL, MappingDesc::ORIGIN, "origin-3"},
94    {0x0B000000000ULL, 0x0F000000000ULL, MappingDesc::INVALID, "invalid"},
95    {0x0F000000000ULL, 0x10000000000ULL, MappingDesc::APP, "app-3"},
96    {0x10000000000ULL, 0x11000000000ULL, MappingDesc::INVALID, "invalid"},
97    {0x11000000000ULL, 0x12000000000ULL, MappingDesc::APP, "app-4"},
98    {0x12000000000ULL, 0x17000000000ULL, MappingDesc::INVALID, "invalid"},
99    {0x17000000000ULL, 0x18000000000ULL, MappingDesc::SHADOW, "shadow-4"},
100    {0x18000000000ULL, 0x19000000000ULL, MappingDesc::ORIGIN, "origin-4"},
101    {0x19000000000ULL, 0x20000000000ULL, MappingDesc::INVALID, "invalid"},
102    {0x20000000000ULL, 0x21000000000ULL, MappingDesc::APP, "app-5"},
103    {0x21000000000ULL, 0x26000000000ULL, MappingDesc::INVALID, "invalid"},
104    {0x26000000000ULL, 0x27000000000ULL, MappingDesc::SHADOW, "shadow-5"},
105    {0x27000000000ULL, 0x28000000000ULL, MappingDesc::ORIGIN, "origin-5"},
106    {0x28000000000ULL, 0x29000000000ULL, MappingDesc::SHADOW, "shadow-7"},
107    {0x29000000000ULL, 0x2A000000000ULL, MappingDesc::ORIGIN, "origin-7"},
108    {0x2A000000000ULL, 0x2B000000000ULL, MappingDesc::APP, "app-6"},
109    {0x2B000000000ULL, 0x2C000000000ULL, MappingDesc::INVALID, "invalid"},
110    {0x2C000000000ULL, 0x2D000000000ULL, MappingDesc::SHADOW, "shadow-6"},
111    {0x2D000000000ULL, 0x2E000000000ULL, MappingDesc::ORIGIN, "origin-6"},
112    {0x2E000000000ULL, 0x2F000000000ULL, MappingDesc::APP, "app-7"},
113    {0x2F000000000ULL, 0x39000000000ULL, MappingDesc::INVALID, "invalid"},
114    {0x39000000000ULL, 0x3A000000000ULL, MappingDesc::SHADOW, "shadow-9"},
115    {0x3A000000000ULL, 0x3B000000000ULL, MappingDesc::ORIGIN, "origin-9"},
116    {0x3B000000000ULL, 0x3C000000000ULL, MappingDesc::APP, "app-8"},
117    {0x3C000000000ULL, 0x3D000000000ULL, MappingDesc::INVALID, "invalid"},
118    {0x3D000000000ULL, 0x3E000000000ULL, MappingDesc::SHADOW, "shadow-8"},
119    {0x3E000000000ULL, 0x3F000000000ULL, MappingDesc::ORIGIN, "origin-8"},
120    {0x3F000000000ULL, 0x40000000000ULL, MappingDesc::APP, "app-9"},
121    // The mappings below are used only for 48-bits VMA.
122    // TODO(unknown): 48-bit mapping ony covers the usual PIE, non-PIE
123    // segments and some more segments totalizing 262144GB of VMA (which cover
124    // only 0.32% of all 48-bit VMA). Memory avaliability can be increase by
125    // adding multiple application segments like 39 and 42 mapping.
126    {0x0040000000000ULL, 0x0041000000000ULL, MappingDesc::INVALID, "invalid"},
127    {0x0041000000000ULL, 0x0042000000000ULL, MappingDesc::APP, "app-10"},
128    {0x0042000000000ULL, 0x0047000000000ULL, MappingDesc::INVALID, "invalid"},
129    {0x0047000000000ULL, 0x0048000000000ULL, MappingDesc::SHADOW, "shadow-10"},
130    {0x0048000000000ULL, 0x0049000000000ULL, MappingDesc::ORIGIN, "origin-10"},
131    {0x0049000000000ULL, 0x0050000000000ULL, MappingDesc::INVALID, "invalid"},
132    {0x0050000000000ULL, 0x0051000000000ULL, MappingDesc::APP, "app-11"},
133    {0x0051000000000ULL, 0x0056000000000ULL, MappingDesc::INVALID, "invalid"},
134    {0x0056000000000ULL, 0x0057000000000ULL, MappingDesc::SHADOW, "shadow-11"},
135    {0x0057000000000ULL, 0x0058000000000ULL, MappingDesc::ORIGIN, "origin-11"},
136    {0x0058000000000ULL, 0x0059000000000ULL, MappingDesc::APP, "app-12"},
137    {0x0059000000000ULL, 0x005E000000000ULL, MappingDesc::INVALID, "invalid"},
138    {0x005E000000000ULL, 0x005F000000000ULL, MappingDesc::SHADOW, "shadow-12"},
139    {0x005F000000000ULL, 0x0060000000000ULL, MappingDesc::ORIGIN, "origin-12"},
140    {0x0060000000000ULL, 0x0061000000000ULL, MappingDesc::INVALID, "invalid"},
141    {0x0061000000000ULL, 0x0062000000000ULL, MappingDesc::APP, "app-13"},
142    {0x0062000000000ULL, 0x0067000000000ULL, MappingDesc::INVALID, "invalid"},
143    {0x0067000000000ULL, 0x0068000000000ULL, MappingDesc::SHADOW, "shadow-13"},
144    {0x0068000000000ULL, 0x0069000000000ULL, MappingDesc::ORIGIN, "origin-13"},
145    {0x0069000000000ULL, 0x0AAAAA0000000ULL, MappingDesc::INVALID, "invalid"},
146    {0x0AAAAA0000000ULL, 0x0AAAB00000000ULL, MappingDesc::APP, "app-14"},
147    {0x0AAAB00000000ULL, 0x0AACAA0000000ULL, MappingDesc::INVALID, "invalid"},
148    {0x0AACAA0000000ULL, 0x0AACB00000000ULL, MappingDesc::SHADOW, "shadow-14"},
149    {0x0AACB00000000ULL, 0x0AADAA0000000ULL, MappingDesc::INVALID, "invalid"},
150    {0x0AADAA0000000ULL, 0x0AADB00000000ULL, MappingDesc::ORIGIN, "origin-14"},
151    {0x0AADB00000000ULL, 0x0FF9F00000000ULL, MappingDesc::INVALID, "invalid"},
152    {0x0FF9F00000000ULL, 0x0FFA000000000ULL, MappingDesc::SHADOW, "shadow-15"},
153    {0x0FFA000000000ULL, 0x0FFAF00000000ULL, MappingDesc::INVALID, "invalid"},
154    {0x0FFAF00000000ULL, 0x0FFB000000000ULL, MappingDesc::ORIGIN, "origin-15"},
155    {0x0FFB000000000ULL, 0x0FFFF00000000ULL, MappingDesc::INVALID, "invalid"},
156    {0x0FFFF00000000ULL, 0x1000000000000ULL, MappingDesc::APP, "app-15"},
157};
158# define MEM_TO_SHADOW(mem) ((uptr)mem ^ 0x6000000000ULL)
159# define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x1000000000ULL)
160
161#elif SANITIZER_LINUX && SANITIZER_PPC64
162const MappingDesc kMemoryLayout[] = {
163    {0x000000000000ULL, 0x000200000000ULL, MappingDesc::APP, "low memory"},
164    {0x000200000000ULL, 0x080000000000ULL, MappingDesc::INVALID, "invalid"},
165    {0x080000000000ULL, 0x180200000000ULL, MappingDesc::SHADOW, "shadow"},
166    {0x180200000000ULL, 0x1C0000000000ULL, MappingDesc::INVALID, "invalid"},
167    {0x1C0000000000ULL, 0x2C0200000000ULL, MappingDesc::ORIGIN, "origin"},
168    {0x2C0200000000ULL, 0x300000000000ULL, MappingDesc::INVALID, "invalid"},
169    {0x300000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
170
171// Various kernels use different low end ranges but we can combine them into one
172// big range. They also use different high end ranges but we can map them all to
173// one range.
174// Maps low and high app ranges to contiguous space with zero base:
175//   Low:  0000 0000 0000 - 0001 ffff ffff  ->  1000 0000 0000 - 1001 ffff ffff
176//   High: 3000 0000 0000 - 3fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
177//   High: 4000 0000 0000 - 4fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
178//   High: 7000 0000 0000 - 7fff ffff ffff  ->  0000 0000 0000 - 0fff ffff ffff
179#define LINEARIZE_MEM(mem) \
180  (((uptr)(mem) & ~0xE00000000000ULL) ^ 0x100000000000ULL)
181#define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x080000000000ULL)
182#define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x140000000000ULL)
183
184#elif SANITIZER_FREEBSD && SANITIZER_WORDSIZE == 64
185
186// Low memory: main binary, MAP_32BIT mappings and modules
187// High memory: heap, modules and main thread stack
188const MappingDesc kMemoryLayout[] = {
189    {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "low memory"},
190    {0x010000000000ULL, 0x100000000000ULL, MappingDesc::INVALID, "invalid"},
191    {0x100000000000ULL, 0x310000000000ULL, MappingDesc::SHADOW, "shadow"},
192    {0x310000000000ULL, 0x380000000000ULL, MappingDesc::INVALID, "invalid"},
193    {0x380000000000ULL, 0x590000000000ULL, MappingDesc::ORIGIN, "origin"},
194    {0x590000000000ULL, 0x600000000000ULL, MappingDesc::INVALID, "invalid"},
195    {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "high memory"}};
196
197// Maps low and high app ranges to contiguous space with zero base:
198//   Low:  0000 0000 0000 - 00ff ffff ffff  ->  2000 0000 0000 - 20ff ffff ffff
199//   High: 6000 0000 0000 - 7fff ffff ffff  ->  0000 0000 0000 - 1fff ffff ffff
200#define LINEARIZE_MEM(mem) \
201  (((uptr)(mem) & ~0xc00000000000ULL) ^ 0x200000000000ULL)
202#define MEM_TO_SHADOW(mem) (LINEARIZE_MEM((mem)) + 0x100000000000ULL)
203#define SHADOW_TO_ORIGIN(shadow) (((uptr)(shadow)) + 0x280000000000)
204
205#elif SANITIZER_NETBSD || (SANITIZER_LINUX && SANITIZER_WORDSIZE == 64)
206
207#ifdef MSAN_LINUX_X86_64_OLD_MAPPING
208// Requries PIE binary and ASLR enabled.
209// Main thread stack and DSOs at 0x7f0000000000 (sometimes 0x7e0000000000).
210// Heap at 0x600000000000.
211const MappingDesc kMemoryLayout[] = {
212    {0x000000000000ULL, 0x200000000000ULL, MappingDesc::INVALID, "invalid"},
213    {0x200000000000ULL, 0x400000000000ULL, MappingDesc::SHADOW, "shadow"},
214    {0x400000000000ULL, 0x600000000000ULL, MappingDesc::ORIGIN, "origin"},
215    {0x600000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app"}};
216
217#define MEM_TO_SHADOW(mem) (((uptr)(mem)) & ~0x400000000000ULL)
218#define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x200000000000ULL)
219#else  // MSAN_LINUX_X86_64_OLD_MAPPING
220// All of the following configurations are supported.
221// ASLR disabled: main executable and DSOs at 0x555550000000
222// PIE and ASLR: main executable and DSOs at 0x7f0000000000
223// non-PIE: main executable below 0x100000000, DSOs at 0x7f0000000000
224// Heap at 0x700000000000.
225const MappingDesc kMemoryLayout[] = {
226    {0x000000000000ULL, 0x010000000000ULL, MappingDesc::APP, "app-1"},
227    {0x010000000000ULL, 0x100000000000ULL, MappingDesc::SHADOW, "shadow-2"},
228    {0x100000000000ULL, 0x110000000000ULL, MappingDesc::INVALID, "invalid"},
229    {0x110000000000ULL, 0x200000000000ULL, MappingDesc::ORIGIN, "origin-2"},
230    {0x200000000000ULL, 0x300000000000ULL, MappingDesc::SHADOW, "shadow-3"},
231    {0x300000000000ULL, 0x400000000000ULL, MappingDesc::ORIGIN, "origin-3"},
232    {0x400000000000ULL, 0x500000000000ULL, MappingDesc::INVALID, "invalid"},
233    {0x500000000000ULL, 0x510000000000ULL, MappingDesc::SHADOW, "shadow-1"},
234    {0x510000000000ULL, 0x600000000000ULL, MappingDesc::APP, "app-2"},
235    {0x600000000000ULL, 0x610000000000ULL, MappingDesc::ORIGIN, "origin-1"},
236    {0x610000000000ULL, 0x700000000000ULL, MappingDesc::INVALID, "invalid"},
237    {0x700000000000ULL, 0x800000000000ULL, MappingDesc::APP, "app-3"}};
238#define MEM_TO_SHADOW(mem) (((uptr)(mem)) ^ 0x500000000000ULL)
239#define SHADOW_TO_ORIGIN(mem) (((uptr)(mem)) + 0x100000000000ULL)
240#endif  // MSAN_LINUX_X86_64_OLD_MAPPING
241
242#else
243#error "Unsupported platform"
244#endif
245
246const uptr kMemoryLayoutSize = sizeof(kMemoryLayout) / sizeof(kMemoryLayout[0]);
247
248#define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW((mem))))
249
250#ifndef __clang__
251__attribute__((optimize("unroll-loops")))
252#endif
253inline bool addr_is_type(uptr addr, MappingDesc::Type mapping_type) {
254// It is critical for performance that this loop is unrolled (because then it is
255// simplified into just a few constant comparisons).
256#ifdef __clang__
257#pragma unroll
258#endif
259  for (unsigned i = 0; i < kMemoryLayoutSize; ++i)
260    if (kMemoryLayout[i].type == mapping_type &&
261        addr >= kMemoryLayout[i].start && addr < kMemoryLayout[i].end)
262      return true;
263  return false;
264}
265
266#define MEM_IS_APP(mem) addr_is_type((uptr)(mem), MappingDesc::APP)
267#define MEM_IS_SHADOW(mem) addr_is_type((uptr)(mem), MappingDesc::SHADOW)
268#define MEM_IS_ORIGIN(mem) addr_is_type((uptr)(mem), MappingDesc::ORIGIN)
269
270// These constants must be kept in sync with the ones in MemorySanitizer.cpp.
271const int kMsanParamTlsSize = 800;
272const int kMsanRetvalTlsSize = 800;
273
274namespace __msan {
275extern int msan_inited;
276extern bool msan_init_is_running;
277extern int msan_report_count;
278
279bool ProtectRange(uptr beg, uptr end);
280bool InitShadow(bool init_origins);
281char *GetProcSelfMaps();
282void InitializeInterceptors();
283
284void MsanAllocatorInit();
285void MsanAllocatorThreadFinish();
286void MsanDeallocate(StackTrace *stack, void *ptr);
287
288void *msan_malloc(uptr size, StackTrace *stack);
289void *msan_calloc(uptr nmemb, uptr size, StackTrace *stack);
290void *msan_realloc(void *ptr, uptr size, StackTrace *stack);
291void *msan_reallocarray(void *ptr, uptr nmemb, uptr size, StackTrace *stack);
292void *msan_valloc(uptr size, StackTrace *stack);
293void *msan_pvalloc(uptr size, StackTrace *stack);
294void *msan_aligned_alloc(uptr alignment, uptr size, StackTrace *stack);
295void *msan_memalign(uptr alignment, uptr size, StackTrace *stack);
296int msan_posix_memalign(void **memptr, uptr alignment, uptr size,
297                        StackTrace *stack);
298
299void InstallTrapHandler();
300void InstallAtExitHandler();
301
302const char *GetStackOriginDescr(u32 id, uptr *pc);
303
304void EnterSymbolizer();
305void ExitSymbolizer();
306bool IsInSymbolizer();
307
308struct SymbolizerScope {
309  SymbolizerScope() { EnterSymbolizer(); }
310  ~SymbolizerScope() { ExitSymbolizer(); }
311};
312
313void PrintWarning(uptr pc, uptr bp);
314void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin);
315
316// Unpoison first n function arguments.
317void UnpoisonParam(uptr n);
318void UnpoisonThreadLocalState();
319
320// Returns a "chained" origin id, pointing to the given stack trace followed by
321// the previous origin id.
322u32 ChainOrigin(u32 id, StackTrace *stack);
323
324const int STACK_TRACE_TAG_POISON = StackTrace::TAG_CUSTOM + 1;
325
326#define GET_MALLOC_STACK_TRACE                                            \
327  BufferedStackTrace stack;                                               \
328  if (__msan_get_track_origins() && msan_inited)                          \
329    stack.Unwind(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(),         \
330                 nullptr, common_flags()->fast_unwind_on_malloc,          \
331                 common_flags()->malloc_context_size)
332
333// For platforms which support slow unwinder only, we restrict the store context
334// size to 1, basically only storing the current pc. We do this because the slow
335// unwinder which is based on libunwind is not async signal safe and causes
336// random freezes in forking applications as well as in signal handlers.
337#define GET_STORE_STACK_TRACE_PC_BP(pc, bp)                                    \
338  BufferedStackTrace stack;                                                    \
339  if (__msan_get_track_origins() > 1 && msan_inited) {                         \
340    int size = flags()->store_context_size;                                    \
341    if (!SANITIZER_CAN_FAST_UNWIND)                                            \
342      size = Min(size, 1);                                                     \
343    stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_malloc, size);\
344  }
345
346#define GET_STORE_STACK_TRACE \
347  GET_STORE_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
348
349#define GET_FATAL_STACK_TRACE_PC_BP(pc, bp)                              \
350  BufferedStackTrace stack;                                              \
351  if (msan_inited) {                                                     \
352    stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal); \
353  }
354
355#define GET_FATAL_STACK_TRACE_HERE \
356  GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME())
357
358#define PRINT_CURRENT_STACK_CHECK() \
359  {                                 \
360    GET_FATAL_STACK_TRACE_HERE;     \
361    stack.Print();                  \
362  }
363
364class ScopedThreadLocalStateBackup {
365 public:
366  ScopedThreadLocalStateBackup() { Backup(); }
367  ~ScopedThreadLocalStateBackup() { Restore(); }
368  void Backup();
369  void Restore();
370 private:
371  u64 va_arg_overflow_size_tls;
372};
373
374void MsanTSDInit(void (*destructor)(void *tsd));
375void *MsanTSDGet();
376void MsanTSDSet(void *tsd);
377void MsanTSDDtor(void *tsd);
378
379}  // namespace __msan
380
381#define MSAN_MALLOC_HOOK(ptr, size)       \
382  do {                                    \
383    if (&__sanitizer_malloc_hook) {       \
384      UnpoisonParam(2);                   \
385      __sanitizer_malloc_hook(ptr, size); \
386    }                                     \
387    RunMallocHooks(ptr, size);            \
388  } while (false)
389#define MSAN_FREE_HOOK(ptr)       \
390  do {                            \
391    if (&__sanitizer_free_hook) { \
392      UnpoisonParam(1);           \
393      __sanitizer_free_hook(ptr); \
394    }                             \
395    RunFreeHooks(ptr);            \
396  } while (false)
397
398#endif  // MSAN_H
399