Skip to content

Commit 6ffed6e

Browse files
authored
Merge pull request #1866 from apple/eng/PR-62476022
[ubsan] nullability-arg: Fix crash on C++ member pointers
2 parents 3de614b + 815c690 commit 6ffed6e

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
@@ -3746,10 +3746,7 @@ void CodeGenFunction::EmitNonNullArgCheck(RValue RV, QualType ArgType,
37463746
}
37473747

37483748
SanitizerScope SanScope(this);
3749-
assert(RV.isScalar());
3750-
llvm::Value *V = RV.getScalarVal();
3751-
llvm::Value *Cond =
3752-
Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
3749+
llvm::Value *Cond = EmitNonNullRValueCheck(RV, ArgType);
37533750
llvm::Constant *StaticData[] = {
37543751
EmitCheckSourceLocation(ArgLoc), EmitCheckSourceLocation(AttrLoc),
37553752
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
@@ -1170,6 +1170,13 @@ Address CodeGenFunction::EmitPointerWithAlignment(const Expr *E,
11701170
return Address(EmitScalarExpr(E), Align);
11711171
}
11721172

1173+
llvm::Value *CodeGenFunction::EmitNonNullRValueCheck(RValue RV, QualType T) {
1174+
llvm::Value *V = RV.getScalarVal();
1175+
if (auto MPT = T->getAs<MemberPointerType>())
1176+
return CGM.getCXXABI().EmitMemberPointerIsNotNull(*this, V, MPT);
1177+
return Builder.CreateICmpNE(V, llvm::Constant::getNullValue(V->getType()));
1178+
}
1179+
11731180
RValue CodeGenFunction::GetUndefRValue(QualType Ty) {
11741181
if (Ty->isVoidType())
11751182
return RValue::get(nullptr);

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,6 +3563,9 @@ class CodeGenFunction : public CodeGenTypeCache {
35633563
// LValue Expression Emission
35643564
//===--------------------------------------------------------------------===//
35653565

3566+
/// Create a check that a scalar RValue is non-null.
3567+
llvm::Value *EmitNonNullRValueCheck(RValue RV, QualType T);
3568+
35663569
/// GetUndefRValue - Get an appropriate 'undef' rvalue for the given type.
35673570
RValue GetUndefRValue(QualType Ty);
35683571

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)