[PATCH] D15607: [sanitizer] [msan] Fix origin store of array types

Adhemerval Zanella via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 5 12:17:07 PST 2016


zatrazz updated this revision to Diff 44043.
zatrazz added a comment.

Changes from previous versions:

- Use getCleanShadow on the extracted elements;
- Moved updateOrigin call out of the loop;
- Simplified the testcase.


http://reviews.llvm.org/D15607

Files:
  lib/Transforms/Instrumentation/MemorySanitizer.cpp
  test/Instrumentation/MemorySanitizer/origin-array.ll

Index: test/Instrumentation/MemorySanitizer/origin-array.ll
===================================================================
--- /dev/null
+++ test/Instrumentation/MemorySanitizer/origin-array.ll
@@ -0,0 +1,32 @@
+; RUN: opt < %s -msan -msan-check-access-address=0 -msan-track-origins=2 -S | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
+target triple = "aarch64-unknown-linux-gnu"
+
+; Check origin handling of array types.  Since the instrumentation will
+; potentially creates icmp instruction to check origin values, it requires
+; the intrumentation pass to create icmp for each element.
+
+define void @foo([2 x i64] %v, [2 x i64]* %p) sanitize_memory {
+entry:
+  store [2 x i64] %v, [2 x i64]* %p, align 8
+  ret void
+}
+
+; CHECK-LABEL: @foo
+; CHECK: load {{.*}} @__msan_param_tls
+; CHECK: [[ORIGIN:%[01-9a-z]+]] = load {{.*}} @__msan_param_origin_tls
+
+; Extract and compare the first element.
+; CHECK: {{.*}} extractvalue {{.*}}, 0
+; CHECK: icmp
+; CHECK: br i1 {{.*}}
+; CHECK: ; <label>{{.*}}
+
+; And then the second element.
+; CHECK: {{.*}} extractvalue {{.*}} 1
+; CHECK: icmp
+; CHECK: br i1 {{.*}}
+; CHECK: ; <label>{{.*}}
+
+; CHECK: {{.*}} call i32 @__msan_chain_origin(i32 {{.*}}[[ORIGIN]])
Index: lib/Transforms/Instrumentation/MemorySanitizer.cpp
===================================================================
--- lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -718,14 +718,33 @@
                             IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
                             Origin});
       } else {
-        Value *Cmp = IRB.CreateICmpNE(
-            ConvertedShadow, getCleanShadow(ConvertedShadow), "_mscmp");
-        Instruction *CheckTerm = SplitBlockAndInsertIfThen(
-            Cmp, &*IRB.GetInsertPoint(), false, MS.OriginStoreWeights);
-        IRBuilder<> IRBNew(CheckTerm);
-        paintOrigin(IRBNew, updateOrigin(Origin, IRBNew),
-                    getOriginPtr(Addr, IRBNew, Alignment), StoreSize,
-                    OriginAlignment);
+        if (ArrayType *AT = dyn_cast<ArrayType>(ConvertedShadow->getType())) {
+          // For ArrayType we need to apply the icmp to each element (since the
+          // the instruction does not support array types).
+          for (int i = 0, s = AT->getNumElements(); i < s; i++) {
+            Value *ConvertedShadowElem = IRB.CreateExtractValue(
+                ConvertedShadow, i);
+            Value *CleanShadowElem = getCleanShadow(ConvertedShadowElem);
+            Value *Cmp = IRB.CreateICmpNE(
+                ConvertedShadowElem, CleanShadowElem, "_mscmp");
+
+            Instruction *CheckTerm = SplitBlockAndInsertIfThen(
+                Cmp, &*IRB.GetInsertPoint(), false, MS.OriginStoreWeights);
+            IRB.SetInsertPoint(CheckTerm);
+          }
+          paintOrigin(IRB, updateOrigin(Origin, IRB),
+                      getOriginPtr(Addr, IRB, Alignment), StoreSize,
+                      OriginAlignment);
+        } else {
+          Value *Cmp = IRB.CreateICmpNE(
+              ConvertedShadow, getCleanShadow(ConvertedShadow), "_mscmp");
+          Instruction *CheckTerm = SplitBlockAndInsertIfThen(
+              Cmp, &*IRB.GetInsertPoint(), false, MS.OriginStoreWeights);
+          IRBuilder<> IRBNew(CheckTerm);
+          paintOrigin(IRBNew, updateOrigin(Origin, IRBNew),
+                      getOriginPtr(Addr, IRBNew, Alignment), StoreSize,
+                      OriginAlignment);
+        }
       }
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15607.44043.patch
Type: text/x-patch
Size: 3566 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160105/e853449f/attachment.bin>


More information about the llvm-commits mailing list