Skip to content

Commit a760923

Browse files
committed
Resign pointer when materializing temporary
1 parent f098322 commit a760923

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,15 @@ EmitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *M) {
527527
// initialized it.
528528
if (!Var->hasInitializer()) {
529529
Var->setInitializer(CGM.EmitNullConstant(E->getType()));
530-
EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/true);
530+
QualType RefType = M->getType();
531+
if (RefType.getPointerAuth()) {
532+
// Use the qualifier of the reference temporary to sign the pointer.
533+
auto LV = MakeRawAddrLValue(Object.getPointer(), RefType,
534+
Object.getAlignment());
535+
EmitScalarInit(E, M->getExtendingDecl(), LV, false);
536+
} else {
537+
EmitAnyExprToMem(E, Object, Qualifiers(), /*IsInit*/ true);
538+
}
531539
}
532540
} else {
533541
switch (M->getStorageDuration()) {

clang/test/CodeGen/ptrauth-qualifier-function.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,51 @@ void test_const_ptr_function_call(void) {
8282
#ifdef __cplusplus
8383
void (* get_fptr(void))(int);
8484
void (* __ptrauth(0, 0, 42) f_const_ptr2)(int) = get_fptr();
85-
void (* const __ptrauth(0, 0, 42) &f_ref)(int) = f_const_ptr2;
85+
void (* const __ptrauth(0, 1, 43) &f_ref)(int) = f_const_ptr2;
86+
87+
// CHECK-CXX-LABEL: define internal void @__cxx_global_var_init()
88+
// CHECK-CXX: [[ENTRY:.*]]:
89+
// CHECK-CXX: %[[CALL:.*]] = call ptr @get_fptr()
90+
// CHECK-CXX: %[[V0:.*]] = icmp ne ptr %[[CALL]], null
91+
// CHECK-CXX: br i1 %[[V0]], label %[[RESIGN_NONNULL:.*]], label %[[RESIGN_CONT:.*]]
92+
93+
// CHECK-CXX: [[RESIGN_NONNULL]]:
94+
// CHECK-CXX: %[[V1:.*]] = ptrtoint ptr %[[CALL]] to i64
95+
// CHECK-CXX: %[[V2:.*]] = call i64 @llvm.ptrauth.resign(i64 %[[V1]], i32 0, i64 2712, i32 0, i64 42)
96+
// CHECK-CXX: %[[V3:.*]] = inttoptr i64 %[[V2]] to ptr
97+
// CHECK-CXX: br label %[[RESIGN_CONT]]
98+
99+
// CHECK-CXX: [[RESIGN_CONT]]:
100+
// CHECK-CXX: %[[V4:.*]] = phi ptr [ null, %[[ENTRY]] ], [ %[[V3]], %[[RESIGN_NONNULL]] ]
101+
// CHECK-CXX: store ptr %[[V4]], ptr @f_const_ptr2, align 8
102+
103+
// CHECK-CXX-LABEL: define internal void @__cxx_global_var_init.1()
104+
// CHECK-CXX: [[ENTRY:.*]]:
105+
// CHECK-CXX: %[[V0:.*]] = load ptr, ptr @f_const_ptr2, align 8
106+
// CHECK-CXX: %[[V1:.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @_ZGR5f_ref_ to i64), i64 43)
107+
// CHECK-CXX: %[[V2:.*]] = icmp ne ptr %[[V0]], null
108+
// CHECK-CXX: br i1 %[[V2]], label %[[RESIGN_NONNULL:.*]], label %[[RESIGN_CONT:.*]]
109+
110+
// CHECK-CXX: [[RESIGN_NONNULL]]:
111+
// CHECK-CXX: %[[V3:.*]] = ptrtoint ptr %[[V0]] to i64
112+
// CHECK-CXX: %[[V4:.*]] = call i64 @llvm.ptrauth.resign(i64 %[[V3]], i32 0, i64 42, i32 0, i64 %[[V1]])
113+
// CHECK-CXX: %[[V5:.*]] = inttoptr i64 %[[V4]] to ptr
114+
// CHECK-CXX: br label %[[RESIGN_CONT]]
115+
116+
// CHECK-CXX: [[RESIGN_CONT]]:
117+
// CHECK-CXX: %[[V6:.*]] = phi ptr [ null, %[[ENTRY]] ], [ %[[V5]], %[[RESIGN_NONNULL]] ]
118+
// CHECK-CXX: store ptr %[[V6]], ptr @_ZGR5f_ref_, align 8
119+
// CHECK-CXX: store ptr @_ZGR5f_ref_, ptr @f_ref, align 8
86120

87121
// CHECK-CXX-LABEL: define void @test_const_ptr_ref_function_call()
88122
void test_const_ptr_ref_function_call(void) {
89123
f_ref(1);
90124

91-
// CHECK-CXX: %[[V0:.*]] = load ptr, ptr @f_const_ptr2, align 8
92-
// CHECK-CXX: call void %[[V0]](i32 noundef 1) [ "ptrauth"(i32 0, i64 42) ]
125+
// CHECK-CXX: %[[V0:.*]] = load ptr, ptr @f_ref, align 8
126+
// CHECK-CXX: %[[V1:.*]] = load ptr, ptr %[[V0]], align 8
127+
// CHECK-CXX: %[[V2:.*]] = ptrtoint ptr %[[V0]] to i64
128+
// CHECK-CXX: %[[V3:.*]] = call i64 @llvm.ptrauth.blend(i64 %[[V2]], i64 43)
129+
// CHECK-CXX: call void %[[V1]](i32 noundef 1) [ "ptrauth"(i32 0, i64 %[[V3]]) ]
93130
}
94131
}
95132
#endif

0 commit comments

Comments
 (0)