Skip to content

Commit 1d70f5a

Browse files
committed
Address review comments
Created using spr 1.3.6-beta.1
2 parents 1504c88 + f6e1d57 commit 1d70f5a

File tree

4 files changed

+95
-91
lines changed

4 files changed

+95
-91
lines changed

lld/ELF/Relocations.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,9 +1175,8 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
11751175
<< " cannot be used against ifunc symbol '" << &sym << "'";
11761176
printLocation(diag, *sec, sym, offset);
11771177
} else {
1178-
part.relaDyn->addReloc({ctx.target->iRelativeRel, sec, offset,
1179-
DynamicReloc::AddendOnlyWithTargetVA, sym,
1180-
addend, R_ABS});
1178+
part.relaDyn->addReloc({ctx.target->iRelativeRel, sec, offset, false,
1179+
sym, addend, R_ABS});
11811180
return;
11821181
}
11831182
}

llvm/docs/LangRef.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31200,14 +31200,14 @@ third argument is 1). When loading from the pointer, the inverse operation
3120031200
is done on the loaded pointer after it is loaded. Specifically, when the
3120131201
third argument is 1, the pointer is signed (using pointer authentication
3120231202
instructions or emulated PAC if not supported by the hardware) using
31203-
the struct address before being stored, and authenticated after being
31203+
the discriminator before being stored, and authenticated after being
3120431204
loaded. Note that it is currently unsupported to have the third argument
3120531205
be 1 on targets other than AArch64. When the third argument is 0, it is
3120631206
rotated left by 16 bits and the discriminator is subtracted before being
3120731207
stored, and the discriminator is added and the pointer is rotated right
3120831208
by 16 bits after being loaded.
3120931209

31210-
If the pointer is used otherwise than for loading or storing (e.g. its
31210+
If the pointer is used other than for loading or storing (e.g. its
3121131211
address escapes), that will disable all blending operations using
3121231212
the deactivation symbol specified in the intrinsic's operand bundle.
3121331213
The deactivation symbol operand bundle is copied onto any sign and auth

llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp

Lines changed: 40 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
#include "llvm/Transforms/Utils/LowerMemIntrinsics.h"
4040
#include "llvm/Transforms/Utils/LowerVectorIntrinsics.h"
4141

42-
#include <set>
43-
4442
using namespace llvm;
4543

4644
/// Threshold to leave statically sized memory intrinsic calls. Calls of known
@@ -476,8 +474,8 @@ enum class PointerEncoding {
476474
bool expandProtectedFieldPtr(Function &Intr) {
477475
Module &M = *Intr.getParent();
478476

479-
std::set<GlobalValue *> DSsToDeactivate;
480-
std::set<Instruction *> LoadsStores;
477+
SmallPtrSet<GlobalValue *, 2> DSsToDeactivate;
478+
SmallPtrSet<Instruction *, 2> LoadsStores;
481479

482480
Type *Int8Ty = Type::getInt8Ty(M.getContext());
483481
Type *Int64Ty = Type::getInt64Ty(M.getContext());
@@ -520,111 +518,75 @@ bool expandProtectedFieldPtr(Function &Intr) {
520518
for (User *U : Intr.users()) {
521519
auto *Call = cast<CallInst>(U);
522520
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);
521+
522+
for (Use &U : Call->uses()) {
523+
if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
524+
if (isa<PointerType>(LI->getType())) {
525+
LoadsStores.insert(LI);
544526
continue;
545527
}
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);
558528
}
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;
529+
if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
530+
if (U.getOperandNo() == 1 &&
531+
isa<PointerType>(SI->getValueOperand()->getType())) {
532+
LoadsStores.insert(SI);
533+
continue;
581534
}
582535
}
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;
536+
// Comparisons against null cannot be used to recover the original
537+
// pointer so we allow them.
538+
if (auto *CI = dyn_cast<ICmpInst>(U.getUser())) {
539+
if (auto *Op = dyn_cast<Constant>(CI->getOperand(0)))
540+
if (Op->isNullValue())
541+
continue;
542+
if (auto *Op = dyn_cast<Constant>(CI->getOperand(1)))
543+
if (Op->isNullValue())
544+
continue;
588545
}
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;
546+
if (DS)
547+
DSsToDeactivate.insert(DS);
598548
}
549+
}
550+
551+
for (Instruction *I : LoadsStores) {
552+
auto *PointerOperand = isa<StoreInst>(I)
553+
? cast<StoreInst>(I)->getPointerOperand()
554+
: cast<LoadInst>(I)->getPointerOperand();
555+
auto *Call = cast<CallInst>(PointerOperand);
556+
557+
auto *Disc = Call->getArgOperand(1);
558+
bool UseHWEncoding = cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue();
599559

600-
GlobalValue *DS = *DSs.begin();
560+
GlobalValue *DS = GetDeactivationSymbol(Call);
601561
OperandBundleDef DSBundle("deactivation-symbol", DS);
602562

603563
if (auto *LI = dyn_cast<LoadInst>(I)) {
604564
IRBuilder<> B(LI->getNextNode());
605565
auto *LIInt = cast<Instruction>(B.CreatePtrToInt(LI, B.getInt64Ty()));
606566
Value *Auth;
607567
if (UseHWEncoding) {
608-
Auth = CreateAuth(B, LIInt, *Discs.begin(), DSBundle);
568+
Auth = CreateAuth(B, LIInt, Disc, DSBundle);
609569
} else {
610-
Auth = B.CreateAdd(LIInt, *Discs.begin());
570+
Auth = B.CreateAdd(LIInt, Disc);
611571
Auth = B.CreateIntrinsic(
612572
Auth->getType(), Intrinsic::fshr,
613573
{Auth, Auth, ConstantInt::get(Auth->getType(), 16)});
614574
}
615575
LI->replaceAllUsesWith(B.CreateIntToPtr(Auth, B.getPtrTy()));
616576
LIInt->setOperand(0, LI);
617-
} else if (auto *SI = dyn_cast<StoreInst>(I)) {
577+
} else {
578+
auto *SI = cast<StoreInst>(I);
618579
IRBuilder<> B(SI);
619580
auto *SIValInt =
620581
B.CreatePtrToInt(SI->getValueOperand(), B.getInt64Ty());
621582
Value *Sign;
622583
if (UseHWEncoding) {
623-
Sign = CreateSign(B, SIValInt, *Discs.begin(), DSBundle);
584+
Sign = CreateSign(B, SIValInt, Disc, DSBundle);
624585
} else {
625586
Sign = B.CreateIntrinsic(
626587
SIValInt->getType(), Intrinsic::fshl,
627588
{SIValInt, SIValInt, ConstantInt::get(SIValInt->getType(), 16)});
589+
Sign = B.CreateSub(Sign, Disc);
628590
}
629591
SI->setOperand(0, B.CreateIntToPtr(Sign, B.getPtrTy()));
630592
}

llvm/test/Transforms/PreISelIntrinsicLowering/protected-field-pointer.ll

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ target triple = "aarch64-unknown-linux-gnu"
77
@ds1 = external global i8
88
; CHECK: @ds2 = external global i8
99
@ds2 = external global i8
10-
; CHECK: @ds3 = hidden alias i8, inttoptr (i64 3573751839 to ptr)
10+
; CHECK: @ds3 = external global i8
1111
@ds3 = external global i8
12+
; CHECK: @ds4 = external global i8
13+
@ds4 = external global i8
14+
; CHECK: @ds5 = external global i8
15+
@ds5 = external global i8
16+
; CHECK: @ds6 = hidden alias i8, inttoptr (i64 3573751839 to ptr)
17+
@ds6 = external global i8
1218

13-
; CHECK: define ptr @f1
14-
define ptr @f1(ptr %ptrptr) {
19+
; CHECK: define ptr @load_hw
20+
define ptr @load_hw(ptr %ptrptr) {
1521
; CHECK: %ptr = load ptr, ptr %ptrptr, align 8
1622
; CHECK: %1 = ptrtoint ptr %ptr to i64
1723
; NOPAUTH: %2 = call i64 @__emupac_autda(i64 %1, i64 1) [ "deactivation-symbol"(ptr @ds1) ]
@@ -23,8 +29,8 @@ define ptr @f1(ptr %ptrptr) {
2329
ret ptr %ptr
2430
}
2531

26-
; CHECK: define void @f2
27-
define void @f2(ptr %ptrptr, ptr %ptr) {
32+
; CHECK: define void @store_hw
33+
define void @store_hw(ptr %ptrptr, ptr %ptr) {
2834
; CHECK: %1 = ptrtoint ptr %ptr to i64
2935
; NOPAUTH: %2 = call i64 @__emupac_pacda(i64 %1, i64 2) [ "deactivation-symbol"(ptr @ds2) ]
3036
; PAUTH: %2 = call i64 @llvm.ptrauth.sign(i64 %1, i32 2, i64 2) [ "deactivation-symbol"(ptr @ds2) ]
@@ -36,10 +42,47 @@ define void @f2(ptr %ptrptr, ptr %ptr) {
3642
ret void
3743
}
3844

39-
; CHECK: define ptr @f3
40-
define ptr @f3(ptr %ptrptr) {
45+
; CHECK: define ptr @load_sw
46+
define ptr @load_sw(ptr %ptrptr) {
47+
; CHECK: %ptr = load ptr, ptr %ptrptr, align 8
48+
; CHECK: %1 = ptrtoint ptr %ptr to i64
49+
; CHECK: %2 = add i64 %1, 1
50+
; CHECK: %3 = call i64 @llvm.fshr.i64(i64 %2, i64 %2, i64 16)
51+
; CHECK: %4 = inttoptr i64 %3 to ptr
52+
; CHECK: ret ptr %4
53+
%protptrptr = call ptr @llvm.protected.field.ptr(ptr %ptrptr, i64 1, i1 false) [ "deactivation-symbol"(ptr @ds3) ]
54+
%ptr = load ptr, ptr %protptrptr
55+
ret ptr %ptr
56+
}
57+
58+
; CHECK: define void @store_sw
59+
define void @store_sw(ptr %ptrptr, ptr %ptr) {
60+
; CHECK: %1 = ptrtoint ptr %ptr to i64
61+
; CHECK: %2 = call i64 @llvm.fshl.i64(i64 %1, i64 %1, i64 16)
62+
; CHECK: %3 = sub i64 %2, 2
63+
; CHECK: %4 = inttoptr i64 %3 to ptr
64+
; CHECK: store ptr %4, ptr %ptrptr, align 8
65+
; CHECK: ret void
66+
%protptrptr = call ptr @llvm.protected.field.ptr(ptr %ptrptr, i64 2, i1 false) [ "deactivation-symbol"(ptr @ds4) ]
67+
store ptr %ptr, ptr %protptrptr
68+
ret void
69+
}
70+
71+
; CHECK: define i1 @compare
72+
define i1 @compare(ptr %ptrptr) {
73+
%protptrptr = call ptr @llvm.protected.field.ptr(ptr %ptrptr, i64 3, i1 true) [ "deactivation-symbol"(ptr @ds5) ]
74+
; CHECK: icmp eq ptr %ptrptr, null
75+
%cmp1 = icmp eq ptr %protptrptr, null
76+
; CHECK: icmp eq ptr null, %ptrptr
77+
%cmp2 = icmp eq ptr null, %protptrptr
78+
%cmp = or i1 %cmp1, %cmp2
79+
ret i1 %cmp
80+
}
81+
82+
; CHECK: define ptr @escape
83+
define ptr @escape(ptr %ptrptr) {
4184
; CHECK: ret ptr %ptrptr
42-
%protptrptr = call ptr @llvm.protected.field.ptr(ptr %ptrptr, i64 3, i1 true) [ "deactivation-symbol"(ptr @ds3) ]
85+
%protptrptr = call ptr @llvm.protected.field.ptr(ptr %ptrptr, i64 3, i1 true) [ "deactivation-symbol"(ptr @ds6) ]
4386
ret ptr %protptrptr
4487
}
4588

0 commit comments

Comments
 (0)