[Openmp-commits] [openmp] [OpenMP][AArch64] Fix branch protection in microtasks (PR #102317)

Tulio Magno Quites Machado Filho via Openmp-commits openmp-commits at lists.llvm.org
Fri Aug 9 12:06:04 PDT 2024


https://github.com/tuliom updated https://github.com/llvm/llvm-project/pull/102317

>From d3d9b1e1b3170f85e5f4abf72aca54994088ce0d Mon Sep 17 00:00:00 2001
From: Tulio Magno Quites Machado Filho <tuliom at redhat.com>
Date: Wed, 7 Aug 2024 11:13:19 -0300
Subject: [PATCH 1/2] [OpenMP][AArch64] Fix branch protection in microtasks

Start __kmp_invoke_microtask with BTI in order to identify the function
as a valid branch target.
Also add the BTI marker to z_Linux_asm.S.

With this patch, libomp.so can now be generated with
DT_AARCH64_BTI_PLT when built with -mbranch-protection=standard.

The implementation is based on the code available in compiler-rt.
---
 openmp/runtime/src/z_Linux_asm.S | 45 ++++++++++++++++++++++++++++++++
 1 file changed, 45 insertions(+)

diff --git a/openmp/runtime/src/z_Linux_asm.S b/openmp/runtime/src/z_Linux_asm.S
index f119f64647daa4..f7ed3ada982f6f 100644
--- a/openmp/runtime/src/z_Linux_asm.S
+++ b/openmp/runtime/src/z_Linux_asm.S
@@ -17,6 +17,27 @@
 
 #include "kmp_config.h"
 
+#if KMP_OS_LINUX
+// BTI and PAC gnu property note
+#define NT_GNU_PROPERTY_TYPE_0 5
+#define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
+#define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 1
+#define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 2
+
+# define GNU_PROPERTY(type, value)                                             \
+  .pushsection .note.gnu.property, "a";                                        \
+  .p2align 3;                                                                  \
+  .word 4;                                                                     \
+  .word 16;                                                                    \
+  .word NT_GNU_PROPERTY_TYPE_0;                                                \
+  .asciz "GNU";                                                                \
+  .word type;                                                                  \
+  .word 4;                                                                     \
+  .word value;                                                                 \
+  .word 0;                                                                     \
+  .popsection
+#endif
+
 #if KMP_ARCH_X86 || KMP_ARCH_X86_64
 
 # if KMP_MIC
@@ -176,6 +197,25 @@ KMP_PREFIX_UNDERSCORE(\proc):
 .endm
 # endif // KMP_OS_DARWIN
 
+# if defined(__ARM_FEATURE_BTI_DEFAULT)
+#  define BTI_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_BTI
+# else
+#  define BTI_FLAG 0
+# endif
+# if __ARM_FEATURE_PAC_DEFAULT & 3
+#  define PAC_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_PAC
+# else
+#  define PAC_FLAG 0
+# endif
+
+# if (BTI_FLAG | PAC_FLAG) != 0
+#  define BTI_C hint #34
+#  define GNU_PROPERTY_BTI_PAC \
+    GNU_PROPERTY(GNU_PROPERTY_AARCH64_FEATURE_1_AND, BTI_FLAG | PAC_FLAG)
+# else
+#  define BTI_C
+#  define GNU_PROPERTY_BTI_PAC
+# endif
 #endif // (KMP_OS_LINUX || KMP_OS_DARWIN || KMP_OS_WINDOWS) && (KMP_ARCH_AARCH64 || KMP_ARCH_AARCH64_32 || KMP_ARCH_ARM)
 
 .macro COMMON name, size, align_power
@@ -1296,6 +1336,7 @@ __tid = 8
 // mark_begin;
 	.text
 	PROC __kmp_invoke_microtask
+	BTI_C
 
 	stp	x29, x30, [sp, #-16]!
 # if OMPT_SUPPORT
@@ -2452,3 +2493,7 @@ KMP_PREFIX_UNDERSCORE(__kmp_unnamed_critical_addr):
 .section .note.GNU-stack,"", at progbits
 # endif
 #endif
+
+#if KMP_OS_LINUX && (KMP_ARCH_AARCH64 || KMP_ARCH_AARCH64_32)
+GNU_PROPERTY_BTI_PAC
+#endif

>From 756ec89798011125803f3b37cb1dd594656a7c42 Mon Sep 17 00:00:00 2001
From: Tulio Magno Quites Machado Filho <tuliom at redhat.com>
Date: Fri, 9 Aug 2024 16:03:31 -0300
Subject: [PATCH 2/2] fixup! [OpenMP][AArch64] Fix branch protection in
 microtasks

Start using paciasp and autiasp as suggested by Florian when PAC_FLAG is
enabled.
---
 openmp/runtime/src/z_Linux_asm.S | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/openmp/runtime/src/z_Linux_asm.S b/openmp/runtime/src/z_Linux_asm.S
index f7ed3ada982f6f..5b5ed02af7172f 100644
--- a/openmp/runtime/src/z_Linux_asm.S
+++ b/openmp/runtime/src/z_Linux_asm.S
@@ -209,11 +209,18 @@ KMP_PREFIX_UNDERSCORE(\proc):
 # endif
 
 # if (BTI_FLAG | PAC_FLAG) != 0
-#  define BTI_C hint #34
+#  if PAC_FLAG != 0
+#   define PACBTI_C hint #25
+#   define PACBTI_RET hint #29
+#  else
+#   define PACBTI_C hint #34
+#   define PACBTI_RET
+#  endif
 #  define GNU_PROPERTY_BTI_PAC \
     GNU_PROPERTY(GNU_PROPERTY_AARCH64_FEATURE_1_AND, BTI_FLAG | PAC_FLAG)
 # else
-#  define BTI_C
+#  define PACBTI_C
+#  define PACBTI_RET
 #  define GNU_PROPERTY_BTI_PAC
 # endif
 #endif // (KMP_OS_LINUX || KMP_OS_DARWIN || KMP_OS_WINDOWS) && (KMP_ARCH_AARCH64 || KMP_ARCH_AARCH64_32 || KMP_ARCH_ARM)
@@ -1336,7 +1343,7 @@ __tid = 8
 // mark_begin;
 	.text
 	PROC __kmp_invoke_microtask
-	BTI_C
+	PACBTI_C
 
 	stp	x29, x30, [sp, #-16]!
 # if OMPT_SUPPORT
@@ -1400,6 +1407,7 @@ KMP_LABEL(kmp_1):
 	ldp	x19, x20, [sp], #16
 # endif
 	ldp	x29, x30, [sp], #16
+	PACBTI_RET
 	ret
 
 	DEBUG_INFO __kmp_invoke_microtask



More information about the Openmp-commits mailing list