summaryrefslogtreecommitdiff
blob: e44baa8a5164b9d14da4e3119c099fdae3816dba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
From 8b0f2fff6e080c053e4fd94d44a694768b8c156e Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Wed, 5 Aug 2015 05:35:26 -0700
Subject: [PATCH 78/84] Apply split-layout-llvm.patch

https://llvm.org/bugs/show_bug.cgi?id=24155
---
 lib/Transforms/Instrumentation/MemorySanitizer.cpp | 31 ++++++++++++++++------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/lib/Transforms/Instrumentation/MemorySanitizer.cpp
index 286a563..2ab8bfc 100644
--- a/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ b/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -194,6 +194,12 @@ static cl::opt<bool> ClCheckConstantShadow("msan-check-constant-shadow",
 static const char *const kMsanModuleCtorName = "msan.module_ctor";
 static const char *const kMsanInitName = "__msan_init";
 
+static cl::opt<bool> ClSplitLayout(
+    "msan-split-layout", cl::desc(
+                             "use experimental memory layout compatible with "
+                             "non-pie and non-aslr execution"),
+    cl::Hidden, cl::init(true));
+
 namespace {
 
 // Memory map parameters used in application-to-shadow address calculation.
@@ -228,6 +234,13 @@ static const MemoryMapParams Linux_X86_64_MemoryMapParams = {
   0x200000000000,  // OriginBase
 };
 
+static const MemoryMapParams Linux_X86_64_Split_MemoryMapParams = {
+  0,               // AndMask (not used)
+  0x500000000000,  // XorMask
+  0,               // ShadowBase (not used)
+  0x100000000000,  // OriginBase
+};
+
 // mips64 Linux
 static const MemoryMapParams Linux_MIPS64_MemoryMapParams = {
   0x004000000000,  // AndMask
@@ -483,7 +496,8 @@ bool MemorySanitizer::doInitialization(Module &M) {
     case Triple::Linux:
       switch (TargetTriple.getArch()) {
         case Triple::x86_64:
-          MapParams = Linux_X86_MemoryMapParams.bits64;
+          MapParams = ClSplitLayout ? &Linux_X86_64_Split_MemoryMapParams
+                                    : Linux_X86_MemoryMapParams.bits64;
           break;
         case Triple::x86:
           MapParams = Linux_X86_MemoryMapParams.bits32;
@@ -893,16 +907,17 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
   ///
   /// Offset = (Addr & ~AndMask) ^ XorMask
   Value *getShadowPtrOffset(Value *Addr, IRBuilder<> &IRB) {
+    Value *OffsetLong = IRB.CreatePointerCast(Addr, MS.IntptrTy);
+
     uint64_t AndMask = MS.MapParams->AndMask;
-    assert(AndMask != 0 && "AndMask shall be specified");
-    Value *OffsetLong =
-      IRB.CreateAnd(IRB.CreatePointerCast(Addr, MS.IntptrTy),
-                    ConstantInt::get(MS.IntptrTy, ~AndMask));
+    if (AndMask)
+      OffsetLong =
+          IRB.CreateAnd(OffsetLong, ConstantInt::get(MS.IntptrTy, ~AndMask));
 
     uint64_t XorMask = MS.MapParams->XorMask;
-    if (XorMask != 0)
-      OffsetLong = IRB.CreateXor(OffsetLong,
-                                 ConstantInt::get(MS.IntptrTy, XorMask));
+    if (XorMask)
+      OffsetLong =
+          IRB.CreateXor(OffsetLong, ConstantInt::get(MS.IntptrTy, XorMask));
     return OffsetLong;
   }
 
-- 
2.6.4