Skip to content

Commit 65f0785

Browse files
committed
[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
1 parent 600f2e1 commit 65f0785

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,11 @@ void CodeGenFunction::EmitReturnValueCheck(llvm::Value *RV) {
30353035
if (!CurCodeDecl)
30363036
return;
30373037

3038+
// If the return block isn't reachable, neither is this check, so don't emit
3039+
// it.
3040+
if (ReturnBlock.isValid() && ReturnBlock.getBlock()->use_empty())
3041+
return;
3042+
30383043
ReturnsNonNullAttr *RetNNAttr = nullptr;
30393044
if (SanOpts.has(SanitizerKind::ReturnsNonnullAttribute))
30403045
RetNNAttr = CurCodeDecl->getAttr<ReturnsNonNullAttr>();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %clang_cc1 -fsanitize=nullability-return -emit-llvm %s -o - -triple x86_64-apple-macosx10.10.0 -Wno-objc-root-class | FileCheck %s
2+
3+
// CHECK-LABEL: define internal i8* @"\01-[I init]"
4+
// CHECK: unreachable
5+
// CHECK-NEXT: }
6+
7+
#pragma clang assume_nonnull begin
8+
@interface I
9+
- (instancetype)init __attribute__((unavailable));
10+
@end
11+
@implementation I
12+
- (instancetype)init __attribute__((unavailable)) { __builtin_unreachable(); }
13+
@end
14+
#pragma clang assume_nonnull end

0 commit comments

Comments
 (0)