Skip to content

Commit 1027531

Browse files
committed
[OpenMP][AArch64] Fix branch protection in microtasks (#102317)
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)
1 parent 28f2d04 commit 1027531

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

openmp/runtime/src/z_Linux_asm.S

+53
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,53 @@ KMP_PREFIX_UNDERSCORE(\proc):
176176
.endm
177177
# endif // KMP_OS_DARWIN
178178

179+
# if KMP_OS_LINUX
180+
// BTI and PAC gnu property note
181+
# define NT_GNU_PROPERTY_TYPE_0 5
182+
# define GNU_PROPERTY_AARCH64_FEATURE_1_AND 0xc0000000
183+
# define GNU_PROPERTY_AARCH64_FEATURE_1_BTI 1
184+
# define GNU_PROPERTY_AARCH64_FEATURE_1_PAC 2
185+
186+
# define GNU_PROPERTY(type, value) \
187+
.pushsection .note.gnu.property, "a"; \
188+
.p2align 3; \
189+
.word 4; \
190+
.word 16; \
191+
.word NT_GNU_PROPERTY_TYPE_0; \
192+
.asciz "GNU"; \
193+
.word type; \
194+
.word 4; \
195+
.word value; \
196+
.word 0; \
197+
.popsection
198+
# endif
199+
200+
# if defined(__ARM_FEATURE_BTI_DEFAULT)
201+
# define BTI_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_BTI
202+
# else
203+
# define BTI_FLAG 0
204+
# endif
205+
# if __ARM_FEATURE_PAC_DEFAULT & 3
206+
# define PAC_FLAG GNU_PROPERTY_AARCH64_FEATURE_1_PAC
207+
# else
208+
# define PAC_FLAG 0
209+
# endif
210+
211+
# if (BTI_FLAG | PAC_FLAG) != 0
212+
# if PAC_FLAG != 0
213+
# define PACBTI_C hint #25
214+
# define PACBTI_RET hint #29
215+
# else
216+
# define PACBTI_C hint #34
217+
# define PACBTI_RET
218+
# endif
219+
# define GNU_PROPERTY_BTI_PAC \
220+
GNU_PROPERTY(GNU_PROPERTY_AARCH64_FEATURE_1_AND, BTI_FLAG | PAC_FLAG)
221+
# else
222+
# define PACBTI_C
223+
# define PACBTI_RET
224+
# define GNU_PROPERTY_BTI_PAC
225+
# endif
179226
#endif // (KMP_OS_LINUX || KMP_OS_DARWIN || KMP_OS_WINDOWS) && (KMP_ARCH_AARCH64 || KMP_ARCH_AARCH64_32 || KMP_ARCH_ARM)
180227

181228
.macro COMMON name, size, align_power
@@ -1296,6 +1343,7 @@ __tid = 8
12961343
// mark_begin;
12971344
.text
12981345
PROC __kmp_invoke_microtask
1346+
PACBTI_C
12991347

13001348
stp x29, x30, [sp, #-16]!
13011349
# if OMPT_SUPPORT
@@ -1359,6 +1407,7 @@ KMP_LABEL(kmp_1):
13591407
ldp x19, x20, [sp], #16
13601408
# endif
13611409
ldp x29, x30, [sp], #16
1410+
PACBTI_RET
13621411
ret
13631412

13641413
DEBUG_INFO __kmp_invoke_microtask
@@ -2472,3 +2521,7 @@ __kmp_unnamed_critical_addr:
24722521
.4byte .gomp_critical_user_
24732522
.size __kmp_unnamed_critical_addr, 4
24742523
#endif
2524+
2525+
#if KMP_OS_LINUX && (KMP_ARCH_AARCH64 || KMP_ARCH_AARCH64_32)
2526+
GNU_PROPERTY_BTI_PAC
2527+
#endif

0 commit comments

Comments
 (0)