-
Notifications
You must be signed in to change notification settings - Fork 17.2k
[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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 (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) | ||
|
|
@@ -2661,6 +2662,27 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { | |
| AuthDisc = CI->getArgOperand(2); | ||
| } else | ||
| break; | ||
| } else if (auto *PtrToInt = dyn_cast<PtrToIntOperator>(Ptr)) { | ||
|
ahmedbougacha marked this conversation as resolved.
Outdated
|
||
| // ptrauth constants are equivalent to a call to @llvm.ptrauth.sign for | ||
| // our purposes, so check for that too. | ||
| 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())); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| return eraseInstFromFunction(*II); | ||
| } | ||
|
|
||
| // auth(ptrauth(p,k,d),k,d) -> p | ||
| BasePtr = Builder.CreatePtrToInt(CPA->getPointer(), II->getType()); | ||
| } else | ||
| break; | ||
|
|
||
|
|
@@ -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; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.