Skip to content

Commit 06bc685

Browse files
committed
[ubsan] nullability-arg: Fix crash on C++ member pointers
Extend -fsanitize=nullability-arg to handle call sites which accept C++ member pointers. rdar://62476022 Differential Revision: https://reviews.llvm.org/D88336
1 parent b5f7e9e commit 06bc685

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3785,10 +3785,7 @@ void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
37853785
}
37863786

37873787
SanitizerScope SanScope(this);
3788-
assert(RV.isScalar());
3789-
llvm::Value *V = RV.getScalarVal();
3790-
llvm::Value *Cond =
3791-
Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
3788+
llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);
37923789
llvm::Constant *StaticData[] = {
37933790
EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),
37943791
llvm::ConstantInt::get(Int32Ty, ArgNo + 1),

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,13 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
11751175
return Address(EmitScalarExpr(E), Align);
11761176
}
11771177

1178+
llvm::Value *CodeGenFunction::EmitNonNullRValueCheck(RValue RV, QualType T) {
1179+
llvm::Value *V = RV.getScalarVal();
1180+
if (auto MPT = T->getAs<MemberPointerType>())
1181+
return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, V, MPT);
1182+
return Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
1183+
}
1184+
11781185
RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
11791186
if (Ty->isVoidType())
11801187
return RValue::get(nullptr);

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,6 +3566,9 @@ class CodeGenFunction : public CodeGenTypeCache {
35663566
// LValue Expression Emission
35673567
//===--------------------------------------------------------------------===//
35683568

3569+
/// Create a check that a scalar RValue is non-null.
3570+
llvm::Value *EmitNonNullRValueCheck(RValue RV, QualType T);
3571+
35693572
/// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
35703573
RValue GetUndefRValue(QualType Ty);
35713574

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %clang_cc1 -x c++ -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fsanitize=nullability-arg | FileCheck %s -check-prefixes=ITANIUM,ALL
2+
// RUN: %clang_cc1 -x c++ -triple x86_64-pc-windows-msvc -emit-llvm -o - %s -fsanitize=nullability-arg | FileCheck %s -check-prefixes=MSVC,ALL
3+
4+
namespace method_ptr {
5+
6+
struct S0 {
7+
void foo1();
8+
};
9+
10+
void foo1(void (S0::*_Nonnull f)());
11+
12+
// ITANIUM-LABEL: @_ZN10method_ptr5test1Ev(){{.*}} {
13+
// ITANIUM: br i1 icmp ne (i64 ptrtoint (void (%"struct.method_ptr::S0"*)* @_ZN10method_ptr2S04foo1Ev to i64), i64 0), label %[[CONT:.*]], label %[[FAIL:[^,]*]]
14+
// ITANIUM-EMPTY:
15+
// ITANIUM-NEXT: [[FAIL]]:
16+
// ITANIUM-NEXT: call void @__ubsan_handle_nullability_arg
17+
18+
// MSVC-LABEL: @"?test1@method_ptr@@YAXXZ"(){{.*}} {
19+
// MSVC: br i1 true, label %[[CONT:.*]], label %[[FAIL:[^,]*]]
20+
// MSVC-EMPTY:
21+
// MSVC-NEXT: [[FAIL]]:
22+
// MSVC-NEXT: call void @__ubsan_handle_nullability_arg
23+
void test1() {
24+
foo1(&S0::foo1);
25+
}
26+
27+
} // namespace method_ptr
28+
29+
namespace data_ptr {
30+
31+
struct S0 {
32+
int field1;
33+
};
34+
35+
using member_ptr = int S0::*;
36+
37+
void foo1(member_ptr _Nonnull);
38+
39+
// ITANIUM-LABEL: @_ZN8data_ptr5test1ENS_2S0E(
40+
// MSVC-LABEL: @"?test1@data_ptr@@YAXUS0@1@@Z"(
41+
// ALL: [[DATA_PTR_CHECK:%.*]] = icmp ne {{.*}}, -1, !nosanitize
42+
// ALL-NEXT: br i1 [[DATA_PTR_CHECK]], label %[[CONT:.*]], label %[[FAIL:[^,]+]]
43+
// ALL-EMPTY:
44+
// ALL-NEXT: [[FAIL]]:
45+
// ALL-NEXT: call void @__ubsan_handle_nullability_arg
46+
void test1(S0 s) {
47+
int S0::*member = &S0::field1;
48+
foo1(member);
49+
}
50+
51+
} // namespace data_ptr

0 commit comments

Comments
 (0)