Skip to contentSkip to conversation

xen: address violations of MISRA C:2012 Rule 11.8

PATCH Superseded Simone Ballarin 4 participants 11 messages Nov 24, 2023 – Nov 30, 2023

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover xen: address violations of MISRA C:2012 Rule 11.8

From: Maria Celeste Cesario <[email protected]>

The xen sources contains violations of MISRA C:2012 Rule 11.8 whose headline states:
"A conversion shall not remove any const, volatile or Atomic qualification from the type pointed to by a pointer".

This patch amends or removes casts that unnecessarily drop const qualifiers. Example:

  static inline int atomic_read(const atomic_t *v)
  {
    return *(volatile int *)&v->counter;    /* Non-compliant */
  }

In this example, the const qualifier is unnnecessarily removed from the original object, therefore making it non-compliant.

Maria Celeste Cesario (5):
x86/platform_hypercall: address violations of MISRA C:2012 Rule 11.8 x86/boot/reloc: address violations of MISRA C:2012 Rule 11.8 AMD/IOMMU: address violations of MISRA C:2012 Rule 11.8 x86/atomic: address violations of MISRA C:2012 Rule 11.8 xen/arm: address violations of MISRA C:2012 Rule 11.8

 xen/arch/arm/domain_build.c              | 2 +-
 xen/arch/arm/include/asm/atomic.h        | 2 +-
 xen/arch/arm/include/asm/regs.h          | 2 +-
 xen/arch/x86/boot/reloc.c                | 2 +-
 xen/arch/x86/include/asm/atomic.h        | 8 ++++----
 xen/arch/x86/platform_hypercall.c        | 2 +-
 xen/drivers/passthrough/amd/iommu_acpi.c | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)
Signature · 1 line
2.40.0
#1 lore raw

PATCH 1/5 x86/platform_hypercall: address violations of MISRA C:2012 Rule 11.8

From: Maria Celeste Cesario <[email protected]>

Add const qualifier in cast that unnecessarily removes it to comply with Rule 11.8.
The variable info is declared with a const qualified type. No functional change.

---
 xen/arch/x86/platform_hypercall.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/platform_hypercall.c +1 -1
index c1ab552c57..4dde71db27 100644
@@ -364,7 +364,7 @@ ret_t do_platform_op(
             if ( length > info->edd_device_params.length )
                 length = info->edd_device_params.length;
             if ( copy_to_compat(op->u.firmware_info.u.disk_info.edd_params,
-                                (u8 *)&info->edd_device_params,
+                                (const uint8_t *)&info->edd_device_params,
                                 length) )
                 break;
             if ( copy_to_compat(op->u.firmware_info.u.disk_info.edd_params,
Signature · 1 line
2.40.0
#2 lore raw

PATCH 2/5 x86/boot/reloc: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

Add missing const qualifier in casting to comply with Rule 11.8. Argument tag is typically const qualified.
No functional change.

---
 xen/arch/x86/boot/reloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/boot/reloc.c +1 -1
index e093b7389c..df0bc710a7 100644
@@ -67,7 +67,7 @@ struct vesa_mode_info {
 };
 #endif /* CONFIG_VIDEO */
 
-#define get_mb2_data(tag, type, member)   (((multiboot2_tag_##type##_t *)(tag))->member)
+#define get_mb2_data(tag, type, member)   (((const multiboot2_tag_##type##_t *)(tag))->member)
 #define get_mb2_string(tag, type, member) ((u32)get_mb2_data(tag, type, member))
 
 static u32 alloc;
Signature · 1 line
2.40.0
#3 lore raw

PATCH 3/5 AMD/IOMMU: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

Add missing const qualifier in casting to comply with Rule 11.8. The type of the formal parameter ivhd_block is const qualified. No functional change.

---
 xen/drivers/passthrough/amd/iommu_acpi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/passthrough/amd/iommu_acpi.c +1 -1
index 699d33f429..00923a6bb5 100644
@@ -1232,7 +1232,7 @@ static int __init get_last_bdf_ivhd(
     while ( ivhd_block->header.length >=
             (block_length + sizeof(struct acpi_ivrs_de_header)) )
     {
-        ivhd_device = (const void *)((u8 *)ivhd_block + block_length);
+        ivhd_device = (const void *)((const uint8_t *)ivhd_block + block_length);
 
         switch ( ivhd_device->header.type )
         {
Signature · 1 line
2.40.0
#4 lore raw

Re: [PATCH 3/5] AMD/IOMMU: address violations of MISRA C:2012 Rule 11.8

Quoted 17 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

Add missing const qualifier in casting to comply with Rule 11.8. The type of the formal parameter ivhd_block is const qualified. No functional change.

---
 xen/drivers/passthrough/amd/iommu_acpi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/passthrough/amd/iommu_acpi.c +0 -0
index 699d33f429..00923a6bb5 100644

xen/drivers/passthrough/amd/iommu_acpi.c static int __init get_last_bdf_ivhd( · line 1235

Quoted 5 lines
xen/drivers/passthrough/amd/iommu_acpi.c +1 -1
@@ -1232,7 +1232,7 @@ static int __init get_last_bdf_ivhd(
     while ( ivhd_block->header.length >=
             (block_length + sizeof(struct acpi_ivrs_de_header)) )
     {
-        ivhd_device = (const void *)((u8 *)ivhd_block + block_length);
+        ivhd_device = (const void *)((const uint8_t *)ivhd_block + block_length);

This we should simplify into just:

    ivhd_device = (const void *)ivhd_block + block_length;

We use this GCC extension in many other places too.

~Andrew

#5 lore raw

PATCH 4/5 x86/atomic: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

Edit casts that unnecessarily remove const qualifiers to comply with Rule 11.8.
The type of the provided pointer may be const qualified. No functional change.

---
 xen/arch/x86/include/asm/atomic.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
xen/arch/x86/include/asm/atomic.h +4 -4
index 27aad43aaa..16bd0ebfd7 100644
@@ -51,10 +51,10 @@ void __bad_atomic_size(void);
     unsigned long x_;                                     \
     CLANG_DISABLE_WARN_GCC_COMPAT_START                   \
     switch ( sizeof(*(p)) ) {                             \
-    case 1: x_ = read_u8_atomic((uint8_t *)(p)); break;   \
-    case 2: x_ = read_u16_atomic((uint16_t *)(p)); break; \
-    case 4: x_ = read_u32_atomic((uint32_t *)(p)); break; \
-    case 8: x_ = read_u64_atomic((uint64_t *)(p)); break; \
+    case 1: x_ = read_u8_atomic((const uint8_t *)(p)); break;   \
+    case 2: x_ = read_u16_atomic((const uint16_t *)(p)); break; \
+    case 4: x_ = read_u32_atomic((const uint32_t *)(p)); break; \
+    case 8: x_ = read_u64_atomic((const uint64_t *)(p)); break; \
     default: x_ = 0; __bad_atomic_size(); break;          \
     }                                                     \
     CLANG_DISABLE_WARN_GCC_COMPAT_END                     \
Signature · 1 line
2.40.0
#6 lore raw

PATCH 5/5 xen/arm: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

Add or amend casts to comply with Rule 11.8.

The violations are resolved either:
- by adding a missing const qualifier in the cast
- by removing a cast to non-const on a const-qualified object

No functional change.

---
 xen/arch/arm/domain_build.c       | 2 +-
 xen/arch/arm/include/asm/atomic.h | 2 +-
 xen/arch/arm/include/asm/regs.h   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)
xen/arch/arm/domain_build.c +1 -1
index 2dd2926b41..c17214f738 100644
@@ -2749,7 +2749,7 @@ static int __init handle_passthrough_prop(struct kernel_info *kinfo,
     if ( node == NULL )
     {
         printk(XENLOG_ERR "Couldn't find node %s in host_dt!\n",
-               (char *)xen_path->data);
+               xen_path->data);
         return -EINVAL;
     }
 
xen/arch/arm/include/asm/atomic.h +1 -1
index 64314d59b3..517216d2a8 100644
@@ -154,7 +154,7 @@ static always_inline void write_atomic_size(volatile void *p,
  */
 static inline int atomic_read(const atomic_t *v)
 {
-    return *(volatile int *)&v->counter;
+    return *(const volatile int *)&v->counter;
 }
 
 static inline int _atomic_read(atomic_t v)
xen/arch/arm/include/asm/regs.h +1 -1
index 8a0db95415..79050937f3 100644
@@ -48,7 +48,7 @@ static inline bool regs_mode_is_32bit(const struct cpu_user_regs *regs)
 
 static inline bool guest_mode(const struct cpu_user_regs *r)
 {
-    unsigned long diff = (char *)guest_cpu_user_regs() - (char *)(r);
+    unsigned long diff = (char *)guest_cpu_user_regs() - (const char *)(r);
     /* Frame pointer must point into current CPU stack. */
     ASSERT(diff < STACK_SIZE);
     /* If not a guest frame, it must be a hypervisor frame. */
Signature · 1 line
2.40.0
#7 lore raw

Re: [PATCH 5/5] xen/arm: address violations of MISRA C:2012 Rule 11.8

Hi,

Quoted 23 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

Add or amend casts to comply with Rule 11.8.

The violations are resolved either:
- by adding a missing const qualifier in the cast
- by removing a cast to non-const on a const-qualified object

No functional change.

---
  xen/arch/arm/domain_build.c       | 2 +-
  xen/arch/arm/include/asm/atomic.h | 2 +-
  xen/arch/arm/include/asm/regs.h   | 2 +-
  3 files changed, 3 insertions(+), 3 deletions(-)
xen/arch/arm/domain_build.c +0 -0
index 2dd2926b41..c17214f738 100644

xen/arch/arm/domain_build.c static int __init handle_passthrough_prop(struct kernel_info *kinfo, · line 2752

Quoted 5 lines
xen/arch/arm/domain_build.c +1 -1
@@ -2749,7 +2749,7 @@ static int __init handle_passthrough_prop(struct kernel_info *kinfo,
      if ( node == NULL )
      {
          printk(XENLOG_ERR "Couldn't find node %s in host_dt!\n",
-               (char *)xen_path->data);
+               xen_path->data);

I am a little bit puzzled why the cast was originally added here. Stefano, do you remember?

Also, this hunk will not apply on staging (the code has moved to dom0less-build.c). This will want a new version.

Quoted 21 lines
xen/arch/arm/domain_build.c +0 -0
          return -EINVAL;
      }
 
xen/arch/arm/include/asm/atomic.h +1 -1
index 64314d59b3..517216d2a8 100644
@@ -154,7 +154,7 @@ static always_inline void write_atomic_size(volatile void *p,
   */
  static inline int atomic_read(const atomic_t *v)
  {
-    return *(volatile int *)&v->counter;
+    return *(const volatile int *)&v->counter;
  }
 
  static inline int _atomic_read(atomic_t v)
xen/arch/arm/include/asm/regs.h +0 -0
index 8a0db95415..79050937f3 100644

xen/arch/arm/include/asm/regs.h static inline bool regs_mode_is_32bit(const struct cpu_user_regs *regs) · line 51

Quoted 5 lines
xen/arch/arm/include/asm/regs.h +1 -1
@@ -48,7 +48,7 @@ static inline bool regs_mode_is_32bit(const struct cpu_user_regs *regs)
 
  static inline bool guest_mode(const struct cpu_user_regs *r)
  {
-    unsigned long diff = (char *)guest_cpu_user_regs() - (char *)(r);
+    unsigned long diff = (char *)guest_cpu_user_regs() - (const char *)(r);

NIT: I would take the opportunity to use 'const char*' for the first one as well.

xen/arch/arm/include/asm/regs.h static inline bool regs_mode_is_32bit(const struct cpu_user_regs *regs) · line 54

Quoted 3 lines
xen/arch/arm/include/asm/regs.h +0 -0
      /* Frame pointer must point into current CPU stack. */
      ASSERT(diff < STACK_SIZE);
      /* If not a guest frame, it must be a hypervisor frame. */

Cheers,

Signature · 1 line
Julien Grall
#8 lore raw

Re: [PATCH 5/5] xen/arm: address violations of MISRA C:2012 Rule 11.8

Quoted 5 lines · Julien Grall
Quoted 4 lines · Simone Ballarin
xen/arch/arm/include/asm/regs.h +0 -0
@@ -48,7 +48,7 @@ static inline bool regs_mode_is_32bit(const struct cpu_user_regs *regs)
 
Quoted 7 lines

xen/arch/arm/include/asm/regs.h static inline bool regs_mode_is_32bit(const struct cpu_user_regs *regs) · line 51

Quoted 4 lines
xen/arch/arm/include/asm/regs.h +1 -1
  static inline bool guest_mode(const struct cpu_user_regs *r)
  {
-    unsigned long diff = (char *)guest_cpu_user_regs() - (char *)(r);
+    unsigned long diff = (char *)guest_cpu_user_regs() - (const char *)(r);

NIT: I would take the opportunity to use 'const char*' for the first one as well.

Considering a similar change likely is being (or is to be) done on x86, I'd favor not abusing "char *" here. Either use "void *", or - considering the intended result type - go straight to "unsigned long" (or uintptr_t). I would certainly ask for that on the x86 side.

Jan

#9 lore raw
In reply to Simone Ballarin
Quoted 6 lines · Simone Ballarin

Maria Celeste Cesario (5):
x86/platform_hypercall: address violations of MISRA C:2012 Rule 11.8 x86/boot/reloc: address violations of MISRA C:2012 Rule 11.8 AMD/IOMMU: address violations of MISRA C:2012 Rule 11.8 x86/atomic: address violations of MISRA C:2012 Rule 11.8 xen/arm: address violations of MISRA C:2012 Rule 11.8

Patches 1,2,4 Acked-by: Andrew Cooper <[email protected]>

Patch 3 I'm happy to fix on commit if you're happy with the suggestion.

~Andrew

#10 lore raw
Quoted 11 lines · Andrew Cooper
Quoted 6 lines · Simone Ballarin

Maria Celeste Cesario (5):
x86/platform_hypercall: address violations of MISRA C:2012 Rule 11.8 x86/boot/reloc: address violations of MISRA C:2012 Rule 11.8 AMD/IOMMU: address violations of MISRA C:2012 Rule 11.8 x86/atomic: address violations of MISRA C:2012 Rule 11.8 xen/arm: address violations of MISRA C:2012 Rule 11.8

Patches 1,2,4 Acked-by: Andrew Cooper <[email protected]>

Patch 3 I'm happy to fix on commit if you're happy with the suggestion.

Yes, the change is fine. Thanks!

Quoted 3 lines

~Andrew

Signature · 3 lines
Simone Ballarin, M.Sc.

Field Application Engineer, BUGSENG (https://bugseng.com)