Skip to content

[RISCV] Use the thread local stack protector for Android targets #87672

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

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Apr 4, 2024

Android supports per thread stack protectors that are individually managed and
initialized, which can provide stronger protections than using the global stack
protector cookie. This patch matches the convention for other architectures
targeting Android platforms.

ilovepi added 2 commits April 4, 2024 11:30
Created using spr 1.3.4
@llvmbot
Copy link
Member

llvmbot commented Apr 4, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Paul Kirth (ilovepi)

Changes

Android supports per thread stack protectors that are individually managed and
initialized, which can provide stronger protections than using the global stack
protector cookie. This patch matches the convention for other architectures
targeting Android platforms.


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

1 Files Affected:

  • (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+6)
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 279d8a435a04ca..5bd96d5e2c403c 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -20657,6 +20657,12 @@ Value *RISCVTargetLowering::getIRStackGuard(IRBuilderBase &IRB) const {
   if (Subtarget.isTargetFuchsia())
     return useTpOffset(IRB, -0x10);
 
+  // Android provides a fixed TLS slot for the stack cookie. See the definition
+  // of TLS_SLOT_STACK_GUARD in
+  // https://android.googlesource.com/platform/bionic/+/master/libc/platform/bionic/tls_defines.h
+  if (Subtarget.isTargetAndroid())
+    return useTpOffset(IRB, -0x18);
+
   return TargetLowering::getIRStackGuard(IRB);
 }
 

@ilovepi ilovepi marked this pull request as draft April 4, 2024 18:33
ilovepi added 2 commits April 4, 2024 11:54
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
@ilovepi ilovepi marked this pull request as ready for review April 4, 2024 18:55
Copy link
Contributor

@enh-google enh-google left a comment

Choose a reason for hiding this comment

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

s/master/main/ in the url to get the current version. (master "works" but it's frozen in time; main will track future changes.)

otherwise lgtm...

@topperc
Copy link
Collaborator

topperc commented Apr 4, 2024

s/master/main/ in the url to get the current version. (master "works" but it's frozen in time; main will track future changes.)

otherwise lgtm...

Probably someone should update AArch64 which has the same comment?

ilovepi added 2 commits April 4, 2024 15:31
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
Copy link
Contributor

@rprichard rprichard left a comment

Choose a reason for hiding this comment

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

LGTM

@ilovepi
Copy link
Contributor Author

ilovepi commented Apr 5, 2024

s/master/main/ in the url to get the current version. (master "works" but it's frozen in time; main will track future changes.)
otherwise lgtm...

Probably someone should update AArch64 which has the same comment?

Done in #87726

@ilovepi ilovepi changed the base branch from users/ilovepi/spr/main.riscv-use-the-thread-local-stack-protector-for-android-targets to main April 5, 2024 16:47
@ilovepi ilovepi changed the base branch from main to users/ilovepi/spr/main.riscv-use-the-thread-local-stack-protector-for-android-targets April 5, 2024 16:50
ilovepi added 2 commits April 5, 2024 10:07
Created using spr 1.3.4

[skip ci]
Created using spr 1.3.4
@ilovepi ilovepi changed the base branch from users/ilovepi/spr/main.riscv-use-the-thread-local-stack-protector-for-android-targets to main April 24, 2024 15:58
Created using spr 1.3.4
@ilovepi
Copy link
Contributor Author

ilovepi commented May 10, 2024

I plan to land this today, after presubmits are green.

@ilovepi ilovepi merged commit d95f7c9 into main May 13, 2024
4 checks passed
@ilovepi ilovepi deleted the users/ilovepi/spr/riscv-use-the-thread-local-stack-protector-for-android-targets branch May 13, 2024 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants