llvmorg-github-actions[bot] wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-driver

Author: Sharjeel Khan (Sharjeel-Khan)

<details>
<summary>Changes</summary>

We enable it by default in the Android Ecosystem so it makes sense to set it as 
the default for Android in the Clang driver. This change will make sure anyone 
compiling for Android (like through NDK) automatically gets PAC/BTI. It should 
not cause issues with older Android devices since they become nops.

Fixes: https://github.com/android/ndk/issues/1914

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


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+2) 
- (modified) clang/lib/Driver/ToolChains/Clang.cpp (+2-2) 
- (modified) clang/test/Driver/aarch64-security-options.c (+12) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 3c6694f510952..dc20b38b0a7a7 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -580,6 +580,8 @@ features cannot lower the translation-unit ABI level;
 
 #### Android Support
 
+- Enabled PAC and BTI by default for AArch64 Android targets.
+
 #### Windows Support
 
 - Fixed a bug where Clang did not match the MSVC ABI on Arm64 when an
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index dabc8c8d964d6..44573460ce72c 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1392,7 +1392,7 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, 
const ArgList &Args,
                                        options::OPT_mbranch_protection_EQ)
                      : Args.getLastArg(options::OPT_mbranch_protection_EQ);
   if (!A) {
-    if (Triple.isOSOpenBSD() && isAArch64) {
+    if ((Triple.isOSOpenBSD() || Triple.isAndroid()) && isAArch64) {
       CmdArgs.push_back("-msign-return-address=non-leaf");
       CmdArgs.push_back("-msign-return-address-key=a_key");
       CmdArgs.push_back("-mbranch-target-enforce");
@@ -1414,7 +1414,7 @@ static void CollectARMPACBTIOptions(const ToolChain &TC, 
const ArgList &Args,
       D.Diag(diag::err_drv_unsupported_option_argument)
           << A->getSpelling() << Scope;
     Key = "a_key";
-    IndirectBranches = Triple.isOSOpenBSD() && isAArch64;
+    IndirectBranches = (Triple.isOSOpenBSD() || Triple.isAndroid()) && 
isAArch64;
     BranchProtectionPAuthLR = false;
     GuardedControlStack = false;
   } else {
diff --git a/clang/test/Driver/aarch64-security-options.c 
b/clang/test/Driver/aarch64-security-options.c
index 967a6122a333d..125e90b2b51fb 100644
--- a/clang/test/Driver/aarch64-security-options.c
+++ b/clang/test/Driver/aarch64-security-options.c
@@ -33,6 +33,18 @@
 // RUN: %clang --target=aarch64 -### -o /dev/null -mbranch-protection=standard 
/dev/null 2>&1 | \
 // RUN: FileCheck --allow-empty %s --check-prefix=LINKER-DRIVER
 
+// Check that Android enables PAC and BTI by default on AArch64.
+// RUN: %clang --target=aarch64-linux-android -### -c %s 2>&1 | \
+// RUN: FileCheck %s --check-prefix=RA-NON-LEAF --check-prefix=KEY-A 
--check-prefix=BTE-ON --check-prefix=GCS-OFF --check-prefix=WARN
+
+// Check that the Android default can be overridden.
+// RUN: %clang --target=aarch64-linux-android -mbranch-protection=none -### -c 
%s 2>&1 | \
+// RUN: FileCheck %s --check-prefix=RA-OFF --check-prefix=KEY 
--check-prefix=BTE-OFF --check-prefix=GCS-OFF --check-prefix=WARN
+
+// Check that Android enables BTI by default when -msign-return-address is 
passed.
+// RUN: %clang --target=aarch64-linux-android -msign-return-address=non-leaf 
-### -c %s 2>&1 | \
+// RUN: FileCheck %s --check-prefix=RA-NON-LEAF --check-prefix=KEY-A 
--check-prefix=BTE-ON --check-prefix=GCS-OFF --check-prefix=WARN
+
 // WARN-NOT: warning: ignoring '-mbranch-protection=' option because the 
'aarch64' architecture does not support it [-Wbranch-protection]
 
 // RA-OFF: "-msign-return-address=none"

``````````

</details>


https://github.com/llvm/llvm-project/pull/218066
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to