Skip to content

Commit 95c0934

Browse files
committed
Make the ptrauth builtins no-ops when ptrauth is disabled.
1 parent 03e4f05 commit 95c0934

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

lib/IRGen/GenBuiltin.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,25 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
266266
args = std::move(replacement);
267267
}
268268

269+
// Implement the ptrauth builtins as no-ops when the Clang
270+
// intrinsics are disabled.
271+
if ((IID == llvm::Intrinsic::ptrauth_sign ||
272+
IID == llvm::Intrinsic::ptrauth_auth ||
273+
IID == llvm::Intrinsic::ptrauth_resign ||
274+
IID == llvm::Intrinsic::ptrauth_strip) &&
275+
!IGF.IGM.getClangASTContext().getLangOpts().PointerAuthIntrinsics) {
276+
out.add(args.claimNext()); // Return the input pointer.
277+
(void) args.claimNext(); // Ignore the key.
278+
if (IID != llvm::Intrinsic::ptrauth_strip) {
279+
(void) args.claimNext(); // Ignore the discriminator.
280+
}
281+
if (IID == llvm::Intrinsic::ptrauth_resign) {
282+
(void) args.claimNext(); // Ignore the new key.
283+
(void) args.claimNext(); // Ignore the new discriminator.
284+
}
285+
return;
286+
}
287+
269288
if (IID != llvm::Intrinsic::not_intrinsic) {
270289
SmallVector<llvm::Type*, 4> ArgTys;
271290
for (auto T : IInfo.Types)

test/IRGen/ptrauth-builtins.sil

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// RUN: %target-swift-frontend -parse-sil -emit-ir %s -module-name test | %FileCheck %s -check-prefix=CHECK -check-prefix=CHECK-%target-ptrauth
2+
3+
import Builtin
4+
5+
sil @test_strip : $@convention(thin) (Builtin.Int64) -> Builtin.Int64 {
6+
bb0(%ptr : $Builtin.Int64):
7+
%key = integer_literal $Builtin.Int32, 0
8+
%result = builtin "int_ptrauth_strip_Int64"(%ptr : $Builtin.Int64, %key : $Builtin.Int32) : $Builtin.Int64
9+
return %result : $Builtin.Int64
10+
}
11+
// CHECK-LABEL: define{{ protected | dllexport | }}swiftcc i64 @test_strip(i64 %0)
12+
// CHECK: entry:
13+
// CHECK-ptrauth-NEXT: %1 = call i64 @llvm.ptrauth.strip.i64(i64 %0, i32 0)
14+
// CHECK-ptrauth-NEXT: ret i64 %1
15+
// CHECK-noptrauth-NEXT: ret i64 %0
16+
// CHECK-NEXT: }
17+
18+
sil @test_sign : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
19+
bb0(%ptr : $Builtin.Int64, %disc : $Builtin.Int64):
20+
%key = integer_literal $Builtin.Int32, 0
21+
%result = builtin "int_ptrauth_sign_Int64"(%ptr : $Builtin.Int64, %key : $Builtin.Int32, %disc : $Builtin.Int64) : $Builtin.Int64
22+
return %result : $Builtin.Int64
23+
}
24+
// CHECK-LABEL: define{{ protected | dllexport | }}swiftcc i64 @test_sign(i64 %0, i64 %1)
25+
// CHECK: entry:
26+
// CHECK-ptrauth-NEXT: %2 = call i64 @llvm.ptrauth.sign.i64(i64 %0, i32 0, i64 %1)
27+
// CHECK-ptrauth-NEXT: ret i64 %2
28+
// CHECK-noptrauth-NEXT: ret i64 %0
29+
// CHECK-NEXT: }
30+
31+
sil @test_auth : $@convention(thin) (Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
32+
bb0(%ptr : $Builtin.Int64, %disc : $Builtin.Int64):
33+
%key = integer_literal $Builtin.Int32, 0
34+
%result = builtin "int_ptrauth_auth_Int64"(%ptr : $Builtin.Int64, %key : $Builtin.Int32, %disc : $Builtin.Int64) : $Builtin.Int64
35+
return %result : $Builtin.Int64
36+
}
37+
// CHECK-LABEL: define{{ protected | dllexport | }}swiftcc i64 @test_auth(i64 %0, i64 %1)
38+
// CHECK: entry:
39+
// CHECK-ptrauth-NEXT: %2 = call i64 @llvm.ptrauth.auth.i64(i64 %0, i32 0, i64 %1)
40+
// CHECK-ptrauth-NEXT: ret i64 %2
41+
// CHECK-noptrauth-NEXT: ret i64 %0
42+
// CHECK-NEXT: }
43+
44+
sil @test_resign : $@convention(thin) (Builtin.Int64, Builtin.Int64, Builtin.Int64) -> Builtin.Int64 {
45+
bb0(%ptr : $Builtin.Int64, %oldDisc : $Builtin.Int64, %newDisc : $Builtin.Int64):
46+
%key = integer_literal $Builtin.Int32, 0
47+
%result = builtin "int_ptrauth_resign_Int64"(%ptr : $Builtin.Int64, %key : $Builtin.Int32, %oldDisc : $Builtin.Int64, %key : $Builtin.Int32, %newDisc : $Builtin.Int64) : $Builtin.Int64
48+
return %result : $Builtin.Int64
49+
}
50+
// CHECK-LABEL: define{{ protected | dllexport | }}swiftcc i64 @test_resign(i64 %0, i64 %1, i64 %2)
51+
// CHECK: entry:
52+
// CHECK-ptrauth-NEXT: %3 = call i64 @llvm.ptrauth.resign.i64(i64 %0, i32 0, i64 %1, i32 0, i64 %2)
53+
// CHECK-ptrauth-NEXT: ret i64 %3
54+
// CHECK-noptrauth-NEXT: ret i64 %0
55+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)