On 10/23/26 3:37 AM, Michiel Derhaeg wrote:
From: Malhar Anand Badve <[email protected]>
This implementation aligns with the existing LLVM implementation as much as
possible.
The gp register holds the shadow call stack pointer. Each function that
saves the return address to the stack gets a push in the prologue and a
pop in the epilogue, on rv64:
addi gp,gp,8
sd ra,-8(gp)
...
ld t0,-8(gp)
addi gp,gp,-8
jr t0
Linker relaxation that uses gp to address global data has to be disabled
when linking, with --no-relax-gp in the case of GNU ld. This relaxation
is disabled by default in the case of lld.
Exception handling is not yet supported, further changes to libgcc will
be needed.
If hardware shadow stack (Zicfiss) is enabled, it will be used instead.
User will need to provide startup code that initializes gp with the base
of the shadow call stack before any instrumented function runs.
Regtested on rv32gc and rv64gc.
gcc/ChangeLog:
* config/riscv/riscv.cc: Include asan.h.
(riscv_emit_scs_push): New function.
(riscv_emit_scs_pop): Likewise.
(riscv_for_each_saved_reg): Restore the return address to t0 for
shadow call stack instrumentation.
(riscv_expand_prologue): Push the return address onto the shadow
call stack.
(riscv_expand_epilogue): Pop the return address from the shadow
call stack.
(riscv_epilogue_uses): Mark gp as used by the epilogue.
(riscv_can_use_return_insn): Return false for shadow call stack
instrumentation.
(need_shadow_stack_push_pop_p): Rename to...
(need_zicfiss_push_pop_p): ...this.
(need_scs_push_pop_p): New function for checking
whether the current function requires shadow call stack
instrumentation.
(TARGET_HAVE_SHADOW_CALL_STACK): Mark that RISC-V has support for
shadow call stack.
* config/riscv/riscv.h (need_shadow_stack_push_pop_p): Rename to...
(need_zicfiss_push_pop_p): ...this.
(need_scs_push_pop_p): Declare.
* config/riscv/riscv.md (save_stack_nonlocal): Update for the
renamed predicate.
(restore_stack_nonlocal): Likewise.
* cppbuiltin.cc (define_builtin_macros_for_compilation_flags):
Define __SANITIZE_SHADOW_CALL_STACK__.
* doc/cpp.texi: Document the builtin macro mentioned above.
* doc/invoke.texi: Document RISC-V changes for
-fsanitize=shadow-call-stack.
gcc/testsuite/ChangeLog:
* gcc.target/riscv/shadow_call_stack-1.c: New test.
* gcc.target/riscv/shadow_call_stack-2.c: New test.
* gcc.target/riscv/shadow_call_stack-3.c: New test.
Co-authored-by: Michiel Derhaeg <[email protected]>
Signed-off-by: Malhar Anand Badve <[email protected]>
You probably want to address some of these Sashiko issues. I looked at
them from a high level and they look generally real.
https://gcc.sashiko.dev/#/patchset/20261023093753.22304-1-michiel%40derhaeg.be
Jeff