[PATCH] D85559: [MSAN] Reintroduce libatomic load/store instrumentation

Gui Andrade via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 15:50:37 PDT 2020


guiand updated this revision to Diff 284076.
guiand edited the summary of this revision.
guiand added a reviewer: rsmith.
guiand added a comment.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Simplified by returning to the old implementation, but having libatomic calls made nounwind (so we never see them as invokes).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85559/new/

https://reviews.llvm.org/D85559

Files:
  clang/lib/CodeGen/CGAtomic.cpp
  compiler-rt/test/msan/libatomic_load_exceptions.cpp
  llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp


Index: llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -3505,7 +3505,13 @@
         IRB.CreateExtractElement(makeAddAcquireOrderingTable(IRB), Ordering);
     CB.setArgOperand(3, NewOrdering);
 
-    IRBuilder<> NextIRB(CB.getNextNode());
+    Instruction *InsPoint = CB.getNextNode();
+    if (!InsPoint) {
+      llvm::errs() << "MSAN -- cannot instrument libatomic call with no "
+                      "successor. Ignoring!\n";
+      return;
+    }
+    IRBuilder<> NextIRB(InsPoint);
     NextIRB.SetCurrentDebugLocation(CB.getDebugLoc());
 
     Value *SrcShadowPtr, *SrcOriginPtr;
Index: compiler-rt/test/msan/libatomic_load_exceptions.cpp
===================================================================
--- /dev/null
+++ compiler-rt/test/msan/libatomic_load_exceptions.cpp
@@ -0,0 +1,36 @@
+// RUN: %clangxx_msan -fexceptions -fsanitize-memory-track-origins=2 -latomic -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-SHADOW
+
+// PPC has no libatomic
+// UNSUPPORTED: powerpc64-target-arch
+// UNSUPPORTED: powerpc64le-target-arch
+
+#include <sanitizer/msan_interface.h>
+#include <stdatomic.h>
+
+typedef struct __attribute((packed)) {
+  uint8_t val[3];
+} i24;
+
+void copy(i24 *dst, i24 *src);
+
+int main() {
+  i24 uninit;
+  i24 init = {0};
+
+  __msan_check_mem_is_initialized(&init, 3);
+  copy(&init, &uninit);
+  __msan_check_mem_is_initialized(&init, 3);
+}
+
+void copy(i24 *dst, i24 *src) {
+  try {
+    __atomic_load(src, dst, __ATOMIC_RELAXED);
+  } catch (...) {
+  }
+}
+
+// CHECK: MemorySanitizer: use-of-uninitialized-value
+// CHECK: #0 {{0x[a-f0-9]+}} in main{{.*}}libatomic_load_exceptions.cpp:[[@LINE-10]]
+
+// CHECK-SHADOW: Uninitialized value was stored to memory at
+// CHECK-SHADOW: #0 {{0x[a-f0-9]+}} in copy{{.*}}libatomic_load_exceptions.cpp:[[@LINE-8]]
Index: clang/lib/CodeGen/CGAtomic.cpp
===================================================================
--- clang/lib/CodeGen/CGAtomic.cpp
+++ clang/lib/CodeGen/CGAtomic.cpp
@@ -307,7 +307,12 @@
   const CGFunctionInfo &fnInfo =
     CGF.CGM.getTypes().arrangeBuiltinFunctionCall(resultType, args);
   llvm::FunctionType *fnTy = CGF.CGM.getTypes().GetFunctionType(fnInfo);
-  llvm::FunctionCallee fn = CGF.CGM.CreateRuntimeFunction(fnTy, fnName);
+  llvm::AttributeList fnAttrs;
+  fnAttrs = fnAttrs.addAttribute(CGF.getLLVMContext(),
+                                 llvm::AttributeList::FunctionIndex,
+                                 llvm::Attribute::NoUnwind);
+  llvm::FunctionCallee fn =
+      CGF.CGM.CreateRuntimeFunction(fnTy, fnName, fnAttrs);
   auto callee = CGCallee::forDirect(fn);
   return CGF.EmitCall(fnInfo, callee, ReturnValueSlot(), args);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85559.284076.patch
Type: text/x-patch
Size: 2912 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200807/af869496/attachment.bin>


More information about the llvm-commits mailing list