Skip to content

Commit 73b0195

Browse files
atishp04alistair23
authored andcommitted
target/riscv: Add asserts for out-of-bound access
Coverity complained about the possible out-of-bounds access with counter_virt/counter_virt_prev because these two arrays are accessed with privilege mode. However, these two arrays are accessed only when virt is enabled. Thus, the privilege mode can't be M mode. Add the asserts anyways to detect any wrong usage of these arrays in the future. Suggested-by: Peter Maydell <[email protected]> Signed-off-by: Atish Patra <[email protected]> Fixes: Coverity CID 1558459 Fixes: Coverity CID 1558462 Reviewed-by: Alistair Francis <[email protected]> Message-ID: <[email protected]> Signed-off-by: Alistair Francis <[email protected]>
1 parent 5e54b43 commit 73b0195

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

target/riscv/pmu.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ static void riscv_pmu_icount_update_priv(CPURISCVState *env,
204204
}
205205

206206
if (env->virt_enabled) {
207+
g_assert(env->priv <= PRV_S);
207208
counter_arr = env->pmu_fixed_ctrs[1].counter_virt;
208209
snapshot_prev = env->pmu_fixed_ctrs[1].counter_virt_prev;
209210
} else {
@@ -212,6 +213,7 @@ static void riscv_pmu_icount_update_priv(CPURISCVState *env,
212213
}
213214

214215
if (new_virt) {
216+
g_assert(newpriv <= PRV_S);
215217
snapshot_new = env->pmu_fixed_ctrs[1].counter_virt_prev;
216218
} else {
217219
snapshot_new = env->pmu_fixed_ctrs[1].counter_prev;
@@ -242,6 +244,7 @@ static void riscv_pmu_cycle_update_priv(CPURISCVState *env,
242244
}
243245

244246
if (env->virt_enabled) {
247+
g_assert(env->priv <= PRV_S);
245248
counter_arr = env->pmu_fixed_ctrs[0].counter_virt;
246249
snapshot_prev = env->pmu_fixed_ctrs[0].counter_virt_prev;
247250
} else {
@@ -250,6 +253,7 @@ static void riscv_pmu_cycle_update_priv(CPURISCVState *env,
250253
}
251254

252255
if (new_virt) {
256+
g_assert(newpriv <= PRV_S);
253257
snapshot_new = env->pmu_fixed_ctrs[0].counter_virt_prev;
254258
} else {
255259
snapshot_new = env->pmu_fixed_ctrs[0].counter_prev;

0 commit comments

Comments
 (0)