Fix LoongArch coverity error and cpu name bug
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
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
@@ -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
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 argumentcpu_modelalready has the suffix '-loongarch-cpu', so we should remove theLOONGARCH_CPU_TYPE_NAME(cpu_model)macro. And add the assertion that 'cpu_model' resolves to a class of the appropriate type.Signed-off-by Xiaojuan Yang <[email protected]>--- target/loongarch/cpu.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
r~
Quoted 22 lines
target/loongarch/cpu.c +5
-4@@ -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; }
PATCH v2 2/5 hw/intc/loongarch_pch_pic: Fix coverity errors in update irq
In reply to Xiaojuan YangFix 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
@@ -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
Re: [PATCH 2/5] hw/intc/loongarch_pch_pic: Fix coverity errors in update irq
Quoted 22 lines · Xiaojuan Yang
Fix coverity errors:
1. Infind_first_bitfunction, 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
Signed-off-by Xiaojuan Yang <[email protected]>--- hw/intc/loongarch_pch_pic.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-)hw/intc/loongarch_pch_pic.c +0
-0
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+ } } } }
PATCH v2 3/5 target/loongarch/cpu: Fix coverity errors about excp_names
In reply to Xiaojuan YangFix 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
@@ -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
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_namesshould be 0 to ARRAY_SIZE(excp_names)-1. However, the general code do not consider the max boundary.Fix coverity CID: 1489758
Signed-off-by Xiaojuan Yang <[email protected]>--- 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@@ -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 (); }
PATCH v2 4/5 target/loongarch/tlb_helper: Fix coverity integer overflow error
In reply to Xiaojuan YangReplace '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
@@ -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
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 intlb_helperfile.Fix coverity CID: 1489759 1489762
Signed-off-by Xiaojuan Yang<[email protected]>--- target/loongarch/tlb_helper.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
r~
PATCH v2 5/5 target/loongarch/op_helper: Fix coverity cond_at_most error
In reply to Xiaojuan YangThe 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
@@ -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
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
Signed-off-by Xiaojuan Yang <[email protected]>--- target/loongarch/op_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/loongarch/op_helper.c +1
-1@@ -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)