address violations of MISRA C:2012 Rule 2.1
PATCH 7/7 x86/xstate: move BUILD_BUG_ON to address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniThe string literal inside the expansion of BUILD_BUG_ON is considered unreachable code; however, such statement can be moved earlier with no functional change.
---
The motivation for this code movement is that keeping it inside the switch statement matches MISRA's definition of unreachable code, but does not fall into the category of declarations without initialization, which is already a deviated aspect. An alternative approach would be to deviate BUILD_BUG_ON as well.
--- xen/arch/x86/xstate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/xstate.c +2 -1
@@ -396,9 +396,10 @@ void xrstor(struct vcpu *v, uint64_t mask)*/ for ( prev_faults = faults =0 ; ; prev_faults = faults ) { +BUILD_BUG_ON (sizeof (faults) !=4 );/* Clang doesn't support %z in asm. */ +switch (__builtin_expect (ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET ],8 ) ) { -BUILD_BUG_ON (sizeof (faults) !=4 );/* Clang doesn't support %z in asm. */ #define _xrstor (insn) \ asm volatile ( "1: .byte " insn "\n" \ "3:\n" \
Signature · 1 line
2.34.1
PATCH 1/7 xen/shutdown: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniGiven that 'hwdom_shutdown' is a noreturn function, unreachable breaks can be eliminated to resolve violations of Rule 2.1. On the occasion, the type of its parameter is changed to uint8_t.
No functional change.
--- xen/common/shutdown.c | 11 ++--------- xen/include/xen/shutdown.h | 2 +- 2 files changed, 3 insertions(+), 10 deletions(-)
xen/common/shutdown.c +2 -9
@@ -30,7 +30,7 @@ static void noreturn maybe_reboot(void) } } -void hwdom_shutdown (u8 reason) +void hwdom_shutdown (uint8_t reason) {switch ( reason ) {
@@ -38,39 +38,32 @@ void hwdom_shutdown(u8 reason)printk ("Hardware Dom%u halted: halting machine\n" , hardware_domain->domain_id);machine_halt (); -break ;/* not reached */ case SHUTDOWN_crash:debugger_trap_immediate ();printk ("Hardware Dom%u crashed: " , hardware_domain->domain_id);kexec_crash (CRASHREASON_HWDOM );maybe_reboot (); -break ;/* not reached */ case SHUTDOWN_reboot:printk ("Hardware Dom%u shutdown: rebooting machine\n" , hardware_domain->domain_id);machine_restart (0 ); -break ;/* not reached */ case SHUTDOWN_watchdog:printk ("Hardware Dom%u shutdown: watchdog rebooting machine\n" , hardware_domain->domain_id);kexec_crash (CRASHREASON_WATCHDOG );machine_restart (0 ); -break ;/* not reached */ case SHUTDOWN_soft_reset:printk ("Hardware domain %d did unsupported soft reset, rebooting.\n" , hardware_domain->domain_id);machine_restart (0 ); -break ;/* not reached */ default :printk ("Hardware Dom%u shutdown (unknown reason %u): " , hardware_domain->domain_id, reason);maybe_reboot (); -break ;/* not reached */ } -} - +}
xen/include/xen/shutdown.h +1 -1
@@ -6,7 +6,7 @@/* opt_noreboot: If true, machine will need manual reset on error. */ extern bool opt_noreboot; -void noreturnhwdom_shutdown (u8 reason); +void noreturnhwdom_shutdown (uint8_t reason);void noreturnmachine_restart (unsigned int delay_millisecs);void noreturnmachine_halt (void );
Signature · 1 line
2.34.1
PATCH 5/7 xen/arm: traps: add ASSERT_UNREACHABLE() where needed
In reply to Nicola VetriniThe branches of the switch after a call to 'do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked with ASSERT_UNREACHABLE().
--- xen/arch/arm/traps.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/arm/traps.c +3 -3
@@ -2152,7 +2152,7 @@ void do_trap_hyp_sync(struct cpu_user_regs *regs)case HSR_EC_SVE :/* An SVE exception is a bug somewhere in hypervisor code */ do_unexpected_trap ("SVE trap at EL2" , regs); -break ; +ASSERT_UNREACHABLE ();#endif case HSR_EC_DATA_ABORT_CURR_EL :case HSR_EC_INSTR_ABORT_CURR_EL :
@@ -2171,13 +2171,13 @@ void do_trap_hyp_sync(struct cpu_user_regs *regs)dump_hyp_walk (get_hfar (is_data));do_unexpected_trap (fault, regs); - -break ; +ASSERT_UNREACHABLE (); }default :printk ("Hypervisor Trap. HSR=%#" PRIregister" EC=0x%x IL=%x Syndrome=0x%" PRIx32"\n" , hsr.bits, hsr.ec, hsr.len, hsr.iss);do_unexpected_trap ("Hypervisor" , regs); +ASSERT_UNREACHABLE (); } }
Signature · 1 line
2.34.1
PATCH 2/7 x86/mm: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniThe "return 0" after the swich statement in 'xen/arch/x86/mm.c' is unreachable because all switch clauses end with returns. However, some of them can be substituted with "break"s to allow the "return 0" outside the switch to be reachable.
No functional changes.
--- xen/arch/x86/mm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
xen/arch/x86/mm.c +3 -3
@@ -4753,7 +4753,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) }spin_unlock (&d->arch.e820_lock); -return 0 ; +break ; }case XENMEM_machine_memory_map:
@@ -4818,7 +4818,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if (__copy_to_guest (arg, &ctxt.map,1 ) )return -EFAULT ; -return 0 ; +break ; }case XENMEM_machphys_mapping:
@@ -4834,7 +4834,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if (copy_to_guest (arg, &mapping,1 ) )return -EFAULT ; -return 0 ; +break ; }#ifdef CONFIG_HVM
Signature · 1 line
2.34.1
PATCH 4/7 xen/sched: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniThe break statement after the return statement is definitely unreachable and can be removed with no functional change.
--- xen/common/sched/core.c | 1 - 1 file changed, 1 deletion(-)
xen/common/sched/core.c +0 -1
@@ -2239,7 +2239,6 @@ static bool sched_tasklet_check_cpu(unsigned int cpu)/* fallthrough */ case TASKLET_enqueued|TASKLET_scheduled:return true; -break ;case TASKLET_scheduled:clear_bit (_TASKLET_scheduled, tasklet_work);/* fallthrough */
Signature · 1 line
2.34.1
cover address violations of MISRA C:2012 Rule 2.1
MISRA C:2012 Rule 2.1 states: "A project shall not contain unreachable code". As such, this series eliminates various instances of unreachable code found in Xen, by providing equivalent compliant constructs.
This series is loosely based on my earlier series [1], but the overall approach has changed since; for instance, declarations without initialization are now ignored, as detailed in docs/misra/deviations.rst.
[1] https://lore.kernel.org/xen-devel/[email protected]/
Nicola Vetrini (7):
xen/shutdown: address MISRA C:2012 Rule 2.1
x86/mm: address MISRA C:2012 Rule 2.1
xen/arm: address MISRA C:2012 Rule 2.1
xen/sched: address MISRA C:2012 Rule 2.1
xen/arm: traps: add ASSERT_UNREACHABLE() where needed x86/platform: removed break to address MISRA C:2012 Rule 2.1 x86/xstate: move BUILD_BUG_ON to address MISRA C:2012 Rule 2.1
xen/arch/arm/mm.c | 2 +- xen/arch/arm/traps.c | 6 +++--- xen/arch/arm/vgic-v3-its.c | 10 +++++----- xen/arch/x86/mm.c | 6 +++--- xen/arch/x86/platform_hypercall.c | 1 - xen/arch/x86/xstate.c | 3 ++- xen/common/sched/core.c | 1 - xen/common/shutdown.c | 11 ++--------- xen/include/xen/shutdown.h | 2 +- 9 files changed, 17 insertions(+), 25 deletions(-)
Signature · 1 line
2.34.1
PATCH 6/7 x86/platform: removed break to address MISRA C:2012 Rule 2.1
The break statement is redundant, hence it can be removed.
--- xen/arch/x86/platform_hypercall.c | 1 - 1 file changed, 1 deletion(-)
xen/arch/x86/platform_hypercall.c +0 -1
@@ -723,7 +723,6 @@ ret_t do_platform_op( ret =continue_hypercall_on_cpu (0 , cpu_down_helper, (void *)(unsigned long )cpu); -break ; }break ;
Signature · 1 line
2.34.1
PATCH 3/7 xen/arm: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniThe "return 1;" statements at the end of some cases in the switch of function 'vgic_v3_its_mmio_write' in 'vcig-v3-its.c' cause the unreachability of the "return 1;" statement after the switch, thus violating MISRA C:2012 Rule 2.1:
"A project shall not contain unreachable code".
The same is true for the switch in 'arch_memory_op' from 'xen/arch/arm/mm.c'.
--- xen/arch/arm/mm.c | 2 +- xen/arch/arm/vgic-v3-its.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-)
xen/arch/arm/mm.c +1 -1
@@ -277,7 +277,7 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE_PARAM(void) arg)/* XXX: memsharing not working yet */ case XENMEM_get_sharing_shared_pages:case XENMEM_get_sharing_freed_pages: -return 0 ; +break ;default :return -ENOSYS ;
xen/arch/arm/vgic-v3-its.c +5 -5
@@ -1255,7 +1255,7 @@ static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info, reg32 & GITS_CTLR_ENABLE);spin_unlock (&its->its_lock);spin_unlock (&its->vcmd_lock); -return 1 ; +break ; }case VREG32 (GITS_IIDR ):
@@ -1292,7 +1292,7 @@ static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info, its->creadr =0 ;spin_unlock (&its->its_lock); -return 1 ; +break ;case VREG64 (GITS_CWRITER ):if ( !vgic_reg64_check_access (info->dabt) ) goto bad_width;
@@ -1308,7 +1308,7 @@ static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info,spin_unlock (&its->vcmd_lock); -return 1 ; +break ;case VREG64 (GITS_CREADR ): goto write_ignore_64;
@@ -1353,7 +1353,7 @@ static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info, its->baser_dev = reg;spin_unlock (&its->its_lock); -return 1 ; +break ;case VREG64 (GITS_BASER1 ):/* collection table */ if ( !vgic_reg64_check_access (info->dabt) ) goto bad_width;
@@ -1384,7 +1384,7 @@ static int vgic_v3_its_mmio_write(struct vcpu *v, mmio_info_t *info, its->max_collections =0 ; its->baser_coll = reg;spin_unlock (&its->its_lock); -return 1 ; +break ;case VRANGE64 (GITS_BASER2 ,GITS_BASER7 ): goto write_ignore_64;
Signature · 1 line
2.34.1
Re: [XEN PATCH 3/7] xen/arm: address MISRA C:2012 Rule 2.1
Hi,
Quoted 8 lines · Nicola Vetrini
The "return 1;" statements at the end of some cases in the switch of function '
vgic_v3_its_mmio_write' in 'vcig-v3-its.c' cause the unreachability of the "return 1;" statement after the switch, thus violating MISRA C:2012 Rule 2.1:
"A project shall not contain unreachable code".The same is true for the switch in '
arch_memory_op' from 'xen/arch/arm/mm.c'.
For both cases, I actually much prefer the "return" version in the cases. In particular for the vGIC emulation the switch is quite large and it would not be trivial to know what happens after the break.
IOW, I would much prefer if we remove the "return ..." outside of the switch.
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
In reply to Nicola VetriniHi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().
I don't understand why this is necessary. The code should never be reachable because do_unexpected_trap() is a noreturn().
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 3/7] xen/arm: address MISRA C:2012 Rule 2.1
In reply to Julien GrallQuoted 11 lines · Julien Grall
Hi,
Quoted 6 lines · Nicola Vetrini
The "return 1;" statements at the end of some cases in the switch of function '
vgic_v3_its_mmio_write' in 'vcig-v3-its.c' cause the unreachability of the "return 1;" statement after the switch, thus violating MISRA C:2012 Rule 2.1:
"A project shall not contain unreachable code".
Quoted 6 lines
Quoted 2 lines
The same is true for the switch in '
arch_memory_op' from 'xen/arch/arm/mm.c'.For both cases, I actually much prefer the "return" version in the cases. In particular for the vGIC emulation the switch is quite large and it would not be trivial to know what happens after the break.
Because of this...
Quoted 3 lines
IOW, I would much prefer if we remove the "return ..." outside of the switch.
wouldn't it be better to add ASSERT_UNREACHABLE() before this return instead of removing it? This is what we have in e.g. vpl011 and it prevents mistakes.
~Michal
Re: [XEN PATCH 4/7] xen/sched: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 5 lines · Nicola Vetrini
The break statement after the return statement is definitely unreachable and can be removed with no functional change.
Signed-off-by Nicola Vetrini <[email protected]>
Re: [XEN PATCH 3/7] xen/arm: address MISRA C:2012 Rule 2.1
In reply to Michal OrzelQuoted 20 lines · Michal Orzel
Quoted 11 lines · Julien Grall
Hi,
Quoted 6 lines · Nicola Vetrini
The "return 1;" statements at the end of some cases in the switch of function '
vgic_v3_its_mmio_write' in 'vcig-v3-its.c' cause the unreachability of the "return 1;" statement after the switch, thus violating MISRA C:2012 Rule 2.1:
"A project shall not contain unreachable code".Quoted 6 lines
Quoted 2 lines
The same is true for the switch in '
arch_memory_op' from 'xen/arch/arm/mm.c'.For both cases, I actually much prefer the "return" version in the cases. In particular for the vGIC emulation the switch is quite large and it would not be trivial to know what happens after the break.
Quoted 7 lines
Because of this...
Quoted 3 lines
IOW, I would much prefer if we remove the "return ..." outside of the switch.
wouldn't it be better to add
ASSERT_UNREACHABLE()before this return instead of removing it? This is what we have in e.g. vpl011 and it prevents mistakes.
I am ok with that. But I am not sure if this would make ECLAIR unhappy.
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 3/7] xen/arm: address MISRA C:2012 Rule 2.1
Quoted 24 lines · Julien Grall
Quoted 23 lines · Michal Orzel
Quoted 11 lines · Julien Grall
Hi,
Quoted 6 lines · Nicola Vetrini
The "return 1;" statements at the end of some cases in the switch of function '
vgic_v3_its_mmio_write' in 'vcig-v3-its.c' cause the unreachability of the "return 1;" statement after the switch, thus violating MISRA C:2012 Rule 2.1:
"A project shall not contain unreachable code".Quoted 6 lines
Quoted 2 lines
The same is true for the switch in '
arch_memory_op' from 'xen/arch/arm/mm.c'.For both cases, I actually much prefer the "return" version in the cases. In particular for the vGIC emulation the switch is quite large and it would not be trivial to know what happens after the break.
Because of this...
Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
IOW, I would much prefer if we remove the "return ..." outside of the switch.
wouldn't it be better to add
ASSERT_UNREACHABLE()before this return instead of removing it? This is what we have in e.g. vpl011 and it prevents mistakes.I am ok with that. But I am not sure if this would make ECLAIR unhappy.
Cheers,
Should be okay. I'll test and report back
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
In reply to Julien GrallQuoted 9 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().Cheers,
It was meant as a safeguard against mistakes. There are MISRA rules that deal with this aspect (e.g., a noreturn function should not return), but they are not in Amendment 2, which is what Xen is following.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
Hi Nicola,
Quoted 4 lines · Nicola Vetrini
Quoted 3 lines · Julien Grall
Hi,
On 11/12/2023 10:30, Nicola Vetrini wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().Cheers,
It was meant as a safeguard against mistakes.
I am confused about which mistakes you are trying to prevent. Are you saying you are not trusting the noreturn attribute?
And if so, are you intending to add ASSERT_UNREACHABLE() after every single call to noreturn functions?
Quoted 1 line
There are MISRA rules that deal with this aspect (e.g., a noreturn function should not return), but they are not in Amendment 2, which is what Xen is following.
Do you mind providing a bit more details about the rules you are talking about? At least the numbers would be helpful.
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
Quoted 12 lines · Julien Grall
Hi Nicola,
Quoted 9 lines · Nicola Vetrini
Quoted 8 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
Cheers,
It was meant as a safeguard against mistakes.
I am confused about which mistakes you are trying to prevent. Are you saying you are not trusting the noreturn attribute?
And if so, are you intending to add
ASSERT_UNREACHABLE()after every single call to noreturn functions?
Replying to myself. What's confusing the most is that in [1], you decided to not add the ASSERT_UNREACHABLE(). Yet the problem is similar.
I'd also like to point out that by removing the "break", then if the 'noreturn' function turns out to return, then in prod build you would fallthrough to the next case. And who knows what's going to happen...
All of this really adds some confusion...
Quoted 3 lines
Quoted 2 lines
There are MISRA rules that deal with this aspect (e.g., a noreturn function should not return), but they are not in Amendment 2, which is what Xen is following.
Do you mind providing a bit more details about the rules you are talking about? At least the numbers would be helpful.
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
Quoted 19 lines · Julien Grall
Quoted 18 lines · Julien Grall
Hi Nicola,
Quoted 4 lines · Nicola Vetrini
Quoted 3 lines · Julien Grall
Hi,
On 11/12/2023 10:30, Nicola Vetrini wrote:
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().Cheers,
It was meant as a safeguard against mistakes.
I am confused about which mistakes you are trying to prevent. Are you saying you are not trusting the noreturn attribute?
Quoted 8 lines
Quoted 1 line
And if so, are you intending to add
ASSERT_UNREACHABLE()after every single call to noreturn functions?Replying to myself. What's confusing the most is that in [1], you decided to not add the
ASSERT_UNREACHABLE(). Yet the problem is similar.I'd also like to point out that by removing the "break", then if the 'noreturn' function turns out to return, then in prod build you would fallthrough to the next case. And who knows what's going to happen...
All of this really adds some confusion...
I should have checked before responding: do_trap_hyp_sync is not afaik noreturn. Specifically, do_trap_brk may return. If I worked under the wrong assumption, then certainly the ASSERT_UNREACHABLE-s should be dropped.
Quoted 7 lines
Quoted 3 lines
Quoted 2 lines
There are MISRA rules that deal with this aspect (e.g., a noreturn function should not return), but they are not in Amendment 2, which is what Xen is following.
Do you mind providing a bit more details about the rules you are talking about? At least the numbers would be helpful.
Cheers,
Sure. Here are the rules concerned with _Noreturn:
R17.9: A function declared with a _Noreturn function specifier shall not return to its caller R17.10: A function declared with a _Noreturn function specifier shall have void return type R17.11: A function that never returns should be declared with a _Noreturn function specifier
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
Quoted 32 lines · Nicola Vetrini
Quoted 31 lines · Julien Grall
Quoted 15 lines · Julien Grall
Hi Nicola,
Quoted 12 lines · Nicola Vetrini
Quoted 10 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().Cheers,
Quoted 7 lines
Quoted 1 line
It was meant as a safeguard against mistakes.
I am confused about which mistakes you are trying to prevent. Are you saying you are not trusting the noreturn attribute?
And if so, are you intending to add
ASSERT_UNREACHABLE()after every single call to noreturn functions?Replying to myself. What's confusing the most is that in [1], you decided to not add the
ASSERT_UNREACHABLE(). Yet the problem is similar.I'd also like to point out that by removing the "break", then if the 'noreturn' function turns out to return, then in prod build you would fallthrough to the next case. And who knows what's going to happen...
Quoted 6 lines
Quoted 2 lines
All of this really adds some confusion...
I should have checked before responding:
do_trap_hyp_syncis not afaik noreturn. Specifically,do_trap_brkmay return. If I worked under the wrong assumption, then certainly the ASSERT_UNREACHABLE-s should be dropped.
It looks like we could add noreturn to do_trap_brk. Julien what do you think?
Re: [XEN PATCH 1/7] xen/shutdown: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 7 lines · Nicola Vetrini
Given that '
hwdom_shutdown' is a noreturn function, unreachable breaks can be eliminated to resolve violations of Rule 2.1. On the occasion, the type of its parameter is changed touint8_t.No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
Re: [XEN PATCH 2/7] x86/mm: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 6 lines · Nicola Vetrini
The "return 0" after the swich statement in '
xen/arch/x86/mm.c' is unreachable because all switch clauses end with returns. However, some of them can be substituted with "break"s to allow the "return 0" outside the switch to be reachable.No functional changes.
This is correct but makes the code inconsistent. I would either remove the return 0; at the end of arch_memory_op, or do the following:
- initialize rc to 0 at the beginning: int rc = 0;
- all switch clauses break instead of return;
- at the end: return rc;
Quoted 40 lines
Signed-off-by Nicola Vetrini <[email protected]>--- xen/arch/x86/mm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)xen/arch/x86/mm.c +3
-3@@ -4753,7 +4753,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) }spin_unlock (&d->arch.e820_lock); -return 0 ; +break ; }case XENMEM_machine_memory_map:@@ -4818,7 +4818,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if (__copy_to_guest (arg, &ctxt.map,1 ) )return -EFAULT ; -return 0 ; +break ; }case XENMEM_machphys_mapping:@@ -4834,7 +4834,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)if (copy_to_guest (arg, &mapping,1 ) )return -EFAULT ; -return 0 ; +break ; }#ifdef CONFIG_HVM Signature · 1 line
2.34.1
Re: [XEN PATCH 4/7] xen/sched: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 4 lines · Nicola Vetrini
The break statement after the return statement is definitely unreachable and can be removed with no functional change.
Signed-off-by Nicola Vetrini <[email protected]>
Re: [XEN PATCH 6/7] x86/platform: removed break to address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 3 lines · Nicola Vetrini
The break statement is redundant, hence it can be removed.
Signed-off-by Nicola Vetrini <[email protected]>
Re: [XEN PATCH 7/7] x86/xstate: move BUILD_BUG_ON to address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 10 lines · Nicola Vetrini
The string literal inside the expansion of
BUILD_BUG_ONis considered unreachable code; however, such statement can be moved earlier with no functional change.Signed-off-by Nicola Vetrini <[email protected]>---
The motivation for this code movement is that keeping it inside the switch statement matches MISRA's definition of unreachable code, but does not fall into the category of declarations without initialization, which is already a deviated aspect. An alternative approach would be to deviateBUILD_BUG_ONas well.
I think that deviating BUILD_BUG_ON would be totally fine. But given that this patch is obviously correct:
Quoted 23 lines
--- xen/arch/x86/xstate.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)xen/arch/x86/xstate.c +2
-1@@ -396,9 +396,10 @@ void xrstor(struct vcpu *v, uint64_t mask)*/ for ( prev_faults = faults =0 ; ; prev_faults = faults ) { +BUILD_BUG_ON (sizeof (faults) !=4 );/* Clang doesn't support %z in asm. */ +switch (__builtin_expect (ptr->fpu_sse.x[FPU_WORD_SIZE_OFFSET ],8 ) ) { -BUILD_BUG_ON (sizeof (faults) !=4 );/* Clang doesn't support %z in asm. */ #define _xrstor (insn) \ asm volatile ( "1: .byte " insn "\n" \ "3:\n" \Signature · 1 line
2.34.1
Re: [XEN PATCH 2/7] x86/mm: address MISRA C:2012 Rule 2.1
In reply to Stefano StabelliniQuoted 6 lines · Stefano Stabellini
Quoted 5 lines · Nicola Vetrini
The "return 0" after the swich statement in '
xen/arch/x86/mm.c' is unreachable because all switch clauses end with returns. However, some of them can be substituted with "break"s to allow the "return 0" outside the switch to be reachable.
Quoted 9 lines
Quoted 1 line
No functional changes.
This is correct but makes the code inconsistent. I would either remove the return 0; at the end of
arch_memory_op, or do the following:- initialize rc to 0 at the beginning: int rc = 0;
- all switch clauses break instead of return;
- at the end: return rc;
Given the feedback on the Arm side, the first solution is likely to be preferred.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
In reply to Stefano StabelliniHi Stefano,
Quoted 36 lines · Stefano Stabellini
Quoted 35 lines · Nicola Vetrini
Quoted 33 lines · Julien Grall
Quoted 15 lines · Julien Grall
Hi Nicola,
Quoted 12 lines · Nicola Vetrini
Quoted 10 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().Cheers,
Quoted 7 lines
Quoted 1 line
It was meant as a safeguard against mistakes.
I am confused about which mistakes you are trying to prevent. Are you saying you are not trusting the noreturn attribute?
And if so, are you intending to add
ASSERT_UNREACHABLE()after every single call to noreturn functions?Replying to myself. What's confusing the most is that in [1], you decided to not add the
ASSERT_UNREACHABLE(). Yet the problem is similar.I'd also like to point out that by removing the "break", then if the 'noreturn' function turns out to return, then in prod build you would fallthrough to the next case. And who knows what's going to happen...
All of this really adds some confusion...
Quoted 6 lines
Quoted 3 lines
I should have checked before responding:
do_trap_hyp_syncis not afaik noreturn. Specifically,do_trap_brkmay return. If I worked under the wrong assumption, then certainly the ASSERT_UNREACHABLE-s should be dropped.It looks like we could add noreturn to
do_trap_brk. Julien what do you think?
You can't add noreturn to do_trap_brk(). It is used to handle WARN_ON() that *will* return.
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 1/7] xen/shutdown: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 3 lines · Nicola Vetrini
Given that '
hwdom_shutdown' is a noreturn function, unreachable breaks can be eliminated to resolve violations of Rule 2.1. On the occasion, the type of its parameter is changed touint8_t.
I don't particularly mind that extra change, but it's entirely unrelated here. We do such when we touch lines (or maybe adjacent lines) anyway, but here you had no need to touch either declaration or the header of the definition.
Quoted 13 lines
xen/common/shutdown.c +1
-1@@ -30,7 +30,7 @@ static void noreturn maybe_reboot(void) } } -void hwdom_shutdown (u8 reason) +void hwdom_shutdown (uint8_t reason) {switch ( reason ) {@@ -38,39 +38,32 @@ void hwdom_shutdown(u8 reason) printk("Hardware Dom%u halted: halting machine\n",
xen/common/shutdown.c void hwdom_shutdown(u8 reason) · line 48
Quoted 10 lines
xen/common/shutdown.c +0
-2hardware_domain->domain_id);machine_halt (); -break ;/* not reached */ case SHUTDOWN_crash:debugger_trap_immediate ();printk ("Hardware Dom%u crashed: " , hardware_domain->domain_id);kexec_crash (CRASHREASON_HWDOM );maybe_reboot (); -break ;/* not reached */
Given the function's name, I don't view this removal (and the similar one further down) as acceptable. The function first needs renaming so that from its name one can no longer (wrongly) infer that it may reboot or else return.
Jan
Re: [XEN PATCH 1/7] xen/shutdown: address MISRA C:2012 Rule 2.1
Quoted 10 lines · Jan Beulich
Quoted 3 lines · Nicola Vetrini
Given that '
hwdom_shutdown' is a noreturn function, unreachable breaks can be eliminated to resolve violations of Rule 2.1. On the occasion, the type of its parameter is changed touint8_t.I don't particularly mind that extra change, but it's entirely unrelated here. We do such when we touch lines (or maybe adjacent lines) anyway, but here you had no need to touch either declaration or the header of the definition.
Ok. I can make this a separate patch, since I'm already touching the function
Quoted 30 lines
Quoted 14 lines
xen/common/shutdown.c +1
-1@@ -30,7 +30,7 @@ static void noreturn maybe_reboot(void) } } -void hwdom_shutdown (u8 reason) +void hwdom_shutdown (uint8_t reason) {switch ( reason ) {@@ -38,39 +38,32 @@ void hwdom_shutdown(u8 reason)printk ("Hardware Dom%u halted: halting machine\n" , hardware_domain->domain_id);xen/common/shutdown.c void hwdom_shutdown(u8 reason) · line 49
Quoted 10 lines
xen/common/shutdown.c +0
-2machine_halt (); -break ;/* not reached */ case SHUTDOWN_crash:debugger_trap_immediate ();printk ("Hardware Dom%u crashed: " , hardware_domain->domain_id);kexec_crash (CRASHREASON_HWDOM );maybe_reboot (); -break ;/* not reached */ Given the function's name, I don't view this removal (and the similar one further down) as acceptable. The function first needs renaming so that from its name one can no longer (wrongly) infer that it may reboot or else return.
Quoted 1 line
Jan
Something like "reboot_or_halt" would be okay?
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 2/7] x86/mm: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 12 lines · Nicola Vetrini
Quoted 11 lines · Stefano Stabellini
Quoted 6 lines · Nicola Vetrini
The "return 0" after the swich statement in '
xen/arch/x86/mm.c' is unreachable because all switch clauses end with returns. However, some of them can be substituted with "break"s to allow the "return 0" outside the switch to be reachable.No functional changes.
This is correct but makes the code inconsistent. I would either remove the return 0; at the end of
arch_memory_op, or do the following:
Quoted 6 lines
Quoted 3 lines
- initialize rc to 0 at the beginning: int rc = 0;
- all switch clauses break instead of return;
- at the end: return rc;Given the feedback on the Arm side, the first solution is likely to be preferred.
I wouldn't mind either option, with
- the former ensured to be okay with all compiler versions we (still) support,
- the latter having the initialize rc to 0 part dropped; imo it's better if every case block makes sure to set the intended value explicitly.
Jan
Re: [XEN PATCH 7/7] x86/xstate: move BUILD_BUG_ON to address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 3 lines · Nicola Vetrini
The string literal inside the expansion of
BUILD_BUG_ONis considered unreachable code; however, such statement can be moved earlier with no functional change.
First: Why is this deemed dead code in its present position, but okay when moved? Second: While moving is indeed no functional change (really BUILD_BUG_ON() can be moved about anywhere, for not producing any code in the final binary), it removes the connection between it and the respective asm() (where %z would have been nice to use).
Jan
Re: [XEN PATCH 7/7] x86/xstate: move BUILD_BUG_ON to address MISRA C:2012 Rule 2.1
Quoted 10 lines · Jan Beulich
Quoted 3 lines · Nicola Vetrini
The string literal inside the expansion of
BUILD_BUG_ONis considered unreachable code; however, such statement can be moved earlier with no functional change.First: Why is this deemed dead code in its present position, but okay when moved? Second: While moving is indeed no functional change (really
BUILD_BUG_ON()can be moved about anywhere, for not producing any code in the final binary), it removes the connection between it and the respectiveasm()(where %z would have been nice to use).
Oh, and third: Which string literal? I expect you're not building with an ancient compiler, so it got to be
#define BUILD_BUG_ON(cond) ({ _Static_assert(!(cond), "!(" #cond ")"); })
which you see in use. Yet that string literal isn't "code" or "data", but an argument to _Static_assert(). Is Eclair perhaps not properly aware of _Static_assert()?
Jan
Re: [XEN PATCH 6/7] x86/platform: removed break to address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 1 line · Nicola Vetrini
The break statement is redundant, hence it can be removed.
Except ...
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 727
Quoted 9 lines
xen/arch/x86/platform_hypercall.c +0
-1@@ -723,7 +723,6 @@ ret_t do_platform_op( ret =continue_hypercall_on_cpu (0 , cpu_down_helper, (void *)(unsigned long )cpu); -break ; }break ;
... it wants to be the other break that is removed, imo. Andrew, Roger, what do you think? There are many such (again: imo) oddly placed break-s in that switch() ... In some cases there are also inner scopes without there being new local variables in there. IOW imo throughout this switch()
- pointless inner scopes want dropping,
- all "main" break-s want to have the same indentation.
Jan
Re: [XEN PATCH 1/7] xen/shutdown: address MISRA C:2012 Rule 2.1
In reply to Nicola VetriniQuoted 23 lines · Nicola Vetrini
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Nicola Vetrini
xen/common/shutdown.c +0
-1@@ -38,39 +38,32 @@ void hwdom_shutdown(u8 reason)printk ("Hardware Dom%u halted: halting machine\n" , hardware_domain->domain_id);machine_halt (); -break ;/* not reached */ Quoted 14 lines
xen/common/shutdown.c void hwdom_shutdown(u8 reason) · line 73
Quoted 7 lines
xen/common/shutdown.c +0
-1case SHUTDOWN_crash:debugger_trap_immediate ();printk ("Hardware Dom%u crashed: " , hardware_domain->domain_id);kexec_crash (CRASHREASON_HWDOM );maybe_reboot (); -break ;/* not reached */ Given the function's name, I don't view this removal (and the similar one
further down) as acceptable. The function first needs renaming so that from its name one can no longer (wrongly) infer that it may reboot or else
return.
Quoted 1 line
Something like "
reboot_or_halt" would be okay?
I think so, yes.
Jan
Re: [XEN PATCH 7/7] x86/xstate: move BUILD_BUG_ON to address MISRA C:2012 Rule 2.1
In reply to Jan BeulichQuoted 15 lines · Jan Beulich
Quoted 10 lines · Jan Beulich
Quoted 3 lines · Nicola Vetrini
The string literal inside the expansion of
BUILD_BUG_ONis considered unreachable code; however, such statement can be moved earlier with no functional change.First: Why is this deemed dead code in its present position, but okay when moved? Second: While moving is indeed no functional change (really
BUILD_BUG_ON()can be moved about anywhere, for not producing any code in the final binary), it removes the connection between it and the respectiveasm()(where %z would have been nice to use).Oh, and third: Which string literal? I expect you're not building with an ancient compiler, so it got to be
Quoted 7 lines
#define
BUILD_BUG_ON(cond)({_Static_assert(!(cond), "!(" #cond ")"); })which you see in use. Yet that string literal isn't "code" or "data", but an argument to
_Static_assert(). Is Eclair perhaps not properly aware of_Static_assert()?Jan
On further inspection, this should have fallen into the deviation for pure decls. This patch can be dropped, we'll adjust this inside ECLAIR.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 7/7] x86/xstate: move BUILD_BUG_ON to address MISRA C:2012 Rule 2.1
Quoted 22 lines · Nicola Vetrini
Quoted 21 lines · Jan Beulich
Quoted 5 lines · Jan Beulich
Quoted 3 lines · Nicola Vetrini
The string literal inside the expansion of
BUILD_BUG_ONis considered unreachable code; however, such statement can be moved earlier with no functional change.Quoted 8 lines
First: Why is this deemed dead code in its present position, but okay when
moved? Second: While moving is indeed no functional change (reallyBUILD_BUG_ON()can be moved about anywhere, for not producing any code in
the final binary), it removes the connection between it and the respectiveasm()(where %z would have been nice to use).Oh, and third: Which string literal? I expect you're not building with an ancient compiler, so it got to be
#define
BUILD_BUG_ON(cond)({_Static_assert(!(cond), "!(" #cond ")"); })
Quoted 8 lines
Quoted 5 lines
which you see in use. Yet that string literal isn't "code" or "data", but
an argument to_Static_assert(). Is Eclair perhaps not properly aware of_Static_assert()?On further inspection, this should have fallen into the deviation for pure decls. This patch can be dropped, we'll adjust this inside ECLAIR.
What's the connection to "pure" here? Or are you merely piggybacking on that attribute for this non-function?
Jan
Re: [XEN PATCH 7/7] x86/xstate: move BUILD_BUG_ON to address MISRA C:2012 Rule 2.1
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Nicola Vetrini
Quoted 26 lines · Jan Beulich
Quoted 5 lines · Jan Beulich
Quoted 3 lines · Nicola Vetrini
The string literal inside the expansion of
BUILD_BUG_ONis considered unreachable code; however, such statement can be moved earlier with no functional change.Quoted 8 lines
First: Why is this deemed dead code in its present position, but okay when
moved? Second: While moving is indeed no functional change (reallyBUILD_BUG_ON()can be moved about anywhere, for not producing any code in
the final binary), it removes the connection between it and the respectiveasm()(where %z would have been nice to use).Oh, and third: Which string literal? I expect you're not building with an ancient compiler, so it got to be
#define
BUILD_BUG_ON(cond)({_Static_assert(!(cond), "!(" #cond ")"); })which you see in use. Yet that string literal isn't "code" or "data", but
an argument to_Static_assert(). Is Eclair perhaps not properly aware of_Static_assert()?
Quoted 7 lines
Quoted 2 lines
On further inspection, this should have fallen into the deviation for pure decls. This patch can be dropped, we'll adjust this inside ECLAIR.
What's the connection to "pure" here? Or are you merely piggybacking on that attribute for this non-function?
Jan
Just a naming coincidence, there aren't any attributes involved. No change to Xen code is needed.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
In reply to Julien GrallHi,
Quoted 7 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().
From the matrix discussion, it wasn't clear what was my position on this patch.
I would much prefer if the breaks are kept. I could accept:
ASSERT_UNREACHABLE();
break;
But this solution is a Nack because if you are concerned about functions like do_unexpected_trap() to return by mistaken, then it needs to also be safe in production.
The current proposal is not safe.
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 6/7] x86/platform: removed break to address MISRA C:2012 Rule 2.1
In reply to Jan BeulichQuoted 9 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
The break statement is redundant, hence it can be removed.
Except ...
Quoted 4 lines
xen/arch/x86/platform_hypercall.c +0
-0@@ -723,7 +723,6 @@ ret_t do_platform_op(
Quoted 7 lines
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 727
Quoted 5 lines
xen/arch/x86/platform_hypercall.c +0
-1ret =continue_hypercall_on_cpu (0 , cpu_down_helper, (void *)(unsigned long )cpu); -break ; }break ;... it wants to be the other break that is removed, imo.
I was also about to comment about which of the two breaks to remove... I didn't know if there are x86 specific conventions so I didn't say anything about it. But I also think it is more natural to keep the other break.
Re: [XEN PATCH 6/7] x86/platform: removed break to address MISRA C:2012 Rule 2.1
In reply to Jan BeulichQuoted 23 lines · Jan Beulich
Quoted 1 line · Nicola Vetrini
The break statement is redundant, hence it can be removed.
Except ...
xen/arch/x86/platform_hypercall.c ret_t do_platform_op( · line 727
Quoted 9 lines
xen/arch/x86/platform_hypercall.c +0
-1@@ -723,7 +723,6 @@ ret_t do_platform_op( ret =continue_hypercall_on_cpu (0 , cpu_down_helper, (void *)(unsigned long )cpu); -break ; }break ;... it wants to be the other break that is removed, imo. Andrew, Roger, what do you think? There are many such (again: imo) oddly placed break-s in that
switch()... In some cases there are also inner scopes without there being new local variables in there. IOW imo throughout thisswitch()
- pointless inner scopes want dropping,
- all "main" break-s want to have the same indentation.
Quoted 1 line
Jan
Ok. I'm not particularly keen on doing a full style cleanup; I can drop the other break, for the sake of resolving the MISRA violation, so that the cleanup can happen anytime.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
In reply to Julien GrallQuoted 15 lines · Julien Grall
Hi,
Quoted 7 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().From the matrix discussion, it wasn't clear what was my position on this patch.
I would much prefer if the breaks are kept. I could accept:
Quoted 8 lines
ASSERT_UNREACHABLE();
break;But this solution is a Nack because if you are concerned about functions like
do_unexpected_trap()to return by mistaken, then it needs to also be safe in production.The current proposal is not safe.
Cheers,
Ok. I wonder whether the should be applied here in vcpreg.c:
xen/arch/arm/vcpreg.c +2 -1
@@ -707,7 +707,8 @@ void do_cp10(struct cpu_user_regs *regs, const union hsr hsr)inject_undef_exception (regs, hsr);return ; } - + +ASSERT_UNREACHABLE ();advance_pc (regs, hsr); }
the rationale being that, should the switch somehow fail to return, the advance_pc would be called, rather than doing nothing.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 2/7] x86/mm: address MISRA C:2012 Rule 2.1
In reply to Jan BeulichQuoted 17 lines · Jan Beulich
Quoted 16 lines · Nicola Vetrini
Quoted 14 lines · Stefano Stabellini
Quoted 6 lines · Nicola Vetrini
The "return 0" after the swich statement in '
xen/arch/x86/mm.c' is unreachable because all switch clauses end with returns. However, some of them can be substituted with "break"s to allow the "return 0" outside the switch to be reachable.No functional changes.
This is correct but makes the code inconsistent. I would either remove the return 0; at the end of
arch_memory_op, or do the following:- initialize rc to 0 at the beginning: int rc = 0;
- all switch clauses break instead of return;
- at the end: return rc;
Quoted 6 lines
Quoted 2 lines
Given the feedback on the Arm side, the first solution is likely to be preferred.
I wouldn't mind either option, with
- the former ensured to be okay with all compiler versions we (still) support,
I tested a stripped-down version of the switch on godbolt.org (as far back as gcc-4.8.5) and it doesn't complain. It should be tested on a real Xen build, though.
Quoted 4 lines
- the latter having the initialize rc to 0 part dropped; imo it's better
if every case block makes sure to set the intended value explicitly.
This is a lot of churn, I'd rather avoid it.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 2/7] x86/mm: address MISRA C:2012 Rule 2.1
Quoted 22 lines · Nicola Vetrini
Quoted 21 lines · Jan Beulich
Quoted 19 lines · Nicola Vetrini
Quoted 8 lines · Stefano Stabellini
Quoted 6 lines · Nicola Vetrini
The "return 0" after the swich statement in '
xen/arch/x86/mm.c' is unreachable because all switch clauses end with returns. However, some of them can be substituted with "break"s to allow the "return 0" outside the switch to be reachable.No functional changes.
Quoted 7 lines
This is correct but makes the code inconsistent. I would either remove
the return 0; at the end ofarch_memory_op, or do the following:- initialize rc to 0 at the beginning: int rc = 0;
- all switch clauses break instead of return;
- at the end: return rc;Given the feedback on the Arm side, the first solution is likely to be preferred.
Quoted 7 lines
Quoted 3 lines
I wouldn't mind either option, with
- the former ensured to be okay with all compiler versions we (still) support,I tested a stripped-down version of the switch on godbolt.org (as far back as gcc-4.8.5) and it doesn't complain. It should be tested on a real Xen build, though.
I didn't fear any issue when going back to just 4.8. Quoting ./README:
- For x86:
- GCC 4.1.2_20070115 or later
Quoted 5 lines
Quoted 3 lines
- the latter having the initialize rc to 0 part dropped; imo it's better
if every case block makes sure to set the intended value explicitly.This is a lot of churn, I'd rather avoid it.
Rant (sorry): There's already excessive churn for entirely benign issues that Misra claims need adjusting.
Jan
Re: [XEN PATCH 2/7] x86/mm: address MISRA C:2012 Rule 2.1
Quoted 27 lines · Jan Beulich
Quoted 26 lines · Nicola Vetrini
Quoted 24 lines · Jan Beulich
Quoted 13 lines · Nicola Vetrini
Quoted 12 lines · Stefano Stabellini
Quoted 6 lines · Nicola Vetrini
The "return 0" after the swich statement in '
xen/arch/x86/mm.c' is unreachable because all switch clauses end with returns. However, some of them can be substituted with "break"s to allow the "return 0" outside the switch to be reachable.No functional changes.
This is correct but makes the code inconsistent. I would either remove
the return 0; at the end ofarch_memory_op, or do the following:Quoted 6 lines
Quoted 3 lines
- initialize rc to 0 at the beginning: int rc = 0;
- all switch clauses break instead of return;
- at the end: return rc;Given the feedback on the Arm side, the first solution is likely to be preferred.
I wouldn't mind either option, with
- the former ensured to be okay with all compiler versions we (still) support,
Quoted 9 lines
Quoted 3 lines
I tested a stripped-down version of the switch on godbolt.org (as far back as gcc-4.8.5) and it doesn't complain. It should be tested on a real Xen build, though.
I didn't fear any issue when going back to just 4.8. Quoting ./README:
- For x86:
- GCC 4.1.2_20070115 or later
I found no issue in 4.1.2 (see https://godbolt.org/z/cxecnKseG)
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
- the latter having the initialize rc to 0 part dropped; imo it's better
if every case block makes sure to set the intended value
explicitly.This is a lot of churn, I'd rather avoid it.
Rant (sorry): There's already excessive churn for entirely benign issues that Misra claims need adjusting.
Jan
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
In reply to Nicola VetriniHi,
Quoted 42 lines · Nicola Vetrini
Quoted 15 lines · Julien Grall
Hi,
Quoted 7 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().From the matrix discussion, it wasn't clear what was my position on this patch.
I would much prefer if the breaks are kept. I could accept:
Quoted 8 lines
ASSERT_UNREACHABLE();
break;But this solution is a Nack because if you are concerned about functions like
do_unexpected_trap()to return by mistaken, then it needs to also be safe in production.The current proposal is not safe.
Cheers,
Ok. I wonder whether the should be applied here in
vcpreg.c:xen/arch/arm/vcpreg.c +2
-1@@ -707,7 +707,8 @@ void do_cp10(struct cpu_user_regs *regs, const union hsr hsr) inject_undef_exception(regs, hsr); return; } - + + ASSERT_UNREACHABLE(); advance_pc(regs, hsr); }
Quoted 1 line
the rationale being that, should the switch somehow fail to return, the
advance_pcwould be called, rather than doing nothing.
To clarify, advance_pc(regs, hsr) would still be called in production build. So if you are concerned about advance_pc() been called, then adding an ASSERT_UNREACHABLE() is not going to help.
It took me a little while to confirm that none of the path effectively returns due to the macros (in hindsight, it wasn't a good idea of mine to introduce them).
Depending on what we are trying to solve there are 3 possible approach:
1. Leave advance_pc(). This means we could potentially
a. Advance the PC twice (if it was already called) and therefore
skipping an instruction
b. Advance the PC once without an emulation
2. Remove advance_pc(). If we already called the function, then there
is no problem. Otherwise, we would trap in a loop effectively rendering the guest vCPU unusable.
3. Replace with domain_crash()
Here it feels, that 3 is more suitable as this gives a clear indication why/where the emulation gone wrong.
This may still need to be accompanied with a ASSERT_UNREACHABLE() to please MISRA.
Bertrand, Michal, Stefano, what do you think?
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
Quoted 29 lines · Julien Grall
Hi,
Quoted 26 lines · Nicola Vetrini
Quoted 25 lines · Julien Grall
Hi,
Quoted 8 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().From the matrix discussion, it wasn't clear what was my position on this patch.
I would much prefer if the breaks are kept. I could accept:
ASSERT_UNREACHABLE();
break;But this solution is a Nack because if you are concerned about functions like
do_unexpected_trap()to return by mistaken, then it needs to also be safe in production.The current proposal is not safe.
I re-read the email thread. I also do not think that this is useful:
do_unexpected_trap("SVE trap at EL2", regs);- break;
+ ASSERT_UNREACHABLE();
I also do not think that we should be concerned about functions like do_unexpected_trap() to return by mistaken.
That said, what is the problem from MISRA point of view that we are trying to fix? Is the only problem the presence of break; after the call to a noreturn function?
If that's not allowed, I would suggest to do this:
do_unexpected_trap("SVE trap at EL2", regs);- break;
+ /* break; */
Or deviate "break" globally as it doesn't seem to be a safety risk in my opinion. If nothing else, it should make the code a bit safer because in case of mistakes in do_unexpected_trap, at least we would continue to follow a more reasonable code path rather than blindly falling through the next switch case by accident.
Quoted 38 lines
Quoted 17 lines
Ok. I wonder whether the should be applied here in
vcpreg.c:xen/arch/arm/vcpreg.c +2
-1@@ -707,7 +707,8 @@ void do_cp10(struct cpu_user_regs *regs, const union hsr hsr) inject_undef_exception(regs, hsr); return; } - + + ASSERT_UNREACHABLE(); advance_pc(regs, hsr); }Quoted 2 lines
the rationale being that, should the switch somehow fail to return, the
advance_pcwould be called, rather than doing nothing.To clarify,
advance_pc(regs, hsr)would still be called in production build. So if you are concerned aboutadvance_pc()been called, then adding anASSERT_UNREACHABLE()is not going to help.It took me a little while to confirm that none of the path effectively returns due to the macros (in hindsight, it wasn't a good idea of mine to introduce them).
Depending on what we are trying to solve there are 3 possible approach:
1. Leaveadvance_pc(). This means we could potentially
a. Advance the PC twice (if it was already called) and therefore skipping
an instruction
b. Advance the PC once without an emulation
2. Removeadvance_pc(). If we already called the function, then there is no
problem. Otherwise, we would trap in a loop effectively rendering the guest vCPU unusable.
3. Replace withdomain_crash()
Quoted 7 lines
Here it feels, that 3 is more suitable as this gives a clear indication why/where the emulation gone wrong.
This may still need to be accompanied with a
ASSERT_UNREACHABLE()to please MISRA.Bertrand, Michal, Stefano, what do you think?
Yes, I would go with 3., replace advance_pc with domain_crash. Assuming that it would also solve the violation in ECLAIR.
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
Quoted 52 lines · Stefano Stabellini
Quoted 29 lines · Julien Grall
Hi,
Quoted 26 lines · Nicola Vetrini
Quoted 25 lines · Julien Grall
Hi,
Quoted 8 lines · Julien Grall
Hi,
Quoted 3 lines · Nicola Vetrini
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().From the matrix discussion, it wasn't clear what was my position on this patch.
I would much prefer if the breaks are kept. I could accept:
ASSERT_UNREACHABLE();
break;But this solution is a Nack because if you are concerned about functions like
do_unexpected_trap()to return by mistaken, then it needs to also be safe in production.The current proposal is not safe.
I re-read the email thread. I also do not think that this is useful:
do_unexpected_trap("SVE trap at EL2", regs);(quoted diff) +1
-1- break; + ASSERT_UNREACHABLE();I also do not think that we should be concerned about functions like
do_unexpected_trap()to return by mistaken.That said, what is the problem from MISRA point of view that we are trying to fix? Is the only problem the presence of break; after the call to a noreturn function?
If that's not allowed, I would suggest to do this:
do_unexpected_trap("SVE trap at EL2", regs);(quoted diff) +1
-1- break; + /* break; */
Quoted 7 lines
Or deviate "break" globally as it doesn't seem to be a safety risk in my opinion. If nothing else, it should make the code a bit safer because in case of mistakes in
do_unexpected_trap, at least we would continue to follow a more reasonable code path rather than blindly falling through the next switch case by accident.
That doesn't seem like a good idea to deviate just "break". However, Julien's earlier proposal
ASSERT_UNREACHABLE();
break;
is ok, though it could be shrunk in a macro
#define unreachable_break ASSERT_UNREACHABLE(); break;
or just
#define unreachable_break break;
so that "unreachable_break" can be deviated.
Quoted 43 lines
Quoted 43 lines
Quoted 17 lines
Ok. I wonder whether the should be applied here in
vcpreg.c:xen/arch/arm/vcpreg.c +2
-1@@ -707,7 +707,8 @@ void do_cp10(struct cpu_user_regs *regs, const union hsr hsr) inject_undef_exception(regs, hsr); return; } - + + ASSERT_UNREACHABLE(); advance_pc(regs, hsr); }Quoted 2 lines
the rationale being that, should the switch somehow fail to return, the
advance_pcwould be called, rather than doing nothing.To clarify,
advance_pc(regs, hsr)would still be called in production build. So if you are concerned aboutadvance_pc()been called, then adding anASSERT_UNREACHABLE()is not going to help.It took me a little while to confirm that none of the path effectively returns due to the macros (in hindsight, it wasn't a good idea of mine to introduce them).
Depending on what we are trying to solve there are 3 possible approach:
1. Leaveadvance_pc(). This means we could potentially
a. Advance the PC twice (if it was already called) and therefore
skipping
an instruction
b. Advance the PC once without an emulation
2. Removeadvance_pc(). If we already called the function, then
there is no
problem. Otherwise, we would trap in a loop effectively rendering the guest vCPU unusable.
3. Replace withdomain_crash()Here it feels, that 3 is more suitable as this gives a clear indication why/where the emulation gone wrong.
Quoted 7 lines
Quoted 4 lines
This may still need to be accompanied with a
ASSERT_UNREACHABLE()to please MISRA.Bertrand, Michal, Stefano, what do you think?
Yes, I would go with 3., replace
advance_pcwithdomain_crash. Assuming that it would also solve the violation in ECLAIR.
It needs to be prefixed with an ASSERT_UNREACHABLE(), though, because it's still a violation if there is no execution path leading to domain_crash(), but other than that it seems the safest choice.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
Hi Julien,
Quoted 4 lines
Quoted 2 lines
Yes, I would go with 3., replace
advance_pcwithdomain_crash. Assuming that it would also solve the violation in ECLAIR.It needs to be prefixed with an
ASSERT_UNREACHABLE(), though, because it's still a violation if there is no execution path leading todomain_crash(), but other than that it seems the safest choice.
Assuming there are no objections to going forward with this proposal, would you mind telling me how can I do the proper domain_crash call. Most of the examples get a "struct domain *" from a parameter or from the macro "current", so I was thinking of
domain_crash(current->domain);
but I'm not so sure about this, as there are no other uses in vcpreg.c. You can also submit the patch yourself, if you prefer.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
Hi,
Quoted 10 lines · Nicola Vetrini
Quoted 4 lines
Quoted 2 lines
Yes, I would go with 3., replace
advance_pcwithdomain_crash. Assuming that it would also solve the violation in ECLAIR.It needs to be prefixed with an
ASSERT_UNREACHABLE(), though, because it's still a violation if there is no execution path leading todomain_crash(), but other than that it seems the safest choice.Assuming there are no objections to going forward with this proposal, would you mind telling me how can I do the proper
domain_crashcall. Most of the examples get a "struct domain *" from a parameter or from the macro "current", so I was thinking of
domain_crash(current->domain);but I'm not so sure about this, as there are no other uses in
vcpreg.c.
That would be correct. All the helpers in vcpreg.c are meant to work on the current vCPU.
Quoted 1 line
You can also submit the patch yourself, if you prefer.
I am not entirely sure about which justification you want to use for MISRA. So here the diff:
xen/arch/arm/vcpreg.c +7 -1
@@ -708,7 +708,13 @@ void do_cp10(struct cpu_user_regs *regs, const union hsr hsr)return ; } -advance_pc (regs, hsr); +/* +* All the cases in the switch should return. If this is not the +* case, then something went wrong and it is best to crash the +* domain. +*/ +ASSERT_UNREACHABLE (); +domain_crash (current->domain); }void do_cp (struct cpu_user_regs *regs,const union hsr hsr)
Cheers,
Signature · 1 line
Julien Grall
Re: [XEN PATCH 5/7] xen/arm: traps: add ASSERT_UNREACHABLE() where needed
In reply to Nicola VetriniQuoted 72 lines · Nicola Vetrini
Quoted 55 lines · Stefano Stabellini
Quoted 22 lines · Julien Grall
Hi,
Quoted 6 lines · Nicola Vetrini
Quoted 5 lines · Julien Grall
Hi,
Quoted 2 lines · Julien Grall
Hi,
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines · Nicola Vetrini
Quoted 3 lines
The branches of the switch after a call to '
do_unexpected_trap' cannot return, but there is one path that may return, hence only some clauses are marked withASSERT_UNREACHABLE().I don't understand why this is necessary. The code should never be reachable because
do_unexpected_trap()is anoreturn().From the matrix discussion, it wasn't clear what was my position on
this
Quoted 4 lines
Quoted 4 lines
patch.
I would much prefer if the breaks are kept. I could accept:
Quoted 10 lines
Quoted 4 lines
Quoted 4 lines
ASSERT_UNREACHABLE();
break;But this solution is a Nack because if you are concerned about
functions
Quoted 1 line
Quoted 1 line
like
do_unexpected_trap()to return by mistaken, then it needs to alsobe
Quoted 3 lines
Quoted 3 lines
safe in production.
The current proposal is not safe.
I re-read the email thread. I also do not think that this is useful:
do_unexpected_trap("SVE trap at EL2", regs);(quoted diff) +1
-1- break; + ASSERT_UNREACHABLE();I also do not think that we should be concerned about functions like
do_unexpected_trap()to return by mistaken.That said, what is the problem from MISRA point of view that we are trying to fix? Is the only problem the presence of break; after the call to a noreturn function?
If that's not allowed, I would suggest to do this:
do_unexpected_trap("SVE trap at EL2", regs);(quoted diff) +1
-1- break; + /* break; */Quoted 7 lines
Or deviate "break" globally as it doesn't seem to be a safety risk in my opinion. If nothing else, it should make the code a bit safer because in case of mistakes in
do_unexpected_trap, at least we would continue to follow a more reasonable code path rather than blindly falling through the next switch case by accident.That doesn't seem like a good idea to deviate just "break". However, Julien's earlier proposal
ASSERT_UNREACHABLE();
break;is ok, though it could be shrunk in a macro
Quoted 7 lines
#define
unreachable_breakASSERT_UNREACHABLE(); break;or just
#define
unreachable_breakbreak;so that "
unreachable_break" can be deviated.
Let's just go with:
ASSERT_UNREACHABLE();
break;
If Julien is OK with it too.
Just to make sure we are all aligned on what the problem and solution are:
- we are not concerned that do_unexpected_trap could return
- it is just forbidden to have any code after something that doesn't return
- not even a break;
- so we need to use ASSERT_UNREACHABLE() as a marker to tell ECLAIR to ignore the violation