target/riscv: Fix PC sync in trans_sspopchk for CFI exception handling
PATCH 1/1 target/riscv: Fix PC sync in trans_sspopchk for CFI exception handling
From: Max Chou <[email protected]>
Move gen_update_pc call before conditional logic to ensure consistent PC state regardless of execution path.
Previously, the host instructions generated to update the cpu_pc were only executed in the failure path when shadow stack validation failed. This created inconsistent PC synchronization.
This inconsistency caused issues in CF_PCREL mode where subsequent instructions calculated wrong relative offsets from stale pc_save values, and could lead to incorrect exception return addresses.
This fix ensures PC is always synchronized before any helper that might raise an exception, maintaining consistent translator state across all execution paths.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4118
[ahshehab: rebased on current master; file moved to
target/riscv/tcg/insn_trans/ and the ssp load is now 64-bit wide]
---
This is a repost of Max Chou's patch from 2025-11-05 [1], which did not receive any review. Rebased onto current master: the file moved to target/riscv/tcg/insn_trans/ and the ssp load is now a 64-bit load, so the original patch no longer applies.
I opened a GitLab issue (#4118) with a minimal, self-contained bare-metal reproducer for this bug. It runs an sspopchk that matches the shadow stack (the common case) followed by an auipc in the same translation block; on current master the auipc returns an address 4 bytes too low (exit 42) and with this patch it is correct (exit 0). The reproducer is included in the issue.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc +1 -1
@@ -32,6 +32,7 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) TCGLabel *skip = gen_new_label(); uint32_t tmp = (get_xl(ctx) == MXL_RV64) ? 8 : 4; TCGv data = tcg_temp_new(); + gen_update_pc(ctx, 0); TCGv_i64 wide_addr = tcg_temp_new_i64(); tcg_gen_ld_i64(wide_addr, tcg_env, offsetof(CPURISCVState, ssp)); tcg_gen_trunc_i64_tl(addr, wide_addr);
@@ -42,7 +43,6 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) tcg_gen_brcond_tl(TCG_COND_EQ, data, rs1, skip); tcg_gen_st8_i32(tcg_constant_i32(RISCV_EXCP_SW_CHECK_BCFI_TVAL), tcg_env, offsetof(CPURISCVState, sw_check_code)); - gen_update_pc(ctx, 0); gen_helper_raise_exception(tcg_env, tcg_constant_i32(RISCV_EXCP_SW_CHECK)); gen_set_label(skip);
Signature · 1 line
2.53.0
Quoted 17 lines · A-Shehab
From: Max Chou <[email protected]>
Move
gen_update_pccall before conditional logic to ensure consistent PC state regardless of execution path.Previously, the host instructions generated to update the
cpu_pcwere only executed in the failure path when shadow stack validation failed. This created inconsistent PC synchronization.This inconsistency caused issues in
CF_PCRELmode where subsequent instructions calculated wrong relative offsets from stalepc_savevalues, and could lead to incorrect exception return addresses.This fix ensures PC is always synchronized before any helper that might raise an exception, maintaining consistent translator state across all execution paths.
Quoted 7 lines
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4118
Signed-off-by Max Chou <[email protected]>[ahshehab: rebased on current master; file moved to
target/riscv/tcg/insn_trans/ and the ssp load is now 64-bit wide]Tested-by A-Shehab <[email protected]>Signed-off-by A-Shehab <[email protected]>---
Quoted 37 lines
This is a repost of Max Chou's patch from 2025-11-05 [1], which did not receive any review. Rebased onto current master: the file moved to target/riscv/tcg/insn_trans/ and the ssp load is now a 64-bit load, so the original patch no longer applies.
I opened a GitLab issue (#4118) with a minimal, self-contained bare-metal reproducer for this bug. It runs an sspopchk that matches the shadow stack (the common case) followed by an auipc in the same translation block; on current master the auipc returns an address 4 bytes too low (exit 42) and with this patch it is correct (exit 0). The reproducer is included in the issue.
[1] https://lore.kernel.org/qemu-devel/[email protected]/
target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc +1
-1@@ -32,6 +32,7 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) TCGLabel *skip = gen_new_label(); uint32_t tmp = (get_xl(ctx) == MXL_RV64) ? 8 : 4; TCGv data = tcg_temp_new(); + gen_update_pc(ctx, 0); TCGv_i64 wide_addr = tcg_temp_new_i64(); tcg_gen_ld_i64(wide_addr, tcg_env, offsetof(CPURISCVState, ssp)); tcg_gen_trunc_i64_tl(addr, wide_addr);@@ -42,7 +43,6 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) tcg_gen_brcond_tl(TCG_COND_EQ, data, rs1, skip); tcg_gen_st8_i32(tcg_constant_i32(RISCV_EXCP_SW_CHECK_BCFI_TVAL), tcg_env, offsetof(CPURISCVState, sw_check_code)); - gen_update_pc(ctx, 0); gen_helper_raise_exception(tcg_env, tcg_constant_i32(RISCV_EXCP_SW_CHECK)); gen_set_label(skip);
Thank you A-Shehab
I forgot to ping and update this patch
Quoted 69 lines · On Fri, Jul 31, 2026 at 02:20 A-Shehab <[email protected]> wrote:
From: Max Chou <[email protected]>
Move
gen_update_pccall before conditional logic to ensure consistent PC state regardless of execution path.Previously, the host instructions generated to update the
cpu_pcwere only executed in the failure path when shadow stack validation failed. This created inconsistent PC synchronization.This inconsistency caused issues in
CF_PCRELmode where subsequent instructions calculated wrong relative offsets from stalepc_savevalues, and could lead to incorrect exception return addresses.This fix ensures PC is always synchronized before any helper that might raise an exception, maintaining consistent translator state across all execution paths.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4118
Signed-off-by Max Chou <[email protected]>[ahshehab: rebased on current master; file moved to
target/riscv/tcg/insn_trans/ and the ssp load is now 64-bit wide]Tested-by A-Shehab <[email protected]>Signed-off-by A-Shehab <[email protected]>---
This is a repost of Max Chou's patch from 2025-11-05 [1], which did not receive any review. Rebased onto current master: the file moved to target/riscv/tcg/insn_trans/ and the ssp load is now a 64-bit load, so the original patch no longer applies.I opened a GitLab issue (#4118) with a minimal, self-contained bare-metal reproducer for this bug. It runs an sspopchk that matches the shadow stack (the common case) followed by an auipc in the same translation block; on current master the auipc returns an address 4 bytes too low (exit 42) and with this patch it is correct (exit 0). The reproducer is included in the issue.
[1]
https://lore.kernel.org/qemu-devel/[email protected]/target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc +1
-0@@ -32,6 +32,7 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) TCGLabel *skip = gen_new_label(); uint32_t tmp = (get_xl(ctx) == MXL_RV64) ? 8 : 4; TCGv data = tcg_temp_new(); + gen_update_pc(ctx, 0); TCGv_i64 wide_addr = tcg_temp_new_i64(); tcg_gen_ld_i64(wide_addr, tcg_env, offsetof(CPURISCVState, ssp));tcg_gen_trunc_i64_tl(addr, wide_addr);target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc +0
-1@@ -42,7 +43,6 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) tcg_gen_brcond_tl(TCG_COND_EQ, data, rs1, skip); tcg_gen_st8_i32(tcg_constant_i32(RISCV_EXCP_SW_CHECK_BCFI_TVAL), tcg_env, offsetof(CPURISCVState, sw_check_code)); - gen_update_pc(ctx, 0); gen_helper_raise_exception(tcg_env, tcg_constant_i32(RISCV_EXCP_SW_CHECK));gen_set_label(skip);--
2.53.0
👍
Ahmed Shehab reacted via Gmail
<https://www.google.com/gmail/about/?utm_source=gmail-in-product&utm_medium=et&utm_campaign=emojireactionemail#app>
{
"emoji": "👍",
"version": 1
}
Alistair
AppliedQuoted 18 lines · A-Shehab
From: Max Chou <[email protected]>
Move
gen_update_pccall before conditional logic to ensure consistent PC state regardless of execution path.Previously, the host instructions generated to update the
cpu_pcwere only executed in the failure path when shadow stack validation failed.
This created inconsistent PC synchronization.This inconsistency caused issues in
CF_PCRELmode where subsequent instructions calculated wrong relative offsets from stalepc_savevalues, and could lead to incorrect exception return addresses.This fix ensures PC is always synchronized before any helper that might raise an exception, maintaining consistent translator state across all execution paths.
Quoted 6 lines
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4118
Signed-off-by Max Chou <[email protected]>[ahshehab: rebased on current master; file moved to target/riscv/tcg/insn_trans/ and the ssp load is now 64-bit wide]
Tested-by A-Shehab <[email protected]>Signed-off-by A-Shehab <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 51 lines
---
This is a repost of Max Chou's patch from 2025-11-05 [1], which did not
receive any review. Rebased onto current master: the file moved to target/riscv/tcg/insn_trans/ and the ssp load is now a 64-bit load, so
the original patch no longer applies.I opened a GitLab issue (#4118) with a minimal, self-contained bare- metal
reproducer for this bug. It runs an sspopchk that matches the shadow stack
(the common case) followed by an auipc in the same translation block; on
current master the auipc returns an address 4 bytes too low (exit 42) and
with this patch it is correct (exit 0). The reproducer is included in the
issue.[1]
https://lore.kernel.org/qemu-devel/[email protected]/target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc +1
-0@@ -32,6 +32,7 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) TCGLabel *skip = gen_new_label(); uint32_t tmp = (get_xl(ctx) == MXL_RV64) ? 8 : 4; TCGv data = tcg_temp_new(); + gen_update_pc(ctx, 0); TCGv_i64 wide_addr = tcg_temp_new_i64(); tcg_gen_ld_i64(wide_addr, tcg_env, offsetof(CPURISCVState,ssp));
tcg_gen_trunc_i64_tl(addr, wide_addr);target/riscv/tcg/insn_trans/trans_rvzicfiss.c.inc +0
-1@@ -42,7 +43,6 @@ static bool trans_sspopchk(DisasContext *ctx, arg_sspopchk *a) tcg_gen_brcond_tl(TCG_COND_EQ, data, rs1, skip); tcg_gen_st8_i32(tcg_constant_i32(RISCV_EXCP_SW_CHECK_BCFI_TVAL), tcg_env, offsetof(CPURISCVState, sw_check_code)); - gen_update_pc(ctx, 0); gen_helper_raise_exception(tcg_env,
tcg_constant_i32(RISCV_EXCP_SW_CHECK));gen_set_label(skip);
Quoted 17 lines · A-Shehab
From: Max Chou <[email protected]>
Move
gen_update_pccall before conditional logic to ensure consistent PC state regardless of execution path.Previously, the host instructions generated to update the
cpu_pcwere only executed in the failure path when shadow stack validation failed. This created inconsistent PC synchronization.This inconsistency caused issues in
CF_PCRELmode where subsequent instructions calculated wrong relative offsets from stalepc_savevalues, and could lead to incorrect exception return addresses.This fix ensures PC is always synchronized before any helper that might raise an exception, maintaining consistent translator state across all execution paths.
Quoted 6 lines
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4118
Signed-off-by Max Chou <[email protected]>[ahshehab: rebased on current master; file moved to
target/riscv/tcg/insn_trans/ and the ssp load is now 64-bit wide]Tested-by A-Shehab <[email protected]>Signed-off-by A-Shehab <[email protected]>
I'm applying this to the current qemu stable series. In 11.0.x and 10.0.x, it is the original version of this patch (though maybe it is more productive to pick up other changes in this area to older stable branches and apply this change as-is).
Please let me know if I shouldn't pick it up.
Thanks,
/mjt