|
21 | 21 | #include "llvm/CodeGen/TargetLowering.h"
|
22 | 22 | #include "llvm/CodeGen/TargetPassConfig.h"
|
23 | 23 | #include "llvm/IR/Function.h"
|
| 24 | +#include "llvm/IR/GlobalValue.h" |
24 | 25 | #include "llvm/IR/IRBuilder.h"
|
25 | 26 | #include "llvm/IR/Instructions.h"
|
26 | 27 | #include "llvm/IR/IntrinsicInst.h"
|
| 28 | +#include "llvm/IR/Metadata.h" |
27 | 29 | #include "llvm/IR/Module.h"
|
28 | 30 | #include "llvm/IR/RuntimeLibcalls.h"
|
29 | 31 | #include "llvm/IR/Type.h"
|
|
37 | 39 | #include "llvm/Transforms/Utils/LowerMemIntrinsics.h"
|
38 | 40 | #include "llvm/Transforms/Utils/LowerVectorIntrinsics.h"
|
39 | 41 |
|
| 42 | +#include <set> |
| 43 | + |
40 | 44 | using namespace llvm;
|
41 | 45 |
|
42 | 46 | /// Threshold to leave statically sized memory intrinsic calls. Calls of known
|
@@ -461,6 +465,198 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(
|
461 | 465 | return Changed;
|
462 | 466 | }
|
463 | 467 |
|
| 468 | +namespace { |
| 469 | + |
| 470 | +enum class PointerEncoding { |
| 471 | + Rotate, |
| 472 | + PACCopyable, |
| 473 | + PACNonCopyable, |
| 474 | +}; |
| 475 | + |
| 476 | +bool expandProtectedFieldPtr(Function &Intr) { |
| 477 | + Module &M = *Intr.getParent(); |
| 478 | + |
| 479 | + std::set<GlobalValue *> DSsToDeactivate; |
| 480 | + std::set<Instruction *> LoadsStores; |
| 481 | + |
| 482 | + Type *Int8Ty = Type::getInt8Ty(M.getContext()); |
| 483 | + Type *Int64Ty = Type::getInt64Ty(M.getContext()); |
| 484 | + PointerType *PtrTy = PointerType::get(M.getContext(), 0); |
| 485 | + |
| 486 | + Function *SignIntr = |
| 487 | + Intrinsic::getOrInsertDeclaration(&M, Intrinsic::ptrauth_sign, {}); |
| 488 | + Function *AuthIntr = |
| 489 | + Intrinsic::getOrInsertDeclaration(&M, Intrinsic::ptrauth_auth, {}); |
| 490 | + |
| 491 | + auto *EmuFnTy = FunctionType::get(Int64Ty, {Int64Ty, Int64Ty}, false); |
| 492 | + FunctionCallee EmuSignIntr = M.getOrInsertFunction("__emupac_pacda", EmuFnTy); |
| 493 | + FunctionCallee EmuAuthIntr = M.getOrInsertFunction("__emupac_autda", EmuFnTy); |
| 494 | + |
| 495 | + auto CreateSign = [&](IRBuilder<> &B, Value *Val, Value *Disc, |
| 496 | + OperandBundleDef DSBundle) { |
| 497 | + Function *F = B.GetInsertBlock()->getParent(); |
| 498 | + Attribute FSAttr = F->getFnAttribute("target-features"); |
| 499 | + if (FSAttr.isValid() && FSAttr.getValueAsString().contains("+pauth")) |
| 500 | + return B.CreateCall(SignIntr, {Val, B.getInt32(2), Disc}, DSBundle); |
| 501 | + return B.CreateCall(EmuSignIntr, {Val, Disc}, DSBundle); |
| 502 | + }; |
| 503 | + |
| 504 | + auto CreateAuth = [&](IRBuilder<> &B, Value *Val, Value *Disc, |
| 505 | + OperandBundleDef DSBundle) { |
| 506 | + Function *F = B.GetInsertBlock()->getParent(); |
| 507 | + Attribute FSAttr = F->getFnAttribute("target-features"); |
| 508 | + if (FSAttr.isValid() && FSAttr.getValueAsString().contains("+pauth")) |
| 509 | + return B.CreateCall(AuthIntr, {Val, B.getInt32(2), Disc}, DSBundle); |
| 510 | + return B.CreateCall(EmuAuthIntr, {Val, Disc}, DSBundle); |
| 511 | + }; |
| 512 | + |
| 513 | + auto GetDeactivationSymbol = [&](CallInst *Call) -> GlobalValue * { |
| 514 | + if (auto Bundle = |
| 515 | + Call->getOperandBundle(LLVMContext::OB_deactivation_symbol)) |
| 516 | + return cast<GlobalValue>(Bundle->Inputs[0]); |
| 517 | + return nullptr; |
| 518 | + }; |
| 519 | + |
| 520 | + for (User *U : Intr.users()) { |
| 521 | + auto *Call = cast<CallInst>(U); |
| 522 | + auto *DS = GetDeactivationSymbol(Call); |
| 523 | + std::set<PHINode *> VisitedPhis; |
| 524 | + |
| 525 | + std::function<void(Instruction *)> FindLoadsStores; |
| 526 | + FindLoadsStores = [&](Instruction *I) { |
| 527 | + for (Use &U : I->uses()) { |
| 528 | + if (auto *LI = dyn_cast<LoadInst>(U.getUser())) { |
| 529 | + if (isa<PointerType>(LI->getType())) { |
| 530 | + LoadsStores.insert(LI); |
| 531 | + continue; |
| 532 | + } |
| 533 | + } |
| 534 | + if (auto *SI = dyn_cast<StoreInst>(U.getUser())) { |
| 535 | + if (U.getOperandNo() == 1 && |
| 536 | + isa<PointerType>(SI->getValueOperand()->getType())) { |
| 537 | + LoadsStores.insert(SI); |
| 538 | + continue; |
| 539 | + } |
| 540 | + } |
| 541 | + if (auto *P = dyn_cast<PHINode>(U.getUser())) { |
| 542 | + if (VisitedPhis.insert(P).second) |
| 543 | + FindLoadsStores(P); |
| 544 | + continue; |
| 545 | + } |
| 546 | + // Comparisons against null cannot be used to recover the original |
| 547 | + // pointer so we allow them. |
| 548 | + if (auto *CI = dyn_cast<ICmpInst>(U.getUser())) { |
| 549 | + if (auto *Op = dyn_cast<Constant>(CI->getOperand(0))) |
| 550 | + if (Op->isNullValue()) |
| 551 | + continue; |
| 552 | + if (auto *Op = dyn_cast<Constant>(CI->getOperand(1))) |
| 553 | + if (Op->isNullValue()) |
| 554 | + continue; |
| 555 | + } |
| 556 | + if (DS) |
| 557 | + DSsToDeactivate.insert(DS); |
| 558 | + } |
| 559 | + }; |
| 560 | + |
| 561 | + FindLoadsStores(Call); |
| 562 | + } |
| 563 | + |
| 564 | + for (Instruction *I : LoadsStores) { |
| 565 | + std::set<Value *> Pointers; |
| 566 | + std::set<Value *> Discs; |
| 567 | + std::set<GlobalValue *> DSs; |
| 568 | + std::set<PHINode *> VisitedPhis; |
| 569 | + bool UseHWEncoding = false; |
| 570 | + |
| 571 | + std::function<void(Value *)> FindFields; |
| 572 | + FindFields = [&](Value *V) { |
| 573 | + if (auto *Call = dyn_cast<CallInst>(V)) { |
| 574 | + if (Call->getCalledOperand() == &Intr) { |
| 575 | + Pointers.insert(Call->getArgOperand(0)); |
| 576 | + Discs.insert(Call->getArgOperand(1)); |
| 577 | + if (cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue()) |
| 578 | + UseHWEncoding = true; |
| 579 | + DSs.insert(GetDeactivationSymbol(Call)); |
| 580 | + return; |
| 581 | + } |
| 582 | + } |
| 583 | + if (auto *P = dyn_cast<PHINode>(V)) { |
| 584 | + if (VisitedPhis.insert(P).second) |
| 585 | + for (Value *V : P->incoming_values()) |
| 586 | + FindFields(V); |
| 587 | + return; |
| 588 | + } |
| 589 | + Pointers.insert(nullptr); |
| 590 | + }; |
| 591 | + FindFields(isa<StoreInst>(I) ? cast<StoreInst>(I)->getPointerOperand() |
| 592 | + : cast<LoadInst>(I)->getPointerOperand()); |
| 593 | + if (Pointers.size() != 1 || Discs.size() != 1 || DSs.size() != 1) { |
| 594 | + for (GlobalValue *DS : DSs) |
| 595 | + if (DS) |
| 596 | + DSsToDeactivate.insert(DS); |
| 597 | + continue; |
| 598 | + } |
| 599 | + |
| 600 | + GlobalValue *DS = *DSs.begin(); |
| 601 | + OperandBundleDef DSBundle("deactivation-symbol", DS); |
| 602 | + |
| 603 | + if (auto *LI = dyn_cast<LoadInst>(I)) { |
| 604 | + IRBuilder<> B(LI->getNextNode()); |
| 605 | + auto *LIInt = cast<Instruction>(B.CreatePtrToInt(LI, B.getInt64Ty())); |
| 606 | + Value *Auth; |
| 607 | + if (UseHWEncoding) { |
| 608 | + Auth = CreateAuth(B, LIInt, *Discs.begin(), DSBundle); |
| 609 | + } else { |
| 610 | + Auth = B.CreateAdd(LIInt, *Discs.begin()); |
| 611 | + Auth = B.CreateIntrinsic( |
| 612 | + Auth->getType(), Intrinsic::fshr, |
| 613 | + {Auth, Auth, ConstantInt::get(Auth->getType(), 16)}); |
| 614 | + } |
| 615 | + LI->replaceAllUsesWith(B.CreateIntToPtr(Auth, B.getPtrTy())); |
| 616 | + LIInt->setOperand(0, LI); |
| 617 | + } else if (auto *SI = dyn_cast<StoreInst>(I)) { |
| 618 | + IRBuilder<> B(SI); |
| 619 | + auto *SIValInt = |
| 620 | + B.CreatePtrToInt(SI->getValueOperand(), B.getInt64Ty()); |
| 621 | + Value *Sign; |
| 622 | + if (UseHWEncoding) { |
| 623 | + Sign = CreateSign(B, SIValInt, *Discs.begin(), DSBundle); |
| 624 | + } else { |
| 625 | + Sign = B.CreateIntrinsic( |
| 626 | + SIValInt->getType(), Intrinsic::fshl, |
| 627 | + {SIValInt, SIValInt, ConstantInt::get(SIValInt->getType(), 16)}); |
| 628 | + } |
| 629 | + SI->setOperand(0, B.CreateIntToPtr(Sign, B.getPtrTy())); |
| 630 | + } |
| 631 | + } |
| 632 | + |
| 633 | + for (User *U : llvm::make_early_inc_range(Intr.users())) { |
| 634 | + auto *Call = cast<CallInst>(U); |
| 635 | + auto *Pointer = Call->getArgOperand(0); |
| 636 | + |
| 637 | + Call->replaceAllUsesWith(Pointer); |
| 638 | + Call->eraseFromParent(); |
| 639 | + } |
| 640 | + |
| 641 | + if (!DSsToDeactivate.empty()) { |
| 642 | + Constant *Nop = |
| 643 | + ConstantExpr::getIntToPtr(ConstantInt::get(Int64Ty, 0xd503201f), PtrTy); |
| 644 | + for (GlobalValue *OldDS : DSsToDeactivate) { |
| 645 | + GlobalValue *DS = GlobalAlias::create( |
| 646 | + Int8Ty, 0, GlobalValue::ExternalLinkage, OldDS->getName(), Nop, &M); |
| 647 | + DS->setVisibility(GlobalValue::HiddenVisibility); |
| 648 | + if (OldDS) { |
| 649 | + DS->takeName(OldDS); |
| 650 | + OldDS->replaceAllUsesWith(DS); |
| 651 | + OldDS->eraseFromParent(); |
| 652 | + } |
| 653 | + } |
| 654 | + } |
| 655 | + return true; |
| 656 | +} |
| 657 | + |
| 658 | +} |
| 659 | + |
464 | 660 | bool PreISelIntrinsicLowering::lowerIntrinsics(Module &M) const {
|
465 | 661 | // Map unique constants to globals.
|
466 | 662 | DenseMap<Constant *, GlobalVariable *> CMap;
|
@@ -598,6 +794,9 @@ bool PreISelIntrinsicLowering::lowerIntrinsics(Module &M) const {
|
598 | 794 | return lowerUnaryVectorIntrinsicAsLoop(M, CI);
|
599 | 795 | });
|
600 | 796 | break;
|
| 797 | + case Intrinsic::protected_field_ptr: |
| 798 | + Changed |= expandProtectedFieldPtr(F); |
| 799 | + break; |
601 | 800 | }
|
602 | 801 | }
|
603 | 802 | return Changed;
|
|
0 commit comments