Skip to contentSkip to conversation

target/arm: Adding a check for the result of calling the CPU information check function

PATCH In review Sergey Mironov 3 participants 4 messages Oct 12, 2023 – Oct 17, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/arm: Adding a check for the result of calling the CPU information check function

6 out of 7 calls to get_arm_cp_reginfo() are checked

---
 target/arm/helper.c | 1 +
 1 file changed, 1 insertion(+)
target/arm/helper.c +1 -0
index 74fbb6e1d7..cffbbaf571 100644
@@ -198,6 +198,7 @@ static void add_cpreg_to_list(gpointer key, gpointer opaque)
     uint32_t regidx = (uintptr_t)key;
     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
 
+    assert(ri != NULL);
     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
         /* The value array need not be initialized at this point */
Signature · 1 line
2.31.1
#1 lore raw
Quoted 1 line · Sergey Mironov

6 out of 7 calls to get_arm_cp_reginfo() are checked

Yes but we should be careful with asserts (vs if (ri) legs) because I don't think get_arm_cp_reginfo() guarantees it will always be successful.

Quoted 11 lines
---
 target/arm/helper.c | 1 +
 1 file changed, 1 insertion(+)
target/arm/helper.c +0 -0
index 74fbb6e1d7..cffbbaf571 100644

target/arm/helper.c static void add_cpreg_to_list(gpointer key, gpointer opaque) · line 201

Quoted 4 lines
target/arm/helper.c +1 -0
@@ -198,6 +198,7 @@ static void add_cpreg_to_list(gpointer key, gpointer opaque)
     uint32_t regidx = (uintptr_t)key;
     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
 
+    assert(ri != NULL);

/* must always succeed as we are iterating the keys of cp_regs */ assert(ri);

is enough for a !NULL check.

target/arm/helper.c static void add_cpreg_to_list(gpointer key, gpointer opaque) · line 204

Quoted 3 lines
target/arm/helper.c +0 -0
     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
         /* The value array need not be initialized at this point */

That said we already have an assert that would fire in init_cpregs_list():

  assert(cpu->cpreg_array_len == arraylen);

so I'm not sure what this is adding to ensuring the contract is kept.

Signature · 2 lines
Alex Bennée
Virtualisation Tech Lead @ Linaro
#2 lore raw
In reply to Sergey Mironov
Quoted 2 lines · Sergey Mironov

6 out of 7 calls to get_arm_cp_reginfo() are checked

This sounds like it's talking about a Coverity warning, though it doesn't say so. Is that the motivation here ? If so, it would be good to say so in the commit message. If not, the commit message should explain why we're making the change.

That particular Coverity warning is quite prone to false positives, since it's only a heuristic. Sometimes it's useful to add an assert(), if it helps both Coverity and human readers, but not always.

assert()s are also most useful if there's a comment that explains why we can assume the thing they're assuming, as Alex suggests.

Quoted 18 lines
---
 target/arm/helper.c | 1 +
 1 file changed, 1 insertion(+)
target/arm/helper.c +1 -0
index 74fbb6e1d7..cffbbaf571 100644
@@ -198,6 +198,7 @@ static void add_cpreg_to_list(gpointer key, gpointer opaque)
     uint32_t regidx = (uintptr_t)key;
     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
 
+    assert(ri != NULL);
 
     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
         /* The value array need not be initialized at this point */
Quoted 2 lines

--
2.31.1

thanks
-- PMM

#3 lore raw

Yes, the warning was initially received in the static analyzer SVACE,

the same type as Coverity.

In this case, return value of a function 'get_arm_cp_reginfo' is referenced at helper.c without checking for ALL, but it is usually checked for this function (8/9).

________________________________
От: Peter Maydell <[email protected]>
Отправлено: 16 октября 2023 г. 19:23
Кому: Миронов Сергей Владимирович
Копия: [email protected]; [email protected]
Тема: Re: [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function

Quoted 2 lines · Sergey Mironov

6 out of 7 calls to get_arm_cp_reginfo() are checked

This sounds like it's talking about a Coverity warning, though it doesn't say so. Is that the motivation here ? If so, it would be good to say so in the commit message. If not, the commit message should explain why we're making the change.

That particular Coverity warning is quite prone to false positives, since it's only a heuristic. Sometimes it's useful to add an assert(), if it helps both Coverity and human readers, but not always.

assert()s are also most useful if there's a comment that explains why we can assume the thing they're assuming, as Alex suggests.

Quoted 18 lines
---
 target/arm/helper.c | 1 +
 1 file changed, 1 insertion(+)
target/arm/helper.c +1 -0
index 74fbb6e1d7..cffbbaf571 100644
@@ -198,6 +198,7 @@ static void add_cpreg_to_list(gpointer key, gpointer opaque)
     uint32_t regidx = (uintptr_t)key;
     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
 
+    assert(ri != NULL);
 
     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
         /* The value array need not be initialized at this point */
Quoted 2 lines

--
2.31.1

thanks
-- PMM