Skip to content

[C API] Support new ptrauth constant type #93909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ They are described in detail in the `debug info migration guide <https://llvm.or
* ``LLVMGEPGetNoWrapFlags``
* ``LLVMGEPSetNoWrapFlags``

* Added the following functions for creating and accessing data for ConstantPtrAuth constants:

* ``LLVMConstantPtrAuth``
* ``LLVMGetConstantPtrAuthPointer``
* ``LLVMGetConstantPtrAuthKey``
* ``LLVMGetConstantPtrAuthDiscriminator``
* ``LLVMGetConstantPtrAuthAddrDiscriminator``

Changes to the CodeGen infrastructure
-------------------------------------

Expand Down
45 changes: 45 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ typedef enum {
LLVMInstructionValueKind,
LLVMPoisonValueValueKind,
LLVMConstantTargetNoneValueKind,
LLVMConstantPtrAuthValueKind,
} LLVMValueKind;

typedef enum {
Expand Down Expand Up @@ -1666,6 +1667,35 @@ LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
*/
unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy);

/**
* Get the pointer value for the associated ConstantPtrAuth constant.
*
* @see llvm::ConstantPtrAuth::getPointer
*/
LLVMValueRef LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth);

/**
* Get the key value for the associated ConstantPtrAuth constant.
*
* @see llvm::ConstantPtrAuth::getKey
*/
LLVMValueRef LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth);

/**
* Get the discriminator value for the associated ConstantPtrAuth constant.
*
* @see llvm::ConstantPtrAuth::getDiscriminator
*/
LLVMValueRef LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth);

/**
* Get the address discriminator value for the associated ConstantPtrAuth
* constant.
*
* @see llvm::ConstantPtrAuth::getAddrDiscriminator
*/
LLVMValueRef LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth);

/**
* @}
*/
Expand Down Expand Up @@ -1789,6 +1819,10 @@ unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx);
* @{
*/

// Currently, clang-format tries to format the LLVM_FOR_EACH_VALUE_SUBCLASS
// macro in a progressively-indented fashion, which is not desired
// clang-format off

#define LLVM_FOR_EACH_VALUE_SUBCLASS(macro) \
macro(Argument) \
macro(BasicBlock) \
Expand All @@ -1808,6 +1842,7 @@ unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx);
macro(ConstantStruct) \
macro(ConstantTokenNone) \
macro(ConstantVector) \
macro(ConstantPtrAuth) \
macro(GlobalValue) \
macro(GlobalAlias) \
macro(GlobalObject) \
Expand Down Expand Up @@ -1879,6 +1914,8 @@ unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx);
macro(AtomicRMWInst) \
macro(FenceInst)

// clang-format on

/**
* @defgroup LLVMCCoreValueGeneral General APIs
*
Expand Down Expand Up @@ -2372,6 +2409,14 @@ LLVM_ATTRIBUTE_C_DEPRECATED(
*/
LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size);

/**
* Create a ConstantPtrAuth constant with the given values.
*
* @see llvm::ConstantPtrAuth::get()
*/
LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,
LLVMValueRef Disc, LLVMValueRef AddrDisc);

/**
* @}
*/
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/Value.def
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ HANDLE_CONSTANT(BlockAddress)
HANDLE_CONSTANT(ConstantExpr)
HANDLE_CONSTANT_EXCLUDE_LLVM_C_API(DSOLocalEquivalent)
HANDLE_CONSTANT_EXCLUDE_LLVM_C_API(NoCFIValue)
HANDLE_CONSTANT_EXCLUDE_LLVM_C_API(ConstantPtrAuth)
HANDLE_CONSTANT(ConstantPtrAuth)

// ConstantAggregate.
HANDLE_CONSTANT(ConstantArray)
Expand Down
23 changes: 23 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,22 @@ unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
}

LLVMValueRef LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth) {
return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getPointer());
}

LLVMValueRef LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth) {
return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getKey());
}

LLVMValueRef LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth) {
return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getDiscriminator());
}

LLVMValueRef LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth) {
return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getAddrDiscriminator());
}

/*--.. Operations on other types ...........................................--*/

LLVMTypeRef LLVMPointerTypeInContext(LLVMContextRef C, unsigned AddressSpace) {
Expand Down Expand Up @@ -1663,6 +1679,13 @@ LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size) {
ArrayRef(unwrap<Constant>(ScalarConstantVals, Size), Size)));
}

LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,
LLVMValueRef Disc, LLVMValueRef AddrDisc) {
return wrap(ConstantPtrAuth::get(
unwrap<Constant>(Ptr), unwrap<ConstantInt>(Key),
unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc)));
}

/*-- Opcode mapping */

static LLVMOpcode map_to_llvmopcode(int opcode)
Expand Down
5 changes: 5 additions & 0 deletions llvm/test/Bindings/llvm-c/echo.ll
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ module asm "classical GAS"

@ifunc = ifunc i32 (i32), ptr @ifunc_resolver

@ptrauth_addr_disc = global i32 0
@ptrauth_data = global i32 0
@ptrauth_ptr_01 = global ptr ptrauth (ptr @ptrauth_data, i32 77, i64 1001, ptr @ptrauth_addr_disc)
@ptrauth_ptr_02 = global ptr ptrauth (ptr @ptrauth_data, i32 11, i64 99, ptr null)

define ptr @ifunc_resolver() {
entry:
ret ptr null
Expand Down
10 changes: 10 additions & 0 deletions llvm/tools/llvm-c-test/echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,16 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
return LLVMConstVector(Elts.data(), EltCount);
}

if (LLVMIsAConstantPtrAuth(Cst)) {
LLVMValueRef Ptr = clone_constant(LLVMGetConstantPtrAuthPointer(Cst), M);
LLVMValueRef Key = clone_constant(LLVMGetConstantPtrAuthKey(Cst), M);
LLVMValueRef Disc =
clone_constant(LLVMGetConstantPtrAuthDiscriminator(Cst), M);
LLVMValueRef AddrDisc =
clone_constant(LLVMGetConstantPtrAuthAddrDiscriminator(Cst), M);
return LLVMConstantPtrAuth(Ptr, Key, Disc, AddrDisc);
}

// At this point, if it's not a constant expression, it's a kind of constant
// which is not supported
if (!LLVMIsAConstantExpr(Cst))
Expand Down
Loading