|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: [PATCH v2] misra: deviate MISRA C Rule 18.3 for is_kernel*() macros
On 2026-12-04 09:44, Dmytro Prokopchuk1 wrote: MISRA C Rule 18.3 states: "The relational operators >, >=, < and <= shallnot 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 lieswithin a section of the Xen image by comparing it against linker-definedsection 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@xxxxxxxx> --- 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 accordinglyWith v2 applied the remaining R18.3 violations (ARM64 and x86_64) are allunrelated 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.eclindex 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." -- Nicola Vetrini, B.Sc. Software Engineer BUGSENG (https://bugseng.com) LinkedIn: https://www.linkedin.com/in/nicola-vetrini-a42471253
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |