[PATCH] D78607: [bounds] Ignore volatile operations

Vitaly Buka via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 21 19:29:21 PDT 2020


vitalybuka created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

-fsanitize=bounds promises to ignore volatile pointers
https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#volatile


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D78607

Files:
  llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
  llvm/test/Instrumentation/BoundsChecking/simple.ll


Index: llvm/test/Instrumentation/BoundsChecking/simple.ll
===================================================================
--- llvm/test/Instrumentation/BoundsChecking/simple.ll
+++ llvm/test/Instrumentation/BoundsChecking/simple.ll
@@ -46,6 +46,16 @@
   ret void
 }
 
+; CHECK: @store_volatile
+define void @store_volatile(i64 %x) nounwind {
+  %1 = tail call i8* @calloc(i64 4, i64 %x)
+  %2 = bitcast i8* %1 to i32*
+  %idx = getelementptr inbounds i32, i32* %2, i64 8
+; CHECK-NOT: trap
+  store volatile i32 3, i32* %idx, align 4
+  ret void
+}
+
 ; CHECK: @f4
 define void @f4(i64 %x) nounwind {
   %1 = tail call i8* @realloc(i8* null, i64 %x) nounwind
@@ -144,12 +154,23 @@
 define i64 @f12(i64 %x, i64 %y) nounwind {
   %1 = tail call i8* @calloc(i64 1, i64 %x)
 ; CHECK: mul i64 %y, 8
+; CHECK: trap
   %2 = bitcast i8* %1 to i64*
   %3 = getelementptr inbounds i64, i64* %2, i64 %y
   %4 = load i64, i64* %3, align 8
   ret i64 %4
 }
 
+; CHECK: @load_volatile
+define i64 @load_volatile(i64 %x, i64 %y) nounwind {
+  %1 = tail call i8* @calloc(i64 1, i64 %x)
+; CHECK-NOT: trap
+  %2 = bitcast i8* %1 to i64*
+  %3 = getelementptr inbounds i64, i64* %2, i64 %y
+  %4 = load volatile i64, i64* %3, align 8
+  ret i64 %4
+}
+
 ; PR17402
 ; CHECK-LABEL: @f13
 define void @f13() nounwind {
Index: llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
+++ llvm/lib/Transforms/Instrumentation/BoundsChecking.cpp
@@ -154,17 +154,22 @@
     Value *Or = nullptr;
     BuilderTy IRB(I.getParent(), BasicBlock::iterator(&I), TargetFolder(DL));
     if (LoadInst *LI = dyn_cast<LoadInst>(&I)) {
-      Or = getBoundsCheckCond(LI->getPointerOperand(), LI, DL, TLI,
-                              ObjSizeEval, IRB, SE);
+      if (!LI->isVolatile())
+        Or = getBoundsCheckCond(LI->getPointerOperand(), LI, DL, TLI,
+                                ObjSizeEval, IRB, SE);
     } else if (StoreInst *SI = dyn_cast<StoreInst>(&I)) {
-      Or = getBoundsCheckCond(SI->getPointerOperand(), SI->getValueOperand(),
-                              DL, TLI, ObjSizeEval, IRB, SE);
+      if (!SI->isVolatile())
+        Or = getBoundsCheckCond(SI->getPointerOperand(), SI->getValueOperand(),
+                                DL, TLI, ObjSizeEval, IRB, SE);
     } else if (AtomicCmpXchgInst *AI = dyn_cast<AtomicCmpXchgInst>(&I)) {
-      Or = getBoundsCheckCond(AI->getPointerOperand(), AI->getCompareOperand(),
-                              DL, TLI, ObjSizeEval, IRB, SE);
+      if (!AI->isVolatile())
+        Or =
+            getBoundsCheckCond(AI->getPointerOperand(), AI->getCompareOperand(),
+                               DL, TLI, ObjSizeEval, IRB, SE);
     } else if (AtomicRMWInst *AI = dyn_cast<AtomicRMWInst>(&I)) {
-      Or = getBoundsCheckCond(AI->getPointerOperand(), AI->getValOperand(), DL,
-                              TLI, ObjSizeEval, IRB, SE);
+      if (!AI->isVolatile())
+        Or = getBoundsCheckCond(AI->getPointerOperand(), AI->getValOperand(),
+                                DL, TLI, ObjSizeEval, IRB, SE);
     }
     if (Or)
       TrapInfo.push_back(std::make_pair(&I, Or));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78607.259149.patch
Type: text/x-patch
Size: 3245 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200422/41ecfbf4/attachment-0001.bin>


More information about the llvm-commits mailing list