public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jeff Law <jeffreyalaw@gmail.com>
To: Fei Gao <gaofei@eswincomputing.com>, gcc-patches@gcc.gnu.org
Cc: kito.cheng@gmail.com, palmer@dabbelt.com
Subject: Re: [PATCH 1/1] RISC-V: fix stack access before allocation.
Date: Mon, 28 Nov 2022 09:57:14 -0700	[thread overview]
Message-ID: <eb9b0714-6e2e-e755-e4e8-b57df0739fb7@gmail.com> (raw)
In-Reply-To: <20221128052829.36087-2-gaofei@eswincomputing.com>


On 11/27/22 22:28, Fei Gao wrote:
> 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.
>
> gcc/ChangeLog:
>
>          * config/riscv/riscv.cc (riscv_first_stack_step):
>
> gcc/testsuite/ChangeLog:
>
>          * gcc.target/riscv/pr93304.c: Adapt testcase for the change, constrain match to assembly instructions only.
>          * gcc.target/riscv/rvv/base/spill-11.c: Adapt testcase for the change.
>          * gcc.target/riscv/stack_frame.c: New test.

They key here is that the MIN_FIRST_STEP wasn't including the FP save 
space.  The stack layout diagram before riscv_stack_align, combined with 
the info you provided made that pretty clear.


Good job tracking it down -- these can be tough to reproduce and thus 
tough to debug/fix.


I made minor adjustments to the ChangeLog and committed your change to 
the trunk.


Thanks,

Jeff




  reply	other threads:[~2022-11-28 16:57 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-28  5:28 [PATCH 0/1] " Fei Gao
2022-11-28  5:28 ` [PATCH 1/1] " Fei Gao
2022-11-28 16:57   ` Jeff Law [this message]
2022-11-28  5:29 [PATCH 0/1] " Fei Gao
2022-11-28  5:29 ` [PATCH 1/1] " Fei Gao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=eb9b0714-6e2e-e755-e4e8-b57df0739fb7@gmail.com \
    --to=jeffreyalaw@gmail.com \
    --cc=gaofei@eswincomputing.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=kito.cheng@gmail.com \
    --cc=palmer@dabbelt.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).