Skip to content

[InstCombine] Combine ptrauth constants into ptrauth intrinsics. #94705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2643,13 +2643,14 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
// (sign|resign) + (auth|resign) can be folded by omitting the middle
// sign+auth component if the key and discriminator match.
bool NeedSign = II->getIntrinsicID() == Intrinsic::ptrauth_resign;
Value *Ptr = II->getArgOperand(0);
Value *Key = II->getArgOperand(1);
Value *Disc = II->getArgOperand(2);

// AuthKey will be the key we need to end up authenticating against in
// whatever we replace this sequence with.
Value *AuthKey = nullptr, *AuthDisc = nullptr, *BasePtr;
if (auto CI = dyn_cast<CallBase>(II->getArgOperand(0))) {
if (const auto *CI = dyn_cast<CallBase>(Ptr)) {
BasePtr = CI->getArgOperand(0);
if (CI->getIntrinsicID() == Intrinsic::ptrauth_sign) {
if (CI->getArgOperand(1) != Key || CI->getArgOperand(2) != Disc)
Expand All @@ -2661,6 +2662,27 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
AuthDisc = CI->getArgOperand(2);
} else
break;
} else if (const auto *PtrToInt = dyn_cast<PtrToIntOperator>(Ptr)) {
// ptrauth constants are equivalent to a call to @llvm.ptrauth.sign for
// our purposes, so check for that too.
const auto *CPA = dyn_cast<ConstantPtrAuth>(PtrToInt->getOperand(0));
if (!CPA || !CPA->isKnownCompatibleWith(Key, Disc, DL))
break;

// resign(ptrauth(p,ks,ds),ks,ds,kr,dr) -> ptrauth(p,kr,dr)
if (NeedSign && isa<ConstantInt>(II->getArgOperand(4))) {
auto *SignKey = cast<ConstantInt>(II->getArgOperand(3));
auto *SignDisc = cast<ConstantInt>(II->getArgOperand(4));
auto *SignAddrDisc = ConstantPointerNull::get(Builder.getPtrTy());
auto *NewCPA = ConstantPtrAuth::get(CPA->getPointer(), SignKey,
SignDisc, SignAddrDisc);
replaceInstUsesWith(
*II, ConstantExpr::getPointerCast(NewCPA, II->getType()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this getPointerCast() needed? If so, I think it's missing test coverage.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh I thought this was another remnant of typed pointers, but this one is indeed needed, to reconcile the i64-centric ptrauth intrinsics with ptrs in ptr contexts elsewhere, i.e. the ptrtoint in:

; CHECK-NEXT:    ret i64 ptrtoint (ptr ptrauth (ptr @foo, i32 0, i64 42) to i64)

  %tmp0 = ptrtoint ptr %p to i64
  %authed = call i64 @llvm.ptrauth.resign(i64 ptrtoint(ptr ptrauth(ptr @foo, i32 1, i64 1234) to i64), i32 1, i64 1234, i32 0, i64 42)
  ret i64 %authed

return eraseInstFromFunction(*II);
}

// auth(ptrauth(p,k,d),k,d) -> p
BasePtr = Builder.CreatePtrToInt(CPA->getPointer(), II->getType());
} else
break;

Expand All @@ -2677,8 +2699,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
} else {
// sign(0) + auth(0) = nop
replaceInstUsesWith(*II, BasePtr);
eraseInstFromFunction(*II);
return nullptr;
return eraseInstFromFunction(*II);
}

SmallVector<Value *, 4> CallArgs;
Expand Down
74 changes: 74 additions & 0 deletions llvm/test/Transforms/InstCombine/ptrauth-intrinsics.ll
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ define i64 @test_ptrauth_nop(ptr %p) {
ret i64 %authed
}

declare void @foo()
declare void @bar()

define i64 @test_ptrauth_nop_constant() {
; CHECK-LABEL: @test_ptrauth_nop_constant(
; CHECK-NEXT: ret i64 ptrtoint (ptr @foo to i64)
;
%authed = call i64 @llvm.ptrauth.auth(i64 ptrtoint(ptr ptrauth(ptr @foo, i32 1, i64 1234) to i64), i32 1, i64 1234)
ret i64 %authed
}

define i64 @test_ptrauth_nop_constant_addrdisc() {
; CHECK-LABEL: @test_ptrauth_nop_constant_addrdisc(
; CHECK-NEXT: ret i64 ptrtoint (ptr @foo to i64)
;
%addr = ptrtoint ptr @foo to i64
%blended = call i64 @llvm.ptrauth.blend(i64 %addr, i64 1234)
%authed = call i64 @llvm.ptrauth.auth(i64 ptrtoint(ptr ptrauth(ptr @foo, i32 1, i64 1234, ptr @foo) to i64), i32 1, i64 %blended)
ret i64 %authed
}

define i64 @test_ptrauth_nop_mismatch(ptr %p) {
; CHECK-LABEL: @test_ptrauth_nop_mismatch(
; CHECK-NEXT: [[TMP0:%.*]] = ptrtoint ptr [[P:%.*]] to i64
Expand Down Expand Up @@ -87,6 +108,59 @@ define i64 @test_ptrauth_resign_auth_mismatch(ptr %p) {
ret i64 %authed
}

define i64 @test_ptrauth_nop_constant_mismatch() {
; CHECK-LABEL: @test_ptrauth_nop_constant_mismatch(
; CHECK-NEXT: [[AUTHED:%.*]] = call i64 @llvm.ptrauth.auth(i64 ptrtoint (ptr ptrauth (ptr @foo, i32 1, i64 1234) to i64), i32 1, i64 12)
; CHECK-NEXT: ret i64 [[AUTHED]]
;
%authed = call i64 @llvm.ptrauth.auth(i64 ptrtoint(ptr ptrauth(ptr @foo, i32 1, i64 1234) to i64), i32 1, i64 12)
ret i64 %authed
}

define i64 @test_ptrauth_nop_constant_mismatch_key() {
; CHECK-LABEL: @test_ptrauth_nop_constant_mismatch_key(
; CHECK-NEXT: [[AUTHED:%.*]] = call i64 @llvm.ptrauth.auth(i64 ptrtoint (ptr ptrauth (ptr @foo, i32 1, i64 1234) to i64), i32 0, i64 1234)
; CHECK-NEXT: ret i64 [[AUTHED]]
;
%authed = call i64 @llvm.ptrauth.auth(i64 ptrtoint(ptr ptrauth(ptr @foo, i32 1, i64 1234) to i64), i32 0, i64 1234)
ret i64 %authed
}

define i64 @test_ptrauth_nop_constant_addrdisc_mismatch() {
; CHECK-LABEL: @test_ptrauth_nop_constant_addrdisc_mismatch(
; CHECK-NEXT: [[BLENDED:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @foo to i64), i64 12)
; CHECK-NEXT: [[AUTHED:%.*]] = call i64 @llvm.ptrauth.auth(i64 ptrtoint (ptr ptrauth (ptr @foo, i32 1, i64 1234, ptr @foo) to i64), i32 1, i64 [[BLENDED]])
; CHECK-NEXT: ret i64 [[AUTHED]]
;
%addr = ptrtoint ptr @foo to i64
%blended = call i64 @llvm.ptrauth.blend(i64 %addr, i64 12)
%authed = call i64 @llvm.ptrauth.auth(i64 ptrtoint(ptr ptrauth(ptr @foo, i32 1, i64 1234, ptr @foo) to i64), i32 1, i64 %blended)
ret i64 %authed
}

define i64 @test_ptrauth_nop_constant_addrdisc_mismatch2() {
; CHECK-LABEL: @test_ptrauth_nop_constant_addrdisc_mismatch2(
; CHECK-NEXT: [[BLENDED:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @bar to i64), i64 1234)
; CHECK-NEXT: [[AUTHED:%.*]] = call i64 @llvm.ptrauth.auth(i64 ptrtoint (ptr ptrauth (ptr @foo, i32 1, i64 1234, ptr @foo) to i64), i32 1, i64 [[BLENDED]])
; CHECK-NEXT: ret i64 [[AUTHED]]
;
%addr = ptrtoint ptr @bar to i64
%blended = call i64 @llvm.ptrauth.blend(i64 %addr, i64 1234)
%authed = call i64 @llvm.ptrauth.auth(i64 ptrtoint(ptr ptrauth(ptr @foo, i32 1, i64 1234, ptr @foo) to i64), i32 1, i64 %blended)
ret i64 %authed
}

define i64 @test_ptrauth_resign_ptrauth_constant(ptr %p) {
; CHECK-LABEL: @test_ptrauth_resign_ptrauth_constant(
; CHECK-NEXT: ret i64 ptrtoint (ptr ptrauth (ptr @foo, i32 0, i64 42) to i64)
;

%tmp0 = ptrtoint ptr %p to i64
%authed = call i64 @llvm.ptrauth.resign(i64 ptrtoint(ptr ptrauth(ptr @foo, i32 1, i64 1234) to i64), i32 1, i64 1234, i32 0, i64 42)
ret i64 %authed
}

declare i64 @llvm.ptrauth.auth(i64, i32, i64)
declare i64 @llvm.ptrauth.sign(i64, i32, i64)
declare i64 @llvm.ptrauth.resign(i64, i32, i64, i32, i64)
declare i64 @llvm.ptrauth.blend(i64, i64)
Loading