msan_linux.cc revision 4c9ddc143839c9f4b79152737cd2869c99e8e86d
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
75static void MsanTrap(int, siginfo_t *siginfo, void *context) {
76  ucontext_t *ucontext = (ucontext_t*)context;
77  uptr pc = ucontext->uc_mcontext.gregs[REG_RIP];
78  uptr bp = ucontext->uc_mcontext.gregs[REG_RBP];
79  PrintWarning(pc + 1 /*1 will be subtracted in StackTrace::Print */, bp);
80  ucontext->uc_mcontext.gregs[REG_RIP] += 2;
81}
82
83void InstallTrapHandler() {
84  struct sigaction sigact;
85  internal_memset(&sigact, 0, sizeof(sigact));
86  sigact.sa_sigaction = MsanTrap;
87  sigact.sa_flags = SA_SIGINFO;
88  CHECK_EQ(0, sigaction(SIGILL, &sigact, 0));
89}
90
91void MsanDie() {
92  _exit(flags()->exit_code);
93}
94}
95
96#endif  // __linux__
97