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]>
---
gcc/config/riscv/riscv.cc | 93 +++++++++++++++----
gcc/config/riscv/riscv.h | 3 +-
gcc/config/riscv/riscv.md | 4 +-
gcc/cppbuiltin.cc | 3 +
gcc/doc/cpp.texi | 4 +
gcc/doc/invoke.texi | 16 +++-
.../gcc.target/riscv/shadow_call_stack-1.c | 68 ++++++++++++++
.../gcc.target/riscv/shadow_call_stack-2.c | 51 ++++++++++
.../gcc.target/riscv/shadow_call_stack-3.c | 23 +++++
9 files changed, 244 insertions(+), 21 deletions(-)
create mode 100644 gcc/testsuite/gcc.target/riscv/shadow_call_stack-1.c
create mode 100644 gcc/testsuite/gcc.target/riscv/shadow_call_stack-2.c
create mode 100644 gcc/testsuite/gcc.target/riscv/shadow_call_stack-3.c
diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 47b20b8cdb4..1c9022ae68f 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -82,6 +82,7 @@ along with GCC; see the file COPYING3. If not see
#include "cgraph.h"
#include "langhooks.h"
#include "gimplify.h"
+#include "asan.h"
/* This file should be included last. */
#include "target-def.h"
@@ -9954,7 +9955,8 @@ riscv_for_each_saved_reg (poly_int64 sp_offset,
riscv_save_restore_fn fn,
}
}
- if (need_shadow_stack_push_pop_p () && epilogue && !sibcall_p
+ if ((need_zicfiss_push_pop_p () || need_scs_push_pop_p ())
+ && epilogue && !sibcall_p
&& !(maybe_eh_return && crtl->calls_eh_return)
&& (regno == RETURN_ADDR_REGNUM)
&& !cfun->machine->interrupt_handler_p)
@@ -10553,6 +10555,41 @@ riscv_allocate_and_probe_stack_space (rtx temp1,
HOST_WIDE_INT size)
}
}
+/* Push the return address onto the shadow call stack. */
+
+static void
+riscv_emit_scs_push (void)
+{
+ rtx gp = gen_rtx_REG (Pmode, GP_REGNUM);
+
+ rtx_insn *insn
+ = emit_insn (gen_add3_insn (gp, gp, GEN_INT (UNITS_PER_WORD)));
+ add_reg_note (insn, REG_CFA_VAL_EXPRESSION,
+ gen_rtx_SET (gp, plus_constant (Pmode, gp, -UNITS_PER_WORD)));
+ RTX_FRAME_RELATED_P (insn) = 1;
+
+ emit_move_insn (gen_rtx_MEM (Pmode,
+ plus_constant (Pmode, gp, -UNITS_PER_WORD)),
+ gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM));
+}
+
+/* Pop the return address from the shadow call stack into REG. */
+
+static void
+riscv_emit_scs_pop (rtx reg)
+{
+ rtx gp = gen_rtx_REG (Pmode, GP_REGNUM);
+
+ emit_move_insn (reg,
+ gen_rtx_MEM (Pmode,
+ plus_constant (Pmode, gp, -UNITS_PER_WORD)));
+
+ rtx_insn *insn
+ = emit_insn (gen_add3_insn (gp, gp, GEN_INT (-UNITS_PER_WORD)));
+ add_reg_note (insn, REG_CFA_RESTORE, gp);
+ RTX_FRAME_RELATED_P (insn) = 1;
+}
+
/* Expand the "prologue" pattern. */
void
@@ -10572,8 +10609,10 @@ riscv_expand_prologue (void)
if (cfun->machine->naked_p)
return;
- if (need_shadow_stack_push_pop_p ())
+ if (need_zicfiss_push_pop_p ())
emit_insn (gen_sspush (Pmode, gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM)));
+ else if (need_scs_push_pop_p ())
+ riscv_emit_scs_push ();
/* Prefer multi-push to save-restore libcall. */
if (riscv_use_multi_push (frame))
@@ -11091,7 +11130,8 @@ riscv_expand_epilogue (int style)
frame->mask = mask;
frame->fmask = fmask;
bool use_popret = style == NORMAL_RETURN
- && !need_shadow_stack_push_pop_p ();
+ && !(need_zicfiss_push_pop_p ()
+ || need_scs_push_pop_p ());
riscv_gen_multi_pop_insn (use_popret, frame->mask, multipop_size);
if (use_popret)
return;
@@ -11112,16 +11152,22 @@ riscv_expand_epilogue (int style)
emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx,
EH_RETURN_STACKADJ_RTX));
- if (need_shadow_stack_push_pop_p ()
- && !((style == EXCEPTION_RETURN) && crtl->calls_eh_return))
- {
- if (BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM)
+ /* Must match the t0 redirect condition in riscv_for_each_saved_reg. */
+ bool stack_return_address_in_t0
+ = BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM)
&& style != SIBCALL_RETURN
&& !cfun->machine->interrupt_handler_p
- && !use_multi_pop)
- emit_insn (gen_sspopchk (Pmode, t0));
- else
- emit_insn (gen_sspopchk (Pmode, ra));
+ && !use_multi_pop;
+
+ /* Handle the shadow stack epilogue expand. */
+ if (!((style == EXCEPTION_RETURN) && crtl->calls_eh_return))
+ {
+ rtx addr_reg = stack_return_address_in_t0 ? t0 : ra;
+
+ if (need_zicfiss_push_pop_p ())
+ emit_insn (gen_sspopchk (Pmode, addr_reg));
+ else if (need_scs_push_pop_p ())
+ riscv_emit_scs_pop (addr_reg);
}
/* Return from interrupt. */
@@ -11142,11 +11188,9 @@ riscv_expand_epilogue (int style)
}
else if (style != SIBCALL_RETURN)
{
- if (need_shadow_stack_push_pop_p ()
+ if ((need_zicfiss_push_pop_p () || need_scs_push_pop_p ())
&& !((style == EXCEPTION_RETURN) && crtl->calls_eh_return)
- && BITSET_P (cfun->machine->frame.mask, RETURN_ADDR_REGNUM)
- && !cfun->machine->interrupt_handler_p
- && !use_multi_pop)
+ && stack_return_address_in_t0)
emit_jump_insn (gen_simple_return_internal (t0));
else
emit_jump_insn (gen_simple_return_internal (ra));
@@ -11161,6 +11205,9 @@ riscv_epilogue_uses (unsigned int regno)
if (regno == RETURN_ADDR_REGNUM)
return true;
+ if (regno == GP_REGNUM && sanitize_flags_p (SANITIZE_SHADOW_CALL_STACK))
+ return true;
+
if (epilogue_completed && cfun->machine->interrupt_handler_p)
{
/* An interrupt function restores temp regs, so we must indicate that
@@ -11348,7 +11395,8 @@ riscv_can_use_return_insn (void)
{
return (reload_completed && known_eq (cfun->machine->frame.total_size, 0)
&& ! cfun->machine->interrupt_handler_p
- && ! need_shadow_stack_push_pop_p ());
+ && ! need_zicfiss_push_pop_p ()
+ && ! need_scs_push_pop_p ());
}
/* Given that there exists at least one variable that is set (produced)
@@ -16096,11 +16144,19 @@ bool is_zicfilp_p ()
return false;
}
-bool need_shadow_stack_push_pop_p ()
+bool
+need_zicfiss_push_pop_p ()
{
return is_zicfiss_p () && riscv_save_return_addr_reg_p ();
}
+bool
+need_scs_push_pop_p ()
+{
+ return sanitize_flags_p (SANITIZE_SHADOW_CALL_STACK)
+ && riscv_save_return_addr_reg_p ();
+}
+
/* Synthesize OPERANDS[0] = OPERANDS[1] CODE OPERANDS[2].
OPERANDS[0] and OPERANDS[1] will be a REG and may be the same
@@ -17113,6 +17169,9 @@ riscv_memtag_tag_bitsize ()
#define TARGET_SHRINK_WRAP_SET_HANDLED_COMPONENTS \
riscv_set_handled_components
+#undef TARGET_HAVE_SHADOW_CALL_STACK
+#define TARGET_HAVE_SHADOW_CALL_STACK true
+
/* The generic ELF target does not always have TLS support. */
#ifdef HAVE_AS_TLS
#undef TARGET_HAVE_TLS
diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
index 7ba2a726ac4..1aa675aa733 100644
--- a/gcc/config/riscv/riscv.h
+++ b/gcc/config/riscv/riscv.h
@@ -1250,7 +1250,8 @@ extern poly_int64 riscv_v_adjust_precision (enum
machine_mode, int);
extern poly_int64 riscv_v_adjust_bytesize (enum machine_mode, int);
extern bool is_zicfiss_p ();
extern bool is_zicfilp_p ();
-extern bool need_shadow_stack_push_pop_p ();
+extern bool need_zicfiss_push_pop_p ();
+extern bool need_scs_push_pop_p ();
/* The number of bits and bytes in a RVV vector. */
#define BITS_PER_RISCV_VECTOR (poly_uint16 (riscv_vector_chunks *
riscv_bytes_per_vector_chunk * 8))
#define BYTES_PER_RISCV_VECTOR (poly_uint16 (riscv_vector_chunks *
riscv_bytes_per_vector_chunk))
diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index da6394d1ae6..1ab72f36c5a 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -4529,7 +4529,7 @@
{
rtx stack_slot;
- if (need_shadow_stack_push_pop_p ())
+ if (need_zicfiss_push_pop_p ())
{
/* Copy shadow stack pointer to the first slot
and stack pointer to the second slot. */
@@ -4558,7 +4558,7 @@
{
rtx stack_slot;
- if (need_shadow_stack_push_pop_p ())
+ if (need_zicfiss_push_pop_p ())
{
rtx t0 = gen_rtx_REG (Pmode, RISCV_PROLOGUE_TEMP_REGNUM);
/* Restore shadow stack pointer from the first slot
diff --git a/gcc/cppbuiltin.cc b/gcc/cppbuiltin.cc
index 0dbe2591178..96f43ef211a 100644
--- a/gcc/cppbuiltin.cc
+++ b/gcc/cppbuiltin.cc
@@ -99,6 +99,9 @@ define_builtin_macros_for_compilation_flags (cpp_reader
*pfile)
if (flag_sanitize & SANITIZE_THREAD)
cpp_define (pfile, "__SANITIZE_THREAD__");
+ if (flag_sanitize & SANITIZE_SHADOW_CALL_STACK)
+ cpp_define (pfile, "__SANITIZE_SHADOW_CALL_STACK__");
+
if (optimize_size)
cpp_define (pfile, "__OPTIMIZE_SIZE__");
if (optimize)
diff --git a/gcc/doc/cpp.texi b/gcc/doc/cpp.texi
index 7694bb651a4..2aae3a58e7c 100644
--- a/gcc/doc/cpp.texi
+++ b/gcc/doc/cpp.texi
@@ -2377,6 +2377,10 @@ or @option{-fsanitize=kernel-address} are in use.
@item __SANITIZE_THREAD__
This macro is defined, with value 1, when @option{-fsanitize=thread} is in use.
+@item __SANITIZE_SHADOW_CALL_STACK__
+This macro is defined, with value 1, when @option{-fsanitize=shadow-call-stack}
+is in use.
+
@item __TIMESTAMP__
This macro expands to a string constant that describes the date and time
of the last modification of the current source file. The string constant
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 66d80f17e74..d88b2a04bf0 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -17590,7 +17590,7 @@ shadow call stack in the function prologue and
restoring the return address
from the shadow call stack in the function epilogue. Instrumentation only
occurs in functions that need to save the return address to the stack.
-Currently it only supports the aarch64 platform. It is specifically
+Currently it only supports the AArch64 and RISC-V platforms. It is
specifically
designed for linux kernels that enable the CONFIG_SHADOW_CALL_STACK option.
For the user space programs, runtime support is not currently provided
in libc and libgcc. Users who want to use this feature in user space need
@@ -17604,6 +17604,20 @@ compiled with ShadowCallStack must be compiled with
the flag
@option{-ffixed-x18} might clobber @code{x18} and so corrupt the shadow
stack pointer.
+On RISC-V, the instrumentation uses the platform register @code{gp}.
+Unlike AArch64, @code{gp} is a fixed register, hence @option{-ffixed-gp}
+is not needed. Linker relaxation that uses @code{gp} to address global data
+must however be disabled when linking, which GNU @command{ld} does with the
+@option{--no-relax-gp} option.
+
+Runtime support is the responsibility of the user on RISC-V: @code{gp} must be
+set to the base of the shadow call stack by the startup code before any
+instrumented function runs.
+
+If the @code{zicfiss} extension is enabled and @option{-fcf-protection=return}
+is given, its hardware shadow stack is used instead and
+@option{-fsanitize=shadow-call-stack} has no further effect.
+
Also, because there is no userspace runtime support, code compiled with
ShadowCallStack cannot use exception handling. Use @option{-fno-exceptions}
to turn off exceptions.
diff --git a/gcc/testsuite/gcc.target/riscv/shadow_call_stack-1.c
b/gcc/testsuite/gcc.target/riscv/shadow_call_stack-1.c
new file mode 100644
index 00000000000..b8f4fd9412f
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/shadow_call_stack-1.c
@@ -0,0 +1,68 @@
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=shadow-call-stack -funwind-tables
-fomit-frame-pointer" } */
+/* { dg-skip-if "" { *-*-* } {"-O0" "-O2" "-O3" "-Og" "-Os" "-Oz" "-flto"} } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+#if __SANITIZE_SHADOW_CALL_STACK__ != 1
+#error "__SANITIZE_SHADOW_CALL_STACK__ is not defined to 1"
+#endif
+
+int g (int);
+
+/*
+**f1:
+** li a0,0
+** ret
+*/
+int f1 (void) { return 0; }
+
+/*
+**f2:
+** addi gp,gp,[48]
+**...
+** s[wd] ra,-[48]\(gp\)
+**...
+** l[wd] t0,-[48]\(gp\)
+**...
+** addi gp,gp,-[48]
+** jr t0
+*/
+int f2 (void) { asm volatile ("":::"ra"); return 0; }
+
+/*
+**f3:
+** addi gp,gp,[48]
+**...
+** s[wd] ra,-[48]\(gp\)
+**...
+** call g
+**...
+** l[wd] t0,-[48]\(gp\)
+**...
+** addi gp,gp,-[48]
+** jr t0
+*/
+int f3 (int a) { return g (a) + 1; }
+
+/*
+**f4:
+** addi sp,sp,-16
+** s[wd] ra,(8|12)\(sp\)
+** call g
+** addiw? a0,a0,1
+** l[wd] ra,(8|12)\(sp\)
+** addi sp,sp,16
+** jr ra
+*/
+int __attribute__((no_sanitize("shadow-call-stack"))) f4 (int a)
+{
+ return g (a) + 1;
+}
+
+/*
+**f5:
+*/
+void __attribute__((naked)) f5 (void) {}
+
+/* { dg-final { scan-assembler-times {\.cfi_escape 0x16,0x3,0x2,0x73,0x7[c8]}
2 } } */
+/* { dg-final { scan-assembler-times {\.cfi_restore 3} 2 } } */
diff --git a/gcc/testsuite/gcc.target/riscv/shadow_call_stack-2.c
b/gcc/testsuite/gcc.target/riscv/shadow_call_stack-2.c
new file mode 100644
index 00000000000..1e8087c79cd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/shadow_call_stack-2.c
@@ -0,0 +1,51 @@
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=shadow-call-stack -fno-omit-frame-pointer
-foptimize-sibling-calls" } */
+/* { dg-skip-if "" { *-*-* } {"-O0" "-O2" "-O3" "-Og" "-Os" "-Oz" "-flto"} } */
+/* { dg-final { check-function-bodies "**" "" } } */
+
+int g (int);
+
+/*
+**f1:
+** addi gp,gp,[48]
+**...
+** s[wd] ra,-[48]\(gp\)
+**...
+** l[wd] ra,-[48]\(gp\)
+**...
+** addi gp,gp,-[48]
+** tail g
+*/
+int f1 (int a) { return g (a); }
+
+/*
+**f2:
+** addi gp,gp,[48]
+**...
+** s[wd] ra,-[48]\(gp\)
+**...
+** l[wd] ra,-[48]\(gp\)
+**...
+** addi gp,gp,-[48]
+** mret
+*/
+__attribute__((interrupt("machine"))) void f2 (void)
+{
+ asm volatile ("":::"ra");
+}
+
+/*
+**f3:
+** addi gp,gp,[48]
+**...
+** s[wd] ra,-[48]\(gp\)
+**...
+** l[wd] ra,-[48]\(gp\)
+**...
+** addi gp,gp,-[48]
+** sret
+*/
+__attribute__((interrupt("supervisor"))) void f3 (void)
+{
+ asm volatile ("":::"ra");
+}
diff --git a/gcc/testsuite/gcc.target/riscv/shadow_call_stack-3.c
b/gcc/testsuite/gcc.target/riscv/shadow_call_stack-3.c
new file mode 100644
index 00000000000..78148d765f8
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/shadow_call_stack-3.c
@@ -0,0 +1,23 @@
+/* { dg-do compile } */
+/* { dg-options "-fsanitize=shadow-call-stack -fomit-frame-pointer" } */
+/* { dg-skip-if "" { *-*-* } {"-O0" "-O1" "-O2" "-O3" "-Og" "-Oz" "-flto"} } */
+/* { dg-additional-options "-march=rv32imac_zcmp -mabi=ilp32" { target rv32 }
} */
+/* { dg-additional-options "-march=rv64imac_zcmp -mabi=lp64" { target rv64 } }
*/
+/* { dg-final { check-function-bodies "**" "" } } */
+
+/*
+**f1:
+** addi gp,gp,[48]
+**...
+** s[wd] ra,-[48]\(gp\)
+**...
+** cm\.push \{ra\}, -16
+**...
+** cm\.pop \{ra\}, 16
+**...
+** l[wd] ra,-[48]\(gp\)
+**...
+** addi gp,gp,-[48]
+** jr ra
+*/
+int f1 (int a) { asm volatile ("":::"ra"); return a; }
--
2.55.0