19#include "llvm/Analysis/ValueTracking.h"
20#include "llvm/Support/SipHash.h"
34 assert(!
Type.isNull() &&
"type not provided for type-discriminated schema");
35 return llvm::ConstantInt::get(
39 assert(
Decl.getDecl() &&
40 "declaration not provided for decl-discriminated schema");
41 return llvm::ConstantInt::get(
IntPtrTy,
47 llvm_unreachable(
"bad discrimination kind");
63 uint16_t &EntityHash = PtrAuthDiscriminatorHashes[
Declaration];
65 if (EntityHash == 0) {
67 if (ND->hasAttr<AsmLabelAttr>() &&
68 ND->getAttr<AsmLabelAttr>()->getLabel().starts_with(
75 llvm::raw_svector_ostream Out(Buffer);
77 EntityHash = llvm::getPointerAuthStableSipHash(Out.str());
80 EntityHash = llvm::getPointerAuthStableSipHash(Name);
94 assert(!Schema.isAddressDiscriminated() &&
95 "function pointers cannot use address-specific discrimination");
97 llvm::Constant *Discriminator =
nullptr;
98 if (T->isFunctionPointerType() || T->isFunctionReferenceType())
99 T = T->getPointeeType();
100 if (T->isFunctionType())
110 llvm::Value *Discriminator) {
112 auto Intrinsic =
CGM.getIntrinsic(llvm::Intrinsic::ptrauth_blend);
113 return Builder.CreateCall(Intrinsic, {StorageAddress, Discriminator});
124 llvm::Value *Discriminator =
125 CGM.getPointerAuthOtherDiscriminator(Schema, SchemaDecl, SchemaType);
128 assert(StorageAddress &&
129 "address not provided for address-discriminated schema");
146 assert(Qual &&
"don't call this if you don't know that the Qual is present");
150 llvm::Value *Discriminator =
nullptr;
155 assert(StorageAddress.
isValid() &&
156 "address discrimination without address");
186 return ::getPointerAuthInfoForPointeeType(*
this, T);
201 if (PointeeType.isNull())
204 return ::getPointerAuthInfoForPointeeType(CGM, PointeeType);
208 return ::getPointerAuthInfoForType(*
this, T);
211static std::pair<llvm::Value *, CGPointerAuthInfo>
220 return {
Value, AuthInfo};
225std::pair<llvm::Value *, CGPointerAuthInfo>
230 if (
const auto *Load = dyn_cast<ImplicitCastExpr>(E)) {
231 if (Load->getCastKind() == CK_LValueToRValue) {
232 E = Load->getSubExpr()->IgnoreParens();
235 if (
const auto *RefExpr = dyn_cast<DeclRefExpr>(E)) {
238 if (!
Result.isReference())
239 return {
Result.getValue(),
263 assert(DestQualifier);
275 assert(DestQualifier);
286 Address CurStorageAddress,
bool IsKnownNonNull) {
287 assert(CurQualifier);
297 if (
const auto *CI = dyn_cast<llvm::ConstantInt>(
Value))
304 assert((Left.isSigned() || Right.isSigned()) &&
305 "shouldn't be called if neither is signed");
306 if (Left.isSigned() != Right.isSigned())
308 return Left.getKey() == Right.getKey() &&
309 Left.getAuthenticationMode() == Right.getAuthenticationMode() &&
310 Left.isIsaPointer() == Right.isIsaPointer() &&
311 Left.authenticatesNullValues() == Right.authenticatesNullValues() &&
312 Left.getDiscriminator() == Right.getDiscriminator();
319 return Discriminator ? Discriminator : Builder.getSize(0);
326 assert(CurAuth && NewAuth);
348 auto *Intrinsic =
CGM.getIntrinsic(llvm::Intrinsic::ptrauth_resign);
350 Intrinsic, {
Value, CurKey, CurDiscriminator, NewKey, NewDiscriminator});
361 if (!CurAuthInfo && !NewAuthInfo)
364 llvm::Value *Null =
nullptr;
366 if (
auto *PointerValue = dyn_cast<llvm::PointerType>(
Value->
getType())) {
367 Null =
CGM.getNullPointer(PointerValue,
Type);
370 Null = llvm::ConstantInt::get(
IntPtrTy, 0);
387 llvm::BasicBlock *InitBB =
Builder.GetInsertBlock();
388 llvm::BasicBlock *ResignBB =
nullptr, *ContBB =
nullptr;
392 if (!IsKnownNonNull && !llvm::isKnownNonZero(
Value,
CGM.getDataLayout())) {
397 Builder.CreateCondBr(IsNonNull, ResignBB, ContBB);
404 else if (!CurAuthInfo)
413 Phi->addIncoming(Null, InitBB);
414 Phi->addIncoming(
Value, ResignBB);
440 llvm::Constant *StorageAddress,
441 llvm::ConstantInt *OtherDiscriminator) {
442 llvm::Constant *AddressDiscriminator;
443 if (StorageAddress) {
445 AddressDiscriminator = StorageAddress;
447 AddressDiscriminator = llvm::Constant::getNullValue(
DefaultPtrTy);
450 llvm::ConstantInt *IntegerDiscriminator;
451 if (OtherDiscriminator) {
452 assert(OtherDiscriminator->getType() ==
Int64Ty);
453 IntegerDiscriminator = OtherDiscriminator;
455 IntegerDiscriminator = llvm::ConstantInt::get(
Int64Ty, 0);
458 return llvm::ConstantPtrAuth::get(
459 Pointer, llvm::ConstantInt::get(
Int32Ty, Key), IntegerDiscriminator,
460 AddressDiscriminator,
475 llvm::Constant *StorageAddress,
GlobalDecl SchemaDecl,
478 llvm::ConstantInt *OtherDiscriminator =
487 unsigned Key, llvm::Constant *StorageAddress,
488 llvm::ConstantInt *OtherDiscriminator) {
503 Pointer, PointerAuth.getKey(),
nullptr,
504 cast_or_null<llvm::ConstantInt>(PointerAuth.getDiscriminator()));
516 if (!FD->hasPrototype())
518 FuncType = Context.getFunctionNoProtoType(Proto->getReturnType(),
519 Proto->getExtInfo());
530 assert(!Schema.isAddressDiscriminated() &&
531 "function pointers cannot use address-specific discrimination");
533 llvm::ConstantInt *Discriminator =
537 false, Discriminator);
544 Pointer, PointerAuth.getKey(),
nullptr,
545 cast_or_null<llvm::ConstantInt>(PointerAuth.getDiscriminator()));
547 if (
const auto *MFT = dyn_cast<MemberPointerType>(FT.
getTypePtr())) {
548 if (MFT->hasPointeeToCFIUncheckedCalleeFunctionType())
563std::optional<PointerAuthQualifier>
564CodeGenModule::computeVTPointerAuthentication(
const CXXRecordDecl *ThisClass) {
566 if (!DefaultAuthentication)
569 Context.baseForVTableAuthentication(ThisClass);
571 unsigned Key = DefaultAuthentication.getKey();
572 bool AddressDiscriminated = DefaultAuthentication.isAddressDiscriminated();
573 auto DefaultDiscrimination = DefaultAuthentication.getOtherDiscrimination();
574 unsigned TypeBasedDiscriminator =
575 Context.getPointerAuthVTablePointerDiscriminator(PrimaryBase);
576 unsigned Discriminator;
578 Discriminator = TypeBasedDiscriminator;
579 }
else if (DefaultDiscrimination ==
581 Discriminator = DefaultAuthentication.getConstantDiscrimination();
586 if (
auto ExplicitAuthentication =
587 PrimaryBase->
getAttr<VTablePointerAuthenticationAttr>()) {
588 auto ExplicitAddressDiscrimination =
589 ExplicitAuthentication->getAddressDiscrimination();
590 auto ExplicitDiscriminator =
591 ExplicitAuthentication->getExtraDiscrimination();
593 unsigned ExplicitKey = ExplicitAuthentication->getKey();
594 if (ExplicitKey == VTablePointerAuthenticationAttr::NoKey)
597 if (ExplicitKey != VTablePointerAuthenticationAttr::DefaultKey) {
598 if (ExplicitKey == VTablePointerAuthenticationAttr::ProcessIndependent)
601 assert(ExplicitKey ==
602 VTablePointerAuthenticationAttr::ProcessDependent);
607 if (ExplicitAddressDiscrimination !=
608 VTablePointerAuthenticationAttr::DefaultAddressDiscrimination)
609 AddressDiscriminated =
610 ExplicitAddressDiscrimination ==
611 VTablePointerAuthenticationAttr::AddressDiscrimination;
613 if (ExplicitDiscriminator ==
614 VTablePointerAuthenticationAttr::TypeDiscrimination)
615 Discriminator = TypeBasedDiscriminator;
616 else if (ExplicitDiscriminator ==
617 VTablePointerAuthenticationAttr::CustomDiscrimination)
618 Discriminator = ExplicitAuthentication->getCustomDiscriminationValue();
619 else if (ExplicitDiscriminator ==
620 VTablePointerAuthenticationAttr::NoExtraDiscrimination)
629std::optional<PointerAuthQualifier>
631 if (!
Record->getDefinition() || !
Record->isPolymorphic())
634 auto Existing = VTablePtrAuthInfos.find(
Record);
635 std::optional<PointerAuthQualifier> Authentication;
636 if (Existing != VTablePtrAuthInfos.end()) {
637 Authentication = Existing->getSecond();
639 Authentication = computeVTPointerAuthentication(
Record);
640 VTablePtrAuthInfos.insert(std::make_pair(
Record, Authentication));
642 return Authentication;
645std::optional<CGPointerAuthInfo>
648 llvm::Value *StorageAddress) {
653 llvm::Value *Discriminator =
nullptr;
654 if (
auto ExtraDiscriminator = Authentication->getExtraDiscriminator())
655 Discriminator = llvm::ConstantInt::get(
IntPtrTy, ExtraDiscriminator);
657 if (Authentication->isAddressDiscriminated()) {
658 assert(StorageAddress &&
659 "address not provided for address-discriminated schema");
670 false, Discriminator);
683 if (!CurAuthInfo && !NewAuthInfo)
689 CurAuthInfo =
CGM.getFunctionPointerAuthInfo(SourceType);
692 NewAuthInfo =
CGM.getFunctionPointerAuthInfo(DestType);
708 if (!CurAuthInfo && !NewAuthInfo)
720 NewAuthInfo =
CGM.getFunctionPointerAuthInfo(DestType);
733 :
CGM.getPointerAuthInfoForPointeeType(PointeeTy);
734 return Addr.getResignedAddress(Info, *
this);
739 assert(
isValid() &&
"pointer isn't valid");
748 assert(ElementType &&
"Effective type has to be set");
749 assert(!Offset &&
"unexpected non-null offset");
780 return Addr.isValid() ?
Addr.emitRawPointer(CGF) :
nullptr;
static std::pair< llvm::Value *, CGPointerAuthInfo > emitLoadOfOrigPointerRValue(CodeGenFunction &CGF, const LValue &LV, SourceLocation Loc)
static bool isZeroConstant(const llvm::Value *Value)
static llvm::Value * getDiscriminatorOrZero(const CGPointerAuthInfo &Info, CGBuilderTy &Builder)
static bool equalAuthPolicies(const CGPointerAuthInfo &Left, const CGPointerAuthInfo &Right)
static CGPointerAuthInfo getPointerAuthInfoForPointeeType(CodeGenModule &CGM, QualType PointeeType)
Return the natural pointer authentication for values of the given pointee type.
static CGPointerAuthInfo getPointerAuthInfoForType(CodeGenModule &CGM, QualType PointerType)
Return the natural pointer authentication for values of the given pointer type.
llvm::MachO::Record Record
QualType getMemberPointerType(QualType T, NestedNameSpecifier Qualifier, const CXXRecordDecl *Cls) const
Return the uniqued reference to the type for a member pointer to the specified type in the specified ...
uint16_t getPointerAuthTypeDiscriminator(QualType T)
Return the "other" type-specific discriminator for the given type.
Represents a C++ struct/union/class.
PointerAuthOptions PointerAuth
Configuration for pointer-signing.
Like RawAddress, an abstract representation of an aligned address, but the pointer contained in this ...
llvm::Value * getBasePointer() const
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
Return the pointer contained in this class after authenticating it and adding offset to it if necessa...
CharUnits getAlignment() const
llvm::Type * getElementType() const
Return the type of the values stored in this address.
void setPointerAuthInfo(const CGPointerAuthInfo &Info)
KnownNonNull_t isKnownNonNull() const
Whether the pointer is known not to be null.
Address getResignedAddress(const CGPointerAuthInfo &NewInfo, CodeGenFunction &CGF) const
const CGPointerAuthInfo & getPointerAuthInfo() const
MangleContext & getMangleContext()
Gets the mangle context.
PointerAuthenticationMode getAuthenticationMode() const
llvm::Value * getDiscriminator() const
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
llvm::Value * EmitPointerAuthQualify(PointerAuthQualifier Qualifier, llvm::Value *Pointer, QualType ValueType, Address StorageAddress, bool IsKnownNonNull)
llvm::Value * getAsNaturalPointerTo(Address Addr, QualType PointeeType)
llvm::Value * emitPointerAuthResignCall(llvm::Value *Pointer, const CGPointerAuthInfo &CurInfo, const CGPointerAuthInfo &NewInfo)
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
bool isPointerKnownNonNull(const Expr *E)
llvm::Value * EmitPointerAuthUnqualify(PointerAuthQualifier Qualifier, llvm::Value *Pointer, QualType PointerType, Address StorageAddress, bool IsKnownNonNull)
@ TCK_Load
Checking the operand of a load. Must be suitably sized and aligned.
llvm::Value * EmitPointerAuthSign(const CGPointerAuthInfo &Info, llvm::Value *Pointer)
Address getAsNaturalAddressOf(Address Addr, QualType PointeeTy)
CGPointerAuthInfo EmitPointerAuthInfo(const PointerAuthSchema &Schema, llvm::Value *StorageAddress, GlobalDecl SchemaDecl, QualType SchemaType)
Emit the concrete pointer authentication informaton for the given authentication schema.
void EmitPointerAuthCopy(PointerAuthQualifier Qualifier, QualType Type, Address DestField, Address SrcField)
llvm::Value * emitPointerAuthResign(llvm::Value *Pointer, QualType PointerType, const CGPointerAuthInfo &CurAuthInfo, const CGPointerAuthInfo &NewAuthInfo, bool IsKnownNonNull)
ASTContext & getContext() const
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
llvm::Value * authPointerToPointerCast(llvm::Value *ResultPtr, QualType SourceType, QualType DestType)
LValue EmitCheckedLValue(const Expr *E, TypeCheckKind TCK)
Same as EmitLValue but additionally we generate checking code to guard against undefined behavior.
llvm::Value * EmitPointerAuthBlendDiscriminator(llvm::Value *StorageAddress, llvm::Value *Discriminator)
Create the discriminator from the storage address and the entity hash.
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
ConstantEmission tryEmitAsConstant(const DeclRefExpr *RefExpr)
Try to emit a reference to the given value without producing it as an l-value.
std::pair< llvm::Value *, CGPointerAuthInfo > EmitOrigPointerRValue(const Expr *E)
Retrieve a pointer rvalue and its ptrauth info.
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
llvm::Value * EmitPointerAuthAuth(const CGPointerAuthInfo &Info, llvm::Value *Pointer)
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Constant * getRawFunctionPointer(GlobalDecl GD, llvm::Type *Ty=nullptr)
Return a function pointer for a reference to the given function.
llvm::Constant * getFunctionPointer(GlobalDecl GD, llvm::Type *Ty=nullptr)
Return the ABI-correct function pointer value for a reference to the given function.
CGPointerAuthInfo getMemberFunctionPointerAuthInfo(QualType FT)
llvm::ConstantInt * getPointerAuthOtherDiscriminator(const PointerAuthSchema &Schema, GlobalDecl SchemaDecl, QualType SchemaType)
Given a pointer-authentication schema, return a concrete "other" discriminator for it.
CGPointerAuthInfo getPointerAuthInfoForPointeeType(QualType type)
CGCXXABI & getCXXABI() const
CGPointerAuthInfo getFunctionPointerAuthInfo(QualType T)
Return the abstract pointer authentication schema for a pointer to the given function type.
llvm::Constant * getMemberFunctionPointer(const FunctionDecl *FD, llvm::Type *Ty=nullptr)
std::optional< PointerAuthQualifier > getVTablePointerAuthentication(const CXXRecordDecl *thisClass)
uint16_t getPointerAuthDeclDiscriminator(GlobalDecl GD)
Return the "other" decl-specific discriminator for the given decl.
ASTContext & getContext() const
const CodeGenOptions & getCodeGenOpts() const
StringRef getMangledName(GlobalDecl GD)
std::optional< CGPointerAuthInfo > getVTablePointerAuthInfo(CodeGenFunction *Context, const CXXRecordDecl *Record, llvm::Value *StorageAddress)
llvm::Constant * getConstantSignedPointer(llvm::Constant *Pointer, const PointerAuthSchema &Schema, llvm::Constant *StorageAddress, GlobalDecl SchemaDecl, QualType SchemaType)
Sign a constant pointer using the given scheme, producing a constant with the same IR type.
bool shouldSignPointer(const PointerAuthSchema &Schema)
Does a given PointerAuthScheme require us to sign a value.
CGPointerAuthInfo getPointerAuthInfoForType(QualType type)
LValue - This represents an lvalue references.
llvm::Value * emitResignedPointer(QualType PointeeTy, CodeGenFunction &CGF) const
llvm::Value * getPointer(CodeGenFunction &CGF) const
llvm::Value * emitRawPointer(CodeGenFunction &CGF) const
This represents one expression.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Represents a function declaration or definition.
Represents a prototype with parameter type info, e.g.
FunctionType - C99 6.7.5.3 - Function Declarators.
GlobalDecl - represents a global declaration.
const Decl * getDecl() const
virtual void mangleCXXName(GlobalDecl GD, raw_ostream &)=0
A pointer to member type per C++ 8.3.3 - Pointers to members.
Pointer-authentication qualifiers.
bool isIsaPointer() const
static PointerAuthQualifier Create(unsigned Key, bool IsAddressDiscriminated, unsigned ExtraDiscriminator, PointerAuthenticationMode AuthenticationMode, bool IsIsaPointer, bool AuthenticatesNullValues)
bool authenticatesNullValues() const
bool isAddressDiscriminated() const
unsigned getExtraDiscriminator() const
PointerAuthenticationMode getAuthenticationMode() const
Discrimination getOtherDiscrimination() const
bool isAddressDiscriminated() const
@ None
No additional discrimination.
@ Type
Include a hash of the entity's type.
@ Decl
Include a hash of the entity's identity.
@ Constant
Discriminate using a constant value.
bool authenticatesNullValues() const
PointerAuthenticationMode getAuthenticationMode() const
uint16_t getConstantDiscrimination() const
bool isIsaPointer() const
PointerType - C99 6.7.5.1 - Pointer Declarators.
QualType getPointeeType() const
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Encodes a location in the source.
bool isBlockPointerType() const
bool isFunctionReferenceType() const
bool isSignableType(const ASTContext &Ctx) const
bool isFunctionPointerType() const
bool isFunctionType() const
const T * getAs() const
Member-template getAs<specific type>'.
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
uint16_t getPointerAuthDeclDiscriminator(CodeGenModule &CGM, GlobalDecl GD)
Return a declaration discriminator for the given global decl.
llvm::Constant * getConstantSignedPointer(CodeGenModule &CGM, llvm::Constant *Pointer, unsigned Key, llvm::Constant *StorageAddress, llvm::ConstantInt *OtherDiscriminator)
Return a signed constant pointer.
uint16_t getPointerAuthTypeDiscriminator(CodeGenModule &CGM, QualType FunctionType)
Return a type discriminator for the given function type.
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
U cast(CodeGen::Address addr)
llvm::IntegerType * Int64Ty
llvm::IntegerType * Int32Ty
llvm::IntegerType * IntPtrTy
llvm::PointerType * DefaultPtrTy
static constexpr llvm::StringLiteral FunctionLabelPrefix
PointerAuthSchema CXXVTablePointers
The ABI for C++ virtual table pointers (the pointer to the table itself) as installed in an actual cl...
PointerAuthSchema FunctionPointers
The ABI for C function pointers.
PointerAuthSchema CXXMemberFunctionPointers
The ABI for C++ member function pointers.