Skip to contentSkip to conversation

xen: address MISRA C Rule 11.8 violations

PATCH In review Dmytro Prokopchuk1 4 participants 18 messages Nov 3 – Nov 19

lore
Share & tools
lore
#0 lore raw

cover xen: address MISRA C Rule 11.8 violations

This series addresses a number of violations of MISRA C:2012 Rule 11.8 ("A cast shall not remove any const or volatile qualification from the type pointed to by a pointer").

Patches 1-3 remove the offending casts.
Patch 4 deviates the casts in livepatch.

Test CI pipeline:
https://gitlab.com/xen-project/people/dimaprkp4k/xen/-/pipelines/3021645211

Dmytro Prokopchuk (4):
xen: make stop_machine_run() take const void *
xen/arm: acpi: resolve MISRA C Rule 11.8 in acpi_xsdt_modify_entry()
xen: resolve MISRA C Rule 11.8 in xen_build_id_check() automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

 automation/eclair_analysis/ECLAIR/deviations.ecl | 7 +++++++
 docs/misra/deviations.rst                        | 7 +++++++
 docs/misra/rules.rst                             | 3 ++-
 xen/arch/arm/acpi/domain_build.c                 | 2 +-
 xen/arch/arm/alternative.c                       | 4 ++--
 xen/arch/arm/cpuerrata.c                         | 8 ++++----
 xen/arch/arm/cpufeature.c                        | 4 ++--
 xen/arch/arm/include/asm/cpufeature.h            | 2 +-
 xen/arch/x86/cpu/microcode/core.c                | 4 ++--
 xen/common/cpu.c                                 | 4 ++--
 xen/common/stop_machine.c                        | 7 ++++---
 xen/common/version.c                             | 4 ++--
 xen/include/xen/elf.h                            | 4 ++++
 xen/include/xen/stop_machine.h                   | 5 +++--
 14 files changed, 43 insertions(+), 22 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/4 xen: make stop_machine_run() take const void *

MISRA C Rule 11.8 forbids casts that remove the const qualifier from the pointed-to type. On Arm, enable_cpu_capabilities() and enable_nonboot_cpu_caps() cast a const struct arm_cpu_capabilities pointer to void * so that it can be passed to the capability enable callback, either directly or through stop_machine_run().

None of the stop_machine_run() callbacks modify the data they are given. Make stop_machine_run() and its callback type take const void *, and update the callbacks to match:

- the Arm capability enable hook and its implementations (enable_smccc_arch_workaround_1(), enable_spectre_bhb_workaround(), enable_bp_inv_hardening() and enable_ic_inv_hardening()),
- __apply_alternatives_multi_stop() on Arm,
- do_microcode_update() on x86,
- take_cpu_down() in common code.

This allows the casts in cpufeature.c to be dropped.

take_cpu_down() is always invoked with NULL, so it now passes NULL to _take_cpu_down() explicitly. _take_cpu_down() keeps its void * argument because it is also used as an on_selected_cpus() callback.

No functional changes.

---
 xen/arch/arm/alternative.c            | 4 ++--
 xen/arch/arm/cpuerrata.c              | 8 ++++----
 xen/arch/arm/cpufeature.c             | 4 ++--
 xen/arch/arm/include/asm/cpufeature.h | 2 +-
 xen/arch/x86/cpu/microcode/core.c     | 4 ++--
 xen/common/cpu.c                      | 4 ++--
 xen/common/stop_machine.c             | 7 ++++---
 xen/include/xen/stop_machine.h        | 5 +++--
 8 files changed, 20 insertions(+), 18 deletions(-)
xen/arch/arm/alternative.c +2 -2
index 44b5e09732..e17b5d6e8a 100644
@@ -160,7 +160,7 @@ static int __apply_alternatives(const struct alt_region *region,
  * We might be patching the stop_machine state machine, so implement a
  * really simple polling protocol here.
  */
-static int __apply_alternatives_multi_stop(void *xenmap)
+static int __apply_alternatives_multi_stop(const void *xenmap)
 {
     static int patched = 0;
 
@@ -181,7 +181,7 @@ static int __apply_alternatives_multi_stop(void *xenmap)
         region.begin = __alt_instructions;
         region.end = __alt_instructions_end;
 
-        ret = __apply_alternatives(&region, xenmap - (void *)_start);
+        ret = __apply_alternatives(&region, xenmap - (const void *)_start);
         /* The patching is not expected to fail during boot. */
         BUG_ON(ret != 0);
 
xen/arch/arm/cpuerrata.c +4 -4
index 35ad98d29d..030304fbd8 100644
@@ -156,7 +156,7 @@ extern char __mitigate_spectre_bhb_loop_start_24[],
 extern char __mitigate_spectre_bhb_loop_start_32[],
             __mitigate_spectre_bhb_loop_end_32[];
 
-static int enable_smccc_arch_workaround_1(void *data)
+static int enable_smccc_arch_workaround_1(const void *data)
 {
     struct arm_smccc_res res;
     const struct arm_cpu_capabilities *entry = data;
@@ -207,7 +207,7 @@ warn:
  *   the CPU).
  * - Mitigating using the firmware.
  */
-static int enable_spectre_bhb_workaround(void *data)
+static int enable_spectre_bhb_workaround(const void *data)
 {
     const struct arm_cpu_capabilities *entry = data;
 
@@ -314,14 +314,14 @@ install_bp_hardening_vecs(const struct arm_cpu_capabilities *entry,
     this_cpu(bp_harden_vecs) = hyp_vecs;
 }
 
-static int enable_bp_inv_hardening(void *data)
+static int enable_bp_inv_hardening(const void *data)
 {
     install_bp_hardening_vecs(data, hyp_traps_vector_bp_inv,
                               "execute BPIALL");
     return 0;
 }
 
-static int enable_ic_inv_hardening(void *data)
+static int enable_ic_inv_hardening(const void *data)
 {
     install_bp_hardening_vecs(data, hyp_traps_vector_ic_inv,
                               "execute ICIALLU");
xen/arch/arm/cpufeature.c +2 -2
index 8d8cd19a96..701232fe01 100644
@@ -72,7 +72,7 @@ void __init enable_cpu_capabilities(const struct arm_cpu_capabilities *caps)
              * an IPI, giving us a PSTATE that disappears when we
              * return.
              */
-            ret = stop_machine_run(caps->enable, (void *)caps, NR_CPUS);
+            ret = stop_machine_run(caps->enable, caps, NR_CPUS);
             /* stop_machine_run should never fail at this stage of the boot. */
             BUG_ON(ret);
         }
@@ -108,7 +108,7 @@ int enable_nonboot_cpu_caps(const struct arm_cpu_capabilities *caps)
 
         if ( caps->enable )
         {
-            int ret = caps->enable((void *)caps);
+            int ret = caps->enable(caps);
 
             if ( ret )
                 rc = ret;
xen/arch/arm/include/asm/cpufeature.h +1 -1
index c554686415..53afd1ca5c 100644
@@ -123,7 +123,7 @@ struct arm_cpu_capabilities {
     const char *desc;
     u16 capability;
     bool (*matches)(const struct arm_cpu_capabilities *entry);
-    int (*enable)(void *data); /* Called on every active CPUs */
+    int (*enable)(const void *data); /* Called on every active CPUs */
     union {
         struct {    /* To be used for eratum handling only */
             u32 midr_model;
xen/arch/x86/cpu/microcode/core.c +2 -2
index 9b8d1e09cb..dd984fc923 100644
@@ -457,11 +457,11 @@ static int control_thread_fn(const struct microcode_patch *patch,
     return ret;
 }
 
-static int cf_check do_microcode_update(void *_patch_with_flags)
+static int cf_check do_microcode_update(const void *_patch_with_flags)
 {
     unsigned int cpu = smp_processor_id();
     int ret;
-    struct patch_with_flags *patch_with_flags = _patch_with_flags;
+    const struct patch_with_flags *patch_with_flags = _patch_with_flags;
 
     /*
      * The control thread set state to coordinate ucode loading. Primary
xen/common/cpu.c +2 -2
index f09af0444b..c86899b4c0 100644
@@ -101,9 +101,9 @@ static void cf_check _take_cpu_down(void *unused)
     __cpu_disable();
 }
 
-static int cf_check take_cpu_down(void *arg)
+static int cf_check take_cpu_down(const void *unused)
 {
-    _take_cpu_down(arg);
+    _take_cpu_down(NULL);
     return 0;
 }
 
xen/common/stop_machine.c +4 -3
index 398cfd507c..38091eff50 100644
@@ -46,8 +46,8 @@ struct stopmachine_data {
 
     unsigned int fn_cpu;
     int fn_result;
-    int (*fn)(void *data);
-    void *fn_data;
+    int (*fn)(const void *data);
+    const void *fn_data;
 };
 
 static DEFINE_PER_CPU(struct tasklet, stopmachine_tasklet);
@@ -73,7 +73,8 @@ static void stopmachine_wait_state(void)
  * mandatory to be called only on an idle vcpu, as otherwise active core
  * scheduling might hang.
  */
-int stop_machine_run(int (*fn)(void *data), void *data, unsigned int cpu)
+int stop_machine_run(int (*fn)(const void *data), const void *data,
+                     unsigned int cpu)
 {
     unsigned int i, nr_cpus;
     unsigned int this = smp_processor_id();
xen/include/xen/stop_machine.h +3 -2
index 0bbf71f112..905b9af0f5 100644
@@ -4,7 +4,7 @@
 /**
  * stop_machine_run: freeze the machine on all CPUs and run this function
  * @fn: the function to run
- * @data: the data ptr for the @fn()
+ * @data: read-only argument passed to @fn()
  * @cpu: the cpu to run @fn() on (or all, if @cpu == NR_CPUS).
  *
  * Description: This causes every other cpu to enter a safe point, with
@@ -14,6 +14,7 @@
  *
  * This can be thought of as a very heavy write lock, equivalent to
  * grabbing every spinlock in the kernel. */
-int stop_machine_run(int (*fn)(void *data), void *data, unsigned int cpu);
+int stop_machine_run(int (*fn)(const void *data), const void *data,
+                     unsigned int cpu);
 
 #endif /* __XEN_STOP_MACHINE_H__ */
Signature · 1 line
2.43.0
#7 lore raw

PATCH 3/4 xen: resolve MISRA C Rule 11.8 in xen_build_id_check()

In reply to Dmytro Prokopchuk1

ELFNOTE_NAME() unconditionally casts its argument to 'char *', and ELFNOTE_DESC() is built on top of it. xen_build_id_check() passes a 'const Elf_Note *' to both of them, so the const qualifier is cast away, which is a violation of MISRA C Rule 11.8.

Introduce ELFNOTE_NAME_CONST() and ELFNOTE_DESC_CONST() for read-only access to the note, and use them in xen_build_id_check().

No functional changes.

---
 xen/common/version.c  | 4 ++--
 xen/include/xen/elf.h | 4 ++++
 2 files changed, 6 insertions(+), 2 deletions(-)
xen/common/version.c +2 -2
index 80e7a9b981..587c07ab52 100644
@@ -133,13 +133,13 @@ int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
         return -EINVAL;
 
     /* Sanity check, name should be "GNU" for ld-generated build-id. */
-    if ( strncmp(ELFNOTE_NAME(n), "GNU", n->namesz) != 0 )
+    if ( strncmp(ELFNOTE_NAME_CONST(n), "GNU", n->namesz) != 0 )
         return -ENODATA;
 
     if ( len )
         *len = n->descsz;
     if ( p )
-        *p = ELFNOTE_DESC(n);
+        *p = ELFNOTE_DESC_CONST(n);
 
     return 0;
 }
xen/include/xen/elf.h +4 -0
index 4199b4d6e6..3337b8cabe 100644
@@ -34,4 +34,8 @@
 #define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + ELFNOTE_ALIGN((_n_)->namesz))
 #define ELFNOTE_NEXT(_n_) ((Elf_Note *)(ELFNOTE_DESC(_n_) + ELFNOTE_ALIGN((_n_)->descsz)))
 
+#define ELFNOTE_NAME_CONST(_n_) ((const char *)(_n_) + sizeof(*(_n_)))
+#define ELFNOTE_DESC_CONST(_n_) \
+    (ELFNOTE_NAME_CONST(_n_) + ELFNOTE_ALIGN((_n_)->namesz))
+
 #endif /* __XEN_ELF_H__ */
Signature · 1 line
2.43.0
#4 lore raw

PATCH 2/4 xen/arm: acpi: resolve MISRA C Rule 11.8 in acpi_xsdt_modify_entry()

In reply to Dmytro Prokopchuk1

ACPI_COMPARE_NAME() casts both of its arguments to 'u32 *' through ACPI_CAST_PTR(). The 'signature' argument of acpi_xsdt_modify_entry() is 'const char *', so the macro casts away its const-ness, which is a violation of MISRA C Rule 11.8.

Use strncmp() with ACPI_NAME_SIZE instead. It takes 'const char *' arguments, so no cast is needed at all. It also avoids reading a 'u32' through a possibly unaligned char pointer.

No functional changes.

---
 xen/arch/arm/acpi/domain_build.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/arm/acpi/domain_build.c +1 -1
index db16f7fa94..a77a51afd5 100644
@@ -253,7 +253,7 @@ static void __init acpi_xsdt_modify_entry(u64 entry[],
     for( i = 0; i < entry_count; i++ )
     {
         table = acpi_os_map_memory(entry[i], size);
-        if ( ACPI_COMPARE_NAME(table->signature, signature) )
+        if ( !strncmp(table->signature, signature, ACPI_NAME_SIZE) )
         {
             entry[i] = addr;
             acpi_os_unmap_memory(table, size);
Signature · 1 line
2.43.0
#10 lore raw

PATCH 4/4 automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

In reply to Dmytro Prokopchuk1

Livepatch passes const-qualified pointers from struct payload to xfree() and vfree(), which take a 'void *' argument. The cast that removes the const qualifier is a violation of MISRA C Rule 11.8.

Such casts are safe. The deallocation functions neither read nor write the content of the object being freed, and the object is not accessed after it has been freed. Deviate the rule for casts that are direct arguments of calls to xfree() and vfree().

No functional changes.

---
 automation/eclair_analysis/ECLAIR/deviations.ecl | 7 +++++++
 docs/misra/deviations.rst                        | 7 +++++++
 docs/misra/rules.rst                             | 3 ++-
 3 files changed, 16 insertions(+), 1 deletion(-)
automation/eclair_analysis/ECLAIR/deviations.ecl +7 -0
index c7a800c62f..2fce0d0f68 100644
@@ -451,6 +451,13 @@ write or not"
 -config=MC3A2.R11.8,reports+={safe,"any_area(any_loc(file(hvm_guestcopy)&&text(^.*__hvm_copy.*HVMCOPY_to_guest doesn't modify.*$)))"}
 -doc_end
 
+-doc_begin="Casting away the const qualifier from the argument of the memory
+deallocation functions xfree() and vfree() is safe. These functions neither
+read nor write the content of the object being freed, and the object is not
+accessed after it has been freed."
+-config=MC3A2.R11.8,casts+={safe, "context(skip_to(__expr_non_syntactic_or_paren_contexts, call_child(name(xfree||vfree), arg)))"}
+-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=MC3A2.R11.9,reports+={deliberate, "any_area(any_loc(any_exp(macro(^__ACCESS_ONCE$))))"
 }
docs/misra/deviations.rst +7 -0
index 76be6b3251..d8d074ae1a 100644
@@ -440,6 +440,13 @@ Deviations related to MISRA C:2012 Rules:
        readability.
      - Tagged as `safe` for ECLAIR.
 
+   * - R11.8
+     - Casting away the const qualifier from the argument of the memory
+       deallocation functions xfree() and vfree() is safe. These functions
+       neither read nor write the content of the object being freed, and the
+       object is not accessed after it has been freed.
+     - Tagged as `safe` 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
docs/misra/rules.rst +2 -1
index 5f9fe4c860..deff947c4f 100644
@@ -475,7 +475,8 @@ maintainers if you want to suggest a change.
    * - `Rule 11.8 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_08.c>`_
      - Required
      - A cast shall not remove any const or volatile qualification from the type pointed to by a pointer
-     -
+     - Casts removing the const qualifier from the argument of the memory
+       deallocation functions xfree() and vfree() are permitted.
 
    * - `Rule 11.9 <https://gitlab.com/MISRA/MISRA-C/MISRA-C-2012/Example-Suite/-/blob/master/R_11_09.c>`_
      - Required
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH 2/4] xen/arm: acpi: resolve MISRA C Rule 11.8 in acpi_xsdt_modify_entry()

In reply to Dmytro Prokopchuk1
Quoted 5 lines · On 03.11.2026 11:29, Dmytro Prokopchuk1 wrote:

ACPI_COMPARE_NAME() casts both of its arguments to 'u32 *' through ACPI_CAST_PTR(). The 'signature' argument of acpi_xsdt_modify_entry() is 'const char *', so the macro casts away its const-ness, which is a violation of MISRA C Rule 11.8.

Quoted 7 lines

Use strncmp() with ACPI_NAME_SIZE instead. It takes 'const char *' arguments, so no cast is needed at all. It also avoids reading a 'u32' through a possibly unaligned char pointer.

No functional changes.

Worth mentioning perhaps that this (apparently) is an alternative to
https://lists.xen.org/archives/html/xen-devel/2026-09/msg00044.html.

Jan

Quoted 11 lines
xen/arch/arm/acpi/domain_build.c +1 -1
@@ -253,7 +253,7 @@ static void __init acpi_xsdt_modify_entry(u64 entry[],
     for( i = 0; i < entry_count; i++ )
     {
         table = acpi_os_map_memory(entry[i], size);
-        if ( ACPI_COMPARE_NAME(table->signature, signature) )
+        if ( !strncmp(table->signature, signature, ACPI_NAME_SIZE) )
         {
             entry[i] = addr;
             acpi_os_unmap_memory(table, size);
#8 lore raw

Re: [PATCH 3/4] xen: resolve MISRA C Rule 11.8 in xen_build_id_check()

In reply to Dmytro Prokopchuk1
Quoted 11 lines · On 03.11.2026 11:29, Dmytro Prokopchuk1 wrote:

ELFNOTE_NAME() unconditionally casts its argument to 'char *', and ELFNOTE_DESC() is built on top of it. xen_build_id_check() passes a 'const Elf_Note *' to both of them, so the const qualifier is cast away, which is a violation of MISRA C Rule 11.8.

Introduce ELFNOTE_NAME_CONST() and ELFNOTE_DESC_CONST() for read-only access to the note, and use them in xen_build_id_check().

No functional changes.

It again would have been nice if it was mentioned that this is an alternative proposal to an earlier posted patch, [1]. Personally here I prefer my variant (with the adjustment as per review feedback, pending a v2 submission).

Jan

[1] https://lists.xen.org/archives/html/xen-devel/2026-09/msg00045.html

Quoted 31 lines
xen/common/version.c +2 -2
@@ -133,13 +133,13 @@ int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
         return -EINVAL;
 
     /* Sanity check, name should be "GNU" for ld-generated build-id. */
-    if ( strncmp(ELFNOTE_NAME(n), "GNU", n->namesz) != 0 )
+    if ( strncmp(ELFNOTE_NAME_CONST(n), "GNU", n->namesz) != 0 )
         return -ENODATA;
 
     if ( len )
         *len = n->descsz;
     if ( p )
-        *p = ELFNOTE_DESC(n);
+        *p = ELFNOTE_DESC_CONST(n);
 
     return 0;
 }
xen/include/xen/elf.h +4 -0
index 4199b4d6e6..3337b8cabe 100644
@@ -34,4 +34,8 @@
 #define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + ELFNOTE_ALIGN((_n_)->namesz))
 #define ELFNOTE_NEXT(_n_) ((Elf_Note *)(ELFNOTE_DESC(_n_) + ELFNOTE_ALIGN((_n_)->descsz)))
 
+#define ELFNOTE_NAME_CONST(_n_) ((const char *)(_n_) + sizeof(*(_n_)))
+#define ELFNOTE_DESC_CONST(_n_) \
+    (ELFNOTE_NAME_CONST(_n_) + ELFNOTE_ALIGN((_n_)->namesz))
+
 #endif /* __XEN_ELF_H__ */
#11 lore raw

Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

In reply to Dmytro Prokopchuk1
Quoted 3 lines · On 03.11.2026 11:29, Dmytro Prokopchuk1 wrote:

Livepatch passes const-qualified pointers from struct payload to xfree() and vfree(), which take a 'void *' argument. The cast that removes the const qualifier is a violation of MISRA C Rule 11.8.

I can only repeat my prior suggestion here: xfree(), vfree(), and other similar freeing / unmapping functions should take pointer-to-const. Andrew has been of the opposite opinion, thus being unhappy about e.g. vunmap() or unmap_domain_page() taking pointer-to-const. I think we need to come to a project-wide position towards this, and only then can we decide whether (and if so how) to deviate any of those functions.

Further, if you deviate xfree() and vfree(), why not also xvfree()?

Jan

#12 lore raw

Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

In reply to Dmytro Prokopchuk1
Quoted 7 lines · On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote:

Livepatch passes const-qualified pointers from struct payload to xfree() and vfree(), which take a 'void *' argument. The cast that removes the const qualifier is a violation of MISRA C Rule 11.8.

Such casts are safe. The deallocation functions neither read nor write the content of the object being freed, and the object is not accessed after it has been freed.

I have a very firm objection to this.

It is impossible for any allocation from malloc()/calloc()/etc to point to read-only memory, which is why free() takes a mutable pointer, not a const pointer.  The same is true of any other heap implementation.

It is impossible to make an object in read-only memory undefined. Objects in read-only memory by definition have a global lifetime, and it is absurd to argue that a specific heap implementation ought to permit making such objects undefined.

The bug here is livepatch storing a heap allocation in a const pointer.

This comes from deeper problem where some contributors/reviewers want the const keyword to mean something it doesn't.

~Andrew

#2 lore raw

Re: [PATCH 1/4] xen: make stop_machine_run() take const void *

In reply to Dmytro Prokopchuk1
Quoted 19 lines · On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote:

MISRA C Rule 11.8 forbids casts that remove the const qualifier from the pointed-to type. On Arm, enable_cpu_capabilities() and enable_nonboot_cpu_caps() cast a const struct arm_cpu_capabilities pointer to void * so that it can be passed to the capability enable callback, either directly or through stop_machine_run().

None of the stop_machine_run() callbacks modify the data they are given. Make stop_machine_run() and its callback type take const void *, and update the callbacks to match:

- the Arm capability enable hook and its implementations (enable_smccc_arch_workaround_1(), enable_spectre_bhb_workaround(), enable_bp_inv_hardening() and enable_ic_inv_hardening()),
- __apply_alternatives_multi_stop() on Arm,
- do_microcode_update() on x86,
- take_cpu_down() in common code.

This allows the casts in cpufeature.c to be dropped.

Quoted 7 lines

take_cpu_down() is always invoked with NULL, so it now passes NULL to _take_cpu_down() explicitly. _take_cpu_down() keeps its void * argument because it is also used as an on_selected_cpus() callback.

No functional changes.

I think this is a backwards step.  It is pure luck that none of the callbacks right now modify their passed pointer, but it is very intentional that some callbacks might.

Callbacks use mutable void pointers in C, and this is a known friction with the C type system.  If there existed a way of matching mutability between the callback and the pointer, we'd be using that, but it doesn't exist in C.

The safety here comes from the fact that an individual function doesn't modify the thing it was passed a pointer to.  I think those want local deviations.

~Andrew

#3 lore raw

Re: [PATCH 1/4] xen: make stop_machine_run() take const void *

Quoted 37 lines · Andrew Cooper
Quoted 19 lines · On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote:

MISRA C Rule 11.8 forbids casts that remove the const qualifier from the pointed-to type. On Arm, enable_cpu_capabilities() and enable_nonboot_cpu_caps() cast a const struct arm_cpu_capabilities pointer to void * so that it can be passed to the capability enable callback, either directly or through stop_machine_run().

None of the stop_machine_run() callbacks modify the data they are given. Make stop_machine_run() and its callback type take const void *, and update the callbacks to match:

- the Arm capability enable hook and its implementations (enable_smccc_arch_workaround_1(), enable_spectre_bhb_workaround(), enable_bp_inv_hardening() and enable_ic_inv_hardening()),
- __apply_alternatives_multi_stop() on Arm,
- do_microcode_update() on x86,
- take_cpu_down() in common code.

This allows the casts in cpufeature.c to be dropped.

Quoted 7 lines

take_cpu_down() is always invoked with NULL, so it now passes NULL to _take_cpu_down() explicitly. _take_cpu_down() keeps its void * argument because it is also used as an on_selected_cpus() callback.

No functional changes.

I think this is a backwards step.  It is pure luck that none of the callbacks right now modify their passed pointer, but it is very intentional that some callbacks might.

Callbacks use mutable void pointers in C, and this is a known friction with the C type system.  If there existed a way of matching mutability between the callback and the pointer, we'd be using that, but it doesn't exist in C.

Quoted 5 lines

The safety here comes from the fact that an individual function doesn't modify the thing it was passed a pointer to.  I think those want local deviations.

~Andrew

Understood, thanks. I will drop this patch. And I will think about the deviations.

BR, Dmytro.

#6 lore raw

Re: [PATCH 2/4] xen/arm: acpi: resolve MISRA C Rule 11.8 in acpi_xsdt_modify_entry()

In reply to Jan Beulich
Quoted 10 lines · Jan Beulich
Quoted 9 lines · On 03.11.2026 11:29, Dmytro Prokopchuk1 wrote:

ACPI_COMPARE_NAME() casts both of its arguments to 'u32 *' through ACPI_CAST_PTR(). The 'signature' argument of acpi_xsdt_modify_entry() is 'const char *', so the macro casts away its const-ness, which is a violation of MISRA C Rule 11.8.

Use strncmp() with ACPI_NAME_SIZE instead. It takes 'const char *' arguments, so no cast is needed at all. It also avoids reading a 'u32' through a possibly unaligned char pointer.

Quoted 7 lines
Quoted 3 lines

No functional changes.

Worth mentioning perhaps that this (apparently) is an alternative to
https://lists.xen.org/archives/html/xen-devel/2026-09/msg00044.html.

Thanks for pointing that out. I should have referenced your patch.

Quoted 14 lines

Jan

xen/arch/arm/acpi/domain_build.c static void __init acpi_xsdt_modify_entry(u64 entry[], · line 259

Quoted 11 lines
xen/arch/arm/acpi/domain_build.c +1 -1
@@ -253,7 +253,7 @@ static void __init acpi_xsdt_modify_entry(u64 entry[],
      for( i = 0; i < entry_count; i++ )
      {
          table = acpi_os_map_memory(entry[i], size);
-        if ( ACPI_COMPARE_NAME(table->signature, signature) )
+        if ( !strncmp(table->signature, signature, ACPI_NAME_SIZE) )
          {
              entry[i] = addr;
              acpi_os_unmap_memory(table, size);

Dmytro.

#9 lore raw

Re: [PATCH 3/4] xen: resolve MISRA C Rule 11.8 in xen_build_id_check()

In reply to Jan Beulich
Quoted 13 lines · Jan Beulich
Quoted 11 lines · On 03.11.2026 11:29, Dmytro Prokopchuk1 wrote:

ELFNOTE_NAME() unconditionally casts its argument to 'char *', and ELFNOTE_DESC() is built on top of it. xen_build_id_check() passes a 'const Elf_Note *' to both of them, so the const qualifier is cast away, which is a violation of MISRA C Rule 11.8.

Introduce ELFNOTE_NAME_CONST() and ELFNOTE_DESC_CONST() for read-only access to the note, and use them in xen_build_id_check().

No functional changes.

Quoted 8 lines

It again would have been nice if it was mentioned that this is an alternative proposal to an earlier posted patch, [1]. Personally here I prefer my variant (with the adjustment as per review feedback, pending a v2 submission).

Jan

[1] https://lists.xen.org/archives/html/xen-devel/2026-09/msg00045.html

Sorry for not referencing [1].
I will drop this patch and wait for your v2.

Quoted 26 lines
Quoted 26 lines
xen/common/version.c +2 -2
@@ -133,13 +133,13 @@ int xen_build_id_check(const Elf_Note *n, unsigned int n_sz,
          return -EINVAL;
 
      /* Sanity check, name should be "GNU" for ld-generated build-id. */
-    if ( strncmp(ELFNOTE_NAME(n), "GNU", n->namesz) != 0 )
+    if ( strncmp(ELFNOTE_NAME_CONST(n), "GNU", n->namesz) != 0 )
          return -ENODATA;
 
      if ( len )
          *len = n->descsz;
      if ( p )
-        *p = ELFNOTE_DESC(n);
+        *p = ELFNOTE_DESC_CONST(n);
 
      return 0;
  }
xen/include/xen/elf.h +0 -0
index 4199b4d6e6..3337b8cabe 100644
@@ -34,4 +34,8 @@
  #define ELFNOTE_DESC(_n_) (ELFNOTE_NAME(_n_) + ELFNOTE_ALIGN((_n_)->namesz))
  #define ELFNOTE_NEXT(_n_) ((Elf_Note *)(ELFNOTE_DESC(_n_) + ELFNOTE_ALIGN((_n_)->descsz)))
 
Quoted 6 lines

xen/include/xen/elf.h line 41

Quoted 5 lines
xen/include/xen/elf.h +4 -0
+#define ELFNOTE_NAME_CONST(_n_) ((const char *)(_n_) + sizeof(*(_n_)))
+#define ELFNOTE_DESC_CONST(_n_) \
+    (ELFNOTE_NAME_CONST(_n_) + ELFNOTE_ALIGN((_n_)->namesz))
+
  #endif /* __XEN_ELF_H__ */

BR, Dmytro.

#13 lore raw

Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

In reply to Andrew Cooper
Quoted 21 lines · Andrew Cooper
Quoted 7 lines · On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote:

Livepatch passes const-qualified pointers from struct payload to xfree() and vfree(), which take a 'void *' argument. The cast that removes the const qualifier is a violation of MISRA C Rule 11.8.

Such casts are safe. The deallocation functions neither read nor write the content of the object being freed, and the object is not accessed after it has been freed.

I have a very firm objection to this.

It is impossible for any allocation from malloc()/calloc()/etc to point to read-only memory, which is why free() takes a mutable pointer, not a const pointer.  The same is true of any other heap implementation.

It is impossible to make an object in read-only memory undefined. Objects in read-only memory by definition have a global lifetime, and it is absurd to argue that a specific heap implementation ought to permit making such objects undefined.

Quoted 6 lines

The bug here is livepatch storing a heap allocation in a const pointer.

This comes from deeper problem where some contributors/reviewers want the const keyword to mean something it doesn't.

~Andrew

Jan, Andrew,

thanks for the review. Given the disagreement, I will drop this deviation for now.

If the preferred fix is to stop storing heap allocations in
const-qualified members of 'struct payload' (so the casts go away), I
can prepare a follow-up patch. Please confirm that is the direction you want for livepatch.

BR, Dmytro.

#14 lore raw

Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

Quoted 34 lines · Dmytro Prokopchuk
Quoted 22 lines · Andrew Cooper
Quoted 8 lines · On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote:

Livepatch passes const-qualified pointers from struct payload to xfree()
and vfree(), which take a 'void *' argument. The cast that removes the const qualifier is a violation of MISRA C Rule 11.8.

Such casts are safe.  The deallocation functions neither read nor write the content of the object being freed, and the object is not accessed after it has been freed.

I have a very firm objection to this.

It is impossible for any allocation from malloc()/calloc()/etc to point to read-only memory, which is why free() takes a mutable pointer, not a const pointer.  The same is true of any other heap implementation.

It is impossible to make an object in read-only memory undefined. Objects in read-only memory by definition have a global lifetime, and it is absurd to argue that a specific heap implementation ought to permit making such objects undefined.

Quoted 6 lines

The bug here is livepatch storing a heap allocation in a const pointer.

This comes from deeper problem where some contributors/reviewers want the const keyword to mean something it doesn't.

~Andrew

Jan, Andrew,

Quoted 7 lines

thanks for the review.  Given the disagreement, I will drop this deviation for now.

If the preferred fix is to stop storing heap allocations in const-qualified members of 'struct payload' (so the casts go away), I can prepare a follow-up patch.  Please confirm that is the direction you want for livepatch.

Please remove the const qualification in struct payload.  The logic should not have been accepted in that state to begin with.

This is the only course of action here which does not create a class of undefined behaviour, or permit said undefined behaviour to go unnoticed.

~Andrew

#15 lore raw

Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

Quoted 43 lines · Andrew Cooper
Quoted 34 lines · Dmytro Prokopchuk
Quoted 22 lines · Andrew Cooper
Quoted 8 lines · On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote:

Livepatch passes const-qualified pointers from struct payload to xfree()
and vfree(), which take a 'void *' argument. The cast that removes the const qualifier is a violation of MISRA C Rule 11.8.

Such casts are safe.  The deallocation functions neither read nor write the content of the object being freed, and the object is not accessed after it has been freed.

I have a very firm objection to this.

It is impossible for any allocation from malloc()/calloc()/etc to point to read-only memory, which is why free() takes a mutable pointer, not a const pointer.  The same is true of any other heap implementation.

It is impossible to make an object in read-only memory undefined. Objects in read-only memory by definition have a global lifetime, and it is absurd to argue that a specific heap implementation ought to permit making such objects undefined.

Quoted 6 lines

The bug here is livepatch storing a heap allocation in a const pointer.

This comes from deeper problem where some contributors/reviewers want the const keyword to mean something it doesn't.

~Andrew

Jan, Andrew,

Quoted 7 lines

thanks for the review.  Given the disagreement, I will drop this deviation for now.

If the preferred fix is to stop storing heap allocations in const-qualified members of 'struct payload' (so the casts go away), I can prepare a follow-up patch.  Please confirm that is the direction you want for livepatch.

Quoted 5 lines

Please remove the const qualification in struct payload.  The logic should not have been accepted in that state to begin with.

This is the only course of action here which does not create a class of undefined behaviour, or permit said undefined behaviour to go unnoticed.

I agree with Andrew. xfree() does write into the freed block, so a const parameter would be misleading.

The real issue is livepatch keeping owned heap allocations in const pointers. text_addr, symtab and strtab all come from non-const allocations, so dropping const from those three fields of struct payload removes all six casts, with no deviation needed.

If the issue were more widespread I would also consider a deviation but with wording like:

Casting away the const qualifier from the argument of the memory deallocation functions xfree() and vfree() is safe. The pointer always refers to memory obtained from the corresponding allocator, which is never an object defined as const, and the object's lifetime ends with the call, so no access through a const-qualified lvalue can follow.

#16 lore raw

Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

Quoted 42 lines · Stefano Stabellini
Quoted 41 lines · Andrew Cooper
Quoted 39 lines · Dmytro Prokopchuk
Quoted 22 lines · Andrew Cooper
Quoted 8 lines · On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote:

Livepatch passes const-qualified pointers from struct payload to xfree()
and vfree(), which take a 'void *' argument. The cast that removes the const qualifier is a violation of MISRA C Rule 11.8.

Such casts are safe.  The deallocation functions neither read nor write the content of the object being freed, and the object is not accessed after it has been freed.

I have a very firm objection to this.

It is impossible for any allocation from malloc()/calloc()/etc to point to read-only memory, which is why free() takes a mutable pointer, not a const pointer.  The same is true of any other heap implementation.

It is impossible to make an object in read-only memory undefined. Objects in read-only memory by definition have a global lifetime, and it is absurd to argue that a specific heap implementation ought to permit making such objects undefined.

Quoted 6 lines

The bug here is livepatch storing a heap allocation in a const pointer.

This comes from deeper problem where some contributors/reviewers want the const keyword to mean something it doesn't.

~Andrew

Jan, Andrew,

thanks for the review.  Given the disagreement, I will drop this deviation for now.

If the preferred fix is to stop storing heap allocations in const-qualified members of 'struct payload' (so the casts go away), I can prepare a follow-up patch.  Please confirm that is the direction you want for livepatch.

Quoted 8 lines
Quoted 5 lines

Please remove the const qualification in struct payload.  The logic should not have been accepted in that state to begin with.

This is the only course of action here which does not create a class of undefined behaviour, or permit said undefined behaviour to go unnoticed.

I agree with Andrew. xfree() does write into the freed block, so a const parameter would be misleading.

Well, this is the point: Yes, xfree() modifies the underlying memory. But no, it does not modify the object a pointer to which was passed into it. That object simply ceases to exist. And what C is concerned about are objects, not the underlying memory.

I also still don't quite understand why Andrew considers such uses of const wrong: I don't think the spec says anything in this regard, and the C library's free() taking plain void * can easily be seen as heritage. Using const like this properly conveys to compiler and readers that post- allocation / -population such objects aren't supposed to be altered again. Together with forbidding the casting away of const-ness, this serves as an extra level of safety of the code overall.

Jan

#17 lore raw

Re: [PATCH 4/4] automation/eclair: deviate MISRA C Rule 11.8 for xfree() and vfree()

Quoted 57 lines · Jan Beulich
Quoted 42 lines · Stefano Stabellini
Quoted 41 lines · Andrew Cooper
Quoted 39 lines · Dmytro Prokopchuk
Quoted 22 lines · Andrew Cooper
Quoted 8 lines · On 03/11/2026 10:29 am, Dmytro Prokopchuk1 wrote:

Livepatch passes const-qualified pointers from struct payload to xfree()
and vfree(), which take a 'void *' argument. The cast that removes the const qualifier is a violation of MISRA C Rule 11.8.

Such casts are safe.  The deallocation functions neither read nor write the content of the object being freed, and the object is not accessed after it has been freed.

I have a very firm objection to this.

It is impossible for any allocation from malloc()/calloc()/etc to point to read-only memory, which is why free() takes a mutable pointer, not a const pointer.  The same is true of any other heap implementation.

It is impossible to make an object in read-only memory undefined. Objects in read-only memory by definition have a global lifetime, and it is absurd to argue that a specific heap implementation ought to permit making such objects undefined.

Quoted 6 lines

The bug here is livepatch storing a heap allocation in a const pointer.

This comes from deeper problem where some contributors/reviewers want the const keyword to mean something it doesn't.

~Andrew

Jan, Andrew,

thanks for the review.  Given the disagreement, I will drop this deviation for now.

If the preferred fix is to stop storing heap allocations in const-qualified members of 'struct payload' (so the casts go away), I can prepare a follow-up patch.  Please confirm that is the direction you want for livepatch.

Quoted 8 lines
Quoted 5 lines

Please remove the const qualification in struct payload.  The logic should not have been accepted in that state to begin with.

This is the only course of action here which does not create a class of undefined behaviour, or permit said undefined behaviour to go unnoticed.

I agree with Andrew. xfree() does write into the freed block, so a const parameter would be misleading.

Well, this is the point: Yes, xfree() modifies the underlying memory. But no, it does not modify the object a pointer to which was passed into it. That object simply ceases to exist. And what C is concerned about are objects, not the underlying memory.

Quoted 7 lines

I also still don't quite understand why Andrew considers such uses of const wrong: I don't think the spec says anything in this regard, and the C library's free() taking plain void * can easily be seen as heritage. Using const like this properly conveys to compiler and readers that post- allocation / -population such objects aren't supposed to be altered again. Together with forbidding the casting away of const-ness, this serves as an extra level of safety of the code overall.

From a theoretical perspective I don't disagree with you, but there are two problems.

First, in this specific case the const in struct payload is wrong regardless of the xfree() question: these fields are not read-only. rw_addr and text_addr are owning pointers to mutable buffers.

Second, I would agree with you if free/unmap functions could take a pointer-to-const without casting const away internally. That is the case for vunmap() and unmap_domain_page(), which only use the pointer as an address, and it would be the case for vfree(), since _vfree() already takes const void *. It is not the case for xfree(). A const parameter would just move the Rule 11.8 cast from the callers into the allocator. This is not a good idea.

So I think we should remove the const from those livepatch fields, that removes all six casts without any deviation. I don't mind adding const to vfree(), xvfree(), or other functions where it doesn't require casting const away in the implementation, but I don't think xfree() should be one of them.