LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 47341 - Support -mstack-protector-guard=sysreg for arm64 and riscv
Summary: Support -mstack-protector-guard=sysreg for arm64 and riscv
Status: NEW
Alias: None
Product: clang
Classification: Unclassified
Component: -New Bugs (show other bugs)
Version: trunk
Hardware: PC Linux
: P enhancement
Assignee: Unassigned Clang Bugs
URL: https://github.com/ClangBuiltLinux/li...
Keywords:
Depends on:
Blocks: 4068
  Show dependency tree
 
Reported: 2020-08-28 00:04 PDT by Kees Cook
Modified: 2021-05-17 12:00 PDT (History)
7 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kees Cook 2020-08-28 00:04:58 PDT
The Linux kernel uses this for having a per-thread stack canary. Implementing this for both arm64 and riscv is desired.

For example, gcc's arm64 support is used like this in the kernel:

-mstack-protector-guard=sysreg
-mstack-protector-guard-reg=sp_el0
-mstack-protector-guard-offset=0

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=0a1213fa7432778b71a1c0166bf56660a3aab030


Here are discussions on the start of riscv support:

https://lore.kernel.org/lkml/CAJF2gTTr_ENhGRmjqNbGBhEc8y4D3YSqAResvwHuB1ykemKTUw@mail.gmail.com/
Comment 1 Nick Desaulniers 2021-04-20 16:07:51 PDT
SelectionDAGBuilder::visitSPDescriptorParent is probably the most relevant method for this. I'm not sure yet how best to modify getLoadStackGuard in order to change this from a load of a global Value to an aarch64 specific `mrs` instruction.

Here's a basic test case demonstrating the differences between these 3 new flags, vs without them (but still with -fstack-protector-strong).

https://godbolt.org/z/oEc1fvMPo
Comment 2 Nick Desaulniers 2021-04-20 17:51:36 PDT
Ah looks like I can just add custom expansion for AArch64::LOAD_STACK_GUARD in AArch64InstrInfo::expandPostRAPseudo() and that seems to work.
Comment 3 Nick Desaulniers 2021-04-20 18:55:56 PDT
aarch64: https://reviews.llvm.org/D100919
Comment 4 Nick Desaulniers 2021-05-17 11:52:05 PDT
https://reviews.llvm.org/rG0f417789192e74f9d2fad0f6aee4efc394257176 has landed for aarch64 with the caveats that we only support sp_el0 for valid sysreg, and we don't try too hard to support arbitrary int32_t's for offsets.
Comment 5 Nick Desaulniers 2021-05-17 12:00:06 PDT
For the Linux kernel, it looks like risc-v uses

-mstack-protector-guard=tls
-mstack-protector-guard-reg=tp
-mstack-protector-guard-offset=

It looks like ppc uses:

-mstack-protector-guard=tls
-mstack-protector-guard-reg={r13|r2}
-mstack-protector-guard-offset=

x86 uses:

-mstack-protector-guard-reg=fs
-mstack-protector-guard-symbol=__stack_chk_guard
-mstack-protector-guard=global

See https://bugs.llvm.org/show_bug.cgi?id=49209 for -mstack-protector-guard-symbol=.