Skip to contentSkip to conversation

xen: address violations of MISRA C:2012 Rule 11.8

PATCH Superseded Simone Ballarin 3 participants 32 messages Dec 14, 2023 – Dec 19, 2023

v1 of 2 Superseded Latest is v2 →

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 contain 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 and volatile qualifiers.

Example:

static always_inline bool int_##name(volatile void *p) {
volatile uint32_t *ptr = (uint32_t *)p; /* Non-compliant */
volatile uint32_t *ptr = (volatile uint32_t *)p; /* Compliant, proposed change */
}

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

Deviate the following violations:

- removal of const qualifier to comply with function signature It is safe to cast away const qualifiers to comply with function signature if the function does not modify the pointee. A SAF-3-safe comment was added to deviate this violation.

- violations in macro container_of
Violations caused by this macro are due to pointer arithmetic operations with the provided offset. The resulting pointer is then immediately cast back to its original type, which preserves the qualifier. This use can be deemed as safe.

- violations in function ERR_CAST
This function is made to explicitly cast an error-valued pointer to a void pointer type to make it clear that's what's going on, so the violation is deliberate.

Maria Celeste Cesario (9):
xen/arm64: address violations of MISRA C:2012 Rule 11.8 xen/arm: address violations of MISRA C:2012 Rule 11.8 xen/efi: address violations of MISRA C:2012 Rule 11.8
ACPI: address violations of MISRA C:2012 Rule 11.8 xen/x86: address violations of MISRA C:2012 Rule 11.8 xen/ppc: address violations of MISRA C:2012 Rule 11.8. x86/hvm: address violations of MISRA C:2012 Rule 11.8
xen: add deviations for Rule 11.8
xen: add SAF deviation for safe cast removal.

 automation/eclair_analysis/ECLAIR/deviations.ecl | 12 ++++++++++++
 docs/misra/deviations.rst                        | 13 +++++++++++++
 docs/misra/safe.json                             |  8 ++++++++
 xen/arch/arm/arm64/lib/bitops.c                  |  6 ++++--
 xen/arch/arm/bootfdt.c                           |  6 +++---
 xen/arch/arm/include/asm/alternative.h           |  2 +-
 xen/arch/arm/include/asm/arm64/cmpxchg.h         | 10 +++++-----
 xen/arch/ppc/include/asm/atomic.h                |  2 +-
 xen/arch/x86/boot/reloc.c                        |  4 ++--
 xen/arch/x86/hvm/hvm.c                           |  3 ++-
 xen/common/efi/boot.c                            |  6 +++---
 xen/common/version.c                             |  2 +-
 xen/include/acpi/acmacros.h                      |  2 +-
 13 files changed, 56 insertions(+), 20 deletions(-)
Signature · 1 line
2.40.0
#1 lore raw

PATCH 1/9 xen/arm64: address violations of MISRA C:2012 Rule 11.8

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Add volatile qualifiers missing in casts. Arguments p and ptr are originally volatile-qualified. There's no reason to drop the qualifiers.
No functional change.

---
 xen/arch/arm/arm64/lib/bitops.c          |  6 ++++--
 xen/arch/arm/include/asm/arm64/cmpxchg.h | 10 +++++-----
 2 files changed, 9 insertions(+), 7 deletions(-)
xen/arch/arm/arm64/lib/bitops.c +4 -2
index 20e3f3d6ce..275a780329 100644
@@ -32,7 +32,8 @@
 static always_inline bool int_##name(int nr, volatile void *p, bool timeout,\
                                      unsigned int max_try)                  \
 {                                                                           \
-    volatile uint32_t *ptr = (uint32_t *)p + BITOP_WORD((unsigned int)nr);  \
+    volatile uint32_t *ptr = (volatile uint32_t *)p +                       \
+                             BITOP_WORD((unsigned int)nr);                  \
     const uint32_t mask = BITOP_MASK((unsigned int)nr);                     \
     unsigned long res, tmp;                                                 \
                                                                             \
@@ -67,7 +68,8 @@ bool name##_timeout(int nr, volatile void *p, unsigned int max_try)         \
 static always_inline bool int_##name(int nr, volatile void *p, int *oldbit, \
                                      bool timeout, unsigned int max_try)    \
 {                                                                           \
-    volatile uint32_t *ptr = (uint32_t *)p + BITOP_WORD((unsigned int)nr);  \
+    volatile uint32_t *ptr = (volatile uint32_t *)p +                       \
+                             BITOP_WORD((unsigned int)nr);                  \
     unsigned int bit = (unsigned int)nr % BITOP_BITS_PER_WORD;              \
     const uint32_t mask = BITOP_MASK(bit);                                  \
     unsigned long res, tmp;                                                 \
xen/arch/arm/include/asm/arm64/cmpxchg.h +5 -5
index dbfaf91567..031fa6d92a 100644
@@ -13,7 +13,7 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 		"1:	ldxrb	%w0, %2\n"
 		"	stlxrb	%w1, %w3, %2\n"
 		"	cbnz	%w1, 1b\n"
-			: "=&r" (ret), "=&r" (tmp), "+Q" (*(u8 *)ptr)
+			: "=&r" (ret), "=&r" (tmp), "+Q" (*(volatile u8 *)ptr)
 			: "r" (x)
 			: "memory");
 		break;
@@ -22,7 +22,7 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 		"1:	ldxrh	%w0, %2\n"
 		"	stlxrh	%w1, %w3, %2\n"
 		"	cbnz	%w1, 1b\n"
-			: "=&r" (ret), "=&r" (tmp), "+Q" (*(u16 *)ptr)
+			: "=&r" (ret), "=&r" (tmp), "+Q" (*(volatile u16 *)ptr)
 			: "r" (x)
 			: "memory");
 		break;
@@ -31,7 +31,7 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 		"1:	ldxr	%w0, %2\n"
 		"	stlxr	%w1, %w3, %2\n"
 		"	cbnz	%w1, 1b\n"
-			: "=&r" (ret), "=&r" (tmp), "+Q" (*(u32 *)ptr)
+			: "=&r" (ret), "=&r" (tmp), "+Q" (*(volatile u32 *)ptr)
 			: "r" (x)
 			: "memory");
 		break;
@@ -40,7 +40,7 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 		"1:	ldxr	%0, %2\n"
 		"	stlxr	%w1, %3, %2\n"
 		"	cbnz	%w1, 1b\n"
-			: "=&r" (ret), "=&r" (tmp), "+Q" (*(u64 *)ptr)
+			: "=&r" (ret), "=&r" (tmp), "+Q" (*(volatile u64 *)ptr)
 			: "r" (x)
 			: "memory");
 		break;
@@ -82,7 +82,7 @@ static inline bool __cmpxchg_case_##name(volatile void *ptr,		\
 		"	stxr" #sz "	%w0, %" #w "4, %2\n"		\
 		"1:\n"							\
 		: "=&r" (res), "=&r" (oldval),				\
-		  "+Q" (*(unsigned long *)ptr)				\
+		  "+Q" (*(volatile unsigned long *)ptr)	\
 		: "Ir" (*old), "r" (new)				\
 		: "cc");						\
 									\
Signature · 1 line
2.40.0
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] xen/arm64: address violations of MISRA C:2012 Rule 11.8

Quoted 7 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Quoted 7 lines

Add volatile qualifiers missing in casts. Arguments p and ptr are originally volatile-qualified. There's no reason to drop the qualifiers.
No functional change.

Quoted 80 lines
 xen/arch/arm/arm64/lib/bitops.c          |  6 ++++--
 xen/arch/arm/include/asm/arm64/cmpxchg.h | 10 +++++-----
 2 files changed, 9 insertions(+), 7 deletions(-)
xen/arch/arm/arm64/lib/bitops.c +4 -2
index 20e3f3d6ce..275a780329 100644
@@ -32,7 +32,8 @@
 static always_inline bool int_##name(int nr, volatile void *p, bool timeout,\
                                      unsigned int max_try)                  \
 {                                                                           \
-    volatile uint32_t *ptr = (uint32_t *)p + BITOP_WORD((unsigned int)nr);  \
+    volatile uint32_t *ptr = (volatile uint32_t *)p +                       \
+                             BITOP_WORD((unsigned int)nr);                  \
     const uint32_t mask = BITOP_MASK((unsigned int)nr);                     \
     unsigned long res, tmp;                                                 \
                                                                             \
@@ -67,7 +68,8 @@ bool name##_timeout(int nr, volatile void *p, unsigned int max_try)         \
 static always_inline bool int_##name(int nr, volatile void *p, int *oldbit, \
                                      bool timeout, unsigned int max_try)    \
 {                                                                           \
-    volatile uint32_t *ptr = (uint32_t *)p + BITOP_WORD((unsigned int)nr);  \
+    volatile uint32_t *ptr = (volatile uint32_t *)p +                       \
+                             BITOP_WORD((unsigned int)nr);                  \
     unsigned int bit = (unsigned int)nr % BITOP_BITS_PER_WORD;              \
     const uint32_t mask = BITOP_MASK(bit);                                  \
     unsigned long res, tmp;                                                 \
xen/arch/arm/include/asm/arm64/cmpxchg.h +5 -5
index dbfaf91567..031fa6d92a 100644
@@ -13,7 +13,7 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 		"1:	ldxrb	%w0, %2\n"
 		"	stlxrb	%w1, %w3, %2\n"
 		"	cbnz	%w1, 1b\n"
-			: "=&r" (ret), "=&r" (tmp), "+Q" (*(u8 *)ptr)
+			: "=&r" (ret), "=&r" (tmp), "+Q" (*(volatile u8 *)ptr)
 			: "r" (x)
 			: "memory");
 		break;
@@ -22,7 +22,7 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 		"1:	ldxrh	%w0, %2\n"
 		"	stlxrh	%w1, %w3, %2\n"
 		"	cbnz	%w1, 1b\n"
-			: "=&r" (ret), "=&r" (tmp), "+Q" (*(u16 *)ptr)
+			: "=&r" (ret), "=&r" (tmp), "+Q" (*(volatile u16 *)ptr)
 			: "r" (x)
 			: "memory");
 		break;
@@ -31,7 +31,7 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 		"1:	ldxr	%w0, %2\n"
 		"	stlxr	%w1, %w3, %2\n"
 		"	cbnz	%w1, 1b\n"
-			: "=&r" (ret), "=&r" (tmp), "+Q" (*(u32 *)ptr)
+			: "=&r" (ret), "=&r" (tmp), "+Q" (*(volatile u32 *)ptr)
 			: "r" (x)
 			: "memory");
 		break;
@@ -40,7 +40,7 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 		"1:	ldxr	%0, %2\n"
 		"	stlxr	%w1, %3, %2\n"
 		"	cbnz	%w1, 1b\n"
-			: "=&r" (ret), "=&r" (tmp), "+Q" (*(u64 *)ptr)
+			: "=&r" (ret), "=&r" (tmp), "+Q" (*(volatile u64 *)ptr)
 			: "r" (x)
 			: "memory");
 		break;
@@ -82,7 +82,7 @@ static inline bool __cmpxchg_case_##name(volatile void *ptr,		\
 		"	stxr" #sz "	%w0, %" #w "4, %2\n"		\
 		"1:\n"							\
 		: "=&r" (res), "=&r" (oldval),				\
-		  "+Q" (*(unsigned long *)ptr)				\
+		  "+Q" (*(volatile unsigned long *)ptr)	\
 		: "Ir" (*old), "r" (new)				\
 		: "cc");						\
 									\
Signature · 1 line
2.40.0
#3 lore raw

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

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Add missing const qualifiers in casts.
The variables are originally const-qualified.
There's no reason to drop the qualifiers.
No functional change.

---
 xen/arch/arm/bootfdt.c                 | 6 +++---
 xen/arch/arm/include/asm/alternative.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
xen/arch/arm/bootfdt.c +3 -3
index f496a8cf94..e7948ea3e8 100644
@@ -377,7 +377,7 @@ static int __init process_chosen_node(const void *fdt, int node,
         printk("linux,initrd-start property has invalid length %d\n", len);
         return -EINVAL;
     }
-    start = dt_read_paddr((void *)&prop->data, dt_size_to_cells(len));
+    start = dt_read_paddr((const void *)&prop->data, dt_size_to_cells(len));
 
     prop = fdt_get_property(fdt, node, "linux,initrd-end", &len);
     if ( !prop )
@@ -390,7 +390,7 @@ static int __init process_chosen_node(const void *fdt, int node,
         printk("linux,initrd-end property has invalid length %d\n", len);
         return -EINVAL;
     }
-    end = dt_read_paddr((void *)&prop->data, dt_size_to_cells(len));
+    end = dt_read_paddr((const void *)&prop->data, dt_size_to_cells(len));
 
     if ( start >= end )
     {
@@ -541,7 +541,7 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
 
     add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
 
-    ret = device_tree_for_each_node((void *)fdt, 0, early_scan_node, NULL);
+    ret = device_tree_for_each_node((const void *)fdt, 0, early_scan_node, NULL);
     if ( ret )
         panic("Early FDT parsing failed (%d)\n", ret);
 
xen/arch/arm/include/asm/alternative.h +1 -1
index d3210e82f9..c7632668b6 100644
@@ -20,7 +20,7 @@ struct alt_instr {
 };
 
 /* Xen: helpers used by common code. */
-#define __ALT_PTR(a,f)		((void *)&(a)->f + (a)->f)
+#define __ALT_PTR(a,f)		((const void *)&(a)->f + (a)->f)
 #define ALT_ORIG_PTR(a)		__ALT_PTR(a, orig_offset)
 #define ALT_REPL_PTR(a)		__ALT_PTR(a, repl_offset)
 
Signature · 1 line
2.40.0
#4 lore raw

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

Quoted 42 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Add missing const qualifiers in casts.
The variables are originally const-qualified.
There's no reason to drop the qualifiers.
No functional change.

---
 xen/arch/arm/bootfdt.c                 | 6 +++---
 xen/arch/arm/include/asm/alternative.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
xen/arch/arm/bootfdt.c +2 -2
index f496a8cf94..e7948ea3e8 100644
@@ -377,7 +377,7 @@ static int __init process_chosen_node(const void *fdt, int node,
         printk("linux,initrd-start property has invalid length %d\n", len);
         return -EINVAL;
     }
-    start = dt_read_paddr((void *)&prop->data, dt_size_to_cells(len));
+    start = dt_read_paddr((const void *)&prop->data, dt_size_to_cells(len));
 
     prop = fdt_get_property(fdt, node, "linux,initrd-end", &len);
     if ( !prop )
@@ -390,7 +390,7 @@ static int __init process_chosen_node(const void *fdt, int node,
         printk("linux,initrd-end property has invalid length %d\n", len);
         return -EINVAL;
     }
-    end = dt_read_paddr((void *)&prop->data, dt_size_to_cells(len));
+    end = dt_read_paddr((const void *)&prop->data, dt_size_to_cells(len));
 
     if ( start >= end )
     {

xen/arch/arm/bootfdt.c size_t __init boot_fdt_info(const void *fdt, paddr_t paddr) · line 546

Quoted 7 lines
xen/arch/arm/bootfdt.c +1 -1
@@ -541,7 +541,7 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
 
     add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
 
-    ret = device_tree_for_each_node((void *)fdt, 0, early_scan_node, NULL);
+    ret = device_tree_for_each_node((const void *)fdt, 0, early_scan_node, NULL);
     if ( ret )
         panic("Early FDT parsing failed (%d)\n", ret);

This cast can be dropped. The rest looks OK.

Quoted 16 lines
xen/arch/arm/include/asm/alternative.h +1 -1
index d3210e82f9..c7632668b6 100644
@@ -20,7 +20,7 @@ struct alt_instr {
 };
 
 /* Xen: helpers used by common code. */
-#define __ALT_PTR(a,f)		((void *)&(a)->f + (a)->f)
+#define __ALT_PTR(a,f)		((const void *)&(a)->f + (a)->f)
 #define ALT_ORIG_PTR(a)		__ALT_PTR(a, orig_offset)
 #define ALT_REPL_PTR(a)		__ALT_PTR(a, repl_offset)
 
Signature · 1 line
2.40.0
#5 lore raw

PATCH 3/9 xen/efi: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Add missing const qualifiers in casts.
The variables are originally const-qualified.
There's no reason to drop the qualifiers.

---
 xen/common/efi/boot.c | 6 +++---
 xen/common/version.c  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
xen/common/efi/boot.c +3 -3
index 6110819918..efbec00af9 100644
@@ -1248,10 +1248,10 @@ static void __init efi_exit_boot(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *Syste
 #endif
 
     /* Adjust pointers into EFI. */
-    efi_ct = (void *)efi_ct + DIRECTMAP_VIRT_START;
-    efi_rs = (void *)efi_rs + DIRECTMAP_VIRT_START;
+    efi_ct = (const void *)efi_ct + DIRECTMAP_VIRT_START;
+    efi_rs = (const void *)efi_rs + DIRECTMAP_VIRT_START;
     efi_memmap = (void *)efi_memmap + DIRECTMAP_VIRT_START;
-    efi_fw_vendor = (void *)efi_fw_vendor + DIRECTMAP_VIRT_START;
+    efi_fw_vendor = (const void *)efi_fw_vendor + DIRECTMAP_VIRT_START;
 }
 
 /* SAF-1-safe */
xen/common/version.c +1 -1
index d320135208..6ac5a52700 100644
@@ -178,7 +178,7 @@ void __init xen_build_init(void)
     if ( &n[1] >= __note_gnu_build_id_end )
         return;
 
-    sz = (void *)__note_gnu_build_id_end - (void *)n;
+    sz = (const void *)__note_gnu_build_id_end - (const void *)n;
 
     rc = xen_build_id_check(n, sz, &build_id_p, &build_id_len);
 
Signature · 1 line
2.40.0
Reviewed-by on v1 3/9
#6 lore raw

Re: [PATCH 3/9] xen/efi: address violations of MISRA C:2012 Rule 11.8

xen/common/version.c void __init xen_build_init(void) · line 184

Quoted 11 lines · Simone Ballarin
xen/common/version.c +1 -1
@@ -178,7 +178,7 @@ void __init xen_build_init(void)
     if ( &n[1] >= __note_gnu_build_id_end )
         return;
 
-    sz = (void *)__note_gnu_build_id_end - (void *)n;
+    sz = (const void *)__note_gnu_build_id_end - (const void *)n;
 
     rc = xen_build_id_check(n, sz, &build_id_p, &build_id_len);
 

How does this change fit the subject? I'm also inclined to ask that these casts be changed to unsigned long or uintptr_t.

For the actual EFI change:

Jan

#7 lore raw

Re: [PATCH 3/9] xen/efi: address violations of MISRA C:2012 Rule 11.8

Quoted 16 lines · Jan Beulich

xen/common/version.c void __init xen_build_init(void) · line 184

Quoted 11 lines · Simone Ballarin
xen/common/version.c +1 -1
@@ -178,7 +178,7 @@ void __init xen_build_init(void)
      if ( &n[1] >= __note_gnu_build_id_end )
          return;
 
-    sz = (void *)__note_gnu_build_id_end - (void *)n;
+    sz = (const void *)__note_gnu_build_id_end - (const void *)n;
 
      rc = xen_build_id_check(n, sz, &build_id_p, &build_id_len);
 

How does this change fit the subject? I'm also inclined to ask that these casts be changed to unsigned long or uintptr_t.

Quoted 5 lines

For the actual EFI change:

Jan

Ok, v2 will use uintptr_t.

Signature · 3 lines
Simone Ballarin, M.Sc.

Field Application Engineer, BUGSENG (https://bugseng.com)
Reviewed-by on v1 3/9
#8 lore raw

Re: [PATCH 3/9] xen/efi: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin
Quoted 7 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Quoted 6 lines

Add missing const qualifiers in casts.
The variables are originally const-qualified.
There's no reason to drop the qualifiers.

Quoted 39 lines
---
 xen/common/efi/boot.c | 6 +++---
 xen/common/version.c  | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
xen/common/efi/boot.c +3 -3
index 6110819918..efbec00af9 100644
@@ -1248,10 +1248,10 @@ static void __init efi_exit_boot(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *Syste
 #endif
 
     /* Adjust pointers into EFI. */
-    efi_ct = (void *)efi_ct + DIRECTMAP_VIRT_START;
-    efi_rs = (void *)efi_rs + DIRECTMAP_VIRT_START;
+    efi_ct = (const void *)efi_ct + DIRECTMAP_VIRT_START;
+    efi_rs = (const void *)efi_rs + DIRECTMAP_VIRT_START;
     efi_memmap = (void *)efi_memmap + DIRECTMAP_VIRT_START;
-    efi_fw_vendor = (void *)efi_fw_vendor + DIRECTMAP_VIRT_START;
+    efi_fw_vendor = (const void *)efi_fw_vendor + DIRECTMAP_VIRT_START;
 }
 
 /* SAF-1-safe */
xen/common/version.c +1 -1
index d320135208..6ac5a52700 100644
@@ -178,7 +178,7 @@ void __init xen_build_init(void)
     if ( &n[1] >= __note_gnu_build_id_end )
         return;
 
-    sz = (void *)__note_gnu_build_id_end - (void *)n;
+    sz = (const void *)__note_gnu_build_id_end - (const void *)n;
 
     rc = xen_build_id_check(n, sz, &build_id_p, &build_id_len);
 
Signature · 1 line
2.40.0
#9 lore raw

PATCH 4/9 ACPI: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Add missing const qualifiers missing in casting.
There's no reason to drop the const qualifier in ACPI_COMPARE_NAME since the macro arguments are not modified in its body.

---
 xen/include/acpi/acmacros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/acpi/acmacros.h +1 -1
index 86c503c20f..d7c74c5769 100644
@@ -116,7 +116,7 @@
 #define ACPI_PTR_TO_PHYSADDR(i)         ACPI_TO_INTEGER(i)
 
 #ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED
-#define ACPI_COMPARE_NAME(a,b)          (*ACPI_CAST_PTR (u32,(a)) == *ACPI_CAST_PTR (u32,(b)))
+#define ACPI_COMPARE_NAME(a,b)          (*ACPI_CAST_PTR (const u32,(a)) == *ACPI_CAST_PTR (const u32,(b)))
 #else
 #define ACPI_COMPARE_NAME(a,b)          (!ACPI_STRNCMP (ACPI_CAST_PTR (char,(a)), ACPI_CAST_PTR (char,(b)), ACPI_NAME_SIZE))
 #endif
Signature · 1 line
2.40.0
#10 lore raw

Re: [PATCH 4/9] ACPI: address violations of MISRA C:2012 Rule 11.8

xen/include/acpi/acmacros.h line 119

Quoted 8 lines · Simone Ballarin
xen/include/acpi/acmacros.h +1 -1
@@ -116,7 +116,7 @@
 #define ACPI_PTR_TO_PHYSADDR(i)         ACPI_TO_INTEGER(i)
 
 #ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED
-#define ACPI_COMPARE_NAME(a,b)          (*ACPI_CAST_PTR (u32,(a)) == *ACPI_CAST_PTR (u32,(b)))
+#define ACPI_COMPARE_NAME(a,b)          (*ACPI_CAST_PTR (const u32,(a)) == *ACPI_CAST_PTR (const u32,(b)))

Hmm, I'm a little hesitant to take changes to this header. We've inherited it from Linux, who in turn inherited / imported it from ACPI CA.

xen/include/acpi/acmacros.h line 122

Quoted 3 lines
xen/include/acpi/acmacros.h +0 -0
 #else
 #define ACPI_COMPARE_NAME(a,b)          (!ACPI_STRNCMP (ACPI_CAST_PTR (char,(a)), ACPI_CAST_PTR (char,(b)), ACPI_NAME_SIZE))
 #endif

What about this alternative code, btw?

Jan

#11 lore raw

Re: [PATCH 4/9] ACPI: address violations of MISRA C:2012 Rule 11.8

Quoted 14 lines · Jan Beulich

xen/include/acpi/acmacros.h line 119

Quoted 8 lines · Simone Ballarin
xen/include/acpi/acmacros.h +1 -1
@@ -116,7 +116,7 @@
  #define ACPI_PTR_TO_PHYSADDR(i)         ACPI_TO_INTEGER(i)
 
  #ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED
-#define ACPI_COMPARE_NAME(a,b)          (*ACPI_CAST_PTR (u32,(a)) == *ACPI_CAST_PTR (u32,(b)))
+#define ACPI_COMPARE_NAME(a,b)          (*ACPI_CAST_PTR (const u32,(a)) == *ACPI_CAST_PTR (const u32,(b)))

Hmm, I'm a little hesitant to take changes to this header. We've inherited it from Linux, who in turn inherited / imported it from ACPI CA.

Quoted 8 lines

xen/include/acpi/acmacros.h line 122

Quoted 3 lines
xen/include/acpi/acmacros.h +0 -0
  #else
  #define ACPI_COMPARE_NAME(a,b)          (!ACPI_STRNCMP (ACPI_CAST_PTR (char,(a)), ACPI_CAST_PTR (char,(b)), ACPI_NAME_SIZE))
  #endif

What about this alternative code, btw?

Jan

If the file is not supposed to be changed, I would consider adding it in docs/misra/exclude-list*.
For the moment, I think it is better to drop the change.

Signature · 3 lines
Simone Ballarin, M.Sc.

Field Application Engineer, BUGSENG (https://bugseng.com)
#12 lore raw

Re: [PATCH 4/9] ACPI: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin
Quoted 22 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Add missing const qualifiers missing in casting.
There's no reason to drop the const qualifier in ACPI_COMPARE_NAME since the macro arguments are not modified in its body.

---
 xen/include/acpi/acmacros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/acpi/acmacros.h +0 -0
index 86c503c20f..d7c74c5769 100644

xen/include/acpi/acmacros.h line 119

Quoted 5 lines
xen/include/acpi/acmacros.h +1 -1
@@ -116,7 +116,7 @@
 #define ACPI_PTR_TO_PHYSADDR(i)         ACPI_TO_INTEGER(i)
 
 #ifndef ACPI_MISALIGNMENT_NOT_SUPPORTED
-#define ACPI_COMPARE_NAME(a,b)          (*ACPI_CAST_PTR (u32,(a)) == *ACPI_CAST_PTR (u32,(b)))
+#define ACPI_COMPARE_NAME(a,b)          (*ACPI_CAST_PTR (const u32,(a)) == *ACPI_CAST_PTR (const u32,(b)))

I am a bit confused but this one. The implementation of ACPI_CAST_PTR is:

#define ACPI_CAST_PTR(t, p) ((t *) (acpi_uintptr_t) (p))

The first cast to acpi_uintptr_t would already drop the const anyway?

xen/include/acpi/acmacros.h line 122

Quoted 3 lines
xen/include/acpi/acmacros.h +0 -0
 #else
 #define ACPI_COMPARE_NAME(a,b)          (!ACPI_STRNCMP (ACPI_CAST_PTR (char,(a)), ACPI_CAST_PTR (char,(b)), ACPI_NAME_SIZE))
 #endif

Would it make sense to also add the const here too if nothing else for consistency?

#13 lore raw

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

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Add missing const qualifiers in casts.

Macro get_mb2_data returns values that are const-qualified. The results are stored in const struct pointers, hence there's no need to cast away the const qualifiers.

---
 xen/arch/x86/boot/reloc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/boot/reloc.c +2 -2
index df0bc710a7..77fdb2be05 100644
@@ -300,8 +300,8 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out)
                 const struct vesa_mode_info *mi;
 
                 video = _p(video_out);
-                ci = (void *)get_mb2_data(tag, vbe, vbe_control_info);
-                mi = (void *)get_mb2_data(tag, vbe, vbe_mode_info);
+                ci = (const void *)get_mb2_data(tag, vbe, vbe_control_info);
+                mi = (const void *)get_mb2_data(tag, vbe, vbe_mode_info);
 
                 if ( ci->version >= 0x0200 && (mi->attrib & 0x9b) == 0x9b )
                 {
Signature · 1 line
2.40.0
Acked-by on v1 5/9
#14 lore raw

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

xen/arch/x86/boot/reloc.c static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out) · line 304

Quoted 10 lines · Simone Ballarin
xen/arch/x86/boot/reloc.c +2 -2
@@ -300,8 +300,8 @@ static multiboot_info_t *mbi2_reloc(uint32_t mbi_in, uint32_t video_out)
                 const struct vesa_mode_info *mi;
 
                 video = _p(video_out);
-                ci = (void *)get_mb2_data(tag, vbe, vbe_control_info);
-                mi = (void *)get_mb2_data(tag, vbe, vbe_mode_info);
+                ci = (const void *)get_mb2_data(tag, vbe, vbe_control_info);
+                mi = (const void *)get_mb2_data(tag, vbe, vbe_mode_info);

This wasn't and isn't very nice, but I also can't think of a way to improve it, so

Jan

Reviewed-by on v1 5/9
#15 lore raw

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

In reply to Simone Ballarin
Quoted 7 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Quoted 8 lines

Add missing const qualifiers in casts.

Macro get_mb2_data returns values that are const-qualified. The results are stored in const struct pointers, hence there's no need to cast away the const qualifiers.

#16 lore raw

PATCH 6/9 xen/ppc: address violations of MISRA C:2012 Rule 11.8.

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Fix violation by adding missing const qualifier in cast.

---
Adaptation requested by the community to make the code more consistent.

---
 xen/arch/ppc/include/asm/atomic.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/ppc/include/asm/atomic.h +1 -1
index 64168aa3f1..fe778579fb 100644
@@ -16,7 +16,7 @@
 
 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)
Signature · 1 line
2.40.0
Reviewed-by on v1 6/9
#17 lore raw

Re: [PATCH 6/9] xen/ppc: address violations of MISRA C:2012 Rule 11.8.

Quoted 11 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Fix violation by adding missing const qualifier in cast.

#18 lore raw

PATCH 7/9 x86/hvm: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Remove unnecessary cast.
from is a const-qualified pointer to void and the function hvm_copy_to_guest_linear requires a const void* type argument, therefore the cast to void* is not necessary.

No functional change.

---
 xen/arch/x86/hvm/hvm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/hvm/hvm.c +1 -1
index 35a30df3b1..523e0df57c 100644
@@ -3462,7 +3462,7 @@ unsigned int copy_to_user_hvm(void *to, const void *from, unsigned int len)
         return 0;
     }
 
-    rc = hvm_copy_to_guest_linear((unsigned long)to, (void *)from, len, 0, NULL);
+    rc = hvm_copy_to_guest_linear((unsigned long)to, from, len, 0, NULL);
     return rc ? len : 0; /* fake a copy_to_user() return code */
 }
 
Signature · 1 line
2.40.0
Reviewed-by on v1 7/9
#19 lore raw

Re: [PATCH 7/9] x86/hvm: address violations of MISRA C:2012 Rule 11.8

Quoted 7 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Quoted 8 lines

Remove unnecessary cast.
from is a const-qualified pointer to void and the function hvm_copy_to_guest_linear requires a const void* type argument, therefore the cast to void* is not necessary.

No functional change.

Reviewed-by on v1 7/9
#20 lore raw

Re: [PATCH 7/9] x86/hvm: address violations of MISRA C:2012 Rule 11.8

In reply to Simone Ballarin
Quoted 7 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Quoted 8 lines

Remove unnecessary cast.
from is a const-qualified pointer to void and the function hvm_copy_to_guest_linear requires a const void* type argument, therefore the cast to void* is not necessary.

No functional change.

#21 lore raw

PATCH 8/9 xen: add deviations for Rule 11.8

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

Deviate use of macro container_of.
Deviate use of function ERR_CAST.

---
container_of: Fixing this violation would require the declaration of a new
macro taking advantage of the return value of the ternary operator ?: :
its return value preserves qualifiers present on both expression, hence returning void* or qualified void* depending on pointer type, like in this macro:
'#define same_constness_void_ptr(ptr) ((typeof((false ? (void*) 1 : (ptr)))) (ptr))' The result could then be used with a Generic to avoid the cast.

---
 automation/eclair_analysis/ECLAIR/deviations.ecl | 12 ++++++++++++
 docs/misra/deviations.rst                        | 13 +++++++++++++
 2 files changed, 25 insertions(+)
automation/eclair_analysis/ECLAIR/deviations.ecl +12 -0
index 683f2bbfe8..d3e32a6ac8 100644
@@ -292,6 +292,18 @@ constant expressions are required.\""
 # Series 11
 #
 
+-doc_begin="Violations caused by container_of are due to pointer arithmetic operations
+with the provided offset. The resulting pointer is then immediately cast back to its
+original type, which preserves the qualifier. This use can be deemed as safe.
+Fixing this violation would require to increase code complexity and lower readability."
+-config=MC3R1.R11.8,reports+={safe,"any_area(any_loc(any_exp(macro(^container_of$))))"}
+-doc_end
+
+-doc_begin="This function is made to explicitly cast an error-valued pointer to a void
+pointer type to make it clear that's what's going on, so the violation is deliberate."
+-config=MC3R1.R11.8,reports+={deliberate,"all_area(context(^ERR_CAST\\(.*$))"}
+-doc_end
+
 -doc_begin="This construct is used to check if the type is scalar, and for this purpose the use of 0 as a null pointer constant is deliberate."
 -config=MC3R1.R11.9,reports+={deliberate, "any_area(any_loc(any_exp(macro(^__ACCESS_ONCE$))))"
 }
docs/misra/deviations.rst +13 -0
index eda3c8100c..98733d636d 100644
@@ -248,6 +248,19 @@ Deviations related to MISRA C:2012 Rules:
        If no bits are set, 0 is returned.
      - Tagged as `safe` for ECLAIR.
 
+   * - R11.8
+     - Violations caused by container_of are due to pointer arithmetic operations
+       with the provided offset. The resulting pointer is then immediately cast back to its
+       original type, which preserves the qualifier. This use can be deemed as safe.
+       Fixing this violation would require to increase code complexity and lower readability.
+     - Tagged as `safe` for ECLAIR.
+    
+   * - R11.8
+     - This function is made to explicitly cast an error-valued pointer to a
+       void pointer type to make it clear that's what's going on, so the
+       violation is deliberate.
+     - Tagged ad `deliberate` for ECLAIR.
+
    * - R11.9
      - __ACCESS_ONCE uses an integer, which happens to be zero, as a
        compile time check. The typecheck uses a cast. The usage of zero or other
Signature · 1 line
2.40.0
#22 lore raw

Re: [PATCH 8/9] xen: add deviations for Rule 11.8

Quoted 7 lines · Simone Ballarin
automation/eclair_analysis/ECLAIR/deviations.ecl +1 -0
@@ -292,6 +292,18 @@ constant expressions are required.\""
 # Series 11
 #
 
+-doc_begin="Violations caused by container_of are due to pointer arithmetic operations

automation/eclair_analysis/ECLAIR/deviations.ecl constant expressions are required.\"" · line 305

Quoted 10 lines
automation/eclair_analysis/ECLAIR/deviations.ecl +10 -0
+with the provided offset. The resulting pointer is then immediately cast back to its
+original type, which preserves the qualifier. This use can be deemed as safe.
+Fixing this violation would require to increase code complexity and lower readability."
+-config=MC3R1.R11.8,reports+={safe,"any_area(any_loc(any_exp(macro(^container_of$))))"}
+-doc_end
+
+-doc_begin="This function is made to explicitly cast an error-valued pointer to a void
+pointer type to make it clear that's what's going on, so the violation is deliberate."
+-config=MC3R1.R11.8,reports+={deliberate,"all_area(context(^ERR_CAST\\(.*$))"}
+-doc_end

While the wording is okay here, ...

Quoted 8 lines
docs/misra/deviations.rst +2 -0
@@ -248,6 +248,19 @@ Deviations related to MISRA C:2012 Rules:
        If no bits are set, 0 is returned.
      - Tagged as `safe` for ECLAIR.
 
+   * - R11.8
+     - Violations caused by container_of are due to pointer arithmetic operations

docs/misra/deviations.rst Deviations related to MISRA C:2012 Rules: · line 262

Quoted 10 lines
docs/misra/deviations.rst +10 -0
+       with the provided offset. The resulting pointer is then immediately cast back to its
+       original type, which preserves the qualifier. This use can be deemed as safe.
+       Fixing this violation would require to increase code complexity and lower readability.
+     - Tagged as `safe` for ECLAIR.
+    
+   * - R11.8
+     - This function is made to explicitly cast an error-valued pointer to a
+       void pointer type to make it clear that's what's going on, so the
+       violation is deliberate.
+     - Tagged ad `deliberate` for ECLAIR.

... "This function" here is entirely unclear. However, seeing ERR_CAST() has no users, wouldn't we be better off simply removing it? (Otherwise, nit:
"Tagged as ...".)

Jan

#23 lore raw

PATCH 9/9 xen: add SAF deviation for safe cast removal.

In reply to Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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".

In function __hvm_copy, the const qualifier is cast away to comply with its function signature. There's no modification of the pointee during its execution, therefore its use can be deemed as safe.

---
 docs/misra/safe.json   | 8 ++++++++
 xen/arch/x86/hvm/hvm.c | 1 +
 2 files changed, 9 insertions(+)
docs/misra/safe.json +8 -0
index 952324f85c..e748bc6cf5 100644
@@ -28,6 +28,14 @@
         },
         {
             "id": "SAF-3-safe",
+            "analyser": {
+                "eclair": "MC3R1.R11.8"
+            },
+            "name": "MC3R1.R11.8: removal of const qualifier to comply with function signature",
+            "text": "It is safe to cast away const qualifiers to comply with function signature if the function does not modify the pointee."
+        },
+        {
+            "id": "SAF-4-safe",
             "analyser": {},
             "name": "Sentinel",
             "text": "Next ID to be used"
xen/arch/x86/hvm/hvm.c +1 -0
index 523e0df57c..414853254f 100644
@@ -3413,6 +3413,7 @@ static enum hvm_translation_result __hvm_copy(
 enum hvm_translation_result hvm_copy_to_guest_phys(
     paddr_t paddr, const void *buf, unsigned int size, struct vcpu *v)
 {
+    /* SAF-3-safe */
     return __hvm_copy((void *)buf /* HVMCOPY_to_guest doesn't modify */,
                       paddr, size, v,
                       HVMCOPY_to_guest | HVMCOPY_phys, 0, NULL);
Signature · 1 line
2.40.0
#24 lore raw

Re: [PATCH 9/9] xen: add SAF deviation for safe cast removal.

docs/misra/safe.json line 35

Quoted 11 lines · Simone Ballarin
docs/misra/safe.json +5 -0
@@ -28,6 +28,14 @@
         },
         {
             "id": "SAF-3-safe",
+            "analyser": {
+                "eclair": "MC3R1.R11.8"
+            },
+            "name": "MC3R1.R11.8: removal of const qualifier to comply with function signature",
+            "text": "It is safe to cast away const qualifiers to comply with function signature if the function does not modify the pointee."

I'm not happy with this description, as it invites for all sorts of abuse. Yet I'm also puzzled that ...

xen/arch/x86/hvm/hvm.c static enum hvm_translation_result __hvm_copy( · line 3419

Quoted 10 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -3413,6 +3413,7 @@ static enum hvm_translation_result __hvm_copy(
 enum hvm_translation_result hvm_copy_to_guest_phys(
     paddr_t paddr, const void *buf, unsigned int size, struct vcpu *v)
 {
+    /* SAF-3-safe */
     return __hvm_copy((void *)buf /* HVMCOPY_to_guest doesn't modify */,
                       paddr, size, v,
                       HVMCOPY_to_guest | HVMCOPY_phys, 0, NULL);

... this is the only place you then use it. Afaict some of Arm's copy_guest() callers ought to have a similar issue. If so, an enlarged patch should be discussed with a larger audience, to see how we collectively think we want to put this specific kind of deviation.

Jan

#25 lore raw

Re: [PATCH 9/9] xen: add SAF deviation for safe cast removal.

Quoted 15 lines · Jan Beulich

docs/misra/safe.json line 35

Quoted 11 lines · Simone Ballarin
docs/misra/safe.json +5 -0
@@ -28,6 +28,14 @@
         },
         {
             "id": "SAF-3-safe",
+            "analyser": {
+                "eclair": "MC3R1.R11.8"
+            },
+            "name": "MC3R1.R11.8: removal of const qualifier to comply with function signature",
+            "text": "It is safe to cast away const qualifiers to comply with function signature if the function does not modify the pointee."

I'm not happy with this description, as it invites for all sorts of abuse. Yet I'm also puzzled that ...

We can improve the language but the concept would still be the same. For instance:

A single function might or might not modify the pointee depending on other function parameters (for instance a single function that could either read or write depending on how it is called). It is safe to cast away const qualifiers when passing a parameter to a function of this type when the other parameters are triggering a read-only operation.

Quoted 7 lines
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -3413,6 +3413,7 @@ static enum hvm_translation_result __hvm_copy(
 enum hvm_translation_result hvm_copy_to_guest_phys(
     paddr_t paddr, const void *buf, unsigned int size, struct vcpu *v)
 {
+    /* SAF-3-safe */
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static enum hvm_translation_result __hvm_copy( · line 3419

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
     return __hvm_copy((void *)buf /* HVMCOPY_to_guest doesn't modify */,
                       paddr, size, v,
                       HVMCOPY_to_guest | HVMCOPY_phys, 0, NULL);

... this is the only place you then use it. Afaict some of Arm's copy_guest() callers ought to have a similar issue. If so, an enlarged patch should be discussed with a larger audience, to see how we collectively think we want to put this specific kind of deviation.

We have a similar problem, see xen/arch/arm/guestcopy.c raw_copy_to_guest and raw_copy_from_guest.

I would use the SAF deviation there too.

In the case here, I think the comment "HVMCOPY_to_guest doesn't modify" could be removed as it becomes redundant with SAF-3-safe, but I'll leave it to you.

#26 lore raw

Re: [PATCH 9/9] xen: add SAF deviation for safe cast removal.

Quoted 17 lines · Stefano Stabellini
Quoted 15 lines · Jan Beulich

docs/misra/safe.json line 35

Quoted 11 lines · Simone Ballarin
docs/misra/safe.json +5 -0
@@ -28,6 +28,14 @@
         },
         {
             "id": "SAF-3-safe",
+            "analyser": {
+                "eclair": "MC3R1.R11.8"
+            },
+            "name": "MC3R1.R11.8: removal of const qualifier to comply with function signature",
+            "text": "It is safe to cast away const qualifiers to comply with function signature if the function does not modify the pointee."

I'm not happy with this description, as it invites for all sorts of abuse. Yet I'm also puzzled that ...

Quoted 8 lines

We can improve the language but the concept would still be the same. For instance:

A single function might or might not modify the pointee depending on other function parameters (for instance a single function that could either read or write depending on how it is called). It is safe to cast away const qualifiers when passing a parameter to a function of this type when the other parameters are triggering a read-only operation.

Right, but I think the next here needs to be setting as tight boundaries as possible: It should cover only this one specific pattern. Anything else would better get its own deviation, imo.

Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -3413,6 +3413,7 @@ static enum hvm_translation_result __hvm_copy(
 enum hvm_translation_result hvm_copy_to_guest_phys(
     paddr_t paddr, const void *buf, unsigned int size, struct vcpu *v)
 {
+    /* SAF-3-safe */
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static enum hvm_translation_result __hvm_copy( · line 3419

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
     return __hvm_copy((void *)buf /* HVMCOPY_to_guest doesn't modify */,
                       paddr, size, v,
                       HVMCOPY_to_guest | HVMCOPY_phys, 0, NULL);

... this is the only place you then use it. Afaict some of Arm's copy_guest() callers ought to have a similar issue. If so, an enlarged patch should be discussed with a larger audience, to see how we collectively think we want to put this specific kind of deviation.

Quoted 8 lines

We have a similar problem, see xen/arch/arm/guestcopy.c raw_copy_to_guest and raw_copy_from_guest.

I would use the SAF deviation there too.

In the case here, I think the comment "HVMCOPY_to_guest doesn't modify" could be removed as it becomes redundant with SAF-3-safe, but I'll leave it to you.

No, the comment cannot be removed: The SAF comment says exactly nothing until you go and look up its description. The two comments could be folded, though. Which is something I was trying to advocate for in
general: Unless entirely obvious, what exactly it is that is "safe" would better be (briefly) stated in these SAF comments.

Jan

#27 lore raw

Re: [PATCH 9/9] xen: add SAF deviation for safe cast removal.

Quoted 27 lines · Jan Beulich
Quoted 17 lines · Stefano Stabellini
Quoted 15 lines · Jan Beulich

docs/misra/safe.json line 35

Quoted 11 lines · Simone Ballarin
docs/misra/safe.json +5 -0
@@ -28,6 +28,14 @@
         },
         {
             "id": "SAF-3-safe",
+            "analyser": {
+                "eclair": "MC3R1.R11.8"
+            },
+            "name": "MC3R1.R11.8: removal of const qualifier to comply with function signature",
+            "text": "It is safe to cast away const qualifiers to comply with function signature if the function does not modify the pointee."

I'm not happy with this description, as it invites for all sorts of abuse. Yet I'm also puzzled that ...

Quoted 8 lines

We can improve the language but the concept would still be the same. For instance:

A single function might or might not modify the pointee depending on other function parameters (for instance a single function that could either read or write depending on how it is called). It is safe to cast away const qualifiers when passing a parameter to a function of this type when the other parameters are triggering a read-only operation.

Quoted 3 lines

Right, but I think the next here needs to be setting as tight boundaries as possible: It should cover only this one specific pattern. Anything else would better get its own deviation, imo.

OK. What about:

A single function might or might not modify the pointee depending on other function parameters, for instance a common pattern is to implement one function that could either read or write depending on how it is called. It is safe to cast away const qualifiers when passing a parameter to a function following this pattern when the other parameters are triggering a read-only operation.

Feel free to suggest a better wording.

Quoted 25 lines
Quoted 16 lines
Quoted 7 lines
Quoted 7 lines
xen/arch/x86/hvm/hvm.c +1 -0
@@ -3413,6 +3413,7 @@ static enum hvm_translation_result __hvm_copy(
 enum hvm_translation_result hvm_copy_to_guest_phys(
     paddr_t paddr, const void *buf, unsigned int size, struct vcpu *v)
 {
+    /* SAF-3-safe */
Quoted 8 lines

xen/arch/x86/hvm/hvm.c static enum hvm_translation_result __hvm_copy( · line 3419

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
     return __hvm_copy((void *)buf /* HVMCOPY_to_guest doesn't modify */,
                       paddr, size, v,
                       HVMCOPY_to_guest | HVMCOPY_phys, 0, NULL);

... this is the only place you then use it. Afaict some of Arm's copy_guest() callers ought to have a similar issue. If so, an enlarged patch should be discussed with a larger audience, to see how we collectively think we want to put this specific kind of deviation.

Quoted 8 lines

We have a similar problem, see xen/arch/arm/guestcopy.c raw_copy_to_guest and raw_copy_from_guest.

I would use the SAF deviation there too.

In the case here, I think the comment "HVMCOPY_to_guest doesn't modify" could be removed as it becomes redundant with SAF-3-safe, but I'll leave it to you.

Quoted 5 lines

No, the comment cannot be removed: The SAF comment says exactly nothing until you go and look up its description. The two comments could be folded, though. Which is something I was trying to advocate for in
general: Unless entirely obvious, what exactly it is that is "safe" would better be (briefly) stated in these SAF comments.

I agree with you

#28 lore raw

Re: [PATCH 9/9] xen: add SAF deviation for safe cast removal.

Quoted 34 lines · Stefano Stabellini
Quoted 27 lines · Jan Beulich
Quoted 17 lines · Stefano Stabellini
Quoted 15 lines · Jan Beulich

docs/misra/safe.json line 35

Quoted 11 lines · Simone Ballarin
docs/misra/safe.json +5 -0
@@ -28,6 +28,14 @@
         },
         {
             "id": "SAF-3-safe",
+            "analyser": {
+                "eclair": "MC3R1.R11.8"
+            },
+            "name": "MC3R1.R11.8: removal of const qualifier to comply with function signature",
+            "text": "It is safe to cast away const qualifiers to comply with function signature if the function does not modify the pointee."

I'm not happy with this description, as it invites for all sorts of abuse. Yet I'm also puzzled that ...

Quoted 8 lines

We can improve the language but the concept would still be the same. For instance:

A single function might or might not modify the pointee depending on other function parameters (for instance a single function that could either read or write depending on how it is called). It is safe to cast away const qualifiers when passing a parameter to a function of this type when the other parameters are triggering a read-only operation.

Quoted 3 lines

Right, but I think the next here needs to be setting as tight boundaries as possible: It should cover only this one specific pattern. Anything else would better get its own deviation, imo.

OK. What about:

Quoted 8 lines

A single function might or might not modify the pointee depending on other function parameters, for instance a common pattern is to implement one function that could either read or write depending on how it is called. It is safe to cast away const qualifiers when passing a parameter to a function following this pattern when the other parameters are triggering a read-only operation.

Feel free to suggest a better wording.

Well, my point was to get rid of "for instance" and "common pattern" (and anything alike). E.g.:

"A single function could either read or write through a passed in pointer,
depending on how it is called. It is deemed safe to cast away a const qualifier when passing a pointer to such a function, when the other parameters guarantee read-only operation."

Jan

#29 lore raw

Re: [PATCH 9/9] xen: add SAF deviation for safe cast removal.

Quoted 44 lines · Jan Beulich
Quoted 34 lines · Stefano Stabellini
Quoted 27 lines · Jan Beulich
Quoted 17 lines · Stefano Stabellini
Quoted 15 lines · Jan Beulich

docs/misra/safe.json line 35

Quoted 11 lines · Simone Ballarin
docs/misra/safe.json +5 -0
@@ -28,6 +28,14 @@
         },
         {
             "id": "SAF-3-safe",
+            "analyser": {
+                "eclair": "MC3R1.R11.8"
+            },
+            "name": "MC3R1.R11.8: removal of const qualifier to comply with function signature",
+            "text": "It is safe to cast away const qualifiers to comply with function signature if the function does not modify the pointee."

I'm not happy with this description, as it invites for all sorts of abuse. Yet I'm also puzzled that ...

Quoted 8 lines

We can improve the language but the concept would still be the same. For instance:

A single function might or might not modify the pointee depending on other function parameters (for instance a single function that could either read or write depending on how it is called). It is safe to cast away const qualifiers when passing a parameter to a function of this type when the other parameters are triggering a read-only operation.

Quoted 3 lines

Right, but I think the next here needs to be setting as tight boundaries as possible: It should cover only this one specific pattern. Anything else would better get its own deviation, imo.

OK. What about:

Quoted 8 lines

A single function might or might not modify the pointee depending on other function parameters, for instance a common pattern is to implement one function that could either read or write depending on how it is called. It is safe to cast away const qualifiers when passing a parameter to a function following this pattern when the other parameters are triggering a read-only operation.

Feel free to suggest a better wording.

Quoted 7 lines

Well, my point was to get rid of "for instance" and "common pattern" (and anything alike). E.g.:

"A single function could either read or write through a passed in pointer,
depending on how it is called. It is deemed safe to cast away a const qualifier when passing a pointer to such a function, when the other parameters guarantee read-only operation."

That's fine by me

#30 lore raw
In reply to Simone Ballarin
Quoted 10 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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 and volatile qualifiers.

Quoted 7 lines

Example:

static always_inline bool int_##name(volatile void *p) {
volatile uint32_t *ptr = (uint32_t *)p; /* Non-compliant */
volatile uint32_t *ptr = (volatile uint32_t *)p; /* Compliant, proposed change */
}

Why would you further complicate things when here the cast can simply be dropped?

Jan

#31 lore raw
Quoted 19 lines · Jan Beulich
Quoted 10 lines · Simone Ballarin

From: Maria Celeste Cesario <[email protected]>

The xen sources contain 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 and volatile qualifiers.

Quoted 7 lines

Example:

  static always_inline bool int_##name(volatile void *p)
  {
     volatile uint32_t *ptr = (uint32_t *)p;             /* Non-compliant */
     volatile uint32_t *ptr = (volatile uint32_t *)p;    /* Compliant, proposed change */
  }
Quoted 4 lines

Why would you further complicate things when here the cast can simply be dropped?

Jan

Of course, the example will be improved in v2.

Signature · 3 lines
Simone Ballarin, M.Sc.

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