Skip to content

Commit d3b5bfd

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. Pull Request: llvm#133537
1 parent cf7a27a commit d3b5bfd

File tree

19 files changed

+155
-43
lines changed

19 files changed

+155
-43
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ 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,
441+
// deactivation_symbol]
440442
};
441443

442444
/// 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
@@ -1033,10 +1033,10 @@ class ConstantPtrAuth final : public Constant {
10331033
friend struct ConstantPtrAuthKeyType;
10341034
friend class Constant;
10351035

1036-
constexpr static IntrusiveOperandsAllocMarker AllocMarker{4};
1036+
constexpr static IntrusiveOperandsAllocMarker AllocMarker{5};
10371037

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

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

@@ -1046,7 +1046,8 @@ class ConstantPtrAuth final : public Constant {
10461046
public:
10471047
/// Return a pointer signed with the specified parameters.
10481048
LLVM_ABI static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
1049-
ConstantInt *Disc, Constant *AddrDisc);
1049+
ConstantInt *Disc, Constant *AddrDisc,
1050+
Constant *DeactivationSymbol);
10501051

10511052
/// Produce a new ptrauth expression signing the given value using
10521053
/// the same schema as is stored in one.
@@ -1078,6 +1079,10 @@ class ConstantPtrAuth final : public Constant {
10781079
return !getAddrDiscriminator()->isNullValue();
10791080
}
10801081

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

11071112
template <>
11081113
struct OperandTraits<ConstantPtrAuth>
1109-
: public FixedNumOperandTraits<ConstantPtrAuth, 4> {};
1114+
: public FixedNumOperandTraits<ConstantPtrAuth, 5> {};
11101115

11111116
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPtrAuth, Constant)
11121117

llvm/include/llvm/SandboxIR/Constant.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1363,7 +1363,8 @@ class ConstantPtrAuth final : public Constant {
13631363
public:
13641364
/// Return a pointer signed with the specified parameters.
13651365
LLVM_ABI static ConstantPtrAuth *get(Constant *Ptr, ConstantInt *Key,
1366-
ConstantInt *Disc, Constant *AddrDisc);
1366+
ConstantInt *Disc, Constant *AddrDisc,
1367+
Constant *DeactivationSymbol);
13671368
/// The pointer that is signed in this ptrauth signed pointer.
13681369
LLVM_ABI Constant *getPointer() const;
13691370

@@ -1378,6 +1379,8 @@ class ConstantPtrAuth final : public Constant {
13781379
/// the only global-initializer user of the ptrauth signed pointer.
13791380
LLVM_ABI Constant *getAddrDiscriminator() const;
13801381

1382+
Constant *getDeactivationSymbol() const;
1383+
13811384
/// Whether there is any non-null address discriminator.
13821385
bool hasAddressDiscriminator() const {
13831386
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
@@ -4216,11 +4216,12 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42164216
}
42174217
case lltok::kw_ptrauth: {
42184218
// ValID ::= 'ptrauth' '(' ptr @foo ',' i32 <key>
4219-
// (',' i64 <disc> (',' ptr addrdisc)? )? ')'
4219+
// (',' i64 <disc> (',' ptr addrdisc (',' ptr ds)? )? )? ')'
42204220
Lex.Lex();
42214221

42224222
Constant *Ptr, *Key;
4223-
Constant *Disc = nullptr, *AddrDisc = nullptr;
4223+
Constant *Disc = nullptr, *AddrDisc = nullptr,
4224+
*DeactivationSymbol = nullptr;
42244225

42254226
if (parseToken(lltok::lparen,
42264227
"expected '(' in constant ptrauth expression") ||
@@ -4229,11 +4230,14 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42294230
"expected comma in constant ptrauth expression") ||
42304231
parseGlobalTypeAndValue(Key))
42314232
return true;
4232-
// If present, parse the optional disc/addrdisc.
4233-
if (EatIfPresent(lltok::comma))
4234-
if (parseGlobalTypeAndValue(Disc) ||
4235-
(EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(AddrDisc)))
4236-
return true;
4233+
// If present, parse the optional disc/addrdisc/ds.
4234+
if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(Disc))
4235+
return true;
4236+
if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(AddrDisc))
4237+
return true;
4238+
if (EatIfPresent(lltok::comma) &&
4239+
parseGlobalTypeAndValue(DeactivationSymbol))
4240+
return true;
42374241
if (parseToken(lltok::rparen,
42384242
"expected ')' in constant ptrauth expression"))
42394243
return true;
@@ -4264,7 +4268,16 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42644268
AddrDisc = ConstantPointerNull::get(PointerType::get(Context, 0));
42654269
}
42664270

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

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

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

1611-
C = ConstantPtrAuth::get(ConstOps[0], Key, Disc, ConstOps[3]);
1611+
auto *DeactivationSymbol =
1612+
ConstOps.size() > 4 ? ConstOps[4]
1613+
: ConstantPointerNull::get(cast<PointerType>(
1614+
ConstOps[3]->getType()));
1615+
1616+
C = ConstantPtrAuth::get(ConstOps[0], Key, Disc, ConstOps[3],
1617+
DeactivationSymbol);
16121618
break;
16131619
}
16141620
case BitcodeConstant::NoCFIOpcode: {
@@ -3808,6 +3814,16 @@ Error BitcodeReader::parseConstants() {
38083814
(unsigned)Record[2], (unsigned)Record[3]});
38093815
break;
38103816
}
3817+
case bitc::CST_CODE_PTRAUTH2: {
3818+
if (Record.size() < 4)
3819+
return error("Invalid ptrauth record");
3820+
// Ptr, Key, Disc, AddrDisc, DeactivationSymbol
3821+
V = BitcodeConstant::create(
3822+
Alloc, CurTy, BitcodeConstant::ConstantPtrAuthOpcode,
3823+
{(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2],
3824+
(unsigned)Record[3], (unsigned)Record[4]});
3825+
break;
3826+
}
38113827
}
38123828

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

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3010,11 +3010,12 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
30103010
Record.push_back(VE.getTypeID(NC->getGlobalValue()->getType()));
30113011
Record.push_back(VE.getValueID(NC->getGlobalValue()));
30123012
} else if (const auto *CPA = dyn_cast<ConstantPtrAuth>(C)) {
3013-
Code = bitc::CST_CODE_PTRAUTH;
3013+
Code = bitc::CST_CODE_PTRAUTH2;
30143014
Record.push_back(VE.getValueID(CPA->getPointer()));
30153015
Record.push_back(VE.getValueID(CPA->getKey()));
30163016
Record.push_back(VE.getValueID(CPA->getDiscriminator()));
30173017
Record.push_back(VE.getValueID(CPA->getAddrDiscriminator()));
3018+
Record.push_back(VE.getValueID(CPA->getDeactivationSymbol()));
30183019
} else {
30193020
#ifndef NDEBUG
30203021
C->dump();

llvm/lib/IR/AsmWriter.cpp

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

1670-
// ptrauth (ptr CST, i32 KEY[, i64 DISC[, ptr ADDRDISC]?]?)
1670+
// ptrauth (ptr CST, i32 KEY[, i64 DISC[, ptr ADDRDISC[, ptr DS]?]?]?)
16711671
unsigned NumOpsToWrite = 2;
16721672
if (!CPA->getOperand(2)->isNullValue())
16731673
NumOpsToWrite = 3;
16741674
if (!CPA->getOperand(3)->isNullValue())
16751675
NumOpsToWrite = 4;
1676+
if (!CPA->getOperand(4)->isNullValue())
1677+
NumOpsToWrite = 5;
16761678

16771679
ListSeparator LS;
16781680
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
@@ -2060,19 +2060,22 @@ Value *NoCFIValue::handleOperandChangeImpl(Value *From, Value *To) {
20602060
//
20612061

20622062
ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key,
2063-
ConstantInt *Disc, Constant *AddrDisc) {
2064-
Constant *ArgVec[] = {Ptr, Key, Disc, AddrDisc};
2063+
ConstantInt *Disc, Constant *AddrDisc,
2064+
Constant *DeactivationSymbol) {
2065+
Constant *ArgVec[] = {Ptr, Key, Disc, AddrDisc, DeactivationSymbol};
20652066
ConstantPtrAuthKeyType MapKey(ArgVec);
20662067
LLVMContextImpl *pImpl = Ptr->getContext().pImpl;
20672068
return pImpl->ConstantPtrAuths.getOrCreate(Ptr->getType(), MapKey);
20682069
}
20692070

20702071
ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const {
2071-
return get(Pointer, getKey(), getDiscriminator(), getAddrDiscriminator());
2072+
return get(Pointer, getKey(), getDiscriminator(), getAddrDiscriminator(),
2073+
getDeactivationSymbol());
20722074
}
20732075

20742076
ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key,
2075-
ConstantInt *Disc, Constant *AddrDisc)
2077+
ConstantInt *Disc, Constant *AddrDisc,
2078+
Constant *DeactivationSymbol)
20762079
: Constant(Ptr->getType(), Value::ConstantPtrAuthVal, AllocMarker) {
20772080
assert(Ptr->getType()->isPointerTy());
20782081
assert(Key->getBitWidth() == 32);
@@ -2082,6 +2085,7 @@ ConstantPtrAuth::ConstantPtrAuth(Constant *Ptr, ConstantInt *Key,
20822085
setOperand(1, Key);
20832086
setOperand(2, Disc);
20842087
setOperand(3, AddrDisc);
2088+
setOperand(4, DeactivationSymbol);
20852089
}
20862090

20872091
/// 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

0 commit comments

Comments
 (0)