Author: Vedant Kumar
Date: 2020-02-06T10:24:03-08:00
New Revision: 65f0785fff0e45f8cd1b9e90328597197beef899

URL: 
https://github.com/llvm/llvm-project/commit/65f0785fff0e45f8cd1b9e90328597197beef899
DIFF: 
https://github.com/llvm/llvm-project/commit/65f0785fff0e45f8cd1b9e90328597197beef899.diff

LOG: [ubsan] Omit return value check when return block is unreachable

If the return block is unreachable, clang removes it in
CodeGenFunction::FinishFunction(). This removal can leave dangling
references to values defined in the return block if the return block has
successors, which it /would/ if UBSan's return value check is emitted.

In this case, as the UBSan check wouldn't be reachable, it's better to
simply not emit it.

rdar://59196131

Added: 
    clang/test/CodeGenObjC/ubsan-nullability-return-unreachable.m

Modified: 
    clang/lib/CodeGen/CGCall.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index cdd3ca474edf..b55d5856d92d 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -3035,6 +3035,11 @@ void CodeGenFunction::EmitReturnValueCheck(llvm::Value 
*RV) {
   if (!CurCodeDecl)
     return;
 
+  // If the return block isn't reachable, neither is this check, so don't emit
+  // it.
+  if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty())
+    return;
+
   ReturnsNonNullAttr *RetNNAttr = nullptr;
   if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))
     RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();

diff  --git a/clang/test/CodeGenObjC/ubsan-nullability-return-unreachable.m 
b/clang/test/CodeGenObjC/ubsan-nullability-return-unreachable.m
new file mode 100644
index 000000000000..eabc33c91e78
--- /dev/null
+++ b/clang/test/CodeGenObjC/ubsan-nullability-return-unreachable.m
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -fsanitize=nullability-return -emit-llvm %s -o - -triple 
x86_64-apple-macosx10.10.0 -Wno-objc-root-class | FileCheck %s
+
+// CHECK-LABEL: define internal i8* @"\01-[I init]"
+// CHECK: unreachable
+// CHECK-NEXT: }
+
+#pragma clang assume_nonnull begin
+@interface I
+- (instancetype)init __attribute__((unavailable));
+@end
+@implementation I
+- (instancetype)init __attribute__((unavailable)) { __builtin_unreachable(); }
+@end
+#pragma clang assume_nonnull end


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to