Skip to contentSkip to conversation

Fix LoongArch coverity error and cpu name bug

PATCH Superseded Xiaojuan Yang 2 participants 11 messages Jul 13, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fix LoongArch coverity error and cpu name bug

Make some changes following Richard's advice based on version v1. And the v1 patches fix some coverity errors and loongarch_cpu_class_by_name function for LoongArch virt machine.

1. Use MAKE_64BIT_MASK(shift, len) to replace 'xxx << shift'.
2. Use ARRAY_SIZE(arrqy) to get the array size.
3. Add the assertion that 'cpu_model' resolve to a class of the appropriate type.

Xiaojuan Yang (5):
target/loongarch/cpu: Fix cpu_class_by_name function hw/intc/loongarch_pch_pic: Fix coverity errors in update irq target/loongarch/cpu: Fix coverity errors about excp_names target/loongarch/tlb_helper: Fix coverity integer overflow error target/loongarch/op_helper: Fix coverity cond_at_most error

 hw/intc/loongarch_pch_pic.c   | 19 ++++++++++++-------
 target/loongarch/cpu.c        | 15 ++++++++-------
 target/loongarch/op_helper.c  |  2 +-
 target/loongarch/tlb_helper.c |  4 ++--
 4 files changed, 23 insertions(+), 17 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/5 target/loongarch/cpu: Fix cpu_class_by_name function

In loongarch_cpu_class_by_name(char *cpu_model) function, the argument cpu_model already has the suffix '-loongarch-cpu', so we should remove the LOONGARCH_CPU_TYPE_NAME(cpu_model) macro. And add the assertion that 'cpu_model' resolves to a class of the appropriate type.

---
 target/loongarch/cpu.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
target/loongarch/cpu.c +5 -4
index e21715592a..ed26f9beed 100644
@@ -571,11 +571,12 @@ static void loongarch_cpu_init(Object *obj)
 static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
 {
     ObjectClass *oc;
-    char *typename;
 
-    typename = g_strdup_printf(LOONGARCH_CPU_TYPE_NAME("%s"), cpu_model);
-    oc = object_class_by_name(typename);
-    g_free(typename);
+    oc = object_class_by_name(cpu_model);
+    if (!oc || !object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU) ||
+        object_class_is_abstract(oc)) {
+        return NULL;
+    }
     return oc;
 }
 
Signature · 1 line
2.31.1
Reviewed-by on v2 1/5
#2 lore raw

Re: [PATCH 1/5] target/loongarch/cpu: Fix cpu_class_by_name function

Quoted 10 lines · Xiaojuan Yang

In loongarch_cpu_class_by_name(char *cpu_model) function, the argument cpu_model already has the suffix '-loongarch-cpu', so we should remove the LOONGARCH_CPU_TYPE_NAME(cpu_model) macro. And add the assertion that 'cpu_model' resolves to a class of the appropriate type.

---
  target/loongarch/cpu.c | 9 +++++----
  1 file changed, 5 insertions(+), 4 deletions(-)

r~

Quoted 22 lines
target/loongarch/cpu.c +5 -4
index e21715592a..ed26f9beed 100644
@@ -571,11 +571,12 @@ static void loongarch_cpu_init(Object *obj)
  static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
  {
      ObjectClass *oc;
-    char *typename;
 
-    typename = g_strdup_printf(LOONGARCH_CPU_TYPE_NAME("%s"), cpu_model);
-    oc = object_class_by_name(typename);
-    g_free(typename);
+    oc = object_class_by_name(cpu_model);
+    if (!oc || !object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU) ||
+        object_class_is_abstract(oc)) {
+        return NULL;
+    }
      return oc;
  }
 
#3 lore raw

PATCH v2 2/5 hw/intc/loongarch_pch_pic: Fix coverity errors in update irq

In reply to Xiaojuan Yang

Fix coverity errors:
1. In find_first_bit function, the 'size' argument need 'unsigned long' type, so we change the 'size' to unsigned long type when use the function.
2. In expression 1ULL << irq, left shifting by more than 63 bits has undefined behavior. And out-of-bounds access error occured when 'irq' >= 64. So we add a condition to avoid this.
3. Use 'MAKE_64BIT_MASK(irq, 1)' to replace '1ULL << shift'.

Fix coverity CID: 1489761 1489764 1489765

---
 hw/intc/loongarch_pch_pic.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
hw/intc/loongarch_pch_pic.c +12 -7
index 3c9814a3b4..040b89861c 100644
@@ -15,22 +15,27 @@
 
 static void pch_pic_update_irq(LoongArchPCHPIC *s, uint64_t mask, int level)
 {
-    unsigned long val;
+    unsigned long val, max_irq;
     int irq;
 
+    max_irq = 64;
     if (level) {
         val = mask & s->intirr & ~s->int_mask;
         if (val) {
-            irq = find_first_bit(&val, 64);
-            s->intisr |= 0x1ULL << irq;
-            qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 1);
+            irq = find_first_bit(&val, max_irq);
+            if (irq < max_irq) {
+                s->intisr |= MAKE_64BIT_MASK(irq, 1);
+                qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 1);
+            }
         }
     } else {
         val = mask & s->intisr;
         if (val) {
-            irq = find_first_bit(&val, 64);
-            s->intisr &= ~(0x1ULL << irq);
-            qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 0);
+            irq = find_first_bit(&val, max_irq);
+            if (irq < max_irq) {
+                s->intisr &= ~(MAKE_64BIT_MASK(irq, 1));
+                qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 0);
+            }
         }
     }
 }
Signature · 1 line
2.31.1
#4 lore raw

Re: [PATCH 2/5] hw/intc/loongarch_pch_pic: Fix coverity errors in update irq

Quoted 22 lines · Xiaojuan Yang

Fix coverity errors:
1. In find_first_bit function, the 'size' argument need 'unsigned long' type, so we change the 'size' to unsigned long type when use the function.
2. In expression 1ULL << irq, left shifting by more than 63 bits has undefined behavior. And out-of-bounds access error occured when 'irq' >= 64. So we add a condition to avoid this.
3. Use 'MAKE_64BIT_MASK(irq, 1)' to replace '1ULL << shift'.

Fix coverity CID: 1489761 1489764 1489765

---
  hw/intc/loongarch_pch_pic.c | 19 ++++++++++++-------
  1 file changed, 12 insertions(+), 7 deletions(-)
hw/intc/loongarch_pch_pic.c +0 -0
index 3c9814a3b4..040b89861c 100644

hw/intc/loongarch_pch_pic.c line 18

Quoted 5 lines
hw/intc/loongarch_pch_pic.c +1 -1
@@ -15,22 +15,27 @@
 
  static void pch_pic_update_irq(LoongArchPCHPIC *s, uint64_t mask, int level)
  {
-    unsigned long val;
+    unsigned long val, max_irq;

You did not follow any of my direction from v1.

(1) val must be uint64_t.

(and, generally, any use of 'unsigned long' is probably a bug)

hw/intc/loongarch_pch_pic.c line 19

Quoted 1 line
hw/intc/loongarch_pch_pic.c +1 -0
+            irq = find_first_bit(&val, max_irq);

Use ctz64().

hw/intc/loongarch_pch_pic.c line 20

Quoted 1 line
hw/intc/loongarch_pch_pic.c +1 -0
+            if (irq < max_irq) {

This, really, should be a test of val != 0 before the ctz.

Quoted 4 lines
hw/intc/loongarch_pch_pic.c +3 -0
+                s->intisr |= MAKE_64BIT_MASK(irq, 1);
+                qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 1);
+            }
          }

hw/intc/loongarch_pch_pic.c line 37

Quoted 10 lines
hw/intc/loongarch_pch_pic.c +4 -3
      } else {
          val = mask & s->intisr;
          if (val) {
-            irq = find_first_bit(&val, 64);
-            s->intisr &= ~(0x1ULL << irq);
-            qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 0);
+            irq = find_first_bit(&val, max_irq);
+            if (irq < max_irq) {
+                s->intisr &= ~(MAKE_64BIT_MASK(irq, 1));
+                qemu_set_irq(s->parent_irq[s->htmsi_vector[irq]], 0);

etc.

r~

Quoted 4 lines
hw/intc/loongarch_pch_pic.c +1 -0
+            }
          }
      }
  }
#5 lore raw

PATCH v2 3/5 target/loongarch/cpu: Fix coverity errors about excp_names

In reply to Xiaojuan Yang

Fix out-of-bounds errors when access excp_names[] array. the valid boundary size of excp_names should be 0 to ARRAY_SIZE(excp_names)-1. However, the general code do not consider the max boundary.

Fix coverity CID: 1489758

---
 target/loongarch/cpu.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
target/loongarch/cpu.c +3 -3
index ed26f9beed..89ea971cde 100644
@@ -140,7 +140,7 @@ static void loongarch_cpu_do_interrupt(CPUState *cs)
 
     if (cs->exception_index != EXCCODE_INT) {
         if (cs->exception_index < 0 ||
-            cs->exception_index > ARRAY_SIZE(excp_names)) {
+            cs->exception_index >= ARRAY_SIZE(excp_names)) {
             name = "unknown";
         } else {
             name = excp_names[cs->exception_index];
@@ -190,8 +190,8 @@ static void loongarch_cpu_do_interrupt(CPUState *cs)
         cause = cs->exception_index;
         break;
     default:
-        qemu_log("Error: exception(%d) '%s' has not been supported\n",
-                 cs->exception_index, excp_names[cs->exception_index]);
+        qemu_log("Error: exception(%d) has not been supported\n",
+                 cs->exception_index);
         abort();
     }
 
Signature · 1 line
2.31.1
#6 lore raw

Re: [PATCH 3/5] target/loongarch/cpu: Fix coverity errors about excp_names

Quoted 10 lines · Xiaojuan Yang

Fix out-of-bounds errors when access excp_names[] array. the valid boundary size of excp_names should be 0 to ARRAY_SIZE(excp_names)-1. However, the general code do not consider the max boundary.

Fix coverity CID: 1489758

---
  target/loongarch/cpu.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)

I gave you a reviewed-by for this patch in v1.
You must copy those into v2 so that I don't have to do it again.

r~

Quoted 25 lines
target/loongarch/cpu.c +3 -3
index ed26f9beed..89ea971cde 100644
@@ -140,7 +140,7 @@ static void loongarch_cpu_do_interrupt(CPUState *cs)
 
      if (cs->exception_index != EXCCODE_INT) {
          if (cs->exception_index < 0 ||
-            cs->exception_index > ARRAY_SIZE(excp_names)) {
+            cs->exception_index >= ARRAY_SIZE(excp_names)) {
              name = "unknown";
          } else {
              name = excp_names[cs->exception_index];
@@ -190,8 +190,8 @@ static void loongarch_cpu_do_interrupt(CPUState *cs)
          cause = cs->exception_index;
          break;
      default:
-        qemu_log("Error: exception(%d) '%s' has not been supported\n",
-                 cs->exception_index, excp_names[cs->exception_index]);
+        qemu_log("Error: exception(%d) has not been supported\n",
+                 cs->exception_index);
          abort();
      }
 
#7 lore raw

PATCH v2 4/5 target/loongarch/tlb_helper: Fix coverity integer overflow error

In reply to Xiaojuan Yang

Replace '1 << shift' with 'MAKE_64BIT_MASK(shift, 1)' to fix unintentional integer overflow errors in tlb_helper file.

Fix coverity CID: 1489759 1489762

---
 target/loongarch/tlb_helper.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/loongarch/tlb_helper.c +2 -2
index bab19c7e05..610b6d123c 100644
@@ -298,7 +298,7 @@ static void invalidate_tlb_entry(CPULoongArchState *env, int index)
     } else {
         tlb_ps = FIELD_EX64(env->CSR_STLBPS, CSR_STLBPS, PS);
     }
-    pagesize = 1 << tlb_ps;
+    pagesize = MAKE_64BIT_MASK(tlb_ps, 1);
     mask = MAKE_64BIT_MASK(0, tlb_ps + 1);
 
     if (tlb_v0) {
@@ -736,7 +736,7 @@ void helper_ldpte(CPULoongArchState *env, target_ulong base, target_ulong odd,
                 (tmp0 & (~(1 << R_TLBENTRY_G_SHIFT)));
         ps = ptbase + ptwidth - 1;
         if (odd) {
-            tmp0 += (1 << ps);
+            tmp0 += MAKE_64BIT_MASK(ps, 1);
         }
     } else {
         /* 0:64bit, 1:128bit, 2:192bit, 3:256bit */
Signature · 1 line
2.31.1
Reviewed-by on v2 4/5
#8 lore raw

Re: [PATCH 4/5] target/loongarch/tlb_helper: Fix coverity integer overflow error

Quoted 9 lines · Xiaojuan Yang

Replace '1 << shift' with 'MAKE_64BIT_MASK(shift, 1)' to fix unintentional integer overflow errors in tlb_helper file.

Fix coverity CID: 1489759 1489762

---
  target/loongarch/tlb_helper.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

r~

#9 lore raw

PATCH v2 5/5 target/loongarch/op_helper: Fix coverity cond_at_most error

In reply to Xiaojuan Yang

The boundary size of cpucfg array should be 0 to 20. So, using index bigger than 20 to access cpucfg[] must be forbidden.

Fix coverity CID: 1489760

---
 target/loongarch/op_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/loongarch/op_helper.c +1 -1
index 4b429b6699..568c071601 100644
@@ -81,7 +81,7 @@ target_ulong helper_crc32c(target_ulong val, target_ulong m, uint64_t sz)
 
 target_ulong helper_cpucfg(CPULoongArchState *env, target_ulong rj)
 {
-    return rj > 21 ? 0 : env->cpucfg[rj];
+    return rj >= ARRAY_SIZE(env->cpucfg) ? 0 : env->cpucfg[rj];
 }
 
 uint64_t helper_rdtime_d(CPULoongArchState *env)
Signature · 1 line
2.31.1
Reviewed-by on v2 5/5
#10 lore raw

Re: [PATCH 5/5] target/loongarch/op_helper: Fix coverity cond_at_most error

Quoted 2 lines · Xiaojuan Yang

The boundary size of cpucfg array should be 0 to 20. So, using index bigger than 20 to access cpucfg[] must be forbidden.

You must update the comment to match the code, which no longer mentions "20" at all. With that change,

r~

Quoted 21 lines

Fix coverity CID: 1489760

---
  target/loongarch/op_helper.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
target/loongarch/op_helper.c +1 -1
index 4b429b6699..568c071601 100644
@@ -81,7 +81,7 @@ target_ulong helper_crc32c(target_ulong val, target_ulong m, uint64_t sz)
 
  target_ulong helper_cpucfg(CPULoongArchState *env, target_ulong rj)
  {
-    return rj > 21 ? 0 : env->cpucfg[rj];
+    return rj >= ARRAY_SIZE(env->cpucfg) ? 0 : env->cpucfg[rj];
  }
 
  uint64_t helper_rdtime_d(CPULoongArchState *env)