Skip to content

Commit 3668daf

Browse files
authored
Enable pac bti support (#3775)
* readme: update meson build command Fixes warning: WARNING: Running the setup command as `meson [options]` instead of `meson setup [options]` is ambiguous and deprecated. Signed-off-by: Bill Roberts <[email protected]> * meson.build: fix suffix on HAVE_NEON_AARCH64 rename HAVE_NEON_AARCH64 to the in-use define for controlling NEON: HAVE_NEON_AARCH64 Signed-off-by: Bill Roberts <[email protected]> * aarch64: enable PAC/BTI Enable Pointer Authentication Codes (PAC) and Branch Target Identification (BTI) support for ARM 64 targets. OpenH264 does not require any of the PAC signing and verification as the leaf functions do not store x30 to the stack. Also, no indirect branches are performed so no need to annotate branch targets with bti j landing pad instructions. The only thing required is to label the function entry points with bti c instructions and ensure the GNU Notes are updated for their respective features when enabled for ELF files. A detailed summary on how PAC and BTI work are provided below for clarity. PAC works by signing the LR with either an A key or B key and verifying the return address. There are quite a few instructions capable of doing this, however, the Linux ARM ABI is to use hint compatible instructions that can be safely NOP'd on older hardware and can be assembled and linked with older binutils. This limits the instruction set to paciasp, pacibsp, autiasp and autibsp. Instructions prefixed with pac are for signing and instructions prefixed with aut are for signing. Both instructions are then followed with an a or b to indicate which signing key they are using. The keys can be controlled using -mbranch-protection=pac-ret for the A key and -mbranch-protection=pac-ret+b-key for the B key. BTI works by marking all indirect call and jump positions with bti c and bti j instructions respectively. If execution control transfers to an instruction other than a BTI instruction, the execution is killed via SIGILL. Note that to remove one instruction, the aforementioned pac instructions will also work as a BTI landing pad for bti c usages. For BTI to work, all object files linked for a unit of execution, whether an executable or a library must have the GNU Notes section of the ELF file marked to indicate BTI support. This is so loader/linkers can apply the proper permission bits (PROT_BRI) on the memory region. PAC can also be annotated in the GNU ELF notes section, but it's not required for enablement, as interleaved PAC and non-pac code works as expected since it's the callee that performs all the checking. Testing was done under the following CFLAGS and CXXFLAGS for all combinations: 1. -mbranch-protection=none 2. -mbranch-protection=standard 3. -mbranch-protection=pac-ret 4. -mbranch-protection=pac-ret+b-key 5. -mbranch-protection=bti Signed-off-by: Bill Roberts <[email protected]> --------- Signed-off-by: Bill Roberts <[email protected]>
1 parent cea886e commit 3668daf

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ See <http://mesonbuild.com/Installing.html> for instructions on how to
159159
install meson, then:
160160

161161
``` shell
162-
meson builddir
162+
meson setup builddir
163163
ninja -C builddir
164164
```
165165

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*!
2+
*@page License
3+
*
4+
* \copy
5+
* Copyright (c) 2024, ARM Ltd.
6+
* All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
*
12+
* * Redistributions of source code must retain the above copyright
13+
* notice, this list of conditions and the following disclaimer.
14+
*
15+
* * Redistributions in binary form must reproduce the above copyright
16+
* notice, this list of conditions and the following disclaimer in
17+
* the documentation and/or other materials provided with the
18+
* distribution.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
*/
34+
35+
#ifndef CODEC_COMMON_ARM64_ARM_AARCH64_COMMON_H_
36+
#define CODEC_COMMON_ARM64_ARM_AARCH64_COMMON_H_
37+
38+
/*
39+
; Support macros for
40+
; - Armv8.3-A Pointer Authentication and
41+
; - Armv8.5-A Branch Target Identification
42+
; Further documentation can be found at:
43+
; - https://developer.arm.com/documentation/101028/0012/5--Feature-test-macros
44+
;
45+
; Since openh264 aarch64 assembly code provides functions with no storage of the
46+
; LR(x30) on the stack, PAC is not needed as modification of the LR value would
47+
; require modification of x30 and not memory. Additionally, no indirect control
48+
; flow changes are performed, so bti j instructions are not needed. Thus, just
49+
; mark the entry points with bti c landing pads and the ELF files as supporting
50+
; BTI and PAC.
51+
*/
52+
#if defined(__ARM_FEATURE_BTI_DEFAULT) && __ARM_FEATURE_BTI_DEFAULT == 1
53+
/* BTI is enabled */
54+
#define BTI_C hint 34
55+
#define GNU_PROPERTY_AARCH64_BTI 0x1 /* Property for notes section in ELF */
56+
#else
57+
/* BTI is NOT enabled */
58+
#define BTI_C
59+
#define GNU_PROPERTY_AARCH64_BTI 0
60+
#endif
61+
62+
#if defined(__ARM_FEATURE_PAC_DEFAULT)
63+
/* PAC is enabled */
64+
#define GNU_PROPERTY_AARCH64_POINTER_AUTH 0x2 /* Property for notes section in ELF */
65+
#else
66+
/* PAC is not enabled */
67+
#define GNU_PROPERTY_AARCH64_POINTER_AUTH 0
68+
#endif
69+
70+
/* Add the notes section to ELF only */
71+
#if defined(__ELF__)
72+
.pushsection .note.gnu.property, "a";
73+
.balign 8;
74+
.long 4;
75+
.long 0x10;
76+
.long 0x5;
77+
.asciz "GNU";
78+
.long 0xc0000000; /* GNU_PROPERTY_AARCH64_FEATURE_1_AND */
79+
.long 4;
80+
.long(GNU_PROPERTY_AARCH64_POINTER_AUTH | GNU_PROPERTY_AARCH64_BTI);
81+
.long 0;
82+
.popsection;
83+
#endif
84+
85+
#endif /* CODEC_COMMON_ARM64_ARM_AARCH64_COMMON_H_ */

codec/common/arm64/arm_arch64_common_macro.S

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*
3131
*/
3232

33+
#include "arm_aarch64_common.h"
34+
3335
#ifdef __APPLE__
3436

3537
.text
@@ -60,6 +62,7 @@ ret
6062
.func \funcName
6163
#endif
6264
\funcName:
65+
BTI_C
6366
.endm
6467

6568
.macro WELS_ASM_AARCH64_FUNC_END

meson.build

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ if ['linux', 'android', 'ios', 'darwin'].contains(system)
8585
casm_inc = include_directories(join_paths('codec', 'common', 'arm'))
8686
elif cpu_family == 'aarch64'
8787
asm_format = asm_format64
88-
add_project_arguments('-DHAVE_NEON_ARM64', language: 'c')
89-
add_project_arguments('-DHAVE_NEON_ARM64', language: 'cpp')
88+
add_project_arguments('-DHAVE_NEON_AARCH64', language: 'c')
89+
add_project_arguments('-DHAVE_NEON_AARCH64', language: 'cpp')
9090
casm_inc = include_directories(join_paths('codec', 'common', 'arm64'))
9191
elif cpu_family == 'loongarch32'
9292
asm_format = asm_format32

0 commit comments

Comments
 (0)