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