All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros
@ 2026-12-04  8:44 Dmytro Prokopchuk1
  2026-12-04  9:24 ` Nicola Vetrini
  0 siblings, 1 reply; 5+ messages in thread
From: Dmytro Prokopchuk1 @ 2026-12-04  8:44 UTC (permalink / raw)
  To: xen-devel@lists.xenproject.org
  Cc: Dmytro Prokopchuk1, Nicola Vetrini, Doug Goldstein,
	Stefano Stabellini, Andrew Cooper, Anthony PERARD, Michal Orzel,
	Jan Beulich, Julien Grall, Roger Pau Monné

MISRA C Rule 18.3 states: "The relational operators >, >=, < and <= shall
not be applied to objects of pointer type except where they point into
the same object".

The is_kernel(), is_kernel_text(), is_kernel_rodata() and
is_kernel_inittext() macros check whether an arbitrary address lies
within a section of the Xen image by comparing it against linker-defined
section boundaries. A pointer outside the section is a legitimate input
for them and simply yields false, hence such comparisons are safe.

Deviate the rule for comparisons encapsulated by these macros.

Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>
---
Changes in v2:
- restrict the deviation to the is_kernel*() macros instead of covering
  comparisons against all linker symbols
- update the commit subject, message and deviations.rst accordingly

With v2 applied the remaining R18.3 violations (ARM64 and x86_64) are all
unrelated to linker symbols.

Test CI pipeline:
https://gitlab.com/xen-project/people/dimaprkp4k/xen/-/pipelines/3152444935
---
 automation/eclair_analysis/ECLAIR/deviations.ecl | 5 +++++
 docs/misra/deviations.rst                        | 7 +++++++
 2 files changed, 12 insertions(+)

diff --git a/automation/eclair_analysis/ECLAIR/deviations.ecl b/automation/eclair_analysis/ECLAIR/deviations.ecl
index cdcdb4622c..b7a7fb2a52 100644
--- a/automation/eclair_analysis/ECLAIR/deviations.ecl
+++ b/automation/eclair_analysis/ECLAIR/deviations.ecl
@@ -630,6 +630,11 @@ C99 Undefined Behaviour 45: Pointers that do not point into, or just beyond, the
 -config=MC3A2.R18.2,reports+={safe, "any_area(any_loc(any_exp(macro(^page_to_mfn$))))"}
 -doc_end
 
+-doc_begin="The following macros check whether an arbitrary address lies within a section of the Xen image by comparing it
+against linker-defined section boundaries. A pointer outside the section is a legitimate input and simply yields false."
+-config=MC3A2.R18.3,reports+={safe, "any_area(any_loc(any_exp(macro(^is_kernel(_text|_rodata|_inittext)?$))))"}
+-doc_end
+
 -doc_begin="Flexible array members are deliberately used and XEN developers are aware of the dangers related to them:
 unexpected result when the structure is given as argument to a sizeof() operator and the truncation in assignment between structures."
 -config=MC3A2.R18.7,reports+={deliberate, "any()"}
diff --git a/docs/misra/deviations.rst b/docs/misra/deviations.rst
index eb5817f88b..df77f8c3cd 100644
--- a/docs/misra/deviations.rst
+++ b/docs/misra/deviations.rst
@@ -644,6 +644,13 @@ Deviations related to MISRA C:2012 Rules:
        are safe.
      - Tagged as `safe` for ECLAIR.
 
+   * - R18.3
+     - Relational comparisons between pointers encapsulated by macros
+       is_kernel(), is_kernel_text(), is_kernel_rodata(), is_kernel_inittext()
+       are safe, as they check whether an arbitrary address lies within a
+       section of the Xen image.
+     - Tagged as `safe` for ECLAIR.
+
    * - R20.4
      - The override of the keyword \"inline\" in xen/compiler.h is present so
        that section contents checks pass when the compiler chooses not to
-- 
2.43.0

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros
  2026-12-04  8:44 [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros Dmytro Prokopchuk1
@ 2026-12-04  9:24 ` Nicola Vetrini
  2026-12-04 11:15   ` Dmytro Prokopchuk
  0 siblings, 1 reply; 5+ messages in thread
From: Nicola Vetrini @ 2026-12-04  9:24 UTC (permalink / raw)
  To: Dmytro Prokopchuk1
  Cc: xen-devel, Doug Goldstein, Stefano Stabellini, Andrew Cooper,
	Anthony PERARD, Michal Orzel, Jan Beulich, Julien Grall,
	Roger Pau Monné

On 2026-12-04 09:44, Dmytro Prokopchuk1 wrote:
> MISRA C Rule 18.3 states: "The relational operators >, >=, < and <= 
> shall
> not be applied to objects of pointer type except where they point into
> the same object".
> 
> The is_kernel(), is_kernel_text(), is_kernel_rodata() and
> is_kernel_inittext() macros check whether an arbitrary address lies
> within a section of the Xen image by comparing it against 
> linker-defined
> section boundaries. A pointer outside the section is a legitimate input
> for them and simply yields false, hence such comparisons are safe.
> 

Why would this matter? The standard only guarantees defined behavior if 
you compare pointers inside the same array, no matter that the compiler 
doesn't take advantage of it (probably due to the cast on p). To me, it 
looks like a more reasonable approach would be to do the comparison in 
unsigned long and then rely on the assumption that no information is 
lost in the char* -> unsigned long conversion.

e.g.

extern char _stext[], _etext[];
#define is_kernel_text(p) ({                    \
     unsigned long __p = (unsigned long)(p);     \
     (__p >= (unsigned long)_stext) && (__p < (unsigned long)_etext);     
      \
})

in this way you avoid the violation at the root.

> Deviate the rule for comparisons encapsulated by these macros.
> 
> Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>
> ---
> Changes in v2:
> - restrict the deviation to the is_kernel*() macros instead of covering
>   comparisons against all linker symbols
> - update the commit subject, message and deviations.rst accordingly
> 
> With v2 applied the remaining R18.3 violations (ARM64 and x86_64) are 
> all
> unrelated to linker symbols.
> 
> Test CI pipeline:
> https://gitlab.com/xen-project/people/dimaprkp4k/xen/-/pipelines/3152444935
> ---
>  automation/eclair_analysis/ECLAIR/deviations.ecl | 5 +++++
>  docs/misra/deviations.rst                        | 7 +++++++
>  2 files changed, 12 insertions(+)
> 
> diff --git a/automation/eclair_analysis/ECLAIR/deviations.ecl 
> b/automation/eclair_analysis/ECLAIR/deviations.ecl
> index cdcdb4622c..b7a7fb2a52 100644
> --- a/automation/eclair_analysis/ECLAIR/deviations.ecl
> +++ b/automation/eclair_analysis/ECLAIR/deviations.ecl
> @@ -630,6 +630,11 @@ C99 Undefined Behaviour 45: Pointers that do not 
> point into, or just beyond, the
>  -config=MC3A2.R18.2,reports+={safe, 
> "any_area(any_loc(any_exp(macro(^page_to_mfn$))))"}
>  -doc_end
> 
> +-doc_begin="The following macros check whether an arbitrary address 
> lies within a section of the Xen image by comparing it
> +against linker-defined section boundaries. A pointer outside the 
> section is a legitimate input and simply yields false."
> +-config=MC3A2.R18.3,reports+={safe, 
> "any_area(any_loc(any_exp(macro(^is_kernel(_text|_rodata|_inittext)?$))))"}
> +-doc_end
> +
>  -doc_begin="Flexible array members are deliberately used and XEN 
> developers are aware of the dangers related to them:
>  unexpected result when the structure is given as argument to a 
> sizeof() operator and the truncation in assignment between structures."
>  -config=MC3A2.R18.7,reports+={deliberate, "any()"}
> diff --git a/docs/misra/deviations.rst b/docs/misra/deviations.rst
> index eb5817f88b..df77f8c3cd 100644
> --- a/docs/misra/deviations.rst
> +++ b/docs/misra/deviations.rst
> @@ -644,6 +644,13 @@ Deviations related to MISRA C:2012 Rules:
>         are safe.
>       - Tagged as `safe` for ECLAIR.
> 
> +   * - R18.3
> +     - Relational comparisons between pointers encapsulated by macros
> +       is_kernel(), is_kernel_text(), is_kernel_rodata(), 
> is_kernel_inittext()
> +       are safe, as they check whether an arbitrary address lies 
> within a
> +       section of the Xen image.
> +     - Tagged as `safe` for ECLAIR.
> +
>     * - R20.4
>       - The override of the keyword \"inline\" in xen/compiler.h is 
> present so
>         that section contents checks pass when the compiler chooses not 
> to

-- 
Nicola Vetrini, B.Sc.
Software Engineer
BUGSENG (https://bugseng.com)
LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros
  2026-12-04  9:24 ` Nicola Vetrini
@ 2026-12-04 11:15   ` Dmytro Prokopchuk
  2026-12-04 11:17     ` Jan Beulich
  0 siblings, 1 reply; 5+ messages in thread
From: Dmytro Prokopchuk @ 2026-12-04 11:15 UTC (permalink / raw)
  To: Nicola Vetrini
  Cc: xen-devel, Doug Goldstein, Stefano Stabellini, Andrew Cooper,
	Anthony PERARD, Michal Orzel, Jan Beulich, Julien Grall,
	Roger Pau Monné



On 12/4/26 11:24, Nicola Vetrini wrote:
> On 2026-12-04 09:44, Dmytro Prokopchuk1 wrote:
>> MISRA C Rule 18.3 states: "The relational operators >, >=, < and <= shall
>> not be applied to objects of pointer type except where they point into
>> the same object".
>>
>> The is_kernel(), is_kernel_text(), is_kernel_rodata() and
>> is_kernel_inittext() macros check whether an arbitrary address lies
>> within a section of the Xen image by comparing it against linker-defined
>> section boundaries. A pointer outside the section is a legitimate input
>> for them and simply yields false, hence such comparisons are safe.
>>
> 
> Why would this matter? The standard only guarantees defined behavior if 
> you compare pointers inside the same array, no matter that the compiler 
> doesn't take advantage of it (probably due to the cast on p). To me, it 
> looks like a more reasonable approach would be to do the comparison in 
> unsigned long and then rely on the assumption that no information is 
> lost in the char* -> unsigned long conversion.

Looks like Xen has this deviation established already:

-doc_begin="Conversions from and to integral types are safe, in the 
assumption that the target type has enough bits to store the value.
See also Section \"4.7 Arrays and Pointers\" of \"GCC_MANUAL\""
-config=MC3A2.R11.6,casts+={safe, ...

> 
> e.g.
> 
> extern char _stext[], _etext[];
> #define is_kernel_text(p) ({                    \
>      unsigned long __p = (unsigned long)(p);     \
>      (__p >= (unsigned long)_stext) && (__p < (unsigned long)_etext); 
>       \
> })
> 
> in this way you avoid the violation at the root.

And we can update macros without new violations.
And all four macros should be updated: is_kernel, is_kernel_text, 
is_kernel_rodata, is_kernel_inittext, to be aligned.

Well, let's wait someone else review.

BR, Dmytro.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros
  2026-12-04 11:15   ` Dmytro Prokopchuk
@ 2026-12-04 11:17     ` Jan Beulich
  2026-12-04 11:40       ` Dmytro Prokopchuk
  0 siblings, 1 reply; 5+ messages in thread
From: Jan Beulich @ 2026-12-04 11:17 UTC (permalink / raw)
  To: Dmytro Prokopchuk
  Cc: xen-devel, Doug Goldstein, Stefano Stabellini, Andrew Cooper,
	Anthony PERARD, Michal Orzel, Julien Grall, Roger Pau Monné,
	Nicola Vetrini

On 04.12.2026 12:15, Dmytro Prokopchuk wrote:
> 
> 
> On 12/4/26 11:24, Nicola Vetrini wrote:
>> On 2026-12-04 09:44, Dmytro Prokopchuk1 wrote:
>>> MISRA C Rule 18.3 states: "The relational operators >, >=, < and <= shall
>>> not be applied to objects of pointer type except where they point into
>>> the same object".
>>>
>>> The is_kernel(), is_kernel_text(), is_kernel_rodata() and
>>> is_kernel_inittext() macros check whether an arbitrary address lies
>>> within a section of the Xen image by comparing it against linker-defined
>>> section boundaries. A pointer outside the section is a legitimate input
>>> for them and simply yields false, hence such comparisons are safe.
>>>
>>
>> Why would this matter? The standard only guarantees defined behavior if 
>> you compare pointers inside the same array, no matter that the compiler 
>> doesn't take advantage of it (probably due to the cast on p). To me, it 
>> looks like a more reasonable approach would be to do the comparison in 
>> unsigned long and then rely on the assumption that no information is 
>> lost in the char* -> unsigned long conversion.
> 
> Looks like Xen has this deviation established already:
> 
> -doc_begin="Conversions from and to integral types are safe, in the 
> assumption that the target type has enough bits to store the value.
> See also Section \"4.7 Arrays and Pointers\" of \"GCC_MANUAL\""
> -config=MC3A2.R11.6,casts+={safe, ...
> 
>>
>> e.g.
>>
>> extern char _stext[], _etext[];
>> #define is_kernel_text(p) ({                    \
>>      unsigned long __p = (unsigned long)(p);     \
>>      (__p >= (unsigned long)_stext) && (__p < (unsigned long)_etext); 
>>       \
>> })
>>
>> in this way you avoid the violation at the root.
> 
> And we can update macros without new violations.
> And all four macros should be updated: is_kernel, is_kernel_text, 
> is_kernel_rodata, is_kernel_inittext, to be aligned.
> 
> Well, let's wait someone else review.

I certainly agree with Nicola: Why add a deviation when we can easily get
away without.

Jan


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros
  2026-12-04 11:17     ` Jan Beulich
@ 2026-12-04 11:40       ` Dmytro Prokopchuk
  0 siblings, 0 replies; 5+ messages in thread
From: Dmytro Prokopchuk @ 2026-12-04 11:40 UTC (permalink / raw)
  To: Jan Beulich
  Cc: xen-devel, Doug Goldstein, Stefano Stabellini, Andrew Cooper,
	Anthony PERARD, Michal Orzel, Julien Grall, Roger Pau Monné,
	Nicola Vetrini



On 12/4/26 13:17, Jan Beulich wrote:
> On 04.12.2026 12:15, Dmytro Prokopchuk wrote:
>>
>>
>> On 12/4/26 11:24, Nicola Vetrini wrote:
>>> On 2026-12-04 09:44, Dmytro Prokopchuk1 wrote:
>>>> MISRA C Rule 18.3 states: "The relational operators >, >=, < and <= shall
>>>> not be applied to objects of pointer type except where they point into
>>>> the same object".
>>>>
>>>> The is_kernel(), is_kernel_text(), is_kernel_rodata() and
>>>> is_kernel_inittext() macros check whether an arbitrary address lies
>>>> within a section of the Xen image by comparing it against linker-defined
>>>> section boundaries. A pointer outside the section is a legitimate input
>>>> for them and simply yields false, hence such comparisons are safe.
>>>>
>>>
>>> Why would this matter? The standard only guarantees defined behavior if
>>> you compare pointers inside the same array, no matter that the compiler
>>> doesn't take advantage of it (probably due to the cast on p). To me, it
>>> looks like a more reasonable approach would be to do the comparison in
>>> unsigned long and then rely on the assumption that no information is
>>> lost in the char* -> unsigned long conversion.
>>
>> Looks like Xen has this deviation established already:
>>
>> -doc_begin="Conversions from and to integral types are safe, in the
>> assumption that the target type has enough bits to store the value.
>> See also Section \"4.7 Arrays and Pointers\" of \"GCC_MANUAL\""
>> -config=MC3A2.R11.6,casts+={safe, ...
>>
>>>
>>> e.g.
>>>
>>> extern char _stext[], _etext[];
>>> #define is_kernel_text(p) ({                    \
>>>       unsigned long __p = (unsigned long)(p);     \
>>>       (__p >= (unsigned long)_stext) && (__p < (unsigned long)_etext);
>>>        \
>>> })
>>>
>>> in this way you avoid the violation at the root.
>>
>> And we can update macros without new violations.
>> And all four macros should be updated: is_kernel, is_kernel_text,
>> is_kernel_rodata, is_kernel_inittext, to be aligned.
>>
>> Well, let's wait someone else review.
> 
> I certainly agree with Nicola: Why add a deviation when we can easily get
> away without.

I agree. ( actually BUGSENG youtube videos say the same :) )
So, I will prepare V3.

> 
> Jan



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-04 11:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04  8:44 [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros Dmytro Prokopchuk1
2026-12-04  9:24 ` Nicola Vetrini
2026-12-04 11:15   ` Dmytro Prokopchuk
2026-12-04 11:17     ` Jan Beulich
2026-12-04 11:40       ` Dmytro Prokopchuk

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.