Skip to content

Commit e2da04d

Browse files
Benjinsyuxuanchen1997
authored andcommitted
[C API] Support new ptrauth constant type (#93909)
Summary: This is a new constant type that was added to the C++ API in 0edc97f. This adds the ability to create instances of this constant and get its values to the C API. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250977
1 parent de5cbe5 commit e2da04d

File tree

6 files changed

+92
-1
lines changed

6 files changed

+92
-1
lines changed

llvm/docs/ReleaseNotes.rst

+8
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ They are described in detail in the `debug info migration guide <https://llvm.or
320320
* ``LLVMGEPGetNoWrapFlags``
321321
* ``LLVMGEPSetNoWrapFlags``
322322

323+
* Added the following functions for creating and accessing data for ConstantPtrAuth constants:
324+
325+
* ``LLVMConstantPtrAuth``
326+
* ``LLVMGetConstantPtrAuthPointer``
327+
* ``LLVMGetConstantPtrAuthKey``
328+
* ``LLVMGetConstantPtrAuthDiscriminator``
329+
* ``LLVMGetConstantPtrAuthAddrDiscriminator``
330+
323331
Changes to the CodeGen infrastructure
324332
-------------------------------------
325333

llvm/include/llvm-c/Core.h

+45
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ typedef enum {
286286
LLVMInstructionValueKind,
287287
LLVMPoisonValueValueKind,
288288
LLVMConstantTargetNoneValueKind,
289+
LLVMConstantPtrAuthValueKind,
289290
} LLVMValueKind;
290291

291292
typedef enum {
@@ -1666,6 +1667,35 @@ LLVMTypeRef LLVMScalableVectorType(LLVMTypeRef ElementType,
16661667
*/
16671668
unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy);
16681669

1670+
/**
1671+
* Get the pointer value for the associated ConstantPtrAuth constant.
1672+
*
1673+
* @see llvm::ConstantPtrAuth::getPointer
1674+
*/
1675+
LLVMValueRef LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth);
1676+
1677+
/**
1678+
* Get the key value for the associated ConstantPtrAuth constant.
1679+
*
1680+
* @see llvm::ConstantPtrAuth::getKey
1681+
*/
1682+
LLVMValueRef LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth);
1683+
1684+
/**
1685+
* Get the discriminator value for the associated ConstantPtrAuth constant.
1686+
*
1687+
* @see llvm::ConstantPtrAuth::getDiscriminator
1688+
*/
1689+
LLVMValueRef LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth);
1690+
1691+
/**
1692+
* Get the address discriminator value for the associated ConstantPtrAuth
1693+
* constant.
1694+
*
1695+
* @see llvm::ConstantPtrAuth::getAddrDiscriminator
1696+
*/
1697+
LLVMValueRef LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth);
1698+
16691699
/**
16701700
* @}
16711701
*/
@@ -1789,6 +1819,10 @@ unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx);
17891819
* @{
17901820
*/
17911821

1822+
// Currently, clang-format tries to format the LLVM_FOR_EACH_VALUE_SUBCLASS
1823+
// macro in a progressively-indented fashion, which is not desired
1824+
// clang-format off
1825+
17921826
#define LLVM_FOR_EACH_VALUE_SUBCLASS(macro) \
17931827
macro(Argument) \
17941828
macro(BasicBlock) \
@@ -1808,6 +1842,7 @@ unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx);
18081842
macro(ConstantStruct) \
18091843
macro(ConstantTokenNone) \
18101844
macro(ConstantVector) \
1845+
macro(ConstantPtrAuth) \
18111846
macro(GlobalValue) \
18121847
macro(GlobalAlias) \
18131848
macro(GlobalObject) \
@@ -1879,6 +1914,8 @@ unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx);
18791914
macro(AtomicRMWInst) \
18801915
macro(FenceInst)
18811916

1917+
// clang-format on
1918+
18821919
/**
18831920
* @defgroup LLVMCCoreValueGeneral General APIs
18841921
*
@@ -2372,6 +2409,14 @@ LLVM_ATTRIBUTE_C_DEPRECATED(
23722409
*/
23732410
LLVMValueRef LLVMConstVector(LLVMValueRef *ScalarConstantVals, unsigned Size);
23742411

2412+
/**
2413+
* Create a ConstantPtrAuth constant with the given values.
2414+
*
2415+
* @see llvm::ConstantPtrAuth::get()
2416+
*/
2417+
LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,
2418+
LLVMValueRef Disc, LLVMValueRef AddrDisc);
2419+
23752420
/**
23762421
* @}
23772422
*/

llvm/include/llvm/IR/Value.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ HANDLE_CONSTANT(BlockAddress)
8181
HANDLE_CONSTANT(ConstantExpr)
8282
HANDLE_CONSTANT_EXCLUDE_LLVM_C_API(DSOLocalEquivalent)
8383
HANDLE_CONSTANT_EXCLUDE_LLVM_C_API(NoCFIValue)
84-
HANDLE_CONSTANT_EXCLUDE_LLVM_C_API(ConstantPtrAuth)
84+
HANDLE_CONSTANT(ConstantPtrAuth)
8585

8686
// ConstantAggregate.
8787
HANDLE_CONSTANT(ConstantArray)

llvm/lib/IR/Core.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,22 @@ unsigned LLVMGetVectorSize(LLVMTypeRef VectorTy) {
917917
return unwrap<VectorType>(VectorTy)->getElementCount().getKnownMinValue();
918918
}
919919

920+
LLVMValueRef LLVMGetConstantPtrAuthPointer(LLVMValueRef PtrAuth) {
921+
return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getPointer());
922+
}
923+
924+
LLVMValueRef LLVMGetConstantPtrAuthKey(LLVMValueRef PtrAuth) {
925+
return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getKey());
926+
}
927+
928+
LLVMValueRef LLVMGetConstantPtrAuthDiscriminator(LLVMValueRef PtrAuth) {
929+
return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getDiscriminator());
930+
}
931+
932+
LLVMValueRef LLVMGetConstantPtrAuthAddrDiscriminator(LLVMValueRef PtrAuth) {
933+
return wrap(unwrap<ConstantPtrAuth>(PtrAuth)->getAddrDiscriminator());
934+
}
935+
920936
/*--.. Operations on other types ...........................................--*/
921937

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

1682+
LLVMValueRef LLVMConstantPtrAuth(LLVMValueRef Ptr, LLVMValueRef Key,
1683+
LLVMValueRef Disc, LLVMValueRef AddrDisc) {
1684+
return wrap(ConstantPtrAuth::get(
1685+
unwrap<Constant>(Ptr), unwrap<ConstantInt>(Key),
1686+
unwrap<ConstantInt>(Disc), unwrap<Constant>(AddrDisc)));
1687+
}
1688+
16661689
/*-- Opcode mapping */
16671690

16681691
static LLVMOpcode map_to_llvmopcode(int opcode)

llvm/test/Bindings/llvm-c/echo.ll

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ module asm "classical GAS"
3737

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

40+
@ptrauth_addr_disc = global i32 0
41+
@ptrauth_data = global i32 0
42+
@ptrauth_ptr_01 = global ptr ptrauth (ptr @ptrauth_data, i32 77, i64 1001, ptr @ptrauth_addr_disc)
43+
@ptrauth_ptr_02 = global ptr ptrauth (ptr @ptrauth_data, i32 11, i64 99, ptr null)
44+
4045
define ptr @ifunc_resolver() {
4146
entry:
4247
ret ptr null

llvm/tools/llvm-c-test/echo.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ static LLVMValueRef clone_constant_impl(LLVMValueRef Cst, LLVMModuleRef M) {
391391
return LLVMConstVector(Elts.data(), EltCount);
392392
}
393393

394+
if (LLVMIsAConstantPtrAuth(Cst)) {
395+
LLVMValueRef Ptr = clone_constant(LLVMGetConstantPtrAuthPointer(Cst), M);
396+
LLVMValueRef Key = clone_constant(LLVMGetConstantPtrAuthKey(Cst), M);
397+
LLVMValueRef Disc =
398+
clone_constant(LLVMGetConstantPtrAuthDiscriminator(Cst), M);
399+
LLVMValueRef AddrDisc =
400+
clone_constant(LLVMGetConstantPtrAuthAddrDiscriminator(Cst), M);
401+
return LLVMConstantPtrAuth(Ptr, Key, Disc, AddrDisc);
402+
}
403+
394404
// At this point, if it's not a constant expression, it's a kind of constant
395405
// which is not supported
396406
if (!LLVMIsAConstantExpr(Cst))

0 commit comments

Comments
 (0)