msan_linux.cc revision 62355e98e83906e99a792441a72362005c740d4d
1//===-- msan_linux.cc -----------------------------------------------------===// 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// Linux-specific code. 13//===----------------------------------------------------------------------===// 14 15#include "sanitizer_common/sanitizer_platform.h" 16#if SANITIZER_LINUX 17 18#include "msan.h" 19 20#include <elf.h> 21#include <link.h> 22#include <stdio.h> 23#include <stdlib.h> 24#include <signal.h> 25#include <unistd.h> 26#include <unwind.h> 27#include <execinfo.h> 28#include <sys/time.h> 29#include <sys/resource.h> 30 31#include "sanitizer_common/sanitizer_common.h" 32#include "sanitizer_common/sanitizer_procmaps.h" 33 34namespace __msan { 35 36static const uptr kMemBeg = 0x600000000000; 37static const uptr kMemEnd = 0x7fffffffffff; 38static const uptr kShadowBeg = MEM_TO_SHADOW(kMemBeg); 39static const uptr kShadowEnd = MEM_TO_SHADOW(kMemEnd); 40static const uptr kBad1Beg = 0x100000000; // 4G 41static const uptr kBad1End = kShadowBeg - 1; 42static const uptr kBad2Beg = kShadowEnd + 1; 43static const uptr kBad2End = kMemBeg - 1; 44static const uptr kOriginsBeg = kBad2Beg; 45static const uptr kOriginsEnd = kBad2End; 46 47bool InitShadow(bool prot1, bool prot2, bool map_shadow, bool init_origins) { 48 if ((uptr) & InitShadow < kMemBeg) { 49 Printf("FATAL: Code below application range: %p < %p. Non-PIE build?\n", 50 &InitShadow, (void *)kMemBeg); 51 return false; 52 } 53 54 if (common_flags()->verbosity) { 55 Printf("__msan_init %p\n", &__msan_init); 56 Printf("Memory : %p %p\n", kMemBeg, kMemEnd); 57 Printf("Bad2 : %p %p\n", kBad2Beg, kBad2End); 58 Printf("Origins : %p %p\n", kOriginsBeg, kOriginsEnd); 59 Printf("Shadow : %p %p\n", kShadowBeg, kShadowEnd); 60 Printf("Bad1 : %p %p\n", kBad1Beg, kBad1End); 61 } 62 63 if (!MemoryRangeIsAvailable(kShadowBeg, 64 init_origins ? kOriginsEnd : kShadowEnd)) { 65 Printf("FATAL: Shadow memory range is not available.\n"); 66 return false; 67 } 68 69 if (prot1 && !Mprotect(kBad1Beg, kBad1End - kBad1Beg)) 70 return false; 71 if (prot2 && !Mprotect(kBad2Beg, kBad2End - kBad2Beg)) 72 return false; 73 if (map_shadow) { 74 void *shadow = MmapFixedNoReserve(kShadowBeg, kShadowEnd - kShadowBeg); 75 if (shadow != (void*)kShadowBeg) return false; 76 } 77 if (init_origins) { 78 void *origins = MmapFixedNoReserve(kOriginsBeg, kOriginsEnd - kOriginsBeg); 79 if (origins != (void*)kOriginsBeg) return false; 80 } 81 return true; 82} 83 84void MsanDie() { 85 _exit(flags()->exit_code); 86} 87 88static void MsanAtExit(void) { 89 if (msan_report_count > 0) { 90 ReportAtExitStatistics(); 91 if (flags()->exit_code) 92 _exit(flags()->exit_code); 93 } 94} 95 96void InstallAtExitHandler() { 97 atexit(MsanAtExit); 98} 99 100} // namespace __msan 101 102#endif // __linux__ 103