public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 0/1] RISC-V: fix stack access before allocation.
@ 2022-11-28  5:29 Fei Gao
  2022-11-28  5:29 ` [PATCH 1/1] " Fei Gao
  0 siblings, 1 reply; 3+ messages in thread
From: Fei Gao @ 2022-11-28  5:29 UTC (permalink / raw)
  To: gcc-patches; +Cc: kito.cheng, palmer, Fei Gao

In current riscv stack frame allocation, 2 steps are used. The first step allocates memories at least for callee saved GPRs and FPRs, and the second step allocates the rest if stack size is greater than signed 12-bit range. But it's observed in some cases, like gcc.target/riscv/stack_frame.c in my patch, callee saved FPRs fail to be included in the first step allocation, so we get generated instructions like this:

	li	t0,-16384
	addi	sp,sp,-48
	addi	t0,t0,752
	...
	fsw	fs4,-4(sp) #issue here of accessing before allocation
	...
	add	sp,sp,t0

"fsw	fs4,-4(sp)" has issue here of accessing stack before allocation. Although "add	sp,sp,t0" reserves later the memory for fs4, it exposes a risk when an interrupt comes in between "fsw	fs4,-4(sp)" and "add	sp,sp,t0", resulting in a corruption in the stack storing fs4 after interrupt context saving and a failure to get the correct value of fs4 later.

This patch fixes issue above, adapts testcases identified in regression tests, and add a new testcase for the change.

Fei Gao (1):
  RISC-V: fix stack access before allocation.

 gcc/config/riscv/riscv.cc                     |  2 +-
 gcc/testsuite/gcc.target/riscv/pr93304.c      |  2 +-
 .../gcc.target/riscv/rvv/base/spill-11.c      |  3 +--
 gcc/testsuite/gcc.target/riscv/stack_frame.c  | 26 +++++++++++++++++++
 4 files changed, 29 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/stack_frame.c

-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread
* [PATCH 0/1] RISC-V: fix stack access before allocation.
@ 2022-11-28  5:28 Fei Gao
  0 siblings, 0 replies; 3+ messages in thread
From: Fei Gao @ 2022-11-28  5:28 UTC (permalink / raw)
  To: gcc-patches; +Cc: kito.cheng, palmer, Fei Gao

In current riscv stack frame allocation, 2 steps are used. The first step allocates memories at least for callee saved GPRs and FPRs, and the second step allocates the rest if stack size is greater than signed 12-bit range. But it's observed in some cases, like gcc.target/riscv/stack_frame.c in my patch, callee saved FPRs fail to be included in the first step allocation, so we get generated instructions like this:

	li	t0,-16384
	addi	sp,sp,-48
	addi	t0,t0,752
	...
	fsw	fs4,-4(sp) #issue here of accessing before allocation
	...
	add	sp,sp,t0

"fsw	fs4,-4(sp)" has issue here of accessing stack before allocation. Although "add	sp,sp,t0" reserves later the memory for fs4, it exposes a risk when an interrupt comes in between "fsw	fs4,-4(sp)" and "add	sp,sp,t0", resulting in a corruption in the stack storing fs4 after interrupt context saving and a failure to get the correct value of fs4 later.

This patch fixes issue above, adapts testcases identified in regression tests, and add a new testcase for the change.

Fei Gao (1):
  RISC-V: fix stack access before allocation.

 gcc/config/riscv/riscv.cc                     |  2 +-
 gcc/testsuite/gcc.target/riscv/pr93304.c      |  2 +-
 .../gcc.target/riscv/rvv/base/spill-11.c      |  3 +--
 gcc/testsuite/gcc.target/riscv/stack_frame.c  | 26 +++++++++++++++++++
 4 files changed, 29 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/stack_frame.c

-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-11-28  5:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-28  5:29 [PATCH 0/1] RISC-V: fix stack access before allocation Fei Gao
2022-11-28  5:29 ` [PATCH 1/1] " Fei Gao
  -- strict thread matches above, loose matches on Subject: below --
2022-11-28  5:28 [PATCH 0/1] " Fei Gao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).