================
@@ -461,6 +463,156 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(
return Changed;
}
+static bool expandProtectedFieldPtr(Function &Intr) {
+ Module &M = *Intr.getParent();
+
+ SmallPtrSet<GlobalValue *, 2> DSsToDeactivate;
+
+ Type *Int8Ty = Type::getInt8Ty(M.getContext());
+ Type *Int64Ty = Type::getInt64Ty(M.getContext());
+ PointerType *PtrTy = PointerType::get(M.getContext(), 0);
+
+ Function *SignIntr =
+ Intrinsic::getOrInsertDeclaration(&M, Intrinsic::ptrauth_sign, {});
+ Function *AuthIntr =
+ Intrinsic::getOrInsertDeclaration(&M, Intrinsic::ptrauth_auth, {});
+
+ auto *EmuFnTy = FunctionType::get(Int64Ty, {Int64Ty, Int64Ty}, false);
+ FunctionCallee EmuSignIntr = M.getOrInsertFunction("__emupac_pacda",
EmuFnTy);
+ FunctionCallee EmuAuthIntr = M.getOrInsertFunction("__emupac_autda",
EmuFnTy);
+
+ auto CreateSign = [&](IRBuilder<> &B, Value *Val, Value *Disc,
+ OperandBundleDef DSBundle) {
+ Function *F = B.GetInsertBlock()->getParent();
+ Attribute FSAttr = F->getFnAttribute("target-features");
+ if (FSAttr.isValid() && FSAttr.getValueAsString().contains("+pauth"))
+ return B.CreateCall(SignIntr, {Val, B.getInt32(2), Disc}, DSBundle);
+ return B.CreateCall(EmuSignIntr, {Val, Disc}, DSBundle);
+ };
+
+ auto CreateAuth = [&](IRBuilder<> &B, Value *Val, Value *Disc,
+ OperandBundleDef DSBundle) {
+ Function *F = B.GetInsertBlock()->getParent();
+ Attribute FSAttr = F->getFnAttribute("target-features");
+ if (FSAttr.isValid() && FSAttr.getValueAsString().contains("+pauth"))
+ return B.CreateCall(AuthIntr, {Val, B.getInt32(2), Disc}, DSBundle);
+ return B.CreateCall(EmuAuthIntr, {Val, Disc}, DSBundle);
+ };
+
+ auto GetDeactivationSymbol = [&](CallInst *Call) -> GlobalValue * {
+ if (auto Bundle =
+ Call->getOperandBundle(LLVMContext::OB_deactivation_symbol))
+ return cast<GlobalValue>(Bundle->Inputs[0]);
+ return nullptr;
+ };
+
+ for (User *U : llvm::make_early_inc_range(Intr.users())) {
+ auto *Call = cast<CallInst>(U);
+
+ auto *Pointer = Call->getArgOperand(0);
+ auto *Disc = Call->getArgOperand(1);
+ bool UseHWEncoding =
+ cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue();
+
+ auto *DS = GetDeactivationSymbol(Call);
+ OperandBundleDef DSBundle("deactivation-symbol", DS);
+
+ for (Use &U : llvm::make_early_inc_range(Call->uses())) {
+ if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
+ if (isa<PointerType>(LI->getType())) {
+ IRBuilder<> B(LI);
+ auto *NewLI = cast<LoadInst>(LI->clone());
+ NewLI->setOperand(0, Pointer);
+ B.Insert(NewLI);
+ auto *LIInt = B.CreatePtrToInt(NewLI, B.getInt64Ty());
+ Value *Auth;
+ if (UseHWEncoding) {
+ Auth = CreateAuth(B, LIInt, Disc, DSBundle);
+ } else {
+ // FIXME: These don't have deactivation symbol attachments, we'll
+ // need to figure out how to add them.
+ Auth = B.CreateAdd(LIInt, Disc);
+ Auth = B.CreateIntrinsic(
+ Auth->getType(), Intrinsic::fshr,
+ {Auth, Auth, ConstantInt::get(Auth->getType(), 16)});
+ }
+ LI->replaceAllUsesWith(B.CreateIntToPtr(Auth, B.getPtrTy()));
+ LI->eraseFromParent();
+ continue;
+ }
+ }
+ if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
+ if (U.getOperandNo() == 1 &&
+ isa<PointerType>(SI->getValueOperand()->getType())) {
+ IRBuilder<> B(SI);
+ auto *SIValInt =
+ B.CreatePtrToInt(SI->getValueOperand(), B.getInt64Ty());
+ Value *Sign;
+ if (UseHWEncoding) {
+ Sign = CreateSign(B, SIValInt, Disc, DSBundle);
+ } else {
+ // FIXME: These don't have deactivation symbol attachments, we'll
+ // need to figure out how to add them.
+ Sign =
+ B.CreateIntrinsic(SIValInt->getType(), Intrinsic::fshl,
+ {SIValInt, SIValInt,
+ ConstantInt::get(SIValInt->getType(), 16)});
+ Sign = B.CreateSub(Sign, Disc);
+ }
+ SI->setOperand(0, B.CreateIntToPtr(Sign, B.getPtrTy()));
+ SI->setOperand(1, Pointer);
+ continue;
+ }
+ }
+
+ // Comparisons against null cannot be used to recover the original
+ // pointer so we replace them with comparisons against the original
+ // pointer.
+ if (auto *CI = dyn_cast<ICmpInst>(U.getUser())) {
+ if (auto *Op = dyn_cast<Constant>(CI->getOperand(0))) {
+ if (Op->isNullValue()) {
+ CI->setOperand(1, Pointer);
+ continue;
+ }
+ }
+ if (auto *Op = dyn_cast<Constant>(CI->getOperand(1))) {
+ if (Op->isNullValue()) {
+ CI->setOperand(0, Pointer);
+ continue;
+ }
+ }
+ }
+
+ // We couldn't rewrite away this use of the intrinsic. Replace it with
the
+ // pointer operand, and arrange to define a deactivation symbol.
+ U.set(Pointer);
+ if (DS)
+ DSsToDeactivate.insert(DS);
+ }
+
+ Call->eraseFromParent();
+ }
+
+ if (!DSsToDeactivate.empty()) {
+ // This is an AArch64 NOP instruction. When the deactivation symbol support
+ // is expanded to more architectures, there will likely need to be an API
+ // for retrieving this constant.
+ Constant *Nop =
+ ConstantExpr::getIntToPtr(ConstantInt::get(Int64Ty, 0xd503201f),
PtrTy);
+ for (GlobalValue *OldDS : DSsToDeactivate) {
+ GlobalValue *DS = GlobalAlias::create(
+ Int8Ty, 0, GlobalValue::ExternalLinkage, OldDS->getName(), Nop, &M);
+ DS->setVisibility(GlobalValue::HiddenVisibility);
+ if (OldDS) {
----------------
pcc wrote:
It can't. My mistake, that was left over from an older version of the code
where it could be. Fixed now.
https://github.com/llvm/llvm-project/pull/151647
_______________________________________________
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits