[llvm] [C API] Support new ptrauth constant type (PR #93909)
Benji Smith via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 16 04:55:59 PDT 2024
https://github.com/Benjins updated https://github.com/llvm/llvm-project/pull/93909
>From 9238042574fb1a8dcab5ff071709019a5de92c87 Mon Sep 17 00:00:00 2001
From: Benji Smith <benjsith at gmail.com>
Date: Thu, 30 May 2024 20:23:01 -0400
Subject: [PATCH 1/2] [C API] Support new ptrauth constant type
This is a new constant type that was added to the C++ API in
0edc97f119f3ac3ff96b11183fe5c001a48a9a8d. This adds the ability to create
instances of this constant and get its values to the C API
---
llvm/docs/ReleaseNotes.rst | 8 +++++++
llvm/include/llvm-c/Core.h | 39 +++++++++++++++++++++++++++++++
llvm/include/llvm/IR/Value.def | 2 +-
llvm/lib/IR/Core.cpp | 23 ++++++++++++++++++
llvm/test/Bindings/llvm-c/echo.ll | 5 ++++
llvm/tools/llvm-c-test/echo.cpp | 10 ++++++++
6 files changed, 86 insertions(+), 1 deletion(-)
diff --git a/llvm/docs/ReleaseNotes.rst b/llvm/docs/ReleaseNotes.rst
index 1e1ccb495c366..398d2308e212d 100644
--- a/llvm/docs/ReleaseNotes.rst
+++ b/llvm/docs/ReleaseNotes.rst
@@ -186,6 +186,14 @@ Changes to the C API
* ``LLVMGetCallBrNumIndirectDests``
* ``LLVMGetCallBrIndirectDest``
+* Added the following functions for creating and accessing data for ConstantPtrAuth constants:
+
+ * ``LLVMConstantPtrAuth``
+ * ``LLVMGetConstantPtrAuthPointer``
+ * ``LLVMGetConstantPtrAuthKey``
+ * ``LLVMGetConstantPtrAuthDiscriminator``
+ * ``LLVMGetConstantPtrAuthAddrDiscriminator``
+
Changes to the CodeGen infrastructure
-------------------------------------
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 9d09546513f0e..fdd81cd76299b 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -286,6 +286,7 @@ typedef enum {
LLVMInstructionValueKind,
LLVMPoisonValueValueKind,
LLVMConstantTargetNoneValueKind,
+ LLVMConstantPtrAuthValueKind,
} LLVMValueKind;
typedef enum {
@@ -1652,6 +1653,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);
+
/**
* @}
*/
@@ -1758,6 +1788,7 @@ LLVMTypeRef LLVMTargetExtTypeInContext(LLVMContextRef C, const char *Name,
macro(ConstantStruct) \
macro(ConstantTokenNone) \
macro(ConstantVector) \
+ macro(ConstantPtrAuth) \
macro(GlobalValue) \
macro(GlobalAlias) \
macro(GlobalObject) \
@@ -2322,6 +2353,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);
+
/**
* @}
*/
diff --git a/llvm/include/llvm/IR/Value.def b/llvm/include/llvm/IR/Value.def
index 3ece66a529e12..160e0f8513e2a 100644
--- a/llvm/include/llvm/IR/Value.def
+++ b/llvm/include/llvm/IR/Value.def
@@ -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)
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index df90b88341123..40327b8ad578c 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -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) {
@@ -1637,6 +1653,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)
diff --git a/llvm/test/Bindings/llvm-c/echo.ll b/llvm/test/Bindings/llvm-c/echo.ll
index bb5fae0dcd12e..983853eaf163f 100644
--- a/llvm/test/Bindings/llvm-c/echo.ll
+++ b/llvm/test/Bindings/llvm-c/echo.ll
@@ -34,6 +34,11 @@ module asm "classical GAS"
@ifunc = ifunc i32 (i32), ptr @ifunc_resolver
+ at ptrauth_addr_disc = global i32 0
+ at ptrauth_data = global i32 0
+ at ptrauth_ptr_01 = global ptr ptrauth (ptr @ptrauth_data, i32 77, i64 1001, ptr @ptrauth_addr_disc)
+ at ptrauth_ptr_02 = global ptr ptrauth (ptr @ptrauth_data, i32 11, i64 99, ptr null)
+
define ptr @ifunc_resolver() {
entry:
ret ptr null
diff --git a/llvm/tools/llvm-c-test/echo.cpp b/llvm/tools/llvm-c-test/echo.cpp
index 518716168c423..0b8675350f994 100644
--- a/llvm/tools/llvm-c-test/echo.cpp
+++ b/llvm/tools/llvm-c-test/echo.cpp
@@ -373,6 +373,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))
>From 31aa5248ed13f9753320787f69ef0b5c5099d839 Mon Sep 17 00:00:00 2001
From: Benji Smith <6193112+Benjins at users.noreply.github.com>
Date: Tue, 16 Jul 2024 07:55:27 -0400
Subject: [PATCH 2/2] Disable clang formatting for LLVM_FOR_EACH_VALUE_SUBCLASS
macro definition
---
llvm/include/llvm-c/Core.h | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 77b1589d44d97..223d8efe57daa 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -1819,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) \
@@ -1910,6 +1914,8 @@ unsigned LLVMGetTargetExtTypeIntParam(LLVMTypeRef TargetExtTy, unsigned Idx);
macro(AtomicRMWInst) \
macro(FenceInst)
+// clang-format on
+
/**
* @defgroup LLVMCCoreValueGeneral General APIs
*
More information about the llvm-commits
mailing list