Skip to content

[OpenMP][AArch64] Fix branch protection in microtasks #102317

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 3 commits into from
Aug 13, 2024
Merged

Conversation

tuliom
Copy link
Contributor

@tuliom tuliom commented Aug 7, 2024

Start __kmp_invoke_microtask with PACBTI in order to identify the function as a valid branch target. Before returning, SP is authenticated.
Also add the BTI and PAC markers 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.

@llvmbot llvmbot added the openmp:libomp OpenMP host runtime label Aug 7, 2024
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.
@llvmbot
Copy link
Member

llvmbot commented Aug 9, 2024

@llvm/pr-subscribers-backend-aarch64

Author: Tulio Magno Quites Machado Filho (tuliom)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/102317.diff

1 Files Affected:

  • (modified) openmp/runtime/src/z_Linux_asm.S (+45)
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,"",@progbits
 # endif
 #endif
+
+#if KMP_OS_LINUX && (KMP_ARCH_AARCH64 || KMP_ARCH_AARCH64_32)
+GNU_PROPERTY_BTI_PAC
+#endif

Start using paciasp and autiasp as suggested by Florian when PAC_FLAG is
enabled.
@tuliom
Copy link
Contributor Author

tuliom commented Aug 9, 2024

I updated the description in order to reflect my latest change.

@davemgreen
Copy link
Collaborator

(I'm not really a pac-bti expert, but the changes look like they might well be sensible. Hopefully one of the other reviewers added can check and see).

Copy link
Member

@DanielKristofKiss DanielKristofKiss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a small suggestion, LGTM otherwise.

@tuliom tuliom merged commit 0aa22dc into llvm:main Aug 13, 2024
6 checks passed
@tuliom
Copy link
Contributor Author

tuliom commented Aug 13, 2024

/cherry-pick 0aa22dc

@tuliom tuliom added this to the LLVM 19.X Release milestone Aug 13, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 13, 2024

Failed to cherry-pick: 0aa22dc

https://github.com/llvm/llvm-project/actions/runs/10375652579

Please manually backport the fix and push it to your github fork. Once this is done, please create a pull request

tuliom added a commit to tuliom/llvm-project that referenced this pull request Aug 14, 2024
Start __kmp_invoke_microtask with PACBTI in order to identify the
function as a valid branch target. Before returning, SP is
authenticated.
Also add the BTI and PAC markers 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.

(cherry picked from commit 0aa22dc)
@tuliom
Copy link
Contributor Author

tuliom commented Aug 14, 2024

I manually backported this patch to release/19.x and opened #103491.

tuliom added a commit to tuliom/llvm-project that referenced this pull request Aug 16, 2024
Start __kmp_invoke_microtask with PACBTI in order to identify the
function as a valid branch target. Before returning, SP is
authenticated.
Also add the BTI and PAC markers 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.

(cherry picked from commit 0aa22dc)
tru pushed a commit to tuliom/llvm-project that referenced this pull request Aug 20, 2024
Start __kmp_invoke_microtask with PACBTI in order to identify the
function as a valid branch target. Before returning, SP is
authenticated.
Also add the BTI and PAC markers 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.

(cherry picked from commit 0aa22dc)
pradt2 pushed a commit to pradt2/llvm-project that referenced this pull request Feb 11, 2025
Start __kmp_invoke_microtask with PACBTI in order to identify the
function as a valid branch target. Before returning, SP is
authenticated.
Also add the BTI and PAC markers 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.

(cherry picked from commit 0aa22dc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

6 participants