From b9c443579a83939049d79a61347afdab9d565913 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 24 Jan 2026 08:01:20 +0900 Subject: [PATCH 1/2] Use BTI c in aarch64_outline_atomic.rs --- compiler-builtins/src/aarch64_outline_atomics.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler-builtins/src/aarch64_outline_atomics.rs b/compiler-builtins/src/aarch64_outline_atomics.rs index df0cf7650..1520eb975 100644 --- a/compiler-builtins/src/aarch64_outline_atomics.rs +++ b/compiler-builtins/src/aarch64_outline_atomics.rs @@ -181,6 +181,7 @@ macro_rules! compare_and_swap { ) -> int_ty!($bytes) { // We can't use `AtomicI8::compare_and_swap`; we *are* compare_and_swap. core::arch::naked_asm! { + "hint #34", // BTI c // CAS s(0), s(1), [x2]; if LSE supported. try_lse_op!("cas", $ordering, $bytes, 0, 1, [x2]), // UXT s(tmp0), s(0) @@ -213,6 +214,7 @@ macro_rules! compare_and_swap_i128 { expected: i128, desired: i128, ptr: *mut i128 ) -> i128 { core::arch::naked_asm! { + "hint #34", // BTI c // CASP x0, x1, x2, x3, [x4]; if LSE supported. try_lse_op!("cas", $ordering, 16, 0, 1, 2, 3, [x4]), "mov x16, x0", @@ -245,6 +247,7 @@ macro_rules! swap { left: int_ty!($bytes), right_ptr: *mut int_ty!($bytes) ) -> int_ty!($bytes) { core::arch::naked_asm! { + "hint #34", // BTI c // SWP s(0), s(0), [x1]; if LSE supported. try_lse_op!("swp", $ordering, $bytes, 0, 0, [x1]), // mov s(tmp0), s(0) @@ -273,6 +276,7 @@ macro_rules! fetch_op { val: int_ty!($bytes), ptr: *mut int_ty!($bytes) ) -> int_ty!($bytes) { core::arch::naked_asm! { + "hint #34", // BTI c // LSEOP s(0), s(0), [x1]; if LSE supported. try_lse_op!($lse_op, $ordering, $bytes, 0, 0, [x1]), // mov s(tmp0), s(0) From 228d38bcf8397d42d8d7852f90b46069a6f29bf4 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Sat, 24 Jan 2026 10:17:08 +0900 Subject: [PATCH 2/2] wip --- .github/workflows/main.yaml | 24 ++++++++++++++++++ compiler-builtins/build.rs | 21 ++++++++++++++++ .../src/aarch64_outline_atomics.rs | 25 +++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 767566dd4..edf633295 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -50,6 +50,26 @@ jobs: os: macos-15 - target: aarch64-unknown-linux-gnu os: ubuntu-24.04-arm + - target: aarch64-unknown-linux-gnu + os: ubuntu-24.04-arm + flags: -Z branch-protection=bti -C link-arg=-Wl,-z,force-bti + - target: aarch64-unknown-linux-gnu + os: ubuntu-24.04-arm + flags: -Z branch-protection=pac-ret,bti -C link-arg=-Wl,-z,force-bti + - target: aarch64-unknown-linux-gnu + os: ubuntu-24.04-arm + flags: -Z branch-protection=pac-ret,bti,b-key -C link-arg=-Wl,-z,force-bti + - target: aarch64-unknown-linux-gnu + os: ubuntu-24.04 # QEMU + - target: aarch64-unknown-linux-gnu + os: ubuntu-24.04 # QEMU + flags: -Z branch-protection=bti -C link-arg=-Wl,-z,force-bti + - target: aarch64-unknown-linux-gnu + os: ubuntu-24.04 # QEMU + flags: -Z branch-protection=pac-ret,bti -C link-arg=-Wl,-z,force-bti + - target: aarch64-unknown-linux-gnu + os: ubuntu-24.04 # QEMU + flags: -Z branch-protection=pac-ret,bti,b-key -C link-arg=-Wl,-z,force-bti - target: aarch64-pc-windows-msvc os: windows-11-arm - target: arm-unknown-linux-gnueabi @@ -174,11 +194,15 @@ jobs: if: matrix.os != 'ubuntu-24.04' shell: bash run: ./ci/run.sh ${{ matrix.target }} + env: + RUSTFLAGS: ${{ matrix.flags }} ${{ env.RUSTFLAGS }} # Otherwise we use our docker containers to run builds - name: Run in Docker if: matrix.os == 'ubuntu-24.04' run: ./ci/run-docker.sh ${{ matrix.target }} + env: + RUSTFLAGS: ${{ matrix.flags }} ${{ env.RUSTFLAGS }} - name: Print test logs if available if: always() diff --git a/compiler-builtins/build.rs b/compiler-builtins/build.rs index 6e1d230e3..cb6871c8e 100644 --- a/compiler-builtins/build.rs +++ b/compiler-builtins/build.rs @@ -21,6 +21,8 @@ fn main() { println!("cargo::rustc-check-cfg=cfg(kernel_user_helpers)"); println!("cargo::rustc-check-cfg=cfg(feature, values(\"mem-unaligned\"))"); + branch_protection_cfg(); + // Emscripten's runtime includes all the builtins if target.os == "emscripten" { return; @@ -180,6 +182,25 @@ fn configure_check_cfg() { // FIXME: these come from libm and should be changed there println!("cargo::rustc-check-cfg=cfg(feature, values(\"checked\"))"); println!("cargo::rustc-check-cfg=cfg(assert_no_panic)"); + + println!("cargo::rustc-check-cfg=cfg(branch_protection, values(\"pac-ret\",\"bti\"))"); +} + +fn branch_protection_cfg() { + // flags: -Z branch-protection=pac-ret,bti,b-key + if let Some(rustflags) = env::var_os("CARGO_ENCODED_RUSTFLAGS") { + for mut flag in rustflags.to_string_lossy().split('\x1f') { + if flag.starts_with("-Z") { + flag = &flag["-Z".len()..]; + } + if let Some(branch_protection) = flag.strip_prefix("branch-protection=") { + for x in branch_protection.split(",") { + println!("cargo:rustc-cfg=branch_protection=\"{x}\"") + } + return; + } + } + } } #[cfg(feature = "c")] diff --git a/compiler-builtins/src/aarch64_outline_atomics.rs b/compiler-builtins/src/aarch64_outline_atomics.rs index 1520eb975..26ce14e40 100644 --- a/compiler-builtins/src/aarch64_outline_atomics.rs +++ b/compiler-builtins/src/aarch64_outline_atomics.rs @@ -401,3 +401,28 @@ foreach_ldadd!(add); foreach_ldclr!(and); foreach_ldeor!(xor); foreach_ldset!(or); + +core::arch::global_asm!( + " + .pushsection .note.gnu.property, \"a\" + .balign 8 + .word 4 + .word 0x10 + .word 0x5 // NT_GNU_PROPERTY_TYPE_0 + .asciz \"GNU\" + .word 0xc0000000 // GNU_PROPERTY_AARCH64_FEATURE_1_AND + .word 4 + ", + #[cfg(all(branch_protection = "bti", branch_protection = "pac-ret"))] + ".word 3", + #[cfg(all(not(branch_protection = "bti"), branch_protection = "pac-ret"))] + ".word 2", + #[cfg(all(branch_protection = "bti", not(branch_protection = "pac-ret")))] + ".word 1", + #[cfg(all(not(branch_protection = "bti"), not(branch_protection = "pac-ret")))] + ".word 0", + " + .word 0 + .popsection + " +);