Skip to content

Commit 23e0789

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 be7ef9a commit 23e0789

File tree

16 files changed

+127
-42
lines changed

16 files changed

+127
-42
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
@@ -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
@@ -4213,11 +4213,12 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42134213
}
42144214
case lltok::kw_ptrauth: {
42154215
// ValID ::= 'ptrauth' '(' ptr @foo ',' i32 <key>
4216-
// (',' i64 <disc> (',' ptr addrdisc)? )? ')'
4216+
// (',' i64 <disc> (',' ptr addrdisc (',' ptr ds)? )? )? ')'
42174217
Lex.Lex();
42184218

42194219
Constant *Ptr, *Key;
4220-
Constant *Disc = nullptr, *AddrDisc = nullptr;
4220+
Constant *Disc = nullptr, *AddrDisc = nullptr,
4221+
*DeactivationSymbol = nullptr;
42214222

42224223
if (parseToken(lltok::lparen,
42234224
"expected '(' in constant ptrauth expression") ||
@@ -4226,11 +4227,14 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42264227
"expected comma in constant ptrauth expression") ||
42274228
parseGlobalTypeAndValue(Key))
42284229
return true;
4229-
// If present, parse the optional disc/addrdisc.
4230-
if (EatIfPresent(lltok::comma))
4231-
if (parseGlobalTypeAndValue(Disc) ||
4232-
(EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(AddrDisc)))
4233-
return true;
4230+
// If present, parse the optional disc/addrdisc/ds.
4231+
if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(Disc))
4232+
return true;
4233+
if (EatIfPresent(lltok::comma) && parseGlobalTypeAndValue(AddrDisc))
4234+
return true;
4235+
if (EatIfPresent(lltok::comma) &&
4236+
parseGlobalTypeAndValue(DeactivationSymbol))
4237+
return true;
42344238
if (parseToken(lltok::rparen,
42354239
"expected ')' in constant ptrauth expression"))
42364240
return true;
@@ -4261,7 +4265,16 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) {
42614265
AddrDisc = ConstantPointerNull::get(PointerType::get(Context, 0));
42624266
}
42634267

4264-
ID.ConstantVal = ConstantPtrAuth::get(Ptr, KeyC, DiscC, AddrDisc);
4268+
if (DeactivationSymbol) {
4269+
if (!DeactivationSymbol->getType()->isPointerTy())
4270+
return error(
4271+
ID.Loc, "constant ptrauth deactivation symbol must be a pointer");
4272+
} else {
4273+
DeactivationSymbol = ConstantPointerNull::get(PointerType::get(Context, 0));
4274+
}
4275+
4276+
ID.ConstantVal =
4277+
ConstantPtrAuth::get(Ptr, KeyC, DiscC, AddrDisc, DeactivationSymbol);
42654278
ID.Kind = ValID::t_Constant;
42664279
return false;
42674280
}

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

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

1603-
C = ConstantPtrAuth::get(ConstOps[0], Key, Disc, ConstOps[3]);
1603+
auto *DeactivationSymbol =
1604+
ConstOps.size() > 4 ? ConstOps[4]
1605+
: ConstantPointerNull::get(cast<PointerType>(
1606+
ConstOps[3]->getType()));
1607+
1608+
C = ConstantPtrAuth::get(ConstOps[0], Key, Disc, ConstOps[3],
1609+
DeactivationSymbol);
16041610
break;
16051611
}
16061612
case BitcodeConstant::NoCFIOpcode: {
@@ -3800,6 +3806,16 @@ Error BitcodeReader::parseConstants() {
38003806
(unsigned)Record[2], (unsigned)Record[3]});
38013807
break;
38023808
}
3809+
case bitc::CST_CODE_PTRAUTH2: {
3810+
if (Record.size() < 4)
3811+
return error("Invalid ptrauth record");
3812+
// Ptr, Key, Disc, AddrDisc, DeactivationSymbol
3813+
V = BitcodeConstant::create(
3814+
Alloc, CurTy, BitcodeConstant::ConstantPtrAuthOpcode,
3815+
{(unsigned)Record[0], (unsigned)Record[1], (unsigned)Record[2],
3816+
(unsigned)Record[3], (unsigned)Record[4]});
3817+
break;
3818+
}
38033819
}
38043820

38053821
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
@@ -1664,12 +1664,14 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
16641664
if (const ConstantPtrAuth *CPA = dyn_cast<ConstantPtrAuth>(CV)) {
16651665
Out << "ptrauth (";
16661666

1667-
// ptrauth (ptr CST, i32 KEY[, i64 DISC[, ptr ADDRDISC]?]?)
1667+
// ptrauth (ptr CST, i32 KEY[, i64 DISC[, ptr ADDRDISC[, ptr DS]?]?]?)
16681668
unsigned NumOpsToWrite = 2;
16691669
if (!CPA->getOperand(2)->isNullValue())
16701670
NumOpsToWrite = 3;
16711671
if (!CPA->getOperand(3)->isNullValue())
16721672
NumOpsToWrite = 4;
1673+
if (!CPA->getOperand(4)->isNullValue())
1674+
NumOpsToWrite = 5;
16731675

16741676
ListSeparator LS;
16751677
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

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)