Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 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 | |
Evgeniy Stepanov | 24e1372 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 15 | #include "sanitizer_common/sanitizer_platform.h" |
| 16 | #if SANITIZER_LINUX |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 17 | |
| 18 | #include "msan.h" |
| 19 | |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 20 | #include <elf.h> |
| 21 | #include <link.h> |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 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 | |
| 34 | namespace __msan { |
| 35 | |
| 36 | static const uptr kMemBeg = 0x600000000000; |
| 37 | static const uptr kMemEnd = 0x7fffffffffff; |
| 38 | static const uptr kShadowBeg = MEM_TO_SHADOW(kMemBeg); |
| 39 | static const uptr kShadowEnd = MEM_TO_SHADOW(kMemEnd); |
| 40 | static const uptr kBad1Beg = 0x100000000; // 4G |
| 41 | static const uptr kBad1End = kShadowBeg - 1; |
| 42 | static const uptr kBad2Beg = kShadowEnd + 1; |
| 43 | static const uptr kBad2End = kMemBeg - 1; |
| 44 | static const uptr kOriginsBeg = kBad2Beg; |
| 45 | static const uptr kOriginsEnd = kBad2End; |
| 46 | |
| 47 | bool InitShadow(bool prot1, bool prot2, bool map_shadow, bool init_origins) { |
Dmitry Vyukov | 6866dba | 2013-10-15 13:28:51 +0000 | [diff] [blame] | 48 | if (common_flags()->verbosity) { |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 49 | Printf("__msan_init %p\n", &__msan_init); |
| 50 | Printf("Memory : %p %p\n", kMemBeg, kMemEnd); |
| 51 | Printf("Bad2 : %p %p\n", kBad2Beg, kBad2End); |
| 52 | Printf("Origins : %p %p\n", kOriginsBeg, kOriginsEnd); |
| 53 | Printf("Shadow : %p %p\n", kShadowBeg, kShadowEnd); |
| 54 | Printf("Bad1 : %p %p\n", kBad1Beg, kBad1End); |
| 55 | } |
| 56 | |
Evgeniy Stepanov | 4c9ddc1 | 2012-12-26 06:37:23 +0000 | [diff] [blame] | 57 | if (!MemoryRangeIsAvailable(kShadowBeg, |
| 58 | init_origins ? kOriginsEnd : kShadowEnd)) { |
| 59 | Printf("FATAL: Shadow memory range is not available.\n"); |
| 60 | return false; |
| 61 | } |
| 62 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 63 | if (prot1 && !Mprotect(kBad1Beg, kBad1End - kBad1Beg)) |
| 64 | return false; |
| 65 | if (prot2 && !Mprotect(kBad2Beg, kBad2End - kBad2Beg)) |
| 66 | return false; |
| 67 | if (map_shadow) { |
| 68 | void *shadow = MmapFixedNoReserve(kShadowBeg, kShadowEnd - kShadowBeg); |
| 69 | if (shadow != (void*)kShadowBeg) return false; |
| 70 | } |
| 71 | if (init_origins) { |
| 72 | void *origins = MmapFixedNoReserve(kOriginsBeg, kOriginsEnd - kOriginsBeg); |
| 73 | if (origins != (void*)kOriginsBeg) return false; |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 78 | void MsanDie() { |
| 79 | _exit(flags()->exit_code); |
| 80 | } |
Evgeniy Stepanov | 99bf1d7 | 2013-01-10 11:17:55 +0000 | [diff] [blame] | 81 | |
| 82 | static void MsanAtExit(void) { |
| 83 | if (msan_report_count > 0) { |
| 84 | ReportAtExitStatistics(); |
| 85 | if (flags()->exit_code) |
| 86 | _exit(flags()->exit_code); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void InstallAtExitHandler() { |
| 91 | atexit(MsanAtExit); |
| 92 | } |
Reid Kleckner | 0f92deb | 2013-03-11 18:07:42 +0000 | [diff] [blame] | 93 | |
Alexey Samsonov | ba5e996 | 2013-01-30 07:45:58 +0000 | [diff] [blame] | 94 | } // namespace __msan |
Evgeniy Stepanov | 78c56c3 | 2012-12-11 12:27:27 +0000 | [diff] [blame] | 95 | |
| 96 | #endif // __linux__ |