Skip to contentSkip to conversation

xen: address MISRA C:2012 Rule 2.1

PATCH In review Nicola Vetrini 5 participants 56 messages Aug 2, 2023 – Aug 18, 2023

lore
Share & tools
lore
#0 lore raw

cover xen: address MISRA C:2012 Rule 2.1

The headline for Rule 2.1 states:
"A project shall not contain unreachable code". Violations of this rule addressed by this patch are caused by two constructs:

1. Declarations inside switch statements, before any clause. This construct is allowed by the language, but is considered unreachable code, since the program execution doesn't ever reach these statements, even though the variables are introduced in the switch block.
2. Code following functions that are not intended to return to the caller or additional statements required by defensive programming practices or other MISRA rules (such as a break to end each switch clause, even if that break is effectively unreachable).

(1) is resolved by moving the declarations in an appropriate scope, while (2) is addressed by adding ASSERT_UNREACHABLE() calls to signal that the code below is intentionally unreachable, thus motivating the violation of the rule.

---
The approach taken in this series to address the violations is the outcome of a MISRA C group meeting held 25/07/2023.

Nicola Vetrini (11):
x86/efi: move variable declaration to address MISRA C:2012 Rule 2.1
x86: move declarations to address MISRA C:2012 Rule 2.1 x86/uaccess: move declarations to address MISRA C:2012 Rule 2.1
x86emul: move variable definitions to address MISRA C:2012 Rule 2.1 drivers/pci: move variable definitions to address MISRA C:2012 Rule

    2.1

xen/ioreq: move variable declaration to address MISRA C:2012 Rule 2.1
xen: address MISRA C:2012 Rule 2.1
xen: move declarations to address MISRA C:2012 Rule 2.1 x86/xstate: moved BUILD_BUG_ON to address MISRA C:2012 Rule 2.1 xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1 x86/mm: Add assertion to address MISRA C:2012 Rule 2.1

 xen/arch/x86/cpuid.c                   |  3 +--
 xen/arch/x86/domain.c                  | 23 +++++++++++------------
 xen/arch/x86/efi/efi-boot.h            |  5 ++---
 xen/arch/x86/hvm/emulate.c             |  9 ++++-----
 xen/arch/x86/hvm/hvm.c                 | 10 ++++------
 xen/arch/x86/include/asm/uaccess.h     |  6 ++++--
 xen/arch/x86/irq.c                     |  3 +--
 xen/arch/x86/mm.c                      |  1 +
 xen/arch/x86/mm/p2m-pod.c              |  1 +
 xen/arch/x86/msr.c                     |  3 +--
 xen/arch/x86/x86_emulate/0f01.c        |  7 +++----
 xen/arch/x86/x86_emulate/blk.c         | 17 ++++++++---------
 xen/arch/x86/x86_emulate/decode.c      |  3 ++-
 xen/arch/x86/x86_emulate/fpu.c         |  3 +--
 xen/arch/x86/x86_emulate/util-xen.c    |  4 ++--
 xen/arch/x86/x86_emulate/x86_emulate.c | 14 +++++++-------
 xen/arch/x86/xstate.c                  |  3 ++-
 xen/common/compat/memory.c             |  3 +--
 xen/common/domain.c                    | 15 +++++++--------
 xen/common/ioreq.c                     |  3 +--
 xen/common/sched/core.c                |  1 +
 xen/common/shutdown.c                  | 18 ++++++++++++------
 xen/drivers/passthrough/pci.c          | 10 ++++------
 23 files changed, 81 insertions(+), 84 deletions(-)

--
2.34.1

#1 lore raw

PATCH 1/11 x86/efi: move variable declaration to address MISRA C:2012 Rule 2.1

The variable declaration is moved where it's actually used, rather than being declared in the switch before any clause, thus being classified as unreachable code.

No functional changes.

---
 xen/arch/x86/efi/efi-boot.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/efi/efi-boot.h +2 -3
index 92f4cfe8bd..b00441b1a2 100644
@@ -390,8 +390,6 @@ static void __init efi_arch_edd(void)
         {
             switch ( DevicePathType(devp.DevPath) )
             {
-                const u8 *p;
-
             case ACPI_DEVICE_PATH:
                 if ( state != root || boot_edd_info_nr > EDD_INFO_MAX )
                     break;
@@ -463,7 +461,8 @@ static void __init efi_arch_edd(void)
                 params->device_path_info_length =
                     sizeof(struct edd_device_params) -
                     offsetof(struct edd_device_params, key);
-                for ( p = (const u8 *)&params->key; p < &params->checksum; ++p )
+                for ( const u8 *p = (const u8 *)&params->key;
+                      p < &params->checksum; ++p )
                     params->checksum -= *p;
                 break;
             case MEDIA_DEVICE_PATH:
Signature · 1 line
2.34.1
#2 lore raw

Re: [XEN PATCH 01/11] x86/efi: move variable declaration to address MISRA C:2012 Rule 2.1

Quoted 25 lines · Nicola Vetrini

The variable declaration is moved where it's actually used, rather than being declared in the switch before any clause, thus being classified as unreachable code.

No functional changes.

---
 xen/arch/x86/efi/efi-boot.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/efi/efi-boot.h +0 -2
index 92f4cfe8bd..b00441b1a2 100644
@@ -390,8 +390,6 @@ static void __init efi_arch_edd(void)
         {
             switch ( DevicePathType(devp.DevPath) )
             {
-                const u8 *p;
-
             case ACPI_DEVICE_PATH:
                 if ( state != root || boot_edd_info_nr > EDD_INFO_MAX )
                     break;

xen/arch/x86/efi/efi-boot.h static void __init efi_arch_edd(void) · line 465

Quoted 6 lines
xen/arch/x86/efi/efi-boot.h +2 -1
@@ -463,7 +461,8 @@ static void __init efi_arch_edd(void)
                 params->device_path_info_length =
                     sizeof(struct edd_device_params) -
                     offsetof(struct edd_device_params, key);
-                for ( p = (const u8 *)&params->key; p < &params->checksum; ++p )
+                for ( const u8 *p = (const u8 *)&params->key;
+                      p < &params->checksum; ++p )

In Xen we don't mix declaration and code. So the following is not something we use:

for (int i = 0; i < 10; i++)

I think you'd have to introduce another block under case MESSAGING_DEVICE_PATH so that you can moved const u8 *p there

Quoted 6 lines
xen/arch/x86/efi/efi-boot.h +0 -0
                     params->checksum -= *p;
                 break;
             case MEDIA_DEVICE_PATH:
Signature · 1 line
2.34.1
#3 lore raw

Re: [XEN PATCH 01/11] x86/efi: move variable declaration to address MISRA C:2012 Rule 2.1

Quoted 36 lines · Stefano Stabellini
Quoted 25 lines · Nicola Vetrini

The variable declaration is moved where it's actually used, rather than being declared in the switch before any clause, thus being classified as unreachable code.

No functional changes.

---
 xen/arch/x86/efi/efi-boot.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/efi/efi-boot.h +0 -2
index 92f4cfe8bd..b00441b1a2 100644
@@ -390,8 +390,6 @@ static void __init efi_arch_edd(void)
         {
             switch ( DevicePathType(devp.DevPath) )
             {
-                const u8 *p;
-
             case ACPI_DEVICE_PATH:
                 if ( state != root || boot_edd_info_nr > EDD_INFO_MAX )
                     break;

xen/arch/x86/efi/efi-boot.h static void __init efi_arch_edd(void) · line 465

Quoted 6 lines
xen/arch/x86/efi/efi-boot.h +2 -1
@@ -463,7 +461,8 @@ static void __init efi_arch_edd(void)
                 params->device_path_info_length =
                     sizeof(struct edd_device_params) -
                     offsetof(struct edd_device_params, key);
-                for ( p = (const u8 *)&params->key; p < &params->checksum; ++p )
+                for ( const u8 *p = (const u8 *)&params->key;
+                      p < &params->checksum; ++p )

In Xen we don't mix declaration and code. So the following is not something we use:

Quoted 1 line

for (int i = 0; i < 10; i++)

You're aware that we gained a couple of such uses already? I also think that when we discussed this we said this style could be at least okay-ish (until formalized in ./CODING_STYLE).

What I'm unhappy with here is the retaining of u8, when it could easily become uint8_t at this occasion.

Jan

#4 lore raw

Re: [XEN PATCH 01/11] x86/efi: move variable declaration to address MISRA C:2012 Rule 2.1

Quoted 41 lines · Jan Beulich
Quoted 38 lines · Stefano Stabellini
Quoted 26 lines · Nicola Vetrini

The variable declaration is moved where it's actually used, rather than being declared in the switch before any clause, thus being classified as unreachable code.

No functional changes.

---
 xen/arch/x86/efi/efi-boot.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
xen/arch/x86/efi/efi-boot.h +0 -2
index 92f4cfe8bd..b00441b1a2 100644
@@ -390,8 +390,6 @@ static void __init efi_arch_edd(void)
         {
             switch ( DevicePathType(devp.DevPath) )
             {
-                const u8 *p;
-
             case ACPI_DEVICE_PATH:
                 if ( state != root || boot_edd_info_nr > 
 EDD_INFO_MAX )
                     break;

xen/arch/x86/efi/efi-boot.h static void __init efi_arch_edd(void) · line 466

Quoted 7 lines
xen/arch/x86/efi/efi-boot.h +2 -1
@@ -463,7 +461,8 @@ static void __init efi_arch_edd(void)
                 params->device_path_info_length =
                     sizeof(struct edd_device_params) -
                     offsetof(struct edd_device_params, key);
-                for ( p = (const u8 *)&params->key; p < 
 &params->checksum; ++p )
+                for ( const u8 *p = (const u8 *)&params->key;
+                      p < &params->checksum; ++p )

In Xen we don't mix declaration and code. So the following is not something we use:

Quoted 1 line

for (int i = 0; i < 10; i++)

Quoted 8 lines

You're aware that we gained a couple of such uses already? I also think that when we discussed this we said this style could be at least okay-ish (until formalized in ./CODING_STYLE).

What I'm unhappy with here is the retaining of u8, when it could easily become uint8_t at this occasion.

Jan

Sure

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#5 lore raw

PATCH 2/11 x86: move declarations to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the declarations are moved in the smallest enclosing scope, near other variable definitions.

---
 xen/arch/x86/cpuid.c  |  3 +--
 xen/arch/x86/domain.c | 23 +++++++++++------------
 xen/arch/x86/irq.c    |  3 +--
 xen/arch/x86/msr.c    |  3 +--
 4 files changed, 14 insertions(+), 18 deletions(-)
xen/arch/x86/cpuid.c +1 -2
index 455a09b2dd..90e1370e90 100644
@@ -37,6 +37,7 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,
 {
     const struct domain *d = v->domain;
     const struct cpu_policy *p = d->arch.cpu_policy;
+    const struct cpu_user_regs *regs;
 
     *res = EMPTY_LEAF;
 
@@ -136,8 +137,6 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,
      */
     switch ( leaf )
     {
-        const struct cpu_user_regs *regs;
-
     case 0x1:
         /* TODO: Rework topology logic. */
         res->b &= 0x00ffffffu;
xen/arch/x86/domain.c +11 -12
index 5f66c2ae33..015f7b14ab 100644
@@ -2268,6 +2268,17 @@ int domain_relinquish_resources(struct domain *d)
 {
     int ret;
     struct vcpu *v;
+    enum {
+        PROG_iommu_pagetables = 1,
+        PROG_shared,
+        PROG_paging,
+        PROG_vcpu_pagetables,
+        PROG_xen,
+        PROG_l4,
+        PROG_l3,
+        PROG_l2,
+        PROG_done,
+    };
 
     BUG_ON(!cpumask_empty(d->dirty_cpumask));
 
@@ -2291,18 +2302,6 @@ int domain_relinquish_resources(struct domain *d)
 #define PROGRESS(x)                                                     \
         d->arch.rel_priv = PROG_ ## x; /* Fallthrough */ case PROG_ ## x
 
-        enum {
-            PROG_iommu_pagetables = 1,
-            PROG_shared,
-            PROG_paging,
-            PROG_vcpu_pagetables,
-            PROG_xen,
-            PROG_l4,
-            PROG_l3,
-            PROG_l2,
-            PROG_done,
-        };
-
     case 0:
         ret = pci_release_devices(d);
         if ( ret )
xen/arch/x86/irq.c +1 -2
index 6abfd81621..4fd0cc163d 100644
@@ -1135,6 +1135,7 @@ static void cf_check irq_guest_eoi_timer_fn(void *data)
     struct irq_desc *desc = data;
     unsigned int i, irq = desc - irq_desc;
     irq_guest_action_t *action;
+    cpumask_t *cpu_eoi_map;
 
     spin_lock_irq(&desc->lock);
     
@@ -1169,8 +1170,6 @@ static void cf_check irq_guest_eoi_timer_fn(void *data)
 
     switch ( action->ack_type )
     {
-        cpumask_t *cpu_eoi_map;
-
     case ACKTYPE_UNMASK:
         if ( desc->handler->end )
             desc->handler->end(desc, 0);
xen/arch/x86/msr.c +1 -2
index ecf126566d..0e61e0fe4e 100644
@@ -335,11 +335,10 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
     const struct cpu_policy *cp = d->arch.cpu_policy;
     struct vcpu_msrs *msrs = v->arch.msrs;
     int ret = X86EMUL_OKAY;
+    uint64_t rsvd;
 
     switch ( msr )
     {
-        uint64_t rsvd;
-
         /* Read-only */
     case MSR_IA32_PLATFORM_ID:
     case MSR_CORE_CAPABILITIES:
Signature · 1 line
2.34.1
#6 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

Quoted 31 lines · Nicola Vetrini
xen/arch/x86/domain.c +11 -5
index 5f66c2ae33..015f7b14ab 100644
@@ -2268,6 +2268,17 @@ int domain_relinquish_resources(struct domain *d)
 {
     int ret;
     struct vcpu *v;
+    enum {
+        PROG_iommu_pagetables = 1,
+        PROG_shared,
+        PROG_paging,
+        PROG_vcpu_pagetables,
+        PROG_xen,
+        PROG_l4,
+        PROG_l3,
+        PROG_l2,
+        PROG_done,
+    };
 
     BUG_ON(!cpumask_empty(d->dirty_cpumask));
 
@@ -2291,18 +2302,6 @@ int domain_relinquish_resources(struct domain *d)
 #define PROGRESS(x)                                                     \
         d->arch.rel_priv = PROG_ ## x; /* Fallthrough */ case PROG_ ## x
 
-        enum {
-            PROG_iommu_pagetables = 1,
-            PROG_shared,
-            PROG_paging,
-            PROG_vcpu_pagetables,

xen/arch/x86/domain.c int domain_relinquish_resources(struct domain *d) · line 2307

Quoted 10 lines
xen/arch/x86/domain.c +0 -7
-            PROG_xen,
-            PROG_l4,
-            PROG_l3,
-            PROG_l2,
-            PROG_done,
-        };
-
     case 0:
         ret = pci_release_devices(d);
         if ( ret )

Why does this get moved?  There's no code (reachable or unreachable) in there.

This is very subtle logic to start with, and you're moving one part of it away from the comment explaining how the magic works.

~Andrew

#7 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini
Quoted 90 lines · Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the declarations are moved in the smallest enclosing scope, near other variable definitions.

---
 xen/arch/x86/cpuid.c  |  3 +--
 xen/arch/x86/domain.c | 23 +++++++++++------------
 xen/arch/x86/irq.c    |  3 +--
 xen/arch/x86/msr.c    |  3 +--
 4 files changed, 14 insertions(+), 18 deletions(-)
xen/arch/x86/cpuid.c +1 -2
index 455a09b2dd..90e1370e90 100644
@@ -37,6 +37,7 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,
 {
     const struct domain *d = v->domain;
     const struct cpu_policy *p = d->arch.cpu_policy;
+    const struct cpu_user_regs *regs;
 
     *res = EMPTY_LEAF;
 
@@ -136,8 +137,6 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,
      */
     switch ( leaf )
     {
-        const struct cpu_user_regs *regs;
-
     case 0x1:
         /* TODO: Rework topology logic. */
         res->b &= 0x00ffffffu;
xen/arch/x86/domain.c +11 -12
index 5f66c2ae33..015f7b14ab 100644
@@ -2268,6 +2268,17 @@ int domain_relinquish_resources(struct domain *d)
 {
     int ret;
     struct vcpu *v;
+    enum {
+        PROG_iommu_pagetables = 1,
+        PROG_shared,
+        PROG_paging,
+        PROG_vcpu_pagetables,
+        PROG_xen,
+        PROG_l4,
+        PROG_l3,
+        PROG_l2,
+        PROG_done,
+    };
 
     BUG_ON(!cpumask_empty(d->dirty_cpumask));
 
@@ -2291,18 +2302,6 @@ int domain_relinquish_resources(struct domain *d)
 #define PROGRESS(x)                                                     \
         d->arch.rel_priv = PROG_ ## x; /* Fallthrough */ case PROG_ ## x
 
-        enum {
-            PROG_iommu_pagetables = 1,
-            PROG_shared,
-            PROG_paging,
-            PROG_vcpu_pagetables,
-            PROG_xen,
-            PROG_l4,
-            PROG_l3,
-            PROG_l2,
-            PROG_done,
-        };
-
     case 0:
         ret = pci_release_devices(d);
         if ( ret )
xen/arch/x86/irq.c +1 -0
index 6abfd81621..4fd0cc163d 100644
@@ -1135,6 +1135,7 @@ static void cf_check irq_guest_eoi_timer_fn(void *data)
     struct irq_desc *desc = data;
     unsigned int i, irq = desc - irq_desc;
     irq_guest_action_t *action;
+    cpumask_t *cpu_eoi_map;
 
     spin_lock_irq(&desc->lock);
 

xen/arch/x86/irq.c static void cf_check irq_guest_eoi_timer_fn(void *data) · line 1172

Quoted 4 lines
xen/arch/x86/irq.c +0 -1
@@ -1169,8 +1170,6 @@ static void cf_check irq_guest_eoi_timer_fn(void *data)
 
     switch ( action->ack_type )
     {
-        cpumask_t *cpu_eoi_map;

It is only used by case ACKTYPE_EOI so it can be moved there (with a new block):

    case ACKTYPE_EOI:
    {
        cpumask_t *cpu_eoi_map = this_cpu(scratch_cpumask);
        cpumask_copy(cpu_eoi_map, action->cpu_eoi_map);
        spin_unlock_irq(&desc->lock);
        on_selected_cpus(cpu_eoi_map, set_eoi_ready, desc, 0);
        return;
    }
    }
Quoted 8 lines
xen/arch/x86/irq.c +0 -0
     case ACKTYPE_UNMASK:
         if ( desc->handler->end )
             desc->handler->end(desc, 0);
xen/arch/x86/msr.c +0 -0
index ecf126566d..0e61e0fe4e 100644

xen/arch/x86/msr.c int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) · line 342

Quoted 9 lines
xen/arch/x86/msr.c +1 -2
@@ -335,11 +335,10 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val)
     const struct cpu_policy *cp = d->arch.cpu_policy;
     struct vcpu_msrs *msrs = v->arch.msrs;
     int ret = X86EMUL_OKAY;
+    uint64_t rsvd;
 
     switch ( msr )
     {
-        uint64_t rsvd;
-

It is only used by case MSR_INTEL_MISC_FEATURES_ENABLES so it can be moved there

Quoted 6 lines
xen/arch/x86/msr.c +0 -0
         /* Read-only */
     case MSR_IA32_PLATFORM_ID:
     case MSR_CORE_CAPABILITIES:
Signature · 1 line
2.34.1
#8 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

Quoted 11 lines · Stefano Stabellini

xen/arch/x86/irq.c static void cf_check irq_guest_eoi_timer_fn(void *data) · line 1172

Quoted 5 lines · Nicola Vetrini
xen/arch/x86/irq.c +0 -1
@@ -1169,8 +1170,6 @@ static void cf_check irq_guest_eoi_timer_fn(void *data)
 
     switch ( action->ack_type )
     {
-        cpumask_t *cpu_eoi_map;

It is only used by case ACKTYPE_EOI so it can be moved there (with a new block):

Quoted 9 lines
    case ACKTYPE_EOI:
    {
        cpumask_t *cpu_eoi_map = this_cpu(scratch_cpumask);
        cpumask_copy(cpu_eoi_map, action->cpu_eoi_map);
        spin_unlock_irq(&desc->lock);
        on_selected_cpus(cpu_eoi_map, set_eoi_ready, desc, 0);
        return;
    }
    }

This pattern (two closing braces at the same level) is why switch scope variable declarations were introduced (at least as far as introductions by me go). If switch scope variables aren't okay (which I continue to consider questionable), then this stylistic aspect needs sorting first (if everyone else thinks the above style is okay - with the missing blank line inserted -, then so be it).

Jan

#9 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

Quoted 23 lines · Jan Beulich
Quoted 12 lines · Stefano Stabellini

xen/arch/x86/irq.c static void cf_check · line 1173

Quoted 6 lines · Nicola Vetrini
xen/arch/x86/irq.c +0 -1
@@ -1169,8 +1170,6 @@ static void cf_check 
 irq_guest_eoi_timer_fn(void *data)
 
     switch ( action->ack_type )
     {
-        cpumask_t *cpu_eoi_map;

It is only used by case ACKTYPE_EOI so it can be moved there (with a new block):

Quoted 9 lines
    case ACKTYPE_EOI:
    {
        cpumask_t *cpu_eoi_map = this_cpu(scratch_cpumask);
        cpumask_copy(cpu_eoi_map, action->cpu_eoi_map);
        spin_unlock_irq(&desc->lock);
        on_selected_cpus(cpu_eoi_map, set_eoi_ready, desc, 0);
        return;
    }
    }
Quoted 8 lines

This pattern (two closing braces at the same level) is why switch scope variable declarations were introduced (at least as far as introductions by me go). If switch scope variables aren't okay (which I continue to consider questionable), then this stylistic aspect needs sorting first (if everyone else thinks the above style is okay - with the missing blank line inserted -, then so be it).

Jan

Actually, they can be deviated because they don't result in wrong code being generated. This, modulo the review comments received, is what most of the code would look like if they weren't, with the biggest pain point being that in many cases the choice is either the pattern with blocks for certain clauses or moving them in the enclosing scope, which may be several hundred lines above. If there's agreement on deviating them, I can drop the patches dealing with switches and do a v2 with just the other modifications.

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#10 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

Quoted 33 lines · Nicola Vetrini
Quoted 23 lines · Jan Beulich
Quoted 12 lines · Stefano Stabellini

xen/arch/x86/irq.c static void cf_check irq_guest_eoi_timer_fn(void · line 1173

Quoted 6 lines · Nicola Vetrini
xen/arch/x86/irq.c +0 -1
@@ -1169,8 +1170,6 @@ static void cf_check irq_guest_eoi_timer_fn(void
 *data)
 
     switch ( action->ack_type )
     {
-        cpumask_t *cpu_eoi_map;

It is only used by case ACKTYPE_EOI so it can be moved there (with a new block):

Quoted 9 lines
    case ACKTYPE_EOI:
    {
        cpumask_t *cpu_eoi_map = this_cpu(scratch_cpumask);
        cpumask_copy(cpu_eoi_map, action->cpu_eoi_map);
        spin_unlock_irq(&desc->lock);
        on_selected_cpus(cpu_eoi_map, set_eoi_ready, desc, 0);
        return;
    }
    }
Quoted 8 lines

This pattern (two closing braces at the same level) is why switch scope variable declarations were introduced (at least as far as introductions by me go). If switch scope variables aren't okay (which I continue to consider questionable), then this stylistic aspect needs sorting first (if everyone else thinks the above style is okay - with the missing blank line inserted -, then so be it).

Jan

Quoted 11 lines

Actually, they can be deviated because they don't result in wrong code being generated.
This, modulo the review comments received, is what most of the code would look like if
they weren't, with the biggest pain point being that in many cases the choice is either
the pattern with blocks for certain clauses or moving them in the enclosing scope, which may
be several hundred lines above. If there's agreement on deviating them, I can drop the patches
dealing with switches and do a v2 with just the other modifications.

I think we should deviate them. Good idea to remove them in v2.

#11 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

Quoted 12 lines · Stefano Stabellini
Quoted 11 lines

Actually, they can be deviated because they don't result in wrong code being generated.
This, modulo the review comments received, is what most of the code would look like if
they weren't, with the biggest pain point being that in many cases the choice is either
the pattern with blocks for certain clauses or moving them in the enclosing scope, which may
be several hundred lines above. If there's agreement on deviating them, I can drop the patches
dealing with switches and do a v2 with just the other modifications.

Quoted 1 line

I think we should deviate them. Good idea to remove them in v2.

We should add a note about this to docs/misra/rules.rst as well?

docs/misra/rules.rst +2 -1
index 8f0e4d3f25..e713b0ea99 100644
@@ -106,7 +106,8 @@ maintainers if you want to suggest a change.
    * - `Rule 2.1 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_02_01_1.c>`_
      - Required
      - A project shall not contain unreachable code
-     -
+     - It is acceptable to declare local variables under a switch
+       statement block
 
    * - `Rule 2.6 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_02_06.c>`_
      - Advisory
#12 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini
Quoted 32 lines · Nicola Vetrini
Quoted 24 lines · Jan Beulich
Quoted 13 lines · Stefano Stabellini

xen/arch/x86/irq.c static void cf_check · line 1173

Quoted 6 lines · Nicola Vetrini
xen/arch/x86/irq.c +0 -1
@@ -1169,8 +1170,6 @@ static void cf_check 
 irq_guest_eoi_timer_fn(void *data)
 
     switch ( action->ack_type )
     {
-        cpumask_t *cpu_eoi_map;

It is only used by case ACKTYPE_EOI so it can be moved there (with a new
block):

Quoted 9 lines
    case ACKTYPE_EOI:
    {
        cpumask_t *cpu_eoi_map = this_cpu(scratch_cpumask);
        cpumask_copy(cpu_eoi_map, action->cpu_eoi_map);
        spin_unlock_irq(&desc->lock);
        on_selected_cpus(cpu_eoi_map, set_eoi_ready, desc, 0);
        return;
    }
    }
Quoted 6 lines

This pattern (two closing braces at the same level) is why switch scope variable declarations were introduced (at least as far as introductions by me go). If switch scope variables aren't okay (which I continue to consider questionable), then this stylistic aspect needs sorting first (if everyone else thinks the above style is okay - with the missing blank line inserted -, then so be it).

Quoted 2 lines

Actually, they can be deviated because they don't result in wrong code being generated.

Only later I recalled Andrew's intention to possibly make use of -ftrivial-auto-var-init. While, unlike I think he said, such declared variables aren't getting in the way of this (neither gcc nor clang warn about them), they also don't benefit from it (i.e. they'll be left uninitialized despite the command line option saying otherwise). IOW I think further consideration is going to be needed here.

Jan

#13 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

Quoted 12 lines
Quoted 11 lines
Quoted 8 lines

This pattern (two closing braces at the same level) is why switch scope variable declarations were introduced (at least as far as introductions by me go). If switch scope variables aren't okay (which I continue to consider questionable), then this stylistic aspect needs sorting first (if everyone else thinks the above style is okay - with the missing blank line inserted -, then so be it).

Actually, they can be deviated because they don't result in wrong code being generated.

Quoted 8 lines

Only later I recalled Andrew's intention to possibly make use of -ftrivial-auto-var-init. While, unlike I think he said, such declared variables aren't getting in the way of this (neither gcc nor clang warn about them), they also don't benefit from it (i.e. they'll be left uninitialized despite the command line option saying otherwise). IOW I think further consideration is going to be needed here.

Jan

Well, in that case I'm happy if I can contribute in some way to the discussion, but perhaps this is best sorted out in a separate discussion, so that later I can send a v2 reflecting the decision(s).

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#14 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

In reply to Jan Beulich
Quoted 36 lines · Jan Beulich
Quoted 32 lines · Nicola Vetrini
Quoted 24 lines · Jan Beulich
Quoted 13 lines · Stefano Stabellini

xen/arch/x86/irq.c static void cf_check · line 1173

Quoted 6 lines · Nicola Vetrini
xen/arch/x86/irq.c +0 -1
@@ -1169,8 +1170,6 @@ static void cf_check 
 irq_guest_eoi_timer_fn(void *data)
 
     switch ( action->ack_type )
     {
-        cpumask_t *cpu_eoi_map;

It is only used by case ACKTYPE_EOI so it can be moved there (with a new
block):

Quoted 9 lines
    case ACKTYPE_EOI:
    {
        cpumask_t *cpu_eoi_map = this_cpu(scratch_cpumask);
        cpumask_copy(cpu_eoi_map, action->cpu_eoi_map);
        spin_unlock_irq(&desc->lock);
        on_selected_cpus(cpu_eoi_map, set_eoi_ready, desc, 0);
        return;
    }
    }
Quoted 6 lines

This pattern (two closing braces at the same level) is why switch scope variable declarations were introduced (at least as far as introductions by me go). If switch scope variables aren't okay (which I continue to consider questionable), then this stylistic aspect needs sorting first (if everyone else thinks the above style is okay - with the missing blank line inserted -, then so be it).

Quoted 2 lines

Actually, they can be deviated because they don't result in wrong code being generated.

Quoted 6 lines

Only later I recalled Andrew's intention to possibly make use of -ftrivial-auto-var-init. While, unlike I think he said, such declared variables aren't getting in the way of this (neither gcc nor clang warn about them), they also don't benefit from it (i.e. they'll be left uninitialized despite the command line option saying otherwise). IOW I think further consideration is going to be needed here.

Let me get this right. Are you saying that if we enable -ftrivial-auto-var-init, due to a compiler limitation, variables declared as follow:

  switch(var) {
      int a;
      char b;
      case ...

do not benefit from -ftrivial-auto-var-init ?

So if we moved the variable declarations elsewhere, in one of the two following ways:

1)
int a;
int b;

  switch(var) {

2)
switch(var) {

  case 1:
  {
      int a;
      int b;

Then -ftrivial-auto-var-init would take effect? If so, I think it is worth a discussion on what to do there.

But either way right now I think it is fine to take the switch-related patches out of this series.

#15 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

Quoted 48 lines · Stefano Stabellini
Quoted 36 lines · Jan Beulich
Quoted 32 lines · Nicola Vetrini
Quoted 24 lines · Jan Beulich
Quoted 13 lines · Stefano Stabellini

xen/arch/x86/irq.c static void cf_check · line 1173

Quoted 6 lines · Nicola Vetrini
xen/arch/x86/irq.c +0 -1
@@ -1169,8 +1170,6 @@ static void cf_check 
 irq_guest_eoi_timer_fn(void *data)
 
     switch ( action->ack_type )
     {
-        cpumask_t *cpu_eoi_map;

It is only used by case ACKTYPE_EOI so it can be moved there (with a new
block):

Quoted 9 lines
    case ACKTYPE_EOI:
    {
        cpumask_t *cpu_eoi_map = this_cpu(scratch_cpumask);
        cpumask_copy(cpu_eoi_map, action->cpu_eoi_map);
        spin_unlock_irq(&desc->lock);
        on_selected_cpus(cpu_eoi_map, set_eoi_ready, desc, 0);
        return;
    }
    }
Quoted 6 lines

This pattern (two closing braces at the same level) is why switch scope variable declarations were introduced (at least as far as introductions by me go). If switch scope variables aren't okay (which I continue to consider questionable), then this stylistic aspect needs sorting first (if everyone else thinks the above style is okay - with the missing blank line inserted -, then so be it).

Quoted 2 lines

Actually, they can be deviated because they don't result in wrong code being generated.

Quoted 6 lines

Only later I recalled Andrew's intention to possibly make use of -ftrivial-auto-var-init. While, unlike I think he said, such declared variables aren't getting in the way of this (neither gcc nor clang warn about them), they also don't benefit from it (i.e. they'll be left uninitialized despite the command line option saying otherwise). IOW I think further consideration is going to be needed here.

Let me get this right. Are you saying that if we enable -ftrivial-auto-var-init, due to a compiler limitation, variables declared as follow:

Quoted 7 lines
  switch(var) {
      int a;
      char b;
      case ...

do not benefit from -ftrivial-auto-var-init ?

Yes, that's my observation with both compilers.

Jan

#16 lore raw

Re: [XEN PATCH 02/11] x86: move declarations to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini
Quoted 6 lines · Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the declarations are moved in the smallest enclosing scope, near other variable definitions.

This needs further clarification: There's no "code" involved here in the sense that a compiler might eliminate it. Yet that's what the rule's rationale is focusing on. There's no even mention of declarations like the ones you move. In particular (and on top of my comment on patch 1) ...

Quoted 11 lines
xen/arch/x86/cpuid.c +1 -0
@@ -37,6 +37,7 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,
 {
     const struct domain *d = v->domain;
     const struct cpu_policy *p = d->arch.cpu_policy;
+    const struct cpu_user_regs *regs;
 
     *res = EMPTY_LEAF;
 

xen/arch/x86/cpuid.c void guest_cpuid(const struct vcpu *v, uint32_t leaf, · line 142

Quoted 8 lines
xen/arch/x86/cpuid.c +0 -2
@@ -136,8 +137,6 @@ void guest_cpuid(const struct vcpu *v, uint32_t leaf,
      */
     switch ( leaf )
     {
-        const struct cpu_user_regs *regs;
-
     case 0x1:
         /* TODO: Rework topology logic. */
         res->b &= 0x00ffffffu;

... I'm not happy to see scopes of variables widened.

Jan

#17 lore raw

PATCH 3/11 x86/uaccess: move declarations to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

The variable declarations inside macros {put,get}_unsafe_size are considered unreachable code, as they appear in a switch statement, before any clause. They are moved in the enclosing scope to resolve the violation.

No functional changes.

---
 xen/arch/x86/include/asm/uaccess.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/arch/x86/include/asm/uaccess.h +4 -2
index 684fccd95c..a2aaab1e87 100644
@@ -191,11 +191,12 @@ struct __large_struct { unsigned long buf[100]; };
 
 #define put_unsafe_size(x, ptr, size, grd, retval, errret)                 \
 do {                                                                       \
+    long dummy_;                                                           \
+                                                                           \
     retval = 0;                                                            \
     stac();                                                                \
     switch ( size )                                                        \
     {                                                                      \
-    long dummy_;                                                           \
     case 1:                                                                \
         put_unsafe_asm(x, ptr, grd, retval, "b", "b", "iq", errret);       \
         break;                                                             \
@@ -218,11 +219,12 @@ do {                                                                       \
 
 #define get_unsafe_size(x, ptr, size, grd, retval, errret)                 \
 do {                                                                       \
+    long dummy_;                                                           \
+                                                                           \
     retval = 0;                                                            \
     stac();                                                                \
     switch ( size )                                                        \
     {                                                                      \
-    long dummy_;                                                           \
     case 1: get_unsafe_asm(x, ptr, grd, retval, "b", "=q", errret); break; \
     case 2: get_unsafe_asm(x, ptr, grd, retval, "w", "=r", errret); break; \
     case 4: get_unsafe_asm(x, ptr, grd, retval, "k", "=r", errret); break; \
Signature · 1 line
2.34.1
Reviewed-by on v1 3/11
#18 lore raw

Re: [XEN PATCH 03/11] x86/uaccess: move declarations to address MISRA C:2012 Rule 2.1

Quoted 7 lines · Nicola Vetrini

The variable declarations inside macros {put,get}_unsafe_size are considered unreachable code, as they appear in a switch statement, before any clause. They are moved in the enclosing scope to resolve the violation.

No functional changes.

Quoted 39 lines
---
 xen/arch/x86/include/asm/uaccess.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
xen/arch/x86/include/asm/uaccess.h +4 -2
index 684fccd95c..a2aaab1e87 100644
@@ -191,11 +191,12 @@ struct __large_struct { unsigned long buf[100]; };
 
 #define put_unsafe_size(x, ptr, size, grd, retval, errret)                 \
 do {                                                                       \
+    long dummy_;                                                           \
+                                                                           \
     retval = 0;                                                            \
     stac();                                                                \
     switch ( size )                                                        \
     {                                                                      \
-    long dummy_;                                                           \
     case 1:                                                                \
         put_unsafe_asm(x, ptr, grd, retval, "b", "b", "iq", errret);       \
         break;                                                             \
@@ -218,11 +219,12 @@ do {                                                                       \
 
 #define get_unsafe_size(x, ptr, size, grd, retval, errret)                 \
 do {                                                                       \
+    long dummy_;                                                           \
+                                                                           \
     retval = 0;                                                            \
     stac();                                                                \
     switch ( size )                                                        \
     {                                                                      \
-    long dummy_;                                                           \
     case 1: get_unsafe_asm(x, ptr, grd, retval, "b", "=q", errret); break; \
     case 2: get_unsafe_asm(x, ptr, grd, retval, "w", "=r", errret); break; \
     case 4: get_unsafe_asm(x, ptr, grd, retval, "k", "=r", errret); break; \
Signature · 1 line
2.34.1
#19 lore raw

PATCH 4/11 x86emul: move variable definitions to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the variable declarations are moved in the only clause scope that uses it.

---
 xen/arch/x86/hvm/emulate.c             |  9 ++++-----
 xen/arch/x86/hvm/hvm.c                 | 10 ++++------
 xen/arch/x86/x86_emulate/0f01.c        |  7 +++----
 xen/arch/x86/x86_emulate/blk.c         | 17 ++++++++---------
 xen/arch/x86/x86_emulate/decode.c      |  3 ++-
 xen/arch/x86/x86_emulate/fpu.c         |  3 +--
 xen/arch/x86/x86_emulate/util-xen.c    |  4 ++--
 xen/arch/x86/x86_emulate/x86_emulate.c | 14 +++++++-------
 8 files changed, 31 insertions(+), 36 deletions(-)
xen/arch/x86/hvm/emulate.c +4 -5
index 75ee98a73b..2261e8655b 100644
@@ -1994,6 +1994,7 @@ static int cf_check hvmemul_rep_stos(
     bool_t df = !!(ctxt->regs->eflags & X86_EFLAGS_DF);
     int rc = hvmemul_virtual_to_linear(seg, offset, bytes_per_rep, reps,
                                        hvm_access_write, hvmemul_ctxt, &addr);
+    char *buf;
 
     if ( rc != X86EMUL_OKAY )
         return rc;
@@ -2025,7 +2026,6 @@ static int cf_check hvmemul_rep_stos(
     switch ( p2mt )
     {
         unsigned long bytes;
-        char *buf;
 
     default:
         /* Allocate temporary buffer. */
@@ -2289,16 +2289,15 @@ static int cf_check hvmemul_cache_op(
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     uint32_t pfec = PFEC_page_present;
+    unsigned long addr;
+    int rc;
+    void *mapping;
 
     if ( !cache_flush_permitted(current->domain) )
         return X86EMUL_OKAY;
 
     switch ( op )
     {
-        unsigned long addr;
-        int rc;
-        void *mapping;
-
     case x86emul_clflush:
     case x86emul_clflushopt:
     case x86emul_clwb:
xen/arch/x86/hvm/hvm.c +4 -6
index 2180abeb33..4170343b34 100644
@@ -1494,10 +1494,10 @@ static int cf_check hvm_load_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
 
     for ( i = 0; i < ctxt->count; ++i )
     {
+        int rc;
+
         switch ( ctxt->msr[i].index )
         {
-            int rc;
-
         case MSR_SPEC_CTRL:
         case MSR_INTEL_MISC_FEATURES_ENABLES:
         case MSR_PKRS:
@@ -3522,6 +3522,7 @@ int hvm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
     struct domain *d = v->domain;
     uint64_t *var_range_base, *fixed_range_base;
     int ret;
+    unsigned int index;
 
     var_range_base = (uint64_t *)v->arch.hvm.mtrr.var_ranges;
     fixed_range_base = (uint64_t *)v->arch.hvm.mtrr.fixed_ranges;
@@ -3533,8 +3534,6 @@ int hvm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
 
     switch ( msr )
     {
-        unsigned int index;
-
     case MSR_EFER:
         *msr_content = v->arch.hvm.guest_efer;
         break;
@@ -3636,6 +3635,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,
     struct vcpu *v = current;
     struct domain *d = v->domain;
     int ret;
+    unsigned int index;
 
     HVMTRACE_3D(MSR_WRITE, msr,
                (uint32_t)msr_content, (uint32_t)(msr_content >> 32));
@@ -3668,8 +3668,6 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,
 
     switch ( msr )
     {
-        unsigned int index;
-
     case MSR_EFER:
         if ( hvm_set_efer(msr_content) )
            return X86EMUL_EXCEPTION;
xen/arch/x86/x86_emulate/0f01.c +3 -4
index ba43fc394b..22a14b12c3 100644
@@ -24,13 +24,12 @@ int x86emul_0f01(struct x86_emulate_state *s,
 {
     enum x86_segment seg = (s->modrm_reg & 1) ? x86_seg_idtr : x86_seg_gdtr;
     int rc;
+    unsigned long base, limit, cr0, cr0w, cr4;
+    struct segment_register sreg;
+    uint64_t msr_val;
 
     switch ( s->modrm )
     {
-        unsigned long base, limit, cr0, cr0w, cr4;
-        struct segment_register sreg;
-        uint64_t msr_val;
-
     case 0xc6:
         switch ( s->vex.pfx )
         {
xen/arch/x86/x86_emulate/blk.c +8 -9
index e790f4f900..f2956c0d52 100644
@@ -26,19 +26,18 @@ int x86_emul_blk(
     struct x86_emulate_ctxt *ctxt)
 {
     int rc = X86EMUL_OKAY;
-
-    switch ( s->blk )
-    {
-        bool zf;
+    bool zf;
 #ifndef X86EMUL_NO_FPU
+    struct {
+        struct x87_env32 env;
         struct {
-            struct x87_env32 env;
-            struct {
-               uint8_t bytes[10];
-            } freg[8];
-        } fpstate;
+           uint8_t bytes[10];
+        } freg[8];
+    } fpstate;
 #endif
 
+    switch ( s->blk )
+    {
         /*
          * Throughout this switch(), memory clobbers are used to compensate
          * that other operands may not properly express the (full) memory
xen/arch/x86/x86_emulate/decode.c +2 -1
index f58ca3984e..daebf3a9bd 100644
@@ -1758,9 +1758,9 @@ int x86emul_decode(struct x86_emulate_state *s,
     /* Fetch the immediate operand, if present. */
     switch ( d & SrcMask )
     {
+    case SrcImm: {
         unsigned int bytes;
 
-    case SrcImm:
         if ( !(d & ByteOp) )
         {
             if ( mode_64bit() && !amd_like(ctxt) &&
@@ -1785,6 +1785,7 @@ int x86emul_decode(struct x86_emulate_state *s,
     case SrcImm16:
         s->imm1 = insn_fetch_type(uint16_t);
         break;
+		}
     }
 
     ctxt->opcode = opcode;
xen/arch/x86/x86_emulate/fpu.c +1 -2
index 480d879657..002d5e1253 100644
@@ -84,11 +84,10 @@ int x86emul_fpu(struct x86_emulate_state *s,
     uint8_t b;
     int rc;
     struct x86_emulate_stub stub = {};
+    unsigned long dummy;
 
     switch ( b = ctxt->opcode )
     {
-        unsigned long dummy;
-
     case 0x9b:  /* wait/fwait */
         host_and_vcpu_must_have(fpu);
         get_fpu(X86EMUL_FPU_wait);
xen/arch/x86/x86_emulate/util-xen.c +2 -2
index 5e90818010..7ab2ac712f 100644
@@ -77,10 +77,10 @@ bool cf_check x86_insn_is_portio(const struct x86_emulate_state *s,
 bool cf_check x86_insn_is_cr_access(const struct x86_emulate_state *s,
                                     const struct x86_emulate_ctxt *ctxt)
 {
+    unsigned int ext;
+
     switch ( ctxt->opcode )
     {
-        unsigned int ext;
-
     case X86EMUL_OPC(0x0f, 0x01):
         if ( x86_insn_modrm(s, NULL, &ext) >= 0
              && (ext & 5) == 4 ) /* SMSW / LMSW */
xen/arch/x86/x86_emulate/x86_emulate.c +7 -7
index e88245eae9..d6c04fd5f3 100644
@@ -1479,15 +1479,15 @@ x86_emulate(
         break;
     }
 
+    enum x86_segment seg;
+    struct segment_register cs, sreg;
+    struct cpuid_leaf leaf;
+    uint64_t msr_val;
+    unsigned int i, n;
+    unsigned long dummy;
+
     switch ( ctxt->opcode )
     {
-        enum x86_segment seg;
-        struct segment_register cs, sreg;
-        struct cpuid_leaf leaf;
-        uint64_t msr_val;
-        unsigned int i, n;
-        unsigned long dummy;
-
     case 0x00: case 0x01: add: /* add reg,mem */
         if ( ops->rmw && dst.type == OP_MEM )
             state->rmw = rmw_add;
Signature · 1 line
2.34.1
#20 lore raw

Re: [XEN PATCH 04/11] x86emul: move variable definitions to address MISRA C:2012 Rule 2.1

Quoted 32 lines · Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the variable declarations are moved in the only clause scope that uses it.

---
 xen/arch/x86/hvm/emulate.c             |  9 ++++-----
 xen/arch/x86/hvm/hvm.c                 | 10 ++++------
 xen/arch/x86/x86_emulate/0f01.c        |  7 +++----
 xen/arch/x86/x86_emulate/blk.c         | 17 ++++++++---------
 xen/arch/x86/x86_emulate/decode.c      |  3 ++-
 xen/arch/x86/x86_emulate/fpu.c         |  3 +--
 xen/arch/x86/x86_emulate/util-xen.c    |  4 ++--
 xen/arch/x86/x86_emulate/x86_emulate.c | 14 +++++++-------
 8 files changed, 31 insertions(+), 36 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -0
index 75ee98a73b..2261e8655b 100644
@@ -1994,6 +1994,7 @@ static int cf_check hvmemul_rep_stos(
     bool_t df = !!(ctxt->regs->eflags & X86_EFLAGS_DF);
     int rc = hvmemul_virtual_to_linear(seg, offset, bytes_per_rep, reps,
                                        hvm_access_write, hvmemul_ctxt, &addr);
+    char *buf;
 
     if ( rc != X86EMUL_OKAY )
         return rc;

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_rep_stos( · line 2028

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +0 -1
@@ -2025,7 +2026,6 @@ static int cf_check hvmemul_rep_stos(
     switch ( p2mt )
     {
         unsigned long bytes;
-        char *buf;

why can "bytes" where it is?
Bith buf and bytes could be declared under "default" with a new block

Quoted 8 lines
xen/arch/x86/hvm/emulate.c +2 -0
     default:
         /* Allocate temporary buffer. */
@@ -2289,16 +2289,15 @@ static int cf_check hvmemul_cache_op(
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     uint32_t pfec = PFEC_page_present;
+    unsigned long addr;
+    int rc;

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_cache_op( · line 2300

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +1 -3
+    void *mapping;
 
     if ( !cache_flush_permitted(current->domain) )
         return X86EMUL_OKAY;
 
     switch ( op )
     {
-        unsigned long addr;
-        int rc;
-        void *mapping;

These three could be...

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_cache_op( · line 2303

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +0 -0
     case x86emul_clflush:
     case x86emul_clflushopt:
     case x86emul_clwb:

... here in a new block

Quoted 56 lines
xen/arch/x86/hvm/hvm.c +4 -6
index 2180abeb33..4170343b34 100644
@@ -1494,10 +1494,10 @@ static int cf_check hvm_load_cpu_msrs(struct domain *d, hvm_domain_context_t *h)
 
     for ( i = 0; i < ctxt->count; ++i )
     {
+        int rc;
+
         switch ( ctxt->msr[i].index )
         {
-            int rc;
-
         case MSR_SPEC_CTRL:
         case MSR_INTEL_MISC_FEATURES_ENABLES:
         case MSR_PKRS:
@@ -3522,6 +3522,7 @@ int hvm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
     struct domain *d = v->domain;
     uint64_t *var_range_base, *fixed_range_base;
     int ret;
+    unsigned int index;
 
     var_range_base = (uint64_t *)v->arch.hvm.mtrr.var_ranges;
     fixed_range_base = (uint64_t *)v->arch.hvm.mtrr.fixed_ranges;
@@ -3533,8 +3534,6 @@ int hvm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
 
     switch ( msr )
     {
-        unsigned int index;
-
     case MSR_EFER:
         *msr_content = v->arch.hvm.guest_efer;
         break;
@@ -3636,6 +3635,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,
     struct vcpu *v = current;
     struct domain *d = v->domain;
     int ret;
+    unsigned int index;
 
     HVMTRACE_3D(MSR_WRITE, msr,
                (uint32_t)msr_content, (uint32_t)(msr_content >> 32));
@@ -3668,8 +3668,6 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,
 
     switch ( msr )
     {
-        unsigned int index;
-
     case MSR_EFER:
         if ( hvm_set_efer(msr_content) )
            return X86EMUL_EXCEPTION;
xen/arch/x86/x86_emulate/0f01.c +0 -0
index ba43fc394b..22a14b12c3 100644

xen/arch/x86/x86_emulate/0f01.c int x86emul_0f01(struct x86_emulate_state *s, · line 29

Quoted 6 lines
xen/arch/x86/x86_emulate/0f01.c +3 -0
@@ -24,13 +24,12 @@ int x86emul_0f01(struct x86_emulate_state *s,
 {
     enum x86_segment seg = (s->modrm_reg & 1) ? x86_seg_idtr : x86_seg_gdtr;
     int rc;
+    unsigned long base, limit, cr0, cr0w, cr4;
+    struct segment_register sreg;
+    uint64_t msr_val;

base and limit can go under case 0xfc

cr0 and cr0w can go under case GRP7_ALL(6)

Quoted 46 lines
xen/arch/x86/x86_emulate/0f01.c +0 -4
     switch ( s->modrm )
     {
-        unsigned long base, limit, cr0, cr0w, cr4;
-        struct segment_register sreg;
-        uint64_t msr_val;
-
     case 0xc6:
         switch ( s->vex.pfx )
         {
xen/arch/x86/x86_emulate/blk.c +8 -9
index e790f4f900..f2956c0d52 100644
@@ -26,19 +26,18 @@ int x86_emul_blk(
     struct x86_emulate_ctxt *ctxt)
 {
     int rc = X86EMUL_OKAY;
-
-    switch ( s->blk )
-    {
-        bool zf;
+    bool zf;
 #ifndef X86EMUL_NO_FPU
+    struct {
+        struct x87_env32 env;
         struct {
-            struct x87_env32 env;
-            struct {
-               uint8_t bytes[10];
-            } freg[8];
-        } fpstate;
+           uint8_t bytes[10];
+        } freg[8];
+    } fpstate;
 #endif
 
+    switch ( s->blk )
+    {
         /*
          * Throughout this switch(), memory clobbers are used to compensate
          * that other operands may not properly express the (full) memory
xen/arch/x86/x86_emulate/decode.c +0 -0
index f58ca3984e..daebf3a9bd 100644

xen/arch/x86/x86_emulate/decode.c int x86emul_decode(struct x86_emulate_state *s, · line 1761

Quoted 4 lines
xen/arch/x86/x86_emulate/decode.c +1 -0
@@ -1758,9 +1758,9 @@ int x86emul_decode(struct x86_emulate_state *s,
     /* Fetch the immediate operand, if present. */
     switch ( d & SrcMask )
     {
+    case SrcImm: {

The Xen coding style is:

    case SrcImm:
    {

Also, this change looks wrong?

Quoted 36 lines
xen/arch/x86/x86_emulate/decode.c +1 -1
         unsigned int bytes;
 
-    case SrcImm:
         if ( !(d & ByteOp) )
         {
             if ( mode_64bit() && !amd_like(ctxt) &&
@@ -1785,6 +1785,7 @@ int x86emul_decode(struct x86_emulate_state *s,
     case SrcImm16:
         s->imm1 = insn_fetch_type(uint16_t);
         break;
+		}
     }
 
     ctxt->opcode = opcode;
xen/arch/x86/x86_emulate/fpu.c +1 -2
index 480d879657..002d5e1253 100644
@@ -84,11 +84,10 @@ int x86emul_fpu(struct x86_emulate_state *s,
     uint8_t b;
     int rc;
     struct x86_emulate_stub stub = {};
+    unsigned long dummy;
 
     switch ( b = ctxt->opcode )
     {
-        unsigned long dummy;
-
     case 0x9b:  /* wait/fwait */
         host_and_vcpu_must_have(fpu);
         get_fpu(X86EMUL_FPU_wait);
xen/arch/x86/x86_emulate/util-xen.c +0 -0
index 5e90818010..7ab2ac712f 100644

xen/arch/x86/x86_emulate/util-xen.c bool cf_check x86_insn_is_portio(const struct x86_emulate_state *s, · line 82

Quoted 8 lines
xen/arch/x86/x86_emulate/util-xen.c +2 -1
@@ -77,10 +77,10 @@ bool cf_check x86_insn_is_portio(const struct x86_emulate_state *s,
 bool cf_check x86_insn_is_cr_access(const struct x86_emulate_state *s,
                                     const struct x86_emulate_ctxt *ctxt)
 {
+    unsigned int ext;
+
     switch ( ctxt->opcode )
     {
-        unsigned int ext;

This can go under case X86EMUL_OPC with a new block

Quoted 16 lines
xen/arch/x86/x86_emulate/util-xen.c +0 -0
     case X86EMUL_OPC(0x0f, 0x01):
         if ( x86_insn_modrm(s, NULL, &ext) >= 0
              && (ext & 5) == 4 ) /* SMSW / LMSW */
xen/arch/x86/x86_emulate/x86_emulate.c +5 -0
index e88245eae9..d6c04fd5f3 100644
@@ -1479,15 +1479,15 @@ x86_emulate(
         break;
     }
 
+    enum x86_segment seg;
+    struct segment_register cs, sreg;
+    struct cpuid_leaf leaf;
+    uint64_t msr_val;
+    unsigned int i, n;

xen/arch/x86/x86_emulate/x86_emulate.c x86_emulate( · line 1489

Quoted 10 lines
xen/arch/x86/x86_emulate/x86_emulate.c +2 -6
+    unsigned long dummy;
+
     switch ( ctxt->opcode )
     {
-        enum x86_segment seg;
-        struct segment_register cs, sreg;
-        struct cpuid_leaf leaf;
-        uint64_t msr_val;
-        unsigned int i, n;
-        unsigned long dummy;

In Xen we don't mix declarations and code, so they would have to go to the top of the function

Quoted 6 lines
xen/arch/x86/x86_emulate/x86_emulate.c +0 -0
     case 0x00: case 0x01: add: /* add reg,mem */
         if ( ops->rmw && dst.type == OP_MEM )
             state->rmw = rmw_add;
Signature · 1 line
2.34.1
#21 lore raw

Re: [XEN PATCH 04/11] x86emul: move variable definitions to address MISRA C:2012 Rule 2.1

Quoted 21 lines · Stefano Stabellini
Quoted 6 lines · Nicola Vetrini
xen/arch/x86/hvm/emulate.c +2 -0
@@ -2289,16 +2289,15 @@ static int cf_check hvmemul_cache_op(
     struct hvm_emulate_ctxt *hvmemul_ctxt =
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     uint32_t pfec = PFEC_page_present;
+    unsigned long addr;
+    int rc;

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_cache_op( · line 2300

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +1 -3
+    void *mapping;
 
     if ( !cache_flush_permitted(current->domain) )
         return X86EMUL_OKAY;
 
     switch ( op )
     {
-        unsigned long addr;
-        int rc;
-        void *mapping;

These three could be...

Quoted 5 lines

xen/arch/x86/hvm/emulate.c static int cf_check hvmemul_cache_op( · line 2303

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +0 -0
     case x86emul_clflush:
     case x86emul_clflushopt:
     case x86emul_clwb:

... here in a new block

Except they're likely to be re-used as new enumerators are added.

Quoted 5 lines
Quoted 5 lines
xen/arch/x86/x86_emulate/util-xen.c +0 -0
@@ -77,10 +77,10 @@ bool cf_check x86_insn_is_portio(const struct x86_emulate_state *s,
 bool cf_check x86_insn_is_cr_access(const struct x86_emulate_state *s,
                                     const struct x86_emulate_ctxt *ctxt)
Quoted 8 lines

xen/arch/x86/x86_emulate/util-xen.c bool cf_check x86_insn_is_portio(const struct x86_emulate_state *s, · line 82

Quoted 6 lines
xen/arch/x86/x86_emulate/util-xen.c +2 -1
 {
+    unsigned int ext;
+
     switch ( ctxt->opcode )
     {
-        unsigned int ext;

This can go under case X86EMUL_OPC with a new block

Same here.

Jan

#22 lore raw

PATCH 5/11 drivers/pci: move variable definitions to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the variable declarations are moved in the smallest enclosing scope, near other variable definitions.

---
 xen/drivers/passthrough/pci.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
xen/drivers/passthrough/pci.c +4 -6
index 33452791a8..3f5fa5beef 100644
@@ -315,6 +315,7 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn)
     struct pci_dev *pdev;
     unsigned int pos;
     int rc;
+    unsigned int cap, sec_bus, sub_bus;
 
     list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list )
         if ( pdev->bus == bus && pdev->devfn == devfn )
@@ -343,8 +344,6 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn)
     /* update bus2bridge */
     switch ( pdev->type = pdev_type(pseg->nr, bus, devfn) )
     {
-        unsigned int cap, sec_bus, sub_bus;
-
         case DEV_TYPE_PCIe2PCI_BRIDGE:
         case DEV_TYPE_LEGACY_PCI_BRIDGE:
             sec_bus = pci_conf_read8(pdev->sbdf, PCI_SECONDARY_BUS);
@@ -424,11 +423,11 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn)
 
 static void free_pdev(struct pci_seg *pseg, struct pci_dev *pdev)
 {
+    unsigned int sec_bus, sub_bus;
+
     /* update bus2bridge */
     switch ( pdev->type )
     {
-        unsigned int sec_bus, sub_bus;
-
         case DEV_TYPE_PCIe2PCI_BRIDGE:
         case DEV_TYPE_LEGACY_PCI_BRIDGE:
             sec_bus = pci_conf_read8(pdev->sbdf, PCI_SECONDARY_BUS);
@@ -1555,11 +1554,10 @@ int iommu_do_pci_domctl(
     u8 bus, devfn;
     int ret = 0;
     uint32_t machine_sbdf;
+    unsigned int flags;
 
     switch ( domctl->cmd )
     {
-        unsigned int flags;
-
     case XEN_DOMCTL_get_device_group:
     {
         u32 max_sdevs;
Signature · 1 line
2.34.1
#23 lore raw

Re: [XEN PATCH 05/11] drivers/pci: move variable definitions to address MISRA C:2012 Rule 2.1

Quoted 25 lines · Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the variable declarations are moved in the smallest enclosing scope, near other variable definitions.

---
 xen/drivers/passthrough/pci.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
xen/drivers/passthrough/pci.c +1 -0
index 33452791a8..3f5fa5beef 100644
@@ -315,6 +315,7 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn)
     struct pci_dev *pdev;
     unsigned int pos;
     int rc;
+    unsigned int cap, sec_bus, sub_bus;
 
     list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list )
         if ( pdev->bus == bus && pdev->devfn == devfn )

xen/drivers/passthrough/pci.c static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn) · line 346

Quoted 4 lines
xen/drivers/passthrough/pci.c +0 -1
@@ -343,8 +344,6 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn)
     /* update bus2bridge */
     switch ( pdev->type = pdev_type(pseg->nr, bus, devfn) )
     {
-        unsigned int cap, sec_bus, sub_bus;

sub_bus and sec_bus could go under case DEV_TYPE_LEGACY_PCI_BRIDGE cap could go under case DEV_TYPE_PCIe_ENDPOINT

xen/drivers/passthrough/pci.c static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn) · line 430

Quoted 13 lines
xen/drivers/passthrough/pci.c +2 -1
         case DEV_TYPE_PCIe2PCI_BRIDGE:
         case DEV_TYPE_LEGACY_PCI_BRIDGE:
             sec_bus = pci_conf_read8(pdev->sbdf, PCI_SECONDARY_BUS);
@@ -424,11 +423,11 @@ static struct pci_dev *alloc_pdev(struct pci_seg *pseg, u8 bus, u8 devfn)
 
 static void free_pdev(struct pci_seg *pseg, struct pci_dev *pdev)
 {
+    unsigned int sec_bus, sub_bus;
+
     /* update bus2bridge */
     switch ( pdev->type )
     {
-        unsigned int sec_bus, sub_bus;

They could go under case DEV_TYPE_LEGACY_PCI_BRIDGE

xen/drivers/passthrough/pci.c int iommu_do_pci_domctl( · line 1561

Quoted 12 lines
xen/drivers/passthrough/pci.c +1 -1
         case DEV_TYPE_PCIe2PCI_BRIDGE:
         case DEV_TYPE_LEGACY_PCI_BRIDGE:
             sec_bus = pci_conf_read8(pdev->sbdf, PCI_SECONDARY_BUS);
@@ -1555,11 +1554,10 @@ int iommu_do_pci_domctl(
     u8 bus, devfn;
     int ret = 0;
     uint32_t machine_sbdf;
+    unsigned int flags;
 
     switch ( domctl->cmd )
     {
-        unsigned int flags;

it could go under case XEN_DOMCTL_test_assign_device

Quoted 6 lines
xen/drivers/passthrough/pci.c +0 -0
     case XEN_DOMCTL_get_device_group:
     {
         u32 max_sdevs;
Signature · 1 line
2.34.1
#24 lore raw

PATCH 6/11 xen/ioreq: move variable declaration to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the variable declarations are moved in the smallest enclosing scope, near other variable definitions.

---
 xen/common/ioreq.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
xen/common/ioreq.c +1 -2
index 7cb717f7a2..5044f43b81 100644
@@ -1111,6 +1111,7 @@ struct ioreq_server *ioreq_server_select(struct domain *d,
     FOR_EACH_IOREQ_SERVER(d, id, s)
     {
         struct rangeset *r;
+        unsigned long start, end;
 
         if ( !s->enabled )
             continue;
@@ -1119,8 +1120,6 @@ struct ioreq_server *ioreq_server_select(struct domain *d,
 
         switch ( type )
         {
-            unsigned long start, end;
-
         case XEN_DMOP_IO_RANGE_PORT:
             start = addr;
             end = start + p->size - 1;
Signature · 1 line
2.34.1
#25 lore raw

PATCH 7/11 xen: address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE() macro to justify the violation of the rule.

---
 xen/common/shutdown.c | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
xen/common/shutdown.c +12 -6
index a933ee001e..88f735a30e 100644
@@ -38,39 +38,45 @@ void hwdom_shutdown(u8 reason)
         printk("Hardware Dom%u halted: halting machine\n",
                hardware_domain->domain_id);
         machine_halt();
-        break; /* not reached */
+        ASSERT_UNREACHABLE();
+        break;
 
     case SHUTDOWN_crash:
         debugger_trap_immediate();
         printk("Hardware Dom%u crashed: ", hardware_domain->domain_id);
         kexec_crash(CRASHREASON_HWDOM);
         maybe_reboot();
-        break; /* not reached */
+        ASSERT_UNREACHABLE();
+        break;
 
     case SHUTDOWN_reboot:
         printk("Hardware Dom%u shutdown: rebooting machine\n",
                hardware_domain->domain_id);
         machine_restart(0);
-        break; /* not reached */
+        ASSERT_UNREACHABLE();
+        break;
 
     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 */
+        ASSERT_UNREACHABLE();
+        break;
 
     case SHUTDOWN_soft_reset:
         printk("Hardware domain %d did unsupported soft reset, rebooting.\n",
                hardware_domain->domain_id);
         machine_restart(0);
-        break; /* not reached */
+        ASSERT_UNREACHABLE();
+        break;
 
     default:
         printk("Hardware Dom%u shutdown (unknown reason %u): ",
                hardware_domain->domain_id, reason);
         maybe_reboot();
-        break; /* not reached */
+        ASSERT_UNREACHABLE();
+        break;
     }
 }  
 
Signature · 1 line
2.34.1
#26 lore raw
Quoted 9 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE() macro to justify the violation of the rule.

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Jan

#27 lore raw
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE() macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve into an assert, but retains its role of a code marker.

Does it work?

#28 lore raw
Quoted 18 lines · Stefano Stabellini
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE() macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Quoted 8 lines

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve into an assert, but retains its role of a code marker.

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

Jan

#29 lore raw
Quoted 26 lines · Jan Beulich
Quoted 25 lines · Stefano Stabellini
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE() macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve into an assert, but retains its role of a code marker.

Quoted 7 lines
Quoted 1 line

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

Jan

It's not clear to me whether you dislike the patch itself or the commit message. If it's the latter, how about:
"ASSERT_UNREACHABLE() is used as a marker for intentionally unreachable code, which constitutes a motivated deviation from Rule 2.1. Additionally, in non-release builds, this macro performs a failing assertion to detect errors."

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#30 lore raw

Hi,

Quoted 33 lines · Nicola Vetrini
Quoted 32 lines · Jan Beulich
Quoted 18 lines · Stefano Stabellini
Quoted 11 lines · Jan Beulich
Quoted 9 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE() macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Quoted 8 lines

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve into an assert, but retains its role of a code marker.

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

Quoted 8 lines
Quoted 1 line

Jan

It's not clear to me whether you dislike the patch itself or the commit message. If it's the latter, how about:
"ASSERT_UNREACHABLE() is used as a marker for intentionally unreachable code, which
constitutes a motivated deviation from Rule 2.1. Additionally, in non-release builds, this macro performs a failing assertion to detect errors."

Any feedback on this (with one edit: s/a failing assertion/an assertion/)

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#31 lore raw
Quoted 47 lines · Nicola Vetrini

Hi,

Quoted 36 lines · Nicola Vetrini
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Stefano Stabellini
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE()
macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually
assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve
into an assert, but retains its role of a code marker.

Quoted 5 lines
Quoted 1 line

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

Quoted 7 lines

It's not clear to me whether you dislike the patch itself or the commit message. If it's the latter, how about:
"ASSERT_UNREACHABLE() is used as a marker for intentionally unreachable code, which
constitutes a motivated deviation from Rule 2.1. Additionally, in non-release
builds, this macro performs a failing assertion to detect errors."

Quoted 2 lines

Any feedback on this (with one edit: s/a failing assertion/an assertion/)

The patch here is kind of okay, but I'm afraid I view my earlier question as not addressed: How will the proposed change prevent the scanner from spotting issues here in release builds? Just stating in the description that there's a deviation is not going to help that.

Jan

#32 lore raw
Quoted 51 lines · Jan Beulich
Quoted 47 lines · Nicola Vetrini

Hi,

Quoted 36 lines · Nicola Vetrini
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Stefano Stabellini
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE()
macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually
assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve
into an assert, but retains its role of a code marker.

Quoted 5 lines
Quoted 1 line

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

Quoted 7 lines

It's not clear to me whether you dislike the patch itself or the commit message. If it's the latter, how about:
"ASSERT_UNREACHABLE() is used as a marker for intentionally unreachable code, which
constitutes a motivated deviation from Rule 2.1. Additionally, in non-release
builds, this macro performs a failing assertion to detect errors."

Quoted 2 lines

Any feedback on this (with one edit: s/a failing assertion/an assertion/)

Quoted 6 lines

The patch here is kind of okay, but I'm afraid I view my earlier question as not addressed: How will the proposed change prevent the scanner from spotting issues here in release builds? Just stating in the description that there's a deviation is not going to help that.

Jan

There is a deviation already in place. At the moment it just ignores anything below an unreachable ASSERT_UNREACHABLE(), no matter what that macro will expand to.

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#33 lore raw
Quoted 58 lines · Nicola Vetrini
Quoted 48 lines · Jan Beulich
Quoted 47 lines · Nicola Vetrini
Quoted 45 lines · Nicola Vetrini
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Stefano Stabellini
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE()
macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually
assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve
into an assert, but retains its role of a code marker.

Quoted 6 lines
Quoted 1 line

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies
rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

It's not clear to me whether you dislike the patch itself or the commit
message. If it's the latter, how about:
"ASSERT_UNREACHABLE() is used as a marker for intentionally unreachable code, which
constitutes a motivated deviation from Rule 2.1. Additionally, in non-release
builds, this macro performs a failing assertion to detect errors."

Quoted 8 lines
Quoted 2 lines

Any feedback on this (with one edit: s/a failing assertion/an assertion/)

The patch here is kind of okay, but I'm afraid I view my earlier question
as not addressed: How will the proposed change prevent the scanner from spotting issues here in release builds? Just stating in the description that there's a deviation is not going to help that.

Quoted 3 lines

There is a deviation already in place. At the moment it just ignores anything below an unreachable ASSERT_UNREACHABLE(), no matter what that macro will expand to.

What do you mean by "in place"? docs/misra/ has nothing, afaics. And automation/eclair_analysis/ECLAIR/deviations.ecl is merely filtering things out of reports, aiui. (Plus of course there should be a single central place where all deviations are recorded, imo.)

Jan

#34 lore raw
Quoted 59 lines · Jan Beulich
Quoted 58 lines · Nicola Vetrini
Quoted 56 lines · Jan Beulich
Quoted 47 lines · Nicola Vetrini
Quoted 37 lines · Nicola Vetrini
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Stefano Stabellini
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE()
macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually
assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve
into an assert, but retains its role of a code marker.

Quoted 6 lines
Quoted 1 line

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies
rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

Quoted 8 lines

It's not clear to me whether you dislike the patch itself or the commit
message. If it's the latter, how about:
"ASSERT_UNREACHABLE() is used as a marker for intentionally unreachable code, which
constitutes a motivated deviation from Rule 2.1. Additionally, in non-release
builds, this macro performs a failing assertion to detect errors."

Quoted 2 lines

Any feedback on this (with one edit: s/a failing assertion/an assertion/)

The patch here is kind of okay, but I'm afraid I view my earlier question
as not addressed: How will the proposed change prevent the scanner from spotting issues here in release builds? Just stating in the description that there's a deviation is not going to help that.

Quoted 8 lines
Quoted 3 lines

There is a deviation already in place. At the moment it just ignores anything below an unreachable ASSERT_UNREACHABLE(), no matter what that macro will expand to.

What do you mean by "in place"? docs/misra/ has nothing, afaics. And automation/eclair_analysis/ECLAIR/deviations.ecl is merely filtering things out of reports, aiui. (Plus of course there should be a single central place where all deviations are recorded, imo.)

The second statement is not quite correct, as some of the configurations instruct the checker how to behave.

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#35 lore raw
Quoted 69 lines · Nicola Vetrini
Quoted 68 lines · Jan Beulich
Quoted 66 lines · Nicola Vetrini
Quoted 53 lines · Jan Beulich
Quoted 49 lines · Nicola Vetrini
Quoted 39 lines · Nicola Vetrini
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Stefano Stabellini
Quoted 13 lines · Jan Beulich
Quoted 11 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE()
macro to justify the violation of the rule.

Quoted 6 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification
will be run against.

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually
assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve
into an assert, but retains its role of a code marker.

Quoted 6 lines
Quoted 1 line

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies
rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

Quoted 8 lines

It's not clear to me whether you dislike the patch itself or the commit
message. If it's the latter, how about:
"ASSERT_UNREACHABLE() is used as a marker for intentionally unreachable code, which
constitutes a motivated deviation from Rule 2.1. Additionally, in non-release
builds, this macro performs a failing assertion to detect errors."

Quoted 2 lines

Any feedback on this (with one edit: s/a failing assertion/an assertion/)

Quoted 7 lines

The patch here is kind of okay, but I'm afraid I view my earlier question
as not addressed: How will the proposed change prevent the scanner from
spotting issues here in release builds? Just stating in the description
that there's a deviation is not going to help that.

There is a deviation already in place. At the moment it just ignores anything below an unreachable ASSERT_UNREACHABLE(), no matter what that
macro will expand to.

Quoted 8 lines
Quoted 4 lines

What do you mean by "in place"? docs/misra/ has nothing, afaics. And automation/eclair_analysis/ECLAIR/deviations.ecl is merely filtering things out of reports, aiui. (Plus of course there should be a single central place where all deviations are recorded, imo.)

The second statement is not quite correct, as some of the configurations instruct the
checker how to behave.

Well, I was referring to the one setting relevant here, and I added "aiui" to indicate that I may be misreading what that file specifies.

Jan

#36 lore raw
In reply to Jan Beulich
Quoted 59 lines · Jan Beulich
Quoted 58 lines · Nicola Vetrini
Quoted 56 lines · Jan Beulich
Quoted 47 lines · Nicola Vetrini
Quoted 37 lines · Nicola Vetrini
Quoted 29 lines · Jan Beulich
Quoted 28 lines · Stefano Stabellini
Quoted 12 lines · Jan Beulich
Quoted 10 lines · Nicola Vetrini

Rule 2.1 states: "A project shall not contain unreachable code".

The functions
- machine_halt
- maybe_reboot
- machine_restart
are not supposed to return, hence the following break statement is marked as intentionally unreachable with the ASSERT_UNREACHABLE()
macro to justify the violation of the rule.

Quoted 5 lines

During the discussion it was mentioned that this won't help with release builds, where right now ASSERT_UNREACHABLE() expands to effectively nothing. You want to clarify here how release builds are to be taken care of, as those are what eventual certification will be run against.

Something along these lines:

ASSERT_UNREACHABLE(), not only is used in non-release builds to actually
assert and detect errors, but it is also used as a marker to tag unreachable code. In release builds ASSERT_UNREACHABLE() doesn't resolve
into an assert, but retains its role of a code marker.

Quoted 6 lines
Quoted 1 line

Does it work?

Well, it states what is happening, but I'm not convinced it satisfies
rule 2.1. There's then still code there which isn't reachable, and which a scanner will spot and report.

Quoted 8 lines

It's not clear to me whether you dislike the patch itself or the commit
message. If it's the latter, how about:
"ASSERT_UNREACHABLE() is used as a marker for intentionally unreachable code, which
constitutes a motivated deviation from Rule 2.1. Additionally, in non-release
builds, this macro performs a failing assertion to detect errors."

Quoted 2 lines

Any feedback on this (with one edit: s/a failing assertion/an assertion/)

The patch here is kind of okay, but I'm afraid I view my earlier question
as not addressed: How will the proposed change prevent the scanner from spotting issues here in release builds? Just stating in the description that there's a deviation is not going to help that.

Quoted 8 lines
Quoted 3 lines

There is a deviation already in place. At the moment it just ignores anything below an unreachable ASSERT_UNREACHABLE(), no matter what that macro will expand to.

What do you mean by "in place"? docs/misra/ has nothing, afaics. And automation/eclair_analysis/ECLAIR/deviations.ecl is merely filtering things out of reports, aiui. (Plus of course there should be a single central place where all deviations are recorded, imo.)

Jan has a point: I think we should record all our deviations and unique ways to interpret the rules under docs/misra. And the Eclair configuration should reflect that. It is not a good idea to only keep the information in the Eclair config because, even if it is now upstream in xen.git, it is still a tool-specific configuration file -- it is not a proper document. MISRA C and its interpretation is important enough that we should have a plain English document to cover it (which now is docs/misra/rules.rst).

Nicola, I volunteer to send patches to make any necessary updates to docs/misra/rules.rst and other docs. Please send out to me a list of deviations/configurations and I'll make sure to reflect them under docs/misra so that they are in sync.

Cheers,

Stefano

#37 lore raw
Quoted 10 lines

Jan has a point: I think we should record all our deviations and unique ways to interpret the rules under docs/misra. And the Eclair configuration should reflect that. It is not a good idea to only keep the information in the Eclair config because, even if it is now upstream in xen.git, it is still a tool-specific configuration file -- it is not a proper document. MISRA C and its interpretation is important enough that we should have a plain English document to cover it (which now is docs/misra/rules.rst).

Quoted 8 lines

Nicola, I volunteer to send patches to make any necessary updates to docs/misra/rules.rst and other docs. Please send out to me a list of deviations/configurations and I'll make sure to reflect them under docs/misra so that they are in sync.

Cheers,

Stefano

Sure, I'll let you know when I have it.
Thanks,

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#38 lore raw

PATCH 8/11 xen: move declarations to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

Declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the variable declarations are moved in the smallest enclosing scope, near other variable definitions.

---
 xen/common/compat/memory.c |  3 +--
 xen/common/domain.c        | 15 +++++++--------
 2 files changed, 8 insertions(+), 10 deletions(-)
xen/common/compat/memory.c +1 -2
index 8ca63ceda6..d4c4204119 100644
@@ -85,13 +85,12 @@ int compat_memory_op(unsigned int cmd, XEN_GUEST_HANDLE_PARAM(void) compat)
             struct compat_mem_access_op mao;
             struct compat_mem_acquire_resource mar;
         } cmp;
+        xen_pfn_t *space;
 
         set_xen_guest_handle(nat.hnd, COMPAT_ARG_XLAT_VIRT_BASE);
         split = 0;
         switch ( op )
         {
-            xen_pfn_t *space;
-
         case XENMEM_increase_reservation:
         case XENMEM_decrease_reservation:
         case XENMEM_populate_physmap:
xen/common/domain.c +7 -8
index 304aa04fa6..e3aeaf059d 100644
@@ -401,6 +401,13 @@ static int domain_teardown(struct domain *d)
 {
     struct vcpu *v;
     int rc;
+    enum {
+        PROG_none,
+        PROG_gnttab_mappings,
+        PROG_vcpu_teardown,
+        PROG_arch_teardown,
+        PROG_done,
+    };
 
     BUG_ON(!d->is_dying);
 
@@ -435,14 +442,6 @@ static int domain_teardown(struct domain *d)
     case PROG_vcpu_ ## x:                       \
         v = d->teardown.vcpu
 
-        enum {
-            PROG_none,
-            PROG_gnttab_mappings,
-            PROG_vcpu_teardown,
-            PROG_arch_teardown,
-            PROG_done,
-        };
-
     case PROG_none:
         BUILD_BUG_ON(PROG_none != 0);
 
Signature · 1 line
2.34.1
#39 lore raw

PATCH 9/11 x86/xstate: moved BUILD_BUG_ON to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

Variable declarations between a switch statement guard and before any case label are unreachable code, and hence violate Rule 2.1:
"A project shall not contain unreachable code".

Therefore the variable declarations are moved in the smallest enclosing scope, near other variable definitions.

---
 xen/arch/x86/xstate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
xen/arch/x86/xstate.c +2 -1
index 92496f3795..cb2b9e720c 100644
@@ -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
#40 lore raw

PATCH 10/11 xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

The break statement after the return statement is definitely unreachable. As such, an call to the ASSERT_UNREACHABLE() macro is added to signal the intentionality of such construct.

---
The break in the clause is mandated by Required Rule 16.3, which is not yet an accepted rule for Xen, but may be in the future.

---
 xen/common/sched/core.c | 1 +
 1 file changed, 1 insertion(+)
xen/common/sched/core.c +1 -0
index 022f548652..fcee902b4e 100644
@@ -2233,6 +2233,7 @@ static bool sched_tasklet_check_cpu(unsigned int cpu)
         /* fallthrough */
     case TASKLET_enqueued|TASKLET_scheduled:
         return true;
+        ASSERT_UNREACHABLE();
         break;
     case TASKLET_scheduled:
         clear_bit(_TASKLET_scheduled, tasklet_work);
Signature · 1 line
2.34.1
#41 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

xen/common/sched/core.c static bool sched_tasklet_check_cpu(unsigned int cpu) · line 2237

Quoted 8 lines · Nicola Vetrini
xen/common/sched/core.c +1 -0
@@ -2233,6 +2233,7 @@ static bool sched_tasklet_check_cpu(unsigned int cpu)
         /* fallthrough */
     case TASKLET_enqueued|TASKLET_scheduled:
         return true;
+        ASSERT_UNREACHABLE();
         break;

What use is "break" after "return"? IOW rather than adding code here, imo a line wants removing.

Jan

#42 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

Quoted 14 lines · Jan Beulich

xen/common/sched/core.c static bool sched_tasklet_check_cpu(unsigned int · line 2238

Quoted 9 lines · Nicola Vetrini
xen/common/sched/core.c +1 -0
@@ -2233,6 +2233,7 @@ static bool sched_tasklet_check_cpu(unsigned int 
 cpu)
         /* fallthrough */
     case TASKLET_enqueued|TASKLET_scheduled:
         return true;
+        ASSERT_UNREACHABLE();
         break;

What use is "break" after "return"? IOW rather than adding code here, imo a line wants removing.

Quoted 1 line

Jan

The "return false" after the switch would still be unreachable. The reasoning behind preserving the break is mainly MISRA Rule 16.3: "An unconditional break statement shall terminate every switch-clause", which has not yet been considered for adoption, but might be in future discussions, leading to putting back the break here.

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#43 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

Quoted 15 lines · Nicola Vetrini
Quoted 13 lines · Jan Beulich

xen/common/sched/core.c static bool sched_tasklet_check_cpu(unsigned int · line 2238

Quoted 9 lines · Nicola Vetrini
xen/common/sched/core.c +1 -0
@@ -2233,6 +2233,7 @@ static bool sched_tasklet_check_cpu(unsigned int 
 cpu)
         /* fallthrough */
     case TASKLET_enqueued|TASKLET_scheduled:
         return true;
+        ASSERT_UNREACHABLE();
         break;

What use is "break" after "return"? IOW rather than adding code here, imo a line wants removing.

Quoted 6 lines

The "return false" after the switch would still be unreachable. The reasoning behind preserving the break
is mainly MISRA Rule 16.3: "An unconditional break statement shall terminate every switch-clause", which has
not yet been considered for adoption, but might be in future discussions, leading to putting back the break here.

Well, adding such bogus "break"s is going to face my opposition, and it is in direct conflict with the "no unreachable code" rule here.

Jan

#44 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

Hi,

Quoted 3 lines · Nicola Vetrini

The break statement after the return statement is definitely unreachable. As such, an call to the ASSERT_UNREACHABLE() macro is added to signal the intentionality of such construct.

How about using unreachable() rather than ASSERT_UNREACHABLE()? The main difference is the later will hint the compiler that the code cannot be reached and will not crash Xen in debug build (this could be changed).

Quoted 14 lines

---
The break in the clause is mandated by Required Rule 16.3, which is not yet an accepted rule for Xen, but may be in the future.

---
  xen/common/sched/core.c | 1 +
  1 file changed, 1 insertion(+)
xen/common/sched/core.c +0 -0
index 022f548652..fcee902b4e 100644

xen/common/sched/core.c static bool sched_tasklet_check_cpu(unsigned int cpu) · line 2239

Quoted 7 lines
xen/common/sched/core.c +1 -0
@@ -2233,6 +2233,7 @@ static bool sched_tasklet_check_cpu(unsigned int cpu)
          /* fallthrough */
      case TASKLET_enqueued|TASKLET_scheduled:
          return true;
+        ASSERT_UNREACHABLE();
          break;
      case TASKLET_scheduled:
          clear_bit(_TASKLET_scheduled, tasklet_work);

Cheers,

Signature · 1 line
Julien Grall
#45 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

Quoted 8 lines · Julien Grall
Quoted 3 lines · Nicola Vetrini

The break statement after the return statement is definitely unreachable. As such, an call to the ASSERT_UNREACHABLE() macro is added to signal the intentionality of such construct.

How about using unreachable() rather than ASSERT_UNREACHABLE()? The main difference is the later will hint the compiler that the code cannot be reached and will not crash Xen in debug build (this could be changed).

Isn't using unreachable() in place of ASSERT_UNREACHABLE() unsafe (not here but in general)? It'll tell the compiler that (in the extreme case) it can omit the function epilogue, including the return instruction. In the resulting build, if the code turns out to be reachable, execution would fall through into whatever follows.

In the case here ...

Quoted 10 lines

xen/common/sched/core.c static bool sched_tasklet_check_cpu(unsigned int cpu) · line 2239

Quoted 10 lines
xen/common/sched/core.c +1 -0
@@ -2233,6 +2233,7 @@ static bool sched_tasklet_check_cpu(unsigned int cpu)
          /* fallthrough */
      case TASKLET_enqueued|TASKLET_scheduled:
          return true;
+        ASSERT_UNREACHABLE();
          break;
      case TASKLET_scheduled:
          clear_bit(_TASKLET_scheduled, tasklet_work);

... "return" alone already tells the compiler that "break" is unreachable. You don't need unreachable() for that. As said before, "break" like this simply want purging (and Misra is wrong to demand "break" everywhere - it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto).

Jan

#46 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

Hi,

Quoted 6 lines · Jan Beulich
Quoted 5 lines · Julien Grall
Quoted 3 lines · Nicola Vetrini

The break statement after the return statement is definitely unreachable. As such, an call to the ASSERT_UNREACHABLE() macro is added to signal the intentionality of such construct.

Quoted 6 lines
Quoted 3 lines

How about using unreachable() rather than ASSERT_UNREACHABLE()? The main difference is the later will hint the compiler that the code cannot be reached and will not crash Xen in debug build (this could be changed).

Isn't using unreachable() in place of ASSERT_UNREACHABLE() unsafe (not here but in general)?

Yes it is.

It'll tell the compiler that (in the extreme case)

Quoted 3 lines

it can omit the function epilogue, including the return instruction. In the resulting build, if the code turns out to be reachable, execution would fall through into whatever follows.

Right, but the problem is somewhat similar with adding ASSERT_UNREACHABLE() without proper error path because there is no guarantee the rest of the code will be correct in the unlikely case it is reachable.

Quoted 12 lines

In the case here ...

Quoted 9 lines
Quoted 9 lines
xen/common/sched/core.c +1 -0
@@ -2233,6 +2233,7 @@ static bool sched_tasklet_check_cpu(unsigned int cpu)
           /* fallthrough */
       case TASKLET_enqueued|TASKLET_scheduled:
           return true;
+        ASSERT_UNREACHABLE();
           break;
       case TASKLET_scheduled:
Quoted 8 lines
Quoted 1 line

xen/common/sched/core.c static bool sched_tasklet_check_cpu(unsigned int cpu) · line 2239

Quoted 1 line
xen/common/sched/core.c +0 -0
           clear_bit(_TASKLET_scheduled, tasklet_work);

... "return" alone already tells the compiler that "break" is unreachable. You don't need unreachable() for that. As said before, "break" like this simply want purging (and Misra is wrong to demand "break" everywhere - it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto).

My suggestion was in the context of this series where we add ASSERT_UNREACHABLE() before break. From my understanding, we don't have a lot of leeway here because, from what Nicola wrote, rule 16.3 is mandatory.

So I think using unreachable is better if we every decide to use break after return.

Cheers,

Signature · 1 line
Julien Grall
#47 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

Quoted 14 lines
Quoted 8 lines

... "return" alone already tells the compiler that "break" is unreachable. You don't need unreachable() for that. As said before, "break" like this simply want purging (and Misra is wrong to demand "break" everywhere - it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto).

My suggestion was in the context of this series where we add ASSERT_UNREACHABLE() before break. From my understanding, we don't have a lot of leeway here because, from what Nicola wrote, rule 16.3 is mandatory.

Quoted 4 lines

So I think using unreachable is better if we every decide to use break after return.

Cheers,

16.3 is not Mandatory, just Required. I was just reluctant to knowingly add one more violation while fixing another before submitting the patch. If indeed 16.3 won't likely be adopted by Xen, then that break should indeed go away.

However, the main thing that's holding me back from doing a slimmed-down v2 here is that evidently there's no consensus even on the ASSERT_UNREACHABLE() patches.

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#48 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

Quoted 14 lines · Nicola Vetrini
Quoted 14 lines
Quoted 8 lines

... "return" alone already tells the compiler that "break" is unreachable. You don't need unreachable() for that. As said before, "break" like this simply want purging (and Misra is wrong to demand "break" everywhere - it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto).

My suggestion was in the context of this series where we add ASSERT_UNREACHABLE() before break. From my understanding, we don't have a lot of leeway here because, from what Nicola wrote, rule 16.3 is mandatory.

Quoted 6 lines
Quoted 4 lines

So I think using unreachable is better if we every decide to use break after return.

Cheers,

16.3 is not Mandatory, just Required.

Ah I misread what you wrote. In that case...

Quoted 6 lines

I was just reluctant to knowingly add one more violation
while fixing another before submitting the patch. If indeed 16.3 won't likely be adopted by Xen, then that break should indeed go away.

However, the main thing that's holding me back from doing a slimmed-down v2 here is that evidently there's no consensus even on the ASSERT_UNREACHABLE() patches.

... I agree with the following statement from Jan:

"it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto"

Cheers,

Signature · 1 line
Julien Grall
#49 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

Quoted 27 lines · Julien Grall
Quoted 12 lines · Nicola Vetrini
Quoted 12 lines
Quoted 6 lines

... "return" alone already tells the compiler that "break" is unreachable. You don't need unreachable() for that. As said before, "break" like this simply want purging (and Misra is wrong to demand "break" everywhere - it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto).

My suggestion was in the context of this series where we add ASSERT_UNREACHABLE() before break. From my understanding, we don't have a lot of leeway here because, from what Nicola wrote, rule 16.3 is mandatory.

Quoted 6 lines
Quoted 4 lines

So I think using unreachable is better if we every decide to use break after return.

Cheers,

16.3 is not Mandatory, just Required.

Ah I misread what you wrote. In that case...

Quoted 5 lines

I was just reluctant to knowingly add one more violation while fixing another before submitting the patch. If indeed 16.3 won't likely be adopted by Xen,
then that break should indeed go away.

Quoted 8 lines
Quoted 3 lines

However, the main thing that's holding me back from doing a slimmed-down v2 here is that
evidently there's no consensus even on the ASSERT_UNREACHABLE() patches.

... I agree with the following statement from Jan:

"it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto"

I also think it is a better idea to have no unannotated fall-through in switch statements (unless we have a "case" with nothing underneath, so two cases next to each other). In other words the following are all OK in my view:

  case A:
    do();
    break;
  case B:
    do();
    return true;
  case Ca:
  case Cb:
    goto fail;
  case D:
    i++;
    continue;
  case E:
    do();
    /* fall-through */
  case F:
    do();
    break;

While the following is not OK:

  case A:
    do();
  case B:
    do();

This should be what we already do in Xen, although it is not documented properly. Jan, Julien do you agree?

If so, could Eclair be configured to check for this pattern (or similar)?

We must have one of the following:
- break, continue, return, goto
- /* fall-through */
- empty case body (case Ca/Cb)

#50 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

Quoted 63 lines · Stefano Stabellini
Quoted 27 lines · Julien Grall
Quoted 12 lines · Nicola Vetrini
Quoted 12 lines
Quoted 6 lines

... "return" alone already tells the compiler that "break" is unreachable. You don't need unreachable() for that. As said before, "break" like this simply want purging (and Misra is wrong to demand "break" everywhere - it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto).

My suggestion was in the context of this series where we add ASSERT_UNREACHABLE() before break. From my understanding, we don't have a lot of leeway here because, from what Nicola wrote, rule 16.3 is mandatory.

Quoted 6 lines
Quoted 4 lines

So I think using unreachable is better if we every decide to use break after return.

Cheers,

16.3 is not Mandatory, just Required.

Ah I misread what you wrote. In that case...

Quoted 5 lines

I was just reluctant to knowingly add one more violation while fixing another before submitting the patch. If indeed 16.3 won't likely be adopted by Xen,
then that break should indeed go away.

Quoted 8 lines
Quoted 3 lines

However, the main thing that's holding me back from doing a slimmed-down v2 here is that
evidently there's no consensus even on the ASSERT_UNREACHABLE() patches.

... I agree with the following statement from Jan:

"it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto"

I also think it is a better idea to have no unannotated fall-through in switch statements (unless we have a "case" with nothing underneath, so two cases next to each other). In other words the following are all OK in my view:

  case A:
    do();
    break;
  case B:
    do();
    return true;
  case Ca:
  case Cb:
    goto fail;
  case D:
    i++;
    continue;
  case E:
    do();
    /* fall-through */
  case F:
    do();
    break;

While the following is not OK:

Quoted 7 lines
  case A:
    do();
  case B:
    do();

This should be what we already do in Xen, although it is not documented properly. Jan, Julien do you agree?

Yes.

Quoted 6 lines

If so, could Eclair be configured to check for this pattern (or similar)?

We must have one of the following:
- break, continue, return, goto
- /* fall-through */

- fallthrough;

(the pseudo keyword that we have; see compiler.h for actually having the documentation you're looking for, albeit missing "continue", and of course not really expected to live [just] there)

Jan

Quoted 1 line

- empty case body (case Ca/Cb)

#51 lore raw

Re: [XEN PATCH 10/11] xen/sched: add ASSERT_UNREACHABLE() to address MISRA C:2012 Rule 2.1

In reply to Stefano Stabellini

Hi Stefano,

Quoted 63 lines · Stefano Stabellini
Quoted 27 lines · Julien Grall
Quoted 12 lines · Nicola Vetrini
Quoted 12 lines
Quoted 6 lines

... "return" alone already tells the compiler that "break" is unreachable. You don't need unreachable() for that. As said before, "break" like this simply want purging (and Misra is wrong to demand "break" everywhere - it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto).

My suggestion was in the context of this series where we add ASSERT_UNREACHABLE() before break. From my understanding, we don't have a lot of leeway here because, from what Nicola wrote, rule 16.3 is mandatory.

Quoted 6 lines
Quoted 4 lines

So I think using unreachable is better if we every decide to use break after return.

Cheers,

16.3 is not Mandatory, just Required.

Ah I misread what you wrote. In that case...

Quoted 5 lines

I was just reluctant to knowingly add one more violation while fixing another before submitting the patch. If indeed 16.3 won't likely be adopted by Xen,
then that break should indeed go away.

Quoted 8 lines
Quoted 3 lines

However, the main thing that's holding me back from doing a slimmed-down v2 here is that
evidently there's no consensus even on the ASSERT_UNREACHABLE() patches.

... I agree with the following statement from Jan:

"it should instead demand no [unannotated] fall-through, which can also be achieved by return, continue, and goto"

I also think it is a better idea to have no unannotated fall-through in switch statements (unless we have a "case" with nothing underneath, so two cases next to each other). In other words the following are all OK in my view:

   case A:
     do();
     break;
   case B:
     do();
     return true;
   case Ca:
   case Cb:
     goto fail;
   case D:
     i++;
     continue;
   case E:
     do();
     /* fall-through */
   case F:
     do();
     break;

While the following is not OK:

Quoted 7 lines
   case A:
     do();
   case B:
     do();

This should be what we already do in Xen, although it is not documented properly. Jan, Julien do you agree?

I agree.

Cheers,

Signature · 1 line
Julien Grall
#52 lore raw

PATCH 11/11 x86/mm: Add assertion to address MISRA C:2012 Rule 2.1

In reply to Nicola Vetrini

The ASSERT_UNREACHABLE() assertion is added before a definitely unreachable return statement to address MISRA C:2012 Rule 2.1, because the explicit return from a non-void function is a defensive coding measure, and thus intended to be unreachable.

No functional changes.

---
 xen/arch/x86/mm.c         | 1 +
 xen/arch/x86/mm/p2m-pod.c | 1 +
 2 files changed, 2 insertions(+)
xen/arch/x86/mm.c +1 -0
index be2b10a391..ebd4f3827a 100644
@@ -4879,6 +4879,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         return subarch_memory_op(cmd, arg);
     }
 
+    ASSERT_UNREACHABLE();
     return 0;
 }
 
xen/arch/x86/mm/p2m-pod.c +1 -0
index 9969eb45fa..cd5501217f 100644
@@ -1045,6 +1045,7 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t *gfns, unsigned int count
     }
 
     return;
+    ASSERT_UNREACHABLE();
 
 out_unmap:
     /*
Signature · 1 line
2.34.1
#53 lore raw

Re: [XEN PATCH 11/11] x86/mm: Add assertion to address MISRA C:2012 Rule 2.1

xen/arch/x86/mm.c long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 4884

Quoted 9 lines · Nicola Vetrini
xen/arch/x86/mm.c +1 -0
@@ -4879,6 +4879,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         return subarch_memory_op(cmd, arg);
     }
 
+    ASSERT_UNREACHABLE();
     return 0;
 }

I'd prefer to instead switch earlier "return 0" to "break".

xen/arch/x86/mm/p2m-pod.c p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t *gfns, unsigned int count · line 1051

Quoted 10 lines
xen/arch/x86/mm/p2m-pod.c +1 -0
@@ -1045,6 +1045,7 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const gfn_t *gfns, unsigned int count
     }
 
     return;
+    ASSERT_UNREACHABLE();
 
 out_unmap:
     /*

In the description you say "before", but here you add something _after_ "return". What's the deal?

Jan

#54 lore raw

Re: [XEN PATCH 11/11] x86/mm: Add assertion to address MISRA C:2012 Rule 2.1

Quoted 8 lines · Jan Beulich
Quoted 7 lines · Nicola Vetrini
xen/arch/x86/mm.c +0 -0
@@ -4879,6 +4879,7 @@ long arch_memory_op(unsigned long cmd, 
 XEN_GUEST_HANDLE_PARAM(void) arg)
         return subarch_memory_op(cmd, arg);
     }
 
Quoted 5 lines

xen/arch/x86/mm.c long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 4884

Quoted 3 lines
xen/arch/x86/mm.c +1 -0
+    ASSERT_UNREACHABLE();
     return 0;
 }

I'd prefer to instead switch earlier "return 0" to "break".

Ok

Quoted 10 lines
Quoted 10 lines
xen/arch/x86/mm/p2m-pod.c +0 -0
@@ -1045,6 +1045,7 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const 

gfn_t *gfns, unsigned int count

     }

     return;

+ ASSERT_UNREACHABLE();

Quoted 7 lines
Quoted 2 lines

out_unmap:

     /*

In the description you say "before", but here you add something _after_ "return". What's the deal?

Jan

In this case the unreachable part is that after the label (looking at it now, I should have put the assert after the label to make it clear), because earlier all jumps to 'out_unmap' are like this:

  ASSERT_UNREACHABLE();
  domain_crash(d);
  goto out_unmap;

As I understood it, this is a defensive coding measure, preventing pages to remain mapped if, for some reason the above code actually executes. Am I correct?

Regards,

Signature · 2 lines
Nicola Vetrini, BSc
Software Engineer, BUGSENG srl (https://bugseng.com)
#55 lore raw

Re: [XEN PATCH 11/11] x86/mm: Add assertion to address MISRA C:2012 Rule 2.1

Quoted 23 lines · Nicola Vetrini
Quoted 7 lines · Jan Beulich
Quoted 6 lines · Nicola Vetrini
xen/arch/x86/mm/p2m-pod.c +0 -0
@@ -1045,6 +1045,7 @@ p2m_pod_zero_check(struct p2m_domain *p2m, const 

gfn_t *gfns, unsigned int count

     }
Quoted 8 lines
Quoted 5 lines
     return;

+ ASSERT_UNREACHABLE();

out_unmap:

     /*

In the description you say "before", but here you add something _after_ "return". What's the deal?

In this case the unreachable part is that after the label (looking at it now, I should have
put the assert after the label to make it clear), because earlier all jumps to
'out_unmap' are like this:

Quoted 7 lines
   ASSERT_UNREACHABLE();
   domain_crash(d);
   goto out_unmap;

As I understood it, this is a defensive coding measure, preventing pages to remain mapped if,
for some reason the above code actually executes. Am I correct?

The comment there says "probably". So the label and following code might be used for another error condition as well. Furthermore both paths presently using "goto out_unmap" already have ASSERT_UNREACHABLE(), so it's hard to see why we would need yet one more.

Jan