target/riscv: Coverity fixes
cover target/riscv: Coverity fixes
Hi,
This series contains Coverity fixes for issues found in the latest RISC-V pull made yesterday.
Coverity CIDs being resolved: 1590355, 1590356, 1590357, 1590358 and 1590359.
Patches based on master.
Daniel Henrique Barboza (5):
target/riscv/csr.c: fix deadcode in rmw_xireg() target/riscv/csr.c: fix 'ret' deadcode in rmw_xireg() target/riscv/csr.c: fix deadcode in rmw_xiregi() target/riscv/csr.c: fix deadcode in aia_smode32() target/riscv/cpu_helper.c: fix bad_shift in riscv_cpu_interrupt()
target/riscv/cpu_helper.c | 4 +++- target/riscv/csr.c | 24 +++--------------------- 2 files changed, 6 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.1
PATCH 1/5 target/riscv/csr.c: fix deadcode in rmw_xireg()
Coverity found a DEADCODE issue in rmw_xireg() claiming that we can't reach 'RISCV_EXCP_VIRT_INSTRUCTION_FAULT' at the 'done' label:
done:
if (ret) {
return (env->virt_enabled && virt) ?
RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST;
}
return RISCV_EXCP_NONE;This happens because the 'virt' flag, which is only used by 'done', is set to 'false' and it will always remain 'false' in any condition where we'll jump to 'done':
switch (csrno) {
(...)
case CSR_VSIREG:
isel = env->vsiselect;
virt = true;
break;
default:
goto done;
};'virt = true' will never reach 'done' because we have a if/else-if/else block right before the label that will always return:
if (xiselect_aia_range(isel)) {
return ...
} else if (...) {
return ...
} else {
return RISCV_EXCP_ILLEGAL_INST;
}All this means that we can preserve the current logic by reducing the 'done' label to:
done:
if (ret) {
return RISCV_EXCP_ILLEGAL_INST;
}
return RISCV_EXCP_NONE;The flag 'virt' is now unused. Remove it.
Fix the 'goto done' identation while we're at it.
Resolves: Coverity CID 1590359
--- target/riscv/csr.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)
target/riscv/csr.c +2 -5
@@ -2658,7 +2658,6 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { - bool virt = false; int ret = -EINVAL;target_ulong isel;
@@ -2680,10 +2679,9 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno,break ;case CSR_VSIREG : isel = env->vsiselect; - virt = true;break ;default : - goto done; + goto done; };/*
@@ -2705,8 +2703,7 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, done:if (ret) { -return (env->virt_enabled && virt) ? -RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; +return RISCV_EXCP_ILLEGAL_INST ; }return RISCV_EXCP_NONE ; }
Signature · 1 line
2.47.1
Re: [PATCH 1/5] target/riscv/csr.c: fix deadcode in rmw_xireg()
Quoted 45 lines · Daniel Henrique Barboza
Coverity found a DEADCODE issue in
rmw_xireg()claiming that we can't reach 'RISCV_EXCP_VIRT_INSTRUCTION_FAULT' at the 'done' label:done:
if (ret) { return (env->virt_enabled && virt) ? RISCV_EXCP_VIRT_INSTRUCTION_FAULT : RISCV_EXCP_ILLEGAL_INST; } return RISCV_EXCP_NONE;This happens because the 'virt' flag, which is only used by 'done', is set to 'false' and it will always remain 'false' in any condition where we'll jump to 'done':
switch (csrno) { (...) case CSR_VSIREG: isel = env->vsiselect; virt = true; break; default: goto done; };'virt = true' will never reach 'done' because we have a if/else-if/else block right before the label that will always return:
if (xiselect_aia_range(isel)) { return ... } else if (...) { return ... } else { return RISCV_EXCP_ILLEGAL_INST; }All this means that we can preserve the current logic by reducing the 'done' label to:
done:
if (ret) { return RISCV_EXCP_ILLEGAL_INST; } return RISCV_EXCP_NONE;
Quoted 7 lines
The flag 'virt' is now unused. Remove it.
Fix the 'goto done' identation while we're at it.
Resolves: Coverity CID 1590359
Fixes dc0280723d ("target/riscv: Decouple AIA processing from xiselect and xireg")Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 42 lines
--- target/riscv/csr.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-)target/riscv/csr.c +2
-6@@ -2658,7 +2658,6 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, target_ulong *val, target_ulong new_val, target_ulong wr_mask) { - bool virt = false; int ret = -EINVAL;target_ulong isel;@@ -2680,10 +2679,9 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno,break ;case CSR_VSIREG : isel = env->vsiselect; - virt = true;break ;default : - goto done; + goto done; };/* @@ -2705,8 +2703,7 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, done:if (ret) { -return (env->virt_enabled && virt) ? -RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; +return RISCV_EXCP_ILLEGAL_INST ; }return RISCV_EXCP_NONE ; } --2.47.1
PATCH 2/5 target/riscv/csr.c: fix 'ret' deadcode in rmw_xireg()
In reply to Daniel Henrique BarbozaCoverity found a second DEADCODE issue in rmw_xireg() claiming that we can't reach 'RISCV_EXCP_NONE' at the 'done' label:
Quoted 8 lines
2706 done:
2707 if (ret) {
2708 return (env->virt_enabled&& virt) ?
2709RISCV_EXCP_VIRT_INSTRUCTION_FAULT:RISCV_EXCP_ILLEGAL_INST;
2710 }Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
CID 1590356: Control flow issues (DEADCODE) Execution cannot reach this statement: "return
RISCV_EXCP_NONE;".2711 return
RISCV_EXCP_NONE;
Our label is now reduced after fixing another deadcode in the previous patch but the problem reported here still remains:
done:
if (ret) {
return RISCV_EXCP_ILLEGAL_INST;
}
return RISCV_EXCP_NONE;This happens because 'ret' changes only once at the start of the function:
ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT);
if (ret != RISCV_EXCP_NONE) {
return ret;
}So it's a guarantee that ret will be RISCV_EXCP_NONE (-1) if we ever reach the label, i.e. "if (ret)" will always be true, and the label can be even further reduced to:
done:
return RISCV_EXCP_ILLEGAL_INST;
To make a better use of the label, remove the 'else' from the xiselect_aia_range() chain and let it fall-through to the 'done' label since they are now both returning RISCV_EXCP_ILLEGAL_INST.
Resolves: Coverity CID 1590356
--- target/riscv/csr.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
target/riscv/csr.c +1 -6
@@ -2697,15 +2697,10 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, }else if (riscv_cpu_cfg (env)->ext_smcsrind ||riscv_cpu_cfg (env)->ext_sscsrind) {return rmw_xireg_csrind (env, csrno, isel, val, new_val, wr_mask); - }else { -return RISCV_EXCP_ILLEGAL_INST ; } done: -if (ret) { -return RISCV_EXCP_ILLEGAL_INST ; - } -return RISCV_EXCP_NONE ; +return RISCV_EXCP_ILLEGAL_INST ; } static RISCVException rmw_xtopei(CPURISCVState *env, int csrno,
Signature · 1 line
2.47.1
Re: [PATCH 2/5] target/riscv/csr.c: fix 'ret' deadcode in rmw_xireg()
Quoted 37 lines · Daniel Henrique Barboza
Coverity found a second DEADCODE issue in
rmw_xireg()claiming that we can't reach 'RISCV_EXCP_NONE' at the 'done' label:Quoted 8 lines
2706 done:
2707 if (ret) {
2708 return (env->virt_enabled&& virt) ?
2709RISCV_EXCP_VIRT_INSTRUCTION_FAULT:RISCV_EXCP_ILLEGAL_INST;
2710 }Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
CID 1590356: Control flow issues (DEADCODE) Execution cannot reach this statement: "return
RISCV_EXCP_NONE;".2711 return
RISCV_EXCP_NONE;Our label is now reduced after fixing another deadcode in the previous patch but the problem reported here still remains:
done:
if (ret) { return RISCV_EXCP_ILLEGAL_INST; } return RISCV_EXCP_NONE;This happens because 'ret' changes only once at the start of the function:
ret = smstateen_acc_ok(env, 0, SMSTATEEN0_SVSLCT); if (ret != RISCV_EXCP_NONE) { return ret; }So it's a guarantee that ret will be
RISCV_EXCP_NONE(-1) if we ever reach the label, i.e. "if (ret)" will always be true, and the label can be even further reduced to:done:
return RISCV_EXCP_ILLEGAL_INST;
Quoted 7 lines
To make a better use of the label, remove the 'else' from the
xiselect_aia_range()chain and let it fall-through to the 'done' label since they are now both returningRISCV_EXCP_ILLEGAL_INST.Resolves: Coverity CID 1590356
Fixes dc0280723d ("target/riscv: Decouple AIA processing from xiselect and xireg")Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 29 lines
--- target/riscv/csr.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)target/riscv/csr.c +1
-7@@ -2697,15 +2697,10 @@ static RISCVException rmw_xireg(CPURISCVState *env, int csrno, }else if (riscv_cpu_cfg (env)->ext_smcsrind ||riscv_cpu_cfg (env)->ext_sscsrind) {return rmw_xireg_csrind (env, csrno, isel, val, new_val, wr_mask); - }else { -return RISCV_EXCP_ILLEGAL_INST ; } done: -if (ret) { -return RISCV_EXCP_ILLEGAL_INST ; - } -return RISCV_EXCP_NONE ; +return RISCV_EXCP_ILLEGAL_INST ; } static RISCVException rmw_xtopei(CPURISCVState *env, int csrno, --2.47.1
PATCH 3/5 target/riscv/csr.c: fix deadcode in rmw_xiregi()
In reply to Daniel Henrique BarbozaCoverity found a DEADCODE issue in rmw_xiregi() claiming that we can't reach 'RISCV_EXCP_VIRT_INSTRUCTION_FAULT' at the 'done' label:
Quoted 3 lines
2652 done: >>> CID 1590357: Control flow issues (DEADCODE) >>> Execution cannot reach the expression "RISCV_EXCP_VIRT_INSTRUCTION_FAULT"
inside this statement: "return (env->virt_enabled &...".
Quoted 2 lines
2653 return (
env->virt_enabled&& virt) ?
2654RISCV_EXCP_VIRT_INSTRUCTION_FAULT:RISCV_EXCP_ILLEGAL_INST;
This happens because 'virt' is being set to 'false' and it will remain as 'false' in any code path where 'done' will be called. The label can be safely reduced to:
done:
return RISCV_EXCP_ILLEGAL_INST;
And that will leave us with the following usage of a 'goto' skipping a single 'return' to do another single 'return':
} else {
goto done;
}
return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask);done:
return RISCV_EXCP_ILLEGAL_INST;
Which we will eliminate it and just do 'return RISCV_EXCP_ILLEGAL_INST' instead.
Resolves: Coverity CID 1590357
--- target/riscv/csr.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
target/riscv/csr.c +1 -7
@@ -2621,7 +2621,6 @@ static int rmw_xireg_csrind(CPURISCVState *env, int csrno,static int rmw_xiregi (CPURISCVState *env,int csrno,target_ulong *val,target_ulong new_val,target_ulong wr_mask) { -bool virt = false;int ret = -EINVAL ;target_ulong isel;
@@ -2642,16 +2641,11 @@ static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val, }else if (CSR_VSIREG <= csrno && csrno <=CSR_VSIREG6 && csrno !=CSR_VSIREG4 -1 ) { isel = env->vsiselect; - virt = true; }else { - goto done; +return RISCV_EXCP_ILLEGAL_INST ; }return rmw_xireg_csrind (env, csrno, isel, val, new_val, wr_mask); - -done: -return (env->virt_enabled && virt) ? -RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; } static RISCVException rmw_xireg(CPURISCVState *env, int csrno,
Signature · 1 line
2.47.1
Re: [PATCH 3/5] target/riscv/csr.c: fix deadcode in rmw_xiregi()
Quoted 28 lines · Daniel Henrique Barboza
Coverity found a DEADCODE issue in
rmw_xiregi()claiming that we can't reach 'RISCV_EXCP_VIRT_INSTRUCTION_FAULT' at the 'done' label:Quoted 3 lines
2652 done: >>> CID 1590357: Control flow issues (DEADCODE) >>> Execution cannot reach the expression "RISCV_EXCP_VIRT_INSTRUCTION_FAULT"inside this statement: "return (env->virt_enabled &...".Quoted 2 lines
2653 return (
env->virt_enabled&& virt) ?
2654RISCV_EXCP_VIRT_INSTRUCTION_FAULT:RISCV_EXCP_ILLEGAL_INST;This happens because 'virt' is being set to 'false' and it will remain as 'false' in any code path where 'done' will be called. The label can be safely reduced to:
done:
return RISCV_EXCP_ILLEGAL_INST;And that will leave us with the following usage of a 'goto' skipping a single 'return' to do another single 'return':
} else { goto done; } return rmw_xireg_csrind(env, csrno, isel, val, new_val, wr_mask);done:
Quoted 8 lines
return RISCV_EXCP_ILLEGAL_INST;Which we will eliminate it and just do 'return
RISCV_EXCP_ILLEGAL_INST' instead.Resolves: Coverity CID 1590357
Fixes 5e33a20827 ("target/riscv: Support generic CSR indirect access")Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 38 lines
--- target/riscv/csr.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)target/riscv/csr.c +1
-8@@ -2621,7 +2621,6 @@ static int rmw_xireg_csrind(CPURISCVState *env, int csrno,static int rmw_xiregi (CPURISCVState *env,int csrno,target_ulong *val,target_ulong new_val,target_ulong wr_mask) { -bool virt = false;int ret = -EINVAL ;target_ulong isel;@@ -2642,16 +2641,11 @@ static int rmw_xiregi(CPURISCVState *env, int csrno, target_ulong *val, }else if (CSR_VSIREG <= csrno && csrno <=CSR_VSIREG6 && csrno !=CSR_VSIREG4 -1 ) { isel = env->vsiselect; - virt = true; }else { - goto done; +return RISCV_EXCP_ILLEGAL_INST ; }return rmw_xireg_csrind (env, csrno, isel, val, new_val, wr_mask); - -done: -return (env->virt_enabled && virt) ? -RISCV_EXCP_VIRT_INSTRUCTION_FAULT :RISCV_EXCP_ILLEGAL_INST ; } static RISCVException rmw_xireg(CPURISCVState *env, int csrno, --2.47.1
PATCH 4/5 target/riscv/csr.c: fix deadcode in aia_smode32()
In reply to Daniel Henrique BarbozaCoverity reported a DEADCODE ticket in this function, as follows:
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
CID 1590358: Control flow issues (DEADCODE) Execution cannot reach this statement: "return ret;".
380 return ret;
381 }
The cause is that the 'if (ret != RISCV_EXCP_NONE)' conditional is duplicated:
ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA);
if (ret != RISCV_EXCP_NONE) {
return ret;
}
if (ret != RISCV_EXCP_NONE) {
return ret;
}Remove the duplication to fix the deadcode.
Resolves: Coverity CID 1590358
--- target/riscv/csr.c | 4 ---- 1 file changed, 4 deletions(-)
target/riscv/csr.c +0 -4
@@ -376,10 +376,6 @@ static RISCVException aia_smode32(CPURISCVState *env, int csrno)return ret; } -if (ret !=RISCV_EXCP_NONE ) { -return ret; - } -return smode32 (env, csrno); }
Signature · 1 line
2.47.1
Re: [PATCH 4/5] target/riscv/csr.c: fix deadcode in aia_smode32()
Quoted 20 lines · Daniel Henrique Barboza
Coverity reported a DEADCODE ticket in this function, as follows:
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
CID 1590358: Control flow issues (DEADCODE) Execution cannot reach this statement: "return ret;".
380 return ret;
381 }The cause is that the 'if (ret !=
RISCV_EXCP_NONE)' conditional is duplicated:ret = smstateen_acc_ok(env, 0, SMSTATEEN0_AIA); if (ret != RISCV_EXCP_NONE) { return ret; } if (ret != RISCV_EXCP_NONE) { return ret; }
Quoted 5 lines
Remove the duplication to fix the deadcode.
Resolves: Coverity CID 1590358
Fixes dbcb6e1ccf ("target/riscv: Enable S*stateen bits for AIA")Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 23 lines
--- target/riscv/csr.c | 4 ---- 1 file changed, 4 deletions(-)target/riscv/csr.c +0
-5@@ -376,10 +376,6 @@ static RISCVException aia_smode32(CPURISCVState *env, int csrno)return ret; } -if (ret !=RISCV_EXCP_NONE ) { -return ret; - } -return smode32 (env, csrno); } --2.47.1
PATCH 5/5 target/riscv/cpu_helper.c: fix bad_shift in riscv_cpu_interrupt()
In reply to Daniel Henrique BarbozaCoverity reported a BAD_SHIFT issue in the following code:
Quoted 3 lines
2097
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
CID 1590355: Integer handling issues (
BAD_SHIFT) In expression "hdeleg >> cause", right shifting by more than 63
bits has undefined behavior. The shift amount, "cause", is at least 64.
Quoted 2 lines
2098
vsmode_exc=env->virt_enabled&& (((hdeleg >> cause) & 1) ||vs_injected);
2099 /*
It is not clear to me how the tool guarantees that '"cause" is at least 64', but indeed there's no guarantees that it would be < 64 in the 'async = true' code path.
A simple fix to avoid a potential UB is to add a 'cause < 64' guard like 'mode' is already doing right before 'vsmode_exc'.
Resolves: Coverity CID 1590355
--- target/riscv/cpu_helper.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +3 -1
@@ -2095,7 +2095,9 @@ void riscv_cpu_do_interrupt(CPUState *cs) mode = env->priv <=PRV_S && cause <64 && (((deleg >> cause) &1 ) || s_injected || vs_injected) ?PRV_S :PRV_M ; - vsmode_exc = env->virt_enabled && (((hdeleg >> cause) &1 ) || vs_injected); + vsmode_exc = env->virt_enabled && cause <64 && + (((hdeleg >> cause) &1 ) || vs_injected); +/* * Check double trap condition only if already in S-mode and targeting * S-mode
Signature · 1 line
2.47.1
Re: [PATCH 5/5] target/riscv/cpu_helper.c: fix bad_shift in riscv_cpu_interrupt()
Quoted 14 lines · Daniel Henrique Barboza
Coverity reported a
BAD_SHIFTissue in the following code:Quoted 3 lines
2097
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
CID 1590355: Integer handling issues (
BAD_SHIFT) In expression "hdeleg >> cause", right shifting by more than 63bits has undefined behavior. The shift amount, "cause", is at least 64.
Quoted 2 lines
2098
vsmode_exc=env->virt_enabled&& (((hdeleg >> cause) & 1) ||vs_injected);
2099 /*It is not clear to me how the tool guarantees that '"cause" is at least 64', but indeed there's no guarantees that it would be < 64 in the 'async = true' code path.
Quoted 6 lines
A simple fix to avoid a potential UB is to add a 'cause < 64' guard like 'mode' is already doing right before '
vsmode_exc'.Resolves: Coverity CID 1590355
Fixes 967760f62c ("target/riscv: Implement Ssdbltrp exception handling")Signed-off-by Daniel Henrique Barboza <[email protected]>
Alistair
Quoted 23 lines
--- target/riscv/cpu_helper.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)target/riscv/cpu_helper.c +3
-2@@ -2095,7 +2095,9 @@ void riscv_cpu_do_interrupt(CPUState *cs) mode = env->priv <=PRV_S && cause <64 && (((deleg >> cause) &1 ) || s_injected || vs_injected) ?PRV_S :PRV_M ; - vsmode_exc = env->virt_enabled && (((hdeleg >> cause) &1 ) || vs_injected); + vsmode_exc = env->virt_enabled && cause <64 && + (((hdeleg >> cause) &1 ) || vs_injected); +/* * Check double trap condition only if already in S-mode and targeting * S-mode -- 2.47.1
Alistair Francis
AppliedQuoted 12 lines · Daniel Henrique Barboza
Hi,
This series contains Coverity fixes for issues found in the latest RISC-V pull made yesterday.
Coverity CIDs being resolved: 1590355, 1590356, 1590357, 1590358 and 1590359.
Patches based on master.
Quoted 6 lines
Daniel Henrique Barboza (5):
target/riscv/csr.c: fix deadcode inrmw_xireg()target/riscv/csr.c: fix 'ret' deadcode inrmw_xireg()target/riscv/csr.c: fix deadcode inrmw_xiregi()target/riscv/csr.c: fix deadcode inaia_smode32()target/riscv/cpu_helper.c: fixbad_shiftinriscv_cpu_interrupt()
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 9 lines
target/riscv/cpu_helper.c | 4 +++- target/riscv/csr.c | 24 +++--------------------- 2 files changed, 6 insertions(+), 22 deletions(-)--
2.47.1