Evgeniy Stepanov | 97edeb3 | 2012-12-25 11:53:51 +0000 | [diff] [blame] | 1 | //===-- msan.h --------------------------------------------------*- C++ -*-===// |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 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 | |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 18 | #include "sanitizer_common/sanitizer_flags.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 19 | #include "sanitizer_common/sanitizer_internal_defs.h" |
| 20 | #include "sanitizer_common/sanitizer_stacktrace.h" |
Evgeniy Stepanov | 12c4693 | 2013-01-29 14:33:29 +0000 | [diff] [blame] | 21 | #include "msan_interface_internal.h" |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 22 | #include "msan_flags.h" |
| 23 | |
Evgeniy Stepanov | a897400 | 2013-04-05 12:03:47 +0000 | [diff] [blame] | 24 | #ifndef MSAN_REPLACE_OPERATORS_NEW_AND_DELETE |
| 25 | # define MSAN_REPLACE_OPERATORS_NEW_AND_DELETE 1 |
| 26 | #endif |
| 27 | |
Evgeniy Stepanov | 450eee6 | 2013-10-24 11:56:03 +0000 | [diff] [blame] | 28 | #define MEM_TO_SHADOW(mem) (((uptr)mem) & ~0x400000000000ULL) |
| 29 | #define SHADOW_TO_ORIGIN(shadow) (((uptr)shadow) + 0x200000000000ULL) |
| 30 | #define MEM_TO_ORIGIN(mem) (SHADOW_TO_ORIGIN(MEM_TO_SHADOW(mem))) |
| 31 | #define MEM_IS_APP(mem) ((uptr)mem >= 0x600000000000ULL) |
| 32 | #define MEM_IS_SHADOW(mem) \ |
| 33 | ((uptr)mem >= 0x200000000000ULL && (uptr)mem <= 0x400000000000ULL) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 34 | |
| 35 | const int kMsanParamTlsSizeInWords = 100; |
| 36 | const int kMsanRetvalTlsSizeInWords = 100; |
| 37 | |
| 38 | namespace __msan { |
| 39 | extern int msan_inited; |
| 40 | extern bool msan_init_is_running; |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 41 | extern int msan_report_count; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 42 | |
| 43 | bool ProtectRange(uptr beg, uptr end); |
| 44 | bool InitShadow(bool prot1, bool prot2, bool map_shadow, bool init_origins); |
| 45 | char *GetProcSelfMaps(); |
| 46 | void InitializeInterceptors(); |
| 47 | |
Evgeniy Stepanov | 7c6bd40 | 2013-10-22 14:31:30 +0000 | [diff] [blame] | 48 | void MsanAllocatorThreadFinish(); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 49 | void *MsanReallocate(StackTrace *stack, void *oldp, uptr size, |
| 50 | uptr alignment, bool zeroise); |
Evgeniy Stepanov | effdc7e | 2013-09-16 11:03:31 +0000 | [diff] [blame] | 51 | void MsanDeallocate(StackTrace *stack, void *ptr); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 52 | void InstallTrapHandler(); |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 53 | void InstallAtExitHandler(); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 54 | void ReplaceOperatorsNewAndDelete(); |
| 55 | |
Evgeniy Stepanov | 6f34605 | 2013-09-13 12:49:13 +0000 | [diff] [blame] | 56 | const char *GetOriginDescrIfStack(u32 id, uptr *pc); |
| 57 | |
Kostya Serebryany | 70c6e3f | 2013-02-13 07:19:47 +0000 | [diff] [blame] | 58 | void EnterSymbolizer(); |
| 59 | void ExitSymbolizer(); |
| 60 | bool IsInSymbolizer(); |
| 61 | |
Reid Kleckner | 93c2602 | 2013-03-06 16:11:58 +0000 | [diff] [blame] | 62 | struct SymbolizerScope { |
| 63 | SymbolizerScope() { EnterSymbolizer(); } |
| 64 | ~SymbolizerScope() { ExitSymbolizer(); } |
| 65 | }; |
| 66 | |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 67 | void EnterLoader(); |
| 68 | void ExitLoader(); |
| 69 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 70 | void MsanDie(); |
| 71 | void PrintWarning(uptr pc, uptr bp); |
| 72 | void PrintWarningWithOrigin(uptr pc, uptr bp, u32 origin); |
| 73 | |
Evgeniy Stepanov | efbc435 | 2013-02-19 12:43:18 +0000 | [diff] [blame] | 74 | void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp, |
| 75 | bool fast); |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 76 | |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 77 | void ReportUMR(StackTrace *stack, u32 origin); |
| 78 | void ReportExpectedUMRNotFound(StackTrace *stack); |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 79 | void ReportAtExitStatistics(); |
Evgeniy Stepanov | db010da | 2012-12-26 09:32:05 +0000 | [diff] [blame] | 80 | |
Alexey Samsonov | c2918bf | 2013-06-27 07:50:56 +0000 | [diff] [blame] | 81 | // Unpoison first n function arguments. |
| 82 | void UnpoisonParam(uptr n); |
Evgeniy Stepanov | 91659d5 | 2013-08-27 12:59:39 +0000 | [diff] [blame] | 83 | void UnpoisonThreadLocalState(); |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 84 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 85 | #define GET_MALLOC_STACK_TRACE \ |
| 86 | StackTrace stack; \ |
| 87 | stack.size = 0; \ |
| 88 | if (__msan_get_track_origins() && msan_inited) \ |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 89 | GetStackTrace(&stack, common_flags()->malloc_context_size, \ |
Evgeniy Stepanov | efbc435 | 2013-02-19 12:43:18 +0000 | [diff] [blame] | 90 | StackTrace::GetCurrentPc(), GET_CURRENT_FRAME(), \ |
Sergey Matveev | 0b4bf4d | 2013-05-06 13:15:14 +0000 | [diff] [blame] | 91 | common_flags()->fast_unwind_on_malloc) |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 92 | |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 93 | class ScopedThreadLocalStateBackup { |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 94 | public: |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 95 | ScopedThreadLocalStateBackup() { Backup(); } |
| 96 | ~ScopedThreadLocalStateBackup() { Restore(); } |
| 97 | void Backup(); |
| 98 | void Restore(); |
Alexey Samsonov | c154820 | 2013-08-28 11:26:09 +0000 | [diff] [blame] | 99 | private: |
Evgeniy Stepanov | 0e38a67 | 2013-08-27 14:08:15 +0000 | [diff] [blame] | 100 | u64 va_arg_overflow_size_tls; |
| 101 | }; |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 102 | } // namespace __msan |
| 103 | |
Evgeniy Stepanov | 5c48a8c | 2013-08-02 14:26:58 +0000 | [diff] [blame] | 104 | #define MSAN_MALLOC_HOOK(ptr, size) \ |
| 105 | if (&__msan_malloc_hook) __msan_malloc_hook(ptr, size) |
| 106 | #define MSAN_FREE_HOOK(ptr) \ |
| 107 | if (&__msan_free_hook) __msan_free_hook(ptr) |
| 108 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 109 | #endif // MSAN_H |