x86: address MISRA C:2012 Rule 5.3
v2 cover x86: address MISRA C:2012 Rule 5.3
This series aims to address some violations of MISRA C:2012 Rule 5.3:
"An identifier declared in an inner scope shall not hide an identifier declared in an outer scope".
To resolve such violations two strategies have been applied:
1. rename several function parameters or local variables;
2. remove a local variable declaration in an inner scope, if using the outer variable does not change the semantics of the function.
Nicola Vetrini (4):
x86: mechanically rename to address MISRA C:2012 Rule 5.3 x86/mm: remove variable to avoid shadowing
x86emul: avoid shadowing to address MISRA C:2012 Rule 5.3
x86emul: remove local variable to address MISRA C:2012 Rule 5.3
xen/arch/x86/cpu/mcheck/mce.c | 8 ++++---- xen/arch/x86/domain.c | 2 +- xen/arch/x86/emul-i8254.c | 2 +- xen/arch/x86/include/asm/e820.h | 2 +- xen/arch/x86/include/asm/hvm/save.h | 8 ++++---- xen/arch/x86/include/asm/hvm/vpt.h | 2 +- xen/arch/x86/mm/p2m.c | 2 -- xen/arch/x86/psr.c | 4 ++-- xen/arch/x86/traps.c | 5 ++--- xen/arch/x86/x86_emulate/x86_emulate.c | 14 ++++++-------- 10 files changed, 22 insertions(+), 27 deletions(-)
--
2.34.1
PATCH v2 1/4 x86: mechanically rename to address MISRA C:2012 Rule 5.3
Rule 5.3 has the following headline:
"An identifier declared in an inner scope shall not hide an identifier declared in an outer scope"
The renames done by this patch avoid shadowing from happening. They are as follows:
- s/i/j/ in 'mce_action' (the shadowing here is due to macro 'x86_mcinfo_lookup' that defines 'i' as a loop counter)
- s/desc/descriptor/ in '_hvm_load_entry'
- s/socket_info/sock_info/ in 'do_write_psr_msrs'
The parameter 'cpu_khz' that causes a violation in 'pit_init' is unused, and hence can be removed.
Parameter 'debug_stack_lines' in 'compat_show_guest_stack' is removed, since the shadowed static variable has the same purpose.
The declaration of 'init_e820' is renamed to be consistent with its definition.
No functional changes.
---
Changes in v2:
- removed renames for 'str' parameters
- removed parameter 'debug_stack_lines' from
'compat_show_guest_stack'
--- xen/arch/x86/cpu/mcheck/mce.c | 8 ++++---- xen/arch/x86/domain.c | 2 +- xen/arch/x86/emul-i8254.c | 2 +- xen/arch/x86/include/asm/e820.h | 2 +- xen/arch/x86/include/asm/hvm/save.h | 8 ++++---- xen/arch/x86/include/asm/hvm/vpt.h | 2 +- xen/arch/x86/psr.c | 4 ++-- xen/arch/x86/traps.c | 5 ++--- 8 files changed, 16 insertions(+), 17 deletions(-)
xen/arch/x86/cpu/mcheck/mce.c +4 -4
@@ -1746,7 +1746,7 @@ static enum mce_result mce_action(const struct cpu_user_regs *regs,struct mcinfo_common *mic =NULL ;struct mca_binfo binfo;const struct mca_error_handler *handlers = mce_dhandlers; -unsigned int i, handler_num = mce_dhandler_num; +unsigned int j, handler_num = mce_dhandler_num;/* When in mce context, regs is valid */ if ( regs )
@@ -1780,11 +1780,11 @@ static enum mce_result mce_action(const struct cpu_user_regs *regs, binfo.mib = (struct mcinfo_bank *)mic; binfo.bank = binfo.mib->mc_bank; bank_result =MCER_NOERROR ; -for ( i =0 ; i < handler_num; i++ ) +for ( j =0 ; j < handler_num; j++ ) { -if ( handlers[i].owned_error (binfo.mib->mc_status) ) +if ( handlers[j].owned_error (binfo.mib->mc_status) ) { - handlers[i].recovery_handler (&binfo, &bank_result, regs); + handlers[j].recovery_handler (&binfo, &bank_result, regs);if ( worst_result < bank_result ) worst_result = bank_result;break ;
xen/arch/x86/domain.c +1 -1
@@ -858,7 +858,7 @@ int arch_domain_create(struct domain *d, }/* PV/PVH guests get an emulated PIT too for video BIOSes to use. */ -pit_init (d, cpu_khz); +pit_init (d);/* * If the FPU does not save FCS/FDS then we can always
xen/arch/x86/emul-i8254.c +1 -1
@@ -495,7 +495,7 @@ void pit_reset(struct domain *d)spin_unlock (&pit->lock); } -void pit_init (struct domain *d,unsigned long cpu_khz) +void pit_init (struct domain *d) {PITState *pit =domain_vpit (d);
xen/arch/x86/include/asm/e820.h +1 -1
@@ -31,7 +31,7 @@ extern int e820_change_range_type( uint32_t orig_type, uint32_t new_type);extern int e820_add_range (struct e820map *,uint64_t s,uint64_t e,uint32_t type); -extern unsigned long init_e820 (const char *,struct e820map *); +extern unsigned long init_e820 (const char *str,struct e820map *raw);extern void print_e820_memory_map (const struct e820entry *map,unsigned int entries);extern struct e820map e820;
xen/arch/x86/include/asm/hvm/save.h +4 -4
@@ -49,21 +49,21 @@ void _hvm_read_entry(struct hvm_domain_context *h,*/ #define _hvm_load_entry (_x, _h, _dst, _strict) ({ \ int r; \ - struct hvm_save_descriptor *desc \ + struct hvm_save_descriptor *descriptor \ = (struct hvm_save_descriptor *)&(_h)->data[(_h)->cur]; \ if ( (r = _hvm_check_entry((_h), HVM_SAVE_CODE(_x), \ HVM_SAVE_LENGTH(_x), (_strict))) == 0 ) \ { \ _hvm_read_entry((_h), (_dst), HVM_SAVE_LENGTH(_x)); \ if ( HVM_SAVE_HAS_COMPAT(_x) && \ - desc->length != HVM_SAVE_LENGTH(_x) ) \ - r = HVM_SAVE_FIX_COMPAT(_x, (_dst), desc->length); \ + descriptor->length != HVM_SAVE_LENGTH(_x) ) \ + r = HVM_SAVE_FIX_COMPAT(_x, (_dst), descriptor->length); \ } \ else if (HVM_SAVE_HAS_COMPAT(_x) \ && (r = _hvm_check_entry((_h), HVM_SAVE_CODE(_x), \ HVM_SAVE_LENGTH_COMPAT(_x), (_strict))) == 0 ) { \ _hvm_read_entry((_h), (_dst), HVM_SAVE_LENGTH_COMPAT(_x)); \ - r = HVM_SAVE_FIX_COMPAT(_x, (_dst), desc->length); \ + r = HVM_SAVE_FIX_COMPAT(_x, (_dst), descriptor->length); \ } \ r; })
xen/arch/x86/include/asm/hvm/vpt.h +1 -1
@@ -179,7 +179,7 @@ void destroy_periodic_time(struct periodic_time *pt);int pv_pit_handler (int port,int data,int write);void pit_reset (struct domain *d); -void pit_init (struct domain *d,unsigned long cpu_khz); +void pit_init (struct domain *d);void pit_stop_channel0_irq (PITState * pit);void pit_deinit (struct domain *d);void rtc_init (struct domain *d);
xen/arch/x86/psr.c +2 -2
@@ -1252,7 +1252,7 @@ static void cf_check do_write_psr_msrs(void *data) {const struct cos_write_info *info = data;unsigned int i, index, cos = info->cos; -const struct psr_socket_info *socket_info = +const struct psr_socket_info *sock_info =get_socket_info (cpu_to_socket (smp_processor_id ()));/*
@@ -1261,7 +1261,7 @@ static void cf_check do_write_psr_msrs(void *data)*/ for ( index = i =0 ; i <ARRAY_SIZE (feat_props); i++ ) { -struct feat_node *feat = socket_info->features[i]; +struct feat_node *feat = sock_info->features[i];const struct feat_props *props = feat_props[i];unsigned int cos_num, j;
xen/arch/x86/traps.c +2 -3
@@ -195,8 +195,7 @@ void show_code(const struct cpu_user_regs *regs) }static void compat_show_guest_stack (struct vcpu *v, -const struct cpu_user_regs *regs, -int debug_stack_lines) +const struct cpu_user_regs *regs) {unsigned int i, *stack, addr, mask =STACK_SIZE ;void *stack_page =NULL ;
@@ -273,7 +272,7 @@ static void show_guest_stack(struct vcpu *v, const struct cpu_user_regs *regs)if (is_pv_32bit_vcpu (v) ) { -compat_show_guest_stack (v, regs, debug_stack_lines); +compat_show_guest_stack (v, regs);return ; }
--
2.34.1
Re: [XEN PATCH v2 1/4] x86: mechanically rename to address MISRA C:2012 Rule 5.3
Quoted 8 lines · Nicola Vetrini
Rule 5.3 has the following headline:
"An identifier declared in an inner scope shall not hide an identifier declared in an outer scope"The renames done by this patch avoid shadowing from happening. They are as follows:
- s/i/j/ in 'mce_action' (the shadowing here is due to macro 'x86_mcinfo_lookup' that defines 'i' as a loop counter)
Then that macro needs adjustment. Especially considering it's in a public header: It's full of name space violations.
Quoted 17 lines
xen/arch/x86/include/asm/hvm/save.h +2
-3@@ -49,21 +49,21 @@ void _hvm_read_entry(struct hvm_domain_context *h,*/ #define _hvm_load_entry (_x, _h, _dst, _strict) ({ \ int r; \ - struct hvm_save_descriptor *desc \ + struct hvm_save_descriptor *descriptor \ = (struct hvm_save_descriptor *)&(_h)->data[(_h)->cur]; \ if ( (r = _hvm_check_entry((_h), HVM_SAVE_CODE(_x), \ HVM_SAVE_LENGTH(_x), (_strict))) == 0 ) \ { \ _hvm_read_entry((_h), (_dst), HVM_SAVE_LENGTH(_x)); \ if ( HVM_SAVE_HAS_COMPAT(_x) && \ - desc->length != HVM_SAVE_LENGTH(_x) ) \ - r = HVM_SAVE_FIX_COMPAT(_x, (_dst), desc->length); \ + descriptor->length != HVM_SAVE_LENGTH(_x) ) \
xen/arch/x86/include/asm/hvm/save.h void _hvm_read_entry(struct hvm_domain_context *h, · line 68
Quoted 10 lines
xen/arch/x86/include/asm/hvm/save.h +2
-1+ r =HVM_SAVE_FIX_COMPAT (_x, (_dst), descriptor->length); \ } \else if (HVM_SAVE_HAS_COMPAT (_x) \ && (r =_hvm_check_entry ((_h),HVM_SAVE_CODE (_x), \HVM_SAVE_LENGTH_COMPAT (_x), (_strict))) ==0 ) { \_hvm_read_entry ((_h), (_dst),HVM_SAVE_LENGTH_COMPAT (_x)); \ - r =HVM_SAVE_FIX_COMPAT (_x, (_dst), desc->length); \ + r =HVM_SAVE_FIX_COMPAT (_x, (_dst), descriptor->length); \ } \ r; })
The macro-local variable gets too long for my taste, to be honest, and it being improperly named anyway suggests it simply wants a trailing underscore added. And then, since for a variable named "r" the risk of shadowing is equally high, that one wants to gain a trailing underscore as well imo. (And while at it, I personally would also drop the leading underscores from the macro parameter names. Furthermore I think it would be nice if at on the lines touched anyway indentation was also corrected. Overall maybe best if I submit a patch.)
Jan
Re: [XEN PATCH v2 1/4] x86: mechanically rename to address MISRA C:2012 Rule 5.3
Quoted 30 lines · Jan Beulich
Quoted 17 lines · Nicola Vetrini
xen/arch/x86/include/asm/hvm/save.h +2
-3@@ -49,21 +49,21 @@ void _hvm_read_entry(struct hvm_domain_context *h,*/ #define _hvm_load_entry (_x, _h, _dst, _strict) ({ \ int r; \ - struct hvm_save_descriptor *desc \ + struct hvm_save_descriptor *descriptor \ = (struct hvm_save_descriptor *)&(_h)->data[(_h)->cur]; \ if ( (r = _hvm_check_entry((_h), HVM_SAVE_CODE(_x), \ HVM_SAVE_LENGTH(_x), (_strict))) == 0 ) \ { \ _hvm_read_entry((_h), (_dst), HVM_SAVE_LENGTH(_x)); \ if ( HVM_SAVE_HAS_COMPAT(_x) && \ - desc->length != HVM_SAVE_LENGTH(_x) ) \ - r = HVM_SAVE_FIX_COMPAT(_x, (_dst), desc->length); \ + descriptor->length != HVM_SAVE_LENGTH(_x) ) \xen/arch/x86/include/asm/hvm/save.h void _hvm_read_entry(struct hvm_domain_context *h, · line 68
Quoted 10 lines
xen/arch/x86/include/asm/hvm/save.h +2
-1+ r =HVM_SAVE_FIX_COMPAT (_x, (_dst), descriptor->length); \ } \else if (HVM_SAVE_HAS_COMPAT (_x) \ && (r =_hvm_check_entry ((_h),HVM_SAVE_CODE (_x), \HVM_SAVE_LENGTH_COMPAT (_x), (_strict))) ==0 ) { \_hvm_read_entry ((_h), (_dst),HVM_SAVE_LENGTH_COMPAT (_x)); \ - r =HVM_SAVE_FIX_COMPAT (_x, (_dst), desc->length); \ + r =HVM_SAVE_FIX_COMPAT (_x, (_dst), descriptor->length); \ } \ r; })The macro-local variable gets too long for my taste, to be honest,
Quoted 8 lines
and it being improperly named anyway suggests it simply wants a trailing underscore added. And then, since for a variable named "r" the risk of shadowing is equally high, that one wants to gain a trailing underscore as well imo. (And while at it, I personally would also drop the leading underscores from the macro parameter names. Furthermore I think it would be nice if at on the lines touched anyway indentation was also corrected. Overall maybe best if I submit a patch.)
In that replacement patch I would like to mention what "desc" this collides with, but your description didn't say so and I'm afraid I also haven't been able to spot it (grep-ing for "desc", even with a couple of extra restrictions, still yields way too many hits).
Jan
Re: [XEN PATCH v2 1/4] x86: mechanically rename to address MISRA C:2012 Rule 5.3
Quoted 63 lines · Jan Beulich
Quoted 53 lines · Jan Beulich
Quoted 43 lines · Nicola Vetrini
xen/arch/x86/include/asm/hvm/save.h +3
-3@@ -49,21 +49,21 @@ void _hvm_read_entry(struct hvm_domain_context*h, */ #define _hvm_load_entry (_x, _h, _dst, _strict) ({ \int r; \ -struct hvm_save_descriptor *desc \ +struct hvm_save_descriptor *descriptor \ = (struct hvm_save_descriptor *)&(_h)->data[(_h)->cur]; \if ( (r =_hvm_check_entry ((_h),HVM_SAVE_CODE (_x), \HVM_SAVE_LENGTH (_x), (_strict))) ==0 ) \ { \_hvm_read_entry ((_h), (_dst),HVM_SAVE_LENGTH (_x)); \if (HVM_SAVE_HAS_COMPAT (_x) && \ - desc->length !=HVM_SAVE_LENGTH (_x) ) \ - r =HVM_SAVE_FIX_COMPAT (_x, (_dst), desc->length); \ + descriptor->length !=HVM_SAVE_LENGTH (_x) ) \ + r =HVM_SAVE_FIX_COMPAT (_x, (_dst), descriptor->length); \ } \else if (HVM_SAVE_HAS_COMPAT (_x) \ && (r =_hvm_check_entry ((_h),HVM_SAVE_CODE (_x), \ HVM_SAVE_LENGTH_COMPAT(_x), (_strict))) == 0) { \ _hvm_read_entry((_h), (_dst), HVM_SAVE_LENGTH_COMPAT(_x));\
xen/arch/x86/include/asm/hvm/save.h
Quoted 7 lines
xen/arch/x86/include/asm/hvm/save.h +1
-1- r =HVM_SAVE_FIX_COMPAT (_x, (_dst), desc->length); \ + r =HVM_SAVE_FIX_COMPAT (_x, (_dst), descriptor->length); \ } \ r; })The macro-local variable gets too long for my taste, to be honest,
Quoted 8 lines
and it being improperly named anyway suggests it simply wants a trailing underscore added. And then, since for a variable named "r" the risk of shadowing is equally high, that one wants to gain a trailing underscore as well imo. (And while at it, I personally would also drop the leading underscores from the macro parameter names. Furthermore I think it would be nice if at on the lines touched anyway indentation was also corrected. Overall maybe best if I submit a patch.)
Quoted 6 lines
In that replacement patch I would like to mention what "desc" this collides with, but your description didn't say so and I'm afraid I also haven't been able to spot it (grep-ing for "desc", even with a couple of extra restrictions, still yields way too many hits).
Jan
It's the local variable in 'xen/arch/x86/hvm/save.c:281', but since the macro is used elsewhere quite a few times, doing a suitable rename inside it (that does not shadow anything) prevents other users from possibly shadowing it in the future. Your points are all agreeable in my opinion. Since your concerns with this macro are also about issues other than MISRA violations, I agree that this patch should be dropped.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
PATCH v2 2/4 x86/mm: remove variable to avoid shadowing
In reply to Nicola VetriniThe local variable 'p2mt' shadows a declaration of the same variable in the enclosing scope, but removing the inner declaration does not alter the semantics ('p2mt' is an output for the get_gfn call later on) and this resolves a violation of
MISRA C:2012 Rule 5.3.
Both variables were last introduced by commitd4f699a0df6cea907c1de5c277500b98c0729685.
No functional changes.
---
Change in v2:
- removed the innermost p2mt instead of renaming it.
--- xen/arch/x86/mm/p2m.c | 2 -- 1 file changed, 2 deletions(-)
xen/arch/x86/mm/p2m.c +0 -2
@@ -2439,8 +2439,6 @@ int xenmem_add_to_physmap_one(case XENMAPSPACE_gmfn: { -p2m_type_t p2mt; - gfn = idx; mfn =get_gfn_unshare (d, gfn, &p2mt);/* If the page is still shared, exit early */
--
2.34.1
Re: [XEN PATCH v2 2/4] x86/mm: remove variable to avoid shadowing
Quoted 8 lines · Nicola Vetrini
The local variable 'p2mt' shadows a declaration of the same variable in the enclosing scope, but removing the inner declaration does not alter the semantics ('p2mt' is an output for the
get_gfncall later on) and this resolves a violation of
MISRA C:2012 Rule 5.3.Both variables were last introduced by commit
d4f699a0df6cea907c1de5c277500b98c0729685.
No, that commit moved the function (without correcting the issue, perhaps because of not noticing it, but perhaps also intentionally).
Quoted 3 lines
No functional changes.
Signed-off-by Nicola Vetrini <[email protected]>
With the misleading part of the description either dropped (which I'd be okay doing while committing) or made meaningful:
Jan
PATCH v2 3/4 x86emul: avoid shadowing to address MISRA C:2012 Rule 5.3
In reply to Nicola VetriniRule 5.3 has the following headline:
"An identifier declared in an inner scope shall not hide an identifier declared in an outer scope"
The declaration of local variable 'cpuid_leaf' causes shadowing with the homonymous function to happen, therefore the variable is renamed to avoid this.
No functional changes.
---
Changes in v2:
- s/res/leaf/ to have a less generic name.
- Split the patch in half (see patch 4/4).
--- xen/arch/x86/x86_emulate/x86_emulate.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +6 -6
@@ -1483,7 +1483,7 @@ x86_emulate( {enum x86_segment seg;struct segment_register cs, sreg; -struct cpuid_leaf cpuid_leaf; +struct cpuid_leaf leaf;uint64_t msr_val;unsigned int i, n;unsigned long dummy;
@@ -5024,13 +5024,13 @@ x86_emulate(generate_exception_if ((msr_val &MSR_MISC_FEATURES_CPUID_FAULTING ),X86_EXC_GP ,0 );/* Faulting active? (Inc. CPL test) */ - rc = ops->cpuid (_regs.eax, _regs.ecx, &cpuid_leaf, ctxt); + rc = ops->cpuid (_regs.eax, _regs.ecx, &leaf, ctxt);if ( rc !=X86EMUL_OKAY ) goto done; - _regs.r (ax) = cpuid_leaf.a; - _regs.r (bx) = cpuid_leaf.b; - _regs.r (cx) = cpuid_leaf.c; - _regs.r (dx) = cpuid_leaf.d; + _regs.r (ax) = leaf.a; + _regs.r (bx) = leaf.b; + _regs.r (cx) = leaf.c; + _regs.r (dx) = leaf.d;break ;case X86EMUL_OPC (0x0f ,0xa3 ): bt:/* bt */
--
2.34.1
Re: [XEN PATCH v2 3/4] x86emul: avoid shadowing to address MISRA C:2012 Rule 5.3
Quoted 11 lines · Nicola Vetrini
Rule 5.3 has the following headline:
"An identifier declared in an inner scope shall not hide an identifier declared in an outer scope"The declaration of local variable '
cpuid_leaf' causes shadowing with the homonymous function to happen, therefore the variable is renamed to avoid this.No functional changes.
Signed-off-by Nicola Vetrini <[email protected]>
PATCH v2 4/4 x86emul: remove local variable to address MISRA C:2012 Rule 5.3
In reply to Nicola VetriniThe local variable removed by this commit shadowed another declared in an outer scope and thus violates Rule 5.3. It was introduced as part of a refactoring by commit abf5fb62f73d0dbeabc6a8cd6dbb55174b36c8d1.
No functional changes.
--- xen/arch/x86/x86_emulate/x86_emulate.c | 2 -- 1 file changed, 2 deletions(-)
xen/arch/x86/x86_emulate/x86_emulate.c +0 -2
@@ -8408,8 +8408,6 @@ x86_emulate(generate_exception (X86_EXC_MF );if ( stub_exn.info.fields.trapnr ==X86_EXC_XM ) { -unsigned long cr4; -if ( !ops->read_cr || ops->read_cr (4 , &cr4, ctxt) !=X86EMUL_OKAY ) cr4 =X86_CR4_OSXMMEXCPT ;generate_exception (cr4 &X86_CR4_OSXMMEXCPT ?X86_EXC_XM :X86_EXC_UD );
Signature · 1 line
2.34.1
Re: [XEN PATCH v2 4/4] x86emul: remove local variable to address MISRA C:2012 Rule 5.3
Quoted 7 lines · Nicola Vetrini
The local variable removed by this commit shadowed another declared in an outer scope and thus violates Rule 5.3. It was introduced as part of a refactoring by commit
abf5fb62f73d0dbeabc6a8cd6dbb55174b36c8d1.No functional changes.
Signed-off-by Nicola Vetrini <[email protected]>
Albeit, just to mention it, the description could have done with making clear why the removal is actually correct (nothing like this can be inferred from pointing at the variable's introduction). But well ...
Jan