Skip to content

Commit e67962a

Browse files
committed
Add deactivation symbol operand to ConstantPtrAuth.
Deactivation symbol operands are supported in the code generator by building on the previously added support for IRELATIVE relocations. TODO: - Fix broken test. - Add bitcode and IR writer support. - Add tests. Pull Request: llvm#133537
1 parent f87901d commit e67962a

File tree

16 files changed

+126
-41
lines changed

16 files changed

+126
-41
lines changed

clang/lib/CodeGen/CGPointerAuth.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,9 @@ CodeGenModule::getConstantSignedPointer(llvm::Constant *Pointer, unsigned Key,
440440
IntegerDiscriminator = llvm::ConstantInt::get(Int64Ty, 0);
441441
}
442442

443-
return llvm::ConstantPtrAuth::get(Pointer,
444-
llvm::ConstantInt::get(Int32Ty, Key),
445-
IntegerDiscriminator, AddressDiscriminator);
443+
return llvm::ConstantPtrAuth::get(
444+
Pointer, llvm::ConstantInt::get(Int32Ty, Key), IntegerDiscriminator,
445+
AddressDiscriminator, llvm::Constant::getNullValue(UnqualPtrTy));
446446
}
447447

448448
/// Does a given PointerAuthScheme require us to sign a value

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ enum ConstantsCodes {
437437
CST_CODE_CE_GEP_WITH_INRANGE = 31, // [opty, flags, range, n x operands]
438438
CST_CODE_CE_GEP = 32, // [opty, flags, n x operands]
439439
CST_CODE_PTRAUTH = 33, // [ptr, key, disc, addrdisc]
440+
CST_CODE_PTRAUTH2 = 34, // [ptr, key, disc, addrdisc, DeactivationSymbol]
440441
};
441442

442443
/// CastOpcodes - These are values used in the bitcode files to encode which

llvm/include/llvm/IR/Constants.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,10 +1031,10 @@ class ConstantPtrAuth final : public Constant {
10311031
friend struct ConstantPtrAuthKeyType;
10321032
friend class Constant;
10331033

1034-
constexpr static IntrusiveOperandsAllocMarker AllocMarker{4};
1034+
constexpr static IntrusiveOperandsAllocMarker AllocMarker{5};
10351035

10361036
ConstantPtrAuth(Constant *Ptr, ConstantInt *Key, ConstantInt *Disc,
1037-
Constant *AddrDisc);
1037+
Constant *AddrDisc, Constant *DeactivationSymbol);
10381038

10391039
void *operator new(size_t s) { return User::operator new(s, AllocMarker); }
10401040

@@ -1044,7 +1044,8 @@ class ConstantPtrAuth final : public Constant {
10441044
public:
10451045
/// Return a pointer signed with the specified parameters.
10461046
static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
1047-
ConstantInt *Disc, Constant *AddrDisc);
1047+
ConstantInt *Disc, Constant *AddrDisc,
1048+
Constant *DeactivationSymbol);
10481049

10491050
/// Produce a new ptrauth expression signing the given value using
10501051
/// the same schema as is stored in one.
@@ -1076,6 +1077,10 @@ class ConstantPtrAuth final : public Constant {
10761077
return !getAddrDiscriminator()->isNullValue();
10771078
}
10781079

1080+
Constant *getDeactivationSymbol() const {
1081+
return cast<Constant>(Op<4>().get());
1082+
}
1083+
10791084
/// A constant value for the address discriminator which has special
10801085
/// significance to ctors/dtors lowering. Regular address discrimination can't
10811086
/// be applied for them since uses of llvm.global_{c|d}tors are disallowed
@@ -1103,7 +1108,7 @@ class ConstantPtrAuth final : public Constant {
11031108

11041109
template <>
11051110
struct OperandTraits<ConstantPtrAuth>
1106-
: public FixedNumOperandTraits<ConstantPtrAuth, 4> {};
1111+
: public FixedNumOperandTraits<ConstantPtrAuth, 5> {};
11071112

11081113
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPtrAuth, Constant)
11091114

llvm/include/llvm/SandboxIR/Constant.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1384,7 +1384,8 @@ class ConstantPtrAuth final : public Constant {
13841384
public:
13851385
/// Return a pointer signed with the specified parameters.
13861386
static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
1387-
ConstantInt *Disc, Constant *AddrDisc);
1387+
ConstantInt *Disc, Constant *AddrDisc,
1388+
Constant *DeactivationSymbol);
13881389
/// The pointer that is signed in this ptrauth signed pointer.
13891390
Constant *getPointer() const;
13901391

@@ -1399,6 +1400,8 @@ class ConstantPtrAuth final : public Constant {
13991400
/// the only global-initializer user of the ptrauth signed pointer.
14001401
Constant *getAddrDiscriminator() const;
14011402

1403+
Constant *getDeactivationSymbol() const;
1404+
14021405
/// Whether there is any non-null address discriminator.
14031406
bool hasAddressDiscriminator() const {
14041407
return cast<llvm::ConstantPtrAuth>(Val)->hasAddressDiscriminator();

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,11 +4218,12 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42184218
}
42194219
case lltok::kw_ptrauth: {
42204220
// ValID ::= 'ptrauth' '(' ptr @foo ',' i32 <key>
4221-
// (',' i64 <disc> (',' ptr addrdisc)? )? ')'
4221+
// (',' i64 <disc> (',' ptr addrdisc (',' ptr ds)? )? )? ')'
42224222
Lex.Lex();
42234223

42244224
Constant *Ptr, *Key;
4225-
Constant *Disc = nullptr, *AddrDisc = nullptr;
4225+
Constant *Disc = nullptr, *AddrDisc = nullptr,
4226+
*DeactivationSymbol = nullptr;
42264227

42274228
if (parseToken(lltok::lparen,
42284229
"expected '(' in constant ptrauth expression") ||
@@ -4231,11 +4232,14 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42314232
"expected comma in constant ptrauth expression") ||
42324233
parseGlobalTypeAndValue(Key))
42334234
return true;
4234-
// If present, parse the optional disc/addrdisc.
4235-
if (EatIfPresent(lltok::comma))
4236-
if (parseGlobalTypeAndValue(Disc) ||
4237-
(EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(AddrDisc)))
4238-
return true;
4235+
// If present, parse the optional disc/addrdisc/ds.
4236+
if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(Disc))
4237+
return true;
4238+
if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(AddrDisc))
4239+
return true;
4240+
if (EatIfPresent(lltok::comma) &&
4241+
parseGlobalTypeAndValue(DeactivationSymbol))
4242+
return true;
42394243
if (parseToken(lltok::rparen,
42404244
"expected ')' in constant ptrauth expression"))
42414245
return true;
@@ -4266,7 +4270,16 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42664270
AddrDisc = ConstantPointerNull::get(PointerType::get(Context, 0));
42674271
}
42684272

4269-
ID.ConstantVal = ConstantPtrAuth::get(Ptr, KeyC, DiscC, AddrDisc);
4273+
if (DeactivationSymbol) {
4274+
if (!DeactivationSymbol->getType()->isPointerTy())
4275+
return error(
4276+
ID.Loc, "constant ptrauth deactivation symbol must be a pointer");
4277+
} else {
4278+
DeactivationSymbol = ConstantPointerNull::get(PointerType::get(Context, 0));
4279+
}
4280+
4281+
ID.ConstantVal =
4282+
ConstantPtrAuth::get(Ptr, KeyC, DiscC, AddrDisc, DeactivationSymbol);
42704283
ID.Kind = ValID::t_Constant;
42714284
return false;
42724285
}

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,13 @@ Expected<Value *> BitcodeReader::materializeValue(unsigned StartValID,
16031603
if (!Disc)
16041604
return error("ptrauth disc operand must be ConstantInt");
16051605

1606-
C = ConstantPtrAuth::get(ConstOps[0], Key, Disc, ConstOps[3]);
1606+
auto *DeactivationSymbol =
1607+
ConstOps.size() > 4 ? ConstOps[4]
1608+
: ConstantPointerNull::get(cast<PointerType>(
1609+
ConstOps[3]->getType()));
1610+
1611+
C = ConstantPtrAuth::get(ConstOps[0], Key, Disc, ConstOps[3],
1612+
DeactivationSymbol);
16071613
break;
16081614
}
16091615
case BitcodeConstant::NoCFIOpcode: {
@@ -3801,6 +3807,16 @@ Error BitcodeReader::parseConstants() {
38013807
(unsigned)Record[2], (unsigned)Record[3]});
38023808
break;
38033809
}
3810+
case bitc::CST_CODE_PTRAUTH2: {
3811+
if (Record.size() < 4)
3812+
return error("Invalid ptrauth record");
3813+
// Ptr, Key, Disc, AddrDisc, DeactivationSymbol
3814+
V = BitcodeConstant::create(
3815+
Alloc, CurTy, BitcodeConstant::ConstantPtrAuthOpcode,
3816+
{(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2],
3817+
(unsigned)Record[3], (unsigned)Record[4]});
3818+
break;
3819+
}
38043820
}
38053821

38063822
assert(V->getType() == getTypeByID(CurTyID) && "Incorrect result type ID");

llvm/lib/IR/AsmWriter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,12 +1658,14 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
16581658
if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(CV)) {
16591659
Out << "ptrauth (";
16601660

1661-
// ptrauth (ptr CST, i32 KEY[, i64 DISC[, ptr ADDRDISC]?]?)
1661+
// ptrauth (ptr CST, i32 KEY[, i64 DISC[, ptr ADDRDISC[, ptr DS]?]?]?)
16621662
unsigned NumOpsToWrite = 2;
16631663
if (!CPA->getOperand(2)->isNullValue())
16641664
NumOpsToWrite = 3;
16651665
if (!CPA->getOperand(3)->isNullValue())
16661666
NumOpsToWrite = 4;
1667+
if (!CPA->getOperand(4)->isNullValue())
1668+
NumOpsToWrite = 5;
16671669

16681670
ListSeparator LS;
16691671
for (unsigned i = 0, e = NumOpsToWrite; i != e; ++i) {

llvm/lib/IR/Constants.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,19 +2056,22 @@ Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) {
20562056
//
20572057

20582058
ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key,
2059-
ConstantInt *Disc, Constant *AddrDisc) {
2060-
Constant *ArgVec[] = {Ptr, Key, Disc, AddrDisc};
2059+
ConstantInt *Disc, Constant *AddrDisc,
2060+
Constant *DeactivationSymbol) {
2061+
Constant *ArgVec[] = {Ptr, Key, Disc, AddrDisc, DeactivationSymbol};
20612062
ConstantPtrAuthKeyType MapKey(ArgVec);
20622063
LLVMContextImpl *pImpl = Ptr->getContext().pImpl;
20632064
return pImpl->ConstantPtrAuths.getOrCreate(Ptr->getType(), MapKey);
20642065
}
20652066

20662067
ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const {
2067-
return get(Pointer, getKey(), getDiscriminator(), getAddrDiscriminator());
2068+
return get(Pointer, getKey(), getDiscriminator(), getAddrDiscriminator(),
2069+
getDeactivationSymbol());
20682070
}
20692071

20702072
ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key,
2071-
ConstantInt *Disc, Constant *AddrDisc)
2073+
ConstantInt *Disc, Constant *AddrDisc,
2074+
Constant *DeactivationSymbol)
20722075
: Constant(Ptr->getType(), Value::ConstantPtrAuthVal, AllocMarker) {
20732076
assert(Ptr->getType()->isPointerTy());
20742077
assert(Key->getBitWidth() == 32);
@@ -2078,6 +2081,7 @@ ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key,
20782081
setOperand(1, Key);
20792082
setOperand(2, Disc);
20802083
setOperand(3, AddrDisc);
2084+
setOperand(4, DeactivationSymbol);
20812085
}
20822086

20832087
/// Remove the constant from the constant table.

llvm/lib/IR/ConstantsContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ struct ConstantPtrAuthKeyType {
539539

540540
ConstantPtrAuth *create(TypeClass *Ty) const {
541541
return new ConstantPtrAuth(Operands[0], cast<ConstantInt>(Operands[1]),
542-
cast<ConstantInt>(Operands[2]), Operands[3]);
542+
cast<ConstantInt>(Operands[2]), Operands[3],
543+
Operands[4]);
543544
}
544545
};
545546

llvm/lib/IR/Core.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,9 @@ LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,
16991699
LLVMValueRef Disc, LLVMValueRef AddrDisc) {
17001700
return wrap(ConstantPtrAuth::get(
17011701
unwrap<Constant>(Ptr), unwrap<ConstantInt>(Key),
1702-
unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc)));
1702+
unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc),
1703+
ConstantPointerNull::get(
1704+
cast<PointerType>(unwrap<Constant>(AddrDisc)->getType()))));
17031705
}
17041706

17051707
/*-- Opcode mapping */

0 commit comments

Comments
 (0)