]> granicus.if.org Git - clang/commitdiff
[CodeGen] Fix some broken IR generated by -fsanitize=unsigned-integer-overflow
authorErik Pilkington <erik.pilkington@gmail.com>
Thu, 28 Feb 2019 00:47:55 +0000 (00:47 +0000)
committerErik Pilkington <erik.pilkington@gmail.com>
Thu, 28 Feb 2019 00:47:55 +0000 (00:47 +0000)
I think the author of the function assumed that `GetInsertBlock()`
wouldn't change from where `atomicPHI` was created, but this isn't
true when `-fsanitize=unsigned-integer-overflow` is enabled (we
generate an overflow/continuation label). Fix by keeping track of the
block we want to return to to complete the cmpxchg loop.

rdar://48406558

Differential revision: https://reviews.llvm.org/D58744

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@355054 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExprScalar.cpp
test/CodeGen/sanitize-atomic-int-overflow.c [new file with mode: 0644]

index bdc07f17769cc643edb5e6429076c94d8e3db33e..e831c823da73b234f63688ed3d20de1750f5de60 100644 (file)
@@ -2555,14 +2555,14 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
   }
 
   if (atomicPHI) {
-    llvm::BasicBlock *opBB = Builder.GetInsertBlock();
+    llvm::BasicBlock *curBlock = Builder.GetInsertBlock();
     llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
     auto Pair = CGF.EmitAtomicCompareExchange(
         LV, RValue::get(atomicPHI), RValue::get(value), E->getExprLoc());
     llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), type);
     llvm::Value *success = Pair.second;
-    atomicPHI->addIncoming(old, opBB);
-    Builder.CreateCondBr(success, contBB, opBB);
+    atomicPHI->addIncoming(old, curBlock);
+    Builder.CreateCondBr(success, contBB, atomicPHI->getParent());
     Builder.SetInsertPoint(contBB);
     return isPre ? value : input;
   }
@@ -2909,14 +2909,14 @@ LValue ScalarExprEmitter::EmitCompoundAssignLValue(
                                 Loc, ScalarConversionOpts(CGF.SanOpts));
 
   if (atomicPHI) {
-    llvm::BasicBlock *opBB = Builder.GetInsertBlock();
+    llvm::BasicBlock *curBlock = Builder.GetInsertBlock();
     llvm::BasicBlock *contBB = CGF.createBasicBlock("atomic_cont", CGF.CurFn);
     auto Pair = CGF.EmitAtomicCompareExchange(
         LHSLV, RValue::get(atomicPHI), RValue::get(Result), E->getExprLoc());
     llvm::Value *old = CGF.EmitToMemory(Pair.first.getScalarVal(), LHSTy);
     llvm::Value *success = Pair.second;
-    atomicPHI->addIncoming(old, opBB);
-    Builder.CreateCondBr(success, contBB, opBB);
+    atomicPHI->addIncoming(old, curBlock);
+    Builder.CreateCondBr(success, contBB, atomicPHI->getParent());
     Builder.SetInsertPoint(contBB);
     return LHSLV;
   }
diff --git a/test/CodeGen/sanitize-atomic-int-overflow.c b/test/CodeGen/sanitize-atomic-int-overflow.c
new file mode 100644 (file)
index 0000000..a1064f4
--- /dev/null
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -triple x86_64-apple-macosx10.14.0 -fsanitize=unsigned-integer-overflow %s -emit-llvm -o - | FileCheck %s
+
+_Atomic(unsigned) atomic;
+
+// CHECK-LABEL: define void @cmpd_assign
+void cmpd_assign() {
+  // CHECK: br label %[[LOOP_START:.*]]
+
+  // CHECK: [[LOOP_START]]:
+  // CHECK-NEXT: phi i32 {{.*}}, [ {{.*}}, %[[INCOMING_BLOCK:.*]] ]
+
+  // CHECK: [[INCOMING_BLOCK]]:
+  // CHECK-NEXT: cmpxchg
+  // CHECK-NEXT: extractvalue
+  // CHECK-NEXT: extractvalue
+  // CHECK-NEXT: br i1 %8, label %{{.*}}, label %[[LOOP_START]]
+  atomic += 1;
+}
+
+// CHECK-LABEL: define void @inc
+void inc() {
+  // CHECK: br label %[[LOOP_START:.*]]
+
+  // CHECK: [[LOOP_START]]:
+  // CHECK-NEXT: phi i32 {{.*}}, [ {{.*}}, %[[INCOMING_BLOCK:.*]] ]
+
+  // CHECK: [[INCOMING_BLOCK]]:
+  // CHECK-NEXT: cmpxchg
+  // CHECK-NEXT: extractvalue
+  // CHECK-NEXT: extractvalue
+  // CHECK-NEXT: br i1 %8, label %{{.*}}, label %[[LOOP_START]]
+  atomic++;
+}